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/>.
26 #include <alpm_list.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
)
41 alpm_db_t
*db_local
= alpm_get_localdb(config
->handle
);
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
)));
50 config
->explicit_removes
= alpm_list_add(config
->explicit_removes
, pkg
);
54 /* fallback to group */
55 alpm_group_t
*grp
= alpm_db_get_group(db_local
, target
);
57 pm_printf(ALPM_LOG_ERROR
, "'%s': target not found\n", target
);
60 for(p
= grp
->packages
; p
; p
= alpm_list_next(p
)) {
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
)));
67 config
->explicit_removes
= alpm_list_add(config
->explicit_removes
, pkg
);
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
)
82 alpm_list_t
*i
, *data
= NULL
;
85 pm_printf(ALPM_LOG_ERROR
, _("no targets specified (use -h for help)\n"));
89 /* Step 0: create a new transaction */
90 if(trans_init(config
->flags
, 0) == -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) {
103 if(remove_target(targ
) == -1) {
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"),
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
);
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
);
140 /* Search for holdpkg in target list */
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
));
150 if(holdpkg
&& (noyes(_("HoldPkg was found in target list. Do you want to continue?")) == 0)) {
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 */
163 print_packages(pkglist
);
167 /* print targets and ask user confirmation */
170 if(yesno(_("Do you want to remove these packages?")) == 0) {
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
)));
183 /* Step 4: release transaction resources */
185 if(trans_release() == -1) {
191 /* vim: set ts=2 sw=2 noet: */