Move important information up in -Si output
[pacman-ng.git] / lib / libalpm / deps.c
blob2a06bb047f2d8b9505f535ccb7aaf5986e624386
1 /*
2 * deps.c
4 * Copyright (c) 2006-2012 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 <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
27 /* libalpm */
28 #include "deps.h"
29 #include "alpm_list.h"
30 #include "util.h"
31 #include "log.h"
32 #include "graph.h"
33 #include "package.h"
34 #include "db.h"
35 #include "handle.h"
36 #include "trans.h"
38 void _alpm_dep_free(alpm_depend_t *dep)
40 FREE(dep->name);
41 FREE(dep->version);
42 FREE(dep->desc);
43 FREE(dep);
46 static alpm_depmissing_t *depmiss_new(const char *target, alpm_depend_t *dep,
47 const char *causingpkg)
49 alpm_depmissing_t *miss;
51 MALLOC(miss, sizeof(alpm_depmissing_t), return NULL);
53 STRDUP(miss->target, target, return NULL);
54 miss->depend = _alpm_dep_dup(dep);
55 STRDUP(miss->causingpkg, causingpkg, return NULL);
57 return miss;
60 void _alpm_depmiss_free(alpm_depmissing_t *miss)
62 _alpm_dep_free(miss->depend);
63 FREE(miss->target);
64 FREE(miss->causingpkg);
65 FREE(miss);
68 /* Does pkg1 depend on pkg2, ie. does pkg2 satisfy a dependency of pkg1? */
69 static int _alpm_dep_edge(alpm_pkg_t *pkg1, alpm_pkg_t *pkg2)
71 alpm_list_t *i;
72 for(i = alpm_pkg_get_depends(pkg1); i; i = i->next) {
73 if(_alpm_depcmp(pkg2, i->data)) {
74 return 1;
77 return 0;
80 /* Convert a list of alpm_pkg_t * to a graph structure,
81 * with a edge for each dependency.
82 * Returns a list of vertices (one vertex = one package)
83 * (used by alpm_sortbydeps)
85 static alpm_list_t *dep_graph_init(alpm_list_t *targets)
87 alpm_list_t *i, *j;
88 alpm_list_t *vertices = NULL;
89 /* We create the vertices */
90 for(i = targets; i; i = i->next) {
91 alpm_graph_t *vertex = _alpm_graph_new();
92 vertex->data = (void *)i->data;
93 vertices = alpm_list_add(vertices, vertex);
96 /* We compute the edges */
97 for(i = vertices; i; i = i->next) {
98 alpm_graph_t *vertex_i = i->data;
99 alpm_pkg_t *p_i = vertex_i->data;
100 /* TODO this should be somehow combined with alpm_checkdeps */
101 for(j = vertices; j; j = j->next) {
102 alpm_graph_t *vertex_j = j->data;
103 alpm_pkg_t *p_j = vertex_j->data;
104 if(_alpm_dep_edge(p_i, p_j)) {
105 vertex_i->children =
106 alpm_list_add(vertex_i->children, vertex_j);
109 vertex_i->childptr = vertex_i->children;
111 return vertices;
114 /* Re-order a list of target packages with respect to their dependencies.
116 * Example (reverse == 0):
117 * A depends on C
118 * B depends on A
119 * Target order is A,B,C,D
121 * Should be re-ordered to C,A,B,D
123 * if reverse is > 0, the dependency order will be reversed.
125 * This function returns the new alpm_list_t* target list.
128 alpm_list_t *_alpm_sortbydeps(alpm_handle_t *handle,
129 alpm_list_t *targets, int reverse)
131 alpm_list_t *newtargs = NULL;
132 alpm_list_t *vertices = NULL;
133 alpm_list_t *vptr;
134 alpm_graph_t *vertex;
136 if(targets == NULL) {
137 return NULL;
140 _alpm_log(handle, ALPM_LOG_DEBUG, "started sorting dependencies\n");
142 vertices = dep_graph_init(targets);
144 vptr = vertices;
145 vertex = vertices->data;
146 while(vptr) {
147 /* mark that we touched the vertex */
148 vertex->state = -1;
149 int found = 0;
150 while(vertex->childptr && !found) {
151 alpm_graph_t *nextchild = vertex->childptr->data;
152 vertex->childptr = vertex->childptr->next;
153 if(nextchild->state == 0) {
154 found = 1;
155 nextchild->parent = vertex;
156 vertex = nextchild;
158 else if(nextchild->state == -1) {
159 alpm_pkg_t *vertexpkg = vertex->data;
160 alpm_pkg_t *childpkg = nextchild->data;
162 _alpm_log(handle, ALPM_LOG_WARNING, _("dependency cycle detected:\n"));
163 if(reverse) {
164 _alpm_log(handle, ALPM_LOG_WARNING,
165 _("%s will be removed after its %s dependency\n"),
166 vertexpkg->name, childpkg->name);
167 } else {
168 _alpm_log(handle, ALPM_LOG_WARNING,
169 _("%s will be installed before its %s dependency\n"),
170 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(handle, ALPM_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(alpm_handle_t *handle)
208 if(!handle->trans) {
209 return 0;
211 return (handle->trans->flags & ALPM_TRANS_FLAG_NODEPVERSION);
214 static alpm_depend_t *filtered_depend(alpm_depend_t *dep, int nodepversion)
216 if(nodepversion) {
217 alpm_depend_t *newdep = _alpm_dep_dup(dep);
218 ASSERT(newdep, return dep);
219 newdep->mod = ALPM_DEP_MOD_ANY;
220 dep = newdep;
222 return dep;
225 static void release_filtered_depend(alpm_depend_t *dep, int nodepversion)
227 if(nodepversion) {
228 free(dep);
232 static alpm_pkg_t *find_dep_satisfier(alpm_list_t *pkgs, alpm_depend_t *dep)
234 alpm_list_t *i;
236 for(i = pkgs; i; i = i->next) {
237 alpm_pkg_t *pkg = i->data;
238 if(_alpm_depcmp(pkg, dep)) {
239 return pkg;
242 return NULL;
245 /** Find a package satisfying a specified dependency.
246 * The dependency can include versions with depmod operators.
247 * @param pkgs an alpm_list_t* of alpm_pkg_t where the satisfier will be searched
248 * @param depstring package or provision name, versioned or not
249 * @return a alpm_pkg_t* satisfying depstring
251 alpm_pkg_t SYMEXPORT *alpm_find_satisfier(alpm_list_t *pkgs, const char *depstring)
253 alpm_depend_t *dep = _alpm_splitdep(depstring);
254 if(!dep) {
255 return NULL;
257 alpm_pkg_t *pkg = find_dep_satisfier(pkgs, dep);
258 _alpm_dep_free(dep);
259 return pkg;
262 /** Checks dependencies and returns missing ones in a list.
263 * Dependencies can include versions with depmod operators.
264 * @param handle the context handle
265 * @param pkglist the list of local packages
266 * @param remove an alpm_list_t* of packages to be removed
267 * @param upgrade an alpm_list_t* of packages to be upgraded (remove-then-upgrade)
268 * @param reversedeps handles the backward dependencies
269 * @return an alpm_list_t* of alpm_depmissing_t pointers.
271 alpm_list_t SYMEXPORT *alpm_checkdeps(alpm_handle_t *handle,
272 alpm_list_t *pkglist, alpm_list_t *rem, alpm_list_t *upgrade,
273 int reversedeps)
275 alpm_list_t *i, *j;
276 alpm_list_t *dblist = NULL, *modified = NULL;
277 alpm_list_t *baddeps = NULL;
278 int nodepversion;
280 CHECK_HANDLE(handle, return NULL);
282 for(i = pkglist; i; i = i->next) {
283 alpm_pkg_t *pkg = i->data;
284 if(_alpm_pkg_find(rem, pkg->name) || _alpm_pkg_find(upgrade, pkg->name)) {
285 modified = alpm_list_add(modified, pkg);
286 } else {
287 dblist = alpm_list_add(dblist, pkg);
291 nodepversion = no_dep_version(handle);
293 /* look for unsatisfied dependencies of the upgrade list */
294 for(i = upgrade; i; i = i->next) {
295 alpm_pkg_t *tp = i->data;
296 _alpm_log(handle, ALPM_LOG_DEBUG, "checkdeps: package %s-%s\n",
297 tp->name, tp->version);
299 for(j = alpm_pkg_get_depends(tp); j; j = j->next) {
300 alpm_depend_t *depend = j->data;
301 depend = filtered_depend(depend, nodepversion);
302 /* 1. we check the upgrade list */
303 /* 2. we check database for untouched satisfying packages */
304 if(!find_dep_satisfier(upgrade, depend) &&
305 !find_dep_satisfier(dblist, depend)) {
306 /* Unsatisfied dependency in the upgrade list */
307 alpm_depmissing_t *miss;
308 char *missdepstring = alpm_dep_compute_string(depend);
309 _alpm_log(handle, ALPM_LOG_DEBUG, "checkdeps: missing dependency '%s' for package '%s'\n",
310 missdepstring, tp->name);
311 free(missdepstring);
312 miss = depmiss_new(tp->name, depend, NULL);
313 baddeps = alpm_list_add(baddeps, miss);
315 release_filtered_depend(depend, nodepversion);
319 if(reversedeps) {
320 /* reversedeps handles the backwards dependencies, ie,
321 * the packages listed in the requiredby field. */
322 for(i = dblist; i; i = i->next) {
323 alpm_pkg_t *lp = i->data;
324 for(j = alpm_pkg_get_depends(lp); j; j = j->next) {
325 alpm_depend_t *depend = j->data;
326 depend = filtered_depend(depend, nodepversion);
327 alpm_pkg_t *causingpkg = find_dep_satisfier(modified, depend);
328 /* we won't break this depend, if it is already broken, we ignore it */
329 /* 1. check upgrade list for satisfiers */
330 /* 2. check dblist for satisfiers */
331 if(causingpkg &&
332 !find_dep_satisfier(upgrade, depend) &&
333 !find_dep_satisfier(dblist, depend)) {
334 alpm_depmissing_t *miss;
335 char *missdepstring = alpm_dep_compute_string(depend);
336 _alpm_log(handle, ALPM_LOG_DEBUG, "checkdeps: transaction would break '%s' dependency of '%s'\n",
337 missdepstring, lp->name);
338 free(missdepstring);
339 miss = depmiss_new(lp->name, depend, causingpkg->name);
340 baddeps = alpm_list_add(baddeps, miss);
342 release_filtered_depend(depend, nodepversion);
347 alpm_list_free(modified);
348 alpm_list_free(dblist);
350 return baddeps;
353 static int dep_vercmp(const char *version1, alpm_depmod_t mod,
354 const char *version2)
356 int equal = 0;
358 if(mod == ALPM_DEP_MOD_ANY) {
359 equal = 1;
360 } else {
361 int cmp = alpm_pkg_vercmp(version1, version2);
362 switch(mod) {
363 case ALPM_DEP_MOD_EQ: equal = (cmp == 0); break;
364 case ALPM_DEP_MOD_GE: equal = (cmp >= 0); break;
365 case ALPM_DEP_MOD_LE: equal = (cmp <= 0); break;
366 case ALPM_DEP_MOD_LT: equal = (cmp < 0); break;
367 case ALPM_DEP_MOD_GT: equal = (cmp > 0); break;
368 default: equal = 1; break;
371 return equal;
374 int _alpm_depcmp_literal(alpm_pkg_t *pkg, alpm_depend_t *dep)
376 if(pkg->name_hash != dep->name_hash
377 || strcmp(pkg->name, dep->name) != 0) {
378 /* skip more expensive checks */
379 return 0;
381 return dep_vercmp(pkg->version, dep->mod, dep->version);
384 int _alpm_depcmp(alpm_pkg_t *pkg, alpm_depend_t *dep)
386 alpm_list_t *i;
387 int satisfy = _alpm_depcmp_literal(pkg, dep);
389 if(satisfy) {
390 return satisfy;
393 /* check provisions, name and version if available */
394 for(i = alpm_pkg_get_provides(pkg); i && !satisfy; i = i->next) {
395 alpm_depend_t *provision = i->data;
397 if(dep->mod == ALPM_DEP_MOD_ANY) {
398 /* any version will satisfy the requirement */
399 satisfy = (provision->name_hash == dep->name_hash
400 && strcmp(provision->name, dep->name) == 0);
401 } else if(provision->mod == ALPM_DEP_MOD_EQ) {
402 /* provision specifies a version, so try it out */
403 satisfy = (provision->name_hash == dep->name_hash
404 && strcmp(provision->name, dep->name) == 0
405 && dep_vercmp(provision->version, dep->mod, dep->version));
409 return satisfy;
412 alpm_depend_t *_alpm_splitdep(const char *depstring)
414 alpm_depend_t *depend;
415 const char *ptr, *version, *desc;
416 size_t deplen;
418 if(depstring == NULL) {
419 return NULL;
422 MALLOC(depend, sizeof(alpm_depend_t), return NULL);
424 /* Note the extra space in ": " to avoid matching the epoch */
425 if((desc = strstr(depstring, ": ")) != NULL) {
426 STRDUP(depend->desc, desc + 2, return NULL);
427 deplen = desc - depstring;
428 } else {
429 /* no description- point desc at NULL at end of string for later use */
430 depend->desc = NULL;
431 deplen = strlen(depstring);
432 desc = depstring + deplen;
435 /* Find a version comparator if one exists. If it does, set the type and
436 * increment the ptr accordingly so we can copy the right strings. */
437 if((ptr = memchr(depstring, '<', deplen))) {
438 if(ptr[1] == '=') {
439 depend->mod = ALPM_DEP_MOD_LE;
440 version = ptr + 2;
441 } else {
442 depend->mod = ALPM_DEP_MOD_LT;
443 version = ptr + 1;
445 } else if((ptr = memchr(depstring, '>', deplen))) {
446 if(ptr[1] == '=') {
447 depend->mod = ALPM_DEP_MOD_GE;
448 version = ptr + 2;
449 } else {
450 depend->mod = ALPM_DEP_MOD_GT;
451 version = ptr + 1;
453 } else if((ptr = memchr(depstring, '=', deplen))) {
454 /* Note: we must do =,<,> checks after <=, >= checks */
455 depend->mod = ALPM_DEP_MOD_EQ;
456 version = ptr + 1;
457 } else {
458 /* no version specified, set ptr to end of string and version to NULL */
459 ptr = depstring + deplen;
460 depend->mod = ALPM_DEP_MOD_ANY;
461 depend->version = NULL;
462 version = NULL;
465 /* copy the right parts to the right places */
466 STRNDUP(depend->name, depstring, ptr - depstring, return NULL);
467 depend->name_hash = _alpm_hash_sdbm(depend->name);
468 if(version) {
469 STRNDUP(depend->version, version, desc - version, return NULL);
472 return depend;
475 alpm_depend_t *_alpm_dep_dup(const alpm_depend_t *dep)
477 alpm_depend_t *newdep;
478 CALLOC(newdep, 1, sizeof(alpm_depend_t), return NULL);
480 STRDUP(newdep->name, dep->name, return NULL);
481 STRDUP(newdep->version, dep->version, return NULL);
482 STRDUP(newdep->desc, dep->desc, return NULL);
483 newdep->name_hash = dep->name_hash;
484 newdep->mod = dep->mod;
486 return newdep;
489 /* These parameters are messy. We check if this package, given a list of
490 * targets and a db is safe to remove. We do NOT remove it if it is in the
491 * target list, or if if the package was explictly installed and
492 * include_explicit == 0 */
493 static int can_remove_package(alpm_db_t *db, alpm_pkg_t *pkg,
494 alpm_list_t *targets, int include_explicit)
496 alpm_list_t *i;
498 if(_alpm_pkg_find(targets, pkg->name)) {
499 return 0;
502 if(!include_explicit) {
503 /* see if it was explicitly installed */
504 if(alpm_pkg_get_reason(pkg) == ALPM_PKG_REASON_EXPLICIT) {
505 _alpm_log(db->handle, ALPM_LOG_DEBUG,
506 "excluding %s -- explicitly installed\n", pkg->name);
507 return 0;
511 /* TODO: checkdeps could be used here, it handles multiple providers
512 * better, but that also makes it slower.
513 * Also this would require to first add the package to the targets list,
514 * then call checkdeps with it, then remove the package from the targets list
515 * if checkdeps detected it would break something */
517 /* see if other packages need it */
518 for(i = _alpm_db_get_pkgcache(db); i; i = i->next) {
519 alpm_pkg_t *lpkg = i->data;
520 if(_alpm_dep_edge(lpkg, pkg) && !_alpm_pkg_find(targets, lpkg->name)) {
521 return 0;
525 /* it's ok to remove */
526 return 1;
530 * @brief Adds unneeded dependencies to an existing list of packages.
531 * By unneeded, we mean dependencies that are only required by packages in the
532 * target list, so they can be safely removed.
533 * If the input list was topo sorted, the output list will be topo sorted too.
535 * @param db package database to do dependency tracing in
536 * @param *targs pointer to a list of packages
537 * @param include_explicit if 0, explicitly installed packages are not included
538 * @return 0 on success, -1 on errors
540 int _alpm_recursedeps(alpm_db_t *db, alpm_list_t *targs, int include_explicit)
542 alpm_list_t *i, *j;
544 if(db == NULL || targs == NULL) {
545 return -1;
548 for(i = targs; i; i = i->next) {
549 alpm_pkg_t *pkg = i->data;
550 for(j = _alpm_db_get_pkgcache(db); j; j = j->next) {
551 alpm_pkg_t *deppkg = j->data;
552 if(_alpm_dep_edge(pkg, deppkg)
553 && can_remove_package(db, deppkg, targs, include_explicit)) {
554 alpm_pkg_t *copy;
555 _alpm_log(db->handle, ALPM_LOG_DEBUG, "adding '%s' to the targets\n",
556 deppkg->name);
557 /* add it to the target list */
558 if(_alpm_pkg_dup(deppkg, &copy)) {
559 return -1;
561 targs = alpm_list_add(targs, copy);
565 return 0;
569 * helper function for resolvedeps: search for dep satisfier in dbs
571 * @param handle the context handle
572 * @param dep is the dependency to search for
573 * @param dbs are the databases to search
574 * @param excluding are the packages to exclude from the search
575 * @param prompt if true, will cause an unresolvable dependency to issue an
576 * interactive prompt asking whether the package should be removed from
577 * the transaction or the transaction aborted; if false, simply returns
578 * an error code without prompting
579 * @return the resolved package
581 static alpm_pkg_t *resolvedep(alpm_handle_t *handle, alpm_depend_t *dep,
582 alpm_list_t *dbs, alpm_list_t *excluding, int prompt)
584 alpm_list_t *i, *j;
585 int ignored = 0;
587 alpm_list_t *providers = NULL;
588 int count;
590 /* 1. literals */
591 for(i = dbs; i; i = i->next) {
592 alpm_pkg_t *pkg = _alpm_db_get_pkgfromcache(i->data, dep->name);
593 if(pkg && _alpm_depcmp_literal(pkg, dep)
594 && !_alpm_pkg_find(excluding, pkg->name)) {
595 if(_alpm_pkg_should_ignore(handle, pkg)) {
596 int install = 0;
597 if(prompt) {
598 QUESTION(handle, ALPM_QUESTION_INSTALL_IGNOREPKG, pkg,
599 NULL, NULL, &install);
600 } else {
601 _alpm_log(handle, ALPM_LOG_WARNING, _("ignoring package %s-%s\n"),
602 pkg->name, pkg->version);
604 if(!install) {
605 ignored = 1;
606 continue;
609 return pkg;
612 /* 2. satisfiers (skip literals here) */
613 for(i = dbs; i; i = i->next) {
614 for(j = _alpm_db_get_pkgcache(i->data); j; j = j->next) {
615 alpm_pkg_t *pkg = j->data;
616 /* with hash != hash, we can even skip the strcmp() as we know they can't
617 * possibly be the same string */
618 if(pkg->name_hash != dep->name_hash && _alpm_depcmp(pkg, dep)
619 && !_alpm_pkg_find(excluding, pkg->name)) {
620 if(_alpm_pkg_should_ignore(handle, pkg)) {
621 int install = 0;
622 if(prompt) {
623 QUESTION(handle, ALPM_QUESTION_INSTALL_IGNOREPKG,
624 pkg, NULL, NULL, &install);
625 } else {
626 _alpm_log(handle, ALPM_LOG_WARNING, _("ignoring package %s-%s\n"),
627 pkg->name, pkg->version);
629 if(!install) {
630 ignored = 1;
631 continue;
634 _alpm_log(handle, ALPM_LOG_DEBUG, "provider found (%s provides %s)\n",
635 pkg->name, dep->name);
636 providers = alpm_list_add(providers, pkg);
637 /* keep looking for other providers in the all dbs */
642 /* first check if one provider is already installed locally */
643 for(i = providers; i; i = i->next) {
644 alpm_pkg_t *pkg = i->data;
645 if(_alpm_db_get_pkgfromcache(handle->db_local, pkg->name)) {
646 alpm_list_free(providers);
647 return pkg;
650 count = alpm_list_count(providers);
651 if(count >= 1) {
652 /* default to first provider if there is no QUESTION callback */
653 int idx = 0;
654 if(count > 1) {
655 /* if there is more than one provider, we ask the user */
656 QUESTION(handle, ALPM_QUESTION_SELECT_PROVIDER,
657 providers, dep, NULL, &idx);
659 if(idx >= 0 && idx < count) {
660 alpm_list_t *nth = alpm_list_nth(providers, idx);
661 alpm_pkg_t *pkg = nth->data;
662 alpm_list_free(providers);
663 return pkg;
665 alpm_list_free(providers);
666 providers = NULL;
669 if(ignored) { /* resolvedeps will override these */
670 handle->pm_errno = ALPM_ERR_PKG_IGNORED;
671 } else {
672 handle->pm_errno = ALPM_ERR_PKG_NOT_FOUND;
674 return NULL;
677 /** Find a package satisfying a specified dependency.
678 * First look for a literal, going through each db one by one. Then look for
679 * providers. The first satisfier found is returned.
680 * The dependency can include versions with depmod operators.
681 * @param handle the context handle
682 * @param dbs an alpm_list_t* of alpm_db_t where the satisfier will be searched
683 * @param depstring package or provision name, versioned or not
684 * @return a alpm_pkg_t* satisfying depstring
686 alpm_pkg_t SYMEXPORT *alpm_find_dbs_satisfier(alpm_handle_t *handle,
687 alpm_list_t *dbs, const char *depstring)
689 alpm_depend_t *dep;
690 alpm_pkg_t *pkg;
692 CHECK_HANDLE(handle, return NULL);
693 ASSERT(dbs, RET_ERR(handle, ALPM_ERR_WRONG_ARGS, NULL));
695 dep = _alpm_splitdep(depstring);
696 ASSERT(dep, return NULL);
697 pkg = resolvedep(handle, dep, dbs, NULL, 1);
698 _alpm_dep_free(dep);
699 return pkg;
703 * Computes resolvable dependencies for a given package and adds that package
704 * and those resolvable dependencies to a list.
706 * @param handle the context handle
707 * @param localpkgs is the list of local packages
708 * @param pkg is the package to resolve
709 * @param preferred packages to prefer when resolving
710 * @param packages is a pointer to a list of packages which will be
711 * searched first for any dependency packages needed to complete the
712 * resolve, and to which will be added any [pkg] and all of its
713 * dependencies not already on the list
714 * @param remove is the set of packages which will be removed in this
715 * transaction
716 * @param data returns the dependency which could not be satisfied in the
717 * event of an error
718 * @return 0 on success, with [pkg] and all of its dependencies not already on
719 * the [*packages] list added to that list, or -1 on failure due to an
720 * unresolvable dependency, in which case the [*packages] list will be
721 * unmodified by this function
723 int _alpm_resolvedeps(alpm_handle_t *handle, alpm_list_t *localpkgs,
724 alpm_pkg_t *pkg, alpm_list_t *preferred, alpm_list_t **packages,
725 alpm_list_t *rem, alpm_list_t **data)
727 int ret = 0;
728 alpm_list_t *i, *j;
729 alpm_list_t *targ;
730 alpm_list_t *deps = NULL;
731 alpm_list_t *packages_copy;
733 if(_alpm_pkg_find(*packages, pkg->name) != NULL) {
734 return 0;
737 /* Create a copy of the packages list, so that it can be restored
738 on error */
739 packages_copy = alpm_list_copy(*packages);
740 /* [pkg] has not already been resolved into the packages list, so put it
741 on that list */
742 *packages = alpm_list_add(*packages, pkg);
744 _alpm_log(handle, ALPM_LOG_DEBUG, "started resolving dependencies\n");
745 for(i = alpm_list_last(*packages); i; i = i->next) {
746 alpm_pkg_t *tpkg = i->data;
747 targ = alpm_list_add(NULL, tpkg);
748 deps = alpm_checkdeps(handle, localpkgs, rem, targ, 0);
749 alpm_list_free(targ);
751 for(j = deps; j; j = j->next) {
752 alpm_depmissing_t *miss = j->data;
753 alpm_depend_t *missdep = miss->depend;
754 /* check if one of the packages in the [*packages] list already satisfies
755 * this dependency */
756 if(find_dep_satisfier(*packages, missdep)) {
757 _alpm_depmiss_free(miss);
758 continue;
760 /* check if one of the packages in the [preferred] list already satisfies
761 * this dependency */
762 alpm_pkg_t *spkg = find_dep_satisfier(preferred, missdep);
763 if(!spkg) {
764 /* find a satisfier package in the given repositories */
765 spkg = resolvedep(handle, missdep, handle->dbs_sync, *packages, 0);
767 if(!spkg) {
768 handle->pm_errno = ALPM_ERR_UNSATISFIED_DEPS;
769 char *missdepstring = alpm_dep_compute_string(missdep);
770 _alpm_log(handle, ALPM_LOG_WARNING,
771 _("cannot resolve \"%s\", a dependency of \"%s\"\n"),
772 missdepstring, tpkg->name);
773 free(missdepstring);
774 if(data) {
775 *data = alpm_list_add(*data, miss);
777 ret = -1;
778 } else {
779 _alpm_log(handle, ALPM_LOG_DEBUG,
780 "pulling dependency %s (needed by %s)\n",
781 spkg->name, tpkg->name);
782 *packages = alpm_list_add(*packages, spkg);
783 _alpm_depmiss_free(miss);
786 alpm_list_free(deps);
789 if(ret != 0) {
790 alpm_list_free(*packages);
791 *packages = packages_copy;
792 } else {
793 alpm_list_free(packages_copy);
795 _alpm_log(handle, ALPM_LOG_DEBUG, "finished resolving dependencies\n");
796 return ret;
799 /** Reverse of splitdep; make a dep string from a alpm_depend_t struct.
800 * The string must be freed!
801 * @param dep the depend to turn into a string
802 * @return a string-formatted dependency with operator if necessary
804 char SYMEXPORT *alpm_dep_compute_string(const alpm_depend_t *dep)
806 const char *name, *opr, *ver, *desc_delim, *desc;
807 char *str;
808 size_t len;
810 ASSERT(dep != NULL, return NULL);
812 if(dep->name) {
813 name = dep->name;
814 } else {
815 name = "";
818 switch(dep->mod) {
819 case ALPM_DEP_MOD_ANY:
820 opr = "";
821 break;
822 case ALPM_DEP_MOD_GE:
823 opr = ">=";
824 break;
825 case ALPM_DEP_MOD_LE:
826 opr = "<=";
827 break;
828 case ALPM_DEP_MOD_EQ:
829 opr = "=";
830 break;
831 case ALPM_DEP_MOD_LT:
832 opr = "<";
833 break;
834 case ALPM_DEP_MOD_GT:
835 opr = ">";
836 break;
837 default:
838 opr = "";
839 break;
842 if(dep->mod != ALPM_DEP_MOD_ANY && dep->version) {
843 ver = dep->version;
844 } else {
845 ver = "";
848 if(dep->desc) {
849 desc_delim = ": ";
850 desc = dep->desc;
851 } else {
852 desc_delim = "";
853 desc = "";
856 /* we can always compute len and print the string like this because opr
857 * and ver will be empty when ALPM_DEP_MOD_ANY is the depend type. the
858 * reassignments above also ensure we do not do a strlen(NULL). */
859 len = strlen(name) + strlen(opr) + strlen(ver)
860 + strlen(desc_delim) + strlen(desc) + 1;
861 MALLOC(str, len, return NULL);
862 snprintf(str, len, "%s%s%s%s%s", name, opr, ver, desc_delim, desc);
864 return str;
866 /* vim: set ts=2 sw=2 noet: */