Prefix alpm_transevt_t members with ALPM
[pacman-ng.git] / lib / libalpm / sync.c
blob77f3bbafc96329a8e3291675c852b1d2ca50a011
1 /*
2 * sync.c
4 * Copyright (c) 2006-2011 Pacman Development Team <pacman-dev@archlinux.org>
5 * Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org>
6 * Copyright (c) 2005 by Aurelien Foret <orelien@chez.com>
7 * Copyright (c) 2005 by Christian Hamar <krics@linuxforum.hu>
8 * Copyright (c) 2005, 2006 by Miklos Vajna <vmiklos@frugalware.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "config.h"
26 #include <sys/types.h> /* off_t */
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdint.h> /* intmax_t */
31 #include <unistd.h>
32 #include <limits.h>
34 /* libalpm */
35 #include "sync.h"
36 #include "alpm_list.h"
37 #include "log.h"
38 #include "package.h"
39 #include "db.h"
40 #include "deps.h"
41 #include "conflict.h"
42 #include "trans.h"
43 #include "add.h"
44 #include "util.h"
45 #include "handle.h"
46 #include "alpm.h"
47 #include "dload.h"
48 #include "delta.h"
49 #include "remove.h"
50 #include "diskspace.h"
51 #include "signing.h"
53 /** Check for new version of pkg in sync repos
54 * (only the first occurrence is considered in sync)
56 alpm_pkg_t SYMEXPORT *alpm_sync_newversion(alpm_pkg_t *pkg, alpm_list_t *dbs_sync)
58 alpm_list_t *i;
59 alpm_pkg_t *spkg = NULL;
61 ASSERT(pkg != NULL, return NULL);
62 pkg->handle->pm_errno = 0;
64 for(i = dbs_sync; !spkg && i; i = i->next) {
65 spkg = _alpm_db_get_pkgfromcache(i->data, alpm_pkg_get_name(pkg));
68 if(spkg == NULL) {
69 _alpm_log(pkg->handle, ALPM_LOG_DEBUG, "'%s' not found in sync db => no upgrade\n",
70 alpm_pkg_get_name(pkg));
71 return NULL;
74 /* compare versions and see if spkg is an upgrade */
75 if(_alpm_pkg_compare_versions(spkg, pkg) > 0) {
76 _alpm_log(pkg->handle, ALPM_LOG_DEBUG, "new version of '%s' found (%s => %s)\n",
77 alpm_pkg_get_name(pkg), alpm_pkg_get_version(pkg),
78 alpm_pkg_get_version(spkg));
79 return spkg;
81 /* spkg is not an upgrade */
82 return NULL;
85 /** Search for packages to upgrade and add them to the transaction. */
86 int SYMEXPORT alpm_sync_sysupgrade(alpm_handle_t *handle, int enable_downgrade)
88 alpm_list_t *i, *j, *k;
89 alpm_trans_t *trans;
90 alpm_db_t *db_local;
91 alpm_list_t *dbs_sync;
93 CHECK_HANDLE(handle, return -1);
94 trans = handle->trans;
95 db_local = handle->db_local;
96 dbs_sync = handle->dbs_sync;
97 ASSERT(trans != NULL, RET_ERR(handle, PM_ERR_TRANS_NULL, -1));
98 ASSERT(trans->state == STATE_INITIALIZED, RET_ERR(handle, PM_ERR_TRANS_NOT_INITIALIZED, -1));
100 _alpm_log(handle, ALPM_LOG_DEBUG, "checking for package upgrades\n");
101 for(i = _alpm_db_get_pkgcache(db_local); i; i = i->next) {
102 alpm_pkg_t *lpkg = i->data;
104 if(_alpm_pkg_find(trans->add, lpkg->name)) {
105 _alpm_log(handle, ALPM_LOG_DEBUG, "%s is already in the target list -- skipping\n", lpkg->name);
106 continue;
109 /* Search for literal then replacers in each sync database.
110 * If found, don't check other databases */
111 for(j = dbs_sync; j; j = j->next) {
112 alpm_db_t *sdb = j->data;
113 /* Check sdb */
114 alpm_pkg_t *spkg = _alpm_db_get_pkgfromcache(sdb, lpkg->name);
115 if(spkg) {
116 /* 1. literal was found in sdb */
117 int cmp = _alpm_pkg_compare_versions(spkg, lpkg);
118 if(cmp > 0) {
119 _alpm_log(handle, ALPM_LOG_DEBUG, "new version of '%s' found (%s => %s)\n",
120 lpkg->name, lpkg->version, spkg->version);
121 /* check IgnorePkg/IgnoreGroup */
122 if(_alpm_pkg_should_ignore(handle, spkg)
123 || _alpm_pkg_should_ignore(handle, lpkg)) {
124 _alpm_log(handle, ALPM_LOG_WARNING, _("%s: ignoring package upgrade (%s => %s)\n"),
125 lpkg->name, lpkg->version, spkg->version);
126 } else {
127 _alpm_log(handle, ALPM_LOG_DEBUG, "adding package %s-%s to the transaction targets\n",
128 spkg->name, spkg->version);
129 trans->add = alpm_list_add(trans->add, spkg);
131 } else if(cmp < 0) {
132 if(enable_downgrade) {
133 /* check IgnorePkg/IgnoreGroup */
134 if(_alpm_pkg_should_ignore(handle, spkg)
135 || _alpm_pkg_should_ignore(handle, lpkg)) {
136 _alpm_log(handle, ALPM_LOG_WARNING, _("%s: ignoring package downgrade (%s => %s)\n"),
137 lpkg->name, lpkg->version, spkg->version);
138 } else {
139 _alpm_log(handle, ALPM_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(handle, ALPM_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(handle, spkg)
159 || _alpm_pkg_should_ignore(handle, lpkg)) {
160 _alpm_log(handle, ALPM_LOG_WARNING, _("ignoring package replacement (%s-%s => %s-%s)\n"),
161 lpkg->name, lpkg->version, spkg->name, spkg->version);
162 continue;
165 int doreplace = 0;
166 QUESTION(trans, PM_TRANS_CONV_REPLACE_PKG, lpkg, spkg, sdb->treename, &doreplace);
167 if(!doreplace) {
168 continue;
171 /* If spkg is already in the target list, we append lpkg to spkg's
172 * removes list */
173 alpm_pkg_t *tpkg = _alpm_pkg_find(trans->add, spkg->name);
174 if(tpkg) {
175 /* sanity check, multiple repos can contain spkg->name */
176 if(tpkg->origin_data.db != sdb) {
177 _alpm_log(handle, ALPM_LOG_WARNING, _("cannot replace %s by %s\n"),
178 lpkg->name, spkg->name);
179 continue;
181 _alpm_log(handle, ALPM_LOG_DEBUG, "appending %s to the removes list of %s\n",
182 lpkg->name, tpkg->name);
183 tpkg->removes = alpm_list_add(tpkg->removes, lpkg);
184 /* check the to-be-replaced package's reason field */
185 if(alpm_pkg_get_reason(lpkg) == ALPM_PKG_REASON_EXPLICIT) {
186 tpkg->reason = ALPM_PKG_REASON_EXPLICIT;
188 } else {
189 /* add spkg to the target list */
190 /* copy over reason */
191 spkg->reason = alpm_pkg_get_reason(lpkg);
192 spkg->removes = alpm_list_add(NULL, lpkg);
193 _alpm_log(handle, ALPM_LOG_DEBUG, "adding package %s-%s to the transaction targets\n",
194 spkg->name, spkg->version);
195 trans->add = alpm_list_add(trans->add, spkg);
199 if(found) {
200 break; /* jump to next local package */
206 return 0;
209 /** Find group members across a list of databases.
210 * If a member exists in several databases, only the first database is used.
211 * IgnorePkg is also handled.
212 * @param dbs the list of alpm_db_t *
213 * @pram name the name of the group
214 * @return the list of alpm_pkg_t * (caller is responsible for alpm_list_free)
216 alpm_list_t SYMEXPORT *alpm_find_group_pkgs(alpm_list_t *dbs,
217 const char *name)
219 alpm_list_t *i, *j, *pkgs = NULL, *ignorelist = NULL;
221 for(i = dbs; i; i = i->next) {
222 alpm_db_t *db = i->data;
223 alpm_group_t *grp = alpm_db_readgroup(db, name);
225 if(!grp)
226 continue;
228 for(j = grp->packages; j; j = j->next) {
229 alpm_pkg_t *pkg = j->data;
231 if(_alpm_pkg_find(ignorelist, alpm_pkg_get_name(pkg))) {
232 continue;
234 if(_alpm_pkg_should_ignore(db->handle, pkg)) {
235 ignorelist = alpm_list_add(ignorelist, pkg);
236 int install = 0;
237 QUESTION(db->handle->trans, PM_TRANS_CONV_INSTALL_IGNOREPKG, pkg,
238 NULL, NULL, &install);
239 if(!install)
240 continue;
242 if(!_alpm_pkg_find(pkgs, alpm_pkg_get_name(pkg))) {
243 pkgs = alpm_list_add(pkgs, pkg);
247 alpm_list_free(ignorelist);
248 return pkgs;
251 /** Compute the size of the files that will be downloaded to install a
252 * package.
253 * @param newpkg the new package to upgrade to
255 static int compute_download_size(alpm_pkg_t *newpkg)
257 const char *fname;
258 char *fpath;
259 off_t size = 0;
260 alpm_handle_t *handle = newpkg->handle;
262 if(newpkg->origin != PKG_FROM_SYNCDB) {
263 newpkg->infolevel |= INFRQ_DSIZE;
264 newpkg->download_size = 0;
265 return 0;
268 fname = alpm_pkg_get_filename(newpkg);
269 ASSERT(fname != NULL, RET_ERR(handle, PM_ERR_PKG_INVALID_NAME, -1));
270 fpath = _alpm_filecache_find(handle, fname);
272 if(fpath) {
273 FREE(fpath);
274 size = 0;
275 } else if(handle->usedelta) {
276 off_t dltsize;
277 off_t pkgsize = alpm_pkg_get_size(newpkg);
279 dltsize = _alpm_shortest_delta_path(handle,
280 alpm_pkg_get_deltas(newpkg),
281 alpm_pkg_get_filename(newpkg),
282 &newpkg->delta_path);
284 if(newpkg->delta_path && (dltsize < pkgsize * MAX_DELTA_RATIO)) {
285 _alpm_log(handle, ALPM_LOG_DEBUG, "using delta size\n");
286 size = dltsize;
287 } else {
288 _alpm_log(handle, ALPM_LOG_DEBUG, "using package size\n");
289 size = alpm_pkg_get_size(newpkg);
290 alpm_list_free(newpkg->delta_path);
291 newpkg->delta_path = NULL;
293 } else {
294 size = alpm_pkg_get_size(newpkg);
297 _alpm_log(handle, ALPM_LOG_DEBUG, "setting download size %jd for pkg %s\n",
298 (intmax_t)size, alpm_pkg_get_name(newpkg));
300 newpkg->infolevel |= INFRQ_DSIZE;
301 newpkg->download_size = size;
302 return 0;
305 int _alpm_sync_prepare(alpm_handle_t *handle, alpm_list_t **data)
307 alpm_list_t *i, *j;
308 alpm_list_t *deps = NULL;
309 alpm_list_t *unresolvable = NULL;
310 alpm_list_t *remove = NULL;
311 int ret = 0;
312 alpm_trans_t *trans = handle->trans;
314 if(data) {
315 *data = NULL;
318 if(!(trans->flags & ALPM_TRANS_FLAG_NODEPS)) {
319 alpm_list_t *resolved = NULL; /* target list after resolvedeps */
321 /* Build up list by repeatedly resolving each transaction package */
322 /* Resolve targets dependencies */
323 EVENT(trans, ALPM_TRANS_EVT_RESOLVEDEPS_START, NULL, NULL);
324 _alpm_log(handle, ALPM_LOG_DEBUG, "resolving target's dependencies\n");
326 /* build remove list for resolvedeps */
327 for(i = trans->add; i; i = i->next) {
328 alpm_pkg_t *spkg = i->data;
329 for(j = spkg->removes; j; j = j->next) {
330 remove = alpm_list_add(remove, j->data);
334 /* Compute the fake local database for resolvedeps (partial fix for the
335 * phonon/qt issue) */
336 alpm_list_t *localpkgs = alpm_list_diff(_alpm_db_get_pkgcache(handle->db_local),
337 trans->add, _alpm_pkg_cmp);
339 /* Resolve packages in the transaction one at a time, in addition
340 building up a list of packages which could not be resolved. */
341 for(i = trans->add; i; i = i->next) {
342 alpm_pkg_t *pkg = i->data;
343 if(_alpm_resolvedeps(handle, localpkgs, pkg, trans->add,
344 &resolved, remove, data) == -1) {
345 unresolvable = alpm_list_add(unresolvable, pkg);
347 /* Else, [resolved] now additionally contains [pkg] and all of its
348 dependencies not already on the list */
350 alpm_list_free(localpkgs);
352 /* If there were unresolvable top-level packages, prompt the user to
353 see if they'd like to ignore them rather than failing the sync */
354 if(unresolvable != NULL) {
355 int remove_unresolvable = 0;
356 QUESTION(trans, PM_TRANS_CONV_REMOVE_PKGS, unresolvable,
357 NULL, NULL, &remove_unresolvable);
358 if(remove_unresolvable) {
359 /* User wants to remove the unresolvable packages from the
360 transaction. The packages will be removed from the actual
361 transaction when the transaction packages are replaced with a
362 dependency-reordered list below */
363 handle->pm_errno = 0; /* pm_errno was set by resolvedeps */
364 if(data) {
365 alpm_list_free_inner(*data, (alpm_list_fn_free)_alpm_depmiss_free);
366 alpm_list_free(*data);
367 *data = NULL;
369 } else {
370 /* pm_errno is set by resolvedeps */
371 alpm_list_free(resolved);
372 ret = -1;
373 goto cleanup;
377 /* Set DEPEND reason for pulled packages */
378 for(i = resolved; i; i = i->next) {
379 alpm_pkg_t *pkg = i->data;
380 if(!_alpm_pkg_find(trans->add, pkg->name)) {
381 pkg->reason = ALPM_PKG_REASON_DEPEND;
385 /* Unresolvable packages will be removed from the target list, so
386 we free the transaction specific fields */
387 alpm_list_free_inner(unresolvable, (alpm_list_fn_free)_alpm_pkg_free_trans);
389 /* re-order w.r.t. dependencies */
390 alpm_list_free(trans->add);
391 trans->add = _alpm_sortbydeps(handle, resolved, 0);
392 alpm_list_free(resolved);
394 EVENT(trans, ALPM_TRANS_EVT_RESOLVEDEPS_DONE, NULL, NULL);
397 if(!(trans->flags & ALPM_TRANS_FLAG_NOCONFLICTS)) {
398 /* check for inter-conflicts and whatnot */
399 EVENT(trans, ALPM_TRANS_EVT_INTERCONFLICTS_START, NULL, NULL);
401 _alpm_log(handle, ALPM_LOG_DEBUG, "looking for conflicts\n");
403 /* 1. check for conflicts in the target list */
404 _alpm_log(handle, ALPM_LOG_DEBUG, "check targets vs targets\n");
405 deps = _alpm_innerconflicts(handle, trans->add);
407 for(i = deps; i; i = i->next) {
408 alpm_conflict_t *conflict = i->data;
409 alpm_pkg_t *rsync, *sync, *sync1, *sync2;
411 /* have we already removed one of the conflicting targets? */
412 sync1 = _alpm_pkg_find(trans->add, conflict->package1);
413 sync2 = _alpm_pkg_find(trans->add, conflict->package2);
414 if(!sync1 || !sync2) {
415 continue;
418 _alpm_log(handle, ALPM_LOG_DEBUG, "conflicting packages in the sync list: '%s' <-> '%s'\n",
419 conflict->package1, conflict->package2);
421 /* if sync1 provides sync2, we remove sync2 from the targets, and vice versa */
422 alpm_depend_t *dep1 = _alpm_splitdep(conflict->package1);
423 alpm_depend_t *dep2 = _alpm_splitdep(conflict->package2);
424 if(_alpm_depcmp(sync1, dep2)) {
425 rsync = sync2;
426 sync = sync1;
427 } else if(_alpm_depcmp(sync2, dep1)) {
428 rsync = sync1;
429 sync = sync2;
430 } else {
431 _alpm_log(handle, ALPM_LOG_ERROR, _("unresolvable package conflicts detected\n"));
432 handle->pm_errno = PM_ERR_CONFLICTING_DEPS;
433 ret = -1;
434 if(data) {
435 alpm_conflict_t *newconflict = _alpm_conflict_dup(conflict);
436 if(newconflict) {
437 *data = alpm_list_add(*data, newconflict);
440 alpm_list_free_inner(deps, (alpm_list_fn_free)_alpm_conflict_free);
441 alpm_list_free(deps);
442 _alpm_dep_free(dep1);
443 _alpm_dep_free(dep2);
444 goto cleanup;
446 _alpm_dep_free(dep1);
447 _alpm_dep_free(dep2);
449 /* Prints warning */
450 _alpm_log(handle, ALPM_LOG_WARNING,
451 _("removing '%s' from target list because it conflicts with '%s'\n"),
452 rsync->name, sync->name);
453 trans->add = alpm_list_remove(trans->add, rsync, _alpm_pkg_cmp, NULL);
454 _alpm_pkg_free_trans(rsync); /* rsync is not transaction target anymore */
455 continue;
458 alpm_list_free_inner(deps, (alpm_list_fn_free)_alpm_conflict_free);
459 alpm_list_free(deps);
460 deps = NULL;
462 /* 2. we check for target vs db conflicts (and resolve)*/
463 _alpm_log(handle, ALPM_LOG_DEBUG, "check targets vs db and db vs targets\n");
464 deps = _alpm_outerconflicts(handle->db_local, trans->add);
466 for(i = deps; i; i = i->next) {
467 alpm_conflict_t *conflict = i->data;
469 /* if conflict->package2 (the local package) is not elected for removal,
470 we ask the user */
471 int found = 0;
472 for(j = trans->add; j && !found; j = j->next) {
473 alpm_pkg_t *spkg = j->data;
474 if(_alpm_pkg_find(spkg->removes, conflict->package2)) {
475 found = 1;
478 if(found) {
479 continue;
482 _alpm_log(handle, ALPM_LOG_DEBUG, "package '%s' conflicts with '%s'\n",
483 conflict->package1, conflict->package2);
485 alpm_pkg_t *sync = _alpm_pkg_find(trans->add, conflict->package1);
486 alpm_pkg_t *local = _alpm_db_get_pkgfromcache(handle->db_local, conflict->package2);
487 int doremove = 0;
488 QUESTION(trans, PM_TRANS_CONV_CONFLICT_PKG, conflict->package1,
489 conflict->package2, conflict->reason, &doremove);
490 if(doremove) {
491 /* append to the removes list */
492 _alpm_log(handle, ALPM_LOG_DEBUG, "electing '%s' for removal\n", conflict->package2);
493 sync->removes = alpm_list_add(sync->removes, local);
494 } else { /* abort */
495 _alpm_log(handle, ALPM_LOG_ERROR, _("unresolvable package conflicts detected\n"));
496 handle->pm_errno = PM_ERR_CONFLICTING_DEPS;
497 ret = -1;
498 if(data) {
499 alpm_conflict_t *newconflict = _alpm_conflict_dup(conflict);
500 if(newconflict) {
501 *data = alpm_list_add(*data, newconflict);
504 alpm_list_free_inner(deps, (alpm_list_fn_free)_alpm_conflict_free);
505 alpm_list_free(deps);
506 goto cleanup;
509 EVENT(trans, ALPM_TRANS_EVT_INTERCONFLICTS_DONE, NULL, NULL);
510 alpm_list_free_inner(deps, (alpm_list_fn_free)_alpm_conflict_free);
511 alpm_list_free(deps);
514 /* Build trans->remove list */
515 for(i = trans->add; i; i = i->next) {
516 alpm_pkg_t *spkg = i->data;
517 for(j = spkg->removes; j; j = j->next) {
518 alpm_pkg_t *rpkg = j->data;
519 if(!_alpm_pkg_find(trans->remove, rpkg->name)) {
520 _alpm_log(handle, ALPM_LOG_DEBUG, "adding '%s' to remove list\n", rpkg->name);
521 trans->remove = alpm_list_add(trans->remove, _alpm_pkg_dup(rpkg));
526 if(!(trans->flags & ALPM_TRANS_FLAG_NODEPS)) {
527 _alpm_log(handle, ALPM_LOG_DEBUG, "checking dependencies\n");
528 deps = alpm_checkdeps(handle, _alpm_db_get_pkgcache(handle->db_local),
529 trans->remove, trans->add, 1);
530 if(deps) {
531 handle->pm_errno = PM_ERR_UNSATISFIED_DEPS;
532 ret = -1;
533 if(data) {
534 *data = deps;
535 } else {
536 alpm_list_free_inner(deps, (alpm_list_fn_free)_alpm_depmiss_free);
537 alpm_list_free(deps);
539 goto cleanup;
542 for(i = trans->add; i; i = i->next) {
543 /* update download size field */
544 alpm_pkg_t *spkg = i->data;
545 if(compute_download_size(spkg) != 0) {
546 ret = -1;
547 goto cleanup;
551 cleanup:
552 alpm_list_free(unresolvable);
553 alpm_list_free(remove);
555 return ret;
558 /** Returns the size of the files that will be downloaded to install a
559 * package.
560 * @param newpkg the new package to upgrade to
561 * @return the size of the download
563 off_t SYMEXPORT alpm_pkg_download_size(alpm_pkg_t *newpkg)
565 if(!(newpkg->infolevel & INFRQ_DSIZE)) {
566 compute_download_size(newpkg);
568 return newpkg->download_size;
571 static int endswith(const char *filename, const char *extension)
573 const char *s = filename + strlen(filename) - strlen(extension);
574 return strcmp(s, extension) == 0;
577 /** Applies delta files to create an upgraded package file.
579 * All intermediate files are deleted, leaving only the starting and
580 * ending package files.
582 * @param handle the context handle
584 * @return 0 if all delta files were able to be applied, 1 otherwise.
586 static int apply_deltas(alpm_handle_t *handle)
588 alpm_list_t *i;
589 int ret = 0;
590 const char *cachedir = _alpm_filecache_setup(handle);
591 alpm_trans_t *trans = handle->trans;
593 for(i = trans->add; i; i = i->next) {
594 alpm_pkg_t *spkg = i->data;
595 alpm_list_t *delta_path = spkg->delta_path;
596 alpm_list_t *dlts = NULL;
598 if(!delta_path) {
599 continue;
602 for(dlts = delta_path; dlts; dlts = dlts->next) {
603 alpm_delta_t *d = dlts->data;
604 char *delta, *from, *to;
605 char command[PATH_MAX];
606 size_t len = 0;
608 delta = _alpm_filecache_find(handle, d->delta);
609 /* the initial package might be in a different cachedir */
610 if(dlts == delta_path) {
611 from = _alpm_filecache_find(handle, d->from);
612 } else {
613 /* len = cachedir len + from len + '/' + null */
614 len = strlen(cachedir) + strlen(d->from) + 2;
615 CALLOC(from, len, sizeof(char), RET_ERR(handle, PM_ERR_MEMORY, 1));
616 snprintf(from, len, "%s/%s", cachedir, d->from);
618 len = strlen(cachedir) + strlen(d->to) + 2;
619 CALLOC(to, len, sizeof(char), RET_ERR(handle, PM_ERR_MEMORY, 1));
620 snprintf(to, len, "%s/%s", cachedir, d->to);
622 /* build the patch command */
623 if(endswith(to, ".gz")) {
624 /* special handling for gzip : we disable timestamp with -n option */
625 snprintf(command, PATH_MAX, "xdelta3 -d -q -R -c -s %s %s | gzip -n > %s", from, delta, to);
626 } else {
627 snprintf(command, PATH_MAX, "xdelta3 -d -q -s %s %s %s", from, delta, to);
630 _alpm_log(handle, ALPM_LOG_DEBUG, "command: %s\n", command);
632 EVENT(trans, ALPM_TRANS_EVT_DELTA_PATCH_START, d->to, d->delta);
634 int retval = system(command);
635 if(retval == 0) {
636 EVENT(trans, ALPM_TRANS_EVT_DELTA_PATCH_DONE, NULL, NULL);
638 /* delete the delta file */
639 unlink(delta);
641 /* Delete the 'from' package but only if it is an intermediate
642 * package. The starting 'from' package should be kept, just
643 * as if deltas were not used. */
644 if(dlts != delta_path) {
645 unlink(from);
648 FREE(from);
649 FREE(to);
650 FREE(delta);
652 if(retval != 0) {
653 /* one delta failed for this package, cancel the remaining ones */
654 EVENT(trans, ALPM_TRANS_EVT_DELTA_PATCH_FAILED, NULL, NULL);
655 handle->pm_errno = PM_ERR_DLT_PATCHFAILED;
656 ret = 1;
657 break;
662 return ret;
665 /** Compares the md5sum of a file to the expected value.
667 * If the md5sum does not match, the user is asked whether the file
668 * should be deleted.
670 * @param trans the transaction
671 * @param filename the absolute path of the file to test
672 * @param md5sum the expected md5sum of the file
674 * @return 0 if the md5sum matched, 1 if not, -1 in case of errors
676 static int test_md5sum(alpm_trans_t *trans, const char *filepath,
677 const char *md5sum)
679 int ret = _alpm_test_md5sum(filepath, md5sum);
680 if(ret == 1) {
681 int doremove = 0;
682 QUESTION(trans, PM_TRANS_CONV_CORRUPTED_PKG, (char *)filepath,
683 NULL, NULL, &doremove);
684 if(doremove) {
685 unlink(filepath);
689 return ret;
692 static int validate_deltas(alpm_handle_t *handle, alpm_list_t *deltas,
693 alpm_list_t **data)
695 int errors = 0, ret = 0;
696 alpm_list_t *i;
697 alpm_trans_t *trans = handle->trans;
699 if(!deltas) {
700 return 0;
703 /* Check integrity of deltas */
704 EVENT(trans, ALPM_TRANS_EVT_DELTA_INTEGRITY_START, NULL, NULL);
706 for(i = deltas; i; i = i->next) {
707 alpm_delta_t *d = alpm_list_getdata(i);
708 char *filepath = _alpm_filecache_find(handle, d->delta);
710 if(test_md5sum(trans, filepath, d->delta_md5) != 0) {
711 errors++;
712 *data = alpm_list_add(*data, strdup(d->delta));
714 FREE(filepath);
716 if(errors) {
717 handle->pm_errno = PM_ERR_DLT_INVALID;
718 return -1;
720 EVENT(trans, ALPM_TRANS_EVT_DELTA_INTEGRITY_DONE, NULL, NULL);
722 /* Use the deltas to generate the packages */
723 EVENT(trans, ALPM_TRANS_EVT_DELTA_PATCHES_START, NULL, NULL);
724 ret = apply_deltas(handle);
725 EVENT(trans, ALPM_TRANS_EVT_DELTA_PATCHES_DONE, NULL, NULL);
726 return ret;
729 static int download_files(alpm_handle_t *handle, alpm_list_t **deltas)
731 const char *cachedir;
732 alpm_list_t *i, *j;
733 alpm_list_t *files = NULL;
734 int errors = 0;
736 cachedir = _alpm_filecache_setup(handle);
737 handle->trans->state = STATE_DOWNLOADING;
739 /* Total progress - figure out the total download size if required to
740 * pass to the callback. This function is called once, and it is up to the
741 * frontend to compute incremental progress. */
742 if(handle->totaldlcb) {
743 off_t total_size = (off_t)0;
744 /* sum up the download size for each package and store total */
745 for(i = handle->trans->add; i; i = i->next) {
746 alpm_pkg_t *spkg = i->data;
747 total_size += spkg->download_size;
749 handle->totaldlcb(total_size);
752 /* group sync records by repository and download */
753 for(i = handle->dbs_sync; i; i = i->next) {
754 alpm_db_t *current = i->data;
756 for(j = handle->trans->add; j; j = j->next) {
757 alpm_pkg_t *spkg = j->data;
759 if(spkg->origin != PKG_FROM_FILE && current == spkg->origin_data.db) {
760 const char *fname = NULL;
762 fname = alpm_pkg_get_filename(spkg);
763 ASSERT(fname != NULL, RET_ERR(handle, PM_ERR_PKG_INVALID_NAME, -1));
764 alpm_list_t *delta_path = spkg->delta_path;
765 if(delta_path) {
766 /* using deltas */
767 alpm_list_t *dlts;
768 for(dlts = delta_path; dlts; dlts = dlts->next) {
769 alpm_delta_t *delta = dlts->data;
770 if(delta->download_size != 0) {
771 files = alpm_list_add(files, strdup(delta->delta));
773 /* keep a list of all the delta files for md5sums */
774 *deltas = alpm_list_add(*deltas, delta);
777 } else if(spkg->download_size != 0) {
778 files = alpm_list_add(files, strdup(fname));
784 if(files) {
785 EVENT(handle->trans, ALPM_TRANS_EVT_RETRIEVE_START, current->treename, NULL);
786 for(j = files; j; j = j->next) {
787 const char *filename = j->data;
788 alpm_list_t *server;
789 int ret = -1;
790 for(server = current->servers; server; server = server->next) {
791 const char *server_url = server->data;
792 char *fileurl;
793 size_t len;
795 /* print server + filename into a buffer */
796 len = strlen(server_url) + strlen(filename) + 2;
797 CALLOC(fileurl, len, sizeof(char), RET_ERR(handle, PM_ERR_MEMORY, -1));
798 snprintf(fileurl, len, "%s/%s", server_url, filename);
800 ret = _alpm_download(handle, fileurl, cachedir, 0, 1, 0);
801 FREE(fileurl);
802 if(ret != -1) {
803 break;
806 if(ret == -1) {
807 errors++;
811 FREELIST(files);
812 if(errors) {
813 _alpm_log(handle, ALPM_LOG_WARNING, _("failed to retrieve some files from %s\n"),
814 current->treename);
815 if(handle->pm_errno == 0) {
816 handle->pm_errno = PM_ERR_RETRIEVE;
818 return -1;
823 for(j = handle->trans->add; j; j = j->next) {
824 alpm_pkg_t *pkg = j->data;
825 pkg->infolevel &= ~INFRQ_DSIZE;
826 pkg->download_size = 0;
829 /* clear out value to let callback know we are done */
830 if(handle->totaldlcb) {
831 handle->totaldlcb(0);
833 return 0;
836 int _alpm_sync_commit(alpm_handle_t *handle, alpm_list_t **data)
838 alpm_list_t *i;
839 alpm_list_t *deltas = NULL;
840 size_t numtargs, current = 0, replaces = 0;
841 int errors;
842 alpm_trans_t *trans = handle->trans;
844 if(download_files(handle, &deltas)) {
845 alpm_list_free(deltas);
846 return -1;
849 if(validate_deltas(handle, deltas, data)) {
850 alpm_list_free(deltas);
851 return -1;
853 alpm_list_free(deltas);
855 /* Check integrity of packages */
856 numtargs = alpm_list_count(trans->add);
857 EVENT(trans, ALPM_TRANS_EVT_INTEGRITY_START, NULL, NULL);
859 errors = 0;
861 for(i = trans->add; i; i = i->next, current++) {
862 alpm_pkg_t *spkg = i->data;
863 int percent = (current * 100) / numtargs;
864 const char *filename;
865 char *filepath;
866 pgp_verify_t check_sig;
868 PROGRESS(trans, PM_TRANS_PROGRESS_INTEGRITY_START, "", percent,
869 numtargs, current);
870 if(spkg->origin == PKG_FROM_FILE) {
871 continue; /* pkg_load() has been already called, this package is valid */
874 filename = alpm_pkg_get_filename(spkg);
875 filepath = _alpm_filecache_find(handle, filename);
876 alpm_db_t *sdb = alpm_pkg_get_db(spkg);
877 check_sig = alpm_db_get_sigverify_level(sdb);
879 /* load the package file and replace pkgcache entry with it in the target list */
880 /* TODO: alpm_pkg_get_db() will not work on this target anymore */
881 _alpm_log(handle, ALPM_LOG_DEBUG,
882 "replacing pkgcache entry with package file for target %s\n",
883 spkg->name);
884 alpm_pkg_t *pkgfile =_alpm_pkg_load_internal(handle, filepath, 1, spkg->md5sum,
885 spkg->base64_sig, check_sig);
886 if(!pkgfile) {
887 errors++;
888 *data = alpm_list_add(*data, strdup(filename));
889 FREE(filepath);
890 continue;
892 FREE(filepath);
893 pkgfile->reason = spkg->reason; /* copy over install reason */
894 i->data = pkgfile;
895 _alpm_pkg_free_trans(spkg); /* spkg has been removed from the target list */
898 PROGRESS(trans, PM_TRANS_PROGRESS_INTEGRITY_START, "", 100,
899 numtargs, current);
900 EVENT(trans, ALPM_TRANS_EVT_INTEGRITY_DONE, NULL, NULL);
903 if(errors) {
904 RET_ERR(handle, PM_ERR_PKG_INVALID, -1);
907 if(trans->flags & ALPM_TRANS_FLAG_DOWNLOADONLY) {
908 return 0;
911 trans->state = STATE_COMMITING;
913 replaces = alpm_list_count(trans->remove);
915 /* fileconflict check */
916 if(!(trans->flags & ALPM_TRANS_FLAG_FORCE)) {
917 EVENT(trans, ALPM_TRANS_EVT_FILECONFLICTS_START, NULL, NULL);
919 _alpm_log(handle, ALPM_LOG_DEBUG, "looking for file conflicts\n");
920 alpm_list_t *conflict = _alpm_db_find_fileconflicts(handle,
921 trans->add, trans->remove);
922 if(conflict) {
923 if(data) {
924 *data = conflict;
925 } else {
926 alpm_list_free_inner(conflict, (alpm_list_fn_free)_alpm_fileconflict_free);
927 alpm_list_free(conflict);
929 RET_ERR(handle, PM_ERR_FILE_CONFLICTS, -1);
932 EVENT(trans, ALPM_TRANS_EVT_FILECONFLICTS_DONE, NULL, NULL);
935 /* check available disk space */
936 if(handle->checkspace) {
937 EVENT(trans, ALPM_TRANS_EVT_DISKSPACE_START, NULL, NULL);
939 _alpm_log(handle, ALPM_LOG_DEBUG, "checking available disk space\n");
940 if(_alpm_check_diskspace(handle) == -1) {
941 _alpm_log(handle, ALPM_LOG_ERROR, "%s\n", _("not enough free disk space"));
942 return -1;
945 EVENT(trans, ALPM_TRANS_EVT_DISKSPACE_DONE, NULL, NULL);
948 /* remove conflicting and to-be-replaced packages */
949 if(replaces) {
950 _alpm_log(handle, ALPM_LOG_DEBUG, "removing conflicting and to-be-replaced packages\n");
951 /* we want the frontend to be aware of commit details */
952 if(_alpm_remove_packages(handle) == -1) {
953 _alpm_log(handle, ALPM_LOG_ERROR, _("could not commit removal transaction\n"));
954 return -1;
958 /* install targets */
959 _alpm_log(handle, ALPM_LOG_DEBUG, "installing packages\n");
960 if(_alpm_upgrade_packages(handle) == -1) {
961 _alpm_log(handle, ALPM_LOG_ERROR, _("could not commit transaction\n"));
962 return -1;
965 return 0;
968 /* vim: set ts=2 sw=2 noet: */