summaryrefslogtreecommitdiffstats
path: root/lib/libalpm/deps.c
blob: cf7605eee7b0e0d2f8ece31854f57f95a0c4cbfd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
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
274
275
276
277
278
279
280
281
282
283
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
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
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
/*
 *  deps.c
 *
 *  Copyright (c) 2006-2013 Pacman Development Team <pacman-dev@archlinux.org>
 *  Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org>
 *  Copyright (c) 2005 by Aurelien Foret <orelien@chez.com>
 *  Copyright (c) 2005, 2006 by Miklos Vajna <vmiklos@frugalware.org>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

/* libalpm */
#include "deps.h"
#include "alpm_list.h"
#include "util.h"
#include "log.h"
#include "graph.h"
#include "package.h"
#include "db.h"
#include "handle.h"
#include "trans.h"

void _alpm_dep_free(alpm_depend_t *dep)
{
	FREE(dep->name);
	FREE(dep->version);
	FREE(dep->desc);
	FREE(dep);
}

static alpm_depmissing_t *depmiss_new(const char *target, alpm_depend_t *dep,
		const char *causingpkg)
{
	alpm_depmissing_t *miss;

	MALLOC(miss, sizeof(alpm_depmissing_t), return NULL);

	STRDUP(miss->target, target, return NULL);
	miss->depend = _alpm_dep_dup(dep);
	STRDUP(miss->causingpkg, causingpkg, return NULL);

	return miss;
}

void _alpm_depmiss_free(alpm_depmissing_t *miss)
{
	_alpm_dep_free(miss->depend);
	FREE(miss->target);
	FREE(miss->causingpkg);
	FREE(miss);
}

/** Check if pkg2 satisfies a dependency of pkg1 */
static int _alpm_pkg_depends_on(alpm_pkg_t *pkg1, alpm_pkg_t *pkg2)
{
	alpm_list_t *i;
	for(i = alpm_pkg_get_depends(pkg1); i; i = i->next) {
		if(_alpm_depcmp(pkg2, i->data)) {
			return 1;
		}
	}
	return 0;
}

static alpm_pkg_t *find_dep_satisfier(alpm_list_t *pkgs, alpm_depend_t *dep)
{
	alpm_list_t *i;

	for(i = pkgs; i; i = i->next) {
		alpm_pkg_t *pkg = i->data;
		if(_alpm_depcmp(pkg, dep)) {
			return pkg;
		}
	}
	return NULL;
}

/** Check if pkg2 is anywhere in pkg1's dependency tree.
 * @param pkg1
 * @param pkg2
 * @param targets if a package in this list is an intermediate dependency
 *        between pkg1 and pkg2, the pkg1 -> pkg2 dependency will not be
 *        reported
 * @param ignore packages which should not be recursively checked for
 *        intermediate dependencies.  Used internally for state to avoid
 *        getting stuck on cyclic dependencies.
 * @return 1 if pkg2 is found in pkg1's dependency tree
 */
static int _alpm_pkg_in_dep_tree(alpm_pkg_t *pkg1, alpm_pkg_t *pkg2,
		alpm_list_t *targets, alpm_list_t **ignore)
{
	alpm_list_t *i, *pkgs = alpm_db_get_pkgcache(pkg1->handle->db_local);

	if(_alpm_pkg_depends_on(pkg1, pkg2)) {
		return 1;
	}

	*ignore = alpm_list_add(*ignore, pkg1);

	 /* pkg1 does not directly depend on pkg2, but if this is an upgrade
		* operation there may be an indirect dependency through an installed
		* dependency not part of the current transaction */
	for(i = alpm_pkg_get_depends(pkg1); i; i = i->next) {
		alpm_depend_t *dep = i->data;
		alpm_pkg_t *lpkg = find_dep_satisfier(pkgs, dep);

		if(!lpkg) {
			continue;
		} else if(alpm_list_find(targets, lpkg, _alpm_pkg_cmp)) {
			/* lpkg's upgrade is part of the transaction, any dependency will be
			 * detected separately as pkg1 -> lpkg and lpkg -> pkg2 */
			continue;
		} else if(alpm_list_find(*ignore, lpkg, _alpm_pkg_cmp)) {
			/* we've already checked lpkg, move on */
			continue;
		} else if(_alpm_pkg_in_dep_tree(lpkg, pkg2, targets, ignore)) {
			/* we have an indirect dependency: pkg1 -> lpkg -> ... -> pkg2 */
			return 1;
		}
	}

	return 0;
}

