- 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 */
- }
- }
-
- errno = 0;
- if (read(tdb->fd, &tdb->header, sizeof(tdb->header)) != sizeof(tdb->header)
- || strcmp(tdb->header.magic_food, TDB_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));