/* Free is empty. */
newdb.hdr.free_table = offsetof(struct new_database, ftable);
memset(&newdb.ftable, 0, sizeof(newdb.ftable));
- set_header(NULL, &newdb.ftable.hdr, TDB_FTABLE_MAGIC, 0,
- sizeof(newdb.ftable) - sizeof(newdb.ftable.hdr),
- sizeof(newdb.ftable) - sizeof(newdb.ftable.hdr), 0);
+ tdb->ecode = set_header(NULL, &newdb.ftable.hdr, TDB_FTABLE_MAGIC, 0,
+ sizeof(newdb.ftable) - sizeof(newdb.ftable.hdr),
+ sizeof(newdb.ftable) - sizeof(newdb.ftable.hdr),
+ 0);
+ if (tdb->ecode != TDB_SUCCESS) {
+ return -1;
+ }
/* Magic food */
memset(newdb.hdr.magic_food, 0, sizeof(newdb.hdr.magic_food));
ssize_t rlen;
struct tdb_header hdr;
struct tdb_attribute_seed *seed = NULL;
+ enum TDB_ERROR ecode;
tdb = malloc(sizeof(*tdb));
if (!tdb) {
fcntl(tdb->fd, F_SETFD, v | FD_CLOEXEC);
/* ensure there is only one process initialising at once */
- if (tdb_lock_open(tdb, TDB_LOCK_WAIT|TDB_LOCK_NOCHECK) == -1) {
- /* errno set by tdb_brlock */
- saved_errno = errno;
+ tdb->ecode = tdb_lock_open(tdb, TDB_LOCK_WAIT|TDB_LOCK_NOCHECK);
+ if (tdb->ecode != TDB_SUCCESS) {
goto fail;
}
tdb->methods->oob(tdb, tdb->map_size + 1, true);
/* Now it's fully formed, recover if necessary. */
- if (tdb_needs_recovery(tdb) && tdb_lock_and_recover(tdb) == -1) {
- goto fail;
+ if (tdb_needs_recovery(tdb)) {
+ ecode = tdb_lock_and_recover(tdb);
+ if (ecode != TDB_SUCCESS) {
+ tdb->ecode = ecode;
+ goto fail;
+ }
}
- if (tdb_ftable_init(tdb) == -1)
+ tdb->ecode = tdb_ftable_init(tdb);
+ if (tdb->ecode != TDB_SUCCESS) {
goto fail;
+ }
tdb->next = tdbs;
tdbs = tdb;
uint64_t h)
{
uint64_t dataroom = rec_data_length(rec) + rec_extra_padding(rec);
+ enum TDB_ERROR ecode;
- if (set_header(tdb, rec, TDB_USED_MAGIC, keylen, datalen,
- keylen + dataroom, h))
+ ecode = set_header(tdb, rec, TDB_USED_MAGIC, keylen, datalen,
+ keylen + dataroom, h);
+ if (ecode != TDB_SUCCESS) {
+ tdb->ecode = ecode;
return -1;
+ }
- return tdb_write_convert(tdb, off, rec, sizeof(*rec));
+ ecode = tdb_write_convert(tdb, off, rec, sizeof(*rec));
+ if (ecode != TDB_SUCCESS) {
+ tdb->ecode = ecode;
+ return -1;
+ }
+ return 0;
}
/* Returns -1 on error, 0 on OK */
bool growing)
{
tdb_off_t new_off;
+ enum TDB_ERROR ecode;
/* Allocate a new record. */
new_off = alloc(tdb, key.dsize, dbuf.dsize, h->h, TDB_USED_MAGIC,
growing);
- if (unlikely(new_off == TDB_OFF_ERR))
+ if (TDB_OFF_IS_ERR(new_off)) {
+ tdb->ecode = new_off;
return -1;
+ }
/* We didn't like the existing one: remove it. */
if (old_off) {
add_stat(tdb, frees, 1);
- add_free_record(tdb, old_off,
- sizeof(struct tdb_used_record)
- + key.dsize + old_room);
- if (replace_in_hash(tdb, h, new_off) == -1)
- return -1;
+ ecode = add_free_record(tdb, old_off,
+ sizeof(struct tdb_used_record)
+ + key.dsize + old_room);
+ if (ecode == TDB_SUCCESS)
+ ecode = replace_in_hash(tdb, h, new_off);
} else {
- if (add_to_hash(tdb, h, new_off) == -1)
- return -1;
+ ecode = add_to_hash(tdb, h, new_off);
+ }
+ if (ecode != TDB_SUCCESS) {
+ tdb->ecode = ecode;
+ return -1;
}
new_off += sizeof(struct tdb_used_record);
- if (tdb->methods->twrite(tdb, new_off, key.dptr, key.dsize) == -1)
+ ecode = tdb->methods->twrite(tdb, new_off, key.dptr, key.dsize);
+ if (ecode != TDB_SUCCESS) {
+ tdb->ecode = ecode;
return -1;
+ }
new_off += key.dsize;
- if (tdb->methods->twrite(tdb, new_off, dbuf.dptr, dbuf.dsize) == -1)
+ ecode = tdb->methods->twrite(tdb, new_off, dbuf.dptr, dbuf.dsize);
+ if (ecode != TDB_SUCCESS) {
+ tdb->ecode = ecode;
return -1;
+ }
/* FIXME: tdb_increment_seqnum(tdb); */
return 0;
tdb_len_t old_room = 0;
struct tdb_used_record rec;
int ret;
+ enum TDB_ERROR ecode;
off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL);
- if (unlikely(off == TDB_OFF_ERR))
+ if (TDB_OFF_IS_ERR(off)) {
+ tdb->ecode = off;
return -1;
+ }
/* Now we have lock on this hash bucket. */
if (flag == TDB_INSERT) {
key.dsize, dbuf.dsize,
&rec, h.h))
goto fail;
- if (tdb->methods->twrite(tdb, off + sizeof(rec)
- + key.dsize,
- dbuf.dptr, dbuf.dsize))
+ ecode = tdb->methods->twrite(tdb,
+ off + sizeof(rec)
+ + key.dsize,
+ dbuf.dptr,
+ dbuf.dsize);
+ if (ecode != TDB_SUCCESS) {
+ tdb->ecode = ecode;
goto fail;
+ }
tdb_unlock_hashes(tdb, h.hlock_start,
h.hlock_range, F_WRLCK);
return 0;
tdb_len_t old_room = 0, old_dlen;
unsigned char *newdata;
struct tdb_data new_dbuf;
+ enum TDB_ERROR ecode;
int ret;
off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL);
- if (unlikely(off == TDB_OFF_ERR))
+ if (TDB_OFF_IS_ERR(off)) {
+ tdb->ecode = off;
return -1;
+ }
if (off) {
old_dlen = rec_data_length(&rec);
goto fail;
off += sizeof(rec) + key.dsize + old_dlen;
- if (tdb->methods->twrite(tdb, off, dbuf.dptr,
- dbuf.dsize) == -1)
+ ecode = tdb->methods->twrite(tdb, off, dbuf.dptr,
+ dbuf.dsize);
+ if (ecode != TDB_SUCCESS) {
+ tdb->ecode = ecode;
goto fail;
+ }
/* FIXME: tdb_increment_seqnum(tdb); */
tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range,
(size_t)(key.dsize+old_dlen+dbuf.dsize));
goto fail;
}
- if (tdb->methods->tread(tdb, off + sizeof(rec) + key.dsize,
- newdata, old_dlen) != 0) {
+ ecode = tdb->methods->tread(tdb, off + sizeof(rec) + key.dsize,
+ newdata, old_dlen);
+ if (ecode != TDB_SUCCESS) {
+ tdb->ecode = ecode;
free(newdata);
goto fail;
}
struct tdb_data ret;
off = find_and_lock(tdb, key, F_RDLCK, &h, &rec, NULL);
- if (unlikely(off == TDB_OFF_ERR))
+ if (TDB_OFF_IS_ERR(off)) {
+ tdb->ecode = off;
return tdb_null;
+ }
if (!off) {
tdb->ecode = TDB_ERR_NOEXIST;
ret.dsize = rec_data_length(&rec);
ret.dptr = tdb_alloc_read(tdb, off + sizeof(rec) + key.dsize,
ret.dsize);
+ if (TDB_PTR_IS_ERR(ret.dptr)) {
+ tdb->ecode = TDB_PTR_ERR(ret.dptr);
+ ret = tdb_null;
+ }
}
tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_RDLCK);
tdb_off_t off;
struct tdb_used_record rec;
struct hash_info h;
+ enum TDB_ERROR ecode;
off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL);
- if (unlikely(off == TDB_OFF_ERR))
+ if (TDB_OFF_IS_ERR(off)) {
+ tdb->ecode = off;
return -1;
+ }
if (!off) {
tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
return -1;
}
- if (delete_from_hash(tdb, &h) == -1)
+ ecode = delete_from_hash(tdb, &h);
+ if (ecode != TDB_SUCCESS) {
+ tdb->ecode = ecode;
goto unlock_err;
+ }
/* Free the deleted entry. */
add_stat(tdb, frees, 1);
- if (add_free_record(tdb, off,
- sizeof(struct tdb_used_record)
- + rec_key_length(&rec)
- + rec_data_length(&rec)
- + rec_extra_padding(&rec)) != 0)
+ ecode = add_free_record(tdb, off,
+ sizeof(struct tdb_used_record)
+ + rec_key_length(&rec)
+ + rec_data_length(&rec)
+ + rec_extra_padding(&rec));
+ if (ecode != TDB_SUCCESS) {
+ tdb->ecode = ecode;
goto unlock_err;
+ }
tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
return 0;