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