bt_close.c

Go to the documentation of this file.
00001 /*-
00002  * Copyright (c) 1990, 1993, 1994
00003  * The Regents of the University of California.  All rights reserved.
00004  *
00005  * This code is derived from software contributed to Berkeley by
00006  * Mike Olson.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions
00010  * are met:
00011  * 1. Redistributions of source code must retain the above copyright
00012  *    notice, this list of conditions and the following disclaimer.
00013  * 2. Redistributions in binary form must reproduce the above copyright
00014  *    notice, this list of conditions and the following disclaimer in the
00015  *    documentation and/or other materials provided with the distribution.
00016  * 3. All advertising materials mentioning features or use of this software
00017  *    must display the following acknowledgement:
00018  * This product includes software developed by the University of
00019  * California, Berkeley and its contributors.
00020  * 4. Neither the name of the University nor the names of its contributors
00021  *    may be used to endorse or promote products derived from this software
00022  *    without specific prior written permission.
00023  *
00024  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00025  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00026  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00027  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00028  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00029  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00030  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00031  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00032  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00033  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00034  * SUCH DAMAGE.
00035  */
00036 
00037 #if defined(LIBC_SCCS) && !defined(lint)
00038 static char sccsid[] = "@(#)bt_close.c 8.7 (Berkeley) 8/17/94";
00039 #endif /* LIBC_SCCS and not lint */
00040 
00041 #include <sys/param.h>
00042 
00043 #include <errno.h>
00044 #include <stdio.h>
00045 #include <stdlib.h>
00046 #include <string.h>
00047 #include <unistd.h>
00048 
00049 #include "../include/db.h"
00050 #include "btree.h"
00051 
00052 static int bt_meta __P((BTREE *));
00053 
00054 /*
00055  * BT_CLOSE -- Close a btree.
00056  *
00057  * Parameters:
00058  * dbp:  pointer to access method
00059  *
00060  * Returns:
00061  * RET_ERROR, RET_SUCCESS
00062  */
00063 int
00064 __bt_close(dbp)
00065    DB *dbp;
00066 {
00067    BTREE *t;
00068    int fd;
00069 
00070    t = dbp->internal;
00071 
00072    /* Toss any page pinned across calls. */
00073    if (t->bt_pinned != NULL) {
00074       mpool_put(t->bt_mp, t->bt_pinned, 0);
00075       t->bt_pinned = NULL;
00076    }
00077 
00078    /* Sync the tree. */
00079    if (__bt_sync(dbp, 0) == RET_ERROR)
00080       return (RET_ERROR);
00081 
00082    /* Close the memory pool. */
00083    if (mpool_close(t->bt_mp) == RET_ERROR)
00084       return (RET_ERROR);
00085 
00086    /* Free random memory. */
00087    if (t->bt_cursor.key.data != NULL) {
00088       free(t->bt_cursor.key.data);
00089       t->bt_cursor.key.size = 0;
00090       t->bt_cursor.key.data = NULL;
00091    }
00092    if (t->bt_rkey.data) {
00093       free(t->bt_rkey.data);
00094       t->bt_rkey.size = 0;
00095       t->bt_rkey.data = NULL;
00096    }
00097    if (t->bt_rdata.data) {
00098       free(t->bt_rdata.data);
00099       t->bt_rdata.size = 0;
00100       t->bt_rdata.data = NULL;
00101    }
00102 
00103    fd = t->bt_fd;
00104    free(t);
00105    free(dbp);
00106    return (close(fd) ? RET_ERROR : RET_SUCCESS);
00107 }
00108 
00109 /*
00110  * BT_SYNC -- sync the btree to disk.
00111  *
00112  * Parameters:
00113  * dbp:  pointer to access method
00114  *
00115  * Returns:
00116  * RET_SUCCESS, RET_ERROR.
00117  */
00118 int
00119 __bt_sync(dbp, flags)
00120    const DB *dbp;
00121    u_int flags;
00122 {
00123    BTREE *t;
00124    int status;
00125 
00126    t = dbp->internal;
00127 
00128    /* Toss any page pinned across calls. */
00129    if (t->bt_pinned != NULL) {
00130       mpool_put(t->bt_mp, t->bt_pinned, 0);
00131       t->bt_pinned = NULL;
00132    }
00133 
00134    /* Sync doesn't currently take any flags. */
00135    if (flags != 0) {
00136       errno = EINVAL;
00137       return (RET_ERROR);
00138    }
00139 
00140    if (F_ISSET(t, B_INMEM | B_RDONLY) || !F_ISSET(t, B_MODIFIED))
00141       return (RET_SUCCESS);
00142 
00143    if (F_ISSET(t, B_METADIRTY) && bt_meta(t) == RET_ERROR)
00144       return (RET_ERROR);
00145 
00146    if ((status = mpool_sync(t->bt_mp)) == RET_SUCCESS)
00147       F_CLR(t, B_MODIFIED);
00148 
00149    return (status);
00150 }
00151 
00152 /*
00153  * BT_META -- write the tree meta data to disk.
00154  *
00155  * Parameters:
00156  * t: tree
00157  *
00158  * Returns:
00159  * RET_ERROR, RET_SUCCESS
00160  */
00161 static int
00162 bt_meta(t)
00163    BTREE *t;
00164 {
00165    BTMETA m;
00166    void *p;
00167 
00168    if ((p = mpool_get(t->bt_mp, P_META, 0)) == NULL)
00169       return (RET_ERROR);
00170 
00171    /* Fill in metadata. */
00172    m.magic = BTREEMAGIC;
00173    m.version = BTREEVERSION;
00174    m.psize = t->bt_psize;
00175    m.free = t->bt_free;
00176    m.nrecs = t->bt_nrecs;
00177    m.flags = F_ISSET(t, SAVEMETA);
00178 
00179    memmove(p, &m, sizeof(BTMETA));
00180    mpool_put(t->bt_mp, p, MPOOL_DIRTY);
00181    return (RET_SUCCESS);
00182 }

Generated on Thu Apr 16 06:27:16 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6