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*54362Sbostic static char sccsid[] = "@(#)rec_open.c 5.5 (Berkeley) 06/24/92"; 1350995Sbostic #endif /* LIBC_SCCS and not lint */ 1450995Sbostic 1550995Sbostic #include <sys/types.h> 1650995Sbostic #include <sys/mman.h> 1750995Sbostic #include <sys/stat.h> 18*54362Sbostic 1950995Sbostic #include <fcntl.h> 2050995Sbostic #include <errno.h> 2150995Sbostic #include <limits.h> 2250995Sbostic #include <db.h> 2350995Sbostic #include <unistd.h> 2450995Sbostic #include <stdio.h> 2550995Sbostic #include <stddef.h> 2651090Sbostic #include "recno.h" 2750995Sbostic 2850995Sbostic DB * 2950995Sbostic __rec_open(fname, flags, mode, openinfo) 3050995Sbostic const char *fname; 3150995Sbostic int flags, mode; 3250995Sbostic const RECNOINFO *openinfo; 3350995Sbostic { 3450995Sbostic BTREE *t; 3550995Sbostic BTREEINFO btopeninfo; 3650995Sbostic DB *dbp; 3750995Sbostic PAGE *h; 3850995Sbostic struct stat sb; 3950995Sbostic int rfd; 4050995Sbostic 4150995Sbostic /* Open the user's file -- if this fails, we're done. */ 4250995Sbostic if ((rfd = open(fname, flags, mode)) < 0) 4350995Sbostic return (NULL); 4450995Sbostic 4550995Sbostic /* Create a btree in memory (backed by disk). */ 4650995Sbostic if (openinfo) { 4751090Sbostic if (openinfo->flags & ~(R_FIXEDLEN|R_NOKEY|R_SNAPSHOT)) { 4851090Sbostic errno = EINVAL; 4951090Sbostic goto err; 5051090Sbostic } 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) { 7050995Sbostic if (openinfo->flags & R_FIXEDLEN) 7150995Sbostic t->bt_flags |= BTF_FIXEDLEN; 7250995Sbostic 7350995Sbostic t->bt_reclen = openinfo->reclen; 7450995Sbostic if (t->bt_reclen == 0) { 7550995Sbostic errno = EINVAL; 7650995Sbostic goto err; 7750995Sbostic } 7850995Sbostic 7950995Sbostic t->bt_bval = openinfo->bval; 8050995Sbostic } else 8150995Sbostic t->bt_bval = '\n'; 8250995Sbostic 8350995Sbostic t->bt_flags = BTF_RECNO; 8454286Sbostic t->bt_reof = 0; 8550995Sbostic 8650995Sbostic /* 8754286Sbostic * In 4.4BSD stat(2) returns true for ISSOCK on pipes. Until 8854286Sbostic * then, this is fairly close. Pipes are read-only. 8954286Sbostic */ 9050995Sbostic if (lseek(rfd, 0L, SEEK_CUR) == -1 && errno == ESPIPE) { 9150995Sbostic SET(t, BTF_RDONLY); 9250995Sbostic if ((t->bt_rfp = fdopen(rfd, "r")) == NULL) 9350995Sbostic goto err; 9450995Sbostic t->bt_irec = ISSET(t, BTF_FIXEDLEN) ? __rec_fpipe : __rec_vpipe; 9550995Sbostic } else { 9650995Sbostic if (fstat(rfd, &sb)) 9750995Sbostic goto err; 9850995Sbostic if (!(flags & (O_RDWR | O_WRONLY))) 9950995Sbostic SET(t, BTF_RDONLY); 100*54362Sbostic if (sb.st_size > SIZE_T_MAX) { 10154286Sbostic errno = EFBIG; 10250995Sbostic goto err; 10354286Sbostic } 10454292Sbostic if ((t->bt_smap = mmap(NULL, 10554292Sbostic (size_t)sb.st_size, PROT_READ, 0, rfd, (off_t)0)) == NULL) 10654286Sbostic goto err; 10750995Sbostic t->bt_emap = t->bt_smap + sb.st_size; 10850995Sbostic t->bt_rfd = rfd; 10954286Sbostic t->bt_rfp = NULL; 11050995Sbostic t->bt_irec = ISSET(t, BTF_FIXEDLEN) ? __rec_fmap : __rec_vmap; 11150995Sbostic } 11250995Sbostic 11350995Sbostic /* Use the recno routines. */ 11450995Sbostic dbp->close = __rec_close; 11550995Sbostic dbp->del = __rec_delete; 11650995Sbostic dbp->get = __rec_get; 11750995Sbostic dbp->put = __rec_put; 11850995Sbostic dbp->seq = __rec_seq; 11950995Sbostic dbp->sync = __rec_sync; 12050995Sbostic 12150995Sbostic /* If the root page was created, reset the flags. */ 12250995Sbostic if ((h = mpool_get(t->bt_mp, P_ROOT, 0)) == NULL) 12350995Sbostic goto err; 12450995Sbostic if ((h->flags & P_TYPE) == P_BLEAF) { 12550995Sbostic h->flags = h->flags & ~P_TYPE | P_RLEAF; 12650995Sbostic mpool_put(t->bt_mp, h, MPOOL_DIRTY); 12750995Sbostic } else 12850995Sbostic mpool_put(t->bt_mp, h, 0); 12950995Sbostic 13050995Sbostic if (openinfo && openinfo->flags & R_SNAPSHOT && 13150995Sbostic t->bt_irec(t, MAX_REC_NUMBER) == RET_ERROR) 13250995Sbostic goto err; 13350995Sbostic return (dbp); 13450995Sbostic 13551090Sbostic err: if (dbp) 13651090Sbostic __bt_close(dbp); 13750995Sbostic (void)close(rfd); 13850995Sbostic return (NULL); 13950995Sbostic } 140