Move important information up in -Si output
[pacman-ng.git] / src / pacman / remove.c
blob472adb525a4f8f18ea4eb1e17433cf759acca824
1 /*
2 * remove.c
4 * Copyright (c) 2006-2012 Pacman Development Team <pacman-dev@archlinux.org>
5 * Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include <fnmatch.h>
22 #include <stdlib.h>
23 #include <stdio.h>
25 #include <alpm.h>
26 #include <alpm_list.h>
28 /* pacman */
29 #include "pacman.h"
30 #include "util.h"
31 #include "conf.h"
33 static int fnmatch_cmp(const void *pattern, const void *string)
35 return fnmatch(pattern, string, 0);
38 static int remove_target(const char *target)
40 alpm_pkg_t *pkg;
41 alpm_db_t *db_local = alpm_get_localdb(config->handle);
42 alpm_list_t *p;
44 if((pkg = alpm_db_get_pkg(db_local, target)) != NULL) {
45 if(alpm_remove_pkg(config->handle, pkg) == -1) {
46 pm_printf(ALPM_LOG_ERROR, "'%s': %s\n", target,
47 alpm_strerror(alpm_errno(config->handle)));
48 return -1;
50 config->explicit_removes = alpm_list_add(config->explicit_removes, pkg);
51 return 0;
54 /* fallback to group */
55 alpm_group_t *grp = alpm_db_get_group(db_local, target);
56 if(grp == NULL) {
57 pm_printf(ALPM_LOG_ERROR, _("target not found: %s\n"), target);
58 return -1;
60 for(p = grp->packages; p; p = alpm_list_next(p)) {
61 pkg = p->data;
62 if(alpm_remove_pkg(config->handle, pkg) == -1) {
63 pm_printf(ALPM_LOG_ERROR, "'%s': %s\n", target,
64 alpm_strerror(alpm_errno(config->handle)));
65 return -1;
67 config->explicit_removes = alpm_list_add(config->explicit_removes, pkg);
69 return 0;
72 /**
73 * @brief Remove a specified list of packages.
75 * @param targets a list of packages (as strings) to remove from the system
77 * @return 0 on success, 1 on failure
79 int pacman_remove(alpm_list_t *targets)
81 int retval = 0;
82 alpm_list_t *i, *data = NULL;
84 if(targets == NULL) {
85 pm_printf(ALPM_LOG_ERROR, _("no targets specified (use -h for help)\n"));
86 return 1;
89 /* Step 0: create a new transaction */
90 if(trans_init(config->flags, 0) == -1) {
91 return 1;
94 /* Step 1: add targets to the created transaction */
95 for(i = targets; i; i = alpm_list_next(i)) {
96 char *target = i->data;
97 char *targ = strchr(target, '/');
98 if(targ && strncmp(target, "local", 5) == 0) {
99 targ++;
100 } else {
101 targ = target;
103 if(remove_target(targ) == -1) {
104 retval = 1;
108 if(retval == 1) {
109 goto cleanup;
112 /* Step 2: prepare the transaction based on its type, targets and flags */
113 if(alpm_trans_prepare(config->handle, &data) == -1) {
114 alpm_errno_t err = alpm_errno(config->handle);
115 pm_printf(ALPM_LOG_ERROR, _("failed to prepare transaction (%s)\n"),
116 alpm_strerror(err));
117 switch(err) {
118 case ALPM_ERR_UNSATISFIED_DEPS:
119 for(i = data; i; i = alpm_list_next(i)) {
120 alpm_depmissing_t *miss = i->data;
121 char *depstring = alpm_dep_compute_string(miss->depend);
122 printf(_(":: %s: requires %s\n"), miss->target, depstring);
123 free(depstring);
125 break;
126 default:
127 break;
129 FREELIST(data);
130 retval = 1;
131 goto cleanup;
134 /* Search for holdpkg in target list */
135 int holdpkg = 0;
136 for(i = alpm_trans_get_remove(config->handle); i; i = alpm_list_next(i)) {
137 alpm_pkg_t *pkg = i->data;
138 if(alpm_list_find(config->holdpkg, alpm_pkg_get_name(pkg), fnmatch_cmp)) {
139 pm_printf(ALPM_LOG_WARNING, _("%s is designated as a HoldPkg.\n"),
140 alpm_pkg_get_name(pkg));
141 holdpkg = 1;
144 if(holdpkg && (noyes(_("HoldPkg was found in target list. Do you want to continue?")) == 0)) {
145 retval = 1;
146 goto cleanup;
149 /* Step 3: actually perform the removal */
150 alpm_list_t *pkglist = alpm_trans_get_remove(config->handle);
151 if(pkglist == NULL) {
152 printf(_(" there is nothing to do\n"));
153 goto cleanup; /* we are done */
156 if(config->print) {
157 print_packages(pkglist);
158 goto cleanup;
161 /* print targets and ask user confirmation */
162 display_targets();
163 printf("\n");
164 if(yesno(_("Do you want to remove these packages?")) == 0) {
165 retval = 1;
166 goto cleanup;
169 if(alpm_trans_commit(config->handle, &data) == -1) {
170 pm_printf(ALPM_LOG_ERROR, _("failed to commit transaction (%s)\n"),
171 alpm_strerror(alpm_errno(config->handle)));
172 retval = 1;
175 FREELIST(data);
177 /* Step 4: release transaction resources */
178 cleanup:
179 if(trans_release() == -1) {
180 retval = 1;
182 return retval;
185 /* vim: set ts=2 sw=2 noet: */