apt-internal-solver.cc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 <string.h>
  26. #include <iostream>
  27. #include <sstream>
  28. #include <list>
  29. #include <string>
  30. #include <unistd.h>
  31. #include <cstdio>
  32. #include <stdlib.h>
  33. #include <apti18n.h>
  34. /*}}}*/
  35. // ShowHelp - Show a help screen /*{{{*/
  36. // ---------------------------------------------------------------------
  37. /* */
  38. static bool ShowHelp(CommandLine &) {
  39. ioprintf(std::cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,PACKAGE_VERSION,
  40. COMMON_ARCH,__DATE__,__TIME__);
  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. DropPrivs();
  73. CommandLine CmdL(Args,_config);
  74. if (pkgInitConfig(*_config) == false ||
  75. CmdL.Parse(argc,argv) == false) {
  76. _error->DumpErrors();
  77. return 2;
  78. }
  79. // See if the help should be shown
  80. if (_config->FindB("help") == true ||
  81. _config->FindB("version") == true) {
  82. ShowHelp(CmdL);
  83. return 1;
  84. }
  85. if (CmdL.FileList[0] != 0 && strcmp(CmdL.FileList[0], "scenario") == 0)
  86. {
  87. if (pkgInitSystem(*_config,_system) == false) {
  88. std::cerr << "System could not be initialized!" << std::endl;
  89. return 1;
  90. }
  91. pkgCacheFile CacheFile;
  92. CacheFile.Open(NULL, false);
  93. APT::PackageSet pkgset = APT::PackageSet::FromCommandLine(CacheFile, CmdL.FileList + 1);
  94. FILE* output = stdout;
  95. if (pkgset.empty() == true)
  96. EDSP::WriteScenario(CacheFile, output);
  97. else
  98. EDSP::WriteLimitedScenario(CacheFile, output, pkgset);
  99. fclose(output);
  100. _error->DumpErrors(std::cerr);
  101. return 0;
  102. }
  103. // Deal with stdout not being a tty
  104. if (!isatty(STDOUT_FILENO) && _config->FindI("quiet", -1) == -1)
  105. _config->Set("quiet","1");
  106. if (_config->FindI("quiet", 0) < 1)
  107. _config->Set("Debug::EDSP::WriteSolution", true);
  108. _config->Set("APT::Solver", "internal");
  109. _config->Set("edsp::scenario", "stdin");
  110. int input = STDIN_FILENO;
  111. FILE* output = stdout;
  112. SetNonBlock(input, false);
  113. EDSP::WriteProgress(0, "Start up solver…", output);
  114. if (pkgInitSystem(*_config,_system) == false)
  115. DIE("System could not be initialized!");
  116. EDSP::WriteProgress(1, "Read request…", output);
  117. if (WaitFd(input, false, 5) == false)
  118. DIE("WAIT timed out in the resolver");
  119. std::list<std::string> install, remove;
  120. bool upgrade, distUpgrade, autoRemove;
  121. if (EDSP::ReadRequest(input, install, remove, upgrade, distUpgrade, autoRemove) == false)
  122. DIE("Parsing the request failed!");
  123. EDSP::WriteProgress(5, "Read scenario…", output);
  124. pkgCacheFile CacheFile;
  125. if (CacheFile.Open(NULL, false) == false)
  126. DIE("Failed to open CacheFile!");
  127. EDSP::WriteProgress(50, "Apply request on scenario…", output);
  128. if (EDSP::ApplyRequest(install, remove, CacheFile) == false)
  129. DIE("Failed to apply request to depcache!");
  130. pkgProblemResolver Fix(CacheFile);
  131. for (std::list<std::string>::const_iterator i = remove.begin();
  132. i != remove.end(); ++i) {
  133. pkgCache::PkgIterator P = CacheFile->FindPkg(*i);
  134. Fix.Clear(P);
  135. Fix.Protect(P);
  136. Fix.Remove(P);
  137. }
  138. for (std::list<std::string>::const_iterator i = install.begin();
  139. i != install.end(); ++i) {
  140. pkgCache::PkgIterator P = CacheFile->FindPkg(*i);
  141. Fix.Clear(P);
  142. Fix.Protect(P);
  143. }
  144. for (std::list<std::string>::const_iterator i = install.begin();
  145. i != install.end(); ++i)
  146. CacheFile->MarkInstall(CacheFile->FindPkg(*i), true);
  147. EDSP::WriteProgress(60, "Call problemresolver on current scenario…", output);
  148. std::string failure;
  149. if (upgrade == true) {
  150. if (APT::Upgrade::Upgrade(CacheFile, APT::Upgrade::FORBID_REMOVE_PACKAGES | APT::Upgrade::FORBID_INSTALL_NEW_PACKAGES) == false)
  151. failure = "ERR_UNSOLVABLE_UPGRADE";
  152. } else if (distUpgrade == true) {
  153. if (APT::Upgrade::Upgrade(CacheFile, APT::Upgrade::ALLOW_EVERYTHING) == false)
  154. failure = "ERR_UNSOLVABLE_DIST_UPGRADE";
  155. } else if (Fix.Resolve() == false)
  156. failure = "ERR_UNSOLVABLE";
  157. if (failure.empty() == false) {
  158. std::ostringstream broken;
  159. ShowBroken(broken, CacheFile, false);
  160. EDSP::WriteError(failure.c_str(), broken.str(), output);
  161. return 0;
  162. }
  163. EDSP::WriteProgress(95, "Write solution…", output);
  164. if (EDSP::WriteSolution(CacheFile, output) == false)
  165. DIE("Failed to output the solution!");
  166. EDSP::WriteProgress(100, "Done", output);
  167. bool const Errors = _error->PendingError();
  168. if (_config->FindI("quiet",0) > 0)
  169. _error->DumpErrors(std::cerr);
  170. else
  171. _error->DumpErrors(std::cerr, GlobalError::DEBUG);
  172. return Errors == true ? 100 : 0;
  173. }
  174. /*}}}*/