-
- /* internal databases don't need any of the rest. */
- if (tdb->flags & TDB_INTERNAL) {
- tdb->flags |= (TDB_NOLOCK | TDB_NOMMAP);
- if (tdb_new_database(tdb, seed, &hdr) != 0) {
- goto fail;
- }
- tdb_convert(tdb, &hdr.hash_seed, sizeof(hdr.hash_seed));
- tdb->hash_seed = hdr.hash_seed;
- tdb_ftable_init(tdb);
- return tdb;
- }
-
- if ((tdb->fd = open(name, open_flags, mode)) == -1) {
- /* errno set by open(2) */
- saved_errno = errno;
- tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_ERROR,
- "tdb_open: could not open file %s: %s",
- name, strerror(errno));
- goto fail;
- }
-
- /* 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 (tdb_lock_open(tdb, TDB_LOCK_WAIT|TDB_LOCK_NOCHECK) == -1) {
- /* errno set by tdb_brlock */
- saved_errno = errno;
- goto fail;
- }
-
- if (!tdb_pread_all(tdb->fd, &hdr, sizeof(hdr), 0)
- || strcmp(hdr.magic_food, TDB_MAGIC_FOOD) != 0) {
- if (!(open_flags & O_CREAT)) {
- tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_ERROR,
- "tdb_open: %s is not a tdb file", name);
- goto fail;
- }
- if (tdb_new_database(tdb, seed, &hdr) == -1) {
- goto fail;
- }
- } else if (hdr.version != TDB_VERSION) {
- if (hdr.version == bswap_64(TDB_VERSION))
- tdb->flags |= TDB_CONVERT;
- else {
- /* wrong version */
- tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_ERROR,
- "tdb_open: %s is unknown version 0x%llx",
- name, (long long)hdr.version);
- goto fail;
- }
- }
-
- tdb_convert(tdb, &hdr, sizeof(hdr));
- tdb->hash_seed = hdr.hash_seed;
- hash_test = TDB_HASH_MAGIC;
- hash_test = tdb_hash(tdb, &hash_test, sizeof(hash_test));
- if (hdr.hash_test != hash_test) {
- /* wrong hash variant */
- tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_ERROR,
- "tdb_open: %s uses a different hash function",
- name);
- goto fail;