X-Git-Url: https://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Ftdb2%2Ftdb.c;h=254aac5f25a94e1daae9692ea03a9c42b1a6ddce;hp=a1ec332a09b1af790cff0934fac37b084dfae914;hb=f6067e4cbd7b7415571f12438aec00faec5657fb;hpb=41e027e11e0b15100e50c14e49aaa4c2b418f431 diff --git a/ccan/tdb2/tdb.c b/ccan/tdb2/tdb.c index a1ec332a..254aac5f 100644 --- a/ccan/tdb2/tdb.c +++ b/ccan/tdb2/tdb.c @@ -2,9 +2,6 @@ #include #include -/* The null return. */ -struct tdb_data tdb_null = { .dptr = NULL, .dsize = 0 }; - static enum TDB_ERROR update_rec_hdr(struct tdb_context *tdb, tdb_off_t off, tdb_len_t keylen, @@ -66,7 +63,9 @@ static enum TDB_ERROR replace_data(struct tdb_context *tdb, return ecode; } - /* FIXME: tdb_increment_seqnum(tdb); */ + if (tdb->flags & TDB_SEQNUM) + tdb_inc_seqnum(tdb); + return TDB_SUCCESS; } @@ -82,6 +81,9 @@ static enum TDB_ERROR update_data(struct tdb_context *tdb, /* Put a zero in; future versions may append other data. */ ecode = tdb->methods->twrite(tdb, off + dbuf.dsize, "", 1); } + if (tdb->flags & TDB_SEQNUM) + tdb_inc_seqnum(tdb); + return ecode; } @@ -96,7 +98,7 @@ enum TDB_ERROR tdb_store(struct tdb_context *tdb, off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL); if (TDB_OFF_IS_ERR(off)) { - return off; + return tdb->last_error = off; } /* Now we have lock on this hash bucket. */ @@ -126,7 +128,7 @@ enum TDB_ERROR tdb_store(struct tdb_context *tdb, } tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK); - return TDB_SUCCESS; + return tdb->last_error = TDB_SUCCESS; } } else { if (flag == TDB_MODIFY) { @@ -143,7 +145,7 @@ enum TDB_ERROR tdb_store(struct tdb_context *tdb, ecode = replace_data(tdb, &h, key, dbuf, off, old_room, off); out: tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK); - return ecode; + return tdb->last_error = ecode; } enum TDB_ERROR tdb_append(struct tdb_context *tdb, @@ -159,7 +161,7 @@ enum TDB_ERROR tdb_append(struct tdb_context *tdb, off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL); if (TDB_OFF_IS_ERR(off)) { - return off; + return tdb->last_error = off; } if (off) { @@ -211,7 +213,7 @@ out_free_newdata: free(newdata); out: tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK); - return ecode; + return tdb->last_error = ecode; } enum TDB_ERROR tdb_fetch(struct tdb_context *tdb, struct tdb_data key, @@ -224,7 +226,7 @@ enum TDB_ERROR tdb_fetch(struct tdb_context *tdb, struct tdb_data key, off = find_and_lock(tdb, key, F_RDLCK, &h, &rec, NULL); if (TDB_OFF_IS_ERR(off)) { - return off; + return tdb->last_error = off; } if (!off) { @@ -240,7 +242,24 @@ enum TDB_ERROR tdb_fetch(struct tdb_context *tdb, struct tdb_data key, } tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_RDLCK); - return ecode; + return tdb->last_error = ecode; +} + +bool tdb_exists(struct tdb_context *tdb, TDB_DATA key) +{ + tdb_off_t off; + struct tdb_used_record rec; + struct hash_info h; + + off = find_and_lock(tdb, key, F_RDLCK, &h, &rec, NULL); + if (TDB_OFF_IS_ERR(off)) { + tdb->last_error = off; + return false; + } + tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_RDLCK); + + tdb->last_error = TDB_SUCCESS; + return off ? true : false; } enum TDB_ERROR tdb_delete(struct tdb_context *tdb, struct tdb_data key) @@ -252,7 +271,7 @@ enum TDB_ERROR tdb_delete(struct tdb_context *tdb, struct tdb_data key) off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL); if (TDB_OFF_IS_ERR(off)) { - return off; + return tdb->last_error = off; } if (!off) { @@ -273,9 +292,12 @@ enum TDB_ERROR tdb_delete(struct tdb_context *tdb, struct tdb_data key) + rec_data_length(&rec) + rec_extra_padding(&rec)); + if (tdb->flags & TDB_SEQNUM) + tdb_inc_seqnum(tdb); + unlock: tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK); - return ecode; + return tdb->last_error = ecode; } unsigned int tdb_get_flags(struct tdb_context *tdb) @@ -286,8 +308,9 @@ unsigned int tdb_get_flags(struct tdb_context *tdb) void tdb_add_flag(struct tdb_context *tdb, unsigned flag) { if (tdb->flags & TDB_INTERNAL) { - tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR, - "tdb_add_flag: internal db"); + tdb->last_error = tdb_logerr(tdb, TDB_ERR_EINVAL, + TDB_LOG_USE_ERROR, + "tdb_add_flag: internal db"); return; } switch (flag) { @@ -301,17 +324,23 @@ void tdb_add_flag(struct tdb_context *tdb, unsigned flag) case TDB_NOSYNC: tdb->flags |= TDB_NOSYNC; break; + case TDB_SEQNUM: + tdb->flags |= TDB_SEQNUM; + break; default: - tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR, - "tdb_add_flag: Unknown flag %u", flag); + tdb->last_error = tdb_logerr(tdb, TDB_ERR_EINVAL, + TDB_LOG_USE_ERROR, + "tdb_add_flag: Unknown flag %u", + flag); } } void tdb_remove_flag(struct tdb_context *tdb, unsigned flag) { if (tdb->flags & TDB_INTERNAL) { - tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR, - "tdb_remove_flag: internal db"); + tdb->last_error = tdb_logerr(tdb, TDB_ERR_EINVAL, + TDB_LOG_USE_ERROR, + "tdb_remove_flag: internal db"); return; } switch (flag) { @@ -325,9 +354,14 @@ void tdb_remove_flag(struct tdb_context *tdb, unsigned flag) case TDB_NOSYNC: tdb->flags &= ~TDB_NOSYNC; break; + case TDB_SEQNUM: + tdb->flags &= ~TDB_SEQNUM; + break; default: - tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR, - "tdb_remove_flag: Unknown flag %u", flag); + tdb->last_error = tdb_logerr(tdb, TDB_ERR_EINVAL, + TDB_LOG_USE_ERROR, + "tdb_remove_flag: Unknown flag %u", + flag); } } @@ -377,3 +411,62 @@ enum TDB_ERROR COLD tdb_logerr(struct tdb_context *tdb, errno = saved_errno; return ecode; } + +enum TDB_ERROR tdb_parse_record_(struct tdb_context *tdb, + TDB_DATA key, + enum TDB_ERROR (*parse)(TDB_DATA key, + TDB_DATA data, + void *p), + void *p) +{ + tdb_off_t off; + struct tdb_used_record rec; + struct hash_info h; + TDB_DATA data; + enum TDB_ERROR ecode; + + off = find_and_lock(tdb, key, F_RDLCK, &h, &rec, NULL); + if (TDB_OFF_IS_ERR(off)) { + return tdb->last_error = off; + } + + if (!off) { + ecode = TDB_ERR_NOEXIST; + } else { + data.dsize = rec_data_length(&rec); + data.dptr = (void *)tdb_access_read(tdb, + off + sizeof(rec) + + key.dsize, + data.dsize, false); + if (TDB_PTR_IS_ERR(data.dptr)) { + ecode = TDB_PTR_ERR(data.dptr); + } else { + ecode = parse(key, data, p); + tdb_access_release(tdb, data.dptr); + } + } + + tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_RDLCK); + return tdb->last_error = ecode; +} + +const char *tdb_name(const struct tdb_context *tdb) +{ + return tdb->name; +} + +int64_t tdb_get_seqnum(struct tdb_context *tdb) +{ + tdb_off_t off = tdb_read_off(tdb, offsetof(struct tdb_header, seqnum)); + if (TDB_OFF_IS_ERR(off)) + tdb->last_error = off; + else + tdb->last_error = TDB_SUCCESS; + return off; +} + + +int tdb_fd(const struct tdb_context *tdb) +{ + return tdb->file->fd; +}