- 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_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
- "tdb1_open_ex: "
- "allow_nesting and disallow_nesting are not allowed together!");
- errno = EINVAL;
- goto fail;
- }
-
- /*
- * 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) {
- tdb_logerr(tdb, tdb->last_error, TDB_LOG_ERROR,
- "tdb1_open_ex: tdb1_new_database failed!");
- goto fail;
- }
- goto internal;
- }
-
- if ((tdb->fd = open(name, open_flags, mode)) == -1) {
- tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
- "tdb1_open_ex: could not open file %s: %s",
- 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, TDB_LOCK_WAIT) == -1) {
- tdb_logerr(tdb, tdb->last_error, TDB_LOG_ERROR,
- "tdb1_open_ex: failed to get open lock on %s: %s",
- 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, TDB_LOCK_NOWAIT|TDB_LOCK_PROBE) == 0))) {
- open_flags |= O_CREAT;
- if (ftruncate(tdb->fd, 0) == -1) {
- tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
- "tdb1_open_ex: "
- "failed to truncate %s: %s",
- name, strerror(errno));
- goto fail; /* errno set by ftruncate */