commit 4508630

default  ·  2023-02-22 18:09:05 +0000 UTC
parent 23c433e
Added index locks a bit stronger.
1 files changed,  +24, -2
M data.c
M data.c
+24, -2
 1@@ -243,11 +243,25 @@ double mtime_nl(const char *fn, int *n_link)
 2 
 3 /** indexes **/
 4 
 5+
 6+FILE *index_lock(const char *fn)
 7+{
 8+    xs *lck = xs_fmt("%s.lck", fn);
 9+    FILE *f = fopen(lck, "a");
10+    flock(fileno(f), LOCK_EX);
11+
12+    return f;
13+}
14+
15+
16 int index_add_md5(const char *fn, const char *md5)
17 /* adds an md5 to an index */
18 {
19     int status = 201; /* Created */
20-    FILE *f;
21+    FILE *l, *f;
22+
23+    if ((l = index_lock(fn)) == NULL)
24+        return 500;
25 
26     if ((f = fopen(fn, "a")) != NULL) {
27         flock(fileno(f), LOCK_EX);
28@@ -261,6 +275,8 @@ int index_add_md5(const char *fn, const char *md5)
29     else
30         status = 500;
31 
32+    fclose(l);
33+
34     return status;
35 }
36 
37@@ -277,7 +293,10 @@ int index_del_md5(const char *fn, const char *md5)
38 /* deletes an md5 from an index */
39 {
40     int status = 404;
41-    FILE *i, *o;
42+    FILE *l, *i, *o;
43+
44+    if ((l = index_lock(fn)) == NULL)
45+        return 500;
46 
47     if ((i = fopen(fn, "r")) != NULL) {
48         flock(fileno(i), LOCK_EX);
49@@ -296,6 +315,7 @@ int index_del_md5(const char *fn, const char *md5)
50 
51             xs *ofn = xs_fmt("%s.bak", fn);
52 
53+            unlink(ofn);
54             link(fn, ofn);
55             rename(nfn, fn);
56         }
57@@ -307,6 +327,8 @@ int index_del_md5(const char *fn, const char *md5)
58     else
59         status = 500;
60 
61+    fclose(l);
62+
63     return status;
64 }
65