apt-internal-solver.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. {0,0,0,0}};
  51. CommandLine CmdL(Args,_config);
  52. if (pkgInitConfig(*_config) == false ||
  53. CmdL.Parse(argc,argv) == false) {
  54. _error->DumpErrors();
  55. return 2;
  56. }
  57. // See if the help should be shown
  58. if (_config->FindB("help") == true ||
  59. _config->FindB("version") == true) {
  60. ShowHelp(CmdL);
  61. return 1;
  62. }
  63. // Deal with stdout not being a tty
  64. if (!isatty(STDOUT_FILENO) && _config->FindI("quiet", -1) == -1)
  65. _config->Set("quiet","1");
  66. if (_config->FindI("quiet", 0) < 1)
  67. _config->Set("Debug::EDSP::WriteSolution", true);
  68. _config->Set("APT::Solver::Name", "internal");
  69. _config->Set("edsp::scenario", "stdin");
  70. int input = STDIN_FILENO;
  71. FILE* output = stdout;
  72. SetNonBlock(input, false);
  73. if (pkgInitSystem(*_config,_system) == false) {
  74. std::cerr << "System could not be initialized!" << std::endl;
  75. return 1;
  76. }
  77. if (WaitFd(input, false, 5) == false)
  78. std::cerr << "WAIT timed out in the resolver" << std::endl;
  79. std::list<std::string> install, remove;
  80. bool upgrade, distUpgrade, autoRemove;
  81. if (EDSP::ReadRequest(input, install, remove, upgrade, distUpgrade, autoRemove) == false) {
  82. std::cerr << "Parsing the request failed!" << std::endl;
  83. return 2;
  84. }
  85. pkgCacheFile CacheFile;
  86. CacheFile.Open(NULL, false);
  87. if (EDSP::ApplyRequest(install, remove, CacheFile) == false) {
  88. std::cerr << "Failed to apply request to depcache!" << std::endl;
  89. return 3;
  90. }
  91. for (std::list<std::string>::const_iterator i = install.begin();
  92. i != install.end(); ++i)
  93. CacheFile->MarkInstall(CacheFile->FindPkg(*i), true);
  94. pkgProblemResolver Fix(CacheFile);
  95. if (Fix.Resolve() == false) {
  96. EDSP::WriteError("An error occured", output);
  97. return 0;
  98. }
  99. if (EDSP::WriteSolution(CacheFile, output) == false) {
  100. std::cerr << "Failed to output the solution!" << std::endl;
  101. return 4;
  102. }
  103. bool const Errors = _error->PendingError();
  104. if (_config->FindI("quiet",0) > 0)
  105. _error->DumpErrors();
  106. else
  107. _error->DumpErrors(GlobalError::DEBUG);
  108. return Errors == true ? 100 : 0;
  109. }
  110. /*}}}*/