You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

191 lines
5.6 KiB

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