150995Sbostic /*- 250995Sbostic * Copyright (c) 1990 The Regents of the University of California. 350995Sbostic * All rights reserved. 450995Sbostic * 550995Sbostic * This code is derived from software contributed to Berkeley by 650995Sbostic * Mike Olson. 750995Sbostic * 850995Sbostic * %sccs.include.redist.c% 950995Sbostic */ 1050995Sbostic 1150995Sbostic #if defined(LIBC_SCCS) && !defined(lint) 12*58751Sbostic static char sccsid[] = "@(#)rec_open.c 5.15 (Berkeley) 03/19/93"; 1350995Sbostic #endif /* LIBC_SCCS and not lint */ 1450995Sbostic 1550995Sbostic #include <sys/types.h> 1650995Sbostic #include <sys/mman.h> 1750995Sbostic #include <sys/stat.h> 1854362Sbostic 1956757Sbostic #include <errno.h> 2050995Sbostic #include <fcntl.h> 2150995Sbostic #include <limits.h> 2256757Sbostic #include <stddef.h> 2356757Sbostic #include <stdio.h> 2450995Sbostic #include <unistd.h> 2556757Sbostic 2657933Sbostic #include <db.h> 2751090Sbostic #include "recno.h" 2850995Sbostic 2950995Sbostic DB * 3050995Sbostic __rec_open(fname, flags, mode, openinfo) 3150995Sbostic const char *fname; 3250995Sbostic int flags, mode; 3350995Sbostic const RECNOINFO *openinfo; 3450995Sbostic { 3550995Sbostic BTREE *t; 3650995Sbostic BTREEINFO btopeninfo; 3750995Sbostic DB *dbp; 3850995Sbostic PAGE *h; 3950995Sbostic struct stat sb; 40*58751Sbostic int rfd, sverrno; 4150995Sbostic 4250995Sbostic /* Open the user's file -- if this fails, we're done. */ 4356757Sbostic if (fname != NULL && (rfd = open(fname, flags, mode)) < 0) 4450995Sbostic return (NULL); 4550995Sbostic 4650995Sbostic /* Create a btree in memory (backed by disk). */ 4756757Sbostic dbp = NULL; 4850995Sbostic if (openinfo) { 4956757Sbostic if (openinfo->flags & ~(R_FIXEDLEN | R_NOKEY | R_SNAPSHOT)) 5056703Sbostic goto einval; 5150995Sbostic btopeninfo.flags = 0; 5250995Sbostic btopeninfo.cachesize = openinfo->cachesize; 5350995Sbostic btopeninfo.psize = 0; 5450995Sbostic btopeninfo.compare = NULL; 5550995Sbostic btopeninfo.lorder = openinfo->lorder; 5650995Sbostic dbp = __bt_open(NULL, O_RDWR, S_IRUSR | S_IWUSR, &btopeninfo); 5750995Sbostic } else 5850995Sbostic dbp = __bt_open(NULL, O_RDWR, S_IRUSR | S_IWUSR, NULL); 5951090Sbostic if (dbp == NULL) 6051090Sbostic goto err; 6150995Sbostic 6250995Sbostic /* 6350995Sbostic * Some fields in the tree structure are recno specific. Fill them 6451090Sbostic * in and make the btree structure look like a recno structure. We 6551090Sbostic * don't change the bt_ovflsize value, it's close enough and slightly 6651090Sbostic * bigger. 6750995Sbostic */ 6850995Sbostic t = dbp->internal; 6950995Sbostic if (openinfo) { 7055295Sbostic if (openinfo->flags & R_FIXEDLEN) { 7156757Sbostic SET(t, BTF_FIXEDLEN); 7255295Sbostic t->bt_reclen = openinfo->reclen; 7356703Sbostic if (t->bt_reclen == 0) 7456703Sbostic goto einval; 7550995Sbostic } 7650995Sbostic t->bt_bval = openinfo->bval; 7750995Sbostic } else 7850995Sbostic t->bt_bval = '\n'; 7950995Sbostic 8056757Sbostic SET(t, BTF_RECNO); 81*58751Sbostic if (fname == NULL) 82*58751Sbostic SET(t, BTF_EOF | BTF_RINMEM); 83*58751Sbostic else 84*58751Sbostic t->bt_rfd = rfd; 8557007Sbostic t->bt_rcursor = 0; 8650995Sbostic 8750995Sbostic /* 8854286Sbostic * In 4.4BSD stat(2) returns true for ISSOCK on pipes. Until 8954286Sbostic * then, this is fairly close. Pipes are read-only. 9054286Sbostic */ 9156757Sbostic if (fname != NULL) 9256757Sbostic if (lseek(rfd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE) { 93*58751Sbostic switch(flags & O_ACCMODE) { 94*58751Sbostic case O_RDONLY: 95*58751Sbostic SET(t, BTF_RDONLY); 96*58751Sbostic break; 97*58751Sbostic case O_RDWR: 98*58751Sbostic case O_WRONLY: 99*58751Sbostic default: 100*58751Sbostic goto einval; 101*58751Sbostic } 10257227Sbostic slow: if ((t->bt_rfp = fdopen(rfd, "r")) == NULL) 10356757Sbostic goto err; 104*58751Sbostic SET(t, BTF_CLOSEFP); 10556757Sbostic t->bt_irec = 10656757Sbostic ISSET(t, BTF_FIXEDLEN) ? __rec_fpipe : __rec_vpipe; 10756757Sbostic } else { 10856757Sbostic switch(flags & O_ACCMODE) { 10956757Sbostic case O_RDONLY: 11056757Sbostic SET(t, BTF_RDONLY); 11156757Sbostic break; 11256757Sbostic case O_RDWR: 11356757Sbostic break; 11456757Sbostic case O_WRONLY: 11556757Sbostic default: 11656757Sbostic goto einval; 11756757Sbostic } 11856757Sbostic 119*58751Sbostic if (fstat(rfd, &sb)) 120*58751Sbostic goto err; 12158311Sbostic if (sb.st_size > (off_t)SIZE_T_MAX) { 12256757Sbostic errno = EFBIG; 12356757Sbostic goto err; 12456757Sbostic } 125*58751Sbostic if (sb.st_size == 0) 126*58751Sbostic SET(t, BTF_EOF); 127*58751Sbostic else { 128*58751Sbostic t->bt_msize = sb.st_size; 129*58751Sbostic if ((t->bt_smap = 130*58751Sbostic mmap(NULL, t->bt_msize, PROT_READ, 0, rfd, 131*58751Sbostic (off_t)0)) == (caddr_t)-1) 132*58751Sbostic goto slow; 133*58751Sbostic t->bt_cmap = t->bt_smap; 134*58751Sbostic t->bt_emap = t->bt_smap + sb.st_size; 135*58751Sbostic t->bt_irec = ISSET(t, BTF_FIXEDLEN) ? 136*58751Sbostic __rec_fmap : __rec_vmap; 137*58751Sbostic SET(t, BTF_MEMMAPPED); 138*58751Sbostic } 13956703Sbostic } 14050995Sbostic 14150995Sbostic /* Use the recno routines. */ 14250995Sbostic dbp->close = __rec_close; 14350995Sbostic dbp->del = __rec_delete; 14450995Sbostic dbp->get = __rec_get; 14550995Sbostic dbp->put = __rec_put; 14650995Sbostic dbp->seq = __rec_seq; 14750995Sbostic dbp->sync = __rec_sync; 14850995Sbostic 14950995Sbostic /* If the root page was created, reset the flags. */ 15050995Sbostic if ((h = mpool_get(t->bt_mp, P_ROOT, 0)) == NULL) 15150995Sbostic goto err; 15250995Sbostic if ((h->flags & P_TYPE) == P_BLEAF) { 15350995Sbostic h->flags = h->flags & ~P_TYPE | P_RLEAF; 15450995Sbostic mpool_put(t->bt_mp, h, MPOOL_DIRTY); 15550995Sbostic } else 15650995Sbostic mpool_put(t->bt_mp, h, 0); 15750995Sbostic 15850995Sbostic if (openinfo && openinfo->flags & R_SNAPSHOT && 159*58751Sbostic !ISSET(t, BTF_EOF | BTF_RINMEM) && 16050995Sbostic t->bt_irec(t, MAX_REC_NUMBER) == RET_ERROR) 16150995Sbostic goto err; 16250995Sbostic return (dbp); 16350995Sbostic 16456703Sbostic einval: errno = EINVAL; 165*58751Sbostic err: sverrno = errno; 166*58751Sbostic if (dbp != NULL) 167*58751Sbostic (void)__bt_close(dbp); 16856757Sbostic if (fname != NULL) 16956757Sbostic (void)close(rfd); 170*58751Sbostic errno = sverrno; 17150995Sbostic return (NULL); 17250995Sbostic } 173