diff --git a/data.c b/data.c index 40c663d..4d892cc 100644 --- a/data.c +++ b/data.c @@ -14,13 +14,16 @@ #include #include #include - +#include double disk_layout = 2.7; +/* storage serializer */ +pthread_mutex_t data_mutex = {0}; int snac_upgrade(d_char **error); + int srv_open(char *basedir, int auto_upgrade) /* opens a server */ { @@ -29,6 +32,8 @@ int srv_open(char *basedir, int auto_upgrade) FILE *f; d_char *error = NULL; + pthread_mutex_init(&data_mutex, NULL); + srv_basedir = xs_str_new(basedir); if (xs_endswith(srv_basedir, "/")) @@ -121,6 +126,8 @@ void srv_free(void) xs_free(srv_basedir); xs_free(srv_config); xs_free(srv_baseurl); + + pthread_mutex_destroy(&data_mutex); } @@ -250,6 +257,8 @@ int index_add_md5(const char *fn, const char *md5) int status = 201; /* Created */ FILE *f; + pthread_mutex_lock(&data_mutex); + if ((f = fopen(fn, "a")) != NULL) { flock(fileno(f), LOCK_EX); @@ -262,6 +271,8 @@ int index_add_md5(const char *fn, const char *md5) else status = 500; + pthread_mutex_unlock(&data_mutex); + return status; } @@ -280,6 +291,8 @@ int index_del_md5(const char *fn, const char *md5) int status = 404; FILE *i, *o; + pthread_mutex_lock(&data_mutex); + if ((i = fopen(fn, "r")) != NULL) { flock(fileno(i), LOCK_EX); @@ -309,6 +322,8 @@ int index_del_md5(const char *fn, const char *md5) else status = 500; + pthread_mutex_unlock(&data_mutex); + return status; }