-
- /* 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_LOG_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 */
- tdb->ecode = tdb_lock_open(tdb, TDB_LOCK_WAIT|TDB_LOCK_NOCHECK);
- if (tdb->ecode != TDB_SUCCESS) {
- goto fail;
- }
-
- /* If they used O_TRUNC, read will return 0. */
- rlen = read(tdb->fd, &hdr, sizeof(hdr));
- if (rlen == 0 && (open_flags & O_CREAT)) {
- if (tdb_new_database(tdb, seed, &hdr) == -1) {
- goto fail;
- }
- } else if (rlen < 0) {
- tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
- "tdb_open: error %s reading %s",
- strerror(errno), name);
- goto fail;
- } else if (rlen < sizeof(hdr)
- || strcmp(hdr.magic_food, TDB_MAGIC_FOOD) != 0) {
- tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
- "tdb_open: %s is not a tdb file", name);
- goto fail;
- }
-
- 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_LOG_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_LOG_ERROR,
- "tdb_open: %s uses a different hash function",
- name);
- goto fail;
- }
-
- if (fstat(tdb->fd, &st) == -1) {
- saved_errno = errno;
- tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
- "tdb_open: could not stat open %s: %s",
- name, strerror(errno));
- goto fail;
- }
-
- /* Is it already in the open list? If so, fail. */
- if (tdb_already_open(st.st_dev, st.st_ino)) {
- /* FIXME */
- tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_USE_ERROR,
- "tdb_open: %s (%d,%d) is already open in this"
- " process",
- name, (int)st.st_dev, (int)st.st_ino);
- goto fail;
- }
-
- tdb->name = strdup(name);
- if (!tdb->name) {
- tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
- "tdb_open: failed to allocate name");
- goto fail;
- }
-
- tdb->device = st.st_dev;
- tdb->inode = st.st_ino;
- tdb_unlock_open(tdb);
-
- /* This make sure we have current map_size and mmap. */
- tdb->methods->oob(tdb, tdb->map_size + 1, true);
-
- /* Now it's fully formed, recover if necessary. */
- if (tdb_needs_recovery(tdb)) {
- ecode = tdb_lock_and_recover(tdb);
- if (ecode != TDB_SUCCESS) {
- tdb->ecode = ecode;
- goto fail;
- }