};
/* 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;
/* 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),
uint64_t hash_test;
unsigned v;
struct tdb_header hdr;
+ struct tdb_attribute_seed *seed = NULL;
tdb = malloc(sizeof(*tdb));
if (!tdb) {
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",
tdb->read_only = true;
/* read only databases don't do locking */
tdb->flags |= TDB_NOLOCK;
- } else
+ 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, &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;
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 */
}
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;
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;
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;
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;
{
return tdb->ecode;
}
+
+const char *tdb_errorstr(struct tdb_context *tdb)
+{
+ /* Gcc warns if you miss a case in the switch, so use that. */
+ switch (tdb->ecode) {
+ case TDB_SUCCESS: return "Success";
+ case TDB_ERR_CORRUPT: return "Corrupt database";
+ case TDB_ERR_IO: return "IO Error";
+ case TDB_ERR_LOCK: return "Locking error";
+ case TDB_ERR_OOM: return "Out of memory";
+ case TDB_ERR_EXISTS: return "Record exists";
+ case TDB_ERR_NESTING: return "Transaction already started";
+ case TDB_ERR_EINVAL: return "Invalid parameter";
+ case TDB_ERR_NOEXIST: return "Record does not exist";
+ case TDB_ERR_RDONLY: return "write not permitted";
+ }
+ return "Invalid error code";
+}