/** Check if pkg2 is anywhere in pkg1's dependency tree.
 * Wrapper for _alpm_pkg_in_dep_tree to handle creating and destroying state.
 * @param pkg1
 * @param pkg2
 * @param targets if a package in this list is an intermediate dependency
 *        between pkg1 and pkg2, the pkg1 -> pkg2 dependency will not be
 *        reported
 * @return 1 if pkg2 is found in pkg1's dependency tree
 */
static int _alpm_dep_edge(alpm_pkg_t *pkg1, alpm_pkg_t *pkg2,
		alpm_list_t *targets)
{
	alpm_list_t *ignore = NULL;
	int ret = _alpm_pkg_in_dep_tree(pkg1, pkg2, targets, &ignore);
	alpm_list_free(ignore);
	return ret;
}

/* Convert a list of alpm_pkg_t * to a graph structure,
 * with a edge for each dependency.
 * Returns a list of vertices (one vertex = one package)
 * (used by alpm_sortbydeps)
 */
static alpm_list_t *dep_graph_init(alpm_list_t *targets)
{
	alpm_list_t *i, *j;
	alpm_list_t *vertices = NULL;
	/* We create the vertices */
	for(i = targets; i; i = i->next) {
		alpm_graph_t *vertex = _alpm_graph_new();
		vertex->data = (void *)i->data;
		vertices = alpm_list_add(vertices, vertex);
	}

	/* We compute the edges */
	for(i = vertices; i; i = i->next) {
		alpm_graph_t *vertex_i = i->data;
		alpm_pkg_t *p_i = vertex_i->data;
		/* TODO this should be somehow combined with alpm_checkdeps */
		for(j = vertices; j; j = j->next) {
			alpm_graph_t *vertex_j = j->data;
			alpm_pkg_t *p_j = vertex_j->data;
			if(_alpm_dep_edge(p_i, p_j, targets)) {
				vertex_i->children =
					alpm_list_add(vertex_i->children, vertex_j);
			}
		}
		vertex_i->childptr = vertex_i->children;
	}
	return vertices;
}

/* Re-order a list of target packages with respect to their dependencies.
 *
 * Example (reverse == 0):
 *   A depends on C
 *   B depends on A
 *   Target order is A,B,C,D
 *
 *   Should be re-ordered to C,A,B,D
 *
 * if reverse is > 0, the dependency order will be reversed.
 *
 * This function returns the new alpm_list_t* target list.
 *
 */
alpm_list_t *_alpm_sortbydeps(alpm_handle_t *handle,
		alpm_list_t *targets, int reverse)
{
	alpm_list_t *newtargs = NULL;
	alpm_list_t *vertices = NULL;
	alpm_list_t *vptr;
	alpm_graph_t *vertex;

	if(targets == NULL) {
		return NULL;
	}

	_alpm_log(handle, ALPM_LOG_DEBUG, "started sorting dependencies\n");

	vertices = dep_graph_init(targets);

	vptr = vertices;
	vertex = vertices->data;
	while(vptr) {
		/* mark that we touched the vertex */
		vertex->state = -1;
		int found = 0;
		while(vertex->childptr && !found) {
			alpm_graph_t *nextchild = vertex->childptr->data;
			vertex->childptr = vertex->childptr->next;
			if(nextchild->state == 0) {
				found = 1;
				nextchild->parent = vertex;
				vertex = nextchild;
			}
			else if(nextchild->state == -1) {
				alpm_pkg_t *vertexpkg = vertex->data;
				alpm_pkg_t *childpkg = nextchild->data;

				_alpm_log(handle, ALPM_LOG_WARNING, _("dependency cycle detected:\n"));
				if(reverse) {
					_alpm_log(handle, ALPM_LOG_WARNING,
							_("%s will be removed after its %s dependency\n"),
							vertexpkg->name, childpkg->name);
				} else {
					_alpm_log(handle, ALPM_LOG_WARNING,
							_("%s will be installed before its %s dependency\n"),
							vertexpkg->name, childpkg->name);
				}
			}
		}
		if(!found) {
			newtargs = alpm_list_add(newtargs, vertex->data);
			/* mark that we've left this vertex */
			vertex->state = 1;
			vertex = vertex->parent;
			if(!vertex) {
				vptr = vptr->next;
				while(vptr) {
					vertex = vptr->data;
					if(vertex->state == 0) break;
					vptr = vptr->next;
				}
			}
		}
	}

	_alpm_log(handle, ALPM_LOG_DEBUG, "sorting dependencies finished\n");

	if(reverse) {
		/* reverse the order */
		alpm_list_t *tmptargs = alpm_list_reverse(newtargs);
		/* free the old one */
		alpm_list_free(newtargs);
		newtargs = tmptargs;
	}

	alpm_list_free_inner(vertices, _alpm_graph_free);
	alpm_list_free(vertices);

	return newtargs;
}

