rec_open.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*-
  2. * Copyright (c) 1990, 1993, 1994
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * This code is derived from software contributed to Berkeley by
  6. * Mike Olson.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. All advertising materials mentioning features or use of this software
  17. * must display the following acknowledgement:
  18. * This product includes software developed by the University of
  19. * California, Berkeley and its contributors.
  20. * 4. Neither the name of the University nor the names of its contributors
  21. * may be used to endorse or promote products derived from this software
  22. * without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34. * SUCH DAMAGE.
  35. */
  36. #if defined(LIBC_SCCS) && !defined(lint)
  37. static char sccsid[] = "@(#)rec_open.c 8.10 (Berkeley) 9/1/94";
  38. #endif /* LIBC_SCCS and not lint */
  39. #include <sys/types.h>
  40. #include <sys/mman.h>
  41. #include <sys/stat.h>
  42. #include <errno.h>
  43. #include <fcntl.h>
  44. #include <limits.h>
  45. #include <stddef.h>
  46. #include <stdio.h>
  47. #include <unistd.h>
  48. #include "../include/db.h"
  49. #include "recno.h"
  50. DB *
  51. __rec_open(fname, flags, mode, openinfo, dflags)
  52. const char *fname;
  53. int flags, mode, dflags;
  54. const RECNOINFO *openinfo;
  55. {
  56. BTREE *t;
  57. BTREEINFO btopeninfo;
  58. DB *dbp;
  59. PAGE *h;
  60. struct stat sb;
  61. int rfd = 0, sverrno;
  62. /* Open the user's file -- if this fails, we're done. */
  63. if (fname != NULL && (rfd = open(fname, flags, mode)) < 0)
  64. return (NULL);
  65. /* Create a btree in memory (backed by disk). */
  66. dbp = NULL;
  67. if (openinfo) {
  68. if (openinfo->flags & ~(R_FIXEDLEN | R_NOKEY | R_SNAPSHOT))
  69. goto einval;
  70. btopeninfo.flags = 0;
  71. btopeninfo.cachesize = openinfo->cachesize;
  72. btopeninfo.maxkeypage = 0;
  73. btopeninfo.minkeypage = 0;
  74. btopeninfo.psize = openinfo->psize;
  75. btopeninfo.compare = NULL;
  76. btopeninfo.prefix = NULL;
  77. btopeninfo.lorder = openinfo->lorder;
  78. dbp = __bt_open(openinfo->bfname,
  79. O_RDWR, S_IRUSR | S_IWUSR, &btopeninfo, dflags);
  80. } else
  81. dbp = __bt_open(NULL, O_RDWR, S_IRUSR | S_IWUSR, NULL, dflags);
  82. if (dbp == NULL)
  83. goto err;
  84. /*
  85. * Some fields in the tree structure are recno specific. Fill them
  86. * in and make the btree structure look like a recno structure. We
  87. * don't change the bt_ovflsize value, it's close enough and slightly
  88. * bigger.
  89. */
  90. t = dbp->internal;
  91. if (openinfo) {
  92. if (openinfo->flags & R_FIXEDLEN) {
  93. F_SET(t, R_FIXLEN);
  94. t->bt_reclen = openinfo->reclen;
  95. if (t->bt_reclen == 0)
  96. goto einval;
  97. }
  98. t->bt_bval = openinfo->bval;
  99. } else
  100. t->bt_bval = '\n';
  101. F_SET(t, R_RECNO);
  102. if (fname == NULL)
  103. F_SET(t, R_EOF | R_INMEM);
  104. else
  105. t->bt_rfd = rfd;
  106. if (fname != NULL) {
  107. /*
  108. * In 4.4BSD, stat(2) returns true for ISSOCK on pipes.
  109. * Unfortunately, that's not portable, so we use lseek
  110. * and check the errno values.
  111. */
  112. errno = 0;
  113. if (lseek(rfd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE) {
  114. switch (flags & O_ACCMODE) {
  115. case O_RDONLY:
  116. F_SET(t, R_RDONLY);
  117. break;
  118. default:
  119. goto einval;
  120. }
  121. slow: if ((t->bt_rfp = fdopen(rfd, "r")) == NULL)
  122. goto err;
  123. F_SET(t, R_CLOSEFP);
  124. t->bt_irec =
  125. F_ISSET(t, R_FIXLEN) ? __rec_fpipe : __rec_vpipe;
  126. } else {
  127. switch (flags & O_ACCMODE) {
  128. case O_RDONLY:
  129. F_SET(t, R_RDONLY);
  130. break;
  131. case O_RDWR:
  132. break;
  133. default:
  134. goto einval;
  135. }
  136. if (fstat(rfd, &sb))
  137. goto err;
  138. /*
  139. * Kluge -- we'd like to test to see if the file is too
  140. * big to mmap. Since, we don't know what size or type
  141. * off_t's or size_t's are, what the largest unsigned
  142. * integral type is, or what random insanity the local
  143. * C compiler will perpetrate, doing the comparison in
  144. * a portable way is flatly impossible. Hope that mmap
  145. * fails if the file is too large.
  146. */
  147. if (sb.st_size == 0)
  148. F_SET(t, R_EOF);
  149. else {
  150. #ifdef MMAP_NOT_AVAILABLE
  151. /*
  152. * XXX
  153. * Mmap doesn't work correctly on many current
  154. * systems. In particular, it can fail subtly,
  155. * with cache coherency problems. Don't use it
  156. * for now.
  157. */
  158. t->bt_msize = sb.st_size;
  159. if ((t->bt_smap = mmap(NULL, t->bt_msize,
  160. PROT_READ, MAP_PRIVATE, rfd,
  161. (off_t)0)) == MAP_FAILED
  162. goto slow;
  163. t->bt_cmap = t->bt_smap;
  164. t->bt_emap = t->bt_smap + sb.st_size;
  165. t->bt_irec = F_ISSET(t, R_FIXLEN) ?
  166. __rec_fmap : __rec_vmap;
  167. F_SET(t, R_MEMMAPPED);
  168. #else
  169. goto slow;
  170. #endif
  171. }
  172. }
  173. }
  174. /* Use the recno routines. */
  175. dbp->close = __rec_close;
  176. dbp->del = __rec_delete;
  177. dbp->fd = __rec_fd;
  178. dbp->get = __rec_get;
  179. dbp->put = __rec_put;
  180. dbp->seq = __rec_seq;
  181. dbp->sync = __rec_sync;
  182. /* If the root page was created, reset the flags. */
  183. if ((h = mpool_get(t->bt_mp, P_ROOT, 0)) == NULL)
  184. goto err;
  185. if ((h->flags & P_TYPE) == P_BLEAF) {
  186. F_CLR(h, P_TYPE);
  187. F_SET(h, P_RLEAF);
  188. mpool_put(t->bt_mp, h, MPOOL_DIRTY);
  189. } else
  190. mpool_put(t->bt_mp, h, 0);
  191. if (openinfo && openinfo->flags & R_SNAPSHOT &&
  192. !F_ISSET(t, R_EOF | R_INMEM) &&
  193. t->bt_irec(t, MAX_REC_NUMBER) == RET_ERROR)
  194. goto err;
  195. return (dbp);
  196. einval: errno = EINVAL;
  197. err: sverrno = errno;
  198. if (dbp != NULL)
  199. (void)__bt_close(dbp);
  200. if (fname != NULL)
  201. (void)close(rfd);
  202. errno = sverrno;
  203. return (NULL);
  204. }
  205. int
  206. __rec_fd(dbp)
  207. const DB *dbp;
  208. {
  209. BTREE *t;
  210. t = dbp->internal;
  211. /* Toss any page pinned across calls. */
  212. if (t->bt_pinned != NULL) {
  213. mpool_put(t->bt_mp, t->bt_pinned, 0);
  214. t->bt_pinned = NULL;
  215. }
  216. /* In-memory database can't have a file descriptor. */
  217. if (F_ISSET(t, R_INMEM)) {
  218. errno = ENOENT;
  219. return (-1);
  220. }
  221. return (t->bt_rfd);
  222. }