summaryrefslogtreecommitdiff
path: root/lib/ntdb/test/api-60-noop-transaction.c
diff options
context:
space:
mode:
authorRusty Russell <rusty@rustcorp.com.au>2013-02-17 20:56:34 +1030
committerRusty Russell <rusty@rustcorp.com.au>2013-02-20 05:31:19 +0100
commit3d82f786ecdd6747e90fe480a15de8c3fcea5f7b (patch)
tree4197bb0051b0effbae636d7dc559a433647ee428 /lib/ntdb/test/api-60-noop-transaction.c
parent85b6329b91f402da474547410e82438ea2554775 (diff)
downloadsamba-3d82f786ecdd6747e90fe480a15de8c3fcea5f7b.tar.gz
samba-3d82f786ecdd6747e90fe480a15de8c3fcea5f7b.tar.bz2
samba-3d82f786ecdd6747e90fe480a15de8c3fcea5f7b.zip
ntdb: fix database corruption when transaction doesn't change anything.
ntdb's transaction code has an optimization which tdb's doesnt: it only writes the parts of blocks whose contents have changed. This means we can actually have a transaction which turns out to need no recovery region. This breaks the recovery setup logic, which sets the current recovery size to 0 if there's no recovery area, and assumes that we'll always create a new recovery area since the recovery will always need > 0 bytes. In fact, if we really haven't changed anything, we can skip the transaction commit altogether: since this happens at least once with Samba, it's worth doing. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
Diffstat (limited to 'lib/ntdb/test/api-60-noop-transaction.c')
-rw-r--r--lib/ntdb/test/api-60-noop-transaction.c58
1 files changed, 58 insertions, 0 deletions
diff --git a/lib/ntdb/test/api-60-noop-transaction.c b/lib/ntdb/test/api-60-noop-transaction.c
new file mode 100644
index 0000000000..a429e67907
--- /dev/null
+++ b/lib/ntdb/test/api-60-noop-transaction.c
@@ -0,0 +1,58 @@
+#include "private.h" // struct ntdb_context
+#include "ntdb.h"
+#include "tap-interface.h"
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include "logging.h"
+
+int main(int argc, char *argv[])
+{
+ unsigned int i;
+ struct ntdb_context *ntdb;
+ int flags[] = { NTDB_DEFAULT, NTDB_NOMMAP,
+ NTDB_CONVERT, NTDB_NOMMAP|NTDB_CONVERT };
+ NTDB_DATA key = ntdb_mkdata("key", 3);
+ NTDB_DATA data = ntdb_mkdata("data", 4), d;
+
+ plan_tests(sizeof(flags) / sizeof(flags[0]) * 12 + 1);
+
+ for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
+ ntdb = ntdb_open("api-60-transaction.ntdb",
+ flags[i]|MAYBE_NOSYNC,
+ O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
+ ok1(ntdb);
+ if (!ntdb)
+ continue;
+
+ ok1(ntdb_store(ntdb, key, data, NTDB_INSERT) == 0);
+
+ ok1(ntdb_transaction_start(ntdb) == 0);
+ /* Do an identical replace. */
+ ok1(ntdb_store(ntdb, key, data, NTDB_REPLACE) == 0);
+ ok1(ntdb_transaction_commit(ntdb) == 0);
+
+ ok1(ntdb_check(ntdb, NULL, NULL) == 0);
+ ok1(ntdb_fetch(ntdb, key, &d) == NTDB_SUCCESS);
+ ok1(ntdb_deq(data, d));
+ free(d.dptr);
+ ntdb_close(ntdb);
+
+ /* Reopen, fetch. */
+ ntdb = ntdb_open("api-60-transaction.ntdb",
+ flags[i]|MAYBE_NOSYNC,
+ O_RDWR, 0600, &tap_log_attr);
+ ok1(ntdb);
+ if (!ntdb)
+ continue;
+ ok1(ntdb_check(ntdb, NULL, NULL) == 0);
+ ok1(ntdb_fetch(ntdb, key, &d) == NTDB_SUCCESS);
+ ok1(ntdb_deq(data, d));
+ free(d.dptr);
+ ntdb_close(ntdb);
+ }
+
+ ok1(tap_log_messages == 0);
+ return exit_status();
+}