mirror of
https://github.com/samba-team/samba.git
synced 2024-12-27 03:21:53 +03:00
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)
This commit is contained in:
parent
eff985b7ef
commit
3f99fc16a0
@ -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;
|
||||
|
@ -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;
|
||||
};
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user