summaryrefslogtreecommitdiffstats
path: root/lib/libalpm/deps.c
blob: b2d8a7a3b457e1f39347664ed896174cc941b8f4 (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
/*
 *  deps.c
 * 
 *  Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.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 <stdlib.h>
#include <stdio.h>
#include <string.h>
/* pacman */
#include "util.h"
#include "log.h"
#include "error.h"
#include "list.h"
#include "package.h"
#include "db.h"
#include "cache.h"
#include "provide.h"
#include "deps.h"
#include "versioncmp.h"
#include "handle.h"

extern pmhandle_t *handle;

/* 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 PMList* target list.
 *
 */ 
PMList *sortbydeps(PMList *targets, int mode)
{
	PMList *newtargs = NULL;
	PMList *i, *j, *k;
	int change = 1;
	int numscans = 0;
	int numtargs = 0;

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

	for(i = targets; i; i = i->next) {
		newtargs = pm_list_add(newtargs, i->data);
		numtargs++;
	}

	while(change) {
		PMList *tmptargs = NULL;
		change = 0;
		if(numscans > numtargs) {
			_alpm_log(PM_LOG_WARNING, "possible dependency cycle detected");
			continue;
		}
		numscans++;
		/* run thru targets, moving up packages as necessary */
		for(i = newtargs; i; i = i->next) {
			pmpkg_t *p = (pmpkg_t*)i->data;
			for(j = p->depends; j; j = j->next) {
				pmdepend_t dep;
				pmpkg_t *q = NULL;
				if(splitdep(j->data, &dep)) {
					continue;
				}
				/* look for dep.name -- if it's farther down in the list, then
				 * move it up above p
				 */
				for(k = i->next; k; k = k->next) {
					q = (pmpkg_t *)k->data;
					if(!strcmp(dep.name, q->name)) {
						if(!pkg_isin(q, tmptargs)) {
							change = 1;
							tmptargs = pm_list_add(tmptargs, q);
						}
						break;
					}
				}
			}
			if(!pkg_isin(p, tmptargs)) {
				tmptargs = pm_list_add(tmptargs, p);
			}
		}
		FREELISTPTR(newtargs);
		newtargs = tmptargs;
	}

	if(mode == PM_TRANS_TYPE_REMOVE) {
		/* we're removing packages, so reverse the order */
		PMList *tmptargs = _alpm_list_reverse(newtargs);
		/* free the old one */
		FREELISTPTR(newtargs);
		newtargs = tmptargs;
	}

	return(newtargs);
}

/* Returns a PMList* of missing_t pointers.
 *
 * conflicts are always name only, but dependencies can include versions
 * with depmod operators.
 *
 */
PMList *checkdeps(pmdb_t *db, unsigned char op, PMList *packages)
{
	pmpkg_t *info = NULL;
	pmdepend_t depend;
	PMList *i, *j, *k;
	int cmp;
	int found = 0;
	PMList *baddeps = NULL;
	pmdepmissing_t *miss = NULL;

	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 *tp = i->data;
			pmpkg_t *oldpkg;
			if(tp == NULL) {
				continue;
			}

			if((oldpkg = db_get_pkgfromcache(db, tp->name)) == NULL) {
				continue;
			}
			for(j = oldpkg->requiredby; j; j = j->next) {
				char *ver;
				pmpkg_t *p;
				found = 0;
				if((p = db_get_pkgfromcache(db, j->data)) == NULL) {
					/* hmmm... package isn't installed.. */
					continue;
				}
				if(pkg_isin(p, packages)) {
					/* this package is also in the upgrade list, so don't worry about it */
					continue;
				}
				for(k = p->depends; k && !found; k = k->next) {
					/* find the dependency info in p->depends */
					splitdep(k->data, &depend);
					if(!strcmp(depend.name, oldpkg->name)) {
						found = 1;
					}
				}
				if(found == 0) {
					/* look for packages that list depend.name as a "provide" */
					PMList *provides = _alpm_db_whatprovides(db, depend.name);
					if(provides == NULL) {
						/* not found */
						continue;
					}
					/* we found an installed package that provides depend.name */
					FREELISTPTR(provides);
				}
				found = 0;
				if(depend.mod == PM_DEP_MOD_ANY) {
					found = 1;
				} else {
					/* note that we use the version from the NEW package in the check */
					ver = strdup(tp->version);
					if(!index(depend.version,'-')) {
						char *ptr;
						for(ptr = ver; *ptr != '-'; ptr++);
						*ptr = '\0';
					}
					cmp = versioncmp(ver, depend.version);
					switch(depend.mod) {
						case PM_DEP_MOD_EQ: found = (cmp == 0); break;
						case PM_DEP_MOD_GE: found = (cmp >= 0); break;
						case PM_DEP_MOD_LE: found = (cmp <= 0); break;
					}
					FREE(ver);
				}
				if(!found) {
					MALLOC(miss, sizeof(pmdepmissing_t));
					miss->type = PM_DEP_TYPE_REQUIRED;
					miss->depend.mod = depend.mod;
					STRNCPY(miss->target, p->name, PKG_NAME_LEN);
					STRNCPY(miss->depend.name, depend.name, PKG_NAME_LEN);
					STRNCPY(miss->depend.version, depend.version, PKG_VERSION_LEN);
					if(!pm_list_is_in(miss, baddeps)) {
						baddeps = pm_list_add(baddeps, miss);
					} else {
						FREE(miss);
					}
				}
			}
		}
	}
	if(op == PM_TRANS_TYPE_ADD || op == PM_TRANS_TYPE_UPGRADE) {
		for(i = packages; i; i = i->next) {
			pmpkg_t *tp = i->data;
			if(tp == NULL) {
				continue;
			}

			/* CONFLICTS */
			for(j = tp->conflicts; j; j = j->next) {
				if(!strcmp(tp->name, j->data)) {
					/* a package cannot conflict with itself -- that's just not nice */
					continue;
				}
				/* CHECK 1: check targets against database */
				for(k = db_get_pkgcache(db); k; k = k->next) {
					pmpkg_t *dp = (pmpkg_t *)k->data;
					if(!strcmp(j->data, dp->name)) {
						/* confict */
						MALLOC(miss, sizeof(pmdepmissing_t));
						miss->type = PM_DEP_TYPE_CONFLICT;
						miss->depend.mod = PM_DEP_MOD_ANY;
						miss->depend.version[0] = '\0';
						STRNCPY(miss->target, tp->name, PKG_NAME_LEN);
						STRNCPY(miss->depend.name, dp->name, PKG_NAME_LEN);
						if(!pm_list_is_in(miss, baddeps)) {
							baddeps = pm_list_add(baddeps, miss);
						} else {
							FREE(miss);
						}
					} else {
						/* see if dp provides something in tp's conflict list */
						PMList *m;
						for(m = dp->provides; m; m = m->next) {
							if(!strcmp(m->data, j->data)) {
								/* confict */
								MALLOC(miss, sizeof(pmdepmissing_t));
								miss->type = PM_DEP_TYPE_CONFLICT;
								miss->depend.mod = PM_DEP_MOD_ANY;
								miss->depend.version[0] = '\0';
								STRNCPY(miss->target, tp->name, PKG_NAME_LEN);
								STRNCPY(miss->depend.name, dp->name, PKG_NAME_LEN);
								if(!pm_list_is_in(miss, baddeps)) {
									baddeps = pm_list_add(baddeps, miss);
								} else {
									FREE(miss);
								}
							}
						}
					}
				}
				/* CHECK 2: check targets against targets */
				for(k = packages; k; k = k->next) {
					pmpkg_t *a = (pmpkg_t *)k->data;
					if(!strcmp(a->name, (char *)j->data)) {
						MALLOC(miss, sizeof(pmdepmissing_t));
						miss->type = PM_DEP_TYPE_CONFLICT;
						miss->depend.mod = PM_DEP_MOD_ANY;
						miss->depend.version[0] = '\0';
						STRNCPY(miss->target, tp->name, PKG_NAME_LEN);
						STRNCPY(miss->depend.name, a->name, PKG_NAME_LEN);
						if(!pm_list_is_in(miss, baddeps)) {
							baddeps = pm_list_add(baddeps, miss);
						} else {
							FREE(miss);
						}
					}
				}
			}
			/* CHECK 3: check database against targets */
			for(k = db_get_pkgcache(db); k; k = k->next) {
				int conflict = 0;
				info = k->data;
				for(j = info->conflicts; j; j = j->next) {
					if(!strcmp((char *)j->data, tp->name)) {
						MALLOC(miss, sizeof(pmdepmissing_t));
						miss->type = PM_DEP_TYPE_CONFLICT;
						miss->depend.mod = PM_DEP_MOD_ANY;
						miss->depend.version[0] = '\0';
						STRNCPY(miss->target, tp->name, PKG_NAME_LEN);
						STRNCPY(miss->depend.name, info->name, PKG_NAME_LEN);
						if(!pm_list_is_in(miss, baddeps)) {
							baddeps = pm_list_add(baddeps, miss);
						} else {
							FREE(miss);
						}
					} else {
						/* see if info and tp both provide something that info wants to
						 * exclusively provide (ie, conflicts&provides package X) */
						PMList *m;
						for(m = info->conflicts; m; m = m->next) {
							PMList *n;
							for(n = tp->provides; n; n = n->next) {
								if(!strcmp(m->data, n->data)) {
									MALLOC(miss, sizeof(pmdepmissing_t));
									miss->type = PM_DEP_TYPE_CONFLICT;
									miss->depend.mod = PM_DEP_MOD_ANY;
									miss->depend.version[0] = '\0';
									STRNCPY(miss->target, tp->name, PKG_NAME_LEN);
									STRNCPY(miss->depend.name, info->name, PKG_NAME_LEN);
									if(!pm_list_is_in(miss, baddeps)) {
										baddeps = pm_list_add(baddeps, miss);
									} else {
										FREE(miss);
									}
								}
							}
						}
					}
				}
				if(conflict) {
				}
			}

			/* PROVIDES -- check to see if another package already provides what
			 *             we offer
 			 */
			/* XXX: disabled -- we allow multiple packages to provide the same thing.
			 *      list packages in conflicts if they really do conflict.
			for(j = tp->provides; j; j = j->next) {
				PMList *provs = whatprovides(db, j->data);
				for(k = provs; k; k = k->next) {
					if(!strcmp(tp->name, k->data->name)) {
						// this is the same package -- skip it
						continue;
					}
					// we treat this just like a conflict
					MALLOC(miss, sizeof(pmdepmissing_t));
					miss->type = PM_DEP_TYPE_CONFLICT;
					miss->depend.mod = PM_DEP_MOD_ANY;
					miss->depend.version[0] = '\0';
					STRNCPY(miss->target, tp->name, PKG_NAME_LEN);
					STRNCPY(miss->depend.name, k->data, PKG_NAME_LEN);
					if(!pm_list_is_in(baddeps, miss)) {
						baddeps = pm_list_add(baddeps, miss);
					}
					k->data = NULL;
				}
				FREELIST(provs);
			}*/

			/* DEPENDENCIES -- look for unsatisfied dependencies */
			for(j = tp->depends; j; j = j->next) {
				/* split into name/version pairs */
				splitdep((char *)j->data, &depend);
				found = 0;
				/* check database for literal packages */
				for(k = db_get_pkgcache(db); k && !found; k = k->next) {
					pmpkg_t *p = (pmpkg_t *)k->data;
					if(!strcmp(p->name, depend.name)) {
						if(depend.mod == PM_DEP_MOD_ANY) {
							/* accept any version */
							found = 1;
						} else {
							char *ver = strdup(p->version);
							/* check for a release in depend.version.  if it's
							 * missing remove it from p->version as well.
							 */
							if(!index(depend.version,'-')) {
								char *ptr;
								for(ptr = ver; *ptr != '-'; ptr++);
								*ptr = '\0';
							}
							cmp = versioncmp(ver, depend.version);
							switch(depend.mod) {
								case PM_DEP_MOD_EQ: found = (cmp == 0); break;
								case PM_DEP_MOD_GE: found = (cmp >= 0); break;
								case PM_DEP_MOD_LE: found = (cmp <= 0); break;
							}
							FREE(ver);
						}
					}
				}
				/* check other targets */
				for(k = packages; k && !found; k = k->next) {
					pmpkg_t *p = (pmpkg_t *)k->data;
					/* see if the package names match OR if p provides depend.name */
					if(!strcmp(p->name, depend.name) || pm_list_is_strin(depend.name, p->provides)) {
						if(depend.mod == PM_DEP_MOD_ANY) {
							/* accept any version */
							found = 1;
						} else {
							char *ver = strdup(p->version);
							/* check for a release in depend.version.  if it's
							 * missing remove it from p->version as well.
							 */
							if(!index(depend.version,'-')) {
								char *ptr;
								for(ptr = ver; *ptr != '-'; ptr++);
								*ptr = '\0';
							}
							cmp = versioncmp(ver, depend.version);
							switch(depend.mod) {
								case PM_DEP_MOD_EQ: found = (cmp == 0); break;
								case PM_DEP_MOD_GE: found = (cmp >= 0); break;
								case PM_DEP_MOD_LE: found = (cmp <= 0); break;
							}
							FREE(ver);
						}
					}
				}
				/* check database for provides matches */
				if(!found){
					k = _alpm_db_whatprovides(db, depend.name);
					if(k) {
						/* grab the first one (there should only really be one, anyway) */
						pmpkg_t *p = k->data;
						if(depend.mod == PM_DEP_MOD_ANY) {
							/* accept any version */
							found = 1;
						} else {
							char *ver = strdup(p->version);
							/* check for a release in depend.version.  if it's
							 * missing remove it from p->version as well.
							 */
							if(!index(depend.version,'-')) {
								char *ptr;
								for(ptr = ver; *ptr != '-'; ptr++);
								*ptr = '\0';
							}
							cmp = versioncmp(ver, depend.version);
							switch(depend.mod) {
								case PM_DEP_MOD_EQ: found = (cmp == 0); break;
								case PM_DEP_MOD_GE: found = (cmp >= 0); break;
								case PM_DEP_MOD_LE: found = (cmp <= 0); break;
							}
							FREE(ver);
						}
						FREELISTPTR(k);
					}
				}
				/* else if still not found... */
				if(!found) {
					MALLOC(miss, sizeof(pmdepmissing_t));
					miss->type = PM_DEP_TYPE_DEPEND;
					miss->depend.mod = depend.mod;
					STRNCPY(miss->target, tp->name, PKG_NAME_LEN);
					STRNCPY(miss->depend.name, depend.name, PKG_NAME_LEN);
					STRNCPY(miss->depend.version, depend.version, PKG_VERSION_LEN);
					if(!pm_list_is_in(miss, baddeps)) {
						baddeps = pm_list_add(baddeps, miss);
					} else {
						FREE(miss);
					}
				}
			}
		}
	} else if(op == PM_TRANS_TYPE_REMOVE) {
		/* check requiredby fields */
		for(i = packages; i; i = i->next) {
			pmpkg_t *tp;
			if(i->data == NULL) {
				continue;
			}
			tp = (pmpkg_t*)i->data;
			for(j = tp->requiredby; j; j = j->next) {
				if(!pm_list_is_strin((char *)j->data, packages)) {
					MALLOC(miss, sizeof(pmdepmissing_t));
					miss->type = PM_DEP_TYPE_REQUIRED;
					miss->depend.mod = PM_DEP_MOD_ANY;
					miss->depend.version[0] = '\0';
					STRNCPY(miss->target, tp->name, PKG_NAME_LEN);
					STRNCPY(miss->depend.name, (char *)j->data, PKG_NAME_LEN);
					if(!pm_list_is_in(miss, baddeps)) {
						baddeps = pm_list_add(baddeps, miss);
					} else {
						FREE(miss);
					}
				}
			}
		}
	}

	return(baddeps);
}

int splitdep(char *depstr, pmdepend_t *depend)
{
	char *str = NULL;
	char *ptr = NULL;

	if(depstr == NULL || depend == NULL) {
		return(-1);
	}

	depend->mod = 0;
	depend->name[0] = 0;
	depend->version[0] = 0;

	str = strdup(depstr);

	if((ptr = strstr(str, ">="))) {
		depend->mod = PM_DEP_MOD_GE;
	} else if((ptr = strstr(str, "<="))) {
		depend->mod = PM_DEP_MOD_LE;
	} else if((ptr = strstr(str, "="))) {
		depend->mod = PM_DEP_MOD_EQ;
	} else {
		/* no version specified - accept any */
		depend->mod = PM_DEP_MOD_ANY;
		STRNCPY(depend->name, str, PKG_NAME_LEN);
	}

	if(ptr == NULL) {
		FREE(str);
		return(0);
	}
	*ptr = '\0';
	STRNCPY(depend->name, str, PKG_NAME_LEN);
	ptr++;
	if(depend->mod != PM_DEP_MOD_EQ) {
		ptr++;
	}
	STRNCPY(depend->version, ptr, PKG_VERSION_LEN);
	FREE(str);

	return(0);
}

/* return a new PMList 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.
 */
PMList* removedeps(pmdb_t *db, PMList *targs)
{
	PMList *i, *j, *k;
	PMList *newtargs = targs;

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

	for(i = targs; i; i = i->next) {
		for(j = ((pmpkg_t *)i->data)->depends; j; j = j->next) {
			pmdepend_t depend;
			pmpkg_t *dep;
			int needed = 0;

			if(splitdep(j->data, &depend)) {
				continue;
			}

			dep = db_get_pkgfromcache(db, depend.name);
			if(dep == NULL) {
				/* package not found... look for a provisio instead */
				k = _alpm_db_whatprovides(db, depend.name);
				if(k == NULL) {
					_alpm_log(PM_LOG_WARNING, "cannot find package \"%s\" or anything that provides it!", depend.name);
					continue;
				}
				dep = db_get_pkgfromcache(db, ((pmpkg_t *)k->data)->name);
				if(dep == NULL) {
					_alpm_log(PM_LOG_ERROR, "dep is NULL!");
					continue;
				}
				FREELISTPTR(k);
			}
			if(pkg_isin(dep, targs)) {
				continue;
			}

			/* see if it was explicitly installed */
			if(dep->reason == PM_PKG_REASON_EXPLICIT) {
				_alpm_log(PM_LOG_FLOW2, "excluding %s -- explicitly installed", dep->name);
				needed = 1;
			}

			/* see if other packages need it */
			for(k = dep->requiredby; k && !needed; k = k->next) {
				pmpkg_t *dummy = db_get_pkgfromcache(db, k->data);
				if(!pkg_isin(dummy, targs)) {
					needed = 1;
				}
			}
			if(!needed) {
				char *name;
				pmpkg_t *pkg = pkg_dummy(dep->name, dep->version);
				if(pkg == NULL) {
					_alpm_log(PM_LOG_ERROR, "could not allocate memory for a package structure");
					continue;
				}
				asprintf(&name, "%s-%s", dep->name, dep->version);
				/* add it to the target list */
				db_read(db, name, INFRQ_ALL, pkg);
				newtargs = pm_list_add(newtargs, pkg);
				_alpm_log(PM_LOG_FLOW2, "adding %s to the targets", pkg->name);
				newtargs = removedeps(db, newtargs);
				FREE(name);
			}
		}
	}

	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 resolvedeps(pmdb_t *local, PMList *dbs_sync, pmpkg_t *syncpkg, PMList *list, PMList *trail, pmtrans_t *trans)
{
	PMList *i, *j, *k;
	PMList *targ;
	PMList *deps = NULL;

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

	targ = pm_list_add(NULL, syncpkg);
	deps = checkdeps(local, PM_TRANS_TYPE_ADD, targ);
	FREELISTPTR(targ);

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

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

		/* XXX: conflicts are now treated specially in the _add and _sync functions */

		/*if(miss->type == PM_DEP_TYPE_CONFLICT) {
			_alpm_log(PM_LOG_ERROR, "cannot resolve dependencies for \"%s\" (it conflict with %s)", miss->target, miss->depend.name);
			RET_ERR(???, -1);
		} else*/

		if(miss->type == PM_DEP_TYPE_DEPEND) {
			pmpkg_t *sync = NULL;
			int provisio_match = 0;

			_alpm_log(PM_LOG_DEBUG, "resolving dependency %s", miss->depend.name);

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

			/* find the package in one of the repositories */
			/* check literals */
			for(j = dbs_sync; !sync && j; j = j->next) {
				PMList *k;
				pmdb_t *dbs = j->data;
				for(k = db_get_pkgcache(dbs); !sync && k; k = k->next) {
					pmpkg_t *pkg = k->data;
					if(!strcmp(miss->depend.name, pkg->name)) {
						sync = pkg;
					}
				}
			}
			/* check provides */
			for(j = dbs_sync; !sync && j; j = j->next) {
				PMList *provides;
				pmdb_t *dbs = j->data;
				provides = _alpm_db_whatprovides(dbs, miss->depend.name);
				if(provides) {
					sync = provides->data;
				}
				FREELISTPTR(provides);
			}
			if(sync == NULL) {
				_alpm_log(PM_LOG_ERROR, "cannot resolve dependencies for \"%s\" (\"%s\" is not in the package set)",
				          miss->target, miss->depend.name);
				pm_errno = PM_ERR_UNRESOLVABLE_DEPS;
				goto error;
			}
			found = 0;
			for(j = list; j && !found; j = j->next) {
				pmpkg_t *tmp = j->data;
				if(tmp && !strcmp(tmp->name, sync->name)) {
					_alpm_log(PM_LOG_DEBUG, "dependency %s is already in the target list - skipping",
					          sync->name);
					found = 1;
				}
			}
			if(found) {
				/* this dep is already in the target list */
				continue;
			}

			found = 0;
			for(j = trail; j; j = j->next) {
				pmpkg_t *tmp = j->data;
				if(tmp && !strcmp(tmp->name, sync->name)) {
					found = 1;
				}
			}
			if(!found) {
				/* check pmo_ignorepkg and pmo_s_ignore to make sure we haven't pulled in
				 * something we're not supposed to.
				 */
				int usedep = 1;
				found = 0;
				for(j = handle->ignorepkg; j && !found; j = j->next) {
					if(!strcmp(j->data, sync->name)) {
						found = 1;
					}
				}
				if(found) {
					pmpkg_t *dummypkg = pkg_dummy(miss->target, NULL);
					QUESTION(trans, PM_TRANS_CONV_INSTALL_IGNOREPKG, dummypkg, sync, NULL, &usedep);
					FREEPKG(dummypkg);
				}
				if(usedep) {
					trail = pm_list_add(trail, sync);
					if(resolvedeps(local, dbs_sync, sync, list, trail, trans)) {
						goto error;
					}
					_alpm_log(PM_LOG_DEBUG, "pulling dependency %s (needed by %s)",
					          sync->name, syncpkg->name);
					list = pm_list_add(list, sync);
				} else {
					_alpm_log(PM_LOG_ERROR, "cannot resolve dependencies for \"%s\"", miss->target);
					pm_errno = PM_ERR_UNRESOLVABLE_DEPS;
					goto error;
				}
			} else {
				/* cycle detected -- skip it */
				_alpm_log(PM_LOG_DEBUG, "dependency cycle detected: %s", sync->name);
			}
		}
	}

	FREELIST(deps);

	return(0);

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

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