Asterisk - The Open Source Telephony Project GIT-master-f3e88d3
Functions
rec_open.c File Reference
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stddef.h>
#include <stdio.h>
#include <unistd.h>
#include "../include/db.h"
#include "recno.h"
Include dependency graph for rec_open.c:

Go to the source code of this file.

Functions

int __rec_fd (DB *dbp) const
 
DB__rec_open (char *fname, int flags, int mode, const RECNOINFO *openinfo, int dflags) const
 

Function Documentation

◆ __rec_fd()

int __rec_fd ( DB dbp) const

Definition at line 222 of file rec_open.c.

224{
225 BTREE *t;
226
227 t = dbp->internal;
228
229 /* Toss any page pinned across calls. */
230 if (t->bt_pinned != NULL) {
231 mpool_put(t->bt_mp, t->bt_pinned, 0);
232 t->bt_pinned = NULL;
233 }
234
235 /* In-memory database can't have a file descriptor. */
236 if (F_ISSET(t, R_INMEM)) {
237 errno = ENOENT;
238 return (-1);
239 }
240 return (t->bt_rfd);
241}
#define F_ISSET(p, f)
Definition: btree.h:42
#define R_INMEM
Definition: btree.h:381
static DB * dbp
Definition: hsearch.c:49
int errno
int mpool_put(MPOOL *mp, void *page, u_int flags)
Definition: mpool.c:251
#define NULL
Definition: resample.c:96
void * internal
Definition: db.h:137
Definition: btree.h:312
MPOOL * bt_mp
Definition: btree.h:313
PAGE * bt_pinned
Definition: btree.h:318
int bt_rfd
Definition: btree.h:353

References _btree::bt_mp, _btree::bt_pinned, _btree::bt_rfd, dbp, errno, F_ISSET, __db::internal, mpool_put(), NULL, and R_INMEM.

Referenced by __rec_open().

◆ __rec_open()

DB * __rec_open ( char *  fname,
int  flags,
int  mode,
const RECNOINFO openinfo,
int  dflags 
) const

Definition at line 56 of file rec_open.c.

