home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / ixemul-45.0-src.tgz / tar.out / contrib / ixemul / db / recno / rec_close.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  5KB  |  170 lines

  1. /*    $NetBSD: rec_close.c,v 1.6 1995/02/27 13:24:37 cgd Exp $    */
  2.  
  3. /*-
  4.  * Copyright (c) 1990, 1993
  5.  *    The Regents of the University of California.  All rights reserved.
  6.  *
  7.  * Redistribution and use in source and binary forms, with or without
  8.  * modification, are permitted provided that the following conditions
  9.  * are met:
  10.  * 1. Redistributions of source code must retain the above copyright
  11.  *    notice, this list of conditions and the following disclaimer.
  12.  * 2. Redistributions in binary form must reproduce the above copyright
  13.  *    notice, this list of conditions and the following disclaimer in the
  14.  *    documentation and/or other materials provided with the distribution.
  15.  * 3. All advertising materials mentioning features or use of this software
  16.  *    must display the following acknowledgement:
  17.  *    This product includes software developed by the University of
  18.  *    California, Berkeley and its contributors.
  19.  * 4. Neither the name of the University nor the names of its contributors
  20.  *    may be used to endorse or promote products derived from this software
  21.  *    without specific prior written permission.
  22.  *
  23.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  24.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  27.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  29.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  30.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  32.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  33.  * SUCH DAMAGE.
  34.  */
  35.  
  36. #if defined(LIBC_SCCS) && !defined(lint)
  37. #if 0
  38. static char sccsid[] = "@(#)rec_close.c    8.3 (Berkeley) 2/21/94";
  39. #else
  40. static char rcsid[] = "$NetBSD: rec_close.c,v 1.6 1995/02/27 13:24:37 cgd Exp $";
  41. #endif
  42. #endif /* LIBC_SCCS and not lint */
  43.  
  44. #include <sys/types.h>
  45. #include <sys/uio.h>
  46. #include <sys/mman.h>
  47.  
  48. #include <errno.h>
  49. #include <limits.h>
  50. #include <stdio.h>
  51. #include <unistd.h>
  52.  
  53. #include <db.h>
  54. #include "recno.h"
  55.  
  56. /*
  57.  * __REC_CLOSE -- Close a recno tree.
  58.  *
  59.  * Parameters:
  60.  *    dbp:    pointer to access method
  61.  *
  62.  * Returns:
  63.  *    RET_ERROR, RET_SUCCESS
  64.  */
  65. int
  66. __rec_close(dbp)
  67.     DB *dbp;
  68. {
  69.     BTREE *t;
  70.     int status;
  71.  
  72.     t = dbp->internal;
  73.  
  74.     /* Toss any page pinned across calls. */
  75.     if (t->bt_pinned != NULL) {
  76.         mpool_put(t->bt_mp, t->bt_pinned, 0);
  77.         t->bt_pinned = NULL;
  78.     }
  79.  
  80.     if (__rec_sync(dbp, 0) == RET_ERROR)
  81.         return (RET_ERROR);
  82.  
  83.     /* Committed to closing. */
  84.     status = RET_SUCCESS;
  85.     if (ISSET(t, R_MEMMAPPED) && munmap(t->bt_smap, t->bt_msize))
  86.         status = RET_ERROR;
  87.  
  88.     if (!ISSET(t, R_INMEM))
  89.         if (ISSET(t, R_CLOSEFP)) {
  90.             if (fclose(t->bt_rfp))
  91.                 status = RET_ERROR;
  92.         } else
  93.             if (close(t->bt_rfd))
  94.                 status = RET_ERROR;
  95.  
  96.     if (__bt_close(dbp) == RET_ERROR)
  97.         status = RET_ERROR;
  98.  
  99.     return (status);
  100. }
  101.  
  102. /*
  103.  * __REC_SYNC -- sync the recno tree to disk.
  104.  *
  105.  * Parameters:
  106.  *    dbp:    pointer to access method
  107.  *
  108.  * Returns:
  109.  *    RET_SUCCESS, RET_ERROR.
  110.  */
  111. int
  112. __rec_sync(dbp, flags)
  113.     const DB *dbp;
  114.     u_int flags;
  115. {
  116.     struct iovec iov[2];
  117.     BTREE *t;
  118.     DBT data, key;
  119.     off_t off;
  120.     recno_t scursor, trec;
  121.     int status;
  122.  
  123.     t = dbp->internal;
  124.  
  125.     /* Toss any page pinned across calls. */
  126.     if (t->bt_pinned != NULL) {
  127.         mpool_put(t->bt_mp, t->bt_pinned, 0);
  128.         t->bt_pinned = NULL;
  129.     }
  130.  
  131.     if (flags == R_RECNOSYNC)
  132.         return (__bt_sync(dbp, 0));
  133.  
  134.     if (ISSET(t, R_RDONLY | R_INMEM) || !ISSET(t, R_MODIFIED))
  135.         return (RET_SUCCESS);
  136.  
  137.     /* Read any remaining records into the tree. */
  138.     if (!ISSET(t, R_EOF) && t->bt_irec(t, MAX_REC_NUMBER) == RET_ERROR)
  139.         return (RET_ERROR);
  140.  
  141.     /* Rewind the file descriptor. */
  142.     if (lseek(t->bt_rfd, (off_t)0, SEEK_SET) != 0)
  143.         return (RET_ERROR);
  144.  
  145.     iov[1].iov_base = "\n";
  146.     iov[1].iov_len = 1;
  147.     scursor = t->bt_rcursor;
  148.  
  149.     key.size = sizeof(recno_t);
  150.     key.data = &trec;
  151.  
  152.     status = (dbp->seq)(dbp, &key, &data, R_FIRST);
  153.         while (status == RET_SUCCESS) {
  154.         iov[0].iov_base = data.data;
  155.         iov[0].iov_len = data.size;
  156.         if (writev(t->bt_rfd, iov, 2) != data.size + 1)
  157.             return (RET_ERROR);
  158.                 status = (dbp->seq)(dbp, &key, &data, R_NEXT);
  159.         }
  160.     t->bt_rcursor = scursor;
  161.     if (status == RET_ERROR)
  162.         return (RET_ERROR);
  163.     if ((off = lseek(t->bt_rfd, (off_t)0, SEEK_CUR)) == -1)
  164.         return (RET_ERROR);
  165.     if (ftruncate(t->bt_rfd, off))
  166.         return (RET_ERROR);
  167.     CLR(t, R_MODIFIED);
  168.     return (RET_SUCCESS);
  169. }
  170.