diff options
| -rw-r--r-- | data.c | 26 |
1 files changed, 24 insertions, 2 deletions
| @@ -243,11 +243,25 @@ double mtime_nl(const char *fn, int *n_link) | |||
| 243 | 243 | ||
| 244 | /** indexes **/ | 244 | /** indexes **/ |
| 245 | 245 | ||
| 246 | |||
| 247 | FILE *index_lock(const char *fn) | ||
| 248 | { | ||
| 249 | xs *lck = xs_fmt("%s.lck", fn); | ||
| 250 | FILE *f = fopen(lck, "a"); | ||
| 251 | flock(fileno(f), LOCK_EX); | ||
| 252 | |||
| 253 | return f; | ||
| 254 | } | ||
| 255 | |||
| 256 | |||
| 246 | int index_add_md5(const char *fn, const char *md5) | 257 | int index_add_md5(const char *fn, const char *md5) |
| 247 | /* adds an md5 to an index */ | 258 | /* adds an md5 to an index */ |
| 248 | { | 259 | { |
| 249 | int status = 201; /* Created */ | 260 | int status = 201; /* Created */ |
| 250 | FILE *f; | 261 | FILE *l, *f; |
| 262 | |||
| 263 | if ((l = index_lock(fn)) == NULL) | ||
| 264 | return 500; | ||
| 251 | 265 | ||
| 252 | if ((f = fopen(fn, "a")) != NULL) { | 266 | if ((f = fopen(fn, "a")) != NULL) { |
| 253 | flock(fileno(f), LOCK_EX); | 267 | flock(fileno(f), LOCK_EX); |
| @@ -261,6 +275,8 @@ int index_add_md5(const char *fn, const char *md5) | |||
| 261 | else | 275 | else |
| 262 | status = 500; | 276 | status = 500; |
| 263 | 277 | ||
| 278 | fclose(l); | ||
| 279 | |||
| 264 | return status; | 280 | return status; |
| 265 | } | 281 | } |
| 266 | 282 | ||
| @@ -277,7 +293,10 @@ int index_del_md5(const char *fn, const char *md5) | |||
| 277 | /* deletes an md5 from an index */ | 293 | /* deletes an md5 from an index */ |
| 278 | { | 294 | { |
| 279 | int status = 404; | 295 | int status = 404; |
| 280 | FILE *i, *o; | 296 | FILE *l, *i, *o; |
| 297 | |||
| 298 | if ((l = index_lock(fn)) == NULL) | ||
| 299 | return 500; | ||
| 281 | 300 | ||
| 282 | if ((i = fopen(fn, "r")) != NULL) { | 301 | if ((i = fopen(fn, "r")) != NULL) { |
| 283 | flock(fileno(i), LOCK_EX); | 302 | flock(fileno(i), LOCK_EX); |
| @@ -296,6 +315,7 @@ int index_del_md5(const char *fn, const char *md5) | |||
| 296 | 315 | ||
| 297 | xs *ofn = xs_fmt("%s.bak", fn); | 316 | xs *ofn = xs_fmt("%s.bak", fn); |
| 298 | 317 | ||
| 318 | unlink(ofn); | ||
| 299 | link(fn, ofn); | 319 | link(fn, ofn); |
| 300 | rename(nfn, fn); | 320 | rename(nfn, fn); |
| 301 | } | 321 | } |
| @@ -307,6 +327,8 @@ int index_del_md5(const char *fn, const char *md5) | |||
| 307 | else | 327 | else |
| 308 | status = 500; | 328 | status = 500; |
| 309 | 329 | ||
| 330 | fclose(l); | ||
| 331 | |||
| 310 | return status; | 332 | return status; |
| 311 | } | 333 | } |
| 312 | 334 | ||