From 5ba76d616a2f5924e8ad7fdeb806f5bfa0c82e94 Mon Sep 17 00:00:00 2001 From: Magnus Ahltorp Date: Fri, 10 Jun 2016 15:36:29 +0200 Subject: Lock permdb database files with flock --- c_src/permdb.c | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) (limited to 'c_src/permdb.c') diff --git a/c_src/permdb.c b/c_src/permdb.c index db4498f..ef25be5 100644 --- a/c_src/permdb.c +++ b/c_src/permdb.c @@ -432,14 +432,27 @@ permdb_alloc(const char *dbpath) } permdb_object *state = malloc(sizeof(permdb_object)); - state->datafile = bf_open(dbpath, O_RDWR|O_CREAT, "datafile"); - state->indexfile = bf_open(idxpath, O_RDWR|O_CREAT, "indexfile"); - - free(idxpath); state->nodecache = NULL; state->dirtynodes = NULL; state->error = NULL; + + state->datafile = bf_open(dbpath, O_RDWR|O_CREAT, "datafile", 1); + if (state->datafile == NULL) { + permdb_free(state); + free(idxpath); + return NULL; + } + + state->indexfile = bf_open(idxpath, O_RDWR|O_CREAT, "indexfile", 1); + if (state->indexfile == NULL) { + permdb_free(state); + free(idxpath); + return NULL; + } + + free(idxpath); + if (bf_total_length(state->datafile) == 0 && bf_total_length(state->indexfile) == 0) { dprintf(WRITE, (stderr, "writing header\n")); -- cgit v1.1