ALPM API adjustments for sanity and consistency
[pacman-ng.git] / src / pacman / remove.c
blobd63782369b7aae591904776992ea1797a69fe7e9
1 /*
2 * remove.c
4 * Copyright (c) 2006-2011 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, "'%s': target not found\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_PKG_INVALID_ARCH:
119 for(i = data; i; i = alpm_list_next(i)) {
120 const char *pkg = i->data;
121 printf(_(":: package %s does not have a valid architecture\n"), pkg);
123 break;
124 case ALPM_ERR_UNSATISFIED_DEPS:
125 for(i = data; i; i = alpm_list_next(i)) {
126 alpm_depmissing_t *miss = i->data;
127 char *depstring = alpm_dep_compute_string(miss->depend);
128 printf(_(":: %s: requires %s\n"), miss->target, depstring);
129 free(depstring);
131 break;
132 default:
133 break;
135 FREELIST(data);
136 retval = 1;
137 goto cleanup;
140 /* Search for holdpkg in target list */
141 int holdpkg = 0;
142 for(i = alpm_trans_get_remove(config->handle); i; i = alpm_list_next(i)) {
143 alpm_pkg_t *pkg = i->data;
144 if(alpm_list_find(config->holdpkg, alpm_pkg_get_name(pkg), fnmatch_cmp)) {
145 pm_printf(ALPM_LOG_WARNING, _("%s is designated as a HoldPkg.\n"),
146 alpm_pkg_get_name(pkg));
147 holdpkg = 1;
150 if(holdpkg && (noyes(_("HoldPkg was found in target list. Do you want to continue?")) == 0)) {
151 retval = 1;
152 goto cleanup;
155 /* Step 3: actually perform the removal */
156 alpm_list_t *pkglist = alpm_trans_get_remove(config->handle);
157 if(pkglist == NULL) {
158 printf(_(" there is nothing to do\n"));
159 goto cleanup; /* we are done */
162 if(config->print) {
163 print_packages(pkglist);
164 goto cleanup;
167 /* print targets and ask user confirmation */
168 display_targets();
169 printf("\n");
170 if(yesno(_("Do you want to remove these packages?")) == 0) {
171 retval = 1;
172 goto cleanup;
175 if(alpm_trans_commit(config->handle, &data) == -1) {
176 pm_printf(ALPM_LOG_ERROR, _("failed to commit transaction (%s)\n"),
177 alpm_strerror(alpm_errno(config->handle)));
178 retval = 1;
181 FREELIST(data);
183 /* Step 4: release transaction resources */
184 cleanup:
185 if(trans_release() == -1) {
186 retval = 1;
188 return retval;
191 /* vim: set ts=2 sw=2 noet: */