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*54292Sbostic static char sccsid[] = "@(#)rec_open.c 5.4 (Berkeley) 06/23/92"; 1350995Sbostic #endif /* LIBC_SCCS and not lint */ 1450995Sbostic 1550995Sbostic #include <sys/types.h> 1650995Sbostic #include <sys/mman.h> 1750995Sbostic #include <sys/stat.h> 1850995Sbostic #include <fcntl.h> 1950995Sbostic #include <errno.h> 2050995Sbostic #include <limits.h> 2150995Sbostic #include <db.h> 2250995Sbostic #include <unistd.h> 2350995Sbostic #include <stdio.h> 2450995Sbostic #include <stddef.h> 2551090Sbostic #include "recno.h" 2650995Sbostic 2750995Sbostic DB * 2850995Sbostic __rec_open(fname, flags, mode, openinfo) 2950995Sbostic const char *fname; 3050995Sbostic int flags, mode; 3150995Sbostic const RECNOINFO *openinfo; 3250995Sbostic { 3350995Sbostic BTREE *t; 3450995Sbostic BTREEINFO btopeninfo; 3550995Sbostic DB *dbp; 3650995Sbostic PAGE *h; 3750995Sbostic struct stat sb; 3850995Sbostic int rfd; 3950995Sbostic 4050995Sbostic /* Open the user's file -- if this fails, we're done. */ 4150995Sbostic if ((rfd = open(fname, flags, mode)) < 0) 4250995Sbostic return (NULL); 4350995Sbostic 4450995Sbostic /* Create a btree in memory (backed by disk). */ 4550995Sbostic if (openinfo) { 4651090Sbostic if (openinfo->flags & ~(R_FIXEDLEN|R_NOKEY|R_SNAPSHOT)) { 4751090Sbostic errno = EINVAL; 4851090Sbostic goto err; 4951090Sbostic } 5050995Sbostic btopeninfo.flags = 0; 5150995Sbostic btopeninfo.cachesize = openinfo->cachesize; 5250995Sbostic btopeninfo.psize = 0; 5350995Sbostic btopeninfo.compare = NULL; 5450995Sbostic btopeninfo.lorder = openinfo->lorder; 5550995Sbostic dbp = __bt_open(NULL, O_RDWR, S_IRUSR | S_IWUSR, &btopeninfo); 5650995Sbostic } else 5750995Sbostic dbp = __bt_open(NULL, O_RDWR, S_IRUSR | S_IWUSR, NULL); 5851090Sbostic if (dbp == NULL) 5951090Sbostic goto err; 6050995Sbostic 6150995Sbostic /* 6250995Sbostic * Some fields in the tree structure are recno specific. Fill them 6351090Sbostic * in and make the btree structure look like a recno structure. We 6451090Sbostic * don't change the bt_ovflsize value, it's close enough and slightly 6551090Sbostic * bigger. 6650995Sbostic */ 6750995Sbostic t = dbp->internal; 6850995Sbostic if (openinfo) { 6950995Sbostic if (openinfo->flags & R_FIXEDLEN) 7050995Sbostic t->bt_flags |= BTF_FIXEDLEN; 7150995Sbostic 7250995Sbostic t->bt_reclen = openinfo->reclen; 7350995Sbostic if (t->bt_reclen == 0) { 7450995Sbostic errno = EINVAL; 7550995Sbostic goto err; 7650995Sbostic } 7750995Sbostic 7850995Sbostic t->bt_bval = openinfo->bval; 7950995Sbostic } else 8050995Sbostic t->bt_bval = '\n'; 8150995Sbostic 8250995Sbostic t->bt_flags = BTF_RECNO; 8354286Sbostic t->bt_reof = 0; 8450995Sbostic 8550995Sbostic /* 8654286Sbostic * In 4.4BSD stat(2) returns true for ISSOCK on pipes. Until 8754286Sbostic * then, this is fairly close. Pipes are read-only. 8854286Sbostic */ 8950995Sbostic if (lseek(rfd, 0L, SEEK_CUR) == -1 && errno == ESPIPE) { 9050995Sbostic SET(t, BTF_RDONLY); 9150995Sbostic if ((t->bt_rfp = fdopen(rfd, "r")) == NULL) 9250995Sbostic goto err; 9350995Sbostic t->bt_irec = ISSET(t, BTF_FIXEDLEN) ? __rec_fpipe : __rec_vpipe; 9450995Sbostic } else { 9550995Sbostic if (fstat(rfd, &sb)) 9650995Sbostic goto err; 9750995Sbostic if (!(flags & (O_RDWR | O_WRONLY))) 9850995Sbostic SET(t, BTF_RDONLY); 99*54292Sbostic if (sb.st_size > UINT_MAX) { 10054286Sbostic errno = EFBIG; 10150995Sbostic goto err; 10254286Sbostic } 103*54292Sbostic if ((t->bt_smap = mmap(NULL, 104*54292Sbostic (size_t)sb.st_size, PROT_READ, 0, rfd, (off_t)0)) == NULL) 10554286Sbostic goto err; 10650995Sbostic t->bt_emap = t->bt_smap + sb.st_size; 10750995Sbostic t->bt_rfd = rfd; 10854286Sbostic t->bt_rfp = NULL; 10950995Sbostic t->bt_irec = ISSET(t, BTF_FIXEDLEN) ? __rec_fmap : __rec_vmap; 11050995Sbostic } 11150995Sbostic 11250995Sbostic /* Use the recno routines. */ 11350995Sbostic dbp->close = __rec_close; 11450995Sbostic dbp->del = __rec_delete; 11550995Sbostic dbp->get = __rec_get; 11650995Sbostic dbp->put = __rec_put; 11750995Sbostic dbp->seq = __rec_seq; 11850995Sbostic dbp->sync = __rec_sync; 11950995Sbostic 12050995Sbostic /* If the root page was created, reset the flags. */ 12150995Sbostic if ((h = mpool_get(t->bt_mp, P_ROOT, 0)) == NULL) 12250995Sbostic goto err; 12350995Sbostic if ((h->flags & P_TYPE) == P_BLEAF) { 12450995Sbostic h->flags = h->flags & ~P_TYPE | P_RLEAF; 12550995Sbostic mpool_put(t->bt_mp, h, MPOOL_DIRTY); 12650995Sbostic } else 12750995Sbostic mpool_put(t->bt_mp, h, 0); 12850995Sbostic 12950995Sbostic if (openinfo && openinfo->flags & R_SNAPSHOT && 13050995Sbostic t->bt_irec(t, MAX_REC_NUMBER) == RET_ERROR) 13150995Sbostic goto err; 13250995Sbostic return (dbp); 13350995Sbostic 13451090Sbostic err: if (dbp) 13551090Sbostic __bt_close(dbp); 13650995Sbostic (void)close(rfd); 13750995Sbostic return (NULL); 13850995Sbostic } 139