Remove duplicates from the unresolvable list before prompting user
[pacman-ng.git] / lib / libalpm / sync.c
blobe6923abe0645357c8ee305a55b196d0470a6180e
1 /*
2 * sync.c
4 * Copyright (c) 2002-2007 by Judd Vinet <jvinet@zeroflux.org>
5 * Copyright (c) 2005 by Aurelien Foret <orelien@chez.com>
6 * Copyright (c) 2005 by Christian Hamar <krics@linuxforum.hu>
7 * Copyright (c) 2005, 2006 by Miklos Vajna <vmiklos@frugalware.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "config.h"
25 #include <sys/types.h> /* off_t */
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <fcntl.h>
29 #include <string.h>
30 #include <stdint.h> /* intmax_t */
31 #include <unistd.h>
32 #include <time.h>
33 #include <dirent.h>
35 /* libalpm */
36 #include "sync.h"
37 #include "alpm_list.h"
38 #include "log.h"
39 #include "package.h"
40 #include "db.h"
41 #include "cache.h"
42 #include "deps.h"
43 #include "conflict.h"
44 #include "trans.h"
45 #include "util.h"
46 #include "handle.h"
47 #include "alpm.h"
48 #include "dload.h"
49 #include "delta.h"
51 /* Find recommended replacements for packages during a sync.
53 static int find_replacements(pmtrans_t *trans, pmdb_t *db_local, alpm_list_t *dbs_sync)
55 alpm_list_t *i, *j, *k; /* wow */
57 ALPM_LOG_FUNC;
59 /* check for "recommended" package replacements */
60 _alpm_log(PM_LOG_DEBUG, "checking for package replacements\n");
61 for(i = dbs_sync; i; i = i->next) {
62 pmdb_t *db = i->data;
64 /* for each db, check each package's REPLACES list */
65 for(j = _alpm_db_get_pkgcache(db); j; j = j->next) {
66 pmpkg_t *spkg = j->data;
68 for(k = alpm_pkg_get_replaces(spkg); k; k = k->next) {
69 const char *replacement = k->data;
71 pmpkg_t *lpkg = _alpm_db_get_pkgfromcache(db_local, replacement);
72 if(!lpkg) {
73 continue;
76 _alpm_log(PM_LOG_DEBUG, "checking replacement '%s' for package '%s'\n",
77 replacement, spkg->name);
78 /* ignore if EITHER the local or replacement package are to be ignored */
79 if(_alpm_pkg_should_ignore(spkg) || _alpm_pkg_should_ignore(lpkg)) {
80 _alpm_log(PM_LOG_WARNING, _("%s-%s: ignoring package upgrade (to be replaced by %s-%s)\n"),
81 alpm_pkg_get_name(lpkg), alpm_pkg_get_version(lpkg),
82 alpm_pkg_get_name(spkg), alpm_pkg_get_version(spkg));
83 } else {
84 int doreplace = 0;
85 QUESTION(trans, PM_TRANS_CONV_REPLACE_PKG, lpkg, spkg, db->treename, &doreplace);
86 if(!doreplace) {
87 continue;
90 /* if confirmed, add this to the 'final' list, designating 'lpkg' as
91 * the package to replace.
94 /* check if spkg->name is already in the packages list. */
95 /* TODO: same package name doesn't mean same package */
96 pmpkg_t *tpkg = _alpm_pkg_find(trans->packages, alpm_pkg_get_name(spkg));
97 if(tpkg) {
98 /* found it -- just append to the removes list */
99 tpkg->removes = alpm_list_add(tpkg->removes, lpkg);
100 /* check the to-be-replaced package's reason field */
101 if(alpm_pkg_get_reason(lpkg) == PM_PKG_REASON_EXPLICIT) {
102 tpkg->reason = PM_PKG_REASON_EXPLICIT;
104 } else {
105 /* none found -- enter pkg into the final sync list */
106 /* copy over reason */
107 spkg->reason = alpm_pkg_get_reason(lpkg);
108 spkg->removes = alpm_list_add(NULL, lpkg);
109 _alpm_log(PM_LOG_DEBUG, "adding package %s-%s to the transaction targets\n",
110 alpm_pkg_get_name(spkg), alpm_pkg_get_version(spkg));
111 trans->packages = alpm_list_add(trans->packages, spkg);
113 _alpm_log(PM_LOG_DEBUG, "%s-%s elected for removal (to be replaced by %s-%s)\n",
114 alpm_pkg_get_name(lpkg), alpm_pkg_get_version(lpkg),
115 alpm_pkg_get_name(spkg), alpm_pkg_get_version(spkg));
120 return(0);
123 /** Check for new version of pkg in sync repos
124 * (only the first occurrence is considered in sync)
126 pmpkg_t SYMEXPORT *alpm_sync_newversion(pmpkg_t *pkg, alpm_list_t *dbs_sync)
128 alpm_list_t *i;
129 pmpkg_t *spkg = NULL;
130 int cmp;
132 for(i = dbs_sync; !spkg && i; i = i->next) {
133 spkg = _alpm_db_get_pkgfromcache(i->data, alpm_pkg_get_name(pkg));
136 if(spkg == NULL) {
137 _alpm_log(PM_LOG_DEBUG, "'%s' not found in sync db => no upgrade\n",
138 alpm_pkg_get_name(pkg));
139 return(NULL);
142 /* compare versions and see if spkg is an upgrade */
143 cmp = _alpm_pkg_compare_versions(spkg, pkg);
144 if(cmp > 0) {
145 _alpm_log(PM_LOG_DEBUG, "new version of '%s' found (%s => %s)\n",
146 alpm_pkg_get_name(pkg), alpm_pkg_get_version(pkg),
147 alpm_pkg_get_version(spkg));
148 return(spkg);
150 if (cmp < 0) {
151 pmdb_t *db = spkg->origin_data.db;
152 _alpm_log(PM_LOG_WARNING, _("%s: local (%s) is newer than %s (%s)\n"),
153 alpm_pkg_get_name(pkg), alpm_pkg_get_version(pkg),
154 alpm_db_get_name(db), alpm_pkg_get_version(spkg));
156 return(NULL);
159 int _alpm_sync_sysupgrade(pmtrans_t *trans, pmdb_t *db_local, alpm_list_t *dbs_sync)
161 alpm_list_t *i, *j, *replaced = NULL;
163 ALPM_LOG_FUNC;
165 /* check for "recommended" package replacements */
166 if(find_replacements(trans, db_local, dbs_sync)) {
167 return(-1);
170 /* compute the to-be-replaced packages for efficiency */
171 for(i = trans->packages; i; i = i->next) {
172 pmpkg_t *spkg = i->data;
173 for(j = spkg->removes; j; j = j->next) {
174 replaced = alpm_list_add(replaced, j->data);
178 /* for all not-replaced local package we check for upgrade */
179 _alpm_log(PM_LOG_DEBUG, "checking for package upgrades\n");
180 for(i = _alpm_db_get_pkgcache(db_local); i; i = i->next) {
181 pmpkg_t *local = i->data;
183 if(_alpm_pkg_find(replaced, alpm_pkg_get_name(local))) {
184 _alpm_log(PM_LOG_DEBUG, "'%s' is already elected for removal -- skipping\n",
185 alpm_pkg_get_name(local));
186 continue;
189 pmpkg_t *spkg = alpm_sync_newversion(local, dbs_sync);
190 if(spkg) {
191 /* we found a new version */
192 /* skip packages in IgnorePkg or in IgnoreGroup */
193 if(_alpm_pkg_should_ignore(spkg)) {
194 _alpm_log(PM_LOG_WARNING, _("%s: ignoring package upgrade (%s => %s)\n"),
195 alpm_pkg_get_name(local), alpm_pkg_get_version(local),
196 alpm_pkg_get_version(spkg));
197 continue;
200 /* add the upgrade package to the target list */
201 if(_alpm_pkg_find(trans->packages, alpm_pkg_get_name(spkg))) {
202 /* avoid duplicated targets */
203 continue;
205 _alpm_log(PM_LOG_DEBUG, "adding package %s-%s to the transaction targets\n",
206 alpm_pkg_get_name(spkg), alpm_pkg_get_version(spkg));
207 /* we don't set any reason here,
208 it will be calculated from local package in add_commit */
209 trans->packages = alpm_list_add(trans->packages, spkg);
213 alpm_list_free(replaced);
214 return(0);
217 int _alpm_sync_addtarget(pmtrans_t *trans, pmdb_t *db_local, alpm_list_t *dbs_sync, char *name)
219 char *targline;
220 char *targ;
221 alpm_list_t *j;
222 pmpkg_t *local, *spkg;
223 pmdepend_t *dep; /* provisions and dependencies are also allowed */
225 ALPM_LOG_FUNC;
227 ASSERT(db_local != NULL, RET_ERR(PM_ERR_DB_NULL, -1));
228 ASSERT(trans != NULL, RET_ERR(PM_ERR_TRANS_NULL, -1));
229 ASSERT(name != NULL, RET_ERR(PM_ERR_WRONG_ARGS, -1));
231 STRDUP(targline, name, RET_ERR(PM_ERR_MEMORY, -1));
232 targ = strchr(targline, '/');
233 if(targ) {
234 /* we are looking for a package in a specific database */
235 alpm_list_t *dbs = NULL;
236 *targ = '\0';
237 targ++;
238 _alpm_log(PM_LOG_DEBUG, "searching for target '%s' in repo '%s'\n", targ, targline);
239 for(j = dbs_sync; j; j = j->next) {
240 pmdb_t *db = j->data;
241 if(strcmp(db->treename, targline) == 0) {
242 dbs = alpm_list_add(NULL, db);
243 break;
246 if(dbs == NULL) {
247 _alpm_log(PM_LOG_ERROR, _("repository '%s' not found\n"), targline);
248 FREE(targline);
249 RET_ERR(PM_ERR_PKG_REPO_NOT_FOUND, -1);
251 dep = _alpm_splitdep(targ);
252 spkg = _alpm_resolvedep(dep, dbs, NULL, 1);
253 _alpm_dep_free(dep);
254 alpm_list_free(dbs);
255 } else {
256 dep = _alpm_splitdep(targline);
257 spkg = _alpm_resolvedep(dep, dbs_sync, NULL, 1);
258 _alpm_dep_free(dep);
260 FREE(targline);
262 if(spkg == NULL) {
263 /* pm_errno is set by _alpm_resolvedep */
264 return(-1);
267 if(_alpm_pkg_find(trans->packages, alpm_pkg_get_name(spkg))) {
268 RET_ERR(PM_ERR_TRANS_DUP_TARGET, -1);
271 local = _alpm_db_get_pkgfromcache(db_local, alpm_pkg_get_name(spkg));
272 if(local) {
273 int cmp = _alpm_pkg_compare_versions(spkg, local);
274 if(cmp == 0) {
275 if(trans->flags & PM_TRANS_FLAG_NEEDED) {
276 /* with the NEEDED flag, packages up to date are not reinstalled */
277 _alpm_log(PM_LOG_WARNING, _("%s-%s is up to date -- skipping\n"),
278 alpm_pkg_get_name(local), alpm_pkg_get_version(local));
279 return(0);
280 } else {
281 _alpm_log(PM_LOG_WARNING, _("%s-%s is up to date -- reinstalling\n"),
282 alpm_pkg_get_name(local), alpm_pkg_get_version(local));
285 } else if(cmp < 0) {
286 /* local version is newer */
287 _alpm_log(PM_LOG_WARNING, _("downgrading package %s (%s => %s)\n"),
288 alpm_pkg_get_name(local), alpm_pkg_get_version(local),
289 alpm_pkg_get_version(spkg));
293 /* add the package to the transaction */
294 spkg->reason = PM_PKG_REASON_EXPLICIT;
295 _alpm_log(PM_LOG_DEBUG, "adding package %s-%s to the transaction targets\n",
296 alpm_pkg_get_name(spkg), alpm_pkg_get_version(spkg));
297 trans->packages = alpm_list_add(trans->packages, spkg);
299 return(0);
302 /** Compute the size of the files that will be downloaded to install a
303 * package.
304 * @param newpkg the new package to upgrade to
306 static int compute_download_size(pmpkg_t *newpkg)
308 const char *fname;
309 char *fpath;
310 off_t size = 0;
312 fname = alpm_pkg_get_filename(newpkg);
313 ASSERT(fname != NULL, RET_ERR(PM_ERR_PKG_INVALID_NAME, -1));
314 fpath = _alpm_filecache_find(fname);
316 if(fpath) {
317 FREE(fpath);
318 size = 0;
319 } else if(handle->usedelta) {
320 off_t dltsize;
321 off_t pkgsize = alpm_pkg_get_size(newpkg);
323 dltsize = _alpm_shortest_delta_path(
324 alpm_pkg_get_deltas(newpkg),
325 alpm_pkg_get_filename(newpkg),
326 &newpkg->delta_path);
328 if(newpkg->delta_path && (dltsize < pkgsize * MAX_DELTA_RATIO)) {
329 _alpm_log(PM_LOG_DEBUG, "using delta size\n");
330 size = dltsize;
331 } else {
332 _alpm_log(PM_LOG_DEBUG, "using package size\n");
333 size = alpm_pkg_get_size(newpkg);
334 alpm_list_free(newpkg->delta_path);
335 newpkg->delta_path = NULL;
337 } else {
338 size = alpm_pkg_get_size(newpkg);
341 _alpm_log(PM_LOG_DEBUG, "setting download size %jd for pkg %s\n",
342 (intmax_t)size, alpm_pkg_get_name(newpkg));
344 newpkg->download_size = size;
345 return(0);
348 int _alpm_sync_prepare(pmtrans_t *trans, pmdb_t *db_local, alpm_list_t *dbs_sync, alpm_list_t **data)
350 alpm_list_t *deps = NULL;
351 alpm_list_t *unresolvable = NULL;
352 alpm_list_t *remove = NULL; /* allow checkdeps usage with trans->packages */
353 alpm_list_t *i, *j;
354 int ret = 0;
356 ALPM_LOG_FUNC;
358 ASSERT(db_local != NULL, RET_ERR(PM_ERR_DB_NULL, -1));
359 ASSERT(trans != NULL, RET_ERR(PM_ERR_TRANS_NULL, -1));
361 if(data) {
362 *data = NULL;
365 if(!(trans->flags & PM_TRANS_FLAG_NODEPS)) {
366 alpm_list_t *resolved = NULL; /* target list after resolvedeps */
368 /* Build up list by repeatedly resolving each transaction package */
369 /* Resolve targets dependencies */
370 EVENT(trans, PM_TRANS_EVT_RESOLVEDEPS_START, NULL, NULL);
371 _alpm_log(PM_LOG_DEBUG, "resolving target's dependencies\n");
373 /* build remove list for resolvedeps */
374 for(i = trans->packages; i; i = i->next) {
375 pmpkg_t *spkg = i->data;
376 for(j = spkg->removes; j; j = j->next) {
377 remove = alpm_list_add(remove, j->data);
381 /* Resolve packages in the transaction one at a time, in addtion
382 building up a list of packages which could not be resolved. */
383 for(i = trans->packages; i; i = i->next) {
384 pmpkg_t *pkg = i->data;
385 if(_alpm_resolvedeps(db_local, dbs_sync, pkg, &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 */
392 /* If there were unresolvable top-level packages, prompt the user to
393 see if they'd like to ignore them rather than failing the sync */
394 if(unresolvable != NULL) {
395 unresolvable = alpm_list_remove_dupes(unresolvable);
396 int remove_unresolvable = 0;
397 QUESTION(handle->trans, PM_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 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 /* Unresolvable packages will be removed from the target list, so
419 we free the transaction specific field: pkg->removes */
420 for(i = unresolvable; i; i = i->next) {
421 pmpkg_t *pkg = i->data;
422 alpm_list_free(pkg->removes);
423 pkg->removes = NULL;
426 /* Set DEPEND reason for pulled packages */
427 for(i = resolved; i; i = i->next) {
428 pmpkg_t *pkg = i->data;
429 if(!_alpm_pkg_find(trans->packages, pkg->name)) {
430 pkg->reason = PM_PKG_REASON_DEPEND;
434 /* re-order w.r.t. dependencies */
435 alpm_list_free(trans->packages);
436 trans->packages = _alpm_sortbydeps(resolved, 0);
437 alpm_list_free(resolved);
439 EVENT(trans, PM_TRANS_EVT_RESOLVEDEPS_DONE, NULL, NULL);
442 if(!(trans->flags & PM_TRANS_FLAG_NOCONFLICTS)) {
443 /* check for inter-conflicts and whatnot */
444 EVENT(trans, PM_TRANS_EVT_INTERCONFLICTS_START, NULL, NULL);
446 _alpm_log(PM_LOG_DEBUG, "looking for conflicts\n");
448 /* 1. check for conflicts in the target list */
449 _alpm_log(PM_LOG_DEBUG, "check targets vs targets\n");
450 deps = _alpm_innerconflicts(trans->packages);
452 for(i = deps; i; i = i->next) {
453 pmconflict_t *conflict = i->data;
454 pmpkg_t *rsync, *sync, *sync1, *sync2;
456 /* have we already removed one of the conflicting targets? */
457 sync1 = _alpm_pkg_find(trans->packages, conflict->package1);
458 sync2 = _alpm_pkg_find(trans->packages, conflict->package2);
459 if(!sync1 || !sync2) {
460 continue;
463 _alpm_log(PM_LOG_DEBUG, "conflicting packages in the sync list: '%s' <-> '%s'\n",
464 conflict->package1, conflict->package2);
466 /* if sync1 provides sync2, we remove sync2 from the targets, and vice versa */
467 pmdepend_t *dep1 = _alpm_splitdep(conflict->package1);
468 pmdepend_t *dep2 = _alpm_splitdep(conflict->package2);
469 if(alpm_depcmp(sync1, dep2)) {
470 rsync = sync2;
471 sync = sync1;
472 } else if(alpm_depcmp(sync2, dep1)) {
473 rsync = sync1;
474 sync = sync2;
475 } else {
476 _alpm_log(PM_LOG_ERROR, _("unresolvable package conflicts detected\n"));
477 pm_errno = PM_ERR_CONFLICTING_DEPS;
478 ret = -1;
479 if(data) {
480 pmconflict_t *newconflict = _alpm_conflict_dup(conflict);
481 if(newconflict) {
482 *data = alpm_list_add(*data, newconflict);
485 alpm_list_free_inner(deps, (alpm_list_fn_free)_alpm_conflict_free);
486 alpm_list_free(deps);
487 _alpm_dep_free(dep1);
488 _alpm_dep_free(dep2);
489 goto cleanup;
491 _alpm_dep_free(dep1);
492 _alpm_dep_free(dep2);
494 /* Prints warning */
495 _alpm_log(PM_LOG_WARNING,
496 _("removing '%s' from target list because it conflicts with '%s'\n"),
497 rsync->name, sync->name);
498 alpm_list_free(rsync->removes); /* rsync is not transaction target anymore */
499 rsync->removes = NULL;
500 trans->packages = alpm_list_remove(trans->packages, rsync, _alpm_pkg_cmp, NULL);
501 continue;
504 alpm_list_free_inner(deps, (alpm_list_fn_free)_alpm_conflict_free);
505 alpm_list_free(deps);
506 deps = NULL;
508 /* 2. we check for target vs db conflicts (and resolve)*/
509 _alpm_log(PM_LOG_DEBUG, "check targets vs db and db vs targets\n");
510 deps = _alpm_outerconflicts(db_local, trans->packages);
512 for(i = deps; i; i = i->next) {
513 pmconflict_t *conflict = i->data;
515 /* if conflict->package2 (the local package) is not elected for removal,
516 we ask the user */
517 int found = 0;
518 for(j = trans->packages; j && !found; j = j->next) {
519 pmpkg_t *spkg = j->data;
520 if(_alpm_pkg_find(spkg->removes, conflict->package2)) {
521 found = 1;
524 if(found) {
525 continue;
528 _alpm_log(PM_LOG_DEBUG, "package '%s' conflicts with '%s'\n",
529 conflict->package1, conflict->package2);
531 pmpkg_t *sync = _alpm_pkg_find(trans->packages, conflict->package1);
532 pmpkg_t *local = _alpm_db_get_pkgfromcache(db_local, conflict->package2);
533 int doremove = 0;
534 QUESTION(trans, PM_TRANS_CONV_CONFLICT_PKG, conflict->package1,
535 conflict->package2, NULL, &doremove);
536 if(doremove) {
537 /* append to the removes list */
538 _alpm_log(PM_LOG_DEBUG, "electing '%s' for removal\n", conflict->package2);
539 sync->removes = alpm_list_add(sync->removes, local);
540 } else { /* abort */
541 _alpm_log(PM_LOG_ERROR, _("unresolvable package conflicts detected\n"));
542 pm_errno = PM_ERR_CONFLICTING_DEPS;
543 ret = -1;
544 if(data) {
545 pmconflict_t *newconflict = _alpm_conflict_dup(conflict);
546 if(newconflict) {
547 *data = alpm_list_add(*data, newconflict);
550 alpm_list_free_inner(deps, (alpm_list_fn_free)_alpm_conflict_free);
551 alpm_list_free(deps);
552 goto cleanup;
555 EVENT(trans, PM_TRANS_EVT_INTERCONFLICTS_DONE, NULL, NULL);
556 alpm_list_free_inner(deps, (alpm_list_fn_free)_alpm_conflict_free);
557 alpm_list_free(deps);
560 if(!(trans->flags & PM_TRANS_FLAG_NODEPS)) {
561 /* rebuild remove list */
562 alpm_list_free(remove);
563 remove = NULL;
564 for(i = trans->packages; i; i = i->next) {
565 pmpkg_t *spkg = i->data;
566 for(j = spkg->removes; j; j = j->next) {
567 remove = alpm_list_add(remove, j->data);
571 _alpm_log(PM_LOG_DEBUG, "checking dependencies\n");
572 deps = alpm_checkdeps(_alpm_db_get_pkgcache(db_local), 1, remove, trans->packages);
573 if(deps) {
574 pm_errno = PM_ERR_UNSATISFIED_DEPS;
575 ret = -1;
576 if(data) {
577 *data = deps;
578 } else {
579 alpm_list_free_inner(deps, (alpm_list_fn_free)_alpm_depmiss_free);
580 alpm_list_free(deps);
582 goto cleanup;
585 for(i = trans->packages; i; i = i->next) {
586 /* update download size field */
587 pmpkg_t *spkg = i->data;
588 if(compute_download_size(spkg) != 0) {
589 ret = -1;
590 goto cleanup;
594 cleanup:
595 alpm_list_free(remove);
596 alpm_list_free(unresolvable);
598 return(ret);
601 /** Returns the size of the files that will be downloaded to install a
602 * package.
603 * @param newpkg the new package to upgrade to
604 * @return the size of the download
606 off_t SYMEXPORT alpm_pkg_download_size(pmpkg_t *newpkg)
608 return(newpkg->download_size);
611 static int endswith(const char *filename, const char *extension)
613 const char *s = filename + strlen(filename) - strlen(extension);
614 return(strcmp(s, extension) == 0);
617 /** Applies delta files to create an upgraded package file.
619 * All intermediate files are deleted, leaving only the starting and
620 * ending package files.
622 * @param trans the transaction
624 * @return 0 if all delta files were able to be applied, 1 otherwise.
626 static int apply_deltas(pmtrans_t *trans)
628 alpm_list_t *i;
629 int ret = 0;
630 const char *cachedir = _alpm_filecache_setup();
632 for(i = trans->packages; i; i = i->next) {
633 pmpkg_t *spkg = i->data;
634 alpm_list_t *delta_path = spkg->delta_path;
635 alpm_list_t *dlts = NULL;
637 if(!delta_path) {
638 continue;
641 for(dlts = delta_path; dlts; dlts = dlts->next) {
642 pmdelta_t *d = dlts->data;
643 char *delta, *from, *to;
644 char command[PATH_MAX];
645 int len = 0;
647 delta = _alpm_filecache_find(d->delta);
648 /* the initial package might be in a different cachedir */
649 if(dlts == delta_path) {
650 from = _alpm_filecache_find(d->from);
651 } else {
652 /* len = cachedir len + from len + '/' + null */
653 len = strlen(cachedir) + strlen(d->from) + 2;
654 CALLOC(from, len, sizeof(char), RET_ERR(PM_ERR_MEMORY, 1));
655 snprintf(from, len, "%s/%s", cachedir, d->from);
657 len = strlen(cachedir) + strlen(d->to) + 2;
658 CALLOC(to, len, sizeof(char), RET_ERR(PM_ERR_MEMORY, 1));
659 snprintf(to, len, "%s/%s", cachedir, d->to);
661 /* build the patch command */
662 /* compression command */
663 char *compress = "cat";
664 if(endswith(to, ".gz")) {
665 compress = "gzip -n";
666 } else if(endswith(to, ".bz2")) {
667 compress = "bzip";
669 /* -R for disabling external recompression, -c for sending to stdout */
670 snprintf(command, PATH_MAX, "xdelta3 -d -q -R -c -s %s %s | %s > %s", from, delta, compress, to);
672 _alpm_log(PM_LOG_DEBUG, "command: %s\n", command);
674 EVENT(trans, PM_TRANS_EVT_DELTA_PATCH_START, d->to, d->delta);
676 int retval = system(command);
677 if(retval == 0) {
678 EVENT(trans, PM_TRANS_EVT_DELTA_PATCH_DONE, NULL, NULL);
680 /* delete the delta file */
681 unlink(delta);
683 /* Delete the 'from' package but only if it is an intermediate
684 * package. The starting 'from' package should be kept, just
685 * as if deltas were not used. */
686 if(dlts != delta_path) {
687 unlink(from);
690 FREE(from);
691 FREE(to);
692 FREE(delta);
694 if(retval != 0) {
695 /* one delta failed for this package, cancel the remaining ones */
696 EVENT(trans, PM_TRANS_EVT_DELTA_PATCH_FAILED, NULL, NULL);
697 ret = 1;
698 break;
703 return(ret);
706 /** Compares the md5sum of a file to the expected value.
708 * If the md5sum does not match, the user is asked whether the file
709 * should be deleted.
711 * @param trans the transaction
712 * @param filename the filename of the file to test
713 * @param md5sum the expected md5sum of the file
715 * @return 0 if the md5sum matched, 1 if not, -1 in case of errors
717 static int test_md5sum(pmtrans_t *trans, const char *filename,
718 const char *md5sum)
720 char *filepath;
721 int ret;
723 filepath = _alpm_filecache_find(filename);
725 ret = _alpm_test_md5sum(filepath, md5sum);
727 if(ret == 1) {
728 int doremove = 0;
729 QUESTION(trans, PM_TRANS_CONV_CORRUPTED_PKG, (char *)filename,
730 NULL, NULL, &doremove);
731 if(doremove) {
732 unlink(filepath);
736 FREE(filepath);
738 return(ret);
741 int _alpm_sync_commit(pmtrans_t *trans, pmdb_t *db_local, alpm_list_t **data)
743 alpm_list_t *i, *j, *files = NULL;
744 alpm_list_t *deltas = NULL;
745 pmtrans_t *tr_remove = NULL, *tr_upgrade = NULL;
746 int replaces = 0;
747 int errors = 0;
748 const char *cachedir = NULL;
749 int ret = -1;
751 ALPM_LOG_FUNC;
753 ASSERT(trans != NULL, RET_ERR(PM_ERR_TRANS_NULL, -1));
755 cachedir = _alpm_filecache_setup();
756 trans->state = STATE_DOWNLOADING;
758 /* Total progress - figure out the total download size if required to
759 * pass to the callback. This function is called once, and it is up to the
760 * frontend to compute incremental progress. */
761 if(handle->totaldlcb) {
762 off_t total_size = (off_t)0;
763 /* sum up the download size for each package and store total */
764 for(i = trans->packages; i; i = i->next) {
765 pmpkg_t *spkg = i->data;
766 total_size += spkg->download_size;
768 handle->totaldlcb(total_size);
771 /* group sync records by repository and download */
772 for(i = handle->dbs_sync; i; i = i->next) {
773 pmdb_t *current = i->data;
775 for(j = trans->packages; j; j = j->next) {
776 pmpkg_t *spkg = j->data;
777 pmdb_t *dbs = spkg->origin_data.db;
779 if(current == dbs) {
780 const char *fname = NULL;
782 fname = alpm_pkg_get_filename(spkg);
783 ASSERT(fname != NULL, RET_ERR(PM_ERR_PKG_INVALID_NAME, -1));
784 alpm_list_t *delta_path = spkg->delta_path;
785 if(delta_path) {
786 /* using deltas */
787 alpm_list_t *dlts = NULL;
789 for(dlts = delta_path; dlts; dlts = dlts->next) {
790 pmdelta_t *d = dlts->data;
792 if(d->download_size != 0) {
793 /* add the delta filename to the download list if needed */
794 files = alpm_list_add(files, strdup(d->delta));
797 /* keep a list of all the delta files for md5sums */
798 deltas = alpm_list_add(deltas, d);
801 } else {
802 /* not using deltas */
803 if(spkg->download_size != 0) {
804 /* add the filename to the download list if needed */
805 files = alpm_list_add(files, strdup(fname));
812 if(files) {
813 EVENT(trans, PM_TRANS_EVT_RETRIEVE_START, current->treename, NULL);
814 if(_alpm_download_files(files, current->servers, cachedir)) {
815 _alpm_log(PM_LOG_WARNING, _("failed to retrieve some files from %s\n"),
816 current->treename);
817 pm_errno = PM_ERR_RETRIEVE;
818 goto error;
820 FREELIST(files);
824 /* clear out value to let callback know we are done */
825 if(handle->totaldlcb) {
826 handle->totaldlcb(0);
829 /* if we have deltas to work with */
830 if(handle->usedelta && deltas) {
831 int ret = 0;
832 errors = 0;
833 /* Check integrity of deltas */
834 EVENT(trans, PM_TRANS_EVT_DELTA_INTEGRITY_START, NULL, NULL);
836 for(i = deltas; i; i = i->next) {
837 pmdelta_t *d = alpm_list_getdata(i);
838 const char *filename = alpm_delta_get_filename(d);
839 const char *md5sum = alpm_delta_get_md5sum(d);
841 if(test_md5sum(trans, filename, md5sum) != 0) {
842 errors++;
843 *data = alpm_list_add(*data, strdup(filename));
846 if(errors) {
847 pm_errno = PM_ERR_DLT_INVALID;
848 goto error;
850 EVENT(trans, PM_TRANS_EVT_DELTA_INTEGRITY_DONE, NULL, NULL);
852 /* Use the deltas to generate the packages */
853 EVENT(trans, PM_TRANS_EVT_DELTA_PATCHES_START, NULL, NULL);
854 ret = apply_deltas(trans);
855 EVENT(trans, PM_TRANS_EVT_DELTA_PATCHES_DONE, NULL, NULL);
857 if(ret) {
858 pm_errno = PM_ERR_DLT_PATCHFAILED;
859 goto error;
863 /* Check integrity of packages */
864 EVENT(trans, PM_TRANS_EVT_INTEGRITY_START, NULL, NULL);
866 errors = 0;
867 for(i = trans->packages; i; i = i->next) {
868 pmpkg_t *spkg = i->data;
869 const char *filename = alpm_pkg_get_filename(spkg);
870 const char *md5sum = alpm_pkg_get_md5sum(spkg);
872 if(test_md5sum(trans, filename, md5sum) != 0) {
873 errors++;
874 *data = alpm_list_add(*data, strdup(filename));
877 if(errors) {
878 pm_errno = PM_ERR_PKG_INVALID;
879 goto error;
881 EVENT(trans, PM_TRANS_EVT_INTEGRITY_DONE, NULL, NULL);
882 if(trans->flags & PM_TRANS_FLAG_DOWNLOADONLY) {
883 return(0);
886 trans->state = STATE_COMMITING;
888 /* Create remove and upgrade transactions */
889 tr_remove = _alpm_trans_new();
890 if(tr_remove == NULL) {
891 _alpm_log(PM_LOG_ERROR, _("could not create removal transaction\n"));
892 goto error;
894 tr_upgrade = _alpm_trans_new();
895 if(tr_upgrade == NULL) {
896 _alpm_log(PM_LOG_ERROR, _("could not create transaction\n"));
897 goto error;
900 if(_alpm_trans_init(tr_remove, PM_TRANS_TYPE_REMOVE, PM_TRANS_FLAG_NODEPS, NULL, NULL, NULL) == -1) {
901 _alpm_log(PM_LOG_ERROR, _("could not initialize the removal transaction\n"));
902 goto error;
904 if(_alpm_trans_init(tr_upgrade, PM_TRANS_TYPE_UPGRADE, trans->flags, trans->cb_event, trans->cb_conv, trans->cb_progress) == -1) {
905 _alpm_log(PM_LOG_ERROR, _("could not initialize transaction\n"));
906 goto error;
909 /* adding targets */
910 for(i = trans->packages; i; i = i->next) {
911 pmpkg_t *spkg = i->data;
912 alpm_list_t *j;
913 /* remove transaction */
914 for(j = spkg->removes; j; j = j->next) {
915 pmpkg_t *pkg = j->data;
916 if(!_alpm_pkg_find(tr_remove->packages, pkg->name)) {
917 if(_alpm_trans_addtarget(tr_remove, pkg->name) == -1) {
918 goto error;
920 replaces++;
923 /* upgrade transaction */
924 const char *fname;
925 char *fpath;
927 fname = alpm_pkg_get_filename(spkg);
928 if(fname == NULL) {
929 goto error;
931 /* Loop through the cache dirs until we find a matching file */
932 fpath = _alpm_filecache_find(fname);
934 if(_alpm_trans_addtarget(tr_upgrade, fpath) == -1) {
935 FREE(fpath);
936 goto error;
938 FREE(fpath);
940 /* using alpm_list_last() is ok because addtarget() adds the new target at the
941 * end of the tr->packages list */
942 pmpkg_t *ipkg = alpm_list_last(tr_upgrade->packages)->data;
943 ipkg->reason = spkg->reason;
946 /* fileconflict check */
947 if(!(trans->flags & PM_TRANS_FLAG_FORCE)) {
948 EVENT(trans, PM_TRANS_EVT_FILECONFLICTS_START, NULL, NULL);
950 _alpm_log(PM_LOG_DEBUG, "looking for file conflicts\n");
951 alpm_list_t *conflict = _alpm_db_find_fileconflicts(db_local, tr_upgrade,
952 tr_upgrade->packages, tr_remove->packages);
953 if(conflict) {
954 pm_errno = PM_ERR_FILE_CONFLICTS;
955 if(data) {
956 *data = conflict;
957 } else {
958 alpm_list_free_inner(conflict, (alpm_list_fn_free)_alpm_fileconflict_free);
959 alpm_list_free(conflict);
961 goto error;
964 EVENT(trans, PM_TRANS_EVT_FILECONFLICTS_DONE, NULL, NULL);
967 /* remove conflicting and to-be-replaced packages */
968 if(replaces) {
969 _alpm_log(PM_LOG_DEBUG, "removing conflicting and to-be-replaced packages\n");
970 if(_alpm_trans_prepare(tr_remove, data) == -1) {
971 _alpm_log(PM_LOG_ERROR, _("could not prepare removal transaction\n"));
972 goto error;
974 /* we want the frontend to be aware of commit details */
975 tr_remove->cb_event = trans->cb_event;
976 if(_alpm_trans_commit(tr_remove, NULL) == -1) {
977 _alpm_log(PM_LOG_ERROR, _("could not commit removal transaction\n"));
978 goto error;
982 /* install targets */
983 _alpm_log(PM_LOG_DEBUG, "installing packages\n");
984 /* add_prepare is not needed */
985 if(_alpm_trans_commit(tr_upgrade, NULL) == -1) {
986 _alpm_log(PM_LOG_ERROR, _("could not commit transaction\n"));
987 goto error;
989 ret = 0;
991 error:
992 FREELIST(files);
993 alpm_list_free(deltas);
994 _alpm_trans_free(tr_remove);
995 _alpm_trans_free(tr_upgrade);
996 return(ret);
999 /* vim: set ts=2 sw=2 noet: */