]> git.saurik.com Git - apt.git/commitdiff
refactor: move solver execution into his own EDSP method
authorDavid Kalnischkies <kalnischkies@gmail.com>
Mon, 2 May 2011 11:55:51 +0000 (13:55 +0200)
committerDavid Kalnischkies <kalnischkies@gmail.com>
Mon, 2 May 2011 11:55:51 +0000 (13:55 +0200)
apt-pkg/algorithms.cc
apt-pkg/edsp.cc
apt-pkg/edsp.h

index e40f74122e02884c71b5e3967ba7b733bd8d591d..82b1d608dccff159e04473f962168ec68ae8381b 100644 (file)
@@ -743,51 +743,19 @@ bool pkgProblemResolver::Resolve(bool BrokenFix)
 
    if (solver != "internal")
    {
-      std::vector<std::string> const solverDirs = _config->FindVector("Dir::Bin::Solvers");
-      std::string file;
-      for (std::vector<std::string>::const_iterator dir = solverDirs.begin();
-          dir != solverDirs.end(); ++dir) {
-        file = flCombine(*dir, solver);
-        if (RealFileExists(file.c_str()) == true)
-           break;
-        file.clear();
-      }
-
-      if (file.empty() == true)
-        return _error->Error("Can't call external solver '%s' as it is not in a configured directory!", solver.c_str());
-      int external[4] = {-1, -1, -1, -1};
-      if (pipe(external) != 0 || pipe(external + 2) != 0)
-        return _error->Errno("Resolve", "Can't create needed IPC pipes for EDSP");
-      for (int i = 0; i < 4; ++i)
-        SetCloseExec(external[i], true);
-
-      pid_t Solver = ExecFork();
-      if (Solver == 0)
-      {
-        dup2(external[0], STDIN_FILENO);
-        dup2(external[3], STDOUT_FILENO);
-        const char* calling[2] = { file.c_str(), 0 };
-        execv(calling[0], (char**) calling);
-        std::cerr << "Failed to execute solver '" << solver << "'!" << std::endl;
-        _exit(100);
-      }
-      close(external[0]);
-      close(external[3]);
-
-      if (WaitFd(external[1], true, 5) == false)
-         return _error->Errno("Resolve", "Waiting on availability of solver stdin timed out");
+      int solver_in, solver_out;
+      if (EDSP::ExecuteSolver(solver.c_str(), &solver_in, &solver_out) == false)
+        return false;
 
-      FILE* output = fdopen(external[1], "w");
+      FILE* output = fdopen(solver_in, "w");
       if (output == NULL)
          return _error->Errno("Resolve", "fdopen on solver stdin failed");
       EDSP::WriteRequest(Cache, output);
       EDSP::WriteScenario(Cache, output);
       fclose(output);
 
-      if (EDSP::ReadResponse(external[2], Cache) == false)
+      if (EDSP::ReadResponse(solver_out, Cache) == false)
         return _error->Error("Reading solver response failed");
-
-      return ExecWait(Solver, solver.c_str(), false);
    }
    return ResolveInternal(BrokenFix);
 }
index 170e2a4c691a4f8c9c54e91aa1061d534caa42c2..c3e608d1730ace7d55c92d2f30f1a9f7898433b5 100644 (file)
@@ -441,3 +441,45 @@ bool EDSP::WriteProgress(unsigned short const percent, const char* const message
 }
                                                                        /*}}}*/
 bool EDSP::WriteError(std::string const &message, FILE* output) { return false; }
+
+// EDSP::ExecuteSolver - fork requested solver and setup ipc pipes     {{{*/
+bool EDSP::ExecuteSolver(const char* const solver, int *solver_in, int *solver_out) {
+      std::vector<std::string> const solverDirs = _config->FindVector("Dir::Bin::Solvers");
+      std::string file;
+      for (std::vector<std::string>::const_iterator dir = solverDirs.begin();
+          dir != solverDirs.end(); ++dir) {
+        file = flCombine(*dir, solver);
+        if (RealFileExists(file.c_str()) == true)
+           break;
+        file.clear();
+      }
+
+      if (file.empty() == true)
+        return _error->Error("Can't call external solver '%s' as it is not in a configured directory!", solver);
+      int external[4] = {-1, -1, -1, -1};
+      if (pipe(external) != 0 || pipe(external + 2) != 0)
+        return _error->Errno("Resolve", "Can't create needed IPC pipes for EDSP");
+      for (int i = 0; i < 4; ++i)
+        SetCloseExec(external[i], true);
+
+      pid_t Solver = ExecFork();
+      if (Solver == 0)
+      {
+        dup2(external[0], STDIN_FILENO);
+        dup2(external[3], STDOUT_FILENO);
+        const char* calling[2] = { file.c_str(), 0 };
+        execv(calling[0], (char**) calling);
+        std::cerr << "Failed to execute solver '" << solver << "'!" << std::endl;
+        _exit(100);
+      }
+      close(external[0]);
+      close(external[3]);
+
+      if (WaitFd(external[1], true, 5) == false)
+         return _error->Errno("Resolve", "Timed out while Waiting on availability of solver stdin");
+
+      *solver_in = external[1];
+      *solver_out = external[2];
+      return true;
+}
+                                                                       /*}}}*/
index a05de94487e72bc76a4b475354ee8c5367a81c93..df6e1d21c530668090beec5c6ed7c4fcee5c842b 100644 (file)
@@ -52,6 +52,7 @@ public:
        bool static WriteProgress(unsigned short const percent, const char* const message, FILE* output);
        bool static WriteError(std::string const &message, FILE* output);
 
+       bool static ExecuteSolver(const char* const solver, int *solver_in, int *solver_out);
 };
                                                                        /*}}}*/
 #endif