apt-internal-solver.cc 5.4 KB

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