solver.c 34.6 KB
Newer Older
1 2 3
/* solver.c - Alpine Package Keeper (APK)
 * A backtracking, forward checking dependency graph solver.
 *
4
 * Copyright (C) 2008-2011 Timo Teräs <timo.teras@iki.fi>
5 6 7 8 9 10 11 12 13 14 15 16 17
 * 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.
 */

#include <stdlib.h>
#include "apk_defines.h"
#include "apk_database.h"
#include "apk_package.h"
#include "apk_solver.h"

Timo Teräs's avatar
Timo Teräs committed
18 19
#include "apk_print.h"

20 21 22 23 24 25 26 27 28
#if 0
#include <stdio.h>
#define dbg_printf(args...) fprintf(stderr, args)
#else
#define dbg_printf(args...)
#endif

#define APK_PKGSTF_NOINSTALL		0
#define APK_PKGSTF_INSTALL		1
Timo Teräs's avatar
Timo Teräs committed
29 30 31 32 33 34 35 36
#define APK_PKGSTF_ALT_BRANCH		2
#define APK_PKGSTF_INSTALLIF		4
#define APK_PKGSTF_DECIDED		8

struct apk_score {
	unsigned short unsatisfiable;
	unsigned short preference;
};
37 38 39

struct apk_package_state {
	struct apk_package *backtrack;
40
	unsigned int topology_soft;
Timo Teräs's avatar
Timo Teräs committed
41
	struct apk_score saved_score;
42 43 44 45 46 47
	unsigned short flags;
	unsigned short conflicts;
};

struct apk_name_state {
	struct list_head unsolved_list;
Timo Teräs's avatar
Timo Teräs committed
48
	struct apk_name *name;
49 50
	struct apk_package *chosen;
	unsigned short requirers;
51
	unsigned short install_ifs;
Timo Teräs's avatar
Timo Teräs committed
52 53 54 55 56 57 58

	unsigned int solver_flags_local : 4;
	unsigned int solver_flags_local_mask : 4;
	unsigned int solver_flags_inherited : 4;

	unsigned int availability_checked : 1;
	unsigned int locked : 1;
Timo Teräs's avatar
Timo Teräs committed
59
	unsigned int in_changeset : 1;
60 61 62 63
};

struct apk_solver_state {
	struct apk_database *db;
64 65
	unsigned num_topology_positions;

66 67 68 69
	struct list_head unsolved_list_head;
	struct apk_package *latest_decision;
	unsigned int topology_position;
	unsigned int assigned_names;
Timo Teräs's avatar
Timo Teräs committed
70
	struct apk_score score;
71

Timo Teräs's avatar
Timo Teräs committed
72 73
	unsigned int solver_flags : 4;
	unsigned int refresh_name_states : 1;
Timo Teräs's avatar
Timo Teräs committed
74

75
	struct apk_package_array *best_solution;
Timo Teräs's avatar
Timo Teräs committed
76
	struct apk_score best_score;
77 78
};

79 80 81 82
static void apply_constraint(struct apk_solver_state *ss, struct apk_dependency *dep);
static void undo_constraint(struct apk_solver_state *ss, struct apk_dependency *dep);
static void push_decision(struct apk_solver_state *ss, struct apk_package *pkg,
			  int flags);
83

Timo Teräs's avatar
Timo Teräs committed
84
static struct apk_package_state *pkg_to_ps(struct apk_package *pkg)
85 86 87 88
{
	return (struct apk_package_state*) pkg->state_ptr;
}

Timo Teräs's avatar
Timo Teräs committed
89 90
static struct apk_name_state *name_to_ns(struct apk_name *name)
{
Timo Teräs's avatar
Timo Teräs committed
91 92 93 94 95 96 97 98 99 100
	struct apk_name_state *ns;

	if (name->state_ptr == NULL) {
		ns = calloc(1, sizeof(struct apk_name_state));
		ns->name = name;
		name->state_ptr = ns;
	} else {
		ns = (struct apk_name_state*) name->state_ptr;
	}
	return ns;
Timo Teräs's avatar
Timo Teräs committed
101 102
}

103 104 105 106 107 108 109 110 111 112 113
static inline int pkg_available(struct apk_database *db, struct apk_package *pkg)
{
	if (pkg->installed_size == 0)
		return TRUE;
	if (pkg->filename != NULL)
		return TRUE;
	if (apk_db_select_repo(db, pkg) != NULL)
		return TRUE;
	return FALSE;
}

114 115 116
static void foreach_dependency_pkg(
	struct apk_solver_state *ss, struct apk_dependency_array *depends,
	void (*cb)(struct apk_solver_state *ss, struct apk_package *dependency))
117 118 119
{
	int i, j;

120 121 122
	/* And sort the main dependencies */
	for (i = 0; i < depends->num; i++) {
		struct apk_dependency *dep = &depends->item[i];
123 124 125 126
		struct apk_name *name0 = dep->name;

		for (j = 0; j < name0->pkgs->num; j++) {
			struct apk_package *pkg0 = name0->pkgs->item[j];
127

128 129 130 131
			/* conflict depends on all to be not installed */
			if (dep->result_mask != APK_DEPMASK_CONFLICT &&
			    !apk_dep_is_satisfied(dep, pkg0))
				continue;
132 133

			cb(ss, pkg0);
134 135
		}
	}
136
}
137

138 139 140 141 142 143 144 145 146 147 148 149
static void foreach_rinstall_if_pkg(
	struct apk_solver_state *ss, struct apk_package *pkg,
	void (*cb)(struct apk_solver_state *ss, struct apk_package *rinstall_if))
{
	struct apk_name *name = pkg->name;
	int i, j, k;

	for (i = 0; i < pkg->name->rinstall_if->num; i++) {
		struct apk_name *name0 = pkg->name->rinstall_if->item[i];

		dbg_printf(PKG_VER_FMT ": rinstall_if %s\n",
			   PKG_VER_PRINTF(pkg), name0->name);
150

151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
		for (j = 0; j < name0->pkgs->num; j++) {
			struct apk_package *pkg0 = name0->pkgs->item[j];

			for (k = 0; k < pkg0->install_if->num; k++) {
				struct apk_dependency *dep = &pkg0->install_if->item[k];
				if (dep->name == name &&
				    (dep->result_mask == APK_DEPMASK_CONFLICT ||
				     apk_dep_is_satisfied(dep, pkg)))
					break;
			}
			if (k >= pkg0->install_if->num)
				continue;

			/* pkg depends (via install_if) on pkg0 */
			cb(ss, pkg0);
		}
	}
}

