snac2

Fork of https://codeberg.org/grunfink/snac2
git clone https://git.inz.fi/snac2
Log | Files | Refs | README | LICENSE

commit 4eec2157296122909ee2513a0ce408cf6158b895
parent 90838f07c69d21f1629e208e503257ffd9c91fe0
Author: default <nobody@localhost>
Date:   Thu, 23 Feb 2023 09:32:47 +0100

Serialize some data writes.

Diffstat:
Mdata.c | 17++++++++++++++++-
1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/data.c b/data.c @@ -14,13 +14,16 @@ #include <sys/stat.h> #include <sys/file.h> #include <fcntl.h> - +#include <pthread.h> 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; }