diff options
author | Simo Sorce <idra@samba.org> | 2002-04-07 22:04:39 +0000 |
---|---|---|
committer | Simo Sorce <idra@samba.org> | 2002-04-07 22:04:39 +0000 |
commit | 68518222a7cbc5621d8b51e691e3725ab576c52b (patch) | |
tree | cfbdf561b5846cde847942afcab1c53b582e63e5 | |
parent | 4f804664801fcc9a9f78bb166f429173c0c85397 (diff) | |
download | samba-68518222a7cbc5621d8b51e691e3725ab576c52b.tar.gz samba-68518222a7cbc5621d8b51e691e3725ab576c52b.tar.bz2 samba-68518222a7cbc5621d8b51e691e3725ab576c52b.zip |
uint32 store and fectch functions, a signed int is not enough sometimes
(This used to be commit f07b2b3d5295202e0c8e530c43fae6d458b2cf2a)
-rw-r--r-- | source3/tdb/tdbutil.c | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/source3/tdb/tdbutil.c b/source3/tdb/tdbutil.c index 793b44d89d..f7cebb58d5 100644 --- a/source3/tdb/tdbutil.c +++ b/source3/tdb/tdbutil.c @@ -112,6 +112,68 @@ int tdb_store_int32(TDB_CONTEXT *tdb, char *keystr, int32 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, char *keyval, size_t len, uint32 *value) +{ + TDB_DATA key, data; + + key.dptr = keyval; + key.dsize = len; + data = tdb_fetch(tdb, key); + if (!data.dptr || data.dsize != sizeof(uint32)) + 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, char *keystr, uint32 *value) +{ + return tdb_fetch_uint32_byblob(tdb, keystr, strlen(keystr) + 1, 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, char *keystr, size_t len, uint32 value) +{ + TDB_DATA key, data; + uint32 v_store; + BOOL ret = True; + + key.dptr = keystr; + key.dsize = len; + SIVAL(&v_store, 0, value); + data.dptr = (void *)&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, char *keystr, uint32 value) +{ + return tdb_store_uint32_byblob(tdb, keystr, strlen(keystr) + 1, value); +} +/**************************************************************************** Store a buffer by a null terminated string key. Return 0 on success, -1 on failure. ****************************************************************************/ @@ -179,6 +241,40 @@ int32 tdb_change_int32_atomic(TDB_CONTEXT *tdb, char *keystr, int32 *oldval, int } /**************************************************************************** + Atomic unsigned integer change. Returns old value. To create, set initial value in *oldval. +****************************************************************************/ + +BOOL tdb_change_uint32_atomic(TDB_CONTEXT *tdb, 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)) { + if (tdb_error(tdb) != TDB_ERR_NOEXIST) + goto err_out; + + val = *oldval; + + } else { + *oldval = val; + 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. ****************************************************************************/ |