static void sort_hard_dependencies(struct apk_solver_state *ss, struct apk_package *pkg)
{
	struct apk_package_state *ps;

	if (pkg->state_ptr == NULL)
		pkg->state_ptr = calloc(1, sizeof(struct apk_package_state));

	ps = pkg_to_ps(pkg);
178
	if (ps->topology_soft)
179
		return;
180
	pkg->topology_hard = -1;
181
	ps->topology_soft = -1;
182 183 184 185 186

	/* Consider hard dependencies only */
	foreach_dependency_pkg(ss, pkg->depends, sort_hard_dependencies);
	foreach_dependency_pkg(ss, pkg->install_if, sort_hard_dependencies);

187
	ps->topology_soft = pkg->topology_hard = ++ss->num_topology_positions;
188
	dbg_printf(PKG_VER_FMT ": topology_hard=%d\n",
189
		   PKG_VER_PRINTF(pkg), pkg->topology_hard);
190 191 192 193 194 195 196 197 198
}

static void sort_soft_dependencies(struct apk_solver_state *ss, struct apk_package *pkg)
{
	struct apk_package_state *ps;

	sort_hard_dependencies(ss, pkg);

	ps = pkg_to_ps(pkg);
199
	if (ps->topology_soft != pkg->topology_hard)
200
		return;
Timo Teräs's avatar
Timo Teräs committed
201
	ps->topology_soft = -1;
202 203 204 205 206 207 208 209 210

	/* Soft reverse dependencies aka. install_if */
	foreach_rinstall_if_pkg(ss, pkg, sort_hard_dependencies);
	foreach_dependency_pkg(ss, pkg->depends, sort_soft_dependencies);

	/* Assign a topology sorting order */
	ps->topology_soft = ++ss->num_topology_positions;
	dbg_printf(PKG_VER_FMT ": topology_soft=%d\n",
		   PKG_VER_PRINTF(pkg), ps->topology_soft);
211 212 213 214 215 216 217
}

static void sort_name(struct apk_solver_state *ss, struct apk_name *name)
{
	int i;

	for (i = 0; i < name->pkgs->num; i++)
218
		sort_soft_dependencies(ss, name->pkgs->item[i]);
219 220
}

221 222 223 224 225
static void prepare_name(struct apk_solver_state *ss, struct apk_name *name,
			 struct apk_name_state *ns)
{
	int i;

Timo Teräs's avatar
Timo Teräs committed
226
	if (ns->availability_checked)
227 228 229 230
		return;

	for (i = 0; i < name->pkgs->num; i++) {
		struct apk_package *pkg = name->pkgs->item[i];
231
		struct apk_package_state *ps = pkg_to_ps(pkg);
Timo Teräs's avatar
Timo Teräs committed
232
		struct apk_name_state *ns = name_to_ns(pkg->name);
233 234

		/* if package is needed for (re-)install */
Timo Teräs's avatar
Timo Teräs committed
235
		if ((pkg->ipkg == NULL) ||
Timo Teräs's avatar
Timo Teräs committed
236 237
		    ((ns->solver_flags_local | ns->solver_flags_inherited |
		      ss->solver_flags) & APK_SOLVERF_REINSTALL)) {
238 239
			/* and it's not available, we can't use it */
			if (!pkg_available(ss->db, pkg))
240
				ps->conflicts = 1024;
241 242 243
		}
	}

Timo Teräs's avatar
Timo Teräs committed
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
	ns->availability_checked = 1;
}

static void foreach_locked_reverse_dependency(
	struct apk_name *name,
	void (*cb)(struct apk_package *rdepend, void *ctx), void *ctx)
{
	int i, j;

	if (name == NULL)
		return;

	for (i = 0; i < name->rdepends->num; i++) {
		struct apk_name *name0 = name->rdepends->item[i];
		struct apk_name_state *ns0 = name_to_ns(name0);
		struct apk_package *pkg0 = ns0->chosen;

		if (!ns0->locked || ns0->chosen == NULL)
			continue;

		for (j = 0; j < pkg0->depends->num; j++) {
			struct apk_dependency *dep = &pkg0->depends->item[j];
			if (dep->name == name)
				break;
		}
		if (j >= pkg0->depends->num)
			continue;

		cb(pkg0, ctx);
	}
274 275
}

276 277
static void foreach_dependency(struct apk_solver_state *ss, struct apk_dependency_array *deps,
			       void (*func)(struct apk_solver_state *ss, struct apk_dependency *dep))
278
{
279
	int i;
280
	for (i = 0; i < deps->num; i++)
281
		func(ss, &deps->item[i]);
282 283
}

Timo Teräs's avatar
Timo Teräs committed
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
static int compare_package_preference(unsigned short solver_flags,
				      struct apk_package *pkgA,
				      struct apk_package *pkgB)
{
	if (solver_flags & APK_SOLVERF_AVAILABLE) {
		if (pkgA->repos != 0 && pkgB->repos == 0)
			return 1;
		if (pkgB->repos != 0 && pkgA->repos == 0)
			return -1;
	}

	if (!(solver_flags & APK_SOLVERF_UPGRADE)) {
		/* not upgrading, prefer the installed package */
		if (pkgA->ipkg != NULL && pkgB->ipkg == NULL)
			return 1;
		if (pkgB->ipkg != NULL && pkgA->ipkg == NULL)
			return -1;
	}

	/* upgrading, or neither of the package is installed, so
	 * we just fall back comparing to versions */
	switch (apk_pkg_version_compare(pkgA, pkgB)) {
	case APK_VERSION_GREATER:
		return 1;
	case APK_VERSION_LESS:
		return -1;
	}

	/* prefer the one with lowest available repository */
	return ffsl(pkgB->repos) - ffsl(pkgA->repos);
}

static int get_preference(struct apk_solver_state *ss,
			  struct apk_package *pkg,
			  int installable_only)
319 320
{
	struct apk_name *name = pkg->name;
321
	struct apk_name_state *ns = name_to_ns(name);
Timo Teräs's avatar
Timo Teräs committed
322 323 324 325 326
	unsigned short name_flags = ns->solver_flags_local
		| ns->solver_flags_inherited
		| ss->solver_flags;
	unsigned short preference = 0;
	int i;
327

328
	for (i = 0; i < name->pkgs->num; i++) {
329
		struct apk_package *pkg0 = name->pkgs->item[i];
330
		struct apk_package_state *ps0 = pkg_to_ps(pkg0);
331

Timo Teräs's avatar
Timo Teräs committed
332
		if (pkg0 == pkg || ps0 == NULL)
333 334
			continue;

Timo Teräs's avatar
Timo Teräs committed
335 336 337 338 339 340 341
		if (compare_package_preference(name_flags, pkg, pkg0) < 0) {
			if (installable_only) {
				if (ss->topology_position > pkg0->topology_hard &&
				    !(ps0->flags & APK_PKGSTF_DECIDED))
					preference++;
			} else
				preference++;
342 343 344
		}
	}

Timo Teräs's avatar
Timo Teräs committed
345
	return preference;
346 347
}

