]> git.ipfire.org Git - people/ms/pakfire.git/blob - src/cli/lib/upload_delete.c
cli: Check for root privileges when needed
[people/ms/pakfire.git] / src / cli / lib / upload_delete.c
1 /*#############################################################################
2 # #
3 # Pakfire - The IPFire package management system #
4 # Copyright (C) 2023 Pakfire development team #
5 # #
6 # This program is free software: you can redistribute it and/or modify #
7 # it under the terms of the GNU General Public License as published by #
8 # the Free Software Foundation, either version 3 of the License, or #
9 # (at your option) any later version. #
10 # #
11 # This program is distributed in the hope that it will be useful, #
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14 # GNU General Public License for more details. #
15 # #
16 # You should have received a copy of the GNU General Public License #
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
18 # #
19 #############################################################################*/
20
21 #include <argp.h>
22
23 #include <pakfire/buildservice.h>
24 #include <pakfire/util.h>
25
26 #include "command.h"
27 #include "pakfire.h"
28 #include "upload_delete.h"
29
30 static const char* args_doc = "FILES...";
31
32 static const char* doc = "Deletes an uploaded file";
33
34 #define MAX_UPLOADS 32
35
36 struct config {
37 const char* uploads[MAX_UPLOADS];
38 unsigned int num_uploads;
39 };
40
41 static error_t parse(int key, char* arg, struct argp_state* state, void* data) {
42 struct config* config = data;
43
44 switch (key) {
45 case ARGP_KEY_ARG:
46 if (config->num_uploads >= MAX_UPLOADS)
47 return -ENOBUFS;
48
49 // Validate the UUID
50 if (!pakfire_uuid_is_valid(arg))
51 argp_error(state, "Invalid UUID");
52
53 config->uploads[config->num_uploads++] = arg;
54 break;
55
56 default:
57 return ARGP_ERR_UNKNOWN;
58 }
59
60 return 0;
61 }
62
63 int cli_upload_delete(void* data, int argc, char* argv[]) {
64 struct pakfire_buildservice* service = NULL;
65 struct config config = {};
66 int r;
67
68 struct cli_config* cli_config = data;
69
70 // Parse the command line
71 r = cli_parse(NULL, NULL, args_doc, doc, parse, 0, argc, argv, &config);
72 if (r)
73 goto ERROR;
74
75 // Connect to the build service
76 r = pakfire_buildservice_create(&service, cli_config->ctx);
77 if (r)
78 goto ERROR;
79
80 // Delete uploads
81 for (unsigned int i = 0; i < config.num_uploads; i++) {
82 r = pakfire_buildservice_delete_upload(service, config.uploads[i]);
83 if (r)
84 goto ERROR;
85 }
86
87 ERROR:
88 if (service)
89 pakfire_buildservice_unref(service);
90
91 return r;
92 }