]> git.ipfire.org Git - pakfire.git/blame - src/cli/lib/remove.c
cli: Check for root privileges when needed
[pakfire.git] / src / cli / lib / remove.c
CommitLineData
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
MT
22
23#include <pakfire/pakfire.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
30static const char* args_doc = "remove [OPTIONS...] PACKAGES...";
31
32static const char* doc = "Remove packages";
33
34#define MAX_PACKAGES 128
35
c9abb903
MT
36struct config {
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
44enum {
45 OPT_KEEP_DEPENDENCIES = 1,
46};
c9abb903 47
38d531d3
MT
48static struct argp_option options[] = {
49 { "keep-dependencies", OPT_KEEP_DEPENDENCIES, NULL, 0, "Keep previously installed dependencies", 0 },
50 { NULL },
51};
c9abb903 52
38d531d3
MT
53static error_t parse(int key, char* arg, struct argp_state* state, void* data) {
54 struct config* config = data;
c9abb903 55
38d531d3
MT
56 switch (key) {
57 case OPT_KEEP_DEPENDENCIES:
58 config->job_flags |= PAKFIRE_JOB_KEEP_DEPS;
59 break;
c9abb903 60
38d531d3
MT
61 case ARGP_KEY_ARG:
62 if (config->num_packages >= MAX_PACKAGES)
63 return -ENOBUFS;
c9abb903 64
38d531d3
MT
65 config->packages[config->num_packages++] = arg;
66 break;
c9abb903 67
38d531d3
MT
68 default:
69 return ARGP_ERR_UNKNOWN;
c9abb903
MT
70 }
71
72 return 0;
73}
74
75static int __cli_remove(struct pakfire_transaction* transaction, int argc, char* argv[], void* data) {
76 struct config* config = (struct config*)data;
77 int r;
78
38d531d3 79 for (unsigned int i = 0; i < config->num_packages; i++) {
c9abb903 80 r = pakfire_transaction_request(transaction,
38d531d3 81 PAKFIRE_JOB_ERASE, config->packages[i], config->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
MT
91int cli_remove(void* data, int argc, char* argv[]) {
92 struct pakfire* pakfire = NULL;
93 struct config config = {};
c9abb903
MT
94 int r;
95
38d531d3
MT
96 struct cli_config* cli_config = data;
97
98 // Parse the command line
9b8d2c6e 99 r = cli_parse(options, NULL, args_doc, doc, parse, 0, argc, argv, &config);
c9abb903 100 if (r)
38d531d3
MT
101 goto ERROR;
102
103 // Setup Pakfire
104 r = cli_setup_pakfire(&pakfire, cli_config);
105 if (r)
106 goto ERROR;
107
108 r = cli_transaction(pakfire, argc, argv, 0, __cli_remove, &config);
109
110ERROR:
111 if (pakfire)
112 pakfire_unref(pakfire);
c9abb903 113
38d531d3 114 return r;
c9abb903 115}