apt-internal-planner.cc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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/fileutl.h>
  17. #include <apt-pkg/pkgsystem.h>
  18. #include <apt-pkg/configuration.h>
  19. #include <apt-pkg/packagemanager.h>
  20. #include <apt-pkg/prettyprinters.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-planner\n"
  41. "\n"
  42. "apt-internal-planner is an interface to use the current internal\n"
  43. "installation planner for the APT family like an external one,\n"
  44. "for debugging or 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. class PMOutput: public pkgPackageManager /*{{{*/
  60. {
  61. FileFd &output;
  62. bool const Debug;
  63. protected:
  64. virtual bool Install(PkgIterator Pkg,std::string) APT_OVERRIDE
  65. {
  66. //std::cerr << "INSTALL: " << APT::PrettyPkg(&Cache, Pkg) << std::endl;
  67. return EDSP::WriteSolutionStanza(output, "Unpack", Cache[Pkg].InstVerIter(Cache));
  68. }
  69. virtual bool Configure(PkgIterator Pkg) APT_OVERRIDE
  70. {
  71. //std::cerr << "CONFIGURE: " << APT::PrettyPkg(&Cache, Pkg) << " " << std::endl;
  72. return EDSP::WriteSolutionStanza(output, "Configure", Cache[Pkg].InstVerIter(Cache));
  73. }
  74. virtual bool Remove(PkgIterator Pkg,bool) APT_OVERRIDE
  75. {
  76. //std::cerr << "REMOVE: " << APT::PrettyPkg(&Cache, Pkg) << " " << std::endl;
  77. return EDSP::WriteSolutionStanza(output, "Remove", Pkg.CurrentVer());
  78. }
  79. public:
  80. PMOutput(pkgDepCache *Cache, FileFd &file) : pkgPackageManager(Cache), output(file),
  81. Debug(_config->FindB("Debug::EDSP::WriteSolution", false))
  82. {}
  83. bool ApplyRequest(std::list<std::pair<std::string,EIPP::PKG_ACTION>> const &actions)
  84. {
  85. for (auto && a: actions)
  86. {
  87. auto const Pkg = Cache.FindPkg(a.first);
  88. if (unlikely(Pkg.end() == true))
  89. continue;
  90. switch (a.second)
  91. {
  92. case EIPP::PKG_ACTION::NOOP:
  93. break;
  94. case EIPP::PKG_ACTION::INSTALL:
  95. case EIPP::PKG_ACTION::REINSTALL:
  96. FileNames[Pkg->ID] = "EIPP";
  97. break;
  98. case EIPP::PKG_ACTION::REMOVE:
  99. break;
  100. }
  101. }
  102. return true;
  103. }
  104. };
  105. /*}}}*/
  106. int main(int argc,const char *argv[]) /*{{{*/
  107. {
  108. // we really don't need anything
  109. DropPrivileges();
  110. CommandLine CmdL;
  111. ParseCommandLine(CmdL, APT_CMD::APT_INTERNAL_PLANNER, &_config, NULL, argc, argv, &ShowHelp, &GetCommands);
  112. // Deal with stdout not being a tty
  113. if (!isatty(STDOUT_FILENO) && _config->FindI("quiet", -1) == -1)
  114. _config->Set("quiet","1");
  115. if (_config->FindI("quiet", 0) < 1)
  116. _config->Set("Debug::EIPP::WriteSolution", true);
  117. _config->Set("APT::System", "Debian APT planner interface");
  118. _config->Set("APT::Planner", "internal");
  119. _config->Set("eipp::scenario", "/nonexistent/stdin");
  120. FileFd output;
  121. if (output.OpenDescriptor(STDOUT_FILENO, FileFd::WriteOnly | FileFd::BufferedWrite, true) == false)
  122. DIE("stdout couldn't be opened");
  123. int const input = STDIN_FILENO;
  124. SetNonBlock(input, false);
  125. EDSP::WriteProgress(0, "Start up planner…", output);
  126. if (pkgInitSystem(*_config,_system) == false)
  127. DIE("System could not be initialized!");
  128. EDSP::WriteProgress(1, "Read request…", output);
  129. if (WaitFd(input, false, 5) == false)
  130. DIE("WAIT timed out in the planner");
  131. std::list<std::pair<std::string,EIPP::PKG_ACTION>> actions;
  132. unsigned int flags;
  133. if (EIPP::ReadRequest(input, actions, flags) == false)
  134. DIE("Parsing the request failed!");
  135. _config->Set("APT::Immediate-Configure", (flags & EIPP::Request::NO_IMMEDIATE_CONFIGURATION) == 0);
  136. _config->Set("APT::Immediate-Configure-All", (flags & EIPP::Request::IMMEDIATE_CONFIGURATION_ALL) != 0);
  137. _config->Set("APT::Force-LoopBreak", (flags & EIPP::Request::ALLOW_TEMPORARY_REMOVE_OF_ESSENTIALS) != 0);
  138. EDSP::WriteProgress(5, "Read scenario…", output);
  139. pkgCacheFile CacheFile;
  140. if (CacheFile.Open(NULL, false) == false)
  141. DIE("Failed to open CacheFile!");
  142. EDSP::WriteProgress(50, "Apply request on scenario…", output);
  143. if (EIPP::ApplyRequest(actions, CacheFile) == false)
  144. DIE("Failed to apply request to depcache!");
  145. EDSP::WriteProgress(60, "Call orderinstall on current scenario…", output);
  146. //_config->Set("Debug::pkgOrderList", true);
  147. //_config->Set("Debug::pkgPackageManager", true);
  148. PMOutput PM(CacheFile, output);
  149. if (PM.ApplyRequest(actions) == false)
  150. DIE("Failed to apply request to packagemanager!");
  151. pkgPackageManager::OrderResult const Res = PM.DoInstallPreFork();
  152. std::ostringstream broken;
  153. switch (Res)
  154. {
  155. case pkgPackageManager::Completed:
  156. EDSP::WriteProgress(100, "Done", output);
  157. break;
  158. case pkgPackageManager::Incomplete:
  159. broken << "Planner could only incompletely plan an installation order!" << std::endl;
  160. _error->DumpErrors(broken, GlobalError::DEBUG);
  161. EDSP::WriteError("pm-incomplete", broken.str(), output);
  162. break;
  163. case pkgPackageManager::Failed:
  164. broken << "Planner failed to find an installation order!" << std::endl;
  165. _error->DumpErrors(broken, GlobalError::DEBUG);
  166. EDSP::WriteError("pm-failed", broken.str(), output);
  167. break;
  168. }
  169. return DispatchCommandLine(CmdL, {});
  170. }
  171. /*}}}*/