60{
61 BTREE *t;
62 BTREEINFO btopeninfo;
63 DB *dbp;
64 PAGE *h;
65 struct stat sb;
66 int rfd = 0, sverrno;
67
68 /* Open the user's file -- if this fails, we're done. */
69 if (fname != NULL && (rfd = open(fname, flags, mode)) < 0)
70 return (NULL);
71
72 /* Create a btree in memory (backed by disk). */
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 * Some fields in the tree structure are recno specific. Fill them
94 * in and make the btree structure look like a recno structure. We
95 * don't change the bt_ovflsize value, it's close enough and slightly
96 * bigger.
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 * In 4.4BSD, stat(2) returns true for ISSOCK on pipes.
119 * Unfortunately, that's not portable, so we use lseek
120 * and check the errno values.
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 }
131slow: if ((t->bt_rfp = fdopen(rfd, "r")) == NULL)
132 goto err;
133 F_SET(t, R_CLOSEFP);
134 t->bt_irec =
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 * Kluge -- we'd like to test to see if the file is too
151 * big to mmap. Since, we don't know what size or type
152 * off_t's or size_t's are, what the largest unsigned
153 * integral type is, or what random insanity the local
154 * C compiler will perpetrate, doing the comparison in
155 * a portable way is flatly impossible. Hope that mmap
156 * fails if the file is too large.
157 */
158 if (sb.st_size == 0)
159 F_SET(t, R_EOF);
160 else {
161#ifdef MMAP_NOT_AVAILABLE
162 /*
163 * XXX
164 * Mmap doesn't work correctly on many current
165 * systems. In particular, it can fail subtly,
166 * with cache coherency problems. Don't use it
167 * for now.
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) ?
178 F_SET(t, R_MEMMAPPED);
179#else
180 goto slow;
181#endif
182 }
183 }
184 }
185
186 /* Use the recno routines. */
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 /* If the root page was created, reset the flags. */
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);
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
211einval: errno = EINVAL;
212err: 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}
dflags
Definition: app_dictate.c:63
int __bt_close(DB *dbp)
Definition: bt_close.c:64
DB * __bt_open(char *fname, int flags, int mode, const BTREEINFO *openinfo, int dflags) const
Definition: bt_open.c:90
#define P_RLEAF
Definition: btree.h:84
#define R_CLOSEFP
Definition: btree.h:377
#define R_RECNO
Definition: btree.h:375
#define R_MEMMAPPED
Definition: btree.h:380
#define P_TYPE
Definition: btree.h:85
#define R_RDONLY
Definition: btree.h:383
#define F_SET(p, f)
Definition: btree.h:40
#define F_CLR(p, f)
Definition: btree.h:41
#define R_EOF
Definition: btree.h:378
#define P_BLEAF
Definition: btree.h:81
#define R_FIXLEN
Definition: btree.h:379
#define P_ROOT
Definition: btree.h:65
#define R_SNAPSHOT
Definition: db.h:177
#define MAX_REC_NUMBER
Definition: db.h:81
#define RET_ERROR
Definition: db.h:51
#define R_NOKEY
Definition: db.h:176
#define R_FIXEDLEN
Definition: db.h:175
void * mpool_get(MPOOL *mp, pgno_t pgno, u_int flags)
Definition: mpool.c:165
#define MPOOL_DIRTY
Definition: mpool.h:61
int __rec_close(DB *dbp)
Definition: rec_close.c:60
int __rec_sync(DB *dbp, u_int flags) const
Definition: rec_close.c:107
int __rec_delete(DB *dbp, const DBT *key, u_int flags) const
Definition: rec_delete.c:64
int __rec_vmap(BTREE *t, recno_t top)
Definition: rec_get.c:285
int __rec_fpipe(BTREE *t, recno_t top)
Definition: rec_get.c:122
int __rec_get(DB *dbp, const DBT *key, DBT *data, u_int flags) const
Definition: rec_get.c:63
int __rec_vpipe(BTREE *t, recno_t top)
Definition: rec_get.c:178
int __rec_fmap(BTREE *t, recno_t top)
Definition: rec_get.c:235
int __rec_fd(DB *dbp) const
Definition: rec_open.c:222
int __rec_put(DB *dbp, DBT *key, const DBT *data, u_int flags) const
Definition: rec_put.c:62
int __rec_seq(DB *dbp, DBT *key, DBT *data, u_int flags) const
Definition: rec_seq.c:61
Definition: db.h:145
u_int cachesize
Definition: db.h:148
int minkeypage
Definition: db.h:150
u_int psize
Definition: db.h:151
int lorder
Definition: db.h:156
int maxkeypage
Definition: db.h:149
u_long flags
Definition: db.h:147
u_int cachesize
Definition: db.h:179
char * bfname
Definition: db.h:184
size_t reclen
Definition: db.h:182
u_int psize
Definition: db.h:180
u_char bval
Definition: db.h:183
int lorder
Definition: db.h:181
u_long flags
Definition: db.h:178
Definition: db.h:129
caddr_t bt_cmap
Definition: btree.h:355
caddr_t bt_emap
Definition: btree.h:357
u_char bt_bval
Definition: btree.h:362
size_t bt_msize
Definition: btree.h:358
size_t bt_reclen
Definition: btree.h:361
FILE * bt_rfp
Definition: btree.h:352
caddr_t bt_smap
Definition: btree.h:356
Definition: btree.h:75
u_int32_t flags
Definition: btree.h:87

References __bt_close(), __bt_open(), __rec_close(), __rec_delete(), __rec_fd(), __rec_fmap(), __rec_fpipe(), __rec_get(), __rec_put(), __rec_seq(), __rec_sync(), __rec_vmap(), __rec_vpipe(), RECNOINFO::bfname, _btree::bt_bval, _btree::bt_cmap, _btree::bt_emap, _btree::bt_mp, _btree::bt_msize, _btree::bt_reclen, _btree::bt_rfd, _btree::bt_rfp, _btree::bt_smap, RECNOINFO::bval, BTREEINFO::cachesize, RECNOINFO::cachesize, dbp, errno, F_CLR, F_ISSET, F_SET, _page::flags, BTREEINFO::flags, RECNOINFO::flags, __db::internal, BTREEINFO::lorder, RECNOINFO::lorder, MAX_REC_NUMBER, BTREEINFO::maxkeypage, BTREEINFO::minkeypage, MPOOL_DIRTY, mpool_get(), mpool_put(), NULL, P_BLEAF, P_RLEAF, P_ROOT, P_TYPE, BTREEINFO::psize, RECNOINFO::psize, R_CLOSEFP, R_EOF, R_FIXEDLEN, R_FIXLEN, R_INMEM, R_MEMMAPPED, R_NOKEY, R_RDONLY, R_RECNO, R_SNAPSHOT, RECNOINFO::reclen, and RET_ERROR.

Referenced by dbopen().