static int no_dep_version(alpm_handle_t *handle)
{
	if(!handle->trans) {
		return 0;
	}
	return (handle->trans->flags & ALPM_TRANS_FLAG_NODEPVERSION);
}

static alpm_depend_t *filtered_depend(alpm_depend_t *dep, int nodepversion)
{
	if(nodepversion) {
		alpm_depend_t *newdep = _alpm_dep_dup(dep);
		ASSERT(newdep, return dep);
		newdep->mod = ALPM_DEP_MOD_ANY;
		dep = newdep;
	}
	return dep;
}

static void release_filtered_depend(alpm_depend_t *dep, int nodepversion)
{
	if(nodepversion) {
		free(dep);
	}
}

/** Find a package satisfying a specified dependency.
 * The dependency can include versions with depmod operators.
 * @param pkgs an alpm_list_t* of alpm_pkg_t where the satisfier will be searched
 * @param depstring package or provision name, versioned or not
 * @return a alpm_pkg_t* satisfying depstring
 */
alpm_pkg_t SYMEXPORT *alpm_find_satisfier(alpm_list_t *pkgs, const char *depstring)
{
	alpm_depend_t *dep = _alpm_splitdep(depstring);
	if(!dep) {
		return NULL;
	}
	alpm_pkg_t *pkg = find_dep_satisfier(pkgs, dep);
	_alpm_dep_free(dep);
	return pkg;
}

/** Checks dependencies and returns missing ones in a list.
 * Dependencies can include versions with depmod operators.
 * @param handle the context handle
 * @param pkglist the list of local packages
 * @param remove an alpm_list_t* of packages to be removed
 * @param upgrade an alpm_list_t* of packages to be upgraded (remove-then-upgrade)
 * @param reversedeps handles the backward dependencies
 * @return an alpm_list_t* of alpm_depmissing_t pointers.
 */
