summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRusty Russell <rusty@rustcorp.com.au>2011-12-05 17:04:30 +1030
committerRusty Russell <rusty@rustcorp.com.au>2011-12-05 17:04:30 +1030
commit3f99fc16a0a3aead82cf8cdfb9703f8f18388623 (patch)
tree01eecd8d641c7de4bd76506592379a7070e1feff
parenteff985b7ef3f1adb3da383baaea013f7f3797bb2 (diff)
downloadsamba-3f99fc16a0a3aead82cf8cdfb9703f8f18388623.tar.gz
samba-3f99fc16a0a3aead82cf8cdfb9703f8f18388623.tar.bz2
samba-3f99fc16a0a3aead82cf8cdfb9703f8f18388623.zip
tdb2: provide tdb_layout_write() rather than implying it by new_tdb_layout arg.
Neater API. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au> (Imported from CCAN commit 79d603a5f73dfbb655d8d08f67eecb5f2da542d5)
-rw-r--r--lib/tdb2/test/layout.c36
-rw-r--r--lib/tdb2/test/layout.h6
-rw-r--r--lib/tdb2/test/run-03-coalesce.c35
-rw-r--r--lib/tdb2/test/run-50-multiple-freelists.c2
4 files changed, 47 insertions, 32 deletions
diff --git a/lib/tdb2/test/layout.c b/lib/tdb2/test/layout.c
index 95cca6a810..6c601ddb85 100644
--- a/lib/tdb2/test/layout.c
+++ b/lib/tdb2/test/layout.c
@@ -6,10 +6,9 @@
#include <err.h>
#include "logging.h"
-struct tdb_layout *new_tdb_layout(const char *filename)
+struct tdb_layout *new_tdb_layout(void)
{
struct tdb_layout *layout = malloc(sizeof(*layout));
- layout->filename = filename;
layout->num_elems = 0;
layout->elem = NULL;
return layout;
@@ -314,26 +313,25 @@ struct tdb_context *tdb_layout_get(struct tdb_layout *layout,
}
tdb->tdb2.ftable_off = find_ftable(layout, 0)->base.off;
-
- /* Get physical if they asked for it. */
- if (layout->filename) {
- int fd = open(layout->filename, O_WRONLY|O_TRUNC|O_CREAT,
- 0600);
- if (fd < 0)
- err(1, "opening %s for writing", layout->filename);
- if (write(fd, tdb->file->map_ptr, tdb->file->map_size)
- != tdb->file->map_size)
- err(1, "writing %s", layout->filename);
- close(fd);
- tdb_close(tdb);
- /* NOMMAP is for lockcheck. */
- tdb = tdb_open(layout->filename, TDB_NOMMAP, O_RDWR, 0,
- &tap_log_attr);
- }
-
return tdb;
}
+void tdb_layout_write(struct tdb_layout *layout, union tdb_attribute *attr,
+ const char *filename)
+{
+ struct tdb_context *tdb = tdb_layout_get(layout, attr);
+ int fd;
+
+ fd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, 0600);
+ if (fd < 0)
+ err(1, "opening %s for writing", filename);
+ if (write(fd, tdb->file->map_ptr, tdb->file->map_size)
+ != tdb->file->map_size)
+ err(1, "writing %s", filename);
+ close(fd);
+ tdb_close(tdb);
+}
+
void tdb_layout_free(struct tdb_layout *layout)
{
unsigned int i;
diff --git a/lib/tdb2/test/layout.h b/lib/tdb2/test/layout.h
index 96ecb683b8..6649113766 100644
--- a/lib/tdb2/test/layout.h
+++ b/lib/tdb2/test/layout.h
@@ -2,7 +2,7 @@
#define TDB2_TEST_LAYOUT_H
#include <ccan/tdb2/private.h>
-struct tdb_layout *new_tdb_layout(const char *filename);
+struct tdb_layout *new_tdb_layout(void);
void tdb_layout_add_freetable(struct tdb_layout *layout);
void tdb_layout_add_free(struct tdb_layout *layout, tdb_len_t len,
unsigned ftable);
@@ -17,6 +17,9 @@ void tdb_layout_add_hashtable(struct tdb_layout *layout,
#endif
struct tdb_context *tdb_layout_get(struct tdb_layout *layout,
union tdb_attribute *attr);
+void tdb_layout_write(struct tdb_layout *layout, union tdb_attribute *attr,
+ const char *filename);
+
void tdb_layout_free(struct tdb_layout *layout);
enum layout_type {
@@ -62,7 +65,6 @@ union tdb_layout_elem {
};
struct tdb_layout {
- const char *filename;
unsigned int num_elems;
union tdb_layout_elem *elem;
};
diff --git a/lib/tdb2/test/run-03-coalesce.c b/lib/tdb2/test/run-03-coalesce.c
index 93b8172844..c64b2bc57c 100644
--- a/lib/tdb2/test/run-03-coalesce.c
+++ b/lib/tdb2/test/run-03-coalesce.c
@@ -32,11 +32,14 @@ int main(int argc, char *argv[])
key = tdb_mkdata("hello", 5);
/* No coalescing can be done due to EOF */
- layout = new_tdb_layout("run-03-coalesce.tdb");
+ layout = new_tdb_layout();
tdb_layout_add_freetable(layout);
len = 1024;
tdb_layout_add_free(layout, len, 0);
- tdb = tdb_layout_get(layout, &tap_log_attr);
+ tdb_layout_write(layout, &tap_log_attr, "run-03-coalesce.tdb");
+ /* NOMMAP is for lockcheck. */
+ tdb = tdb_open("run-03-coalesce.tdb", TDB_NOMMAP, O_RDWR, 0,
+ &tap_log_attr);
ok1(tdb_check(tdb, NULL, NULL) == 0);
ok1(free_record_length(tdb, layout->elem[1].base.off) == len);
@@ -55,11 +58,14 @@ int main(int argc, char *argv[])
tdb_layout_free(layout);
/* No coalescing can be done due to used record */
- layout = new_tdb_layout("run-03-coalesce.tdb");
+ layout = new_tdb_layout();
tdb_layout_add_freetable(layout);
tdb_layout_add_free(layout, 1024, 0);
tdb_layout_add_used(layout, key, data, 6);
- tdb = tdb_layout_get(layout, &tap_log_attr);
+ tdb_layout_write(layout, &tap_log_attr, "run-03-coalesce.tdb");
+ /* NOMMAP is for lockcheck. */
+ tdb = tdb_open("run-03-coalesce.tdb", TDB_NOMMAP, O_RDWR, 0,
+ &tap_log_attr);
ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024);
ok1(tdb_check(tdb, NULL, NULL) == 0);
@@ -78,11 +84,14 @@ int main(int argc, char *argv[])
tdb_layout_free(layout);
/* Coalescing can be done due to two free records, then EOF */
- layout = new_tdb_layout("run-03-coalesce.tdb");
+ layout = new_tdb_layout();
tdb_layout_add_freetable(layout);
tdb_layout_add_free(layout, 1024, 0);
tdb_layout_add_free(layout, 2048, 0);
- tdb = tdb_layout_get(layout, &tap_log_attr);
+ tdb_layout_write(layout, &tap_log_attr, "run-03-coalesce.tdb");
+ /* NOMMAP is for lockcheck. */
+ tdb = tdb_open("run-03-coalesce.tdb", TDB_NOMMAP, O_RDWR, 0,
+ &tap_log_attr);
ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024);
ok1(free_record_length(tdb, layout->elem[2].base.off) == 2048);
ok1(tdb_check(tdb, NULL, NULL) == 0);
@@ -104,12 +113,15 @@ int main(int argc, char *argv[])
tdb_layout_free(layout);
/* Coalescing can be done due to two free records, then data */
- layout = new_tdb_layout("run-03-coalesce.tdb");
+ layout = new_tdb_layout();
tdb_layout_add_freetable(layout);
tdb_layout_add_free(layout, 1024, 0);
tdb_layout_add_free(layout, 512, 0);
tdb_layout_add_used(layout, key, data, 6);
- tdb = tdb_layout_get(layout, &tap_log_attr);
+ tdb_layout_write(layout, &tap_log_attr, "run-03-coalesce.tdb");
+ /* NOMMAP is for lockcheck. */
+ tdb = tdb_open("run-03-coalesce.tdb", TDB_NOMMAP, O_RDWR, 0,
+ &tap_log_attr);
ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024);
ok1(free_record_length(tdb, layout->elem[2].base.off) == 512);
ok1(tdb_check(tdb, NULL, NULL) == 0);
@@ -130,12 +142,15 @@ int main(int argc, char *argv[])
tdb_layout_free(layout);
/* Coalescing can be done due to three free records, then EOF */
- layout = new_tdb_layout("run-03-coalesce.tdb");
+ layout = new_tdb_layout();
tdb_layout_add_freetable(layout);
tdb_layout_add_free(layout, 1024, 0);
tdb_layout_add_free(layout, 512, 0);
tdb_layout_add_free(layout, 256, 0);
- tdb = tdb_layout_get(layout, &tap_log_attr);
+ tdb_layout_write(layout, &tap_log_attr, "run-03-coalesce.tdb");
+ /* NOMMAP is for lockcheck. */
+ tdb = tdb_open("run-03-coalesce.tdb", TDB_NOMMAP, O_RDWR, 0,
+ &tap_log_attr);
ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024);
ok1(free_record_length(tdb, layout->elem[2].base.off) == 512);
ok1(free_record_length(tdb, layout->elem[3].base.off) == 256);
diff --git a/lib/tdb2/test/run-50-multiple-freelists.c b/lib/tdb2/test/run-50-multiple-freelists.c
index dae90e2398..10eaf41d98 100644
--- a/lib/tdb2/test/run-50-multiple-freelists.c
+++ b/lib/tdb2/test/run-50-multiple-freelists.c
@@ -21,7 +21,7 @@ int main(int argc, char *argv[])
data = tdb_mkdata("world", 5);
/* Create a TDB with three free tables. */
- layout = new_tdb_layout(NULL);
+ layout = new_tdb_layout();
tdb_layout_add_freetable(layout);
tdb_layout_add_freetable(layout);
tdb_layout_add_freetable(layout);