solver.c 27.3 KB
Newer Older
1
/* solver.c - Alpine Package Keeper (APK)
2
 * Up- and down-propagating, forwarding checking, deductive dependency solver.
3
 *
4
 * Copyright (C) 2008-2013 Timo Teräs <timo.teras@iki.fi>
5 6 7 8 9 10 11
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published
 * by the Free Software Foundation. See http://www.gnu.org/ for details.
 */

12
#include <stdint.h>
13
#include <unistd.h>
14
#include <strings.h>
15 16 17 18 19
#include "apk_defines.h"
#include "apk_database.h"
#include "apk_package.h"
#include "apk_solver.h"

20 21
#include "apk_print.h"

22 23 24
//#define DEBUG_PRINT

#ifdef DEBUG_PRINT
25 26 27 28 29 30
#include <stdio.h>
#define dbg_printf(args...) fprintf(stderr, args)
#else
#define dbg_printf(args...)
#endif

31
#define ASSERT(cond, fmt...)	if (!(cond)) { apk_error(fmt); *(char*)NULL = 0; }
32

33 34
struct apk_solver_state {
	struct apk_database *db;
35 36 37 38
	struct apk_changeset *changeset;
	struct list_head dirty_head;
	struct list_head unresolved_head;
	unsigned int errors;
39
	unsigned int solver_flags_inherit;
40 41
	unsigned int pinning_inherit;
	unsigned int default_repos;
42
};
43

44 45 46
static struct apk_provider provider_none = {
	.pkg = NULL,
	.version = &apk_null_blob
47 48
};

49 50 51
void apk_solver_set_name_flags(struct apk_name *name,
			       unsigned short solver_flags,
			       unsigned short solver_flags_inheritable)
52
{
53
	struct apk_provider *p;
54

55 56
	foreach_array_item(p, name->providers) {
		struct apk_package *pkg = p->pkg;
57 58 59
		pkg->ss.solver_flags |= solver_flags;
		pkg->ss.solver_flags_inheritable |= solver_flags_inheritable;
	}
60 61
}

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
static int get_tag(struct apk_database *db, unsigned int pinning_mask, unsigned int repos)
{
	int i;

	for (i = 0; i < db->num_repo_tags; i++) {
		if (!(BIT(i) & pinning_mask))
			continue;
		if (db->repo_tags[i].allowed_repos & repos)
			return i;
	}
	return APK_DEFAULT_REPOSITORY_TAG;
}

static unsigned int get_pkg_repos(struct apk_database *db, struct apk_package *pkg)
{
	return pkg->repos | (pkg->ipkg ? db->repo_tags[pkg->ipkg->repository_tag].allowed_repos : 0);
}

80 81 82 83 84 85 86 87
static void mark_error(struct apk_solver_state *ss, struct apk_package *pkg)
{
	if (pkg == NULL || pkg->ss.error)
		return;
	pkg->ss.error = 1;
	ss->errors++;
}

88
static void queue_dirty(struct apk_solver_state *ss, struct apk_name *name)
89
{
90 91 92
	if (list_hashed(&name->ss.dirty_list) || name->ss.locked ||
	    (name->ss.requirers == 0 && !name->ss.reevaluate_iif))
		return;
93

94 95
	dbg_printf("queue_dirty: %s\n", name->name);
	list_add_tail(&name->ss.dirty_list, &ss->dirty_head);
96 97
}

98
static void queue_unresolved(struct apk_solver_state *ss, struct apk_name *name)
99
{
100
	int want;
101

102 103
	if (name->ss.locked)
		return;
104

105
	want = (name->ss.requirers > 0) || (name->ss.has_iif);
106
	dbg_printf("queue_unresolved: %s, want=%d (requirers=%d, has_iif=%d)\n", name->name, want, name->ss.requirers, name->ss.has_iif);
107 108 109 110
	if (want && !list_hashed(&name->ss.unresolved_list))
		list_add(&name->ss.unresolved_list, &ss->unresolved_head);
	else if (!want && list_hashed(&name->ss.unresolved_list))
		list_del_init(&name->ss.unresolved_list);
111 112
}

113
static void reevaluate_reverse_deps(struct apk_solver_state *ss, struct apk_name *name)
114
{
115 116 117 118 119 120 121 122 123
	struct apk_name **pname0, *name0;

	foreach_array_item(pname0, name->rdepends) {
		name0 = *pname0;
		if (!name0->ss.seen)
			continue;
		name0->ss.reevaluate_deps = 1;
		queue_dirty(ss, name0);
	}
124
}
125

126
static void reevaluate_reverse_installif(struct apk_solver_state *ss, struct apk_name *name)
127
{
128 129 130 131 132 133
	struct apk_name **pname0, *name0;

	foreach_array_item(pname0, name->rinstall_if) {
		name0 = *pname0;
		if (!name0->ss.seen)
			continue;
134 135
		if (name0->ss.no_iif)
			continue;
136 137 138
		name0->ss.reevaluate_iif = 1;
		queue_dirty(ss, name0);
	}
139 140
}

