apt-internal-solver.cc 5.5 KB

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