]> git.ipfire.org Git - pakfire.git/blob - src/cli/lib/remove.c
5d13a40e178f3e80df4b6c00772b44c6d539e0dd
[pakfire.git] / src / cli / lib / remove.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/root.h>
24
25 #include "command.h"
26 #include "pakfire.h"
27 #include "remove.h"
28 #include "transaction.h"
29
30 static const char* args_doc = "remove [OPTIONS...] PACKAGES...";
31
32 static const char* doc = "Remove packages";
33
34 #define MAX_PACKAGES 128
35
36 struct cli_local_args {
37 int job_flags;
38
39 // Packages
40 char* packages[MAX_PACKAGES];
41 unsigned int num_packages;
42 };
43
44 enum {
45 OPT_KEEP_DEPENDENCIES = 1,
46 };
47
48 static struct argp_option options[] = {
49 { "keep-dependencies", OPT_KEEP_DEPENDENCIES, NULL, 0, "Keep previously installed dependencies", 0 },
50 { NULL },
51 };
52
53 static error_t parse(int key, char* arg, struct argp_state* state, void* data) {
54 struct cli_local_args* args = data;
55
56 switch (key) {
57 case OPT_KEEP_DEPENDENCIES:
58 args->job_flags |= PAKFIRE_JOB_KEEP_DEPS;
59 break;
60
61 case ARGP_KEY_ARG:
62 if (args->num_packages >= MAX_PACKAGES)
63 return -ENOBUFS;
64
65 args->packages[args->num_packages++] = arg;
66 break;
67
68 default:
69 return ARGP_ERR_UNKNOWN;
70 }
71
72 return 0;
73 }
74
75 static int __cli_remove(pakfire_transaction* transaction, int argc, char* argv[], void* data) {
76 struct cli_local_args* args = data;
77 int r;
78
79 for (unsigned int i = 0; i < args->num_packages; i++) {
80 r = pakfire_transaction_request(transaction,
81 PAKFIRE_JOB_ERASE, args->packages[i], args->job_flags);
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
91 int cli_remove(void* data, int argc, char* argv[]) {
92 struct cli_global_args* global_args = data;
93 struct cli_local_args local_args = {};
94 pakfire_root* root = NULL;
95 int r;
96
97 // Parse the command line
98 r = cli_parse(options, NULL, args_doc, doc, parse, 0, argc, argv, &local_args);
99 if (r)
100 goto ERROR;
101
102 // Setup root
103 r = cli_setup_root(&root, global_args);
104 if (r)
105 goto ERROR;
106
107 r = cli_transaction(global_args->ctx, root, argc, argv, 0, __cli_remove, &local_args);
108
109 ERROR:
110 if (root)
111 pakfire_root_unref(root);
112
113 return r;
114 }