141
static void disqualify_package(struct apk_solver_state *ss, struct apk_package *pkg, const char *reason)
142
{
143
	struct apk_dependency *p;
144

145
	dbg_printf("disqualify_package: " PKG_VER_FMT " (%s)\n", PKG_VER_PRINTF(pkg), reason);
146
	pkg->ss.pkg_selectable = 0;
147 148 149 150
	reevaluate_reverse_deps(ss, pkg->name);
	foreach_array_item(p, pkg->provides)
		reevaluate_reverse_deps(ss, p->name);
	reevaluate_reverse_installif(ss, pkg->name);
151 152
}

153
static int dependency_satisfiable(struct apk_solver_state *ss, struct apk_dependency *dep)
154
{
155
	struct apk_name *name = dep->name;
156
	struct apk_provider *p;
157

158 159
	if (name->ss.locked)
		return apk_dep_is_provided(dep, &name->ss.chosen);
160

161 162
	if (name->ss.requirers == 0 && apk_dep_is_provided(dep, &provider_none))
		return TRUE;
163

164
	foreach_array_item(p, name->providers)
165
		if (p->pkg->ss.pkg_selectable && apk_dep_is_provided(dep, p))
166
			return TRUE;
167

168
	return FALSE;
169 170
}

171
static void discover_name(struct apk_solver_state *ss, struct apk_name *name)
172
{
173
	struct apk_database *db = ss->db;
174 175 176
	struct apk_name **pname0;
	struct apk_provider *p;
	struct apk_dependency *dep;
177
	unsigned int repos;
178

179 180
	if (name->ss.seen)
		return;
181

182
	name->ss.seen = 1;
183
	name->ss.no_iif = 1;
184 185
	foreach_array_item(p, name->providers) {
		struct apk_package *pkg = p->pkg;
186
		if (pkg->ss.seen)
187
			continue;
188

189
		pkg->ss.seen = 1;
190

191 192
		pkg->ss.pinning_allowed = APK_DEFAULT_PINNING_MASK;
		pkg->ss.pinning_preferred = APK_DEFAULT_PINNING_MASK;
193 194
		pkg->ss.pkg_available =
			(pkg->filename != NULL) ||
195 196 197 198 199
			(pkg->repos & db->available_repos & ~BIT(APK_REPOSITORY_CACHED));
		/* Package is in 'cached' repository if filename is provided,
		 * or it's a 'virtual' package with install_size zero */
		pkg->ss.pkg_selectable =
			(pkg->repos & db->available_repos) ||
200
			pkg->cached_non_repository ||
201
			pkg->ipkg;
202

203 204 205 206 207 208 209 210 211
		/* Prune install_if packages that are no longer available,
		 * currently works only if SOLVERF_AVAILABLE is set in the
		 * global solver flags. */
		pkg->ss.iif_failed =
			(pkg->install_if->num == 0) ||
			((ss->solver_flags_inherit & APK_SOLVERF_AVAILABLE) &&
			 !pkg->ss.pkg_available);
		name->ss.no_iif &= pkg->ss.iif_failed;

212
		repos = get_pkg_repos(db, pkg);
213 214 215
		pkg->ss.tag_preferred =
			(pkg->filename != NULL) ||
			(pkg->installed_size == 0) ||
216 217 218 219 220
			(repos & ss->default_repos);
		pkg->ss.tag_ok =
			pkg->ss.tag_preferred ||
			pkg->cached_non_repository ||
			pkg->ipkg;
221

222 223
		foreach_array_item(dep, pkg->depends) {
			discover_name(ss, dep->name);
224
			pkg->ss.max_dep_chain = max(pkg->ss.max_dep_chain,
225 226
						    dep->name->ss.max_dep_chain+1);
		}
227
		name->ss.max_dep_chain = max(name->ss.max_dep_chain, pkg->ss.max_dep_chain);
228

229
		dbg_printf("discover " PKG_VER_FMT ": tag_ok=%d, tag_pref=%d max_dep_chain=%d selectable=%d\n",
230 231 232
			PKG_VER_PRINTF(pkg),
			pkg->ss.tag_ok,
			pkg->ss.tag_preferred,
233
			pkg->ss.max_dep_chain,
234
			pkg->ss.pkg_selectable);
235
	}
236 237
	foreach_array_item(pname0, name->rinstall_if)
		discover_name(ss, *pname0);
238 239
}

240
static void name_requirers_changed(struct apk_solver_state *ss, struct apk_name *name)
241
{
242
	queue_unresolved(ss, name);
243
	reevaluate_reverse_installif(ss, name);
244
	queue_dirty(ss, name);
245 246
}

247 248
static void inherit_pinning_and_flags(
	struct apk_solver_state *ss, struct apk_package *pkg, struct apk_package *ppkg)
