From 450863031bdef9856de388a5e6bd440aa4bbfc83 Mon Sep 17 00:00:00 2001 From: default Date: Wed, 22 Feb 2023 19:09:05 +0100 Subject: [PATCH] Added index locks a bit stronger. --- data.c | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/data.c b/data.c index 9a250e6..20bf13f 100644 --- a/data.c +++ b/data.c @@ -243,11 +243,25 @@ double mtime_nl(const char *fn, int *n_link) /** indexes **/ + +FILE *index_lock(const char *fn) +{ + xs *lck = xs_fmt("%s.lck", fn); + FILE *f = fopen(lck, "a"); + flock(fileno(f), LOCK_EX); + + return f; +} + + int index_add_md5(const char *fn, const char *md5) /* adds an md5 to an index */ { int status = 201; /* Created */ - FILE *f; + FILE *l, *f; + + if ((l = index_lock(fn)) == NULL) + return 500; if ((f = fopen(fn, "a")) != NULL) { flock(fileno(f), LOCK_EX); @@ -261,6 +275,8 @@ int index_add_md5(const char *fn, const char *md5) else status = 500; + fclose(l); + return status; } @@ -277,7 +293,10 @@ int index_del_md5(const char *fn, const char *md5) /* deletes an md5 from an index */ { int status = 404; - FILE *i, *o; + FILE *l, *i, *o; + + if ((l = index_lock(fn)) == NULL) + return 500; if ((i = fopen(fn, "r")) != NULL) { flock(fileno(i), LOCK_EX); @@ -296,6 +315,7 @@ int index_del_md5(const char *fn, const char *md5) xs *ofn = xs_fmt("%s.bak", fn); + unlink(ofn); link(fn, ofn); rename(nfn, fn); } @@ -307,6 +327,8 @@ int index_del_md5(const char *fn, const char *md5) else status = 500; + fclose(l); + return status; }