apt-internal-solver.cc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 <string.h>
  27. #include <iostream>
  28. #include <sstream>
  29. #include <list>
  30. #include <string>
  31. #include <unistd.h>
  32. #include <cstdio>
  33. #include <stdlib.h>
  34. #include <apti18n.h>
  35. /*}}}*/
  36. // ShowHelp - Show a help screen /*{{{*/
  37. // ---------------------------------------------------------------------
  38. /* */
  39. static bool ShowHelp(CommandLine &) {
  40. ioprintf(std::cout, "%s %s (%s)\n", PACKAGE, PACKAGE_VERSION, COMMON_ARCH);
  41. std::cout <<
  42. _("Usage: apt-internal-solver\n"
  43. "\n"
  44. "apt-internal-solver is an interface to use the current internal\n"
  45. "like an external resolver for the APT family for debugging or alike\n"
  46. "\n"
  47. "Options:\n"
  48. " -h This help text.\n"
  49. " -q Loggable output - no progress indicator\n"
  50. " -c=? Read this configuration file\n"
  51. " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n");
  52. return true;
  53. }
  54. /*}}}*/
  55. APT_NORETURN static void DIE(std::string const &message) { /*{{{*/
  56. std::cerr << "ERROR: " << message << std::endl;
  57. _error->DumpErrors(std::cerr);
  58. exit(EXIT_FAILURE);
  59. }
  60. /*}}}*/
  61. int main(int argc,const char *argv[]) /*{{{*/
  62. {
  63. CommandLine::Args Args[] = {
  64. {'h',"help","help",0},
  65. {'v',"version","version",0},
  66. {'q',"quiet","quiet",CommandLine::IntLevel},
  67. {'q',"silent","quiet",CommandLine::IntLevel},
  68. {'c',"config-file",0,CommandLine::ConfigFile},
  69. {'o',"option",0,CommandLine::ArbItem},
  70. {0,0,0,0}};
  71. // we really don't need anything
  72. DropPrivileges();
  73. CommandLine CmdL;
  74. ParseCommandLine(CmdL, NULL, Args, &_config, NULL, argc, argv, ShowHelp);
  75. if (CmdL.FileList[0] != 0 && strcmp(CmdL.FileList[0], "scenario") == 0)
  76. {
  77. if (pkgInitSystem(*_config,_system) == false) {
  78. std::cerr << "System could not be initialized!" << std::endl;
  79. return 1;
  80. }
  81. pkgCacheFile CacheFile;
  82. CacheFile.Open(NULL, false);
  83. APT::PackageSet pkgset = APT::PackageSet::FromCommandLine(CacheFile, CmdL.FileList + 1);
  84. FILE* output = stdout;
  85. if (pkgset.empty() == true)
  86. EDSP::WriteScenario(CacheFile, output);
  87. else
  88. EDSP::WriteLimitedScenario(CacheFile, output, pkgset);
  89. fclose(output);
  90. _error->DumpErrors(std::cerr);
  91. return 0;
  92. }
  93. // Deal with stdout not being a tty
  94. if (!isatty(STDOUT_FILENO) && _config->FindI("quiet", -1) == -1)
  95. _config->Set("quiet","1");
  96. if (_config->FindI("quiet", 0) < 1)
  97. _config->Set("Debug::EDSP::WriteSolution", true);
  98. _config->Set("APT::System", "Debian APT solver interface");
  99. _config->Set("APT::Solver", "internal");
  100. _config->Set("edsp::scenario", "/nonexistent/stdin");
  101. int input = STDIN_FILENO;
  102. FILE* output = stdout;
  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. bool upgrade, distUpgrade, autoRemove;
  112. if (EDSP::ReadRequest(input, install, remove, upgrade, distUpgrade, autoRemove) == 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 (upgrade == true) {
  141. if (APT::Upgrade::Upgrade(CacheFile, APT::Upgrade::FORBID_REMOVE_PACKAGES | APT::Upgrade::FORBID_INSTALL_NEW_PACKAGES) == false)
  142. failure = "ERR_UNSOLVABLE_UPGRADE";
  143. } else if (distUpgrade == true) {
  144. if (APT::Upgrade::Upgrade(CacheFile, APT::Upgrade::ALLOW_EVERYTHING) == false)
  145. failure = "ERR_UNSOLVABLE_DIST_UPGRADE";
  146. } else if (Fix.Resolve() == false)
  147. failure = "ERR_UNSOLVABLE";
  148. if (failure.empty() == false) {
  149. std::ostringstream broken;
  150. ShowBroken(broken, CacheFile, false);
  151. EDSP::WriteError(failure.c_str(), broken.str(), output);
  152. return 0;
  153. }
  154. EDSP::WriteProgress(95, "Write solution…", output);
  155. if (EDSP::WriteSolution(CacheFile, output) == false)
  156. DIE("Failed to output the solution!");
  157. EDSP::WriteProgress(100, "Done", output);
  158. bool const Errors = _error->PendingError();
  159. if (_config->FindI("quiet",0) > 0)
  160. _error->DumpErrors(std::cerr);
  161. else
  162. _error->DumpErrors(std::cerr, GlobalError::DEBUG);
  163. return Errors == true ? 100 : 0;
  164. }
  165. /*}}}*/