249 250 251
{
	unsigned int repos = get_pkg_repos(ss->db, pkg);

252 253 254 255 256 257 258 259 260 261 262 263
	if (ppkg != NULL) {
		/* inherited */
		pkg->ss.solver_flags |= ppkg->ss.solver_flags_inheritable;
		pkg->ss.solver_flags_inheritable |= ppkg->ss.solver_flags_inheritable;
		pkg->ss.pinning_allowed |= ppkg->ss.pinning_allowed;
	} else {
		/* world dependency */
		pkg->ss.solver_flags |= ss->solver_flags_inherit;
		pkg->ss.solver_flags_inheritable |= ss->solver_flags_inherit;
		pkg->ss.pinning_allowed |= ss->pinning_inherit;
		/* also prefer main pinnings */
		pkg->ss.pinning_preferred = ss->pinning_inherit;
264 265
		pkg->ss.tag_preferred = !!(repos & apk_db_get_pinning_mask_repos(ss->db, pkg->ss.pinning_preferred));
	}
266 267 268 269
	pkg->ss.tag_ok |= !!(repos & apk_db_get_pinning_mask_repos(ss->db, pkg->ss.pinning_allowed));

	dbg_printf(PKG_VER_FMT ": tag_ok=%d, tag_pref=%d\n",
		PKG_VER_PRINTF(pkg), pkg->ss.tag_ok, pkg->ss.tag_preferred);
270 271
}

272
static void apply_constraint(struct apk_solver_state *ss, struct apk_package *ppkg, struct apk_dependency *dep)
273
{
274
	struct apk_name *name = dep->name;
275 276
	struct apk_provider *p0;
	int is_provided;
277

278
	dbg_printf("    apply_constraint: %s%s%s" BLOB_FMT "\n",
279 280 281 282 283 284
		dep->conflict ? "!" : "",
		name->name,
		apk_version_op_string(dep->result_mask),
		BLOB_PRINTF(*dep->version));

	name->ss.requirers += !dep->conflict;
285 286
	if (name->ss.requirers == 1 && !dep->conflict)
		name_requirers_changed(ss, name);
287

288
	foreach_array_item(p0, name->providers) {
289
		struct apk_package *pkg0 = p0->pkg;
290

291
		is_provided = apk_dep_is_provided(dep, p0);
292
		dbg_printf("    apply_constraint: provider: %s-" BLOB_FMT ": %d\n",
293
			pkg0->name->name, BLOB_PRINTF(*p0->version), is_provided);
294

295
		pkg0->ss.conflicts += !is_provided;
296
		if (unlikely(pkg0->ss.pkg_selectable && pkg0->ss.conflicts))
297
			disqualify_package(ss, pkg0, "conflicting dependency");
298

299 300
		if (is_provided)
			inherit_pinning_and_flags(ss, pkg0, ppkg);
301
	}
302 303
}

304 305 306 307 308
static void exclude_non_providers(struct apk_solver_state *ss, struct apk_name *name, struct apk_name *must_provide)
{
	struct apk_provider *p;
	struct apk_dependency *d;

309 310 311
	if (name == must_provide)
		return;

312 313 314
	dbg_printf("%s must provide %s\n", name->name, must_provide->name);

	foreach_array_item(p, name->providers) {
315
		if (p->pkg->name == must_provide || !p->pkg->ss.pkg_selectable)
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
			goto next;
		foreach_array_item(d, p->pkg->provides)
			if (d->name == must_provide)
				goto next;
		disqualify_package(ss, p->pkg, "provides transitivity");
	next: ;
	}
}

static inline void merge_index(unsigned short *index, int num_options)
{
	if (*index == num_options)
		*index = num_options + 1;
}

static inline int merge_index_complete(unsigned short *index, int num_options)
{
	int ret;

	ret = (*index == num_options);
	*index = 0;

	return ret;
}

