summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordefault <nobody@localhost>2023-02-23 10:42:09 +0100
committerdefault <nobody@localhost>2023-02-23 10:42:09 +0100
commit307c9420c30406d1a85cbf604a13d0c9db2a6206 (patch)
treef7ebf1176090884dccc266078967cf07c39c6df0
parent4eec2157296122909ee2513a0ce408cf6158b895 (diff)
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)
}
+int index_gc(const char *fn)
+/* garbage-collects an index, deleting objects that are not here */
+{
+ FILE *i, *o;
+ int gc = -1;
+
+ pthread_mutex_lock(&data_mutex);
+
+ if ((i = fopen(fn, "r")) != NULL) {
+ xs *nfn = xs_fmt("%s.new", fn);
+ char line[256];
+
+ if ((o = fopen(nfn, "w")) != NULL) {
+ gc = 0;
+
+ while (fgets(line, sizeof(line), i) != NULL) {
+ line[32] = '\0';
+
+ if (object_here_by_md5(line))
+ fprintf(o, "%s\n", line);
+ else
+ gc++;
+ }
+
+ fclose(o);
+
+ xs *ofn = xs_fmt("%s.bak", fn);
+
+ unlink(ofn);
+ link(fn, ofn);
+ rename(nfn, fn);
+ }
+
+ fclose(i);
+ }
+
+ pthread_mutex_unlock(&data_mutex);
+
+ return gc;
+}
+
+
int index_del_md5(const char *fn, const char *md5)
/* deletes an md5 from an index */
{
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);
#define mtime(fn) mtime_nl(fn, NULL)
int index_add(const char *fn, const char *md5);
+int index_gc(const char *fn);
int index_del(const char *fn, const char *md5);
int index_first(const char *fn, char *buf, int size);
int index_len(const char *fn);