]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
kr_cache_gc: separated main function
authorLibor Peltan <libor.peltan@nic.cz>
Fri, 20 Apr 2018 08:21:26 +0000 (10:21 +0200)
committerPetr Špaček <petr.spacek@nic.cz>
Wed, 10 Jul 2019 13:59:20 +0000 (15:59 +0200)
utils/kr_cache_gc/Makefile
utils/kr_cache_gc/kr_cache_gc.c
utils/kr_cache_gc/kr_cache_gc.h [new file with mode: 0644]
utils/kr_cache_gc/main.c [new file with mode: 0644]

index e38925cd6d7aee58d1cb4e91f971618558f53c41..fe45efea34d1d76fcaeffa00196005e889abac7a 100644 (file)
@@ -1,7 +1,12 @@
 
 all: kr_cache_gc
 
-kr_cache_gc: kr_cache_gc.c ../../contrib/dynarray.h ../../lib/defines.h ../../lib/cache/api.h ../../lib/cache/impl.h
-       gcc -std=gnu99 -o $@ $< -Wl,-Bdynamic -L../../lib -lknot -lkres -I../.. -I../../contrib -I/usr/include/luajit-2.0
+kr_cache_gc: kr_cache_gc.o main.c kr_cache_gc.h ../../lib/defines.h
+       gcc -std=gnu99 -o $@ main.c kr_cache_gc.o -Wl,-Bdynamic -L../../lib -lknot -lkres -I../..
 
+kr_cache_gc.o: kr_cache_gc.c ../../contrib/dynarray.h ../../lib/defines.h ../../lib/cache/api.h ../../lib/cache/impl.h
+       gcc -std=gnu99 -o $@ -c $< -I../.. -I../../contrib -I/usr/include/luajit-2.0
+
+clean:
+       rm -f kr_cache_gc kr_cache_gc.o
 
index 00109e8dbc6f7b8d0eefc04e4d247da536715f4e..772aed6341c62db11fa1945f0ad63a569e4c6a0a 100644 (file)
@@ -12,8 +12,6 @@
 #include <lib/cache/impl.h>
 #include <lib/defines.h>
 
-#define KR_CACHE_GC_VERSION "0.1"
-
 // TODO remove and use time(NULL) ! this is just for debug with pre-generated cache
 int64_t now = 1523701784;
 
@@ -128,22 +126,15 @@ dynarray_declare(entry, knot_db_val_t, DYNARRAY_VISIBILITY_STATIC, 256);
 dynarray_define(entry, knot_db_val_t, DYNARRAY_VISIBILITY_STATIC);
 
 
-int main(int argc, char *argv[])
+int kr_cache_gc(const char *cache)
 {
-       printf("Knot Resolver Cache Garbage Collector v. %s\n", KR_CACHE_GC_VERSION);
-       if (argc < 2 || argv[1][0] == '-') {
-               printf("Usage: %s <path/to/kres/cache>\n", argv[0]);
-               return 0;
-       }
-
-       const char *cache = argv[1];
        char cache_data[strlen(cache) + 10];
        snprintf(cache_data, sizeof(cache_data), "%s/data.mdb", cache);
 
        struct stat st = { 0 };
        if (stat(cache, &st) || !(st.st_mode & S_IFDIR) || stat(cache_data, &st)) {
                printf("Error: %s does not exist or is not a LMDB.\n", cache);
-               return 1;
+               return -ENOENT;
        }
 
        size_t cache_size = st.st_size;
@@ -154,7 +145,7 @@ int main(int argc, char *argv[])
        int ret = kr_cache_open(&krc, NULL, &opts, NULL);
        if (ret || krc.db == NULL) {
                printf("Error opening Resolver cache (%s).\n", kr_strerror(ret));
-               return 2;
+               return -EINVAL;
        }
 
        const knot_db_api_t *api = knot_db_lmdb_api();
@@ -249,5 +240,5 @@ fail:
        free(db);
        kr_cache_close(&krc);
 
-       return (ret ? 10 : 0);
+       return ret;
 }
diff --git a/utils/kr_cache_gc/kr_cache_gc.h b/utils/kr_cache_gc/kr_cache_gc.h
new file mode 100644 (file)
index 0000000..b492f8b
--- /dev/null
@@ -0,0 +1,3 @@
+int kr_cache_gc(const char *cache);
+
+#define KR_CACHE_GC_VERSION "0.1"
diff --git a/utils/kr_cache_gc/main.c b/utils/kr_cache_gc/main.c
new file mode 100644 (file)
index 0000000..f642a8d
--- /dev/null
@@ -0,0 +1,50 @@
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <lib/defines.h>
+
+#include "kr_cache_gc.h"
+
+static void print_help()
+{
+       printf("Usage: kr_cache_gc -c <resolver_cache>\n");
+}
+
+int main(int argc, char *argv[])
+{
+       printf("Knot Resolver Cache Garbage Collector v. %s\n", KR_CACHE_GC_VERSION);
+
+       const char *cache_path = NULL;
+
+       int o;
+       while ((o = getopt(argc, argv, "hc:")) != -1) {
+               switch (o) {
+               case 'c':
+                       cache_path = optarg;
+                       break;
+               case ':':
+               case '?':
+               case 'h':
+                       print_help();
+                       return 1;
+               default:
+                       assert(0);
+               }
+       }
+
+       if (cache_path == NULL) {
+               print_help();
+               return 1;
+       }
+
+       int ret = kr_cache_gc(cache_path);
+       if (ret) {
+               printf("Error (%s)\n", kr_strerror(ret));
+               return 10;
+       }
+
+       return 0;
+}
+