-
- internal:
- /* Internal (memory-only) databases skip all the code above to
- * do with disk files, and resume here by releasing their
- * open lock and hooking into the active list. */
- if (tdb1_nest_unlock(tdb, TDB1_OPEN_LOCK, F_WRLCK) == -1) {
- goto fail;
- }
- return tdb;
-
- fail:
- { int save_errno = errno;
-
- if (!tdb)
- return NULL;
-
- if (tdb->file->map_ptr) {
- if (tdb->flags & TDB_INTERNAL)
- SAFE_FREE(tdb->file->map_ptr);
- else
- tdb1_munmap(tdb);
- }
- if (tdb->file->fd != -1)
- if (close(tdb->file->fd) != 0)
- tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
- "tdb1_open_ex: failed to close tdb->fd on error!");
- if (tdb->file) {
- SAFE_FREE(tdb->file->lockrecs);
- SAFE_FREE(tdb->file);
- }
- SAFE_FREE(tdb->name);
- SAFE_FREE(tdb);
- errno = save_errno;
- return NULL;
- }
-}
-
-/* Temporart wrapper for transition. */
-struct tdb_context *tdb1_open(const char *name, int tdb1_flags,
- int open_flags, mode_t mode,
- union tdb_attribute *attr)
-{
- struct tdb1_logging_context *log_ctx = NULL, log;
- tdb1_hash_func hash_fn = NULL;
- struct tdb_attribute_tdb1_hashsize *hsize = NULL;
-
- while (attr) {
- switch (attr->base.attr) {
- case TDB_ATTRIBUTE_HASH:
- hash_fn = attr->hash.fn;
- break;
- case TDB_ATTRIBUTE_LOG:
- log.log_fn = attr->log.fn;
- log.log_private = attr->log.data;
- log_ctx = &log;
- break;
- case TDB_ATTRIBUTE_TDB1_HASHSIZE:
- hsize = &attr->tdb1_hashsize;
- break;
- break;
- default:
- abort();
- }
- attr = attr->base.next;
- }
-
- if (hsize && !(open_flags & O_CREAT)) {
- if (log_ctx) {
- log_ctx->log_fn(NULL,
- TDB_ERR_EINVAL,
- TDB_LOG_USE_ERROR,
- "tdb_open: can only use"
- " TDB_ATTRIBUTE_TDB1_HASHSIZE when"
- " creating a tdb",
- log_ctx->log_private);
- }
- errno = EINVAL;
- return NULL;
- }
- return tdb1_open_ex(name, hsize ? hsize->hsize : TDB1_DEFAULT_HASH_SIZE,
- tdb1_flags, open_flags, mode,
- log_ctx, hash_fn);
-}
-
-/*
- * Set the maximum number of dead records per hash chain
- */
-
-void tdb1_set_max_dead(struct tdb_context *tdb, int max_dead)
-{
- tdb->tdb1.max_dead_records = max_dead;
-}
-
-/**
- * Close a database.
- *
- * @returns -1 for error; 0 for success.
- **/
-int tdb1_close(struct tdb_context *tdb)
-{
- int ret = 0;
-
- if (tdb->tdb1.transaction) {
- tdb1_transaction_cancel(tdb);
- }
-
- if (tdb->file->map_ptr) {
- if (tdb->flags & TDB_INTERNAL)
- SAFE_FREE(tdb->file->map_ptr);
- else
- tdb1_munmap(tdb);
- }
- SAFE_FREE(tdb->name);
- if (tdb->file->fd != -1) {
- ret = close(tdb->file->fd);
- tdb->file->fd = -1;
- }
- SAFE_FREE(tdb->file->lockrecs);
- SAFE_FREE(tdb->file);
-
- memset(tdb, 0, sizeof(*tdb));
- SAFE_FREE(tdb);
-
- return ret;