]>
Commit | Line | Data |
---|---|---|
c9abb903 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 | ||
38d531d3 | 21 | #include <argp.h> |
c9abb903 | 22 | |
e0ad83d5 | 23 | #include <pakfire/root.h> |
c9abb903 | 24 | |
38d531d3 MT |
25 | #include "command.h" |
26 | #include "pakfire.h" | |
c9abb903 MT |
27 | #include "remove.h" |
28 | #include "transaction.h" | |
29 | ||
38d531d3 MT |
30 | static const char* args_doc = "remove [OPTIONS...] PACKAGES..."; |
31 | ||
32 | static const char* doc = "Remove packages"; | |
33 | ||
34 | #define MAX_PACKAGES 128 | |
35 | ||
343d9736 | 36 | struct cli_local_args { |
c9abb903 | 37 | int job_flags; |
38d531d3 MT |
38 | |
39 | // Packages | |
40 | char* packages[MAX_PACKAGES]; | |
41 | unsigned int num_packages; | |
c9abb903 MT |
42 | }; |
43 | ||
38d531d3 MT |
44 | enum { |
45 | OPT_KEEP_DEPENDENCIES = 1, | |
46 | }; | |
c9abb903 | 47 | |
38d531d3 MT |
48 | static struct argp_option options[] = { |
49 | { "keep-dependencies", OPT_KEEP_DEPENDENCIES, NULL, 0, "Keep previously installed dependencies", 0 }, | |
50 | { NULL }, | |
51 | }; | |
c9abb903 | 52 | |
38d531d3 | 53 | static error_t parse(int key, char* arg, struct argp_state* state, void* data) { |
343d9736 | 54 | struct cli_local_args* args = data; |
c9abb903 | 55 | |
38d531d3 MT |
56 | switch (key) { |
57 | case OPT_KEEP_DEPENDENCIES: | |
343d9736 | 58 | args->job_flags |= PAKFIRE_JOB_KEEP_DEPS; |
38d531d3 | 59 | break; |
c9abb903 | 60 | |
38d531d3 | 61 | case ARGP_KEY_ARG: |
343d9736 | 62 | if (args->num_packages >= MAX_PACKAGES) |
38d531d3 | 63 | return -ENOBUFS; |
c9abb903 | 64 | |
343d9736 | 65 | args->packages[args->num_packages++] = arg; |
38d531d3 | 66 | break; |
c9abb903 | 67 | |
38d531d3 MT |
68 | default: |
69 | return ARGP_ERR_UNKNOWN; | |
c9abb903 MT |
70 | } |
71 | ||
72 | return 0; | |
73 | } | |
74 | ||
afe7ff29 | 75 | static int __cli_remove(pakfire_transaction* transaction, int argc, char* argv[], void* data) { |
343d9736 | 76 | struct cli_local_args* args = data; |
c9abb903 MT |
77 | int r; |
78 | ||
343d9736 | 79 | for (unsigned int i = 0; i < args->num_packages; i++) { |
c9abb903 | 80 | r = pakfire_transaction_request(transaction, |
343d9736 | 81 | PAKFIRE_JOB_ERASE, args->packages[i], args->job_flags); |
c9abb903 MT |
82 | if (r) { |
83 | fprintf(stderr, "Could not find '%s': %m\n", argv[i]); | |
84 | return r; | |
85 | } | |
86 | } | |
87 | ||
88 | return 0; | |
89 | } | |
90 | ||
38d531d3 | 91 | int cli_remove(void* data, int argc, char* argv[]) { |
343d9736 MT |
92 | struct cli_global_args* global_args = data; |
93 | struct cli_local_args local_args = {}; | |
c87e1399 | 94 | pakfire_root* root = NULL; |
c9abb903 MT |
95 | int r; |
96 | ||
38d531d3 | 97 | // Parse the command line |
343d9736 | 98 | r = cli_parse(options, NULL, args_doc, doc, parse, 0, argc, argv, &local_args); |
c9abb903 | 99 | if (r) |
38d531d3 MT |
100 | goto ERROR; |
101 | ||
c87e1399 MT |
102 | // Setup root |
103 | r = cli_setup_root(&root, global_args); | |
38d531d3 MT |
104 | if (r) |
105 | goto ERROR; | |
106 | ||
614a64c7 | 107 | r = cli_transaction(global_args->ctx, root, argc, argv, 0, __cli_remove, &local_args); |
38d531d3 MT |
108 | |
109 | ERROR: | |
c87e1399 MT |
110 | if (root) |
111 | pakfire_root_unref(root); | |
c9abb903 | 112 | |
38d531d3 | 113 | return r; |
c9abb903 | 114 | } |