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(const char *target
)
37 alpm_db_t
*db_local
= alpm_option_get_localdb(config
->handle
);
40 if((info
= alpm_db_get_pkg(db_local
, target
)) != NULL
) {
41 if(alpm_remove_pkg(config
->handle
, info
) == -1) {
42 pm_fprintf(stderr
, ALPM_LOG_ERROR
, "'%s': %s\n", target
,
43 alpm_strerror(alpm_errno(config
->handle
)));
49 /* fallback to group */
50 alpm_group_t
*grp
= alpm_db_readgroup(db_local
, target
);
52 pm_fprintf(stderr
, ALPM_LOG_ERROR
, "'%s': target not found\n", target
);
55 for(p
= grp
->packages
; p
; p
= alpm_list_next(p
)) {
56 alpm_pkg_t
*pkg
= alpm_list_getdata(p
);
57 if(alpm_remove_pkg(config
->handle
, pkg
) == -1) {
58 pm_fprintf(stderr
, ALPM_LOG_ERROR
, "'%s': %s\n", target
,
59 alpm_strerror(alpm_errno(config
->handle
)));
67 * @brief Remove a specified list of packages.
69 * @param targets a list of packages (as strings) to remove from the system
71 * @return 0 on success, 1 on failure
73 int pacman_remove(alpm_list_t
*targets
)
76 alpm_list_t
*i
, *data
= NULL
;
79 pm_printf(ALPM_LOG_ERROR
, _("no targets specified (use -h for help)\n"));
83 /* Step 0: create a new transaction */
84 if(trans_init(config
->flags
, 0) == -1) {
88 /* Step 1: add targets to the created transaction */
89 for(i
= targets
; i
; i
= alpm_list_next(i
)) {
90 char *target
= alpm_list_getdata(i
);
91 char *targ
= strchr(target
, '/');
92 if(targ
&& strncmp(target
, "local", 5) == 0) {
97 if(remove_target(targ
) == -1) {
103 /* Step 2: prepare the transaction based on its type, targets and flags */
104 if(alpm_trans_prepare(config
->handle
, &data
) == -1) {
105 enum _alpm_errno_t err
= alpm_errno(config
->handle
);
106 pm_fprintf(stderr
, ALPM_LOG_ERROR
, _("failed to prepare transaction (%s)\n"),
109 case ALPM_ERR_PKG_INVALID_ARCH
:
110 for(i
= data
; i
; i
= alpm_list_next(i
)) {
111 char *pkg
= alpm_list_getdata(i
);
112 printf(_(":: package %s does not have a valid architecture\n"), pkg
);
115 case ALPM_ERR_UNSATISFIED_DEPS
:
116 for(i
= data
; i
; i
= alpm_list_next(i
)) {
117 alpm_depmissing_t
*miss
= alpm_list_getdata(i
);
118 char *depstring
= alpm_dep_compute_string(miss
->depend
);
119 printf(_(":: %s: requires %s\n"), miss
->target
, depstring
);
131 /* Search for holdpkg in target list */
133 for(i
= alpm_trans_get_remove(config
->handle
); i
; i
= alpm_list_next(i
)) {
134 alpm_pkg_t
*pkg
= alpm_list_getdata(i
);
135 if(alpm_list_find_str(config
->holdpkg
, alpm_pkg_get_name(pkg
))) {
136 pm_printf(ALPM_LOG_WARNING
, _("%s is designated as a HoldPkg.\n"),
137 alpm_pkg_get_name(pkg
));
141 if(holdpkg
&& (noyes(_("HoldPkg was found in target list. Do you want to continue?")) == 0)) {
146 /* Step 3: actually perform the removal */
147 alpm_list_t
*pkglist
= alpm_trans_get_remove(config
->handle
);
148 if(pkglist
== NULL
) {
149 printf(_(" there is nothing to do\n"));
150 goto cleanup
; /* we are done */
154 print_packages(pkglist
);
158 /* print targets and ask user confirmation */
159 display_targets(pkglist
, 0);
161 if(yesno(_("Do you want to remove these packages?")) == 0) {
166 if(alpm_trans_commit(config
->handle
, &data
) == -1) {
167 pm_fprintf(stderr
, ALPM_LOG_ERROR
, _("failed to commit transaction (%s)\n"),
168 alpm_strerror(alpm_errno(config
->handle
)));
174 /* Step 4: release transaction resources */
176 if(trans_release() == -1) {
182 /* vim: set ts=2 sw=2 noet: */