summaryrefslogtreecommitdiff
path: root/source4/lib/gencache.c
diff options
context:
space:
mode:
authorAndrew Tridgell <tridge@samba.org>2004-10-16 13:47:00 +0000
committerGerald (Jerry) Carter <jerry@samba.org>2007-10-10 12:59:56 -0500
commit12ea0fd34cec2d7b6d8c8374dfd95728112585b3 (patch)
tree108bca68e9642ac4bb301a9f74e8e73d09c08abc /source4/lib/gencache.c
parent645067efc69ff510612f7b63cf262cc9d60df700 (diff)
downloadsamba-12ea0fd34cec2d7b6d8c8374dfd95728112585b3.tar.gz
samba-12ea0fd34cec2d7b6d8c8374dfd95728112585b3.tar.bz2
samba-12ea0fd34cec2d7b6d8c8374dfd95728112585b3.zip
r3005: added talloc wrappers around tdb_open() and ldb_connect(), so that the
caller doesn't have to worry about the constraint of only opening a database a single time in a process. These wrappers will ensure that only a single open is done, and will auto-close when the last instance is gone. When you are finished with a database pointer, use talloc_free() to close it. note that this code does not take account of the threads process model, and does not yet take account of symlinks or hard links to tdb files. (This used to be commit 04e1171996612ddb15f84134cadded68f0d173b2)
Diffstat (limited to 'source4/lib/gencache.c')
-rw-r--r--source4/lib/gencache.c27
1 files changed, 13 insertions, 14 deletions
diff --git a/source4/lib/gencache.c b/source4/lib/gencache.c
index 1a66d8dc3d..a176dc5ae8 100644
--- a/source4/lib/gencache.c
+++ b/source4/lib/gencache.c
@@ -29,7 +29,7 @@
#define TIMEOUT_LEN 12
#define CACHE_DATA_FMT "%12u/%s"
-static TDB_CONTEXT *cache;
+static struct tdb_wrap *cache;
/**
* @file gencache.c
@@ -62,8 +62,8 @@ BOOL gencache_init(void)
return False;
}
- cache = tdb_open_log(cache_fname, 0, TDB_DEFAULT,
- O_RDWR|O_CREAT, 0644);
+ cache = tdb_wrap_open(NULL, cache_fname, 0, TDB_DEFAULT,
+ O_RDWR|O_CREAT, 0644);
SAFE_FREE(cache_fname);
if (!cache) {
@@ -83,10 +83,10 @@ BOOL gencache_init(void)
BOOL gencache_shutdown(void)
{
- /* tdb_close routine returns -1 on error */
if (!cache) return False;
DEBUG(5, ("Closing cache file\n"));
- return tdb_close(cache) != -1;
+ talloc_free(cache);
+ return True;
}
@@ -125,7 +125,7 @@ BOOL gencache_set(const char *keystr, const char *value, time_t timeout)
= %s (%d seconds %s)\n", keybuf.dptr, value, ctime(&timeout),
(int)(timeout - time(NULL)), timeout > time(NULL) ? "ahead" : "in the past"));
- ret = tdb_store(cache, keybuf, databuf, 0);
+ ret = tdb_store(cache->tdb, keybuf, databuf, 0);
SAFE_FREE(valstr);
SAFE_FREE(keybuf.dptr);
SAFE_FREE(databuf.dptr);
@@ -178,7 +178,7 @@ BOOL gencache_set_only(const char *keystr, const char *valstr, time_t timeout)
timeout > time(NULL) ? "ahead" : "in the past"));
- ret = tdb_store(cache, keybuf, databuf, TDB_REPLACE);
+ ret = tdb_store(cache->tdb, keybuf, databuf, TDB_REPLACE);
SAFE_FREE(datastr);
SAFE_FREE(old_valstr);
@@ -211,7 +211,7 @@ BOOL gencache_del(const char *keystr)
keybuf.dptr = strdup(keystr);
keybuf.dsize = strlen(keystr)+1;
DEBUG(10, ("Deleting cache entry (key = %s)\n", keystr));
- ret = tdb_delete(cache, keybuf);
+ ret = tdb_delete(cache->tdb, keybuf);
SAFE_FREE(keybuf.dptr);
return ret == 0;
@@ -243,7 +243,7 @@ BOOL gencache_get(const char *keystr, char **valstr, time_t *timeout)
keybuf.dptr = strdup(keystr);
keybuf.dsize = strlen(keystr)+1;
- databuf = tdb_fetch(cache, keybuf);
+ databuf = tdb_fetch(cache->tdb, keybuf);
SAFE_FREE(keybuf.dptr);
if (databuf.dptr && databuf.dsize > TIMEOUT_LEN) {
@@ -317,7 +317,7 @@ void gencache_iterate(void (*fn)(const char* key, const char *value, time_t time
if (!gencache_init()) return;
DEBUG(5, ("Searching cache keys with pattern %s\n", keystr_pattern));
- node = tdb_search_keys(cache, keystr_pattern);
+ node = tdb_search_keys(cache->tdb, keystr_pattern);
first_node = node;
while (node) {
@@ -328,7 +328,7 @@ void gencache_iterate(void (*fn)(const char* key, const char *value, time_t time
* We don't use gencache_get function, because we need to iterate through
* all of the entries. Validity verification is up to fn routine.
*/
- databuf = tdb_fetch(cache, node->node_key);
+ databuf = tdb_fetch(cache->tdb, node->node_key);
if (!databuf.dptr || databuf.dsize <= TIMEOUT_LEN) {
SAFE_FREE(databuf.dptr);
SAFE_FREE(keystr);
@@ -360,7 +360,7 @@ void gencache_iterate(void (*fn)(const char* key, const char *value, time_t time
int gencache_lock_entry( const char *key )
{
- return tdb_lock_bystring(cache, key, 0);
+ return tdb_lock_bystring(cache->tdb, key, 0);
}
/********************************************************************
@@ -369,8 +369,7 @@ int gencache_lock_entry( const char *key )
void gencache_unlock_entry( const char *key )
{
- tdb_unlock_bystring(cache, key);
- return;
+ tdb_unlock_bystring(cache->tdb, key);
}