lib/sync: use full delta size as max download size
[pacman-ng.git] / lib / libalpm / sync.c
bloba946a7df16774e3d8533d9bd7f86b98e991f98a2
1 /*
2 * sync.c
4 * Copyright (c) 2006-2011 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 "config.h"
26 #include <sys/types.h> /* off_t */
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdint.h> /* intmax_t */
31 #include <unistd.h>
32 #include <limits.h>
34 /* libalpm */
35 #include "sync.h"
36 #include "alpm_list.h"
37 #include "log.h"
38 #include "package.h"
39 #include "db.h"
40 #include "deps.h"
41 #include "conflict.h"
42 #include "trans.h"
43 #include "add.h"
44 #include "util.h"
45 #include "handle.h"
46 #include "alpm.h"
47 #include "dload.h"
48 #include "delta.h"
49 #include "remove.h"
50 #include "diskspace.h"
51 #include "signing.h"
53 /** Check for new version of pkg in sync repos
54 * (only the first occurrence is considered in sync)
56 alpm_pkg_t SYMEXPORT *alpm_sync_newversion(alpm_pkg_t *pkg, alpm_list_t *dbs_sync)
58 alpm_list_t *i;
59 alpm_pkg_t *spkg = NULL;
61 ASSERT(pkg != NULL, return NULL);
62 pkg->handle->pm_errno = 0;
64 for(i = dbs_sync; !spkg && i; i = i->next) {
65 spkg = _alpm_db_get_pkgfromcache(i->data, pkg->name);
68 if(spkg == NULL) {
69 _alpm_log(pkg->handle, ALPM_LOG_DEBUG, "'%s' not found in sync db => no upgrade\n",
70 pkg->name);
71 return NULL;
74 /* compare versions and see if spkg is an upgrade */
75 if(_alpm_pkg_compare_versions(spkg, pkg) > 0) {
76 _alpm_log(pkg->handle, ALPM_LOG_DEBUG, "new version of '%s' found (%s => %s)\n",
77 pkg->name, pkg->version, spkg->version);
78 return spkg;
80 /* spkg is not an upgrade */
81 return NULL;
84 static int check_literal(alpm_handle_t *handle, alpm_pkg_t *lpkg,
85 alpm_pkg_t *spkg, int enable_downgrade)
87 /* 1. literal was found in sdb */
88 int cmp = _alpm_pkg_compare_versions(spkg, lpkg);
89 if(cmp > 0) {
90 _alpm_log(handle, ALPM_LOG_DEBUG, "new version of '%s' found (%s => %s)\n",
91 lpkg->name, lpkg->version, spkg->version);
92 /* check IgnorePkg/IgnoreGroup */
93 if(_alpm_pkg_should_ignore(handle, spkg)
94 || _alpm_pkg_should_ignore(handle, lpkg)) {
95 _alpm_log(handle, ALPM_LOG_WARNING, _("%s: ignoring package upgrade (%s => %s)\n"),
96 lpkg->name, lpkg->version, spkg->version);
97 } else {
98 _alpm_log(handle, ALPM_LOG_DEBUG, "adding package %s-%s to the transaction targets\n",
99 spkg->name, spkg->version);
100 return 1;
102 } else if(cmp < 0) {
103 if(enable_downgrade) {
104 /* check IgnorePkg/IgnoreGroup */
105 if(_alpm_pkg_should_ignore(handle, spkg)
106 || _alpm_pkg_should_ignore(handle, lpkg)) {
107 _alpm_log(handle, ALPM_LOG_WARNING, _("%s: ignoring package downgrade (%s => %s)\n"),
108 lpkg->name, lpkg->version, spkg->version);
109 } else {
110 _alpm_log(handle, ALPM_LOG_WARNING, _("%s: downgrading from version %s to version %s\n"),
111 lpkg->name, lpkg->version, spkg->version);
112 return 1;
114 } else {
115 alpm_db_t *sdb = alpm_pkg_get_db(spkg);
116 _alpm_log(handle, ALPM_LOG_WARNING, _("%s: local (%s) is newer than %s (%s)\n"),
117 lpkg->name, lpkg->version, sdb->treename, spkg->version);
120 return 0;
123 static alpm_list_t *check_replacers(alpm_handle_t *handle, alpm_pkg_t *lpkg,
124 alpm_db_t *sdb)
126 /* 2. search for replacers in sdb */
127 alpm_list_t *replacers = NULL;
128 alpm_list_t *k;
129 _alpm_log(handle, ALPM_LOG_DEBUG,
130 "searching for replacements for %s\n", lpkg->name);
131 for(k = _alpm_db_get_pkgcache(sdb); k; k = k->next) {
132 int found = 0;
133 alpm_pkg_t *spkg = k->data;
134 alpm_list_t *l;
135 for(l = alpm_pkg_get_replaces(spkg); l; l = l->next) {
136 alpm_depend_t *replace = l->data;
137 /* we only want to consider literal matches at this point. */
138 if(_alpm_depcmp_literal(lpkg, replace)) {
139 found = 1;
140 break;
143 if(found) {
144 int doreplace = 0;
145 alpm_pkg_t *tpkg;
146 /* check IgnorePkg/IgnoreGroup */
147 if(_alpm_pkg_should_ignore(handle, spkg)
148 || _alpm_pkg_should_ignore(handle, lpkg)) {
149 _alpm_log(handle, ALPM_LOG_WARNING,
150 _("ignoring package replacement (%s-%s => %s-%s)\n"),
151 lpkg->name, lpkg->version, spkg->name, spkg->version);
152 continue;
155 QUESTION(handle, ALPM_QUESTION_REPLACE_PKG, lpkg, spkg,
156 sdb->treename, &doreplace);
157 if(!doreplace) {
158 continue;
161 /* If spkg is already in the target list, we append lpkg to spkg's
162 * removes list */
163 tpkg = _alpm_pkg_find(handle->trans->add, spkg->name);
164 if(tpkg) {
165 /* sanity check, multiple repos can contain spkg->name */
166 if(tpkg->origin_data.db != sdb) {
167 _alpm_log(handle, ALPM_LOG_WARNING, _("cannot replace %s by %s\n"),
168 lpkg->name, spkg->name);
169 continue;
171 _alpm_log(handle, ALPM_LOG_DEBUG, "appending %s to the removes list of %s\n",
172 lpkg->name, tpkg->name);
173 tpkg->removes = alpm_list_add(tpkg->removes, lpkg);
174 /* check the to-be-replaced package's reason field */
175 if(alpm_pkg_get_reason(lpkg) == ALPM_PKG_REASON_EXPLICIT) {
176 tpkg->reason = ALPM_PKG_REASON_EXPLICIT;
178 } else {
179 /* add spkg to the target list */
180 /* copy over reason */
181 spkg->reason = alpm_pkg_get_reason(lpkg);
182 spkg->removes = alpm_list_add(NULL, lpkg);
183 _alpm_log(handle, ALPM_LOG_DEBUG,
184 "adding package %s-%s to the transaction targets\n",
185 spkg->name, spkg->version);
186 replacers = alpm_list_add(replacers, spkg);
190 return replacers;
193 /** Search for packages to upgrade and add them to the transaction. */
194 int SYMEXPORT alpm_sync_sysupgrade(alpm_handle_t *handle, int enable_downgrade)
196 alpm_list_t *i, *j;
197 alpm_trans_t *trans;
199 CHECK_HANDLE(handle, return -1);
200 trans = handle->trans;
201 ASSERT(trans != NULL, RET_ERR(handle, ALPM_ERR_TRANS_NULL, -1));
202 ASSERT(trans->state == STATE_INITIALIZED, RET_ERR(handle, ALPM_ERR_TRANS_NOT_INITIALIZED, -1));
204 _alpm_log(handle, ALPM_LOG_DEBUG, "checking for package upgrades\n");
205 for(i = _alpm_db_get_pkgcache(handle->db_local); i; i = i->next) {
206 alpm_pkg_t *lpkg = i->data;
208 if(_alpm_pkg_find(trans->add, lpkg->name)) {
209 _alpm_log(handle, ALPM_LOG_DEBUG, "%s is already in the target list -- skipping\n", lpkg->name);
210 continue;
213 /* Search for literal then replacers in each sync database. */
214 for(j = handle->dbs_sync; j; j = j->next) {
215 alpm_db_t *sdb = j->data;
216 /* Check sdb */
217 alpm_pkg_t *spkg = _alpm_db_get_pkgfromcache(sdb, lpkg->name);
218 int literal_upgrade = 0;
219 if(spkg) {
220 literal_upgrade = check_literal(handle, lpkg, spkg, enable_downgrade);
221 if(literal_upgrade) {
222 trans->add = alpm_list_add(trans->add, spkg);
224 /* jump to next local package */
225 break;
226 } else {
227 alpm_list_t *replacers;
228 replacers = check_replacers(handle, lpkg, sdb);
229 if(replacers) {
230 trans->add = alpm_list_join(trans->add, replacers);
236 return 0;
239 /** Find group members across a list of databases.
240 * If a member exists in several databases, only the first database is used.
241 * IgnorePkg is also handled.
242 * @param dbs the list of alpm_db_t *
243 * @param name the name of the group
244 * @return the list of alpm_pkg_t * (caller is responsible for alpm_list_free)
246 alpm_list_t SYMEXPORT *alpm_find_group_pkgs(alpm_list_t *dbs,
247 const char *name)
249 alpm_list_t *i, *j, *pkgs = NULL, *ignorelist = NULL;
251 for(i = dbs; i; i = i->next) {
252 alpm_db_t *db = i->data;
253 alpm_group_t *grp = alpm_db_readgroup(db, name);
255 if(!grp)
256 continue;
258 for(j = grp->packages; j; j = j->next) {
259 alpm_pkg_t *pkg = j->data;
261 if(_alpm_pkg_find(ignorelist, pkg->name)) {
262 continue;
264 if(_alpm_pkg_should_ignore(db->handle, pkg)) {
265 ignorelist = alpm_list_add(ignorelist, pkg);
266 int install = 0;
267 QUESTION(db->handle, ALPM_QUESTION_INSTALL_IGNOREPKG, pkg,
268 NULL, NULL, &install);
269 if(!install)
270 continue;
272 if(!_alpm_pkg_find(pkgs, pkg->name)) {
273 pkgs = alpm_list_add(pkgs, pkg);
277 alpm_list_free(ignorelist);
278 return pkgs;
281 /** Compute the size of the files that will be downloaded to install a
282 * package.
283 * @param newpkg the new package to upgrade to
285 static int compute_download_size(alpm_pkg_t *newpkg)
287 const char *fname;
288 char *fpath, *fnamepart = NULL;
289 off_t size = 0;
290 alpm_handle_t *handle = newpkg->handle;
291 int ret = 0;
293 if(newpkg->origin != PKG_FROM_SYNCDB) {
294 newpkg->infolevel |= INFRQ_DSIZE;
295 newpkg->download_size = 0;
296 return 0;
299 ASSERT(newpkg->filename != NULL, RET_ERR(handle, ALPM_ERR_PKG_INVALID_NAME, -1));
300 fname = newpkg->filename;
301 fpath = _alpm_filecache_find(handle, fname);
303 /* downloaded file exists, so there's nothing to grab */
304 if(fpath) {
305 size = 0;
306 goto finish;
309 CALLOC(fnamepart, strlen(fname) + 6, sizeof(char), return -1);
310 sprintf(fnamepart, "%s.part", fname);
311 fpath = _alpm_filecache_find(handle, fnamepart);
312 if(fpath) {
313 struct stat st;
314 if(stat(fpath, &st) == 0) {
315 /* subtract the size of the .part file */
316 _alpm_log(handle, ALPM_LOG_DEBUG, "using (package - .part) size\n");
317 size = newpkg->size - st.st_size;
318 size = size < 0 ? 0 : size;
321 /* tell the caller that we have a partial */
322 ret = 1;
323 } else if(handle->usedelta) {
324 off_t dltsize;
326 dltsize = _alpm_shortest_delta_path(handle, newpkg->deltas,
327 newpkg->filename, &newpkg->delta_path);
329 if(newpkg->delta_path && (dltsize < newpkg->size * MAX_DELTA_RATIO)) {
330 _alpm_log(handle, ALPM_LOG_DEBUG, "using delta size\n");
331 size = dltsize;
332 } else {
333 _alpm_log(handle, ALPM_LOG_DEBUG, "using package size\n");
334 size = newpkg->size;
335 alpm_list_free(newpkg->delta_path);
336 newpkg->delta_path = NULL;
338 } else {
339 size = newpkg->size;
342 finish:
343 _alpm_log(handle, ALPM_LOG_DEBUG, "setting download size %jd for pkg %s\n",
344 (intmax_t)size, newpkg->name);
346 newpkg->infolevel |= INFRQ_DSIZE;
347 newpkg->download_size = size;
349 FREE(fpath);
350 FREE(fnamepart);
352 return ret;
355 int _alpm_sync_prepare(alpm_handle_t *handle, alpm_list_t **data)
357 alpm_list_t *i, *j;
358 alpm_list_t *deps = NULL;
359 alpm_list_t *unresolvable = NULL;
360 size_t from_sync = 0;
361 int ret = 0;
362 alpm_trans_t *trans = handle->trans;
364 if(data) {
365 *data = NULL;
368 for(i = trans->add; i; i = i->next) {
369 alpm_pkg_t *spkg = i->data;
370 from_sync += (spkg->origin == PKG_FROM_SYNCDB);
373 /* ensure all sync database are valid if we will be using them */
374 for(i = handle->dbs_sync; i; i = i->next) {
375 const alpm_db_t *db = i->data;
376 if(db->status & DB_STATUS_INVALID) {
377 RET_ERR(handle, ALPM_ERR_DB_INVALID, -1);
379 /* missing databases are not allowed if we have sync targets */
380 if(from_sync && db->status & DB_STATUS_MISSING) {
381 RET_ERR(handle, ALPM_ERR_DB_NOT_FOUND, -1);
385 if(!(trans->flags & ALPM_TRANS_FLAG_NODEPS)) {
386 alpm_list_t *resolved = NULL;
387 alpm_list_t *remove = NULL;
388 alpm_list_t *localpkgs;
390 /* Build up list by repeatedly resolving each transaction package */
391 /* Resolve targets dependencies */
392 EVENT(handle, ALPM_EVENT_RESOLVEDEPS_START, NULL, NULL);
393 _alpm_log(handle, ALPM_LOG_DEBUG, "resolving target's dependencies\n");
395 /* build remove list for resolvedeps */
396 for(i = trans->add; i; i = i->next) {
397 alpm_pkg_t *spkg = i->data;
398 for(j = spkg->removes; j; j = j->next) {
399 remove = alpm_list_add(remove, j->data);
403 /* Compute the fake local database for resolvedeps (partial fix for the
404 * phonon/qt issue) */
405 localpkgs = alpm_list_diff(_alpm_db_get_pkgcache(handle->db_local),
406 trans->add, _alpm_pkg_cmp);
408 /* Resolve packages in the transaction one at a time, in addition
409 building up a list of packages which could not be resolved. */
410 for(i = trans->add; i; i = i->next) {
411 alpm_pkg_t *pkg = i->data;
412 if(_alpm_resolvedeps(handle, localpkgs, pkg, trans->add,
413 &resolved, remove, data) == -1) {
414 unresolvable = alpm_list_add(unresolvable, pkg);
416 /* Else, [resolved] now additionally contains [pkg] and all of its
417 dependencies not already on the list */
419 alpm_list_free(localpkgs);
420 alpm_list_free(remove);
422 /* If there were unresolvable top-level packages, prompt the user to
423 see if they'd like to ignore them rather than failing the sync */
424 if(unresolvable != NULL) {
425 int remove_unresolvable = 0;
426 enum _alpm_errno_t saved_err = handle->pm_errno;
427 QUESTION(handle, ALPM_QUESTION_REMOVE_PKGS, unresolvable,
428 NULL, NULL, &remove_unresolvable);
429 if(remove_unresolvable) {
430 /* User wants to remove the unresolvable packages from the
431 transaction. The packages will be removed from the actual
432 transaction when the transaction packages are replaced with a
433 dependency-reordered list below */
434 handle->pm_errno = 0;
435 if(data) {
436 alpm_list_free_inner(*data, (alpm_list_fn_free)_alpm_depmiss_free);
437 alpm_list_free(*data);
438 *data = NULL;
440 } else {
441 /* pm_errno was set by resolvedeps, callback may have overwrote it */
442 handle->pm_errno = saved_err;
443 alpm_list_free(resolved);
444 ret = -1;
445 goto cleanup;
449 /* Set DEPEND reason for pulled packages */
450 for(i = resolved; i; i = i->next) {
451 alpm_pkg_t *pkg = i->data;
452 if(!_alpm_pkg_find(trans->add, pkg->name)) {
453 pkg->reason = ALPM_PKG_REASON_DEPEND;
457 /* Unresolvable packages will be removed from the target list; set these
458 * aside in the transaction as a list we won't operate on. If we free them
459 * before the end of the transaction, we may kill pointers the frontend
460 * holds to package objects. */
461 trans->unresolvable = unresolvable;
463 /* re-order w.r.t. dependencies */
464 alpm_list_free(trans->add);
465 trans->add = _alpm_sortbydeps(handle, resolved, 0);
466 alpm_list_free(resolved);
468 EVENT(handle, ALPM_EVENT_RESOLVEDEPS_DONE, NULL, NULL);
471 if(!(trans->flags & ALPM_TRANS_FLAG_NOCONFLICTS)) {
472 /* check for inter-conflicts and whatnot */
473 EVENT(handle, ALPM_EVENT_INTERCONFLICTS_START, NULL, NULL);
475 _alpm_log(handle, ALPM_LOG_DEBUG, "looking for conflicts\n");
477 /* 1. check for conflicts in the target list */
478 _alpm_log(handle, ALPM_LOG_DEBUG, "check targets vs targets\n");
479 deps = _alpm_innerconflicts(handle, trans->add);
481 for(i = deps; i; i = i->next) {
482 alpm_conflict_t *conflict = i->data;
483 alpm_pkg_t *rsync, *sync, *sync1, *sync2;
485 /* have we already removed one of the conflicting targets? */
486 sync1 = _alpm_pkg_find(trans->add, conflict->package1);
487 sync2 = _alpm_pkg_find(trans->add, conflict->package2);
488 if(!sync1 || !sync2) {
489 continue;
492 _alpm_log(handle, ALPM_LOG_DEBUG, "conflicting packages in the sync list: '%s' <-> '%s'\n",
493 conflict->package1, conflict->package2);
495 /* if sync1 provides sync2, we remove sync2 from the targets, and vice versa */
496 alpm_depend_t *dep1 = _alpm_splitdep(conflict->package1);
497 alpm_depend_t *dep2 = _alpm_splitdep(conflict->package2);
498 if(_alpm_depcmp(sync1, dep2)) {
499 rsync = sync2;
500 sync = sync1;
501 } else if(_alpm_depcmp(sync2, dep1)) {
502 rsync = sync1;
503 sync = sync2;
504 } else {
505 _alpm_log(handle, ALPM_LOG_ERROR, _("unresolvable package conflicts detected\n"));
506 handle->pm_errno = ALPM_ERR_CONFLICTING_DEPS;
507 ret = -1;
508 if(data) {
509 alpm_conflict_t *newconflict = _alpm_conflict_dup(conflict);
510 if(newconflict) {
511 *data = alpm_list_add(*data, newconflict);
514 alpm_list_free_inner(deps, (alpm_list_fn_free)_alpm_conflict_free);
515 alpm_list_free(deps);
516 _alpm_dep_free(dep1);
517 _alpm_dep_free(dep2);
518 goto cleanup;
520 _alpm_dep_free(dep1);
521 _alpm_dep_free(dep2);
523 /* Prints warning */
524 _alpm_log(handle, ALPM_LOG_WARNING,
525 _("removing '%s' from target list because it conflicts with '%s'\n"),
526 rsync->name, sync->name);
527 trans->add = alpm_list_remove(trans->add, rsync, _alpm_pkg_cmp, NULL);
528 /* rsync is not a transaction target anymore */
529 trans->unresolvable = alpm_list_add(trans->unresolvable, rsync);
530 continue;
533 alpm_list_free_inner(deps, (alpm_list_fn_free)_alpm_conflict_free);
534 alpm_list_free(deps);
535 deps = NULL;
537 /* 2. we check for target vs db conflicts (and resolve)*/
538 _alpm_log(handle, ALPM_LOG_DEBUG, "check targets vs db and db vs targets\n");
539 deps = _alpm_outerconflicts(handle->db_local, trans->add);
541 for(i = deps; i; i = i->next) {
542 alpm_conflict_t *conflict = i->data;
544 /* if conflict->package2 (the local package) is not elected for removal,
545 we ask the user */
546 int found = 0;
547 for(j = trans->add; j && !found; j = j->next) {
548 alpm_pkg_t *spkg = j->data;
549 if(_alpm_pkg_find(spkg->removes, conflict->package2)) {
550 found = 1;
553 if(found) {
554 continue;
557 _alpm_log(handle, ALPM_LOG_DEBUG, "package '%s' conflicts with '%s'\n",
558 conflict->package1, conflict->package2);
560 alpm_pkg_t *sync = _alpm_pkg_find(trans->add, conflict->package1);
561 alpm_pkg_t *local = _alpm_db_get_pkgfromcache(handle->db_local, conflict->package2);
562 int doremove = 0;
563 QUESTION(handle, ALPM_QUESTION_CONFLICT_PKG, conflict->package1,
564 conflict->package2, conflict->reason->name, &doremove);
565 if(doremove) {
566 /* append to the removes list */
567 _alpm_log(handle, ALPM_LOG_DEBUG, "electing '%s' for removal\n", conflict->package2);
568 sync->removes = alpm_list_add(sync->removes, local);
569 } else { /* abort */
570 _alpm_log(handle, ALPM_LOG_ERROR, _("unresolvable package conflicts detected\n"));
571 handle->pm_errno = ALPM_ERR_CONFLICTING_DEPS;
572 ret = -1;
573 if(data) {
574 alpm_conflict_t *newconflict = _alpm_conflict_dup(conflict);
575 if(newconflict) {
576 *data = alpm_list_add(*data, newconflict);
579 alpm_list_free_inner(deps, (alpm_list_fn_free)_alpm_conflict_free);
580 alpm_list_free(deps);
581 goto cleanup;
584 EVENT(handle, ALPM_EVENT_INTERCONFLICTS_DONE, NULL, NULL);
585 alpm_list_free_inner(deps, (alpm_list_fn_free)_alpm_conflict_free);
586 alpm_list_free(deps);
589 /* Build trans->remove list */
590 for(i = trans->add; i; i = i->next) {
591 alpm_pkg_t *spkg = i->data;
592 for(j = spkg->removes; j; j = j->next) {
593 alpm_pkg_t *rpkg = j->data;
594 if(!_alpm_pkg_find(trans->remove, rpkg->name)) {
595 alpm_pkg_t *copy;
596 _alpm_log(handle, ALPM_LOG_DEBUG, "adding '%s' to remove list\n", rpkg->name);
597 if(_alpm_pkg_dup(rpkg, &copy) == -1) {
598 return -1;
600 trans->remove = alpm_list_add(trans->remove, copy);
605 if(!(trans->flags & ALPM_TRANS_FLAG_NODEPS)) {
606 _alpm_log(handle, ALPM_LOG_DEBUG, "checking dependencies\n");
607 deps = alpm_checkdeps(handle, _alpm_db_get_pkgcache(handle->db_local),
608 trans->remove, trans->add, 1);
609 if(deps) {
610 handle->pm_errno = ALPM_ERR_UNSATISFIED_DEPS;
611 ret = -1;
612 if(data) {
613 *data = deps;
614 } else {
615 alpm_list_free_inner(deps, (alpm_list_fn_free)_alpm_depmiss_free);
616 alpm_list_free(deps);
618 goto cleanup;
621 for(i = trans->add; i; i = i->next) {
622 /* update download size field */
623 alpm_pkg_t *spkg = i->data;
624 if(compute_download_size(spkg) < 0) {
625 ret = -1;
626 goto cleanup;
630 cleanup:
631 return ret;
634 /** Returns the size of the files that will be downloaded to install a
635 * package.
636 * @param newpkg the new package to upgrade to
637 * @return the size of the download
639 off_t SYMEXPORT alpm_pkg_download_size(alpm_pkg_t *newpkg)
641 if(!(newpkg->infolevel & INFRQ_DSIZE)) {
642 compute_download_size(newpkg);
644 return newpkg->download_size;
647 static int endswith(const char *filename, const char *extension)
649 const char *s = filename + strlen(filename) - strlen(extension);
650 return strcmp(s, extension) == 0;
653 /** Applies delta files to create an upgraded package file.
655 * All intermediate files are deleted, leaving only the starting and
656 * ending package files.
658 * @param handle the context handle
660 * @return 0 if all delta files were able to be applied, 1 otherwise.
662 static int apply_deltas(alpm_handle_t *handle)
664 alpm_list_t *i;
665 int deltas_found = 0, ret = 0;
666 const char *cachedir = _alpm_filecache_setup(handle);
667 alpm_trans_t *trans = handle->trans;
669 for(i = trans->add; i; i = i->next) {
670 alpm_pkg_t *spkg = i->data;
671 alpm_list_t *delta_path = spkg->delta_path;
672 alpm_list_t *dlts = NULL;
674 if(!delta_path) {
675 continue;
678 if(!deltas_found) {
679 /* only show this if we actually have deltas to apply, and it is before
680 * the very first one */
681 EVENT(handle, ALPM_EVENT_DELTA_PATCHES_START, NULL, NULL);
682 deltas_found = 1;
685 for(dlts = delta_path; dlts; dlts = dlts->next) {
686 alpm_delta_t *d = dlts->data;
687 char *delta, *from, *to;
688 char command[PATH_MAX];
689 size_t len = 0;
691 delta = _alpm_filecache_find(handle, d->delta);
692 /* the initial package might be in a different cachedir */
693 if(dlts == delta_path) {
694 from = _alpm_filecache_find(handle, d->from);
695 } else {
696 /* len = cachedir len + from len + '/' + null */
697 len = strlen(cachedir) + strlen(d->from) + 2;
698 CALLOC(from, len, sizeof(char), RET_ERR(handle, ALPM_ERR_MEMORY, 1));
699 snprintf(from, len, "%s/%s", cachedir, d->from);
701 len = strlen(cachedir) + strlen(d->to) + 2;
702 CALLOC(to, len, sizeof(char), RET_ERR(handle, ALPM_ERR_MEMORY, 1));
703 snprintf(to, len, "%s/%s", cachedir, d->to);
705 /* build the patch command */
706 if(endswith(to, ".gz")) {
707 /* special handling for gzip : we disable timestamp with -n option */
708 snprintf(command, PATH_MAX, "xdelta3 -d -q -R -c -s %s %s | gzip -n > %s", from, delta, to);
709 } else {
710 snprintf(command, PATH_MAX, "xdelta3 -d -q -s %s %s %s", from, delta, to);
713 _alpm_log(handle, ALPM_LOG_DEBUG, "command: %s\n", command);
715 EVENT(handle, ALPM_EVENT_DELTA_PATCH_START, d->to, d->delta);
717 int retval = system(command);
718 if(retval == 0) {
719 EVENT(handle, ALPM_EVENT_DELTA_PATCH_DONE, NULL, NULL);
721 /* delete the delta file */
722 unlink(delta);
724 /* Delete the 'from' package but only if it is an intermediate
725 * package. The starting 'from' package should be kept, just
726 * as if deltas were not used. */
727 if(dlts != delta_path) {
728 unlink(from);
731 FREE(from);
732 FREE(to);
733 FREE(delta);
735 if(retval != 0) {
736 /* one delta failed for this package, cancel the remaining ones */
737 EVENT(handle, ALPM_EVENT_DELTA_PATCH_FAILED, NULL, NULL);
738 handle->pm_errno = ALPM_ERR_DLT_PATCHFAILED;
739 ret = 1;
740 break;
744 if(deltas_found) {
745 EVENT(handle, ALPM_EVENT_DELTA_PATCHES_DONE, NULL, NULL);
748 return ret;
752 * Prompts to delete the file now that we know it is invalid.
753 * @param handle the context handle
754 * @param filename the absolute path of the file to test
755 * @param reason an error code indicating the reason for package invalidity
757 * @return 1 if file was removed, 0 otherwise
759 static int prompt_to_delete(alpm_handle_t *handle, const char *filepath,
760 enum _alpm_errno_t reason)
762 int doremove = 0;
763 QUESTION(handle, ALPM_QUESTION_CORRUPTED_PKG, (char *)filepath,
764 &reason, NULL, &doremove);
765 if(doremove) {
766 unlink(filepath);
768 return doremove;
771 static int validate_deltas(alpm_handle_t *handle, alpm_list_t *deltas)
773 alpm_list_t *i, *errors = NULL;
775 if(!deltas) {
776 return 0;
779 /* Check integrity of deltas */
780 EVENT(handle, ALPM_EVENT_DELTA_INTEGRITY_START, NULL, NULL);
781 for(i = deltas; i; i = i->next) {
782 alpm_delta_t *d = i->data;
783 char *filepath = _alpm_filecache_find(handle, d->delta);
785 if(_alpm_test_checksum(filepath, d->delta_md5, ALPM_CSUM_MD5)) {
786 errors = alpm_list_add(errors, filepath);
787 } else {
788 FREE(filepath);
791 EVENT(handle, ALPM_EVENT_DELTA_INTEGRITY_DONE, NULL, NULL);
793 if(errors) {
794 for(i = errors; i; i = i->next) {
795 char *filepath = i->data;
796 prompt_to_delete(handle, filepath, ALPM_ERR_DLT_INVALID);
797 FREE(filepath);
799 alpm_list_free(errors);
800 handle->pm_errno = ALPM_ERR_DLT_INVALID;
801 return -1;
803 return 0;
806 static int download_files(alpm_handle_t *handle, alpm_list_t **deltas)
808 const char *cachedir;
809 alpm_list_t *i, *j;
810 alpm_list_t *files = NULL;
811 int errors = 0;
813 cachedir = _alpm_filecache_setup(handle);
814 handle->trans->state = STATE_DOWNLOADING;
816 /* Total progress - figure out the total download size if required to
817 * pass to the callback. This function is called once, and it is up to the
818 * frontend to compute incremental progress. */
819 if(handle->totaldlcb) {
820 off_t total_size = (off_t)0;
821 /* sum up the download size for each package and store total */
822 for(i = handle->trans->add; i; i = i->next) {
823 alpm_pkg_t *spkg = i->data;
824 total_size += spkg->download_size;
826 handle->totaldlcb(total_size);
829 /* group sync records by repository and download */
830 for(i = handle->dbs_sync; i; i = i->next) {
831 alpm_db_t *current = i->data;
833 for(j = handle->trans->add; j; j = j->next) {
834 alpm_pkg_t *spkg = j->data;
836 if(spkg->origin != PKG_FROM_FILE && current == spkg->origin_data.db) {
837 alpm_list_t *delta_path = spkg->delta_path;
838 if(delta_path) {
839 /* using deltas */
840 alpm_list_t *dlts;
841 for(dlts = delta_path; dlts; dlts = dlts->next) {
842 alpm_delta_t *delta = dlts->data;
843 if(delta->download_size != 0) {
844 struct dload_payload *dpayload;
846 CALLOC(dpayload, 1, sizeof(*dpayload), RET_ERR(handle, ALPM_ERR_MEMORY, -1));
847 STRDUP(dpayload->remote_name, delta->delta, RET_ERR(handle, ALPM_ERR_MEMORY, -1));
848 dpayload->max_size = delta->delta_size;
850 files = alpm_list_add(files, dpayload);
852 /* keep a list of all the delta files for md5sums */
853 *deltas = alpm_list_add(*deltas, delta);
856 } else if(spkg->download_size != 0) {
857 struct dload_payload *payload;
859 ASSERT(spkg->filename != NULL, RET_ERR(handle, ALPM_ERR_PKG_INVALID_NAME, -1));
860 CALLOC(payload, 1, sizeof(*payload), RET_ERR(handle, ALPM_ERR_MEMORY, -1));
861 STRDUP(payload->remote_name, spkg->filename, RET_ERR(handle, ALPM_ERR_MEMORY, -1));
862 payload->max_size = spkg->size;
864 files = alpm_list_add(files, payload);
870 if(files) {
871 if(!current->servers) {
872 handle->pm_errno = ALPM_ERR_SERVER_NONE;
873 _alpm_log(handle, ALPM_LOG_ERROR, "%s: %s\n",
874 alpm_strerror(handle->pm_errno), current->treename);
875 errors++;
876 continue;
879 EVENT(handle, ALPM_EVENT_RETRIEVE_START, current->treename, NULL);
880 for(j = files; j; j = j->next) {
881 struct dload_payload *payload = j->data;
882 alpm_list_t *server;
883 int ret = -1;
884 for(server = current->servers; server; server = server->next) {
885 const char *server_url = server->data;
886 size_t len;
888 /* print server + filename into a buffer */
889 len = strlen(server_url) + strlen(payload->remote_name) + 2;
890 MALLOC(payload->fileurl, len, RET_ERR(handle, ALPM_ERR_MEMORY, -1));
891 snprintf(payload->fileurl, len, "%s/%s", server_url, payload->remote_name);
892 payload->handle = handle;
893 payload->allow_resume = 1;
895 ret = _alpm_download(payload, cachedir, NULL);
896 if(ret != -1) {
897 break;
900 if(ret == -1) {
901 errors++;
902 _alpm_log(handle, ALPM_LOG_WARNING, _("failed to retrieve some files from %s\n"),
903 current->treename);
907 alpm_list_free_inner(files, (alpm_list_fn_free)_alpm_dload_payload_reset);
908 FREELIST(files);
912 for(j = handle->trans->add; j; j = j->next) {
913 alpm_pkg_t *pkg = j->data;
914 pkg->infolevel &= ~INFRQ_DSIZE;
915 pkg->download_size = 0;
918 /* clear out value to let callback know we are done */
919 if(handle->totaldlcb) {
920 handle->totaldlcb(0);
923 return errors;
926 static int check_validity(alpm_handle_t *handle,
927 size_t total, size_t total_bytes)
929 struct validity {
930 alpm_pkg_t *pkg;
931 char *path;
932 alpm_siglist_t *siglist;
933 alpm_siglevel_t level;
934 enum _alpm_errno_t error;
936 size_t current = 0, current_bytes = 0;
937 alpm_list_t *i, *errors = NULL;
939 /* Check integrity of packages */
940 EVENT(handle, ALPM_EVENT_INTEGRITY_START, NULL, NULL);
942 for(i = handle->trans->add; i; i = i->next, current++) {
943 struct validity v = { i->data, NULL, NULL, 0, 0 };
944 int percent = (int)(((double)current_bytes / total_bytes) * 100);
946 PROGRESS(handle, ALPM_PROGRESS_INTEGRITY_START, "", percent,
947 total, current);
948 if(v.pkg->origin == PKG_FROM_FILE) {
949 continue; /* pkg_load() has been already called, this package is valid */
952 current_bytes += v.pkg->size;
953 v.path = _alpm_filecache_find(handle, v.pkg->filename);
954 v.level = alpm_db_get_siglevel(alpm_pkg_get_db(v.pkg));
956 if(_alpm_pkg_validate_internal(handle, v.path, v.pkg,
957 v.level, &v.siglist) == -1) {
958 v.error = handle->pm_errno;
959 struct validity *invalid = malloc(sizeof(struct validity));
960 memcpy(invalid, &v, sizeof(struct validity));
961 errors = alpm_list_add(errors, invalid);
962 } else {
963 alpm_siglist_cleanup(v.siglist);
964 free(v.siglist);
965 free(v.path);
969 PROGRESS(handle, ALPM_PROGRESS_INTEGRITY_START, "", 100,
970 total, current);
971 EVENT(handle, ALPM_EVENT_INTEGRITY_DONE, NULL, NULL);
973 if(errors) {
974 int tryagain = 0;
975 for(i = errors; i; i = i->next) {
976 struct validity *v = i->data;
977 if(v->error == ALPM_ERR_PKG_INVALID_SIG) {
978 int retry = _alpm_process_siglist(handle, v->pkg->name, v->siglist,
979 v->level & ALPM_SIG_PACKAGE_OPTIONAL,
980 v->level & ALPM_SIG_PACKAGE_MARGINAL_OK,
981 v->level & ALPM_SIG_PACKAGE_UNKNOWN_OK);
982 tryagain += retry;
983 } else if(v->error == ALPM_ERR_PKG_INVALID_CHECKSUM) {
984 prompt_to_delete(handle, v->path, v->error);
986 alpm_siglist_cleanup(v->siglist);
987 free(v->siglist);
988 free(v->path);
989 free(v);
991 alpm_list_free(errors);
993 if(tryagain == 0) {
994 if(!handle->pm_errno) {
995 RET_ERR(handle, ALPM_ERR_PKG_INVALID, -1);
997 return -1;
999 /* we were told at least once we can try again */
1000 return 1;
1003 return 0;
1006 static int load_packages(alpm_handle_t *handle, alpm_list_t **data,
1007 size_t total, size_t total_bytes)
1009 size_t current = 0, current_bytes = 0;
1010 int errors = 0;
1011 alpm_list_t *i;
1013 /* load packages from disk now that they are known-valid */
1014 EVENT(handle, ALPM_EVENT_LOAD_START, NULL, NULL);
1016 for(i = handle->trans->add; i; i = i->next, current++) {
1017 alpm_pkg_t *spkg = i->data;
1018 char *filepath;
1019 int percent = (int)(((double)current_bytes / total_bytes) * 100);
1021 PROGRESS(handle, ALPM_PROGRESS_LOAD_START, "", percent,
1022 total, current);
1023 if(spkg->origin == PKG_FROM_FILE) {
1024 continue; /* pkg_load() has been already called, this package is valid */
1027 current_bytes += spkg->size;
1028 filepath = _alpm_filecache_find(handle, spkg->filename);
1030 /* load the package file and replace pkgcache entry with it in the target list */
1031 /* TODO: alpm_pkg_get_db() will not work on this target anymore */
1032 _alpm_log(handle, ALPM_LOG_DEBUG,
1033 "replacing pkgcache entry with package file for target %s\n",
1034 spkg->name);
1035 alpm_pkg_t *pkgfile =_alpm_pkg_load_internal(handle, filepath, 1);
1036 if(!pkgfile) {
1037 errors++;
1038 *data = alpm_list_add(*data, strdup(spkg->filename));
1039 free(filepath);
1040 continue;
1042 free(filepath);
1043 /* copy over the install reason */
1044 pkgfile->reason = spkg->reason;
1045 i->data = pkgfile;
1046 /* spkg has been removed from the target list, so we can free the
1047 * sync-specific fields */
1048 _alpm_pkg_free_trans(spkg);
1051 PROGRESS(handle, ALPM_PROGRESS_LOAD_START, "", 100,
1052 total, current);
1053 EVENT(handle, ALPM_EVENT_LOAD_DONE, NULL, NULL);
1055 if(errors) {
1056 if(!handle->pm_errno) {
1057 RET_ERR(handle, ALPM_ERR_PKG_INVALID, -1);
1059 return -1;
1062 return 0;
1065 int _alpm_sync_commit(alpm_handle_t *handle, alpm_list_t **data)
1067 alpm_list_t *i, *deltas = NULL;
1068 size_t total = 0, total_bytes = 0;
1069 alpm_trans_t *trans = handle->trans;
1071 if(download_files(handle, &deltas)) {
1072 alpm_list_free(deltas);
1073 return -1;
1076 if(validate_deltas(handle, deltas)) {
1077 alpm_list_free(deltas);
1078 return -1;
1080 alpm_list_free(deltas);
1082 /* Use the deltas to generate the packages */
1083 if(apply_deltas(handle)) {
1084 return -1;
1087 /* get the total size of all packages so we can adjust the progress bar more
1088 * realistically if there are small and huge packages involved */
1089 for(i = trans->add; i; i = i->next) {
1090 alpm_pkg_t *spkg = i->data;
1091 if(spkg->origin != PKG_FROM_FILE) {
1092 total_bytes += spkg->size;
1094 total++;
1096 /* this can only happen maliciously */
1097 total_bytes = total_bytes ? total_bytes : 1;
1099 /* this one is special: -1 is failure, 1 is retry, 0 is success */
1100 while(1) {
1101 int ret = check_validity(handle, total, total_bytes);
1102 if(ret == 0) {
1103 break;
1104 } else if(ret < 0) {
1105 return -1;
1109 if(trans->flags & ALPM_TRANS_FLAG_DOWNLOADONLY) {
1110 return 0;
1113 if(load_packages(handle, data, total, total_bytes)) {
1114 return -1;
1117 trans->state = STATE_COMMITING;
1119 /* fileconflict check */
1120 if(!(trans->flags & ALPM_TRANS_FLAG_FORCE)) {
1121 EVENT(handle, ALPM_EVENT_FILECONFLICTS_START, NULL, NULL);
1123 _alpm_log(handle, ALPM_LOG_DEBUG, "looking for file conflicts\n");
1124 alpm_list_t *conflict = _alpm_db_find_fileconflicts(handle,
1125 trans->add, trans->remove);
1126 if(conflict) {
1127 if(data) {
1128 *data = conflict;
1129 } else {
1130 alpm_list_free_inner(conflict, (alpm_list_fn_free)_alpm_fileconflict_free);
1131 alpm_list_free(conflict);
1133 RET_ERR(handle, ALPM_ERR_FILE_CONFLICTS, -1);
1136 EVENT(handle, ALPM_EVENT_FILECONFLICTS_DONE, NULL, NULL);
1139 /* check available disk space */
1140 if(handle->checkspace) {
1141 EVENT(handle, ALPM_EVENT_DISKSPACE_START, NULL, NULL);
1143 _alpm_log(handle, ALPM_LOG_DEBUG, "checking available disk space\n");
1144 if(_alpm_check_diskspace(handle) == -1) {
1145 _alpm_log(handle, ALPM_LOG_ERROR, "%s\n", _("not enough free disk space"));
1146 return -1;
1149 EVENT(handle, ALPM_EVENT_DISKSPACE_DONE, NULL, NULL);
1152 /* remove conflicting and to-be-replaced packages */
1153 if(trans->remove) {
1154 _alpm_log(handle, ALPM_LOG_DEBUG, "removing conflicting and to-be-replaced packages\n");
1155 /* we want the frontend to be aware of commit details */
1156 if(_alpm_remove_packages(handle, 0) == -1) {
1157 _alpm_log(handle, ALPM_LOG_ERROR, _("could not commit removal transaction\n"));
1158 return -1;
1162 /* install targets */
1163 _alpm_log(handle, ALPM_LOG_DEBUG, "installing packages\n");
1164 if(_alpm_upgrade_packages(handle) == -1) {
1165 _alpm_log(handle, ALPM_LOG_ERROR, _("could not commit transaction\n"));
1166 return -1;
1169 return 0;
1172 /* vim: set ts=2 sw=2 noet: */