348 349
static int install_if_missing(struct apk_solver_state *ss, struct apk_package *pkg)
{
Timo Teräs's avatar
Timo Teräs committed
350
	struct apk_name_state *ns;
351 352 353 354 355
	int i, missing = 0;

	for (i = 0; i < pkg->install_if->num; i++) {
		struct apk_dependency *dep = &pkg->install_if->item[i];

Timo Teräs's avatar
Timo Teräs committed
356
		ns = name_to_ns(dep->name);
Timo Teräs's avatar
Timo Teräs committed
357
		if (!ns->locked || !apk_dep_is_satisfied(dep, ns->chosen))
358 359 360 361 362 363
			missing++;
	}

	return missing;
}

Timo Teräs's avatar
Timo Teräs committed
364
static int update_name_state(struct apk_solver_state *ss, struct apk_name *name)
365
{
Timo Teräs's avatar
Timo Teräs committed
366
	struct apk_name_state *ns = name_to_ns(name);
367 368
	struct apk_package *best_pkg = NULL;
	unsigned int best_topology = 0;
369
	int i, options = 0, skipped_options = 0;
370 371 372

	for (i = 0; i < name->pkgs->num; i++) {
		struct apk_package *pkg0 = name->pkgs->item[i];
373
		struct apk_package_state *ps0 = pkg_to_ps(pkg0);
374

375
		if (ps0 == NULL ||
376
		    pkg0->topology_hard >= ss->topology_position ||
377
		    (ps0->flags & APK_PKGSTF_DECIDED))
378 379
			continue;

380 381 382 383 384 385
		if (ns->requirers == 0 && ns->install_ifs != 0 &&
		    install_if_missing(ss, pkg0)) {
			skipped_options++;
			continue;
		}

386
		options++;
387 388 389
		if (ps0->topology_soft < ss->topology_position &&
		    ps0->topology_soft > best_topology)
			best_pkg = pkg0, best_topology = ps0->topology_soft;
390 391
		else if (pkg0->topology_hard > best_topology)
			best_pkg = pkg0, best_topology = pkg0->topology_hard;
392
	}
393

394
	if (skipped_options == 0 && options == 0) {
Timo Teräs's avatar
Timo Teräs committed
395 396 397 398
		if (!ns->locked) {
			ss->score.unsatisfiable += ns->requirers;
			ss->score.preference += name->pkgs->num;
		}
399 400 401 402 403 404
		ns->locked = 1;
		ns->chosen = NULL;
	} else {
		ns->locked = 0;
	}

Timo Teräs's avatar
Timo Teräs committed
405
	if (ns->requirers == 0 && ns->install_ifs == 0) {
406 407 408 409 410
		if (list_hashed(&ns->unsolved_list)) {
			list_del(&ns->unsolved_list);
			list_init(&ns->unsolved_list);
			ns->chosen = NULL;
		}
411 412
		dbg_printf("%s: deleted from unsolved: %d requirers, %d install_ifs, %d options, %d skipped\n",
			   name->name, ns->requirers, ns->install_ifs, options, skipped_options);
413
	} else {
414 415 416
		dbg_printf("%s: added to unsolved: %d requirers, %d install_ifs, %d options (next topology %d)\n",
			   name->name, ns->requirers, ns->install_ifs, options,
			   best_topology);
417 418
		if (!list_hashed(&ns->unsolved_list))
			list_add(&ns->unsolved_list, &ss->unsolved_list_head);
419
		ns->chosen = best_pkg;
420 421
	}

422
	return options + skipped_options;
423 424
}

425 426 427 428
static void trigger_install_if(struct apk_solver_state *ss,
			       struct apk_package *pkg)
{
	if (install_if_missing(ss, pkg) == 0) {
Timo Teräs's avatar
Timo Teräs committed
429
		struct apk_name_state *ns = ns = name_to_ns(pkg->name);
430 431 432 433

		dbg_printf("trigger_install_if: " PKG_VER_FMT " triggered\n",
			   PKG_VER_PRINTF(pkg));
		ns->install_ifs++;
Timo Teräs's avatar
Timo Teräs committed
434
		update_name_state(ss, pkg->name);
435 436 437 438 439 440 441
	}
}

static void untrigger_install_if(struct apk_solver_state *ss,
			       struct apk_package *pkg)
{
	if (install_if_missing(ss, pkg) != 1) {
Timo Teräs's avatar
Timo Teräs committed
442
		struct apk_name_state *ns = name_to_ns(pkg->name);
443 444 445 446

		dbg_printf("untrigger_install_if: " PKG_VER_FMT " no longer triggered\n",
			   PKG_VER_PRINTF(pkg));
		ns->install_ifs--;
Timo Teräs's avatar
Timo Teräs committed
447
		update_name_state(ss, pkg->name);
448 449 450
	}
}

451 452 453
static void apply_decision(struct apk_solver_state *ss,
			   struct apk_package *pkg,
			   struct apk_package_state *ps)
454
{
Timo Teräs's avatar
Timo Teräs committed
455
	struct apk_name_state *ns = name_to_ns(pkg->name);
456 457 458 459 460 461

	dbg_printf("apply_decision: " PKG_VER_FMT " %s\n", PKG_VER_PRINTF(pkg),
		   (ps->flags & APK_PKGSTF_INSTALL) ? "INSTALL" : "NO_INSTALL");

	if (ps->flags & APK_PKGSTF_INSTALL) {
		ss->assigned_names++;
Timo Teräs's avatar
Timo Teräs committed
462 463
		ss->score.unsatisfiable += ps->conflicts;
		ss->score.preference += get_preference(ss, pkg, FALSE);
464
		ns->chosen = pkg;
Timo Teräs's avatar
Timo Teräs committed
465
		ns->locked = 1;
466 467 468 469 470 471 472

		list_del(&ns->unsolved_list);
		list_init(&ns->unsolved_list);
		dbg_printf("%s: deleting from unsolved list\n",
			   pkg->name->name);

		foreach_dependency(ss, pkg->depends, apply_constraint);
473
		foreach_rinstall_if_pkg(ss, pkg, trigger_install_if);
474
	} else {
Timo Teräs's avatar
Timo Teräs committed
475
		update_name_state(ss, pkg->name);
476 477 478 479 480 481 482
	}
}

static void undo_decision(struct apk_solver_state *ss,
			  struct apk_package *pkg,
			  struct apk_package_state *ps)
{
Timo Teräs's avatar
Timo Teräs committed
483
	struct apk_name_state *ns = name_to_ns(pkg->name);
484 485 486 487

