Improve pkghash_remove algorithm
[pacman-ng.git] / src / pacman / remove.c
blobfb02e242d82627a44bcf52a7e5948de671221df6
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 "config.h"
23 #include <stdlib.h>
24 #include <stdio.h>
26 #include <alpm.h>
27 #include <alpm_list.h>
29 /* pacman */
30 #include "pacman.h"
31 #include "util.h"
32 #include "conf.h"
34 static int remove_target(char *target)
36 pmpkg_t *info;
37 pmdb_t *db_local = alpm_option_get_localdb();
38 alpm_list_t *p;
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());
43 return(-1);
45 return(0);
48 /* fallback to group */
49 pmgrp_t *grp = alpm_db_readgrp(db_local, target);
50 if(grp == NULL) {
51 pm_fprintf(stderr, PM_LOG_ERROR, "'%s': target not found\n", target);
52 return(-1);
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());
58 return(-1);
61 return(0);
64 /**
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)
73 int retval = 0;
74 alpm_list_t *i, *data = NULL;
76 if(targets == NULL) {
77 pm_printf(PM_LOG_ERROR, _("no targets specified (use -h for help)\n"));
78 return(1);
81 /* Step 0: create a new transaction */
82 if(trans_init(config->flags) == -1) {
83 return(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) {
91 targ++;
92 } else {
93 targ = target;
95 if(remove_target(targ) == -1) {
96 retval = 1;
97 goto cleanup;
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());
105 switch(pm_errno) {
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);
111 break;
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),
118 depstring);
119 free(depstring);
121 FREELIST(data);
122 break;
123 default:
124 break;
126 retval = 1;
127 goto cleanup;
130 /* Search for holdpkg in target list */
131 int holdpkg = 0;
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));
137 holdpkg = 1;
140 if(holdpkg && (noyes(_("HoldPkg was found in target list. Do you want to continue?")) == 0)) {
141 retval = 1;
142 goto cleanup;
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 */
152 if(config->print) {
153 print_packages(pkglist);
154 goto cleanup;
157 /* print targets and ask user confirmation */
158 display_targets(pkglist, 0);
159 printf("\n");
160 if(yesno(_("Do you want to remove these packages?")) == 0) {
161 retval = 1;
162 goto cleanup;
165 if(alpm_trans_commit(NULL) == -1) {
166 pm_fprintf(stderr, PM_LOG_ERROR, _("failed to commit transaction (%s)\n"),
167 alpm_strerrorlast());
168 retval = 1;
171 /* Step 4: release transaction resources */
172 cleanup:
173 if(trans_release() == -1) {
174 retval = 1;
176 return(retval);
179 /* vim: set ts=2 sw=2 noet: */