Improve pkghash_remove algorithm
[pacman-ng.git] / src / pacman / upgrade.c
blob595e4aabc5a7398afb45c4a7249e0a4f0f16c8e5
1 /*
2 * upgrade.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>
25 #include <string.h>
27 #include <alpm.h>
28 #include <alpm_list.h>
30 /* pacman */
31 #include "pacman.h"
32 #include "conf.h"
33 #include "util.h"
35 /**
36 * @brief Upgrade a specified list of packages.
38 * @param targets a list of packages (as strings) to upgrade
40 * @return 0 on success, 1 on failure
42 int pacman_upgrade(alpm_list_t *targets)
44 alpm_list_t *i, *data = NULL;
45 int retval = 0;
47 if(targets == NULL) {
48 pm_printf(PM_LOG_ERROR, _("no targets specified (use -h for help)\n"));
49 return(1);
52 /* Check for URL targets and process them
54 for(i = targets; i; i = alpm_list_next(i)) {
55 if(strstr(i->data, "://")) {
56 char *str = alpm_fetch_pkgurl(i->data);
57 if(str == NULL) {
58 return(1);
59 } else {
60 free(i->data);
61 i->data = str;
66 /* Step 1: create a new transaction */
67 if(trans_init(config->flags) == -1) {
68 return(1);
71 /* add targets to the created transaction */
72 for(i = targets; i; i = alpm_list_next(i)) {
73 char *targ = alpm_list_getdata(i);
74 pmpkg_t *pkg;
76 if(alpm_pkg_load(targ, 1, &pkg) != 0) {
77 pm_fprintf(stderr, PM_LOG_ERROR, "'%s': %s\n",
78 targ, alpm_strerrorlast());
79 trans_release();
80 return(1);
82 if(alpm_add_pkg(pkg) == -1) {
83 pm_fprintf(stderr, PM_LOG_ERROR, "'%s': %s\n",
84 targ, alpm_strerrorlast());
85 alpm_pkg_free(pkg);
86 trans_release();
87 return(1);
91 /* Step 2: "compute" the transaction based on targets and flags */
92 /* TODO: No, compute nothing. This is stupid. */
93 if(alpm_trans_prepare(&data) == -1) {
94 pm_fprintf(stderr, PM_LOG_ERROR, _("failed to prepare transaction (%s)\n"),
95 alpm_strerrorlast());
96 switch(pm_errno) {
97 case PM_ERR_PKG_INVALID_ARCH:
98 for(i = data; i; i = alpm_list_next(i)) {
99 char *pkg = alpm_list_getdata(i);
100 printf(_(":: package %s does not have a valid architecture\n"), pkg);
102 break;
103 case PM_ERR_UNSATISFIED_DEPS:
104 for(i = data; i; i = alpm_list_next(i)) {
105 pmdepmissing_t *miss = alpm_list_getdata(i);
106 pmdepend_t *dep = alpm_miss_get_dep(miss);
107 char *depstring = alpm_dep_compute_string(dep);
109 /* TODO indicate if the error was a virtual package or not:
110 * :: %s: requires %s, provided by %s
112 printf(_(":: %s: requires %s\n"), alpm_miss_get_target(miss),
113 depstring);
114 free(depstring);
116 break;
117 case PM_ERR_CONFLICTING_DEPS:
118 for(i = data; i; i = alpm_list_next(i)) {
119 pmconflict_t *conflict = alpm_list_getdata(i);
120 const char *package1 = alpm_conflict_get_package1(conflict);
121 const char *package2 = alpm_conflict_get_package2(conflict);
122 const char *reason = alpm_conflict_get_reason(conflict);
123 /* only print reason if it contains new information */
124 if(strcmp(package1, reason) == 0 || strcmp(package2, reason) == 0) {
125 printf(_(":: %s and %s are in conflict\n"), package1, package2);
126 } else {
127 printf(_(":: %s and %s are in conflict (%s)\n"), package1, package2, reason);
130 break;
131 default:
132 break;
134 trans_release();
135 FREELIST(data);
136 return(1);
139 /* Step 3: perform the installation */
141 if(config->print) {
142 print_packages(alpm_trans_get_add());
143 trans_release();
144 return(0);
147 /* print targets and ask user confirmation */
148 alpm_list_t *packages = alpm_trans_get_add();
149 if(packages == NULL) { /* we are done */
150 printf(_(" there is nothing to do\n"));
151 trans_release();
152 return(retval);
154 display_targets(alpm_trans_get_remove(), 0);
155 display_targets(alpm_trans_get_add(), 1);
156 printf("\n");
157 int confirm = yesno(_("Proceed with installation?"));
158 if(!confirm) {
159 trans_release();
160 return(retval);
163 if(alpm_trans_commit(&data) == -1) {
164 pm_fprintf(stderr, PM_LOG_ERROR, _("failed to commit transaction (%s)\n"),
165 alpm_strerrorlast());
166 switch(pm_errno) {
167 alpm_list_t *i;
168 case PM_ERR_FILE_CONFLICTS:
169 for(i = data; i; i = alpm_list_next(i)) {
170 pmfileconflict_t *conflict = alpm_list_getdata(i);
171 switch(alpm_fileconflict_get_type(conflict)) {
172 case PM_FILECONFLICT_TARGET:
173 printf(_("%s exists in both '%s' and '%s'\n"),
174 alpm_fileconflict_get_file(conflict),
175 alpm_fileconflict_get_target(conflict),
176 alpm_fileconflict_get_ctarget(conflict));
177 break;
178 case PM_FILECONFLICT_FILESYSTEM:
179 printf(_("%s: %s exists in filesystem\n"),
180 alpm_fileconflict_get_target(conflict),
181 alpm_fileconflict_get_file(conflict));
182 break;
185 break;
186 case PM_ERR_PKG_INVALID:
187 case PM_ERR_DLT_INVALID:
188 for(i = data; i; i = alpm_list_next(i)) {
189 char *filename = alpm_list_getdata(i);
190 printf(_("%s is invalid or corrupted\n"), filename);
192 break;
193 default:
194 break;
196 FREELIST(data);
197 trans_release();
198 return(1);
201 if(trans_release() == -1) {
202 retval = 1;
204 return(retval);
207 /* vim: set ts=2 sw=2 noet: */