summaryrefslogtreecommitdiff
path: root/lib/libalpm/deps.c
blob: ddbd99a2373c4f2bea0dd690b40296d39d03da11 (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
/*
 *  deps.c
 * 
 *  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, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 
 *  USA.
 */

#include "config.h"

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

/* libalpm */
#include "deps.h"
#include "alpm_list.h"
#include "util.h"
#include "log.h"
#include "error.h"
#include "package.h"
#include "db.h"
#include "cache.h"
#include "provide.h"
#include "handle.h"

extern pmhandle_t *handle;

static pmgraph_t *_alpm_graph_new(void)
{
	pmgraph_t *graph = NULL;

	graph = (pmgraph_t *)malloc(sizeof(pmgraph_t));
	if(graph) {
		graph->state = 0;
		graph->data = NULL;
		graph->parent = NULL;
		graph->children = NULL;
		graph->childptr = NULL;
	}
	return(graph);
}

static void _alpm_graph_free(void *data)
{
	pmgraph_t *graph = data;
	alpm_list_free(graph->children);
	free(graph);
}

pmdepmissing_t *_alpm_depmiss_new(const char *target, pmdeptype_t type,
                                  pmdepmod_t depmod, const char *depname,
                                  const char *depversion)
{
	pmdepmissing_t *miss;

	ALPM_LOG_FUNC;

	miss = malloc(sizeof(pmdepmissing_t));
	if(miss == NULL) {
		_alpm_log(PM_LOG_ERROR, _("malloc failure: could not allocate %d bytes"), sizeof(pmdepmissing_t));
		RET_ERR(PM_ERR_MEMORY, NULL);
	}

	strncpy(miss->target, target, PKG_NAME_LEN);
	miss->type = type;
	miss->depend.mod = depmod;
	strncpy(miss->depend.name, depname, PKG_NAME_LEN);
	if(depversion) {
		strncpy(miss->depend.version, depversion, PKG_VERSION_LEN);
	} else {
		miss->depend.version[0] = 0;
	}

	return(miss);
}

int _alpm_depmiss_isin(pmdepmissing_t *needle, alpm_list_t *haystack)
{
	alpm_list_t *i;

	ALPM_LOG_FUNC;

	for(i = haystack; i; i = i->next) {
		pmdepmissing_t *miss = i->data;
		if(!memcmp(needle, miss, sizeof(pmdepmissing_t))
		   && !memcmp(&needle->depend, &miss->depend, sizeof(pmdepend_t))) {
			return(1);
		}
	}

	return(0);
}

/* Re-order a list of target packages with respect to their dependencies.
 *
 * Example (PM_TRANS_TYPE_ADD):
 *   A depends on C
 *   B depends on A
 *   Target order is A,B,C,D
 *
 *   Should be re-ordered to C,A,B,D
 * 
 * mode should be either PM_TRANS_TYPE_ADD or PM_TRANS_TYPE_REMOVE.  This
 * affects the dependency order sortbydeps() will use.
 *
 * This function returns the new alpm_list_t* target list.
 *
 */ 
alpm_list_t *_alpm_sortbydeps(alpm_list_t *targets, pmtranstype_t mode)
{
	alpm_list_t *newtargs = NULL;
	alpm_list_t *i, *j, *k;
	alpm_list_t *vertices = NULL;
	alpm_list_t *vptr;
	pmgraph_t *vertex;

	ALPM_LOG_FUNC;

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

	_alpm_log(PM_LOG_DEBUG, "started sorting dependencies");

	/* We create the vertices */
	for(i = targets; i; i = i->next) {
		pmgraph_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) {
		pmgraph_t *vertex_i = i->data;
		pmpkg_t *p_i = vertex_i->data;
		/* TODO this should be somehow combined with _alpm_checkdeps */
		for(j = vertices; j; j = j->next) {
			pmgraph_t *vertex_j = j->data;
			pmpkg_t *p_j = vertex_j->data;
			int child = 0;
			for(k = alpm_pkg_get_depends(p_i); k && !child; k = k->next) {
				pmdepend_t *depend = alpm_splitdep(k->data);
				child = alpm_depcmp(p_j, depend);
				free(depend);
			}
			if(child) vertex_i->children =
				alpm_list_add(vertex_i->children, vertex_j);
		}
		vertex_i->childptr = vertex_i->children;
	}

	vptr = vertices;
	vertex = vertices->data;
	while(vptr) {
		/* mark that we touched the vertex */
		vertex->state = -1;
		int found = 0;
		while(vertex->childptr && !found) {
			pmgraph_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_log(PM_LOG_WARNING, _("dependency cycle detected\n"));
			}
		}
		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(PM_LOG_DEBUG, "sorting dependencies finished");

	if(mode == PM_TRANS_TYPE_REMOVE) {
		/* we're removing packages, so 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);

	return(newtargs);
}

/** Checks dependencies and returns missing ones in a list. Dependencies can include versions with depmod operators.
 * @param db pointer to the local package database
 * @param op transaction type
 * @param packages an alpm_list_t* of packages to be checked
 * @return an alpm_list_t* of pmpkg_t* of missing_t pointers.
 */
alpm_list_t *_alpm_checkdeps(pmdb_t *db, pmtranstype_t op,
                             alpm_list_t *packages)
{
	alpm_list_t *i, *j, *k, *l;
	int found = 0;
	alpm_list_t *baddeps = NULL;
	pmdepmissing_t *miss = NULL;

	ALPM_LOG_FUNC;

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

	if(op == PM_TRANS_TYPE_UPGRADE) {
		/* PM_TRANS_TYPE_UPGRADE handles the backwards dependencies, ie, the packages
		 * listed in the requiredby field.
		 */
		for(i = packages; i; i = i->next) {
			pmpkg_t *newpkg = i->data;
			pmpkg_t *oldpkg;
			if(newpkg == NULL) {
				_alpm_log(PM_LOG_DEBUG, "null package found in package list");
				continue;
			}

			if((oldpkg = _alpm_db_get_pkgfromcache(db, alpm_pkg_get_name(newpkg))) == NULL) {
				_alpm_log(PM_LOG_DEBUG, "cannot find package installed '%s'",
									alpm_pkg_get_name(newpkg));
				continue;
			}
			for(j = alpm_pkg_get_requiredby(oldpkg); j; j = j->next) {
				pmpkg_t *p;
				found = 0;

				if(_alpm_pkg_find(j->data, packages)) {
					/* this package also in the upgrade list, so don't worry about it */
					continue;
				}
				if((p = _alpm_db_get_pkgfromcache(db, j->data)) == NULL) {
					/* hmmm... package isn't installed.. */
					continue;
				}

				for(k = alpm_pkg_get_depends(p); k; k = k->next) {
					/* don't break any existing dependencies (possible provides) */
					pmdepend_t *depend = alpm_splitdep(k->data);
					if(depend == NULL) {
						continue;
					}

					/* if oldpkg satisfied this dep, and newpkg doesn't */
					if(alpm_depcmp(oldpkg, depend) && !alpm_depcmp(newpkg, depend)) {
						/* we've found a dep that was removed... see if any other package
						 * still contains/provides the dep */
						int satisfied = 0;
						for(l = packages; l; l = l->next) {
							pmpkg_t *pkg = l->data;

							if(alpm_depcmp(pkg, depend)) {
								_alpm_log(PM_LOG_DEBUG, "checkdeps: dependency '%s' has moved from '%s' to '%s'",
													depend->name, alpm_pkg_get_name(oldpkg), alpm_pkg_get_name(pkg));
								satisfied = 1;
								break;
							}
						}

						if(!satisfied) {
							/* worst case... check installed packages to see if anything else
							 * satisfies this... */
							for(l = _alpm_db_get_pkgcache(db); l; l = l->next) {
								pmpkg_t *pkg = l->data;

								if(alpm_depcmp(pkg, depend) && !_alpm_pkg_find(alpm_pkg_get_name(pkg), packages)) {
									/* we ignore packages that will be updated because we know
									 * that the updated ones don't satisfy depend */
									_alpm_log(PM_LOG_DEBUG, "checkdeps: dependency '%s' satisfied by installed package '%s'",
														depend->name, alpm_pkg_get_name(pkg));
									satisfied = 1;
									break;
								}
							}
						}

						if(!satisfied) {
							_alpm_log(PM_LOG_DEBUG, "checkdeps: updated '%s' won't satisfy a dependency of '%s'",
												alpm_pkg_get_name(oldpkg), alpm_pkg_get_name(p));
							miss = _alpm_depmiss_new(p->name, PM_DEP_TYPE_DEPEND, depend->mod,
																			 depend->name, depend->version);
							if(!_alpm_depmiss_isin(miss, baddeps)) {
								baddeps = alpm_list_add(baddeps, miss);
							} else {
								FREE(miss);
							}
						}
					}
					free(depend);
				}
			}
		}
	}
	if(op == PM_TRANS_TYPE_ADD || op == PM_TRANS_TYPE_UPGRADE) {
		/* DEPENDENCIES -- look for unsatisfied dependencies */
		for(i = packages; i; i = i->next) {
			pmpkg_t *tp = i->data;
			if(tp == NULL) {
				_alpm_log(PM_LOG_DEBUG, "null package found in package list");
				continue;
			}

			for(j = alpm_pkg_get_depends(tp); j; j = j->next) {
				/* split into name/version pairs */
				pmdepend_t *depend = alpm_splitdep((char*)j->data);
				if(depend == NULL) {
					continue;
				}
				
				found = 0;
 				/* check other targets */
 				for(k = packages; k && !found; k = k->next) {
 					pmpkg_t *p = k->data;
					found = alpm_depcmp(p, depend);
				}

				/* check database for satisfying packages */
				/* we can ignore packages being updated, they were checked above */
				for(k = _alpm_db_get_pkgcache(db); k && !found; k = k->next) {
					pmpkg_t *p = k->data;
					found = alpm_depcmp(p, depend)
						&& !_alpm_pkg_find(alpm_pkg_get_name(p), packages);
				}

				/* else if still not found... */
				if(!found) {
					_alpm_log(PM_LOG_DEBUG, "missing dependency '%s' for package '%s'",
					                          depend->name, alpm_pkg_get_name(tp));
					miss = _alpm_depmiss_new(alpm_pkg_get_name(tp), PM_DEP_TYPE_DEPEND, depend->mod,
					                         depend->name, depend->version);
					if(!_alpm_depmiss_isin(miss, baddeps)) {
						baddeps = alpm_list_add(baddeps, miss);
					} else {
						FREE(miss);
					}
				}
				free(depend);
			}
		}
	} else if(op == PM_TRANS_TYPE_REMOVE) {
		/* check requiredby fields */
		for(i = packages; i; i = i->next) {
			pmpkg_t *rmpkg = alpm_list_getdata(i);

			if(rmpkg == NULL) {
				_alpm_log(PM_LOG_DEBUG, "null package found in package list");
				continue;
			}
			for(j = alpm_pkg_get_requiredby(rmpkg); j; j = j->next) {
				pmpkg_t *p;
				found = 0;
				if(_alpm_pkg_find(j->data, packages)) {
					/* package also in the remove list, so don't worry about it */
					continue;
				}

				if((p = _alpm_db_get_pkgfromcache(db, j->data)) == NULL) {
					/* hmmm... package isn't installed... */
					continue;
				}
				for(k = alpm_pkg_get_depends(p); k; k = k->next) {
					pmdepend_t *depend = alpm_splitdep(k->data);
					if(depend == NULL) {
						continue;
					}
					/* if rmpkg satisfied this dep, try to find an other satisfier
					 * (which won't be removed)*/
					if(alpm_depcmp(rmpkg, depend)) {
						int satisfied = 0;
						for(l = _alpm_db_get_pkgcache(db); l; l = l->next) {
							pmpkg_t *pkg = l->data;
							if(alpm_depcmp(pkg, depend) && !_alpm_pkg_find(alpm_pkg_get_name(pkg), packages)) {
								_alpm_log(PM_LOG_DEBUG, "checkdeps: dependency '%s' satisfied by installed package '%s'",
										depend->name, alpm_pkg_get_name(pkg));
								satisfied = 1;
								break;
							}
						}

						if(!satisfied) {
							_alpm_log(PM_LOG_DEBUG, "checkdeps: found %s which requires %s",
									alpm_pkg_get_name(p), alpm_pkg_get_name(rmpkg));
							miss = _alpm_depmiss_new(alpm_pkg_get_name(p),
									PM_DEP_TYPE_DEPEND, depend->mod, depend->name,
									depend->version);
							if(!_alpm_depmiss_isin(miss, baddeps)) {
								baddeps = alpm_list_add(baddeps, miss);
							} else {
								FREE(miss);
							}
						}
					}
					free(depend);
				}
			}
		}
	}

	return(baddeps);
}

int SYMEXPORT alpm_depcmp(pmpkg_t *pkg, pmdepend_t *dep)
{
	int equal = 0;

	ALPM_LOG_FUNC;

  if(strcmp(pkg->name, dep->name) == 0
	    || alpm_list_find_str(alpm_pkg_get_provides(pkg), dep->name)) {
		if(dep->mod == PM_DEP_MOD_ANY) {
			equal = 1;
		} else {
			int cmp = _alpm_versioncmp(alpm_pkg_get_version(pkg), dep->version);
			switch(dep->mod) {
				case PM_DEP_MOD_EQ: equal = (cmp == 0); break;
				case PM_DEP_MOD_GE: equal = (cmp >= 0); break;
				case PM_DEP_MOD_LE: equal = (cmp <= 0); break;
				default: equal = 1; break;
			}
		}

		char *mod = "~=";
		switch(dep->mod) {
			case PM_DEP_MOD_EQ: mod = "=="; break;
			case PM_DEP_MOD_GE: mod = ">="; break;
			case PM_DEP_MOD_LE: mod = "<="; break;
			default: break;
		}

		if(strlen(dep->version) > 0) {
			_alpm_log(PM_LOG_DEBUG, "depcmp: %s-%s %s %s-%s => %s",
								alpm_pkg_get_name(pkg), alpm_pkg_get_version(pkg),
								mod, dep->name, dep->version, (equal ? "match" : "no match"));
		} else {
			_alpm_log(PM_LOG_DEBUG, "depcmp: %s-%s %s %s => %s",
								alpm_pkg_get_name(pkg), alpm_pkg_get_version(pkg),
								mod, dep->name, (equal ? "match" : "no match"));
		}
	}

	return equal;
}

pmdepend_t SYMEXPORT *alpm_splitdep(const char *depstring)
{
	pmdepend_t *depend;
	char *ptr = NULL;
	char *newstr = NULL;

	if(depstring == NULL) {
		return(NULL);
	}
	newstr = strdup(depstring);
	
	depend = malloc(sizeof(pmdepend_t));
	if(depend == NULL) {
		_alpm_log(PM_LOG_ERROR, _("malloc failure: could not allocate %d bytes"), sizeof(pmdepend_t));
		return(NULL);
	}

	/* 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 = strstr(newstr, ">="))) {
		depend->mod = PM_DEP_MOD_GE;
		*ptr = '\0';
		ptr += 2;
	} else if((ptr = strstr(newstr, "<="))) {
		depend->mod = PM_DEP_MOD_LE;
		*ptr = '\0';
		ptr += 2;
	} else if((ptr = strstr(newstr, "="))) {
		depend->mod = PM_DEP_MOD_EQ;
		*ptr = '\0';
		ptr += 1;
	} else {
		/* no version specified - copy in the name and return it */
		depend->mod = PM_DEP_MOD_ANY;
		strncpy(depend->name, newstr, PKG_NAME_LEN);
		depend->version[0] = '\0';
		free(newstr);
		return(depend);
	}

	/* if we get here, we have a version comparator, copy the right parts
	 * to the right places */
	strncpy(depend->name, newstr, PKG_NAME_LEN);
	strncpy(depend->version, ptr, PKG_VERSION_LEN);
	free(newstr);

	return(depend);
}

/* 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 */
static int can_remove_package(pmdb_t *db, pmpkg_t *pkg, alpm_list_t *targets)
{
	alpm_list_t *i;

	if(_alpm_pkg_find(alpm_pkg_get_name(pkg), targets)) {
		return(0);
	}

	/* see if it was explicitly installed */
	if(alpm_pkg_get_reason(pkg) == PM_PKG_REASON_EXPLICIT) {
		_alpm_log(PM_LOG_DEBUG, "excluding %s -- explicitly installed",
				alpm_pkg_get_name(pkg));
		return(0);
	}

	/* see if other packages need it */
	for(i = alpm_pkg_get_requiredby(pkg); i; i = i->next) {
		pmpkg_t *reqpkg = _alpm_db_get_pkgfromcache(db, i->data);
		if(reqpkg && !_alpm_pkg_find(alpm_pkg_get_name(reqpkg), targets)) {
			return(0);
		}
	}

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

/* return a new alpm_list_t target list containing all packages in the original
 * target list, as well as all their un-needed dependencies.  By un-needed,
 * I mean dependencies that are *only* required for packages in the target
 * list, so they can be safely removed.  This function is recursive.
 */
alpm_list_t *_alpm_removedeps(pmdb_t *db, alpm_list_t *targs)
{
	alpm_list_t *i, *j, *k;
	alpm_list_t *newtargs = targs;

	ALPM_LOG_FUNC;

	if(db == NULL) {
		return(newtargs);
	}

	for(i = targs; i; i = i->next) {
		pmpkg_t *pkg = i->data;
		for(j = alpm_pkg_get_depends(pkg); j; j = j->next) {
			pmdepend_t *depend = alpm_splitdep(j->data);
			pmpkg_t *deppkg;
			if(depend == NULL) {
				continue;
			}

			deppkg = _alpm_db_get_pkgfromcache(db, depend->name);
			if(deppkg == NULL) {
				/* package not found... look for a provision instead */
				alpm_list_t *provides = _alpm_db_whatprovides(db, depend->name);
				if(!provides) {
					/* Not found, that's fine, carry on */
					_alpm_log(PM_LOG_DEBUG, "cannot find package \"%s\" or anything that provides it!", depend->name);
					continue;
				}
				for(k = provides; k; k = k->next) {
					pmpkg_t *provpkg = k->data;
					if(can_remove_package(db, provpkg, newtargs)) {
						pmpkg_t *pkg = _alpm_pkg_dup(provpkg);

						_alpm_log(PM_LOG_DEBUG, "adding '%s' to the targets",
								alpm_pkg_get_name(pkg));

						/* add it to the target list */
						newtargs = alpm_list_add(newtargs, pkg);
						newtargs = _alpm_removedeps(db, newtargs);
					}
				}
				alpm_list_free(provides);
			} else if(can_remove_package(db, deppkg, newtargs)) {
				pmpkg_t *pkg = _alpm_pkg_dup(deppkg);

				_alpm_log(PM_LOG_DEBUG, "adding '%s' to the targets",
						alpm_pkg_get_name(pkg));

				/* add it to the target list */
				newtargs = alpm_list_add(newtargs, pkg);
				newtargs = _alpm_removedeps(db, newtargs);
			}
			free(depend);
		}
	}

	return(newtargs);
}

/* populates *list with packages that need to be installed to satisfy all
 * dependencies (recursive) for syncpkg
 *
 * make sure *list and *trail are already initialized
 */
int _alpm_resolvedeps(pmdb_t *local, alpm_list_t *dbs_sync, pmpkg_t *syncpkg,
                      alpm_list_t *list, alpm_list_t *trail, pmtrans_t *trans,
                      alpm_list_t **data)
{
	alpm_list_t *i, *j;
	alpm_list_t *targ;
	alpm_list_t *deps = NULL;

	ALPM_LOG_FUNC;

	if(local == NULL || dbs_sync == NULL || syncpkg == NULL) {
		return(-1);
	}

	_alpm_log(PM_LOG_DEBUG, "started resolving dependencies");
	targ = alpm_list_add(NULL, syncpkg);
	deps = _alpm_checkdeps(local, PM_TRANS_TYPE_ADD, targ);
	alpm_list_free(targ);

	if(deps == NULL) {
		return(0);
	}

	for(i = deps; i; i = i->next) {
		int found = 0;
		pmdepmissing_t *miss = i->data;
		pmpkg_t *sync = NULL;

		/* check if one of the packages in *list already provides this dependency */
		for(j = list; j && !found; j = j->next) {
			pmpkg_t *sp = j->data;
			if(alpm_list_find_str(alpm_pkg_get_provides(sp), miss->depend.name)) {
				_alpm_log(PM_LOG_DEBUG, "%s provides dependency %s -- skipping",
				          alpm_pkg_get_name(sp), miss->depend.name);
				found = 1;
			}
		}
		if(found) {
			continue;
		}

		/* find the package in one of the repositories */
		/* check literals */
		for(j = dbs_sync; !sync && j; j = j->next) {
			sync = _alpm_db_get_pkgfromcache(j->data, miss->depend.name);
		}
		/*TODO this autoresolves the first 'provides' package... we should fix this
		 * somehow */
		/* check provides */
		if(!sync) {
			for(j = dbs_sync; !sync && j; j = j->next) {
				alpm_list_t *provides;
				provides = _alpm_db_whatprovides(j->data, miss->depend.name);
				if(provides) {
					sync = provides->data;
				}
				alpm_list_free(provides);
			}
		}

		if(!sync) {
			_alpm_log(PM_LOG_ERROR, _("cannot resolve dependencies for \"%s\" (\"%s\" is not in the package set)"),
			          miss->target, miss->depend.name);
			if(data) {
				if((miss = malloc(sizeof(pmdepmissing_t))) == NULL) {
					_alpm_log(PM_LOG_ERROR, _("malloc failure: could not allocate %d bytes"), sizeof(pmdepmissing_t));
					FREELIST(*data);
					pm_errno = PM_ERR_MEMORY;
					goto error;
				}
				*miss = *(pmdepmissing_t *)i->data;
				*data = alpm_list_add(*data, miss);
			}
			pm_errno = PM_ERR_UNSATISFIED_DEPS;
			goto error;
		}
		if(_alpm_pkg_find(alpm_pkg_get_name(sync), list)) {
			/* this dep is already in the target list */
			_alpm_log(PM_LOG_DEBUG, "dependency %s is already in the target list -- skipping",
								alpm_pkg_get_name(sync));
			continue;
		}

		if(!_alpm_pkg_find(alpm_pkg_get_name(sync), trail)) {
			/* check pmo_ignorepkg and pmo_s_ignore to make sure we haven't pulled in
			 * something we're not supposed to.
			 */
			int usedep = 1;
			if(alpm_list_find_str(handle->ignorepkg, alpm_pkg_get_name(sync))) {
				pmpkg_t *dummypkg = _alpm_pkg_new(miss->target, NULL);
				QUESTION(trans, PM_TRANS_CONV_INSTALL_IGNOREPKG, dummypkg, sync, NULL, &usedep);
				_alpm_pkg_free(dummypkg);
			}
			if(usedep) {
				trail = alpm_list_add(trail, sync);
				if(_alpm_resolvedeps(local, dbs_sync, sync, list, trail, trans, data)) {
					goto error;
				}
				_alpm_log(PM_LOG_DEBUG, "pulling dependency %s (needed by %s)",
									alpm_pkg_get_name(sync), alpm_pkg_get_name(syncpkg));
				list = alpm_list_add(list, sync);
			} else {
				_alpm_log(PM_LOG_ERROR, _("cannot resolve dependencies for \"%s\""), miss->target);
				if(data) {
					if((miss = malloc(sizeof(pmdepmissing_t))) == NULL) {
						_alpm_log(PM_LOG_ERROR, _("malloc failure: could not allocate %d bytes"), sizeof(pmdepmissing_t));
						FREELIST(*data);
						pm_errno = PM_ERR_MEMORY;
						goto error;
					}
					*miss = *(pmdepmissing_t *)i->data;
					*data = alpm_list_add(*data, miss);
				}
				pm_errno = PM_ERR_UNSATISFIED_DEPS;
				goto error;
			}
		} else {
			/* cycle detected -- skip it */
			_alpm_log(PM_LOG_DEBUG, "dependency cycle detected: %s", sync->name);
		}
	}
	
	_alpm_log(PM_LOG_DEBUG, "finished resolving dependencies");

	FREELIST(deps);

	return(0);

error:
	FREELIST(deps);
	return(-1);
}

const char SYMEXPORT *alpm_dep_get_target(pmdepmissing_t *miss)
{
	ALPM_LOG_FUNC;

	/* Sanity checks */
	ASSERT(handle != NULL, return(NULL));
	ASSERT(miss != NULL, return(NULL));

	return miss->target;
}

pmdeptype_t SYMEXPORT alpm_dep_get_type(pmdepmissing_t *miss)
{
	ALPM_LOG_FUNC;

	/* Sanity checks */
	ASSERT(handle != NULL, return(-1));
	ASSERT(miss != NULL, return(-1));

	return miss->type;
}

pmdepmod_t SYMEXPORT alpm_dep_get_mod(pmdepmissing_t *miss)
{
	ALPM_LOG_FUNC;

	/* Sanity checks */
	ASSERT(handle != NULL, return(-1));
	ASSERT(miss != NULL, return(-1));

	return miss->depend.mod;
}

const char SYMEXPORT *alpm_dep_get_name(pmdepmissing_t *miss)
{
	ALPM_LOG_FUNC;

	/* Sanity checks */
	ASSERT(handle != NULL, return(NULL));
	ASSERT(miss != NULL, return(NULL));

	return miss->depend.name;
}

const char SYMEXPORT *alpm_dep_get_version(pmdepmissing_t *miss)
{
	ALPM_LOG_FUNC;

	/* Sanity checks */
	ASSERT(handle != NULL, return(NULL));
	ASSERT(miss != NULL, return(NULL));

	return miss->depend.version;
}

const char SYMEXPORT *alpm_depend_get_name(pmdepend_t *dep)
{
	ALPM_LOG_FUNC;

	/* Sanity checks */
	ASSERT(handle != NULL, return(NULL));
	ASSERT(dep != NULL, return(NULL));

	return dep->name;
}

/* vim: set ts=2 sw=2 noet: */