341
static void reconsider_name(struct apk_solver_state *ss, struct apk_name *name)
342
{
343
	struct apk_name *name0, **pname0;
344
	struct apk_dependency *dep;
345
	struct apk_package *first_candidate = NULL, *pkg;
346 347
	struct apk_provider *p;
	int reevaluate_deps, reevaluate_iif;
348
	int num_options = 0, num_tag_not_ok = 0, has_iif = 0, no_iif = 1;
349

350
	dbg_printf("reconsider_name: %s\n", name->name);
351

352 353
	reevaluate_deps = name->ss.reevaluate_deps;
	reevaluate_iif = name->ss.reevaluate_iif;
354
	name->ss.reevaluate_deps = 0;
355
	name->ss.reevaluate_iif = 0;
356

357 358
	/* propagate down by merging common dependencies and
	 * applying new constraints */
359
	foreach_array_item(p, name->providers) {
360
		/* check if this pkg's dependencies have become unsatisfiable */
361
		pkg = p->pkg;
Timo Teräs's avatar
Timo Teräs committed
362
		pkg->ss.dependencies_merged = 0;
363
		if (reevaluate_deps) {
364
			if (!pkg->ss.pkg_selectable)
365
				continue;
366
			foreach_array_item(dep, pkg->depends) {
367 368 369 370 371 372
				if (!dependency_satisfiable(ss, dep)) {
					disqualify_package(ss, pkg, "dependency no longer satisfiable");
					break;
				}
			}
		}
373
		if (!pkg->ss.pkg_selectable)
374
			continue;
375

376 377 378
		if (reevaluate_iif &&
		    (pkg->ss.iif_triggered == 0 &&
		     pkg->ss.iif_failed == 0)) {
379
			pkg->ss.iif_triggered = 1;
380
			pkg->ss.iif_failed = 0;
381
			foreach_array_item(dep, pkg->install_if) {
382 383 384 385 386 387
				if (!dep->name->ss.locked) {
					pkg->ss.iif_triggered = 0;
					pkg->ss.iif_failed = 0;
					break;
				}
				if (!apk_dep_is_provided(dep, &dep->name->ss.chosen)) {
388
					pkg->ss.iif_triggered = 0;
389
					pkg->ss.iif_failed = 1;
390
					break;
391
				}
392 393
			}
		}
394 395 396 397 398 399
		if (reevaluate_iif && pkg->ss.iif_triggered) {
			foreach_array_item(dep, pkg->install_if)
				inherit_pinning_and_flags(ss, pkg, dep->name->ss.chosen.pkg);
		}
		dbg_printf("  "PKG_VER_FMT": iif_triggered=%d iif_failed=%d\n",
			PKG_VER_PRINTF(pkg), pkg->ss.iif_triggered, pkg->ss.iif_failed);
400
		has_iif |= pkg->ss.iif_triggered;
401
		no_iif  &= pkg->ss.iif_failed;
402

403
		if (name->ss.requirers == 0)
404
			continue;
405

406
		/* merge common dependencies */
Timo Teräs's avatar
Timo Teräs committed
407
		pkg->ss.dependencies_merged = 1;
408 409
		if (first_candidate == NULL)
			first_candidate = pkg;
410 411 412 413 414 415 416 417 418 419

		/* FIXME: can merge also conflicts */
		foreach_array_item(dep, pkg->depends)
			if (!dep->conflict)
				merge_index(&dep->name->ss.merge_depends, num_options);

		merge_index(&pkg->name->ss.merge_provides, num_options);
		foreach_array_item(dep, pkg->provides)
			if (dep->version != &apk_null_blob)
				merge_index(&dep->name->ss.merge_provides, num_options);
420 421 422

		num_tag_not_ok += !pkg->ss.tag_ok;
		num_options++;
423
	}
424
	name->ss.has_options = (num_options > 1 || num_tag_not_ok > 0);
425
	name->ss.has_iif = has_iif;
426
	name->ss.no_iif = no_iif;
427 428
	queue_unresolved(ss, name);

429
	if (first_candidate != NULL) {
430
		pkg = first_candidate;
431 432 433
		foreach_array_item(p, name->providers)
			p->pkg->ss.dependencies_used = p->pkg->ss.dependencies_merged;

434
		/* propagate down common dependencies */
435 436
		if (num_options == 1) {
			/* FIXME: keeps increasing counts, use bit fields instead? */
437 438 439
			foreach_array_item(dep, pkg->depends)
				if (merge_index_complete(&dep->name->ss.merge_depends, num_options))
					apply_constraint(ss, pkg, dep);
440 441
		} else {
			/* FIXME: could merge versioning bits too */
442
			foreach_array_item(dep, pkg->depends) {
443
				name0 = dep->name;
444 445
				if (merge_index_complete(&name0->ss.merge_depends, num_options) &&
				    name0->ss.requirers == 0) {
446
					/* common dependency name with all */
447 448 449 450
					dbg_printf("%s common dependency: %s\n",
						   name->name, name0->name);
					name0->ss.requirers++;
					name_requirers_changed(ss, name0);
451 452
				}
			}
453
		}
454 455 456 457 458 459 460

		/* provides transitivity */
		if (merge_index_complete(&pkg->name->ss.merge_provides, num_options))
			exclude_non_providers(ss, pkg->name, name);
		foreach_array_item(dep, pkg->provides)
			if (merge_index_complete(&dep->name->ss.merge_provides, num_options))
				exclude_non_providers(ss, dep->name, name);
461
	}
462

463 464 465 466 467 468 469 470 471 472 473
	name->ss.reverse_deps_done = 1;
	foreach_array_item(pname0, name->rdepends) {
		name0 = *pname0;
		if (name0->ss.seen && !name0->ss.locked) {
			name->ss.reverse_deps_done = 0;
			break;
		}
	}

	dbg_printf("reconsider_name: %s [finished], has_options=%d, reverse_deps_done=%d\n",
		name->name, name->ss.has_options, name->ss.reverse_deps_done);
474 475
}

476 477
static int compare_providers(struct apk_solver_state *ss,
			     struct apk_provider *pA, struct apk_provider *pB)
