summaryrefslogtreecommitdiff
path: root/source3/tdb
diff options
context:
space:
mode:
authorJeremy Allison <jra@samba.org>2001-05-25 00:48:28 +0000
committerJeremy Allison <jra@samba.org>2001-05-25 00:48:28 +0000
commit15e66ba37ae56787897e49039e217b5426924829 (patch)
tree7b0c9c15a4e24f2df40691bd4a0fa917344a0216 /source3/tdb
parent9ff6634db923da17b0946141abf3ce7df61a0dab (diff)
downloadsamba-15e66ba37ae56787897e49039e217b5426924829.tar.gz
samba-15e66ba37ae56787897e49039e217b5426924829.tar.bz2
samba-15e66ba37ae56787897e49039e217b5426924829.zip
Added tdb_change_int_atomic() to allow atomic updates of a tdb int value.
Jeremy. (This used to be commit cf5015f15935605cf69078bc15251db61ddc48c7)
Diffstat (limited to 'source3/tdb')
-rw-r--r--source3/tdb/tdbutil.c35
1 files changed, 32 insertions, 3 deletions
diff --git a/source3/tdb/tdbutil.c b/source3/tdb/tdbutil.c
index a620f085a9..40f5a1246d 100644
--- a/source3/tdb/tdbutil.c
+++ b/source3/tdb/tdbutil.c
@@ -46,9 +46,6 @@ void tdb_unlock_bystring(TDB_CONTEXT *tdb, char *keyval)
tdb_chainunlock(tdb, key);
}
-/* lock a chain by string key */
-
-
/* fetch a value by a arbitrary blob key, return -1 if not found */
int tdb_fetch_int_byblob(TDB_CONTEXT *tdb, char *keyval, size_t len)
{
@@ -107,6 +104,7 @@ int tdb_store_by_string(TDB_CONTEXT *tdb, char *keystr, void *buffer, int len)
/* Fetch a buffer using a null terminated string key. Don't forget to call
free() on the result dptr. */
+
TDB_DATA tdb_fetch_by_string(TDB_CONTEXT *tdb, char *keystr)
{
TDB_DATA key;
@@ -117,6 +115,37 @@ TDB_DATA tdb_fetch_by_string(TDB_CONTEXT *tdb, char *keystr)
return tdb_fetch(tdb, key);
}
+/* Atomic integer change. Returns old value. To create, set initial value in *oldval. */
+
+int tdb_change_int_atomic(TDB_CONTEXT *tdb, char *keystr, int *oldval, int change_val)
+{
+ int val;
+ int ret = -1;
+
+ if (tdb_lock_bystring(tdb, keystr) == -1)
+ return -1;
+
+ if ((val = tdb_fetch_int(tdb, keystr)) == -1) {
+ if (tdb_error(tdb) != TDB_ERR_NOEXIST)
+ goto err_out;
+
+ val = *oldval;
+
+ } else {
+ *oldval = val;
+ val += change_val;
+ }
+
+ if (tdb_store_int(tdb, keystr, val) == -1)
+ goto err_out;
+
+ ret = 0;
+
+ err_out:
+
+ tdb_unlock_bystring(tdb, keystr);
+ return ret;
+}
/* useful pair of routines for packing/unpacking data consisting of
integers and strings */