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/>.
26 #include <sys/types.h> /* off_t */
30 #include <stdint.h> /* intmax_t */
36 #include "alpm_list.h"
50 #include "diskspace.h"
53 /* global handle variable */
54 extern pmhandle_t
*handle
;
56 /** Check for new version of pkg in sync repos
57 * (only the first occurrence is considered in sync)
59 pmpkg_t SYMEXPORT
*alpm_sync_newversion(pmpkg_t
*pkg
, alpm_list_t
*dbs_sync
)
61 ASSERT(pkg
!= NULL
, return NULL
);
66 for(i
= dbs_sync
; !spkg
&& i
; i
= i
->next
) {
67 spkg
= _alpm_db_get_pkgfromcache(i
->data
, alpm_pkg_get_name(pkg
));
71 _alpm_log(PM_LOG_DEBUG
, "'%s' not found in sync db => no upgrade\n",
72 alpm_pkg_get_name(pkg
));
76 /* compare versions and see if spkg is an upgrade */
77 if(_alpm_pkg_compare_versions(spkg
, pkg
) > 0) {
78 _alpm_log(PM_LOG_DEBUG
, "new version of '%s' found (%s => %s)\n",
79 alpm_pkg_get_name(pkg
), alpm_pkg_get_version(pkg
),
80 alpm_pkg_get_version(spkg
));
83 /* spkg is not an upgrade */
87 /** Search for packages to upgrade and add them to the transaction. */
88 int SYMEXPORT
alpm_sync_sysupgrade(int enable_downgrade
)
90 alpm_list_t
*i
, *j
, *k
;
93 alpm_list_t
*dbs_sync
;
95 ASSERT(handle
!= NULL
, RET_ERR(PM_ERR_HANDLE_NULL
, -1));
96 trans
= handle
->trans
;
97 db_local
= handle
->db_local
;
98 dbs_sync
= handle
->dbs_sync
;
99 ASSERT(trans
!= NULL
, RET_ERR(PM_ERR_TRANS_NULL
, -1));
100 ASSERT(trans
->state
== STATE_INITIALIZED
, RET_ERR(PM_ERR_TRANS_NOT_INITIALIZED
, -1));
102 _alpm_log(PM_LOG_DEBUG
, "checking for package upgrades\n");
103 for(i
= _alpm_db_get_pkgcache(db_local
); i
; i
= i
->next
) {
104 pmpkg_t
*lpkg
= i
->data
;
106 if(_alpm_pkg_find(trans
->add
, lpkg
->name
)) {
107 _alpm_log(PM_LOG_DEBUG
, "%s is already in the target list -- skipping\n", lpkg
->name
);
111 /* Search for literal then replacers in each sync database.
112 * If found, don't check other databases */
113 for(j
= dbs_sync
; j
; j
= j
->next
) {
114 pmdb_t
*sdb
= j
->data
;
116 pmpkg_t
*spkg
= _alpm_db_get_pkgfromcache(sdb
, lpkg
->name
);
118 /* 1. literal was found in sdb */
119 int cmp
= _alpm_pkg_compare_versions(spkg
, lpkg
);
121 _alpm_log(PM_LOG_DEBUG
, "new version of '%s' found (%s => %s)\n",
122 lpkg
->name
, lpkg
->version
, spkg
->version
);
123 /* check IgnorePkg/IgnoreGroup */
124 if(_alpm_pkg_should_ignore(spkg
) || _alpm_pkg_should_ignore(lpkg
)) {
125 _alpm_log(PM_LOG_WARNING
, _("%s: ignoring package upgrade (%s => %s)\n"),
126 lpkg
->name
, lpkg
->version
, spkg
->version
);
128 _alpm_log(PM_LOG_DEBUG
, "adding package %s-%s to the transaction targets\n",
129 spkg
->name
, spkg
->version
);
130 trans
->add
= alpm_list_add(trans
->add
, spkg
);
133 if(enable_downgrade
) {
134 /* check IgnorePkg/IgnoreGroup */
135 if(_alpm_pkg_should_ignore(spkg
) || _alpm_pkg_should_ignore(lpkg
)) {
136 _alpm_log(PM_LOG_WARNING
, _("%s: ignoring package downgrade (%s => %s)\n"),
137 lpkg
->name
, lpkg
->version
, spkg
->version
);
139 _alpm_log(PM_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
);
144 _alpm_log(PM_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 */
151 /* 2. search for replacers in sdb */
153 for(k
= _alpm_db_get_pkgcache(sdb
); k
; k
= k
->next
) {
155 if(alpm_list_find_str(alpm_pkg_get_replaces(spkg
), lpkg
->name
)) {
157 /* check IgnorePkg/IgnoreGroup */
158 if(_alpm_pkg_should_ignore(spkg
) || _alpm_pkg_should_ignore(lpkg
)) {
159 _alpm_log(PM_LOG_WARNING
, _("ignoring package replacement (%s-%s => %s-%s)\n"),
160 lpkg
->name
, lpkg
->version
, spkg
->name
, spkg
->version
);
165 QUESTION(trans
, PM_TRANS_CONV_REPLACE_PKG
, lpkg
, spkg
, sdb
->treename
, &doreplace
);
170 /* If spkg is already in the target list, we append lpkg to spkg's
172 pmpkg_t
*tpkg
= _alpm_pkg_find(trans
->add
, spkg
->name
);
174 /* sanity check, multiple repos can contain spkg->name */
175 if(tpkg
->origin_data
.db
!= sdb
) {
176 _alpm_log(PM_LOG_WARNING
, _("cannot replace %s by %s\n"),
177 lpkg
->name
, spkg
->name
);
180 _alpm_log(PM_LOG_DEBUG
, "appending %s to the removes list of %s\n",
181 lpkg
->name
, tpkg
->name
);
182 tpkg
->removes
= alpm_list_add(tpkg
->removes
, lpkg
);
183 /* check the to-be-replaced package's reason field */
184 if(alpm_pkg_get_reason(lpkg
) == PM_PKG_REASON_EXPLICIT
) {
185 tpkg
->reason
= PM_PKG_REASON_EXPLICIT
;
188 /* add spkg to the target list */
189 /* copy over reason */
190 spkg
->reason
= alpm_pkg_get_reason(lpkg
);
191 spkg
->removes
= alpm_list_add(NULL
, lpkg
);
192 _alpm_log(PM_LOG_DEBUG
, "adding package %s-%s to the transaction targets\n",
193 spkg
->name
, spkg
->version
);
194 trans
->add
= alpm_list_add(trans
->add
, spkg
);
199 break; /* jump to next local package */
208 /** Find group members across a list of databases.
209 * If a member exists in several databases, only the first database is used.
210 * IgnorePkg is also handled.
211 * @param dbs the list of pmdb_t *
212 * @pram name the name of the group
213 * @return the list of pmpkg_t * (caller is responsible for alpm_list_free)
215 alpm_list_t SYMEXPORT
*alpm_find_grp_pkgs(alpm_list_t
*dbs
,
218 alpm_list_t
*i
, *j
, *pkgs
= NULL
, *ignorelist
= NULL
;
220 for(i
= dbs
; i
; i
= i
->next
) {
221 pmdb_t
*db
= i
->data
;
222 pmgrp_t
*grp
= alpm_db_readgrp(db
, name
);
227 for(j
= alpm_grp_get_pkgs(grp
); j
; j
= j
->next
) {
228 pmpkg_t
*pkg
= j
->data
;
230 if(_alpm_pkg_find(ignorelist
, alpm_pkg_get_name(pkg
))) {
233 if(_alpm_pkg_should_ignore(pkg
)) {
234 ignorelist
= alpm_list_add(ignorelist
, pkg
);
236 QUESTION(db
->handle
->trans
, PM_TRANS_CONV_INSTALL_IGNOREPKG
, pkg
,
237 NULL
, NULL
, &install
);
241 if(!_alpm_pkg_find(pkgs
, alpm_pkg_get_name(pkg
))) {
242 pkgs
= alpm_list_add(pkgs
, pkg
);
246 alpm_list_free(ignorelist
);
250 /** Compute the size of the files that will be downloaded to install a
252 * @param newpkg the new package to upgrade to
254 static int compute_download_size(pmpkg_t
*newpkg
)
260 if(newpkg
->origin
!= PKG_FROM_SYNCDB
) {
261 newpkg
->infolevel
|= INFRQ_DSIZE
;
262 newpkg
->download_size
= 0;
266 fname
= alpm_pkg_get_filename(newpkg
);
267 ASSERT(fname
!= NULL
, RET_ERR(PM_ERR_PKG_INVALID_NAME
, -1));
268 fpath
= _alpm_filecache_find(fname
);
273 } else if(newpkg
->handle
->usedelta
) {
275 off_t pkgsize
= alpm_pkg_get_size(newpkg
);
277 dltsize
= _alpm_shortest_delta_path(
278 alpm_pkg_get_deltas(newpkg
),
279 alpm_pkg_get_filename(newpkg
),
280 &newpkg
->delta_path
);
282 if(newpkg
->delta_path
&& (dltsize
< pkgsize
* MAX_DELTA_RATIO
)) {
283 _alpm_log(PM_LOG_DEBUG
, "using delta size\n");
286 _alpm_log(PM_LOG_DEBUG
, "using package size\n");
287 size
= alpm_pkg_get_size(newpkg
);
288 alpm_list_free(newpkg
->delta_path
);
289 newpkg
->delta_path
= NULL
;
292 size
= alpm_pkg_get_size(newpkg
);
295 _alpm_log(PM_LOG_DEBUG
, "setting download size %jd for pkg %s\n",
296 (intmax_t)size
, alpm_pkg_get_name(newpkg
));
298 newpkg
->infolevel
|= INFRQ_DSIZE
;
299 newpkg
->download_size
= size
;
303 int _alpm_sync_prepare(pmhandle_t
*handle
, alpm_list_t
**data
)
305 alpm_list_t
*deps
= NULL
;
306 alpm_list_t
*unresolvable
= NULL
;
308 alpm_list_t
*remove
= NULL
;
310 pmtrans_t
*trans
= handle
->trans
;
311 pmdb_t
*db_local
= handle
->db_local
;
317 if(!(trans
->flags
& PM_TRANS_FLAG_NODEPS
)) {
318 alpm_list_t
*resolved
= NULL
; /* target list after resolvedeps */
320 /* Build up list by repeatedly resolving each transaction package */
321 /* Resolve targets dependencies */
322 EVENT(trans
, PM_TRANS_EVT_RESOLVEDEPS_START
, NULL
, NULL
);
323 _alpm_log(PM_LOG_DEBUG
, "resolving target's dependencies\n");
325 /* build remove list for resolvedeps */
326 for(i
= trans
->add
; i
; i
= i
->next
) {
327 pmpkg_t
*spkg
= i
->data
;
328 for(j
= spkg
->removes
; j
; j
= j
->next
) {
329 remove
= alpm_list_add(remove
, j
->data
);
333 /* Compute the fake local database for resolvedeps (partial fix for the
334 * phonon/qt issue) */
335 alpm_list_t
*localpkgs
= alpm_list_diff(_alpm_db_get_pkgcache(db_local
),
336 trans
->add
, _alpm_pkg_cmp
);
338 /* Resolve packages in the transaction one at a time, in addition
339 building up a list of packages which could not be resolved. */
340 for(i
= trans
->add
; i
; i
= i
->next
) {
341 pmpkg_t
*pkg
= i
->data
;
342 if(_alpm_resolvedeps(localpkgs
, handle
->dbs_sync
, pkg
, trans
->add
,
343 &resolved
, remove
, data
) == -1) {
344 unresolvable
= alpm_list_add(unresolvable
, pkg
);
346 /* Else, [resolved] now additionally contains [pkg] and all of its
347 dependencies not already on the list */
349 alpm_list_free(localpkgs
);
351 /* If there were unresolvable top-level packages, prompt the user to
352 see if they'd like to ignore them rather than failing the sync */
353 if(unresolvable
!= NULL
) {
354 int remove_unresolvable
= 0;
355 QUESTION(handle
->trans
, PM_TRANS_CONV_REMOVE_PKGS
, unresolvable
,
356 NULL
, NULL
, &remove_unresolvable
);
357 if(remove_unresolvable
) {
358 /* User wants to remove the unresolvable packages from the
359 transaction. The packages will be removed from the actual
360 transaction when the transaction packages are replaced with a
361 dependency-reordered list below */
362 pm_errno
= 0; /* pm_errno was set by resolvedeps */
364 alpm_list_free_inner(*data
, (alpm_list_fn_free
)_alpm_depmiss_free
);
365 alpm_list_free(*data
);
369 /* pm_errno is set by resolvedeps */
370 alpm_list_free(resolved
);
376 /* Set DEPEND reason for pulled packages */
377 for(i
= resolved
; i
; i
= i
->next
) {
378 pmpkg_t
*pkg
= i
->data
;
379 if(!_alpm_pkg_find(trans
->add
, pkg
->name
)) {
380 pkg
->reason
= PM_PKG_REASON_DEPEND
;
384 /* Unresolvable packages will be removed from the target list, so
385 we free the transaction specific fields */
386 alpm_list_free_inner(unresolvable
, (alpm_list_fn_free
)_alpm_pkg_free_trans
);
388 /* re-order w.r.t. dependencies */
389 alpm_list_free(trans
->add
);
390 trans
->add
= _alpm_sortbydeps(resolved
, 0);
391 alpm_list_free(resolved
);
393 EVENT(trans
, PM_TRANS_EVT_RESOLVEDEPS_DONE
, NULL
, NULL
);
396 if(!(trans
->flags
& PM_TRANS_FLAG_NOCONFLICTS
)) {
397 /* check for inter-conflicts and whatnot */
398 EVENT(trans
, PM_TRANS_EVT_INTERCONFLICTS_START
, NULL
, NULL
);
400 _alpm_log(PM_LOG_DEBUG
, "looking for conflicts\n");
402 /* 1. check for conflicts in the target list */
403 _alpm_log(PM_LOG_DEBUG
, "check targets vs targets\n");
404 deps
= _alpm_innerconflicts(trans
->add
);
406 for(i
= deps
; i
; i
= i
->next
) {
407 pmconflict_t
*conflict
= i
->data
;
408 pmpkg_t
*rsync
, *sync
, *sync1
, *sync2
;
410 /* have we already removed one of the conflicting targets? */
411 sync1
= _alpm_pkg_find(trans
->add
, conflict
->package1
);
412 sync2
= _alpm_pkg_find(trans
->add
, conflict
->package2
);
413 if(!sync1
|| !sync2
) {
417 _alpm_log(PM_LOG_DEBUG
, "conflicting packages in the sync list: '%s' <-> '%s'\n",
418 conflict
->package1
, conflict
->package2
);
420 /* if sync1 provides sync2, we remove sync2 from the targets, and vice versa */
421 pmdepend_t
*dep1
= _alpm_splitdep(conflict
->package1
);
422 pmdepend_t
*dep2
= _alpm_splitdep(conflict
->package2
);
423 if(_alpm_depcmp(sync1
, dep2
)) {
426 } else if(_alpm_depcmp(sync2
, dep1
)) {
430 _alpm_log(PM_LOG_ERROR
, _("unresolvable package conflicts detected\n"));
431 pm_errno
= PM_ERR_CONFLICTING_DEPS
;
434 pmconflict_t
*newconflict
= _alpm_conflict_dup(conflict
);
436 *data
= alpm_list_add(*data
, newconflict
);
439 alpm_list_free_inner(deps
, (alpm_list_fn_free
)_alpm_conflict_free
);
440 alpm_list_free(deps
);
441 _alpm_dep_free(dep1
);
442 _alpm_dep_free(dep2
);
445 _alpm_dep_free(dep1
);
446 _alpm_dep_free(dep2
);
449 _alpm_log(PM_LOG_WARNING
,
450 _("removing '%s' from target list because it conflicts with '%s'\n"),
451 rsync
->name
, sync
->name
);
452 trans
->add
= alpm_list_remove(trans
->add
, rsync
, _alpm_pkg_cmp
, NULL
);
453 _alpm_pkg_free_trans(rsync
); /* rsync is not transaction target anymore */
457 alpm_list_free_inner(deps
, (alpm_list_fn_free
)_alpm_conflict_free
);
458 alpm_list_free(deps
);
461 /* 2. we check for target vs db conflicts (and resolve)*/
462 _alpm_log(PM_LOG_DEBUG
, "check targets vs db and db vs targets\n");
463 deps
= _alpm_outerconflicts(db_local
, trans
->add
);
465 for(i
= deps
; i
; i
= i
->next
) {
466 pmconflict_t
*conflict
= i
->data
;
468 /* if conflict->package2 (the local package) is not elected for removal,
471 for(j
= trans
->add
; j
&& !found
; j
= j
->next
) {
472 pmpkg_t
*spkg
= j
->data
;
473 if(_alpm_pkg_find(spkg
->removes
, conflict
->package2
)) {
481 _alpm_log(PM_LOG_DEBUG
, "package '%s' conflicts with '%s'\n",
482 conflict
->package1
, conflict
->package2
);
484 pmpkg_t
*sync
= _alpm_pkg_find(trans
->add
, conflict
->package1
);
485 pmpkg_t
*local
= _alpm_db_get_pkgfromcache(db_local
, conflict
->package2
);
487 QUESTION(trans
, PM_TRANS_CONV_CONFLICT_PKG
, conflict
->package1
,
488 conflict
->package2
, conflict
->reason
, &doremove
);
490 /* append to the removes list */
491 _alpm_log(PM_LOG_DEBUG
, "electing '%s' for removal\n", conflict
->package2
);
492 sync
->removes
= alpm_list_add(sync
->removes
, local
);
494 _alpm_log(PM_LOG_ERROR
, _("unresolvable package conflicts detected\n"));
495 pm_errno
= PM_ERR_CONFLICTING_DEPS
;
498 pmconflict_t
*newconflict
= _alpm_conflict_dup(conflict
);
500 *data
= alpm_list_add(*data
, newconflict
);
503 alpm_list_free_inner(deps
, (alpm_list_fn_free
)_alpm_conflict_free
);
504 alpm_list_free(deps
);
508 EVENT(trans
, PM_TRANS_EVT_INTERCONFLICTS_DONE
, NULL
, NULL
);
509 alpm_list_free_inner(deps
, (alpm_list_fn_free
)_alpm_conflict_free
);
510 alpm_list_free(deps
);
513 /* Build trans->remove list */
514 for(i
= trans
->add
; i
; i
= i
->next
) {
515 pmpkg_t
*spkg
= i
->data
;
516 for(j
= spkg
->removes
; j
; j
= j
->next
) {
517 pmpkg_t
*rpkg
= j
->data
;
518 if(!_alpm_pkg_find(trans
->remove
, rpkg
->name
)) {
519 _alpm_log(PM_LOG_DEBUG
, "adding '%s' to remove list\n", rpkg
->name
);
520 trans
->remove
= alpm_list_add(trans
->remove
, _alpm_pkg_dup(rpkg
));
525 if(!(trans
->flags
& PM_TRANS_FLAG_NODEPS
)) {
526 _alpm_log(PM_LOG_DEBUG
, "checking dependencies\n");
527 deps
= alpm_checkdeps(_alpm_db_get_pkgcache(db_local
), 1, trans
->remove
, trans
->add
);
529 pm_errno
= PM_ERR_UNSATISFIED_DEPS
;
534 alpm_list_free_inner(deps
, (alpm_list_fn_free
)_alpm_depmiss_free
);
535 alpm_list_free(deps
);
540 for(i
= trans
->add
; i
; i
= i
->next
) {
541 /* update download size field */
542 pmpkg_t
*spkg
= i
->data
;
543 if(compute_download_size(spkg
) != 0) {
550 alpm_list_free(unresolvable
);
551 alpm_list_free(remove
);
556 /** Returns the size of the files that will be downloaded to install a
558 * @param newpkg the new package to upgrade to
559 * @return the size of the download
561 off_t SYMEXPORT
alpm_pkg_download_size(pmpkg_t
*newpkg
)
563 if(!(newpkg
->infolevel
& INFRQ_DSIZE
)) {
564 compute_download_size(newpkg
);
566 return newpkg
->download_size
;
569 static int endswith(const char *filename
, const char *extension
)
571 const char *s
= filename
+ strlen(filename
) - strlen(extension
);
572 return strcmp(s
, extension
) == 0;
575 /** Applies delta files to create an upgraded package file.
577 * All intermediate files are deleted, leaving only the starting and
578 * ending package files.
580 * @param trans the transaction
582 * @return 0 if all delta files were able to be applied, 1 otherwise.
584 static int apply_deltas(pmtrans_t
*trans
)
588 const char *cachedir
= _alpm_filecache_setup();
590 for(i
= trans
->add
; i
; i
= i
->next
) {
591 pmpkg_t
*spkg
= i
->data
;
592 alpm_list_t
*delta_path
= spkg
->delta_path
;
593 alpm_list_t
*dlts
= NULL
;
599 for(dlts
= delta_path
; dlts
; dlts
= dlts
->next
) {
600 pmdelta_t
*d
= dlts
->data
;
601 char *delta
, *from
, *to
;
602 char command
[PATH_MAX
];
605 delta
= _alpm_filecache_find(d
->delta
);
606 /* the initial package might be in a different cachedir */
607 if(dlts
== delta_path
) {
608 from
= _alpm_filecache_find(d
->from
);
610 /* len = cachedir len + from len + '/' + null */
611 len
= strlen(cachedir
) + strlen(d
->from
) + 2;
612 CALLOC(from
, len
, sizeof(char), RET_ERR(PM_ERR_MEMORY
, 1));
613 snprintf(from
, len
, "%s/%s", cachedir
, d
->from
);
615 len
= strlen(cachedir
) + strlen(d
->to
) + 2;
616 CALLOC(to
, len
, sizeof(char), RET_ERR(PM_ERR_MEMORY
, 1));
617 snprintf(to
, len
, "%s/%s", cachedir
, d
->to
);
619 /* build the patch command */
620 if(endswith(to
, ".gz")) {
621 /* special handling for gzip : we disable timestamp with -n option */
622 snprintf(command
, PATH_MAX
, "xdelta3 -d -q -R -c -s %s %s | gzip -n > %s", from
, delta
, to
);
624 snprintf(command
, PATH_MAX
, "xdelta3 -d -q -s %s %s %s", from
, delta
, to
);
627 _alpm_log(PM_LOG_DEBUG
, "command: %s\n", command
);
629 EVENT(trans
, PM_TRANS_EVT_DELTA_PATCH_START
, d
->to
, d
->delta
);
631 int retval
= system(command
);
633 EVENT(trans
, PM_TRANS_EVT_DELTA_PATCH_DONE
, NULL
, NULL
);
635 /* delete the delta file */
638 /* Delete the 'from' package but only if it is an intermediate
639 * package. The starting 'from' package should be kept, just
640 * as if deltas were not used. */
641 if(dlts
!= delta_path
) {
650 /* one delta failed for this package, cancel the remaining ones */
651 EVENT(trans
, PM_TRANS_EVT_DELTA_PATCH_FAILED
, NULL
, NULL
);
652 pm_errno
= PM_ERR_DLT_PATCHFAILED
;
662 /** Compares the md5sum of a file to the expected value.
664 * If the md5sum does not match, the user is asked whether the file
667 * @param trans the transaction
668 * @param filename the absolute path of the file to test
669 * @param md5sum the expected md5sum of the file
671 * @return 0 if the md5sum matched, 1 if not, -1 in case of errors
673 static int test_md5sum(pmtrans_t
*trans
, const char *filepath
,
676 int ret
= _alpm_test_md5sum(filepath
, md5sum
);
679 QUESTION(trans
, PM_TRANS_CONV_CORRUPTED_PKG
, (char *)filepath
,
680 NULL
, NULL
, &doremove
);
689 static int validate_deltas(pmtrans_t
*trans
, alpm_list_t
*deltas
,
692 int errors
= 0, ret
= 0;
699 /* Check integrity of deltas */
700 EVENT(trans
, PM_TRANS_EVT_DELTA_INTEGRITY_START
, NULL
, NULL
);
702 for(i
= deltas
; i
; i
= i
->next
) {
703 pmdelta_t
*d
= alpm_list_getdata(i
);
704 const char *filename
= alpm_delta_get_filename(d
);
705 char *filepath
= _alpm_filecache_find(filename
);
706 const char *md5sum
= alpm_delta_get_md5sum(d
);
708 if(test_md5sum(trans
, filepath
, md5sum
) != 0) {
710 *data
= alpm_list_add(*data
, strdup(filename
));
715 pm_errno
= PM_ERR_DLT_INVALID
;
718 EVENT(trans
, PM_TRANS_EVT_DELTA_INTEGRITY_DONE
, NULL
, NULL
);
720 /* Use the deltas to generate the packages */
721 EVENT(trans
, PM_TRANS_EVT_DELTA_PATCHES_START
, NULL
, NULL
);
722 ret
= apply_deltas(trans
);
723 EVENT(trans
, PM_TRANS_EVT_DELTA_PATCHES_DONE
, NULL
, NULL
);
727 static int download_files(pmhandle_t
*handle
, alpm_list_t
**deltas
)
729 const char *cachedir
;
731 alpm_list_t
*files
= NULL
;
734 cachedir
= _alpm_filecache_setup();
735 handle
->trans
->state
= STATE_DOWNLOADING
;
737 /* Total progress - figure out the total download size if required to
738 * pass to the callback. This function is called once, and it is up to the
739 * frontend to compute incremental progress. */
740 if(handle
->totaldlcb
) {
741 off_t total_size
= (off_t
)0;
742 /* sum up the download size for each package and store total */
743 for(i
= handle
->trans
->add
; i
; i
= i
->next
) {
744 pmpkg_t
*spkg
= i
->data
;
745 total_size
+= spkg
->download_size
;
747 handle
->totaldlcb(total_size
);
750 /* group sync records by repository and download */
751 for(i
= handle
->dbs_sync
; i
; i
= i
->next
) {
752 pmdb_t
*current
= i
->data
;
754 for(j
= handle
->trans
->add
; j
; j
= j
->next
) {
755 pmpkg_t
*spkg
= j
->data
;
757 if(spkg
->origin
!= PKG_FROM_FILE
&& current
== spkg
->origin_data
.db
) {
758 const char *fname
= NULL
;
760 fname
= alpm_pkg_get_filename(spkg
);
761 ASSERT(fname
!= NULL
, RET_ERR(PM_ERR_PKG_INVALID_NAME
, -1));
762 alpm_list_t
*delta_path
= spkg
->delta_path
;
766 for(dlts
= delta_path
; dlts
; dlts
= dlts
->next
) {
767 pmdelta_t
*delta
= dlts
->data
;
768 if(delta
->download_size
!= 0) {
769 files
= alpm_list_add(files
, strdup(delta
->delta
));
771 /* keep a list of all the delta files for md5sums */
772 *deltas
= alpm_list_add(*deltas
, delta
);
775 } else if(spkg
->download_size
!= 0) {
776 files
= alpm_list_add(files
, strdup(fname
));
783 EVENT(handle
->trans
, PM_TRANS_EVT_RETRIEVE_START
, current
->treename
, NULL
);
784 for(j
= files
; j
; j
= j
->next
) {
785 const char *filename
= j
->data
;
788 for(server
= current
->servers
; server
; server
= server
->next
) {
789 const char *server_url
= server
->data
;
793 /* print server + filename into a buffer */
794 len
= strlen(server_url
) + strlen(filename
) + 2;
795 CALLOC(fileurl
, len
, sizeof(char), RET_ERR(PM_ERR_MEMORY
, -1));
796 snprintf(fileurl
, len
, "%s/%s", server_url
, filename
);
798 ret
= _alpm_download(fileurl
, cachedir
, 0, 1, 0);
811 _alpm_log(PM_LOG_WARNING
, _("failed to retrieve some files from %s\n"),
814 pm_errno
= PM_ERR_RETRIEVE
;
821 for(j
= handle
->trans
->add
; j
; j
= j
->next
) {
822 pmpkg_t
*pkg
= j
->data
;
823 pkg
->infolevel
&= ~INFRQ_DSIZE
;
824 pkg
->download_size
= 0;
827 /* clear out value to let callback know we are done */
828 if(handle
->totaldlcb
) {
829 handle
->totaldlcb(0);
834 int _alpm_sync_commit(pmhandle_t
*handle
, alpm_list_t
**data
)
837 alpm_list_t
*deltas
= NULL
;
838 size_t numtargs
, current
= 0, replaces
= 0;
840 pmtrans_t
*trans
= handle
->trans
;
842 if(download_files(handle
, &deltas
)) {
843 alpm_list_free(deltas
);
847 if(validate_deltas(trans
, deltas
, data
)) {
848 alpm_list_free(deltas
);
851 alpm_list_free(deltas
);
853 /* Check integrity of packages */
854 numtargs
= alpm_list_count(trans
->add
);
855 EVENT(trans
, PM_TRANS_EVT_INTEGRITY_START
, NULL
, NULL
);
859 for(i
= trans
->add
; i
; i
= i
->next
, current
++) {
860 pmpkg_t
*spkg
= i
->data
;
861 int percent
= (current
* 100) / numtargs
;
862 const char *filename
;
864 pgp_verify_t check_sig
;
866 PROGRESS(trans
, PM_TRANS_PROGRESS_INTEGRITY_START
, "", percent
,
868 if(spkg
->origin
== PKG_FROM_FILE
) {
869 continue; /* pkg_load() has been already called, this package is valid */
872 filename
= alpm_pkg_get_filename(spkg
);
873 filepath
= _alpm_filecache_find(filename
);
874 pmdb_t
*sdb
= alpm_pkg_get_db(spkg
);
875 check_sig
= _alpm_db_get_sigverify_level(sdb
);
877 /* load the package file and replace pkgcache entry with it in the target list */
878 /* TODO: alpm_pkg_get_db() will not work on this target anymore */
879 _alpm_log(PM_LOG_DEBUG
, "replacing pkgcache entry with package file for target %s\n", spkg
->name
);
880 pmpkg_t
*pkgfile
=_alpm_pkg_load_internal(filepath
, 1, spkg
->md5sum
,
881 spkg
->base64_sig
, check_sig
);
884 *data
= alpm_list_add(*data
, strdup(filename
));
889 pkgfile
->reason
= spkg
->reason
; /* copy over install reason */
891 _alpm_pkg_free_trans(spkg
); /* spkg has been removed from the target list */
894 PROGRESS(trans
, PM_TRANS_PROGRESS_INTEGRITY_START
, "", 100,
896 EVENT(trans
, PM_TRANS_EVT_INTEGRITY_DONE
, NULL
, NULL
);
900 RET_ERR(PM_ERR_PKG_INVALID
, -1);
903 if(trans
->flags
& PM_TRANS_FLAG_DOWNLOADONLY
) {
907 trans
->state
= STATE_COMMITING
;
909 replaces
= alpm_list_count(trans
->remove
);
911 /* fileconflict check */
912 if(!(trans
->flags
& PM_TRANS_FLAG_FORCE
)) {
913 EVENT(trans
, PM_TRANS_EVT_FILECONFLICTS_START
, NULL
, NULL
);
915 _alpm_log(PM_LOG_DEBUG
, "looking for file conflicts\n");
916 alpm_list_t
*conflict
= _alpm_db_find_fileconflicts(handle
,
917 trans
->add
, trans
->remove
);
922 alpm_list_free_inner(conflict
, (alpm_list_fn_free
)_alpm_fileconflict_free
);
923 alpm_list_free(conflict
);
925 RET_ERR(PM_ERR_FILE_CONFLICTS
, -1);
928 EVENT(trans
, PM_TRANS_EVT_FILECONFLICTS_DONE
, NULL
, NULL
);
931 /* check available disk space */
932 if(handle
->checkspace
) {
933 EVENT(trans
, PM_TRANS_EVT_DISKSPACE_START
, NULL
, NULL
);
935 _alpm_log(PM_LOG_DEBUG
, "checking available disk space\n");
936 if(_alpm_check_diskspace(handle
) == -1) {
937 _alpm_log(PM_LOG_ERROR
, "%s\n", _("not enough free disk space"));
941 EVENT(trans
, PM_TRANS_EVT_DISKSPACE_DONE
, NULL
, NULL
);
944 /* remove conflicting and to-be-replaced packages */
946 _alpm_log(PM_LOG_DEBUG
, "removing conflicting and to-be-replaced packages\n");
947 /* we want the frontend to be aware of commit details */
948 if(_alpm_remove_packages(handle
) == -1) {
949 _alpm_log(PM_LOG_ERROR
, _("could not commit removal transaction\n"));
954 /* install targets */
955 _alpm_log(PM_LOG_DEBUG
, "installing packages\n");
956 if(_alpm_upgrade_packages(handle
) == -1) {
957 _alpm_log(PM_LOG_ERROR
, _("could not commit transaction\n"));
964 /* vim: set ts=2 sw=2 noet: */