X-Git-Url: http://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Ftdb2%2Ftdb.c;h=42e75f9d1840b8f79875f01ac05b6637a6d9f358;hp=f6ca01657518af29c1111700c530abda29ba7d43;hb=ed81f39468c4d9089310fb4950b09c1f5886c4ef;hpb=a39bf3aca1b60365efaf8b1eeb2b2c58b09ffab6 diff --git a/ccan/tdb2/tdb.c b/ccan/tdb2/tdb.c index f6ca0165..42e75f9d 100644 --- a/ccan/tdb2/tdb.c +++ b/ccan/tdb2/tdb.c @@ -1,4 +1,5 @@ #include "private.h" +#include #include #include #include @@ -111,6 +112,7 @@ static enum TDB_ERROR tdb_new_database(struct tdb_context *tdb, newdb.hdr.hash_seed, tdb->hash_priv); newdb.hdr.recovery = 0; + newdb.hdr.features_used = newdb.hdr.features_offered = TDB_FEATURE_MASK; memset(newdb.hdr.reserved, 0, sizeof(newdb.hdr.reserved)); /* Initial hashes are empty. */ memset(newdb.hdr.hashtable, 0, sizeof(newdb.hdr.hashtable)); @@ -236,6 +238,13 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags, attr = attr->base.next; } + if (tdb_flags & ~(TDB_INTERNAL | TDB_NOLOCK | TDB_NOMMAP | TDB_CONVERT + | TDB_NOSYNC)) { + ecode = tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR, + "tdb_open: unknown flags %u", tdb_flags); + goto fail; + } + if ((open_flags & O_ACCMODE) == O_WRONLY) { ecode = tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR, "tdb_open: can't open tdb %s write-only", @@ -353,6 +362,16 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags, goto fail; } + /* Clear any features we don't understand. */ + if ((open_flags & O_ACCMODE) != O_RDONLY) { + hdr.features_used &= TDB_FEATURE_MASK; + if (tdb_write_convert(tdb, offsetof(struct tdb_header, + features_used), + &hdr.features_used, + sizeof(hdr.features_used)) == -1) + goto fail; + } + tdb->device = st.st_dev; tdb->inode = st.st_ino; tdb_unlock_open(tdb); @@ -491,6 +510,21 @@ static enum TDB_ERROR replace_data(struct tdb_context *tdb, return TDB_SUCCESS; } +static enum TDB_ERROR update_data(struct tdb_context *tdb, + tdb_off_t off, + struct tdb_data dbuf, + tdb_len_t extra) +{ + enum TDB_ERROR ecode; + + ecode = tdb->methods->twrite(tdb, off, dbuf.dptr, dbuf.dsize); + if (ecode == TDB_SUCCESS && extra) { + /* Put a zero in; future versions may append other data. */ + ecode = tdb->methods->twrite(tdb, off + dbuf.dsize, "", 1); + } + return ecode; +} + enum TDB_ERROR tdb_store(struct tdb_context *tdb, struct tdb_data key, struct tdb_data dbuf, int flag) { @@ -523,11 +557,10 @@ enum TDB_ERROR tdb_store(struct tdb_context *tdb, if (ecode != TDB_SUCCESS) { goto out; } - ecode = tdb->methods->twrite(tdb, - off + sizeof(rec) - + key.dsize, - dbuf.dptr, - dbuf.dsize); + ecode = update_data(tdb, + off + sizeof(rec) + + key.dsize, dbuf, + old_room - dbuf.dsize); if (ecode != TDB_SUCCESS) { goto out; } @@ -583,8 +616,8 @@ enum TDB_ERROR tdb_append(struct tdb_context *tdb, } off += sizeof(rec) + key.dsize + old_dlen; - ecode = tdb->methods->twrite(tdb, off, dbuf.dptr, - dbuf.dsize); + ecode = update_data(tdb, off, dbuf, + rec_extra_padding(&rec)); goto out; } @@ -756,23 +789,18 @@ enum TDB_ERROR COLD tdb_logerr(struct tdb_context *tdb, if (!tdb->logfn) return ecode; - /* FIXME: Doesn't assume asprintf. */ va_start(ap, fmt); - len = vsnprintf(NULL, 0, fmt, ap); + len = vasprintf(&message, fmt, ap); va_end(ap); - message = malloc(len + 1); - if (!message) { + if (len < 0) { tdb->logfn(tdb, TDB_LOG_ERROR, tdb->log_private, "out of memory formatting message:"); tdb->logfn(tdb, level, tdb->log_private, fmt); - return ecode; + } else { + tdb->logfn(tdb, level, tdb->log_private, message); + free(message); } - va_start(ap, fmt); - len = vsprintf(message, fmt, ap); - va_end(ap); - tdb->logfn(tdb, level, tdb->log_private, message); - free(message); errno = saved_errno; return ecode; }