478
{
479 480
	struct apk_database *db = ss->db;
	struct apk_package *pkgA = pA->pkg, *pkgB = pB->pkg;
481
	unsigned int solver_flags;
482
	int r;
483

484 485 486
	/* Prefer existing package */
	if (pkgA == NULL || pkgB == NULL)
		return (pkgA != NULL) - (pkgB != NULL);
487

488 489 490 491 492 493 494 495 496
	/* Latest version required? */
	solver_flags = pkgA->ss.solver_flags | pkgB->ss.solver_flags;
	if ((solver_flags & APK_SOLVERF_LATEST) &&
	    (pkgA->ss.pinning_allowed == APK_DEFAULT_PINNING_MASK) &&
	    (pkgB->ss.pinning_allowed == APK_DEFAULT_PINNING_MASK)) {
		/* Prefer allowed pinning */
		r = (int)pkgA->ss.tag_ok - (int)pkgB->ss.tag_ok;
		if (r)
			return r;
497

498
		/* Prefer available */
499
		if (solver_flags & APK_SOLVERF_AVAILABLE) {
500
			r = (int)pkgA->ss.pkg_available - (int)pkgB->ss.pkg_available;
501 502
			if (r)
				return r;
503 504 505 506
		} else if (solver_flags & APK_SOLVERF_REINSTALL) {
			r = (int)pkgA->ss.pkg_selectable - (int)pkgB->ss.pkg_selectable;
			if (r)
				return r;
507 508 509
		}
	} else {
		/* Prefer without errors */
510
		r = (int)pkgA->ss.pkg_selectable - (int)pkgB->ss.pkg_selectable;
511 512
		if (r)
			return r;
Timo Teräs's avatar
Timo Teräs committed
513

514 515 516 517 518
		/* Prefer those that were in last dependency merging group */
		r = (int)pkgA->ss.dependencies_used - (int)pkgB->ss.dependencies_used;
		if (r)
			return r;
		r = pkgB->ss.conflicts - pkgA->ss.conflicts;
Timo Teräs's avatar
Timo Teräs committed
519 520 521
		if (r)
			return r;

522 523 524 525 526 527
		/* Prefer installed on self-upgrade */
		if (db->performing_self_update && !(solver_flags & APK_SOLVERF_UPGRADE)) {
			r = (pkgA->ipkg != NULL) - (pkgB->ipkg != NULL);
			if (r)
				return r;
		}
528

529 530
		/* Prefer allowed pinning */
		r = (int)pkgA->ss.tag_ok - (int)pkgB->ss.tag_ok;
531 532
		if (r)
			return r;
533

534
		/* Prefer available */
535
		if (solver_flags & APK_SOLVERF_AVAILABLE) {
536
			r = (int)pkgA->ss.pkg_available - (int)pkgB->ss.pkg_available;
537 538
			if (r)
				return r;
539 540 541 542
		} else if (solver_flags & APK_SOLVERF_REINSTALL) {
			r = (int)pkgA->ss.pkg_selectable - (int)pkgB->ss.pkg_selectable;
			if (r)
				return r;
543
		}
544

545 546
		/* Prefer preferred pinning */
		r = (int)pkgA->ss.tag_preferred - (int)pkgB->ss.tag_preferred;
547 548
		if (r)
			return r;
549 550 551 552 553 554 555

		/* Prefer installed */
		if (!(solver_flags & APK_SOLVERF_UPGRADE)) {
			r = (pkgA->ipkg != NULL) - (pkgB->ipkg != NULL);
			if (r)
				return r;
		}
556
	}
557

558 559 560 561 562 563 564
	/* Select latest by requested name */
	switch (apk_version_compare_blob(*pA->version, *pB->version)) {
	case APK_VERSION_LESS:
		return -1;
	case APK_VERSION_GREATER:
		return 1;
	}
565

566 567 568 569 570 571 572 573 574
	/* Select latest by principal name */
	if (pkgA->name == pkgB->name) {
		switch (apk_version_compare_blob(*pkgA->version, *pkgB->version)) {
		case APK_VERSION_LESS:
			return -1;
		case APK_VERSION_GREATER:
			return 1;
		}
	}
Timo Teräs's avatar
Timo Teräs committed
575

576 577 578 579
	/* Prefer installed (matches here if upgrading) */
	r = (pkgA->ipkg != NULL) - (pkgB->ipkg != NULL);
	if (r)
		return r;
580

581
	/* Prefer lowest available repository */
582
	return ffs(pkgB->repos) - ffs(pkgA->repos);
583
}
584

