apt-internal-solver.cc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. FileFd output;
  76. if (output.OpenDescriptor(STDOUT_FILENO, FileFd::WriteOnly | FileFd::BufferedWrite, true) == false)
  77. return 2;
  78. if (pkgset.empty() == true)
  79. EDSP::WriteScenario(CacheFile, output);
  80. else
  81. EDSP::WriteLimitedScenario(CacheFile, output, pkgset);
  82. output.Close();
  83. _error->DumpErrors(std::cerr);
  84. return 0;
  85. }
  86. // Deal with stdout not being a tty
  87. if (!isatty(STDOUT_FILENO) && _config->FindI("quiet", -1) == -1)
  88. _config->Set("quiet","1");
  89. if (_config->FindI("quiet", 0) < 1)
  90. _config->Set("Debug::EDSP::WriteSolution", true);
  91. _config->Set("APT::System", "Debian APT solver interface");
  92. _config->Set("APT::Solver", "internal");
  93. _config->Set("edsp::scenario", "/nonexistent/stdin");
  94. FileFd output;
  95. if (output.OpenDescriptor(STDOUT_FILENO, FileFd::WriteOnly | FileFd::BufferedWrite, true) == false)
  96. DIE("stdout couldn't be opened");
  97. int const input = STDIN_FILENO;
  98. SetNonBlock(input, false);
  99. EDSP::WriteProgress(0, "Start up solver…", output);
  100. if (pkgInitSystem(*_config,_system) == false)
  101. DIE("System could not be initialized!");
  102. EDSP::WriteProgress(1, "Read request…", output);
  103. if (WaitFd(input, false, 5) == false)
  104. DIE("WAIT timed out in the resolver");
  105. std::list<std::string> install, remove;
  106. unsigned int flags;
  107. if (EDSP::ReadRequest(input, install, remove, flags) == false)
  108. DIE("Parsing the request failed!");
  109. EDSP::WriteProgress(5, "Read scenario…", output);
  110. pkgCacheFile CacheFile;
  111. if (CacheFile.Open(NULL, false) == false)
  112. DIE("Failed to open CacheFile!");
  113. EDSP::WriteProgress(50, "Apply request on scenario…", output);
  114. if (EDSP::ApplyRequest(install, remove, CacheFile) == false)
  115. DIE("Failed to apply request to depcache!");
  116. pkgProblemResolver Fix(CacheFile);
  117. for (std::list<std::string>::const_iterator i = remove.begin();
  118. i != remove.end(); ++i) {
  119. pkgCache::PkgIterator P = CacheFile->FindPkg(*i);
  120. Fix.Clear(P);
  121. Fix.Protect(P);
  122. Fix.Remove(P);
  123. }
  124. for (std::list<std::string>::const_iterator i = install.begin();
  125. i != install.end(); ++i) {
  126. pkgCache::PkgIterator P = CacheFile->FindPkg(*i);
  127. Fix.Clear(P);
  128. Fix.Protect(P);
  129. }
  130. for (std::list<std::string>::const_iterator i = install.begin();
  131. i != install.end(); ++i)
  132. CacheFile->MarkInstall(CacheFile->FindPkg(*i), true);
  133. EDSP::WriteProgress(60, "Call problemresolver on current scenario…", output);
  134. std::string failure;
  135. if (flags & EDSP::Request::UPGRADE_ALL) {
  136. int upgrade_flags = APT::Upgrade::ALLOW_EVERYTHING;
  137. if (flags & EDSP::Request::FORBID_NEW_INSTALL)
  138. upgrade_flags |= APT::Upgrade::FORBID_INSTALL_NEW_PACKAGES;
  139. if (flags & EDSP::Request::FORBID_REMOVE)
  140. upgrade_flags |= APT::Upgrade::FORBID_REMOVE_PACKAGES;
  141. if (APT::Upgrade::Upgrade(CacheFile, upgrade_flags))
  142. ;
  143. else if (upgrade_flags == APT::Upgrade::ALLOW_EVERYTHING)
  144. failure = "ERR_UNSOLVABLE_FULL_UPGRADE";
  145. else
  146. failure = "ERR_UNSOLVABLE_UPGRADE";
  147. } else if (Fix.Resolve() == false)
  148. failure = "ERR_UNSOLVABLE";
  149. if (failure.empty() == false) {
  150. std::ostringstream broken;
  151. ShowBroken(broken, CacheFile, false);
  152. EDSP::WriteError(failure.c_str(), broken.str(), output);
  153. return 0;
  154. }
  155. EDSP::WriteProgress(95, "Write solution…", output);
  156. if (EDSP::WriteSolution(CacheFile, output) == false)
  157. DIE("Failed to output the solution!");
  158. EDSP::WriteProgress(100, "Done", output);
  159. return DispatchCommandLine(CmdL, {});
  160. }
  161. /*}}}*/