	dbg_printf("undo_decision: " PKG_VER_FMT " %s\n", PKG_VER_PRINTF(pkg),
		   (ps->flags & APK_PKGSTF_INSTALL) ? "INSTALL" : "NO_INSTALL");

488 489 490
	if (ps->flags & APK_PKGSTF_INSTALLIF)
		ss->topology_position = ps->topology_soft;
	else
491
		ss->topology_position = pkg->topology_hard;
492

493 494
	if (ps->flags & APK_PKGSTF_INSTALL) {
		ss->assigned_names--;
495 496

		foreach_rinstall_if_pkg(ss, pkg, untrigger_install_if);
497 498
		foreach_dependency(ss, pkg->depends, undo_constraint);

Timo Teräs's avatar
Timo Teräs committed
499
		ns->locked = 0;
500
		ns->chosen = NULL;
501
	}
502

Timo Teräs's avatar
Timo Teräs committed
503
	ss->score = ps->saved_score;
Timo Teräs's avatar
Timo Teräs committed
504
	update_name_state(ss, pkg->name);
505 506
}

507 508
static void push_decision(struct apk_solver_state *ss, struct apk_package *pkg,
			  int flags)
509
{
510
	struct apk_package_state *ps = pkg_to_ps(pkg);
511 512

	ps->backtrack = ss->latest_decision;
513
	ps->flags = flags | APK_PKGSTF_DECIDED;
Timo Teräs's avatar
Timo Teräs committed
514
	ps->saved_score = ss->score;
515 516 517 518 519 520 521

	if (ps->topology_soft < ss->topology_position) {
		if (flags & APK_PKGSTF_INSTALL)
			ps->flags |= APK_PKGSTF_INSTALLIF;
		ss->topology_position = ps->topology_soft;
	} else {
		ps->flags &= ~APK_PKGSTF_INSTALLIF;
522
		ss->topology_position = pkg->topology_hard;
523
	}
524
	ss->latest_decision = pkg;
525

Timo Teräs's avatar
Timo Teräs committed
526 527
	dbg_printf("push_decision: adding new BRANCH at topology_position %d\n",
		   ss->topology_position);
528

529 530 531 532
	if (ps->flags & APK_PKGSTF_INSTALLIF)
		dbg_printf("triggers due to " PKG_VER_FMT "\n",
			   PKG_VER_PRINTF(pkg));

533
	apply_decision(ss, pkg, ps);
534 535 536 537 538 539 540
}

static int next_branch(struct apk_solver_state *ss)
{
	struct apk_package *pkg;
	struct apk_package_state *ps;

541
	while (ss->latest_decision != NULL) {
542
		pkg = ss->latest_decision;
543
		ps = pkg_to_ps(pkg);
544 545 546 547 548
		undo_decision(ss, pkg, ps);

		if (ps->flags & APK_PKGSTF_ALT_BRANCH) {
			dbg_printf("next_branch: undo decision at topology_position %d\n",
				   ss->topology_position);
549 550
			ps->flags &= ~(APK_PKGSTF_ALT_BRANCH | APK_PKGSTF_DECIDED);
			ss->latest_decision = ps->backtrack;
Timo Teräs's avatar
Timo Teräs committed
551
			ss->refresh_name_states = 1;
552 553 554 555 556 557 558
		} else {
			dbg_printf("next_branch: swapping BRANCH at topology_position %d\n",
				   ss->topology_position);

			ps->flags |= APK_PKGSTF_ALT_BRANCH;
			ps->flags ^= APK_PKGSTF_INSTALL;

559 560
			apply_decision(ss, pkg, ps);
			return 0;
561 562
		}
	}
563 564 565

	dbg_printf("next_branch: no more branches\n");
	return 1;
566 567
}

Timo Teräs's avatar
Timo Teräs committed
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
static void inherit_name_state(struct apk_name *to, struct apk_name *from)
{
	struct apk_name_state *tns = name_to_ns(to);
	struct apk_name_state *fns = name_to_ns(from);

	tns->solver_flags_inherited |=
		fns->solver_flags_inherited |
		(fns->solver_flags_local & fns->solver_flags_local_mask);
}

static void inherit_name_state_wrapper(struct apk_package *rdepend, void *ctx)
{
	struct apk_name *name = (struct apk_name *) ctx;
	inherit_name_state(name, rdepend->name);
}

static int has_inherited_state(struct apk_name *name)
{
	struct apk_name_state *ns = name_to_ns(name);

	if (name == NULL)
		return 0;
	if (ns->solver_flags_inherited || (ns->solver_flags_local & ns->solver_flags_local_mask))
		return 1;
	return 0;
}

static void recalculate_inherted_name_state(struct apk_name *name)
{
	struct apk_name_state *ns = name_to_ns(name);

	ns->solver_flags_inherited = 0;
	foreach_locked_reverse_dependency(name, inherit_name_state_wrapper, name);
}

603
static void apply_constraint(struct apk_solver_state *ss, struct apk_dependency *dep)
604 605
{
	struct apk_name *name = dep->name;
Timo Teräs's avatar
Timo Teräs committed
606
	struct apk_name_state *ns = name_to_ns(name);
607
	int i;
608

609
	prepare_name(ss, name, ns);
610

Timo Teräs's avatar
Timo Teräs committed
611
	if (ns->locked) {
612 613 614
		dbg_printf(PKG_VER_FMT " selected already for %s\n",
			   PKG_VER_PRINTF(ns->chosen), dep->name->name);
		if (!apk_dep_is_satisfied(dep, ns->chosen))
Timo Teräs's avatar
Timo Teräs committed
615
			ss->score.unsatisfiable++;
616 617 618
		return;
	}

619 620
	for (i = 0; i < name->pkgs->num; i++) {
		struct apk_package *pkg0 = name->pkgs->item[i];
621
		struct apk_package_state *ps0 = pkg_to_ps(pkg0);
622

623
		if (ps0 == NULL ||
624
		    pkg0->topology_hard >= ss->topology_position)
625 626 627 628 629 630 631 632 633 634
			continue;

		if (!apk_dep_is_satisfied(dep, pkg0)) {
			ps0->conflicts++;
			dbg_printf(PKG_VER_FMT ": conflicts++ -> %d\n",
				   PKG_VER_PRINTF(pkg0),
				   ps0->conflicts);
		}
	}

Timo Teräs's avatar
Timo Teräs committed
635 636 637
	if (ss->latest_decision != NULL)
		inherit_name_state(name, ss->latest_decision->name);

Timo Teräs's avatar
Timo Teräs committed
638 639 640
	if (dep->result_mask != APK_DEPMASK_CONFLICT)
		ns->requirers++;
	update_name_state(ss, name);
641 642
}

