summaryrefslogtreecommitdiff
path: root/data.c
diff options
context:
space:
mode:
authordefault <nobody@localhost>2023-02-22 19:09:05 +0100
committerdefault <nobody@localhost>2023-02-22 19:09:05 +0100
commit450863031bdef9856de388a5e6bd440aa4bbfc83 (patch)
treeac23acf83b5de75aca37b022eb874ee002e43077 /data.c
parent23c433ee0257baf704eb4ff4b7f7a77d549f088c (diff)
Added index locks a bit stronger.
Diffstat (limited to 'data.c')
-rw-r--r--data.c26
1 files 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;
}