apt-internal-solver.cc 5.6 KB

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