alpm_list_t SYMEXPORT *alpm_checkdeps(alpm_handle_t *handle,
		alpm_list_t *pkglist, alpm_list_t *rem, alpm_list_t *upgrade,
		int reversedeps)
{
	alpm_list_t *i, *j;
	alpm_list_t *dblist = NULL, *modified = NULL;
	alpm_list_t *baddeps = NULL;
	int nodepversion;

	CHECK_HANDLE(handle, return NULL);

	for(i = pkglist; i; i = i->next) {
		alpm_pkg_t *pkg = i->data;
		if(alpm_pkg_find(rem, pkg->name) || alpm_pkg_find(upgrade, pkg->name)) {
			modified = alpm_list_add(modified, pkg);
		} else {
			dblist = alpm_list_add(dblist, pkg);
		}
	}

	nodepversion = no_dep_version(handle);

	/* look for unsatisfied dependencies of the upgrade list */
	for(i = upgrade; i; i = i->next) {
		alpm_pkg_t *tp = i->data;
		_alpm_log(handle, ALPM_LOG_DEBUG, "checkdeps: package %s-%s\n",
				tp->name, tp->version);

		for(j = alpm_pkg_get_depends(tp); j; j = j->next) {
			alpm_depend_t *depend = j->data;
			depend = filtered_depend(depend, nodepversion);
			/* 1. we check the upgrade list */
			/* 2. we check database for untouched satisfying packages */
			if(!find_dep_satisfier(upgrade, depend) &&
					!find_dep_satisfier(dblist, depend)) {
				/* Unsatisfied dependency in the upgrade list */
				alpm_depmissing_t *miss;
				char *missdepstring = alpm_dep_compute_string(depend);
				_alpm_log(handle, ALPM_LOG_DEBUG, "checkdeps: missing dependency '%s' for package '%s'\n",
						missdepstring, tp->name);
				free(missdepstring);
				miss = depmiss_new(tp->name, depend, NULL);
				baddeps = alpm_list_add(baddeps, miss);
			}
			release_filtered_depend(depend, nodepversion);
		}
	}

	if(reversedeps) {
		/* reversedeps handles the backwards dependencies, ie,
		 * the packages listed in the requiredby field. */
		for(i = dblist; i; i = i->next) {
			alpm_pkg_t *lp = i->data;
			for(j = alpm_pkg_get_depends(lp); j; j = j->next) {
				alpm_depend_t *depend = j->data;
				depend = filtered_depend(depend, nodepversion);
				alpm_pkg_t *causingpkg = find_dep_satisfier(modified, depend);
				/* we won't break this depend, if it is already broken, we ignore it */
				/* 1. check upgrade list for satisfiers */
				/* 2. check dblist for satisfiers */
				if(causingpkg &&
				   !find_dep_satisfier(upgrade, depend) &&
				   !find_dep_satisfier(dblist, depend)) {
					alpm_depmissing_t *miss;
					char *missdepstring = alpm_dep_compute_string(depend);
					_alpm_log(handle, ALPM_LOG_DEBUG, "checkdeps: transaction would break '%s' dependency of '%s'\n",
							missdepstring, lp->name);
					free(missdepstring);
					miss = depmiss_new(lp->name, depend, causingpkg->name);
					baddeps = alpm_list_add(baddeps, miss);
				}
				release_filtered_depend(depend, nodepversion);
			}
		}
	}

	alpm_list_free(modified);
	alpm_list_free(dblist);

	return baddeps;
}

static int dep_vercmp(const char *version1, alpm_depmod_t mod,
		const char *version2)
{
	int equal = 0;

	if(mod == ALPM_DEP_MOD_ANY) {
		equal = 1;
	} else {
		int cmp = alpm_pkg_vercmp(version1, version2);
		switch(mod) {
			case ALPM_DEP_MOD_EQ: equal = (cmp == 0); break;
			case ALPM_DEP_MOD_GE: equal = (cmp >= 0); break;
			case ALPM_DEP_MOD_LE: equal = (cmp <= 0); break;
			case ALPM_DEP_MOD_LT: equal = (cmp < 0); break;
			case ALPM_DEP_MOD_GT: equal = (cmp > 0); break;
			default: equal = 1; break;
		}
	}
	return equal;
}

int _alpm_depcmp_literal(alpm_pkg_t *pkg, alpm_depend_t *dep)
{
	if(pkg->name_hash != dep->name_hash
			|| strcmp(pkg->name, dep->name) != 0) {
		/* skip more expensive checks */
		return 0;
	}
	return dep_vercmp(pkg->version, dep->mod, dep->version);
}

int _alpm_depcmp(alpm_pkg_t *pkg, alpm_depend_t *dep)
{
	alpm_list_t *i;
	int satisfy = _alpm_depcmp_literal(pkg, dep);

	if(satisfy) {
		return satisfy;
	}

	/* check provisions, name and version if available */
	for(i = alpm_pkg_get_provides(pkg); i && !satisfy; i = i->next) {
		alpm_depend_t *provision = i->data;

		if(dep->mod == ALPM_DEP_MOD_ANY) {
			/* any version will satisfy the requirement */
			satisfy = (provision->name_hash == dep->name_hash
					&& strcmp(provision->name, dep->name) == 0);
		} else if(provision->mod == ALPM_DEP_MOD_EQ) {
			/* provision specifies a version, so try it out */
			satisfy = (provision->name_hash == dep->name_hash
					&& strcmp(provision->name, dep->name) == 0
					&& dep_vercmp(provision->version, dep->mod, dep->version));
		}
	}

	return satisfy;
}

