Require handle for alpm_pkg_load()
[pacman-ng.git] / lib / libalpm / sync.c
blob1882888198d61e7a10dfb665d93154858938685a
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 pmpkg_t SYMEXPORT *alpm_sync_newversion(pmpkg_t *pkg, alpm_list_t *dbs_sync)
58 ASSERT(pkg != NULL, return NULL);
60 alpm_list_t *i;
61 pmpkg_t *spkg = NULL;
63 for(i = dbs_sync; !spkg && i; i = i->next) {
64 spkg = _alpm_db_get_pkgfromcache(i->data, alpm_pkg_get_name(pkg));
67 if(spkg == NULL) {
68 _alpm_log(PM_LOG_DEBUG, "'%s' not found in sync db => no upgrade\n",
69 alpm_pkg_get_name(pkg));
70 return NULL;
73 /* compare versions and see if spkg is an upgrade */
74 if(_alpm_pkg_compare_versions(spkg, pkg) > 0) {
75 _alpm_log(PM_LOG_DEBUG, "new version of '%s' found (%s => %s)\n",
76 alpm_pkg_get_name(pkg), alpm_pkg_get_version(pkg),
77 alpm_pkg_get_version(spkg));
78 return spkg;
80 /* spkg is not an upgrade */
81 return NULL;
84 /** Search for packages to upgrade and add them to the transaction. */
85 int SYMEXPORT alpm_sync_sysupgrade(pmhandle_t *handle, int enable_downgrade)
87 alpm_list_t *i, *j, *k;
88 pmtrans_t *trans;
89 pmdb_t *db_local;
90 alpm_list_t *dbs_sync;
92 ASSERT(handle != NULL, RET_ERR(PM_ERR_HANDLE_NULL, -1));
93 trans = handle->trans;
94 db_local = handle->db_local;
95 dbs_sync = handle->dbs_sync;
96 ASSERT(trans != NULL, RET_ERR(PM_ERR_TRANS_NULL, -1));
97 ASSERT(trans->state == STATE_INITIALIZED, RET_ERR(PM_ERR_TRANS_NOT_INITIALIZED, -1));
99 _alpm_log(PM_LOG_DEBUG, "checking for package upgrades\n");
100 for(i = _alpm_db_get_pkgcache(db_local); i; i = i->next) {
101 pmpkg_t *lpkg = i->data;
103 if(_alpm_pkg_find(trans->add, lpkg->name)) {
104 _alpm_log(PM_LOG_DEBUG, "%s is already in the target list -- skipping\n", lpkg->name);
105 continue;
108 /* Search for literal then replacers in each sync database.
109 * If found, don't check other databases */
110 for(j = dbs_sync; j; j = j->next) {
111 pmdb_t *sdb = j->data;
112 /* Check sdb */
113 pmpkg_t *spkg = _alpm_db_get_pkgfromcache(sdb, lpkg->name);
114 if(spkg) {
115 /* 1. literal was found in sdb */
116 int cmp = _alpm_pkg_compare_versions(spkg, lpkg);
117 if(cmp > 0) {
118 _alpm_log(PM_LOG_DEBUG, "new version of '%s' found (%s => %s)\n",
119 lpkg->name, lpkg->version, spkg->version);
120 /* check IgnorePkg/IgnoreGroup */
121 if(_alpm_pkg_should_ignore(spkg) || _alpm_pkg_should_ignore(lpkg)) {
122 _alpm_log(PM_LOG_WARNING, _("%s: ignoring package upgrade (%s => %s)\n"),
123 lpkg->name, lpkg->version, spkg->version);
124 } else {
125 _alpm_log(PM_LOG_DEBUG, "adding package %s-%s to the transaction targets\n",
126 spkg->name, spkg->version);
127 trans->add = alpm_list_add(trans->add, spkg);
129 } else if(cmp < 0) {
130 if(enable_downgrade) {
131 /* check IgnorePkg/IgnoreGroup */
132 if(_alpm_pkg_should_ignore(spkg) || _alpm_pkg_should_ignore(lpkg)) {
133 _alpm_log(PM_LOG_WARNING, _("%s: ignoring package downgrade (%s => %s)\n"),
134 lpkg->name, lpkg->version, spkg->version);
135 } else {
136 _alpm_log(PM_LOG_WARNING, _("%s: downgrading from version %s to version %s\n"),
137 lpkg->name, lpkg->version, spkg->version);
138 trans->add = alpm_list_add(trans->add, spkg);
140 } else {
141 _alpm_log(PM_LOG_WARNING, _("%s: local (%s) is newer than %s (%s)\n"),
142 lpkg->name, lpkg->version, sdb->treename, spkg->version);
145 /* jump to next local package */
146 break;
147 } else {
148 /* 2. search for replacers in sdb */
149 int found = 0;
150 for(k = _alpm_db_get_pkgcache(sdb); k; k = k->next) {
151 spkg = k->data;
152 if(alpm_list_find_str(alpm_pkg_get_replaces(spkg), lpkg->name)) {
153 found = 1;
154 /* check IgnorePkg/IgnoreGroup */
155 if(_alpm_pkg_should_ignore(spkg) || _alpm_pkg_should_ignore(lpkg)) {
156 _alpm_log(PM_LOG_WARNING, _("ignoring package replacement (%s-%s => %s-%s)\n"),
157 lpkg->name, lpkg->version, spkg->name, spkg->version);
158 continue;
161 int doreplace = 0;
162 QUESTION(trans, PM_TRANS_CONV_REPLACE_PKG, lpkg, spkg, sdb->treename, &doreplace);
163 if(!doreplace) {
164 continue;
167 /* If spkg is already in the target list, we append lpkg to spkg's
168 * removes list */
169 pmpkg_t *tpkg = _alpm_pkg_find(trans->add, spkg->name);
170 if(tpkg) {
171 /* sanity check, multiple repos can contain spkg->name */
172 if(tpkg->origin_data.db != sdb) {
173 _alpm_log(PM_LOG_WARNING, _("cannot replace %s by %s\n"),
174 lpkg->name, spkg->name);
175 continue;
177 _alpm_log(PM_LOG_DEBUG, "appending %s to the removes list of %s\n",
178 lpkg->name, tpkg->name);
179 tpkg->removes = alpm_list_add(tpkg->removes, lpkg);
180 /* check the to-be-replaced package's reason field */
181 if(alpm_pkg_get_reason(lpkg) == PM_PKG_REASON_EXPLICIT) {
182 tpkg->reason = PM_PKG_REASON_EXPLICIT;
184 } else {
185 /* add spkg to the target list */
186 /* copy over reason */
187 spkg->reason = alpm_pkg_get_reason(lpkg);
188 spkg->removes = alpm_list_add(NULL, lpkg);
189 _alpm_log(PM_LOG_DEBUG, "adding package %s-%s to the transaction targets\n",
190 spkg->name, spkg->version);
191 trans->add = alpm_list_add(trans->add, spkg);
195 if(found) {
196 break; /* jump to next local package */
202 return 0;
205 /** Find group members across a list of databases.
206 * If a member exists in several databases, only the first database is used.
207 * IgnorePkg is also handled.
208 * @param dbs the list of pmdb_t *
209 * @pram name the name of the group
210 * @return the list of pmpkg_t * (caller is responsible for alpm_list_free)
212 alpm_list_t SYMEXPORT *alpm_find_grp_pkgs(alpm_list_t *dbs,
213 const char *name)
215 alpm_list_t *i, *j, *pkgs = NULL, *ignorelist = NULL;
217 for(i = dbs; i; i = i->next) {
218 pmdb_t *db = i->data;
219 pmgrp_t *grp = alpm_db_readgrp(db, name);
221 if(!grp)
222 continue;
224 for(j = alpm_grp_get_pkgs(grp); j; j = j->next) {
225 pmpkg_t *pkg = j->data;
227 if(_alpm_pkg_find(ignorelist, alpm_pkg_get_name(pkg))) {
228 continue;
230 if(_alpm_pkg_should_ignore(pkg)) {
231 ignorelist = alpm_list_add(ignorelist, pkg);
232 int install = 0;
233 QUESTION(db->handle->trans, PM_TRANS_CONV_INSTALL_IGNOREPKG, pkg,
234 NULL, NULL, &install);
235 if(!install)
236 continue;
238 if(!_alpm_pkg_find(pkgs, alpm_pkg_get_name(pkg))) {
239 pkgs = alpm_list_add(pkgs, pkg);
243 alpm_list_free(ignorelist);
244 return pkgs;
247 /** Compute the size of the files that will be downloaded to install a
248 * package.
249 * @param newpkg the new package to upgrade to
251 static int compute_download_size(pmpkg_t *newpkg)
253 const char *fname;
254 char *fpath;
255 off_t size = 0;
257 if(newpkg->origin != PKG_FROM_SYNCDB) {
258 newpkg->infolevel |= INFRQ_DSIZE;
259 newpkg->download_size = 0;
260 return 0;
263 fname = alpm_pkg_get_filename(newpkg);
264 ASSERT(fname != NULL, RET_ERR(PM_ERR_PKG_INVALID_NAME, -1));
265 fpath = _alpm_filecache_find(newpkg->handle, fname);
267 if(fpath) {
268 FREE(fpath);
269 size = 0;
270 } else if(newpkg->handle->usedelta) {
271 off_t dltsize;
272 off_t pkgsize = alpm_pkg_get_size(newpkg);
274 dltsize = _alpm_shortest_delta_path(newpkg->handle,
275 alpm_pkg_get_deltas(newpkg),
276 alpm_pkg_get_filename(newpkg),
277 &newpkg->delta_path);
279 if(newpkg->delta_path && (dltsize < pkgsize * MAX_DELTA_RATIO)) {
280 _alpm_log(PM_LOG_DEBUG, "using delta size\n");
281 size = dltsize;
282 } else {
283 _alpm_log(PM_LOG_DEBUG, "using package size\n");
284 size = alpm_pkg_get_size(newpkg);
285 alpm_list_free(newpkg->delta_path);
286 newpkg->delta_path = NULL;
288 } else {
289 size = alpm_pkg_get_size(newpkg);
292 _alpm_log(PM_LOG_DEBUG, "setting download size %jd for pkg %s\n",
293 (intmax_t)size, alpm_pkg_get_name(newpkg));
295 newpkg->infolevel |= INFRQ_DSIZE;
296 newpkg->download_size = size;
297 return 0;
300 int _alpm_sync_prepare(pmhandle_t *handle, alpm_list_t **data)
302 alpm_list_t *deps = NULL;
303 alpm_list_t *unresolvable = NULL;
304 alpm_list_t *i, *j;
305 alpm_list_t *remove = NULL;
306 int ret = 0;
307 pmtrans_t *trans = handle->trans;
308 pmdb_t *db_local = handle->db_local;
310 if(data) {
311 *data = NULL;
314 if(!(trans->flags & PM_TRANS_FLAG_NODEPS)) {
315 alpm_list_t *resolved = NULL; /* target list after resolvedeps */
317 /* Build up list by repeatedly resolving each transaction package */
318 /* Resolve targets dependencies */
319 EVENT(trans, PM_TRANS_EVT_RESOLVEDEPS_START, NULL, NULL);
320 _alpm_log(PM_LOG_DEBUG, "resolving target's dependencies\n");
322 /* build remove list for resolvedeps */
323 for(i = trans->add; i; i = i->next) {
324 pmpkg_t *spkg = i->data;
325 for(j = spkg->removes; j; j = j->next) {
326 remove = alpm_list_add(remove, j->data);
330 /* Compute the fake local database for resolvedeps (partial fix for the
331 * phonon/qt issue) */
332 alpm_list_t *localpkgs = alpm_list_diff(_alpm_db_get_pkgcache(db_local),
333 trans->add, _alpm_pkg_cmp);
335 /* Resolve packages in the transaction one at a time, in addition
336 building up a list of packages which could not be resolved. */
337 for(i = trans->add; i; i = i->next) {
338 pmpkg_t *pkg = i->data;
339 if(_alpm_resolvedeps(localpkgs, handle->dbs_sync, pkg, trans->add,
340 &resolved, remove, data) == -1) {
341 unresolvable = alpm_list_add(unresolvable, pkg);
343 /* Else, [resolved] now additionally contains [pkg] and all of its
344 dependencies not already on the list */
346 alpm_list_free(localpkgs);
348 /* If there were unresolvable top-level packages, prompt the user to
349 see if they'd like to ignore them rather than failing the sync */
350 if(unresolvable != NULL) {
351 int remove_unresolvable = 0;
352 QUESTION(handle->trans, PM_TRANS_CONV_REMOVE_PKGS, unresolvable,
353 NULL, NULL, &remove_unresolvable);
354 if(remove_unresolvable) {
355 /* User wants to remove the unresolvable packages from the
356 transaction. The packages will be removed from the actual
357 transaction when the transaction packages are replaced with a
358 dependency-reordered list below */
359 pm_errno = 0; /* pm_errno was set by resolvedeps */
360 if(data) {
361 alpm_list_free_inner(*data, (alpm_list_fn_free)_alpm_depmiss_free);
362 alpm_list_free(*data);
363 *data = NULL;
365 } else {
366 /* pm_errno is set by resolvedeps */
367 alpm_list_free(resolved);
368 ret = -1;
369 goto cleanup;
373 /* Set DEPEND reason for pulled packages */
374 for(i = resolved; i; i = i->next) {
375 pmpkg_t *pkg = i->data;
376 if(!_alpm_pkg_find(trans->add, pkg->name)) {
377 pkg->reason = PM_PKG_REASON_DEPEND;
381 /* Unresolvable packages will be removed from the target list, so
382 we free the transaction specific fields */
383 alpm_list_free_inner(unresolvable, (alpm_list_fn_free)_alpm_pkg_free_trans);
385 /* re-order w.r.t. dependencies */
386 alpm_list_free(trans->add);
387 trans->add = _alpm_sortbydeps(resolved, 0);
388 alpm_list_free(resolved);
390 EVENT(trans, PM_TRANS_EVT_RESOLVEDEPS_DONE, NULL, NULL);
393 if(!(trans->flags & PM_TRANS_FLAG_NOCONFLICTS)) {
394 /* check for inter-conflicts and whatnot */
395 EVENT(trans, PM_TRANS_EVT_INTERCONFLICTS_START, NULL, NULL);
397 _alpm_log(PM_LOG_DEBUG, "looking for conflicts\n");
399 /* 1. check for conflicts in the target list */
400 _alpm_log(PM_LOG_DEBUG, "check targets vs targets\n");
401 deps = _alpm_innerconflicts(trans->add);
403 for(i = deps; i; i = i->next) {
404 pmconflict_t *conflict = i->data;
405 pmpkg_t *rsync, *sync, *sync1, *sync2;
407 /* have we already removed one of the conflicting targets? */
408 sync1 = _alpm_pkg_find(trans->add, conflict->package1);
409 sync2 = _alpm_pkg_find(trans->add, conflict->package2);
410 if(!sync1 || !sync2) {
411 continue;
414 _alpm_log(PM_LOG_DEBUG, "conflicting packages in the sync list: '%s' <-> '%s'\n",
415 conflict->package1, conflict->package2);
417 /* if sync1 provides sync2, we remove sync2 from the targets, and vice versa */
418 pmdepend_t *dep1 = _alpm_splitdep(conflict->package1);
419 pmdepend_t *dep2 = _alpm_splitdep(conflict->package2);
420 if(_alpm_depcmp(sync1, dep2)) {
421 rsync = sync2;
422 sync = sync1;
423 } else if(_alpm_depcmp(sync2, dep1)) {
424 rsync = sync1;
425 sync = sync2;
426 } else {
427 _alpm_log(PM_LOG_ERROR, _("unresolvable package conflicts detected\n"));
428 pm_errno = PM_ERR_CONFLICTING_DEPS;
429 ret = -1;
430 if(data) {
431 pmconflict_t *newconflict = _alpm_conflict_dup(conflict);
432 if(newconflict) {
433 *data = alpm_list_add(*data, newconflict);
436 alpm_list_free_inner(deps, (alpm_list_fn_free)_alpm_conflict_free);
437 alpm_list_free(deps);
438 _alpm_dep_free(dep1);
439 _alpm_dep_free(dep2);
440 goto cleanup;
442 _alpm_dep_free(dep1);
443 _alpm_dep_free(dep2);
445 /* Prints warning */
446 _alpm_log(PM_LOG_WARNING,
447 _("removing '%s' from target list because it conflicts with '%s'\n"),
448 rsync->name, sync->name);
449 trans->add = alpm_list_remove(trans->add, rsync, _alpm_pkg_cmp, NULL);
450 _alpm_pkg_free_trans(rsync); /* rsync is not transaction target anymore */
451 continue;
454 alpm_list_free_inner(deps, (alpm_list_fn_free)_alpm_conflict_free);
455 alpm_list_free(deps);
456 deps = NULL;
458 /* 2. we check for target vs db conflicts (and resolve)*/
459 _alpm_log(PM_LOG_DEBUG, "check targets vs db and db vs targets\n");
460 deps = _alpm_outerconflicts(db_local, trans->add);
462 for(i = deps; i; i = i->next) {
463 pmconflict_t *conflict = i->data;
465 /* if conflict->package2 (the local package) is not elected for removal,
466 we ask the user */
467 int found = 0;
468 for(j = trans->add; j && !found; j = j->next) {
469 pmpkg_t *spkg = j->data;
470 if(_alpm_pkg_find(spkg->removes, conflict->package2)) {
471 found = 1;
474 if(found) {
475 continue;
478 _alpm_log(PM_LOG_DEBUG, "package '%s' conflicts with '%s'\n",
479 conflict->package1, conflict->package2);
481 pmpkg_t *sync = _alpm_pkg_find(trans->add, conflict->package1);
482 pmpkg_t *local = _alpm_db_get_pkgfromcache(db_local, conflict->package2);
483 int doremove = 0;
484 QUESTION(trans, PM_TRANS_CONV_CONFLICT_PKG, conflict->package1,
485 conflict->package2, conflict->reason, &doremove);
486 if(doremove) {
487 /* append to the removes list */
488 _alpm_log(PM_LOG_DEBUG, "electing '%s' for removal\n", conflict->package2);
489 sync->removes = alpm_list_add(sync->removes, local);
490 } else { /* abort */
491 _alpm_log(PM_LOG_ERROR, _("unresolvable package conflicts detected\n"));
492 pm_errno = PM_ERR_CONFLICTING_DEPS;
493 ret = -1;
494 if(data) {
495 pmconflict_t *newconflict = _alpm_conflict_dup(conflict);
496 if(newconflict) {
497 *data = alpm_list_add(*data, newconflict);
500 alpm_list_free_inner(deps, (alpm_list_fn_free)_alpm_conflict_free);
501 alpm_list_free(deps);
502 goto cleanup;
505 EVENT(trans, PM_TRANS_EVT_INTERCONFLICTS_DONE, NULL, NULL);
506 alpm_list_free_inner(deps, (alpm_list_fn_free)_alpm_conflict_free);
507 alpm_list_free(deps);
510 /* Build trans->remove list */
511 for(i = trans->add; i; i = i->next) {
512 pmpkg_t *spkg = i->data;
513 for(j = spkg->removes; j; j = j->next) {
514 pmpkg_t *rpkg = j->data;
515 if(!_alpm_pkg_find(trans->remove, rpkg->name)) {
516 _alpm_log(PM_LOG_DEBUG, "adding '%s' to remove list\n", rpkg->name);
517 trans->remove = alpm_list_add(trans->remove, _alpm_pkg_dup(rpkg));
522 if(!(trans->flags & PM_TRANS_FLAG_NODEPS)) {
523 _alpm_log(PM_LOG_DEBUG, "checking dependencies\n");
524 deps = alpm_checkdeps(_alpm_db_get_pkgcache(db_local), 1, trans->remove, trans->add);
525 if(deps) {
526 pm_errno = PM_ERR_UNSATISFIED_DEPS;
527 ret = -1;
528 if(data) {
529 *data = deps;
530 } else {
531 alpm_list_free_inner(deps, (alpm_list_fn_free)_alpm_depmiss_free);
532 alpm_list_free(deps);
534 goto cleanup;
537 for(i = trans->add; i; i = i->next) {
538 /* update download size field */
539 pmpkg_t *spkg = i->data;
540 if(compute_download_size(spkg) != 0) {
541 ret = -1;
542 goto cleanup;
546 cleanup:
547 alpm_list_free(unresolvable);
548 alpm_list_free(remove);
550 return ret;
553 /** Returns the size of the files that will be downloaded to install a
554 * package.
555 * @param newpkg the new package to upgrade to
556 * @return the size of the download
558 off_t SYMEXPORT alpm_pkg_download_size(pmpkg_t *newpkg)
560 if(!(newpkg->infolevel & INFRQ_DSIZE)) {
561 compute_download_size(newpkg);
563 return newpkg->download_size;
566 static int endswith(const char *filename, const char *extension)
568 const char *s = filename + strlen(filename) - strlen(extension);
569 return strcmp(s, extension) == 0;
572 /** Applies delta files to create an upgraded package file.
574 * All intermediate files are deleted, leaving only the starting and
575 * ending package files.
577 * @param handle the context handle
579 * @return 0 if all delta files were able to be applied, 1 otherwise.
581 static int apply_deltas(pmhandle_t *handle)
583 alpm_list_t *i;
584 int ret = 0;
585 const char *cachedir = _alpm_filecache_setup(handle);
586 pmtrans_t *trans = handle->trans;
588 for(i = trans->add; i; i = i->next) {
589 pmpkg_t *spkg = i->data;
590 alpm_list_t *delta_path = spkg->delta_path;
591 alpm_list_t *dlts = NULL;
593 if(!delta_path) {
594 continue;
597 for(dlts = delta_path; dlts; dlts = dlts->next) {
598 pmdelta_t *d = dlts->data;
599 char *delta, *from, *to;
600 char command[PATH_MAX];
601 size_t len = 0;
603 delta = _alpm_filecache_find(handle, d->delta);
604 /* the initial package might be in a different cachedir */
605 if(dlts == delta_path) {
606 from = _alpm_filecache_find(handle, d->from);
607 } else {
608 /* len = cachedir len + from len + '/' + null */
609 len = strlen(cachedir) + strlen(d->from) + 2;
610 CALLOC(from, len, sizeof(char), RET_ERR(PM_ERR_MEMORY, 1));
611 snprintf(from, len, "%s/%s", cachedir, d->from);
613 len = strlen(cachedir) + strlen(d->to) + 2;
614 CALLOC(to, len, sizeof(char), RET_ERR(PM_ERR_MEMORY, 1));
615 snprintf(to, len, "%s/%s", cachedir, d->to);
617 /* build the patch command */
618 if(endswith(to, ".gz")) {
619 /* special handling for gzip : we disable timestamp with -n option */
620 snprintf(command, PATH_MAX, "xdelta3 -d -q -R -c -s %s %s | gzip -n > %s", from, delta, to);
621 } else {
622 snprintf(command, PATH_MAX, "xdelta3 -d -q -s %s %s %s", from, delta, to);
625 _alpm_log(PM_LOG_DEBUG, "command: %s\n", command);
627 EVENT(trans, PM_TRANS_EVT_DELTA_PATCH_START, d->to, d->delta);
629 int retval = system(command);
630 if(retval == 0) {
631 EVENT(trans, PM_TRANS_EVT_DELTA_PATCH_DONE, NULL, NULL);
633 /* delete the delta file */
634 unlink(delta);
636 /* Delete the 'from' package but only if it is an intermediate
637 * package. The starting 'from' package should be kept, just
638 * as if deltas were not used. */
639 if(dlts != delta_path) {
640 unlink(from);
643 FREE(from);
644 FREE(to);
645 FREE(delta);
647 if(retval != 0) {
648 /* one delta failed for this package, cancel the remaining ones */
649 EVENT(trans, PM_TRANS_EVT_DELTA_PATCH_FAILED, NULL, NULL);
650 pm_errno = PM_ERR_DLT_PATCHFAILED;
651 ret = 1;
652 break;
657 return ret;
660 /** Compares the md5sum of a file to the expected value.
662 * If the md5sum does not match, the user is asked whether the file
663 * should be deleted.
665 * @param trans the transaction
666 * @param filename the absolute path of the file to test
667 * @param md5sum the expected md5sum of the file
669 * @return 0 if the md5sum matched, 1 if not, -1 in case of errors
671 static int test_md5sum(pmtrans_t *trans, const char *filepath,
672 const char *md5sum)
674 int ret = _alpm_test_md5sum(filepath, md5sum);
675 if(ret == 1) {
676 int doremove = 0;
677 QUESTION(trans, PM_TRANS_CONV_CORRUPTED_PKG, (char *)filepath,
678 NULL, NULL, &doremove);
679 if(doremove) {
680 unlink(filepath);
684 return ret;
687 static int validate_deltas(pmhandle_t *handle, alpm_list_t *deltas,
688 alpm_list_t **data)
690 int errors = 0, ret = 0;
691 alpm_list_t *i;
692 pmtrans_t *trans = handle->trans;
694 if(!deltas) {
695 return 0;
698 /* Check integrity of deltas */
699 EVENT(trans, PM_TRANS_EVT_DELTA_INTEGRITY_START, NULL, NULL);
701 for(i = deltas; i; i = i->next) {
702 pmdelta_t *d = alpm_list_getdata(i);
703 const char *filename = alpm_delta_get_filename(d);
704 char *filepath = _alpm_filecache_find(handle, filename);
705 const char *md5sum = alpm_delta_get_md5sum(d);
707 if(test_md5sum(trans, filepath, md5sum) != 0) {
708 errors++;
709 *data = alpm_list_add(*data, strdup(filename));
711 FREE(filepath);
713 if(errors) {
714 pm_errno = PM_ERR_DLT_INVALID;
715 return -1;
717 EVENT(trans, PM_TRANS_EVT_DELTA_INTEGRITY_DONE, NULL, NULL);
719 /* Use the deltas to generate the packages */
720 EVENT(trans, PM_TRANS_EVT_DELTA_PATCHES_START, NULL, NULL);
721 ret = apply_deltas(handle);
722 EVENT(trans, PM_TRANS_EVT_DELTA_PATCHES_DONE, NULL, NULL);
723 return ret;
726 static int download_files(pmhandle_t *handle, alpm_list_t **deltas)
728 const char *cachedir;
729 alpm_list_t *i, *j;
730 alpm_list_t *files = NULL;
731 int errors = 0;
733 cachedir = _alpm_filecache_setup(handle);
734 handle->trans->state = STATE_DOWNLOADING;
736 /* Total progress - figure out the total download size if required to
737 * pass to the callback. This function is called once, and it is up to the
738 * frontend to compute incremental progress. */
739 if(handle->totaldlcb) {
740 off_t total_size = (off_t)0;
741 /* sum up the download size for each package and store total */
742 for(i = handle->trans->add; i; i = i->next) {
743 pmpkg_t *spkg = i->data;
744 total_size += spkg->download_size;
746 handle->totaldlcb(total_size);
749 /* group sync records by repository and download */
750 for(i = handle->dbs_sync; i; i = i->next) {
751 pmdb_t *current = i->data;
753 for(j = handle->trans->add; j; j = j->next) {
754 pmpkg_t *spkg = j->data;
756 if(spkg->origin != PKG_FROM_FILE && current == spkg->origin_data.db) {
757 const char *fname = NULL;
759 fname = alpm_pkg_get_filename(spkg);
760 ASSERT(fname != NULL, RET_ERR(PM_ERR_PKG_INVALID_NAME, -1));
761 alpm_list_t *delta_path = spkg->delta_path;
762 if(delta_path) {
763 /* using deltas */
764 alpm_list_t *dlts;
765 for(dlts = delta_path; dlts; dlts = dlts->next) {
766 pmdelta_t *delta = dlts->data;
767 if(delta->download_size != 0) {
768 files = alpm_list_add(files, strdup(delta->delta));
770 /* keep a list of all the delta files for md5sums */
771 *deltas = alpm_list_add(*deltas, delta);
774 } else if(spkg->download_size != 0) {
775 files = alpm_list_add(files, strdup(fname));
781 if(files) {
782 EVENT(handle->trans, PM_TRANS_EVT_RETRIEVE_START, current->treename, NULL);
783 for(j = files; j; j = j->next) {
784 const char *filename = j->data;
785 alpm_list_t *server;
786 int ret = -1;
787 for(server = current->servers; server; server = server->next) {
788 const char *server_url = server->data;
789 char *fileurl;
790 size_t len;
792 /* print server + filename into a buffer */
793 len = strlen(server_url) + strlen(filename) + 2;
794 CALLOC(fileurl, len, sizeof(char), RET_ERR(PM_ERR_MEMORY, -1));
795 snprintf(fileurl, len, "%s/%s", server_url, filename);
797 ret = _alpm_download(fileurl, cachedir, 0, 1, 0);
798 FREE(fileurl);
799 if(ret != -1) {
800 break;
803 if(ret == -1) {
804 errors++;
808 FREELIST(files);
809 if(errors) {
810 _alpm_log(PM_LOG_WARNING, _("failed to retrieve some files from %s\n"),
811 current->treename);
812 if(pm_errno == 0) {
813 pm_errno = PM_ERR_RETRIEVE;
815 return -1;
820 for(j = handle->trans->add; j; j = j->next) {
821 pmpkg_t *pkg = j->data;
822 pkg->infolevel &= ~INFRQ_DSIZE;
823 pkg->download_size = 0;
826 /* clear out value to let callback know we are done */
827 if(handle->totaldlcb) {
828 handle->totaldlcb(0);
830 return 0;
833 int _alpm_sync_commit(pmhandle_t *handle, alpm_list_t **data)
835 alpm_list_t *i;
836 alpm_list_t *deltas = NULL;
837 size_t numtargs, current = 0, replaces = 0;
838 int errors;
839 pmtrans_t *trans = handle->trans;
841 if(download_files(handle, &deltas)) {
842 alpm_list_free(deltas);
843 return -1;
846 if(validate_deltas(handle, deltas, data)) {
847 alpm_list_free(deltas);
848 return -1;
850 alpm_list_free(deltas);
852 /* Check integrity of packages */
853 numtargs = alpm_list_count(trans->add);
854 EVENT(trans, PM_TRANS_EVT_INTEGRITY_START, NULL, NULL);
856 errors = 0;
858 for(i = trans->add; i; i = i->next, current++) {
859 pmpkg_t *spkg = i->data;
860 int percent = (current * 100) / numtargs;
861 const char *filename;
862 char *filepath;
863 pgp_verify_t check_sig;
865 PROGRESS(trans, PM_TRANS_PROGRESS_INTEGRITY_START, "", percent,
866 numtargs, current);
867 if(spkg->origin == PKG_FROM_FILE) {
868 continue; /* pkg_load() has been already called, this package is valid */
871 filename = alpm_pkg_get_filename(spkg);
872 filepath = _alpm_filecache_find(handle, filename);
873 pmdb_t *sdb = alpm_pkg_get_db(spkg);
874 check_sig = _alpm_db_get_sigverify_level(sdb);
876 /* load the package file and replace pkgcache entry with it in the target list */
877 /* TODO: alpm_pkg_get_db() will not work on this target anymore */
878 _alpm_log(PM_LOG_DEBUG, "replacing pkgcache entry with package file for target %s\n", spkg->name);
879 pmpkg_t *pkgfile =_alpm_pkg_load_internal(handle, filepath, 1, spkg->md5sum,
880 spkg->base64_sig, check_sig);
881 if(!pkgfile) {
882 errors++;
883 *data = alpm_list_add(*data, strdup(filename));
884 FREE(filepath);
885 continue;
887 FREE(filepath);
888 pkgfile->reason = spkg->reason; /* copy over install reason */
889 i->data = pkgfile;
890 _alpm_pkg_free_trans(spkg); /* spkg has been removed from the target list */
893 PROGRESS(trans, PM_TRANS_PROGRESS_INTEGRITY_START, "", 100,
894 numtargs, current);
895 EVENT(trans, PM_TRANS_EVT_INTEGRITY_DONE, NULL, NULL);
898 if(errors) {
899 RET_ERR(PM_ERR_PKG_INVALID, -1);
902 if(trans->flags & PM_TRANS_FLAG_DOWNLOADONLY) {
903 return 0;
906 trans->state = STATE_COMMITING;
908 replaces = alpm_list_count(trans->remove);
910 /* fileconflict check */
911 if(!(trans->flags & PM_TRANS_FLAG_FORCE)) {
912 EVENT(trans, PM_TRANS_EVT_FILECONFLICTS_START, NULL, NULL);
914 _alpm_log(PM_LOG_DEBUG, "looking for file conflicts\n");
915 alpm_list_t *conflict = _alpm_db_find_fileconflicts(handle,
916 trans->add, trans->remove);
917 if(conflict) {
918 if(data) {
919 *data = conflict;
920 } else {
921 alpm_list_free_inner(conflict, (alpm_list_fn_free)_alpm_fileconflict_free);
922 alpm_list_free(conflict);
924 RET_ERR(PM_ERR_FILE_CONFLICTS, -1);
927 EVENT(trans, PM_TRANS_EVT_FILECONFLICTS_DONE, NULL, NULL);
930 /* check available disk space */
931 if(handle->checkspace) {
932 EVENT(trans, PM_TRANS_EVT_DISKSPACE_START, NULL, NULL);
934 _alpm_log(PM_LOG_DEBUG, "checking available disk space\n");
935 if(_alpm_check_diskspace(handle) == -1) {
936 _alpm_log(PM_LOG_ERROR, "%s\n", _("not enough free disk space"));
937 return -1;
940 EVENT(trans, PM_TRANS_EVT_DISKSPACE_DONE, NULL, NULL);
943 /* remove conflicting and to-be-replaced packages */
944 if(replaces) {
945 _alpm_log(PM_LOG_DEBUG, "removing conflicting and to-be-replaced packages\n");
946 /* we want the frontend to be aware of commit details */
947 if(_alpm_remove_packages(handle) == -1) {
948 _alpm_log(PM_LOG_ERROR, _("could not commit removal transaction\n"));
949 return -1;
953 /* install targets */
954 _alpm_log(PM_LOG_DEBUG, "installing packages\n");
955 if(_alpm_upgrade_packages(handle) == -1) {
956 _alpm_log(PM_LOG_ERROR, _("could not commit transaction\n"));
957 return -1;
960 return 0;
963 /* vim: set ts=2 sw=2 noet: */