]> git.ipfire.org Git - pakfire.git/commitdiff
buildservice: Implement deleting uploads
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 19 Oct 2023 21:15:31 +0000 (21:15 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 19 Oct 2023 21:15:31 +0000 (21:15 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/cli/lib/upload.c
src/cli/lib/upload_delete.c [new file with mode: 0644]
src/cli/lib/upload_delete.h [new file with mode: 0644]
src/libpakfire/buildservice.c
src/libpakfire/include/pakfire/buildservice.h

index 20dc64ff1ed6b82f718eecdf925f10a09dbfc6ca..85e9e5fc968b903ac926d586f3ba2360ddd62645 100644 (file)
@@ -513,6 +513,8 @@ libcli_la_SOURCES = \
        src/cli/lib/upload.h \
        src/cli/lib/upload_create.c \
        src/cli/lib/upload_create.h \
+       src/cli/lib/upload_delete.c \
+       src/cli/lib/upload_delete.h \
        src/cli/lib/upload_list.c \
        src/cli/lib/upload_list.h \
        src/cli/lib/version.c \
index 1396dc4ae84722fd4adba60015c4a0d940d7711e..5a5dfd83817dfda184b399965e6a0d46fb09075a 100644 (file)
 #include "command.h"
 #include "upload.h"
 #include "upload_create.h"
+#include "upload_delete.h"
 #include "upload_list.h"
 
 static const char* args_doc =
        "create PATH\n"
-       "list";
+       "list\n"
+       "delete UUID...";
 
 int cli_upload(void* data, int argc, char* argv[]) {
        static const struct command commands[] = {
                { "create", cli_upload_create, 1, -1, 0 },
+               { "delete", cli_upload_delete, 1, -1, 0 },
                { "list",   cli_upload_list,   0,  0, 0 },
                { NULL },
        };
diff --git a/src/cli/lib/upload_delete.c b/src/cli/lib/upload_delete.c
new file mode 100644 (file)
index 0000000..e85477e
--- /dev/null
@@ -0,0 +1,92 @@
+/*#############################################################################
+#                                                                             #
+# Pakfire - The IPFire package management system                              #
+# Copyright (C) 2023 Pakfire development team                                 #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#include <argp.h>
+
+#include <pakfire/buildservice.h>
+#include <pakfire/ctx.h>
+#include <pakfire/util.h>
+
+#include "command.h"
+#include "upload_delete.h"
+
+static const char* args_doc = "FILES...";
+
+static const char* doc = "Deletes an uploaded file";
+
+#define MAX_UPLOADS 32
+
+struct config {
+       const char* uploads[MAX_UPLOADS];
+       unsigned int num_uploads;
+};
+
+static error_t parse(int key, char* arg, struct argp_state* state, void* data) {
+       struct config* config = data;
+
+       switch (key) {
+               case ARGP_KEY_ARG:
+                       if (config->num_uploads >= MAX_UPLOADS)
+                               return -ENOBUFS;
+
+                       // Validate the UUID
+                       if (!pakfire_uuid_is_valid(arg))
+                               argp_error(state, "Invalid UUID");
+
+                       config->uploads[config->num_uploads++] = arg;
+                       break;
+
+               default:
+                       return ARGP_ERR_UNKNOWN;
+       }
+
+       return 0;
+}
+
+int cli_upload_delete(void* data, int argc, char* argv[]) {
+       struct pakfire_buildservice* service = NULL;
+       struct config config = {};
+       int r;
+
+       struct pakfire_ctx* ctx = data;
+
+       // Parse the command line
+       r = cli_parse(NULL, NULL, args_doc, doc, parse, argc, argv, &config);
+       if (r)
+               goto ERROR;
+
+       // Connect to the build service
+       r = pakfire_buildservice_create(&service, ctx);
+       if (r)
+               goto ERROR;
+
+       // Delete uploads
+       for (unsigned int i = 0; i < config.num_uploads; i++) {
+               r = pakfire_buildservice_delete_upload(service, config.uploads[i]);
+               if (r)
+                       goto ERROR;
+       }
+
+ERROR:
+       if (service)
+               pakfire_buildservice_unref(service);
+
+       return r;
+}
diff --git a/src/cli/lib/upload_delete.h b/src/cli/lib/upload_delete.h
new file mode 100644 (file)
index 0000000..e16c3c6
--- /dev/null
@@ -0,0 +1,26 @@
+/*#############################################################################
+#                                                                             #
+# Pakfire - The IPFire package management system                              #
+# Copyright (C) 2023 Pakfire development team                                 #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#ifndef PAKFIRE_CLI_UPLOAD_DELETE_H
+#define PAKFIRE_CLI_UPLOAD_DELETE_H
+
+int cli_upload_delete(void* data, int argc, char* argv[]);
+
+#endif /* PAKFIRE_CLI_UPLOAD_DELETE_H */
index 1506e72f4734d57d6428b274bea36b429d3cb758..a64361e76990367f49f7099944e22e35e7fcd957 100644 (file)
@@ -577,3 +577,60 @@ ERROR:
 
        return r;
 }
+
+PAKFIRE_EXPORT int pakfire_buildservice_delete_upload(
+               struct pakfire_buildservice* service, const char* uuid) {
+       struct pakfire_transfer* transfer = NULL;
+       struct json_object* response = NULL;
+       char* buffer = NULL;
+       size_t length = 0;
+       char url[PATH_MAX];
+       int r;
+
+       // Compose the URL
+       r = pakfire_string_format(url, "/api/v1/uploads/%s", uuid);
+       if (r)
+               goto ERROR;
+
+       // Create a new transfer
+       r = pakfire_buildservice_create_transfer(&transfer, service, url);
+       if (r)
+               goto ERROR;
+
+       // Ask to DELETE
+       r = pakfire_downloader_transfer_set_method(transfer, PAKFIRE_METHOD_DELETE);
+       if (r)
+               goto ERROR;
+
+       // Enable authentication
+       r = pakfire_downloader_transfer_auth(transfer);
+       if (r)
+               goto ERROR;
+
+       // Write the response to the buffer
+       r = pakfire_downloader_transfer_set_output_buffer(transfer, &buffer, &length);
+       if (r)
+               goto ERROR;
+
+       // Run the transfer
+       r = pakfire_downloader_transfer_run(transfer, PAKFIRE_TRANSFER_NO_PROGRESS);
+       if (r)
+               goto ERROR;
+
+       // Parse the response
+       r = pakfire_buildservice_parse_response(service, transfer, buffer, length, &response);
+       if (r) {
+               CTX_ERROR(service->ctx, "Could not parse the response: %s\n", strerror(-r));
+               goto ERROR;
+       }
+
+ERROR:
+       if (transfer)
+               pakfire_downloader_transfer_unref(transfer);
+       if (response)
+               json_object_put(response);
+       if (buffer)
+               free(buffer);
+
+       return r;
+}
index 80576e623bd4c853309038598bb394ae88b82bcd..4b70d10d220e17fcc5a0993e8bc05386e8ae90dd 100644 (file)
@@ -38,5 +38,7 @@ int pakfire_buildservice_upload(struct pakfire_buildservice* service,
        const char* path, const char* filename);
 int pakfire_buildservice_list_uploads(
        struct pakfire_buildservice* service, struct json_object** uploads);
+int pakfire_buildservice_delete_upload(
+       struct pakfire_buildservice* service, const char* uuid);
 
 #endif /* PAKFIRE_BUILDSERVICE_H */