Rename pmdb_t to alpm_db_t
[pacman-ng.git] / src / pacman / remove.c
blobf30f5068a22da36eb941a9de585b8e35c9d4d342
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(const char *target)
36 pmpkg_t *info;
37 alpm_db_t *db_local = alpm_option_get_localdb(config->handle);
38 alpm_list_t *p;
40 if((info = alpm_db_get_pkg(db_local, target)) != NULL) {
41 if(alpm_remove_pkg(config->handle, info) == -1) {
42 pm_fprintf(stderr, PM_LOG_ERROR, "'%s': %s\n", target,
43 alpm_strerror(alpm_errno(config->handle)));
44 return -1;
46 return 0;
49 /* fallback to group */
50 pmgrp_t *grp = alpm_db_readgrp(db_local, target);
51 if(grp == NULL) {
52 pm_fprintf(stderr, PM_LOG_ERROR, "'%s': target not found\n", target);
53 return -1;
55 for(p = grp->packages; p; p = alpm_list_next(p)) {
56 pmpkg_t *pkg = alpm_list_getdata(p);
57 if(alpm_remove_pkg(config->handle, pkg) == -1) {
58 pm_fprintf(stderr, PM_LOG_ERROR, "'%s': %s\n", target,
59 alpm_strerror(alpm_errno(config->handle)));
60 return -1;
63 return 0;
66 /**
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)
75 int retval = 0;
76 alpm_list_t *i, *data = NULL;
78 if(targets == NULL) {
79 pm_printf(PM_LOG_ERROR, _("no targets specified (use -h for help)\n"));
80 return 1;
83 /* Step 0: create a new transaction */
84 if(trans_init(config->flags) == -1) {
85 return 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) {
93 targ++;
94 } else {
95 targ = target;
97 if(remove_target(targ) == -1) {
98 retval = 1;
99 goto cleanup;
103 /* Step 2: prepare the transaction based on its type, targets and flags */
104 if(alpm_trans_prepare(config->handle, &data) == -1) {
105 enum _pmerrno_t err = alpm_errno(config->handle);
106 pm_fprintf(stderr, PM_LOG_ERROR, _("failed to prepare transaction (%s)\n"),
107 alpm_strerror(err));
108 switch(err) {
109 case PM_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);
114 break;
115 case PM_ERR_UNSATISFIED_DEPS:
116 for(i = data; i; i = alpm_list_next(i)) {
117 pmdepmissing_t *miss = alpm_list_getdata(i);
118 char *depstring = alpm_dep_compute_string(miss->depend);
119 printf(_(":: %s: requires %s\n"), miss->target, depstring);
120 free(depstring);
122 break;
123 default:
124 break;
126 FREELIST(data);
127 retval = 1;
128 goto cleanup;
131 /* Search for holdpkg in target list */
132 int holdpkg = 0;
133 for(i = alpm_trans_get_remove(config->handle); i; i = alpm_list_next(i)) {
134 pmpkg_t *pkg = alpm_list_getdata(i);
135 if(alpm_list_find_str(config->holdpkg, alpm_pkg_get_name(pkg))) {
136 pm_printf(PM_LOG_WARNING, _("%s is designated as a HoldPkg.\n"),
137 alpm_pkg_get_name(pkg));
138 holdpkg = 1;
141 if(holdpkg && (noyes(_("HoldPkg was found in target list. Do you want to continue?")) == 0)) {
142 retval = 1;
143 goto cleanup;
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 */
153 if(config->print) {
154 print_packages(pkglist);
155 goto cleanup;
158 /* print targets and ask user confirmation */
159 display_targets(pkglist, 0);
160 printf("\n");
161 if(yesno(_("Do you want to remove these packages?")) == 0) {
162 retval = 1;
163 goto cleanup;
166 if(alpm_trans_commit(config->handle, &data) == -1) {
167 pm_fprintf(stderr, PM_LOG_ERROR, _("failed to commit transaction (%s)\n"),
168 alpm_strerror(alpm_errno(config->handle)));
169 retval = 1;
172 FREELIST(data);
174 /* Step 4: release transaction resources */
175 cleanup:
176 if(trans_release() == -1) {
177 retval = 1;
179 return retval;
182 /* vim: set ts=2 sw=2 noet: */