summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar default2023-02-23 10:42:09 +0100
committerGravatar default2023-02-23 10:42:09 +0100
commit307c9420c30406d1a85cbf604a13d0c9db2a6206 (patch)
treef7ebf1176090884dccc266078967cf07c39c6df0
parentSerialize some data writes. (diff)
downloadpenes-snac2-307c9420c30406d1a85cbf604a13d0c9db2a6206.tar.gz
penes-snac2-307c9420c30406d1a85cbf604a13d0c9db2a6206.tar.xz
penes-snac2-307c9420c30406d1a85cbf604a13d0c9db2a6206.zip
New function index_gc().
-rw-r--r--data.c42
-rw-r--r--snac.h1
2 files changed, 43 insertions, 0 deletions
diff --git a/data.c b/data.c
index 4d892cc..8888d4a 100644
--- a/data.c
+++ b/data.c
@@ -285,6 +285,48 @@ int index_add(const char *fn, const char *id)
285} 285}
286 286
287 287
288int index_gc(const char *fn)
289/* garbage-collects an index, deleting objects that are not here */
290{
291 FILE *i, *o;
292 int gc = -1;
293
294 pthread_mutex_lock(&data_mutex);
295
296 if ((i = fopen(fn, "r")) != NULL) {
297 xs *nfn = xs_fmt("%s.new", fn);
298 char line[256];
299
300 if ((o = fopen(nfn, "w")) != NULL) {
301 gc = 0;
302
303 while (fgets(line, sizeof(line), i) != NULL) {
304 line[32] = '\0';
305
306 if (object_here_by_md5(line))
307 fprintf(o, "%s\n", line);
308 else
309 gc++;
310 }
311
312 fclose(o);
313
314 xs *ofn = xs_fmt("%s.bak", fn);
315
316 unlink(ofn);
317 link(fn, ofn);
318 rename(nfn, fn);
319 }
320
321 fclose(i);
322 }
323
324 pthread_mutex_unlock(&data_mutex);
325
326 return gc;
327}
328
329
288int index_del_md5(const char *fn, const char *md5) 330int index_del_md5(const char *fn, const char *md5)
289/* deletes an md5 from an index */ 331/* deletes an md5 from an index */
290{ 332{
diff --git a/snac.h b/snac.h
index ba8b680..5c02871 100644
--- a/snac.h
+++ b/snac.h
@@ -60,6 +60,7 @@ double mtime_nl(const char *fn, int *n_link);
60#define mtime(fn) mtime_nl(fn, NULL) 60#define mtime(fn) mtime_nl(fn, NULL)
61 61
62int index_add(const char *fn, const char *md5); 62int index_add(const char *fn, const char *md5);
63int index_gc(const char *fn);
63int index_del(const char *fn, const char *md5); 64int index_del(const char *fn, const char *md5);
64int index_first(const char *fn, char *buf, int size); 65int index_first(const char *fn, char *buf, int size);
65int index_len(const char *fn); 66int index_len(const char *fn);