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/>.
25 #include <sys/types.h> /* off_t */
30 #include <stdint.h> /* intmax_t */
37 #include "alpm_list.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 */
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
) {
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
);
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
));
85 QUESTION(trans
, PM_TRANS_CONV_REPLACE_PKG
, lpkg
, spkg
, db
->treename
, &doreplace
);
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
));
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
;
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
));
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
)
129 pmpkg_t
*spkg
= NULL
;
132 for(i
= dbs_sync
; !spkg
&& i
; i
= i
->next
) {
133 spkg
= _alpm_db_get_pkgfromcache(i
->data
, alpm_pkg_get_name(pkg
));
137 _alpm_log(PM_LOG_DEBUG
, "'%s' not found in sync db => no upgrade\n",
138 alpm_pkg_get_name(pkg
));
142 /* compare versions and see if spkg is an upgrade */
143 cmp
= _alpm_pkg_compare_versions(spkg
, pkg
);
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
));
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
));
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
;
165 /* check for "recommended" package replacements */
166 if(find_replacements(trans
, db_local
, dbs_sync
)) {
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
));
189 pmpkg_t
*spkg
= alpm_sync_newversion(local
, dbs_sync
);
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
));
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 */
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
);
217 int _alpm_sync_addtarget(pmtrans_t
*trans
, pmdb_t
*db_local
, alpm_list_t
*dbs_sync
, char *name
)
222 pmpkg_t
*local
, *spkg
;
223 pmdepend_t
*dep
; /* provisions and dependencies are also allowed */
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
, '/');
234 /* we are looking for a package in a specific database */
235 alpm_list_t
*dbs
= NULL
;
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
);
247 _alpm_log(PM_LOG_ERROR
, _("repository '%s' not found\n"), targline
);
249 RET_ERR(PM_ERR_PKG_REPO_NOT_FOUND
, -1);
251 dep
= _alpm_splitdep(targ
);
252 spkg
= _alpm_resolvedep(dep
, dbs
, NULL
, 1);
256 dep
= _alpm_splitdep(targline
);
257 spkg
= _alpm_resolvedep(dep
, dbs_sync
, NULL
, 1);
263 /* pm_errno is set by _alpm_resolvedep */
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
));
273 int cmp
= _alpm_pkg_compare_versions(spkg
, local
);
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
));
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
));
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
);
302 /** Compute the size of the files that will be downloaded to install a
304 * @param newpkg the new package to upgrade to
306 static int compute_download_size(pmpkg_t
*newpkg
)
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
);
319 } else if(handle
->usedelta
) {
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");
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
;
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
;
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 */
358 ASSERT(db_local
!= NULL
, RET_ERR(PM_ERR_DB_NULL
, -1));
359 ASSERT(trans
!= NULL
, RET_ERR(PM_ERR_TRANS_NULL
, -1));
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 */
406 alpm_list_free_inner(*data
, (alpm_list_fn_free
)_alpm_depmiss_free
);
407 alpm_list_free(*data
);
411 /* pm_errno is set by resolvedeps */
412 alpm_list_free(resolved
);
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
);
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
) {
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
)) {
472 } else if(alpm_depcmp(sync2
, dep1
)) {
476 _alpm_log(PM_LOG_ERROR
, _("unresolvable package conflicts detected\n"));
477 pm_errno
= PM_ERR_CONFLICTING_DEPS
;
480 pmconflict_t
*newconflict
= _alpm_conflict_dup(conflict
);
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
);
491 _alpm_dep_free(dep1
);
492 _alpm_dep_free(dep2
);
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
);
504 alpm_list_free_inner(deps
, (alpm_list_fn_free
)_alpm_conflict_free
);
505 alpm_list_free(deps
);
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,
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
)) {
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
);
534 QUESTION(trans
, PM_TRANS_CONV_CONFLICT_PKG
, conflict
->package1
,
535 conflict
->package2
, NULL
, &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
);
541 _alpm_log(PM_LOG_ERROR
, _("unresolvable package conflicts detected\n"));
542 pm_errno
= PM_ERR_CONFLICTING_DEPS
;
545 pmconflict_t
*newconflict
= _alpm_conflict_dup(conflict
);
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
);
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
);
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
);
574 pm_errno
= PM_ERR_UNSATISFIED_DEPS
;
579 alpm_list_free_inner(deps
, (alpm_list_fn_free
)_alpm_depmiss_free
);
580 alpm_list_free(deps
);
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) {
595 alpm_list_free(remove
);
596 alpm_list_free(unresolvable
);
601 /** Returns the size of the files that will be downloaded to install a
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
)
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
;
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
];
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
);
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")) {
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
);
678 EVENT(trans
, PM_TRANS_EVT_DELTA_PATCH_DONE
, NULL
, NULL
);
680 /* delete the delta file */
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
) {
695 /* one delta failed for this package, cancel the remaining ones */
696 EVENT(trans
, PM_TRANS_EVT_DELTA_PATCH_FAILED
, NULL
, NULL
);
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
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
,
723 filepath
= _alpm_filecache_find(filename
);
725 ret
= _alpm_test_md5sum(filepath
, md5sum
);
729 QUESTION(trans
, PM_TRANS_CONV_CORRUPTED_PKG
, (char *)filename
,
730 NULL
, NULL
, &doremove
);
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
;
748 const char *cachedir
= NULL
;
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
;
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
;
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
);
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
));
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"),
817 pm_errno
= PM_ERR_RETRIEVE
;
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
) {
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) {
843 *data
= alpm_list_add(*data
, strdup(filename
));
847 pm_errno
= PM_ERR_DLT_INVALID
;
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
);
858 pm_errno
= PM_ERR_DLT_PATCHFAILED
;
863 /* Check integrity of packages */
864 EVENT(trans
, PM_TRANS_EVT_INTEGRITY_START
, NULL
, NULL
);
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) {
874 *data
= alpm_list_add(*data
, strdup(filename
));
878 pm_errno
= PM_ERR_PKG_INVALID
;
881 EVENT(trans
, PM_TRANS_EVT_INTEGRITY_DONE
, NULL
, NULL
);
882 if(trans
->flags
& PM_TRANS_FLAG_DOWNLOADONLY
) {
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"));
894 tr_upgrade
= _alpm_trans_new();
895 if(tr_upgrade
== NULL
) {
896 _alpm_log(PM_LOG_ERROR
, _("could not create transaction\n"));
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"));
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"));
910 for(i
= trans
->packages
; i
; i
= i
->next
) {
911 pmpkg_t
*spkg
= i
->data
;
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) {
923 /* upgrade transaction */
927 fname
= alpm_pkg_get_filename(spkg
);
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) {
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
);
954 pm_errno
= PM_ERR_FILE_CONFLICTS
;
958 alpm_list_free_inner(conflict
, (alpm_list_fn_free
)_alpm_fileconflict_free
);
959 alpm_list_free(conflict
);
964 EVENT(trans
, PM_TRANS_EVT_FILECONFLICTS_DONE
, NULL
, NULL
);
967 /* remove conflicting and to-be-replaced packages */
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"));
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"));
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"));
993 alpm_list_free(deltas
);
994 _alpm_trans_free(tr_remove
);
995 _alpm_trans_free(tr_upgrade
);
999 /* vim: set ts=2 sw=2 noet: */