X-Git-Url: http://git.ozlabs.org/?a=blobdiff_plain;f=ccan%2Ftdb2%2Ftdb1_open.c;h=73f7f26710e1c1a3691d98acdf9bedfa275cf75b;hb=4dc29a338fadeac805b369b4b0851c02f1b152c7;hp=d02a33eb85c506ba5aa375171588203d85cfb31f;hpb=2b50be90a23893a06e0f1436a31d18b97ad0e11d;p=ccan diff --git a/ccan/tdb2/tdb1_open.c b/ccan/tdb2/tdb1_open.c index d02a33eb..73f7f267 100644 --- a/ccan/tdb2/tdb1_open.c +++ b/ccan/tdb2/tdb1_open.c @@ -34,16 +34,10 @@ static struct tdb1_context *tdb1s = NULL; void tdb1_header_hash(struct tdb1_context *tdb, uint32_t *magic1_hash, uint32_t *magic2_hash) { - TDB_DATA hash_key; uint32_t tdb1_magic = TDB1_MAGIC; - hash_key.dptr = (unsigned char *)TDB_MAGIC_FOOD; - hash_key.dsize = sizeof(TDB_MAGIC_FOOD); - *magic1_hash = tdb->hash_fn(&hash_key); - - hash_key.dptr = (unsigned char *)TDB1_CONV(tdb1_magic); - hash_key.dsize = sizeof(tdb1_magic); - *magic2_hash = tdb->hash_fn(&hash_key); + *magic1_hash = tdb_hash(tdb, TDB_MAGIC_FOOD, sizeof(TDB_MAGIC_FOOD)); + *magic2_hash = tdb_hash(tdb, TDB1_CONV(tdb1_magic), sizeof(tdb1_magic)); /* Make sure at least one hash is non-zero! */ if (*magic1_hash == 0 && *magic2_hash == 0) @@ -76,17 +70,18 @@ static int tdb1_new_database(struct tdb1_context *tdb, int hash_size) newdb->rwlocks = TDB1_HASH_RWLOCK_MAGIC; if (tdb->flags & TDB_INTERNAL) { - tdb->map_size = size; - tdb->map_ptr = (char *)newdb; + tdb->file->fd = -1; + tdb->file->map_size = size; + tdb->file->map_ptr = (char *)newdb; memcpy(&tdb->header, newdb, sizeof(tdb->header)); /* Convert the `ondisk' version if asked. */ TDB1_CONV(*newdb); return 0; } - if (lseek(tdb->fd, 0, SEEK_SET) == -1) + if (lseek(tdb->file->fd, 0, SEEK_SET) == -1) goto fail; - if (ftruncate(tdb->fd, 0) == -1) + if (ftruncate(tdb->file->fd, 0) == -1) goto fail; /* This creates an endian-converted header, as if read from disk */ @@ -95,7 +90,7 @@ static int tdb1_new_database(struct tdb1_context *tdb, int hash_size) /* Don't endian-convert the magic food! */ memcpy(newdb->magic_food, TDB_MAGIC_FOOD, strlen(TDB_MAGIC_FOOD)+1); /* we still have "ret == -1" here */ - if (tdb1_write_all(tdb->fd, newdb, size)) + if (tdb1_write_all(tdb->file->fd, newdb, size)) ret = 0; fail: @@ -111,7 +106,7 @@ static int tdb1_already_open(dev_t device, struct tdb1_context *i; for (i = tdb1s; i; i = i->next) { - if (i->device == device && i->inode == ino) { + if (i->file->device == device && i->file->inode == ino) { return 1; } } @@ -176,12 +171,20 @@ struct tdb1_context *tdb1_open_ex(const char *name, int hash_size, int tdb1_flag errno = ENOMEM; goto fail; } + tdb->file = calloc(1, sizeof *tdb->file); + if (!tdb->file) { + free(tdb); + errno = ENOMEM; + goto fail; + } tdb1_io_init(tdb); - tdb->fd = -1; + tdb->file->fd = -1; tdb->name = NULL; - tdb->map_ptr = NULL; + tdb->file->map_ptr = NULL; tdb->flags = tdb1_flags|TDB_VERSION1; tdb->open_flags = open_flags; + tdb->lock_fn = tdb_fcntl_lock; + tdb->unlock_fn = tdb_fcntl_unlock; if (log_ctx) { tdb->log_fn = log_ctx->log_fn; tdb->log_data = log_ctx->log_private; @@ -214,6 +217,7 @@ struct tdb1_context *tdb1_open_ex(const char *name, int hash_size, int tdb1_flag errno = ENOMEM; goto fail; } + tdb->hash_seed = 0; if (hash_fn) { tdb->hash_fn = hash_fn; @@ -246,7 +250,7 @@ struct tdb1_context *tdb1_open_ex(const char *name, int hash_size, int tdb1_flag if (hash_size == 0) hash_size = TDB1_DEFAULT_HASH_SIZE; if ((open_flags & O_ACCMODE) == O_RDONLY) { - tdb->read_only = 1; + tdb->flags |= TDB_RDONLY; /* read only databases don't do locking */ tdb->flags |= TDB_NOLOCK; } @@ -262,7 +266,7 @@ struct tdb1_context *tdb1_open_ex(const char *name, int hash_size, int tdb1_flag goto internal; } - if ((tdb->fd = open(name, open_flags, mode)) == -1) { + if ((tdb->file->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)); @@ -270,8 +274,8 @@ struct tdb1_context *tdb1_open_ex(const char *name, int hash_size, int tdb1_flag } /* on exec, don't inherit the fd */ - v = fcntl(tdb->fd, F_GETFD, 0); - fcntl(tdb->fd, F_SETFD, v | FD_CLOEXEC); + v = fcntl(tdb->file->fd, F_GETFD, 0); + fcntl(tdb->file->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) { @@ -282,7 +286,7 @@ struct tdb1_context *tdb1_open_ex(const char *name, int hash_size, int tdb1_flag } errno = 0; - if (read(tdb->fd, &tdb->header, sizeof(tdb->header)) != sizeof(tdb->header) + if (read(tdb->file->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) { @@ -303,7 +307,7 @@ struct tdb1_context *tdb1_open_ex(const char *name, int hash_size, int tdb1_flag tdb->flags |= TDB_CONVERT; tdb1_convert(&tdb->header, sizeof(tdb->header)); } - if (fstat(tdb->fd, &st) == -1) + if (fstat(tdb->file->fd, &st) == -1) goto fail; if (tdb->header.rwlocks != 0 && @@ -343,9 +347,9 @@ struct tdb1_context *tdb1_open_ex(const char *name, int hash_size, int tdb1_flag goto fail; } - tdb->map_size = st.st_size; - tdb->device = st.st_dev; - tdb->inode = st.st_ino; + tdb->file->map_size = st.st_size; + tdb->file->device = st.st_dev; + tdb->file->inode = st.st_ino; tdb1_mmap(tdb); /* if needed, run recovery */ @@ -370,17 +374,20 @@ struct tdb1_context *tdb1_open_ex(const char *name, int hash_size, int tdb1_flag if (!tdb) return NULL; - if (tdb->map_ptr) { + if (tdb->file->map_ptr) { if (tdb->flags & TDB_INTERNAL) - SAFE_FREE(tdb->map_ptr); + SAFE_FREE(tdb->file->map_ptr); else tdb1_munmap(tdb); } - if (tdb->fd != -1) - if (close(tdb->fd) != 0) + if (tdb->file->fd != -1) + if (close(tdb->file->fd) != 0) tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR, "tdb1_open_ex: failed to close tdb->fd on error!"); - SAFE_FREE(tdb->lockrecs); + if (tdb->file) { + SAFE_FREE(tdb->file->lockrecs); + SAFE_FREE(tdb->file); + } SAFE_FREE(tdb->name); SAFE_FREE(tdb); errno = save_errno; @@ -411,18 +418,19 @@ int tdb1_close(struct tdb1_context *tdb) tdb1_transaction_cancel(tdb); } - if (tdb->map_ptr) { + if (tdb->file->map_ptr) { if (tdb->flags & TDB_INTERNAL) - SAFE_FREE(tdb->map_ptr); + SAFE_FREE(tdb->file->map_ptr); else tdb1_munmap(tdb); } SAFE_FREE(tdb->name); - if (tdb->fd != -1) { - ret = close(tdb->fd); - tdb->fd = -1; + if (tdb->file->fd != -1) { + ret = close(tdb->file->fd); + tdb->file->fd = -1; } - SAFE_FREE(tdb->lockrecs); + SAFE_FREE(tdb->file->lockrecs); + SAFE_FREE(tdb->file); /* Remove from contexts list */ for (i = &tdb1s; *i; i = &(*i)->next) {