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/>.
27 #include <alpm_list.h>
34 static int remove_target(char *target
)
37 pmdb_t
*db_local
= alpm_option_get_localdb();
40 if((info
= alpm_db_get_pkg(db_local
, target
)) != NULL
) {
41 if(alpm_remove_pkg(info
) == -1) {
42 pm_fprintf(stderr
, PM_LOG_ERROR
, "'%s': %s\n", target
, alpm_strerrorlast());
48 /* fallback to group */
49 pmgrp_t
*grp
= alpm_db_readgrp(db_local
, target
);
51 pm_fprintf(stderr
, PM_LOG_ERROR
, "'%s': target not found\n", target
);
54 for(p
= alpm_grp_get_pkgs(grp
); p
; p
= alpm_list_next(p
)) {
55 pmpkg_t
*pkg
= alpm_list_getdata(p
);
56 if(alpm_remove_pkg(pkg
) == -1) {
57 pm_fprintf(stderr
, PM_LOG_ERROR
, "'%s': %s\n", target
, alpm_strerrorlast());
65 * @brief Remove a specified list of packages.
67 * @param targets a list of packages (as strings) to remove from the system
69 * @return 0 on success, 1 on failure
71 int pacman_remove(alpm_list_t
*targets
)
74 alpm_list_t
*i
, *data
= NULL
;
77 pm_printf(PM_LOG_ERROR
, _("no targets specified (use -h for help)\n"));
81 /* Step 0: create a new transaction */
82 if(trans_init(config
->flags
) == -1) {
86 /* Step 1: add targets to the created transaction */
87 for(i
= targets
; i
; i
= alpm_list_next(i
)) {
88 char *target
= alpm_list_getdata(i
);
89 char *targ
= strchr(target
, '/');
90 if(targ
&& strncmp(target
, "local", 5) == 0) {
95 if(remove_target(targ
) == -1) {
101 /* Step 2: prepare the transaction based on its type, targets and flags */
102 if(alpm_trans_prepare(&data
) == -1) {
103 pm_fprintf(stderr
, PM_LOG_ERROR
, _("failed to prepare transaction (%s)\n"),
104 alpm_strerrorlast());
106 case PM_ERR_PKG_INVALID_ARCH
:
107 for(i
= data
; i
; i
= alpm_list_next(i
)) {
108 char *pkg
= alpm_list_getdata(i
);
109 printf(_(":: package %s does not have a valid architecture\n"), pkg
);
112 case PM_ERR_UNSATISFIED_DEPS
:
113 for(i
= data
; i
; i
= alpm_list_next(i
)) {
114 pmdepmissing_t
*miss
= alpm_list_getdata(i
);
115 pmdepend_t
*dep
= alpm_miss_get_dep(miss
);
116 char *depstring
= alpm_dep_compute_string(dep
);
117 printf(_(":: %s: requires %s\n"), alpm_miss_get_target(miss
),
130 /* Search for holdpkg in target list */
132 for(i
= alpm_trans_get_remove(); i
; i
= alpm_list_next(i
)) {
133 pmpkg_t
*pkg
= alpm_list_getdata(i
);
134 if(alpm_list_find_str(config
->holdpkg
, alpm_pkg_get_name(pkg
))) {
135 pm_printf(PM_LOG_WARNING
, _("%s is designated as a HoldPkg.\n"),
136 alpm_pkg_get_name(pkg
));
140 if(holdpkg
&& (noyes(_("HoldPkg was found in target list. Do you want to continue?")) == 0)) {
145 /* Step 3: actually perform the removal */
146 alpm_list_t
*pkglist
= alpm_trans_get_remove();
147 if(pkglist
== NULL
) {
148 printf(_(" there is nothing to do\n"));
149 goto cleanup
; /* we are done */
153 print_packages(pkglist
);
157 /* print targets and ask user confirmation */
158 display_targets(pkglist
, 0);
160 if(yesno(_("Do you want to remove these packages?")) == 0) {
165 if(alpm_trans_commit(NULL
) == -1) {
166 pm_fprintf(stderr
, PM_LOG_ERROR
, _("failed to commit transaction (%s)\n"),
167 alpm_strerrorlast());
171 /* Step 4: release transaction resources */
173 if(trans_release() == -1) {
179 /* vim: set ts=2 sw=2 noet: */