- tdb = malloc(sizeof(*tdb));
- if (!tdb) {
- /* Can't log this */
- errno = ENOMEM;
- return NULL;
- }
- tdb->name = NULL;
- tdb->map_ptr = NULL;
- tdb->direct_access = 0;
- tdb->fd = -1;
- tdb->map_size = sizeof(struct tdb_header);
- tdb->ecode = TDB_SUCCESS;
- tdb->flags = tdb_flags;
- tdb->logfn = NULL;
- tdb->transaction = NULL;
- tdb->stats = NULL;
- tdb->access = NULL;
- tdb_hash_init(tdb);
- tdb_io_init(tdb);
- tdb_lock_init(tdb);
-
- while (attr) {
- switch (attr->base.attr) {
- case TDB_ATTRIBUTE_LOG:
- tdb->logfn = attr->log.log_fn;
- tdb->log_private = attr->log.log_private;
- break;
- case TDB_ATTRIBUTE_HASH:
- tdb->khash = attr->hash.hash_fn;
- tdb->hash_priv = attr->hash.hash_private;
- break;
- case TDB_ATTRIBUTE_SEED:
- seed = &attr->seed;
- break;
- case TDB_ATTRIBUTE_STATS:
- tdb->stats = &attr->stats;
- /* They have stats we don't know about? Tell them. */
- if (tdb->stats->size > sizeof(attr->stats))
- tdb->stats->size = sizeof(attr->stats);
- break;
- default:
- tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
- "tdb_open: unknown attribute type %u",
- attr->base.attr);
- goto fail;
- }
- attr = attr->base.next;
- }
-
- if ((open_flags & O_ACCMODE) == O_WRONLY) {
- tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
- "tdb_open: can't open tdb %s write-only", name);
- goto fail;
- }
-
- if ((open_flags & O_ACCMODE) == O_RDONLY) {
- tdb->read_only = true;
- /* read only databases don't do locking */
- tdb->flags |= TDB_NOLOCK;
- tdb->mmap_flags = PROT_READ;
- } else {
- tdb->read_only = false;
- tdb->mmap_flags = PROT_READ | PROT_WRITE;
- }
-
- /* 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;