alpm_depend_t *_alpm_splitdep(const char *depstring)
{
	alpm_depend_t *depend;
	const char *ptr, *version, *desc;
	size_t deplen;

	if(depstring == NULL) {
		return NULL;
	}

	MALLOC(depend, sizeof(alpm_depend_t), return NULL);

	/* Note the extra space in ": " to avoid matching the epoch */
	if((desc = strstr(depstring, ": ")) != NULL) {
		STRDUP(depend->desc, desc + 2, return NULL);
		deplen = desc - depstring;
	} else {
		/* no description- point desc at NULL at end of string for later use */
		depend->desc = NULL;
		deplen = strlen(depstring);
		desc = depstring + deplen;
	}

	/* Find a version comparator if one exists. If it does, set the type and
	 * increment the ptr accordingly so we can copy the right strings. */
	if((ptr = memchr(depstring, '<', deplen))) {
		if(ptr[1] == '=') {
			depend->mod = ALPM_DEP_MOD_LE;
			version = ptr + 2;
		} else {
			depend->mod = ALPM_DEP_MOD_LT;
			version = ptr + 1;
		}
	} else if((ptr = memchr(depstring, '>', deplen))) {
		if(ptr[1] == '=') {
			depend->mod = ALPM_DEP_MOD_GE;
			version = ptr + 2;
		} else {
			depend->mod = ALPM_DEP_MOD_GT;
			version = ptr + 1;
		}
	} else if((ptr = memchr(depstring, '=', deplen))) {
		/* Note: we must do =,<,> checks after <=, >= checks */
		depend->mod = ALPM_DEP_MOD_EQ;
		version = ptr + 1;
	} else {
		/* no version specified, set ptr to end of string and version to NULL */
		ptr = depstring + deplen;
		depend->mod = ALPM_DEP_MOD_ANY;
		depend->version = NULL;
		version = NULL;
	}

	/* copy the right parts to the right places */
	STRNDUP(depend->name, depstring, ptr - depstring, return NULL);
	depend->name_hash = _alpm_hash_sdbm(depend->name);
	if(version) {
		STRNDUP(depend->version, version, desc - version, return NULL);
	}

	return depend;
}

alpm_depend_t *_alpm_dep_dup(const alpm_depend_t *dep)
{
	alpm_depend_t *newdep;
	CALLOC(newdep, 1, sizeof(alpm_depend_t), return NULL);

	STRDUP(newdep->name, dep->name, return NULL);
	STRDUP(newdep->version, dep->version, return NULL);
	STRDUP(newdep->desc, dep->desc, return NULL);
	newdep->name_hash = dep->name_hash;
	newdep->mod = dep->mod;

	return newdep;
}

/* These parameters are messy. We check if this package, given a list of
 * targets and a db is safe to remove. We do NOT remove it if it is in the
 * target list, or if the package was explicitly installed and
 * include_explicit == 0 */
static int can_remove_package(alpm_db_t *db, alpm_pkg_t *pkg,
		alpm_list_t *targets, int include_explicit)
{
	alpm_list_t *i;

	if(alpm_pkg_find(targets, pkg->name)) {
		return 0;
	}

	if(!include_explicit) {
		/* see if it was explicitly installed */
		if(alpm_pkg_get_reason(pkg) == ALPM_PKG_REASON_EXPLICIT) {
			_alpm_log(db->handle, ALPM_LOG_DEBUG,
					"excluding %s -- explicitly installed\n", pkg->name);
			return 0;
		}
	}

	/* TODO: checkdeps could be used here, it handles multiple providers
	 * better, but that also makes it slower.
	 * Also this would require to first add the package to the targets list,
	 * then call checkdeps with it, then remove the package from the targets list
	 * if checkdeps detected it would break something */

	/* see if other packages need it */
	for(i = _alpm_db_get_pkgcache(db); i; i = i->next) {
		alpm_pkg_t *lpkg = i->data;
		if(_alpm_pkg_depends_on(lpkg, pkg) && !alpm_pkg_find(targets, lpkg->name)) {
			return 0;
		}
	}

	/* it's ok to remove */
	return 1;
}

