]> git.ipfire.org Git - pakfire.git/blob - src/cli/lib/upload_create.c
662b94c91c32fc59d55da21ede062b8a056118d9
[pakfire.git] / src / cli / lib / upload_create.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/client.h>
24
25 #include "api.h"
26 #include "command.h"
27 #include "pakfire.h"
28 #include "upload_create.h"
29
30 static const char* args_doc = "FILES...";
31
32 static const char* doc = "Uploads a file";
33
34 #define MAX_FILES 32
35
36 struct cli_local_args {
37 const char* files[MAX_FILES];
38 unsigned int num_files;
39 };
40
41 static error_t parse(int key, char* arg, struct argp_state* state, void* data) {
42 struct cli_local_args* args = data;
43
44 switch (key) {
45 case ARGP_KEY_ARG:
46 if (args->num_files >= MAX_FILES)
47 return -ENOBUFS;
48
49 args->files[args->num_files++] = arg;
50 break;
51
52 default:
53 return ARGP_ERR_UNKNOWN;
54 }
55
56 return 0;
57 }
58
59 static int upload_callback(pakfire_client* client, const pakfire_xfer_response* response,
60 const char* path, const char* uuid, void* data) {
61 int r;
62
63 // Handle errors
64 r = cli_api_error(response, "Failed to upload the archive");
65 if (r < 0)
66 return 0;
67
68 printf("Successfully uploaded %s as %s\n", path, uuid);
69
70 return 0;
71 }
72
73 static int ready_callback(pakfire_client* client, void* data) {
74 const struct cli_local_args* local_args = data;
75 int r;
76
77 // Create all uploads
78 for (unsigned int i = 0; i < local_args->num_files; i++) {
79 r = pakfire_client_upload_create(client, local_args->files[i], NULL, upload_callback, NULL);
80 if (r < 0)
81 return r;
82 }
83
84 return 0;
85 }
86
87 int cli_upload_create(void* data, int argc, char* argv[]) {
88 struct cli_global_args* global_args = data;
89 struct cli_local_args local_args = {};
90 int r;
91
92 // Parse the command line
93 r = cli_parse(NULL, NULL, args_doc, doc, parse, 0, argc, argv, &local_args);
94 if (r)
95 return r;
96
97 // Run the client
98 return cli_run_client(global_args, ready_callback, &local_args);
99 }