dload: add pointer to server list for each payload
[pacman-ng.git] / lib / libalpm / sync.c
blobf7147db14de654e072108a3a48d5b39851d9a83f
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;
292 if(newpkg->origin != PKG_FROM_SYNCDB) {
293 newpkg->infolevel |= INFRQ_DSIZE;
294 newpkg->download_size = 0;
295 return 0;
298 ASSERT(newpkg->filename != NULL, RET_ERR(handle, ALPM_ERR_PKG_INVALID_NAME, -1));
299 fname = newpkg->filename;
300 fpath = _alpm_filecache_find(handle, fname);
302 /* downloaded file exists, so there's nothing to grab */
303 if(fpath) {
304 size = 0;
305 goto finish;
308 CALLOC(fnamepart, strlen(fname) + 6, sizeof(char), return -1);
309 sprintf(fnamepart, "%s.part", fname);
310 fpath = _alpm_filecache_find(handle, fnamepart);
311 if(fpath) {
312 struct stat st;
313 if(stat(fpath, &st) == 0) {
314 /* subtract the size of the .part file */
315 _alpm_log(handle, ALPM_LOG_DEBUG, "using (package - .part) size\n");
316 size = newpkg->size - st.st_size;
317 size = size < 0 ? 0 : size;
319 } else if(handle->usedelta) {
320 off_t dltsize;
322 dltsize = _alpm_shortest_delta_path(handle, newpkg->deltas,
323 newpkg->filename, &newpkg->delta_path);
325 if(newpkg->delta_path && (dltsize < newpkg->size * MAX_DELTA_RATIO)) {
326 _alpm_log(handle, ALPM_LOG_DEBUG, "using delta size\n");
327 size = dltsize;
328 } else {
329 _alpm_log(handle, ALPM_LOG_DEBUG, "using package size\n");
330 size = newpkg->size;
331 alpm_list_free(newpkg->delta_path);
332 newpkg->delta_path = NULL;
334 } else {
335 size = newpkg->size;
338 finish:
339 _alpm_log(handle, ALPM_LOG_DEBUG, "setting download size %jd for pkg %s\n",
340 (intmax_t)size, newpkg->name);
342 newpkg->infolevel |= INFRQ_DSIZE;
343 newpkg->download_size = size;
345 FREE(fpath);
346 FREE(fnamepart);
348 return 0;
351 int _alpm_sync_prepare(alpm_handle_t *handle, alpm_list_t **data)
353 alpm_list_t *i, *j;
354 alpm_list_t *deps = NULL;
355 alpm_list_t *unresolvable = NULL;
356 alpm_list_t *remove = NULL;
357 int ret = 0;
358 alpm_trans_t *trans = handle->trans;
360 if(data) {
361 *data = NULL;
364 /* ensure all sync database are valid since we will be using them */
365 for(i = handle->dbs_sync; i; i = i->next) {
366 const alpm_db_t *db = i->data;
367 if(db->status & DB_STATUS_INVALID) {
368 RET_ERR(handle, ALPM_ERR_DB_INVALID, -1);
370 if(db->status & DB_STATUS_MISSING) {
371 RET_ERR(handle, ALPM_ERR_DB_NOT_FOUND, -1);
375 if(!(trans->flags & ALPM_TRANS_FLAG_NODEPS)) {
376 alpm_list_t *resolved = NULL; /* target list after resolvedeps */
378 /* Build up list by repeatedly resolving each transaction package */
379 /* Resolve targets dependencies */
380 EVENT(handle, ALPM_EVENT_RESOLVEDEPS_START, NULL, NULL);
381 _alpm_log(handle, ALPM_LOG_DEBUG, "resolving target's dependencies\n");
383 /* build remove list for resolvedeps */
384 for(i = trans->add; i; i = i->next) {
385 alpm_pkg_t *spkg = i->data;
386 for(j = spkg->removes; j; j = j->next) {
387 remove = alpm_list_add(remove, j->data);
391 /* Compute the fake local database for resolvedeps (partial fix for the
392 * phonon/qt issue) */
393 alpm_list_t *localpkgs = alpm_list_diff(_alpm_db_get_pkgcache(handle->db_local),
394 trans->add, _alpm_pkg_cmp);
396 /* Resolve packages in the transaction one at a time, in addition
397 building up a list of packages which could not be resolved. */
398 for(i = trans->add; i; i = i->next) {
399 alpm_pkg_t *pkg = i->data;
400 if(_alpm_resolvedeps(handle, localpkgs, pkg, trans->add,
401 &resolved, remove, data) == -1) {
402 unresolvable = alpm_list_add(unresolvable, pkg);
404 /* Else, [resolved] now additionally contains [pkg] and all of its
405 dependencies not already on the list */
407 alpm_list_free(localpkgs);
409 /* If there were unresolvable top-level packages, prompt the user to
410 see if they'd like to ignore them rather than failing the sync */
411 if(unresolvable != NULL) {
412 int remove_unresolvable = 0;
413 QUESTION(handle, ALPM_QUESTION_REMOVE_PKGS, unresolvable,
414 NULL, NULL, &remove_unresolvable);
415 if(remove_unresolvable) {
416 /* User wants to remove the unresolvable packages from the
417 transaction. The packages will be removed from the actual
418 transaction when the transaction packages are replaced with a
419 dependency-reordered list below */
420 handle->pm_errno = 0; /* pm_errno was set by resolvedeps */
421 if(data) {
422 alpm_list_free_inner(*data, (alpm_list_fn_free)_alpm_depmiss_free);
423 alpm_list_free(*data);
424 *data = NULL;
426 } else {
427 /* pm_errno is set by resolvedeps */
428 alpm_list_free(resolved);
429 ret = -1;
430 goto cleanup;
434 /* Set DEPEND reason for pulled packages */
435 for(i = resolved; i; i = i->next) {
436 alpm_pkg_t *pkg = i->data;
437 if(!_alpm_pkg_find(trans->add, pkg->name)) {
438 pkg->reason = ALPM_PKG_REASON_DEPEND;
442 /* Unresolvable packages will be removed from the target list; set these
443 * aside in the transaction as a list we won't operate on. If we free them
444 * before the end of the transaction, we may kill pointers the frontend
445 * holds to package objects. */
446 trans->unresolvable = unresolvable;
448 /* re-order w.r.t. dependencies */
449 alpm_list_free(trans->add);
450 trans->add = _alpm_sortbydeps(handle, resolved, 0);
451 alpm_list_free(resolved);
453 EVENT(handle, ALPM_EVENT_RESOLVEDEPS_DONE, NULL, NULL);
456 if(!(trans->flags & ALPM_TRANS_FLAG_NOCONFLICTS)) {
457 /* check for inter-conflicts and whatnot */
458 EVENT(handle, ALPM_EVENT_INTERCONFLICTS_START, NULL, NULL);
460 _alpm_log(handle, ALPM_LOG_DEBUG, "looking for conflicts\n");
462 /* 1. check for conflicts in the target list */
463 _alpm_log(handle, ALPM_LOG_DEBUG, "check targets vs targets\n");
464 deps = _alpm_innerconflicts(handle, trans->add);
466 for(i = deps; i; i = i->next) {
467 alpm_conflict_t *conflict = i->data;
468 alpm_pkg_t *rsync, *sync, *sync1, *sync2;
470 /* have we already removed one of the conflicting targets? */
471 sync1 = _alpm_pkg_find(trans->add, conflict->package1);
472 sync2 = _alpm_pkg_find(trans->add, conflict->package2);
473 if(!sync1 || !sync2) {
474 continue;
477 _alpm_log(handle, ALPM_LOG_DEBUG, "conflicting packages in the sync list: '%s' <-> '%s'\n",
478 conflict->package1, conflict->package2);
480 /* if sync1 provides sync2, we remove sync2 from the targets, and vice versa */
481 alpm_depend_t *dep1 = _alpm_splitdep(conflict->package1);
482 alpm_depend_t *dep2 = _alpm_splitdep(conflict->package2);
483 if(_alpm_depcmp(sync1, dep2)) {
484 rsync = sync2;
485 sync = sync1;
486 } else if(_alpm_depcmp(sync2, dep1)) {
487 rsync = sync1;
488 sync = sync2;
489 } else {
490 _alpm_log(handle, ALPM_LOG_ERROR, _("unresolvable package conflicts detected\n"));
491 handle->pm_errno = ALPM_ERR_CONFLICTING_DEPS;
492 ret = -1;
493 if(data) {
494 alpm_conflict_t *newconflict = _alpm_conflict_dup(conflict);
495 if(newconflict) {
496 *data = alpm_list_add(*data, newconflict);
499 alpm_list_free_inner(deps, (alpm_list_fn_free)_alpm_conflict_free);
500 alpm_list_free(deps);
501 _alpm_dep_free(dep1);
502 _alpm_dep_free(dep2);
503 goto cleanup;
505 _alpm_dep_free(dep1);
506 _alpm_dep_free(dep2);
508 /* Prints warning */
509 _alpm_log(handle, ALPM_LOG_WARNING,
510 _("removing '%s' from target list because it conflicts with '%s'\n"),
511 rsync->name, sync->name);
512 trans->add = alpm_list_remove(trans->add, rsync, _alpm_pkg_cmp, NULL);
513 /* rsync is not a transaction target anymore */
514 trans->unresolvable = alpm_list_add(trans->unresolvable, rsync);
515 continue;
518 alpm_list_free_inner(deps, (alpm_list_fn_free)_alpm_conflict_free);
519 alpm_list_free(deps);
520 deps = NULL;
522 /* 2. we check for target vs db conflicts (and resolve)*/
523 _alpm_log(handle, ALPM_LOG_DEBUG, "check targets vs db and db vs targets\n");
524 deps = _alpm_outerconflicts(handle->db_local, trans->add);
526 for(i = deps; i; i = i->next) {
527 alpm_conflict_t *conflict = i->data;
529 /* if conflict->package2 (the local package) is not elected for removal,
530 we ask the user */
531 int found = 0;
532 for(j = trans->add; j && !found; j = j->next) {
533 alpm_pkg_t *spkg = j->data;
534 if(_alpm_pkg_find(spkg->removes, conflict->package2)) {
535 found = 1;
538 if(found) {
539 continue;
542 _alpm_log(handle, ALPM_LOG_DEBUG, "package '%s' conflicts with '%s'\n",
543 conflict->package1, conflict->package2);
545 alpm_pkg_t *sync = _alpm_pkg_find(trans->add, conflict->package1);
546 alpm_pkg_t *local = _alpm_db_get_pkgfromcache(handle->db_local, conflict->package2);
547 int doremove = 0;
548 QUESTION(handle, ALPM_QUESTION_CONFLICT_PKG, conflict->package1,
549 conflict->package2, conflict->reason->name, &doremove);
550 if(doremove) {
551 /* append to the removes list */
552 _alpm_log(handle, ALPM_LOG_DEBUG, "electing '%s' for removal\n", conflict->package2);
553 sync->removes = alpm_list_add(sync->removes, local);
554 } else { /* abort */
555 _alpm_log(handle, ALPM_LOG_ERROR, _("unresolvable package conflicts detected\n"));
556 handle->pm_errno = ALPM_ERR_CONFLICTING_DEPS;
557 ret = -1;
558 if(data) {
559 alpm_conflict_t *newconflict = _alpm_conflict_dup(conflict);
560 if(newconflict) {
561 *data = alpm_list_add(*data, newconflict);
564 alpm_list_free_inner(deps, (alpm_list_fn_free)_alpm_conflict_free);
565 alpm_list_free(deps);
566 goto cleanup;
569 EVENT(handle, ALPM_EVENT_INTERCONFLICTS_DONE, NULL, NULL);
570 alpm_list_free_inner(deps, (alpm_list_fn_free)_alpm_conflict_free);
571 alpm_list_free(deps);
574 /* Build trans->remove list */
575 for(i = trans->add; i; i = i->next) {
576 alpm_pkg_t *spkg = i->data;
577 for(j = spkg->removes; j; j = j->next) {
578 alpm_pkg_t *rpkg = j->data;
579 if(!_alpm_pkg_find(trans->remove, rpkg->name)) {
580 alpm_pkg_t *copy;
581 _alpm_log(handle, ALPM_LOG_DEBUG, "adding '%s' to remove list\n", rpkg->name);
582 if(_alpm_pkg_dup(rpkg, &copy) == -1) {
583 return -1;
585 trans->remove = alpm_list_add(trans->remove, copy);
590 if(!(trans->flags & ALPM_TRANS_FLAG_NODEPS)) {
591 _alpm_log(handle, ALPM_LOG_DEBUG, "checking dependencies\n");
592 deps = alpm_checkdeps(handle, _alpm_db_get_pkgcache(handle->db_local),
593 trans->remove, trans->add, 1);
594 if(deps) {
595 handle->pm_errno = ALPM_ERR_UNSATISFIED_DEPS;
596 ret = -1;
597 if(data) {
598 *data = deps;
599 } else {
600 alpm_list_free_inner(deps, (alpm_list_fn_free)_alpm_depmiss_free);
601 alpm_list_free(deps);
603 goto cleanup;
606 for(i = trans->add; i; i = i->next) {
607 /* update download size field */
608 alpm_pkg_t *spkg = i->data;
609 if(compute_download_size(spkg) != 0) {
610 ret = -1;
611 goto cleanup;
615 cleanup:
616 alpm_list_free(remove);
618 return ret;
621 /** Returns the size of the files that will be downloaded to install a
622 * package.
623 * @param newpkg the new package to upgrade to
624 * @return the size of the download
626 off_t SYMEXPORT alpm_pkg_download_size(alpm_pkg_t *newpkg)
628 if(!(newpkg->infolevel & INFRQ_DSIZE)) {
629 compute_download_size(newpkg);
631 return newpkg->download_size;
634 static int endswith(const char *filename, const char *extension)
636 const char *s = filename + strlen(filename) - strlen(extension);
637 return strcmp(s, extension) == 0;
640 /** Applies delta files to create an upgraded package file.
642 * All intermediate files are deleted, leaving only the starting and
643 * ending package files.
645 * @param handle the context handle
647 * @return 0 if all delta files were able to be applied, 1 otherwise.
649 static int apply_deltas(alpm_handle_t *handle)
651 alpm_list_t *i;
652 int deltas_found = 0, ret = 0;
653 const char *cachedir = _alpm_filecache_setup(handle);
654 alpm_trans_t *trans = handle->trans;
656 for(i = trans->add; i; i = i->next) {
657 alpm_pkg_t *spkg = i->data;
658 alpm_list_t *delta_path = spkg->delta_path;
659 alpm_list_t *dlts = NULL;
661 if(!delta_path) {
662 continue;
665 if(!deltas_found) {
666 /* only show this if we actually have deltas to apply, and it is before
667 * the very first one */
668 EVENT(handle, ALPM_EVENT_DELTA_PATCHES_START, NULL, NULL);
669 deltas_found = 1;
672 for(dlts = delta_path; dlts; dlts = dlts->next) {
673 alpm_delta_t *d = dlts->data;
674 char *delta, *from, *to;
675 char command[PATH_MAX];
676 size_t len = 0;
678 delta = _alpm_filecache_find(handle, d->delta);
679 /* the initial package might be in a different cachedir */
680 if(dlts == delta_path) {
681 from = _alpm_filecache_find(handle, d->from);
682 } else {
683 /* len = cachedir len + from len + '/' + null */
684 len = strlen(cachedir) + strlen(d->from) + 2;
685 MALLOC(from, len, RET_ERR(handle, ALPM_ERR_MEMORY, 1));
686 snprintf(from, len, "%s/%s", cachedir, d->from);
688 len = strlen(cachedir) + strlen(d->to) + 2;
689 MALLOC(to, len, RET_ERR(handle, ALPM_ERR_MEMORY, 1));
690 snprintf(to, len, "%s/%s", cachedir, d->to);
692 /* build the patch command */
693 if(endswith(to, ".gz")) {
694 /* special handling for gzip : we disable timestamp with -n option */
695 snprintf(command, PATH_MAX, "xdelta3 -d -q -R -c -s %s %s | gzip -n > %s", from, delta, to);
696 } else {
697 snprintf(command, PATH_MAX, "xdelta3 -d -q -s %s %s %s", from, delta, to);
700 _alpm_log(handle, ALPM_LOG_DEBUG, "command: %s\n", command);
702 EVENT(handle, ALPM_EVENT_DELTA_PATCH_START, d->to, d->delta);
704 int retval = system(command);
705 if(retval == 0) {
706 EVENT(handle, ALPM_EVENT_DELTA_PATCH_DONE, NULL, NULL);
708 /* delete the delta file */
709 unlink(delta);
711 /* Delete the 'from' package but only if it is an intermediate
712 * package. The starting 'from' package should be kept, just
713 * as if deltas were not used. */
714 if(dlts != delta_path) {
715 unlink(from);
718 FREE(from);
719 FREE(to);
720 FREE(delta);
722 if(retval != 0) {
723 /* one delta failed for this package, cancel the remaining ones */
724 EVENT(handle, ALPM_EVENT_DELTA_PATCH_FAILED, NULL, NULL);
725 handle->pm_errno = ALPM_ERR_DLT_PATCHFAILED;
726 ret = 1;
727 break;
731 if(deltas_found) {
732 EVENT(handle, ALPM_EVENT_DELTA_PATCHES_DONE, NULL, NULL);
735 return ret;
739 * Prompts to delete the file now that we know it is invalid.
740 * @param handle the context handle
741 * @param filename the absolute path of the file to test
742 * @param reason an error code indicating the reason for package invalidity
744 * @return 1 if file was removed, 0 otherwise
746 static int prompt_to_delete(alpm_handle_t *handle, const char *filepath,
747 enum _alpm_errno_t reason)
749 int doremove = 0;
750 QUESTION(handle, ALPM_QUESTION_CORRUPTED_PKG, (char *)filepath,
751 &reason, NULL, &doremove);
752 if(doremove) {
753 unlink(filepath);
755 return doremove;
758 static int validate_deltas(alpm_handle_t *handle, alpm_list_t *deltas)
760 alpm_list_t *i, *errors = NULL;
762 if(!deltas) {
763 return 0;
766 /* Check integrity of deltas */
767 EVENT(handle, ALPM_EVENT_DELTA_INTEGRITY_START, NULL, NULL);
768 for(i = deltas; i; i = i->next) {
769 alpm_delta_t *d = i->data;
770 char *filepath = _alpm_filecache_find(handle, d->delta);
772 if(_alpm_test_checksum(filepath, d->delta_md5, ALPM_CSUM_MD5)) {
773 errors = alpm_list_add(errors, filepath);
774 } else {
775 FREE(filepath);
778 EVENT(handle, ALPM_EVENT_DELTA_INTEGRITY_DONE, NULL, NULL);
780 if(errors) {
781 for(i = errors; i; i = i->next) {
782 char *filepath = i->data;
783 prompt_to_delete(handle, filepath, ALPM_ERR_DLT_INVALID);
784 FREE(filepath);
786 alpm_list_free(errors);
787 handle->pm_errno = ALPM_ERR_DLT_INVALID;
788 return -1;
790 return 0;
793 static int download_files(alpm_handle_t *handle, alpm_list_t **deltas)
795 const char *cachedir;
796 alpm_list_t *i, *j;
797 alpm_list_t *files = NULL;
798 int errors = 0;
800 cachedir = _alpm_filecache_setup(handle);
801 handle->trans->state = STATE_DOWNLOADING;
803 /* Total progress - figure out the total download size if required to
804 * pass to the callback. This function is called once, and it is up to the
805 * frontend to compute incremental progress. */
806 if(handle->totaldlcb) {
807 off_t total_size = (off_t)0;
808 /* sum up the download size for each package and store total */
809 for(i = handle->trans->add; i; i = i->next) {
810 alpm_pkg_t *spkg = i->data;
811 total_size += spkg->download_size;
813 handle->totaldlcb(total_size);
816 /* group sync records by repository and download */
817 for(i = handle->dbs_sync; i; i = i->next) {
818 alpm_db_t *current = i->data;
820 for(j = handle->trans->add; j; j = j->next) {
821 alpm_pkg_t *spkg = j->data;
823 if(spkg->origin != PKG_FROM_FILE && current == spkg->origin_data.db) {
824 alpm_list_t *delta_path = spkg->delta_path;
825 if(delta_path) {
826 /* using deltas */
827 alpm_list_t *dlts;
828 for(dlts = delta_path; dlts; dlts = dlts->next) {
829 alpm_delta_t *delta = dlts->data;
830 if(delta->download_size != 0) {
831 struct dload_payload *dpayload;
833 CALLOC(dpayload, 1, sizeof(*dpayload), RET_ERR(handle, ALPM_ERR_MEMORY, -1));
834 STRDUP(dpayload->remote_name, delta->delta, RET_ERR(handle, ALPM_ERR_MEMORY, -1));
835 dpayload->max_size = delta->download_size;
836 dpayload->servers = current->servers;
838 files = alpm_list_add(files, dpayload);
840 /* keep a list of all the delta files for md5sums */
841 *deltas = alpm_list_add(*deltas, delta);
844 } else if(spkg->download_size != 0) {
845 struct dload_payload *payload;
847 ASSERT(spkg->filename != NULL, RET_ERR(handle, ALPM_ERR_PKG_INVALID_NAME, -1));
848 CALLOC(payload, 1, sizeof(*payload), RET_ERR(handle, ALPM_ERR_MEMORY, -1));
849 STRDUP(payload->remote_name, spkg->filename, RET_ERR(handle, ALPM_ERR_MEMORY, -1));
850 payload->max_size = spkg->size;
851 payload->servers = current->servers;
853 files = alpm_list_add(files, payload);
859 if(files) {
860 if(!current->servers) {
861 handle->pm_errno = ALPM_ERR_SERVER_NONE;
862 _alpm_log(handle, ALPM_LOG_ERROR, "%s: %s\n",
863 alpm_strerror(handle->pm_errno), current->treename);
864 errors++;
865 continue;
868 EVENT(handle, ALPM_EVENT_RETRIEVE_START, current->treename, NULL);
869 for(j = files; j; j = j->next) {
870 struct dload_payload *payload = j->data;
871 alpm_list_t *server;
872 int ret = -1;
873 for(server = current->servers; server; server = server->next) {
874 const char *server_url = server->data;
875 size_t len;
877 /* print server + filename into a buffer */
878 len = strlen(server_url) + strlen(payload->remote_name) + 2;
879 MALLOC(payload->fileurl, len, RET_ERR(handle, ALPM_ERR_MEMORY, -1));
880 snprintf(payload->fileurl, len, "%s/%s", server_url, payload->remote_name);
881 payload->handle = handle;
882 payload->allow_resume = 1;
884 ret = _alpm_download(payload, cachedir, NULL);
885 if(ret != -1) {
886 break;
889 if(ret == -1) {
890 errors++;
891 _alpm_log(handle, ALPM_LOG_WARNING, _("failed to retrieve some files from %s\n"),
892 current->treename);
896 alpm_list_free_inner(files, (alpm_list_fn_free)_alpm_dload_payload_reset);
897 FREELIST(files);
901 for(j = handle->trans->add; j; j = j->next) {
902 alpm_pkg_t *pkg = j->data;
903 pkg->infolevel &= ~INFRQ_DSIZE;
904 pkg->download_size = 0;
907 /* clear out value to let callback know we are done */
908 if(handle->totaldlcb) {
909 handle->totaldlcb(0);
912 return errors;
915 static int check_validity(alpm_handle_t *handle,
916 size_t total, size_t total_bytes)
918 struct validity {
919 alpm_pkg_t *pkg;
920 char *path;
921 alpm_siglist_t *siglist;
922 alpm_siglevel_t level;
923 enum _alpm_errno_t error;
925 size_t current = 0, current_bytes = 0;
926 alpm_list_t *i, *errors = NULL;
928 /* Check integrity of packages */
929 EVENT(handle, ALPM_EVENT_INTEGRITY_START, NULL, NULL);
931 for(i = handle->trans->add; i; i = i->next, current++) {
932 struct validity v = { i->data, NULL, NULL, 0, 0 };
933 int percent = (int)(((double)current_bytes / total_bytes) * 100);
935 PROGRESS(handle, ALPM_PROGRESS_INTEGRITY_START, "", percent,
936 total, current);
937 if(v.pkg->origin == PKG_FROM_FILE) {
938 continue; /* pkg_load() has been already called, this package is valid */
941 current_bytes += v.pkg->size;
942 v.path = _alpm_filecache_find(handle, v.pkg->filename);
943 v.level = alpm_db_get_siglevel(alpm_pkg_get_db(v.pkg));
945 if(_alpm_pkg_validate_internal(handle, v.path, v.pkg,
946 v.level, &v.siglist) == -1) {
947 v.error = handle->pm_errno;
948 struct validity *invalid = malloc(sizeof(struct validity));
949 memcpy(invalid, &v, sizeof(struct validity));
950 errors = alpm_list_add(errors, invalid);
951 } else {
952 alpm_siglist_cleanup(v.siglist);
953 free(v.siglist);
954 free(v.path);
958 PROGRESS(handle, ALPM_PROGRESS_INTEGRITY_START, "", 100,
959 total, current);
960 EVENT(handle, ALPM_EVENT_INTEGRITY_DONE, NULL, NULL);
962 if(errors) {
963 int tryagain = 0;
964 for(i = errors; i; i = i->next) {
965 struct validity *v = i->data;
966 if(v->error == ALPM_ERR_PKG_INVALID_SIG) {
967 int retry = _alpm_process_siglist(handle, v->pkg->name, v->siglist,
968 v->level & ALPM_SIG_PACKAGE_OPTIONAL,
969 v->level & ALPM_SIG_PACKAGE_MARGINAL_OK,
970 v->level & ALPM_SIG_PACKAGE_UNKNOWN_OK);
971 tryagain += retry;
972 } else if(v->error == ALPM_ERR_PKG_INVALID_CHECKSUM) {
973 prompt_to_delete(handle, v->path, v->error);
975 alpm_siglist_cleanup(v->siglist);
976 free(v->siglist);
977 free(v->path);
978 free(v);
980 alpm_list_free(errors);
982 if(tryagain == 0) {
983 if(!handle->pm_errno) {
984 RET_ERR(handle, ALPM_ERR_PKG_INVALID, -1);
986 return -1;
988 /* we were told at least once we can try again */
989 return 1;
992 return 0;
995 static int load_packages(alpm_handle_t *handle, alpm_list_t **data,
996 size_t total, size_t total_bytes)
998 size_t current = 0, current_bytes = 0;
999 int errors = 0;
1000 alpm_list_t *i;
1002 /* load packages from disk now that they are known-valid */
1003 EVENT(handle, ALPM_EVENT_LOAD_START, NULL, NULL);
1005 for(i = handle->trans->add; i; i = i->next, current++) {
1006 alpm_pkg_t *spkg = i->data;
1007 char *filepath;
1008 int percent = (int)(((double)current_bytes / total_bytes) * 100);
1010 PROGRESS(handle, ALPM_PROGRESS_LOAD_START, "", percent,
1011 total, current);
1012 if(spkg->origin == PKG_FROM_FILE) {
1013 continue; /* pkg_load() has been already called, this package is valid */
1016 current_bytes += spkg->size;
1017 filepath = _alpm_filecache_find(handle, spkg->filename);
1019 /* load the package file and replace pkgcache entry with it in the target list */
1020 /* TODO: alpm_pkg_get_db() will not work on this target anymore */
1021 _alpm_log(handle, ALPM_LOG_DEBUG,
1022 "replacing pkgcache entry with package file for target %s\n",
1023 spkg->name);
1024 alpm_pkg_t *pkgfile =_alpm_pkg_load_internal(handle, filepath, 1);
1025 if(!pkgfile) {
1026 errors++;
1027 *data = alpm_list_add(*data, strdup(spkg->filename));
1028 free(filepath);
1029 continue;
1031 free(filepath);
1032 /* copy over the install reason */
1033 pkgfile->reason = spkg->reason;
1034 i->data = pkgfile;
1035 /* spkg has been removed from the target list, so we can free the
1036 * sync-specific fields */
1037 _alpm_pkg_free_trans(spkg);
1040 PROGRESS(handle, ALPM_PROGRESS_LOAD_START, "", 100,
1041 total, current);
1042 EVENT(handle, ALPM_EVENT_LOAD_DONE, NULL, NULL);
1044 if(errors) {
1045 if(!handle->pm_errno) {
1046 RET_ERR(handle, ALPM_ERR_PKG_INVALID, -1);
1048 return -1;
1051 return 0;
1054 int _alpm_sync_commit(alpm_handle_t *handle, alpm_list_t **data)
1056 alpm_list_t *i, *deltas = NULL;
1057 size_t total = 0, total_bytes = 0;
1058 alpm_trans_t *trans = handle->trans;
1060 if(download_files(handle, &deltas)) {
1061 alpm_list_free(deltas);
1062 return -1;
1065 if(validate_deltas(handle, deltas)) {
1066 alpm_list_free(deltas);
1067 return -1;
1069 alpm_list_free(deltas);
1071 /* Use the deltas to generate the packages */
1072 if(apply_deltas(handle)) {
1073 return -1;
1076 /* get the total size of all packages so we can adjust the progress bar more
1077 * realistically if there are small and huge packages involved */
1078 for(i = trans->add; i; i = i->next) {
1079 alpm_pkg_t *spkg = i->data;
1080 if(spkg->origin != PKG_FROM_FILE) {
1081 total_bytes += spkg->size;
1083 total++;
1085 /* this can only happen maliciously */
1086 total_bytes = total_bytes ? total_bytes : 1;
1088 /* this one is special: -1 is failure, 1 is retry, 0 is success */
1089 while(1) {
1090 int ret = check_validity(handle, total, total_bytes);
1091 if(ret == 0) {
1092 break;
1093 } else if(ret < 0) {
1094 return -1;
1098 if(trans->flags & ALPM_TRANS_FLAG_DOWNLOADONLY) {
1099 return 0;
1102 if(load_packages(handle, data, total, total_bytes)) {
1103 return -1;
1106 trans->state = STATE_COMMITING;
1108 /* fileconflict check */
1109 if(!(trans->flags & ALPM_TRANS_FLAG_FORCE)) {
1110 EVENT(handle, ALPM_EVENT_FILECONFLICTS_START, NULL, NULL);
1112 _alpm_log(handle, ALPM_LOG_DEBUG, "looking for file conflicts\n");
1113 alpm_list_t *conflict = _alpm_db_find_fileconflicts(handle,
1114 trans->add, trans->remove);
1115 if(conflict) {
1116 if(data) {
1117 *data = conflict;
1118 } else {
1119 alpm_list_free_inner(conflict, (alpm_list_fn_free)_alpm_fileconflict_free);
1120 alpm_list_free(conflict);
1122 RET_ERR(handle, ALPM_ERR_FILE_CONFLICTS, -1);
1125 EVENT(handle, ALPM_EVENT_FILECONFLICTS_DONE, NULL, NULL);
1128 /* check available disk space */
1129 if(handle->checkspace) {
1130 EVENT(handle, ALPM_EVENT_DISKSPACE_START, NULL, NULL);
1132 _alpm_log(handle, ALPM_LOG_DEBUG, "checking available disk space\n");
1133 if(_alpm_check_diskspace(handle) == -1) {
1134 _alpm_log(handle, ALPM_LOG_ERROR, "%s\n", _("not enough free disk space"));
1135 return -1;
1138 EVENT(handle, ALPM_EVENT_DISKSPACE_DONE, NULL, NULL);
1141 /* remove conflicting and to-be-replaced packages */
1142 if(trans->remove) {
1143 _alpm_log(handle, ALPM_LOG_DEBUG, "removing conflicting and to-be-replaced packages\n");
1144 /* we want the frontend to be aware of commit details */
1145 if(_alpm_remove_packages(handle, 0) == -1) {
1146 _alpm_log(handle, ALPM_LOG_ERROR, _("could not commit removal transaction\n"));
1147 return -1;
1151 /* install targets */
1152 _alpm_log(handle, ALPM_LOG_DEBUG, "installing packages\n");
1153 if(_alpm_upgrade_packages(handle) == -1) {
1154 _alpm_log(handle, ALPM_LOG_ERROR, _("could not commit transaction\n"));
1155 return -1;
1158 return 0;
1161 /* vim: set ts=2 sw=2 noet: */