apt-internal-solver.cc 5.8 KB

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