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 /** Check for new version of pkg in sync repos
54 * (only the first occurrence is considered in sync)
56 pmpkg_t SYMEXPORT
*alpm_sync_newversion(pmpkg_t
*pkg
, alpm_list_t
*dbs_sync
)
58 ASSERT(pkg
!= NULL
, return NULL
);
63 for(i
= dbs_sync
; !spkg
&& i
; i
= i
->next
) {
64 spkg
= _alpm_db_get_pkgfromcache(i
->data
, alpm_pkg_get_name(pkg
));
68 _alpm_log(PM_LOG_DEBUG
, "'%s' not found in sync db => no upgrade\n",
69 alpm_pkg_get_name(pkg
));
73 /* compare versions and see if spkg is an upgrade */
74 if(_alpm_pkg_compare_versions(spkg
, pkg
) > 0) {
75 _alpm_log(PM_LOG_DEBUG
, "new version of '%s' found (%s => %s)\n",
76 alpm_pkg_get_name(pkg
), alpm_pkg_get_version(pkg
),
77 alpm_pkg_get_version(spkg
));
80 /* spkg is not an upgrade */
84 /** Search for packages to upgrade and add them to the transaction. */
85 int SYMEXPORT
alpm_sync_sysupgrade(pmhandle_t
*handle
, int enable_downgrade
)
87 alpm_list_t
*i
, *j
, *k
;
90 alpm_list_t
*dbs_sync
;
92 ASSERT(handle
!= NULL
, RET_ERR(PM_ERR_HANDLE_NULL
, -1));
93 trans
= handle
->trans
;
94 db_local
= handle
->db_local
;
95 dbs_sync
= handle
->dbs_sync
;
96 ASSERT(trans
!= NULL
, RET_ERR(PM_ERR_TRANS_NULL
, -1));
97 ASSERT(trans
->state
== STATE_INITIALIZED
, RET_ERR(PM_ERR_TRANS_NOT_INITIALIZED
, -1));
99 _alpm_log(PM_LOG_DEBUG
, "checking for package upgrades\n");
100 for(i
= _alpm_db_get_pkgcache(db_local
); i
; i
= i
->next
) {
101 pmpkg_t
*lpkg
= i
->data
;
103 if(_alpm_pkg_find(trans
->add
, lpkg
->name
)) {
104 _alpm_log(PM_LOG_DEBUG
, "%s is already in the target list -- skipping\n", lpkg
->name
);
108 /* Search for literal then replacers in each sync database.
109 * If found, don't check other databases */
110 for(j
= dbs_sync
; j
; j
= j
->next
) {
111 pmdb_t
*sdb
= j
->data
;
113 pmpkg_t
*spkg
= _alpm_db_get_pkgfromcache(sdb
, lpkg
->name
);
115 /* 1. literal was found in sdb */
116 int cmp
= _alpm_pkg_compare_versions(spkg
, lpkg
);
118 _alpm_log(PM_LOG_DEBUG
, "new version of '%s' found (%s => %s)\n",
119 lpkg
->name
, lpkg
->version
, spkg
->version
);
120 /* check IgnorePkg/IgnoreGroup */
121 if(_alpm_pkg_should_ignore(spkg
) || _alpm_pkg_should_ignore(lpkg
)) {
122 _alpm_log(PM_LOG_WARNING
, _("%s: ignoring package upgrade (%s => %s)\n"),
123 lpkg
->name
, lpkg
->version
, spkg
->version
);
125 _alpm_log(PM_LOG_DEBUG
, "adding package %s-%s to the transaction targets\n",
126 spkg
->name
, spkg
->version
);
127 trans
->add
= alpm_list_add(trans
->add
, spkg
);
130 if(enable_downgrade
) {
131 /* check IgnorePkg/IgnoreGroup */
132 if(_alpm_pkg_should_ignore(spkg
) || _alpm_pkg_should_ignore(lpkg
)) {
133 _alpm_log(PM_LOG_WARNING
, _("%s: ignoring package downgrade (%s => %s)\n"),
134 lpkg
->name
, lpkg
->version
, spkg
->version
);
136 _alpm_log(PM_LOG_WARNING
, _("%s: downgrading from version %s to version %s\n"),
137 lpkg
->name
, lpkg
->version
, spkg
->version
);
138 trans
->add
= alpm_list_add(trans
->add
, spkg
);
141 _alpm_log(PM_LOG_WARNING
, _("%s: local (%s) is newer than %s (%s)\n"),
142 lpkg
->name
, lpkg
->version
, sdb
->treename
, spkg
->version
);
145 /* jump to next local package */
148 /* 2. search for replacers in sdb */
150 for(k
= _alpm_db_get_pkgcache(sdb
); k
; k
= k
->next
) {
152 if(alpm_list_find_str(alpm_pkg_get_replaces(spkg
), lpkg
->name
)) {
154 /* check IgnorePkg/IgnoreGroup */
155 if(_alpm_pkg_should_ignore(spkg
) || _alpm_pkg_should_ignore(lpkg
)) {
156 _alpm_log(PM_LOG_WARNING
, _("ignoring package replacement (%s-%s => %s-%s)\n"),
157 lpkg
->name
, lpkg
->version
, spkg
->name
, spkg
->version
);
162 QUESTION(trans
, PM_TRANS_CONV_REPLACE_PKG
, lpkg
, spkg
, sdb
->treename
, &doreplace
);
167 /* If spkg is already in the target list, we append lpkg to spkg's
169 pmpkg_t
*tpkg
= _alpm_pkg_find(trans
->add
, spkg
->name
);
171 /* sanity check, multiple repos can contain spkg->name */
172 if(tpkg
->origin_data
.db
!= sdb
) {
173 _alpm_log(PM_LOG_WARNING
, _("cannot replace %s by %s\n"),
174 lpkg
->name
, spkg
->name
);
177 _alpm_log(PM_LOG_DEBUG
, "appending %s to the removes list of %s\n",
178 lpkg
->name
, tpkg
->name
);
179 tpkg
->removes
= alpm_list_add(tpkg
->removes
, lpkg
);
180 /* check the to-be-replaced package's reason field */
181 if(alpm_pkg_get_reason(lpkg
) == PM_PKG_REASON_EXPLICIT
) {
182 tpkg
->reason
= PM_PKG_REASON_EXPLICIT
;
185 /* add spkg to the target list */
186 /* copy over reason */
187 spkg
->reason
= alpm_pkg_get_reason(lpkg
);
188 spkg
->removes
= alpm_list_add(NULL
, lpkg
);
189 _alpm_log(PM_LOG_DEBUG
, "adding package %s-%s to the transaction targets\n",
190 spkg
->name
, spkg
->version
);
191 trans
->add
= alpm_list_add(trans
->add
, spkg
);
196 break; /* jump to next local package */
205 /** Find group members across a list of databases.
206 * If a member exists in several databases, only the first database is used.
207 * IgnorePkg is also handled.
208 * @param dbs the list of pmdb_t *
209 * @pram name the name of the group
210 * @return the list of pmpkg_t * (caller is responsible for alpm_list_free)
212 alpm_list_t SYMEXPORT
*alpm_find_grp_pkgs(alpm_list_t
*dbs
,
215 alpm_list_t
*i
, *j
, *pkgs
= NULL
, *ignorelist
= NULL
;
217 for(i
= dbs
; i
; i
= i
->next
) {
218 pmdb_t
*db
= i
->data
;
219 pmgrp_t
*grp
= alpm_db_readgrp(db
, name
);
224 for(j
= alpm_grp_get_pkgs(grp
); j
; j
= j
->next
) {
225 pmpkg_t
*pkg
= j
->data
;
227 if(_alpm_pkg_find(ignorelist
, alpm_pkg_get_name(pkg
))) {
230 if(_alpm_pkg_should_ignore(pkg
)) {
231 ignorelist
= alpm_list_add(ignorelist
, pkg
);
233 QUESTION(db
->handle
->trans
, PM_TRANS_CONV_INSTALL_IGNOREPKG
, pkg
,
234 NULL
, NULL
, &install
);
238 if(!_alpm_pkg_find(pkgs
, alpm_pkg_get_name(pkg
))) {
239 pkgs
= alpm_list_add(pkgs
, pkg
);
243 alpm_list_free(ignorelist
);
247 /** Compute the size of the files that will be downloaded to install a
249 * @param newpkg the new package to upgrade to
251 static int compute_download_size(pmpkg_t
*newpkg
)
257 if(newpkg
->origin
!= PKG_FROM_SYNCDB
) {
258 newpkg
->infolevel
|= INFRQ_DSIZE
;
259 newpkg
->download_size
= 0;
263 fname
= alpm_pkg_get_filename(newpkg
);
264 ASSERT(fname
!= NULL
, RET_ERR(PM_ERR_PKG_INVALID_NAME
, -1));
265 fpath
= _alpm_filecache_find(newpkg
->handle
, fname
);
270 } else if(newpkg
->handle
->usedelta
) {
272 off_t pkgsize
= alpm_pkg_get_size(newpkg
);
274 dltsize
= _alpm_shortest_delta_path(newpkg
->handle
,
275 alpm_pkg_get_deltas(newpkg
),
276 alpm_pkg_get_filename(newpkg
),
277 &newpkg
->delta_path
);
279 if(newpkg
->delta_path
&& (dltsize
< pkgsize
* MAX_DELTA_RATIO
)) {
280 _alpm_log(PM_LOG_DEBUG
, "using delta size\n");
283 _alpm_log(PM_LOG_DEBUG
, "using package size\n");
284 size
= alpm_pkg_get_size(newpkg
);
285 alpm_list_free(newpkg
->delta_path
);
286 newpkg
->delta_path
= NULL
;
289 size
= alpm_pkg_get_size(newpkg
);
292 _alpm_log(PM_LOG_DEBUG
, "setting download size %jd for pkg %s\n",
293 (intmax_t)size
, alpm_pkg_get_name(newpkg
));
295 newpkg
->infolevel
|= INFRQ_DSIZE
;
296 newpkg
->download_size
= size
;
300 int _alpm_sync_prepare(pmhandle_t
*handle
, alpm_list_t
**data
)
302 alpm_list_t
*deps
= NULL
;
303 alpm_list_t
*unresolvable
= NULL
;
305 alpm_list_t
*remove
= NULL
;
307 pmtrans_t
*trans
= handle
->trans
;
308 pmdb_t
*db_local
= handle
->db_local
;
314 if(!(trans
->flags
& PM_TRANS_FLAG_NODEPS
)) {
315 alpm_list_t
*resolved
= NULL
; /* target list after resolvedeps */
317 /* Build up list by repeatedly resolving each transaction package */
318 /* Resolve targets dependencies */
319 EVENT(trans
, PM_TRANS_EVT_RESOLVEDEPS_START
, NULL
, NULL
);
320 _alpm_log(PM_LOG_DEBUG
, "resolving target's dependencies\n");
322 /* build remove list for resolvedeps */
323 for(i
= trans
->add
; i
; i
= i
->next
) {
324 pmpkg_t
*spkg
= i
->data
;
325 for(j
= spkg
->removes
; j
; j
= j
->next
) {
326 remove
= alpm_list_add(remove
, j
->data
);
330 /* Compute the fake local database for resolvedeps (partial fix for the
331 * phonon/qt issue) */
332 alpm_list_t
*localpkgs
= alpm_list_diff(_alpm_db_get_pkgcache(db_local
),
333 trans
->add
, _alpm_pkg_cmp
);
335 /* Resolve packages in the transaction one at a time, in addition
336 building up a list of packages which could not be resolved. */
337 for(i
= trans
->add
; i
; i
= i
->next
) {
338 pmpkg_t
*pkg
= i
->data
;
339 if(_alpm_resolvedeps(localpkgs
, handle
->dbs_sync
, pkg
, trans
->add
,
340 &resolved
, remove
, data
) == -1) {
341 unresolvable
= alpm_list_add(unresolvable
, pkg
);
343 /* Else, [resolved] now additionally contains [pkg] and all of its
344 dependencies not already on the list */
346 alpm_list_free(localpkgs
);
348 /* If there were unresolvable top-level packages, prompt the user to
349 see if they'd like to ignore them rather than failing the sync */
350 if(unresolvable
!= NULL
) {
351 int remove_unresolvable
= 0;
352 QUESTION(handle
->trans
, PM_TRANS_CONV_REMOVE_PKGS
, unresolvable
,
353 NULL
, NULL
, &remove_unresolvable
);
354 if(remove_unresolvable
) {
355 /* User wants to remove the unresolvable packages from the
356 transaction. The packages will be removed from the actual
357 transaction when the transaction packages are replaced with a
358 dependency-reordered list below */
359 pm_errno
= 0; /* pm_errno was set by resolvedeps */
361 alpm_list_free_inner(*data
, (alpm_list_fn_free
)_alpm_depmiss_free
);
362 alpm_list_free(*data
);
366 /* pm_errno is set by resolvedeps */
367 alpm_list_free(resolved
);
373 /* Set DEPEND reason for pulled packages */
374 for(i
= resolved
; i
; i
= i
->next
) {
375 pmpkg_t
*pkg
= i
->data
;
376 if(!_alpm_pkg_find(trans
->add
, pkg
->name
)) {
377 pkg
->reason
= PM_PKG_REASON_DEPEND
;
381 /* Unresolvable packages will be removed from the target list, so
382 we free the transaction specific fields */
383 alpm_list_free_inner(unresolvable
, (alpm_list_fn_free
)_alpm_pkg_free_trans
);
385 /* re-order w.r.t. dependencies */
386 alpm_list_free(trans
->add
);
387 trans
->add
= _alpm_sortbydeps(resolved
, 0);
388 alpm_list_free(resolved
);
390 EVENT(trans
, PM_TRANS_EVT_RESOLVEDEPS_DONE
, NULL
, NULL
);
393 if(!(trans
->flags
& PM_TRANS_FLAG_NOCONFLICTS
)) {
394 /* check for inter-conflicts and whatnot */
395 EVENT(trans
, PM_TRANS_EVT_INTERCONFLICTS_START
, NULL
, NULL
);
397 _alpm_log(PM_LOG_DEBUG
, "looking for conflicts\n");
399 /* 1. check for conflicts in the target list */
400 _alpm_log(PM_LOG_DEBUG
, "check targets vs targets\n");
401 deps
= _alpm_innerconflicts(trans
->add
);
403 for(i
= deps
; i
; i
= i
->next
) {
404 pmconflict_t
*conflict
= i
->data
;
405 pmpkg_t
*rsync
, *sync
, *sync1
, *sync2
;
407 /* have we already removed one of the conflicting targets? */
408 sync1
= _alpm_pkg_find(trans
->add
, conflict
->package1
);
409 sync2
= _alpm_pkg_find(trans
->add
, conflict
->package2
);
410 if(!sync1
|| !sync2
) {
414 _alpm_log(PM_LOG_DEBUG
, "conflicting packages in the sync list: '%s' <-> '%s'\n",
415 conflict
->package1
, conflict
->package2
);
417 /* if sync1 provides sync2, we remove sync2 from the targets, and vice versa */
418 pmdepend_t
*dep1
= _alpm_splitdep(conflict
->package1
);
419 pmdepend_t
*dep2
= _alpm_splitdep(conflict
->package2
);
420 if(_alpm_depcmp(sync1
, dep2
)) {
423 } else if(_alpm_depcmp(sync2
, dep1
)) {
427 _alpm_log(PM_LOG_ERROR
, _("unresolvable package conflicts detected\n"));
428 pm_errno
= PM_ERR_CONFLICTING_DEPS
;
431 pmconflict_t
*newconflict
= _alpm_conflict_dup(conflict
);
433 *data
= alpm_list_add(*data
, newconflict
);
436 alpm_list_free_inner(deps
, (alpm_list_fn_free
)_alpm_conflict_free
);
437 alpm_list_free(deps
);
438 _alpm_dep_free(dep1
);
439 _alpm_dep_free(dep2
);
442 _alpm_dep_free(dep1
);
443 _alpm_dep_free(dep2
);
446 _alpm_log(PM_LOG_WARNING
,
447 _("removing '%s' from target list because it conflicts with '%s'\n"),
448 rsync
->name
, sync
->name
);
449 trans
->add
= alpm_list_remove(trans
->add
, rsync
, _alpm_pkg_cmp
, NULL
);
450 _alpm_pkg_free_trans(rsync
); /* rsync is not transaction target anymore */
454 alpm_list_free_inner(deps
, (alpm_list_fn_free
)_alpm_conflict_free
);
455 alpm_list_free(deps
);
458 /* 2. we check for target vs db conflicts (and resolve)*/
459 _alpm_log(PM_LOG_DEBUG
, "check targets vs db and db vs targets\n");
460 deps
= _alpm_outerconflicts(db_local
, trans
->add
);
462 for(i
= deps
; i
; i
= i
->next
) {
463 pmconflict_t
*conflict
= i
->data
;
465 /* if conflict->package2 (the local package) is not elected for removal,
468 for(j
= trans
->add
; j
&& !found
; j
= j
->next
) {
469 pmpkg_t
*spkg
= j
->data
;
470 if(_alpm_pkg_find(spkg
->removes
, conflict
->package2
)) {
478 _alpm_log(PM_LOG_DEBUG
, "package '%s' conflicts with '%s'\n",
479 conflict
->package1
, conflict
->package2
);
481 pmpkg_t
*sync
= _alpm_pkg_find(trans
->add
, conflict
->package1
);
482 pmpkg_t
*local
= _alpm_db_get_pkgfromcache(db_local
, conflict
->package2
);
484 QUESTION(trans
, PM_TRANS_CONV_CONFLICT_PKG
, conflict
->package1
,
485 conflict
->package2
, conflict
->reason
, &doremove
);
487 /* append to the removes list */
488 _alpm_log(PM_LOG_DEBUG
, "electing '%s' for removal\n", conflict
->package2
);
489 sync
->removes
= alpm_list_add(sync
->removes
, local
);
491 _alpm_log(PM_LOG_ERROR
, _("unresolvable package conflicts detected\n"));
492 pm_errno
= PM_ERR_CONFLICTING_DEPS
;
495 pmconflict_t
*newconflict
= _alpm_conflict_dup(conflict
);
497 *data
= alpm_list_add(*data
, newconflict
);
500 alpm_list_free_inner(deps
, (alpm_list_fn_free
)_alpm_conflict_free
);
501 alpm_list_free(deps
);
505 EVENT(trans
, PM_TRANS_EVT_INTERCONFLICTS_DONE
, NULL
, NULL
);
506 alpm_list_free_inner(deps
, (alpm_list_fn_free
)_alpm_conflict_free
);
507 alpm_list_free(deps
);
510 /* Build trans->remove list */
511 for(i
= trans
->add
; i
; i
= i
->next
) {
512 pmpkg_t
*spkg
= i
->data
;
513 for(j
= spkg
->removes
; j
; j
= j
->next
) {
514 pmpkg_t
*rpkg
= j
->data
;
515 if(!_alpm_pkg_find(trans
->remove
, rpkg
->name
)) {
516 _alpm_log(PM_LOG_DEBUG
, "adding '%s' to remove list\n", rpkg
->name
);
517 trans
->remove
= alpm_list_add(trans
->remove
, _alpm_pkg_dup(rpkg
));
522 if(!(trans
->flags
& PM_TRANS_FLAG_NODEPS
)) {
523 _alpm_log(PM_LOG_DEBUG
, "checking dependencies\n");
524 deps
= alpm_checkdeps(_alpm_db_get_pkgcache(db_local
), 1, trans
->remove
, trans
->add
);
526 pm_errno
= PM_ERR_UNSATISFIED_DEPS
;
531 alpm_list_free_inner(deps
, (alpm_list_fn_free
)_alpm_depmiss_free
);
532 alpm_list_free(deps
);
537 for(i
= trans
->add
; i
; i
= i
->next
) {
538 /* update download size field */
539 pmpkg_t
*spkg
= i
->data
;
540 if(compute_download_size(spkg
) != 0) {
547 alpm_list_free(unresolvable
);
548 alpm_list_free(remove
);
553 /** Returns the size of the files that will be downloaded to install a
555 * @param newpkg the new package to upgrade to
556 * @return the size of the download
558 off_t SYMEXPORT
alpm_pkg_download_size(pmpkg_t
*newpkg
)
560 if(!(newpkg
->infolevel
& INFRQ_DSIZE
)) {
561 compute_download_size(newpkg
);
563 return newpkg
->download_size
;
566 static int endswith(const char *filename
, const char *extension
)
568 const char *s
= filename
+ strlen(filename
) - strlen(extension
);
569 return strcmp(s
, extension
) == 0;
572 /** Applies delta files to create an upgraded package file.
574 * All intermediate files are deleted, leaving only the starting and
575 * ending package files.
577 * @param handle the context handle
579 * @return 0 if all delta files were able to be applied, 1 otherwise.
581 static int apply_deltas(pmhandle_t
*handle
)
585 const char *cachedir
= _alpm_filecache_setup(handle
);
586 pmtrans_t
*trans
= handle
->trans
;
588 for(i
= trans
->add
; i
; i
= i
->next
) {
589 pmpkg_t
*spkg
= i
->data
;
590 alpm_list_t
*delta_path
= spkg
->delta_path
;
591 alpm_list_t
*dlts
= NULL
;
597 for(dlts
= delta_path
; dlts
; dlts
= dlts
->next
) {
598 pmdelta_t
*d
= dlts
->data
;
599 char *delta
, *from
, *to
;
600 char command
[PATH_MAX
];
603 delta
= _alpm_filecache_find(handle
, d
->delta
);
604 /* the initial package might be in a different cachedir */
605 if(dlts
== delta_path
) {
606 from
= _alpm_filecache_find(handle
, d
->from
);
608 /* len = cachedir len + from len + '/' + null */
609 len
= strlen(cachedir
) + strlen(d
->from
) + 2;
610 CALLOC(from
, len
, sizeof(char), RET_ERR(PM_ERR_MEMORY
, 1));
611 snprintf(from
, len
, "%s/%s", cachedir
, d
->from
);
613 len
= strlen(cachedir
) + strlen(d
->to
) + 2;
614 CALLOC(to
, len
, sizeof(char), RET_ERR(PM_ERR_MEMORY
, 1));
615 snprintf(to
, len
, "%s/%s", cachedir
, d
->to
);
617 /* build the patch command */
618 if(endswith(to
, ".gz")) {
619 /* special handling for gzip : we disable timestamp with -n option */
620 snprintf(command
, PATH_MAX
, "xdelta3 -d -q -R -c -s %s %s | gzip -n > %s", from
, delta
, to
);
622 snprintf(command
, PATH_MAX
, "xdelta3 -d -q -s %s %s %s", from
, delta
, to
);
625 _alpm_log(PM_LOG_DEBUG
, "command: %s\n", command
);
627 EVENT(trans
, PM_TRANS_EVT_DELTA_PATCH_START
, d
->to
, d
->delta
);
629 int retval
= system(command
);
631 EVENT(trans
, PM_TRANS_EVT_DELTA_PATCH_DONE
, NULL
, NULL
);
633 /* delete the delta file */
636 /* Delete the 'from' package but only if it is an intermediate
637 * package. The starting 'from' package should be kept, just
638 * as if deltas were not used. */
639 if(dlts
!= delta_path
) {
648 /* one delta failed for this package, cancel the remaining ones */
649 EVENT(trans
, PM_TRANS_EVT_DELTA_PATCH_FAILED
, NULL
, NULL
);
650 pm_errno
= PM_ERR_DLT_PATCHFAILED
;
660 /** Compares the md5sum of a file to the expected value.
662 * If the md5sum does not match, the user is asked whether the file
665 * @param trans the transaction
666 * @param filename the absolute path of the file to test
667 * @param md5sum the expected md5sum of the file
669 * @return 0 if the md5sum matched, 1 if not, -1 in case of errors
671 static int test_md5sum(pmtrans_t
*trans
, const char *filepath
,
674 int ret
= _alpm_test_md5sum(filepath
, md5sum
);
677 QUESTION(trans
, PM_TRANS_CONV_CORRUPTED_PKG
, (char *)filepath
,
678 NULL
, NULL
, &doremove
);
687 static int validate_deltas(pmhandle_t
*handle
, alpm_list_t
*deltas
,
690 int errors
= 0, ret
= 0;
692 pmtrans_t
*trans
= handle
->trans
;
698 /* Check integrity of deltas */
699 EVENT(trans
, PM_TRANS_EVT_DELTA_INTEGRITY_START
, NULL
, NULL
);
701 for(i
= deltas
; i
; i
= i
->next
) {
702 pmdelta_t
*d
= alpm_list_getdata(i
);
703 const char *filename
= alpm_delta_get_filename(d
);
704 char *filepath
= _alpm_filecache_find(handle
, filename
);
705 const char *md5sum
= alpm_delta_get_md5sum(d
);
707 if(test_md5sum(trans
, filepath
, md5sum
) != 0) {
709 *data
= alpm_list_add(*data
, strdup(filename
));
714 pm_errno
= PM_ERR_DLT_INVALID
;
717 EVENT(trans
, PM_TRANS_EVT_DELTA_INTEGRITY_DONE
, NULL
, NULL
);
719 /* Use the deltas to generate the packages */
720 EVENT(trans
, PM_TRANS_EVT_DELTA_PATCHES_START
, NULL
, NULL
);
721 ret
= apply_deltas(handle
);
722 EVENT(trans
, PM_TRANS_EVT_DELTA_PATCHES_DONE
, NULL
, NULL
);
726 static int download_files(pmhandle_t
*handle
, alpm_list_t
**deltas
)
728 const char *cachedir
;
730 alpm_list_t
*files
= NULL
;
733 cachedir
= _alpm_filecache_setup(handle
);
734 handle
->trans
->state
= STATE_DOWNLOADING
;
736 /* Total progress - figure out the total download size if required to
737 * pass to the callback. This function is called once, and it is up to the
738 * frontend to compute incremental progress. */
739 if(handle
->totaldlcb
) {
740 off_t total_size
= (off_t
)0;
741 /* sum up the download size for each package and store total */
742 for(i
= handle
->trans
->add
; i
; i
= i
->next
) {
743 pmpkg_t
*spkg
= i
->data
;
744 total_size
+= spkg
->download_size
;
746 handle
->totaldlcb(total_size
);
749 /* group sync records by repository and download */
750 for(i
= handle
->dbs_sync
; i
; i
= i
->next
) {
751 pmdb_t
*current
= i
->data
;
753 for(j
= handle
->trans
->add
; j
; j
= j
->next
) {
754 pmpkg_t
*spkg
= j
->data
;
756 if(spkg
->origin
!= PKG_FROM_FILE
&& current
== spkg
->origin_data
.db
) {
757 const char *fname
= NULL
;
759 fname
= alpm_pkg_get_filename(spkg
);
760 ASSERT(fname
!= NULL
, RET_ERR(PM_ERR_PKG_INVALID_NAME
, -1));
761 alpm_list_t
*delta_path
= spkg
->delta_path
;
765 for(dlts
= delta_path
; dlts
; dlts
= dlts
->next
) {
766 pmdelta_t
*delta
= dlts
->data
;
767 if(delta
->download_size
!= 0) {
768 files
= alpm_list_add(files
, strdup(delta
->delta
));
770 /* keep a list of all the delta files for md5sums */
771 *deltas
= alpm_list_add(*deltas
, delta
);
774 } else if(spkg
->download_size
!= 0) {
775 files
= alpm_list_add(files
, strdup(fname
));
782 EVENT(handle
->trans
, PM_TRANS_EVT_RETRIEVE_START
, current
->treename
, NULL
);
783 for(j
= files
; j
; j
= j
->next
) {
784 const char *filename
= j
->data
;
787 for(server
= current
->servers
; server
; server
= server
->next
) {
788 const char *server_url
= server
->data
;
792 /* print server + filename into a buffer */
793 len
= strlen(server_url
) + strlen(filename
) + 2;
794 CALLOC(fileurl
, len
, sizeof(char), RET_ERR(PM_ERR_MEMORY
, -1));
795 snprintf(fileurl
, len
, "%s/%s", server_url
, filename
);
797 ret
= _alpm_download(fileurl
, cachedir
, 0, 1, 0);
810 _alpm_log(PM_LOG_WARNING
, _("failed to retrieve some files from %s\n"),
813 pm_errno
= PM_ERR_RETRIEVE
;
820 for(j
= handle
->trans
->add
; j
; j
= j
->next
) {
821 pmpkg_t
*pkg
= j
->data
;
822 pkg
->infolevel
&= ~INFRQ_DSIZE
;
823 pkg
->download_size
= 0;
826 /* clear out value to let callback know we are done */
827 if(handle
->totaldlcb
) {
828 handle
->totaldlcb(0);
833 int _alpm_sync_commit(pmhandle_t
*handle
, alpm_list_t
**data
)
836 alpm_list_t
*deltas
= NULL
;
837 size_t numtargs
, current
= 0, replaces
= 0;
839 pmtrans_t
*trans
= handle
->trans
;
841 if(download_files(handle
, &deltas
)) {
842 alpm_list_free(deltas
);
846 if(validate_deltas(handle
, deltas
, data
)) {
847 alpm_list_free(deltas
);
850 alpm_list_free(deltas
);
852 /* Check integrity of packages */
853 numtargs
= alpm_list_count(trans
->add
);
854 EVENT(trans
, PM_TRANS_EVT_INTEGRITY_START
, NULL
, NULL
);
858 for(i
= trans
->add
; i
; i
= i
->next
, current
++) {
859 pmpkg_t
*spkg
= i
->data
;
860 int percent
= (current
* 100) / numtargs
;
861 const char *filename
;
863 pgp_verify_t check_sig
;
865 PROGRESS(trans
, PM_TRANS_PROGRESS_INTEGRITY_START
, "", percent
,
867 if(spkg
->origin
== PKG_FROM_FILE
) {
868 continue; /* pkg_load() has been already called, this package is valid */
871 filename
= alpm_pkg_get_filename(spkg
);
872 filepath
= _alpm_filecache_find(handle
, filename
);
873 pmdb_t
*sdb
= alpm_pkg_get_db(spkg
);
874 check_sig
= _alpm_db_get_sigverify_level(sdb
);
876 /* load the package file and replace pkgcache entry with it in the target list */
877 /* TODO: alpm_pkg_get_db() will not work on this target anymore */
878 _alpm_log(PM_LOG_DEBUG
, "replacing pkgcache entry with package file for target %s\n", spkg
->name
);
879 pmpkg_t
*pkgfile
=_alpm_pkg_load_internal(handle
, filepath
, 1, spkg
->md5sum
,
880 spkg
->base64_sig
, check_sig
);
883 *data
= alpm_list_add(*data
, strdup(filename
));
888 pkgfile
->reason
= spkg
->reason
; /* copy over install reason */
890 _alpm_pkg_free_trans(spkg
); /* spkg has been removed from the target list */
893 PROGRESS(trans
, PM_TRANS_PROGRESS_INTEGRITY_START
, "", 100,
895 EVENT(trans
, PM_TRANS_EVT_INTEGRITY_DONE
, NULL
, NULL
);
899 RET_ERR(PM_ERR_PKG_INVALID
, -1);
902 if(trans
->flags
& PM_TRANS_FLAG_DOWNLOADONLY
) {
906 trans
->state
= STATE_COMMITING
;
908 replaces
= alpm_list_count(trans
->remove
);
910 /* fileconflict check */
911 if(!(trans
->flags
& PM_TRANS_FLAG_FORCE
)) {
912 EVENT(trans
, PM_TRANS_EVT_FILECONFLICTS_START
, NULL
, NULL
);
914 _alpm_log(PM_LOG_DEBUG
, "looking for file conflicts\n");
915 alpm_list_t
*conflict
= _alpm_db_find_fileconflicts(handle
,
916 trans
->add
, trans
->remove
);
921 alpm_list_free_inner(conflict
, (alpm_list_fn_free
)_alpm_fileconflict_free
);
922 alpm_list_free(conflict
);
924 RET_ERR(PM_ERR_FILE_CONFLICTS
, -1);
927 EVENT(trans
, PM_TRANS_EVT_FILECONFLICTS_DONE
, NULL
, NULL
);
930 /* check available disk space */
931 if(handle
->checkspace
) {
932 EVENT(trans
, PM_TRANS_EVT_DISKSPACE_START
, NULL
, NULL
);
934 _alpm_log(PM_LOG_DEBUG
, "checking available disk space\n");
935 if(_alpm_check_diskspace(handle
) == -1) {
936 _alpm_log(PM_LOG_ERROR
, "%s\n", _("not enough free disk space"));
940 EVENT(trans
, PM_TRANS_EVT_DISKSPACE_DONE
, NULL
, NULL
);
943 /* remove conflicting and to-be-replaced packages */
945 _alpm_log(PM_LOG_DEBUG
, "removing conflicting and to-be-replaced packages\n");
946 /* we want the frontend to be aware of commit details */
947 if(_alpm_remove_packages(handle
) == -1) {
948 _alpm_log(PM_LOG_ERROR
, _("could not commit removal transaction\n"));
953 /* install targets */
954 _alpm_log(PM_LOG_DEBUG
, "installing packages\n");
955 if(_alpm_upgrade_packages(handle
) == -1) {
956 _alpm_log(PM_LOG_ERROR
, _("could not commit transaction\n"));
963 /* vim: set ts=2 sw=2 noet: */