1
0
mirror of https://github.com/samba-team/samba.git synced 2025-01-12 09:18:10 +03:00

Sync util_tdb implementations.

This commit is contained in:
Jelmer Vernooij 2008-10-12 16:53:17 +02:00
parent a52e729f30
commit 7d371c684d
4 changed files with 43 additions and 546 deletions

View File

@ -21,7 +21,6 @@
#include "includes.h"
#include "../tdb/include/tdb.h"
#include "pstring.h"
#include "../lib/util/util_tdb.h"
/* these are little tdb utility functions that are meant to make
@ -31,21 +30,31 @@
Make a TDB_DATA and keep the const warning in one place
****************************************************************/
static TDB_DATA make_tdb_data(const char *dptr, size_t dsize)
TDB_DATA make_tdb_data(const uint8_t *dptr, size_t dsize)
{
TDB_DATA ret;
ret.dptr = discard_const_p(unsigned char, dptr);
ret.dptr = discard_const_p(uint8_t, dptr);
ret.dsize = dsize;
return ret;
}
TDB_DATA string_tdb_data(const char *string)
{
return make_tdb_data((const uint8 *)string, string ? strlen(string) : 0 );
}
TDB_DATA string_term_tdb_data(const char *string)
{
return make_tdb_data((const uint8 *)string, string ? strlen(string) + 1 : 0);
}
/****************************************************************************
Lock a chain by string. Return -1 if lock failed.
****************************************************************************/
int tdb_lock_bystring(struct tdb_context *tdb, const char *keyval)
{
TDB_DATA key = make_tdb_data(keyval, strlen(keyval)+1);
TDB_DATA key = string_term_tdb_data(keyval);
return tdb_chainlock(tdb, key);
}
@ -56,7 +65,7 @@ int tdb_lock_bystring(struct tdb_context *tdb, const char *keyval)
void tdb_unlock_bystring(struct tdb_context *tdb, const char *keyval)
{
TDB_DATA key = make_tdb_data(keyval, strlen(keyval)+1);
TDB_DATA key = string_term_tdb_data(keyval);
tdb_chainunlock(tdb, key);
}
@ -67,7 +76,7 @@ void tdb_unlock_bystring(struct tdb_context *tdb, const char *keyval)
int tdb_read_lock_bystring(struct tdb_context *tdb, const char *keyval)
{
TDB_DATA key = make_tdb_data(keyval, strlen(keyval)+1);
TDB_DATA key = string_term_tdb_data(keyval);
return tdb_chainlock_read(tdb, key);
}
@ -78,7 +87,7 @@ int tdb_read_lock_bystring(struct tdb_context *tdb, const char *keyval)
void tdb_read_unlock_bystring(struct tdb_context *tdb, const char *keyval)
{
TDB_DATA key = make_tdb_data(keyval, strlen(keyval)+1);
TDB_DATA key = string_term_tdb_data(keyval);
tdb_chainunlock_read(tdb, key);
}
@ -89,9 +98,8 @@ void tdb_read_unlock_bystring(struct tdb_context *tdb, const char *keyval)
Output is int32_t in native byte order.
****************************************************************************/
int32_t tdb_fetch_int32_byblob(struct tdb_context *tdb, const char *keyval, size_t len)
int32_t tdb_fetch_int32_byblob(struct tdb_context *tdb, TDB_DATA key)
{
TDB_DATA key = make_tdb_data(keyval, len);
TDB_DATA data;
int32_t ret;
@ -113,7 +121,7 @@ int32_t tdb_fetch_int32_byblob(struct tdb_context *tdb, const char *keyval, size
int32_t tdb_fetch_int32(struct tdb_context *tdb, const char *keystr)
{
return tdb_fetch_int32_byblob(tdb, keystr, strlen(keystr) + 1);
return tdb_fetch_int32_byblob(tdb, string_term_tdb_data(keystr));
}
/****************************************************************************
@ -121,9 +129,8 @@ int32_t tdb_fetch_int32(struct tdb_context *tdb, const char *keystr)
Input is int32_t in native byte order. Output in tdb is in little-endian.
****************************************************************************/
int tdb_store_int32_byblob(struct tdb_context *tdb, const char *keystr, size_t len, int32_t v)
int tdb_store_int32_byblob(struct tdb_context *tdb, TDB_DATA key, int32_t v)
{
TDB_DATA key = make_tdb_data(keystr, len);
TDB_DATA data;
int32_t v_store;
@ -141,7 +148,7 @@ int tdb_store_int32_byblob(struct tdb_context *tdb, const char *keystr, size_t l
int tdb_store_int32(struct tdb_context *tdb, const char *keystr, int32_t v)
{
return tdb_store_int32_byblob(tdb, keystr, strlen(keystr) + 1, v);
return tdb_store_int32_byblob(tdb, string_term_tdb_data(keystr), v);
}
/****************************************************************************
@ -149,9 +156,8 @@ int tdb_store_int32(struct tdb_context *tdb, const char *keystr, int32_t v)
Output is uint32_t in native byte order.
****************************************************************************/
bool tdb_fetch_uint32_byblob(struct tdb_context *tdb, const char *keyval, size_t len, uint32_t *value)
bool tdb_fetch_uint32_byblob(struct tdb_context *tdb, TDB_DATA key, uint32_t *value)
{
TDB_DATA key = make_tdb_data(keyval, len);
TDB_DATA data;
data = tdb_fetch(tdb, key);
@ -172,7 +178,7 @@ bool tdb_fetch_uint32_byblob(struct tdb_context *tdb, const char *keyval, size_t
bool tdb_fetch_uint32(struct tdb_context *tdb, const char *keystr, uint32_t *value)
{
return tdb_fetch_uint32_byblob(tdb, keystr, strlen(keystr) + 1, value);
return tdb_fetch_uint32_byblob(tdb, string_term_tdb_data(keystr), value);
}
/****************************************************************************
@ -180,9 +186,8 @@ bool tdb_fetch_uint32(struct tdb_context *tdb, const char *keystr, uint32_t *val
Input is uint32_t in native byte order. Output in tdb is in little-endian.
****************************************************************************/
bool tdb_store_uint32_byblob(struct tdb_context *tdb, const char *keystr, size_t len, uint32_t value)
bool tdb_store_uint32_byblob(struct tdb_context *tdb, TDB_DATA key, uint32_t value)
{
TDB_DATA key = make_tdb_data(keystr, len);
TDB_DATA data;
uint32_t v_store;
bool ret = true;
@ -204,7 +209,7 @@ bool tdb_store_uint32_byblob(struct tdb_context *tdb, const char *keystr, size_t
bool tdb_store_uint32(struct tdb_context *tdb, const char *keystr, uint32_t value)
{
return tdb_store_uint32_byblob(tdb, keystr, strlen(keystr) + 1, value);
return tdb_store_uint32_byblob(tdb, string_term_tdb_data(keystr), value);
}
/****************************************************************************
Store a buffer by a null terminated string key. Return 0 on success, -1
@ -213,7 +218,7 @@ bool tdb_store_uint32(struct tdb_context *tdb, const char *keystr, uint32_t valu
int tdb_store_bystring(struct tdb_context *tdb, const char *keystr, TDB_DATA data, int flags)
{
TDB_DATA key = make_tdb_data(keystr, strlen(keystr)+1);
TDB_DATA key = string_term_tdb_data(keystr);
return tdb_store(tdb, key, data, flags);
}
@ -225,7 +230,7 @@ int tdb_store_bystring(struct tdb_context *tdb, const char *keystr, TDB_DATA dat
TDB_DATA tdb_fetch_bystring(struct tdb_context *tdb, const char *keystr)
{
TDB_DATA key = make_tdb_data(keystr, strlen(keystr)+1);
TDB_DATA key = string_term_tdb_data(keystr);
return tdb_fetch(tdb, key);
}
@ -236,7 +241,7 @@ TDB_DATA tdb_fetch_bystring(struct tdb_context *tdb, const char *keystr)
int tdb_delete_bystring(struct tdb_context *tdb, const char *keystr)
{
TDB_DATA key = make_tdb_data(keystr, strlen(keystr)+1);
TDB_DATA key = string_term_tdb_data(keystr);
return tdb_delete(tdb, key);
}
@ -333,214 +338,3 @@ int tdb_traverse_delete_fn(struct tdb_context *the_tdb, TDB_DATA key, TDB_DATA d
{
return tdb_delete(the_tdb, key);
}
/****************************************************************************
Useful pair of routines for packing/unpacking data consisting of
integers and strings.
****************************************************************************/
size_t tdb_pack(TDB_CONTEXT *tdb, char *buf, int bufsize, const char *fmt, ...)
{
va_list ap;
uint8_t bt;
uint16_t w;
uint32_t d;
int i;
void *p;
int len;
char *s;
char c;
char *buf0 = buf;
const char *fmt0 = fmt;
int bufsize0 = bufsize;
tdb_log_func log_fn = tdb_log_fn(tdb);
va_start(ap, fmt);
while (*fmt) {
switch ((c = *fmt++)) {
case 'b': /* unsigned 8-bit integer */
len = 1;
bt = (uint8_t)va_arg(ap, int);
if (bufsize && bufsize >= len)
SSVAL(buf, 0, bt);
break;
case 'w': /* unsigned 16-bit integer */
len = 2;
w = (uint16_t)va_arg(ap, int);
if (bufsize && bufsize >= len)
SSVAL(buf, 0, w);
break;
case 'd': /* signed 32-bit integer (standard int in most systems) */
len = 4;
d = va_arg(ap, uint32_t);
if (bufsize && bufsize >= len)
SIVAL(buf, 0, d);
break;
case 'p': /* pointer */
len = 4;
p = va_arg(ap, void *);
d = p?1:0;
if (bufsize && bufsize >= len)
SIVAL(buf, 0, d);
break;
case 'P': /* null-terminated string */
s = va_arg(ap,char *);
w = strlen(s);
len = w + 1;
if (bufsize && bufsize >= len)
memcpy(buf, s, len);
break;
case 'f': /* null-terminated string */
s = va_arg(ap,char *);
w = strlen(s);
len = w + 1;
if (bufsize && bufsize >= len)
memcpy(buf, s, len);
break;
case 'B': /* fixed-length string */
i = va_arg(ap, int);
s = va_arg(ap, char *);
len = 4+i;
if (bufsize && bufsize >= len) {
SIVAL(buf, 0, i);
memcpy(buf+4, s, i);
}
break;
default:
log_fn(tdb, 0,"Unknown tdb_pack format %c in %s\n",
c, fmt);
len = 0;
break;
}
buf += len;
if (bufsize)
bufsize -= len;
if (bufsize < 0)
bufsize = 0;
}
va_end(ap);
log_fn(tdb, 18,"tdb_pack(%s, %d) -> %d\n",
fmt0, bufsize0, (int)PTR_DIFF(buf, buf0));
return PTR_DIFF(buf, buf0);
}
/****************************************************************************
Useful pair of routines for packing/unpacking data consisting of
integers and strings.
****************************************************************************/
int tdb_unpack(TDB_CONTEXT *tdb, char *buf, int bufsize, const char *fmt, ...)
{
va_list ap;
uint8_t *bt;
uint16_t *w;
uint32_t *d;
int len;
int *i;
void **p;
char *s, **b, **ps;
char c;
char *buf0 = buf;
const char *fmt0 = fmt;
int bufsize0 = bufsize;
tdb_log_func log_fn = tdb_log_fn(tdb);
va_start(ap, fmt);
while (*fmt) {
switch ((c=*fmt++)) {
case 'b':
len = 1;
bt = va_arg(ap, uint8_t *);
if (bufsize < len)
goto no_space;
*bt = SVAL(buf, 0);
break;
case 'w':
len = 2;
w = va_arg(ap, uint16_t *);
if (bufsize < len)
goto no_space;
*w = SVAL(buf, 0);
break;
case 'd':
len = 4;
d = va_arg(ap, uint32_t *);
if (bufsize < len)
goto no_space;
*d = IVAL(buf, 0);
break;
case 'p':
len = 4;
p = va_arg(ap, void **);
if (bufsize < len)
goto no_space;
/*
* This isn't a real pointer - only a token (1 or 0)
* to mark the fact a pointer is present.
*/
*p = (void *)(IVAL(buf, 0) ? (void *)1 : NULL);
break;
case 'P':
/* Return a malloc'ed string. */
ps = va_arg(ap,char ** );
len = strlen((const char *)buf) + 1;
*ps = strdup((const char *)buf);
break;
case 'f':
s = va_arg(ap,char *);
len = strlen(buf) + 1;
if (bufsize < len || len > sizeof(fstring))
goto no_space;
memcpy(s, buf, len);
break;
case 'B':
i = va_arg(ap, int *);
b = va_arg(ap, char **);
len = 4;
if (bufsize < len)
goto no_space;
*i = IVAL(buf, 0);
if (! *i) {
*b = NULL;
break;
}
len += *i;
if (bufsize < len)
goto no_space;
*b = (char *)malloc(*i);
if (! *b)
goto no_space;
memcpy(*b, buf+4, *i);
break;
default:
log_fn(tdb, 0, "Unknown tdb_unpack format %c in %s\n",
c, fmt);
len = 0;
break;
}
buf += len;
bufsize -= len;
}
va_end(ap);
log_fn(tdb, 18, "tdb_unpack(%s, %d) -> %d\n",
fmt0, bufsize0, (int)PTR_DIFF(buf, buf0));
return PTR_DIFF(buf, buf0);
no_space:
return -1;
}

View File

@ -227,7 +227,7 @@ MODULES = $(VFS_MODULES) $(PDB_MODULES) $(RPC_MODULES) $(IDMAP_MODULES) \
# object file lists
######################################################################
TDB_LIB_OBJ = lib/util_tdb.o \
TDB_LIB_OBJ = lib/util_tdb.o ../lib/util/util_tdb.o \
lib/dbwrap.o lib/dbwrap_tdb.o \
lib/dbwrap_ctdb.o \
lib/dbwrap_rbt.o @LIBTDB_STATIC@

View File

@ -48,34 +48,34 @@ struct tdb_validation_status {
typedef int (*tdb_validate_data_func)(TDB_CONTEXT *the_tdb, TDB_DATA kbuf, TDB_DATA dbuf, void *state);
TDB_DATA make_tdb_data(const uint8 *dptr, size_t dsize);
TDB_DATA make_tdb_data(const uint8_t *dptr, size_t dsize);
TDB_DATA string_tdb_data(const char *string);
TDB_DATA string_term_tdb_data(const char *string);
TDB_LIST_NODE *tdb_search_keys(struct tdb_context*, const char*);
void tdb_search_list_free(TDB_LIST_NODE*);
int tdb_chainlock_with_timeout( TDB_CONTEXT *tdb, TDB_DATA key,
int tdb_chainlock_with_timeout( struct tdb_context *tdb, TDB_DATA key,
unsigned int timeout);
int tdb_lock_bystring(struct tdb_context *tdb, const char *keyval);
int tdb_lock_bystring_with_timeout(TDB_CONTEXT *tdb, const char *keyval,
int tdb_lock_bystring_with_timeout(struct tdb_context *tdb, const char *keyval,
int timeout);
void tdb_unlock_bystring(struct tdb_context *tdb, const char *keyval);
int tdb_read_lock_bystring_with_timeout(TDB_CONTEXT *tdb, const char *keyval,
unsigned int timeout);
void tdb_read_unlock_bystring(TDB_CONTEXT *tdb, const char *keyval);
void tdb_read_unlock_bystring(struct tdb_context *tdb, const char *keyval);
int32 tdb_fetch_int32_byblob(TDB_CONTEXT *tdb, TDB_DATA key);
int32 tdb_fetch_int32(struct tdb_context *tdb, const char *keystr);
bool tdb_store_uint32_byblob(TDB_CONTEXT *tdb, TDB_DATA key, uint32 value);
bool tdb_store_uint32(struct tdb_context *tdb, const char *keystr, uint32 value);
int tdb_store_int32_byblob(TDB_CONTEXT *tdb, TDB_DATA key, int32 v);
int tdb_store_int32(struct tdb_context *tdb, const char *keystr, int32 v);
bool tdb_fetch_uint32_byblob(TDB_CONTEXT *tdb, TDB_DATA key, uint32 *value);
bool tdb_fetch_uint32(struct tdb_context *tdb, const char *keystr, uint32 *value);
int32 tdb_change_int32_atomic(struct tdb_context *tdb, const char *keystr, int32 *oldval, int32 change_val);
bool tdb_change_uint32_atomic(TDB_CONTEXT *tdb, const char *keystr,
uint32 *oldval, uint32 change_val);
int32_t tdb_fetch_int32_byblob(struct tdb_context *tdb, TDB_DATA key);
int32_t tdb_fetch_int32(struct tdb_context *tdb, const char *keystr);
bool tdb_store_uint32_byblob(struct tdb_context *tdb, TDB_DATA key, uint32_t value);
bool tdb_store_uint32(struct tdb_context *tdb, const char *keystr, uint32_t value);
int tdb_store_int32_byblob(struct tdb_context *tdb, TDB_DATA key, int32_t v);
int tdb_store_int32(struct tdb_context *tdb, const char *keystr, int32_t v);
bool tdb_fetch_uint32_byblob(struct tdb_context *tdb, TDB_DATA key, uint32_t *value);
bool tdb_fetch_uint32(struct tdb_context *tdb, const char *keystr, uint32_t *value);
int32_t tdb_change_int32_atomic(struct tdb_context *tdb, const char *keystr, int32_t *oldval, int32_t change_val);
bool tdb_change_uint32_atomic(struct tdb_context *tdb, const char *keystr,
uint32_t *oldval, uint32_t change_val);
int tdb_store_bystring(struct tdb_context *tdb, const char *keystr, TDB_DATA data, int flags);
int tdb_trans_store_bystring(TDB_CONTEXT *tdb, const char *keystr,

View File

@ -39,28 +39,6 @@ static void gotalarm_sig(void)
gotalarm = 1;
}
/***************************************************************
Make a TDB_DATA and keep the const warning in one place
****************************************************************/
TDB_DATA make_tdb_data(const uint8 *dptr, size_t dsize)
{
TDB_DATA ret;
ret.dptr = CONST_DISCARD(uint8 *, dptr);
ret.dsize = dsize;
return ret;
}
TDB_DATA string_tdb_data(const char *string)
{
return make_tdb_data((const uint8 *)string, string ? strlen(string) : 0 );
}
TDB_DATA string_term_tdb_data(const char *string)
{
return make_tdb_data((const uint8 *)string, string ? strlen(string) + 1 : 0);
}
/****************************************************************************
Lock a chain with timeout (in seconds).
****************************************************************************/
@ -109,17 +87,6 @@ int tdb_chainlock_with_timeout( TDB_CONTEXT *tdb, TDB_DATA key, unsigned int tim
return tdb_chainlock_with_timeout_internal(tdb, key, timeout, F_WRLCK);
}
/****************************************************************************
Lock a chain by string. Return -1 if timeout or lock failed.
****************************************************************************/
int tdb_lock_bystring(TDB_CONTEXT *tdb, const char *keyval)
{
TDB_DATA key = string_term_tdb_data(keyval);
return tdb_chainlock(tdb, key);
}
int tdb_lock_bystring_with_timeout(TDB_CONTEXT *tdb, const char *keyval,
int timeout)
{
@ -128,17 +95,6 @@ int tdb_lock_bystring_with_timeout(TDB_CONTEXT *tdb, const char *keyval,
return tdb_chainlock_with_timeout(tdb, key, timeout);
}
/****************************************************************************
Unlock a chain by string.
****************************************************************************/
void tdb_unlock_bystring(TDB_CONTEXT *tdb, const char *keyval)
{
TDB_DATA key = string_term_tdb_data(keyval);
tdb_chainunlock(tdb, key);
}
/****************************************************************************
Read lock a chain by string. Return -1 if timeout or lock failed.
****************************************************************************/
@ -150,155 +106,8 @@ int tdb_read_lock_bystring_with_timeout(TDB_CONTEXT *tdb, const char *keyval, un
return tdb_chainlock_with_timeout_internal(tdb, key, timeout, F_RDLCK);
}
/****************************************************************************
Read unlock a chain by string.
****************************************************************************/
void tdb_read_unlock_bystring(TDB_CONTEXT *tdb, const char *keyval)
{
TDB_DATA key = string_term_tdb_data(keyval);
tdb_chainunlock_read(tdb, key);
}
/****************************************************************************
Fetch a int32 value by a arbitrary blob key, return -1 if not found.
Output is int32 in native byte order.
****************************************************************************/
int32 tdb_fetch_int32_byblob(TDB_CONTEXT *tdb, TDB_DATA key)
{
TDB_DATA data;
int32 ret;
data = tdb_fetch(tdb, key);
if (!data.dptr || data.dsize != sizeof(int32)) {
SAFE_FREE(data.dptr);
return -1;
}
ret = IVAL(data.dptr,0);
SAFE_FREE(data.dptr);
return ret;
}
/****************************************************************************
Fetch a int32 value by string key, return -1 if not found.
Output is int32 in native byte order.
****************************************************************************/
int32 tdb_fetch_int32(TDB_CONTEXT *tdb, const char *keystr)
{
TDB_DATA key = string_term_tdb_data(keystr);
return tdb_fetch_int32_byblob(tdb, key);
}
/****************************************************************************
Store a int32 value by an arbitary blob key, return 0 on success, -1 on failure.
Input is int32 in native byte order. Output in tdb is in little-endian.
****************************************************************************/
int tdb_store_int32_byblob(TDB_CONTEXT *tdb, TDB_DATA key, int32 v)
{
TDB_DATA data;
int32 v_store;
SIVAL(&v_store,0,v);
data.dptr = (uint8 *)&v_store;
data.dsize = sizeof(int32);
return tdb_store(tdb, key, data, TDB_REPLACE);
}
/****************************************************************************
Store a int32 value by string key, return 0 on success, -1 on failure.
Input is int32 in native byte order. Output in tdb is in little-endian.
****************************************************************************/
int tdb_store_int32(TDB_CONTEXT *tdb, const char *keystr, int32 v)
{
TDB_DATA key = string_term_tdb_data(keystr);
return tdb_store_int32_byblob(tdb, key, v);
}
/****************************************************************************
Fetch a uint32 value by a arbitrary blob key, return -1 if not found.
Output is uint32 in native byte order.
****************************************************************************/
bool tdb_fetch_uint32_byblob(TDB_CONTEXT *tdb, TDB_DATA key, uint32 *value)
{
TDB_DATA data;
data = tdb_fetch(tdb, key);
if (!data.dptr || data.dsize != sizeof(uint32)) {
SAFE_FREE(data.dptr);
return False;
}
*value = IVAL(data.dptr,0);
SAFE_FREE(data.dptr);
return True;
}
/****************************************************************************
Fetch a uint32 value by string key, return -1 if not found.
Output is uint32 in native byte order.
****************************************************************************/
bool tdb_fetch_uint32(TDB_CONTEXT *tdb, const char *keystr, uint32 *value)
{
TDB_DATA key = string_term_tdb_data(keystr);
return tdb_fetch_uint32_byblob(tdb, key, value);
}
/****************************************************************************
Store a uint32 value by an arbitary blob key, return 0 on success, -1 on failure.
Input is uint32 in native byte order. Output in tdb is in little-endian.
****************************************************************************/
bool tdb_store_uint32_byblob(TDB_CONTEXT *tdb, TDB_DATA key, uint32 value)
{
TDB_DATA data;
uint32 v_store;
bool ret = True;
SIVAL(&v_store, 0, value);
data.dptr = (uint8 *)&v_store;
data.dsize = sizeof(uint32);
if (tdb_store(tdb, key, data, TDB_REPLACE) == -1)
ret = False;
return ret;
}
/****************************************************************************
Store a uint32 value by string key, return 0 on success, -1 on failure.
Input is uint32 in native byte order. Output in tdb is in little-endian.
****************************************************************************/
bool tdb_store_uint32(TDB_CONTEXT *tdb, const char *keystr, uint32 value)
{
TDB_DATA key = string_term_tdb_data(keystr);
return tdb_store_uint32_byblob(tdb, key, value);
}
/****************************************************************************
Store a buffer by a null terminated string key. Return 0 on success, -1
on failure.
****************************************************************************/
int tdb_store_bystring(TDB_CONTEXT *tdb, const char *keystr, TDB_DATA data, int flags)
{
TDB_DATA key = string_term_tdb_data(keystr);
return tdb_store(tdb, key, data, flags);
}
int tdb_trans_store_bystring(TDB_CONTEXT *tdb, const char *keystr,
TDB_DATA data, int flags)
@ -308,112 +117,6 @@ int tdb_trans_store_bystring(TDB_CONTEXT *tdb, const char *keystr,
return tdb_trans_store(tdb, key, data, flags);
}
/****************************************************************************
Fetch a buffer using a null terminated string key. Don't forget to call
free() on the result dptr.
****************************************************************************/
TDB_DATA tdb_fetch_bystring(TDB_CONTEXT *tdb, const char *keystr)
{
TDB_DATA key = string_term_tdb_data(keystr);
return tdb_fetch(tdb, key);
}
/****************************************************************************
Delete an entry using a null terminated string key.
****************************************************************************/
int tdb_delete_bystring(TDB_CONTEXT *tdb, const char *keystr)
{
TDB_DATA key = string_term_tdb_data(keystr);
return tdb_delete(tdb, key);
}
/****************************************************************************
Atomic integer change. Returns old value. To create, set initial value in *oldval.
****************************************************************************/
int32 tdb_change_int32_atomic(TDB_CONTEXT *tdb, const char *keystr, int32 *oldval, int32 change_val)
{
int32 val;
int32 ret = -1;
if (tdb_lock_bystring(tdb, keystr) == -1)
return -1;
if ((val = tdb_fetch_int32(tdb, keystr)) == -1) {
/* The lookup failed */
if (tdb_error(tdb) != TDB_ERR_NOEXIST) {
/* but not because it didn't exist */
goto err_out;
}
/* Start with 'old' value */
val = *oldval;
} else {
/* It worked, set return value (oldval) to tdb data */
*oldval = val;
}
/* Increment value for storage and return next time */
val += change_val;
if (tdb_store_int32(tdb, keystr, val) == -1)
goto err_out;
ret = 0;
err_out:
tdb_unlock_bystring(tdb, keystr);
return ret;
}
/****************************************************************************
Atomic unsigned integer change. Returns old value. To create, set initial value in *oldval.
****************************************************************************/
bool tdb_change_uint32_atomic(TDB_CONTEXT *tdb, const char *keystr, uint32 *oldval, uint32 change_val)
{
uint32 val;
bool ret = False;
if (tdb_lock_bystring(tdb, keystr) == -1)
return False;
if (!tdb_fetch_uint32(tdb, keystr, &val)) {
/* It failed */
if (tdb_error(tdb) != TDB_ERR_NOEXIST) {
/* and not because it didn't exist */
goto err_out;
}
/* Start with 'old' value */
val = *oldval;
} else {
/* it worked, set return value (oldval) to tdb data */
*oldval = val;
}
/* get a new value to store */
val += change_val;
if (!tdb_store_uint32(tdb, keystr, val))
goto err_out;
ret = True;
err_out:
tdb_unlock_bystring(tdb, keystr);
return ret;
}
/****************************************************************************
Useful pair of routines for packing/unpacking data consisting of
integers and strings.