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