2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 1996, 1997, 1998
5 * Sleepycat Software. All rights reserved.
11 static const char sccsid
[] = "@(#)db_err.c 10.25 (Sleepycat) 5/2/98";
14 #ifndef NO_SYSTEM_INCLUDES
15 #include <sys/types.h>
27 #include "common_ext.h"
29 static int __db_keyempty
__P((const DB_ENV
*));
30 static int __db_rdonly
__P((const DB_ENV
*, const char *));
34 * Standard DB error routine.
36 * PUBLIC: #ifdef __STDC__
37 * PUBLIC: void __db_err __P((const DB_ENV *dbenv, const char *fmt, ...));
39 * PUBLIC: void __db_err();
44 __db_err(const DB_ENV
*dbenv
, const char *fmt
, ...)
46 __db_err(dbenv
, fmt
, va_alist
)
53 char errbuf
[2048]; /* XXX: END OF THE STACK DON'T TRUST SPRINTF. */
63 if (dbenv
->db_errcall
!= NULL
) {
64 (void)vsnprintf(errbuf
, sizeof(errbuf
), fmt
, ap
);
65 dbenv
->db_errcall(dbenv
->db_errpfx
, errbuf
);
67 if (dbenv
->db_errfile
!= NULL
) {
68 if (dbenv
->db_errpfx
!= NULL
)
69 (void)fprintf(dbenv
->db_errfile
, "%s: ",
71 (void)vfprintf(dbenv
->db_errfile
, fmt
, ap
);
72 (void)fprintf(dbenv
->db_errfile
, "\n");
73 (void)fflush(dbenv
->db_errfile
);
80 * Provide ANSI C prototypes for the panic functions. Some compilers, (e.g.,
81 * MS VC 4.2) get upset if they aren't here, even though the K&R declaration
82 * appears before the assignment in the __db__panic() call.
84 static int __db_ecursor
__P((DB
*, DB_TXN
*, DBC
**));
85 static int __db_edel
__P((DB
*, DB_TXN
*, DBT
*, u_int32_t
));
86 static int __db_efd
__P((DB
*, int *));
87 static int __db_egp
__P((DB
*, DB_TXN
*, DBT
*, DBT
*, u_int32_t
));
88 static int __db_estat
__P((DB
*, void *, void *(*)(size_t), u_int32_t
));
89 static int __db_esync
__P((DB
*, u_int32_t
));
93 * After-panic cursor routine.
110 * After-panic delete routine.
113 __db_edel(a
, b
, c
, d
)
129 * After-panic fd routine.
144 * After-panic get/put routine.
147 __db_egp(a
, b
, c
, d
, e
)
164 * After-panic stat routine.
167 __db_estat(a
, b
, c
, d
)
170 void *(*c
) __P((size_t));
183 * After-panic sync routine.
198 * Lock out the tree due to unrecoverable error.
200 * PUBLIC: int __db_panic __P((DB *));
208 * We should shut down all of the process's cursors, too.
210 * We should call mpool and have it shut down the file, so we get
211 * other processes sharing this file as well.
213 * Chaos reigns within.
214 * Reflect, repent, and reboot.
215 * Order shall return.
217 dbp
->cursor
= __db_ecursor
;
218 dbp
->del
= __db_edel
;
222 dbp
->stat
= __db_estat
;
223 dbp
->sync
= __db_esync
;
228 /* Check for invalid flags. */
229 #undef DB_CHECK_FLAGS
230 #define DB_CHECK_FLAGS(dbenv, name, flags, ok_flags) \
231 if ((flags) & ~(ok_flags)) \
232 return (__db_ferr(dbenv, name, 0));
233 /* Check for invalid flag combinations. */
234 #undef DB_CHECK_FCOMBO
235 #define DB_CHECK_FCOMBO(dbenv, name, flags, flag1, flag2) \
236 if ((flags) & (flag1) && (flags) & (flag2)) \
237 return (__db_ferr(dbenv, name, 1));
241 * General flags checking routine.
243 * PUBLIC: int __db_fchk __P((DB_ENV *, const char *, u_int32_t, u_int32_t));
246 __db_fchk(dbenv
, name
, flags
, ok_flags
)
249 u_int32_t flags
, ok_flags
;
251 DB_CHECK_FLAGS(dbenv
, name
, flags
, ok_flags
);
257 * General combination flags checking routine.
259 * PUBLIC: int __db_fcchk
260 * PUBLIC: __P((DB_ENV *, const char *, u_int32_t, u_int32_t, u_int32_t));
263 __db_fcchk(dbenv
, name
, flags
, flag1
, flag2
)
266 u_int32_t flags
, flag1
, flag2
;
268 DB_CHECK_FCOMBO(dbenv
, name
, flags
, flag1
, flag2
);
274 * Common cursor delete argument checking routine.
276 * PUBLIC: int __db_cdelchk __P((const DB *, u_int32_t, int, int));
279 __db_cdelchk(dbp
, flags
, isrdonly
, isvalid
)
282 int isrdonly
, isvalid
;
284 /* Check for changes to a read-only tree. */
286 return (__db_rdonly(dbp
->dbenv
, "c_del"));
288 /* Check for invalid dbc->c_del() function flags. */
289 DB_CHECK_FLAGS(dbp
->dbenv
, "c_del", flags
, 0);
292 * The cursor must be initialized, return -1 for an invalid cursor,
295 return (isvalid
? 0 : EINVAL
);
300 * Common cursor get argument checking routine.
302 * PUBLIC: int __db_cgetchk __P((const DB *, DBT *, DBT *, u_int32_t, int));
305 __db_cgetchk(dbp
, key
, data
, flags
, isvalid
)
311 int key_einval
, key_flags
;
313 key_flags
= key_einval
= 0;
315 /* Check for invalid dbc->c_get() function flags. */
325 key_einval
= key_flags
= 1;
331 if (!F_ISSET(dbp
, DB_BT_RECNUM
))
335 if (!F_ISSET(dbp
, DB_BT_RECNUM
))
337 key_einval
= key_flags
= 1;
340 err
: return (__db_ferr(dbp
->dbenv
, "c_get", 0));
343 /* Check for invalid key/data flags. */
345 DB_CHECK_FLAGS(dbp
->dbenv
, "key", key
->flags
,
346 DB_DBT_MALLOC
| DB_DBT_USERMEM
| DB_DBT_PARTIAL
);
347 DB_CHECK_FLAGS(dbp
->dbenv
, "data", data
->flags
,
348 DB_DBT_MALLOC
| DB_DBT_USERMEM
| DB_DBT_PARTIAL
);
350 /* Check dbt's for valid flags when multi-threaded. */
351 if (F_ISSET(dbp
, DB_AM_THREAD
)) {
352 if (!F_ISSET(data
, DB_DBT_USERMEM
| DB_DBT_MALLOC
))
353 return (__db_ferr(dbp
->dbenv
, "threaded data", 1));
355 !F_ISSET(key
, DB_DBT_USERMEM
| DB_DBT_MALLOC
))
356 return (__db_ferr(dbp
->dbenv
, "threaded key", 1));
359 /* Check for missing keys. */
360 if (key_einval
&& (key
->data
== NULL
|| key
->size
== 0))
361 return (__db_keyempty(dbp
->dbenv
));
364 * The cursor must be initialized for DB_CURRENT, return -1 for an
365 * invalid cursor, otherwise 0.
367 return (isvalid
|| flags
!= DB_CURRENT
? 0 : EINVAL
);
372 * Common cursor put argument checking routine.
374 * PUBLIC: int __db_cputchk __P((const DB *,
375 * PUBLIC: const DBT *, DBT *, u_int32_t, int, int));
378 __db_cputchk(dbp
, key
, data
, flags
, isrdonly
, isvalid
)
383 int isrdonly
, isvalid
;
385 int key_einval
, key_flags
;
387 /* Check for changes to a read-only tree. */
389 return (__db_rdonly(dbp
->dbenv
, "c_put"));
391 /* Check for invalid dbc->c_put() function flags. */
392 key_einval
= key_flags
= 0;
396 if (dbp
->type
== DB_RECNO
&& !F_ISSET(dbp
, DB_RE_RENUMBER
))
398 if (dbp
->type
!= DB_RECNO
&& !F_ISSET(dbp
, DB_AM_DUP
))
405 if (dbp
->type
== DB_RECNO
)
407 key_einval
= key_flags
= 1;
410 err
: return (__db_ferr(dbp
->dbenv
, "c_put", 0));
413 /* Check for invalid key/data flags. */
415 DB_CHECK_FLAGS(dbp
->dbenv
, "key", key
->flags
,
416 DB_DBT_MALLOC
| DB_DBT_USERMEM
| DB_DBT_PARTIAL
);
417 DB_CHECK_FLAGS(dbp
->dbenv
, "data", data
->flags
,
418 DB_DBT_MALLOC
| DB_DBT_USERMEM
| DB_DBT_PARTIAL
);
420 /* Check for missing keys. */
421 if (key_einval
&& (key
->data
== NULL
|| key
->size
== 0))
422 return (__db_keyempty(dbp
->dbenv
));
425 * The cursor must be initialized for anything other than DB_KEYFIRST
426 * and DB_KEYLAST, return -1 for an invalid cursor, otherwise 0.
429 (flags
!= DB_KEYFIRST
&& flags
!= DB_KEYLAST
) ? 0 : EINVAL
);
434 * Common delete argument checking routine.
436 * PUBLIC: int __db_delchk __P((const DB *, DBT *, u_int32_t, int));
439 __db_delchk(dbp
, key
, flags
, isrdonly
)
445 /* Check for changes to a read-only tree. */
447 return (__db_rdonly(dbp
->dbenv
, "delete"));
449 /* Check for invalid db->del() function flags. */
450 DB_CHECK_FLAGS(dbp
->dbenv
, "delete", flags
, 0);
452 /* Check for missing keys. */
453 if (key
->data
== NULL
|| key
->size
== 0)
454 return (__db_keyempty(dbp
->dbenv
));
461 * Common get argument checking routine.
463 * PUBLIC: int __db_getchk __P((const DB *, const DBT *, DBT *, u_int32_t));
466 __db_getchk(dbp
, key
, data
, flags
)
472 /* Check for invalid db->get() function flags. */
473 DB_CHECK_FLAGS(dbp
->dbenv
,
474 "get", flags
, F_ISSET(dbp
, DB_BT_RECNUM
) ? DB_SET_RECNO
: 0);
476 /* Check for invalid key/data flags. */
477 DB_CHECK_FLAGS(dbp
->dbenv
, "key", key
->flags
, 0);
478 DB_CHECK_FLAGS(dbp
->dbenv
, "data", data
->flags
,
479 DB_DBT_MALLOC
| DB_DBT_USERMEM
| DB_DBT_PARTIAL
);
480 DB_CHECK_FCOMBO(dbp
->dbenv
,
481 "data", data
->flags
, DB_DBT_MALLOC
, DB_DBT_USERMEM
);
482 if (F_ISSET(dbp
, DB_AM_THREAD
) &&
483 !F_ISSET(data
, DB_DBT_MALLOC
| DB_DBT_USERMEM
))
484 return (__db_ferr(dbp
->dbenv
, "threaded data", 1));
486 /* Check for missing keys. */
487 if (key
->data
== NULL
|| key
->size
== 0)
488 return (__db_keyempty(dbp
->dbenv
));
495 * Common put argument checking routine.
497 * PUBLIC: int __db_putchk
498 * PUBLIC: __P((const DB *, DBT *, const DBT *, u_int32_t, int, int));
501 __db_putchk(dbp
, key
, data
, flags
, isrdonly
, isdup
)
508 /* Check for changes to a read-only tree. */
510 return (__db_rdonly(dbp
->dbenv
, "put"));
512 /* Check for invalid db->put() function flags. */
513 DB_CHECK_FLAGS(dbp
->dbenv
, "put", flags
,
514 DB_NOOVERWRITE
| (dbp
->type
== DB_RECNO
? DB_APPEND
: 0));
516 /* Check for invalid key/data flags. */
517 DB_CHECK_FLAGS(dbp
->dbenv
, "key", key
->flags
, 0);
518 DB_CHECK_FLAGS(dbp
->dbenv
, "data", data
->flags
,
519 DB_DBT_MALLOC
| DB_DBT_USERMEM
| DB_DBT_PARTIAL
);
520 DB_CHECK_FCOMBO(dbp
->dbenv
,
521 "data", data
->flags
, DB_DBT_MALLOC
, DB_DBT_USERMEM
);
523 /* Check for missing keys. */
524 if (key
->data
== NULL
|| key
->size
== 0)
525 return (__db_keyempty(dbp
->dbenv
));
527 /* Check for partial puts in the presence of duplicates. */
528 if (isdup
&& F_ISSET(data
, DB_DBT_PARTIAL
)) {
530 "a partial put in the presence of duplicates requires a cursor operation");
539 * Common stat argument checking routine.
541 * PUBLIC: int __db_statchk __P((const DB *, u_int32_t));
544 __db_statchk(dbp
, flags
)
548 /* Check for invalid db->stat() function flags. */
549 DB_CHECK_FLAGS(dbp
->dbenv
, "stat", flags
, DB_RECORDCOUNT
);
551 if (LF_ISSET(DB_RECORDCOUNT
) &&
552 dbp
->type
== DB_BTREE
&& !F_ISSET(dbp
, DB_BT_RECNUM
))
553 return (__db_ferr(dbp
->dbenv
, "stat", 0));
560 * Common sync argument checking routine.
562 * PUBLIC: int __db_syncchk __P((const DB *, u_int32_t));
565 __db_syncchk(dbp
, flags
)
569 /* Check for invalid db->sync() function flags. */
570 DB_CHECK_FLAGS(dbp
->dbenv
, "sync", flags
, 0);
577 * Common flag errors.
579 * PUBLIC: int __db_ferr __P((const DB_ENV *, const char *, int));
582 __db_ferr(dbenv
, name
, iscombo
)
587 __db_err(dbenv
, "illegal flag %sspecified to %s",
588 iscombo
? "combination " : "", name
);
594 * Common readonly message.
597 __db_rdonly(dbenv
, name
)
601 __db_err(dbenv
, "%s: attempt to modify a read-only tree", name
);
607 * Common missing or empty key value message.
613 __db_err(dbenv
, "missing or empty key value specified");