ccanize tdb further, and add simple test.
authorRusty Russell <rusty@rustcorp.com.au>
Mon, 22 Jun 2009 11:17:39 +0000 (20:47 +0930)
committerRusty Russell <rusty@rustcorp.com.au>
Mon, 22 Jun 2009 11:17:39 +0000 (20:47 +0930)
ccan/tdb/io.c
ccan/tdb/open.c
ccan/tdb/tdb.h
ccan/tdb/tdb_private.h
ccan/tdb/test/run.c [new file with mode: 0644]
ccan/tdb/transaction.c
config.h

index 3bf64443535d6080ed9e7f29a97849b627300ac5..c25f1cb447f8578ea58ca2a68c81412f311bc38b 100644 (file)
@@ -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 !!!!
index b19e4cea293df769a588db89c71df25529d677ec..8cd25cc5b5bc7802a9ebc3e7ad2ce0b7df4c9c28 100644 (file)
@@ -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;
index f1805ecddbd09c4011788d15f551b8dba31ad0d8..a2443ff4539232cdbfe3f143f56b7b2ae740967f 100644 (file)
@@ -33,6 +33,8 @@ extern "C" {
 #ifndef _SAMBA_BUILD_
 /* For mode_t */
 #include <sys/types.h>
+/* For O_* flags. */
+#include <sys/stat.h>
 /* For sig_atomic_t. */
 #include <signal.h>
 #endif
index 9942e503da7e4298535b3cf7f87b67d7cffaab94..c460af4e8dd87d0219dab7e5c1a88089488846fd 100644 (file)
@@ -1,3 +1,5 @@
+#ifndef TDB_PRIVATE_H
+#define TDB_PRIVATE_H
  /* 
    Unix SMB/CIFS implementation.
 
 #include <string.h>
 #include <errno.h>
 #include <stdio.h>
+#include <utime.h>
+#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 (file)
index 0000000..865eef8
--- /dev/null
@@ -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 <stdlib.h>
+#include <err.h>
+
+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();
+}
index 6a34c4526993d73d25a13778474739a3afa0b971..d6db8b01329913e09dbd55e6cec82aa61b0d4f9f 100644 (file)
@@ -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
 
index e63fe2482e1503ec1f36211cc89bb8b5584bab88..54291b4b9565c30986d5a75938f13e9ea6dd146d 100644 (file)
--- 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