- /*
- * 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;
-
- if (tdb->header.rwlocks != 0 &&
- tdb->header.rwlocks != TDB1_HASH_RWLOCK_MAGIC) {
- TDB1_LOG((tdb, TDB1_DEBUG_ERROR, "tdb1_open_ex: spinlocks no longer supported\n"));
- goto fail;
- }
-
- if ((tdb->header.magic1_hash == 0) && (tdb->header.magic2_hash == 0)) {
- /* older TDB without magic hash references */