apt-internal-solver.cc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. static bool WriteSolution(pkgDepCache &Cache, FileFd &output) /*{{{*/
  60. {
  61. bool Okay = output.Failed() == false;
  62. for (pkgCache::PkgIterator Pkg = Cache.PkgBegin(); Pkg.end() == false && likely(Okay); ++Pkg)
  63. {
  64. std::string action;
  65. if (Cache[Pkg].Delete() == true)
  66. Okay &= EDSP::WriteSolutionStanza(output, "Remove", Pkg.CurrentVer());
  67. else if (Cache[Pkg].NewInstall() == true || Cache[Pkg].Upgrade() == true)
  68. Okay &= EDSP::WriteSolutionStanza(output, "Install", Cache.GetCandidateVersion(Pkg));
  69. else if (Cache[Pkg].Garbage == true)
  70. Okay &= EDSP::WriteSolutionStanza(output, "Autoremove", Pkg.CurrentVer());
  71. }
  72. return Okay;
  73. }
  74. /*}}}*/
  75. int main(int argc,const char *argv[]) /*{{{*/
  76. {
  77. // we really don't need anything
  78. DropPrivileges();
  79. CommandLine CmdL;
  80. ParseCommandLine(CmdL, APT_CMD::APT_INTERNAL_SOLVER, &_config, NULL, argc, argv, &ShowHelp, &GetCommands);
  81. if (CmdL.FileList[0] != 0 && strcmp(CmdL.FileList[0], "scenario") == 0)
  82. {
  83. if (pkgInitSystem(*_config,_system) == false) {
  84. std::cerr << "System could not be initialized!" << std::endl;
  85. return 1;
  86. }
  87. pkgCacheFile CacheFile;
  88. CacheFile.Open(NULL, false);
  89. APT::PackageSet pkgset = APT::PackageSet::FromCommandLine(CacheFile, CmdL.FileList + 1);
  90. FileFd output;
  91. if (output.OpenDescriptor(STDOUT_FILENO, FileFd::WriteOnly | FileFd::BufferedWrite, true) == false)
  92. return 2;
  93. if (pkgset.empty() == true)
  94. EDSP::WriteScenario(CacheFile, output);
  95. else
  96. {
  97. std::vector<bool> pkgvec(CacheFile->Head().PackageCount, false);
  98. for (auto const &p: pkgset)
  99. pkgvec[p->ID] = true;
  100. EDSP::WriteLimitedScenario(CacheFile, output, pkgvec);
  101. }
  102. output.Close();
  103. _error->DumpErrors(std::cerr);
  104. return 0;
  105. }
  106. // Deal with stdout not being a tty
  107. if (!isatty(STDOUT_FILENO) && _config->FindI("quiet", -1) == -1)
  108. _config->Set("quiet","1");
  109. if (_config->FindI("quiet", 0) < 1)
  110. _config->Set("Debug::EDSP::WriteSolution", true);
  111. _config->Set("APT::System", "Debian APT solver interface");
  112. _config->Set("APT::Solver", "internal");
  113. _config->Set("edsp::scenario", "/nonexistent/stdin");
  114. _config->Clear("Dir::Log");
  115. FileFd output;
  116. if (output.OpenDescriptor(STDOUT_FILENO, FileFd::WriteOnly | FileFd::BufferedWrite, true) == false)
  117. DIE("stdout couldn't be opened");
  118. int const input = STDIN_FILENO;
  119. SetNonBlock(input, false);
  120. EDSP::WriteProgress(0, "Start up solver…", output);
  121. if (pkgInitSystem(*_config,_system) == false)
  122. DIE("System could not be initialized!");
  123. EDSP::WriteProgress(1, "Read request…", output);
  124. if (WaitFd(input, false, 5) == false)
  125. DIE("WAIT timed out in the resolver");
  126. std::list<std::string> install, remove;
  127. unsigned int flags;
  128. if (EDSP::ReadRequest(input, install, remove, flags) == false)
  129. DIE("Parsing the request failed!");
  130. EDSP::WriteProgress(5, "Read scenario…", output);
  131. pkgCacheFile CacheFile;
  132. if (CacheFile.Open(NULL, false) == false)
  133. DIE("Failed to open CacheFile!");
  134. EDSP::WriteProgress(50, "Apply request on scenario…", output);
  135. if (EDSP::ApplyRequest(install, remove, CacheFile) == false)
  136. DIE("Failed to apply request to depcache!");
  137. pkgProblemResolver Fix(CacheFile);
  138. for (std::list<std::string>::const_iterator i = remove.begin();
  139. i != remove.end(); ++i) {
  140. pkgCache::PkgIterator P = CacheFile->FindPkg(*i);
  141. Fix.Clear(P);
  142. Fix.Protect(P);
  143. Fix.Remove(P);
  144. }
  145. for (std::list<std::string>::const_iterator i = install.begin();
  146. i != install.end(); ++i) {
  147. pkgCache::PkgIterator P = CacheFile->FindPkg(*i);
  148. Fix.Clear(P);
  149. Fix.Protect(P);
  150. }
  151. for (std::list<std::string>::const_iterator i = install.begin();
  152. i != install.end(); ++i)
  153. CacheFile->MarkInstall(CacheFile->FindPkg(*i), true);
  154. EDSP::WriteProgress(60, "Call problemresolver on current scenario…", output);
  155. std::string failure;
  156. if (flags & EDSP::Request::UPGRADE_ALL) {
  157. int upgrade_flags = APT::Upgrade::ALLOW_EVERYTHING;
  158. if (flags & EDSP::Request::FORBID_NEW_INSTALL)
  159. upgrade_flags |= APT::Upgrade::FORBID_INSTALL_NEW_PACKAGES;
  160. if (flags & EDSP::Request::FORBID_REMOVE)
  161. upgrade_flags |= APT::Upgrade::FORBID_REMOVE_PACKAGES;
  162. if (APT::Upgrade::Upgrade(CacheFile, upgrade_flags))
  163. ;
  164. else if (upgrade_flags == APT::Upgrade::ALLOW_EVERYTHING)
  165. failure = "ERR_UNSOLVABLE_FULL_UPGRADE";
  166. else
  167. failure = "ERR_UNSOLVABLE_UPGRADE";
  168. } else if (Fix.Resolve() == false)
  169. failure = "ERR_UNSOLVABLE";
  170. if (failure.empty() == false) {
  171. std::ostringstream broken;
  172. ShowBroken(broken, CacheFile, false);
  173. EDSP::WriteError(failure.c_str(), broken.str(), output);
  174. return 0;
  175. }
  176. EDSP::WriteProgress(95, "Write solution…", output);
  177. if (WriteSolution(CacheFile, output) == false)
  178. DIE("Failed to output the solution!");
  179. EDSP::WriteProgress(100, "Done", output);
  180. return DispatchCommandLine(CmdL, {});
  181. }
  182. /*}}}*/