643
static void undo_constraint(struct apk_solver_state *ss, struct apk_dependency *dep)
644 645
{
	struct apk_name *name = dep->name;
Timo Teräs's avatar
Timo Teräs committed
646
	struct apk_name_state *ns = name_to_ns(name);
647 648
	int i;

Timo Teräs's avatar
Timo Teräs committed
649
	if (ns->locked) {
650 651 652 653 654 655 656
		if (ns->chosen != NULL) {
			dbg_printf(PKG_VER_FMT " selected already for %s\n",
				   PKG_VER_PRINTF(ns->chosen), dep->name->name);
		} else {
			dbg_printf("%s selected to not be satisfied\n",
				   dep->name->name);
		}
657 658
		return;
	}
659 660 661

	for (i = 0; i < name->pkgs->num; i++) {
		struct apk_package *pkg0 = name->pkgs->item[i];
662
		struct apk_package_state *ps0 = pkg_to_ps(pkg0);
663

664
		if (pkg0->topology_hard >= ss->topology_position)
665 666 667 668 669 670 671 672 673 674
			continue;

		if (!apk_dep_is_satisfied(dep, pkg0)) {
			ps0->conflicts--;
			dbg_printf(PKG_VER_FMT ": conflicts-- -> %d\n",
				   PKG_VER_PRINTF(pkg0),
				   ps0->conflicts);
		}
	}

Timo Teräs's avatar
Timo Teräs committed
675 676 677
	if (ss->latest_decision && has_inherited_state(ss->latest_decision->name))
		recalculate_inherted_name_state(name);

Timo Teräs's avatar
Timo Teräs committed
678 679 680
	if (dep->result_mask != APK_DEPMASK_CONFLICT)
		ns->requirers--;
	update_name_state(ss, name);
681 682 683 684
}

static int expand_branch(struct apk_solver_state *ss)
{
685 686
	struct apk_name_state *ns;
	struct apk_package *pkg0 = NULL;
687
	unsigned int topology0 = 0;
Timo Teräs's avatar
Timo Teräs committed
688
	int flags;
689 690 691

	/* FIXME: change unsolved_list to a priority queue */
	list_for_each_entry(ns, &ss->unsolved_list_head, unsolved_list) {
Timo Teräs's avatar
Timo Teräs committed
692 693 694
		if (ss->refresh_name_states)
			update_name_state(ss, ns->name);

695 696 697 698 699 700 701 702 703
		/* ns->chosen can be NULL if the name has only install_if
		 * requirers that got later conflicted, but it still has
		 * other options that can get activated later due to more
		 * complicated install_if rules in some other package. */
		if (ns->chosen == NULL)
			continue;
		if (pkg_to_ps(ns->chosen)->topology_soft < ss->topology_position &&
		    pkg_to_ps(ns->chosen)->topology_soft > topology0)
			pkg0 = ns->chosen, topology0 = pkg_to_ps(pkg0)->topology_soft;
704 705
		else if (ns->chosen->topology_hard > topology0)
			pkg0 = ns->chosen, topology0 = pkg0->topology_hard;
706
	}
Timo Teräs's avatar
Timo Teräs committed
707
	ss->refresh_name_states = 0;
708 709
	if (pkg0 == NULL) {
		dbg_printf("expand_branch: list is empty (%d unsatisfied)\n",
Timo Teräs's avatar
Timo Teräs committed
710
			   ss->score.unsatisfiable);
711 712
		return 1;
	}
713

714 715
	/* someone needs to provide this name -- find next eligible
	 * provider candidate */
Timo Teräs's avatar
Timo Teräs committed
716
	ns = name_to_ns(pkg0->name);
717
	dbg_printf("expand_branch: %s\n", pkg0->name->name);
718

Timo Teräs's avatar
Timo Teräs committed
719 720 721 722 723
	if (get_preference(ss, pkg0, TRUE) == 0)
		flags = APK_PKGSTF_INSTALL;
	else
		flags = APK_PKGSTF_NOINSTALL;
	push_decision(ss, pkg0, flags);
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738

	return 0;
}

static void record_solution(struct apk_solver_state *ss)
{
	struct apk_package *pkg;
	struct apk_package_state *ps;
	int i;

	apk_package_array_resize(&ss->best_solution, ss->assigned_names);

	i = 0;
	pkg = ss->latest_decision;
	while (pkg != NULL) {
739
		ps = pkg_to_ps(pkg);
740 741 742
		if (ps->flags & APK_PKGSTF_INSTALL) {
			if (i >= ss->assigned_names)
				abort();
743
			ss->best_solution->item[i++] = pkg;
744
		}
745 746 747 748 749 750 751

		dbg_printf("record_solution: " PKG_VER_FMT ": %sINSTALL\n",
			   PKG_VER_PRINTF(pkg),
			   (ps->flags & APK_PKGSTF_INSTALL) ? "" : "NO_");

		pkg = ps->backtrack;
	}
752
	apk_package_array_resize(&ss->best_solution, i);
Timo Teräs's avatar
Timo Teräs committed
753
	ss->best_score = ss->score;
754 755 756 757 758 759 760 761
}

static int compare_package_name(const void *p1, const void *p2)
{
	const struct apk_package **c1 = (const struct apk_package **) p1;
	const struct apk_package **c2 = (const struct apk_package **) p2;

	return strcmp((*c1)->name->name, (*c2)->name->name);
762 763
}

Timo Teräs's avatar
Timo Teräs committed
764 765 766 767 768 769 770 771
static int compare_change(const void *p1, const void *p2)
{
	const struct apk_change *c1 = (const struct apk_change *) p1;
	const struct apk_change *c2 = (const struct apk_change *) p2;

	if (c1->newpkg == NULL) {
		if (c2->newpkg == NULL)
			/* both deleted - reverse topology order */
772 773
			return  c2->oldpkg->topology_hard -
				c1->oldpkg->topology_hard;
Timo Teräs's avatar
Timo Teräs committed
774 775 776 777 778 779 780 781

		/* c1 deleted, c2 installed -> c2 first*/
		return -1;
	}
	if (c2->newpkg == NULL)
		/* c1 installed, c2 deleted -> c1 first*/
		return 1;

782 783
	return  c1->newpkg->topology_hard -
		c2->newpkg->topology_hard;
Timo Teräs's avatar
Timo Teräs committed
784 785 786 787
}

static int generate_changeset(struct apk_database *db,
			      struct apk_package_array *solution,
788 789
			      struct apk_changeset *changeset,
			      unsigned short solver_flags)
