Remove use of no-op accessor functions in library
[pacman-ng.git] / lib / libalpm / sync.c
blobb9cb1fbea76e33378c5c58f5e32c41112c68b671
1 /*
2 * sync.c
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/>.
24 #include "config.h"
26 #include <sys/types.h> /* off_t */
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdint.h> /* intmax_t */
31 #include <unistd.h>
32 #include <limits.h>
34 /* libalpm */
35 #include "sync.h"
36 #include "alpm_list.h"
37 #include "log.h"
38 #include "package.h"
39 #include "db.h"
40 #include "deps.h"
41 #include "conflict.h"
42 #include "trans.h"
43 #include "add.h"
44 #include "util.h"
45 #include "handle.h"
46 #include "alpm.h"
47 #include "dload.h"
48 #include "delta.h"
49 #include "remove.h"
50 #include "diskspace.h"
51 #include "signing.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)
58 alpm_list_t *i;
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, pkg->name);
68 if(spkg == NULL) {
69 _alpm_log(pkg->handle, ALPM_LOG_DEBUG, "'%s' not found in sync db => no upgrade\n",
70 pkg->name);
71 return NULL;
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 pkg->name, pkg->version, spkg->version);
78 return spkg;
80 /* spkg is not an upgrade */
81 return NULL;
84 /** Search for packages to upgrade and add them to the transaction. */
85 int SYMEXPORT alpm_sync_sysupgrade(alpm_handle_t *handle, int enable_downgrade)
87 alpm_list_t *i, *j;
88 alpm_trans_t *trans;
90 CHECK_HANDLE(handle, return -1);
91 trans = handle->trans;
92 ASSERT(trans != NULL, RET_ERR(handle, ALPM_ERR_TRANS_NULL, -1));
93 ASSERT(trans->state == STATE_INITIALIZED, RET_ERR(handle, ALPM_ERR_TRANS_NOT_INITIALIZED, -1));
95 _alpm_log(handle, ALPM_LOG_DEBUG, "checking for package upgrades\n");
96 for(i = _alpm_db_get_pkgcache(handle->db_local); i; i = i->next) {
97 alpm_pkg_t *lpkg = i->data;
99 if(_alpm_pkg_find(trans->add, lpkg->name)) {
100 _alpm_log(handle, ALPM_LOG_DEBUG, "%s is already in the target list -- skipping\n", lpkg->name);
101 continue;
104 /* Search for literal then replacers in each sync database.
105 * If found, don't check other databases */
106 for(j = handle->dbs_sync; j; j = j->next) {
107 alpm_db_t *sdb = j->data;
108 /* Check sdb */
109 alpm_pkg_t *spkg = _alpm_db_get_pkgfromcache(sdb, lpkg->name);
110 if(spkg) {
111 /* 1. literal was found in sdb */
112 int cmp = _alpm_pkg_compare_versions(spkg, lpkg);
113 if(cmp > 0) {
114 _alpm_log(handle, ALPM_LOG_DEBUG, "new version of '%s' found (%s => %s)\n",
115 lpkg->name, lpkg->version, spkg->version);
116 /* check IgnorePkg/IgnoreGroup */
117 if(_alpm_pkg_should_ignore(handle, spkg)
118 || _alpm_pkg_should_ignore(handle, lpkg)) {
119 _alpm_log(handle, ALPM_LOG_WARNING, _("%s: ignoring package upgrade (%s => %s)\n"),
120 lpkg->name, lpkg->version, spkg->version);
121 } else {
122 _alpm_log(handle, ALPM_LOG_DEBUG, "adding package %s-%s to the transaction targets\n",
123 spkg->name, spkg->version);
124 trans->add = alpm_list_add(trans->add, spkg);
126 } else if(cmp < 0) {
127 if(enable_downgrade) {
128 /* check IgnorePkg/IgnoreGroup */
129 if(_alpm_pkg_should_ignore(handle, spkg)
130 || _alpm_pkg_should_ignore(handle, lpkg)) {
131 _alpm_log(handle, ALPM_LOG_WARNING, _("%s: ignoring package downgrade (%s => %s)\n"),
132 lpkg->name, lpkg->version, spkg->version);
133 } else {
134 _alpm_log(handle, ALPM_LOG_WARNING, _("%s: downgrading from version %s to version %s\n"),
135 lpkg->name, lpkg->version, spkg->version);
136 trans->add = alpm_list_add(trans->add, spkg);
138 } else {
139 _alpm_log(handle, ALPM_LOG_WARNING, _("%s: local (%s) is newer than %s (%s)\n"),
140 lpkg->name, lpkg->version, sdb->treename, spkg->version);
143 /* jump to next local package */
144 break;
145 } else {
146 /* 2. search for replacers in sdb */
147 alpm_list_t *k, *l;
148 for(k = _alpm_db_get_pkgcache(sdb); k; k = k->next) {
149 int found = 0;
150 spkg = k->data;
151 for(l = alpm_pkg_get_replaces(spkg); l; l = l->next) {
152 alpm_depend_t *replace = l->data;
153 if(_alpm_depcmp(lpkg, replace)) {
154 found = 1;
155 break;
158 if(found) {
159 /* check IgnorePkg/IgnoreGroup */
160 if(_alpm_pkg_should_ignore(handle, spkg)
161 || _alpm_pkg_should_ignore(handle, lpkg)) {
162 _alpm_log(handle, ALPM_LOG_WARNING,
163 _("ignoring package replacement (%s-%s => %s-%s)\n"),
164 lpkg->name, lpkg->version, spkg->name, spkg->version);
165 continue;
168 int doreplace = 0;
169 QUESTION(trans, ALPM_TRANS_CONV_REPLACE_PKG, lpkg, spkg, sdb->treename, &doreplace);
170 if(!doreplace) {
171 continue;
174 /* If spkg is already in the target list, we append lpkg to spkg's
175 * removes list */
176 alpm_pkg_t *tpkg = _alpm_pkg_find(trans->add, spkg->name);
177 if(tpkg) {
178 /* sanity check, multiple repos can contain spkg->name */
179 if(tpkg->origin_data.db != sdb) {
180 _alpm_log(handle, ALPM_LOG_WARNING, _("cannot replace %s by %s\n"),
181 lpkg->name, spkg->name);
182 continue;
184 _alpm_log(handle, ALPM_LOG_DEBUG, "appending %s to the removes list of %s\n",
185 lpkg->name, tpkg->name);
186 tpkg->removes = alpm_list_add(tpkg->removes, lpkg);
187 /* check the to-be-replaced package's reason field */
188 if(alpm_pkg_get_reason(lpkg) == ALPM_PKG_REASON_EXPLICIT) {
189 tpkg->reason = ALPM_PKG_REASON_EXPLICIT;
191 } else {
192 /* add spkg to the target list */
193 /* copy over reason */
194 spkg->reason = alpm_pkg_get_reason(lpkg);
195 spkg->removes = alpm_list_add(NULL, lpkg);
196 _alpm_log(handle, ALPM_LOG_DEBUG, "adding package %s-%s to the transaction targets\n",
197 spkg->name, spkg->version);
198 trans->add = alpm_list_add(trans->add, spkg);
206 return 0;
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,
217 const char *name)
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);
225 if(!grp)
226 continue;
228 for(j = grp->packages; j; j = j->next) {
229 alpm_pkg_t *pkg = j->data;
231 if(_alpm_pkg_find(ignorelist, pkg->name)) {
232 continue;
234 if(_alpm_pkg_should_ignore(db->handle, pkg)) {
235 ignorelist = alpm_list_add(ignorelist, pkg);
236 int install = 0;
237 QUESTION(db->handle->trans, ALPM_TRANS_CONV_INSTALL_IGNOREPKG, pkg,
238 NULL, NULL, &install);
239 if(!install)
240 continue;
242 if(!_alpm_pkg_find(pkgs, pkg->name)) {
243 pkgs = alpm_list_add(pkgs, pkg);
247 alpm_list_free(ignorelist);
248 return pkgs;
251 /** Compute the size of the files that will be downloaded to install a
252 * package.
253 * @param newpkg the new package to upgrade to
255 static int compute_download_size(alpm_pkg_t *newpkg)
257 const char *fname;
258 char *fpath;
259 off_t size = 0;
260 alpm_handle_t *handle = newpkg->handle;
262 if(newpkg->origin != PKG_FROM_SYNCDB) {
263 newpkg->infolevel |= INFRQ_DSIZE;
264 newpkg->download_size = 0;
265 return 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);
272 if(fpath) {
273 FREE(fpath);
274 size = 0;
275 } else if(handle->usedelta) {
276 off_t dltsize;
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");
286 size = dltsize;
287 } else {
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;
293 } else {
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, newpkg->name);
300 newpkg->infolevel |= INFRQ_DSIZE;
301 newpkg->download_size = size;
302 return 0;
305 int _alpm_sync_prepare(alpm_handle_t *handle, alpm_list_t **data)
307 alpm_list_t *i, *j;
308 alpm_list_t *deps = NULL;
309 alpm_list_t *unresolvable = NULL;
310 alpm_list_t *remove = NULL;
311 int ret = 0;
312 alpm_trans_t *trans = handle->trans;
314 if(data) {
315 *data = NULL;
318 /* ensure all sync database are valid since we will be using them */
319 for(i = handle->dbs_sync; i; i = i->next) {
320 const alpm_db_t *db = i->data;
321 if(db->status & DB_STATUS_INVALID) {
322 RET_ERR(handle, ALPM_ERR_DB_INVALID, -1);
324 if(db->status & DB_STATUS_MISSING) {
325 RET_ERR(handle, ALPM_ERR_DB_NOT_FOUND, -1);
329 if(!(trans->flags & ALPM_TRANS_FLAG_NODEPS)) {
330 alpm_list_t *resolved = NULL; /* target list after resolvedeps */
332 /* Build up list by repeatedly resolving each transaction package */
333 /* Resolve targets dependencies */
334 EVENT(trans, ALPM_TRANS_EVT_RESOLVEDEPS_START, NULL, NULL);
335 _alpm_log(handle, ALPM_LOG_DEBUG, "resolving target's dependencies\n");
337 /* build remove list for resolvedeps */
338 for(i = trans->add; i; i = i->next) {
339 alpm_pkg_t *spkg = i->data;
340 for(j = spkg->removes; j; j = j->next) {
341 remove = alpm_list_add(remove, j->data);
345 /* Compute the fake local database for resolvedeps (partial fix for the
346 * phonon/qt issue) */
347 alpm_list_t *localpkgs = alpm_list_diff(_alpm_db_get_pkgcache(handle->db_local),
348 trans->add, _alpm_pkg_cmp);
350 /* Resolve packages in the transaction one at a time, in addition
351 building up a list of packages which could not be resolved. */
352 for(i = trans->add; i; i = i->next) {
353 alpm_pkg_t *pkg = i->data;
354 if(_alpm_resolvedeps(handle, localpkgs, pkg, trans->add,
355 &resolved, remove, data) == -1) {
356 unresolvable = alpm_list_add(unresolvable, pkg);
358 /* Else, [resolved] now additionally contains [pkg] and all of its
359 dependencies not already on the list */
361 alpm_list_free(localpkgs);
363 /* If there were unresolvable top-level packages, prompt the user to
364 see if they'd like to ignore them rather than failing the sync */
365 if(unresolvable != NULL) {
366 int remove_unresolvable = 0;
367 QUESTION(trans, ALPM_TRANS_CONV_REMOVE_PKGS, unresolvable,
368 NULL, NULL, &remove_unresolvable);
369 if(remove_unresolvable) {
370 /* User wants to remove the unresolvable packages from the
371 transaction. The packages will be removed from the actual
372 transaction when the transaction packages are replaced with a
373 dependency-reordered list below */
374 handle->pm_errno = 0; /* pm_errno was set by resolvedeps */
375 if(data) {
376 alpm_list_free_inner(*data, (alpm_list_fn_free)_alpm_depmiss_free);
377 alpm_list_free(*data);
378 *data = NULL;
380 } else {
381 /* pm_errno is set by resolvedeps */
382 alpm_list_free(resolved);
383 ret = -1;
384 goto cleanup;
388 /* Set DEPEND reason for pulled packages */
389 for(i = resolved; i; i = i->next) {
390 alpm_pkg_t *pkg = i->data;
391 if(!_alpm_pkg_find(trans->add, pkg->name)) {
392 pkg->reason = ALPM_PKG_REASON_DEPEND;
396 /* Unresolvable packages will be removed from the target list, so
397 we free the transaction specific fields */
398 alpm_list_free_inner(unresolvable, (alpm_list_fn_free)_alpm_pkg_free_trans);
400 /* re-order w.r.t. dependencies */
401 alpm_list_free(trans->add);
402 trans->add = _alpm_sortbydeps(handle, resolved, 0);
403 alpm_list_free(resolved);
405 EVENT(trans, ALPM_TRANS_EVT_RESOLVEDEPS_DONE, NULL, NULL);
408 if(!(trans->flags & ALPM_TRANS_FLAG_NOCONFLICTS)) {
409 /* check for inter-conflicts and whatnot */
410 EVENT(trans, ALPM_TRANS_EVT_INTERCONFLICTS_START, NULL, NULL);
412 _alpm_log(handle, ALPM_LOG_DEBUG, "looking for conflicts\n");
414 /* 1. check for conflicts in the target list */
415 _alpm_log(handle, ALPM_LOG_DEBUG, "check targets vs targets\n");
416 deps = _alpm_innerconflicts(handle, trans->add);
418 for(i = deps; i; i = i->next) {
419 alpm_conflict_t *conflict = i->data;
420 alpm_pkg_t *rsync, *sync, *sync1, *sync2;
422 /* have we already removed one of the conflicting targets? */
423 sync1 = _alpm_pkg_find(trans->add, conflict->package1);
424 sync2 = _alpm_pkg_find(trans->add, conflict->package2);
425 if(!sync1 || !sync2) {
426 continue;
429 _alpm_log(handle, ALPM_LOG_DEBUG, "conflicting packages in the sync list: '%s' <-> '%s'\n",
430 conflict->package1, conflict->package2);
432 /* if sync1 provides sync2, we remove sync2 from the targets, and vice versa */
433 alpm_depend_t *dep1 = _alpm_splitdep(conflict->package1);
434 alpm_depend_t *dep2 = _alpm_splitdep(conflict->package2);
435 if(_alpm_depcmp(sync1, dep2)) {
436 rsync = sync2;
437 sync = sync1;
438 } else if(_alpm_depcmp(sync2, dep1)) {
439 rsync = sync1;
440 sync = sync2;
441 } else {
442 _alpm_log(handle, ALPM_LOG_ERROR, _("unresolvable package conflicts detected\n"));
443 handle->pm_errno = ALPM_ERR_CONFLICTING_DEPS;
444 ret = -1;
445 if(data) {
446 alpm_conflict_t *newconflict = _alpm_conflict_dup(conflict);
447 if(newconflict) {
448 *data = alpm_list_add(*data, newconflict);
451 alpm_list_free_inner(deps, (alpm_list_fn_free)_alpm_conflict_free);
452 alpm_list_free(deps);
453 _alpm_dep_free(dep1);
454 _alpm_dep_free(dep2);
455 goto cleanup;
457 _alpm_dep_free(dep1);
458 _alpm_dep_free(dep2);
460 /* Prints warning */
461 _alpm_log(handle, ALPM_LOG_WARNING,
462 _("removing '%s' from target list because it conflicts with '%s'\n"),
463 rsync->name, sync->name);
464 trans->add = alpm_list_remove(trans->add, rsync, _alpm_pkg_cmp, NULL);
465 _alpm_pkg_free_trans(rsync); /* rsync is not transaction target anymore */
466 continue;
469 alpm_list_free_inner(deps, (alpm_list_fn_free)_alpm_conflict_free);
470 alpm_list_free(deps);
471 deps = NULL;
473 /* 2. we check for target vs db conflicts (and resolve)*/
474 _alpm_log(handle, ALPM_LOG_DEBUG, "check targets vs db and db vs targets\n");
475 deps = _alpm_outerconflicts(handle->db_local, trans->add);
477 for(i = deps; i; i = i->next) {
478 alpm_conflict_t *conflict = i->data;
480 /* if conflict->package2 (the local package) is not elected for removal,
481 we ask the user */
482 int found = 0;
483 for(j = trans->add; j && !found; j = j->next) {
484 alpm_pkg_t *spkg = j->data;
485 if(_alpm_pkg_find(spkg->removes, conflict->package2)) {
486 found = 1;
489 if(found) {
490 continue;
493 _alpm_log(handle, ALPM_LOG_DEBUG, "package '%s' conflicts with '%s'\n",
494 conflict->package1, conflict->package2);
496 alpm_pkg_t *sync = _alpm_pkg_find(trans->add, conflict->package1);
497 alpm_pkg_t *local = _alpm_db_get_pkgfromcache(handle->db_local, conflict->package2);
498 int doremove = 0;
499 QUESTION(trans, ALPM_TRANS_CONV_CONFLICT_PKG, conflict->package1,
500 conflict->package2, conflict->reason, &doremove);
501 if(doremove) {
502 /* append to the removes list */
503 _alpm_log(handle, ALPM_LOG_DEBUG, "electing '%s' for removal\n", conflict->package2);
504 sync->removes = alpm_list_add(sync->removes, local);
505 } else { /* abort */
506 _alpm_log(handle, ALPM_LOG_ERROR, _("unresolvable package conflicts detected\n"));
507 handle->pm_errno = ALPM_ERR_CONFLICTING_DEPS;
508 ret = -1;
509 if(data) {
510 alpm_conflict_t *newconflict = _alpm_conflict_dup(conflict);
511 if(newconflict) {
512 *data = alpm_list_add(*data, newconflict);
515 alpm_list_free_inner(deps, (alpm_list_fn_free)_alpm_conflict_free);
516 alpm_list_free(deps);
517 goto cleanup;
520 EVENT(trans, ALPM_TRANS_EVT_INTERCONFLICTS_DONE, NULL, NULL);
521 alpm_list_free_inner(deps, (alpm_list_fn_free)_alpm_conflict_free);
522 alpm_list_free(deps);
525 /* Build trans->remove list */
526 for(i = trans->add; i; i = i->next) {
527 alpm_pkg_t *spkg = i->data;
528 for(j = spkg->removes; j; j = j->next) {
529 alpm_pkg_t *rpkg = j->data;
530 if(!_alpm_pkg_find(trans->remove, rpkg->name)) {
531 _alpm_log(handle, ALPM_LOG_DEBUG, "adding '%s' to remove list\n", rpkg->name);
532 trans->remove = alpm_list_add(trans->remove, _alpm_pkg_dup(rpkg));
537 if(!(trans->flags & ALPM_TRANS_FLAG_NODEPS)) {
538 _alpm_log(handle, ALPM_LOG_DEBUG, "checking dependencies\n");
539 deps = alpm_checkdeps(handle, _alpm_db_get_pkgcache(handle->db_local),
540 trans->remove, trans->add, 1);
541 if(deps) {
542 handle->pm_errno = ALPM_ERR_UNSATISFIED_DEPS;
543 ret = -1;
544 if(data) {
545 *data = deps;
546 } else {
547 alpm_list_free_inner(deps, (alpm_list_fn_free)_alpm_depmiss_free);
548 alpm_list_free(deps);
550 goto cleanup;
553 for(i = trans->add; i; i = i->next) {
554 /* update download size field */
555 alpm_pkg_t *spkg = i->data;
556 if(compute_download_size(spkg) != 0) {
557 ret = -1;
558 goto cleanup;
562 cleanup:
563 alpm_list_free(unresolvable);
564 alpm_list_free(remove);
566 return ret;
569 /** Returns the size of the files that will be downloaded to install a
570 * package.
571 * @param newpkg the new package to upgrade to
572 * @return the size of the download
574 off_t SYMEXPORT alpm_pkg_download_size(alpm_pkg_t *newpkg)
576 if(!(newpkg->infolevel & INFRQ_DSIZE)) {
577 compute_download_size(newpkg);
579 return newpkg->download_size;
582 static int endswith(const char *filename, const char *extension)
584 const char *s = filename + strlen(filename) - strlen(extension);
585 return strcmp(s, extension) == 0;
588 /** Applies delta files to create an upgraded package file.
590 * All intermediate files are deleted, leaving only the starting and
591 * ending package files.
593 * @param handle the context handle
595 * @return 0 if all delta files were able to be applied, 1 otherwise.
597 static int apply_deltas(alpm_handle_t *handle)
599 alpm_list_t *i;
600 int ret = 0;
601 const char *cachedir = _alpm_filecache_setup(handle);
602 alpm_trans_t *trans = handle->trans;
604 for(i = trans->add; i; i = i->next) {
605 alpm_pkg_t *spkg = i->data;
606 alpm_list_t *delta_path = spkg->delta_path;
607 alpm_list_t *dlts = NULL;
609 if(!delta_path) {
610 continue;
613 for(dlts = delta_path; dlts; dlts = dlts->next) {
614 alpm_delta_t *d = dlts->data;
615 char *delta, *from, *to;
616 char command[PATH_MAX];
617 size_t len = 0;
619 delta = _alpm_filecache_find(handle, d->delta);
620 /* the initial package might be in a different cachedir */
621 if(dlts == delta_path) {
622 from = _alpm_filecache_find(handle, d->from);
623 } else {
624 /* len = cachedir len + from len + '/' + null */
625 len = strlen(cachedir) + strlen(d->from) + 2;
626 CALLOC(from, len, sizeof(char), RET_ERR(handle, ALPM_ERR_MEMORY, 1));
627 snprintf(from, len, "%s/%s", cachedir, d->from);
629 len = strlen(cachedir) + strlen(d->to) + 2;
630 CALLOC(to, len, sizeof(char), RET_ERR(handle, ALPM_ERR_MEMORY, 1));
631 snprintf(to, len, "%s/%s", cachedir, d->to);
633 /* build the patch command */
634 if(endswith(to, ".gz")) {
635 /* special handling for gzip : we disable timestamp with -n option */
636 snprintf(command, PATH_MAX, "xdelta3 -d -q -R -c -s %s %s | gzip -n > %s", from, delta, to);
637 } else {
638 snprintf(command, PATH_MAX, "xdelta3 -d -q -s %s %s %s", from, delta, to);
641 _alpm_log(handle, ALPM_LOG_DEBUG, "command: %s\n", command);
643 EVENT(trans, ALPM_TRANS_EVT_DELTA_PATCH_START, d->to, d->delta);
645 int retval = system(command);
646 if(retval == 0) {
647 EVENT(trans, ALPM_TRANS_EVT_DELTA_PATCH_DONE, NULL, NULL);
649 /* delete the delta file */
650 unlink(delta);
652 /* Delete the 'from' package but only if it is an intermediate
653 * package. The starting 'from' package should be kept, just
654 * as if deltas were not used. */
655 if(dlts != delta_path) {
656 unlink(from);
659 FREE(from);
660 FREE(to);
661 FREE(delta);
663 if(retval != 0) {
664 /* one delta failed for this package, cancel the remaining ones */
665 EVENT(trans, ALPM_TRANS_EVT_DELTA_PATCH_FAILED, NULL, NULL);
666 handle->pm_errno = ALPM_ERR_DLT_PATCHFAILED;
667 ret = 1;
668 break;
673 return ret;
676 /** Compares the md5sum of a file to the expected value.
678 * If the md5sum does not match, the user is asked whether the file
679 * should be deleted.
681 * @param trans the transaction
682 * @param filename the absolute path of the file to test
683 * @param reason an error code indicating the reason for package invalidity
685 * @return 1 if file was removed, 0 otherwise
687 static int prompt_to_delete(alpm_trans_t *trans, const char *filepath,
688 enum _alpm_errno_t reason)
690 int doremove = 0;
691 QUESTION(trans, ALPM_TRANS_CONV_CORRUPTED_PKG, (char *)filepath,
692 &reason, NULL, &doremove);
693 if(doremove) {
694 unlink(filepath);
696 return doremove;
699 static int validate_deltas(alpm_handle_t *handle, alpm_list_t *deltas,
700 alpm_list_t **data)
702 int errors = 0, ret = 0;
703 alpm_list_t *i;
704 alpm_trans_t *trans = handle->trans;
706 if(!deltas) {
707 return 0;
710 /* Check integrity of deltas */
711 EVENT(trans, ALPM_TRANS_EVT_DELTA_INTEGRITY_START, NULL, NULL);
713 for(i = deltas; i; i = i->next) {
714 alpm_delta_t *d = alpm_list_getdata(i);
715 char *filepath = _alpm_filecache_find(handle, d->delta);
717 ret = _alpm_test_checksum(filepath, d->delta_md5, ALPM_CSUM_MD5);
718 if(ret != 0) {
719 prompt_to_delete(trans, filepath, ALPM_ERR_DLT_INVALID);
720 errors++;
721 *data = alpm_list_add(*data, strdup(d->delta));
723 FREE(filepath);
725 if(errors) {
726 handle->pm_errno = ALPM_ERR_DLT_INVALID;
727 return -1;
729 EVENT(trans, ALPM_TRANS_EVT_DELTA_INTEGRITY_DONE, NULL, NULL);
731 /* Use the deltas to generate the packages */
732 EVENT(trans, ALPM_TRANS_EVT_DELTA_PATCHES_START, NULL, NULL);
733 ret = apply_deltas(handle);
734 EVENT(trans, ALPM_TRANS_EVT_DELTA_PATCHES_DONE, NULL, NULL);
735 return ret;
738 static int download_files(alpm_handle_t *handle, alpm_list_t **deltas)
740 const char *cachedir;
741 alpm_list_t *i, *j;
742 alpm_list_t *files = NULL;
743 int errors = 0;
745 cachedir = _alpm_filecache_setup(handle);
746 handle->trans->state = STATE_DOWNLOADING;
748 /* Total progress - figure out the total download size if required to
749 * pass to the callback. This function is called once, and it is up to the
750 * frontend to compute incremental progress. */
751 if(handle->totaldlcb) {
752 off_t total_size = (off_t)0;
753 /* sum up the download size for each package and store total */
754 for(i = handle->trans->add; i; i = i->next) {
755 alpm_pkg_t *spkg = i->data;
756 total_size += spkg->download_size;
758 handle->totaldlcb(total_size);
761 /* group sync records by repository and download */
762 for(i = handle->dbs_sync; i; i = i->next) {
763 alpm_db_t *current = i->data;
765 for(j = handle->trans->add; j; j = j->next) {
766 alpm_pkg_t *spkg = j->data;
768 if(spkg->origin != PKG_FROM_FILE && current == spkg->origin_data.db) {
769 alpm_list_t *delta_path = spkg->delta_path;
770 if(delta_path) {
771 /* using deltas */
772 alpm_list_t *dlts;
773 for(dlts = delta_path; dlts; dlts = dlts->next) {
774 alpm_delta_t *delta = dlts->data;
775 if(delta->download_size != 0) {
776 struct dload_payload *dpayload;
778 CALLOC(dpayload, 1, sizeof(*dpayload), RET_ERR(handle, ALPM_ERR_MEMORY, -1));
779 STRDUP(dpayload->filename, delta->delta, RET_ERR(handle, ALPM_ERR_MEMORY, -1));
780 dpayload->max_size = delta->download_size;
782 files = alpm_list_add(files, dpayload);
784 /* keep a list of all the delta files for md5sums */
785 *deltas = alpm_list_add(*deltas, delta);
788 } else if(spkg->download_size != 0) {
789 struct dload_payload *payload;
791 ASSERT(spkg->filename != NULL, RET_ERR(handle, ALPM_ERR_PKG_INVALID_NAME, -1));
792 CALLOC(payload, 1, sizeof(*payload), RET_ERR(handle, ALPM_ERR_MEMORY, -1));
793 STRDUP(payload->filename, spkg->filename, RET_ERR(handle, ALPM_ERR_MEMORY, -1));
794 payload->max_size = alpm_pkg_get_size(spkg);
796 files = alpm_list_add(files, payload);
802 if(files) {
803 EVENT(handle->trans, ALPM_TRANS_EVT_RETRIEVE_START, current->treename, NULL);
804 for(j = files; j; j = j->next) {
805 struct dload_payload *payload = j->data;
806 alpm_list_t *server;
807 int ret = -1;
808 for(server = current->servers; server; server = server->next) {
809 const char *server_url = server->data;
810 size_t len;
812 /* print server + filename into a buffer */
813 len = strlen(server_url) + strlen(payload->filename) + 2;
814 CALLOC(payload->fileurl, len, sizeof(char), RET_ERR(handle, ALPM_ERR_MEMORY, -1));
815 snprintf(payload->fileurl, len, "%s/%s", server_url, payload->filename);
816 payload->handle = handle;
817 payload->allow_resume = 1;
819 ret = _alpm_download(payload, cachedir, NULL);
820 if(ret != -1) {
821 break;
824 if(ret == -1) {
825 errors++;
826 _alpm_log(handle, ALPM_LOG_WARNING, _("failed to retrieve some files from %s\n"),
827 current->treename);
831 alpm_list_free_inner(files, (alpm_list_fn_free)_alpm_dload_payload_free);
832 alpm_list_free(files);
833 files = NULL;
837 for(j = handle->trans->add; j; j = j->next) {
838 alpm_pkg_t *pkg = j->data;
839 pkg->infolevel &= ~INFRQ_DSIZE;
840 pkg->download_size = 0;
843 /* clear out value to let callback know we are done */
844 if(handle->totaldlcb) {
845 handle->totaldlcb(0);
848 return errors;
851 int _alpm_sync_commit(alpm_handle_t *handle, alpm_list_t **data)
853 alpm_list_t *i;
854 alpm_list_t *deltas = NULL;
855 size_t numtargs, current = 0, replaces = 0;
856 int errors;
857 alpm_trans_t *trans = handle->trans;
859 if(download_files(handle, &deltas)) {
860 alpm_list_free(deltas);
861 return -1;
864 if(validate_deltas(handle, deltas, data)) {
865 alpm_list_free(deltas);
866 return -1;
868 alpm_list_free(deltas);
870 /* Check integrity of packages */
871 numtargs = alpm_list_count(trans->add);
872 EVENT(trans, ALPM_TRANS_EVT_INTEGRITY_START, NULL, NULL);
874 errors = 0;
876 for(i = trans->add; i; i = i->next, current++) {
877 alpm_pkg_t *spkg = i->data;
878 int percent = (current * 100) / numtargs;
879 const char *filename;
880 char *filepath;
881 alpm_siglevel_t level;
883 PROGRESS(trans, ALPM_TRANS_PROGRESS_INTEGRITY_START, "", percent,
884 numtargs, current);
885 if(spkg->origin == PKG_FROM_FILE) {
886 continue; /* pkg_load() has been already called, this package is valid */
889 filename = alpm_pkg_get_filename(spkg);
890 filepath = _alpm_filecache_find(handle, filename);
891 alpm_db_t *sdb = alpm_pkg_get_db(spkg);
892 level = alpm_db_get_siglevel(sdb);
894 /* load the package file and replace pkgcache entry with it in the target list */
895 /* TODO: alpm_pkg_get_db() will not work on this target anymore */
896 _alpm_log(handle, ALPM_LOG_DEBUG,
897 "replacing pkgcache entry with package file for target %s\n",
898 spkg->name);
899 alpm_pkg_t *pkgfile =_alpm_pkg_load_internal(handle, filepath, 1, spkg->md5sum,
900 spkg->sha256sum, spkg->base64_sig, level);
901 if(!pkgfile) {
902 prompt_to_delete(trans, filepath, handle->pm_errno);
903 errors++;
904 *data = alpm_list_add(*data, strdup(filename));
905 FREE(filepath);
906 continue;
908 FREE(filepath);
909 pkgfile->reason = spkg->reason; /* copy over install reason */
910 i->data = pkgfile;
911 _alpm_pkg_free_trans(spkg); /* spkg has been removed from the target list */
914 PROGRESS(trans, ALPM_TRANS_PROGRESS_INTEGRITY_START, "", 100,
915 numtargs, current);
916 EVENT(trans, ALPM_TRANS_EVT_INTEGRITY_DONE, NULL, NULL);
919 if(errors) {
920 if(!handle->pm_errno) {
921 RET_ERR(handle, ALPM_ERR_PKG_INVALID, -1);
923 return -1;
926 if(trans->flags & ALPM_TRANS_FLAG_DOWNLOADONLY) {
927 return 0;
930 trans->state = STATE_COMMITING;
932 replaces = alpm_list_count(trans->remove);
934 /* fileconflict check */
935 if(!(trans->flags & ALPM_TRANS_FLAG_FORCE)) {
936 EVENT(trans, ALPM_TRANS_EVT_FILECONFLICTS_START, NULL, NULL);
938 _alpm_log(handle, ALPM_LOG_DEBUG, "looking for file conflicts\n");
939 alpm_list_t *conflict = _alpm_db_find_fileconflicts(handle,
940 trans->add, trans->remove);
941 if(conflict) {
942 if(data) {
943 *data = conflict;
944 } else {
945 alpm_list_free_inner(conflict, (alpm_list_fn_free)_alpm_fileconflict_free);
946 alpm_list_free(conflict);
948 RET_ERR(handle, ALPM_ERR_FILE_CONFLICTS, -1);
951 EVENT(trans, ALPM_TRANS_EVT_FILECONFLICTS_DONE, NULL, NULL);
954 /* check available disk space */
955 if(handle->checkspace) {
956 EVENT(trans, ALPM_TRANS_EVT_DISKSPACE_START, NULL, NULL);
958 _alpm_log(handle, ALPM_LOG_DEBUG, "checking available disk space\n");
959 if(_alpm_check_diskspace(handle) == -1) {
960 _alpm_log(handle, ALPM_LOG_ERROR, "%s\n", _("not enough free disk space"));
961 return -1;
964 EVENT(trans, ALPM_TRANS_EVT_DISKSPACE_DONE, NULL, NULL);
967 /* remove conflicting and to-be-replaced packages */
968 if(replaces) {
969 _alpm_log(handle, ALPM_LOG_DEBUG, "removing conflicting and to-be-replaced packages\n");
970 /* we want the frontend to be aware of commit details */
971 if(_alpm_remove_packages(handle) == -1) {
972 _alpm_log(handle, ALPM_LOG_ERROR, _("could not commit removal transaction\n"));
973 return -1;
977 /* install targets */
978 _alpm_log(handle, ALPM_LOG_DEBUG, "installing packages\n");
979 if(_alpm_upgrade_packages(handle) == -1) {
980 _alpm_log(handle, ALPM_LOG_ERROR, _("could not commit transaction\n"));
981 return -1;
984 return 0;
987 /* vim: set ts=2 sw=2 noet: */