585
static void assign_name(struct apk_solver_state *ss, struct apk_name *name, struct apk_provider p)
586
{
587
	struct apk_provider *p0;
588 589 590 591 592 593

	if (name->ss.locked) {
		/* If both are providing this name without version, it's ok */
		if (p.version == &apk_null_blob &&
		    name->ss.chosen.version == &apk_null_blob)
			return;
594 595 596
		/* Conflict: providing same name */
		mark_error(ss, p.pkg);
		mark_error(ss, name->ss.chosen.pkg);
597
		return;
598
	}
599

600 601
	if (p.pkg)
		dbg_printf("assign %s to "PKG_VER_FMT"\n", name->name, PKG_VER_PRINTF(p.pkg));
602

603 604 605 606 607 608 609 610
	name->ss.locked = 1;
	name->ss.chosen = p;
	if (list_hashed(&name->ss.unresolved_list))
		list_del(&name->ss.unresolved_list);
	if (list_hashed(&name->ss.dirty_list))
		list_del(&name->ss.dirty_list);

	/* disqualify all conflicting packages */
611 612
	foreach_array_item(p0, name->providers) {
		if (p0->pkg == p.pkg)
613 614
			continue;
		if (p.version == &apk_null_blob &&
615
		    p0->version == &apk_null_blob)
616
			continue;
617
		disqualify_package(ss, p0->pkg, "conflicting provides");
618
	}
619
	reevaluate_reverse_deps(ss, name);
620
	reevaluate_reverse_installif(ss, name);
621 622
}

623
static void select_package(struct apk_solver_state *ss, struct apk_name *name)
624
{
625
	struct apk_provider chosen = { NULL, &apk_null_blob }, *p;
626
	struct apk_package *pkg = NULL;
627
	struct apk_dependency *d;
628

629
	dbg_printf("select_package: %s (requirers=%d, iif=%d)\n", name->name, name->ss.requirers, name->ss.has_iif);
630

631
	if (name->ss.requirers || name->ss.has_iif) {
632
		foreach_array_item(p, name->providers) {
633 634
			dbg_printf("  consider "PKG_VER_FMT" iif_triggered=%d, tag_ok=%d\n",
				PKG_VER_PRINTF(p->pkg), p->pkg->ss.iif_triggered, p->pkg->ss.tag_ok);
635
			/* Ensure valid pinning and install-if trigger */
636
			if (name->ss.requirers == 0 &&
637 638
			    (!p->pkg->ss.iif_triggered ||
			     !p->pkg->ss.tag_ok))
639
				continue;
640 641 642
			/* Virtual packages cannot be autoselected */
			if (p->version == &apk_null_blob && p->pkg->name->ss.requirers == 0)
				continue;
643 644
			if (compare_providers(ss, p, &chosen) > 0)
				chosen = *p;
645
		}
646
	}
647

648 649
	pkg = chosen.pkg;
	if (pkg) {
650
		if (!pkg->ss.pkg_selectable || !pkg->ss.tag_ok) {
651 652 653
			/* Selecting broken or unallowed package */
			mark_error(ss, pkg);
		}
654
		dbg_printf("selecting: " PKG_VER_FMT ", available: %d\n", PKG_VER_PRINTF(pkg), pkg->ss.pkg_selectable);
655

656
		assign_name(ss, pkg->name, APK_PROVIDER_FROM_PACKAGE(pkg));
657 658 659 660 661
		foreach_array_item(d, pkg->provides)
			assign_name(ss, d->name, APK_PROVIDER_FROM_PROVIDES(pkg, d));

		foreach_array_item(d, pkg->depends)
			apply_constraint(ss, pkg, d);
662 663 664
	} else {
		dbg_printf("selecting: %s [unassigned]\n", name->name);
		assign_name(ss, name, provider_none);
665
		ss->errors += (name->ss.requirers > 0);
666
	}
667 668
}

669
static void record_change(struct apk_solver_state *ss, struct apk_package *opkg, struct apk_package *npkg)
670
{
671 672 673
	struct apk_changeset *changeset = ss->changeset;
	struct apk_change *change;

674
	change = apk_change_array_add(&changeset->changes);
675 676 677
	*change = (struct apk_change) {
		.old_pkg = opkg,
		.old_repository_tag = opkg ? opkg->ipkg->repository_tag : 0,
678 679 680
		.new_pkg = npkg,
		.new_repository_tag = npkg ? get_tag(ss->db, npkg->ss.pinning_allowed, get_pkg_repos(ss->db, npkg)) : 0,
		.reinstall = npkg ? !!(npkg->ss.solver_flags & APK_SOLVERF_REINSTALL) : 0,
681
	};
682
	if (npkg == NULL)
683
		changeset->num_remove++;
684
	else if (opkg == NULL)
685
		changeset->num_install++;
686
	else if (npkg != opkg || change->reinstall || change->new_repository_tag != change->old_repository_tag)
687
		changeset->num_adjust++;
688
}
689

690 691 692 693
static void cset_gen_name_change(struct apk_solver_state *ss, struct apk_name *name);
static void cset_gen_name_remove(struct apk_solver_state *ss, struct apk_package *pkg);
static void cset_gen_dep(struct apk_solver_state *ss, struct apk_package *ppkg, struct apk_dependency *dep);

694
static void cset_track_deps_added(struct apk_package *pkg)
695 696 697
{
	struct apk_dependency *d;

698 699 700 701 702
	foreach_array_item(d, pkg->depends) {
		if (d->conflict || !d->name->ss.installed_name)
			continue;
		d->name->ss.installed_name->ss.requirers++;
	}
703 704 705 706 707 708 709 710
}

