1 /*- 2 * Copyright (c) 1990 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Mike Olson. 7 * 8 * %sccs.include.redist.c% 9 */ 10 11 #if defined(LIBC_SCCS) && !defined(lint) 12 static char sccsid[] = "@(#)rec_open.c 5.3 (Berkeley) 06/23/92"; 13 #endif /* LIBC_SCCS and not lint */ 14 15 #include <sys/types.h> 16 #include <sys/mman.h> 17 #include <sys/stat.h> 18 #include <fcntl.h> 19 #include <errno.h> 20 #include <limits.h> 21 #include <db.h> 22 #include <unistd.h> 23 #include <stdio.h> 24 #include <stddef.h> 25 #include "recno.h" 26 27 DB * 28 __rec_open(fname, flags, mode, openinfo) 29 const char *fname; 30 int flags, mode; 31 const RECNOINFO *openinfo; 32 { 33 BTREE *t; 34 BTREEINFO btopeninfo; 35 DB *dbp; 36 PAGE *h; 37 struct stat sb; 38 int rfd; 39 40 /* Open the user's file -- if this fails, we're done. */ 41 if ((rfd = open(fname, flags, mode)) < 0) 42 return (NULL); 43 44 /* Create a btree in memory (backed by disk). */ 45 if (openinfo) { 46 if (openinfo->flags & ~(R_FIXEDLEN|R_NOKEY|R_SNAPSHOT)) { 47 errno = EINVAL; 48 goto err; 49 } 50 btopeninfo.flags = 0; 51 btopeninfo.cachesize = openinfo->cachesize; 52 btopeninfo.psize = 0; 53 btopeninfo.compare = NULL; 54 btopeninfo.lorder = openinfo->lorder; 55 dbp = __bt_open(NULL, O_RDWR, S_IRUSR | S_IWUSR, &btopeninfo); 56 } else 57 dbp = __bt_open(NULL, O_RDWR, S_IRUSR | S_IWUSR, NULL); 58 if (dbp == NULL) 59 goto err; 60 61 /* 62 * Some fields in the tree structure are recno specific. Fill them 63 * in and make the btree structure look like a recno structure. We 64 * don't change the bt_ovflsize value, it's close enough and slightly 65 * bigger. 66 */ 67 t = dbp->internal; 68 if (openinfo) { 69 if (openinfo->flags & R_FIXEDLEN) 70 t->bt_flags |= BTF_FIXEDLEN; 71 72 t->bt_reclen = openinfo->reclen; 73 if (t->bt_reclen == 0) { 74 errno = EINVAL; 75 goto err; 76 } 77 78 t->bt_bval = openinfo->bval; 79 } else 80 t->bt_bval = '\n'; 81 82 t->bt_flags = BTF_RECNO; 83 t->bt_reof = 0; 84 85 /* 86 * In 4.4BSD stat(2) returns true for ISSOCK on pipes. Until 87 * then, this is fairly close. Pipes are read-only. 88 */ 89 if (lseek(rfd, 0L, SEEK_CUR) == -1 && errno == ESPIPE) { 90 SET(t, BTF_RDONLY); 91 if ((t->bt_rfp = fdopen(rfd, "r")) == NULL) 92 goto err; 93 t->bt_irec = ISSET(t, BTF_FIXEDLEN) ? __rec_fpipe : __rec_vpipe; 94 } else { 95 if (fstat(rfd, &sb)) 96 goto err; 97 if (!(flags & (O_RDWR | O_WRONLY))) 98 SET(t, BTF_RDONLY); 99 if (sb.st_size > INT_MAX) { 100 errno = EFBIG; 101 goto err; 102 } 103 if ((t->bt_smap = mmap(NULL, (int)sb.st_size, 104 PROT_READ, MAP_FILE, rfd, (off_t)0)) == NULL) 105 goto err; 106 t->bt_emap = t->bt_smap + sb.st_size; 107 t->bt_rfd = rfd; 108 t->bt_rfp = NULL; 109 t->bt_irec = ISSET(t, BTF_FIXEDLEN) ? __rec_fmap : __rec_vmap; 110 } 111 112 /* Use the recno routines. */ 113 dbp->close = __rec_close; 114 dbp->del = __rec_delete; 115 dbp->get = __rec_get; 116 dbp->put = __rec_put; 117 dbp->seq = __rec_seq; 118 dbp->sync = __rec_sync; 119 120 /* If the root page was created, reset the flags. */ 121 if ((h = mpool_get(t->bt_mp, P_ROOT, 0)) == NULL) 122 goto err; 123 if ((h->flags & P_TYPE) == P_BLEAF) { 124 h->flags = h->flags & ~P_TYPE | P_RLEAF; 125 mpool_put(t->bt_mp, h, MPOOL_DIRTY); 126 } else 127 mpool_put(t->bt_mp, h, 0); 128 129 if (openinfo && openinfo->flags & R_SNAPSHOT && 130 t->bt_irec(t, MAX_REC_NUMBER) == RET_ERROR) 131 goto err; 132 return (dbp); 133 134 err: if (dbp) 135 __bt_close(dbp); 136 (void)close(rfd); 137 return (NULL); 138 } 139