]>
Commit | Line | Data |
---|---|---|
01840335 MT |
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 | ||
a455cba0 | 23 | #include <pakfire/client.h> |
01840335 | 24 | |
4f4c2d9c | 25 | #include "api.h" |
01840335 | 26 | #include "command.h" |
4e38be65 | 27 | #include "pakfire.h" |
01840335 MT |
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 | ||
343d9736 | 36 | struct cli_local_args { |
01840335 MT |
37 | const char* files[MAX_FILES]; |
38 | unsigned int num_files; | |
39 | }; | |
40 | ||
d673ca6c | 41 | static error_t parse(int key, char* arg, struct argp_state* state, void* data) { |
343d9736 | 42 | struct cli_local_args* args = data; |
01840335 MT |
43 | |
44 | switch (key) { | |
45 | case ARGP_KEY_ARG: | |
343d9736 | 46 | if (args->num_files >= MAX_FILES) |
01840335 MT |
47 | return -ENOBUFS; |
48 | ||
343d9736 | 49 | args->files[args->num_files++] = arg; |
01840335 MT |
50 | break; |
51 | ||
52 | default: | |
53 | return ARGP_ERR_UNKNOWN; | |
54 | } | |
55 | ||
56 | return 0; | |
57 | } | |
58 | ||
4f4c2d9c MT |
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; | |
c3813226 | 62 | |
4f4c2d9c MT |
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); | |
c3813226 MT |
69 | |
70 | return 0; | |
71 | } | |
72 | ||
354eb087 | 73 | static int ready_callback(pakfire_client* client, void* data) { |
c3813226 MT |
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++) { | |
354eb087 | 79 | r = pakfire_client_upload_create(client, local_args->files[i], NULL, upload_callback, NULL); |
c3813226 MT |
80 | if (r < 0) |
81 | return r; | |
82 | } | |
83 | ||
84 | return 0; | |
85 | } | |
86 | ||
01840335 | 87 | int cli_upload_create(void* data, int argc, char* argv[]) { |
343d9736 MT |
88 | struct cli_global_args* global_args = data; |
89 | struct cli_local_args local_args = {}; | |
01840335 MT |
90 | int r; |
91 | ||
01840335 | 92 | // Parse the command line |
343d9736 | 93 | r = cli_parse(NULL, NULL, args_doc, doc, parse, 0, argc, argv, &local_args); |
01840335 | 94 | if (r) |
ed60efa2 | 95 | return r; |
81adcd5c | 96 | |
c3813226 | 97 | // Run the client |
ed60efa2 | 98 | return cli_run_client(global_args, ready_callback, &local_args); |
01840335 | 99 | } |