Timo Teräs's avatar
Timo Teräs committed
790 791 792 793 794 795 796 797 798 799
{
	struct apk_name *name;
	struct apk_name_state *ns;
	struct apk_package *pkg, *pkg0;
	struct apk_installed_package *ipkg;
	int i, j, num_installs = 0, num_removed = 0, ci = 0;

	/* calculate change set size */
	for (i = 0; i < solution->num; i++) {
		pkg = solution->item[i];
Timo Teräs's avatar
Timo Teräs committed
800
		ns = name_to_ns(pkg->name);
Timo Teräs's avatar
Timo Teräs committed
801 802
		ns->chosen = pkg;
		ns->in_changeset = 1;
Timo Teräs's avatar
Timo Teräs committed
803
		if ((pkg->ipkg == NULL) ||
Timo Teräs's avatar
Timo Teräs committed
804 805
		    ((ns->solver_flags_local | ns->solver_flags_inherited |
		      solver_flags) & APK_SOLVERF_REINSTALL))
Timo Teräs's avatar
Timo Teräs committed
806 807 808 809 810
			num_installs++;
	}
	list_for_each_entry(ipkg, &db->installed.packages, installed_pkgs_list) {
		name = ipkg->pkg->name;
		ns = name_to_ns(name);
Timo Teräs's avatar
Timo Teräs committed
811
		if ((ns->chosen == NULL) || !ns->in_changeset)
Timo Teräs's avatar
Timo Teräs committed
812 813 814 815 816 817 818 819
			num_removed++;
	}

	/* construct changeset */
	apk_change_array_resize(&changeset->changes, num_installs + num_removed);
	list_for_each_entry(ipkg, &db->installed.packages, installed_pkgs_list) {
		name = ipkg->pkg->name;
		ns = name_to_ns(name);
Timo Teräs's avatar
Timo Teräs committed
820
		if ((ns->chosen == NULL) || !ns->in_changeset) {
Timo Teräs's avatar
Timo Teräs committed
821 822 823 824 825 826 827
			changeset->changes->item[ci].oldpkg = ipkg->pkg;
			ci++;
		}
	}
	for (i = 0; i < solution->num; i++) {
		pkg = solution->item[i];
		name = pkg->name;
Timo Teräs's avatar
Timo Teräs committed
828
		ns = name_to_ns(name);
Timo Teräs's avatar
Timo Teräs committed
829 830

		if ((pkg->ipkg == NULL) ||
Timo Teräs's avatar
Timo Teräs committed
831 832
		    ((ns->solver_flags_local | ns->solver_flags_inherited |
		      solver_flags) & APK_SOLVERF_REINSTALL)) {
Timo Teräs's avatar
Timo Teräs committed
833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862
			for (j = 0; j < name->pkgs->num; j++) {
				pkg0 = name->pkgs->item[j];
				if (pkg0->ipkg == NULL)
					continue;
				changeset->changes->item[ci].oldpkg = pkg0;
				break;
			}
			changeset->changes->item[ci].newpkg = pkg;
			ci++;
		}
	}

	/* sort changeset to topology order */
	qsort(changeset->changes->item, changeset->changes->num,
	      sizeof(struct apk_change), compare_change);

	return 0;
}

static int free_state(apk_hash_item item, void *ctx)
{
	struct apk_name *name = (struct apk_name *) item;

	if (name->state_ptr != NULL) {
		free(name->state_ptr);
		name->state_ptr = NULL;
	}
	return 0;
}

863 864 865 866 867 868 869 870 871 872 873
static int free_package(apk_hash_item item, void *ctx)
{
	struct apk_package *pkg = (struct apk_package *) item;

	if (pkg->state_ptr != NULL) {
		free(pkg->state_ptr);
		pkg->state_ptr = NULL;
	}
	return 0;
}

874
void apk_solver_set_name_flags(struct apk_name *name,
Timo Teräs's avatar
Timo Teräs committed
875 876
			       unsigned short solver_flags,
			       unsigned short solver_flags_inheritable)
877 878
{
	struct apk_name_state *ns = name_to_ns(name);
Timo Teräs's avatar
Timo Teräs committed
879 880
	ns->solver_flags_local = solver_flags;
	ns->solver_flags_local_mask = solver_flags_inheritable;
881 882
}

883 884 885 886 887 888
static void apk_solver_free(struct apk_database *db)
{
	apk_hash_foreach(&db->available.names, free_state, NULL);
	apk_hash_foreach(&db->available.packages, free_package, NULL);
}

Timo Teräs's avatar
Timo Teräs committed
889 890 891 892 893 894 895 896 897 898 899 900 901
static int cmpscore(struct apk_score *a, struct apk_score *b)
{
	if (a->unsatisfiable < b->unsatisfiable)
		return -1;
	if (a->unsatisfiable > b->unsatisfiable)
		return 1;
	if (a->preference < b->preference)
		return -1;
	if (a->preference > b->preference)
		return 1;
	return 0;
}

Timo Teräs's avatar
Timo Teräs committed
902 903 904 905 906
int apk_solver_solve(struct apk_database *db,
		     unsigned short solver_flags,
		     struct apk_dependency_array *world,
		     struct apk_package_array **solution,
		     struct apk_changeset *changeset)
907 908
{
	struct apk_solver_state *ss;
Timo Teräs's avatar
Timo Teräs committed
909
	struct apk_installed_package *ipkg;
910
	int i, r;
911 912 913

	ss = calloc(1, sizeof(struct apk_solver_state));
	ss->db = db;
Timo Teräs's avatar
Timo Teräs committed
914
	ss->solver_flags = solver_flags;
915
	ss->topology_position = -1;
Timo Teräs's avatar
Timo Teräs committed
916
	ss->best_score = (struct apk_score){ .unsatisfiable = -1, .preference = -1 };
917
	list_init(&ss->unsolved_list_head);
918 919 920

	for (i = 0; i < world->num; i++)
		sort_name(ss, world->item[i].name);
Timo Teräs's avatar
Timo Teräs committed
921 922
	list_for_each_entry(ipkg, &db->installed.packages, installed_pkgs_list)
		sort_name(ss, ipkg->pkg->name);
923

924 925
	foreach_dependency(ss, world, apply_constraint);
	do {
Timo Teräs's avatar
Timo Teräs committed
926
		if (cmpscore(&ss->score, &ss->best_score) < 0) {
927 928
			r = expand_branch(ss);
			if (r) {
Timo Teräs's avatar
Timo Teräs committed
929 930 931 932 933 934 935 936 937
				dbg_printf("solution with: %d unsatisfiable, %d preference\n",
					   ss->score.unsatisfiable,
					   ss->score.preference);

				if (cmpscore(&ss->score, &ss->best_score) < 0)
					record_solution(ss);

				if (ss->score.unsatisfiable == 0 &&
				    ss->score.preference == 0) {
938 939 940 941 942 943 944 945 946 947
					/* found solution - it is optimal because we permutate
					 * each preferred local option first, and permutations
					 * happen in topologally sorted order. */
					r = 0;
					break;
				}
				r = next_branch(ss);
			}
		} else {
			r = next_branch(ss);
948
		}
949
	} while (r == 0);
950 951

	/* collect packages */
Timo Teräs's avatar
Timo Teräs committed
952
	if (ss->best_score.unsatisfiable == 0) {
Timo Teräs's avatar
Timo Teräs committed
953
		if (changeset != NULL)
954 955
			generate_changeset(db, ss->best_solution, changeset,
					   ss->solver_flags);
956
		r = 0;
Timo Teräs's avatar
Timo Teräs committed
957
	} else {
958 959
		qsort(ss->best_solution->item, ss->best_solution->num,
		      sizeof(struct apk_package *), compare_package_name);
Timo Teräs's avatar
Timo Teräs committed
960
		r = ss->best_score.unsatisfiable;
Timo Teräs's avatar
Timo Teräs committed
961 962 963 964 965 966
	}

	if (solution != NULL)
		*solution = ss->best_solution;
	else
		apk_package_array_free(&ss->best_solution);
967

968
	apk_solver_free(db);
969 970 971 972 973
	free(ss);

	return r;
}

