From: Rusty Russell Date: Mon, 22 Jun 2009 11:17:39 +0000 (+0930) Subject: ccanize tdb further, and add simple test. X-Git-Url: https://git.ozlabs.org/?p=ccan;a=commitdiff_plain;h=46b1a03e21303e03b68de213b41c0840767fbc96;hp=2c67a284d11b5b030d8095df7ff2b31275d7d29c ccanize tdb further, and add simple test. --- diff --git a/ccan/tdb/io.c b/ccan/tdb/io.c index 3bf64443..c25f1cb4 100644 --- a/ccan/tdb/io.c +++ b/ccan/tdb/io.c @@ -189,7 +189,7 @@ int tdb_munmap(struct tdb_context *tdb) if (tdb->flags & TDB_INTERNAL) return 0; -#ifdef HAVE_MMAP +#if HAVE_MMAP if (tdb->map_ptr) { int ret = munmap(tdb->map_ptr, tdb->map_size); if (ret != 0) @@ -205,11 +205,11 @@ void tdb_mmap(struct tdb_context *tdb) if (tdb->flags & TDB_INTERNAL) return; -#ifdef HAVE_MMAP +#if HAVE_MMAP if (!(tdb->flags & TDB_NOMMAP)) { tdb->map_ptr = mmap(NULL, tdb->map_size, PROT_READ|(tdb->read_only? 0:PROT_WRITE), - MAP_SHARED|MAP_FILE, tdb->fd, 0); + MAP_SHARED, tdb->fd, 0); /* * NB. When mmap fails it returns MAP_FAILED *NOT* NULL !!!! diff --git a/ccan/tdb/open.c b/ccan/tdb/open.c index b19e4cea..8cd25cc5 100644 --- a/ccan/tdb/open.c +++ b/ccan/tdb/open.c @@ -159,6 +159,7 @@ struct tdb_context *tdb_open_ex(const char *name, int hash_size, int tdb_flags, errno = ENOMEM; goto fail; } + tdb_io_init(tdb); tdb->fd = -1; tdb->name = NULL; diff --git a/ccan/tdb/tdb.h b/ccan/tdb/tdb.h index f1805ecd..a2443ff4 100644 --- a/ccan/tdb/tdb.h +++ b/ccan/tdb/tdb.h @@ -33,6 +33,8 @@ extern "C" { #ifndef _SAMBA_BUILD_ /* For mode_t */ #include +/* For O_* flags. */ +#include /* For sig_atomic_t. */ #include #endif diff --git a/ccan/tdb/tdb_private.h b/ccan/tdb/tdb_private.h index 9942e503..c460af4e 100644 --- a/ccan/tdb/tdb_private.h +++ b/ccan/tdb/tdb_private.h @@ -1,3 +1,5 @@ +#ifndef TDB_PRIVATE_H +#define TDB_PRIVATE_H /* Unix SMB/CIFS implementation. @@ -42,10 +44,12 @@ #include #include #include +#include +#include "config.h" #endif #include "tdb.h" -#ifndef HAVE_GETPAGESIZE +#if HAVE_GETPAGESIZE #define getpagesize() 0x2000 #endif @@ -225,3 +229,4 @@ int tdb_rec_free_read(struct tdb_context *tdb, tdb_off_t off, struct list_struct *rec); +#endif diff --git a/ccan/tdb/test/run.c b/ccan/tdb/test/run.c new file mode 100644 index 00000000..865eef8e --- /dev/null +++ b/ccan/tdb/test/run.c @@ -0,0 +1,48 @@ +#define _XOPEN_SOURCE 500 +#include "tdb/tdb.h" +#include "tdb/io.c" +#include "tdb/tdb.c" +#include "tdb/lock.c" +#include "tdb/freelist.c" +#include "tdb/traverse.c" +#include "tdb/transaction.c" +#include "tdb/error.c" +#include "tdb/open.c" +#include "tap/tap.h" +#include +#include + +int main(int argc, char *argv[]) +{ + struct tdb_context *tdb; + TDB_DATA key, data; + + plan_tests(10); + tdb = tdb_open("/tmp/test.tdb", 1024, TDB_CLEAR_IF_FIRST, + O_CREAT|O_TRUNC|O_RDWR, 0600); + + ok1(tdb); + key.dsize = strlen("hi"); + key.dptr = (void *)"hi"; + data.dsize = strlen("world"); + data.dptr = (void *)"world"; + + ok1(tdb_store(tdb, key, data, TDB_MODIFY) < 0); + ok1(tdb_error(tdb) == TDB_ERR_NOEXIST); + ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0); + ok1(tdb_store(tdb, key, data, TDB_INSERT) < 0); + ok1(tdb_error(tdb) == TDB_ERR_EXISTS); + ok1(tdb_store(tdb, key, data, TDB_MODIFY) == 0); + + data = tdb_fetch(tdb, key); + ok1(data.dsize == strlen("world")); + ok1(memcmp(data.dptr, "world", strlen("world")) == 0); + free(data.dptr); + + key.dsize++; + data = tdb_fetch(tdb, key); + ok1(data.dptr == NULL); + tdb_close(tdb); + + return exit_status(); +} diff --git a/ccan/tdb/transaction.c b/ccan/tdb/transaction.c index 6a34c452..d6db8b01 100644 --- a/ccan/tdb/transaction.c +++ b/ccan/tdb/transaction.c @@ -989,7 +989,7 @@ int tdb_transaction_commit(struct tdb_context *tdb) not be backed up (as tdb rounding to block sizes means that file size changes are quite rare too). The following forces mtime changes when a transaction completes */ -#ifdef HAVE_UTIME +#if HAVE_UTIME utime(tdb->name, NULL); #endif diff --git a/config.h b/config.h index e63fe248..54291b4b 100644 --- a/config.h +++ b/config.h @@ -8,3 +8,6 @@ #define HAVE_BUILTIN_CHOOSE_EXPR 1 #define HAVE_LITTLE_ENDIAN 1 #define HAVE_BIG_ENDIAN 0 +#define HAVE_MMAP 1 +#define HAVE_GETPAGESIZE 1 +#define HAVE_UTIME 1