From f434078a342435ae8a666b599d989c30d4c6a7f5 Mon Sep 17 00:00:00 2001
From: Richard Purdie <richard.purdie@linuxfoundation.org>
Date: Sun, 18 Dec 2011 23:54:30 +0000
Subject: [PATCH 6/7] detect circular dependencies

Add logic to detect circular dependencies. If we see any dependency from
any given parent twice, ignore it the second time and print a notice message
that we did so.

Upstream-Status: Submitted
http://code.google.com/p/opkg/issues/detail?id=93

Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Signed-off-by: Martin Jansa <Martin.Jansa@gmail.com>
---
 libopkg/opkg_install.c | 8 ++++++++
 libopkg/pkg.c          | 2 ++
 libopkg/pkg.h          | 1 +
 libopkg/pkg_depends.c  | 3 +--
 libopkg/pkg_depends.h  | 1 +
 5 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/libopkg/opkg_install.c b/libopkg/opkg_install.c
index 1632066..0216914 100644
--- a/libopkg/opkg_install.c
+++ b/libopkg/opkg_install.c
@@ -84,8 +84,14 @@ satisfy_dependencies_for(pkg_t *pkg)
 	       /* The package was uninstalled when we started, but another
 	          dep earlier in this loop may have depended on it and pulled
 	          it in, so check first. */
+               if (is_pkg_in_pkg_vec(dep->wanted_by, pkg)) {
+		    opkg_msg(NOTICE,"Breaking cicular dependency on %s for %s.\n", pkg->name, dep->name);
+	            continue;
+               }
 	       if ((dep->state_status != SS_INSTALLED) && (dep->state_status != SS_UNPACKED)) {
 		    opkg_msg(DEBUG2,"Calling opkg_install_pkg.\n");
+                    if (!is_pkg_in_pkg_vec(dep->wanted_by, pkg))
+     	                 pkg_vec_insert(dep->wanted_by, pkg);
 		    err = opkg_install_pkg(dep, 0);
 		    /* mark this package as having been automatically installed to
 		     * satisfy a dependency */
@@ -115,6 +121,8 @@ satisfy_dependencies_for(pkg_t *pkg)
 	  /* The package was uninstalled when we started, but another
 	     dep earlier in this loop may have depended on it and pulled
 	     it in, so check first. */
+          if (!is_pkg_in_pkg_vec(dep->wanted_by, pkg))
+	       pkg_vec_insert(dep->wanted_by, pkg);
 	  if ((dep->state_status != SS_INSTALLED)
 	      && (dep->state_status != SS_UNPACKED)) {
                opkg_msg(DEBUG2,"Calling opkg_install_pkg.\n");
diff --git a/libopkg/pkg.c b/libopkg/pkg.c
index 6ccbde2..be486ee 100644
--- a/libopkg/pkg.c
+++ b/libopkg/pkg.c
@@ -86,6 +86,7 @@ pkg_init(pkg_t *pkg)
      pkg->section = NULL;
      pkg->description = NULL;
      pkg->state_want = SW_UNKNOWN;
+     pkg->wanted_by = pkg_vec_alloc();
      pkg->state_flag = SF_OK;
      pkg->state_status = SS_NOT_INSTALLED;
      pkg->depends_str = NULL;
@@ -191,6 +192,7 @@ pkg_deinit(pkg_t *pkg)
 	pkg->description = NULL;
 
 	pkg->state_want = SW_UNKNOWN;
+	pkg_vec_free(pkg->wanted_by);
 	pkg->state_flag = SF_OK;
 	pkg->state_status = SS_NOT_INSTALLED;
 
diff --git a/libopkg/pkg.h b/libopkg/pkg.h
index 775b656..5d468cb 100644
--- a/libopkg/pkg.h
+++ b/libopkg/pkg.h
@@ -129,6 +129,7 @@ struct pkg
      char *description;
      char *tags;
      pkg_state_want_t state_want;
+     pkg_vec_t *wanted_by;
      pkg_state_flag_t state_flag;
      pkg_state_status_t state_status;
      char **depends_str;
diff --git a/libopkg/pkg_depends.c b/libopkg/pkg_depends.c
index 36c76aa..a72eed7 100644
--- a/libopkg/pkg_depends.c
+++ b/libopkg/pkg_depends.c
@@ -30,7 +30,6 @@ static int parseDepends(compound_depend_t *compound_depend, char * depend_str);
 static depend_t * depend_init(void);
 static char ** add_unresolved_dep(pkg_t * pkg, char ** the_lost, int ref_ndx);
 static char ** merge_unresolved(char ** oldstuff, char ** newstuff);
-static int is_pkg_in_pkg_vec(pkg_vec_t * vec, pkg_t * pkg);
 
 static int pkg_installed_and_constraint_satisfied(pkg_t *pkg, void *cdata)
 {
@@ -531,7 +530,7 @@ int pkg_dependence_satisfied(depend_t *depend)
      return 0;
 }
 
-static int is_pkg_in_pkg_vec(pkg_vec_t * vec, pkg_t * pkg)
+int is_pkg_in_pkg_vec(pkg_vec_t * vec, pkg_t * pkg)
 {
     int i;
     pkg_t ** pkgs = vec->pkgs;
diff --git a/libopkg/pkg_depends.h b/libopkg/pkg_depends.h
index b8072e2..ca0801f 100644
--- a/libopkg/pkg_depends.h
+++ b/libopkg/pkg_depends.h
@@ -87,5 +87,6 @@ pkg_vec_t * pkg_hash_fetch_conflicts(pkg_t * pkg);
 int pkg_dependence_satisfiable(depend_t *depend);
 int pkg_dependence_satisfied(depend_t *depend);
 const char* constraint_to_str(enum version_constraint c);
+int is_pkg_in_pkg_vec(pkg_vec_t * vec, pkg_t * pkg);
 
 #endif
-- 
1.7.12