Timo Teräs's avatar
Timo Teräs committed
974 975 976
static void print_change(struct apk_database *db,
			 struct apk_change *change,
			 int cur, int total)
977
{
Timo Teräs's avatar
Timo Teräs committed
978 979 980 981 982 983
	struct apk_name *name;
	struct apk_package *oldpkg = change->oldpkg;
	struct apk_package *newpkg = change->newpkg;
	const char *msg = NULL;
	char status[64];
	int r;
984

Timo Teräs's avatar
Timo Teräs committed
985 986
	snprintf(status, sizeof(status), "(%i/%i)", cur+1, total);
	status[sizeof(status) - 1] = '\0';
987

Timo Teräs's avatar
Timo Teräs committed
988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
	if (oldpkg != NULL)
		name = oldpkg->name;
	else
		name = newpkg->name;

	if (oldpkg == NULL) {
		apk_message("%s Installing %s (" BLOB_FMT ")",
			    status, name->name,
			    BLOB_PRINTF(*newpkg->version));
	} else if (newpkg == NULL) {
		apk_message("%s Purging %s (" BLOB_FMT ")",
			    status, name->name,
			    BLOB_PRINTF(*oldpkg->version));
	} else {
		r = apk_pkg_version_compare(newpkg, oldpkg);
		switch (r) {
		case APK_VERSION_LESS:
			msg = "Downgrading";
			break;
		case APK_VERSION_EQUAL:
			if (newpkg == oldpkg)
				msg = "Re-installing";
			else
				msg = "Replacing";
			break;
		case APK_VERSION_GREATER:
			msg = "Upgrading";
			break;
		}
		apk_message("%s %s %s (" BLOB_FMT " -> " BLOB_FMT ")",
			    status, msg, name->name,
			    BLOB_PRINTF(*oldpkg->version),
			    BLOB_PRINTF(*newpkg->version));
1021
	}
Timo Teräs's avatar
Timo Teräs committed
1022
}
1023

Timo Teräs's avatar
Timo Teräs committed
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
struct apk_stats {
	unsigned int bytes;
	unsigned int packages;
};

static void count_change(struct apk_change *change, struct apk_stats *stats)
{
	if (change->newpkg != NULL) {
		stats->bytes += change->newpkg->installed_size;
		stats->packages ++;
	}
	if (change->oldpkg != NULL)
		stats->packages ++;
}

static void draw_progress(int percent)
{
	const int bar_width = apk_get_screen_width() - 7;
	int i;

	fprintf(stderr, "\e7%3i%% [", percent);
	for (i = 0; i < bar_width * percent / 100; i++)
		fputc('#', stderr);
	for (; i < bar_width; i++)
		fputc(' ', stderr);
	fputc(']', stderr);
	fflush(stderr);
	fputs("\e8\e[0K", stderr);
1052 1053
}

Timo Teräs's avatar
Timo Teräs committed
1054 1055 1056 1057 1058 1059 1060 1061
struct progress {
	struct apk_stats done;
	struct apk_stats total;
	struct apk_package *pkg;
	size_t count;
};

static void progress_cb(void *ctx, size_t progress)
1062
{
Timo Teräs's avatar
Timo Teräs committed
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
	struct progress *prog = (struct progress *) ctx;
	size_t partial = 0, count;

	if (prog->pkg != NULL)
		partial = muldiv(progress, prog->pkg->installed_size, APK_PROGRESS_SCALE);

        count = muldiv(100, prog->done.bytes + prog->done.packages + partial,
		       prog->total.bytes + prog->total.packages);

	if (prog->count != count)
		draw_progress(count);
	prog->count = count;
}

static int dump_packages(struct apk_changeset *changeset,
			 int (*cmp)(struct apk_change *change),
			 const char *msg)
{
	struct apk_change *change;
1082
	struct apk_name *name;
1083
	struct apk_indent indent = { .indent = 2 };
Timo Teräs's avatar
Timo Teräs committed
1084
	int match = 0, i;
1085

Timo Teräs's avatar
Timo Teräs committed
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
	for (i = 0; i < changeset->changes->num; i++) {
		change = &changeset->changes->item[i];
		if (!cmp(change))
			continue;
		if (match == 0)
			printf("%s:\n ", msg);
		if (change->newpkg != NULL)
			name = change->newpkg->name;
		else
			name = change->oldpkg->name;

		apk_print_indented(&indent, APK_BLOB_STR(name->name));
		match++;
1099
	}
Timo Teräs's avatar
Timo Teräs committed
1100 1101 1102 1103
	if (match)
		printf("\n");
	return match;
}
1104

Timo Teräs's avatar
Timo Teräs committed
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
static int cmp_remove(struct apk_change *change)
{
	return change->newpkg == NULL;
}

static int cmp_new(struct apk_change *change)
{
	return change->oldpkg == NULL;
}

static int cmp_downgrade(struct apk_change *change)
{
	if (change->newpkg == NULL || change->oldpkg == NULL)
		return 0;
	if (apk_pkg_version_compare(change->newpkg, change->oldpkg)
	    & APK_VERSION_LESS)
		return 1;
	return 0;
}

static int cmp_upgrade(struct apk_change *change)
{
	if (change->newpkg == NULL || change->oldpkg == NULL)
		return 0;

	/* Count swapping package as upgrade too - this can happen if
	 * same package version is used after it was rebuilt against
	 * newer libraries. Basically, different (and probably newer)
	 * package, but equal version number. */
	if ((apk_pkg_version_compare(change->newpkg, change->oldpkg) &
	     (APK_VERSION_GREATER | APK_VERSION_EQUAL)) &&
	    (change->newpkg != change->oldpkg))
		return 1;

	return 0;
}

Timo Teräs's avatar
Timo Teräs committed
1142
static int compare_package_topology(const void *p1, const void *p2)
1143 1144 1145 1146
{
	struct apk_package *pkg1 = *(struct apk_package **) p1;
	struct apk_package *pkg2 = *(struct apk_package **) p2;

1147
	return pkg1->topology_hard - pkg2->topology_hard;
1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
}

static void run_triggers(struct apk_database *db)
{
	struct apk_package_array *pkgs;
	int i;

	pkgs = apk_db_get_pending_triggers(db);
	if (pkgs == NULL || pkgs->num == 0)
		return;

	qsort(pkgs->item, pkgs->num, sizeof(struct apk_package *),
Timo Teräs's avatar
Timo Teräs committed
1160
	      compare_package_topology);
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173

	for (i = 0; i < pkgs->num; i++) {
		struct apk_package *pkg = pkgs->item[i];
		struct apk_installed_package *ipkg = pkg->ipkg;

		*apk_string_array_add(&ipkg->pending_triggers) = NULL;
		apk_ipkg_run_script(ipkg, db, APK_SCRIPT_TRIGGER,
				    ipkg->pending_triggers->item);
		apk_string_array_free(&ipkg->pending_triggers);
	}
	apk_package_array_free(&pkgs);
}

