- if (!(tdb = (struct tdb1_context *)calloc(1, sizeof *tdb))) {
- /* Can't log this */
- errno = ENOMEM;
- goto fail;
- }
- tdb1_io_init(tdb);
- tdb->fd = -1;
- tdb->name = NULL;
- tdb->map_ptr = NULL;
- tdb->flags = tdb1_flags;
- tdb->open_flags = open_flags;
- if (log_ctx) {
- tdb->log = *log_ctx;
- } else {
- tdb->log.log_fn = null_log_fn;
- tdb->log.log_private = NULL;
- }
-
- if (name == NULL && (tdb1_flags & TDB1_INTERNAL)) {
- name = "__TDB1_INTERNAL__";
- }
-
- if (name == NULL) {
- tdb->name = (char *)"__NULL__";
- TDB1_LOG((tdb, TDB1_DEBUG_FATAL, "tdb1_open_ex: called with name == NULL\n"));
- tdb->name = NULL;
- errno = EINVAL;
- goto fail;
- }
-
- /* now make a copy of the name, as the caller memory might went away */
- if (!(tdb->name = (char *)strdup(name))) {
- /*
- * set the name as the given string, so that tdb1_name() will
- * work in case of an error.
- */
- tdb->name = (char *)name;
- TDB1_LOG((tdb, TDB1_DEBUG_ERROR, "tdb1_open_ex: can't strdup(%s)\n",
- name));
- tdb->name = NULL;
- errno = ENOMEM;
- goto fail;
- }
-
- if (hash_fn) {
- tdb->hash_fn = hash_fn;
- hash_alg = "the user defined";
- } else {
- /* This controls what we use when creating a tdb. */
- if (tdb->flags & TDB1_INCOMPATIBLE_HASH) {
- tdb->hash_fn = tdb1_jenkins_hash;
- } else {
- tdb->hash_fn = tdb1_old_hash;
- }
- hash_alg = "either default";
- }
-
- /* cache the page size */
- tdb->page_size = getpagesize();
- if (tdb->page_size <= 0) {
- tdb->page_size = 0x2000;
- }
-
- tdb->max_dead_records = (tdb1_flags & TDB1_VOLATILE) ? 5 : 0;
-
- if ((open_flags & O_ACCMODE) == O_WRONLY) {
- TDB1_LOG((tdb, TDB1_DEBUG_ERROR, "tdb1_open_ex: can't open tdb %s write-only\n",
- name));
- errno = EINVAL;
- goto fail;
- }
-
- if (hash_size == 0)
- hash_size = TDB1_DEFAULT_HASH_SIZE;
- if ((open_flags & O_ACCMODE) == O_RDONLY) {
- tdb->read_only = 1;
- /* read only databases don't do locking or clear if first */
- tdb->flags |= TDB1_NOLOCK;
- tdb->flags &= ~TDB1_CLEAR_IF_FIRST;
- }
-
- if ((tdb->flags & TDB1_ALLOW_NESTING) &&
- (tdb->flags & TDB1_DISALLOW_NESTING)) {
- tdb->ecode = TDB1_ERR_NESTING;
- TDB1_LOG((tdb, TDB1_DEBUG_FATAL, "tdb1_open_ex: "
- "allow_nesting and disallow_nesting are not allowed together!"));
- errno = EINVAL;
- goto fail;
- }
-
- if (getenv("TDB_NO_FSYNC")) {
- tdb->flags |= TDB1_NOSYNC;
- }
-
- /*
- * TDB1_ALLOW_NESTING is the default behavior.
- * Note: this may change in future versions!
- */
- if (!(tdb->flags & TDB1_DISALLOW_NESTING)) {
- tdb->flags |= TDB1_ALLOW_NESTING;
- }
-
- /* internal databases don't mmap or lock, and start off cleared */
- if (tdb->flags & TDB1_INTERNAL) {
- tdb->flags |= (TDB1_NOLOCK | TDB1_NOMMAP);
- tdb->flags &= ~TDB1_CLEAR_IF_FIRST;
- if (tdb1_new_database(tdb, hash_size) != 0) {
- TDB1_LOG((tdb, TDB1_DEBUG_ERROR, "tdb1_open_ex: tdb1_new_database failed!"));
- goto fail;
- }
- goto internal;
- }
-
- if ((tdb->fd = open(name, open_flags, mode)) == -1) {
- TDB1_LOG((tdb, TDB1_DEBUG_WARNING, "tdb1_open_ex: could not open file %s: %s\n",
- name, strerror(errno)));
- goto fail; /* errno set by open(2) */
- }
-
- /* on exec, don't inherit the fd */
- v = fcntl(tdb->fd, F_GETFD, 0);
- fcntl(tdb->fd, F_SETFD, v | FD_CLOEXEC);
-
- /* ensure there is only one process initialising at once */
- if (tdb1_nest_lock(tdb, TDB1_OPEN_LOCK, F_WRLCK, TDB1_LOCK_WAIT) == -1) {
- TDB1_LOG((tdb, TDB1_DEBUG_ERROR, "tdb1_open_ex: failed to get open lock on %s: %s\n",
- name, strerror(errno)));
- goto fail; /* errno set by tdb1_brlock */
- }
-
- /* we need to zero database if we are the only one with it open */
- if ((tdb1_flags & TDB1_CLEAR_IF_FIRST) &&
- (!tdb->read_only) &&
- (locked = (tdb1_nest_lock(tdb, TDB1_ACTIVE_LOCK, F_WRLCK, TDB1_LOCK_NOWAIT|TDB1_LOCK_PROBE) == 0))) {
- open_flags |= O_CREAT;
- if (ftruncate(tdb->fd, 0) == -1) {
- TDB1_LOG((tdb, TDB1_DEBUG_FATAL, "tdb1_open_ex: "
- "failed to truncate %s: %s\n",
- name, strerror(errno)));
- goto fail; /* errno set by ftruncate */
- }
- }
-
- errno = 0;
- if (read(tdb->fd, &tdb->header, sizeof(tdb->header)) != sizeof(tdb->header)
- || strcmp(tdb->header.magic_food, TDB1_MAGIC_FOOD) != 0) {
- if (!(open_flags & O_CREAT) || tdb1_new_database(tdb, hash_size) == -1) {
- if (errno == 0) {
- errno = EIO; /* ie bad format or something */
- }
- goto fail;
- }
- rev = (tdb->flags & TDB1_CONVERT);
- } else if (tdb->header.version != TDB1_VERSION
- && !(rev = (tdb->header.version==TDB1_BYTEREV(TDB1_VERSION)))) {
- /* wrong version */
- errno = EIO;
- goto fail;
- }
- vp = (unsigned char *)&tdb->header.version;
- vertest = (((uint32_t)vp[0]) << 24) | (((uint32_t)vp[1]) << 16) |
- (((uint32_t)vp[2]) << 8) | (uint32_t)vp[3];
- tdb->flags |= (vertest==TDB1_VERSION) ? TDB1_BIGENDIAN : 0;
- if (!rev)
- tdb->flags &= ~TDB1_CONVERT;
- else {
- tdb->flags |= TDB1_CONVERT;
- tdb1_convert(&tdb->header, sizeof(tdb->header));
- }
- if (fstat(tdb->fd, &st) == -1)
- goto fail;