removed the condition in alpm_db_set_servers since FREELIST is NULL safe
[pacman-ng.git] / lib / libalpm / sync.c
blobca6b507edc476b1751fe0fc3ce7ca663c774111d
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 in %s\n",
129 lpkg->name, sdb->treename);
130 for(k = _alpm_db_get_pkgcache(sdb); k; k = k->next) {
131 int found = 0;
132 alpm_pkg_t *spkg = k->data;
133 alpm_list_t *l;
134 for(l = alpm_pkg_get_replaces(spkg); l; l = l->next) {
135 alpm_depend_t *replace = l->data;
136 /* we only want to consider literal matches at this point. */
137 if(_alpm_depcmp_literal(lpkg, replace)) {
138 found = 1;
139 break;
142 if(found) {
143 int doreplace = 0;
144 alpm_pkg_t *tpkg;
145 /* check IgnorePkg/IgnoreGroup */
146 if(_alpm_pkg_should_ignore(handle, spkg)
147 || _alpm_pkg_should_ignore(handle, lpkg)) {
148 _alpm_log(handle, ALPM_LOG_WARNING,
149 _("ignoring package replacement (%s-%s => %s-%s)\n"),
150 lpkg->name, lpkg->version, spkg->name, spkg->version);
151 continue;
154 QUESTION(handle, ALPM_QUESTION_REPLACE_PKG, lpkg, spkg,
155 sdb->treename, &doreplace);
156 if(!doreplace) {
157 continue;
160 /* If spkg is already in the target list, we append lpkg to spkg's
161 * removes list */
162 tpkg = _alpm_pkg_find(handle->trans->add, spkg->name);
163 if(tpkg) {
164 /* sanity check, multiple repos can contain spkg->name */
165 if(tpkg->origin_data.db != sdb) {
166 _alpm_log(handle, ALPM_LOG_WARNING, _("cannot replace %s by %s\n"),
167 lpkg->name, spkg->name);
168 continue;
170 _alpm_log(handle, ALPM_LOG_DEBUG, "appending %s to the removes list of %s\n",
171 lpkg->name, tpkg->name);
172 tpkg->removes = alpm_list_add(tpkg->removes, lpkg);
173 /* check the to-be-replaced package's reason field */
174 if(alpm_pkg_get_reason(lpkg) == ALPM_PKG_REASON_EXPLICIT) {
175 tpkg->reason = ALPM_PKG_REASON_EXPLICIT;
177 } else {
178 /* add spkg to the target list */
179 /* copy over reason */
180 spkg->reason = alpm_pkg_get_reason(lpkg);
181 spkg->removes = alpm_list_add(NULL, lpkg);
182 _alpm_log(handle, ALPM_LOG_DEBUG,
183 "adding package %s-%s to the transaction targets\n",
184 spkg->name, spkg->version);
185 replacers = alpm_list_add(replacers, spkg);
189 return replacers;
192 /** Search for packages to upgrade and add them to the transaction. */
193 int SYMEXPORT alpm_sync_sysupgrade(alpm_handle_t *handle, int enable_downgrade)
195 alpm_list_t *i, *j;
196 alpm_trans_t *trans;
198 CHECK_HANDLE(handle, return -1);
199 trans = handle->trans;
200 ASSERT(trans != NULL, RET_ERR(handle, ALPM_ERR_TRANS_NULL, -1));
201 ASSERT(trans->state == STATE_INITIALIZED, RET_ERR(handle, ALPM_ERR_TRANS_NOT_INITIALIZED, -1));
203 _alpm_log(handle, ALPM_LOG_DEBUG, "checking for package upgrades\n");
204 for(i = _alpm_db_get_pkgcache(handle->db_local); i; i = i->next) {
205 alpm_pkg_t *lpkg = i->data;
207 if(_alpm_pkg_find(trans->add, lpkg->name)) {
208 _alpm_log(handle, ALPM_LOG_DEBUG, "%s is already in the target list -- skipping\n", lpkg->name);
209 continue;
212 /* Search for literal then replacers in each sync database. */
213 for(j = handle->dbs_sync; j; j = j->next) {
214 alpm_db_t *sdb = j->data;
215 /* Check sdb */
216 alpm_pkg_t *spkg = _alpm_db_get_pkgfromcache(sdb, lpkg->name);
217 int literal_upgrade = 0;
218 if(spkg) {
219 literal_upgrade = check_literal(handle, lpkg, spkg, enable_downgrade);
220 if(literal_upgrade) {
221 trans->add = alpm_list_add(trans->add, spkg);
223 /* jump to next local package */
224 break;
225 } else {
226 alpm_list_t *replacers;
227 replacers = check_replacers(handle, lpkg, sdb);
228 if(replacers) {
229 trans->add = alpm_list_join(trans->add, replacers);
235 return 0;
238 /** Find group members across a list of databases.
239 * If a member exists in several databases, only the first database is used.
240 * IgnorePkg is also handled.
241 * @param dbs the list of alpm_db_t *
242 * @param name the name of the group
243 * @return the list of alpm_pkg_t * (caller is responsible for alpm_list_free)
245 alpm_list_t SYMEXPORT *alpm_find_group_pkgs(alpm_list_t *dbs,
246 const char *name)
248 alpm_list_t *i, *j, *pkgs = NULL, *ignorelist = NULL;
250 for(i = dbs; i; i = i->next) {
251 alpm_db_t *db = i->data;
252 alpm_group_t *grp = alpm_db_get_group(db, name);
254 if(!grp)
255 continue;
257 for(j = grp->packages; j; j = j->next) {
258 alpm_pkg_t *pkg = j->data;
260 if(_alpm_pkg_find(ignorelist, pkg->name)) {
261 continue;
263 if(_alpm_pkg_should_ignore(db->handle, pkg)) {
264 ignorelist = alpm_list_add(ignorelist, pkg);
265 int install = 0;
266 QUESTION(db->handle, ALPM_QUESTION_INSTALL_IGNOREPKG, pkg,
267 NULL, NULL, &install);
268 if(!install)
269 continue;
271 if(!_alpm_pkg_find(pkgs, pkg->name)) {
272 pkgs = alpm_list_add(pkgs, pkg);
276 alpm_list_free(ignorelist);
277 return pkgs;
280 /** Compute the size of the files that will be downloaded to install a
281 * package.
282 * @param newpkg the new package to upgrade to
284 static int compute_download_size(alpm_pkg_t *newpkg)
286 const char *fname;
287 char *fpath, *fnamepart = NULL;
288 off_t size = 0;
289 alpm_handle_t *handle = newpkg->handle;
290 int ret = 0;
292 if(newpkg->origin != ALPM_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;
320 /* tell the caller that we have a partial */
321 ret = 1;
322 } else if(handle->deltaratio > 0.0) {
323 off_t dltsize;
325 dltsize = _alpm_shortest_delta_path(handle, newpkg->deltas,
326 newpkg->filename, &newpkg->delta_path);
328 if(newpkg->delta_path && (dltsize < newpkg->size * handle->deltaratio)) {
329 _alpm_log(handle, ALPM_LOG_DEBUG, "using delta size\n");
330 size = dltsize;
331 } else {
332 _alpm_log(handle, ALPM_LOG_DEBUG, "using package size\n");
333 size = newpkg->size;
334 alpm_list_free(newpkg->delta_path);
335 newpkg->delta_path = NULL;
337 } else {
338 size = newpkg->size;
341 finish:
342 _alpm_log(handle, ALPM_LOG_DEBUG, "setting download size %jd for pkg %s\n",
343 (intmax_t)size, newpkg->name);
345 newpkg->infolevel |= INFRQ_DSIZE;
346 newpkg->download_size = size;
348 FREE(fpath);
349 FREE(fnamepart);
351 return ret;
354 int _alpm_sync_prepare(alpm_handle_t *handle, alpm_list_t **data)
356 alpm_list_t *i, *j;
357 alpm_list_t *deps = NULL;
358 alpm_list_t *unresolvable = NULL;
359 size_t from_sync = 0;
360 int ret = 0;
361 alpm_trans_t *trans = handle->trans;
363 if(data) {
364 *data = NULL;
367 for(i = trans->add; i; i = i->next) {
368 alpm_pkg_t *spkg = i->data;
369 from_sync += (spkg->origin == ALPM_PKG_FROM_SYNCDB);
372 /* ensure all sync database are valid if we will be using them */
373 for(i = handle->dbs_sync; i; i = i->next) {
374 const alpm_db_t *db = i->data;
375 if(db->status & DB_STATUS_INVALID) {
376 RET_ERR(handle, ALPM_ERR_DB_INVALID, -1);
378 /* missing databases are not allowed if we have sync targets */
379 if(from_sync && db->status & DB_STATUS_MISSING) {
380 RET_ERR(handle, ALPM_ERR_DB_NOT_FOUND, -1);
384 if(!(trans->flags & ALPM_TRANS_FLAG_NODEPS)) {
385 alpm_list_t *resolved = NULL;
386 alpm_list_t *remove = NULL;
387 alpm_list_t *localpkgs;
389 /* Build up list by repeatedly resolving each transaction package */
390 /* Resolve targets dependencies */
391 EVENT(handle, ALPM_EVENT_RESOLVEDEPS_START, NULL, NULL);
392 _alpm_log(handle, ALPM_LOG_DEBUG, "resolving target's dependencies\n");
394 /* build remove list for resolvedeps */
395 for(i = trans->add; i; i = i->next) {
396 alpm_pkg_t *spkg = i->data;
397 for(j = spkg->removes; j; j = j->next) {
398 remove = alpm_list_add(remove, j->data);
402 /* Compute the fake local database for resolvedeps (partial fix for the
403 * phonon/qt issue) */
404 localpkgs = alpm_list_diff(_alpm_db_get_pkgcache(handle->db_local),
405 trans->add, _alpm_pkg_cmp);
407 /* Resolve packages in the transaction one at a time, in addition
408 building up a list of packages which could not be resolved. */
409 for(i = trans->add; i; i = i->next) {
410 alpm_pkg_t *pkg = i->data;
411 if(_alpm_resolvedeps(handle, localpkgs, pkg, trans->add,
412 &resolved, remove, data) == -1) {
413 unresolvable = alpm_list_add(unresolvable, pkg);
415 /* Else, [resolved] now additionally contains [pkg] and all of its
416 dependencies not already on the list */
418 alpm_list_free(localpkgs);
419 alpm_list_free(remove);
421 /* If there were unresolvable top-level packages, prompt the user to
422 see if they'd like to ignore them rather than failing the sync */
423 if(unresolvable != NULL) {
424 int remove_unresolvable = 0;
425 alpm_errno_t saved_err = handle->pm_errno;
426 QUESTION(handle, ALPM_QUESTION_REMOVE_PKGS, unresolvable,
427 NULL, NULL, &remove_unresolvable);
428 if(remove_unresolvable) {
429 /* User wants to remove the unresolvable packages from the
430 transaction. The packages will be removed from the actual
431 transaction when the transaction packages are replaced with a
432 dependency-reordered list below */
433 handle->pm_errno = 0;
434 if(data) {
435 alpm_list_free_inner(*data, (alpm_list_fn_free)_alpm_depmiss_free);
436 alpm_list_free(*data);
437 *data = NULL;
439 } else {
440 /* pm_errno was set by resolvedeps, callback may have overwrote it */
441 handle->pm_errno = saved_err;
442 alpm_list_free(resolved);
443 ret = -1;
444 goto cleanup;
448 /* Set DEPEND reason for pulled packages */
449 for(i = resolved; i; i = i->next) {
450 alpm_pkg_t *pkg = i->data;
451 if(!_alpm_pkg_find(trans->add, pkg->name)) {
452 pkg->reason = ALPM_PKG_REASON_DEPEND;
456 /* Unresolvable packages will be removed from the target list; set these
457 * aside in the transaction as a list we won't operate on. If we free them
458 * before the end of the transaction, we may kill pointers the frontend
459 * holds to package objects. */
460 trans->unresolvable = unresolvable;
462 /* re-order w.r.t. dependencies */
463 alpm_list_free(trans->add);
464 trans->add = _alpm_sortbydeps(handle, resolved, 0);
465 alpm_list_free(resolved);
467 EVENT(handle, ALPM_EVENT_RESOLVEDEPS_DONE, NULL, NULL);
470 if(!(trans->flags & ALPM_TRANS_FLAG_NOCONFLICTS)) {
471 /* check for inter-conflicts and whatnot */
472 EVENT(handle, ALPM_EVENT_INTERCONFLICTS_START, NULL, NULL);
474 _alpm_log(handle, ALPM_LOG_DEBUG, "looking for conflicts\n");
476 /* 1. check for conflicts in the target list */
477 _alpm_log(handle, ALPM_LOG_DEBUG, "check targets vs targets\n");
478 deps = _alpm_innerconflicts(handle, trans->add);
480 for(i = deps; i; i = i->next) {
481 alpm_conflict_t *conflict = i->data;
482 alpm_pkg_t *rsync, *sync, *sync1, *sync2;
484 /* have we already removed one of the conflicting targets? */
485 sync1 = _alpm_pkg_find(trans->add, conflict->package1);
486 sync2 = _alpm_pkg_find(trans->add, conflict->package2);
487 if(!sync1 || !sync2) {
488 continue;
491 _alpm_log(handle, ALPM_LOG_DEBUG, "conflicting packages in the sync list: '%s' <-> '%s'\n",
492 conflict->package1, conflict->package2);
494 /* if sync1 provides sync2, we remove sync2 from the targets, and vice versa */
495 alpm_depend_t *dep1 = _alpm_splitdep(conflict->package1);
496 alpm_depend_t *dep2 = _alpm_splitdep(conflict->package2);
497 if(_alpm_depcmp(sync1, dep2)) {
498 rsync = sync2;
499 sync = sync1;
500 } else if(_alpm_depcmp(sync2, dep1)) {
501 rsync = sync1;
502 sync = sync2;
503 } else {
504 _alpm_log(handle, ALPM_LOG_ERROR, _("unresolvable package conflicts detected\n"));
505 handle->pm_errno = ALPM_ERR_CONFLICTING_DEPS;
506 ret = -1;
507 if(data) {
508 alpm_conflict_t *newconflict = _alpm_conflict_dup(conflict);
509 if(newconflict) {
510 *data = alpm_list_add(*data, newconflict);
513 alpm_list_free_inner(deps, (alpm_list_fn_free)_alpm_conflict_free);
514 alpm_list_free(deps);
515 _alpm_dep_free(dep1);
516 _alpm_dep_free(dep2);
517 goto cleanup;
519 _alpm_dep_free(dep1);
520 _alpm_dep_free(dep2);
522 /* Prints warning */
523 _alpm_log(handle, ALPM_LOG_WARNING,
524 _("removing '%s' from target list because it conflicts with '%s'\n"),
525 rsync->name, sync->name);
526 trans->add = alpm_list_remove(trans->add, rsync, _alpm_pkg_cmp, NULL);
527 /* rsync is not a transaction target anymore */
528 trans->unresolvable = alpm_list_add(trans->unresolvable, rsync);
529 continue;
532 alpm_list_free_inner(deps, (alpm_list_fn_free)_alpm_conflict_free);
533 alpm_list_free(deps);
534 deps = NULL;
536 /* 2. we check for target vs db conflicts (and resolve)*/
537 _alpm_log(handle, ALPM_LOG_DEBUG, "check targets vs db and db vs targets\n");
538 deps = _alpm_outerconflicts(handle->db_local, trans->add);
540 for(i = deps; i; i = i->next) {
541 alpm_conflict_t *conflict = i->data;
543 /* if conflict->package2 (the local package) is not elected for removal,
544 we ask the user */
545 int found = 0;
546 for(j = trans->add; j && !found; j = j->next) {
547 alpm_pkg_t *spkg = j->data;
548 if(_alpm_pkg_find(spkg->removes, conflict->package2)) {
549 found = 1;
552 if(found) {
553 continue;
556 _alpm_log(handle, ALPM_LOG_DEBUG, "package '%s' conflicts with '%s'\n",
557 conflict->package1, conflict->package2);
559 alpm_pkg_t *sync = _alpm_pkg_find(trans->add, conflict->package1);
560 alpm_pkg_t *local = _alpm_db_get_pkgfromcache(handle->db_local, conflict->package2);
561 int doremove = 0;
562 QUESTION(handle, ALPM_QUESTION_CONFLICT_PKG, conflict->package1,
563 conflict->package2, conflict->reason->name, &doremove);
564 if(doremove) {
565 /* append to the removes list */
566 _alpm_log(handle, ALPM_LOG_DEBUG, "electing '%s' for removal\n", conflict->package2);
567 sync->removes = alpm_list_add(sync->removes, local);
568 } else { /* abort */
569 _alpm_log(handle, ALPM_LOG_ERROR, _("unresolvable package conflicts detected\n"));
570 handle->pm_errno = ALPM_ERR_CONFLICTING_DEPS;
571 ret = -1;
572 if(data) {
573 alpm_conflict_t *newconflict = _alpm_conflict_dup(conflict);
574 if(newconflict) {
575 *data = alpm_list_add(*data, newconflict);
578 alpm_list_free_inner(deps, (alpm_list_fn_free)_alpm_conflict_free);
579 alpm_list_free(deps);
580 goto cleanup;
583 EVENT(handle, ALPM_EVENT_INTERCONFLICTS_DONE, NULL, NULL);
584 alpm_list_free_inner(deps, (alpm_list_fn_free)_alpm_conflict_free);
585 alpm_list_free(deps);
588 /* Build trans->remove list */
589 for(i = trans->add; i; i = i->next) {
590 alpm_pkg_t *spkg = i->data;
591 for(j = spkg->removes; j; j = j->next) {
592 alpm_pkg_t *rpkg = j->data;
593 if(!_alpm_pkg_find(trans->remove, rpkg->name)) {
594 alpm_pkg_t *copy;
595 _alpm_log(handle, ALPM_LOG_DEBUG, "adding '%s' to remove list\n", rpkg->name);
596 if(_alpm_pkg_dup(rpkg, &copy) == -1) {
597 return -1;
599 trans->remove = alpm_list_add(trans->remove, copy);
604 if(!(trans->flags & ALPM_TRANS_FLAG_NODEPS)) {
605 _alpm_log(handle, ALPM_LOG_DEBUG, "checking dependencies\n");
606 deps = alpm_checkdeps(handle, _alpm_db_get_pkgcache(handle->db_local),
607 trans->remove, trans->add, 1);
608 if(deps) {
609 handle->pm_errno = ALPM_ERR_UNSATISFIED_DEPS;
610 ret = -1;
611 if(data) {
612 *data = deps;
613 } else {
614 alpm_list_free_inner(deps, (alpm_list_fn_free)_alpm_depmiss_free);
615 alpm_list_free(deps);
617 goto cleanup;
620 for(i = trans->add; i; i = i->next) {
621 /* update download size field */
622 alpm_pkg_t *spkg = i->data;
623 if(compute_download_size(spkg) < 0) {
624 ret = -1;
625 goto cleanup;
629 cleanup:
630 return ret;
633 /** Returns the size of the files that will be downloaded to install a
634 * package.
635 * @param newpkg the new package to upgrade to
636 * @return the size of the download
638 off_t SYMEXPORT alpm_pkg_download_size(alpm_pkg_t *newpkg)
640 if(!(newpkg->infolevel & INFRQ_DSIZE)) {
641 compute_download_size(newpkg);
643 return newpkg->download_size;
646 static int endswith(const char *filename, const char *extension)
648 const char *s = filename + strlen(filename) - strlen(extension);
649 return strcmp(s, extension) == 0;
652 /** Applies delta files to create an upgraded package file.
654 * All intermediate files are deleted, leaving only the starting and
655 * ending package files.
657 * @param handle the context handle
659 * @return 0 if all delta files were able to be applied, 1 otherwise.
661 static int apply_deltas(alpm_handle_t *handle)
663 alpm_list_t *i;
664 int deltas_found = 0, ret = 0;
665 const char *cachedir = _alpm_filecache_setup(handle);
666 alpm_trans_t *trans = handle->trans;
668 for(i = trans->add; i; i = i->next) {
669 alpm_pkg_t *spkg = i->data;
670 alpm_list_t *delta_path = spkg->delta_path;
671 alpm_list_t *dlts = NULL;
673 if(!delta_path) {
674 continue;
677 if(!deltas_found) {
678 /* only show this if we actually have deltas to apply, and it is before
679 * the very first one */
680 EVENT(handle, ALPM_EVENT_DELTA_PATCHES_START, NULL, NULL);
681 deltas_found = 1;
684 for(dlts = delta_path; dlts; dlts = dlts->next) {
685 alpm_delta_t *d = dlts->data;
686 char *delta, *from, *to;
687 char command[PATH_MAX];
688 size_t len = 0;
690 delta = _alpm_filecache_find(handle, d->delta);
691 /* the initial package might be in a different cachedir */
692 if(dlts == delta_path) {
693 from = _alpm_filecache_find(handle, d->from);
694 } else {
695 /* len = cachedir len + from len + '/' + null */
696 len = strlen(cachedir) + strlen(d->from) + 2;
697 MALLOC(from, len, RET_ERR(handle, ALPM_ERR_MEMORY, 1));
698 snprintf(from, len, "%s/%s", cachedir, d->from);
700 len = strlen(cachedir) + strlen(d->to) + 2;
701 MALLOC(to, len, RET_ERR(handle, ALPM_ERR_MEMORY, 1));
702 snprintf(to, len, "%s/%s", cachedir, d->to);
704 /* build the patch command */
705 if(endswith(to, ".gz")) {
706 /* special handling for gzip : we disable timestamp with -n option */
707 snprintf(command, PATH_MAX, "xdelta3 -d -q -R -c -s %s %s | gzip -n > %s", from, delta, to);
708 } else {
709 snprintf(command, PATH_MAX, "xdelta3 -d -q -s %s %s %s", from, delta, to);
712 _alpm_log(handle, ALPM_LOG_DEBUG, "command: %s\n", command);
714 EVENT(handle, ALPM_EVENT_DELTA_PATCH_START, d->to, d->delta);
716 int retval = system(command);
717 if(retval == 0) {
718 EVENT(handle, ALPM_EVENT_DELTA_PATCH_DONE, NULL, NULL);
720 /* delete the delta file */
721 unlink(delta);
723 /* Delete the 'from' package but only if it is an intermediate
724 * package. The starting 'from' package should be kept, just
725 * as if deltas were not used. */
726 if(dlts != delta_path) {
727 unlink(from);
730 FREE(from);
731 FREE(to);
732 FREE(delta);
734 if(retval != 0) {
735 /* one delta failed for this package, cancel the remaining ones */
736 EVENT(handle, ALPM_EVENT_DELTA_PATCH_FAILED, NULL, NULL);
737 handle->pm_errno = ALPM_ERR_DLT_PATCHFAILED;
738 ret = 1;
739 break;
743 if(deltas_found) {
744 EVENT(handle, ALPM_EVENT_DELTA_PATCHES_DONE, NULL, NULL);
747 return ret;
751 * Prompts to delete the file now that we know it is invalid.
752 * @param handle the context handle
753 * @param filename the absolute path of the file to test
754 * @param reason an error code indicating the reason for package invalidity
756 * @return 1 if file was removed, 0 otherwise
758 static int prompt_to_delete(alpm_handle_t *handle, const char *filepath,
759 alpm_errno_t reason)
761 int doremove = 0;
762 QUESTION(handle, ALPM_QUESTION_CORRUPTED_PKG, (char *)filepath,
763 &reason, NULL, &doremove);
764 if(doremove) {
765 unlink(filepath);
767 return doremove;
770 static int validate_deltas(alpm_handle_t *handle, alpm_list_t *deltas)
772 alpm_list_t *i, *errors = NULL;
774 if(!deltas) {
775 return 0;
778 /* Check integrity of deltas */
779 EVENT(handle, ALPM_EVENT_DELTA_INTEGRITY_START, NULL, NULL);
780 for(i = deltas; i; i = i->next) {
781 alpm_delta_t *d = i->data;
782 char *filepath = _alpm_filecache_find(handle, d->delta);
784 if(_alpm_test_checksum(filepath, d->delta_md5, ALPM_PKG_VALIDATION_MD5SUM)) {
785 errors = alpm_list_add(errors, filepath);
786 } else {
787 FREE(filepath);
790 EVENT(handle, ALPM_EVENT_DELTA_INTEGRITY_DONE, NULL, NULL);
792 if(errors) {
793 for(i = errors; i; i = i->next) {
794 char *filepath = i->data;
795 prompt_to_delete(handle, filepath, ALPM_ERR_DLT_INVALID);
796 FREE(filepath);
798 alpm_list_free(errors);
799 handle->pm_errno = ALPM_ERR_DLT_INVALID;
800 return -1;
802 return 0;
805 static struct dload_payload *build_payload(alpm_handle_t *handle,
806 const char *filename, size_t size, alpm_list_t *servers)
808 struct dload_payload *payload;
810 CALLOC(payload, 1, sizeof(*payload), RET_ERR(handle, ALPM_ERR_MEMORY, NULL));
811 STRDUP(payload->remote_name, filename, RET_ERR(handle, ALPM_ERR_MEMORY, NULL));
812 payload->max_size = size;
813 payload->servers = servers;
814 return payload;
817 static int find_dl_candidates(alpm_db_t *repo, alpm_list_t **files, alpm_list_t **deltas)
819 alpm_list_t *i;
820 alpm_handle_t *handle = repo->handle;
822 for(i = handle->trans->add; i; i = i->next) {
823 alpm_pkg_t *spkg = i->data;
825 if(spkg->origin != ALPM_PKG_FROM_FILE && repo == spkg->origin_data.db) {
826 alpm_list_t *delta_path = spkg->delta_path;
828 if(!repo->servers) {
829 handle->pm_errno = ALPM_ERR_SERVER_NONE;
830 _alpm_log(handle, ALPM_LOG_ERROR, "%s: %s\n",
831 alpm_strerror(handle->pm_errno), repo->treename);
832 return 1;
835 if(delta_path) {
836 /* using deltas */
837 alpm_list_t *dlts;
838 for(dlts = delta_path; dlts; dlts = dlts->next) {
839 alpm_delta_t *delta = dlts->data;
840 if(delta->download_size != 0) {
841 struct dload_payload *payload = build_payload(
842 handle, delta->delta, delta->delta_size, repo->servers);
843 ASSERT(payload, return -1);
844 *files = alpm_list_add(*files, payload);
846 /* keep a list of all the delta files for md5sums */
847 *deltas = alpm_list_add(*deltas, delta);
850 } else if(spkg->download_size != 0) {
851 struct dload_payload *payload;
852 ASSERT(spkg->filename != NULL, RET_ERR(handle, ALPM_ERR_PKG_INVALID_NAME, -1));
853 payload = build_payload(handle, spkg->filename, spkg->size, repo->servers);
854 ASSERT(payload, return -1);
855 *files = alpm_list_add(*files, payload);
860 return 0;
863 static int download_single_file(alpm_handle_t *handle, struct dload_payload *payload,
864 const char *cachedir)
866 const alpm_list_t *server;
868 payload->handle = handle;
869 payload->allow_resume = 1;
871 for(server = payload->servers; server; server = server->next) {
872 const char *server_url = server->data;
873 size_t len;
875 /* print server + filename into a buffer */
876 len = strlen(server_url) + strlen(payload->remote_name) + 2;
877 MALLOC(payload->fileurl, len, RET_ERR(handle, ALPM_ERR_MEMORY, -1));
878 snprintf(payload->fileurl, len, "%s/%s", server_url, payload->remote_name);
880 if(_alpm_download(payload, cachedir, NULL) != -1) {
881 return 0;
884 FREE(payload->fileurl);
885 payload->unlink_on_fail = 0;
888 return -1;
891 static int download_files(alpm_handle_t *handle, alpm_list_t **deltas)
893 const char *cachedir;
894 alpm_list_t *i, *files = NULL;
895 int errors = 0;
897 cachedir = _alpm_filecache_setup(handle);
898 handle->trans->state = STATE_DOWNLOADING;
900 /* Total progress - figure out the total download size if required to
901 * pass to the callback. This function is called once, and it is up to the
902 * frontend to compute incremental progress. */
903 if(handle->totaldlcb) {
904 off_t total_size = (off_t)0;
905 /* sum up the download size for each package and store total */
906 for(i = handle->trans->add; i; i = i->next) {
907 alpm_pkg_t *spkg = i->data;
908 total_size += spkg->download_size;
910 handle->totaldlcb(total_size);
913 for(i = handle->dbs_sync; i; i = i->next) {
914 errors += find_dl_candidates(i->data, &files, deltas);
917 if(files) {
918 /* check for necessary disk space for download */
919 if(handle->checkspace) {
920 off_t *file_sizes;
921 size_t idx, num_files;
922 int ret;
924 _alpm_log(handle, ALPM_LOG_DEBUG, "checking available disk space for download\n");
926 num_files = alpm_list_count(files);
927 CALLOC(file_sizes, num_files, sizeof(off_t), goto finish);
929 for(i = files, idx = 0; i; i = i->next, idx++) {
930 const struct dload_payload *payload = i->data;
931 file_sizes[idx] = payload->max_size;
934 ret = _alpm_check_downloadspace(handle, cachedir, num_files, file_sizes);
935 free(file_sizes);
937 if(ret != 0) {
938 errors++;
939 goto finish;
943 EVENT(handle, ALPM_EVENT_RETRIEVE_START, NULL, NULL);
944 for(i = files; i; i = i->next) {
945 if(download_single_file(handle, i->data, cachedir) == -1) {
946 errors++;
947 _alpm_log(handle, ALPM_LOG_WARNING, _("failed to retrieve some files\n"));
952 finish:
953 if(files) {
954 alpm_list_free_inner(files, (alpm_list_fn_free)_alpm_dload_payload_reset);
955 FREELIST(files);
958 for(i = handle->trans->add; i; i = i->next) {
959 alpm_pkg_t *pkg = i->data;
960 pkg->infolevel &= ~INFRQ_DSIZE;
961 pkg->download_size = 0;
964 /* clear out value to let callback know we are done */
965 if(handle->totaldlcb) {
966 handle->totaldlcb(0);
969 return errors;
972 static int check_validity(alpm_handle_t *handle,
973 size_t total, size_t total_bytes)
975 struct validity {
976 alpm_pkg_t *pkg;
977 char *path;
978 alpm_siglist_t *siglist;
979 alpm_siglevel_t level;
980 alpm_pkgvalidation_t validation;
981 alpm_errno_t error;
983 size_t current = 0, current_bytes = 0;
984 alpm_list_t *i, *errors = NULL;
986 /* Check integrity of packages */
987 EVENT(handle, ALPM_EVENT_INTEGRITY_START, NULL, NULL);
989 for(i = handle->trans->add; i; i = i->next, current++) {
990 struct validity v = { i->data, NULL, NULL, 0, 0, 0 };
991 int percent = (int)(((double)current_bytes / total_bytes) * 100);
993 PROGRESS(handle, ALPM_PROGRESS_INTEGRITY_START, "", percent,
994 total, current);
995 if(v.pkg->origin == ALPM_PKG_FROM_FILE) {
996 continue; /* pkg_load() has been already called, this package is valid */
999 current_bytes += v.pkg->size;
1000 v.path = _alpm_filecache_find(handle, v.pkg->filename);
1001 v.level = alpm_db_get_siglevel(alpm_pkg_get_db(v.pkg));
1003 if(_alpm_pkg_validate_internal(handle, v.path, v.pkg,
1004 v.level, &v.siglist, &v.validation) == -1) {
1005 v.error = handle->pm_errno;
1006 struct validity *invalid = malloc(sizeof(struct validity));
1007 memcpy(invalid, &v, sizeof(struct validity));
1008 errors = alpm_list_add(errors, invalid);
1009 } else {
1010 alpm_siglist_cleanup(v.siglist);
1011 free(v.siglist);
1012 free(v.path);
1013 v.pkg->validation = v.validation;
1017 PROGRESS(handle, ALPM_PROGRESS_INTEGRITY_START, "", 100,
1018 total, current);
1019 EVENT(handle, ALPM_EVENT_INTEGRITY_DONE, NULL, NULL);
1021 if(errors) {
1022 int tryagain = 0;
1023 for(i = errors; i; i = i->next) {
1024 struct validity *v = i->data;
1025 if(v->error == ALPM_ERR_PKG_INVALID_SIG) {
1026 int retry = _alpm_process_siglist(handle, v->pkg->name, v->siglist,
1027 v->level & ALPM_SIG_PACKAGE_OPTIONAL,
1028 v->level & ALPM_SIG_PACKAGE_MARGINAL_OK,
1029 v->level & ALPM_SIG_PACKAGE_UNKNOWN_OK);
1030 tryagain += retry;
1031 } else if(v->error == ALPM_ERR_PKG_INVALID_CHECKSUM) {
1032 prompt_to_delete(handle, v->path, v->error);
1034 alpm_siglist_cleanup(v->siglist);
1035 free(v->siglist);
1036 free(v->path);
1037 free(v);
1039 alpm_list_free(errors);
1041 if(tryagain == 0) {
1042 if(!handle->pm_errno) {
1043 RET_ERR(handle, ALPM_ERR_PKG_INVALID, -1);
1045 return -1;
1047 /* we were told at least once we can try again */
1048 return 1;
1051 return 0;
1054 static int load_packages(alpm_handle_t *handle, alpm_list_t **data,
1055 size_t total, size_t total_bytes)
1057 size_t current = 0, current_bytes = 0;
1058 int errors = 0;
1059 alpm_list_t *i;
1061 /* load packages from disk now that they are known-valid */
1062 EVENT(handle, ALPM_EVENT_LOAD_START, NULL, NULL);
1064 for(i = handle->trans->add; i; i = i->next, current++) {
1065 alpm_pkg_t *spkg = i->data;
1066 char *filepath;
1067 int percent = (int)(((double)current_bytes / total_bytes) * 100);
1069 PROGRESS(handle, ALPM_PROGRESS_LOAD_START, "", percent,
1070 total, current);
1071 if(spkg->origin == ALPM_PKG_FROM_FILE) {
1072 continue; /* pkg_load() has been already called, this package is valid */
1075 current_bytes += spkg->size;
1076 filepath = _alpm_filecache_find(handle, spkg->filename);
1078 /* load the package file and replace pkgcache entry with it in the target list */
1079 /* TODO: alpm_pkg_get_db() will not work on this target anymore */
1080 _alpm_log(handle, ALPM_LOG_DEBUG,
1081 "replacing pkgcache entry with package file for target %s\n",
1082 spkg->name);
1083 alpm_pkg_t *pkgfile =_alpm_pkg_load_internal(handle, filepath, 1);
1084 if(!pkgfile) {
1085 errors++;
1086 *data = alpm_list_add(*data, strdup(spkg->filename));
1087 free(filepath);
1088 continue;
1090 free(filepath);
1091 /* copy over the install reason */
1092 pkgfile->reason = spkg->reason;
1093 /* copy over validation method */
1094 pkgfile->validation = spkg->validation;
1095 i->data = pkgfile;
1096 /* spkg has been removed from the target list, so we can free the
1097 * sync-specific fields */
1098 _alpm_pkg_free_trans(spkg);
1101 PROGRESS(handle, ALPM_PROGRESS_LOAD_START, "", 100,
1102 total, current);
1103 EVENT(handle, ALPM_EVENT_LOAD_DONE, NULL, NULL);
1105 if(errors) {
1106 if(!handle->pm_errno) {
1107 RET_ERR(handle, ALPM_ERR_PKG_INVALID, -1);
1109 return -1;
1112 return 0;
1115 int _alpm_sync_commit(alpm_handle_t *handle, alpm_list_t **data)
1117 alpm_list_t *i, *deltas = NULL;
1118 size_t total = 0, total_bytes = 0;
1119 alpm_trans_t *trans = handle->trans;
1121 if(download_files(handle, &deltas)) {
1122 alpm_list_free(deltas);
1123 return -1;
1126 if(validate_deltas(handle, deltas)) {
1127 alpm_list_free(deltas);
1128 return -1;
1130 alpm_list_free(deltas);
1132 /* Use the deltas to generate the packages */
1133 if(apply_deltas(handle)) {
1134 return -1;
1137 /* get the total size of all packages so we can adjust the progress bar more
1138 * realistically if there are small and huge packages involved */
1139 for(i = trans->add; i; i = i->next) {
1140 alpm_pkg_t *spkg = i->data;
1141 if(spkg->origin != ALPM_PKG_FROM_FILE) {
1142 total_bytes += spkg->size;
1144 total++;
1146 /* this can only happen maliciously */
1147 total_bytes = total_bytes ? total_bytes : 1;
1149 /* this one is special: -1 is failure, 1 is retry, 0 is success */
1150 while(1) {
1151 int ret = check_validity(handle, total, total_bytes);
1152 if(ret == 0) {
1153 break;
1154 } else if(ret < 0) {
1155 return -1;
1159 if(trans->flags & ALPM_TRANS_FLAG_DOWNLOADONLY) {
1160 return 0;
1163 if(load_packages(handle, data, total, total_bytes)) {
1164 return -1;
1167 trans->state = STATE_COMMITING;
1169 /* fileconflict check */
1170 if(!(trans->flags & (ALPM_TRANS_FLAG_FORCE|ALPM_TRANS_FLAG_DBONLY))) {
1171 EVENT(handle, ALPM_EVENT_FILECONFLICTS_START, NULL, NULL);
1173 _alpm_log(handle, ALPM_LOG_DEBUG, "looking for file conflicts\n");
1174 alpm_list_t *conflict = _alpm_db_find_fileconflicts(handle,
1175 trans->add, trans->remove);
1176 if(conflict) {
1177 if(data) {
1178 *data = conflict;
1179 } else {
1180 alpm_list_free_inner(conflict, (alpm_list_fn_free)_alpm_fileconflict_free);
1181 alpm_list_free(conflict);
1183 RET_ERR(handle, ALPM_ERR_FILE_CONFLICTS, -1);
1186 EVENT(handle, ALPM_EVENT_FILECONFLICTS_DONE, NULL, NULL);
1189 /* check available disk space */
1190 if(handle->checkspace && !(trans->flags & ALPM_TRANS_FLAG_DBONLY)) {
1191 EVENT(handle, ALPM_EVENT_DISKSPACE_START, NULL, NULL);
1193 _alpm_log(handle, ALPM_LOG_DEBUG, "checking available disk space\n");
1194 if(_alpm_check_diskspace(handle) == -1) {
1195 _alpm_log(handle, ALPM_LOG_ERROR, _("not enough free disk space\n"));
1196 return -1;
1199 EVENT(handle, ALPM_EVENT_DISKSPACE_DONE, NULL, NULL);
1202 /* remove conflicting and to-be-replaced packages */
1203 if(trans->remove) {
1204 _alpm_log(handle, ALPM_LOG_DEBUG, "removing conflicting and to-be-replaced packages\n");
1205 /* we want the frontend to be aware of commit details */
1206 if(_alpm_remove_packages(handle, 0) == -1) {
1207 _alpm_log(handle, ALPM_LOG_ERROR, _("could not commit removal transaction\n"));
1208 return -1;
1212 /* install targets */
1213 _alpm_log(handle, ALPM_LOG_DEBUG, "installing packages\n");
1214 if(_alpm_upgrade_packages(handle) == -1) {
1215 _alpm_log(handle, ALPM_LOG_ERROR, _("could not commit transaction\n"));
1216 return -1;
1219 return 0;
1222 /* vim: set ts=2 sw=2 noet: */