X-Git-Url: http://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Ftdb2%2Ftdb1_open.c;h=e668616a045f7bc174eea539951526074752b28a;hp=6e0c25db0556d8eff546e1b59907c781af532dc3;hb=614259f13c3e694fcd6b57fc05a329066e43c76d;hpb=c8c3b3568677e8b0105f84e4ab068c580faf4591 diff --git a/ccan/tdb2/tdb1_open.c b/ccan/tdb2/tdb1_open.c index 6e0c25db..e668616a 100644 --- a/ccan/tdb2/tdb1_open.c +++ b/ccan/tdb2/tdb1_open.c @@ -42,7 +42,8 @@ void tdb1_header_hash(struct tdb_context *tdb, *magic1_hash = 1; } -static void tdb1_context_init(struct tdb_context *tdb) +static void tdb_context_init(struct tdb_context *tdb, + struct tdb_attribute_tdb1_max_dead *max_dead) { assert(tdb->flags & TDB_VERSION1); @@ -58,20 +59,24 @@ static void tdb1_context_init(struct tdb_context *tdb) tdb->tdb1.page_size = 0x2000; } - /* FIXME: Used to be 5 for TDB_VOLATILE. */ - tdb->tdb1.max_dead_records = 0; + if (max_dead) { + tdb->tdb1.max_dead_records = max_dead->max_dead; + } else { + tdb->tdb1.max_dead_records = 0; + } } /* initialise a new database */ enum TDB_ERROR tdb1_new_database(struct tdb_context *tdb, - struct tdb_attribute_tdb1_hashsize *hashsize) + struct tdb_attribute_tdb1_hashsize *hashsize, + struct tdb_attribute_tdb1_max_dead *max_dead) { struct tdb1_header *newdb; size_t size; int hash_size = TDB1_DEFAULT_HASH_SIZE; - enum TDB_ERROR ret = TDB_ERR_IO; + enum TDB_ERROR ret; - tdb1_context_init(tdb); + tdb_context_init(tdb, max_dead); /* Default TDB2 hash becomes default TDB1 hash. */ if (tdb->hash_fn == tdb_jenkins_hash) @@ -83,7 +88,8 @@ enum TDB_ERROR tdb1_new_database(struct tdb_context *tdb, /* We make it up in memory, then write it out if not internal */ size = sizeof(struct tdb1_header) + (hash_size+1)*sizeof(tdb1_off_t); if (!(newdb = (struct tdb1_header *)calloc(size, 1))) { - return TDB_ERR_OOM; + return tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR, + "Could not allocate new database header"); } /* Fill in the header */ @@ -108,15 +114,24 @@ enum TDB_ERROR tdb1_new_database(struct tdb_context *tdb, tdb->file->map_ptr = (char *)newdb; return TDB_SUCCESS; } - if (lseek(tdb->file->fd, 0, SEEK_SET) == -1) + if (lseek(tdb->file->fd, 0, SEEK_SET) == -1) { + ret = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR, + "tdb1_new_database: lseek failed"); goto fail; + } - if (ftruncate(tdb->file->fd, 0) == -1) + if (ftruncate(tdb->file->fd, 0) == -1) { + ret = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR, + "tdb1_new_database: ftruncate failed"); goto fail; + } - /* we still have "ret == TDB_ERR_IO" here */ - if (tdb1_write_all(tdb->file->fd, newdb, size)) - ret = TDB_SUCCESS; + if (!tdb1_write_all(tdb->file->fd, newdb, size)) { + ret = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR, + "tdb1_new_database: write failed"); + goto fail; + } + ret = TDB_SUCCESS; fail: SAFE_FREE(newdb); @@ -164,14 +179,15 @@ static bool check_header_hash(struct tdb_context *tdb, } /* We are hold the TDB open lock on tdb->fd. */ -enum TDB_ERROR tdb1_open(struct tdb_context *tdb) +enum TDB_ERROR tdb1_open(struct tdb_context *tdb, + struct tdb_attribute_tdb1_max_dead *max_dead) { const char *hash_alg; uint32_t magic1, magic2; tdb->flags |= TDB_VERSION1; - tdb1_context_init(tdb); + tdb_context_init(tdb, max_dead); /* Default TDB2 hash becomes default TDB1 hash. */ if (tdb->hash_fn == tdb_jenkins_hash) { @@ -216,12 +232,3 @@ enum TDB_ERROR tdb1_open(struct tdb_context *tdb) } return TDB_SUCCESS; } - -/* - * Set the maximum number of dead records per hash chain - */ - -void tdb1_set_max_dead(struct tdb_context *tdb, int max_dead) -{ - tdb->tdb1.max_dead_records = max_dead; -}