xref: /csrg-svn/lib/libc/db/recno/rec_open.c (revision 55279)
1 /*-
2  * Copyright (c) 1990 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Mike Olson.
7  *
8  * %sccs.include.redist.c%
9  */
10 
11 #if defined(LIBC_SCCS) && !defined(lint)
12 static char sccsid[] = "@(#)rec_open.c	5.6 (Berkeley) 07/15/92";
13 #endif /* LIBC_SCCS and not lint */
14 
15 #include <sys/types.h>
16 #include <sys/mman.h>
17 #include <sys/stat.h>
18 
19 #include <fcntl.h>
20 #include <errno.h>
21 #include <limits.h>
22 #include <db.h>
23 #include <unistd.h>
24 #include <stdio.h>
25 #include <stddef.h>
26 #include "recno.h"
27 
28 DB *
29 __rec_open(fname, flags, mode, openinfo)
30 	const char *fname;
31 	int flags, mode;
32 	const RECNOINFO *openinfo;
33 {
34 	BTREE *t;
35 	BTREEINFO btopeninfo;
36 	DB *dbp;
37 	PAGE *h;
38 	struct stat sb;
39 	int rfd;
40 
41 	/* Open the user's file -- if this fails, we're done. */
42 	if ((rfd = open(fname, flags, mode)) < 0)
43 		return (NULL);
44 
45 	/* Create a btree in memory (backed by disk). */
46 	if (openinfo) {
47 		if (openinfo->flags & ~(R_FIXEDLEN|R_NOKEY|R_SNAPSHOT)) {
48 			errno = EINVAL;
49 			goto err;
50 		}
51 		btopeninfo.flags = 0;
52 		btopeninfo.cachesize = openinfo->cachesize;
53 		btopeninfo.psize = 0;
54 		btopeninfo.compare = NULL;
55 		btopeninfo.lorder = openinfo->lorder;
56 		dbp = __bt_open(NULL, O_RDWR, S_IRUSR | S_IWUSR, &btopeninfo);
57 	} else
58 		dbp = __bt_open(NULL, O_RDWR, S_IRUSR | S_IWUSR, NULL);
59 	if (dbp == NULL)
60 		goto err;
61 
62 	/*
63 	 * Some fields in the tree structure are recno specific.  Fill them
64 	 * in and make the btree structure look like a recno structure.  We
65 	 * don't change the bt_ovflsize value, it's close enough and slightly
66 	 * bigger.
67 	 */
68 	t = dbp->internal;
69 	if (openinfo) {
70 		if (openinfo->flags & R_FIXEDLEN)
71 			t->bt_flags |= BTF_FIXEDLEN;
72 
73 		t->bt_reclen = openinfo->reclen;
74 		if (t->bt_reclen == 0) {
75 			errno = EINVAL;
76 			goto err;
77 		}
78 
79 		t->bt_bval = openinfo->bval;
80 	} else
81 		t->bt_bval = '\n';
82 
83 	t->bt_flags = BTF_RECNO;
84 	t->bt_reof = 0;
85 
86 	/*
87 	 * In 4.4BSD stat(2) returns true for ISSOCK on pipes.  Until
88 	 * then, this is fairly close.  Pipes are read-only.
89 	 */
90 	if (lseek(rfd, 0L, SEEK_CUR) == -1 && errno == ESPIPE) {
91 		SET(t, BTF_RDONLY);
92 		if ((t->bt_rfp = fdopen(rfd, "r")) == NULL)
93 			goto err;
94 		t->bt_irec = ISSET(t, BTF_FIXEDLEN) ? __rec_fpipe : __rec_vpipe;
95 	} else {
96 		if (fstat(rfd, &sb))
97 			goto err;
98 		if (!(flags & (O_RDWR | O_WRONLY)))
99 			SET(t, BTF_RDONLY);
100 		if (sb.st_size > SIZE_T_MAX) {
101 			errno = EFBIG;
102 			goto err;
103 		}
104 		if ((t->bt_smap = mmap(NULL,
105 		    (size_t)sb.st_size, PROT_READ, 0, rfd, (off_t)0)) == -1)
106 			goto err;
107 		t->bt_emap = t->bt_smap + sb.st_size;
108 		t->bt_rfd = rfd;
109 		t->bt_rfp = NULL;
110 		t->bt_irec = ISSET(t, BTF_FIXEDLEN) ? __rec_fmap : __rec_vmap;
111 	}
112 
113 	/* Use the recno routines. */
114 	dbp->close = __rec_close;
115 	dbp->del = __rec_delete;
116 	dbp->get = __rec_get;
117 	dbp->put = __rec_put;
118 	dbp->seq = __rec_seq;
119 	dbp->sync = __rec_sync;
120 
121 	/* If the root page was created, reset the flags. */
122 	if ((h = mpool_get(t->bt_mp, P_ROOT, 0)) == NULL)
123 		goto err;
124 	if ((h->flags & P_TYPE) == P_BLEAF) {
125 		h->flags = h->flags & ~P_TYPE | P_RLEAF;
126 		mpool_put(t->bt_mp, h, MPOOL_DIRTY);
127 	} else
128 		mpool_put(t->bt_mp, h, 0);
129 
130 	if (openinfo && openinfo->flags & R_SNAPSHOT &&
131 	    t->bt_irec(t, MAX_REC_NUMBER) == RET_ERROR)
132                 goto err;
133 	return (dbp);
134 
135 err:	if (dbp)
136 		__bt_close(dbp);
137 	(void)close(rfd);
138 	return (NULL);
139 }
140