apt-internal-solver.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. /* #####################################################################
  4. cover around the internal solver to be able to run it like an external
  5. ##################################################################### */
  6. /*}}}*/
  7. // Include Files /*{{{*/
  8. #include <config.h>
  9. #include <apt-pkg/error.h>
  10. #include <apt-pkg/cmndline.h>
  11. #include <apt-pkg/init.h>
  12. #include <apt-pkg/cachefile.h>
  13. #include <apt-pkg/cacheset.h>
  14. #include <apt-pkg/strutl.h>
  15. #include <apt-pkg/edsp.h>
  16. #include <apt-pkg/algorithms.h>
  17. #include <apt-pkg/fileutl.h>
  18. #include <apt-pkg/pkgsystem.h>
  19. #include <apt-pkg/upgrade.h>
  20. #include <apt-pkg/configuration.h>
  21. #include <apt-pkg/depcache.h>
  22. #include <apt-pkg/pkgcache.h>
  23. #include <apt-pkg/cacheiterators.h>
  24. #include <apt-private/private-output.h>
  25. #include <apt-private/private-cmndline.h>
  26. #include <apt-private/private-main.h>
  27. #include <string.h>
  28. #include <iostream>
  29. #include <sstream>
  30. #include <list>
  31. #include <string>
  32. #include <unistd.h>
  33. #include <cstdio>
  34. #include <stdlib.h>
  35. #include <apti18n.h>
  36. /*}}}*/
  37. static bool ShowHelp(CommandLine &) /*{{{*/
  38. {
  39. std::cout <<
  40. _("Usage: apt-internal-solver\n"
  41. "\n"
  42. "apt-internal-solver is an interface to use the current internal\n"
  43. "resolver for the APT family like an external one, for debugging or\n"
  44. "the like.\n");
  45. return true;
  46. }
  47. /*}}}*/
  48. APT_NORETURN static void DIE(std::string const &message) { /*{{{*/
  49. std::cerr << "ERROR: " << message << std::endl;
  50. _error->DumpErrors(std::cerr);
  51. exit(EXIT_FAILURE);
  52. }
  53. /*}}}*/
  54. static std::vector<aptDispatchWithHelp> GetCommands() /*{{{*/
  55. {
  56. return {};
  57. }
  58. /*}}}*/
  59. int main(int argc,const char *argv[]) /*{{{*/
  60. {
  61. InitLocale();
  62. // we really don't need anything
  63. DropPrivileges();
  64. CommandLine CmdL;
  65. ParseCommandLine(CmdL, APT_CMD::APT_INTERNAL_SOLVER, &_config, NULL, argc, argv, &ShowHelp, &GetCommands);
  66. if (CmdL.FileList[0] != 0 && strcmp(CmdL.FileList[0], "scenario") == 0)
  67. {
  68. if (pkgInitSystem(*_config,_system) == false) {
  69. std::cerr << "System could not be initialized!" << std::endl;
  70. return 1;
  71. }
  72. pkgCacheFile CacheFile;
  73. CacheFile.Open(NULL, false);
  74. APT::PackageSet pkgset = APT::PackageSet::FromCommandLine(CacheFile, CmdL.FileList + 1);
  75. FILE* output = stdout;
  76. if (pkgset.empty() == true)
  77. EDSP::WriteScenario(CacheFile, output);
  78. else
  79. EDSP::WriteLimitedScenario(CacheFile, output, pkgset);
  80. fclose(output);
  81. _error->DumpErrors(std::cerr);
  82. return 0;
  83. }
  84. // Deal with stdout not being a tty
  85. if (!isatty(STDOUT_FILENO) && _config->FindI("quiet", -1) == -1)
  86. _config->Set("quiet","1");
  87. if (_config->FindI("quiet", 0) < 1)
  88. _config->Set("Debug::EDSP::WriteSolution", true);
  89. _config->Set("APT::System", "Debian APT solver interface");
  90. _config->Set("APT::Solver", "internal");
  91. _config->Set("edsp::scenario", "/nonexistent/stdin");
  92. int input = STDIN_FILENO;
  93. FILE* output = stdout;
  94. SetNonBlock(input, false);
  95. EDSP::WriteProgress(0, "Start up solver…", output);
  96. if (pkgInitSystem(*_config,_system) == false)
  97. DIE("System could not be initialized!");
  98. EDSP::WriteProgress(1, "Read request…", output);
  99. if (WaitFd(input, false, 5) == false)
  100. DIE("WAIT timed out in the resolver");
  101. std::list<std::string> install, remove;
  102. bool upgrade, distUpgrade, autoRemove;
  103. if (EDSP::ReadRequest(input, install, remove, upgrade, distUpgrade, autoRemove) == false)
  104. DIE("Parsing the request failed!");
  105. EDSP::WriteProgress(5, "Read scenario…", output);
  106. pkgCacheFile CacheFile;
  107. if (CacheFile.Open(NULL, false) == false)
  108. DIE("Failed to open CacheFile!");
  109. EDSP::WriteProgress(50, "Apply request on scenario…", output);
  110. if (EDSP::ApplyRequest(install, remove, CacheFile) == false)
  111. DIE("Failed to apply request to depcache!");
  112. pkgProblemResolver Fix(CacheFile);
  113. for (std::list<std::string>::const_iterator i = remove.begin();
  114. i != remove.end(); ++i) {
  115. pkgCache::PkgIterator P = CacheFile->FindPkg(*i);
  116. Fix.Clear(P);
  117. Fix.Protect(P);
  118. Fix.Remove(P);
  119. }
  120. for (std::list<std::string>::const_iterator i = install.begin();
  121. i != install.end(); ++i) {
  122. pkgCache::PkgIterator P = CacheFile->FindPkg(*i);
  123. Fix.Clear(P);
  124. Fix.Protect(P);
  125. }
  126. for (std::list<std::string>::const_iterator i = install.begin();
  127. i != install.end(); ++i)
  128. CacheFile->MarkInstall(CacheFile->FindPkg(*i), true);
  129. EDSP::WriteProgress(60, "Call problemresolver on current scenario…", output);
  130. std::string failure;
  131. if (upgrade == true) {
  132. if (APT::Upgrade::Upgrade(CacheFile, APT::Upgrade::FORBID_REMOVE_PACKAGES | APT::Upgrade::FORBID_INSTALL_NEW_PACKAGES) == false)
  133. failure = "ERR_UNSOLVABLE_UPGRADE";
  134. } else if (distUpgrade == true) {
  135. if (APT::Upgrade::Upgrade(CacheFile, APT::Upgrade::ALLOW_EVERYTHING) == false)
  136. failure = "ERR_UNSOLVABLE_DIST_UPGRADE";
  137. } else if (Fix.Resolve() == false)
  138. failure = "ERR_UNSOLVABLE";
  139. if (failure.empty() == false) {
  140. std::ostringstream broken;
  141. ShowBroken(broken, CacheFile, false);
  142. EDSP::WriteError(failure.c_str(), broken.str(), output);
  143. return 0;
  144. }
  145. EDSP::WriteProgress(95, "Write solution…", output);
  146. if (EDSP::WriteSolution(CacheFile, output) == false)
  147. DIE("Failed to output the solution!");
  148. EDSP::WriteProgress(100, "Done", output);
  149. return DispatchCommandLine(CmdL, {});
  150. }
  151. /*}}}*/