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.12 (Berkeley) 12/19/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 19 #include <db.h> 20 #include <errno.h> 21 #include <fcntl.h> 22 #include <limits.h> 23 #include <stddef.h> 24 #include <stdio.h> 25 #include <unistd.h> 26 27 #include "recno.h" 28 29 DB * 30 __rec_open(fname, flags, mode, openinfo) 31 const char *fname; 32 int flags, mode; 33 const RECNOINFO *openinfo; 34 { 35 BTREE *t; 36 BTREEINFO btopeninfo; 37 DB *dbp; 38 PAGE *h; 39 struct stat sb; 40 int rfd; 41 42 /* Open the user's file -- if this fails, we're done. */ 43 if (fname != NULL && (rfd = open(fname, flags, mode)) < 0) 44 return (NULL); 45 46 /* Create a btree in memory (backed by disk). */ 47 dbp = NULL; 48 if (openinfo) { 49 if (openinfo->flags & ~(R_FIXEDLEN | R_NOKEY | R_SNAPSHOT)) 50 goto einval; 51 btopeninfo.flags = 0; 52 btopeninfo.cachesize = openinfo->cachesize; 53 btopeninfo.psize = 0; 54 btopeninfo.compare = NULL; 55 btopeninfo.lorder = openinfo->lorder; 56 dbp = __bt_open(NULL, O_RDWR, S_IRUSR | S_IWUSR, &btopeninfo); 57 } else 58 dbp = __bt_open(NULL, O_RDWR, S_IRUSR | S_IWUSR, NULL); 59 if (dbp == NULL) 60 goto err; 61 62 /* 63 * Some fields in the tree structure are recno specific. Fill them 64 * in and make the btree structure look like a recno structure. We 65 * don't change the bt_ovflsize value, it's close enough and slightly 66 * bigger. 67 */ 68 t = dbp->internal; 69 if (openinfo) { 70 if (openinfo->flags & R_FIXEDLEN) { 71 SET(t, BTF_FIXEDLEN); 72 t->bt_reclen = openinfo->reclen; 73 if (t->bt_reclen == 0) 74 goto einval; 75 } 76 t->bt_bval = openinfo->bval; 77 } else 78 t->bt_bval = '\n'; 79 80 SET(t, BTF_RECNO); 81 if (fname == NULL) { 82 SET(t, BTF_RINMEM); 83 t->bt_reof = 1; 84 } else 85 t->bt_reof = 0; 86 t->bt_rcursor = 0; 87 88 /* 89 * In 4.4BSD stat(2) returns true for ISSOCK on pipes. Until 90 * then, this is fairly close. Pipes are read-only. 91 */ 92 if (fname != NULL) 93 if (lseek(rfd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE) { 94 SET(t, BTF_RDONLY); 95 slow: if ((t->bt_rfp = fdopen(rfd, "r")) == NULL) 96 goto err; 97 t->bt_irec = 98 ISSET(t, BTF_FIXEDLEN) ? __rec_fpipe : __rec_vpipe; 99 } else { 100 if (fstat(rfd, &sb)) 101 goto err; 102 switch(flags & O_ACCMODE) { 103 case O_RDONLY: 104 SET(t, BTF_RDONLY); 105 break; 106 case O_RDWR: 107 break; 108 case O_WRONLY: 109 default: 110 goto einval; 111 } 112 113 if (sb.st_size > SIZE_T_MAX) { 114 errno = EFBIG; 115 goto err; 116 } 117 if ((t->bt_smap = mmap(NULL, (size_t)sb.st_size, 118 PROT_READ, 0, rfd, (off_t)0)) == (caddr_t)-1) 119 goto slow; 120 t->bt_emap = t->bt_smap + sb.st_size; 121 t->bt_rfd = rfd; 122 t->bt_rfp = NULL; 123 t->bt_irec = 124 ISSET(t, BTF_FIXEDLEN) ? __rec_fmap : __rec_vmap; 125 } 126 127 /* Use the recno routines. */ 128 dbp->close = __rec_close; 129 dbp->del = __rec_delete; 130 dbp->get = __rec_get; 131 dbp->put = __rec_put; 132 dbp->seq = __rec_seq; 133 dbp->sync = __rec_sync; 134 135 /* If the root page was created, reset the flags. */ 136 if ((h = mpool_get(t->bt_mp, P_ROOT, 0)) == NULL) 137 goto err; 138 if ((h->flags & P_TYPE) == P_BLEAF) { 139 h->flags = h->flags & ~P_TYPE | P_RLEAF; 140 mpool_put(t->bt_mp, h, MPOOL_DIRTY); 141 } else 142 mpool_put(t->bt_mp, h, 0); 143 144 if (openinfo && openinfo->flags & R_SNAPSHOT && 145 !ISSET(t, BTF_RINMEM) && 146 t->bt_irec(t, MAX_REC_NUMBER) == RET_ERROR) 147 goto err; 148 return (dbp); 149 150 einval: errno = EINVAL; 151 err: if (dbp != NULL) 152 __bt_close(dbp); 153 if (fname != NULL) 154 (void)close(rfd); 155 return (NULL); 156 } 157