Browse Source

ccanize tdb further, and add simple test.

Rusty Russell 16 years ago
parent
commit
46b1a03e21
7 changed files with 64 additions and 5 deletions
  1. 3 3
      ccan/tdb/io.c
  2. 1 0
      ccan/tdb/open.c
  3. 2 0
      ccan/tdb/tdb.h
  4. 6 1
      ccan/tdb/tdb_private.h
  5. 48 0
      ccan/tdb/test/run.c
  6. 1 1
      ccan/tdb/transaction.c
  7. 3 0
      config.h

+ 3 - 3
ccan/tdb/io.c

@@ -189,7 +189,7 @@ int tdb_munmap(struct tdb_context *tdb)
 	if (tdb->flags & TDB_INTERNAL)
 	if (tdb->flags & TDB_INTERNAL)
 		return 0;
 		return 0;
 
 
-#ifdef HAVE_MMAP
+#if HAVE_MMAP
 	if (tdb->map_ptr) {
 	if (tdb->map_ptr) {
 		int ret = munmap(tdb->map_ptr, tdb->map_size);
 		int ret = munmap(tdb->map_ptr, tdb->map_size);
 		if (ret != 0)
 		if (ret != 0)
@@ -205,11 +205,11 @@ void tdb_mmap(struct tdb_context *tdb)
 	if (tdb->flags & TDB_INTERNAL)
 	if (tdb->flags & TDB_INTERNAL)
 		return;
 		return;
 
 
-#ifdef HAVE_MMAP
+#if HAVE_MMAP
 	if (!(tdb->flags & TDB_NOMMAP)) {
 	if (!(tdb->flags & TDB_NOMMAP)) {
 		tdb->map_ptr = mmap(NULL, tdb->map_size, 
 		tdb->map_ptr = mmap(NULL, tdb->map_size, 
 				    PROT_READ|(tdb->read_only? 0:PROT_WRITE), 
 				    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 !!!!
 		 * NB. When mmap fails it returns MAP_FAILED *NOT* NULL !!!!

+ 1 - 0
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;
 		errno = ENOMEM;
 		goto fail;
 		goto fail;
 	}
 	}
+
 	tdb_io_init(tdb);
 	tdb_io_init(tdb);
 	tdb->fd = -1;
 	tdb->fd = -1;
 	tdb->name = NULL;
 	tdb->name = NULL;

+ 2 - 0
ccan/tdb/tdb.h

@@ -33,6 +33,8 @@ extern "C" {
 #ifndef _SAMBA_BUILD_
 #ifndef _SAMBA_BUILD_
 /* For mode_t */
 /* For mode_t */
 #include <sys/types.h>
 #include <sys/types.h>
+/* For O_* flags. */
+#include <sys/stat.h>
 /* For sig_atomic_t. */
 /* For sig_atomic_t. */
 #include <signal.h>
 #include <signal.h>
 #endif
 #endif

+ 6 - 1
ccan/tdb/tdb_private.h

@@ -1,3 +1,5 @@
+#ifndef TDB_PRIVATE_H
+#define TDB_PRIVATE_H
  /* 
  /* 
    Unix SMB/CIFS implementation.
    Unix SMB/CIFS implementation.
 
 
@@ -42,10 +44,12 @@
 #include <string.h>
 #include <string.h>
 #include <errno.h>
 #include <errno.h>
 #include <stdio.h>
 #include <stdio.h>
+#include <utime.h>
+#include "config.h"
 #endif
 #endif
 #include "tdb.h"
 #include "tdb.h"
 
 
-#ifndef HAVE_GETPAGESIZE
+#if HAVE_GETPAGESIZE
 #define getpagesize() 0x2000
 #define getpagesize() 0x2000
 #endif
 #endif
 
 
@@ -225,3 +229,4 @@ int tdb_rec_free_read(struct tdb_context *tdb, tdb_off_t off,
 		      struct list_struct *rec);
 		      struct list_struct *rec);
 
 
 
 
+#endif

+ 48 - 0
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 <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();
+}

+ 1 - 1
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
 	   not be backed up (as tdb rounding to block sizes means that
 	   file size changes are quite rare too). The following forces
 	   file size changes are quite rare too). The following forces
 	   mtime changes when a transaction completes */
 	   mtime changes when a transaction completes */
-#ifdef HAVE_UTIME
+#if HAVE_UTIME
 	utime(tdb->name, NULL);
 	utime(tdb->name, NULL);
 #endif
 #endif
 
 

+ 3 - 0
config.h

@@ -8,3 +8,6 @@
 #define HAVE_BUILTIN_CHOOSE_EXPR 1
 #define HAVE_BUILTIN_CHOOSE_EXPR 1
 #define HAVE_LITTLE_ENDIAN 1
 #define HAVE_LITTLE_ENDIAN 1
 #define HAVE_BIG_ENDIAN 0
 #define HAVE_BIG_ENDIAN 0
+#define HAVE_MMAP 1
+#define HAVE_GETPAGESIZE 1
+#define HAVE_UTIME 1