summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--c_src/filebuffer.c12
-rw-r--r--c_src/filebuffer.h2
-rw-r--r--c_src/permdb.c21
3 files changed, 29 insertions, 6 deletions
diff --git a/c_src/filebuffer.c b/c_src/filebuffer.c
index 8285695..87b9859 100644
--- a/c_src/filebuffer.c
+++ b/c_src/filebuffer.c
@@ -177,7 +177,7 @@ bf_read(buffered_file *file, uint64_t offset, size_t length, char **error)
}
buffered_file *
-bf_open(const char *path, int flags, const char *name)
+bf_open(const char *path, int flags, const char *name, int lock)
{
buffered_file *file = malloc(sizeof(buffered_file));
@@ -187,6 +187,16 @@ bf_open(const char *path, int flags, const char *name)
return NULL;
}
+ if (lock) {
+ int ret;
+
+ ret = flock(file->fd, LOCK_EX|LOCK_NB);
+ if (ret == -1) {
+ warn("flock %s", path);
+ return NULL;
+ }
+ }
+
file->name = name;
off_t datafile_filesize = lseek(file->fd, 0, SEEK_END);
diff --git a/c_src/filebuffer.h b/c_src/filebuffer.h
index 05c656e..f24d86a 100644
--- a/c_src/filebuffer.h
+++ b/c_src/filebuffer.h
@@ -9,7 +9,7 @@
typedef struct buffered_file buffered_file;
buffered_file *
-bf_open(const char *path, int flags, const char *name);
+bf_open(const char *path, int flags, const char *name, int lock);
void
bf_close(buffered_file *file);
void
diff --git a/c_src/permdb.c b/c_src/permdb.c
index 1163dc7..09e1fd7 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"));