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*58790Sralph static char sccsid[] = "@(#)rec_open.c 5.16 (Berkeley) 03/23/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; 53*58790Sralph btopeninfo.maxkeypage = 0; 54*58790Sralph btopeninfo.minkeypage = 0; 5550995Sbostic btopeninfo.psize = 0; 5650995Sbostic btopeninfo.compare = NULL; 57*58790Sralph 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 */ 94*58790Sralph if (fname != NULL) { 9556757Sbostic if (lseek(rfd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE) { 96*58790Sralph 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 { 109*58790Sralph 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 } 118*58790Sralph 11958751Sbostic if (fstat(rfd, &sb)) 12058751Sbostic goto err; 12158311Sbostic if (sb.st_size > (off_t)SIZE_T_MAX) { 12256757Sbostic errno = EFBIG; 12356757Sbostic goto err; 12456757Sbostic } 12558751Sbostic if (sb.st_size == 0) 12658751Sbostic SET(t, BTF_EOF); 12758751Sbostic else { 12858751Sbostic t->bt_msize = sb.st_size; 12958751Sbostic if ((t->bt_smap = 13058751Sbostic mmap(NULL, t->bt_msize, PROT_READ, 0, rfd, 13158751Sbostic (off_t)0)) == (caddr_t)-1) 13258751Sbostic goto slow; 13358751Sbostic t->bt_cmap = t->bt_smap; 13458751Sbostic t->bt_emap = t->bt_smap + sb.st_size; 13558751Sbostic t->bt_irec = ISSET(t, BTF_FIXEDLEN) ? 13658751Sbostic __rec_fmap : __rec_vmap; 13758751Sbostic SET(t, BTF_MEMMAPPED); 13858751Sbostic } 13956703Sbostic } 140*58790Sralph } 14150995Sbostic 14250995Sbostic /* Use the recno routines. */ 14350995Sbostic dbp->close = __rec_close; 14450995Sbostic dbp->del = __rec_delete; 14550995Sbostic dbp->get = __rec_get; 14650995Sbostic dbp->put = __rec_put; 14750995Sbostic dbp->seq = __rec_seq; 14850995Sbostic dbp->sync = __rec_sync; 14950995Sbostic 15050995Sbostic /* If the root page was created, reset the flags. */ 15150995Sbostic if ((h = mpool_get(t->bt_mp, P_ROOT, 0)) == NULL) 15250995Sbostic goto err; 15350995Sbostic if ((h->flags & P_TYPE) == P_BLEAF) { 15450995Sbostic h->flags = h->flags & ~P_TYPE | P_RLEAF; 15550995Sbostic mpool_put(t->bt_mp, h, MPOOL_DIRTY); 15650995Sbostic } else 15750995Sbostic mpool_put(t->bt_mp, h, 0); 15850995Sbostic 15950995Sbostic if (openinfo && openinfo->flags & R_SNAPSHOT && 16058751Sbostic !ISSET(t, BTF_EOF | BTF_RINMEM) && 16150995Sbostic t->bt_irec(t, MAX_REC_NUMBER) == RET_ERROR) 16250995Sbostic goto err; 16350995Sbostic return (dbp); 16450995Sbostic 16556703Sbostic einval: errno = EINVAL; 16658751Sbostic err: sverrno = errno; 16758751Sbostic if (dbp != NULL) 16858751Sbostic (void)__bt_close(dbp); 16956757Sbostic if (fname != NULL) 17056757Sbostic (void)close(rfd); 17158751Sbostic errno = sverrno; 17250995Sbostic return (NULL); 17350995Sbostic } 174