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.2 (Berkeley) 09/11/91"; 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 84 /* 85 * In 4.4BSD stat(2) returns true for ISSOCK on pipes. Until then, 86 * this is fairly close. Pipes are read-only. 87 */ 88 if (lseek(rfd, 0L, SEEK_CUR) == -1 && errno == ESPIPE) { 89 SET(t, BTF_RDONLY); 90 if ((t->bt_rfp = fdopen(rfd, "r")) == NULL) 91 goto err; 92 t->bt_irec = ISSET(t, BTF_FIXEDLEN) ? __rec_fpipe : __rec_vpipe; 93 } else { 94 if (fstat(rfd, &sb)) 95 goto err; 96 if (!(flags & (O_RDWR | O_WRONLY))) 97 SET(t, BTF_RDONLY); 98 if ((t->bt_smap = mmap(NULL, sb.st_size, PROT_READ, MAP_FILE, 99 rfd, (off_t)0)) == NULL) 100 goto err; 101 t->bt_emap = t->bt_smap + sb.st_size; 102 t->bt_rfd = rfd; 103 t->bt_irec = ISSET(t, BTF_FIXEDLEN) ? __rec_fmap : __rec_vmap; 104 } 105 106 /* Use the recno routines. */ 107 dbp->close = __rec_close; 108 dbp->del = __rec_delete; 109 dbp->get = __rec_get; 110 dbp->put = __rec_put; 111 dbp->seq = __rec_seq; 112 dbp->sync = __rec_sync; 113 114 /* If the root page was created, reset the flags. */ 115 if ((h = mpool_get(t->bt_mp, P_ROOT, 0)) == NULL) 116 goto err; 117 if ((h->flags & P_TYPE) == P_BLEAF) { 118 h->flags = h->flags & ~P_TYPE | P_RLEAF; 119 mpool_put(t->bt_mp, h, MPOOL_DIRTY); 120 } else 121 mpool_put(t->bt_mp, h, 0); 122 123 if (openinfo && openinfo->flags & R_SNAPSHOT && 124 t->bt_irec(t, MAX_REC_NUMBER) == RET_ERROR) 125 goto err; 126 return (dbp); 127 128 err: if (dbp) 129 __bt_close(dbp); 130 (void)close(rfd); 131 return (NULL); 132 } 133