1174 1175 1176
int apk_solver_commit_changeset(struct apk_database *db,
				struct apk_changeset *changeset,
				struct apk_dependency_array *world)
Timo Teräs's avatar
Timo Teräs committed
1177 1178 1179 1180 1181
{
	struct progress prog;
	struct apk_change *change;
	int i, r = 0, size_diff = 0;

1182 1183 1184
	if (changeset->changes == NULL)
		goto all_done;

Timo Teräs's avatar
Timo Teräs committed
1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218
	/* Count what needs to be done */
	memset(&prog, 0, sizeof(prog));
	for (i = 0; i < changeset->changes->num; i++) {
		change = &changeset->changes->item[i];
		count_change(change, &prog.total);
		if (change->newpkg)
			size_diff += change->newpkg->installed_size;
		if (change->oldpkg)
			size_diff -= change->oldpkg->installed_size;
	}
	size_diff /= 1024;

	if (apk_verbosity > 1 || (apk_flags & APK_INTERACTIVE)) {
		r = dump_packages(changeset, cmp_remove,
				  "The following packages will be REMOVED");
		r += dump_packages(changeset, cmp_downgrade,
				   "The following packages will be DOWNGRADED");
		if (r || (apk_flags & APK_INTERACTIVE) || apk_verbosity > 2) {
			dump_packages(changeset, cmp_new,
				      "The following NEW packages will be installed");
			dump_packages(changeset, cmp_upgrade,
				      "The following packages will be upgraded");
			printf("After this operation, %d kB of %s\n", abs(size_diff),
			       (size_diff < 0) ?
			       "disk space will be freed." :
			       "additional disk space will be used.");
		}
		if (apk_flags & APK_INTERACTIVE) {
			printf("Do you want to continue [Y/n]? ");
			fflush(stdout);
			r = fgetc(stdin);
			if (r != 'y' && r != 'Y' && r != '\n')
				return -1;
		}
1219 1220
	}

Timo Teräs's avatar
Timo Teräs committed
1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244
	/* Go through changes */
	r = 0;
	for (i = 0; i < changeset->changes->num; i++) {
		change = &changeset->changes->item[i];

		print_change(db, change, i, changeset->changes->num);
		if (apk_flags & APK_PROGRESS)
			draw_progress(prog.count);
		prog.pkg = change->newpkg;

		if (!(apk_flags & APK_SIMULATE)) {
			r = apk_db_install_pkg(db,
					       change->oldpkg, change->newpkg,
					       (apk_flags & APK_PROGRESS) ? progress_cb : NULL,
					       &prog);
			if (r != 0)
				break;
		}

		count_change(change, &prog.done);
	}
	if (apk_flags & APK_PROGRESS)
		draw_progress(100);

1245
	run_triggers(db);
Timo Teräs's avatar
Timo Teräs committed
1246

1247
all_done:
Timo Teräs's avatar
Timo Teräs committed
1248 1249 1250
	apk_dependency_array_copy(&db->world, world);
	apk_db_write_config(db);

1251
	if (r == 0 && !db->performing_self_update) {
Timo Teräs's avatar
Timo Teräs committed
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272
		apk_message("OK: %d packages, %d dirs, %d files",
			    db->installed.stats.packages,
			    db->installed.stats.dirs,
			    db->installed.stats.files);
	}

	return r;
}

static void print_dep_errors(char *label, struct apk_dependency_array *deps)
{
	int i, print_label = 1;
	char buf[256];
	apk_blob_t p;
	struct apk_indent indent;

	for (i = 0; i < deps->num; i++) {
		struct apk_dependency *dep = &deps->item[i];
		struct apk_package *pkg = (struct apk_package*) dep->name->state_ptr;

		if (pkg != NULL && apk_dep_is_satisfied(dep, pkg))
1273
			continue;
Timo Teräs's avatar
Timo Teräs committed
1274 1275 1276 1277 1278 1279 1280 1281 1282 1283

		if (print_label) {
			print_label = 0;
			indent.x = printf("  %s:", label);
			indent.indent = indent.x + 1;
		}
		p = APK_BLOB_BUF(buf);
		apk_blob_push_dep(&p, dep);
		p = apk_blob_pushed(APK_BLOB_BUF(buf), p);
		apk_print_indented(&indent, p);
1284
	}
Timo Teräs's avatar
Timo Teräs committed
1285 1286 1287 1288
	if (!print_label)
		printf("\n");
}

1289 1290 1291 1292
void apk_solver_print_errors(struct apk_database *db,
			     struct apk_package_array *solution,
			     struct apk_dependency_array *world,
			     int unsatisfiable)
Timo Teräs's avatar
Timo Teräs committed
1293 1294 1295 1296 1297
{
	int i;

	apk_error("%d unsatisfiable dependencies:", unsatisfiable);

1298
	for (i = 0; i < solution->num; i++) {
Timo Teräs's avatar
Timo Teräs committed
1299 1300 1301
		struct apk_package *pkg = solution->item[i];
		pkg->name->state_ptr = pkg;
	}
1302

Timo Teräs's avatar
Timo Teräs committed
1303 1304 1305 1306 1307 1308
	print_dep_errors("world", world);
	for (i = 0; i < solution->num; i++) {
		struct apk_package *pkg = solution->item[i];
		char pkgtext[256];
		snprintf(pkgtext, sizeof(pkgtext), PKG_VER_FMT, PKG_VER_PRINTF(solution->item[i]));
		print_dep_errors(pkgtext, pkg->depends);
1309
	}
Timo Teräs's avatar
Timo Teräs committed
1310
}
1311

Timo Teräs's avatar
Timo Teräs committed
1312 1313 1314 1315 1316 1317 1318
int apk_solver_commit(struct apk_database *db,
		      unsigned short solver_flags,
		      struct apk_dependency_array *world)
{
	struct apk_changeset changeset = {};
	struct apk_package_array *solution = NULL;
	int r;
1319

1320
	r = apk_solver_solve(db, solver_flags,
1321
			     world, &solution, &changeset);
Timo Teräs's avatar
Timo Teräs committed
1322 1323 1324 1325 1326
	if (r < 0)
		return r;

	if (r == 0 || (apk_flags & APK_FORCE)) {
		/* Success -- or forced installation of bad graph */
1327
		r = apk_solver_commit_changeset(db, &changeset, world);
Timo Teräs's avatar
Timo Teräs committed
1328 1329
	} else {
		/* Failure -- print errors */
1330
		apk_solver_print_errors(db, solution, world, r);
Timo Teräs's avatar
Timo Teräs committed
1331 1332 1333 1334
	}
	apk_package_array_free(&solution);

	return r;
1335
}