diff options
| author | 2023-02-23 09:32:47 +0100 | |
|---|---|---|
| committer | 2023-02-23 09:32:47 +0100 | |
| commit | 4eec2157296122909ee2513a0ce408cf6158b895 (patch) | |
| tree | 649e28d1745353281ee47d7f0b0e721e89f7155e /data.c | |
| parent | Minor actor name code refactoring. (diff) | |
| download | snac2-4eec2157296122909ee2513a0ce408cf6158b895.tar.gz snac2-4eec2157296122909ee2513a0ce408cf6158b895.tar.xz snac2-4eec2157296122909ee2513a0ce408cf6158b895.zip | |
Serialize some data writes.
Diffstat (limited to 'data.c')
| -rw-r--r-- | data.c | 17 |
1 files changed, 16 insertions, 1 deletions
| @@ -14,13 +14,16 @@ | |||
| 14 | #include <sys/stat.h> | 14 | #include <sys/stat.h> |
| 15 | #include <sys/file.h> | 15 | #include <sys/file.h> |
| 16 | #include <fcntl.h> | 16 | #include <fcntl.h> |
| 17 | 17 | #include <pthread.h> | |
| 18 | 18 | ||
| 19 | double disk_layout = 2.7; | 19 | double disk_layout = 2.7; |
| 20 | 20 | ||
| 21 | /* storage serializer */ | ||
| 22 | pthread_mutex_t data_mutex = {0}; | ||
| 21 | 23 | ||
| 22 | int snac_upgrade(d_char **error); | 24 | int snac_upgrade(d_char **error); |
| 23 | 25 | ||
| 26 | |||
| 24 | int srv_open(char *basedir, int auto_upgrade) | 27 | int srv_open(char *basedir, int auto_upgrade) |
| 25 | /* opens a server */ | 28 | /* opens a server */ |
| 26 | { | 29 | { |
| @@ -29,6 +32,8 @@ int srv_open(char *basedir, int auto_upgrade) | |||
| 29 | FILE *f; | 32 | FILE *f; |
| 30 | d_char *error = NULL; | 33 | d_char *error = NULL; |
| 31 | 34 | ||
| 35 | pthread_mutex_init(&data_mutex, NULL); | ||
| 36 | |||
| 32 | srv_basedir = xs_str_new(basedir); | 37 | srv_basedir = xs_str_new(basedir); |
| 33 | 38 | ||
| 34 | if (xs_endswith(srv_basedir, "/")) | 39 | if (xs_endswith(srv_basedir, "/")) |
| @@ -121,6 +126,8 @@ void srv_free(void) | |||
| 121 | xs_free(srv_basedir); | 126 | xs_free(srv_basedir); |
| 122 | xs_free(srv_config); | 127 | xs_free(srv_config); |
| 123 | xs_free(srv_baseurl); | 128 | xs_free(srv_baseurl); |
| 129 | |||
| 130 | pthread_mutex_destroy(&data_mutex); | ||
| 124 | } | 131 | } |
| 125 | 132 | ||
| 126 | 133 | ||
| @@ -250,6 +257,8 @@ int index_add_md5(const char *fn, const char *md5) | |||
| 250 | int status = 201; /* Created */ | 257 | int status = 201; /* Created */ |
| 251 | FILE *f; | 258 | FILE *f; |
| 252 | 259 | ||
| 260 | pthread_mutex_lock(&data_mutex); | ||
| 261 | |||
| 253 | if ((f = fopen(fn, "a")) != NULL) { | 262 | if ((f = fopen(fn, "a")) != NULL) { |
| 254 | flock(fileno(f), LOCK_EX); | 263 | flock(fileno(f), LOCK_EX); |
| 255 | 264 | ||
| @@ -262,6 +271,8 @@ int index_add_md5(const char *fn, const char *md5) | |||
| 262 | else | 271 | else |
| 263 | status = 500; | 272 | status = 500; |
| 264 | 273 | ||
| 274 | pthread_mutex_unlock(&data_mutex); | ||
| 275 | |||
| 265 | return status; | 276 | return status; |
| 266 | } | 277 | } |
| 267 | 278 | ||
| @@ -280,6 +291,8 @@ int index_del_md5(const char *fn, const char *md5) | |||
| 280 | int status = 404; | 291 | int status = 404; |
| 281 | FILE *i, *o; | 292 | FILE *i, *o; |
| 282 | 293 | ||
| 294 | pthread_mutex_lock(&data_mutex); | ||
| 295 | |||
| 283 | if ((i = fopen(fn, "r")) != NULL) { | 296 | if ((i = fopen(fn, "r")) != NULL) { |
| 284 | flock(fileno(i), LOCK_EX); | 297 | flock(fileno(i), LOCK_EX); |
| 285 | 298 | ||
| @@ -309,6 +322,8 @@ int index_del_md5(const char *fn, const char *md5) | |||
| 309 | else | 322 | else |
| 310 | status = 500; | 323 | status = 500; |
| 311 | 324 | ||
| 325 | pthread_mutex_unlock(&data_mutex); | ||
| 326 | |||
| 312 | return status; | 327 | return status; |
| 313 | } | 328 | } |
| 314 | 329 | ||