Remove global handle from diskspace.c
[pacman-ng.git] / lib / libalpm / deps.c
blob27d04765fe1286e75db63d69a2fc75ba25d95548
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 /* global handle variable */
40 extern pmhandle_t *handle;
42 void _alpm_dep_free(pmdepend_t *dep)
44 FREE(dep->name);
45 FREE(dep->version);
46 FREE(dep);
49 static pmdepmissing_t *depmiss_new(const char *target, pmdepend_t *dep,
50 const char *causingpkg)
52 pmdepmissing_t *miss;
54 MALLOC(miss, sizeof(pmdepmissing_t), RET_ERR(PM_ERR_MEMORY, NULL));
56 STRDUP(miss->target, target, RET_ERR(PM_ERR_MEMORY, NULL));
57 miss->depend = _alpm_dep_dup(dep);
58 STRDUP(miss->causingpkg, causingpkg, RET_ERR(PM_ERR_MEMORY, NULL));
60 return miss;
63 void _alpm_depmiss_free(pmdepmissing_t *miss)
65 _alpm_dep_free(miss->depend);
66 FREE(miss->target);
67 FREE(miss->causingpkg);
68 FREE(miss);
71 /* Does pkg1 depend on pkg2, ie. does pkg2 satisfy a dependency of pkg1? */
72 static int _alpm_dep_edge(pmpkg_t *pkg1, pmpkg_t *pkg2)
74 alpm_list_t *i;
75 for(i = alpm_pkg_get_depends(pkg1); i; i = i->next) {
76 if(_alpm_depcmp(pkg2, i->data)) {
77 return 1;
80 return 0;
83 /* Convert a list of pmpkg_t * to a graph structure,
84 * with a edge for each dependency.
85 * Returns a list of vertices (one vertex = one package)
86 * (used by alpm_sortbydeps)
88 static alpm_list_t *dep_graph_init(alpm_list_t *targets)
90 alpm_list_t *i, *j;
91 alpm_list_t *vertices = NULL;
92 /* We create the vertices */
93 for(i = targets; i; i = i->next) {
94 pmgraph_t *vertex = _alpm_graph_new();
95 vertex->data = (void *)i->data;
96 vertices = alpm_list_add(vertices, vertex);
99 /* We compute the edges */
100 for(i = vertices; i; i = i->next) {
101 pmgraph_t *vertex_i = i->data;
102 pmpkg_t *p_i = vertex_i->data;
103 /* TODO this should be somehow combined with alpm_checkdeps */
104 for(j = vertices; j; j = j->next) {
105 pmgraph_t *vertex_j = j->data;
106 pmpkg_t *p_j = vertex_j->data;
107 if(_alpm_dep_edge(p_i, p_j)) {
108 vertex_i->children =
109 alpm_list_add(vertex_i->children, vertex_j);
112 vertex_i->childptr = vertex_i->children;
114 return vertices;
117 /* Re-order a list of target packages with respect to their dependencies.
119 * Example (reverse == 0):
120 * A depends on C
121 * B depends on A
122 * Target order is A,B,C,D
124 * Should be re-ordered to C,A,B,D
126 * if reverse is > 0, the dependency order will be reversed.
128 * This function returns the new alpm_list_t* target list.
131 alpm_list_t *_alpm_sortbydeps(alpm_list_t *targets, int reverse)
133 alpm_list_t *newtargs = NULL;
134 alpm_list_t *vertices = NULL;
135 alpm_list_t *vptr;
136 pmgraph_t *vertex;
138 if(targets == NULL) {
139 return NULL;
142 _alpm_log(PM_LOG_DEBUG, "started sorting dependencies\n");
144 vertices = dep_graph_init(targets);
146 vptr = vertices;
147 vertex = vertices->data;
148 while(vptr) {
149 /* mark that we touched the vertex */
150 vertex->state = -1;
151 int found = 0;
152 while(vertex->childptr && !found) {
153 pmgraph_t *nextchild = vertex->childptr->data;
154 vertex->childptr = vertex->childptr->next;
155 if(nextchild->state == 0) {
156 found = 1;
157 nextchild->parent = vertex;
158 vertex = nextchild;
160 else if(nextchild->state == -1) {
161 pmpkg_t *vertexpkg = vertex->data;
162 pmpkg_t *childpkg = nextchild->data;
163 const char *message;
165 _alpm_log(PM_LOG_WARNING, _("dependency cycle detected:\n"));
166 if(reverse) {
167 message =_("%s will be removed after its %s dependency\n");
168 } else {
169 message =_("%s will be installed before its %s dependency\n");
171 _alpm_log(PM_LOG_WARNING, message, vertexpkg->name, childpkg->name);
174 if(!found) {
175 newtargs = alpm_list_add(newtargs, vertex->data);
176 /* mark that we've left this vertex */
177 vertex->state = 1;
178 vertex = vertex->parent;
179 if(!vertex) {
180 vptr = vptr->next;
181 while(vptr) {
182 vertex = vptr->data;
183 if(vertex->state == 0) break;
184 vptr = vptr->next;
190 _alpm_log(PM_LOG_DEBUG, "sorting dependencies finished\n");
192 if(reverse) {
193 /* reverse the order */
194 alpm_list_t *tmptargs = alpm_list_reverse(newtargs);
195 /* free the old one */
196 alpm_list_free(newtargs);
197 newtargs = tmptargs;
200 alpm_list_free_inner(vertices, _alpm_graph_free);
201 alpm_list_free(vertices);
203 return newtargs;
206 static int no_dep_version(void)
208 int flags = alpm_trans_get_flags();
209 return flags != -1 && (flags & PM_TRANS_FLAG_NODEPVERSION);
212 static pmdepend_t *filtered_depend(pmdepend_t *dep, int nodepversion)
214 if(nodepversion) {
215 pmdepend_t *newdep = _alpm_dep_dup(dep);
216 ASSERT(newdep, return dep);
217 newdep->mod = PM_DEP_MOD_ANY;
218 dep = newdep;
220 return dep;
223 static void release_filtered_depend(pmdepend_t *dep, int nodepversion)
225 if(nodepversion) {
226 free(dep);
230 static pmpkg_t *find_dep_satisfier(alpm_list_t *pkgs, pmdepend_t *dep)
232 alpm_list_t *i;
234 for(i = pkgs; i; i = alpm_list_next(i)) {
235 pmpkg_t *pkg = i->data;
236 if(_alpm_depcmp(pkg, dep)) {
237 return pkg;
240 return NULL;
243 /** Find a package satisfying a specified dependency.
244 * The dependency can include versions with depmod operators.
245 * @param pkgs an alpm_list_t* of pmpkg_t where the satisfier will be searched
246 * @param depstring package or provision name, versioned or not
247 * @return a pmpkg_t* satisfying depstring
249 pmpkg_t SYMEXPORT *alpm_find_satisfier(alpm_list_t *pkgs, const char *depstring)
251 pmdepend_t *dep = _alpm_splitdep(depstring);
252 pmpkg_t *pkg = find_dep_satisfier(pkgs, dep);
253 _alpm_dep_free(dep);
254 return pkg;
257 /** Checks dependencies and returns missing ones in a list.
258 * Dependencies can include versions with depmod operators.
259 * @param pkglist the list of local packages
260 * @param reversedeps handles the backward dependencies
261 * @param remove an alpm_list_t* of packages to be removed
262 * @param upgrade an alpm_list_t* of packages to be upgraded (remove-then-upgrade)
263 * @return an alpm_list_t* of pmdepmissing_t pointers.
265 alpm_list_t SYMEXPORT *alpm_checkdeps(alpm_list_t *pkglist, int reversedeps,
266 alpm_list_t *remove, alpm_list_t *upgrade)
268 alpm_list_t *i, *j;
269 alpm_list_t *targets, *dblist = NULL, *modified = NULL;
270 alpm_list_t *baddeps = NULL;
271 int nodepversion;
273 targets = alpm_list_join(alpm_list_copy(remove), alpm_list_copy(upgrade));
274 for(i = pkglist; i; i = i->next) {
275 pmpkg_t *pkg = i->data;
276 if(_alpm_pkg_find(targets, pkg->name)) {
277 modified = alpm_list_add(modified, pkg);
278 } else {
279 dblist = alpm_list_add(dblist, pkg);
282 alpm_list_free(targets);
284 nodepversion = no_dep_version();
286 /* look for unsatisfied dependencies of the upgrade list */
287 for(i = upgrade; i; i = i->next) {
288 pmpkg_t *tp = i->data;
289 _alpm_log(PM_LOG_DEBUG, "checkdeps: package %s-%s\n",
290 alpm_pkg_get_name(tp), alpm_pkg_get_version(tp));
292 for(j = alpm_pkg_get_depends(tp); j; j = j->next) {
293 pmdepend_t *depend = j->data;
294 depend = filtered_depend(depend, nodepversion);
295 /* 1. we check the upgrade list */
296 /* 2. we check database for untouched satisfying packages */
297 if(!find_dep_satisfier(upgrade, depend) &&
298 !find_dep_satisfier(dblist, depend)) {
299 /* Unsatisfied dependency in the upgrade list */
300 pmdepmissing_t *miss;
301 char *missdepstring = alpm_dep_compute_string(depend);
302 _alpm_log(PM_LOG_DEBUG, "checkdeps: missing dependency '%s' for package '%s'\n",
303 missdepstring, alpm_pkg_get_name(tp));
304 free(missdepstring);
305 miss = depmiss_new(alpm_pkg_get_name(tp), depend, NULL);
306 baddeps = alpm_list_add(baddeps, miss);
308 release_filtered_depend(depend, nodepversion);
312 if(reversedeps) {
313 /* reversedeps handles the backwards dependencies, ie,
314 * the packages listed in the requiredby field. */
315 for(i = dblist; i; i = i->next) {
316 pmpkg_t *lp = i->data;
317 for(j = alpm_pkg_get_depends(lp); j; j = j->next) {
318 pmdepend_t *depend = j->data;
319 depend = filtered_depend(depend, nodepversion);
320 pmpkg_t *causingpkg = find_dep_satisfier(modified, depend);
321 /* we won't break this depend, if it is already broken, we ignore it */
322 /* 1. check upgrade list for satisfiers */
323 /* 2. check dblist for satisfiers */
324 if(causingpkg &&
325 !find_dep_satisfier(upgrade, depend) &&
326 !find_dep_satisfier(dblist, depend)) {
327 pmdepmissing_t *miss;
328 char *missdepstring = alpm_dep_compute_string(depend);
329 _alpm_log(PM_LOG_DEBUG, "checkdeps: transaction would break '%s' dependency of '%s'\n",
330 missdepstring, alpm_pkg_get_name(lp));
331 free(missdepstring);
332 miss = depmiss_new(lp->name, depend, alpm_pkg_get_name(causingpkg));
333 baddeps = alpm_list_add(baddeps, miss);
335 release_filtered_depend(depend, nodepversion);
340 alpm_list_free(modified);
341 alpm_list_free(dblist);
343 return baddeps;
346 static int dep_vercmp(const char *version1, pmdepmod_t mod,
347 const char *version2)
349 int equal = 0;
351 if(mod == PM_DEP_MOD_ANY) {
352 equal = 1;
353 } else {
354 int cmp = alpm_pkg_vercmp(version1, version2);
355 switch(mod) {
356 case PM_DEP_MOD_EQ: equal = (cmp == 0); break;
357 case PM_DEP_MOD_GE: equal = (cmp >= 0); break;
358 case PM_DEP_MOD_LE: equal = (cmp <= 0); break;
359 case PM_DEP_MOD_LT: equal = (cmp < 0); break;
360 case PM_DEP_MOD_GT: equal = (cmp > 0); break;
361 default: equal = 1; break;
364 return equal;
367 int _alpm_depcmp(pmpkg_t *pkg, pmdepend_t *dep)
369 alpm_list_t *i;
370 int satisfy = 0;
372 /* check (pkg->name, pkg->version) */
373 if(pkg->name_hash && dep->name_hash
374 && pkg->name_hash != dep->name_hash) {
375 /* skip more expensive checks */
376 } else {
377 satisfy = (strcmp(pkg->name, dep->name) == 0
378 && dep_vercmp(pkg->version, dep->mod, dep->version));
379 if(satisfy) {
380 return satisfy;
384 /* check provisions, format : "name=version" */
385 for(i = alpm_pkg_get_provides(pkg); i && !satisfy; i = i->next) {
386 const char *provision = i->data;
387 const char *provver = strchr(provision, '=');
389 if(provver == NULL) { /* no provision version */
390 satisfy = (dep->mod == PM_DEP_MOD_ANY
391 && strcmp(provision, dep->name) == 0);
392 } else {
393 /* This is a bit tricker than the old code for performance reasons. To
394 * prevent the need to copy and duplicate strings, strncmp only the name
395 * portion if they are the same length, since there is a version and
396 * operator in play here. Cast is to silence sign conversion warning;
397 * we know provver >= provision if we are here. */
398 size_t namelen = (size_t)(provver - provision);
399 provver += 1;
400 satisfy = (strlen(dep->name) == namelen
401 && strncmp(provision, dep->name, namelen) == 0
402 && dep_vercmp(provver, dep->mod, dep->version));
406 return satisfy;
409 pmdepend_t *_alpm_splitdep(const char *depstring)
411 pmdepend_t *depend;
412 const char *ptr, *version = NULL;
414 if(depstring == NULL) {
415 return NULL;
418 CALLOC(depend, 1, sizeof(pmdepend_t), RET_ERR(PM_ERR_MEMORY, NULL));
420 /* Find a version comparator if one exists. If it does, set the type and
421 * increment the ptr accordingly so we can copy the right strings. */
422 if((ptr = strstr(depstring, ">="))) {
423 depend->mod = PM_DEP_MOD_GE;
424 version = ptr + 2;
425 } else if((ptr = strstr(depstring, "<="))) {
426 depend->mod = PM_DEP_MOD_LE;
427 version = ptr + 2;
428 } else if((ptr = strstr(depstring, "="))) {
429 /* Note: we must do =,<,> checks after <=, >= checks */
430 depend->mod = PM_DEP_MOD_EQ;
431 version = ptr + 1;
432 } else if((ptr = strstr(depstring, "<"))) {
433 depend->mod = PM_DEP_MOD_LT;
434 version = ptr + 1;
435 } else if((ptr = strstr(depstring, ">"))) {
436 depend->mod = PM_DEP_MOD_GT;
437 version = ptr + 1;
438 } else {
439 /* no version specified, leave version and ptr NULL */
440 depend->mod = PM_DEP_MOD_ANY;
443 /* copy the right parts to the right places */
444 STRNDUP(depend->name, depstring, ptr - depstring,
445 RET_ERR(PM_ERR_MEMORY, NULL));
446 depend->name_hash = _alpm_hash_sdbm(depend->name);
447 if(version) {
448 STRDUP(depend->version, version, RET_ERR(PM_ERR_MEMORY, NULL));
451 return depend;
454 pmdepend_t *_alpm_dep_dup(const pmdepend_t *dep)
456 pmdepend_t *newdep;
457 CALLOC(newdep, 1, sizeof(pmdepend_t), RET_ERR(PM_ERR_MEMORY, NULL));
459 STRDUP(newdep->name, dep->name, RET_ERR(PM_ERR_MEMORY, NULL));
460 newdep->name_hash = dep->name_hash;
461 STRDUP(newdep->version, dep->version, RET_ERR(PM_ERR_MEMORY, NULL));
462 newdep->mod = dep->mod;
464 return newdep;
467 /* These parameters are messy. We check if this package, given a list of
468 * targets and a db is safe to remove. We do NOT remove it if it is in the
469 * target list, or if if the package was explictly installed and
470 * include_explicit == 0 */
471 static int can_remove_package(pmdb_t *db, pmpkg_t *pkg, alpm_list_t *targets,
472 int include_explicit)
474 alpm_list_t *i;
476 if(_alpm_pkg_find(targets, alpm_pkg_get_name(pkg))) {
477 return 0;
480 if(!include_explicit) {
481 /* see if it was explicitly installed */
482 if(alpm_pkg_get_reason(pkg) == PM_PKG_REASON_EXPLICIT) {
483 _alpm_log(PM_LOG_DEBUG, "excluding %s -- explicitly installed\n",
484 alpm_pkg_get_name(pkg));
485 return 0;
489 /* TODO: checkdeps could be used here, it handles multiple providers
490 * better, but that also makes it slower.
491 * Also this would require to first add the package to the targets list,
492 * then call checkdeps with it, then remove the package from the targets list
493 * if checkdeps detected it would break something */
495 /* see if other packages need it */
496 for(i = _alpm_db_get_pkgcache(db); i; i = i->next) {
497 pmpkg_t *lpkg = i->data;
498 if(_alpm_dep_edge(lpkg, pkg) && !_alpm_pkg_find(targets, lpkg->name)) {
499 return 0;
503 /* it's ok to remove */
504 return 1;
508 * @brief Adds unneeded dependencies to an existing list of packages.
509 * By unneeded, we mean dependencies that are only required by packages in the
510 * target list, so they can be safely removed.
511 * If the input list was topo sorted, the output list will be topo sorted too.
513 * @param db package database to do dependency tracing in
514 * @param *targs pointer to a list of packages
515 * @param include_explicit if 0, explicitly installed packages are not included
517 void _alpm_recursedeps(pmdb_t *db, alpm_list_t *targs, int include_explicit)
519 alpm_list_t *i, *j;
521 if(db == NULL || targs == NULL) {
522 return;
525 for(i = targs; i; i = i->next) {
526 pmpkg_t *pkg = i->data;
527 for(j = _alpm_db_get_pkgcache(db); j; j = j->next) {
528 pmpkg_t *deppkg = j->data;
529 if(_alpm_dep_edge(pkg, deppkg)
530 && can_remove_package(db, deppkg, targs, include_explicit)) {
531 _alpm_log(PM_LOG_DEBUG, "adding '%s' to the targets\n",
532 alpm_pkg_get_name(deppkg));
533 /* add it to the target list */
534 targs = alpm_list_add(targs, _alpm_pkg_dup(deppkg));
541 * helper function for resolvedeps: search for dep satisfier in dbs
543 * @param dep is the dependency to search for
544 * @param dbs are the databases to search
545 * @param excluding are the packages to exclude from the search
546 * @param prompt if true, will cause an unresolvable dependency to issue an
547 * interactive prompt asking whether the package should be removed from
548 * the transaction or the transaction aborted; if false, simply returns
549 * an error code without prompting
550 * @return the resolved package
552 static pmpkg_t *resolvedep(pmdepend_t *dep, alpm_list_t *dbs,
553 alpm_list_t *excluding, int prompt)
555 alpm_list_t *i, *j;
556 int ignored = 0;
558 alpm_list_t *providers = NULL;
559 int count;
561 /* 1. literals */
562 for(i = dbs; i; i = i->next) {
563 pmpkg_t *pkg = _alpm_db_get_pkgfromcache(i->data, dep->name);
564 if(pkg && _alpm_depcmp(pkg, dep) && !_alpm_pkg_find(excluding, pkg->name)) {
565 if(_alpm_pkg_should_ignore(pkg)) {
566 int install = 0;
567 if(prompt) {
568 QUESTION(handle->trans, PM_TRANS_CONV_INSTALL_IGNOREPKG, pkg,
569 NULL, NULL, &install);
570 } else {
571 _alpm_log(PM_LOG_WARNING, _("ignoring package %s-%s\n"), pkg->name, pkg->version);
573 if(!install) {
574 ignored = 1;
575 continue;
578 return pkg;
581 /* 2. satisfiers (skip literals here) */
582 for(i = dbs; i; i = i->next) {
583 for(j = _alpm_db_get_pkgcache(i->data); j; j = j->next) {
584 pmpkg_t *pkg = j->data;
585 if(_alpm_depcmp(pkg, dep) && strcmp(pkg->name, dep->name) != 0 &&
586 !_alpm_pkg_find(excluding, pkg->name)) {
587 if(_alpm_pkg_should_ignore(pkg)) {
588 int install = 0;
589 if(prompt) {
590 QUESTION(handle->trans, PM_TRANS_CONV_INSTALL_IGNOREPKG,
591 pkg, NULL, NULL, &install);
592 } else {
593 _alpm_log(PM_LOG_WARNING, _("ignoring package %s-%s\n"), pkg->name, pkg->version);
595 if(!install) {
596 ignored = 1;
597 continue;
600 _alpm_log(PM_LOG_DEBUG, "provider found (%s provides %s)\n",
601 pkg->name, dep->name);
602 providers = alpm_list_add(providers, pkg);
603 /* keep looking for other providers in the all dbs */
608 /* first check if one provider is already installed locally */
609 for(i = providers; i; i = i->next) {
610 pmpkg_t *pkg = i->data;
611 if(_alpm_pkghash_find(_alpm_db_get_pkgcache_hash(handle->db_local), pkg->name)) {
612 alpm_list_free(providers);
613 return pkg;
616 count = alpm_list_count(providers);
617 if(count >= 1) {
618 /* default to first provider if there is no QUESTION callback */
619 int index = 0;
620 if(count > 1) {
621 /* if there is more than one provider, we ask the user */
622 QUESTION(handle->trans, PM_TRANS_CONV_SELECT_PROVIDER,
623 providers, dep, NULL, &index);
625 if(index >= 0 && index < count) {
626 pmpkg_t *pkg = alpm_list_getdata(alpm_list_nth(providers, index));
627 alpm_list_free(providers);
628 return pkg;
630 alpm_list_free(providers);
631 providers = NULL;
634 if(ignored) { /* resolvedeps will override these */
635 pm_errno = PM_ERR_PKG_IGNORED;
636 } else {
637 pm_errno = PM_ERR_PKG_NOT_FOUND;
639 return NULL;
642 /** Find a package satisfying a specified dependency.
643 * First look for a literal, going through each db one by one. Then look for
644 * providers. The first satisfier found is returned.
645 * The dependency can include versions with depmod operators.
646 * @param dbs an alpm_list_t* of pmdb_t where the satisfier will be searched
647 * @param depstring package or provision name, versioned or not
648 * @return a pmpkg_t* satisfying depstring
650 pmpkg_t SYMEXPORT *alpm_find_dbs_satisfier(alpm_list_t *dbs, const char *depstring)
652 pmdepend_t *dep;
653 pmpkg_t *pkg;
655 ASSERT(dbs, return NULL);
657 dep = _alpm_splitdep(depstring);
658 ASSERT(dep, return NULL);
659 pkg = resolvedep(dep, dbs, NULL, 1);
660 _alpm_dep_free(dep);
661 return pkg;
665 * Computes resolvable dependencies for a given package and adds that package
666 * and those resolvable dependencies to a list.
668 * @param localpkgs is the list of local packages
669 * @param dbs_sync are the sync databases
670 * @param pkg is the package to resolve
671 * @param packages is a pointer to a list of packages which will be
672 * searched first for any dependency packages needed to complete the
673 * resolve, and to which will be added any [pkg] and all of its
674 * dependencies not already on the list
675 * @param remove is the set of packages which will be removed in this
676 * transaction
677 * @param data returns the dependency which could not be satisfied in the
678 * event of an error
679 * @return 0 on success, with [pkg] and all of its dependencies not already on
680 * the [*packages] list added to that list, or -1 on failure due to an
681 * unresolvable dependency, in which case the [*packages] list will be
682 * unmodified by this function
684 int _alpm_resolvedeps(alpm_list_t *localpkgs, alpm_list_t *dbs_sync, pmpkg_t *pkg,
685 alpm_list_t *preferred, alpm_list_t **packages,
686 alpm_list_t *remove, alpm_list_t **data)
688 int ret = 0;
689 alpm_list_t *i, *j;
690 alpm_list_t *targ;
691 alpm_list_t *deps = NULL;
692 alpm_list_t *packages_copy;
694 if(_alpm_pkg_find(*packages, pkg->name) != NULL) {
695 return 0;
698 /* Create a copy of the packages list, so that it can be restored
699 on error */
700 packages_copy = alpm_list_copy(*packages);
701 /* [pkg] has not already been resolved into the packages list, so put it
702 on that list */
703 *packages = alpm_list_add(*packages, pkg);
705 _alpm_log(PM_LOG_DEBUG, "started resolving dependencies\n");
706 for(i = alpm_list_last(*packages); i; i = i->next) {
707 pmpkg_t *tpkg = i->data;
708 targ = alpm_list_add(NULL, tpkg);
709 deps = alpm_checkdeps(localpkgs, 0, remove, targ);
710 alpm_list_free(targ);
712 for(j = deps; j; j = j->next) {
713 pmdepmissing_t *miss = j->data;
714 pmdepend_t *missdep = alpm_miss_get_dep(miss);
715 /* check if one of the packages in the [*packages] list already satisfies
716 * this dependency */
717 if(find_dep_satisfier(*packages, missdep)) {
718 _alpm_depmiss_free(miss);
719 continue;
721 /* check if one of the packages in the [preferred] list already satisfies
722 * this dependency */
723 pmpkg_t *spkg = find_dep_satisfier(preferred, missdep);
724 if(!spkg) {
725 /* find a satisfier package in the given repositories */
726 spkg = resolvedep(missdep, dbs_sync, *packages, 0);
728 if(!spkg) {
729 pm_errno = PM_ERR_UNSATISFIED_DEPS;
730 char *missdepstring = alpm_dep_compute_string(missdep);
731 _alpm_log(PM_LOG_WARNING,
732 _("cannot resolve \"%s\", a dependency of \"%s\"\n"),
733 missdepstring, tpkg->name);
734 free(missdepstring);
735 if(data) {
736 *data = alpm_list_add(*data, miss);
738 ret = -1;
739 } else {
740 _alpm_log(PM_LOG_DEBUG, "pulling dependency %s (needed by %s)\n",
741 alpm_pkg_get_name(spkg), alpm_pkg_get_name(tpkg));
742 *packages = alpm_list_add(*packages, spkg);
743 _alpm_depmiss_free(miss);
746 alpm_list_free(deps);
749 if(ret != 0) {
750 alpm_list_free(*packages);
751 *packages = packages_copy;
752 } else {
753 alpm_list_free(packages_copy);
755 _alpm_log(PM_LOG_DEBUG, "finished resolving dependencies\n");
756 return ret;
759 const char SYMEXPORT *alpm_miss_get_target(const pmdepmissing_t *miss)
761 /* Sanity checks */
762 ASSERT(miss != NULL, return NULL);
764 return miss->target;
767 const char SYMEXPORT *alpm_miss_get_causingpkg(const pmdepmissing_t *miss)
769 /* Sanity checks */
770 ASSERT(miss != NULL, return NULL);
772 return miss->causingpkg;
775 pmdepend_t SYMEXPORT *alpm_miss_get_dep(pmdepmissing_t *miss)
777 /* Sanity checks */
778 ASSERT(miss != NULL, return NULL);
780 return miss->depend;
783 pmdepmod_t SYMEXPORT alpm_dep_get_mod(const pmdepend_t *dep)
785 /* Sanity checks */
786 ASSERT(dep != NULL, return -1);
788 return dep->mod;
791 const char SYMEXPORT *alpm_dep_get_name(const pmdepend_t *dep)
793 /* Sanity checks */
794 ASSERT(dep != NULL, return NULL);
796 return dep->name;
799 const char SYMEXPORT *alpm_dep_get_version(const pmdepend_t *dep)
801 /* Sanity checks */
802 ASSERT(dep != NULL, return NULL);
804 return dep->version;
807 /** Reverse of splitdep; make a dep string from a pmdepend_t struct.
808 * The string must be freed!
809 * @param dep the depend to turn into a string
810 * @return a string-formatted dependency with operator if necessary
812 char SYMEXPORT *alpm_dep_compute_string(const pmdepend_t *dep)
814 const char *name, *opr, *ver;
815 char *str;
816 size_t len;
818 /* Sanity checks */
819 ASSERT(dep != NULL, return NULL);
821 if(dep->name) {
822 name = dep->name;
823 } else {
824 name = "";
827 switch(dep->mod) {
828 case PM_DEP_MOD_ANY:
829 opr = "";
830 break;
831 case PM_DEP_MOD_GE:
832 opr = ">=";
833 break;
834 case PM_DEP_MOD_LE:
835 opr = "<=";
836 break;
837 case PM_DEP_MOD_EQ:
838 opr = "=";
839 break;
840 case PM_DEP_MOD_LT:
841 opr = "<";
842 break;
843 case PM_DEP_MOD_GT:
844 opr = ">";
845 break;
846 default:
847 opr = "";
848 break;
851 if(dep->mod != PM_DEP_MOD_ANY && dep->version) {
852 ver = dep->version;
853 } else {
854 ver = "";
857 /* we can always compute len and print the string like this because opr
858 * and ver will be empty when PM_DEP_MOD_ANY is the depend type. the
859 * reassignments above also ensure we do not do a strlen(NULL). */
860 len = strlen(name) + strlen(opr) + strlen(ver) + 1;
861 MALLOC(str, len, RET_ERR(PM_ERR_MEMORY, NULL));
862 snprintf(str, len, "%s%s%s", name, opr, ver);
864 return str;
866 /* vim: set ts=2 sw=2 noet: */