Be more robust when copying package data
[pacman-ng.git] / lib / libalpm / sync.c
blobe3e7fa90af174b9a9d80788b50fab363e4eff2b8
1 /*
2 * sync.c
4 * Copyright (c) 2006-2011 Pacman Development Team <pacman-dev@archlinux.org>
5 * Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org>
6 * Copyright (c) 2005 by Aurelien Foret <orelien@chez.com>
7 * Copyright (c) 2005 by Christian Hamar <krics@linuxforum.hu>
8 * Copyright (c) 2005, 2006 by Miklos Vajna <vmiklos@frugalware.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "config.h"
26 #include <sys/types.h> /* off_t */
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdint.h> /* intmax_t */
31 #include <unistd.h>
32 #include <limits.h>
34 /* libalpm */
35 #include "sync.h"
36 #include "alpm_list.h"
37 #include "log.h"
38 #include "package.h"
39 #include "db.h"
40 #include "deps.h"
41 #include "conflict.h"
42 #include "trans.h"
43 #include "add.h"
44 #include "util.h"
45 #include "handle.h"
46 #include "alpm.h"
47 #include "dload.h"
48 #include "delta.h"
49 #include "remove.h"
50 #include "diskspace.h"
51 #include "signing.h"
53 /** Check for new version of pkg in sync repos
54 * (only the first occurrence is considered in sync)
56 alpm_pkg_t SYMEXPORT *alpm_sync_newversion(alpm_pkg_t *pkg, alpm_list_t *dbs_sync)
58 alpm_list_t *i;
59 alpm_pkg_t *spkg = NULL;
61 ASSERT(pkg != NULL, return NULL);
62 pkg->handle->pm_errno = 0;
64 for(i = dbs_sync; !spkg && i; i = i->next) {
65 spkg = _alpm_db_get_pkgfromcache(i->data, pkg->name);
68 if(spkg == NULL) {
69 _alpm_log(pkg->handle, ALPM_LOG_DEBUG, "'%s' not found in sync db => no upgrade\n",
70 pkg->name);
71 return NULL;
74 /* compare versions and see if spkg is an upgrade */
75 if(_alpm_pkg_compare_versions(spkg, pkg) > 0) {
76 _alpm_log(pkg->handle, ALPM_LOG_DEBUG, "new version of '%s' found (%s => %s)\n",
77 pkg->name, pkg->version, spkg->version);
78 return spkg;
80 /* spkg is not an upgrade */
81 return NULL;
84 static int check_literal(alpm_handle_t *handle, alpm_pkg_t *lpkg,
85 alpm_pkg_t *spkg, int enable_downgrade)
87 /* 1. literal was found in sdb */
88 int cmp = _alpm_pkg_compare_versions(spkg, lpkg);
89 if(cmp > 0) {
90 _alpm_log(handle, ALPM_LOG_DEBUG, "new version of '%s' found (%s => %s)\n",
91 lpkg->name, lpkg->version, spkg->version);
92 /* check IgnorePkg/IgnoreGroup */
93 if(_alpm_pkg_should_ignore(handle, spkg)
94 || _alpm_pkg_should_ignore(handle, lpkg)) {
95 _alpm_log(handle, ALPM_LOG_WARNING, _("%s: ignoring package upgrade (%s => %s)\n"),
96 lpkg->name, lpkg->version, spkg->version);
97 } else {
98 _alpm_log(handle, ALPM_LOG_DEBUG, "adding package %s-%s to the transaction targets\n",
99 spkg->name, spkg->version);
100 return 1;
102 } else if(cmp < 0) {
103 if(enable_downgrade) {
104 /* check IgnorePkg/IgnoreGroup */
105 if(_alpm_pkg_should_ignore(handle, spkg)
106 || _alpm_pkg_should_ignore(handle, lpkg)) {
107 _alpm_log(handle, ALPM_LOG_WARNING, _("%s: ignoring package downgrade (%s => %s)\n"),
108 lpkg->name, lpkg->version, spkg->version);
109 } else {
110 _alpm_log(handle, ALPM_LOG_WARNING, _("%s: downgrading from version %s to version %s\n"),
111 lpkg->name, lpkg->version, spkg->version);
112 return 1;
114 } else {
115 alpm_db_t *sdb = alpm_pkg_get_db(spkg);
116 _alpm_log(handle, ALPM_LOG_WARNING, _("%s: local (%s) is newer than %s (%s)\n"),
117 lpkg->name, lpkg->version, sdb->treename, spkg->version);
120 return 0;
123 static alpm_list_t *check_replacers(alpm_handle_t *handle, alpm_pkg_t *lpkg,
124 alpm_db_t *sdb)
126 /* 2. search for replacers in sdb */
127 alpm_list_t *replacers = NULL;
128 alpm_list_t *k;
129 _alpm_log(handle, ALPM_LOG_DEBUG,
130 "searching for replacements for %s\n", lpkg->name);
131 for(k = _alpm_db_get_pkgcache(sdb); k; k = k->next) {
132 int found = 0;
133 alpm_pkg_t *spkg = k->data;
134 alpm_list_t *l;
135 for(l = alpm_pkg_get_replaces(spkg); l; l = l->next) {
136 alpm_depend_t *replace = l->data;
137 /* we only want to consider literal matches at this point. */
138 if(_alpm_depcmp_literal(lpkg, replace)) {
139 found = 1;
140 break;
143 if(found) {
144 int doreplace = 0;
145 alpm_pkg_t *tpkg;
146 /* check IgnorePkg/IgnoreGroup */
147 if(_alpm_pkg_should_ignore(handle, spkg)
148 || _alpm_pkg_should_ignore(handle, lpkg)) {
149 _alpm_log(handle, ALPM_LOG_WARNING,
150 _("ignoring package replacement (%s-%s => %s-%s)\n"),
151 lpkg->name, lpkg->version, spkg->name, spkg->version);
152 continue;
155 QUESTION(handle->trans, ALPM_TRANS_CONV_REPLACE_PKG, lpkg, spkg,
156 sdb->treename, &doreplace);
157 if(!doreplace) {
158 continue;
161 /* If spkg is already in the target list, we append lpkg to spkg's
162 * removes list */
163 tpkg = _alpm_pkg_find(handle->trans->add, spkg->name);
164 if(tpkg) {
165 /* sanity check, multiple repos can contain spkg->name */
166 if(tpkg->origin_data.db != sdb) {
167 _alpm_log(handle, ALPM_LOG_WARNING, _("cannot replace %s by %s\n"),
168 lpkg->name, spkg->name);
169 continue;
171 _alpm_log(handle, ALPM_LOG_DEBUG, "appending %s to the removes list of %s\n",
172 lpkg->name, tpkg->name);
173 tpkg->removes = alpm_list_add(tpkg->removes, lpkg);
174 /* check the to-be-replaced package's reason field */
175 if(alpm_pkg_get_reason(lpkg) == ALPM_PKG_REASON_EXPLICIT) {
176 tpkg->reason = ALPM_PKG_REASON_EXPLICIT;
178 } else {
179 /* add spkg to the target list */
180 /* copy over reason */
181 spkg->reason = alpm_pkg_get_reason(lpkg);
182 spkg->removes = alpm_list_add(NULL, lpkg);
183 _alpm_log(handle, ALPM_LOG_DEBUG,
184 "adding package %s-%s to the transaction targets\n",
185 spkg->name, spkg->version);
186 replacers = alpm_list_add(replacers, spkg);
190 return replacers;
193 /** Search for packages to upgrade and add them to the transaction. */
194 int SYMEXPORT alpm_sync_sysupgrade(alpm_handle_t *handle, int enable_downgrade)
196 alpm_list_t *i, *j;
197 alpm_trans_t *trans;
199 CHECK_HANDLE(handle, return -1);
200 trans = handle->trans;
201 ASSERT(trans != NULL, RET_ERR(handle, ALPM_ERR_TRANS_NULL, -1));
202 ASSERT(trans->state == STATE_INITIALIZED, RET_ERR(handle, ALPM_ERR_TRANS_NOT_INITIALIZED, -1));
204 _alpm_log(handle, ALPM_LOG_DEBUG, "checking for package upgrades\n");
205 for(i = _alpm_db_get_pkgcache(handle->db_local); i; i = i->next) {
206 alpm_pkg_t *lpkg = i->data;
208 if(_alpm_pkg_find(trans->add, lpkg->name)) {
209 _alpm_log(handle, ALPM_LOG_DEBUG, "%s is already in the target list -- skipping\n", lpkg->name);
210 continue;
213 /* Search for literal then replacers in each sync database. */
214 for(j = handle->dbs_sync; j; j = j->next) {
215 alpm_db_t *sdb = j->data;
216 /* Check sdb */
217 alpm_pkg_t *spkg = _alpm_db_get_pkgfromcache(sdb, lpkg->name);
218 int literal_upgrade = 0;
219 if(spkg) {
220 literal_upgrade = check_literal(handle, lpkg, spkg, enable_downgrade);
221 if(literal_upgrade) {
222 trans->add = alpm_list_add(trans->add, spkg);
224 /* jump to next local package */
225 break;
226 } else {
227 alpm_list_t *replacers;
228 replacers = check_replacers(handle, lpkg, sdb);
229 if(replacers) {
230 trans->add = alpm_list_join(trans->add, replacers);
236 return 0;
239 /** Find group members across a list of databases.
240 * If a member exists in several databases, only the first database is used.
241 * IgnorePkg is also handled.
242 * @param dbs the list of alpm_db_t *
243 * @pram name the name of the group
244 * @return the list of alpm_pkg_t * (caller is responsible for alpm_list_free)
246 alpm_list_t SYMEXPORT *alpm_find_group_pkgs(alpm_list_t *dbs,
247 const char *name)
249 alpm_list_t *i, *j, *pkgs = NULL, *ignorelist = NULL;
251 for(i = dbs; i; i = i->next) {
252 alpm_db_t *db = i->data;
253 alpm_group_t *grp = alpm_db_readgroup(db, name);
255 if(!grp)
256 continue;
258 for(j = grp->packages; j; j = j->next) {
259 alpm_pkg_t *pkg = j->data;
261 if(_alpm_pkg_find(ignorelist, pkg->name)) {
262 continue;
264 if(_alpm_pkg_should_ignore(db->handle, pkg)) {
265 ignorelist = alpm_list_add(ignorelist, pkg);
266 int install = 0;
267 QUESTION(db->handle->trans, ALPM_TRANS_CONV_INSTALL_IGNOREPKG, pkg,
268 NULL, NULL, &install);
269 if(!install)
270 continue;
272 if(!_alpm_pkg_find(pkgs, pkg->name)) {
273 pkgs = alpm_list_add(pkgs, pkg);
277 alpm_list_free(ignorelist);
278 return pkgs;
281 /** Compute the size of the files that will be downloaded to install a
282 * package.
283 * @param newpkg the new package to upgrade to
285 static int compute_download_size(alpm_pkg_t *newpkg)
287 const char *fname;
288 char *fpath;
289 off_t size = 0;
290 alpm_handle_t *handle = newpkg->handle;
292 if(newpkg->origin != PKG_FROM_SYNCDB) {
293 newpkg->infolevel |= INFRQ_DSIZE;
294 newpkg->download_size = 0;
295 return 0;
298 fname = alpm_pkg_get_filename(newpkg);
299 ASSERT(fname != NULL, RET_ERR(handle, ALPM_ERR_PKG_INVALID_NAME, -1));
300 fpath = _alpm_filecache_find(handle, fname);
302 if(fpath) {
303 FREE(fpath);
304 size = 0;
305 } else if(handle->usedelta) {
306 off_t dltsize;
307 off_t pkgsize = alpm_pkg_get_size(newpkg);
309 dltsize = _alpm_shortest_delta_path(handle,
310 alpm_pkg_get_deltas(newpkg),
311 alpm_pkg_get_filename(newpkg),
312 &newpkg->delta_path);
314 if(newpkg->delta_path && (dltsize < pkgsize * MAX_DELTA_RATIO)) {
315 _alpm_log(handle, ALPM_LOG_DEBUG, "using delta size\n");
316 size = dltsize;
317 } else {
318 _alpm_log(handle, ALPM_LOG_DEBUG, "using package size\n");
319 size = alpm_pkg_get_size(newpkg);
320 alpm_list_free(newpkg->delta_path);
321 newpkg->delta_path = NULL;
323 } else {
324 size = alpm_pkg_get_size(newpkg);
327 _alpm_log(handle, ALPM_LOG_DEBUG, "setting download size %jd for pkg %s\n",
328 (intmax_t)size, newpkg->name);
330 newpkg->infolevel |= INFRQ_DSIZE;
331 newpkg->download_size = size;
332 return 0;
335 int _alpm_sync_prepare(alpm_handle_t *handle, alpm_list_t **data)
337 alpm_list_t *i, *j;
338 alpm_list_t *deps = NULL;
339 alpm_list_t *unresolvable = NULL;
340 alpm_list_t *remove = NULL;
341 int ret = 0;
342 alpm_trans_t *trans = handle->trans;
344 if(data) {
345 *data = NULL;
348 /* ensure all sync database are valid since we will be using them */
349 for(i = handle->dbs_sync; i; i = i->next) {
350 const alpm_db_t *db = i->data;
351 if(db->status & DB_STATUS_INVALID) {
352 RET_ERR(handle, ALPM_ERR_DB_INVALID, -1);
354 if(db->status & DB_STATUS_MISSING) {
355 RET_ERR(handle, ALPM_ERR_DB_NOT_FOUND, -1);
359 if(!(trans->flags & ALPM_TRANS_FLAG_NODEPS)) {
360 alpm_list_t *resolved = NULL; /* target list after resolvedeps */
362 /* Build up list by repeatedly resolving each transaction package */
363 /* Resolve targets dependencies */
364 EVENT(trans, ALPM_TRANS_EVT_RESOLVEDEPS_START, NULL, NULL);
365 _alpm_log(handle, ALPM_LOG_DEBUG, "resolving target's dependencies\n");
367 /* build remove list for resolvedeps */
368 for(i = trans->add; i; i = i->next) {
369 alpm_pkg_t *spkg = i->data;
370 for(j = spkg->removes; j; j = j->next) {
371 remove = alpm_list_add(remove, j->data);
375 /* Compute the fake local database for resolvedeps (partial fix for the
376 * phonon/qt issue) */
377 alpm_list_t *localpkgs = alpm_list_diff(_alpm_db_get_pkgcache(handle->db_local),
378 trans->add, _alpm_pkg_cmp);
380 /* Resolve packages in the transaction one at a time, in addition
381 building up a list of packages which could not be resolved. */
382 for(i = trans->add; i; i = i->next) {
383 alpm_pkg_t *pkg = i->data;
384 if(_alpm_resolvedeps(handle, localpkgs, pkg, trans->add,
385 &resolved, remove, data) == -1) {
386 unresolvable = alpm_list_add(unresolvable, pkg);
388 /* Else, [resolved] now additionally contains [pkg] and all of its
389 dependencies not already on the list */
391 alpm_list_free(localpkgs);
393 /* If there were unresolvable top-level packages, prompt the user to
394 see if they'd like to ignore them rather than failing the sync */
395 if(unresolvable != NULL) {
396 int remove_unresolvable = 0;
397 QUESTION(trans, ALPM_TRANS_CONV_REMOVE_PKGS, unresolvable,
398 NULL, NULL, &remove_unresolvable);
399 if(remove_unresolvable) {
400 /* User wants to remove the unresolvable packages from the
401 transaction. The packages will be removed from the actual
402 transaction when the transaction packages are replaced with a
403 dependency-reordered list below */
404 handle->pm_errno = 0; /* pm_errno was set by resolvedeps */
405 if(data) {
406 alpm_list_free_inner(*data, (alpm_list_fn_free)_alpm_depmiss_free);
407 alpm_list_free(*data);
408 *data = NULL;
410 } else {
411 /* pm_errno is set by resolvedeps */
412 alpm_list_free(resolved);
413 ret = -1;
414 goto cleanup;
418 /* Set DEPEND reason for pulled packages */
419 for(i = resolved; i; i = i->next) {
420 alpm_pkg_t *pkg = i->data;
421 if(!_alpm_pkg_find(trans->add, pkg->name)) {
422 pkg->reason = ALPM_PKG_REASON_DEPEND;
426 /* Unresolvable packages will be removed from the target list, so
427 we free the transaction specific fields */
428 alpm_list_free_inner(unresolvable, (alpm_list_fn_free)_alpm_pkg_free_trans);
430 /* re-order w.r.t. dependencies */
431 alpm_list_free(trans->add);
432 trans->add = _alpm_sortbydeps(handle, resolved, 0);
433 alpm_list_free(resolved);
435 EVENT(trans, ALPM_TRANS_EVT_RESOLVEDEPS_DONE, NULL, NULL);
438 if(!(trans->flags & ALPM_TRANS_FLAG_NOCONFLICTS)) {
439 /* check for inter-conflicts and whatnot */
440 EVENT(trans, ALPM_TRANS_EVT_INTERCONFLICTS_START, NULL, NULL);
442 _alpm_log(handle, ALPM_LOG_DEBUG, "looking for conflicts\n");
444 /* 1. check for conflicts in the target list */
445 _alpm_log(handle, ALPM_LOG_DEBUG, "check targets vs targets\n");
446 deps = _alpm_innerconflicts(handle, trans->add);
448 for(i = deps; i; i = i->next) {
449 alpm_conflict_t *conflict = i->data;
450 alpm_pkg_t *rsync, *sync, *sync1, *sync2;
452 /* have we already removed one of the conflicting targets? */
453 sync1 = _alpm_pkg_find(trans->add, conflict->package1);
454 sync2 = _alpm_pkg_find(trans->add, conflict->package2);
455 if(!sync1 || !sync2) {
456 continue;
459 _alpm_log(handle, ALPM_LOG_DEBUG, "conflicting packages in the sync list: '%s' <-> '%s'\n",
460 conflict->package1, conflict->package2);
462 /* if sync1 provides sync2, we remove sync2 from the targets, and vice versa */
463 alpm_depend_t *dep1 = _alpm_splitdep(conflict->package1);
464 alpm_depend_t *dep2 = _alpm_splitdep(conflict->package2);
465 if(_alpm_depcmp(sync1, dep2)) {
466 rsync = sync2;
467 sync = sync1;
468 } else if(_alpm_depcmp(sync2, dep1)) {
469 rsync = sync1;
470 sync = sync2;
471 } else {
472 _alpm_log(handle, ALPM_LOG_ERROR, _("unresolvable package conflicts detected\n"));
473 handle->pm_errno = ALPM_ERR_CONFLICTING_DEPS;
474 ret = -1;
475 if(data) {
476 alpm_conflict_t *newconflict = _alpm_conflict_dup(conflict);
477 if(newconflict) {
478 *data = alpm_list_add(*data, newconflict);
481 alpm_list_free_inner(deps, (alpm_list_fn_free)_alpm_conflict_free);
482 alpm_list_free(deps);
483 _alpm_dep_free(dep1);
484 _alpm_dep_free(dep2);
485 goto cleanup;
487 _alpm_dep_free(dep1);
488 _alpm_dep_free(dep2);
490 /* Prints warning */
491 _alpm_log(handle, ALPM_LOG_WARNING,
492 _("removing '%s' from target list because it conflicts with '%s'\n"),
493 rsync->name, sync->name);
494 trans->add = alpm_list_remove(trans->add, rsync, _alpm_pkg_cmp, NULL);
495 _alpm_pkg_free_trans(rsync); /* rsync is not transaction target anymore */
496 continue;
499 alpm_list_free_inner(deps, (alpm_list_fn_free)_alpm_conflict_free);
500 alpm_list_free(deps);
501 deps = NULL;
503 /* 2. we check for target vs db conflicts (and resolve)*/
504 _alpm_log(handle, ALPM_LOG_DEBUG, "check targets vs db and db vs targets\n");
505 deps = _alpm_outerconflicts(handle->db_local, trans->add);
507 for(i = deps; i; i = i->next) {
508 alpm_conflict_t *conflict = i->data;
510 /* if conflict->package2 (the local package) is not elected for removal,
511 we ask the user */
512 int found = 0;
513 for(j = trans->add; j && !found; j = j->next) {
514 alpm_pkg_t *spkg = j->data;
515 if(_alpm_pkg_find(spkg->removes, conflict->package2)) {
516 found = 1;
519 if(found) {
520 continue;
523 _alpm_log(handle, ALPM_LOG_DEBUG, "package '%s' conflicts with '%s'\n",
524 conflict->package1, conflict->package2);
526 alpm_pkg_t *sync = _alpm_pkg_find(trans->add, conflict->package1);
527 alpm_pkg_t *local = _alpm_db_get_pkgfromcache(handle->db_local, conflict->package2);
528 int doremove = 0;
529 QUESTION(trans, ALPM_TRANS_CONV_CONFLICT_PKG, conflict->package1,
530 conflict->package2, conflict->reason->name, &doremove);
531 if(doremove) {
532 /* append to the removes list */
533 _alpm_log(handle, ALPM_LOG_DEBUG, "electing '%s' for removal\n", conflict->package2);
534 sync->removes = alpm_list_add(sync->removes, local);
535 } else { /* abort */
536 _alpm_log(handle, ALPM_LOG_ERROR, _("unresolvable package conflicts detected\n"));
537 handle->pm_errno = ALPM_ERR_CONFLICTING_DEPS;
538 ret = -1;
539 if(data) {
540 alpm_conflict_t *newconflict = _alpm_conflict_dup(conflict);
541 if(newconflict) {
542 *data = alpm_list_add(*data, newconflict);
545 alpm_list_free_inner(deps, (alpm_list_fn_free)_alpm_conflict_free);
546 alpm_list_free(deps);
547 goto cleanup;
550 EVENT(trans, ALPM_TRANS_EVT_INTERCONFLICTS_DONE, NULL, NULL);
551 alpm_list_free_inner(deps, (alpm_list_fn_free)_alpm_conflict_free);
552 alpm_list_free(deps);
555 /* Build trans->remove list */
556 for(i = trans->add; i; i = i->next) {
557 alpm_pkg_t *spkg = i->data;
558 for(j = spkg->removes; j; j = j->next) {
559 alpm_pkg_t *rpkg = j->data;
560 if(!_alpm_pkg_find(trans->remove, rpkg->name)) {
561 alpm_pkg_t *copy;
562 _alpm_log(handle, ALPM_LOG_DEBUG, "adding '%s' to remove list\n", rpkg->name);
563 if(_alpm_pkg_dup(rpkg, &copy) == -1) {
564 return -1;
566 trans->remove = alpm_list_add(trans->remove, copy);
571 if(!(trans->flags & ALPM_TRANS_FLAG_NODEPS)) {
572 _alpm_log(handle, ALPM_LOG_DEBUG, "checking dependencies\n");
573 deps = alpm_checkdeps(handle, _alpm_db_get_pkgcache(handle->db_local),
574 trans->remove, trans->add, 1);
575 if(deps) {
576 handle->pm_errno = ALPM_ERR_UNSATISFIED_DEPS;
577 ret = -1;
578 if(data) {
579 *data = deps;
580 } else {
581 alpm_list_free_inner(deps, (alpm_list_fn_free)_alpm_depmiss_free);
582 alpm_list_free(deps);
584 goto cleanup;
587 for(i = trans->add; i; i = i->next) {
588 /* update download size field */
589 alpm_pkg_t *spkg = i->data;
590 if(compute_download_size(spkg) != 0) {
591 ret = -1;
592 goto cleanup;
596 cleanup:
597 alpm_list_free(unresolvable);
598 alpm_list_free(remove);
600 return ret;
603 /** Returns the size of the files that will be downloaded to install a
604 * package.
605 * @param newpkg the new package to upgrade to
606 * @return the size of the download
608 off_t SYMEXPORT alpm_pkg_download_size(alpm_pkg_t *newpkg)
610 if(!(newpkg->infolevel & INFRQ_DSIZE)) {
611 compute_download_size(newpkg);
613 return newpkg->download_size;
616 static int endswith(const char *filename, const char *extension)
618 const char *s = filename + strlen(filename) - strlen(extension);
619 return strcmp(s, extension) == 0;
622 /** Applies delta files to create an upgraded package file.
624 * All intermediate files are deleted, leaving only the starting and
625 * ending package files.
627 * @param handle the context handle
629 * @return 0 if all delta files were able to be applied, 1 otherwise.
631 static int apply_deltas(alpm_handle_t *handle)
633 alpm_list_t *i;
634 int ret = 0;
635 const char *cachedir = _alpm_filecache_setup(handle);
636 alpm_trans_t *trans = handle->trans;
638 for(i = trans->add; i; i = i->next) {
639 alpm_pkg_t *spkg = i->data;
640 alpm_list_t *delta_path = spkg->delta_path;
641 alpm_list_t *dlts = NULL;
643 if(!delta_path) {
644 continue;
647 for(dlts = delta_path; dlts; dlts = dlts->next) {
648 alpm_delta_t *d = dlts->data;
649 char *delta, *from, *to;
650 char command[PATH_MAX];
651 size_t len = 0;
653 delta = _alpm_filecache_find(handle, d->delta);
654 /* the initial package might be in a different cachedir */
655 if(dlts == delta_path) {
656 from = _alpm_filecache_find(handle, d->from);
657 } else {
658 /* len = cachedir len + from len + '/' + null */
659 len = strlen(cachedir) + strlen(d->from) + 2;
660 CALLOC(from, len, sizeof(char), RET_ERR(handle, ALPM_ERR_MEMORY, 1));
661 snprintf(from, len, "%s/%s", cachedir, d->from);
663 len = strlen(cachedir) + strlen(d->to) + 2;
664 CALLOC(to, len, sizeof(char), RET_ERR(handle, ALPM_ERR_MEMORY, 1));
665 snprintf(to, len, "%s/%s", cachedir, d->to);
667 /* build the patch command */
668 if(endswith(to, ".gz")) {
669 /* special handling for gzip : we disable timestamp with -n option */
670 snprintf(command, PATH_MAX, "xdelta3 -d -q -R -c -s %s %s | gzip -n > %s", from, delta, to);
671 } else {
672 snprintf(command, PATH_MAX, "xdelta3 -d -q -s %s %s %s", from, delta, to);
675 _alpm_log(handle, ALPM_LOG_DEBUG, "command: %s\n", command);
677 EVENT(trans, ALPM_TRANS_EVT_DELTA_PATCH_START, d->to, d->delta);
679 int retval = system(command);
680 if(retval == 0) {
681 EVENT(trans, ALPM_TRANS_EVT_DELTA_PATCH_DONE, NULL, NULL);
683 /* delete the delta file */
684 unlink(delta);
686 /* Delete the 'from' package but only if it is an intermediate
687 * package. The starting 'from' package should be kept, just
688 * as if deltas were not used. */
689 if(dlts != delta_path) {
690 unlink(from);
693 FREE(from);
694 FREE(to);
695 FREE(delta);
697 if(retval != 0) {
698 /* one delta failed for this package, cancel the remaining ones */
699 EVENT(trans, ALPM_TRANS_EVT_DELTA_PATCH_FAILED, NULL, NULL);
700 handle->pm_errno = ALPM_ERR_DLT_PATCHFAILED;
701 ret = 1;
702 break;
707 return ret;
710 /** Compares the md5sum of a file to the expected value.
712 * If the md5sum does not match, the user is asked whether the file
713 * should be deleted.
715 * @param trans the transaction
716 * @param filename the absolute path of the file to test
717 * @param reason an error code indicating the reason for package invalidity
719 * @return 1 if file was removed, 0 otherwise
721 static int prompt_to_delete(alpm_trans_t *trans, const char *filepath,
722 enum _alpm_errno_t reason)
724 int doremove = 0;
725 QUESTION(trans, ALPM_TRANS_CONV_CORRUPTED_PKG, (char *)filepath,
726 &reason, NULL, &doremove);
727 if(doremove) {
728 unlink(filepath);
730 return doremove;
733 static int validate_deltas(alpm_handle_t *handle, alpm_list_t *deltas,
734 alpm_list_t **data)
736 int errors = 0, ret = 0;
737 alpm_list_t *i;
738 alpm_trans_t *trans = handle->trans;
740 if(!deltas) {
741 return 0;
744 /* Check integrity of deltas */
745 EVENT(trans, ALPM_TRANS_EVT_DELTA_INTEGRITY_START, NULL, NULL);
747 for(i = deltas; i; i = i->next) {
748 alpm_delta_t *d = alpm_list_getdata(i);
749 char *filepath = _alpm_filecache_find(handle, d->delta);
751 ret = _alpm_test_checksum(filepath, d->delta_md5, ALPM_CSUM_MD5);
752 if(ret != 0) {
753 prompt_to_delete(trans, filepath, ALPM_ERR_DLT_INVALID);
754 errors++;
755 *data = alpm_list_add(*data, strdup(d->delta));
757 FREE(filepath);
759 if(errors) {
760 handle->pm_errno = ALPM_ERR_DLT_INVALID;
761 return -1;
763 EVENT(trans, ALPM_TRANS_EVT_DELTA_INTEGRITY_DONE, NULL, NULL);
765 /* Use the deltas to generate the packages */
766 EVENT(trans, ALPM_TRANS_EVT_DELTA_PATCHES_START, NULL, NULL);
767 ret = apply_deltas(handle);
768 EVENT(trans, ALPM_TRANS_EVT_DELTA_PATCHES_DONE, NULL, NULL);
769 return ret;
772 static int download_files(alpm_handle_t *handle, alpm_list_t **deltas)
774 const char *cachedir;
775 alpm_list_t *i, *j;
776 alpm_list_t *files = NULL;
777 int errors = 0;
779 cachedir = _alpm_filecache_setup(handle);
780 handle->trans->state = STATE_DOWNLOADING;
782 /* Total progress - figure out the total download size if required to
783 * pass to the callback. This function is called once, and it is up to the
784 * frontend to compute incremental progress. */
785 if(handle->totaldlcb) {
786 off_t total_size = (off_t)0;
787 /* sum up the download size for each package and store total */
788 for(i = handle->trans->add; i; i = i->next) {
789 alpm_pkg_t *spkg = i->data;
790 total_size += spkg->download_size;
792 handle->totaldlcb(total_size);
795 /* group sync records by repository and download */
796 for(i = handle->dbs_sync; i; i = i->next) {
797 alpm_db_t *current = i->data;
799 for(j = handle->trans->add; j; j = j->next) {
800 alpm_pkg_t *spkg = j->data;
802 if(spkg->origin != PKG_FROM_FILE && current == spkg->origin_data.db) {
803 alpm_list_t *delta_path = spkg->delta_path;
804 if(delta_path) {
805 /* using deltas */
806 alpm_list_t *dlts;
807 for(dlts = delta_path; dlts; dlts = dlts->next) {
808 alpm_delta_t *delta = dlts->data;
809 if(delta->download_size != 0) {
810 struct dload_payload *dpayload;
812 CALLOC(dpayload, 1, sizeof(*dpayload), RET_ERR(handle, ALPM_ERR_MEMORY, -1));
813 STRDUP(dpayload->filename, delta->delta, RET_ERR(handle, ALPM_ERR_MEMORY, -1));
814 dpayload->max_size = delta->download_size;
816 files = alpm_list_add(files, dpayload);
818 /* keep a list of all the delta files for md5sums */
819 *deltas = alpm_list_add(*deltas, delta);
822 } else if(spkg->download_size != 0) {
823 struct dload_payload *payload;
825 ASSERT(spkg->filename != NULL, RET_ERR(handle, ALPM_ERR_PKG_INVALID_NAME, -1));
826 CALLOC(payload, 1, sizeof(*payload), RET_ERR(handle, ALPM_ERR_MEMORY, -1));
827 STRDUP(payload->filename, spkg->filename, RET_ERR(handle, ALPM_ERR_MEMORY, -1));
828 payload->max_size = alpm_pkg_get_size(spkg);
830 files = alpm_list_add(files, payload);
836 if(files) {
837 EVENT(handle->trans, ALPM_TRANS_EVT_RETRIEVE_START, current->treename, NULL);
838 for(j = files; j; j = j->next) {
839 struct dload_payload *payload = j->data;
840 alpm_list_t *server;
841 int ret = -1;
842 for(server = current->servers; server; server = server->next) {
843 const char *server_url = server->data;
844 size_t len;
846 /* print server + filename into a buffer */
847 len = strlen(server_url) + strlen(payload->filename) + 2;
848 CALLOC(payload->fileurl, len, sizeof(char), RET_ERR(handle, ALPM_ERR_MEMORY, -1));
849 snprintf(payload->fileurl, len, "%s/%s", server_url, payload->filename);
850 payload->handle = handle;
851 payload->allow_resume = 1;
853 ret = _alpm_download(payload, cachedir, NULL);
854 if(ret != -1) {
855 break;
858 if(ret == -1) {
859 errors++;
860 _alpm_log(handle, ALPM_LOG_WARNING, _("failed to retrieve some files from %s\n"),
861 current->treename);
865 alpm_list_free_inner(files, (alpm_list_fn_free)_alpm_dload_payload_free);
866 alpm_list_free(files);
867 files = NULL;
871 for(j = handle->trans->add; j; j = j->next) {
872 alpm_pkg_t *pkg = j->data;
873 pkg->infolevel &= ~INFRQ_DSIZE;
874 pkg->download_size = 0;
877 /* clear out value to let callback know we are done */
878 if(handle->totaldlcb) {
879 handle->totaldlcb(0);
882 return errors;
885 int _alpm_sync_commit(alpm_handle_t *handle, alpm_list_t **data)
887 alpm_list_t *i;
888 alpm_list_t *deltas = NULL;
889 size_t numtargs, current = 0, replaces = 0;
890 int errors;
891 alpm_trans_t *trans = handle->trans;
893 if(download_files(handle, &deltas)) {
894 alpm_list_free(deltas);
895 return -1;
898 if(validate_deltas(handle, deltas, data)) {
899 alpm_list_free(deltas);
900 return -1;
902 alpm_list_free(deltas);
904 /* Check integrity of packages */
905 numtargs = alpm_list_count(trans->add);
906 EVENT(trans, ALPM_TRANS_EVT_INTEGRITY_START, NULL, NULL);
908 errors = 0;
910 for(i = trans->add; i; i = i->next, current++) {
911 alpm_pkg_t *spkg = i->data;
912 int percent = (current * 100) / numtargs;
913 const char *filename;
914 char *filepath;
915 alpm_siglevel_t level;
917 PROGRESS(trans, ALPM_TRANS_PROGRESS_INTEGRITY_START, "", percent,
918 numtargs, current);
919 if(spkg->origin == PKG_FROM_FILE) {
920 continue; /* pkg_load() has been already called, this package is valid */
923 filename = alpm_pkg_get_filename(spkg);
924 filepath = _alpm_filecache_find(handle, filename);
925 alpm_db_t *sdb = alpm_pkg_get_db(spkg);
926 level = alpm_db_get_siglevel(sdb);
928 /* load the package file and replace pkgcache entry with it in the target list */
929 /* TODO: alpm_pkg_get_db() will not work on this target anymore */
930 _alpm_log(handle, ALPM_LOG_DEBUG,
931 "replacing pkgcache entry with package file for target %s\n",
932 spkg->name);
933 alpm_pkg_t *pkgfile =_alpm_pkg_load_internal(handle, filepath, 1, spkg->md5sum,
934 spkg->sha256sum, spkg->base64_sig, level);
935 if(!pkgfile) {
936 prompt_to_delete(trans, filepath, handle->pm_errno);
937 errors++;
938 *data = alpm_list_add(*data, strdup(filename));
939 FREE(filepath);
940 continue;
942 FREE(filepath);
943 pkgfile->reason = spkg->reason; /* copy over install reason */
944 i->data = pkgfile;
945 _alpm_pkg_free_trans(spkg); /* spkg has been removed from the target list */
948 PROGRESS(trans, ALPM_TRANS_PROGRESS_INTEGRITY_START, "", 100,
949 numtargs, current);
950 EVENT(trans, ALPM_TRANS_EVT_INTEGRITY_DONE, NULL, NULL);
953 if(errors) {
954 if(!handle->pm_errno) {
955 RET_ERR(handle, ALPM_ERR_PKG_INVALID, -1);
957 return -1;
960 if(trans->flags & ALPM_TRANS_FLAG_DOWNLOADONLY) {
961 return 0;
964 trans->state = STATE_COMMITING;
966 replaces = alpm_list_count(trans->remove);
968 /* fileconflict check */
969 if(!(trans->flags & ALPM_TRANS_FLAG_FORCE)) {
970 EVENT(trans, ALPM_TRANS_EVT_FILECONFLICTS_START, NULL, NULL);
972 _alpm_log(handle, ALPM_LOG_DEBUG, "looking for file conflicts\n");
973 alpm_list_t *conflict = _alpm_db_find_fileconflicts(handle,
974 trans->add, trans->remove);
975 if(conflict) {
976 if(data) {
977 *data = conflict;
978 } else {
979 alpm_list_free_inner(conflict, (alpm_list_fn_free)_alpm_fileconflict_free);
980 alpm_list_free(conflict);
982 RET_ERR(handle, ALPM_ERR_FILE_CONFLICTS, -1);
985 EVENT(trans, ALPM_TRANS_EVT_FILECONFLICTS_DONE, NULL, NULL);
988 /* check available disk space */
989 if(handle->checkspace) {
990 EVENT(trans, ALPM_TRANS_EVT_DISKSPACE_START, NULL, NULL);
992 _alpm_log(handle, ALPM_LOG_DEBUG, "checking available disk space\n");
993 if(_alpm_check_diskspace(handle) == -1) {
994 _alpm_log(handle, ALPM_LOG_ERROR, "%s\n", _("not enough free disk space"));
995 return -1;
998 EVENT(trans, ALPM_TRANS_EVT_DISKSPACE_DONE, NULL, NULL);
1001 /* remove conflicting and to-be-replaced packages */
1002 if(replaces) {
1003 _alpm_log(handle, ALPM_LOG_DEBUG, "removing conflicting and to-be-replaced packages\n");
1004 /* we want the frontend to be aware of commit details */
1005 if(_alpm_remove_packages(handle) == -1) {
1006 _alpm_log(handle, ALPM_LOG_ERROR, _("could not commit removal transaction\n"));
1007 return -1;
1011 /* install targets */
1012 _alpm_log(handle, ALPM_LOG_DEBUG, "installing packages\n");
1013 if(_alpm_upgrade_packages(handle) == -1) {
1014 _alpm_log(handle, ALPM_LOG_ERROR, _("could not commit transaction\n"));
1015 return -1;
1018 return 0;
1021 /* vim: set ts=2 sw=2 noet: */