Commit 46b1a03e authored by Rusty Russell's avatar Rusty Russell

ccanize tdb further, and add simple test.

parent 2c67a284
...@@ -189,7 +189,7 @@ int tdb_munmap(struct tdb_context *tdb) ...@@ -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) ...@@ -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 !!!!
......
...@@ -159,6 +159,7 @@ struct tdb_context *tdb_open_ex(const char *name, int hash_size, int tdb_flags, ...@@ -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;
......
...@@ -33,6 +33,8 @@ extern "C" { ...@@ -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
......
#ifndef TDB_PRIVATE_H
#define TDB_PRIVATE_H
/* /*
Unix SMB/CIFS implementation. Unix SMB/CIFS implementation.
...@@ -42,10 +44,12 @@ ...@@ -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, ...@@ -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
#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();
}
...@@ -989,7 +989,7 @@ int tdb_transaction_commit(struct tdb_context *tdb) ...@@ -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
......
...@@ -8,3 +8,6 @@ ...@@ -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
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment