Merge branch 'maint'
[pacman-ng.git] / lib / libalpm / deps.c
blobdda5ed7921d8eac7df09ac272e646dd60cfc4831
1 /*
2 * deps.c
4 * Copyright (c) 2006-2011 Pacman Development Team <pacman-dev@archlinux.org>
5 * Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org>
6 * Copyright (c) 2005 by Aurelien Foret <orelien@chez.com>
7 * Copyright (c) 2005, 2006 by Miklos Vajna <vmiklos@frugalware.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "config.h"
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
29 /* libalpm */
30 #include "deps.h"
31 #include "alpm_list.h"
32 #include "util.h"
33 #include "log.h"
34 #include "graph.h"
35 #include "package.h"
36 #include "db.h"
37 #include "handle.h"
39 void _alpm_dep_free(pmdepend_t *dep)
41 FREE(dep->name);
42 FREE(dep->version);
43 FREE(dep);
46 static pmdepmissing_t *depmiss_new(const char *target, pmdepend_t *dep,
47 const char *causingpkg)
49 pmdepmissing_t *miss;
51 ALPM_LOG_FUNC;
53 MALLOC(miss, sizeof(pmdepmissing_t), RET_ERR(PM_ERR_MEMORY, NULL));
55 STRDUP(miss->target, target, RET_ERR(PM_ERR_MEMORY, NULL));
56 miss->depend = _alpm_dep_dup(dep);
57 STRDUP(miss->causingpkg, causingpkg, RET_ERR(PM_ERR_MEMORY, NULL));
59 return miss;
62 void _alpm_depmiss_free(pmdepmissing_t *miss)
64 _alpm_dep_free(miss->depend);
65 FREE(miss->target);
66 FREE(miss->causingpkg);
67 FREE(miss);
70 /* Does pkg1 depend on pkg2, ie. does pkg2 satisfy a dependency of pkg1? */
71 static int _alpm_dep_edge(pmpkg_t *pkg1, pmpkg_t *pkg2)
73 alpm_list_t *i;
74 for(i = alpm_pkg_get_depends(pkg1); i; i = i->next) {
75 if(_alpm_depcmp(pkg2, i->data)) {
76 return 1;
79 return 0;
82 /* Convert a list of pmpkg_t * to a graph structure,
83 * with a edge for each dependency.
84 * Returns a list of vertices (one vertex = one package)
85 * (used by alpm_sortbydeps)
87 static alpm_list_t *dep_graph_init(alpm_list_t *targets)
89 alpm_list_t *i, *j;
90 alpm_list_t *vertices = NULL;
91 /* We create the vertices */
92 for(i = targets; i; i = i->next) {
93 pmgraph_t *vertex = _alpm_graph_new();
94 vertex->data = (void *)i->data;
95 vertices = alpm_list_add(vertices, vertex);
98 /* We compute the edges */
99 for(i = vertices; i; i = i->next) {
100 pmgraph_t *vertex_i = i->data;
101 pmpkg_t *p_i = vertex_i->data;
102 /* TODO this should be somehow combined with alpm_checkdeps */
103 for(j = vertices; j; j = j->next) {
104 pmgraph_t *vertex_j = j->data;
105 pmpkg_t *p_j = vertex_j->data;
106 if(_alpm_dep_edge(p_i, p_j)) {
107 vertex_i->children =
108 alpm_list_add(vertex_i->children, vertex_j);
111 vertex_i->childptr = vertex_i->children;
113 return vertices;
116 /* Re-order a list of target packages with respect to their dependencies.
118 * Example (reverse == 0):
119 * A depends on C
120 * B depends on A
121 * Target order is A,B,C,D
123 * Should be re-ordered to C,A,B,D
125 * if reverse is > 0, the dependency order will be reversed.
127 * This function returns the new alpm_list_t* target list.
130 alpm_list_t *_alpm_sortbydeps(alpm_list_t *targets, int reverse)
132 alpm_list_t *newtargs = NULL;
133 alpm_list_t *vertices = NULL;
134 alpm_list_t *vptr;
135 pmgraph_t *vertex;
137 ALPM_LOG_FUNC;
139 if(targets == NULL) {
140 return NULL;
143 _alpm_log(PM_LOG_DEBUG, "started sorting dependencies\n");
145 vertices = dep_graph_init(targets);
147 vptr = vertices;
148 vertex = vertices->data;
149 while(vptr) {
150 /* mark that we touched the vertex */
151 vertex->state = -1;
152 int found = 0;
153 while(vertex->childptr && !found) {
154 pmgraph_t *nextchild = vertex->childptr->data;
155 vertex->childptr = vertex->childptr->next;
156 if (nextchild->state == 0) {
157 found = 1;
158 nextchild->parent = vertex;
159 vertex = nextchild;
161 else if(nextchild->state == -1) {
162 pmpkg_t *vertexpkg = vertex->data;
163 pmpkg_t *childpkg = nextchild->data;
164 const char *message;
166 _alpm_log(PM_LOG_WARNING, _("dependency cycle detected:\n"));
167 if(reverse) {
168 message =_("%s will be removed after its %s dependency\n");
169 } else {
170 message =_("%s will be installed before its %s dependency\n");
172 _alpm_log(PM_LOG_WARNING, message, vertexpkg->name, childpkg->name);
175 if(!found) {
176 newtargs = alpm_list_add(newtargs, vertex->data);
177 /* mark that we've left this vertex */
178 vertex->state = 1;
179 vertex = vertex->parent;
180 if(!vertex) {
181 vptr = vptr->next;
182 while(vptr) {
183 vertex = vptr->data;
184 if (vertex->state == 0) break;
185 vptr = vptr->next;
191 _alpm_log(PM_LOG_DEBUG, "sorting dependencies finished\n");
193 if(reverse) {
194 /* reverse the order */
195 alpm_list_t *tmptargs = alpm_list_reverse(newtargs);
196 /* free the old one */
197 alpm_list_free(newtargs);
198 newtargs = tmptargs;
201 alpm_list_free_inner(vertices, _alpm_graph_free);
202 alpm_list_free(vertices);
204 return newtargs;
207 static int no_dep_version(void)
209 int flags = alpm_trans_get_flags();
210 return flags != -1 && (flags & PM_TRANS_FLAG_NODEPVERSION);
213 static pmdepend_t *filtered_depend(pmdepend_t *dep, int nodepversion)
215 if(nodepversion) {
216 pmdepend_t *newdep = _alpm_dep_dup(dep);
217 ASSERT(newdep, return dep);
218 newdep->mod = PM_DEP_MOD_ANY;
219 dep = newdep;
221 return dep;
224 static void release_filtered_depend(pmdepend_t *dep, int nodepversion)
226 if(nodepversion) {
227 free(dep);
231 static pmpkg_t *find_dep_satisfier(alpm_list_t *pkgs, pmdepend_t *dep)
233 alpm_list_t *i;
235 for(i = pkgs; i; i = alpm_list_next(i)) {
236 pmpkg_t *pkg = i->data;
237 if(_alpm_depcmp(pkg, dep)) {
238 return pkg;
241 return NULL;
244 /** Find a package satisfying a specified dependency.
245 * The dependency can include versions with depmod operators.
246 * @param pkgs an alpm_list_t* of pmpkg_t where the satisfier will be searched
247 * @param depstring package or provision name, versioned or not
248 * @return a pmpkg_t* satisfying depstring
250 pmpkg_t SYMEXPORT *alpm_find_satisfier(alpm_list_t *pkgs, const char *depstring)
252 pmdepend_t *dep = _alpm_splitdep(depstring);
253 pmpkg_t *pkg = find_dep_satisfier(pkgs, dep);
254 _alpm_dep_free(dep);
255 return pkg;
258 /** Checks dependencies and returns missing ones in a list.
259 * Dependencies can include versions with depmod operators.
260 * @param pkglist the list of local packages
261 * @param reversedeps handles the backward dependencies
262 * @param remove an alpm_list_t* of packages to be removed
263 * @param upgrade an alpm_list_t* of packages to be upgraded (remove-then-upgrade)
264 * @return an alpm_list_t* of pmdepmissing_t pointers.
266 alpm_list_t SYMEXPORT *alpm_checkdeps(alpm_list_t *pkglist, int reversedeps,
267 alpm_list_t *remove, alpm_list_t *upgrade)
269 alpm_list_t *i, *j;
270 alpm_list_t *targets, *dblist = NULL, *modified = NULL;
271 alpm_list_t *baddeps = NULL;
272 int nodepversion;
274 ALPM_LOG_FUNC;
276 targets = alpm_list_join(alpm_list_copy(remove), alpm_list_copy(upgrade));
277 for(i = pkglist; i; i = i->next) {
278 pmpkg_t *pkg = i->data;
279 if(_alpm_pkg_find(targets, pkg->name)) {
280 modified = alpm_list_add(modified, pkg);
281 } else {
282 dblist = alpm_list_add(dblist, pkg);
285 alpm_list_free(targets);
287 nodepversion = no_dep_version();
289 /* look for unsatisfied dependencies of the upgrade list */
290 for(i = upgrade; i; i = i->next) {
291 pmpkg_t *tp = i->data;
292 _alpm_log(PM_LOG_DEBUG, "checkdeps: package %s-%s\n",
293 alpm_pkg_get_name(tp), alpm_pkg_get_version(tp));
295 for(j = alpm_pkg_get_depends(tp); j; j = j->next) {
296 pmdepend_t *depend = j->data;
297 depend = filtered_depend(depend, nodepversion);
298 /* 1. we check the upgrade list */
299 /* 2. we check database for untouched satisfying packages */
300 if(!find_dep_satisfier(upgrade, depend) &&
301 !find_dep_satisfier(dblist, depend)) {
302 /* Unsatisfied dependency in the upgrade list */
303 pmdepmissing_t *miss;
304 char *missdepstring = alpm_dep_compute_string(depend);
305 _alpm_log(PM_LOG_DEBUG, "checkdeps: missing dependency '%s' for package '%s'\n",
306 missdepstring, alpm_pkg_get_name(tp));
307 free(missdepstring);
308 miss = depmiss_new(alpm_pkg_get_name(tp), depend, NULL);
309 baddeps = alpm_list_add(baddeps, miss);
311 release_filtered_depend(depend, nodepversion);
315 if(reversedeps) {
316 /* reversedeps handles the backwards dependencies, ie,
317 * the packages listed in the requiredby field. */
318 for(i = dblist; i; i = i->next) {
319 pmpkg_t *lp = i->data;
320 for(j = alpm_pkg_get_depends(lp); j; j = j->next) {
321 pmdepend_t *depend = j->data;
322 depend = filtered_depend(depend, nodepversion);
323 pmpkg_t *causingpkg = find_dep_satisfier(modified, depend);
324 /* we won't break this depend, if it is already broken, we ignore it */
325 /* 1. check upgrade list for satisfiers */
326 /* 2. check dblist for satisfiers */
327 if(causingpkg &&
328 !find_dep_satisfier(upgrade, depend) &&
329 !find_dep_satisfier(dblist, depend)) {
330 pmdepmissing_t *miss;
331 char *missdepstring = alpm_dep_compute_string(depend);
332 _alpm_log(PM_LOG_DEBUG, "checkdeps: transaction would break '%s' dependency of '%s'\n",
333 missdepstring, alpm_pkg_get_name(lp));
334 free(missdepstring);
335 miss = depmiss_new(lp->name, depend, alpm_pkg_get_name(causingpkg));
336 baddeps = alpm_list_add(baddeps, miss);
338 release_filtered_depend(depend, nodepversion);
343 alpm_list_free(modified);
344 alpm_list_free(dblist);
346 return baddeps;
349 static int dep_vercmp(const char *version1, pmdepmod_t mod,
350 const char *version2)
352 int equal = 0;
354 if(mod == PM_DEP_MOD_ANY) {
355 equal = 1;
356 } else {
357 int cmp = alpm_pkg_vercmp(version1, version2);
358 switch(mod) {
359 case PM_DEP_MOD_EQ: equal = (cmp == 0); break;
360 case PM_DEP_MOD_GE: equal = (cmp >= 0); break;
361 case PM_DEP_MOD_LE: equal = (cmp <= 0); break;
362 case PM_DEP_MOD_LT: equal = (cmp < 0); break;
363 case PM_DEP_MOD_GT: equal = (cmp > 0); break;
364 default: equal = 1; break;
367 return equal;
370 int _alpm_depcmp(pmpkg_t *pkg, pmdepend_t *dep)
372 alpm_list_t *i;
373 int satisfy = 0;
375 /* check (pkg->name, pkg->version) */
376 if(pkg->name_hash && dep->name_hash
377 && pkg->name_hash != dep->name_hash) {
378 /* skip more expensive checks */
379 } else {
380 satisfy = (strcmp(pkg->name, dep->name) == 0
381 && dep_vercmp(pkg->version, dep->mod, dep->version));
382 if(satisfy) {
383 return satisfy;
387 /* check provisions, format : "name=version" */
388 for(i = alpm_pkg_get_provides(pkg); i && !satisfy; i = i->next) {
389 const char *provision = i->data;
390 const char *provver = strchr(provision, '=');
392 if(provver == NULL) { /* no provision version */
393 satisfy = (dep->mod == PM_DEP_MOD_ANY
394 && strcmp(provision, dep->name) == 0);
395 } else {
396 /* This is a bit tricker than the old code for performance reasons. To
397 * prevent the need to copy and duplicate strings, strncmp only the name
398 * portion if they are the same length, since there is a version and
399 * operator in play here. Cast is to silence sign conversion warning;
400 * we know provver >= provision if we are here. */
401 size_t namelen = (size_t)(provver - provision);
402 provver += 1;
403 satisfy = (strlen(dep->name) == namelen
404 && strncmp(provision, dep->name, namelen) == 0
405 && dep_vercmp(provver, dep->mod, dep->version));
409 return satisfy;
412 pmdepend_t *_alpm_splitdep(const char *depstring)
414 pmdepend_t *depend;
415 const char *ptr, *version = NULL;
417 if(depstring == NULL) {
418 return NULL;
421 CALLOC(depend, 1, sizeof(pmdepend_t), RET_ERR(PM_ERR_MEMORY, NULL));
423 /* Find a version comparator if one exists. If it does, set the type and
424 * increment the ptr accordingly so we can copy the right strings. */
425 if((ptr = strstr(depstring, ">="))) {
426 depend->mod = PM_DEP_MOD_GE;
427 version = ptr + 2;
428 } else if((ptr = strstr(depstring, "<="))) {
429 depend->mod = PM_DEP_MOD_LE;
430 version = ptr + 2;
431 } else if((ptr = strstr(depstring, "="))) {
432 /* Note: we must do =,<,> checks after <=, >= checks */
433 depend->mod = PM_DEP_MOD_EQ;
434 version = ptr + 1;
435 } else if((ptr = strstr(depstring, "<"))) {
436 depend->mod = PM_DEP_MOD_LT;
437 version = ptr + 1;
438 } else if((ptr = strstr(depstring, ">"))) {
439 depend->mod = PM_DEP_MOD_GT;
440 version = ptr + 1;
441 } else {
442 /* no version specified, leave version and ptr NULL */
443 depend->mod = PM_DEP_MOD_ANY;
446 /* copy the right parts to the right places */
447 STRNDUP(depend->name, depstring, ptr - depstring,
448 RET_ERR(PM_ERR_MEMORY, NULL));
449 depend->name_hash = _alpm_hash_sdbm(depend->name);
450 if(version) {
451 STRDUP(depend->version, version, RET_ERR(PM_ERR_MEMORY, NULL));
454 return depend;
457 pmdepend_t *_alpm_dep_dup(const pmdepend_t *dep)
459 pmdepend_t *newdep;
460 CALLOC(newdep, 1, sizeof(pmdepend_t), RET_ERR(PM_ERR_MEMORY, NULL));
462 STRDUP(newdep->name, dep->name, RET_ERR(PM_ERR_MEMORY, NULL));
463 newdep->name_hash = dep->name_hash;
464 STRDUP(newdep->version, dep->version, RET_ERR(PM_ERR_MEMORY, NULL));
465 newdep->mod = dep->mod;
467 return newdep;
470 /* These parameters are messy. We check if this package, given a list of
471 * targets and a db is safe to remove. We do NOT remove it if it is in the
472 * target list, or if if the package was explictly installed and
473 * include_explicit == 0 */
474 static int can_remove_package(pmdb_t *db, pmpkg_t *pkg, alpm_list_t *targets,
475 int include_explicit)
477 alpm_list_t *i;
479 if(_alpm_pkg_find(targets, alpm_pkg_get_name(pkg))) {
480 return 0;
483 if(!include_explicit) {
484 /* see if it was explicitly installed */
485 if(alpm_pkg_get_reason(pkg) == PM_PKG_REASON_EXPLICIT) {
486 _alpm_log(PM_LOG_DEBUG, "excluding %s -- explicitly installed\n",
487 alpm_pkg_get_name(pkg));
488 return 0;
492 /* TODO: checkdeps could be used here, it handles multiple providers
493 * better, but that also makes it slower.
494 * Also this would require to first add the package to the targets list,
495 * then call checkdeps with it, then remove the package from the targets list
496 * if checkdeps detected it would break something */
498 /* see if other packages need it */
499 for(i = _alpm_db_get_pkgcache(db); i; i = i->next) {
500 pmpkg_t *lpkg = i->data;
501 if(_alpm_dep_edge(lpkg, pkg) && !_alpm_pkg_find(targets, lpkg->name)) {
502 return 0;
506 /* it's ok to remove */
507 return 1;
511 * @brief Adds unneeded dependencies to an existing list of packages.
512 * By unneeded, we mean dependencies that are only required by packages in the
513 * target list, so they can be safely removed.
514 * If the input list was topo sorted, the output list will be topo sorted too.
516 * @param db package database to do dependency tracing in
517 * @param *targs pointer to a list of packages
518 * @param include_explicit if 0, explicitly installed packages are not included
520 void _alpm_recursedeps(pmdb_t *db, alpm_list_t *targs, int include_explicit)
522 alpm_list_t *i, *j;
524 ALPM_LOG_FUNC;
526 if(db == NULL || targs == NULL) {
527 return;
530 for(i = targs; i; i = i->next) {
531 pmpkg_t *pkg = i->data;
532 for(j = _alpm_db_get_pkgcache(db); j; j = j->next) {
533 pmpkg_t *deppkg = j->data;
534 if(_alpm_dep_edge(pkg, deppkg)
535 && can_remove_package(db, deppkg, targs, include_explicit)) {
536 _alpm_log(PM_LOG_DEBUG, "adding '%s' to the targets\n",
537 alpm_pkg_get_name(deppkg));
538 /* add it to the target list */
539 targs = alpm_list_add(targs, _alpm_pkg_dup(deppkg));
546 * helper function for resolvedeps: search for dep satisfier in dbs
548 * @param dep is the dependency to search for
549 * @param dbs are the databases to search
550 * @param excluding are the packages to exclude from the search
551 * @param prompt if true, will cause an unresolvable dependency to issue an
552 * interactive prompt asking whether the package should be removed from
553 * the transaction or the transaction aborted; if false, simply returns
554 * an error code without prompting
555 * @return the resolved package
557 static pmpkg_t *resolvedep(pmdepend_t *dep, alpm_list_t *dbs,
558 alpm_list_t *excluding, int prompt)
560 alpm_list_t *i, *j;
561 int ignored = 0;
563 alpm_list_t *providers = NULL;
564 int count;
566 /* 1. literals */
567 for(i = dbs; i; i = i->next) {
568 pmpkg_t *pkg = _alpm_db_get_pkgfromcache(i->data, dep->name);
569 if(pkg && _alpm_depcmp(pkg, dep) && !_alpm_pkg_find(excluding, pkg->name)) {
570 if(_alpm_pkg_should_ignore(pkg)) {
571 int install = 0;
572 if (prompt) {
573 QUESTION(handle->trans, PM_TRANS_CONV_INSTALL_IGNOREPKG, pkg,
574 NULL, NULL, &install);
575 } else {
576 _alpm_log(PM_LOG_WARNING, _("ignoring package %s-%s\n"), pkg->name, pkg->version);
578 if(!install) {
579 ignored = 1;
580 continue;
583 return pkg;
586 /* 2. satisfiers (skip literals here) */
587 for(i = dbs; i; i = i->next) {
588 for(j = _alpm_db_get_pkgcache(i->data); j; j = j->next) {
589 pmpkg_t *pkg = j->data;
590 if(_alpm_depcmp(pkg, dep) && strcmp(pkg->name, dep->name) != 0 &&
591 !_alpm_pkg_find(excluding, pkg->name)) {
592 if(_alpm_pkg_should_ignore(pkg)) {
593 int install = 0;
594 if (prompt) {
595 QUESTION(handle->trans, PM_TRANS_CONV_INSTALL_IGNOREPKG,
596 pkg, NULL, NULL, &install);
597 } else {
598 _alpm_log(PM_LOG_WARNING, _("ignoring package %s-%s\n"), pkg->name, pkg->version);
600 if(!install) {
601 ignored = 1;
602 continue;
605 _alpm_log(PM_LOG_DEBUG, "provider found (%s provides %s)\n",
606 pkg->name, dep->name);
607 providers = alpm_list_add(providers, pkg);
608 /* keep looking for other providers in the all dbs */
613 /* first check if one provider is already installed locally */
614 for(i = providers; i; i = i->next) {
615 pmpkg_t *pkg = i->data;
616 if (_alpm_pkghash_find(_alpm_db_get_pkgcache_hash(handle->db_local), pkg->name)) {
617 alpm_list_free(providers);
618 return pkg;
621 count = alpm_list_count(providers);
622 if (count >= 1) {
623 /* default to first provider if there is no QUESTION callback */
624 int index = 0;
625 if(count > 1) {
626 /* if there is more than one provider, we ask the user */
627 QUESTION(handle->trans, PM_TRANS_CONV_SELECT_PROVIDER,
628 providers, dep, NULL, &index);
630 if(index >= 0 && index < count) {
631 pmpkg_t *pkg = alpm_list_getdata(alpm_list_nth(providers, index));
632 alpm_list_free(providers);
633 return pkg;
635 alpm_list_free(providers);
636 providers = NULL;
639 if(ignored) { /* resolvedeps will override these */
640 pm_errno = PM_ERR_PKG_IGNORED;
641 } else {
642 pm_errno = PM_ERR_PKG_NOT_FOUND;
644 return NULL;
647 /** Find a package satisfying a specified dependency.
648 * First look for a literal, going through each db one by one. Then look for
649 * providers. The first satisfier found is returned.
650 * The dependency can include versions with depmod operators.
651 * @param dbs an alpm_list_t* of pmdb_t where the satisfier will be searched
652 * @param depstring package or provision name, versioned or not
653 * @return a pmpkg_t* satisfying depstring
655 pmpkg_t SYMEXPORT *alpm_find_dbs_satisfier(alpm_list_t *dbs, const char *depstring)
657 pmdepend_t *dep;
658 pmpkg_t *pkg;
660 ASSERT(dbs, return NULL);
662 dep = _alpm_splitdep(depstring);
663 ASSERT(dep, return NULL);
664 pkg = resolvedep(dep, dbs, NULL, 1);
665 _alpm_dep_free(dep);
666 return pkg;
670 * Computes resolvable dependencies for a given package and adds that package
671 * and those resolvable dependencies to a list.
673 * @param localpkgs is the list of local packages
674 * @param dbs_sync are the sync databases
675 * @param pkg is the package to resolve
676 * @param packages is a pointer to a list of packages which will be
677 * searched first for any dependency packages needed to complete the
678 * resolve, and to which will be added any [pkg] and all of its
679 * dependencies not already on the list
680 * @param remove is the set of packages which will be removed in this
681 * transaction
682 * @param data returns the dependency which could not be satisfied in the
683 * event of an error
684 * @return 0 on success, with [pkg] and all of its dependencies not already on
685 * the [*packages] list added to that list, or -1 on failure due to an
686 * unresolvable dependency, in which case the [*packages] list will be
687 * unmodified by this function
689 int _alpm_resolvedeps(alpm_list_t *localpkgs, alpm_list_t *dbs_sync, pmpkg_t *pkg,
690 alpm_list_t *preferred, alpm_list_t **packages,
691 alpm_list_t *remove, alpm_list_t **data)
693 int ret = 0;
694 alpm_list_t *i, *j;
695 alpm_list_t *targ;
696 alpm_list_t *deps = NULL;
697 alpm_list_t *packages_copy;
699 ALPM_LOG_FUNC;
701 if(_alpm_pkg_find(*packages, pkg->name) != NULL) {
702 return 0;
705 /* Create a copy of the packages list, so that it can be restored
706 on error */
707 packages_copy = alpm_list_copy(*packages);
708 /* [pkg] has not already been resolved into the packages list, so put it
709 on that list */
710 *packages = alpm_list_add(*packages, pkg);
712 _alpm_log(PM_LOG_DEBUG, "started resolving dependencies\n");
713 for(i = alpm_list_last(*packages); i; i = i->next) {
714 pmpkg_t *tpkg = i->data;
715 targ = alpm_list_add(NULL, tpkg);
716 deps = alpm_checkdeps(localpkgs, 0, remove, targ);
717 alpm_list_free(targ);
719 for(j = deps; j; j = j->next) {
720 pmdepmissing_t *miss = j->data;
721 pmdepend_t *missdep = alpm_miss_get_dep(miss);
722 /* check if one of the packages in the [*packages] list already satisfies
723 * this dependency */
724 if(find_dep_satisfier(*packages, missdep)) {
725 _alpm_depmiss_free(miss);
726 continue;
728 /* check if one of the packages in the [preferred] list already satisfies
729 * this dependency */
730 pmpkg_t *spkg = find_dep_satisfier(preferred, missdep);
731 if(!spkg) {
732 /* find a satisfier package in the given repositories */
733 spkg = resolvedep(missdep, dbs_sync, *packages, 0);
735 if(!spkg) {
736 pm_errno = PM_ERR_UNSATISFIED_DEPS;
737 char *missdepstring = alpm_dep_compute_string(missdep);
738 _alpm_log(PM_LOG_WARNING,
739 _("cannot resolve \"%s\", a dependency of \"%s\"\n"),
740 missdepstring, tpkg->name);
741 free(missdepstring);
742 if(data) {
743 *data = alpm_list_add(*data, miss);
745 ret = -1;
746 } else {
747 _alpm_log(PM_LOG_DEBUG, "pulling dependency %s (needed by %s)\n",
748 alpm_pkg_get_name(spkg), alpm_pkg_get_name(tpkg));
749 *packages = alpm_list_add(*packages, spkg);
750 _alpm_depmiss_free(miss);
753 alpm_list_free(deps);
756 if(ret != 0) {
757 alpm_list_free(*packages);
758 *packages = packages_copy;
759 } else {
760 alpm_list_free(packages_copy);
762 _alpm_log(PM_LOG_DEBUG, "finished resolving dependencies\n");
763 return ret;
766 const char SYMEXPORT *alpm_miss_get_target(const pmdepmissing_t *miss)
768 ALPM_LOG_FUNC;
770 /* Sanity checks */
771 ASSERT(miss != NULL, return NULL);
773 return miss->target;
776 const char SYMEXPORT *alpm_miss_get_causingpkg(const pmdepmissing_t *miss)
778 ALPM_LOG_FUNC;
780 /* Sanity checks */
781 ASSERT(miss != NULL, return NULL);
783 return miss->causingpkg;
786 pmdepend_t SYMEXPORT *alpm_miss_get_dep(pmdepmissing_t *miss)
788 ALPM_LOG_FUNC;
790 /* Sanity checks */
791 ASSERT(miss != NULL, return NULL);
793 return miss->depend;
796 pmdepmod_t SYMEXPORT alpm_dep_get_mod(const pmdepend_t *dep)
798 ALPM_LOG_FUNC;
800 /* Sanity checks */
801 ASSERT(dep != NULL, return -1);
803 return dep->mod;
806 const char SYMEXPORT *alpm_dep_get_name(const pmdepend_t *dep)
808 ALPM_LOG_FUNC;
810 /* Sanity checks */
811 ASSERT(dep != NULL, return NULL);
813 return dep->name;
816 const char SYMEXPORT *alpm_dep_get_version(const pmdepend_t *dep)
818 ALPM_LOG_FUNC;
820 /* Sanity checks */
821 ASSERT(dep != NULL, return NULL);
823 return dep->version;
826 /** Reverse of splitdep; make a dep string from a pmdepend_t struct.
827 * The string must be freed!
828 * @param dep the depend to turn into a string
829 * @return a string-formatted dependency with operator if necessary
831 char SYMEXPORT *alpm_dep_compute_string(const pmdepend_t *dep)
833 const char *name, *opr, *ver;
834 char *str;
835 size_t len;
837 ALPM_LOG_FUNC;
839 /* Sanity checks */
840 ASSERT(dep != NULL, return NULL);
842 if(dep->name) {
843 name = dep->name;
844 } else {
845 name = "";
848 switch(dep->mod) {
849 case PM_DEP_MOD_ANY:
850 opr = "";
851 break;
852 case PM_DEP_MOD_GE:
853 opr = ">=";
854 break;
855 case PM_DEP_MOD_LE:
856 opr = "<=";
857 break;
858 case PM_DEP_MOD_EQ:
859 opr = "=";
860 break;
861 case PM_DEP_MOD_LT:
862 opr = "<";
863 break;
864 case PM_DEP_MOD_GT:
865 opr = ">";
866 break;
867 default:
868 opr = "";
869 break;
872 if(dep->mod != PM_DEP_MOD_ANY && dep->version) {
873 ver = dep->version;
874 } else {
875 ver = "";
878 /* we can always compute len and print the string like this because opr
879 * and ver will be empty when PM_DEP_MOD_ANY is the depend type. the
880 * reassignments above also ensure we do not do a strlen(NULL). */
881 len = strlen(name) + strlen(opr) + strlen(ver) + 1;
882 MALLOC(str, len, RET_ERR(PM_ERR_MEMORY, NULL));
883 snprintf(str, len, "%s%s%s", name, opr, ver);
885 return str;
887 /* vim: set ts=2 sw=2 noet: */