static void cset_track_deps_removed(struct apk_solver_state *ss, struct apk_package *pkg)
{
	struct apk_dependency *d;
	struct apk_package *pkg0;

	foreach_array_item(d, pkg->depends) {
711
		if (d->conflict || !d->name->ss.installed_name)
712
			continue;
713
		if (--d->name->ss.installed_name->ss.requirers > 0)
714
			continue;
715
		pkg0 = d->name->ss.installed_pkg;
716 717 718 719 720 721 722 723 724
		if (pkg0 != NULL)
			cset_gen_name_remove(ss, pkg0);
	}
}

static void cset_check_removal_by_deps(struct apk_solver_state *ss, struct apk_package *pkg)
{
	if (pkg->name->ss.requirers == 0)
		cset_gen_name_remove(ss, pkg);
725 726
}

727
static void cset_check_install_by_iif(struct apk_solver_state *ss, struct apk_name *name)
728
{
729
	struct apk_package *pkg = name->ss.chosen.pkg;
730
	struct apk_dependency *dep0;
731

732
	if (pkg == NULL || !name->ss.seen || name->ss.in_changeset)
733 734
		return;

735
	foreach_array_item(dep0, pkg->install_if) {
736 737 738 739 740
		struct apk_name *name0 = dep0->name;
		if (!name0->ss.in_changeset)
			return;
		if (!apk_dep_is_provided(dep0, &name0->ss.chosen))
			return;
741
	}
742 743 744 745 746
	cset_gen_name_change(ss, name);
}

static void cset_check_removal_by_iif(struct apk_solver_state *ss, struct apk_name *name)
{
747
	struct apk_package *pkg = name->ss.installed_pkg;
748
	struct apk_dependency *dep0;
749

750
	if (pkg == NULL || name->ss.in_changeset || name->ss.chosen.pkg != NULL)
751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
		return;

	foreach_array_item(dep0, pkg->install_if) {
		if (dep0->name->ss.in_changeset &&
		    dep0->name->ss.chosen.pkg == NULL) {
			cset_check_removal_by_deps(ss, pkg);
			return;
		}
	}
}

static void cset_gen_name_change(struct apk_solver_state *ss, struct apk_name *name)
{
	struct apk_name **pname;
	struct apk_package *pkg = name->ss.chosen.pkg, *opkg;
	struct apk_dependency *d;

	if (pkg == NULL || pkg->ss.in_changeset)
		return;

	pkg->ss.in_changeset = 1;
	pkg->name->ss.in_changeset = 1;
773 774
	foreach_array_item(d, pkg->provides)
		d->name->ss.in_changeset = 1;
775

776
	opkg = pkg->name->ss.installed_pkg;
777 778 779 780 781 782 783 784
	if (opkg) {
		foreach_array_item(pname, opkg->name->rinstall_if)
			cset_check_removal_by_iif(ss, *pname);
	}

	foreach_array_item(d, pkg->depends)
		cset_gen_dep(ss, pkg, d);

785
	dbg_printf("Selecting: "PKG_VER_FMT"%s\n", PKG_VER_PRINTF(pkg), pkg->ss.pkg_selectable ? "" : " [NOT SELECTABLE]");
786 787 788 789 790
	record_change(ss, opkg, pkg);

	foreach_array_item(pname, pkg->name->rinstall_if)
		cset_check_install_by_iif(ss, *pname);

791
	cset_track_deps_added(pkg);
792 793 794 795 796 797 798 799
	if (opkg)
		cset_track_deps_removed(ss, opkg);
}

static void cset_gen_name_remove(struct apk_solver_state *ss, struct apk_package *pkg)
{
	struct apk_name *name = pkg->name, **pname;

800 801 802
	if (pkg->ss.in_changeset ||
	    (name->ss.chosen.pkg != NULL &&
	     name->ss.chosen.pkg->name == name))
803 804 805
		return;

	name->ss.in_changeset = 1;
806
	pkg->ss.in_changeset = 1;
807 808 809 810
	foreach_array_item(pname, pkg->name->rinstall_if)
		cset_check_removal_by_iif(ss, *pname);
	record_change(ss, pkg, NULL);
	cset_track_deps_removed(ss, pkg);
811 812
}

813
static void cset_gen_dep(struct apk_solver_state *ss, struct apk_package *ppkg, struct apk_dependency *dep)
814
{
815
	struct apk_name *name = dep->name;
816
	struct apk_package *pkg = name->ss.chosen.pkg;
817

818
	if (!apk_dep_is_provided(dep, &name->ss.chosen))
819
		mark_error(ss, ppkg);
820

821
	cset_gen_name_change(ss, name);
822 823 824

	if (pkg && pkg->ss.error)
		mark_error(ss, ppkg);
825
}
826

827 828 829 830 831 832 833 834 835
static int cset_reset_name(apk_hash_item item, void *ctx)
{
	struct apk_name *name = (struct apk_name *) item;
	name->ss.installed_pkg = NULL;
	name->ss.installed_name = NULL;
	name->ss.requirers = 0;
	return 0;
}

