apt-internal-solver.cc 5.7 KB

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