/**
 * @brief Adds unneeded dependencies to an existing list of packages.
 * By unneeded, we mean dependencies that are only required by packages in the
 * target list, so they can be safely removed.
 * If the input list was topo sorted, the output list will be topo sorted too.
 *
 * @param db package database to do dependency tracing in
 * @param *targs pointer to a list of packages
 * @param include_explicit if 0, explicitly installed packages are not included
 * @return 0 on success, -1 on errors
 */
int _alpm_recursedeps(alpm_db_t *db, alpm_list_t *targs, int include_explicit)
{
	alpm_list_t *i, *j;

	if(db == NULL || targs == NULL) {
		return -1;
	}

	for(i = targs; i; i = i->next) {
		alpm_pkg_t *pkg = i->data;
		for(j = _alpm_db_get_pkgcache(db); j; j = j->next) {
			alpm_pkg_t *deppkg = j->data;
			if(_alpm_pkg_depends_on(pkg, deppkg)
					&& can_remove_package(db, deppkg, targs, include_explicit)) {
				alpm_pkg_t *copy;
				_alpm_log(db->handle, ALPM_LOG_DEBUG, "adding '%s' to the targets\n",
						deppkg->name);
				/* add it to the target list */
				if(_alpm_pkg_dup(deppkg, &copy)) {
					return -1;
				}
				targs = alpm_list_add(targs, copy);
			}
		}
	}
	return 0;
}

/**
 * helper function for resolvedeps: search for dep satisfier in dbs
 *
 * @param handle the context handle
 * @param dep is the dependency to search for
 * @param dbs are the databases to search
 * @param excluding are the packages to exclude from the search
 * @param prompt if true, will cause an unresolvable dependency to issue an
 *        interactive prompt asking whether the package should be removed from
 *        the transaction or the transaction aborted; if false, simply returns
 *        an error code without prompting
 * @return the resolved package
 **/
static alpm_pkg_t *resolvedep(alpm_handle_t *handle, alpm_depend_t *dep,
		alpm_list_t *dbs, alpm_list_t *excluding, int prompt)
{
	alpm_list_t *i, *j;
	int ignored = 0;

	alpm_list_t *providers = NULL;
	int count;

	/* 1. literals */
	for(i = dbs; i; i = i->next) {
		alpm_pkg_t *pkg;
		alpm_db_t *db = i->data;

		if(!(db->usage & (ALPM_DB_USAGE_INSTALL|ALPM_DB_USAGE_UPGRADE))) {
			continue;
		}

		pkg = _alpm_db_get_pkgfromcache(db, dep->name);
		if(pkg && _alpm_depcmp_literal(pkg, dep)
				&& !alpm_pkg_find(excluding, pkg->name)) {
			if(alpm_pkg_should_ignore(handle, pkg)) {
				int install = 0;
				if(prompt) {
					QUESTION(handle, ALPM_QUESTION_INSTALL_IGNOREPKG, pkg,
							 NULL, NULL, &install);
				} else {
					_alpm_log(handle, ALPM_LOG_WARNING, _("ignoring package %s-%s\n"),
							pkg->name, pkg->version);
				}
				if(!install) {
					ignored = 1;
					continue;
				}
			}
			return pkg;
		}
	}
	/* 2. satisfiers (skip literals here) */
	for(i = dbs; i; i = i->next) {
		alpm_db_t *db = i->data;
		if(!(db->usage & (ALPM_DB_USAGE_INSTALL|ALPM_DB_USAGE_UPGRADE))) {
			continue;
		}
		for(j = _alpm_db_get_pkgcache(db); j; j = j->next) {
			alpm_pkg_t *pkg = j->data;
			/* with hash != hash, we can even skip the strcmp() as we know they can't
			 * possibly be the same string */
			if(pkg->name_hash != dep->name_hash && _alpm_depcmp(pkg, dep)
					&& !alpm_pkg_find(excluding, pkg->name)) {
				if(alpm_pkg_should_ignore(handle, pkg)) {
					int install = 0;
					if(prompt) {
						QUESTION(handle, ALPM_QUESTION_INSTALL_IGNOREPKG,
									pkg, NULL, NULL, &install);
					} else {
						_alpm_log(handle, ALPM_LOG_WARNING, _("ignoring package %s-%s\n"),
								pkg->name, pkg->version);
					}
					if(!install) {
						ignored = 1;
						continue;
					}
				}
				_alpm_log(handle, ALPM_LOG_DEBUG, "provider found (%s provides %s)\n",
						pkg->name, dep->name);
				providers = alpm_list_add(providers, pkg);
				/* keep looking for other providers in the all dbs */
			}
		}
	}

	/* first check if one provider is already installed locally */
	for(i = providers; i; i = i->next) {
		alpm_pkg_t *pkg = i->data;
		if(_alpm_db_get_pkgfromcache(handle->db_local, pkg->name)) {
			alpm_list_free(providers);
			return pkg;
		}
	}
	count = alpm_list_count(providers);
	if(count >= 1) {
		/* default to first provider if there is no QUESTION callback */
		int idx = 0;
		if(count > 1) {
			/* if there is more than one provider, we ask the user */
			QUESTION(handle, ALPM_QUESTION_SELECT_PROVIDER,
					providers, dep, NULL, &idx);
		}
		if(idx >= 0 && idx < count) {
			alpm_list_t *nth = alpm_list_nth(providers, idx);
			alpm_pkg_t *pkg = nth->data;
			alpm_list_free(providers);
			return pkg;
		}
		alpm_list_free(providers);
		providers = NULL;
	}

	if(ignored) { /* resolvedeps will override these */
		handle->pm_errno = ALPM_ERR_PKG_IGNORED;
	} else {
		handle->pm_errno = ALPM_ERR_PKG_NOT_FOUND;
	}
	return NULL;
}

