summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRusty Russell <rusty@rustcorp.com.au>2012-06-22 09:44:42 +0930
committerRusty Russell <rusty@rustcorp.com.au>2012-06-22 07:35:17 +0200
commit02bacf1f95046163dfb5afb40f33b37ccdf1f374 (patch)
tree3e6099c3ae408aec1a771cca028a91955c877794
parent348159d1e4c11c330a75600269b1d45be70d8bb2 (diff)
downloadsamba-02bacf1f95046163dfb5afb40f33b37ccdf1f374.tar.gz
samba-02bacf1f95046163dfb5afb40f33b37ccdf1f374.tar.bz2
samba-02bacf1f95046163dfb5afb40f33b37ccdf1f374.zip
util: util_ntdb ntdb_fetch_int32/ntdb_store_int32 and ntdb_add_int32_atomic
Similar to the util_tdb versions, but return the error code. ntdb_add_int32_atomic seems a clearer name than tdb_change_int32_atomic. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
-rw-r--r--lib/util/util_ntdb.c65
-rw-r--r--lib/util/util_ntdb.h24
2 files changed, 89 insertions, 0 deletions
diff --git a/lib/util/util_ntdb.c b/lib/util/util_ntdb.c
index ad9e0a73d2..be720e55a6 100644
--- a/lib/util/util_ntdb.c
+++ b/lib/util/util_ntdb.c
@@ -237,6 +237,71 @@ enum NTDB_ERROR ntdb_fetch_bystring(struct ntdb_context *ntdb,
return ntdb_fetch(ntdb, key, data);
}
+enum NTDB_ERROR ntdb_fetch_int32(struct ntdb_context *ntdb,
+ const char *keystr, int32_t *val)
+{
+ NTDB_DATA data;
+ enum NTDB_ERROR err;
+
+ err = ntdb_fetch(ntdb, string_term_ntdb_data(keystr), &data);
+ if (err == NTDB_SUCCESS) {
+ if (data.dsize != sizeof(*val)) {
+ err = NTDB_ERR_CORRUPT;
+ } else {
+ *val = IVAL(data.dptr,0);
+ }
+ talloc_free(data.dptr);
+ }
+ return NTDB_SUCCESS;
+}
+
+enum NTDB_ERROR ntdb_store_int32(struct ntdb_context *ntdb,
+ const char *keystr, int32_t val)
+{
+ NTDB_DATA data, key;
+ int32_t v_store;
+
+ SIVAL(&v_store, 0, val);
+ data = ntdb_mkdata(&v_store, sizeof(v_store));
+ key = string_term_ntdb_data(keystr);
+
+ return ntdb_store(ntdb, key, data, NTDB_REPLACE);
+}
+
+enum NTDB_ERROR ntdb_add_int32_atomic(struct ntdb_context *ntdb,
+ const char *keystr,
+ int32_t *oldval, int32_t addval)
+{
+ int32_t val;
+ enum NTDB_ERROR err;
+
+ err = ntdb_lock_bystring(ntdb, keystr);
+ if (err) {
+ return err;
+ }
+
+ err = ntdb_fetch_int32(ntdb, keystr, &val);
+ if (err) {
+ if (err == NTDB_ERR_NOEXIST) {
+ /* Start with 'old' value */
+ val = *oldval;
+ } else {
+ goto err_out;
+ }
+ } else {
+ /* It worked, set return value (oldval) to tdb data */
+ *oldval = val;
+ }
+
+ /* Increase value and store for next time. */
+ val += addval;
+ err = ntdb_store_int32(ntdb, keystr, val);
+
+ err_out:
+ ntdb_unlock_bystring(ntdb, keystr);
+ return err;
+}
+
NTSTATUS map_nt_error_from_ntdb(enum NTDB_ERROR err)
{
NTSTATUS result = NT_STATUS_INTERNAL_ERROR;
diff --git a/lib/util/util_ntdb.h b/lib/util/util_ntdb.h
index eac0db4f02..7531c42dee 100644
--- a/lib/util/util_ntdb.h
+++ b/lib/util/util_ntdb.h
@@ -96,6 +96,30 @@ enum NTDB_ERROR ntdb_fetch_bystring(struct ntdb_context *ntdb,
/****************************************************************************
+ Fetch a int32_t value by a string key. *val is int32_t in native byte order.
+ ntdb must have been created with ntdb_new() (as it uses talloc_free).
+****************************************************************************/
+enum NTDB_ERROR ntdb_fetch_int32(struct ntdb_context *ntdb,
+ const char *keystr, int32_t *val);
+
+/****************************************************************************
+ Store a int32_t value by a string key. val is int32_t in native byte order.
+****************************************************************************/
+enum NTDB_ERROR ntdb_store_int32(struct ntdb_context *ntdb,
+ const char *keystr, int32_t val);
+
+
+/****************************************************************************
+ Atomic integer add; reads the old value into *oldval (if found), then stores
+ *oldval + addval back for next time. Uses chainlock to do this atomically.
+
+ Thus the first time this is ever called, oldval will be unchanged.
+****************************************************************************/
+enum NTDB_ERROR ntdb_add_int32_atomic(struct ntdb_context *ntdb,
+ const char *keystr,
+ int32_t *oldval, int32_t addval);
+
+/****************************************************************************
Turn a nul-terminated string into an NTDB_DATA.
****************************************************************************/
static inline NTDB_DATA string_term_ntdb_data(const char *string)