| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
#if defined(LIBC_SCCS) && !defined(lint) |
| 38 |
static char sccsid[] = "@(#)rec_open.c 8.10 (Berkeley) 9/1/94"; |
| 39 |
#endif |
| 40 |
|
| 41 |
#include <sys/types.h> |
| 42 |
#include <sys/mman.h> |
| 43 |
#include <sys/stat.h> |
| 44 |
|
| 45 |
#include <errno.h> |
| 46 |
#include <fcntl.h> |
| 47 |
#include <limits.h> |
| 48 |
#include <stddef.h> |
| 49 |
#include <stdio.h> |
| 50 |
#include <unistd.h> |
| 51 |
|
| 52 |
#include "../include/db.h" |
| 53 |
#include "recno.h" |
| 54 |
|
| 55 |
DB * |
| 56 |
__rec_open(fname, flags, mode, openinfo, dflags) |
| 57 |
const char *fname; |
| 58 |
int flags, mode, dflags; |
| 59 |
const RECNOINFO *openinfo; |
| 60 |
{ |
| 61 |
BTREE *t; |
| 62 |
BTREEINFO btopeninfo; |
| 63 |
DB *dbp; |
| 64 |
PAGE *h; |
| 65 |
struct stat sb; |
| 66 |
int rfd = 0, sverrno; |
| 67 |
|
| 68 |
|
| 69 |
if (fname != NULL && (rfd = open(fname, flags, mode)) < 0) |
| 70 |
return (NULL); |
| 71 |
|
| 72 |
|
| 73 |
dbp = NULL; |
| 74 |
if (openinfo) { |
| 75 |
if (openinfo->flags & ~(R_FIXEDLEN | R_NOKEY | R_SNAPSHOT)) |
| 76 |
goto einval; |
| 77 |
btopeninfo.flags = 0; |
| 78 |
btopeninfo.cachesize = openinfo->cachesize; |
| 79 |
btopeninfo.maxkeypage = 0; |
| 80 |
btopeninfo.minkeypage = 0; |
| 81 |
btopeninfo.psize = openinfo->psize; |
| 82 |
btopeninfo.compare = NULL; |
| 83 |
btopeninfo.prefix = NULL; |
| 84 |
btopeninfo.lorder = openinfo->lorder; |
| 85 |
dbp = __bt_open(openinfo->bfname, |
| 86 |
O_RDWR, S_IRUSR | S_IWUSR, &btopeninfo, dflags); |
| 87 |
} else |
| 88 |
dbp = __bt_open(NULL, O_RDWR, S_IRUSR | S_IWUSR, NULL, dflags); |
| 89 |
if (dbp == NULL) |
| 90 |
goto err; |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
t = dbp->internal; |
| 99 |
if (openinfo) { |
| 100 |
if (openinfo->flags & R_FIXEDLEN) { |
| 101 |
F_SET(t, R_FIXLEN); |
| 102 |
t->bt_reclen = openinfo->reclen; |
| 103 |
if (t->bt_reclen == 0) |
| 104 |
goto einval; |
| 105 |
} |
| 106 |
t->bt_bval = openinfo->bval; |
| 107 |
} else |
| 108 |
t->bt_bval = '\n'; |
| 109 |
|
| 110 |
F_SET(t, R_RECNO); |
| 111 |
if (fname == NULL) |
| 112 |
F_SET(t, R_EOF | R_INMEM); |
| 113 |
else |
| 114 |
t->bt_rfd = rfd; |
| 115 |
|
| 116 |
if (fname != NULL) { |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
errno = 0; |
| 123 |
if (lseek(rfd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE) { |
| 124 |
switch (flags & O_ACCMODE) { |
| 125 |
case O_RDONLY: |
| 126 |
F_SET(t, R_RDONLY); |
| 127 |
break; |
| 128 |
default: |
| 129 |
goto einval; |
| 130 |
} |
| 131 |
slow: if ((t->bt_rfp = fdopen(rfd, "r")) == NULL) |
| 132 |
goto err; |
| 133 |
F_SET(t, R_CLOSEFP); |
| 134 |
t->bt_irec = |
| 135 |
F_ISSET(t, R_FIXLEN) ? __rec_fpipe : __rec_vpipe; |
| 136 |
} else { |
| 137 |
switch (flags & O_ACCMODE) { |
| 138 |
case O_RDONLY: |
| 139 |
F_SET(t, R_RDONLY); |
| 140 |
break; |
| 141 |
case O_RDWR: |
| 142 |
break; |
| 143 |
default: |
| 144 |
goto einval; |
| 145 |
} |
| 146 |
|
| 147 |
if (fstat(rfd, &sb)) |
| 148 |
goto err; |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
if (sb.st_size == 0) |
| 159 |
F_SET(t, R_EOF); |
| 160 |
else { |
| 161 |
#ifdef MMAP_NOT_AVAILABLE |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
t->bt_msize = sb.st_size; |
| 170 |
if ((t->bt_smap = mmap(NULL, t->bt_msize, |
| 171 |
PROT_READ, MAP_PRIVATE, rfd, |
| 172 |
(off_t)0)) == MAP_FAILED |
| 173 |
goto slow; |
| 174 |
t->bt_cmap = t->bt_smap; |
| 175 |
t->bt_emap = t->bt_smap + sb.st_size; |
| 176 |
t->bt_irec = F_ISSET(t, R_FIXLEN) ? |
| 177 |
__rec_fmap : __rec_vmap; |
| 178 |
F_SET(t, R_MEMMAPPED); |
| 179 |
#else |
| 180 |
goto slow; |
| 181 |
#endif |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
dbp->close = __rec_close; |
| 188 |
dbp->del = __rec_delete; |
| 189 |
dbp->fd = __rec_fd; |
| 190 |
dbp->get = __rec_get; |
| 191 |
dbp->put = __rec_put; |
| 192 |
dbp->seq = __rec_seq; |
| 193 |
dbp->sync = __rec_sync; |
| 194 |
|
| 195 |
|
| 196 |
if ((h = mpool_get(t->bt_mp, P_ROOT, 0)) == NULL) |
| 197 |
goto err; |
| 198 |
if ((h->flags & P_TYPE) == P_BLEAF) { |
| 199 |
F_CLR(h, P_TYPE); |
| 200 |
F_SET(h, P_RLEAF); |
| 201 |
mpool_put(t->bt_mp, h, MPOOL_DIRTY); |
| 202 |
} else |
| 203 |
mpool_put(t->bt_mp, h, 0); |
| 204 |
|
| 205 |
if (openinfo && openinfo->flags & R_SNAPSHOT && |
| 206 |
!F_ISSET(t, R_EOF | R_INMEM) && |
| 207 |
t->bt_irec(t, MAX_REC_NUMBER) == RET_ERROR) |
| 208 |
goto err; |
| 209 |
return (dbp); |
| 210 |
|
| 211 |
einval: errno = EINVAL; |
| 212 |
err: sverrno = errno; |
| 213 |
if (dbp != NULL) |
| 214 |
(void)__bt_close(dbp); |
| 215 |
if (fname != NULL) |
| 216 |
(void)close(rfd); |
| 217 |
errno = sverrno; |
| 218 |
return (NULL); |
| 219 |
} |
| 220 |
|
| 221 |
int |
| 222 |
__rec_fd(dbp) |
| 223 |
const DB *dbp; |
| 224 |
{ |
| 225 |
BTREE *t; |
| 226 |
|
| 227 |
t = dbp->internal; |
| 228 |
|
| 229 |
|
| 230 |
if (t->bt_pinned != NULL) { |
| 231 |
mpool_put(t->bt_mp, t->bt_pinned, 0); |
| 232 |
t->bt_pinned = NULL; |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
if (F_ISSET(t, R_INMEM)) { |
| 237 |
errno = ENOENT; |
| 238 |
return (-1); |
| 239 |
} |
| 240 |
return (t->bt_rfd); |
| 241 |
} |