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 alpm_pkg_t SYMEXPORT
*alpm_sync_newversion(alpm_pkg_t
*pkg
, alpm_list_t
*dbs_sync
)
59 alpm_pkg_t
*spkg
= NULL
;
61 ASSERT(pkg
!= NULL
, return NULL
);
62 pkg
->handle
->pm_errno
= 0;
64 for(i
= dbs_sync
; !spkg
&& i
; i
= i
->next
) {
65 spkg
= _alpm_db_get_pkgfromcache(i
->data
, alpm_pkg_get_name(pkg
));
69 _alpm_log(pkg
->handle
, ALPM_LOG_DEBUG
, "'%s' not found in sync db => no upgrade\n",
70 alpm_pkg_get_name(pkg
));
74 /* compare versions and see if spkg is an upgrade */
75 if(_alpm_pkg_compare_versions(spkg
, pkg
) > 0) {
76 _alpm_log(pkg
->handle
, ALPM_LOG_DEBUG
, "new version of '%s' found (%s => %s)\n",
77 alpm_pkg_get_name(pkg
), alpm_pkg_get_version(pkg
),
78 alpm_pkg_get_version(spkg
));
81 /* spkg is not an upgrade */
85 /** Search for packages to upgrade and add them to the transaction. */
86 int SYMEXPORT
alpm_sync_sysupgrade(alpm_handle_t
*handle
, int enable_downgrade
)
88 alpm_list_t
*i
, *j
, *k
;
91 alpm_list_t
*dbs_sync
;
93 CHECK_HANDLE(handle
, return -1);
94 trans
= handle
->trans
;
95 db_local
= handle
->db_local
;
96 dbs_sync
= handle
->dbs_sync
;
97 ASSERT(trans
!= NULL
, RET_ERR(handle
, ALPM_ERR_TRANS_NULL
, -1));
98 ASSERT(trans
->state
== STATE_INITIALIZED
, RET_ERR(handle
, ALPM_ERR_TRANS_NOT_INITIALIZED
, -1));
100 _alpm_log(handle
, ALPM_LOG_DEBUG
, "checking for package upgrades\n");
101 for(i
= _alpm_db_get_pkgcache(db_local
); i
; i
= i
->next
) {
102 alpm_pkg_t
*lpkg
= i
->data
;
104 if(_alpm_pkg_find(trans
->add
, lpkg
->name
)) {
105 _alpm_log(handle
, ALPM_LOG_DEBUG
, "%s is already in the target list -- skipping\n", lpkg
->name
);
109 /* Search for literal then replacers in each sync database.
110 * If found, don't check other databases */
111 for(j
= dbs_sync
; j
; j
= j
->next
) {
112 alpm_db_t
*sdb
= j
->data
;
114 alpm_pkg_t
*spkg
= _alpm_db_get_pkgfromcache(sdb
, lpkg
->name
);
116 /* 1. literal was found in sdb */
117 int cmp
= _alpm_pkg_compare_versions(spkg
, lpkg
);
119 _alpm_log(handle
, ALPM_LOG_DEBUG
, "new version of '%s' found (%s => %s)\n",
120 lpkg
->name
, lpkg
->version
, spkg
->version
);
121 /* check IgnorePkg/IgnoreGroup */
122 if(_alpm_pkg_should_ignore(handle
, spkg
)
123 || _alpm_pkg_should_ignore(handle
, lpkg
)) {
124 _alpm_log(handle
, ALPM_LOG_WARNING
, _("%s: ignoring package upgrade (%s => %s)\n"),
125 lpkg
->name
, lpkg
->version
, spkg
->version
);
127 _alpm_log(handle
, ALPM_LOG_DEBUG
, "adding package %s-%s to the transaction targets\n",
128 spkg
->name
, spkg
->version
);
129 trans
->add
= alpm_list_add(trans
->add
, spkg
);
132 if(enable_downgrade
) {
133 /* check IgnorePkg/IgnoreGroup */
134 if(_alpm_pkg_should_ignore(handle
, spkg
)
135 || _alpm_pkg_should_ignore(handle
, lpkg
)) {
136 _alpm_log(handle
, ALPM_LOG_WARNING
, _("%s: ignoring package downgrade (%s => %s)\n"),
137 lpkg
->name
, lpkg
->version
, spkg
->version
);
139 _alpm_log(handle
, ALPM_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(handle
, ALPM_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(handle
, spkg
)
159 || _alpm_pkg_should_ignore(handle
, lpkg
)) {
160 _alpm_log(handle
, ALPM_LOG_WARNING
, _("ignoring package replacement (%s-%s => %s-%s)\n"),
161 lpkg
->name
, lpkg
->version
, spkg
->name
, spkg
->version
);
166 QUESTION(trans
, ALPM_TRANS_CONV_REPLACE_PKG
, lpkg
, spkg
, sdb
->treename
, &doreplace
);
171 /* If spkg is already in the target list, we append lpkg to spkg's
173 alpm_pkg_t
*tpkg
= _alpm_pkg_find(trans
->add
, spkg
->name
);
175 /* sanity check, multiple repos can contain spkg->name */
176 if(tpkg
->origin_data
.db
!= sdb
) {
177 _alpm_log(handle
, ALPM_LOG_WARNING
, _("cannot replace %s by %s\n"),
178 lpkg
->name
, spkg
->name
);
181 _alpm_log(handle
, ALPM_LOG_DEBUG
, "appending %s to the removes list of %s\n",
182 lpkg
->name
, tpkg
->name
);
183 tpkg
->removes
= alpm_list_add(tpkg
->removes
, lpkg
);
184 /* check the to-be-replaced package's reason field */
185 if(alpm_pkg_get_reason(lpkg
) == ALPM_PKG_REASON_EXPLICIT
) {
186 tpkg
->reason
= ALPM_PKG_REASON_EXPLICIT
;
189 /* add spkg to the target list */
190 /* copy over reason */
191 spkg
->reason
= alpm_pkg_get_reason(lpkg
);
192 spkg
->removes
= alpm_list_add(NULL
, lpkg
);
193 _alpm_log(handle
, ALPM_LOG_DEBUG
, "adding package %s-%s to the transaction targets\n",
194 spkg
->name
, spkg
->version
);
195 trans
->add
= alpm_list_add(trans
->add
, spkg
);
200 break; /* jump to next local package */
209 /** Find group members across a list of databases.
210 * If a member exists in several databases, only the first database is used.
211 * IgnorePkg is also handled.
212 * @param dbs the list of alpm_db_t *
213 * @pram name the name of the group
214 * @return the list of alpm_pkg_t * (caller is responsible for alpm_list_free)
216 alpm_list_t SYMEXPORT
*alpm_find_group_pkgs(alpm_list_t
*dbs
,
219 alpm_list_t
*i
, *j
, *pkgs
= NULL
, *ignorelist
= NULL
;
221 for(i
= dbs
; i
; i
= i
->next
) {
222 alpm_db_t
*db
= i
->data
;
223 alpm_group_t
*grp
= alpm_db_readgroup(db
, name
);
228 for(j
= grp
->packages
; j
; j
= j
->next
) {
229 alpm_pkg_t
*pkg
= j
->data
;
231 if(_alpm_pkg_find(ignorelist
, alpm_pkg_get_name(pkg
))) {
234 if(_alpm_pkg_should_ignore(db
->handle
, pkg
)) {
235 ignorelist
= alpm_list_add(ignorelist
, pkg
);
237 QUESTION(db
->handle
->trans
, ALPM_TRANS_CONV_INSTALL_IGNOREPKG
, pkg
,
238 NULL
, NULL
, &install
);
242 if(!_alpm_pkg_find(pkgs
, alpm_pkg_get_name(pkg
))) {
243 pkgs
= alpm_list_add(pkgs
, pkg
);
247 alpm_list_free(ignorelist
);
251 /** Compute the size of the files that will be downloaded to install a
253 * @param newpkg the new package to upgrade to
255 static int compute_download_size(alpm_pkg_t
*newpkg
)
260 alpm_handle_t
*handle
= newpkg
->handle
;
262 if(newpkg
->origin
!= PKG_FROM_SYNCDB
) {
263 newpkg
->infolevel
|= INFRQ_DSIZE
;
264 newpkg
->download_size
= 0;
268 fname
= alpm_pkg_get_filename(newpkg
);
269 ASSERT(fname
!= NULL
, RET_ERR(handle
, ALPM_ERR_PKG_INVALID_NAME
, -1));
270 fpath
= _alpm_filecache_find(handle
, fname
);
275 } else if(handle
->usedelta
) {
277 off_t pkgsize
= alpm_pkg_get_size(newpkg
);
279 dltsize
= _alpm_shortest_delta_path(handle
,
280 alpm_pkg_get_deltas(newpkg
),
281 alpm_pkg_get_filename(newpkg
),
282 &newpkg
->delta_path
);
284 if(newpkg
->delta_path
&& (dltsize
< pkgsize
* MAX_DELTA_RATIO
)) {
285 _alpm_log(handle
, ALPM_LOG_DEBUG
, "using delta size\n");
288 _alpm_log(handle
, ALPM_LOG_DEBUG
, "using package size\n");
289 size
= alpm_pkg_get_size(newpkg
);
290 alpm_list_free(newpkg
->delta_path
);
291 newpkg
->delta_path
= NULL
;
294 size
= alpm_pkg_get_size(newpkg
);
297 _alpm_log(handle
, ALPM_LOG_DEBUG
, "setting download size %jd for pkg %s\n",
298 (intmax_t)size
, alpm_pkg_get_name(newpkg
));
300 newpkg
->infolevel
|= INFRQ_DSIZE
;
301 newpkg
->download_size
= size
;
305 int _alpm_sync_prepare(alpm_handle_t
*handle
, alpm_list_t
**data
)
308 alpm_list_t
*deps
= NULL
;
309 alpm_list_t
*unresolvable
= NULL
;
310 alpm_list_t
*remove
= NULL
;
312 alpm_trans_t
*trans
= handle
->trans
;
318 if(!(trans
->flags
& ALPM_TRANS_FLAG_NODEPS
)) {
319 alpm_list_t
*resolved
= NULL
; /* target list after resolvedeps */
321 /* Build up list by repeatedly resolving each transaction package */
322 /* Resolve targets dependencies */
323 EVENT(trans
, ALPM_TRANS_EVT_RESOLVEDEPS_START
, NULL
, NULL
);
324 _alpm_log(handle
, ALPM_LOG_DEBUG
, "resolving target's dependencies\n");
326 /* build remove list for resolvedeps */
327 for(i
= trans
->add
; i
; i
= i
->next
) {
328 alpm_pkg_t
*spkg
= i
->data
;
329 for(j
= spkg
->removes
; j
; j
= j
->next
) {
330 remove
= alpm_list_add(remove
, j
->data
);
334 /* Compute the fake local database for resolvedeps (partial fix for the
335 * phonon/qt issue) */
336 alpm_list_t
*localpkgs
= alpm_list_diff(_alpm_db_get_pkgcache(handle
->db_local
),
337 trans
->add
, _alpm_pkg_cmp
);
339 /* Resolve packages in the transaction one at a time, in addition
340 building up a list of packages which could not be resolved. */
341 for(i
= trans
->add
; i
; i
= i
->next
) {
342 alpm_pkg_t
*pkg
= i
->data
;
343 if(_alpm_resolvedeps(handle
, localpkgs
, pkg
, trans
->add
,
344 &resolved
, remove
, data
) == -1) {
345 unresolvable
= alpm_list_add(unresolvable
, pkg
);
347 /* Else, [resolved] now additionally contains [pkg] and all of its
348 dependencies not already on the list */
350 alpm_list_free(localpkgs
);
352 /* If there were unresolvable top-level packages, prompt the user to
353 see if they'd like to ignore them rather than failing the sync */
354 if(unresolvable
!= NULL
) {
355 int remove_unresolvable
= 0;
356 QUESTION(trans
, ALPM_TRANS_CONV_REMOVE_PKGS
, unresolvable
,
357 NULL
, NULL
, &remove_unresolvable
);
358 if(remove_unresolvable
) {
359 /* User wants to remove the unresolvable packages from the
360 transaction. The packages will be removed from the actual
361 transaction when the transaction packages are replaced with a
362 dependency-reordered list below */
363 handle
->pm_errno
= 0; /* pm_errno was set by resolvedeps */
365 alpm_list_free_inner(*data
, (alpm_list_fn_free
)_alpm_depmiss_free
);
366 alpm_list_free(*data
);
370 /* pm_errno is set by resolvedeps */
371 alpm_list_free(resolved
);
377 /* Set DEPEND reason for pulled packages */
378 for(i
= resolved
; i
; i
= i
->next
) {
379 alpm_pkg_t
*pkg
= i
->data
;
380 if(!_alpm_pkg_find(trans
->add
, pkg
->name
)) {
381 pkg
->reason
= ALPM_PKG_REASON_DEPEND
;
385 /* Unresolvable packages will be removed from the target list, so
386 we free the transaction specific fields */
387 alpm_list_free_inner(unresolvable
, (alpm_list_fn_free
)_alpm_pkg_free_trans
);
389 /* re-order w.r.t. dependencies */
390 alpm_list_free(trans
->add
);
391 trans
->add
= _alpm_sortbydeps(handle
, resolved
, 0);
392 alpm_list_free(resolved
);
394 EVENT(trans
, ALPM_TRANS_EVT_RESOLVEDEPS_DONE
, NULL
, NULL
);
397 if(!(trans
->flags
& ALPM_TRANS_FLAG_NOCONFLICTS
)) {
398 /* check for inter-conflicts and whatnot */
399 EVENT(trans
, ALPM_TRANS_EVT_INTERCONFLICTS_START
, NULL
, NULL
);
401 _alpm_log(handle
, ALPM_LOG_DEBUG
, "looking for conflicts\n");
403 /* 1. check for conflicts in the target list */
404 _alpm_log(handle
, ALPM_LOG_DEBUG
, "check targets vs targets\n");
405 deps
= _alpm_innerconflicts(handle
, trans
->add
);
407 for(i
= deps
; i
; i
= i
->next
) {
408 alpm_conflict_t
*conflict
= i
->data
;
409 alpm_pkg_t
*rsync
, *sync
, *sync1
, *sync2
;
411 /* have we already removed one of the conflicting targets? */
412 sync1
= _alpm_pkg_find(trans
->add
, conflict
->package1
);
413 sync2
= _alpm_pkg_find(trans
->add
, conflict
->package2
);
414 if(!sync1
|| !sync2
) {
418 _alpm_log(handle
, ALPM_LOG_DEBUG
, "conflicting packages in the sync list: '%s' <-> '%s'\n",
419 conflict
->package1
, conflict
->package2
);
421 /* if sync1 provides sync2, we remove sync2 from the targets, and vice versa */
422 alpm_depend_t
*dep1
= _alpm_splitdep(conflict
->package1
);
423 alpm_depend_t
*dep2
= _alpm_splitdep(conflict
->package2
);
424 if(_alpm_depcmp(sync1
, dep2
)) {
427 } else if(_alpm_depcmp(sync2
, dep1
)) {
431 _alpm_log(handle
, ALPM_LOG_ERROR
, _("unresolvable package conflicts detected\n"));
432 handle
->pm_errno
= ALPM_ERR_CONFLICTING_DEPS
;
435 alpm_conflict_t
*newconflict
= _alpm_conflict_dup(conflict
);
437 *data
= alpm_list_add(*data
, newconflict
);
440 alpm_list_free_inner(deps
, (alpm_list_fn_free
)_alpm_conflict_free
);
441 alpm_list_free(deps
);
442 _alpm_dep_free(dep1
);
443 _alpm_dep_free(dep2
);
446 _alpm_dep_free(dep1
);
447 _alpm_dep_free(dep2
);
450 _alpm_log(handle
, ALPM_LOG_WARNING
,
451 _("removing '%s' from target list because it conflicts with '%s'\n"),
452 rsync
->name
, sync
->name
);
453 trans
->add
= alpm_list_remove(trans
->add
, rsync
, _alpm_pkg_cmp
, NULL
);
454 _alpm_pkg_free_trans(rsync
); /* rsync is not transaction target anymore */
458 alpm_list_free_inner(deps
, (alpm_list_fn_free
)_alpm_conflict_free
);
459 alpm_list_free(deps
);
462 /* 2. we check for target vs db conflicts (and resolve)*/
463 _alpm_log(handle
, ALPM_LOG_DEBUG
, "check targets vs db and db vs targets\n");
464 deps
= _alpm_outerconflicts(handle
->db_local
, trans
->add
);
466 for(i
= deps
; i
; i
= i
->next
) {
467 alpm_conflict_t
*conflict
= i
->data
;
469 /* if conflict->package2 (the local package) is not elected for removal,
472 for(j
= trans
->add
; j
&& !found
; j
= j
->next
) {
473 alpm_pkg_t
*spkg
= j
->data
;
474 if(_alpm_pkg_find(spkg
->removes
, conflict
->package2
)) {
482 _alpm_log(handle
, ALPM_LOG_DEBUG
, "package '%s' conflicts with '%s'\n",
483 conflict
->package1
, conflict
->package2
);
485 alpm_pkg_t
*sync
= _alpm_pkg_find(trans
->add
, conflict
->package1
);
486 alpm_pkg_t
*local
= _alpm_db_get_pkgfromcache(handle
->db_local
, conflict
->package2
);
488 QUESTION(trans
, ALPM_TRANS_CONV_CONFLICT_PKG
, conflict
->package1
,
489 conflict
->package2
, conflict
->reason
, &doremove
);
491 /* append to the removes list */
492 _alpm_log(handle
, ALPM_LOG_DEBUG
, "electing '%s' for removal\n", conflict
->package2
);
493 sync
->removes
= alpm_list_add(sync
->removes
, local
);
495 _alpm_log(handle
, ALPM_LOG_ERROR
, _("unresolvable package conflicts detected\n"));
496 handle
->pm_errno
= ALPM_ERR_CONFLICTING_DEPS
;
499 alpm_conflict_t
*newconflict
= _alpm_conflict_dup(conflict
);
501 *data
= alpm_list_add(*data
, newconflict
);
504 alpm_list_free_inner(deps
, (alpm_list_fn_free
)_alpm_conflict_free
);
505 alpm_list_free(deps
);
509 EVENT(trans
, ALPM_TRANS_EVT_INTERCONFLICTS_DONE
, NULL
, NULL
);
510 alpm_list_free_inner(deps
, (alpm_list_fn_free
)_alpm_conflict_free
);
511 alpm_list_free(deps
);
514 /* Build trans->remove list */
515 for(i
= trans
->add
; i
; i
= i
->next
) {
516 alpm_pkg_t
*spkg
= i
->data
;
517 for(j
= spkg
->removes
; j
; j
= j
->next
) {
518 alpm_pkg_t
*rpkg
= j
->data
;
519 if(!_alpm_pkg_find(trans
->remove
, rpkg
->name
)) {
520 _alpm_log(handle
, ALPM_LOG_DEBUG
, "adding '%s' to remove list\n", rpkg
->name
);
521 trans
->remove
= alpm_list_add(trans
->remove
, _alpm_pkg_dup(rpkg
));
526 if(!(trans
->flags
& ALPM_TRANS_FLAG_NODEPS
)) {
527 _alpm_log(handle
, ALPM_LOG_DEBUG
, "checking dependencies\n");
528 deps
= alpm_checkdeps(handle
, _alpm_db_get_pkgcache(handle
->db_local
),
529 trans
->remove
, trans
->add
, 1);
531 handle
->pm_errno
= ALPM_ERR_UNSATISFIED_DEPS
;
536 alpm_list_free_inner(deps
, (alpm_list_fn_free
)_alpm_depmiss_free
);
537 alpm_list_free(deps
);
542 for(i
= trans
->add
; i
; i
= i
->next
) {
543 /* update download size field */
544 alpm_pkg_t
*spkg
= i
->data
;
545 if(compute_download_size(spkg
) != 0) {
552 alpm_list_free(unresolvable
);
553 alpm_list_free(remove
);
558 /** Returns the size of the files that will be downloaded to install a
560 * @param newpkg the new package to upgrade to
561 * @return the size of the download
563 off_t SYMEXPORT
alpm_pkg_download_size(alpm_pkg_t
*newpkg
)
565 if(!(newpkg
->infolevel
& INFRQ_DSIZE
)) {
566 compute_download_size(newpkg
);
568 return newpkg
->download_size
;
571 static int endswith(const char *filename
, const char *extension
)
573 const char *s
= filename
+ strlen(filename
) - strlen(extension
);
574 return strcmp(s
, extension
) == 0;
577 /** Applies delta files to create an upgraded package file.
579 * All intermediate files are deleted, leaving only the starting and
580 * ending package files.
582 * @param handle the context handle
584 * @return 0 if all delta files were able to be applied, 1 otherwise.
586 static int apply_deltas(alpm_handle_t
*handle
)
590 const char *cachedir
= _alpm_filecache_setup(handle
);
591 alpm_trans_t
*trans
= handle
->trans
;
593 for(i
= trans
->add
; i
; i
= i
->next
) {
594 alpm_pkg_t
*spkg
= i
->data
;
595 alpm_list_t
*delta_path
= spkg
->delta_path
;
596 alpm_list_t
*dlts
= NULL
;
602 for(dlts
= delta_path
; dlts
; dlts
= dlts
->next
) {
603 alpm_delta_t
*d
= dlts
->data
;
604 char *delta
, *from
, *to
;
605 char command
[PATH_MAX
];
608 delta
= _alpm_filecache_find(handle
, d
->delta
);
609 /* the initial package might be in a different cachedir */
610 if(dlts
== delta_path
) {
611 from
= _alpm_filecache_find(handle
, d
->from
);
613 /* len = cachedir len + from len + '/' + null */
614 len
= strlen(cachedir
) + strlen(d
->from
) + 2;
615 CALLOC(from
, len
, sizeof(char), RET_ERR(handle
, ALPM_ERR_MEMORY
, 1));
616 snprintf(from
, len
, "%s/%s", cachedir
, d
->from
);
618 len
= strlen(cachedir
) + strlen(d
->to
) + 2;
619 CALLOC(to
, len
, sizeof(char), RET_ERR(handle
, ALPM_ERR_MEMORY
, 1));
620 snprintf(to
, len
, "%s/%s", cachedir
, d
->to
);
622 /* build the patch command */
623 if(endswith(to
, ".gz")) {
624 /* special handling for gzip : we disable timestamp with -n option */
625 snprintf(command
, PATH_MAX
, "xdelta3 -d -q -R -c -s %s %s | gzip -n > %s", from
, delta
, to
);
627 snprintf(command
, PATH_MAX
, "xdelta3 -d -q -s %s %s %s", from
, delta
, to
);
630 _alpm_log(handle
, ALPM_LOG_DEBUG
, "command: %s\n", command
);
632 EVENT(trans
, ALPM_TRANS_EVT_DELTA_PATCH_START
, d
->to
, d
->delta
);
634 int retval
= system(command
);
636 EVENT(trans
, ALPM_TRANS_EVT_DELTA_PATCH_DONE
, NULL
, NULL
);
638 /* delete the delta file */
641 /* Delete the 'from' package but only if it is an intermediate
642 * package. The starting 'from' package should be kept, just
643 * as if deltas were not used. */
644 if(dlts
!= delta_path
) {
653 /* one delta failed for this package, cancel the remaining ones */
654 EVENT(trans
, ALPM_TRANS_EVT_DELTA_PATCH_FAILED
, NULL
, NULL
);
655 handle
->pm_errno
= ALPM_ERR_DLT_PATCHFAILED
;
665 /** Compares the md5sum of a file to the expected value.
667 * If the md5sum does not match, the user is asked whether the file
670 * @param trans the transaction
671 * @param filename the absolute path of the file to test
672 * @param md5sum the expected md5sum of the file
674 * @return 0 if the md5sum matched, 1 if not, -1 in case of errors
676 static int test_md5sum(alpm_trans_t
*trans
, const char *filepath
,
679 int ret
= _alpm_test_md5sum(filepath
, md5sum
);
682 QUESTION(trans
, ALPM_TRANS_CONV_CORRUPTED_PKG
, (char *)filepath
,
683 NULL
, NULL
, &doremove
);
692 static int validate_deltas(alpm_handle_t
*handle
, alpm_list_t
*deltas
,
695 int errors
= 0, ret
= 0;
697 alpm_trans_t
*trans
= handle
->trans
;
703 /* Check integrity of deltas */
704 EVENT(trans
, ALPM_TRANS_EVT_DELTA_INTEGRITY_START
, NULL
, NULL
);
706 for(i
= deltas
; i
; i
= i
->next
) {
707 alpm_delta_t
*d
= alpm_list_getdata(i
);
708 char *filepath
= _alpm_filecache_find(handle
, d
->delta
);
710 if(test_md5sum(trans
, filepath
, d
->delta_md5
) != 0) {
712 *data
= alpm_list_add(*data
, strdup(d
->delta
));
717 handle
->pm_errno
= ALPM_ERR_DLT_INVALID
;
720 EVENT(trans
, ALPM_TRANS_EVT_DELTA_INTEGRITY_DONE
, NULL
, NULL
);
722 /* Use the deltas to generate the packages */
723 EVENT(trans
, ALPM_TRANS_EVT_DELTA_PATCHES_START
, NULL
, NULL
);
724 ret
= apply_deltas(handle
);
725 EVENT(trans
, ALPM_TRANS_EVT_DELTA_PATCHES_DONE
, NULL
, NULL
);
729 static int download_files(alpm_handle_t
*handle
, alpm_list_t
**deltas
)
731 const char *cachedir
;
733 alpm_list_t
*files
= NULL
;
736 cachedir
= _alpm_filecache_setup(handle
);
737 handle
->trans
->state
= STATE_DOWNLOADING
;
739 /* Total progress - figure out the total download size if required to
740 * pass to the callback. This function is called once, and it is up to the
741 * frontend to compute incremental progress. */
742 if(handle
->totaldlcb
) {
743 off_t total_size
= (off_t
)0;
744 /* sum up the download size for each package and store total */
745 for(i
= handle
->trans
->add
; i
; i
= i
->next
) {
746 alpm_pkg_t
*spkg
= i
->data
;
747 total_size
+= spkg
->download_size
;
749 handle
->totaldlcb(total_size
);
752 /* group sync records by repository and download */
753 for(i
= handle
->dbs_sync
; i
; i
= i
->next
) {
754 alpm_db_t
*current
= i
->data
;
756 for(j
= handle
->trans
->add
; j
; j
= j
->next
) {
757 alpm_pkg_t
*spkg
= j
->data
;
759 if(spkg
->origin
!= PKG_FROM_FILE
&& current
== spkg
->origin_data
.db
) {
760 const char *fname
= NULL
;
762 fname
= alpm_pkg_get_filename(spkg
);
763 ASSERT(fname
!= NULL
, RET_ERR(handle
, ALPM_ERR_PKG_INVALID_NAME
, -1));
764 alpm_list_t
*delta_path
= spkg
->delta_path
;
768 for(dlts
= delta_path
; dlts
; dlts
= dlts
->next
) {
769 alpm_delta_t
*delta
= dlts
->data
;
770 if(delta
->download_size
!= 0) {
771 files
= alpm_list_add(files
, strdup(delta
->delta
));
773 /* keep a list of all the delta files for md5sums */
774 *deltas
= alpm_list_add(*deltas
, delta
);
777 } else if(spkg
->download_size
!= 0) {
778 files
= alpm_list_add(files
, strdup(fname
));
785 EVENT(handle
->trans
, ALPM_TRANS_EVT_RETRIEVE_START
, current
->treename
, NULL
);
786 for(j
= files
; j
; j
= j
->next
) {
787 const char *filename
= j
->data
;
790 for(server
= current
->servers
; server
; server
= server
->next
) {
791 const char *server_url
= server
->data
;
795 /* print server + filename into a buffer */
796 len
= strlen(server_url
) + strlen(filename
) + 2;
797 CALLOC(fileurl
, len
, sizeof(char), RET_ERR(handle
, ALPM_ERR_MEMORY
, -1));
798 snprintf(fileurl
, len
, "%s/%s", server_url
, filename
);
800 ret
= _alpm_download(handle
, fileurl
, cachedir
, 0, 1, 0);
813 _alpm_log(handle
, ALPM_LOG_WARNING
, _("failed to retrieve some files from %s\n"),
815 if(handle
->pm_errno
== 0) {
816 handle
->pm_errno
= ALPM_ERR_RETRIEVE
;
823 for(j
= handle
->trans
->add
; j
; j
= j
->next
) {
824 alpm_pkg_t
*pkg
= j
->data
;
825 pkg
->infolevel
&= ~INFRQ_DSIZE
;
826 pkg
->download_size
= 0;
829 /* clear out value to let callback know we are done */
830 if(handle
->totaldlcb
) {
831 handle
->totaldlcb(0);
836 int _alpm_sync_commit(alpm_handle_t
*handle
, alpm_list_t
**data
)
839 alpm_list_t
*deltas
= NULL
;
840 size_t numtargs
, current
= 0, replaces
= 0;
842 alpm_trans_t
*trans
= handle
->trans
;
844 if(download_files(handle
, &deltas
)) {
845 alpm_list_free(deltas
);
849 if(validate_deltas(handle
, deltas
, data
)) {
850 alpm_list_free(deltas
);
853 alpm_list_free(deltas
);
855 /* Check integrity of packages */
856 numtargs
= alpm_list_count(trans
->add
);
857 EVENT(trans
, ALPM_TRANS_EVT_INTEGRITY_START
, NULL
, NULL
);
861 for(i
= trans
->add
; i
; i
= i
->next
, current
++) {
862 alpm_pkg_t
*spkg
= i
->data
;
863 int percent
= (current
* 100) / numtargs
;
864 const char *filename
;
866 pgp_verify_t check_sig
;
868 PROGRESS(trans
, ALPM_TRANS_PROGRESS_INTEGRITY_START
, "", percent
,
870 if(spkg
->origin
== PKG_FROM_FILE
) {
871 continue; /* pkg_load() has been already called, this package is valid */
874 filename
= alpm_pkg_get_filename(spkg
);
875 filepath
= _alpm_filecache_find(handle
, filename
);
876 alpm_db_t
*sdb
= alpm_pkg_get_db(spkg
);
877 check_sig
= alpm_db_get_sigverify_level(sdb
);
879 /* load the package file and replace pkgcache entry with it in the target list */
880 /* TODO: alpm_pkg_get_db() will not work on this target anymore */
881 _alpm_log(handle
, ALPM_LOG_DEBUG
,
882 "replacing pkgcache entry with package file for target %s\n",
884 alpm_pkg_t
*pkgfile
=_alpm_pkg_load_internal(handle
, filepath
, 1, spkg
->md5sum
,
885 spkg
->base64_sig
, check_sig
);
888 *data
= alpm_list_add(*data
, strdup(filename
));
893 pkgfile
->reason
= spkg
->reason
; /* copy over install reason */
895 _alpm_pkg_free_trans(spkg
); /* spkg has been removed from the target list */
898 PROGRESS(trans
, ALPM_TRANS_PROGRESS_INTEGRITY_START
, "", 100,
900 EVENT(trans
, ALPM_TRANS_EVT_INTEGRITY_DONE
, NULL
, NULL
);
904 RET_ERR(handle
, ALPM_ERR_PKG_INVALID
, -1);
907 if(trans
->flags
& ALPM_TRANS_FLAG_DOWNLOADONLY
) {
911 trans
->state
= STATE_COMMITING
;
913 replaces
= alpm_list_count(trans
->remove
);
915 /* fileconflict check */
916 if(!(trans
->flags
& ALPM_TRANS_FLAG_FORCE
)) {
917 EVENT(trans
, ALPM_TRANS_EVT_FILECONFLICTS_START
, NULL
, NULL
);
919 _alpm_log(handle
, ALPM_LOG_DEBUG
, "looking for file conflicts\n");
920 alpm_list_t
*conflict
= _alpm_db_find_fileconflicts(handle
,
921 trans
->add
, trans
->remove
);
926 alpm_list_free_inner(conflict
, (alpm_list_fn_free
)_alpm_fileconflict_free
);
927 alpm_list_free(conflict
);
929 RET_ERR(handle
, ALPM_ERR_FILE_CONFLICTS
, -1);
932 EVENT(trans
, ALPM_TRANS_EVT_FILECONFLICTS_DONE
, NULL
, NULL
);
935 /* check available disk space */
936 if(handle
->checkspace
) {
937 EVENT(trans
, ALPM_TRANS_EVT_DISKSPACE_START
, NULL
, NULL
);
939 _alpm_log(handle
, ALPM_LOG_DEBUG
, "checking available disk space\n");
940 if(_alpm_check_diskspace(handle
) == -1) {
941 _alpm_log(handle
, ALPM_LOG_ERROR
, "%s\n", _("not enough free disk space"));
945 EVENT(trans
, ALPM_TRANS_EVT_DISKSPACE_DONE
, NULL
, NULL
);
948 /* remove conflicting and to-be-replaced packages */
950 _alpm_log(handle
, ALPM_LOG_DEBUG
, "removing conflicting and to-be-replaced packages\n");
951 /* we want the frontend to be aware of commit details */
952 if(_alpm_remove_packages(handle
) == -1) {
953 _alpm_log(handle
, ALPM_LOG_ERROR
, _("could not commit removal transaction\n"));
958 /* install targets */
959 _alpm_log(handle
, ALPM_LOG_DEBUG
, "installing packages\n");
960 if(_alpm_upgrade_packages(handle
) == -1) {
961 _alpm_log(handle
, ALPM_LOG_ERROR
, _("could not commit transaction\n"));
968 /* vim: set ts=2 sw=2 noet: */