Add information on how an installed package was validated
[pacman-ng.git] / lib / libalpm / sync.c
blob5f698008ad32080e63a3fe500ee1b38dd1da787e
1 /*
2 * sync.c
4 * Copyright (c) 2006-2012 Pacman Development Team <pacman-dev@archlinux.org>
5 * Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org>
6 * Copyright (c) 2005 by Aurelien Foret <orelien@chez.com>
7 * Copyright (c) 2005 by Christian Hamar <krics@linuxforum.hu>
8 * Copyright (c) 2005, 2006 by Miklos Vajna <vmiklos@frugalware.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include <sys/types.h> /* off_t */
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdint.h> /* intmax_t */
29 #include <unistd.h>
30 #include <limits.h>
32 /* libalpm */
33 #include "sync.h"
34 #include "alpm_list.h"
35 #include "log.h"
36 #include "package.h"
37 #include "db.h"
38 #include "deps.h"
39 #include "conflict.h"
40 #include "trans.h"
41 #include "add.h"
42 #include "util.h"
43 #include "handle.h"
44 #include "alpm.h"
45 #include "dload.h"
46 #include "delta.h"
47 #include "remove.h"
48 #include "diskspace.h"
49 #include "signing.h"
51 /** Check for new version of pkg in sync repos
52 * (only the first occurrence is considered in sync)
54 alpm_pkg_t SYMEXPORT *alpm_sync_newversion(alpm_pkg_t *pkg, alpm_list_t *dbs_sync)
56 alpm_list_t *i;
57 alpm_pkg_t *spkg = NULL;
59 ASSERT(pkg != NULL, return NULL);
60 pkg->handle->pm_errno = 0;
62 for(i = dbs_sync; !spkg && i; i = i->next) {
63 spkg = _alpm_db_get_pkgfromcache(i->data, pkg->name);
66 if(spkg == NULL) {
67 _alpm_log(pkg->handle, ALPM_LOG_DEBUG, "'%s' not found in sync db => no upgrade\n",
68 pkg->name);
69 return NULL;
72 /* compare versions and see if spkg is an upgrade */
73 if(_alpm_pkg_compare_versions(spkg, pkg) > 0) {
74 _alpm_log(pkg->handle, ALPM_LOG_DEBUG, "new version of '%s' found (%s => %s)\n",
75 pkg->name, pkg->version, spkg->version);
76 return spkg;
78 /* spkg is not an upgrade */
79 return NULL;
82 static int check_literal(alpm_handle_t *handle, alpm_pkg_t *lpkg,
83 alpm_pkg_t *spkg, int enable_downgrade)
85 /* 1. literal was found in sdb */
86 int cmp = _alpm_pkg_compare_versions(spkg, lpkg);
87 if(cmp > 0) {
88 _alpm_log(handle, ALPM_LOG_DEBUG, "new version of '%s' found (%s => %s)\n",
89 lpkg->name, lpkg->version, spkg->version);
90 /* check IgnorePkg/IgnoreGroup */
91 if(_alpm_pkg_should_ignore(handle, spkg)
92 || _alpm_pkg_should_ignore(handle, lpkg)) {
93 _alpm_log(handle, ALPM_LOG_WARNING, _("%s: ignoring package upgrade (%s => %s)\n"),
94 lpkg->name, lpkg->version, spkg->version);
95 } else {
96 _alpm_log(handle, ALPM_LOG_DEBUG, "adding package %s-%s to the transaction targets\n",
97 spkg->name, spkg->version);
98 return 1;
100 } else if(cmp < 0) {
101 if(enable_downgrade) {
102 /* check IgnorePkg/IgnoreGroup */
103 if(_alpm_pkg_should_ignore(handle, spkg)
104 || _alpm_pkg_should_ignore(handle, lpkg)) {
105 _alpm_log(handle, ALPM_LOG_WARNING, _("%s: ignoring package downgrade (%s => %s)\n"),
106 lpkg->name, lpkg->version, spkg->version);
107 } else {
108 _alpm_log(handle, ALPM_LOG_WARNING, _("%s: downgrading from version %s to version %s\n"),
109 lpkg->name, lpkg->version, spkg->version);
110 return 1;
112 } else {
113 alpm_db_t *sdb = alpm_pkg_get_db(spkg);
114 _alpm_log(handle, ALPM_LOG_WARNING, _("%s: local (%s) is newer than %s (%s)\n"),
115 lpkg->name, lpkg->version, sdb->treename, spkg->version);
118 return 0;
121 static alpm_list_t *check_replacers(alpm_handle_t *handle, alpm_pkg_t *lpkg,
122 alpm_db_t *sdb)
124 /* 2. search for replacers in sdb */
125 alpm_list_t *replacers = NULL;
126 alpm_list_t *k;
127 _alpm_log(handle, ALPM_LOG_DEBUG,
128 "searching for replacements for %s\n", lpkg->name);
129 for(k = _alpm_db_get_pkgcache(sdb); k; k = k->next) {
130 int found = 0;
131 alpm_pkg_t *spkg = k->data;
132 alpm_list_t *l;
133 for(l = alpm_pkg_get_replaces(spkg); l; l = l->next) {
134 alpm_depend_t *replace = l->data;
135 /* we only want to consider literal matches at this point. */
136 if(_alpm_depcmp_literal(lpkg, replace)) {
137 found = 1;
138 break;
141 if(found) {
142 int doreplace = 0;
143 alpm_pkg_t *tpkg;
144 /* check IgnorePkg/IgnoreGroup */
145 if(_alpm_pkg_should_ignore(handle, spkg)
146 || _alpm_pkg_should_ignore(handle, lpkg)) {
147 _alpm_log(handle, ALPM_LOG_WARNING,
148 _("ignoring package replacement (%s-%s => %s-%s)\n"),
149 lpkg->name, lpkg->version, spkg->name, spkg->version);
150 continue;
153 QUESTION(handle, ALPM_QUESTION_REPLACE_PKG, lpkg, spkg,
154 sdb->treename, &doreplace);
155 if(!doreplace) {
156 continue;
159 /* If spkg is already in the target list, we append lpkg to spkg's
160 * removes list */
161 tpkg = _alpm_pkg_find(handle->trans->add, spkg->name);
162 if(tpkg) {
163 /* sanity check, multiple repos can contain spkg->name */
164 if(tpkg->origin_data.db != sdb) {
165 _alpm_log(handle, ALPM_LOG_WARNING, _("cannot replace %s by %s\n"),
166 lpkg->name, spkg->name);
167 continue;
169 _alpm_log(handle, ALPM_LOG_DEBUG, "appending %s to the removes list of %s\n",
170 lpkg->name, tpkg->name);
171 tpkg->removes = alpm_list_add(tpkg->removes, lpkg);
172 /* check the to-be-replaced package's reason field */
173 if(alpm_pkg_get_reason(lpkg) == ALPM_PKG_REASON_EXPLICIT) {
174 tpkg->reason = ALPM_PKG_REASON_EXPLICIT;
176 } else {
177 /* add spkg to the target list */
178 /* copy over reason */
179 spkg->reason = alpm_pkg_get_reason(lpkg);
180 spkg->removes = alpm_list_add(NULL, lpkg);
181 _alpm_log(handle, ALPM_LOG_DEBUG,
182 "adding package %s-%s to the transaction targets\n",
183 spkg->name, spkg->version);
184 replacers = alpm_list_add(replacers, spkg);
188 return replacers;
191 /** Search for packages to upgrade and add them to the transaction. */
192 int SYMEXPORT alpm_sync_sysupgrade(alpm_handle_t *handle, int enable_downgrade)
194 alpm_list_t *i, *j;
195 alpm_trans_t *trans;
197 CHECK_HANDLE(handle, return -1);
198 trans = handle->trans;
199 ASSERT(trans != NULL, RET_ERR(handle, ALPM_ERR_TRANS_NULL, -1));
200 ASSERT(trans->state == STATE_INITIALIZED, RET_ERR(handle, ALPM_ERR_TRANS_NOT_INITIALIZED, -1));
202 _alpm_log(handle, ALPM_LOG_DEBUG, "checking for package upgrades\n");
203 for(i = _alpm_db_get_pkgcache(handle->db_local); i; i = i->next) {
204 alpm_pkg_t *lpkg = i->data;
206 if(_alpm_pkg_find(trans->add, lpkg->name)) {
207 _alpm_log(handle, ALPM_LOG_DEBUG, "%s is already in the target list -- skipping\n", lpkg->name);
208 continue;
211 /* Search for literal then replacers in each sync database. */
212 for(j = handle->dbs_sync; j; j = j->next) {
213 alpm_db_t *sdb = j->data;
214 /* Check sdb */
215 alpm_pkg_t *spkg = _alpm_db_get_pkgfromcache(sdb, lpkg->name);
216 int literal_upgrade = 0;
217 if(spkg) {
218 literal_upgrade = check_literal(handle, lpkg, spkg, enable_downgrade);
219 if(literal_upgrade) {
220 trans->add = alpm_list_add(trans->add, spkg);
222 /* jump to next local package */
223 break;
224 } else {
225 alpm_list_t *replacers;
226 replacers = check_replacers(handle, lpkg, sdb);
227 if(replacers) {
228 trans->add = alpm_list_join(trans->add, replacers);
234 return 0;
237 /** Find group members across a list of databases.
238 * If a member exists in several databases, only the first database is used.
239 * IgnorePkg is also handled.
240 * @param dbs the list of alpm_db_t *
241 * @param name the name of the group
242 * @return the list of alpm_pkg_t * (caller is responsible for alpm_list_free)
244 alpm_list_t SYMEXPORT *alpm_find_group_pkgs(alpm_list_t *dbs,
245 const char *name)
247 alpm_list_t *i, *j, *pkgs = NULL, *ignorelist = NULL;
249 for(i = dbs; i; i = i->next) {
250 alpm_db_t *db = i->data;
251 alpm_group_t *grp = alpm_db_get_group(db, name);
253 if(!grp)
254 continue;
256 for(j = grp->packages; j; j = j->next) {
257 alpm_pkg_t *pkg = j->data;
259 if(_alpm_pkg_find(ignorelist, pkg->name)) {
260 continue;
262 if(_alpm_pkg_should_ignore(db->handle, pkg)) {
263 ignorelist = alpm_list_add(ignorelist, pkg);
264 int install = 0;
265 QUESTION(db->handle, ALPM_QUESTION_INSTALL_IGNOREPKG, pkg,
266 NULL, NULL, &install);
267 if(!install)
268 continue;
270 if(!_alpm_pkg_find(pkgs, pkg->name)) {
271 pkgs = alpm_list_add(pkgs, pkg);
275 alpm_list_free(ignorelist);
276 return pkgs;
279 /** Compute the size of the files that will be downloaded to install a
280 * package.
281 * @param newpkg the new package to upgrade to
283 static int compute_download_size(alpm_pkg_t *newpkg)
285 const char *fname;
286 char *fpath, *fnamepart = NULL;
287 off_t size = 0;
288 alpm_handle_t *handle = newpkg->handle;
289 int ret = 0;
291 if(newpkg->origin != PKG_FROM_SYNCDB) {
292 newpkg->infolevel |= INFRQ_DSIZE;
293 newpkg->download_size = 0;
294 return 0;
297 ASSERT(newpkg->filename != NULL, RET_ERR(handle, ALPM_ERR_PKG_INVALID_NAME, -1));
298 fname = newpkg->filename;
299 fpath = _alpm_filecache_find(handle, fname);
301 /* downloaded file exists, so there's nothing to grab */
302 if(fpath) {
303 size = 0;
304 goto finish;
307 CALLOC(fnamepart, strlen(fname) + 6, sizeof(char), return -1);
308 sprintf(fnamepart, "%s.part", fname);
309 fpath = _alpm_filecache_find(handle, fnamepart);
310 if(fpath) {
311 struct stat st;
312 if(stat(fpath, &st) == 0) {
313 /* subtract the size of the .part file */
314 _alpm_log(handle, ALPM_LOG_DEBUG, "using (package - .part) size\n");
315 size = newpkg->size - st.st_size;
316 size = size < 0 ? 0 : size;
319 /* tell the caller that we have a partial */
320 ret = 1;
321 } else if(handle->deltaratio > 0.0) {
322 off_t dltsize;
324 dltsize = _alpm_shortest_delta_path(handle, newpkg->deltas,
325 newpkg->filename, &newpkg->delta_path);
327 if(newpkg->delta_path && (dltsize < newpkg->size * handle->deltaratio)) {
328 _alpm_log(handle, ALPM_LOG_DEBUG, "using delta size\n");
329 size = dltsize;
330 } else {
331 _alpm_log(handle, ALPM_LOG_DEBUG, "using package size\n");
332 size = newpkg->size;
333 alpm_list_free(newpkg->delta_path);
334 newpkg->delta_path = NULL;
336 } else {
337 size = newpkg->size;
340 finish:
341 _alpm_log(handle, ALPM_LOG_DEBUG, "setting download size %jd for pkg %s\n",
342 (intmax_t)size, newpkg->name);
344 newpkg->infolevel |= INFRQ_DSIZE;
345 newpkg->download_size = size;
347 FREE(fpath);
348 FREE(fnamepart);
350 return ret;
353 int _alpm_sync_prepare(alpm_handle_t *handle, alpm_list_t **data)
355 alpm_list_t *i, *j;
356 alpm_list_t *deps = NULL;
357 alpm_list_t *unresolvable = NULL;
358 size_t from_sync = 0;
359 int ret = 0;
360 alpm_trans_t *trans = handle->trans;
362 if(data) {
363 *data = NULL;
366 for(i = trans->add; i; i = i->next) {
367 alpm_pkg_t *spkg = i->data;
368 from_sync += (spkg->origin == PKG_FROM_SYNCDB);
371 /* ensure all sync database are valid if we will be using them */
372 for(i = handle->dbs_sync; i; i = i->next) {
373 const alpm_db_t *db = i->data;
374 if(db->status & DB_STATUS_INVALID) {
375 RET_ERR(handle, ALPM_ERR_DB_INVALID, -1);
377 /* missing databases are not allowed if we have sync targets */
378 if(from_sync && db->status & DB_STATUS_MISSING) {
379 RET_ERR(handle, ALPM_ERR_DB_NOT_FOUND, -1);
383 if(!(trans->flags & ALPM_TRANS_FLAG_NODEPS)) {
384 alpm_list_t *resolved = NULL;
385 alpm_list_t *remove = NULL;
386 alpm_list_t *localpkgs;
388 /* Build up list by repeatedly resolving each transaction package */
389 /* Resolve targets dependencies */
390 EVENT(handle, ALPM_EVENT_RESOLVEDEPS_START, NULL, NULL);
391 _alpm_log(handle, ALPM_LOG_DEBUG, "resolving target's dependencies\n");
393 /* build remove list for resolvedeps */
394 for(i = trans->add; i; i = i->next) {
395 alpm_pkg_t *spkg = i->data;
396 for(j = spkg->removes; j; j = j->next) {
397 remove = alpm_list_add(remove, j->data);
401 /* Compute the fake local database for resolvedeps (partial fix for the
402 * phonon/qt issue) */
403 localpkgs = alpm_list_diff(_alpm_db_get_pkgcache(handle->db_local),
404 trans->add, _alpm_pkg_cmp);
406 /* Resolve packages in the transaction one at a time, in addition
407 building up a list of packages which could not be resolved. */
408 for(i = trans->add; i; i = i->next) {
409 alpm_pkg_t *pkg = i->data;
410 if(_alpm_resolvedeps(handle, localpkgs, pkg, trans->add,
411 &resolved, remove, data) == -1) {
412 unresolvable = alpm_list_add(unresolvable, pkg);
414 /* Else, [resolved] now additionally contains [pkg] and all of its
415 dependencies not already on the list */
417 alpm_list_free(localpkgs);
418 alpm_list_free(remove);
420 /* If there were unresolvable top-level packages, prompt the user to
421 see if they'd like to ignore them rather than failing the sync */
422 if(unresolvable != NULL) {
423 int remove_unresolvable = 0;
424 alpm_errno_t saved_err = handle->pm_errno;
425 QUESTION(handle, ALPM_QUESTION_REMOVE_PKGS, unresolvable,
426 NULL, NULL, &remove_unresolvable);
427 if(remove_unresolvable) {
428 /* User wants to remove the unresolvable packages from the
429 transaction. The packages will be removed from the actual
430 transaction when the transaction packages are replaced with a
431 dependency-reordered list below */
432 handle->pm_errno = 0;
433 if(data) {
434 alpm_list_free_inner(*data, (alpm_list_fn_free)_alpm_depmiss_free);
435 alpm_list_free(*data);
436 *data = NULL;
438 } else {
439 /* pm_errno was set by resolvedeps, callback may have overwrote it */
440 handle->pm_errno = saved_err;
441 alpm_list_free(resolved);
442 ret = -1;
443 goto cleanup;
447 /* Set DEPEND reason for pulled packages */
448 for(i = resolved; i; i = i->next) {
449 alpm_pkg_t *pkg = i->data;
450 if(!_alpm_pkg_find(trans->add, pkg->name)) {
451 pkg->reason = ALPM_PKG_REASON_DEPEND;
455 /* Unresolvable packages will be removed from the target list; set these
456 * aside in the transaction as a list we won't operate on. If we free them
457 * before the end of the transaction, we may kill pointers the frontend
458 * holds to package objects. */
459 trans->unresolvable = unresolvable;
461 /* re-order w.r.t. dependencies */
462 alpm_list_free(trans->add);
463 trans->add = _alpm_sortbydeps(handle, resolved, 0);
464 alpm_list_free(resolved);
466 EVENT(handle, ALPM_EVENT_RESOLVEDEPS_DONE, NULL, NULL);
469 if(!(trans->flags & ALPM_TRANS_FLAG_NOCONFLICTS)) {
470 /* check for inter-conflicts and whatnot */
471 EVENT(handle, ALPM_EVENT_INTERCONFLICTS_START, NULL, NULL);
473 _alpm_log(handle, ALPM_LOG_DEBUG, "looking for conflicts\n");
475 /* 1. check for conflicts in the target list */
476 _alpm_log(handle, ALPM_LOG_DEBUG, "check targets vs targets\n");
477 deps = _alpm_innerconflicts(handle, trans->add);
479 for(i = deps; i; i = i->next) {
480 alpm_conflict_t *conflict = i->data;
481 alpm_pkg_t *rsync, *sync, *sync1, *sync2;
483 /* have we already removed one of the conflicting targets? */
484 sync1 = _alpm_pkg_find(trans->add, conflict->package1);
485 sync2 = _alpm_pkg_find(trans->add, conflict->package2);
486 if(!sync1 || !sync2) {
487 continue;
490 _alpm_log(handle, ALPM_LOG_DEBUG, "conflicting packages in the sync list: '%s' <-> '%s'\n",
491 conflict->package1, conflict->package2);
493 /* if sync1 provides sync2, we remove sync2 from the targets, and vice versa */
494 alpm_depend_t *dep1 = _alpm_splitdep(conflict->package1);
495 alpm_depend_t *dep2 = _alpm_splitdep(conflict->package2);
496 if(_alpm_depcmp(sync1, dep2)) {
497 rsync = sync2;
498 sync = sync1;
499 } else if(_alpm_depcmp(sync2, dep1)) {
500 rsync = sync1;
501 sync = sync2;
502 } else {
503 _alpm_log(handle, ALPM_LOG_ERROR, _("unresolvable package conflicts detected\n"));
504 handle->pm_errno = ALPM_ERR_CONFLICTING_DEPS;
505 ret = -1;
506 if(data) {
507 alpm_conflict_t *newconflict = _alpm_conflict_dup(conflict);
508 if(newconflict) {
509 *data = alpm_list_add(*data, newconflict);
512 alpm_list_free_inner(deps, (alpm_list_fn_free)_alpm_conflict_free);
513 alpm_list_free(deps);
514 _alpm_dep_free(dep1);
515 _alpm_dep_free(dep2);
516 goto cleanup;
518 _alpm_dep_free(dep1);
519 _alpm_dep_free(dep2);
521 /* Prints warning */
522 _alpm_log(handle, ALPM_LOG_WARNING,
523 _("removing '%s' from target list because it conflicts with '%s'\n"),
524 rsync->name, sync->name);
525 trans->add = alpm_list_remove(trans->add, rsync, _alpm_pkg_cmp, NULL);
526 /* rsync is not a transaction target anymore */
527 trans->unresolvable = alpm_list_add(trans->unresolvable, rsync);
528 continue;
531 alpm_list_free_inner(deps, (alpm_list_fn_free)_alpm_conflict_free);
532 alpm_list_free(deps);
533 deps = NULL;
535 /* 2. we check for target vs db conflicts (and resolve)*/
536 _alpm_log(handle, ALPM_LOG_DEBUG, "check targets vs db and db vs targets\n");
537 deps = _alpm_outerconflicts(handle->db_local, trans->add);
539 for(i = deps; i; i = i->next) {
540 alpm_conflict_t *conflict = i->data;
542 /* if conflict->package2 (the local package) is not elected for removal,
543 we ask the user */
544 int found = 0;
545 for(j = trans->add; j && !found; j = j->next) {
546 alpm_pkg_t *spkg = j->data;
547 if(_alpm_pkg_find(spkg->removes, conflict->package2)) {
548 found = 1;
551 if(found) {
552 continue;
555 _alpm_log(handle, ALPM_LOG_DEBUG, "package '%s' conflicts with '%s'\n",
556 conflict->package1, conflict->package2);
558 alpm_pkg_t *sync = _alpm_pkg_find(trans->add, conflict->package1);
559 alpm_pkg_t *local = _alpm_db_get_pkgfromcache(handle->db_local, conflict->package2);
560 int doremove = 0;
561 QUESTION(handle, ALPM_QUESTION_CONFLICT_PKG, conflict->package1,
562 conflict->package2, conflict->reason->name, &doremove);
563 if(doremove) {
564 /* append to the removes list */
565 _alpm_log(handle, ALPM_LOG_DEBUG, "electing '%s' for removal\n", conflict->package2);
566 sync->removes = alpm_list_add(sync->removes, local);
567 } else { /* abort */
568 _alpm_log(handle, ALPM_LOG_ERROR, _("unresolvable package conflicts detected\n"));
569 handle->pm_errno = ALPM_ERR_CONFLICTING_DEPS;
570 ret = -1;
571 if(data) {
572 alpm_conflict_t *newconflict = _alpm_conflict_dup(conflict);
573 if(newconflict) {
574 *data = alpm_list_add(*data, newconflict);
577 alpm_list_free_inner(deps, (alpm_list_fn_free)_alpm_conflict_free);
578 alpm_list_free(deps);
579 goto cleanup;
582 EVENT(handle, ALPM_EVENT_INTERCONFLICTS_DONE, NULL, NULL);
583 alpm_list_free_inner(deps, (alpm_list_fn_free)_alpm_conflict_free);
584 alpm_list_free(deps);
587 /* Build trans->remove list */
588 for(i = trans->add; i; i = i->next) {
589 alpm_pkg_t *spkg = i->data;
590 for(j = spkg->removes; j; j = j->next) {
591 alpm_pkg_t *rpkg = j->data;
592 if(!_alpm_pkg_find(trans->remove, rpkg->name)) {
593 alpm_pkg_t *copy;
594 _alpm_log(handle, ALPM_LOG_DEBUG, "adding '%s' to remove list\n", rpkg->name);
595 if(_alpm_pkg_dup(rpkg, &copy) == -1) {
596 return -1;
598 trans->remove = alpm_list_add(trans->remove, copy);
603 if(!(trans->flags & ALPM_TRANS_FLAG_NODEPS)) {
604 _alpm_log(handle, ALPM_LOG_DEBUG, "checking dependencies\n");
605 deps = alpm_checkdeps(handle, _alpm_db_get_pkgcache(handle->db_local),
606 trans->remove, trans->add, 1);
607 if(deps) {
608 handle->pm_errno = ALPM_ERR_UNSATISFIED_DEPS;
609 ret = -1;
610 if(data) {
611 *data = deps;
612 } else {
613 alpm_list_free_inner(deps, (alpm_list_fn_free)_alpm_depmiss_free);
614 alpm_list_free(deps);
616 goto cleanup;
619 for(i = trans->add; i; i = i->next) {
620 /* update download size field */
621 alpm_pkg_t *spkg = i->data;
622 if(compute_download_size(spkg) < 0) {
623 ret = -1;
624 goto cleanup;
628 cleanup:
629 return ret;
632 /** Returns the size of the files that will be downloaded to install a
633 * package.
634 * @param newpkg the new package to upgrade to
635 * @return the size of the download
637 off_t SYMEXPORT alpm_pkg_download_size(alpm_pkg_t *newpkg)
639 if(!(newpkg->infolevel & INFRQ_DSIZE)) {
640 compute_download_size(newpkg);
642 return newpkg->download_size;
645 static int endswith(const char *filename, const char *extension)
647 const char *s = filename + strlen(filename) - strlen(extension);
648 return strcmp(s, extension) == 0;
651 /** Applies delta files to create an upgraded package file.
653 * All intermediate files are deleted, leaving only the starting and
654 * ending package files.
656 * @param handle the context handle
658 * @return 0 if all delta files were able to be applied, 1 otherwise.
660 static int apply_deltas(alpm_handle_t *handle)
662 alpm_list_t *i;
663 int deltas_found = 0, ret = 0;
664 const char *cachedir = _alpm_filecache_setup(handle);
665 alpm_trans_t *trans = handle->trans;
667 for(i = trans->add; i; i = i->next) {
668 alpm_pkg_t *spkg = i->data;
669 alpm_list_t *delta_path = spkg->delta_path;
670 alpm_list_t *dlts = NULL;
672 if(!delta_path) {
673 continue;
676 if(!deltas_found) {
677 /* only show this if we actually have deltas to apply, and it is before
678 * the very first one */
679 EVENT(handle, ALPM_EVENT_DELTA_PATCHES_START, NULL, NULL);
680 deltas_found = 1;
683 for(dlts = delta_path; dlts; dlts = dlts->next) {
684 alpm_delta_t *d = dlts->data;
685 char *delta, *from, *to;
686 char command[PATH_MAX];
687 size_t len = 0;
689 delta = _alpm_filecache_find(handle, d->delta);
690 /* the initial package might be in a different cachedir */
691 if(dlts == delta_path) {
692 from = _alpm_filecache_find(handle, d->from);
693 } else {
694 /* len = cachedir len + from len + '/' + null */
695 len = strlen(cachedir) + strlen(d->from) + 2;
696 MALLOC(from, len, RET_ERR(handle, ALPM_ERR_MEMORY, 1));
697 snprintf(from, len, "%s/%s", cachedir, d->from);
699 len = strlen(cachedir) + strlen(d->to) + 2;
700 MALLOC(to, len, RET_ERR(handle, ALPM_ERR_MEMORY, 1));
701 snprintf(to, len, "%s/%s", cachedir, d->to);
703 /* build the patch command */
704 if(endswith(to, ".gz")) {
705 /* special handling for gzip : we disable timestamp with -n option */
706 snprintf(command, PATH_MAX, "xdelta3 -d -q -R -c -s %s %s | gzip -n > %s", from, delta, to);
707 } else {
708 snprintf(command, PATH_MAX, "xdelta3 -d -q -s %s %s %s", from, delta, to);
711 _alpm_log(handle, ALPM_LOG_DEBUG, "command: %s\n", command);
713 EVENT(handle, ALPM_EVENT_DELTA_PATCH_START, d->to, d->delta);
715 int retval = system(command);
716 if(retval == 0) {
717 EVENT(handle, ALPM_EVENT_DELTA_PATCH_DONE, NULL, NULL);
719 /* delete the delta file */
720 unlink(delta);
722 /* Delete the 'from' package but only if it is an intermediate
723 * package. The starting 'from' package should be kept, just
724 * as if deltas were not used. */
725 if(dlts != delta_path) {
726 unlink(from);
729 FREE(from);
730 FREE(to);
731 FREE(delta);
733 if(retval != 0) {
734 /* one delta failed for this package, cancel the remaining ones */
735 EVENT(handle, ALPM_EVENT_DELTA_PATCH_FAILED, NULL, NULL);
736 handle->pm_errno = ALPM_ERR_DLT_PATCHFAILED;
737 ret = 1;
738 break;
742 if(deltas_found) {
743 EVENT(handle, ALPM_EVENT_DELTA_PATCHES_DONE, NULL, NULL);
746 return ret;
750 * Prompts to delete the file now that we know it is invalid.
751 * @param handle the context handle
752 * @param filename the absolute path of the file to test
753 * @param reason an error code indicating the reason for package invalidity
755 * @return 1 if file was removed, 0 otherwise
757 static int prompt_to_delete(alpm_handle_t *handle, const char *filepath,
758 alpm_errno_t reason)
760 int doremove = 0;
761 QUESTION(handle, ALPM_QUESTION_CORRUPTED_PKG, (char *)filepath,
762 &reason, NULL, &doremove);
763 if(doremove) {
764 unlink(filepath);
766 return doremove;
769 static int validate_deltas(alpm_handle_t *handle, alpm_list_t *deltas)
771 alpm_list_t *i, *errors = NULL;
773 if(!deltas) {
774 return 0;
777 /* Check integrity of deltas */
778 EVENT(handle, ALPM_EVENT_DELTA_INTEGRITY_START, NULL, NULL);
779 for(i = deltas; i; i = i->next) {
780 alpm_delta_t *d = i->data;
781 char *filepath = _alpm_filecache_find(handle, d->delta);
783 if(_alpm_test_checksum(filepath, d->delta_md5, ALPM_CSUM_MD5)) {
784 errors = alpm_list_add(errors, filepath);
785 } else {
786 FREE(filepath);
789 EVENT(handle, ALPM_EVENT_DELTA_INTEGRITY_DONE, NULL, NULL);
791 if(errors) {
792 for(i = errors; i; i = i->next) {
793 char *filepath = i->data;
794 prompt_to_delete(handle, filepath, ALPM_ERR_DLT_INVALID);
795 FREE(filepath);
797 alpm_list_free(errors);
798 handle->pm_errno = ALPM_ERR_DLT_INVALID;
799 return -1;
801 return 0;
804 static struct dload_payload *build_payload(alpm_handle_t *handle,
805 const char *filename, size_t size, alpm_list_t *servers)
807 struct dload_payload *payload;
809 CALLOC(payload, 1, sizeof(*payload), RET_ERR(handle, ALPM_ERR_MEMORY, NULL));
810 STRDUP(payload->remote_name, filename, RET_ERR(handle, ALPM_ERR_MEMORY, NULL));
811 payload->max_size = size;
812 payload->servers = servers;
813 return payload;
816 static int find_dl_candidates(alpm_db_t *repo, alpm_list_t **files, alpm_list_t **deltas)
818 alpm_list_t *i;
819 alpm_handle_t *handle = repo->handle;
821 for(i = handle->trans->add; i; i = i->next) {
822 alpm_pkg_t *spkg = i->data;
824 if(spkg->origin != PKG_FROM_FILE && repo == spkg->origin_data.db) {
825 alpm_list_t *delta_path = spkg->delta_path;
827 if(!repo->servers) {
828 handle->pm_errno = ALPM_ERR_SERVER_NONE;
829 _alpm_log(handle, ALPM_LOG_ERROR, "%s: %s\n",
830 alpm_strerror(handle->pm_errno), repo->treename);
831 return 1;
834 if(delta_path) {
835 /* using deltas */
836 alpm_list_t *dlts;
837 for(dlts = delta_path; dlts; dlts = dlts->next) {
838 alpm_delta_t *delta = dlts->data;
839 if(delta->download_size != 0) {
840 struct dload_payload *payload = build_payload(
841 handle, delta->delta, delta->delta_size, repo->servers);
842 ASSERT(payload, return -1);
843 *files = alpm_list_add(*files, payload);
845 /* keep a list of all the delta files for md5sums */
846 *deltas = alpm_list_add(*deltas, delta);
849 } else if(spkg->download_size != 0) {
850 struct dload_payload *payload;
851 ASSERT(spkg->filename != NULL, RET_ERR(handle, ALPM_ERR_PKG_INVALID_NAME, -1));
852 payload = build_payload(handle, spkg->filename, spkg->size, repo->servers);
853 ASSERT(payload, return -1);
854 *files = alpm_list_add(*files, payload);
859 return 0;
862 static int download_single_file(alpm_handle_t *handle, struct dload_payload *payload,
863 const char *cachedir)
865 const alpm_list_t *server;
867 for(server = payload->servers; server; server = server->next) {
868 const char *server_url = server->data;
869 size_t len;
871 /* print server + filename into a buffer */
872 len = strlen(server_url) + strlen(payload->remote_name) + 2;
873 MALLOC(payload->fileurl, len, RET_ERR(handle, ALPM_ERR_MEMORY, -1));
874 snprintf(payload->fileurl, len, "%s/%s", server_url, payload->remote_name);
875 payload->handle = handle;
876 payload->allow_resume = 1;
878 if(_alpm_download(payload, cachedir, NULL) != -1) {
879 return 0;
883 return -1;
886 static int download_files(alpm_handle_t *handle, alpm_list_t **deltas)
888 const char *cachedir;
889 alpm_list_t *i, *files = NULL;
890 int errors = 0;
892 cachedir = _alpm_filecache_setup(handle);
893 handle->trans->state = STATE_DOWNLOADING;
895 /* Total progress - figure out the total download size if required to
896 * pass to the callback. This function is called once, and it is up to the
897 * frontend to compute incremental progress. */
898 if(handle->totaldlcb) {
899 off_t total_size = (off_t)0;
900 /* sum up the download size for each package and store total */
901 for(i = handle->trans->add; i; i = i->next) {
902 alpm_pkg_t *spkg = i->data;
903 total_size += spkg->download_size;
905 handle->totaldlcb(total_size);
908 for(i = handle->dbs_sync; i; i = i->next) {
909 errors += find_dl_candidates(i->data, &files, deltas);
912 if(files) {
913 /* check for necessary disk space for download */
914 if(handle->checkspace) {
915 off_t *file_sizes;
916 size_t idx, num_files;
917 int ret;
919 _alpm_log(handle, ALPM_LOG_DEBUG, "checking available disk space for download\n");
921 num_files = alpm_list_count(files);
922 CALLOC(file_sizes, num_files, sizeof(off_t), goto finish);
924 for(i = files, idx = 0; i; i = i->next, idx++) {
925 const struct dload_payload *payload = i->data;
926 file_sizes[idx] = payload->max_size;
929 ret = _alpm_check_downloadspace(handle, cachedir, num_files, file_sizes);
930 free(file_sizes);
932 if(ret != 0) {
933 errors++;
934 goto finish;
938 EVENT(handle, ALPM_EVENT_RETRIEVE_START, NULL, NULL);
939 for(i = files; i; i = i->next) {
940 if(download_single_file(handle, i->data, cachedir) == -1) {
941 errors++;
942 _alpm_log(handle, ALPM_LOG_WARNING, _("failed to retrieve some files\n"));
947 finish:
948 if(files) {
949 alpm_list_free_inner(files, (alpm_list_fn_free)_alpm_dload_payload_reset);
950 FREELIST(files);
953 for(i = handle->trans->add; i; i = i->next) {
954 alpm_pkg_t *pkg = i->data;
955 pkg->infolevel &= ~INFRQ_DSIZE;
956 pkg->download_size = 0;
959 /* clear out value to let callback know we are done */
960 if(handle->totaldlcb) {
961 handle->totaldlcb(0);
964 return errors;
967 static int check_validity(alpm_handle_t *handle,
968 size_t total, size_t total_bytes)
970 struct validity {
971 alpm_pkg_t *pkg;
972 char *path;
973 alpm_siglist_t *siglist;
974 alpm_siglevel_t level;
975 alpm_pkgvalidation_t validation;
976 alpm_errno_t error;
978 size_t current = 0, current_bytes = 0;
979 alpm_list_t *i, *errors = NULL;
981 /* Check integrity of packages */
982 EVENT(handle, ALPM_EVENT_INTEGRITY_START, NULL, NULL);
984 for(i = handle->trans->add; i; i = i->next, current++) {
985 struct validity v = { i->data, NULL, NULL, 0, 0, 0 };
986 int percent = (int)(((double)current_bytes / total_bytes) * 100);
988 PROGRESS(handle, ALPM_PROGRESS_INTEGRITY_START, "", percent,
989 total, current);
990 if(v.pkg->origin == PKG_FROM_FILE) {
991 continue; /* pkg_load() has been already called, this package is valid */
994 current_bytes += v.pkg->size;
995 v.path = _alpm_filecache_find(handle, v.pkg->filename);
996 v.level = alpm_db_get_siglevel(alpm_pkg_get_db(v.pkg));
998 if(_alpm_pkg_validate_internal(handle, v.path, v.pkg,
999 v.level, &v.siglist, &v.validation) == -1) {
1000 v.error = handle->pm_errno;
1001 struct validity *invalid = malloc(sizeof(struct validity));
1002 memcpy(invalid, &v, sizeof(struct validity));
1003 errors = alpm_list_add(errors, invalid);
1004 } else {
1005 alpm_siglist_cleanup(v.siglist);
1006 free(v.siglist);
1007 free(v.path);
1008 v.pkg->validation = v.validation;
1012 PROGRESS(handle, ALPM_PROGRESS_INTEGRITY_START, "", 100,
1013 total, current);
1014 EVENT(handle, ALPM_EVENT_INTEGRITY_DONE, NULL, NULL);
1016 if(errors) {
1017 int tryagain = 0;
1018 for(i = errors; i; i = i->next) {
1019 struct validity *v = i->data;
1020 if(v->error == ALPM_ERR_PKG_INVALID_SIG) {
1021 int retry = _alpm_process_siglist(handle, v->pkg->name, v->siglist,
1022 v->level & ALPM_SIG_PACKAGE_OPTIONAL,
1023 v->level & ALPM_SIG_PACKAGE_MARGINAL_OK,
1024 v->level & ALPM_SIG_PACKAGE_UNKNOWN_OK);
1025 tryagain += retry;
1026 } else if(v->error == ALPM_ERR_PKG_INVALID_CHECKSUM) {
1027 prompt_to_delete(handle, v->path, v->error);
1029 alpm_siglist_cleanup(v->siglist);
1030 free(v->siglist);
1031 free(v->path);
1032 free(v);
1034 alpm_list_free(errors);
1036 if(tryagain == 0) {
1037 if(!handle->pm_errno) {
1038 RET_ERR(handle, ALPM_ERR_PKG_INVALID, -1);
1040 return -1;
1042 /* we were told at least once we can try again */
1043 return 1;
1046 return 0;
1049 static int load_packages(alpm_handle_t *handle, alpm_list_t **data,
1050 size_t total, size_t total_bytes)
1052 size_t current = 0, current_bytes = 0;
1053 int errors = 0;
1054 alpm_list_t *i;
1056 /* load packages from disk now that they are known-valid */
1057 EVENT(handle, ALPM_EVENT_LOAD_START, NULL, NULL);
1059 for(i = handle->trans->add; i; i = i->next, current++) {
1060 alpm_pkg_t *spkg = i->data;
1061 char *filepath;
1062 int percent = (int)(((double)current_bytes / total_bytes) * 100);
1064 PROGRESS(handle, ALPM_PROGRESS_LOAD_START, "", percent,
1065 total, current);
1066 if(spkg->origin == PKG_FROM_FILE) {
1067 continue; /* pkg_load() has been already called, this package is valid */
1070 current_bytes += spkg->size;
1071 filepath = _alpm_filecache_find(handle, spkg->filename);
1073 /* load the package file and replace pkgcache entry with it in the target list */
1074 /* TODO: alpm_pkg_get_db() will not work on this target anymore */
1075 _alpm_log(handle, ALPM_LOG_DEBUG,
1076 "replacing pkgcache entry with package file for target %s\n",
1077 spkg->name);
1078 alpm_pkg_t *pkgfile =_alpm_pkg_load_internal(handle, filepath, 1);
1079 if(!pkgfile) {
1080 errors++;
1081 *data = alpm_list_add(*data, strdup(spkg->filename));
1082 free(filepath);
1083 continue;
1085 free(filepath);
1086 /* copy over the install reason */
1087 pkgfile->reason = spkg->reason;
1088 /* copy over validation method */
1089 pkgfile->validation = spkg->validation;
1090 i->data = pkgfile;
1091 /* spkg has been removed from the target list, so we can free the
1092 * sync-specific fields */
1093 _alpm_pkg_free_trans(spkg);
1096 PROGRESS(handle, ALPM_PROGRESS_LOAD_START, "", 100,
1097 total, current);
1098 EVENT(handle, ALPM_EVENT_LOAD_DONE, NULL, NULL);
1100 if(errors) {
1101 if(!handle->pm_errno) {
1102 RET_ERR(handle, ALPM_ERR_PKG_INVALID, -1);
1104 return -1;
1107 return 0;
1110 int _alpm_sync_commit(alpm_handle_t *handle, alpm_list_t **data)
1112 alpm_list_t *i, *deltas = NULL;
1113 size_t total = 0, total_bytes = 0;
1114 alpm_trans_t *trans = handle->trans;
1116 if(download_files(handle, &deltas)) {
1117 alpm_list_free(deltas);
1118 return -1;
1121 if(validate_deltas(handle, deltas)) {
1122 alpm_list_free(deltas);
1123 return -1;
1125 alpm_list_free(deltas);
1127 /* Use the deltas to generate the packages */
1128 if(apply_deltas(handle)) {
1129 return -1;
1132 /* get the total size of all packages so we can adjust the progress bar more
1133 * realistically if there are small and huge packages involved */
1134 for(i = trans->add; i; i = i->next) {
1135 alpm_pkg_t *spkg = i->data;
1136 if(spkg->origin != PKG_FROM_FILE) {
1137 total_bytes += spkg->size;
1139 total++;
1141 /* this can only happen maliciously */
1142 total_bytes = total_bytes ? total_bytes : 1;
1144 /* this one is special: -1 is failure, 1 is retry, 0 is success */
1145 while(1) {
1146 int ret = check_validity(handle, total, total_bytes);
1147 if(ret == 0) {
1148 break;
1149 } else if(ret < 0) {
1150 return -1;
1154 if(trans->flags & ALPM_TRANS_FLAG_DOWNLOADONLY) {
1155 return 0;
1158 if(load_packages(handle, data, total, total_bytes)) {
1159 return -1;
1162 trans->state = STATE_COMMITING;
1164 /* fileconflict check */
1165 if(!(trans->flags & (ALPM_TRANS_FLAG_FORCE|ALPM_TRANS_FLAG_DBONLY))) {
1166 EVENT(handle, ALPM_EVENT_FILECONFLICTS_START, NULL, NULL);
1168 _alpm_log(handle, ALPM_LOG_DEBUG, "looking for file conflicts\n");
1169 alpm_list_t *conflict = _alpm_db_find_fileconflicts(handle,
1170 trans->add, trans->remove);
1171 if(conflict) {
1172 if(data) {
1173 *data = conflict;
1174 } else {
1175 alpm_list_free_inner(conflict, (alpm_list_fn_free)_alpm_fileconflict_free);
1176 alpm_list_free(conflict);
1178 RET_ERR(handle, ALPM_ERR_FILE_CONFLICTS, -1);
1181 EVENT(handle, ALPM_EVENT_FILECONFLICTS_DONE, NULL, NULL);
1184 /* check available disk space */
1185 if(handle->checkspace && !(trans->flags & ALPM_TRANS_FLAG_DBONLY)) {
1186 EVENT(handle, ALPM_EVENT_DISKSPACE_START, NULL, NULL);
1188 _alpm_log(handle, ALPM_LOG_DEBUG, "checking available disk space\n");
1189 if(_alpm_check_diskspace(handle) == -1) {
1190 _alpm_log(handle, ALPM_LOG_ERROR, "%s\n", _("not enough free disk space"));
1191 return -1;
1194 EVENT(handle, ALPM_EVENT_DISKSPACE_DONE, NULL, NULL);
1197 /* remove conflicting and to-be-replaced packages */
1198 if(trans->remove) {
1199 _alpm_log(handle, ALPM_LOG_DEBUG, "removing conflicting and to-be-replaced packages\n");
1200 /* we want the frontend to be aware of commit details */
1201 if(_alpm_remove_packages(handle, 0) == -1) {
1202 _alpm_log(handle, ALPM_LOG_ERROR, _("could not commit removal transaction\n"));
1203 return -1;
1207 /* install targets */
1208 _alpm_log(handle, ALPM_LOG_DEBUG, "installing packages\n");
1209 if(_alpm_upgrade_packages(handle) == -1) {
1210 _alpm_log(handle, ALPM_LOG_ERROR, _("could not commit transaction\n"));
1211 return -1;
1214 return 0;
1217 /* vim: set ts=2 sw=2 noet: */