Remove global handle dependencies from sync/upgrade paths
[pacman-ng.git] / lib / libalpm / sync.c
blob7a33bc972fe064bd774df0571838f46e0e690422
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 /* global handle variable */
54 extern pmhandle_t *handle;
56 /** Check for new version of pkg in sync repos
57 * (only the first occurrence is considered in sync)
59 pmpkg_t SYMEXPORT *alpm_sync_newversion(pmpkg_t *pkg, alpm_list_t *dbs_sync)
61 ASSERT(pkg != NULL, return NULL);
63 alpm_list_t *i;
64 pmpkg_t *spkg = NULL;
66 for(i = dbs_sync; !spkg && i; i = i->next) {
67 spkg = _alpm_db_get_pkgfromcache(i->data, alpm_pkg_get_name(pkg));
70 if(spkg == NULL) {
71 _alpm_log(PM_LOG_DEBUG, "'%s' not found in sync db => no upgrade\n",
72 alpm_pkg_get_name(pkg));
73 return NULL;
76 /* compare versions and see if spkg is an upgrade */
77 if(_alpm_pkg_compare_versions(spkg, pkg) > 0) {
78 _alpm_log(PM_LOG_DEBUG, "new version of '%s' found (%s => %s)\n",
79 alpm_pkg_get_name(pkg), alpm_pkg_get_version(pkg),
80 alpm_pkg_get_version(spkg));
81 return spkg;
83 /* spkg is not an upgrade */
84 return NULL;
87 /** Search for packages to upgrade and add them to the transaction. */
88 int SYMEXPORT alpm_sync_sysupgrade(int enable_downgrade)
90 alpm_list_t *i, *j, *k;
91 pmtrans_t *trans;
92 pmdb_t *db_local;
93 alpm_list_t *dbs_sync;
95 ASSERT(handle != NULL, RET_ERR(PM_ERR_HANDLE_NULL, -1));
96 trans = handle->trans;
97 db_local = handle->db_local;
98 dbs_sync = handle->dbs_sync;
99 ASSERT(trans != NULL, RET_ERR(PM_ERR_TRANS_NULL, -1));
100 ASSERT(trans->state == STATE_INITIALIZED, RET_ERR(PM_ERR_TRANS_NOT_INITIALIZED, -1));
102 _alpm_log(PM_LOG_DEBUG, "checking for package upgrades\n");
103 for(i = _alpm_db_get_pkgcache(db_local); i; i = i->next) {
104 pmpkg_t *lpkg = i->data;
106 if(_alpm_pkg_find(trans->add, lpkg->name)) {
107 _alpm_log(PM_LOG_DEBUG, "%s is already in the target list -- skipping\n", lpkg->name);
108 continue;
111 /* Search for literal then replacers in each sync database.
112 * If found, don't check other databases */
113 for(j = dbs_sync; j; j = j->next) {
114 pmdb_t *sdb = j->data;
115 /* Check sdb */
116 pmpkg_t *spkg = _alpm_db_get_pkgfromcache(sdb, lpkg->name);
117 if(spkg) {
118 /* 1. literal was found in sdb */
119 int cmp = _alpm_pkg_compare_versions(spkg, lpkg);
120 if(cmp > 0) {
121 _alpm_log(PM_LOG_DEBUG, "new version of '%s' found (%s => %s)\n",
122 lpkg->name, lpkg->version, spkg->version);
123 /* check IgnorePkg/IgnoreGroup */
124 if(_alpm_pkg_should_ignore(spkg) || _alpm_pkg_should_ignore(lpkg)) {
125 _alpm_log(PM_LOG_WARNING, _("%s: ignoring package upgrade (%s => %s)\n"),
126 lpkg->name, lpkg->version, spkg->version);
127 } else {
128 _alpm_log(PM_LOG_DEBUG, "adding package %s-%s to the transaction targets\n",
129 spkg->name, spkg->version);
130 trans->add = alpm_list_add(trans->add, spkg);
132 } else if(cmp < 0) {
133 if(enable_downgrade) {
134 /* check IgnorePkg/IgnoreGroup */
135 if(_alpm_pkg_should_ignore(spkg) || _alpm_pkg_should_ignore(lpkg)) {
136 _alpm_log(PM_LOG_WARNING, _("%s: ignoring package downgrade (%s => %s)\n"),
137 lpkg->name, lpkg->version, spkg->version);
138 } else {
139 _alpm_log(PM_LOG_WARNING, _("%s: downgrading from version %s to version %s\n"),
140 lpkg->name, lpkg->version, spkg->version);
141 trans->add = alpm_list_add(trans->add, spkg);
143 } else {
144 _alpm_log(PM_LOG_WARNING, _("%s: local (%s) is newer than %s (%s)\n"),
145 lpkg->name, lpkg->version, sdb->treename, spkg->version);
148 /* jump to next local package */
149 break;
150 } else {
151 /* 2. search for replacers in sdb */
152 int found = 0;
153 for(k = _alpm_db_get_pkgcache(sdb); k; k = k->next) {
154 spkg = k->data;
155 if(alpm_list_find_str(alpm_pkg_get_replaces(spkg), lpkg->name)) {
156 found = 1;
157 /* check IgnorePkg/IgnoreGroup */
158 if(_alpm_pkg_should_ignore(spkg) || _alpm_pkg_should_ignore(lpkg)) {
159 _alpm_log(PM_LOG_WARNING, _("ignoring package replacement (%s-%s => %s-%s)\n"),
160 lpkg->name, lpkg->version, spkg->name, spkg->version);
161 continue;
164 int doreplace = 0;
165 QUESTION(trans, PM_TRANS_CONV_REPLACE_PKG, lpkg, spkg, sdb->treename, &doreplace);
166 if(!doreplace) {
167 continue;
170 /* If spkg is already in the target list, we append lpkg to spkg's
171 * removes list */
172 pmpkg_t *tpkg = _alpm_pkg_find(trans->add, spkg->name);
173 if(tpkg) {
174 /* sanity check, multiple repos can contain spkg->name */
175 if(tpkg->origin_data.db != sdb) {
176 _alpm_log(PM_LOG_WARNING, _("cannot replace %s by %s\n"),
177 lpkg->name, spkg->name);
178 continue;
180 _alpm_log(PM_LOG_DEBUG, "appending %s to the removes list of %s\n",
181 lpkg->name, tpkg->name);
182 tpkg->removes = alpm_list_add(tpkg->removes, lpkg);
183 /* check the to-be-replaced package's reason field */
184 if(alpm_pkg_get_reason(lpkg) == PM_PKG_REASON_EXPLICIT) {
185 tpkg->reason = PM_PKG_REASON_EXPLICIT;
187 } else {
188 /* add spkg to the target list */
189 /* copy over reason */
190 spkg->reason = alpm_pkg_get_reason(lpkg);
191 spkg->removes = alpm_list_add(NULL, lpkg);
192 _alpm_log(PM_LOG_DEBUG, "adding package %s-%s to the transaction targets\n",
193 spkg->name, spkg->version);
194 trans->add = alpm_list_add(trans->add, spkg);
198 if(found) {
199 break; /* jump to next local package */
205 return 0;
208 /** Find group members across a list of databases.
209 * If a member exists in several databases, only the first database is used.
210 * IgnorePkg is also handled.
211 * @param dbs the list of pmdb_t *
212 * @pram name the name of the group
213 * @return the list of pmpkg_t * (caller is responsible for alpm_list_free)
215 alpm_list_t SYMEXPORT *alpm_find_grp_pkgs(alpm_list_t *dbs,
216 const char *name)
218 alpm_list_t *i, *j, *pkgs = NULL, *ignorelist = NULL;
220 for(i = dbs; i; i = i->next) {
221 pmdb_t *db = i->data;
222 pmgrp_t *grp = alpm_db_readgrp(db, name);
224 if(!grp)
225 continue;
227 for(j = alpm_grp_get_pkgs(grp); j; j = j->next) {
228 pmpkg_t *pkg = j->data;
230 if(_alpm_pkg_find(ignorelist, alpm_pkg_get_name(pkg))) {
231 continue;
233 if(_alpm_pkg_should_ignore(pkg)) {
234 ignorelist = alpm_list_add(ignorelist, pkg);
235 int install = 0;
236 QUESTION(db->handle->trans, PM_TRANS_CONV_INSTALL_IGNOREPKG, pkg,
237 NULL, NULL, &install);
238 if(!install)
239 continue;
241 if(!_alpm_pkg_find(pkgs, alpm_pkg_get_name(pkg))) {
242 pkgs = alpm_list_add(pkgs, pkg);
246 alpm_list_free(ignorelist);
247 return pkgs;
250 /** Compute the size of the files that will be downloaded to install a
251 * package.
252 * @param newpkg the new package to upgrade to
254 static int compute_download_size(pmpkg_t *newpkg)
256 const char *fname;
257 char *fpath;
258 off_t size = 0;
260 if(newpkg->origin != PKG_FROM_SYNCDB) {
261 newpkg->infolevel |= INFRQ_DSIZE;
262 newpkg->download_size = 0;
263 return 0;
266 fname = alpm_pkg_get_filename(newpkg);
267 ASSERT(fname != NULL, RET_ERR(PM_ERR_PKG_INVALID_NAME, -1));
268 fpath = _alpm_filecache_find(fname);
270 if(fpath) {
271 FREE(fpath);
272 size = 0;
273 } else if(newpkg->handle->usedelta) {
274 off_t dltsize;
275 off_t pkgsize = alpm_pkg_get_size(newpkg);
277 dltsize = _alpm_shortest_delta_path(
278 alpm_pkg_get_deltas(newpkg),
279 alpm_pkg_get_filename(newpkg),
280 &newpkg->delta_path);
282 if(newpkg->delta_path && (dltsize < pkgsize * MAX_DELTA_RATIO)) {
283 _alpm_log(PM_LOG_DEBUG, "using delta size\n");
284 size = dltsize;
285 } else {
286 _alpm_log(PM_LOG_DEBUG, "using package size\n");
287 size = alpm_pkg_get_size(newpkg);
288 alpm_list_free(newpkg->delta_path);
289 newpkg->delta_path = NULL;
291 } else {
292 size = alpm_pkg_get_size(newpkg);
295 _alpm_log(PM_LOG_DEBUG, "setting download size %jd for pkg %s\n",
296 (intmax_t)size, alpm_pkg_get_name(newpkg));
298 newpkg->infolevel |= INFRQ_DSIZE;
299 newpkg->download_size = size;
300 return 0;
303 int _alpm_sync_prepare(pmhandle_t *handle, alpm_list_t **data)
305 alpm_list_t *deps = NULL;
306 alpm_list_t *unresolvable = NULL;
307 alpm_list_t *i, *j;
308 alpm_list_t *remove = NULL;
309 int ret = 0;
310 pmtrans_t *trans = handle->trans;
311 pmdb_t *db_local = handle->db_local;
313 if(data) {
314 *data = NULL;
317 if(!(trans->flags & PM_TRANS_FLAG_NODEPS)) {
318 alpm_list_t *resolved = NULL; /* target list after resolvedeps */
320 /* Build up list by repeatedly resolving each transaction package */
321 /* Resolve targets dependencies */
322 EVENT(trans, PM_TRANS_EVT_RESOLVEDEPS_START, NULL, NULL);
323 _alpm_log(PM_LOG_DEBUG, "resolving target's dependencies\n");
325 /* build remove list for resolvedeps */
326 for(i = trans->add; i; i = i->next) {
327 pmpkg_t *spkg = i->data;
328 for(j = spkg->removes; j; j = j->next) {
329 remove = alpm_list_add(remove, j->data);
333 /* Compute the fake local database for resolvedeps (partial fix for the
334 * phonon/qt issue) */
335 alpm_list_t *localpkgs = alpm_list_diff(_alpm_db_get_pkgcache(db_local),
336 trans->add, _alpm_pkg_cmp);
338 /* Resolve packages in the transaction one at a time, in addition
339 building up a list of packages which could not be resolved. */
340 for(i = trans->add; i; i = i->next) {
341 pmpkg_t *pkg = i->data;
342 if(_alpm_resolvedeps(localpkgs, handle->dbs_sync, pkg, trans->add,
343 &resolved, remove, data) == -1) {
344 unresolvable = alpm_list_add(unresolvable, pkg);
346 /* Else, [resolved] now additionally contains [pkg] and all of its
347 dependencies not already on the list */
349 alpm_list_free(localpkgs);
351 /* If there were unresolvable top-level packages, prompt the user to
352 see if they'd like to ignore them rather than failing the sync */
353 if(unresolvable != NULL) {
354 int remove_unresolvable = 0;
355 QUESTION(handle->trans, PM_TRANS_CONV_REMOVE_PKGS, unresolvable,
356 NULL, NULL, &remove_unresolvable);
357 if(remove_unresolvable) {
358 /* User wants to remove the unresolvable packages from the
359 transaction. The packages will be removed from the actual
360 transaction when the transaction packages are replaced with a
361 dependency-reordered list below */
362 pm_errno = 0; /* pm_errno was set by resolvedeps */
363 if(data) {
364 alpm_list_free_inner(*data, (alpm_list_fn_free)_alpm_depmiss_free);
365 alpm_list_free(*data);
366 *data = NULL;
368 } else {
369 /* pm_errno is set by resolvedeps */
370 alpm_list_free(resolved);
371 ret = -1;
372 goto cleanup;
376 /* Set DEPEND reason for pulled packages */
377 for(i = resolved; i; i = i->next) {
378 pmpkg_t *pkg = i->data;
379 if(!_alpm_pkg_find(trans->add, pkg->name)) {
380 pkg->reason = PM_PKG_REASON_DEPEND;
384 /* Unresolvable packages will be removed from the target list, so
385 we free the transaction specific fields */
386 alpm_list_free_inner(unresolvable, (alpm_list_fn_free)_alpm_pkg_free_trans);
388 /* re-order w.r.t. dependencies */
389 alpm_list_free(trans->add);
390 trans->add = _alpm_sortbydeps(resolved, 0);
391 alpm_list_free(resolved);
393 EVENT(trans, PM_TRANS_EVT_RESOLVEDEPS_DONE, NULL, NULL);
396 if(!(trans->flags & PM_TRANS_FLAG_NOCONFLICTS)) {
397 /* check for inter-conflicts and whatnot */
398 EVENT(trans, PM_TRANS_EVT_INTERCONFLICTS_START, NULL, NULL);
400 _alpm_log(PM_LOG_DEBUG, "looking for conflicts\n");
402 /* 1. check for conflicts in the target list */
403 _alpm_log(PM_LOG_DEBUG, "check targets vs targets\n");
404 deps = _alpm_innerconflicts(trans->add);
406 for(i = deps; i; i = i->next) {
407 pmconflict_t *conflict = i->data;
408 pmpkg_t *rsync, *sync, *sync1, *sync2;
410 /* have we already removed one of the conflicting targets? */
411 sync1 = _alpm_pkg_find(trans->add, conflict->package1);
412 sync2 = _alpm_pkg_find(trans->add, conflict->package2);
413 if(!sync1 || !sync2) {
414 continue;
417 _alpm_log(PM_LOG_DEBUG, "conflicting packages in the sync list: '%s' <-> '%s'\n",
418 conflict->package1, conflict->package2);
420 /* if sync1 provides sync2, we remove sync2 from the targets, and vice versa */
421 pmdepend_t *dep1 = _alpm_splitdep(conflict->package1);
422 pmdepend_t *dep2 = _alpm_splitdep(conflict->package2);
423 if(_alpm_depcmp(sync1, dep2)) {
424 rsync = sync2;
425 sync = sync1;
426 } else if(_alpm_depcmp(sync2, dep1)) {
427 rsync = sync1;
428 sync = sync2;
429 } else {
430 _alpm_log(PM_LOG_ERROR, _("unresolvable package conflicts detected\n"));
431 pm_errno = PM_ERR_CONFLICTING_DEPS;
432 ret = -1;
433 if(data) {
434 pmconflict_t *newconflict = _alpm_conflict_dup(conflict);
435 if(newconflict) {
436 *data = alpm_list_add(*data, newconflict);
439 alpm_list_free_inner(deps, (alpm_list_fn_free)_alpm_conflict_free);
440 alpm_list_free(deps);
441 _alpm_dep_free(dep1);
442 _alpm_dep_free(dep2);
443 goto cleanup;
445 _alpm_dep_free(dep1);
446 _alpm_dep_free(dep2);
448 /* Prints warning */
449 _alpm_log(PM_LOG_WARNING,
450 _("removing '%s' from target list because it conflicts with '%s'\n"),
451 rsync->name, sync->name);
452 trans->add = alpm_list_remove(trans->add, rsync, _alpm_pkg_cmp, NULL);
453 _alpm_pkg_free_trans(rsync); /* rsync is not transaction target anymore */
454 continue;
457 alpm_list_free_inner(deps, (alpm_list_fn_free)_alpm_conflict_free);
458 alpm_list_free(deps);
459 deps = NULL;
461 /* 2. we check for target vs db conflicts (and resolve)*/
462 _alpm_log(PM_LOG_DEBUG, "check targets vs db and db vs targets\n");
463 deps = _alpm_outerconflicts(db_local, trans->add);
465 for(i = deps; i; i = i->next) {
466 pmconflict_t *conflict = i->data;
468 /* if conflict->package2 (the local package) is not elected for removal,
469 we ask the user */
470 int found = 0;
471 for(j = trans->add; j && !found; j = j->next) {
472 pmpkg_t *spkg = j->data;
473 if(_alpm_pkg_find(spkg->removes, conflict->package2)) {
474 found = 1;
477 if(found) {
478 continue;
481 _alpm_log(PM_LOG_DEBUG, "package '%s' conflicts with '%s'\n",
482 conflict->package1, conflict->package2);
484 pmpkg_t *sync = _alpm_pkg_find(trans->add, conflict->package1);
485 pmpkg_t *local = _alpm_db_get_pkgfromcache(db_local, conflict->package2);
486 int doremove = 0;
487 QUESTION(trans, PM_TRANS_CONV_CONFLICT_PKG, conflict->package1,
488 conflict->package2, conflict->reason, &doremove);
489 if(doremove) {
490 /* append to the removes list */
491 _alpm_log(PM_LOG_DEBUG, "electing '%s' for removal\n", conflict->package2);
492 sync->removes = alpm_list_add(sync->removes, local);
493 } else { /* abort */
494 _alpm_log(PM_LOG_ERROR, _("unresolvable package conflicts detected\n"));
495 pm_errno = PM_ERR_CONFLICTING_DEPS;
496 ret = -1;
497 if(data) {
498 pmconflict_t *newconflict = _alpm_conflict_dup(conflict);
499 if(newconflict) {
500 *data = alpm_list_add(*data, newconflict);
503 alpm_list_free_inner(deps, (alpm_list_fn_free)_alpm_conflict_free);
504 alpm_list_free(deps);
505 goto cleanup;
508 EVENT(trans, PM_TRANS_EVT_INTERCONFLICTS_DONE, NULL, NULL);
509 alpm_list_free_inner(deps, (alpm_list_fn_free)_alpm_conflict_free);
510 alpm_list_free(deps);
513 /* Build trans->remove list */
514 for(i = trans->add; i; i = i->next) {
515 pmpkg_t *spkg = i->data;
516 for(j = spkg->removes; j; j = j->next) {
517 pmpkg_t *rpkg = j->data;
518 if(!_alpm_pkg_find(trans->remove, rpkg->name)) {
519 _alpm_log(PM_LOG_DEBUG, "adding '%s' to remove list\n", rpkg->name);
520 trans->remove = alpm_list_add(trans->remove, _alpm_pkg_dup(rpkg));
525 if(!(trans->flags & PM_TRANS_FLAG_NODEPS)) {
526 _alpm_log(PM_LOG_DEBUG, "checking dependencies\n");
527 deps = alpm_checkdeps(_alpm_db_get_pkgcache(db_local), 1, trans->remove, trans->add);
528 if(deps) {
529 pm_errno = PM_ERR_UNSATISFIED_DEPS;
530 ret = -1;
531 if(data) {
532 *data = deps;
533 } else {
534 alpm_list_free_inner(deps, (alpm_list_fn_free)_alpm_depmiss_free);
535 alpm_list_free(deps);
537 goto cleanup;
540 for(i = trans->add; i; i = i->next) {
541 /* update download size field */
542 pmpkg_t *spkg = i->data;
543 if(compute_download_size(spkg) != 0) {
544 ret = -1;
545 goto cleanup;
549 cleanup:
550 alpm_list_free(unresolvable);
551 alpm_list_free(remove);
553 return ret;
556 /** Returns the size of the files that will be downloaded to install a
557 * package.
558 * @param newpkg the new package to upgrade to
559 * @return the size of the download
561 off_t SYMEXPORT alpm_pkg_download_size(pmpkg_t *newpkg)
563 if(!(newpkg->infolevel & INFRQ_DSIZE)) {
564 compute_download_size(newpkg);
566 return newpkg->download_size;
569 static int endswith(const char *filename, const char *extension)
571 const char *s = filename + strlen(filename) - strlen(extension);
572 return strcmp(s, extension) == 0;
575 /** Applies delta files to create an upgraded package file.
577 * All intermediate files are deleted, leaving only the starting and
578 * ending package files.
580 * @param trans the transaction
582 * @return 0 if all delta files were able to be applied, 1 otherwise.
584 static int apply_deltas(pmtrans_t *trans)
586 alpm_list_t *i;
587 int ret = 0;
588 const char *cachedir = _alpm_filecache_setup();
590 for(i = trans->add; i; i = i->next) {
591 pmpkg_t *spkg = i->data;
592 alpm_list_t *delta_path = spkg->delta_path;
593 alpm_list_t *dlts = NULL;
595 if(!delta_path) {
596 continue;
599 for(dlts = delta_path; dlts; dlts = dlts->next) {
600 pmdelta_t *d = dlts->data;
601 char *delta, *from, *to;
602 char command[PATH_MAX];
603 size_t len = 0;
605 delta = _alpm_filecache_find(d->delta);
606 /* the initial package might be in a different cachedir */
607 if(dlts == delta_path) {
608 from = _alpm_filecache_find(d->from);
609 } else {
610 /* len = cachedir len + from len + '/' + null */
611 len = strlen(cachedir) + strlen(d->from) + 2;
612 CALLOC(from, len, sizeof(char), RET_ERR(PM_ERR_MEMORY, 1));
613 snprintf(from, len, "%s/%s", cachedir, d->from);
615 len = strlen(cachedir) + strlen(d->to) + 2;
616 CALLOC(to, len, sizeof(char), RET_ERR(PM_ERR_MEMORY, 1));
617 snprintf(to, len, "%s/%s", cachedir, d->to);
619 /* build the patch command */
620 if(endswith(to, ".gz")) {
621 /* special handling for gzip : we disable timestamp with -n option */
622 snprintf(command, PATH_MAX, "xdelta3 -d -q -R -c -s %s %s | gzip -n > %s", from, delta, to);
623 } else {
624 snprintf(command, PATH_MAX, "xdelta3 -d -q -s %s %s %s", from, delta, to);
627 _alpm_log(PM_LOG_DEBUG, "command: %s\n", command);
629 EVENT(trans, PM_TRANS_EVT_DELTA_PATCH_START, d->to, d->delta);
631 int retval = system(command);
632 if(retval == 0) {
633 EVENT(trans, PM_TRANS_EVT_DELTA_PATCH_DONE, NULL, NULL);
635 /* delete the delta file */
636 unlink(delta);
638 /* Delete the 'from' package but only if it is an intermediate
639 * package. The starting 'from' package should be kept, just
640 * as if deltas were not used. */
641 if(dlts != delta_path) {
642 unlink(from);
645 FREE(from);
646 FREE(to);
647 FREE(delta);
649 if(retval != 0) {
650 /* one delta failed for this package, cancel the remaining ones */
651 EVENT(trans, PM_TRANS_EVT_DELTA_PATCH_FAILED, NULL, NULL);
652 pm_errno = PM_ERR_DLT_PATCHFAILED;
653 ret = 1;
654 break;
659 return ret;
662 /** Compares the md5sum of a file to the expected value.
664 * If the md5sum does not match, the user is asked whether the file
665 * should be deleted.
667 * @param trans the transaction
668 * @param filename the absolute path of the file to test
669 * @param md5sum the expected md5sum of the file
671 * @return 0 if the md5sum matched, 1 if not, -1 in case of errors
673 static int test_md5sum(pmtrans_t *trans, const char *filepath,
674 const char *md5sum)
676 int ret = _alpm_test_md5sum(filepath, md5sum);
677 if(ret == 1) {
678 int doremove = 0;
679 QUESTION(trans, PM_TRANS_CONV_CORRUPTED_PKG, (char *)filepath,
680 NULL, NULL, &doremove);
681 if(doremove) {
682 unlink(filepath);
686 return ret;
689 static int validate_deltas(pmtrans_t *trans, alpm_list_t *deltas,
690 alpm_list_t **data)
692 int errors = 0, ret = 0;
693 alpm_list_t *i;
695 if(!deltas) {
696 return 0;
699 /* Check integrity of deltas */
700 EVENT(trans, PM_TRANS_EVT_DELTA_INTEGRITY_START, NULL, NULL);
702 for(i = deltas; i; i = i->next) {
703 pmdelta_t *d = alpm_list_getdata(i);
704 const char *filename = alpm_delta_get_filename(d);
705 char *filepath = _alpm_filecache_find(filename);
706 const char *md5sum = alpm_delta_get_md5sum(d);
708 if(test_md5sum(trans, filepath, md5sum) != 0) {
709 errors++;
710 *data = alpm_list_add(*data, strdup(filename));
712 FREE(filepath);
714 if(errors) {
715 pm_errno = PM_ERR_DLT_INVALID;
716 return -1;
718 EVENT(trans, PM_TRANS_EVT_DELTA_INTEGRITY_DONE, NULL, NULL);
720 /* Use the deltas to generate the packages */
721 EVENT(trans, PM_TRANS_EVT_DELTA_PATCHES_START, NULL, NULL);
722 ret = apply_deltas(trans);
723 EVENT(trans, PM_TRANS_EVT_DELTA_PATCHES_DONE, NULL, NULL);
724 return ret;
727 static int download_files(pmhandle_t *handle, alpm_list_t **deltas)
729 const char *cachedir;
730 alpm_list_t *i, *j;
731 alpm_list_t *files = NULL;
732 int errors = 0;
734 cachedir = _alpm_filecache_setup();
735 handle->trans->state = STATE_DOWNLOADING;
737 /* Total progress - figure out the total download size if required to
738 * pass to the callback. This function is called once, and it is up to the
739 * frontend to compute incremental progress. */
740 if(handle->totaldlcb) {
741 off_t total_size = (off_t)0;
742 /* sum up the download size for each package and store total */
743 for(i = handle->trans->add; i; i = i->next) {
744 pmpkg_t *spkg = i->data;
745 total_size += spkg->download_size;
747 handle->totaldlcb(total_size);
750 /* group sync records by repository and download */
751 for(i = handle->dbs_sync; i; i = i->next) {
752 pmdb_t *current = i->data;
754 for(j = handle->trans->add; j; j = j->next) {
755 pmpkg_t *spkg = j->data;
757 if(spkg->origin != PKG_FROM_FILE && current == spkg->origin_data.db) {
758 const char *fname = NULL;
760 fname = alpm_pkg_get_filename(spkg);
761 ASSERT(fname != NULL, RET_ERR(PM_ERR_PKG_INVALID_NAME, -1));
762 alpm_list_t *delta_path = spkg->delta_path;
763 if(delta_path) {
764 /* using deltas */
765 alpm_list_t *dlts;
766 for(dlts = delta_path; dlts; dlts = dlts->next) {
767 pmdelta_t *delta = dlts->data;
768 if(delta->download_size != 0) {
769 files = alpm_list_add(files, strdup(delta->delta));
771 /* keep a list of all the delta files for md5sums */
772 *deltas = alpm_list_add(*deltas, delta);
775 } else if(spkg->download_size != 0) {
776 files = alpm_list_add(files, strdup(fname));
782 if(files) {
783 EVENT(handle->trans, PM_TRANS_EVT_RETRIEVE_START, current->treename, NULL);
784 for(j = files; j; j = j->next) {
785 const char *filename = j->data;
786 alpm_list_t *server;
787 int ret = -1;
788 for(server = current->servers; server; server = server->next) {
789 const char *server_url = server->data;
790 char *fileurl;
791 size_t len;
793 /* print server + filename into a buffer */
794 len = strlen(server_url) + strlen(filename) + 2;
795 CALLOC(fileurl, len, sizeof(char), RET_ERR(PM_ERR_MEMORY, -1));
796 snprintf(fileurl, len, "%s/%s", server_url, filename);
798 ret = _alpm_download(fileurl, cachedir, 0, 1, 0);
799 FREE(fileurl);
800 if(ret != -1) {
801 break;
804 if(ret == -1) {
805 errors++;
809 FREELIST(files);
810 if(errors) {
811 _alpm_log(PM_LOG_WARNING, _("failed to retrieve some files from %s\n"),
812 current->treename);
813 if(pm_errno == 0) {
814 pm_errno = PM_ERR_RETRIEVE;
816 return -1;
821 for(j = handle->trans->add; j; j = j->next) {
822 pmpkg_t *pkg = j->data;
823 pkg->infolevel &= ~INFRQ_DSIZE;
824 pkg->download_size = 0;
827 /* clear out value to let callback know we are done */
828 if(handle->totaldlcb) {
829 handle->totaldlcb(0);
831 return 0;
834 int _alpm_sync_commit(pmhandle_t *handle, alpm_list_t **data)
836 alpm_list_t *i;
837 alpm_list_t *deltas = NULL;
838 size_t numtargs, current = 0, replaces = 0;
839 int errors;
840 pmtrans_t *trans = handle->trans;
842 if(download_files(handle, &deltas)) {
843 alpm_list_free(deltas);
844 return -1;
847 if(validate_deltas(trans, deltas, data)) {
848 alpm_list_free(deltas);
849 return -1;
851 alpm_list_free(deltas);
853 /* Check integrity of packages */
854 numtargs = alpm_list_count(trans->add);
855 EVENT(trans, PM_TRANS_EVT_INTEGRITY_START, NULL, NULL);
857 errors = 0;
859 for(i = trans->add; i; i = i->next, current++) {
860 pmpkg_t *spkg = i->data;
861 int percent = (current * 100) / numtargs;
862 const char *filename;
863 char *filepath;
864 pgp_verify_t check_sig;
866 PROGRESS(trans, PM_TRANS_PROGRESS_INTEGRITY_START, "", percent,
867 numtargs, current);
868 if(spkg->origin == PKG_FROM_FILE) {
869 continue; /* pkg_load() has been already called, this package is valid */
872 filename = alpm_pkg_get_filename(spkg);
873 filepath = _alpm_filecache_find(filename);
874 pmdb_t *sdb = alpm_pkg_get_db(spkg);
875 check_sig = _alpm_db_get_sigverify_level(sdb);
877 /* load the package file and replace pkgcache entry with it in the target list */
878 /* TODO: alpm_pkg_get_db() will not work on this target anymore */
879 _alpm_log(PM_LOG_DEBUG, "replacing pkgcache entry with package file for target %s\n", spkg->name);
880 pmpkg_t *pkgfile =_alpm_pkg_load_internal(filepath, 1, spkg->md5sum,
881 spkg->base64_sig, check_sig);
882 if(!pkgfile) {
883 errors++;
884 *data = alpm_list_add(*data, strdup(filename));
885 FREE(filepath);
886 continue;
888 FREE(filepath);
889 pkgfile->reason = spkg->reason; /* copy over install reason */
890 i->data = pkgfile;
891 _alpm_pkg_free_trans(spkg); /* spkg has been removed from the target list */
894 PROGRESS(trans, PM_TRANS_PROGRESS_INTEGRITY_START, "", 100,
895 numtargs, current);
896 EVENT(trans, PM_TRANS_EVT_INTEGRITY_DONE, NULL, NULL);
899 if(errors) {
900 RET_ERR(PM_ERR_PKG_INVALID, -1);
903 if(trans->flags & PM_TRANS_FLAG_DOWNLOADONLY) {
904 return 0;
907 trans->state = STATE_COMMITING;
909 replaces = alpm_list_count(trans->remove);
911 /* fileconflict check */
912 if(!(trans->flags & PM_TRANS_FLAG_FORCE)) {
913 EVENT(trans, PM_TRANS_EVT_FILECONFLICTS_START, NULL, NULL);
915 _alpm_log(PM_LOG_DEBUG, "looking for file conflicts\n");
916 alpm_list_t *conflict = _alpm_db_find_fileconflicts(handle,
917 trans->add, trans->remove);
918 if(conflict) {
919 if(data) {
920 *data = conflict;
921 } else {
922 alpm_list_free_inner(conflict, (alpm_list_fn_free)_alpm_fileconflict_free);
923 alpm_list_free(conflict);
925 RET_ERR(PM_ERR_FILE_CONFLICTS, -1);
928 EVENT(trans, PM_TRANS_EVT_FILECONFLICTS_DONE, NULL, NULL);
931 /* check available disk space */
932 if(handle->checkspace) {
933 EVENT(trans, PM_TRANS_EVT_DISKSPACE_START, NULL, NULL);
935 _alpm_log(PM_LOG_DEBUG, "checking available disk space\n");
936 if(_alpm_check_diskspace(handle) == -1) {
937 _alpm_log(PM_LOG_ERROR, "%s\n", _("not enough free disk space"));
938 return -1;
941 EVENT(trans, PM_TRANS_EVT_DISKSPACE_DONE, NULL, NULL);
944 /* remove conflicting and to-be-replaced packages */
945 if(replaces) {
946 _alpm_log(PM_LOG_DEBUG, "removing conflicting and to-be-replaced packages\n");
947 /* we want the frontend to be aware of commit details */
948 if(_alpm_remove_packages(handle) == -1) {
949 _alpm_log(PM_LOG_ERROR, _("could not commit removal transaction\n"));
950 return -1;
954 /* install targets */
955 _alpm_log(PM_LOG_DEBUG, "installing packages\n");
956 if(_alpm_upgrade_packages(handle) == -1) {
957 _alpm_log(PM_LOG_ERROR, _("could not commit transaction\n"));
958 return -1;
961 return 0;
964 /* vim: set ts=2 sw=2 noet: */