home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / dev / gcc / ixemulsrc.lha / ixemul / db / btree / bt_close.c next >
C/C++ Source or Header  |  1996-12-11  |  5KB  |  211 lines

  1. /*    $NetBSD: bt_close.c,v 1.6 1995/02/27 13:19:59 cgd Exp $    */
  2.  
  3. /*-
  4.  * Copyright (c) 1990, 1993
  5.  *    The Regents of the University of California.  All rights reserved.
  6.  *
  7.  * This code is derived from software contributed to Berkeley by
  8.  * Mike Olson.
  9.  *
  10.  * Redistribution and use in source and binary forms, with or without
  11.  * modification, are permitted provided that the following conditions
  12.  * are met:
  13.  * 1. Redistributions of source code must retain the above copyright
  14.  *    notice, this list of conditions and the following disclaimer.
  15.  * 2. Redistributions in binary form must reproduce the above copyright
  16.  *    notice, this list of conditions and the following disclaimer in the
  17.  *    documentation and/or other materials provided with the distribution.
  18.  * 3. All advertising materials mentioning features or use of this software
  19.  *    must display the following acknowledgement:
  20.  *    This product includes software developed by the University of
  21.  *    California, Berkeley and its contributors.
  22.  * 4. Neither the name of the University nor the names of its contributors
  23.  *    may be used to endorse or promote products derived from this software
  24.  *    without specific prior written permission.
  25.  *
  26.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  27.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  30.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  32.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  33.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  34.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  35.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  36.  * SUCH DAMAGE.
  37.  */
  38.  
  39. #if defined(LIBC_SCCS) && !defined(lint)
  40. #if 0
  41. static char sccsid[] = "@(#)bt_close.c    8.3 (Berkeley) 2/21/94";
  42. #else
  43. static char rcsid[] = "$NetBSD: bt_close.c,v 1.6 1995/02/27 13:19:59 cgd Exp $";
  44. #endif
  45. #endif /* LIBC_SCCS and not lint */
  46.  
  47. #include <sys/param.h>
  48.  
  49. #include <errno.h>
  50. #include <stdio.h>
  51. #include <stdlib.h>
  52. #include <string.h>
  53. #include <unistd.h>
  54.  
  55. #include <db.h>
  56. #include "btree.h"
  57.  
  58. static int bt_meta __P((BTREE *));
  59.  
  60. /*
  61.  * BT_CLOSE -- Close a btree.
  62.  *
  63.  * Parameters:
  64.  *    dbp:    pointer to access method
  65.  *
  66.  * Returns:
  67.  *    RET_ERROR, RET_SUCCESS
  68.  */
  69. int
  70. __bt_close(dbp)
  71.     DB *dbp;
  72. {
  73.     BTREE *t;
  74.     int fd;
  75.  
  76.     t = dbp->internal;
  77.  
  78.     /* Toss any page pinned across calls. */
  79.     if (t->bt_pinned != NULL) {
  80.         mpool_put(t->bt_mp, t->bt_pinned, 0);
  81.         t->bt_pinned = NULL;
  82.     }
  83.  
  84.     /*
  85.      * Delete any already deleted record that we've been saving
  86.      * because the cursor pointed to it.
  87.      */
  88.     if (ISSET(t, B_DELCRSR) && __bt_crsrdel(t, &t->bt_bcursor))
  89.         return (RET_ERROR);
  90.  
  91.     if (__bt_sync(dbp, 0) == RET_ERROR)
  92.         return (RET_ERROR);
  93.  
  94.     if (mpool_close(t->bt_mp) == RET_ERROR)
  95.         return (RET_ERROR);
  96.  
  97.     if (t->bt_stack)
  98.         free(t->bt_stack);
  99.     if (t->bt_kbuf)
  100.         free(t->bt_kbuf);
  101.     if (t->bt_dbuf)
  102.         free(t->bt_dbuf);
  103.  
  104.     fd = t->bt_fd;
  105.     free(t);
  106.     free(dbp);
  107.     return (close(fd) ? RET_ERROR : RET_SUCCESS);
  108. }
  109.  
  110. /*
  111.  * BT_SYNC -- sync the btree to disk.
  112.  *
  113.  * Parameters:
  114.  *    dbp:    pointer to access method
  115.  *
  116.  * Returns:
  117.  *    RET_SUCCESS, RET_ERROR.
  118.  */
  119. int
  120. __bt_sync(dbp, flags)
  121.     const DB *dbp;
  122.     u_int flags;
  123. {
  124.     BTREE *t;
  125.     int status;
  126.     PAGE *h;
  127.     void *p = NULL;
  128.  
  129.     t = dbp->internal;
  130.  
  131.     /* Toss any page pinned across calls. */
  132.     if (t->bt_pinned != NULL) {
  133.         mpool_put(t->bt_mp, t->bt_pinned, 0);
  134.         t->bt_pinned = NULL;
  135.     }
  136.  
  137.     /* Sync doesn't currently take any flags. */
  138.     if (flags != 0) {
  139.         errno = EINVAL;
  140.         return (RET_ERROR);
  141.     }
  142.  
  143.     if (ISSET(t, B_INMEM | B_RDONLY) || !ISSET(t, B_MODIFIED))
  144.         return (RET_SUCCESS);
  145.  
  146.     if (ISSET(t, B_METADIRTY) && bt_meta(t) == RET_ERROR)
  147.         return (RET_ERROR);
  148.  
  149.     /*
  150.      * Nastiness.  If the cursor has been marked for deletion, but not
  151.      * actually deleted, we have to make a copy of the page, delete the
  152.      * key/data item, sync the file, and then restore the original page
  153.      * contents.
  154.      */
  155.     if (ISSET(t, B_DELCRSR)) {
  156.         if ((p = (void *)malloc(t->bt_psize)) == NULL)
  157.             return (RET_ERROR);
  158.         if ((h = mpool_get(t->bt_mp, t->bt_bcursor.pgno, 0)) == NULL)
  159.             return (RET_ERROR);
  160.         memmove(p, h, t->bt_psize);
  161.         if ((status =
  162.             __bt_dleaf(t, h, t->bt_bcursor.index)) == RET_ERROR)
  163.             goto ecrsr;
  164.         mpool_put(t->bt_mp, h, MPOOL_DIRTY);
  165.     }
  166.         
  167.     if ((status = mpool_sync(t->bt_mp)) == RET_SUCCESS)
  168.         CLR(t, B_MODIFIED);
  169.  
  170. ecrsr:    if (ISSET(t, B_DELCRSR)) {
  171.         if ((h = mpool_get(t->bt_mp, t->bt_bcursor.pgno, 0)) == NULL)
  172.             return (RET_ERROR);
  173.         memmove(h, p, t->bt_psize);
  174.         free(p);
  175.         mpool_put(t->bt_mp, h, MPOOL_DIRTY);
  176.     }
  177.     return (status);
  178. }
  179.  
  180. /*
  181.  * BT_META -- write the tree meta data to disk.
  182.  *
  183.  * Parameters:
  184.  *    t:    tree
  185.  *
  186.  * Returns:
  187.  *    RET_ERROR, RET_SUCCESS
  188.  */
  189. static int
  190. bt_meta(t)
  191.     BTREE *t;
  192. {
  193.     BTMETA m;
  194.     void *p;
  195.  
  196.     if ((p = mpool_get(t->bt_mp, P_META, 0)) == NULL)
  197.         return (RET_ERROR);
  198.  
  199.     /* Fill in metadata. */
  200.     m.m_magic = BTREEMAGIC;
  201.     m.m_version = BTREEVERSION;
  202.     m.m_psize = t->bt_psize;
  203.     m.m_free = t->bt_free;
  204.     m.m_nrecs = t->bt_nrecs;
  205.     m.m_flags = t->bt_flags & SAVEMETA;
  206.  
  207.     memmove(p, &m, sizeof(BTMETA));
  208.     mpool_put(t->bt_mp, p, MPOOL_DIRTY);
  209.     return (RET_SUCCESS);
  210. }
  211.