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*59772Sbostic static char sccsid[] = "@(#)rec_open.c 5.17 (Berkeley) 05/06/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; 4058751Sbostic 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; 5358790Sralph btopeninfo.maxkeypage = 0; 5458790Sralph btopeninfo.minkeypage = 0; 5550995Sbostic btopeninfo.psize = 0; 5650995Sbostic btopeninfo.compare = NULL; 5758790Sralph btopeninfo.prefix = NULL; 5850995Sbostic btopeninfo.lorder = openinfo->lorder; 5950995Sbostic dbp = __bt_open(NULL, O_RDWR, S_IRUSR | S_IWUSR, &btopeninfo); 6050995Sbostic } else 6150995Sbostic dbp = __bt_open(NULL, O_RDWR, S_IRUSR | S_IWUSR, NULL); 6251090Sbostic if (dbp == NULL) 6351090Sbostic goto err; 6450995Sbostic 6550995Sbostic /* 6650995Sbostic * Some fields in the tree structure are recno specific. Fill them 6751090Sbostic * in and make the btree structure look like a recno structure. We 6851090Sbostic * don't change the bt_ovflsize value, it's close enough and slightly 6951090Sbostic * bigger. 7050995Sbostic */ 7150995Sbostic t = dbp->internal; 7250995Sbostic if (openinfo) { 7355295Sbostic if (openinfo->flags & R_FIXEDLEN) { 7456757Sbostic SET(t, BTF_FIXEDLEN); 7555295Sbostic t->bt_reclen = openinfo->reclen; 7656703Sbostic if (t->bt_reclen == 0) 7756703Sbostic goto einval; 7850995Sbostic } 7950995Sbostic t->bt_bval = openinfo->bval; 8050995Sbostic } else 8150995Sbostic t->bt_bval = '\n'; 8250995Sbostic 8356757Sbostic SET(t, BTF_RECNO); 8458751Sbostic if (fname == NULL) 8558751Sbostic SET(t, BTF_EOF | BTF_RINMEM); 8658751Sbostic else 8758751Sbostic t->bt_rfd = rfd; 8857007Sbostic t->bt_rcursor = 0; 8950995Sbostic 9050995Sbostic /* 9154286Sbostic * In 4.4BSD stat(2) returns true for ISSOCK on pipes. Until 9254286Sbostic * then, this is fairly close. Pipes are read-only. 9354286Sbostic */ 9458790Sralph if (fname != NULL) { 9556757Sbostic if (lseek(rfd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE) { 9658790Sralph switch (flags & O_ACCMODE) { 9758751Sbostic case O_RDONLY: 9858751Sbostic SET(t, BTF_RDONLY); 9958751Sbostic break; 10058751Sbostic default: 10158751Sbostic goto einval; 10258751Sbostic } 10357227Sbostic slow: if ((t->bt_rfp = fdopen(rfd, "r")) == NULL) 10456757Sbostic goto err; 10558751Sbostic SET(t, BTF_CLOSEFP); 10656757Sbostic t->bt_irec = 10756757Sbostic ISSET(t, BTF_FIXEDLEN) ? __rec_fpipe : __rec_vpipe; 10856757Sbostic } else { 10958790Sralph switch (flags & O_ACCMODE) { 11056757Sbostic case O_RDONLY: 11156757Sbostic SET(t, BTF_RDONLY); 11256757Sbostic break; 11356757Sbostic case O_RDWR: 11456757Sbostic break; 11556757Sbostic default: 11656757Sbostic goto einval; 11756757Sbostic } 11858790Sralph 11958751Sbostic if (fstat(rfd, &sb)) 12058751Sbostic goto err; 121*59772Sbostic /* 122*59772Sbostic * Kludge -- but we don't know what size an off_t 123*59772Sbostic * is or what size a size_t is, although we do 124*59772Sbostic * know that the former is signed and the latter 125*59772Sbostic * unsigned. 126*59772Sbostic */ 127*59772Sbostic if (sizeof(sb.st_size) > sizeof(size_t)) { 128*59772Sbostic if (sb.st_size > (off_t)SIZE_T_MAX) { 129*59772Sbostic errno = EFBIG; 130*59772Sbostic goto err; 131*59772Sbostic } 132*59772Sbostic } else { 133*59772Sbostic if ((size_t)sb.st_size > SIZE_T_MAX) { 134*59772Sbostic errno = EFBIG; 135*59772Sbostic goto err; 136*59772Sbostic } 13756757Sbostic } 13858751Sbostic if (sb.st_size == 0) 13958751Sbostic SET(t, BTF_EOF); 14058751Sbostic else { 14158751Sbostic t->bt_msize = sb.st_size; 14258751Sbostic if ((t->bt_smap = 14358751Sbostic mmap(NULL, t->bt_msize, PROT_READ, 0, rfd, 14458751Sbostic (off_t)0)) == (caddr_t)-1) 14558751Sbostic goto slow; 14658751Sbostic t->bt_cmap = t->bt_smap; 14758751Sbostic t->bt_emap = t->bt_smap + sb.st_size; 14858751Sbostic t->bt_irec = ISSET(t, BTF_FIXEDLEN) ? 14958751Sbostic __rec_fmap : __rec_vmap; 15058751Sbostic SET(t, BTF_MEMMAPPED); 15158751Sbostic } 15256703Sbostic } 15358790Sralph } 15450995Sbostic 15550995Sbostic /* Use the recno routines. */ 15650995Sbostic dbp->close = __rec_close; 15750995Sbostic dbp->del = __rec_delete; 15850995Sbostic dbp->get = __rec_get; 15950995Sbostic dbp->put = __rec_put; 16050995Sbostic dbp->seq = __rec_seq; 16150995Sbostic dbp->sync = __rec_sync; 16250995Sbostic 16350995Sbostic /* If the root page was created, reset the flags. */ 16450995Sbostic if ((h = mpool_get(t->bt_mp, P_ROOT, 0)) == NULL) 16550995Sbostic goto err; 16650995Sbostic if ((h->flags & P_TYPE) == P_BLEAF) { 16750995Sbostic h->flags = h->flags & ~P_TYPE | P_RLEAF; 16850995Sbostic mpool_put(t->bt_mp, h, MPOOL_DIRTY); 16950995Sbostic } else 17050995Sbostic mpool_put(t->bt_mp, h, 0); 17150995Sbostic 17250995Sbostic if (openinfo && openinfo->flags & R_SNAPSHOT && 17358751Sbostic !ISSET(t, BTF_EOF | BTF_RINMEM) && 17450995Sbostic t->bt_irec(t, MAX_REC_NUMBER) == RET_ERROR) 17550995Sbostic goto err; 17650995Sbostic return (dbp); 17750995Sbostic 17856703Sbostic einval: errno = EINVAL; 17958751Sbostic err: sverrno = errno; 18058751Sbostic if (dbp != NULL) 18158751Sbostic (void)__bt_close(dbp); 18256757Sbostic if (fname != NULL) 18356757Sbostic (void)close(rfd); 18458751Sbostic errno = sverrno; 18550995Sbostic return (NULL); 18650995Sbostic } 187