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.8 (Berkeley) 07/17/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 <fcntl.h> 20 #include <errno.h> 21 #include <limits.h> 22 #include <db.h> 23 #include <unistd.h> 24 #include <stdio.h> 25 #include <stddef.h> 26 #include "recno.h" 27 28 DB * 29 __rec_open(fname, flags, mode, openinfo) 30 const char *fname; 31 int flags, mode; 32 const RECNOINFO *openinfo; 33 { 34 BTREE *t; 35 BTREEINFO btopeninfo; 36 DB *dbp; 37 PAGE *h; 38 struct stat sb; 39 int rfd; 40 41 /* Open the user's file -- if this fails, we're done. */ 42 if ((rfd = open(fname, flags, mode)) < 0) 43 return (NULL); 44 45 /* Create a btree in memory (backed by disk). */ 46 if (openinfo) { 47 if (openinfo->flags & ~(R_FIXEDLEN|R_NOKEY|R_SNAPSHOT)) { 48 errno = EINVAL; 49 goto err; 50 } 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 t->bt_flags |= BTF_FIXEDLEN; 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, (off_t)0, 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 > SIZE_T_MAX) { 100 errno = EFBIG; 101 goto err; 102 } 103 if ((t->bt_smap = mmap(NULL, (size_t)sb.st_size, 104 PROT_READ, 0, rfd, (off_t)0)) == (caddr_t)-1) 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