pacman: list all unknown targets on removal operation
[pacman-ng.git] / lib / libalpm / deps.c
blob89f6d6915eb78e6321857ef95024756b482c90f8
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"
38 #include "trans.h"
40 void _alpm_dep_free(alpm_depend_t *dep)
42 FREE(dep->name);
43 FREE(dep->version);
44 FREE(dep);
47 static alpm_depmissing_t *depmiss_new(const char *target, alpm_depend_t *dep,
48 const char *causingpkg)
50 alpm_depmissing_t *miss;
52 MALLOC(miss, sizeof(alpm_depmissing_t), return NULL);
54 STRDUP(miss->target, target, return NULL);
55 miss->depend = _alpm_dep_dup(dep);
56 STRDUP(miss->causingpkg, causingpkg, return NULL);
58 return miss;
61 void _alpm_depmiss_free(alpm_depmissing_t *miss)
63 _alpm_dep_free(miss->depend);
64 FREE(miss->target);
65 FREE(miss->causingpkg);
66 FREE(miss);
69 /* Does pkg1 depend on pkg2, ie. does pkg2 satisfy a dependency of pkg1? */
70 static int _alpm_dep_edge(alpm_pkg_t *pkg1, alpm_pkg_t *pkg2)
72 alpm_list_t *i;
73 for(i = alpm_pkg_get_depends(pkg1); i; i = i->next) {
74 if(_alpm_depcmp(pkg2, i->data)) {
75 return 1;
78 return 0;
81 /* Convert a list of alpm_pkg_t * to a graph structure,
82 * with a edge for each dependency.
83 * Returns a list of vertices (one vertex = one package)
84 * (used by alpm_sortbydeps)
86 static alpm_list_t *dep_graph_init(alpm_list_t *targets)
88 alpm_list_t *i, *j;
89 alpm_list_t *vertices = NULL;
90 /* We create the vertices */
91 for(i = targets; i; i = i->next) {
92 alpm_graph_t *vertex = _alpm_graph_new();
93 vertex->data = (void *)i->data;
94 vertices = alpm_list_add(vertices, vertex);
97 /* We compute the edges */
98 for(i = vertices; i; i = i->next) {
99 alpm_graph_t *vertex_i = i->data;
100 alpm_pkg_t *p_i = vertex_i->data;
101 /* TODO this should be somehow combined with alpm_checkdeps */
102 for(j = vertices; j; j = j->next) {
103 alpm_graph_t *vertex_j = j->data;
104 alpm_pkg_t *p_j = vertex_j->data;
105 if(_alpm_dep_edge(p_i, p_j)) {
106 vertex_i->children =
107 alpm_list_add(vertex_i->children, vertex_j);
110 vertex_i->childptr = vertex_i->children;
112 return vertices;
115 /* Re-order a list of target packages with respect to their dependencies.
117 * Example (reverse == 0):
118 * A depends on C
119 * B depends on A
120 * Target order is A,B,C,D
122 * Should be re-ordered to C,A,B,D
124 * if reverse is > 0, the dependency order will be reversed.
126 * This function returns the new alpm_list_t* target list.
129 alpm_list_t *_alpm_sortbydeps(alpm_handle_t *handle,
130 alpm_list_t *targets, int reverse)
132 alpm_list_t *newtargs = NULL;
133 alpm_list_t *vertices = NULL;
134 alpm_list_t *vptr;
135 alpm_graph_t *vertex;
137 if(targets == NULL) {
138 return NULL;
141 _alpm_log(handle, ALPM_LOG_DEBUG, "started sorting dependencies\n");
143 vertices = dep_graph_init(targets);
145 vptr = vertices;
146 vertex = vertices->data;
147 while(vptr) {
148 /* mark that we touched the vertex */
149 vertex->state = -1;
150 int found = 0;
151 while(vertex->childptr && !found) {
152 alpm_graph_t *nextchild = vertex->childptr->data;
153 vertex->childptr = vertex->childptr->next;
154 if(nextchild->state == 0) {
155 found = 1;
156 nextchild->parent = vertex;
157 vertex = nextchild;
159 else if(nextchild->state == -1) {
160 alpm_pkg_t *vertexpkg = vertex->data;
161 alpm_pkg_t *childpkg = nextchild->data;
162 const char *message;
164 _alpm_log(handle, ALPM_LOG_WARNING, _("dependency cycle detected:\n"));
165 if(reverse) {
166 message =_("%s will be removed after its %s dependency\n");
167 } else {
168 message =_("%s will be installed before its %s dependency\n");
170 _alpm_log(handle, ALPM_LOG_WARNING, message, vertexpkg->name, childpkg->name);
173 if(!found) {
174 newtargs = alpm_list_add(newtargs, vertex->data);
175 /* mark that we've left this vertex */
176 vertex->state = 1;
177 vertex = vertex->parent;
178 if(!vertex) {
179 vptr = vptr->next;
180 while(vptr) {
181 vertex = vptr->data;
182 if(vertex->state == 0) break;
183 vptr = vptr->next;
189 _alpm_log(handle, ALPM_LOG_DEBUG, "sorting dependencies finished\n");
191 if(reverse) {
192 /* reverse the order */
193 alpm_list_t *tmptargs = alpm_list_reverse(newtargs);
194 /* free the old one */
195 alpm_list_free(newtargs);
196 newtargs = tmptargs;
199 alpm_list_free_inner(vertices, _alpm_graph_free);
200 alpm_list_free(vertices);
202 return newtargs;
205 static int no_dep_version(alpm_handle_t *handle)
207 int flags = alpm_trans_get_flags(handle);
208 return flags != -1 && (flags & ALPM_TRANS_FLAG_NODEPVERSION);
211 static alpm_depend_t *filtered_depend(alpm_depend_t *dep, int nodepversion)
213 if(nodepversion) {
214 alpm_depend_t *newdep = _alpm_dep_dup(dep);
215 ASSERT(newdep, return dep);
216 newdep->mod = ALPM_DEP_MOD_ANY;
217 dep = newdep;
219 return dep;
222 static void release_filtered_depend(alpm_depend_t *dep, int nodepversion)
224 if(nodepversion) {
225 free(dep);
229 static alpm_pkg_t *find_dep_satisfier(alpm_list_t *pkgs, alpm_depend_t *dep)
231 alpm_list_t *i;
233 for(i = pkgs; i; i = i->next) {
234 alpm_pkg_t *pkg = i->data;
235 if(_alpm_depcmp(pkg, dep)) {
236 return pkg;
239 return NULL;
242 /** Find a package satisfying a specified dependency.
243 * The dependency can include versions with depmod operators.
244 * @param pkgs an alpm_list_t* of alpm_pkg_t where the satisfier will be searched
245 * @param depstring package or provision name, versioned or not
246 * @return a alpm_pkg_t* satisfying depstring
248 alpm_pkg_t SYMEXPORT *alpm_find_satisfier(alpm_list_t *pkgs, const char *depstring)
250 alpm_depend_t *dep = _alpm_splitdep(depstring);
251 if(!dep) {
252 return NULL;
254 alpm_pkg_t *pkg = find_dep_satisfier(pkgs, dep);
255 _alpm_dep_free(dep);
256 return pkg;
259 /** Checks dependencies and returns missing ones in a list.
260 * Dependencies can include versions with depmod operators.
261 * @param handle the context handle
262 * @param pkglist the list of local packages
263 * @param remove an alpm_list_t* of packages to be removed
264 * @param upgrade an alpm_list_t* of packages to be upgraded (remove-then-upgrade)
265 * @param reversedeps handles the backward dependencies
266 * @return an alpm_list_t* of alpm_depmissing_t pointers.
268 alpm_list_t SYMEXPORT *alpm_checkdeps(alpm_handle_t *handle,
269 alpm_list_t *pkglist, alpm_list_t *remove, alpm_list_t *upgrade,
270 int reversedeps)
272 alpm_list_t *i, *j;
273 alpm_list_t *dblist = NULL, *modified = NULL;
274 alpm_list_t *baddeps = NULL;
275 int nodepversion;
277 CHECK_HANDLE(handle, return NULL);
279 for(i = pkglist; i; i = i->next) {
280 alpm_pkg_t *pkg = i->data;
281 if(_alpm_pkg_find(remove, pkg->name) || _alpm_pkg_find(upgrade, pkg->name)) {
282 modified = alpm_list_add(modified, pkg);
283 } else {
284 dblist = alpm_list_add(dblist, pkg);
288 nodepversion = no_dep_version(handle);
290 /* look for unsatisfied dependencies of the upgrade list */
291 for(i = upgrade; i; i = i->next) {
292 alpm_pkg_t *tp = i->data;
293 _alpm_log(handle, ALPM_LOG_DEBUG, "checkdeps: package %s-%s\n",
294 tp->name, tp->version);
296 for(j = alpm_pkg_get_depends(tp); j; j = j->next) {
297 alpm_depend_t *depend = j->data;
298 depend = filtered_depend(depend, nodepversion);
299 /* 1. we check the upgrade list */
300 /* 2. we check database for untouched satisfying packages */
301 if(!find_dep_satisfier(upgrade, depend) &&
302 !find_dep_satisfier(dblist, depend)) {
303 /* Unsatisfied dependency in the upgrade list */
304 alpm_depmissing_t *miss;
305 char *missdepstring = alpm_dep_compute_string(depend);
306 _alpm_log(handle, ALPM_LOG_DEBUG, "checkdeps: missing dependency '%s' for package '%s'\n",
307 missdepstring, tp->name);
308 free(missdepstring);
309 miss = depmiss_new(tp->name, depend, NULL);
310 baddeps = alpm_list_add(baddeps, miss);
312 release_filtered_depend(depend, nodepversion);
316 if(reversedeps) {
317 /* reversedeps handles the backwards dependencies, ie,
318 * the packages listed in the requiredby field. */
319 for(i = dblist; i; i = i->next) {
320 alpm_pkg_t *lp = i->data;
321 for(j = alpm_pkg_get_depends(lp); j; j = j->next) {
322 alpm_depend_t *depend = j->data;
323 depend = filtered_depend(depend, nodepversion);
324 alpm_pkg_t *causingpkg = find_dep_satisfier(modified, depend);
325 /* we won't break this depend, if it is already broken, we ignore it */
326 /* 1. check upgrade list for satisfiers */
327 /* 2. check dblist for satisfiers */
328 if(causingpkg &&
329 !find_dep_satisfier(upgrade, depend) &&
330 !find_dep_satisfier(dblist, depend)) {
331 alpm_depmissing_t *miss;
332 char *missdepstring = alpm_dep_compute_string(depend);
333 _alpm_log(handle, ALPM_LOG_DEBUG, "checkdeps: transaction would break '%s' dependency of '%s'\n",
334 missdepstring, lp->name);
335 free(missdepstring);
336 miss = depmiss_new(lp->name, depend, causingpkg->name);
337 baddeps = alpm_list_add(baddeps, miss);
339 release_filtered_depend(depend, nodepversion);
344 alpm_list_free(modified);
345 alpm_list_free(dblist);
347 return baddeps;
350 static int dep_vercmp(const char *version1, alpm_depmod_t mod,
351 const char *version2)
353 int equal = 0;
355 if(mod == ALPM_DEP_MOD_ANY) {
356 equal = 1;
357 } else {
358 int cmp = alpm_pkg_vercmp(version1, version2);
359 switch(mod) {
360 case ALPM_DEP_MOD_EQ: equal = (cmp == 0); break;
361 case ALPM_DEP_MOD_GE: equal = (cmp >= 0); break;
362 case ALPM_DEP_MOD_LE: equal = (cmp <= 0); break;
363 case ALPM_DEP_MOD_LT: equal = (cmp < 0); break;
364 case ALPM_DEP_MOD_GT: equal = (cmp > 0); break;
365 default: equal = 1; break;
368 return equal;
371 int _alpm_depcmp_literal(alpm_pkg_t *pkg, alpm_depend_t *dep)
373 if(pkg->name_hash != dep->name_hash
374 || strcmp(pkg->name, dep->name) != 0) {
375 /* skip more expensive checks */
376 return 0;
378 return dep_vercmp(pkg->version, dep->mod, dep->version);
381 int _alpm_depcmp(alpm_pkg_t *pkg, alpm_depend_t *dep)
383 alpm_list_t *i;
384 int satisfy = _alpm_depcmp_literal(pkg, dep);
386 if(satisfy) {
387 return satisfy;
390 /* check provisions, name and version if available */
391 for(i = alpm_pkg_get_provides(pkg); i && !satisfy; i = i->next) {
392 alpm_depend_t *provision = i->data;
394 if(dep->mod == ALPM_DEP_MOD_ANY) {
395 /* any version will satisfy the requirement */
396 satisfy = (provision->name_hash == dep->name_hash
397 && strcmp(provision->name, dep->name) == 0);
398 } else if (provision->mod == ALPM_DEP_MOD_EQ) {
399 /* provision specifies a version, so try it out */
400 satisfy = (provision->name_hash == dep->name_hash
401 && strcmp(provision->name, dep->name) == 0
402 && dep_vercmp(provision->version, dep->mod, dep->version));
406 return satisfy;
409 alpm_depend_t *_alpm_splitdep(const char *depstring)
411 alpm_depend_t *depend;
412 const char *ptr, *version;
413 size_t deplen;
415 if(depstring == NULL) {
416 return NULL;
419 MALLOC(depend, sizeof(alpm_depend_t), return NULL);
420 deplen = strlen(depstring);
422 /* Find a version comparator if one exists. If it does, set the type and
423 * increment the ptr accordingly so we can copy the right strings. */
424 if((ptr = memchr(depstring, '<', deplen))) {
425 if(ptr[1] == '=') {
426 depend->mod = ALPM_DEP_MOD_LE;
427 version = ptr + 2;
428 } else {
429 depend->mod = ALPM_DEP_MOD_LT;
430 version = ptr + 1;
432 } else if((ptr = memchr(depstring, '>', deplen))) {
433 if(ptr[1] == '=') {
434 depend->mod = ALPM_DEP_MOD_GE;
435 version = ptr + 2;
436 } else {
437 depend->mod = ALPM_DEP_MOD_GT;
438 version = ptr + 1;
440 } else if((ptr = memchr(depstring, '=', deplen))) {
441 /* Note: we must do =,<,> checks after <=, >= checks */
442 depend->mod = ALPM_DEP_MOD_EQ;
443 version = ptr + 1;
444 } else {
445 /* no version specified, leave ptr NULL and set version to NULL */
446 depend->mod = ALPM_DEP_MOD_ANY;
447 depend->version = NULL;
448 version = NULL;
451 /* copy the right parts to the right places */
452 if(ptr) {
453 STRNDUP(depend->name, depstring, ptr - depstring, return NULL);
454 } else {
455 STRDUP(depend->name, depstring, return NULL);
457 depend->name_hash = _alpm_hash_sdbm(depend->name);
458 if(version) {
459 STRDUP(depend->version, version, return NULL);
462 return depend;
465 alpm_depend_t *_alpm_dep_dup(const alpm_depend_t *dep)
467 alpm_depend_t *newdep;
468 CALLOC(newdep, 1, sizeof(alpm_depend_t), return NULL);
470 STRDUP(newdep->name, dep->name, return NULL);
471 newdep->name_hash = dep->name_hash;
472 STRDUP(newdep->version, dep->version, return NULL);
473 newdep->mod = dep->mod;
475 return newdep;
478 /* These parameters are messy. We check if this package, given a list of
479 * targets and a db is safe to remove. We do NOT remove it if it is in the
480 * target list, or if if the package was explictly installed and
481 * include_explicit == 0 */
482 static int can_remove_package(alpm_db_t *db, alpm_pkg_t *pkg,
483 alpm_list_t *targets, int include_explicit)
485 alpm_list_t *i;
487 if(_alpm_pkg_find(targets, pkg->name)) {
488 return 0;
491 if(!include_explicit) {
492 /* see if it was explicitly installed */
493 if(alpm_pkg_get_reason(pkg) == ALPM_PKG_REASON_EXPLICIT) {
494 _alpm_log(db->handle, ALPM_LOG_DEBUG,
495 "excluding %s -- explicitly installed\n", pkg->name);
496 return 0;
500 /* TODO: checkdeps could be used here, it handles multiple providers
501 * better, but that also makes it slower.
502 * Also this would require to first add the package to the targets list,
503 * then call checkdeps with it, then remove the package from the targets list
504 * if checkdeps detected it would break something */
506 /* see if other packages need it */
507 for(i = _alpm_db_get_pkgcache(db); i; i = i->next) {
508 alpm_pkg_t *lpkg = i->data;
509 if(_alpm_dep_edge(lpkg, pkg) && !_alpm_pkg_find(targets, lpkg->name)) {
510 return 0;
514 /* it's ok to remove */
515 return 1;
519 * @brief Adds unneeded dependencies to an existing list of packages.
520 * By unneeded, we mean dependencies that are only required by packages in the
521 * target list, so they can be safely removed.
522 * If the input list was topo sorted, the output list will be topo sorted too.
524 * @param db package database to do dependency tracing in
525 * @param *targs pointer to a list of packages
526 * @param include_explicit if 0, explicitly installed packages are not included
527 * @return 0 on success, -1 on errors
529 int _alpm_recursedeps(alpm_db_t *db, alpm_list_t *targs, int include_explicit)
531 alpm_list_t *i, *j;
533 if(db == NULL || targs == NULL) {
534 return -1;
537 for(i = targs; i; i = i->next) {
538 alpm_pkg_t *pkg = i->data;
539 for(j = _alpm_db_get_pkgcache(db); j; j = j->next) {
540 alpm_pkg_t *deppkg = j->data;
541 if(_alpm_dep_edge(pkg, deppkg)
542 && can_remove_package(db, deppkg, targs, include_explicit)) {
543 alpm_pkg_t *copy;
544 _alpm_log(db->handle, ALPM_LOG_DEBUG, "adding '%s' to the targets\n",
545 deppkg->name);
546 /* add it to the target list */
547 if(_alpm_pkg_dup(deppkg, &copy)) {
548 return -1;
550 targs = alpm_list_add(targs, copy);
554 return 0;
558 * helper function for resolvedeps: search for dep satisfier in dbs
560 * @param handle the context handle
561 * @param dep is the dependency to search for
562 * @param dbs are the databases to search
563 * @param excluding are the packages to exclude from the search
564 * @param prompt if true, will cause an unresolvable dependency to issue an
565 * interactive prompt asking whether the package should be removed from
566 * the transaction or the transaction aborted; if false, simply returns
567 * an error code without prompting
568 * @return the resolved package
570 static alpm_pkg_t *resolvedep(alpm_handle_t *handle, alpm_depend_t *dep,
571 alpm_list_t *dbs, alpm_list_t *excluding, int prompt)
573 alpm_list_t *i, *j;
574 int ignored = 0;
576 alpm_list_t *providers = NULL;
577 int count;
579 /* 1. literals */
580 for(i = dbs; i; i = i->next) {
581 alpm_pkg_t *pkg = _alpm_db_get_pkgfromcache(i->data, dep->name);
582 if(pkg && _alpm_depcmp_literal(pkg, dep)
583 && !_alpm_pkg_find(excluding, pkg->name)) {
584 if(_alpm_pkg_should_ignore(handle, pkg)) {
585 int install = 0;
586 if(prompt) {
587 QUESTION(handle, ALPM_QUESTION_INSTALL_IGNOREPKG, pkg,
588 NULL, NULL, &install);
589 } else {
590 _alpm_log(handle, ALPM_LOG_WARNING, _("ignoring package %s-%s\n"),
591 pkg->name, pkg->version);
593 if(!install) {
594 ignored = 1;
595 continue;
598 return pkg;
601 /* 2. satisfiers (skip literals here) */
602 for(i = dbs; i; i = i->next) {
603 for(j = _alpm_db_get_pkgcache(i->data); j; j = j->next) {
604 alpm_pkg_t *pkg = j->data;
605 /* with hash != hash, we can even skip the strcmp() as we know they can't
606 * possibly be the same string */
607 if(pkg->name_hash != dep->name_hash && _alpm_depcmp(pkg, dep)
608 && !_alpm_pkg_find(excluding, pkg->name)) {
609 if(_alpm_pkg_should_ignore(handle, pkg)) {
610 int install = 0;
611 if(prompt) {
612 QUESTION(handle, ALPM_QUESTION_INSTALL_IGNOREPKG,
613 pkg, NULL, NULL, &install);
614 } else {
615 _alpm_log(handle, ALPM_LOG_WARNING, _("ignoring package %s-%s\n"),
616 pkg->name, pkg->version);
618 if(!install) {
619 ignored = 1;
620 continue;
623 _alpm_log(handle, ALPM_LOG_DEBUG, "provider found (%s provides %s)\n",
624 pkg->name, dep->name);
625 providers = alpm_list_add(providers, pkg);
626 /* keep looking for other providers in the all dbs */
631 /* first check if one provider is already installed locally */
632 for(i = providers; i; i = i->next) {
633 alpm_pkg_t *pkg = i->data;
634 if(_alpm_db_get_pkgfromcache(handle->db_local, pkg->name)) {
635 alpm_list_free(providers);
636 return pkg;
639 count = alpm_list_count(providers);
640 if(count >= 1) {
641 /* default to first provider if there is no QUESTION callback */
642 int index = 0;
643 if(count > 1) {
644 /* if there is more than one provider, we ask the user */
645 QUESTION(handle, ALPM_QUESTION_SELECT_PROVIDER,
646 providers, dep, NULL, &index);
648 if(index >= 0 && index < count) {
649 alpm_list_t *nth = alpm_list_nth(providers, index);
650 alpm_pkg_t *pkg = nth->data;
651 alpm_list_free(providers);
652 return pkg;
654 alpm_list_free(providers);
655 providers = NULL;
658 if(ignored) { /* resolvedeps will override these */
659 handle->pm_errno = ALPM_ERR_PKG_IGNORED;
660 } else {
661 handle->pm_errno = ALPM_ERR_PKG_NOT_FOUND;
663 return NULL;
666 /** Find a package satisfying a specified dependency.
667 * First look for a literal, going through each db one by one. Then look for
668 * providers. The first satisfier found is returned.
669 * The dependency can include versions with depmod operators.
670 * @param handle the context handle
671 * @param dbs an alpm_list_t* of alpm_db_t where the satisfier will be searched
672 * @param depstring package or provision name, versioned or not
673 * @return a alpm_pkg_t* satisfying depstring
675 alpm_pkg_t SYMEXPORT *alpm_find_dbs_satisfier(alpm_handle_t *handle,
676 alpm_list_t *dbs, const char *depstring)
678 alpm_depend_t *dep;
679 alpm_pkg_t *pkg;
681 CHECK_HANDLE(handle, return NULL);
682 ASSERT(dbs, RET_ERR(handle, ALPM_ERR_WRONG_ARGS, NULL));
684 dep = _alpm_splitdep(depstring);
685 ASSERT(dep, return NULL);
686 pkg = resolvedep(handle, dep, dbs, NULL, 1);
687 _alpm_dep_free(dep);
688 return pkg;
692 * Computes resolvable dependencies for a given package and adds that package
693 * and those resolvable dependencies to a list.
695 * @param handle the context handle
696 * @param localpkgs is the list of local packages
697 * @param pkg is the package to resolve
698 * @param preferred packages to prefer when resolving
699 * @param packages is a pointer to a list of packages which will be
700 * searched first for any dependency packages needed to complete the
701 * resolve, and to which will be added any [pkg] and all of its
702 * dependencies not already on the list
703 * @param remove is the set of packages which will be removed in this
704 * transaction
705 * @param data returns the dependency which could not be satisfied in the
706 * event of an error
707 * @return 0 on success, with [pkg] and all of its dependencies not already on
708 * the [*packages] list added to that list, or -1 on failure due to an
709 * unresolvable dependency, in which case the [*packages] list will be
710 * unmodified by this function
712 int _alpm_resolvedeps(alpm_handle_t *handle, alpm_list_t *localpkgs,
713 alpm_pkg_t *pkg, alpm_list_t *preferred, alpm_list_t **packages,
714 alpm_list_t *remove, alpm_list_t **data)
716 int ret = 0;
717 alpm_list_t *i, *j;
718 alpm_list_t *targ;
719 alpm_list_t *deps = NULL;
720 alpm_list_t *packages_copy;
722 if(_alpm_pkg_find(*packages, pkg->name) != NULL) {
723 return 0;
726 if(handle->trans->flags & ALPM_TRANS_FLAG_RECURSE) {
727 /* removing local packages from the equation causes the entire dep chain to
728 * get pulled for each target- e.g., pactree -u output */
729 localpkgs = NULL;
732 /* Create a copy of the packages list, so that it can be restored
733 on error */
734 packages_copy = alpm_list_copy(*packages);
735 /* [pkg] has not already been resolved into the packages list, so put it
736 on that list */
737 *packages = alpm_list_add(*packages, pkg);
739 _alpm_log(handle, ALPM_LOG_DEBUG, "started resolving dependencies\n");
740 for(i = alpm_list_last(*packages); i; i = i->next) {
741 alpm_pkg_t *tpkg = i->data;
742 targ = alpm_list_add(NULL, tpkg);
743 deps = alpm_checkdeps(handle, localpkgs, remove, targ, 0);
744 alpm_list_free(targ);
746 for(j = deps; j; j = j->next) {
747 alpm_depmissing_t *miss = j->data;
748 alpm_depend_t *missdep = miss->depend;
749 /* check if one of the packages in the [*packages] list already satisfies
750 * this dependency */
751 if(find_dep_satisfier(*packages, missdep)) {
752 _alpm_depmiss_free(miss);
753 continue;
755 /* check if one of the packages in the [preferred] list already satisfies
756 * this dependency */
757 alpm_pkg_t *spkg = find_dep_satisfier(preferred, missdep);
758 if(!spkg) {
759 /* find a satisfier package in the given repositories */
760 spkg = resolvedep(handle, missdep, handle->dbs_sync, *packages, 0);
762 if(!spkg) {
763 handle->pm_errno = ALPM_ERR_UNSATISFIED_DEPS;
764 char *missdepstring = alpm_dep_compute_string(missdep);
765 _alpm_log(handle, ALPM_LOG_WARNING,
766 _("cannot resolve \"%s\", a dependency of \"%s\"\n"),
767 missdepstring, tpkg->name);
768 free(missdepstring);
769 if(data) {
770 *data = alpm_list_add(*data, miss);
772 ret = -1;
773 } else {
774 _alpm_log(handle, ALPM_LOG_DEBUG,
775 "pulling dependency %s (needed by %s)\n",
776 spkg->name, tpkg->name);
777 *packages = alpm_list_add(*packages, spkg);
778 _alpm_depmiss_free(miss);
781 alpm_list_free(deps);
784 if(handle->trans->flags & ALPM_TRANS_FLAG_NEEDED) {
785 /* remove any deps that were pulled that match installed version */
786 /* odd loop syntax so we can modify the list as we iterate */
787 i = *packages;
788 while(i) {
789 alpm_pkg_t *tpkg = i->data;
790 alpm_pkg_t *local = _alpm_db_get_pkgfromcache(
791 handle->db_local, tpkg->name);
792 if(local && _alpm_pkg_compare_versions(tpkg, local) == 0) {
793 /* with the NEEDED flag, packages up to date are not reinstalled */
794 _alpm_log(handle, ALPM_LOG_DEBUG,
795 "not adding dep %s-%s as it is not needed, same version\n",
796 local->name, local->version);
797 j = i;
798 i = i->next;
799 *packages = alpm_list_remove_item(*packages, j);
800 free(j);
801 } else {
802 i = i->next;
807 if(ret != 0) {
808 alpm_list_free(*packages);
809 *packages = packages_copy;
810 } else {
811 alpm_list_free(packages_copy);
813 _alpm_log(handle, ALPM_LOG_DEBUG, "finished resolving dependencies\n");
814 return ret;
817 /** Reverse of splitdep; make a dep string from a alpm_depend_t struct.
818 * The string must be freed!
819 * @param dep the depend to turn into a string
820 * @return a string-formatted dependency with operator if necessary
822 char SYMEXPORT *alpm_dep_compute_string(const alpm_depend_t *dep)
824 const char *name, *opr, *ver;
825 char *str;
826 size_t len;
828 ASSERT(dep != NULL, return NULL);
830 if(dep->name) {
831 name = dep->name;
832 } else {
833 name = "";
836 switch(dep->mod) {
837 case ALPM_DEP_MOD_ANY:
838 opr = "";
839 break;
840 case ALPM_DEP_MOD_GE:
841 opr = ">=";
842 break;
843 case ALPM_DEP_MOD_LE:
844 opr = "<=";
845 break;
846 case ALPM_DEP_MOD_EQ:
847 opr = "=";
848 break;
849 case ALPM_DEP_MOD_LT:
850 opr = "<";
851 break;
852 case ALPM_DEP_MOD_GT:
853 opr = ">";
854 break;
855 default:
856 opr = "";
857 break;
860 if(dep->mod != ALPM_DEP_MOD_ANY && dep->version) {
861 ver = dep->version;
862 } else {
863 ver = "";
866 /* we can always compute len and print the string like this because opr
867 * and ver will be empty when ALPM_DEP_MOD_ANY is the depend type. the
868 * reassignments above also ensure we do not do a strlen(NULL). */
869 len = strlen(name) + strlen(opr) + strlen(ver) + 1;
870 MALLOC(str, len, return NULL);
871 snprintf(str, len, "%s%s%s", name, opr, ver);
873 return str;
875 /* vim: set ts=2 sw=2 noet: */