From: Michael Tremer Date: Thu, 19 Oct 2023 21:15:31 +0000 (+0000) Subject: buildservice: Implement deleting uploads X-Git-Tag: 0.9.30~1434 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=c0b6198a6ed0c77aa858abc46bb8f9d2d327f23c;p=pakfire.git buildservice: Implement deleting uploads Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index 20dc64ff1..85e9e5fc9 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 \ diff --git a/src/cli/lib/upload.c b/src/cli/lib/upload.c index 1396dc4ae..5a5dfd838 100644 --- a/src/cli/lib/upload.c +++ b/src/cli/lib/upload.c @@ -21,15 +21,18 @@ #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 index 000000000..e85477e5f --- /dev/null +++ b/src/cli/lib/upload_delete.c @@ -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 . # +# # +#############################################################################*/ + +#include + +#include +#include +#include + +#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 index 000000000..e16c3c654 --- /dev/null +++ b/src/cli/lib/upload_delete.h @@ -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 . # +# # +#############################################################################*/ + +#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 */ diff --git a/src/libpakfire/buildservice.c b/src/libpakfire/buildservice.c index 1506e72f4..a64361e76 100644 --- a/src/libpakfire/buildservice.c +++ b/src/libpakfire/buildservice.c @@ -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; +} diff --git a/src/libpakfire/include/pakfire/buildservice.h b/src/libpakfire/include/pakfire/buildservice.h index 80576e623..4b70d10d2 100644 --- a/src/libpakfire/include/pakfire/buildservice.h +++ b/src/libpakfire/include/pakfire/buildservice.h @@ -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 */