s3:dbwrap: add a wrapper dbwrap_trans_do()
authorMichael Adam <obnox@samba.org>
Wed, 15 Jul 2009 12:00:42 +0000 (14:00 +0200)
committerMichael Adam <obnox@samba.org>
Wed, 15 Jul 2009 12:00:43 +0000 (14:00 +0200)
This function wraps the action() callback into a db
transaction and the transaction is either committed
or cancelled, depending on the return value of
the action function.

Michael

source3/include/proto.h
source3/lib/dbwrap_util.c

index df7815587c9c207a69915778789a6ae65369fb88..74cedcec184622ca32f9b7f07eba79bf5e9e2447 100644 (file)
@@ -454,6 +454,9 @@ NTSTATUS dbwrap_trans_store_uint32(struct db_context *db, const char *keystr,
 NTSTATUS dbwrap_trans_store_bystring(struct db_context *db, const char *key,
                                     TDB_DATA data, int flags);
 NTSTATUS dbwrap_trans_delete_bystring(struct db_context *db, const char *key);
+NTSTATUS dbwrap_trans_do(struct db_context *db,
+                        NTSTATUS (*action)(struct db_context *, void *),
+                        void *private_data);
 
 /* The following definitions come from lib/debug.c  */
 
index 3be3a49e7defc623305e088d8fa0b086431b79fc..67471b5c96843d77daf49ecb1f1479813130a339 100644 (file)
@@ -307,3 +307,36 @@ NTSTATUS dbwrap_trans_delete_bystring(struct db_context *db, const char *key)
 {
        return dbwrap_trans_delete(db, string_term_tdb_data(key));
 }
+
+/**
+ * Wrap db action(s) into a transaction.
+ */
+NTSTATUS dbwrap_trans_do(struct db_context *db,
+                        NTSTATUS (*action)(struct db_context *, void *),
+                        void *private_data)
+{
+       int res;
+       NTSTATUS status;
+
+       res = db->transaction_start(db);
+       if (res != 0) {
+               DEBUG(5, ("transaction_start failed\n"));
+               return NT_STATUS_INTERNAL_DB_CORRUPTION;
+       }
+
+       status = action(db, private_data);
+       if (!NT_STATUS_IS_OK(status)) {
+               if (db->transaction_cancel(db) != 0) {
+                       smb_panic("Cancelling transaction failed");
+               }
+               return status;
+       }
+
+       res = db->transaction_commit(db);
+       if (res == 0) {
+               return NT_STATUS_OK;
+       }
+
+       DEBUG(2, ("transaction_commit failed\n"));
+       return NT_STATUS_INTERNAL_DB_CORRUPTION;
+}