]>
git.ipfire.org Git - thirdparty/glibc.git/blob - db2/db185/db185.c
2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 1996, 1997
5 * Sleepycat Software. All rights reserved.
11 static const char copyright
[] =
12 "@(#) Copyright (c) 1997\n\
13 Sleepycat Software Inc. All rights reserved.\n";
14 static const char sccsid
[] = "@(#)db185.c 8.14 (Sleepycat) 10/25/97";
17 #ifndef NO_SYSTEM_INCLUDES
18 #include <sys/types.h>
29 #include "db185_int.h"
30 #include "common_ext.h"
32 static int db185_close
__P((DB185
*));
33 static int db185_del
__P((const DB185
*, const DBT185
*, u_int
));
34 static int db185_fd
__P((const DB185
*));
35 static int db185_get
__P((const DB185
*, const DBT185
*, DBT185
*, u_int
));
36 static int db185_put
__P((const DB185
*, DBT185
*, const DBT185
*, u_int
));
37 static int db185_seq
__P((const DB185
*, DBT185
*, DBT185
*, u_int
));
38 static int db185_sync
__P((const DB185
*, u_int
));
41 __dbopen(file
, oflags
, mode
, type
, openinfo
)
52 DB_INFO dbinfo
, *dbinfop
;
55 if ((db185p
= (DB185
*)__db_calloc(1, sizeof(DB185
))) == NULL
)
58 memset(&dbinfo
, 0, sizeof(dbinfo
));
62 * The DBTYPE enum wasn't initialized in DB 185, so it's off-by-one
66 case 0: /* DB_BTREE */
68 if ((bi
= openinfo
) != NULL
) {
70 if (bi
->flags
& ~R_DUP
)
72 if (bi
->flags
& R_DUP
)
73 dbinfop
->flags
|= DB_DUP
;
74 dbinfop
->db_cachesize
= bi
->cachesize
;
75 dbinfop
->bt_maxkey
= bi
->maxkeypage
;
76 dbinfop
->bt_minkey
= bi
->minkeypage
;
77 dbinfop
->db_pagesize
= bi
->psize
;
80 * Comparisons and prefix calls work because the DBT
81 * structures in 1.85 and 2.0 have the same initial
84 dbinfop
->bt_compare
= bi
->compare
;
85 dbinfop
->bt_prefix
= bi
->prefix
;
86 dbinfop
->db_lorder
= bi
->lorder
;
91 if ((hi
= openinfo
) != NULL
) {
93 dbinfop
->db_pagesize
= hi
->bsize
;
94 dbinfop
->h_ffactor
= hi
->ffactor
;
95 dbinfop
->h_nelem
= hi
->nelem
;
96 dbinfop
->db_cachesize
= hi
->cachesize
;
97 dbinfop
->h_hash
= hi
->hash
;
98 dbinfop
->db_lorder
= hi
->lorder
;
102 case 2: /* DB_RECNO */
106 /* DB 1.85 did renumbering by default. */
107 dbinfop
->flags
|= DB_RENUMBER
;
111 * The file name given to DB 1.85 recno is the name of the DB
112 * 2.0 backing file. If the file doesn't exist, create it if
113 * the user has the O_CREAT flag set, DB 1.85 did it for you,
114 * and DB 2.0 doesn't.
117 * Note, the file name in DB 1.85 was a const -- we don't do
118 * that in DB 2.0, so do that cast.
121 if (oflags
& O_CREAT
&& __db_exists(file
, NULL
) != 0)
122 (void)__os_close(__os_open(file
, oflags
, mode
));
123 dbinfop
->re_source
= (char *)file
;
127 if ((ri
= openinfo
) != NULL
) {
130 * We can't support the bfname field.
132 #define BFMSG "DB: DB 1.85's recno bfname field is not supported.\n"
133 if (ri
->bfname
!= NULL
) {
134 (void)__os_write(2, BFMSG
, sizeof(BFMSG
) - 1);
138 if (ri
->flags
& ~(R_FIXEDLEN
| R_NOKEY
| R_SNAPSHOT
))
140 if (ri
->flags
& R_FIXEDLEN
) {
141 dbinfop
->flags
|= DB_FIXEDLEN
;
143 dbinfop
->flags
|= DB_PAD
;
144 dbinfop
->re_pad
= ri
->bval
;
148 dbinfop
->flags
|= DB_DELIMITER
;
149 dbinfop
->re_delim
= ri
->bval
;
154 * We ignore the R_NOKEY flag, but that's okay, it was
155 * only an optimization that was never implemented.
158 if (ri
->flags
& R_SNAPSHOT
)
159 dbinfop
->flags
|= DB_SNAPSHOT
;
161 dbinfop
->db_cachesize
= ri
->cachesize
;
162 dbinfop
->db_pagesize
= ri
->psize
;
163 dbinfop
->db_lorder
= ri
->lorder
;
164 dbinfop
->re_len
= ri
->reclen
;
171 db185p
->close
= db185_close
;
172 db185p
->del
= db185_del
;
173 db185p
->fd
= db185_fd
;
174 db185p
->get
= db185_get
;
175 db185p
->put
= db185_put
;
176 db185p
->seq
= db185_seq
;
177 db185p
->sync
= db185_sync
;
181 * Store the returned pointer to the real DB 2.0 structure in the
182 * internal pointer. Ugly, but we're not going for pretty, here.
184 if ((__set_errno(db_open(file
,
185 type
, __db_oflags(oflags
), mode
, NULL
, dbinfop
, &dbp
))) != 0) {
190 /* Create the cursor used for sequential ops. */
191 if ((__set_errno(dbp
->cursor(dbp
, NULL
, &((DB185
*)db185p
)->dbc
)))
194 (void)dbp
->close(dbp
, 0);
196 __set_errno(s_errno
);
200 db185p
->internal
= dbp
;
203 einval
: __db_free(db185p
);
207 weak_alias (__dbopen
, dbopen
)
215 dbp
= (DB
*)db185p
->internal
;
217 __set_errno(dbp
->close(dbp
, 0));
221 return (errno
== 0 ? 0 : -1);
225 db185_del(db185p
, key185
, flags
)
227 const DBT185
*key185
;
233 dbp
= (DB
*)db185p
->internal
;
235 memset(&key
, 0, sizeof(key
));
236 key
.data
= key185
->data
;
237 key
.size
= key185
->size
;
239 if (flags
& ~R_CURSOR
)
241 if (flags
& R_CURSOR
)
242 __set_errno(db185p
->dbc
->c_del(db185p
->dbc
, 0));
244 __set_errno(dbp
->del(dbp
, NULL
, &key
, 0));
254 einval
: __set_errno(EINVAL
);
265 dbp
= (DB
*)db185p
->internal
;
267 return ((__set_errno(dbp
->fd(dbp
, &fd
))) == 0 ? fd
: -1);
271 db185_get(db185p
, key185
, data185
, flags
)
273 const DBT185
*key185
;
280 dbp
= (DB
*)db185p
->internal
;
282 memset(&key
, 0, sizeof(key
));
283 key
.data
= key185
->data
;
284 key
.size
= key185
->size
;
285 memset(&data
, 0, sizeof(data
));
286 data
.data
= data185
->data
;
287 data
.size
= data185
->size
;
292 switch (__set_errno(dbp
->get(dbp
, NULL
, &key
, &data
, 0))) {
294 data185
->data
= data
.data
;
295 data185
->size
= data
.size
;
302 einval
: __set_errno(EINVAL
);
307 db185_put(db185p
, key185
, data185
, flags
)
310 const DBT185
*data185
;
318 dbp
= (DB
*)db185p
->internal
;
320 memset(&key
, 0, sizeof(key
));
321 key
.data
= key185
->data
;
322 key
.size
= key185
->size
;
323 memset(&data
, 0, sizeof(data
));
324 data
.data
= data185
->data
;
325 data
.size
= data185
->size
;
329 __set_errno(dbp
->put(dbp
, NULL
, &key
, &data
, 0));
333 db185p
->dbc
->c_put(db185p
->dbc
, &key
, &data
, DB_CURRENT
));
337 if (dbp
->type
!= DB_RECNO
)
340 if ((__set_errno(dbp
->cursor(dbp
, NULL
, &dbcp_put
))) != 0)
343 dbcp_put
->c_get(dbcp_put
, &key
, &data
, DB_SET
))) != 0) {
345 (void)dbcp_put
->c_close(dbcp_put
);
346 __set_errno(s_errno
);
349 memset(&data
, 0, sizeof(data
));
350 data
.data
= data185
->data
;
351 data
.size
= data185
->size
;
352 __set_errno(dbcp_put
->c_put(dbcp_put
,
353 &key
, &data
, flags
== R_IAFTER
? DB_AFTER
: DB_BEFORE
));
355 (void)dbcp_put
->c_close(dbcp_put
);
356 __set_errno(s_errno
);
359 __set_errno(dbp
->put(dbp
, NULL
, &key
, &data
, DB_NOOVERWRITE
));
362 if (dbp
->type
!= DB_BTREE
&& dbp
->type
!= DB_RECNO
)
365 if ((__set_errno(dbp
->put(dbp
, NULL
, &key
, &data
, 0))) != 0)
367 __set_errno(db185p
->dbc
->c_get(db185p
->dbc
,
368 &key
, &data
, DB_SET_RANGE
));
376 key185
->data
= key
.data
;
377 key185
->size
= key
.size
;
384 einval
: __set_errno(EINVAL
);
389 db185_seq(db185p
, key185
, data185
, flags
)
391 DBT185
*key185
, *data185
;
397 dbp
= (DB
*)db185p
->internal
;
399 memset(&key
, 0, sizeof(key
));
400 key
.data
= key185
->data
;
401 key
.size
= key185
->size
;
402 memset(&data
, 0, sizeof(data
));
403 data
.data
= data185
->data
;
404 data
.size
= data185
->size
;
408 flags
= DB_SET_RANGE
;
414 if (dbp
->type
!= DB_BTREE
&& dbp
->type
!= DB_RECNO
)
422 if (dbp
->type
!= DB_BTREE
&& dbp
->type
!= DB_RECNO
)
429 switch (__set_errno(db185p
->dbc
->c_get(db185p
->dbc
,
430 &key
, &data
, flags
))) {
432 key185
->data
= key
.data
;
433 key185
->size
= key
.size
;
434 data185
->data
= data
.data
;
435 data185
->size
= data
.size
;
442 einval
: __set_errno(EINVAL
);
447 db185_sync(db185p
, flags
)
453 dbp
= (DB
*)db185p
->internal
;
461 * We can't support the R_RECNOSYNC flag.
463 #define RSMSG "DB: DB 1.85's R_RECNOSYNC sync flag is not supported.\n"
464 (void)__os_write(2, RSMSG
, sizeof(RSMSG
) - 1);
470 return ((__set_errno(dbp
->sync(dbp
, 0))) == 0 ? 0 : -1);
472 einval
: __set_errno(EINVAL
);