1 /* $NetBSD: rec_open.c,v 1.17 2008/09/11 12:58:00 joerg 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 #if HAVE_NBTOOL_CONFIG_H 36 #include "nbtool_config.h" 37 #endif 38 39 #include <sys/cdefs.h> 40 #ifndef __minix 41 __RCSID("$NetBSD: rec_open.c,v 1.17 2008/09/11 12:58:00 joerg Exp $"); 42 #endif 43 44 #ifndef __minix 45 #include "namespace.h" 46 #endif 47 #include <sys/types.h> 48 #include <sys/mman.h> 49 #include <sys/stat.h> 50 51 #include <assert.h> 52 #include <errno.h> 53 #include <fcntl.h> 54 #include <limits.h> 55 #include <stddef.h> 56 #include <stdio.h> 57 #include <unistd.h> 58 59 #include <db.h> 60 #include "recno.h" 61 62 DB * 63 __rec_open(const char *fname, int flags, mode_t mode, const RECNOINFO *openinfo, 64 int dflags) 65 { 66 BTREE *t; 67 BTREEINFO btopeninfo; 68 DB *dbp; 69 PAGE *h; 70 struct stat sb; 71 int rfd = -1; /* pacify gcc */ 72 int sverrno; 73 74 dbp = NULL; 75 /* Open the user's file -- if this fails, we're done. */ 76 if (fname != NULL) { 77 if ((rfd = open(fname, flags, mode)) == -1) 78 return (NULL); 79 if (fcntl(rfd, F_SETFD, FD_CLOEXEC) == -1) 80 goto err; 81 } 82 83 /* Create a btree in memory (backed by disk). */ 84 if (openinfo) { 85 if (openinfo->flags & ~(R_FIXEDLEN | R_NOKEY | R_SNAPSHOT)) 86 goto einval; 87 btopeninfo.flags = 0; 88 btopeninfo.cachesize = openinfo->cachesize; 89 btopeninfo.maxkeypage = 0; 90 btopeninfo.minkeypage = 0; 91 btopeninfo.psize = openinfo->psize; 92 btopeninfo.compare = NULL; 93 btopeninfo.prefix = NULL; 94 btopeninfo.lorder = openinfo->lorder; 95 dbp = __bt_open(openinfo->bfname, 96 O_RDWR, S_IRUSR | S_IWUSR, &btopeninfo, dflags); 97 } else 98 dbp = __bt_open(NULL, O_RDWR, S_IRUSR | S_IWUSR, NULL, dflags); 99 if (dbp == NULL) 100 goto err; 101 102 /* 103 * Some fields in the tree structure are recno specific. Fill them 104 * in and make the btree structure look like a recno structure. We 105 * don't change the bt_ovflsize value, it's close enough and slightly 106 * bigger. 107 */ 108 t = dbp->internal; 109 if (openinfo) { 110 if (openinfo->flags & R_FIXEDLEN) { 111 F_SET(t, R_FIXLEN); 112 t->bt_reclen = openinfo->reclen; 113 if (t->bt_reclen == 0) 114 goto einval; 115 } 116 t->bt_bval = openinfo->bval; 117 } else 118 t->bt_bval = '\n'; 119 120 F_SET(t, R_RECNO); 121 if (fname == NULL) 122 F_SET(t, R_EOF | R_INMEM); 123 else 124 t->bt_rfd = rfd; 125 126 if (fname != NULL) { 127 /* 128 * In 4.4BSD, stat(2) returns true for ISSOCK on pipes. 129 * Unfortunately, that's not portable, so we use lseek 130 * and check the errno values. 131 */ 132 errno = 0; 133 if (lseek(rfd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE) { 134 switch (flags & O_ACCMODE) { 135 case O_RDONLY: 136 F_SET(t, R_RDONLY); 137 break; 138 default: 139 goto einval; 140 } 141 slow: if ((t->bt_rfp = fdopen(rfd, "r")) == NULL) 142 goto err; 143 F_SET(t, R_CLOSEFP); 144 t->bt_irec = 145 F_ISSET(t, R_FIXLEN) ? __rec_fpipe : __rec_vpipe; 146 } else { 147 switch (flags & O_ACCMODE) { 148 case O_RDONLY: 149 F_SET(t, R_RDONLY); 150 break; 151 case O_RDWR: 152 break; 153 default: 154 goto einval; 155 } 156 157 if (fstat(rfd, &sb)) 158 goto err; 159 /* 160 * Kluge -- we'd like to test to see if the file is too 161 * big to mmap. Since, we don't know what size or type 162 * off_t's or size_t's are, what the largest unsigned 163 * integral type is, or what random insanity the local 164 * C compiler will perpetrate, doing the comparison in 165 * a portable way is flatly impossible. Hope that mmap 166 * fails if the file is too large. 167 */ 168 if (sb.st_size == 0) 169 F_SET(t, R_EOF); 170 else { 171 #ifdef MMAP_NOT_AVAILABLE 172 /* 173 * XXX 174 * Mmap doesn't work correctly on many current 175 * systems. In particular, it can fail subtly, 176 * with cache coherency problems. Don't use it 177 * for now. 178 */ 179 t->bt_msize = sb.st_size; 180 if ((t->bt_smap = mmap(NULL, t->bt_msize, 181 PROT_READ, MAP_FILE | MAP_PRIVATE, rfd, 182 (off_t)0)) == (caddr_t)-1) 183 goto slow; 184 t->bt_cmap = t->bt_smap; 185 t->bt_emap = t->bt_smap + sb.st_size; 186 t->bt_irec = F_ISSET(t, R_FIXLEN) ? 187 __rec_fmap : __rec_vmap; 188 F_SET(t, R_MEMMAPPED); 189 #else 190 goto slow; 191 #endif 192 } 193 } 194 } 195 196 /* Use the recno routines. */ 197 dbp->close = __rec_close; 198 dbp->del = __rec_delete; 199 dbp->fd = __rec_fd; 200 dbp->get = __rec_get; 201 dbp->put = __rec_put; 202 dbp->seq = __rec_seq; 203 dbp->sync = __rec_sync; 204 205 /* If the root page was created, reset the flags. */ 206 if ((h = mpool_get(t->bt_mp, P_ROOT, 0)) == NULL) 207 goto err; 208 if ((h->flags & P_TYPE) == P_BLEAF) { 209 F_CLR(h, P_TYPE); 210 F_SET(h, P_RLEAF); 211 mpool_put(t->bt_mp, h, MPOOL_DIRTY); 212 } else 213 mpool_put(t->bt_mp, h, 0); 214 215 if (openinfo && openinfo->flags & R_SNAPSHOT && 216 !F_ISSET(t, R_EOF | R_INMEM) && 217 t->bt_irec(t, MAX_REC_NUMBER) == RET_ERROR) 218 goto err; 219 return (dbp); 220 221 einval: errno = EINVAL; 222 err: sverrno = errno; 223 if (dbp != NULL) 224 (void)__bt_close(dbp); 225 if (fname != NULL) 226 (void)close(rfd); 227 errno = sverrno; 228 return (NULL); 229 } 230 231 int 232 __rec_fd(const DB *dbp) 233 { 234 BTREE *t; 235 236 t = dbp->internal; 237 238 /* Toss any page pinned across calls. */ 239 if (t->bt_pinned != NULL) { 240 mpool_put(t->bt_mp, t->bt_pinned, 0); 241 t->bt_pinned = NULL; 242 } 243 244 /* In-memory database can't have a file descriptor. */ 245 if (F_ISSET(t, R_INMEM)) { 246 errno = ENOENT; 247 return (-1); 248 } 249 return (t->bt_rfd); 250 } 251