bt_close.c 6.1 KB

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