X-Git-Url: http://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Ftdb2%2Ftdb.c;h=e3b3c2303f282ae387eec8171a3327f4d8864a29;hp=659d39b23b1ccefc99060c76d6196fa6be77df8e;hb=8afb9681b3be7149cf8913a5aefe915194fd20f7;hpb=b846677c1ec77e986c7d6b7c913aa2a4b0c82d4e diff --git a/ccan/tdb2/tdb.c b/ccan/tdb2/tdb.c index 659d39b2..e3b3c230 100644 --- a/ccan/tdb2/tdb.c +++ b/ccan/tdb2/tdb.c @@ -10,7 +10,7 @@ struct tdb_data tdb_null = { .dptr = NULL, .dsize = 0 }; /* all contexts, to ensure no double-opens (fcntl locks don't nest!) */ static struct tdb_context *tdbs = NULL; -PRINTF_ATTRIBUTE(4, 5) static void +PRINTF_FMT(4, 5) static void null_log_fn(struct tdb_context *tdb, enum tdb_debug_level level, void *priv, const char *fmt, ...) @@ -94,7 +94,9 @@ struct new_database { }; /* initialise a new database */ -static int tdb_new_database(struct tdb_context *tdb, struct tdb_header *hdr) +static int tdb_new_database(struct tdb_context *tdb, + struct tdb_attribute_seed *seed, + struct tdb_header *hdr) { /* We make it up in memory, then write it out if not internal */ struct new_database newdb; @@ -105,7 +107,10 @@ static int tdb_new_database(struct tdb_context *tdb, struct tdb_header *hdr) /* Fill in the header */ newdb.hdr.version = TDB_VERSION; - newdb.hdr.hash_seed = random_number(tdb); + if (seed) + newdb.hdr.hash_seed = seed->seed; + else + newdb.hdr.hash_seed = random_number(tdb); newdb.hdr.hash_test = TDB_HASH_MAGIC; newdb.hdr.hash_test = tdb->khash(&newdb.hdr.hash_test, sizeof(newdb.hdr.hash_test), @@ -181,6 +186,7 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags, uint64_t hash_test; unsigned v; struct tdb_header hdr; + struct tdb_attribute_seed *seed = NULL; tdb = malloc(sizeof(*tdb)); if (!tdb) { @@ -213,6 +219,9 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags, tdb->khash = attr->hash.hash_fn; tdb->hash_priv = attr->hash.hash_private; break; + case TDB_ATTRIBUTE_SEED: + seed = &attr->seed; + break; default: tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, "tdb_open: unknown attribute type %u\n", @@ -243,7 +252,7 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags, /* 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, &hdr) != 0) { + if (tdb_new_database(tdb, seed, &hdr) != 0) { tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, "tdb_open: tdb_new_database failed!"); goto fail; @@ -275,7 +284,8 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags, if (!tdb_pread_all(tdb->fd, &hdr, sizeof(hdr), 0) || strcmp(hdr.magic_food, TDB_MAGIC_FOOD) != 0) { - if (!(open_flags & O_CREAT) || tdb_new_database(tdb, &hdr) == -1) { + if (!(open_flags & O_CREAT) + || tdb_new_database(tdb, seed, &hdr) == -1) { if (errno == 0) { errno = EIO; /* ie bad format or something */ } @@ -432,7 +442,7 @@ int tdb_store(struct tdb_context *tdb, struct tdb_used_record rec; int ret; - off = find_and_lock(tdb, key, F_WRLCK, &h, &rec); + off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL); if (unlikely(off == TDB_OFF_ERR)) return -1; @@ -494,7 +504,7 @@ int tdb_append(struct tdb_context *tdb, struct tdb_data new_dbuf; int ret; - off = find_and_lock(tdb, key, F_WRLCK, &h, &rec); + off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL); if (unlikely(off == TDB_OFF_ERR)) return -1; @@ -562,7 +572,7 @@ struct tdb_data tdb_fetch(struct tdb_context *tdb, struct tdb_data key) struct hash_info h; struct tdb_data ret; - off = find_and_lock(tdb, key, F_RDLCK, &h, &rec); + off = find_and_lock(tdb, key, F_RDLCK, &h, &rec, NULL); if (unlikely(off == TDB_OFF_ERR)) return tdb_null; @@ -585,7 +595,7 @@ int tdb_delete(struct tdb_context *tdb, struct tdb_data key) struct tdb_used_record rec; struct hash_info h; - off = find_and_lock(tdb, key, F_WRLCK, &h, &rec); + off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL); if (unlikely(off == TDB_OFF_ERR)) return -1;