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