836 837 838
static void generate_changeset(struct apk_solver_state *ss, struct apk_dependency_array *world)
{
	struct apk_changeset *changeset = ss->changeset;
839
	struct apk_package *pkg;
840
	struct apk_installed_package *ipkg;
841
	struct apk_dependency *d;
842

843 844
	apk_change_array_init(&changeset->changes);

845 846 847 848 849 850 851 852 853
	apk_hash_foreach(&ss->db->available.names, cset_reset_name, NULL);
	list_for_each_entry(ipkg, &ss->db->installed.packages, installed_pkgs_list) {
		pkg = ipkg->pkg;
		pkg->name->ss.installed_pkg = pkg;
		pkg->name->ss.installed_name = pkg->name;
		foreach_array_item(d, pkg->provides)
			if (d->version != &apk_null_blob)
				d->name->ss.installed_name = pkg->name;
	}
854
	list_for_each_entry(ipkg, &ss->db->installed.packages, installed_pkgs_list)
855
		cset_track_deps_added(ipkg->pkg);
856 857
	list_for_each_entry(ipkg, &ss->db->installed.packages, installed_pkgs_list)
		cset_check_removal_by_deps(ss, ipkg->pkg);
Timo Teräs's avatar
Timo Teräs committed
858

859
	foreach_array_item(d, world)
860
		cset_gen_dep(ss, NULL, d);
861

862 863
	list_for_each_entry(ipkg, &ss->db->installed.packages, installed_pkgs_list)
		cset_gen_name_remove(ss, ipkg->pkg);
864

865 866 867 868
	changeset->num_total_changes =
		changeset->num_install +
		changeset->num_remove +
		changeset->num_adjust;
869 870
}

871
static int free_name(apk_hash_item item, void *ctx)
872 873 874 875 876
{
	struct apk_name *name = (struct apk_name *) item;
	memset(&name->ss, 0, sizeof(name->ss));
	return 0;
}
Timo Teräs's avatar
Timo Teräs committed
877

878 879 880 881 882
static int free_package(apk_hash_item item, void *ctx)
{
	struct apk_package *pkg = (struct apk_package *) item;
	memset(&pkg->ss, 0, sizeof(pkg->ss));
	return 0;
883 884
}

885 886 887 888
int apk_solver_solve(struct apk_database *db,
		     unsigned short solver_flags,
		     struct apk_dependency_array *world,
		     struct apk_changeset *changeset)
889
{
890
	struct apk_name *name, *name0;
891
	struct apk_package *pkg;
892
	struct apk_solver_state ss_data, *ss = &ss_data;
893
	struct apk_dependency *d;
894

895
restart:
896 897 898
	memset(ss, 0, sizeof(*ss));
	ss->db = db;
	ss->changeset = changeset;
899
	ss->default_repos = apk_db_get_pinning_mask_repos(db, APK_DEFAULT_PINNING_MASK);
900 901
	list_init(&ss->dirty_head);
	list_init(&ss->unresolved_head);
902

903
	dbg_printf("discovering world\n");
904
	ss->solver_flags_inherit = solver_flags;
905
	foreach_array_item(d, world) {
906 907 908 909 910 911 912 913 914
		if (!d->broken)
			discover_name(ss, d->name);
	}
	dbg_printf("applying world\n");
	foreach_array_item(d, world) {
		if (!d->broken) {
			ss->pinning_inherit = BIT(d->repository_tag);
			apply_constraint(ss, NULL, d);
		}
915
	}
916
	ss->solver_flags_inherit = 0;
917
	ss->pinning_inherit = 0;
918
	dbg_printf("applying world [finished]\n");
919

920
	do {
921 922 923
		while (!list_empty(&ss->dirty_head)) {
			name = list_pop(&ss->dirty_head, struct apk_name, ss.dirty_list);
			reconsider_name(ss, name);
924
		}
925

926 927
		name = NULL;
		list_for_each_entry(name0, &ss->unresolved_head, ss.unresolved_list) {
928
			if (name0->ss.reverse_deps_done && name0->ss.requirers && !name0->ss.has_options) {
929
				name = name0;
930
				break;
931 932 933
			}
			if (name == NULL)
				goto prefer;
934
			if ((!!name0->ss.requirers) - (!!name->ss.requirers) < 0)
Timo Teräs's avatar
Timo Teräs committed
935
				continue;
936
			if (name0->ss.max_dep_chain - name->ss.max_dep_chain < 0)
937
				continue;
938 939
		prefer:
			name = name0;
940
		}
941 942
		if (name == NULL)
			break;
943

944 945
		select_package(ss, name);
	} while (1);
946

947
	generate_changeset(ss, world);
948

949
	if (ss->errors && (apk_flags & APK_FORCE)) {
950 951 952
		foreach_array_item(d, world) {
			name = d->name;
			pkg = name->ss.chosen.pkg;