/** Find a package satisfying a specified dependency.
 * First look for a literal, going through each db one by one. Then look for
 * providers. The first satisfier found is returned.
 * The dependency can include versions with depmod operators.
 * @param handle the context handle
 * @param dbs an alpm_list_t* of alpm_db_t where the satisfier will be searched
 * @param depstring package or provision name, versioned or not
 * @return a alpm_pkg_t* satisfying depstring
 */
alpm_pkg_t SYMEXPORT *alpm_find_dbs_satisfier(alpm_handle_t *handle,
		alpm_list_t *dbs, const char *depstring)
{
	alpm_depend_t *dep;
	alpm_pkg_t *pkg;

	CHECK_HANDLE(handle, return NULL);
	ASSERT(dbs, RET_ERR(handle, ALPM_ERR_WRONG_ARGS, NULL));

	dep = _alpm_splitdep(depstring);
	ASSERT(dep, return NULL);
	pkg = resolvedep(handle, dep, dbs, NULL, 1);
	_alpm_dep_free(dep);
	return pkg;
}

/**
 * Computes resolvable dependencies for a given package and adds that package
 * and those resolvable dependencies to a list.
 *
 * @param handle the context handle
 * @param localpkgs is the list of local packages
 * @param pkg is the package to resolve
 * @param preferred packages to prefer when resolving
 * @param packages is a pointer to a list of packages which will be
 *        searched first for any dependency packages needed to complete the
 *        resolve, and to which will be added any [pkg] and all of its
 *        dependencies not already on the list
 * @param remove is the set of packages which will be removed in this
 *        transaction
 * @param data returns the dependency which could not be satisfied in the
 *        event of an error
 * @return 0 on success, with [pkg] and all of its dependencies not already on
 *         the [*packages] list added to that list, or -1 on failure due to an
 *         unresolvable dependency, in which case the [*packages] list will be
 *         unmodified by this function
 */
