apt-internal-solver.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. 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. "like an external resolver for the APT family for debugging or alike.\n");
  44. return true;
  45. }
  46. /*}}}*/
  47. APT_NORETURN static void DIE(std::string const &message) { /*{{{*/
  48. std::cerr << "ERROR: " << message << std::endl;
  49. _error->DumpErrors(std::cerr);
  50. exit(EXIT_FAILURE);
  51. }
  52. /*}}}*/
  53. std::vector<aptDispatchWithHelp> GetCommands() /*{{{*/
  54. {
  55. return {};
  56. }
  57. /*}}}*/
  58. int main(int argc,const char *argv[]) /*{{{*/
  59. {
  60. InitLocale();
  61. // we really don't need anything
  62. DropPrivileges();
  63. CommandLine CmdL;
  64. ParseCommandLine(CmdL, APT_CMD::APT_INTERNAL_SOLVER, &_config, NULL, argc, argv);
  65. if (CmdL.FileList[0] != 0 && strcmp(CmdL.FileList[0], "scenario") == 0)
  66. {
  67. if (pkgInitSystem(*_config,_system) == false) {
  68. std::cerr << "System could not be initialized!" << std::endl;
  69. return 1;
  70. }
  71. pkgCacheFile CacheFile;
  72. CacheFile.Open(NULL, false);
  73. APT::PackageSet pkgset = APT::PackageSet::FromCommandLine(CacheFile, CmdL.FileList + 1);
  74. FILE* output = stdout;
  75. if (pkgset.empty() == true)
  76. EDSP::WriteScenario(CacheFile, output);
  77. else
  78. EDSP::WriteLimitedScenario(CacheFile, output, pkgset);
  79. fclose(output);
  80. _error->DumpErrors(std::cerr);
  81. return 0;
  82. }
  83. // Deal with stdout not being a tty
  84. if (!isatty(STDOUT_FILENO) && _config->FindI("quiet", -1) == -1)
  85. _config->Set("quiet","1");
  86. if (_config->FindI("quiet", 0) < 1)
  87. _config->Set("Debug::EDSP::WriteSolution", true);
  88. _config->Set("APT::System", "Debian APT solver interface");
  89. _config->Set("APT::Solver", "internal");
  90. _config->Set("edsp::scenario", "/nonexistent/stdin");
  91. int input = STDIN_FILENO;
  92. FILE* output = stdout;
  93. SetNonBlock(input, false);
  94. EDSP::WriteProgress(0, "Start up solver…", output);
  95. if (pkgInitSystem(*_config,_system) == false)
  96. DIE("System could not be initialized!");
  97. EDSP::WriteProgress(1, "Read request…", output);
  98. if (WaitFd(input, false, 5) == false)
  99. DIE("WAIT timed out in the resolver");
  100. std::list<std::string> install, remove;
  101. bool upgrade, distUpgrade, autoRemove;
  102. if (EDSP::ReadRequest(input, install, remove, upgrade, distUpgrade, autoRemove) == false)
  103. DIE("Parsing the request failed!");
  104. EDSP::WriteProgress(5, "Read scenario…", output);
  105. pkgCacheFile CacheFile;
  106. if (CacheFile.Open(NULL, false) == false)
  107. DIE("Failed to open CacheFile!");
  108. EDSP::WriteProgress(50, "Apply request on scenario…", output);
  109. if (EDSP::ApplyRequest(install, remove, CacheFile) == false)
  110. DIE("Failed to apply request to depcache!");
  111. pkgProblemResolver Fix(CacheFile);
  112. for (std::list<std::string>::const_iterator i = remove.begin();
  113. i != remove.end(); ++i) {
  114. pkgCache::PkgIterator P = CacheFile->FindPkg(*i);
  115. Fix.Clear(P);
  116. Fix.Protect(P);
  117. Fix.Remove(P);
  118. }
  119. for (std::list<std::string>::const_iterator i = install.begin();
  120. i != install.end(); ++i) {
  121. pkgCache::PkgIterator P = CacheFile->FindPkg(*i);
  122. Fix.Clear(P);
  123. Fix.Protect(P);
  124. }
  125. for (std::list<std::string>::const_iterator i = install.begin();
  126. i != install.end(); ++i)
  127. CacheFile->MarkInstall(CacheFile->FindPkg(*i), true);
  128. EDSP::WriteProgress(60, "Call problemresolver on current scenario…", output);
  129. std::string failure;
  130. if (upgrade == true) {
  131. if (APT::Upgrade::Upgrade(CacheFile, APT::Upgrade::FORBID_REMOVE_PACKAGES | APT::Upgrade::FORBID_INSTALL_NEW_PACKAGES) == false)
  132. failure = "ERR_UNSOLVABLE_UPGRADE";
  133. } else if (distUpgrade == true) {
  134. if (APT::Upgrade::Upgrade(CacheFile, APT::Upgrade::ALLOW_EVERYTHING) == false)
  135. failure = "ERR_UNSOLVABLE_DIST_UPGRADE";
  136. } else if (Fix.Resolve() == false)
  137. failure = "ERR_UNSOLVABLE";
  138. if (failure.empty() == false) {
  139. std::ostringstream broken;
  140. ShowBroken(broken, CacheFile, false);
  141. EDSP::WriteError(failure.c_str(), broken.str(), output);
  142. return 0;
  143. }
  144. EDSP::WriteProgress(95, "Write solution…", output);
  145. if (EDSP::WriteSolution(CacheFile, output) == false)
  146. DIE("Failed to output the solution!");
  147. EDSP::WriteProgress(100, "Done", output);
  148. return DispatchCommandLine(CmdL, {});
  149. }
  150. /*}}}*/