apt-internal-solver.cc 5.8 KB

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