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.1 (Berkeley) 09/04/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 "../btree/btree.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 btopeninfo.flags = 0; 47 btopeninfo.cachesize = openinfo->cachesize; 48 btopeninfo.psize = 0; 49 btopeninfo.compare = NULL; 50 btopeninfo.lorder = openinfo->lorder; 51 dbp = __bt_open(NULL, O_RDWR, S_IRUSR | S_IWUSR, &btopeninfo); 52 } else 53 dbp = __bt_open(NULL, O_RDWR, S_IRUSR | S_IWUSR, NULL); 54 if (dbp == NULL) { 55 (void)close(rfd); 56 return (NULL); 57 } 58 59 /* 60 * Some fields in the tree structure are recno specific. Fill them 61 * in and make the btree structure look like a recno structure. 62 */ 63 t = dbp->internal; 64 if (openinfo) { 65 if (openinfo->flags & R_FIXEDLEN) 66 t->bt_flags |= BTF_FIXEDLEN; 67 68 t->bt_reclen = openinfo->reclen; 69 if (t->bt_reclen == 0) { 70 errno = EINVAL; 71 goto err; 72 } 73 74 t->bt_bval = openinfo->bval; 75 } else 76 t->bt_bval = '\n'; 77 78 t->bt_flags = BTF_RECNO; 79 80 /* 81 * In 4.4BSD stat(2) returns true for ISSOCK on pipes. Until then, 82 * this is fairly close. Pipes are read-only. 83 */ 84 if (lseek(rfd, 0L, SEEK_CUR) == -1 && errno == ESPIPE) { 85 SET(t, BTF_RDONLY); 86 if ((t->bt_rfp = fdopen(rfd, "r")) == NULL) 87 goto err; 88 t->bt_irec = ISSET(t, BTF_FIXEDLEN) ? __rec_fpipe : __rec_vpipe; 89 } else { 90 if (fstat(rfd, &sb)) 91 goto err; 92 if (!(flags & (O_RDWR | O_WRONLY))) 93 SET(t, BTF_RDONLY); 94 if ((t->bt_smap = mmap(NULL, sb.st_size, PROT_READ, MAP_FILE, 95 rfd, (off_t)0)) == NULL) 96 goto err; 97 t->bt_emap = t->bt_smap + sb.st_size; 98 t->bt_rfd = rfd; 99 t->bt_irec = ISSET(t, BTF_FIXEDLEN) ? __rec_fmap : __rec_vmap; 100 } 101 102 /* Use the recno routines. */ 103 dbp->close = __rec_close; 104 dbp->del = __rec_delete; 105 dbp->get = __rec_get; 106 dbp->put = __rec_put; 107 dbp->seq = __rec_seq; 108 dbp->sync = __rec_sync; 109 110 /* If the root page was created, reset the flags. */ 111 if ((h = mpool_get(t->bt_mp, P_ROOT, 0)) == NULL) 112 goto err; 113 if ((h->flags & P_TYPE) == P_BLEAF) { 114 h->flags = h->flags & ~P_TYPE | P_RLEAF; 115 mpool_put(t->bt_mp, h, MPOOL_DIRTY); 116 } else 117 mpool_put(t->bt_mp, h, 0); 118 119 if (openinfo && openinfo->flags & R_SNAPSHOT && 120 t->bt_irec(t, MAX_REC_NUMBER) == RET_ERROR) 121 goto err; 122 return (dbp); 123 124 err: __bt_close(dbp); 125 (void)close(rfd); 126 return (NULL); 127 } 128