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.10 (Berkeley) 11/13/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 87 /* 88 * In 4.4BSD stat(2) returns true for ISSOCK on pipes. Until 89 * then, this is fairly close. Pipes are read-only. 90 */ 91 if (fname != NULL) 92 if (lseek(rfd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE) { 93 SET(t, BTF_RDONLY); 94 if ((t->bt_rfp = fdopen(rfd, "r")) == NULL) 95 goto err; 96 t->bt_irec = 97 ISSET(t, BTF_FIXEDLEN) ? __rec_fpipe : __rec_vpipe; 98 } else { 99 if (fstat(rfd, &sb)) 100 goto err; 101 switch(flags & O_ACCMODE) { 102 case O_RDONLY: 103 SET(t, BTF_RDONLY); 104 break; 105 case O_RDWR: 106 break; 107 case O_WRONLY: 108 default: 109 goto einval; 110 } 111 112 if (sb.st_size > SIZE_T_MAX) { 113 errno = EFBIG; 114 goto err; 115 } 116 if ((t->bt_smap = mmap(NULL, (size_t)sb.st_size, 117 PROT_READ, 0, rfd, (off_t)0)) == (caddr_t)-1) 118 goto err; 119 t->bt_emap = t->bt_smap + sb.st_size; 120 t->bt_rfd = rfd; 121 t->bt_rfp = NULL; 122 t->bt_irec = 123 ISSET(t, BTF_FIXEDLEN) ? __rec_fmap : __rec_vmap; 124 } 125 126 /* Use the recno routines. */ 127 dbp->close = __rec_close; 128 dbp->del = __rec_delete; 129 dbp->get = __rec_get; 130 dbp->put = __rec_put; 131 dbp->seq = __rec_seq; 132 dbp->sync = __rec_sync; 133 134 /* If the root page was created, reset the flags. */ 135 if ((h = mpool_get(t->bt_mp, P_ROOT, 0)) == NULL) 136 goto err; 137 if ((h->flags & P_TYPE) == P_BLEAF) { 138 h->flags = h->flags & ~P_TYPE | P_RLEAF; 139 mpool_put(t->bt_mp, h, MPOOL_DIRTY); 140 } else 141 mpool_put(t->bt_mp, h, 0); 142 143 if (openinfo && openinfo->flags & R_SNAPSHOT && 144 !ISSET(t, BTF_RINMEM) && 145 t->bt_irec(t, MAX_REC_NUMBER) == RET_ERROR) 146 goto err; 147 return (dbp); 148 149 einval: errno = EINVAL; 150 err: if (dbp != NULL) 151 __bt_close(dbp); 152 if (fname != NULL) 153 (void)close(rfd); 154 return (NULL); 155 } 156