Welcome to little lamb

Code » ssp » commit ecc8134

Add command remove

author Olivier Brunel
2023-04-18 12:31:02 UTC
committer Olivier Brunel
2023-04-19 07:34:50 UTC
parent d15cb32cccc7216bcd006b6715ebef27b12841b6

Add command remove

src/ssp/commands.c +2 -0
src/ssp/database.c +7 -5
src/ssp/remove.c +42 -0

diff --git a/src/ssp/commands.c b/src/ssp/commands.c
index cd78a49..8d181b2 100644
--- a/src/ssp/commands.c
+++ b/src/ssp/commands.c
@@ -5,12 +5,14 @@
 
 extern struct command command_add;
 extern struct command command_list;
+extern struct command command_remove;
 extern struct command command_rename;
 extern struct command command_show;
 
 struct command *commands[] = {
     &command_add,
     &command_list,
+    &command_remove,
     &command_rename,
     &command_show,
     0
diff --git a/src/ssp/database.c b/src/ssp/database.c
index 31d6906..b02466c 100644
--- a/src/ssp/database.c
+++ b/src/ssp/database.c
@@ -101,22 +101,24 @@ open_db(struct ssp *ctx)
 void
 rebuild_db(const char *oldkey, const char *newkey, const struct otp *otp, struct ssp *ctx)
 {
-    size_t olen = 0, nlen;
-    size_t vlen = sizeof(*otp) + otp->slen + strlen(otp->data + otp->slen) + 1;
+    size_t olen = 0, nlen = 0, vlen = 0;
     cdbmaker_sa mkr;
     int r = 0;
 
-    nlen = strlen(newkey) + 1;
+    if (newkey) {
+        nlen = strlen(newkey) + 1;
+        vlen = sizeof(*otp) + otp->slen + strlen(otp->data + otp->slen) + 1;
+    }
     if (oldkey) olen = strlen(oldkey) + 1;
 
     if (!cdbmaker_sa_start(&mkr))
         diefusys(EX_TEMPFAIL, "save database");
     /* new file? */
     if (!ctx->cdb.map) {
-        if (!cdbmaker_sa_add(&mkr, newkey, nlen, (char *) otp, vlen))
+        if (nlen && !cdbmaker_sa_add(&mkr, newkey, nlen, (char *) otp, vlen))
             diefusys(EX_TEMPFAIL, "save database");
     } else {
-        int a = 0;
+        int a = !nlen;
         db_reset(ctx);
         while ((r = db_next(ctx)) == 1) {
             /* existing newkey isn't allowed when adding or renaming */
diff --git a/src/ssp/remove.c b/src/ssp/remove.c
new file mode 100644
index 0000000..025da91
--- /dev/null
+++ b/src/ssp/remove.c
@@ -0,0 +1,42 @@
+/* This file is part of ssp                             https://lila.oss/ssp
+ * Copyright (C) 2023 Olivier Brunel                          jjk@jjacky.com */
+/* SPDX-License-Identifier: GPL-2.0-only */
+#include <errno.h>
+#include <limb/command.h>
+#include <limb/exitcode.h>
+#include <limb/output.h>
+#include "ssp.h"
+
+
+COMMAND(remove, "Remove a site from database", "<site>", NULL);
+
+int
+remove_main(int argc, const char *argv[], const char *env[], const char usage[], void *ctx_)
+{
+    struct ssp *ctx = ctx_;
+
+    if (argc != 2)
+        diecmdusage(EX_USAGE, usage, &command_remove);
+
+    const char *site = get_site_name(argv[1], ctx);
+    if (!site)
+        diefusys(exitcode_from_errno(errno), "parse site's name");
+
+    int r = open_db(ctx);
+    if (r < 0)
+        diefu(exitcode_from_errno(errno), "open database");
+    if (!r)
+        dief(0, "no database");
+
+    r = db_find(site, ctx);
+    if (r < 0)
+        diefu(EX_DATA_ERR, "find site ", ESC, site, ESC);
+    if (!r) {
+        out("no site ", ESC, site, ESC, " in database");
+    } else {
+        out("Removing ", ESC, site, ESC, "...");
+        rebuild_db(site, NULL, NULL, ctx);
+    }
+
+    return 0;
+}