apt-internal-solver.cc 5.5 KB

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