int _alpm_resolvedeps(alpm_handle_t *handle, alpm_list_t *localpkgs,
		alpm_pkg_t *pkg, alpm_list_t *preferred, alpm_list_t **packages,
		alpm_list_t *rem, alpm_list_t **data)
{
	int ret = 0;
	alpm_list_t *j;
	alpm_list_t *targ;
	alpm_list_t *deps = NULL;
	alpm_list_t *packages_copy;

	if(alpm_pkg_find(*packages, pkg->name) != NULL) {
		return 0;
	}

	/* Create a copy of the packages list, so that it can be restored
	   on error */
	packages_copy = alpm_list_copy(*packages);
	/* [pkg] has not already been resolved into the packages list, so put it
	   on that list */
	*packages = alpm_list_add(*packages, pkg);

	_alpm_log(handle, ALPM_LOG_DEBUG, "started resolving dependencies\n");
	targ = alpm_list_add(NULL, pkg);
	deps = alpm_checkdeps(handle, localpkgs, rem, targ, 0);
	alpm_list_free(targ);
	targ = NULL;

	for(j = deps; j; j = j->next) {
		alpm_depmissing_t *miss = j->data;
		alpm_depend_t *missdep = miss->depend;
		/* check if one of the packages in the [*packages] list already satisfies
		 * this dependency */
		if(find_dep_satisfier(*packages, missdep)) {
			_alpm_depmiss_free(miss);
			continue;
		}
		/* check if one of the packages in the [preferred] list already satisfies
		 * this dependency */
		alpm_pkg_t *spkg = find_dep_satisfier(preferred, missdep);
		if(!spkg) {
			/* find a satisfier package in the given repositories */
			spkg = resolvedep(handle, missdep, handle->dbs_sync, *packages, 0);
		}
		if(spkg && _alpm_resolvedeps(handle, localpkgs, spkg, preferred, packages, rem, data) == 0) {
			_alpm_log(handle, ALPM_LOG_DEBUG,
					"pulling dependency %s (needed by %s)\n",
					spkg->name, pkg->name);
			_alpm_depmiss_free(miss);
		} else if(resolvedep(handle, missdep, (targ = alpm_list_add(NULL, handle->db_local)), rem, 0)) {
			_alpm_depmiss_free(miss);
		} else {
			handle->pm_errno = ALPM_ERR_UNSATISFIED_DEPS;
			char *missdepstring = alpm_dep_compute_string(missdep);
			_alpm_log(handle, ALPM_LOG_WARNING,
					_("cannot resolve \"%s\", a dependency of \"%s\"\n"),
					missdepstring, pkg->name);
			free(missdepstring);
			if(data) {
				*data = alpm_list_add(*data, miss);
			}
			ret = -1;
		}
	}
	alpm_list_free(targ);
	alpm_list_free(deps);

	if(ret != 0) {
		alpm_list_free(*packages);
		*packages = packages_copy;
	} else {
		alpm_list_free(packages_copy);
	}
	_alpm_log(handle, ALPM_LOG_DEBUG, "finished resolving dependencies\n");
	return ret;
}

/** Reverse of splitdep; make a dep string from a alpm_depend_t struct.
 * The string must be freed!
 * @param dep the depend to turn into a string
 * @return a string-formatted dependency with operator if necessary
 */
char SYMEXPORT *alpm_dep_compute_string(const alpm_depend_t *dep)
{
	const char *name, *opr, *ver, *desc_delim, *desc;
	char *str;
	size_t len;

	ASSERT(dep != NULL, return NULL);

	if(dep->name) {
		name = dep->name;
	} else {
		name = "";
	}

	switch(dep->mod) {
		case ALPM_DEP_MOD_ANY:
			opr = "";
			break;
		case ALPM_DEP_MOD_GE:
			opr = ">=";
			break;
		case ALPM_DEP_MOD_LE:
			opr = "<=";
			break;
		case ALPM_DEP_MOD_EQ:
			opr = "=";
			break;
		case ALPM_DEP_MOD_LT:
			opr = "<";
			break;
		case ALPM_DEP_MOD_GT:
			opr = ">";
			break;
		default:
			opr = "";
			break;
	}

	if(dep->mod != ALPM_DEP_MOD_ANY && dep->version) {
		ver = dep->version;
	} else {
		ver = "";
	}

	if(dep->desc) {
		desc_delim = ": ";
		desc = dep->desc;
	} else {
		desc_delim = "";
		desc = "";
	}

	/* we can always compute len and print the string like this because opr
	 * and ver will be empty when ALPM_DEP_MOD_ANY is the depend type. the
	 * reassignments above also ensure we do not do a strlen(NULL). */
	len = strlen(name) + strlen(opr) + strlen(ver)
		+ strlen(desc_delim) + strlen(desc) + 1;
	MALLOC(str, len, return NULL);
	snprintf(str, len, "%s%s%s%s%s", name, opr, ver, desc_delim, desc);

	return str;
}
/* vim: set ts=2 sw=2 noet: */