1 /* $NetBSD: rec_open.c,v 1.15 2007/02/03 23:46:09 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 1990, 1993, 1994 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Mike Olson. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #include <sys/cdefs.h> 36 #if defined(LIBC_SCCS) && !defined(lint) 37 #if 0 38 static char sccsid[] = "@(#)rec_open.c 8.10 (Berkeley) 9/1/94"; 39 #else 40 __RCSID("$NetBSD: rec_open.c,v 1.15 2007/02/03 23:46:09 christos Exp $"); 41 #endif 42 #endif /* LIBC_SCCS and not lint */ 43 44 #include "namespace.h" 45 #include <sys/types.h> 46 #include <sys/mman.h> 47 #include <sys/stat.h> 48 49 #include <assert.h> 50 #include <errno.h> 51 #include <fcntl.h> 52 #include <limits.h> 53 #include <stddef.h> 54 #include <stdio.h> 55 #include <unistd.h> 56 57 #include <db.h> 58 #include "recno.h" 59 60 DB * 61 __rec_open(const char *fname, int flags, mode_t mode, const RECNOINFO *openinfo, 62 int dflags) 63 { 64 BTREE *t; 65 BTREEINFO btopeninfo; 66 DB *dbp; 67 PAGE *h; 68 struct stat sb; 69 int rfd = -1; /* pacify gcc */ 70 int sverrno; 71 72 dbp = NULL; 73 /* Open the user's file -- if this fails, we're done. */ 74 if (fname != NULL) { 75 if ((rfd = open(fname, flags, mode)) == -1) 76 return (NULL); 77 if (fcntl(rfd, F_SETFD, FD_CLOEXEC) == -1) 78 goto err; 79 } 80 81 /* Create a btree in memory (backed by disk). */ 82 if (openinfo) { 83 if (openinfo->flags & ~(R_FIXEDLEN | R_NOKEY | R_SNAPSHOT)) 84 goto einval; 85 btopeninfo.flags = 0; 86 btopeninfo.cachesize = openinfo->cachesize; 87 btopeninfo.maxkeypage = 0; 88 btopeninfo.minkeypage = 0; 89 btopeninfo.psize = openinfo->psize; 90 btopeninfo.compare = NULL; 91 btopeninfo.prefix = NULL; 92 btopeninfo.lorder = openinfo->lorder; 93 dbp = __bt_open(openinfo->bfname, 94 O_RDWR, S_IRUSR | S_IWUSR, &btopeninfo, dflags); 95 } else 96 dbp = __bt_open(NULL, O_RDWR, S_IRUSR | S_IWUSR, NULL, dflags); 97 if (dbp == NULL) 98 goto err; 99 100 /* 101 * Some fields in the tree structure are recno specific. Fill them 102 * in and make the btree structure look like a recno structure. We 103 * don't change the bt_ovflsize value, it's close enough and slightly 104 * bigger. 105 */ 106 t = dbp->internal; 107 if (openinfo) { 108 if (openinfo->flags & R_FIXEDLEN) { 109 F_SET(t, R_FIXLEN); 110 t->bt_reclen = openinfo->reclen; 111 if (t->bt_reclen == 0) 112 goto einval; 113 } 114 t->bt_bval = openinfo->bval; 115 } else 116 t->bt_bval = '\n'; 117 118 F_SET(t, R_RECNO); 119 if (fname == NULL) 120 F_SET(t, R_EOF | R_INMEM); 121 else 122 t->bt_rfd = rfd; 123 124 if (fname != NULL) { 125 /* 126 * In 4.4BSD, stat(2) returns true for ISSOCK on pipes. 127 * Unfortunately, that's not portable, so we use lseek 128 * and check the errno values. 129 */ 130 errno = 0; 131 if (lseek(rfd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE) { 132 switch (flags & O_ACCMODE) { 133 case O_RDONLY: 134 F_SET(t, R_RDONLY); 135 break; 136 default: 137 goto einval; 138 } 139 slow: if ((t->bt_rfp = fdopen(rfd, "r")) == NULL) 140 goto err; 141 F_SET(t, R_CLOSEFP); 142 t->bt_irec = 143 F_ISSET(t, R_FIXLEN) ? __rec_fpipe : __rec_vpipe; 144 } else { 145 switch (flags & O_ACCMODE) { 146 case O_RDONLY: 147 F_SET(t, R_RDONLY); 148 break; 149 case O_RDWR: 150 break; 151 default: 152 goto einval; 153 } 154 155 if (fstat(rfd, &sb)) 156 goto err; 157 /* 158 * Kluge -- we'd like to test to see if the file is too 159 * big to mmap. Since, we don't know what size or type 160 * off_t's or size_t's are, what the largest unsigned 161 * integral type is, or what random insanity the local 162 * C compiler will perpetrate, doing the comparison in 163 * a portable way is flatly impossible. Hope that mmap 164 * fails if the file is too large. 165 */ 166 if (sb.st_size == 0) 167 F_SET(t, R_EOF); 168 else { 169 #ifdef MMAP_NOT_AVAILABLE 170 /* 171 * XXX 172 * Mmap doesn't work correctly on many current 173 * systems. In particular, it can fail subtly, 174 * with cache coherency problems. Don't use it 175 * for now. 176 */ 177 t->bt_msize = sb.st_size; 178 if ((t->bt_smap = mmap(NULL, t->bt_msize, 179 PROT_READ, MAP_FILE | MAP_PRIVATE, rfd, 180 (off_t)0)) == (caddr_t)-1) 181 goto slow; 182 t->bt_cmap = t->bt_smap; 183 t->bt_emap = t->bt_smap + sb.st_size; 184 t->bt_irec = F_ISSET(t, R_FIXLEN) ? 185 __rec_fmap : __rec_vmap; 186 F_SET(t, R_MEMMAPPED); 187 #else 188 goto slow; 189 #endif 190 } 191 } 192 } 193 194 /* Use the recno routines. */ 195 dbp->close = __rec_close; 196 dbp->del = __rec_delete; 197 dbp->fd = __rec_fd; 198 dbp->get = __rec_get; 199 dbp->put = __rec_put; 200 dbp->seq = __rec_seq; 201 dbp->sync = __rec_sync; 202 203 /* If the root page was created, reset the flags. */ 204 if ((h = mpool_get(t->bt_mp, P_ROOT, 0)) == NULL) 205 goto err; 206 if ((h->flags & P_TYPE) == P_BLEAF) { 207 F_CLR(h, P_TYPE); 208 F_SET(h, P_RLEAF); 209 mpool_put(t->bt_mp, h, MPOOL_DIRTY); 210 } else 211 mpool_put(t->bt_mp, h, 0); 212 213 if (openinfo && openinfo->flags & R_SNAPSHOT && 214 !F_ISSET(t, R_EOF | R_INMEM) && 215 t->bt_irec(t, MAX_REC_NUMBER) == RET_ERROR) 216 goto err; 217 return (dbp); 218 219 einval: errno = EINVAL; 220 err: sverrno = errno; 221 if (dbp != NULL) 222 (void)__bt_close(dbp); 223 if (fname != NULL) 224 (void)close(rfd); 225 errno = sverrno; 226 return (NULL); 227 } 228 229 int 230 __rec_fd(const DB *dbp) 231 { 232 BTREE *t; 233 234 t = dbp->internal; 235 236 /* Toss any page pinned across calls. */ 237 if (t->bt_pinned != NULL) { 238 mpool_put(t->bt_mp, t->bt_pinned, 0); 239 t->bt_pinned = NULL; 240 } 241 242 /* In-memory database can't have a file descriptor. */ 243 if (F_ISSET(t, R_INMEM)) { 244 errno = ENOENT; 245 return (-1); 246 } 247 return (t->bt_rfd); 248 } 249