1 /*
2  *  sync.c
3  *
4  *  Copyright (c) 2006-2018 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>
9  *
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.
14  *
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.
19  *
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/>.
22  */
23 
24 #include <sys/types.h> /* off_t */
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdint.h> /* intmax_t */
29 #include <unistd.h>
30 #include <limits.h>
31 
32 /* libalpm */
33 #include "sync.h"
34 #include "alpm_list.h"
35 #include "log.h"
36 #include "package.h"
37 #include "db.h"
38 #include "deps.h"
39 #include "conflict.h"
40 #include "trans.h"
41 #include "add.h"
42 #include "util.h"
43 #include "handle.h"
44 #include "alpm.h"
45 #include "dload.h"
46 #include "delta.h"
47 #include "remove.h"
48 #include "diskspace.h"
49 #include "signing.h"
50 
51 /** Check for new version of pkg in sync repos
52  * (only the first occurrence is considered in sync)
53  */
alpm_sync_newversion(alpm_pkg_t * pkg,alpm_list_t * dbs_sync)54 alpm_pkg_t SYMEXPORT *alpm_sync_newversion(alpm_pkg_t *pkg, alpm_list_t *dbs_sync)
55 {
56 	alpm_list_t *i;
57 	alpm_pkg_t *spkg = NULL;
58 
59 	ASSERT(pkg != NULL, return NULL);
60 	pkg->handle->pm_errno = ALPM_ERR_OK;
61 
62 	for(i = dbs_sync; !spkg && i; i = i->next) {
63 		alpm_db_t *db = i->data;
64 		if(!(db->usage & ALPM_DB_USAGE_SEARCH)) {
65 			continue;
66 		}
67 
68 		spkg = _alpm_db_get_pkgfromcache(db, pkg->name);
69 	}
70 
71 	if(spkg == NULL) {
72 		_alpm_log(pkg->handle, ALPM_LOG_DEBUG, "'%s' not found in sync db => no upgrade\n",
73 				pkg->name);
74 		return NULL;
75 	}
76 
77 	/* compare versions and see if spkg is an upgrade */
78 	if(_alpm_pkg_compare_versions(spkg, pkg) > 0) {
79 		_alpm_log(pkg->handle, ALPM_LOG_DEBUG, "new version of '%s' found (%s => %s)\n",
80 					pkg->name, pkg->version, spkg->version);
81 		return spkg;
82 	}
83 	/* spkg is not an upgrade */
84 	return NULL;
85 }
86 
check_literal(alpm_handle_t * handle,alpm_pkg_t * lpkg,alpm_pkg_t * spkg,int enable_downgrade)87 static int check_literal(alpm_handle_t *handle, alpm_pkg_t *lpkg,
88 		alpm_pkg_t *spkg, int enable_downgrade)
89 {
90 	/* 1. literal was found in sdb */
91 	int cmp = _alpm_pkg_compare_versions(spkg, lpkg);
92 	if(cmp > 0) {
93 		_alpm_log(handle, ALPM_LOG_DEBUG, "new version of '%s' found (%s => %s)\n",
94 				lpkg->name, lpkg->version, spkg->version);
95 		/* check IgnorePkg/IgnoreGroup */
96 		if(alpm_pkg_should_ignore(handle, spkg)
97 				|| alpm_pkg_should_ignore(handle, lpkg)) {
98 			_alpm_log(handle, ALPM_LOG_WARNING, _("%s: ignoring package upgrade (%s => %s)\n"),
99 					lpkg->name, lpkg->version, spkg->version);
100 		} else {
101 			_alpm_log(handle, ALPM_LOG_DEBUG, "adding package %s-%s to the transaction targets\n",
102 					spkg->name, spkg->version);
103 			return 1;
104 		}
105 	} else if(cmp < 0) {
106 		if(enable_downgrade) {
107 			/* check IgnorePkg/IgnoreGroup */
108 			if(alpm_pkg_should_ignore(handle, spkg)
109 					|| alpm_pkg_should_ignore(handle, lpkg)) {
110 				_alpm_log(handle, ALPM_LOG_WARNING, _("%s: ignoring package downgrade (%s => %s)\n"),
111 						lpkg->name, lpkg->version, spkg->version);
112 			} else {
113 				_alpm_log(handle, ALPM_LOG_WARNING, _("%s: downgrading from version %s to version %s\n"),
114 						lpkg->name, lpkg->version, spkg->version);
115 				return 1;
116 			}
117 		} else {
118 			alpm_db_t *sdb = alpm_pkg_get_db(spkg);
119 			_alpm_log(handle, ALPM_LOG_WARNING, _("%s: local (%s) is newer than %s (%s)\n"),
120 					lpkg->name, lpkg->version, sdb->treename, spkg->version);
121 		}
122 	}
123 	return 0;
124 }
125 
check_replacers(alpm_handle_t * handle,alpm_pkg_t * lpkg,alpm_db_t * sdb)126 static alpm_list_t *check_replacers(alpm_handle_t *handle, alpm_pkg_t *lpkg,
127 		alpm_db_t *sdb)
128 {
129 	/* 2. search for replacers in sdb */
130 	alpm_list_t *replacers = NULL;
131 	alpm_list_t *k;
132 	_alpm_log(handle, ALPM_LOG_DEBUG,
133 			"searching for replacements for %s in %s\n",
134 			lpkg->name, sdb->treename);
135 	for(k = _alpm_db_get_pkgcache(sdb); k; k = k->next) {
136 		int found = 0;
137 		alpm_pkg_t *spkg = k->data;
138 		alpm_list_t *l;
139 		for(l = alpm_pkg_get_replaces(spkg); l; l = l->next) {
140 			alpm_depend_t *replace = l->data;
141 			/* we only want to consider literal matches at this point. */
142 			if(_alpm_depcmp_literal(lpkg, replace)) {
143 				found = 1;
144 				break;
145 			}
146 		}
147 		if(found) {
148 			alpm_question_replace_t question = {
149 				.type = ALPM_QUESTION_REPLACE_PKG,
150 				.replace = 0,
151 				.oldpkg = lpkg,
152 				.newpkg = spkg,
153 				.newdb = sdb
154 			};
155 			alpm_pkg_t *tpkg;
156 			/* check IgnorePkg/IgnoreGroup */
157 			if(alpm_pkg_should_ignore(handle, spkg)
158 					|| alpm_pkg_should_ignore(handle, lpkg)) {
159 				_alpm_log(handle, ALPM_LOG_WARNING,
160 						_("ignoring package replacement (%s-%s => %s-%s)\n"),
161 						lpkg->name, lpkg->version, spkg->name, spkg->version);
162 				continue;
163 			}
164 
165 			QUESTION(handle, &question);
166 			if(!question.replace) {
167 				continue;
168 			}
169 
170 			/* If spkg is already in the target list, we append lpkg to spkg's
171 			 * removes list */
172 			tpkg = alpm_pkg_find(handle->trans->add, spkg->name);
173 			if(tpkg) {
174 				/* sanity check, multiple repos can contain spkg->name */
175 				if(tpkg->origin_data.db != sdb) {
176 					_alpm_log(handle, ALPM_LOG_WARNING, _("cannot replace %s by %s\n"),
177 							lpkg->name, spkg->name);
178 					continue;
179 				}
180 				_alpm_log(handle, ALPM_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) == ALPM_PKG_REASON_EXPLICIT) {
185 					tpkg->reason = ALPM_PKG_REASON_EXPLICIT;
186 				}
187 			} else {
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(handle, ALPM_LOG_DEBUG,
193 						"adding package %s-%s to the transaction targets\n",
194 						spkg->name, spkg->version);
195 				replacers = alpm_list_add(replacers, spkg);
196 			}
197 		}
198 	}
199 	return replacers;
200 }
201 
202 /** Search for packages to upgrade and add them to the transaction. */
alpm_sync_sysupgrade(alpm_handle_t * handle,int enable_downgrade)203 int SYMEXPORT alpm_sync_sysupgrade(alpm_handle_t *handle, int enable_downgrade)
204 {
205 	alpm_list_t *i, *j;
206 	alpm_trans_t *trans;
207 
208 	CHECK_HANDLE(handle, return -1);
209 	trans = handle->trans;
210 	ASSERT(trans != NULL, RET_ERR(handle, ALPM_ERR_TRANS_NULL, -1));
211 	ASSERT(trans->state == STATE_INITIALIZED, RET_ERR(handle, ALPM_ERR_TRANS_NOT_INITIALIZED, -1));
212 
213 	_alpm_log(handle, ALPM_LOG_DEBUG, "checking for package upgrades\n");
214 	for(i = _alpm_db_get_pkgcache(handle->db_local); i; i = i->next) {
215 		alpm_pkg_t *lpkg = i->data;
216 
217 		if(alpm_pkg_find(trans->remove, lpkg->name)) {
218 			_alpm_log(handle, ALPM_LOG_DEBUG, "%s is marked for removal -- skipping\n", lpkg->name);
219 			continue;
220 		}
221 
222 		if(alpm_pkg_find(trans->add, lpkg->name)) {
223 			_alpm_log(handle, ALPM_LOG_DEBUG, "%s is already in the target list -- skipping\n", lpkg->name);
224 			continue;
225 		}
226 
227 		/* Search for replacers then literal (if no replacer) in each sync database. */
228 		for(j = handle->dbs_sync; j; j = j->next) {
229 			alpm_db_t *sdb = j->data;
230 			alpm_list_t *replacers;
231 
232 			if(!(sdb->usage & ALPM_DB_USAGE_UPGRADE)) {
233 				continue;
234 			}
235 
236 			/* Check sdb */
237 			replacers = check_replacers(handle, lpkg, sdb);
238 			if(replacers) {
239 				trans->add = alpm_list_join(trans->add, replacers);
240 				/* jump to next local package */
241 				break;
242 			} else {
243 				alpm_pkg_t *spkg = _alpm_db_get_pkgfromcache(sdb, lpkg->name);
244 				if(spkg) {
245 					if(check_literal(handle, lpkg, spkg, enable_downgrade)) {
246 						trans->add = alpm_list_add(trans->add, spkg);
247 					}
248 					/* jump to next local package */
249 					break;
250 				}
251 			}
252 		}
253 	}
254 
255 	return 0;
256 }
257 
258 /** Find group members across a list of databases.
259  * If a member exists in several databases, only the first database is used.
260  * IgnorePkg is also handled.
261  * @param dbs the list of alpm_db_t *
262  * @param name the name of the group
263  * @return the list of alpm_pkg_t * (caller is responsible for alpm_list_free)
264  */
alpm_find_group_pkgs(alpm_list_t * dbs,const char * name)265 alpm_list_t SYMEXPORT *alpm_find_group_pkgs(alpm_list_t *dbs,
266 		const char *name)
267 {
268 	alpm_list_t *i, *j, *pkgs = NULL, *ignorelist = NULL;
269 
270 	for(i = dbs; i; i = i->next) {
271 		alpm_db_t *db = i->data;
272 		alpm_group_t *grp = alpm_db_get_group(db, name);
273 
274 		if(!grp) {
275 			continue;
276 		}
277 
278 		for(j = grp->packages; j; j = j->next) {
279 			alpm_pkg_t *pkg = j->data;
280 
281 			if(alpm_pkg_find(ignorelist, pkg->name)) {
282 				continue;
283 			}
284 			if(alpm_pkg_should_ignore(db->handle, pkg)) {
285 				alpm_question_install_ignorepkg_t question = {
286 					.type = ALPM_QUESTION_INSTALL_IGNOREPKG,
287 					.install = 0,
288 					.pkg = pkg
289 				};
290 				ignorelist = alpm_list_add(ignorelist, pkg);
291 				QUESTION(db->handle, &question);
292 				if(!question.install) {
293 					continue;
294 				}
295 			}
296 			if(!alpm_pkg_find(pkgs, pkg->name)) {
297 				pkgs = alpm_list_add(pkgs, pkg);
298 			}
299 		}
300 	}
301 	alpm_list_free(ignorelist);
302 	return pkgs;
303 }
304 
305 /** Compute the size of the files that will be downloaded to install a
306  * package.
307  * @param newpkg the new package to upgrade to
308  */
compute_download_size(alpm_pkg_t * newpkg)309 static int compute_download_size(alpm_pkg_t *newpkg)
310 {
311 	const char *fname;
312 	char *fpath, *fnamepart = NULL;
313 	off_t size = 0;
314 	alpm_handle_t *handle = newpkg->handle;
315 	int ret = 0;
316 
317 	if(newpkg->origin != ALPM_PKG_FROM_SYNCDB) {
318 		newpkg->infolevel |= INFRQ_DSIZE;
319 		newpkg->download_size = 0;
320 		return 0;
321 	}
322 
323 	ASSERT(newpkg->filename != NULL, RET_ERR(handle, ALPM_ERR_PKG_INVALID_NAME, -1));
324 	fname = newpkg->filename;
325 	fpath = _alpm_filecache_find(handle, fname);
326 
327 	/* downloaded file exists, so there's nothing to grab */
328 	if(fpath) {
329 		size = 0;
330 		goto finish;
331 	}
332 
333 	CALLOC(fnamepart, strlen(fname) + 6, sizeof(char), return -1);
334 	sprintf(fnamepart, "%s.part", fname);
335 	fpath = _alpm_filecache_find(handle, fnamepart);
336 	if(fpath) {
337 		struct stat st;
338 		if(stat(fpath, &st) == 0) {
339 			/* subtract the size of the .part file */
340 			_alpm_log(handle, ALPM_LOG_DEBUG, "using (package - .part) size\n");
341 			size = newpkg->size - st.st_size;
342 			size = size < 0 ? 0 : size;
343 		}
344 
345 		/* tell the caller that we have a partial */
346 		ret = 1;
347 	} else if(handle->deltaratio > 0.0) {
348 		off_t dltsize;
349 
350 		dltsize = _alpm_shortest_delta_path(handle, newpkg->deltas,
351 				newpkg->filename, &newpkg->delta_path);
352 
353 		if(newpkg->delta_path && (dltsize < newpkg->size * handle->deltaratio)) {
354 			_alpm_log(handle, ALPM_LOG_DEBUG, "using delta size\n");
355 			size = dltsize;
356 		} else {
357 			_alpm_log(handle, ALPM_LOG_DEBUG, "using package size\n");
358 			size = newpkg->size;
359 			alpm_list_free(newpkg->delta_path);
360 			newpkg->delta_path = NULL;
361 		}
362 	} else {
363 		size = newpkg->size;
364 	}
365 
366 finish:
367 	_alpm_log(handle, ALPM_LOG_DEBUG, "setting download size %jd for pkg %s\n",
368 			(intmax_t)size, newpkg->name);
369 
370 	newpkg->infolevel |= INFRQ_DSIZE;
371 	newpkg->download_size = size;
372 
373 	FREE(fpath);
374 	FREE(fnamepart);
375 
376 	return ret;
377 }
378 
_alpm_sync_prepare(alpm_handle_t * handle,alpm_list_t ** data)379 int _alpm_sync_prepare(alpm_handle_t *handle, alpm_list_t **data)
380 {
381 	alpm_list_t *i, *j;
382 	alpm_list_t *deps = NULL;
383 	alpm_list_t *unresolvable = NULL;
384 	int from_sync = 0;
385 	int ret = 0;
386 	alpm_trans_t *trans = handle->trans;
387 	alpm_event_t event;
388 
389 	if(data) {
390 		*data = NULL;
391 	}
392 
393 	for(i = trans->add; i; i = i->next) {
394 		alpm_pkg_t *spkg = i->data;
395 		if (spkg->origin == ALPM_PKG_FROM_SYNCDB){
396 			from_sync = 1;
397 			break;
398 		}
399 	}
400 
401 	/* ensure all sync database are valid if we will be using them */
402 	for(i = handle->dbs_sync; i; i = i->next) {
403 		const alpm_db_t *db = i->data;
404 		if(db->status & DB_STATUS_INVALID) {
405 			RET_ERR(handle, ALPM_ERR_DB_INVALID, -1);
406 		}
407 		/* missing databases are not allowed if we have sync targets */
408 		if(from_sync && db->status & DB_STATUS_MISSING) {
409 			RET_ERR(handle, ALPM_ERR_DB_NOT_FOUND, -1);
410 		}
411 	}
412 
413 	if(!(trans->flags & ALPM_TRANS_FLAG_NODEPS)) {
414 		alpm_list_t *resolved = NULL;
415 		alpm_list_t *remove = alpm_list_copy(trans->remove);
416 		alpm_list_t *localpkgs;
417 
418 		/* Build up list by repeatedly resolving each transaction package */
419 		/* Resolve targets dependencies */
420 		event.type = ALPM_EVENT_RESOLVEDEPS_START;
421 		EVENT(handle, &event);
422 		_alpm_log(handle, ALPM_LOG_DEBUG, "resolving target's dependencies\n");
423 
424 		/* build remove list for resolvedeps */
425 		for(i = trans->add; i; i = i->next) {
426 			alpm_pkg_t *spkg = i->data;
427 			for(j = spkg->removes; j; j = j->next) {
428 				remove = alpm_list_add(remove, j->data);
429 			}
430 		}
431 
432 		/* Compute the fake local database for resolvedeps (partial fix for the
433 		 * phonon/qt issue) */
434 		localpkgs = alpm_list_diff(_alpm_db_get_pkgcache(handle->db_local),
435 				trans->add, _alpm_pkg_cmp);
436 
437 		/* Resolve packages in the transaction one at a time, in addition
438 		   building up a list of packages which could not be resolved. */
439 		for(i = trans->add; i; i = i->next) {
440 			alpm_pkg_t *pkg = i->data;
441 			if(_alpm_resolvedeps(handle, localpkgs, pkg, trans->add,
442 						&resolved, remove, data) == -1) {
443 				unresolvable = alpm_list_add(unresolvable, pkg);
444 			}
445 			/* Else, [resolved] now additionally contains [pkg] and all of its
446 			   dependencies not already on the list */
447 		}
448 		alpm_list_free(localpkgs);
449 		alpm_list_free(remove);
450 
451 		/* If there were unresolvable top-level packages, prompt the user to
452 		   see if they'd like to ignore them rather than failing the sync */
453 		if(unresolvable != NULL) {
454 			alpm_question_remove_pkgs_t question = {
455 				.type = ALPM_QUESTION_REMOVE_PKGS,
456 				.skip = 0,
457 				.packages = unresolvable
458 			};
459 			QUESTION(handle, &question);
460 			if(question.skip) {
461 				/* User wants to remove the unresolvable packages from the
462 				   transaction. The packages will be removed from the actual
463 				   transaction when the transaction packages are replaced with a
464 				   dependency-reordered list below */
465 				handle->pm_errno = ALPM_ERR_OK;
466 				if(data) {
467 					alpm_list_free_inner(*data,
468 							(alpm_list_fn_free)alpm_depmissing_free);
469 					alpm_list_free(*data);
470 					*data = NULL;
471 				}
472 			} else {
473 				/* pm_errno was set by resolvedeps, callback may have overwrote it */
474 				handle->pm_errno = ALPM_ERR_UNSATISFIED_DEPS;
475 				alpm_list_free(resolved);
476 				alpm_list_free(unresolvable);
477 				ret = -1;
478 				goto cleanup;
479 			}
480 		}
481 
482 		/* Set DEPEND reason for pulled packages */
483 		for(i = resolved; i; i = i->next) {
484 			alpm_pkg_t *pkg = i->data;
485 			if(!alpm_pkg_find(trans->add, pkg->name)) {
486 				pkg->reason = ALPM_PKG_REASON_DEPEND;
487 			}
488 		}
489 
490 		/* Unresolvable packages will be removed from the target list; set these
491 		 * aside in the transaction as a list we won't operate on. If we free them
492 		 * before the end of the transaction, we may kill pointers the frontend
493 		 * holds to package objects. */
494 		trans->unresolvable = unresolvable;
495 
496 		alpm_list_free(trans->add);
497 		trans->add = resolved;
498 
499 		event.type = ALPM_EVENT_RESOLVEDEPS_DONE;
500 		EVENT(handle, &event);
501 	}
502 
503 	if(!(trans->flags & ALPM_TRANS_FLAG_NOCONFLICTS)) {
504 		/* check for inter-conflicts and whatnot */
505 		event.type = ALPM_EVENT_INTERCONFLICTS_START;
506 		EVENT(handle, &event);
507 
508 		_alpm_log(handle, ALPM_LOG_DEBUG, "looking for conflicts\n");
509 
510 		/* 1. check for conflicts in the target list */
511 		_alpm_log(handle, ALPM_LOG_DEBUG, "check targets vs targets\n");
512 		deps = _alpm_innerconflicts(handle, trans->add);
513 
514 		for(i = deps; i; i = i->next) {
515 			alpm_conflict_t *conflict = i->data;
516 			alpm_pkg_t *rsync, *sync, *sync1, *sync2;
517 
518 			/* have we already removed one of the conflicting targets? */
519 			sync1 = alpm_pkg_find(trans->add, conflict->package1);
520 			sync2 = alpm_pkg_find(trans->add, conflict->package2);
521 			if(!sync1 || !sync2) {
522 				continue;
523 			}
524 
525 			_alpm_log(handle, ALPM_LOG_DEBUG, "conflicting packages in the sync list: '%s' <-> '%s'\n",
526 					conflict->package1, conflict->package2);
527 
528 			/* if sync1 provides sync2, we remove sync2 from the targets, and vice versa */
529 			alpm_depend_t *dep1 = alpm_dep_from_string(conflict->package1);
530 			alpm_depend_t *dep2 = alpm_dep_from_string(conflict->package2);
531 			if(_alpm_depcmp(sync1, dep2)) {
532 				rsync = sync2;
533 				sync = sync1;
534 			} else if(_alpm_depcmp(sync2, dep1)) {
535 				rsync = sync1;
536 				sync = sync2;
537 			} else {
538 				_alpm_log(handle, ALPM_LOG_ERROR, _("unresolvable package conflicts detected\n"));
539 				handle->pm_errno = ALPM_ERR_CONFLICTING_DEPS;
540 				ret = -1;
541 				if(data) {
542 					alpm_conflict_t *newconflict = _alpm_conflict_dup(conflict);
543 					if(newconflict) {
544 						*data = alpm_list_add(*data, newconflict);
545 					}
546 				}
547 				alpm_list_free_inner(deps, (alpm_list_fn_free)alpm_conflict_free);
548 				alpm_list_free(deps);
549 				alpm_dep_free(dep1);
550 				alpm_dep_free(dep2);
551 				goto cleanup;
552 			}
553 			alpm_dep_free(dep1);
554 			alpm_dep_free(dep2);
555 
556 			/* Prints warning */
557 			_alpm_log(handle, ALPM_LOG_WARNING,
558 					_("removing '%s' from target list because it conflicts with '%s'\n"),
559 					rsync->name, sync->name);
560 			trans->add = alpm_list_remove(trans->add, rsync, _alpm_pkg_cmp, NULL);
561 			/* rsync is not a transaction target anymore */
562 			trans->unresolvable = alpm_list_add(trans->unresolvable, rsync);
563 		}
564 
565 		alpm_list_free_inner(deps, (alpm_list_fn_free)alpm_conflict_free);
566 		alpm_list_free(deps);
567 		deps = NULL;
568 
569 		/* 2. we check for target vs db conflicts (and resolve)*/
570 		_alpm_log(handle, ALPM_LOG_DEBUG, "check targets vs db and db vs targets\n");
571 		deps = _alpm_outerconflicts(handle->db_local, trans->add);
572 
573 		for(i = deps; i; i = i->next) {
574 			alpm_question_conflict_t question = {
575 				.type = ALPM_QUESTION_CONFLICT_PKG,
576 				.remove = 0,
577 				.conflict = i->data
578 			};
579 			alpm_conflict_t *conflict = i->data;
580 			int found = 0;
581 
582 			/* if conflict->package2 (the local package) is not elected for removal,
583 			   we ask the user */
584 			if(alpm_pkg_find(trans->remove, conflict->package2)) {
585 				found = 1;
586 			}
587 			for(j = trans->add; j && !found; j = j->next) {
588 				alpm_pkg_t *spkg = j->data;
589 				if(alpm_pkg_find(spkg->removes, conflict->package2)) {
590 					found = 1;
591 				}
592 			}
593 			if(found) {
594 				continue;
595 			}
596 
597 			_alpm_log(handle, ALPM_LOG_DEBUG, "package '%s' conflicts with '%s'\n",
598 					conflict->package1, conflict->package2);
599 
600 			QUESTION(handle, &question);
601 			if(question.remove) {
602 				/* append to the removes list */
603 				alpm_pkg_t *sync = alpm_pkg_find(trans->add, conflict->package1);
604 				alpm_pkg_t *local = _alpm_db_get_pkgfromcache(handle->db_local, conflict->package2);
605 				_alpm_log(handle, ALPM_LOG_DEBUG, "electing '%s' for removal\n", conflict->package2);
606 				sync->removes = alpm_list_add(sync->removes, local);
607 			} else { /* abort */
608 				_alpm_log(handle, ALPM_LOG_ERROR, _("unresolvable package conflicts detected\n"));
609 				handle->pm_errno = ALPM_ERR_CONFLICTING_DEPS;
610 				ret = -1;
611 				if(data) {
612 					alpm_conflict_t *newconflict = _alpm_conflict_dup(conflict);
613 					if(newconflict) {
614 						*data = alpm_list_add(*data, newconflict);
615 					}
616 				}
617 				alpm_list_free_inner(deps, (alpm_list_fn_free)alpm_conflict_free);
618 				alpm_list_free(deps);
619 				goto cleanup;
620 			}
621 		}
622 		event.type = ALPM_EVENT_INTERCONFLICTS_DONE;
623 		EVENT(handle, &event);
624 		alpm_list_free_inner(deps, (alpm_list_fn_free)alpm_conflict_free);
625 		alpm_list_free(deps);
626 	}
627 
628 	/* Build trans->remove list */
629 	for(i = trans->add; i; i = i->next) {
630 		alpm_pkg_t *spkg = i->data;
631 		for(j = spkg->removes; j; j = j->next) {
632 			alpm_pkg_t *rpkg = j->data;
633 			if(!alpm_pkg_find(trans->remove, rpkg->name)) {
634 				alpm_pkg_t *copy;
635 				_alpm_log(handle, ALPM_LOG_DEBUG, "adding '%s' to remove list\n", rpkg->name);
636 				if(_alpm_pkg_dup(rpkg, &copy) == -1) {
637 					return -1;
638 				}
639 				trans->remove = alpm_list_add(trans->remove, copy);
640 			}
641 		}
642 	}
643 
644 	if(!(trans->flags & ALPM_TRANS_FLAG_NODEPS)) {
645 		_alpm_log(handle, ALPM_LOG_DEBUG, "checking dependencies\n");
646 		deps = alpm_checkdeps(handle, _alpm_db_get_pkgcache(handle->db_local),
647 				trans->remove, trans->add, 1);
648 		if(deps) {
649 			handle->pm_errno = ALPM_ERR_UNSATISFIED_DEPS;
650 			ret = -1;
651 			if(data) {
652 				*data = deps;
653 			} else {
654 				alpm_list_free_inner(deps,
655 						(alpm_list_fn_free)alpm_depmissing_free);
656 				alpm_list_free(deps);
657 			}
658 			goto cleanup;
659 		}
660 	}
661 	for(i = trans->add; i; i = i->next) {
662 		/* update download size field */
663 		alpm_pkg_t *spkg = i->data;
664 		alpm_pkg_t *lpkg = alpm_db_get_pkg(handle->db_local, spkg->name);
665 		if(compute_download_size(spkg) < 0) {
666 			ret = -1;
667 			goto cleanup;
668 		}
669 		if(lpkg && _alpm_pkg_dup(lpkg, &spkg->oldpkg) != 0) {
670 			ret = -1;
671 			goto cleanup;
672 		}
673 	}
674 
675 cleanup:
676 	return ret;
677 }
678 
679 /** Returns the size of the files that will be downloaded to install a
680  * package.
681  * @param newpkg the new package to upgrade to
682  * @return the size of the download
683  */
alpm_pkg_download_size(alpm_pkg_t * newpkg)684 off_t SYMEXPORT alpm_pkg_download_size(alpm_pkg_t *newpkg)
685 {
686 	if(!(newpkg->infolevel & INFRQ_DSIZE)) {
687 		compute_download_size(newpkg);
688 	}
689 	return newpkg->download_size;
690 }
691 
endswith(const char * filename,const char * extension)692 static int endswith(const char *filename, const char *extension)
693 {
694 	const char *s = filename + strlen(filename) - strlen(extension);
695 	return strcmp(s, extension) == 0;
696 }
697 
698 /** Applies delta files to create an upgraded package file.
699  *
700  * All intermediate files are deleted, leaving only the starting and
701  * ending package files.
702  *
703  * @param handle the context handle
704  *
705  * @return 0 if all delta files were able to be applied, 1 otherwise.
706  */
apply_deltas(alpm_handle_t * handle)707 static int apply_deltas(alpm_handle_t *handle)
708 {
709 	alpm_list_t *i;
710 	size_t deltas_found = 0;
711 	int ret = 0;
712 	const char *cachedir = _alpm_filecache_setup(handle);
713 	alpm_trans_t *trans = handle->trans;
714 	alpm_event_delta_patch_t event;
715 
716 	for(i = trans->add; i; i = i->next) {
717 		alpm_pkg_t *spkg = i->data;
718 		alpm_list_t *delta_path = spkg->delta_path;
719 		alpm_list_t *dlts = NULL;
720 
721 		if(!delta_path) {
722 			continue;
723 		}
724 
725 		if(!deltas_found) {
726 			/* only show this if we actually have deltas to apply, and it is before
727 			 * the very first one */
728 			event.type = ALPM_EVENT_DELTA_PATCHES_START;
729 			EVENT(handle, &event);
730 			deltas_found = 1;
731 		}
732 
733 		for(dlts = delta_path; dlts; dlts = dlts->next) {
734 			alpm_delta_t *d = dlts->data;
735 			char *delta, *from, *to;
736 			char command[PATH_MAX];
737 			size_t len = 0;
738 
739 			delta = _alpm_filecache_find(handle, d->delta);
740 			/* the initial package might be in a different cachedir */
741 			if(dlts == delta_path) {
742 				from = _alpm_filecache_find(handle, d->from);
743 			} else {
744 				/* len = cachedir len + from len + '/' + null */
745 				len = strlen(cachedir) + strlen(d->from) + 2;
746 				MALLOC(from, len, free(delta); RET_ERR(handle, ALPM_ERR_MEMORY, 1));
747 				snprintf(from, len, "%s/%s", cachedir, d->from);
748 			}
749 			len = strlen(cachedir) + strlen(d->to) + 2;
750 			MALLOC(to, len, free(delta); free(from); RET_ERR(handle, ALPM_ERR_MEMORY, 1));
751 			snprintf(to, len, "%s/%s", cachedir, d->to);
752 
753 			/* build the patch command */
754 			if(endswith(to, ".gz")) {
755 				/* special handling for gzip : we disable timestamp with -n option */
756 				snprintf(command, PATH_MAX, "xdelta3 -d -q -R -c -s %s %s | gzip -n > %s", from, delta, to);
757 			} else {
758 				snprintf(command, PATH_MAX, "xdelta3 -d -q -s %s %s %s", from, delta, to);
759 			}
760 
761 			_alpm_log(handle, ALPM_LOG_DEBUG, "command: %s\n", command);
762 
763 			event.type = ALPM_EVENT_DELTA_PATCH_START;
764 			event.delta = d;
765 			EVENT(handle, &event);
766 
767 			int retval = system(command);
768 			if(retval == 0) {
769 				event.type = ALPM_EVENT_DELTA_PATCH_DONE;
770 				EVENT(handle, &event);
771 
772 				/* delete the delta file */
773 				unlink(delta);
774 
775 				/* Delete the 'from' package but only if it is an intermediate
776 				 * package. The starting 'from' package should be kept, just
777 				 * as if deltas were not used. */
778 				if(dlts != delta_path) {
779 					unlink(from);
780 				}
781 			}
782 			FREE(from);
783 			FREE(to);
784 			FREE(delta);
785 
786 			if(retval != 0) {
787 				/* one delta failed for this package, cancel the remaining ones */
788 				event.type = ALPM_EVENT_DELTA_PATCH_FAILED;
789 				EVENT(handle, &event);
790 				handle->pm_errno = ALPM_ERR_DLT_PATCHFAILED;
791 				ret = 1;
792 				break;
793 			}
794 		}
795 	}
796 	if(deltas_found) {
797 		event.type = ALPM_EVENT_DELTA_PATCHES_DONE;
798 		EVENT(handle, &event);
799 	}
800 
801 	return ret;
802 }
803 
804 /**
805  * Prompts to delete the file now that we know it is invalid.
806  * @param handle the context handle
807  * @param filename the absolute path of the file to test
808  * @param reason an error code indicating the reason for package invalidity
809  *
810  * @return 1 if file was removed, 0 otherwise
811  */
prompt_to_delete(alpm_handle_t * handle,const char * filepath,alpm_errno_t reason)812 static int prompt_to_delete(alpm_handle_t *handle, const char *filepath,
813 		alpm_errno_t reason)
814 {
815 	alpm_question_corrupted_t question = {
816 		.type = ALPM_QUESTION_CORRUPTED_PKG,
817 		.remove = 0,
818 		.filepath = filepath,
819 		.reason = reason
820 	};
821 	QUESTION(handle, &question);
822 	if(question.remove) {
823 		unlink(filepath);
824 	}
825 	return question.remove;
826 }
827 
validate_deltas(alpm_handle_t * handle,alpm_list_t * deltas)828 static int validate_deltas(alpm_handle_t *handle, alpm_list_t *deltas)
829 {
830 	alpm_list_t *i, *errors = NULL;
831 	alpm_event_t event;
832 
833 	if(!deltas) {
834 		return 0;
835 	}
836 
837 	/* Check integrity of deltas */
838 	event.type = ALPM_EVENT_DELTA_INTEGRITY_START;
839 	EVENT(handle, &event);
840 	for(i = deltas; i; i = i->next) {
841 		alpm_delta_t *d = i->data;
842 		char *filepath = _alpm_filecache_find(handle, d->delta);
843 
844 		if(_alpm_test_checksum(filepath, d->delta_md5, ALPM_PKG_VALIDATION_MD5SUM)) {
845 			errors = alpm_list_add(errors, filepath);
846 		} else {
847 			FREE(filepath);
848 		}
849 	}
850 	event.type = ALPM_EVENT_DELTA_INTEGRITY_DONE;
851 	EVENT(handle, &event);
852 
853 	if(errors) {
854 		for(i = errors; i; i = i->next) {
855 			char *filepath = i->data;
856 			prompt_to_delete(handle, filepath, ALPM_ERR_DLT_INVALID);
857 			FREE(filepath);
858 		}
859 		alpm_list_free(errors);
860 		handle->pm_errno = ALPM_ERR_DLT_INVALID;
861 		return -1;
862 	}
863 	return 0;
864 }
865 
build_payload(alpm_handle_t * handle,const char * filename,size_t size,alpm_list_t * servers)866 static struct dload_payload *build_payload(alpm_handle_t *handle,
867 		const char *filename, size_t size, alpm_list_t *servers)
868 {
869 		struct dload_payload *payload;
870 
871 		CALLOC(payload, 1, sizeof(*payload), RET_ERR(handle, ALPM_ERR_MEMORY, NULL));
872 		STRDUP(payload->remote_name, filename, FREE(payload); RET_ERR(handle, ALPM_ERR_MEMORY, NULL));
873 		payload->max_size = size;
874 		payload->servers = servers;
875 		return payload;
876 }
877 
find_dl_candidates(alpm_db_t * repo,alpm_list_t ** files,alpm_list_t ** deltas)878 static int find_dl_candidates(alpm_db_t *repo, alpm_list_t **files, alpm_list_t **deltas)
879 {
880 	alpm_list_t *i;
881 	alpm_handle_t *handle = repo->handle;
882 
883 	for(i = handle->trans->add; i; i = i->next) {
884 		alpm_pkg_t *spkg = i->data;
885 
886 		if(spkg->origin != ALPM_PKG_FROM_FILE && repo == spkg->origin_data.db) {
887 			alpm_list_t *delta_path = spkg->delta_path;
888 
889 			if(!repo->servers) {
890 				handle->pm_errno = ALPM_ERR_SERVER_NONE;
891 				_alpm_log(handle, ALPM_LOG_ERROR, "%s: %s\n",
892 						alpm_strerror(handle->pm_errno), repo->treename);
893 				return 1;
894 			}
895 
896 			if(delta_path) {
897 				/* using deltas */
898 				alpm_list_t *dlts;
899 				for(dlts = delta_path; dlts; dlts = dlts->next) {
900 					alpm_delta_t *delta = dlts->data;
901 					if(delta->download_size != 0) {
902 						struct dload_payload *payload = build_payload(
903 								handle, delta->delta, delta->delta_size, repo->servers);
904 						ASSERT(payload, return -1);
905 						*files = alpm_list_add(*files, payload);
906 					}
907 					/* keep a list of all the delta files for md5sums */
908 					*deltas = alpm_list_add(*deltas, delta);
909 				}
910 
911 			} else if(spkg->download_size != 0) {
912 				struct dload_payload *payload;
913 				ASSERT(spkg->filename != NULL, RET_ERR(handle, ALPM_ERR_PKG_INVALID_NAME, -1));
914 				payload = build_payload(handle, spkg->filename, spkg->size, repo->servers);
915 				ASSERT(payload, return -1);
916 				*files = alpm_list_add(*files, payload);
917 			}
918 		}
919 	}
920 
921 	return 0;
922 }
923 
download_single_file(alpm_handle_t * handle,struct dload_payload * payload,const char * cachedir)924 static int download_single_file(alpm_handle_t *handle, struct dload_payload *payload,
925 		const char *cachedir)
926 {
927 	alpm_event_pkgdownload_t event = {
928 		.type = ALPM_EVENT_PKGDOWNLOAD_START,
929 		.file = payload->remote_name
930 	};
931 	const alpm_list_t *server;
932 
933 	payload->handle = handle;
934 	payload->allow_resume = 1;
935 
936 	EVENT(handle, &event);
937 	for(server = payload->servers; server; server = server->next) {
938 		const char *server_url = server->data;
939 		size_t len;
940 
941 		/* print server + filename into a buffer */
942 		len = strlen(server_url) + strlen(payload->remote_name) + 2;
943 		MALLOC(payload->fileurl, len, RET_ERR(handle, ALPM_ERR_MEMORY, -1));
944 		snprintf(payload->fileurl, len, "%s/%s", server_url, payload->remote_name);
945 
946 		if(_alpm_download(payload, cachedir, NULL, NULL) != -1) {
947 			event.type = ALPM_EVENT_PKGDOWNLOAD_DONE;
948 			EVENT(handle, &event);
949 			return 0;
950 		}
951 		_alpm_dload_payload_reset_for_retry(payload);
952 	}
953 
954 	event.type = ALPM_EVENT_PKGDOWNLOAD_FAILED;
955 	EVENT(handle, &event);
956 	return -1;
957 }
958 
download_files(alpm_handle_t * handle,alpm_list_t ** deltas)959 static int download_files(alpm_handle_t *handle, alpm_list_t **deltas)
960 {
961 	const char *cachedir;
962 	alpm_list_t *i, *files = NULL;
963 	int errors = 0;
964 	alpm_event_t event;
965 
966 	cachedir = _alpm_filecache_setup(handle);
967 	handle->trans->state = STATE_DOWNLOADING;
968 
969 	/* Total progress - figure out the total download size if required to
970 	 * pass to the callback. This function is called once, and it is up to the
971 	 * frontend to compute incremental progress. */
972 	if(handle->totaldlcb) {
973 		off_t total_size = (off_t)0;
974 		/* sum up the download size for each package and store total */
975 		for(i = handle->trans->add; i; i = i->next) {
976 			alpm_pkg_t *spkg = i->data;
977 			total_size += spkg->download_size;
978 		}
979 		handle->totaldlcb(total_size);
980 	}
981 
982 	for(i = handle->dbs_sync; i; i = i->next) {
983 		errors += find_dl_candidates(i->data, &files, deltas);
984 	}
985 
986 	if(files) {
987 		/* check for necessary disk space for download */
988 		if(handle->checkspace) {
989 			off_t *file_sizes;
990 			size_t idx, num_files;
991 			int ret;
992 
993 			_alpm_log(handle, ALPM_LOG_DEBUG, "checking available disk space for download\n");
994 
995 			num_files = alpm_list_count(files);
996 			CALLOC(file_sizes, num_files, sizeof(off_t), goto finish);
997 
998 			for(i = files, idx = 0; i; i = i->next, idx++) {
999 				const struct dload_payload *payload = i->data;
1000 				file_sizes[idx] = payload->max_size;
1001 			}
1002 
1003 			ret = _alpm_check_downloadspace(handle, cachedir, num_files, file_sizes);
1004 			free(file_sizes);
1005 
1006 			if(ret != 0) {
1007 				errors++;
1008 				goto finish;
1009 			}
1010 		}
1011 
1012 		event.type = ALPM_EVENT_RETRIEVE_START;
1013 		EVENT(handle, &event);
1014 		event.type = ALPM_EVENT_RETRIEVE_DONE;
1015 		for(i = files; i; i = i->next) {
1016 			if(download_single_file(handle, i->data, cachedir) == -1) {
1017 				errors++;
1018 				event.type = ALPM_EVENT_RETRIEVE_FAILED;
1019 				_alpm_log(handle, ALPM_LOG_WARNING, _("failed to retrieve some files\n"));
1020 			}
1021 		}
1022 		EVENT(handle, &event);
1023 	}
1024 
1025 finish:
1026 	if(files) {
1027 		alpm_list_free_inner(files, (alpm_list_fn_free)_alpm_dload_payload_reset);
1028 		FREELIST(files);
1029 	}
1030 
1031 	for(i = handle->trans->add; i; i = i->next) {
1032 		alpm_pkg_t *pkg = i->data;
1033 		pkg->infolevel &= ~INFRQ_DSIZE;
1034 		pkg->download_size = 0;
1035 	}
1036 
1037 	/* clear out value to let callback know we are done */
1038 	if(handle->totaldlcb) {
1039 		handle->totaldlcb(0);
1040 	}
1041 
1042 	return errors;
1043 }
1044 
1045 #ifdef HAVE_LIBGPGME
check_keyring(alpm_handle_t * handle)1046 static int check_keyring(alpm_handle_t *handle)
1047 {
1048 	size_t current = 0, numtargs;
1049 	alpm_list_t *i, *errors = NULL;
1050 	alpm_event_t event;
1051 
1052 	event.type = ALPM_EVENT_KEYRING_START;
1053 	EVENT(handle, &event);
1054 
1055 	numtargs = alpm_list_count(handle->trans->add);
1056 
1057 	for(i = handle->trans->add; i; i = i->next, current++) {
1058 		alpm_pkg_t *pkg = i->data;
1059 		int level;
1060 
1061 		int percent = (current * 100) / numtargs;
1062 		PROGRESS(handle, ALPM_PROGRESS_KEYRING_START, "", percent,
1063 				numtargs, current);
1064 
1065 		if(pkg->origin == ALPM_PKG_FROM_FILE) {
1066 			continue; /* pkg_load() has been already called, this package is valid */
1067 		}
1068 
1069 		level = alpm_db_get_siglevel(alpm_pkg_get_db(pkg));
1070 		if((level & ALPM_SIG_PACKAGE) && pkg->base64_sig) {
1071 			unsigned char *decoded_sigdata = NULL;
1072 			size_t data_len;
1073 			int decode_ret = alpm_decode_signature(pkg->base64_sig,
1074 					&decoded_sigdata, &data_len);
1075 			if(decode_ret == 0) {
1076 				alpm_list_t *keys = NULL;
1077 				if(alpm_extract_keyid(handle, pkg->name, decoded_sigdata,
1078 							data_len, &keys) == 0) {
1079 					alpm_list_t *k;
1080 					for(k = keys; k; k = k->next) {
1081 						char *key = k->data;
1082 						if(!alpm_list_find_str(errors, key) &&
1083 								_alpm_key_in_keychain(handle, key) == 0) {
1084 							errors = alpm_list_add(errors, strdup(key));
1085 						}
1086 					}
1087 					FREELIST(keys);
1088 				}
1089 				free(decoded_sigdata);
1090 			}
1091 		}
1092 	}
1093 
1094 	PROGRESS(handle, ALPM_PROGRESS_KEYRING_START, "", 100,
1095 			numtargs, current);
1096 	event.type = ALPM_EVENT_KEYRING_DONE;
1097 	EVENT(handle, &event);
1098 
1099 	if(errors) {
1100 		event.type = ALPM_EVENT_KEY_DOWNLOAD_START;
1101 		EVENT(handle, &event);
1102 		int fail = 0;
1103 		alpm_list_t *k;
1104 		for(k = errors; k; k = k->next) {
1105 			char *key = k->data;
1106 			if(_alpm_key_import(handle, key) == -1) {
1107 				fail = 1;
1108 			}
1109 		}
1110 		event.type = ALPM_EVENT_KEY_DOWNLOAD_DONE;
1111 		EVENT(handle, &event);
1112 		if(fail) {
1113 			_alpm_log(handle, ALPM_LOG_ERROR, _("required key missing from keyring\n"));
1114 			return -1;
1115 		}
1116 	}
1117 
1118 	return 0;
1119 }
1120 #endif /* HAVE_LIBGPGME */
1121 
check_validity(alpm_handle_t * handle,size_t total,uint64_t total_bytes)1122 static int check_validity(alpm_handle_t *handle,
1123 		size_t total, uint64_t total_bytes)
1124 {
1125 	struct validity {
1126 		alpm_pkg_t *pkg;
1127 		char *path;
1128 		alpm_siglist_t *siglist;
1129 		int siglevel;
1130 		int validation;
1131 		alpm_errno_t error;
1132 	};
1133 	size_t current = 0;
1134 	uint64_t current_bytes = 0;
1135 	alpm_list_t *i, *errors = NULL;
1136 	alpm_event_t event;
1137 
1138 	/* Check integrity of packages */
1139 	event.type = ALPM_EVENT_INTEGRITY_START;
1140 	EVENT(handle, &event);
1141 
1142 	for(i = handle->trans->add; i; i = i->next, current++) {
1143 		struct validity v = { i->data, NULL, NULL, 0, 0, 0 };
1144 		int percent = (int)(((double)current_bytes / total_bytes) * 100);
1145 
1146 		PROGRESS(handle, ALPM_PROGRESS_INTEGRITY_START, "", percent,
1147 				total, current);
1148 		if(v.pkg->origin == ALPM_PKG_FROM_FILE) {
1149 			continue; /* pkg_load() has been already called, this package is valid */
1150 		}
1151 
1152 		current_bytes += v.pkg->size;
1153 		v.path = _alpm_filecache_find(handle, v.pkg->filename);
1154 		v.siglevel = alpm_db_get_siglevel(alpm_pkg_get_db(v.pkg));
1155 
1156 		if(_alpm_pkg_validate_internal(handle, v.path, v.pkg,
1157 					v.siglevel, &v.siglist, &v.validation) == -1) {
1158 			struct validity *invalid;
1159 			v.error = handle->pm_errno;
1160 			MALLOC(invalid, sizeof(struct validity), return -1);
1161 			memcpy(invalid, &v, sizeof(struct validity));
1162 			errors = alpm_list_add(errors, invalid);
1163 		} else {
1164 			alpm_siglist_cleanup(v.siglist);
1165 			free(v.siglist);
1166 			free(v.path);
1167 			v.pkg->validation = v.validation;
1168 		}
1169 	}
1170 
1171 	PROGRESS(handle, ALPM_PROGRESS_INTEGRITY_START, "", 100,
1172 			total, current);
1173 	event.type = ALPM_EVENT_INTEGRITY_DONE;
1174 	EVENT(handle, &event);
1175 
1176 	if(errors) {
1177 		for(i = errors; i; i = i->next) {
1178 			struct validity *v = i->data;
1179 			if(v->error == ALPM_ERR_PKG_MISSING_SIG) {
1180 				_alpm_log(handle, ALPM_LOG_ERROR,
1181 						_("%s: missing required signature\n"), v->pkg->name);
1182 			} else if(v->error == ALPM_ERR_PKG_INVALID_SIG) {
1183 				_alpm_process_siglist(handle, v->pkg->name, v->siglist,
1184 						v->siglevel & ALPM_SIG_PACKAGE_OPTIONAL,
1185 						v->siglevel & ALPM_SIG_PACKAGE_MARGINAL_OK,
1186 						v->siglevel & ALPM_SIG_PACKAGE_UNKNOWN_OK);
1187 				prompt_to_delete(handle, v->path, v->error);
1188 			} else if(v->error == ALPM_ERR_PKG_INVALID_CHECKSUM) {
1189 				prompt_to_delete(handle, v->path, v->error);
1190 			}
1191 			alpm_siglist_cleanup(v->siglist);
1192 			free(v->siglist);
1193 			free(v->path);
1194 			free(v);
1195 		}
1196 		alpm_list_free(errors);
1197 
1198 		if(handle->pm_errno == ALPM_ERR_OK) {
1199 			RET_ERR(handle, ALPM_ERR_PKG_INVALID, -1);
1200 		}
1201 		return -1;
1202 	}
1203 
1204 	return 0;
1205 }
1206 
load_packages(alpm_handle_t * handle,alpm_list_t ** data,size_t total,size_t total_bytes)1207 static int load_packages(alpm_handle_t *handle, alpm_list_t **data,
1208 		size_t total, size_t total_bytes)
1209 {
1210 	size_t current = 0, current_bytes = 0;
1211 	int errors = 0;
1212 	alpm_list_t *i;
1213 	alpm_event_t event;
1214 
1215 	/* load packages from disk now that they are known-valid */
1216 	event.type = ALPM_EVENT_LOAD_START;
1217 	EVENT(handle, &event);
1218 
1219 	for(i = handle->trans->add; i; i = i->next, current++) {
1220 		int error = 0;
1221 		alpm_pkg_t *spkg = i->data;
1222 		char *filepath;
1223 		int percent = (int)(((double)current_bytes / total_bytes) * 100);
1224 
1225 		PROGRESS(handle, ALPM_PROGRESS_LOAD_START, "", percent,
1226 				total, current);
1227 		if(spkg->origin == ALPM_PKG_FROM_FILE) {
1228 			continue; /* pkg_load() has been already called, this package is valid */
1229 		}
1230 
1231 		current_bytes += spkg->size;
1232 		filepath = _alpm_filecache_find(handle, spkg->filename);
1233 
1234 		/* load the package file and replace pkgcache entry with it in the target list */
1235 		/* TODO: alpm_pkg_get_db() will not work on this target anymore */
1236 		_alpm_log(handle, ALPM_LOG_DEBUG,
1237 				"replacing pkgcache entry with package file for target %s\n",
1238 				spkg->name);
1239 		alpm_pkg_t *pkgfile =_alpm_pkg_load_internal(handle, filepath, 1);
1240 		if(!pkgfile) {
1241 			_alpm_log(handle, ALPM_LOG_DEBUG, "failed to load pkgfile internal\n");
1242 			error = 1;
1243 		} else {
1244 			if(strcmp(spkg->name, pkgfile->name) != 0) {
1245 				_alpm_log(handle, ALPM_LOG_DEBUG,
1246 						"internal package name mismatch, expected: '%s', actual: '%s'\n",
1247 						spkg->name, pkgfile->name);
1248 				error = 1;
1249 			}
1250 			if(strcmp(spkg->version, pkgfile->version) != 0) {
1251 				_alpm_log(handle, ALPM_LOG_DEBUG,
1252 						"internal package version mismatch, expected: '%s', actual: '%s'\n",
1253 						spkg->version, pkgfile->version);
1254 				error = 1;
1255 			}
1256 		}
1257 		if(error != 0) {
1258 			errors++;
1259 			*data = alpm_list_add(*data, strdup(spkg->filename));
1260 			free(filepath);
1261 			continue;
1262 		}
1263 		free(filepath);
1264 		/* copy over the install reason */
1265 		pkgfile->reason = spkg->reason;
1266 		/* copy over validation method */
1267 		pkgfile->validation = spkg->validation;
1268 		/* transfer oldpkg */
1269 		pkgfile->oldpkg = spkg->oldpkg;
1270 		spkg->oldpkg = NULL;
1271 		i->data = pkgfile;
1272 		/* spkg has been removed from the target list, so we can free the
1273 		 * sync-specific fields */
1274 		_alpm_pkg_free_trans(spkg);
1275 	}
1276 
1277 	PROGRESS(handle, ALPM_PROGRESS_LOAD_START, "", 100,
1278 			total, current);
1279 	event.type = ALPM_EVENT_LOAD_DONE;
1280 	EVENT(handle, &event);
1281 
1282 	if(errors) {
1283 		if(handle->pm_errno == ALPM_ERR_OK) {
1284 			RET_ERR(handle, ALPM_ERR_PKG_INVALID, -1);
1285 		}
1286 		return -1;
1287 	}
1288 
1289 	return 0;
1290 }
1291 
_alpm_sync_load(alpm_handle_t * handle,alpm_list_t ** data)1292 int _alpm_sync_load(alpm_handle_t *handle, alpm_list_t **data)
1293 {
1294 	alpm_list_t *i, *deltas = NULL;
1295 	size_t total = 0;
1296 	uint64_t total_bytes = 0;
1297 	alpm_trans_t *trans = handle->trans;
1298 
1299 	if(download_files(handle, &deltas)) {
1300 		alpm_list_free(deltas);
1301 		return -1;
1302 	}
1303 
1304 	if(validate_deltas(handle, deltas)) {
1305 		alpm_list_free(deltas);
1306 		return -1;
1307 	}
1308 	alpm_list_free(deltas);
1309 
1310 	/* Use the deltas to generate the packages */
1311 	if(apply_deltas(handle)) {
1312 		return -1;
1313 	}
1314 
1315 #ifdef HAVE_LIBGPGME
1316 	/* make sure all required signatures are in keyring */
1317 	if(check_keyring(handle)) {
1318 		return -1;
1319 	}
1320 #endif
1321 
1322 	/* get the total size of all packages so we can adjust the progress bar more
1323 	 * realistically if there are small and huge packages involved */
1324 	for(i = trans->add; i; i = i->next) {
1325 		alpm_pkg_t *spkg = i->data;
1326 		if(spkg->origin != ALPM_PKG_FROM_FILE) {
1327 			total_bytes += spkg->size;
1328 		}
1329 		total++;
1330 	}
1331 	/* this can only happen maliciously */
1332 	total_bytes = total_bytes ? total_bytes : 1;
1333 
1334 	if(check_validity(handle, total, total_bytes) != 0) {
1335 		return -1;
1336 	}
1337 
1338 	if(trans->flags & ALPM_TRANS_FLAG_DOWNLOADONLY) {
1339 		return 0;
1340 	}
1341 
1342 	if(load_packages(handle, data, total, total_bytes)) {
1343 		return -1;
1344 	}
1345 
1346 	return 0;
1347 }
1348 
_alpm_sync_check(alpm_handle_t * handle,alpm_list_t ** data)1349 int _alpm_sync_check(alpm_handle_t *handle, alpm_list_t **data)
1350 {
1351 	alpm_trans_t *trans = handle->trans;
1352 	alpm_event_t event;
1353 
1354 	/* fileconflict check */
1355 	if(!(trans->flags & ALPM_TRANS_FLAG_DBONLY)) {
1356 		event.type = ALPM_EVENT_FILECONFLICTS_START;
1357 		EVENT(handle, &event);
1358 
1359 		_alpm_log(handle, ALPM_LOG_DEBUG, "looking for file conflicts\n");
1360 		alpm_list_t *conflict = _alpm_db_find_fileconflicts(handle,
1361 				trans->add, trans->remove);
1362 		if(conflict) {
1363 			if(data) {
1364 				*data = conflict;
1365 			} else {
1366 				alpm_list_free_inner(conflict,
1367 						(alpm_list_fn_free)alpm_fileconflict_free);
1368 				alpm_list_free(conflict);
1369 			}
1370 			RET_ERR(handle, ALPM_ERR_FILE_CONFLICTS, -1);
1371 		}
1372 
1373 		event.type = ALPM_EVENT_FILECONFLICTS_DONE;
1374 		EVENT(handle, &event);
1375 	}
1376 
1377 	/* check available disk space */
1378 	if(handle->checkspace && !(trans->flags & ALPM_TRANS_FLAG_DBONLY)) {
1379 		event.type = ALPM_EVENT_DISKSPACE_START;
1380 		EVENT(handle, &event);
1381 
1382 		_alpm_log(handle, ALPM_LOG_DEBUG, "checking available disk space\n");
1383 		if(_alpm_check_diskspace(handle) == -1) {
1384 			_alpm_log(handle, ALPM_LOG_ERROR, _("not enough free disk space\n"));
1385 			return -1;
1386 		}
1387 
1388 		event.type = ALPM_EVENT_DISKSPACE_DONE;
1389 		EVENT(handle, &event);
1390 	}
1391 
1392 	return 0;
1393 }
1394 
_alpm_sync_commit(alpm_handle_t * handle)1395 int _alpm_sync_commit(alpm_handle_t *handle)
1396 {
1397 	alpm_trans_t *trans = handle->trans;
1398 
1399 	/* remove conflicting and to-be-replaced packages */
1400 	if(trans->remove) {
1401 		_alpm_log(handle, ALPM_LOG_DEBUG,
1402 				"removing conflicting and to-be-replaced packages\n");
1403 		/* we want the frontend to be aware of commit details */
1404 		if(_alpm_remove_packages(handle, 0) == -1) {
1405 			_alpm_log(handle, ALPM_LOG_ERROR,
1406 					_("could not commit removal transaction\n"));
1407 			return -1;
1408 		}
1409 	}
1410 
1411 	/* install targets */
1412 	_alpm_log(handle, ALPM_LOG_DEBUG, "installing packages\n");
1413 	if(_alpm_upgrade_packages(handle) == -1) {
1414 		_alpm_log(handle, ALPM_LOG_ERROR, _("could not commit transaction\n"));
1415 		return -1;
1416 	}
1417 
1418 	return 0;
1419 }
1420