private-depends.cc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Include Files /*{{{*/
  2. #include<config.h>
  3. #include <apt-pkg/algorithms.h>
  4. #include <apt-pkg/cachefile.h>
  5. #include <apt-pkg/cacheiterators.h>
  6. #include <apt-pkg/cacheset.h>
  7. #include <apt-pkg/configuration.h>
  8. #include <apt-pkg/cmndline.h>
  9. #include <apt-pkg/error.h>
  10. #include <apt-pkg/pkgcache.h>
  11. #include <apt-private/private-cacheset.h>
  12. #include <apt-private/private-depends.h>
  13. #include <iostream>
  14. #include <string>
  15. #include <vector>
  16. #include <stddef.h>
  17. #include <apti18n.h>
  18. /*}}}*/
  19. // ShowDepends - Helper for printing out a dependency tree /*{{{*/
  20. static bool ShowDepends(CommandLine &CmdL, bool const RevDepends)
  21. {
  22. pkgCacheFile CacheFile;
  23. pkgCache * const Cache = CacheFile.GetPkgCache();
  24. if (unlikely(Cache == nullptr || CacheFile.GetDepCache() == nullptr))
  25. return false;
  26. CacheSetHelperVirtuals helper(false);
  27. APT::VersionList verset = APT::VersionList::FromCommandLine(CacheFile, CmdL.FileList + 1, APT::CacheSetHelper::CANDIDATE, helper);
  28. if (verset.empty() == true && helper.virtualPkgs.empty() == true)
  29. return _error->Error(_("No packages found"));
  30. std::vector<bool> Shown(Cache->Head().PackageCount);
  31. bool const Recurse = _config->FindB("APT::Cache::RecurseDepends", false);
  32. bool const Installed = _config->FindB("APT::Cache::Installed", false);
  33. bool const Important = _config->FindB("APT::Cache::Important", false);
  34. bool const ShowDepType = _config->FindB("APT::Cache::ShowDependencyType", RevDepends == false);
  35. bool const ShowVersion = _config->FindB("APT::Cache::ShowVersion", false);
  36. bool const ShowPreDepends = _config->FindB("APT::Cache::ShowPre-Depends", true);
  37. bool const ShowDepends = _config->FindB("APT::Cache::ShowDepends", true);
  38. bool const ShowRecommends = _config->FindB("APT::Cache::ShowRecommends", Important == false);
  39. bool const ShowSuggests = _config->FindB("APT::Cache::ShowSuggests", Important == false);
  40. bool const ShowReplaces = _config->FindB("APT::Cache::ShowReplaces", Important == false);
  41. bool const ShowConflicts = _config->FindB("APT::Cache::ShowConflicts", Important == false);
  42. bool const ShowBreaks = _config->FindB("APT::Cache::ShowBreaks", Important == false);
  43. bool const ShowEnhances = _config->FindB("APT::Cache::ShowEnhances", Important == false);
  44. bool const ShowOnlyFirstOr = _config->FindB("APT::Cache::ShowOnlyFirstOr", false);
  45. bool const ShowImplicit = _config->FindB("APT::Cache::ShowImplicit", false);
  46. while (verset.empty() != true)
  47. {
  48. pkgCache::VerIterator Ver = *verset.begin();
  49. verset.erase(verset.begin());
  50. pkgCache::PkgIterator Pkg = Ver.ParentPkg();
  51. Shown[Pkg->ID] = true;
  52. std::cout << Pkg.FullName(true) << std::endl;
  53. if (RevDepends == true)
  54. std::cout << "Reverse Depends:" << std::endl;
  55. for (pkgCache::DepIterator D = RevDepends ? Pkg.RevDependsList() : Ver.DependsList();
  56. D.end() == false; ++D)
  57. {
  58. switch (D->Type) {
  59. case pkgCache::Dep::PreDepends: if (!ShowPreDepends) continue; break;
  60. case pkgCache::Dep::Depends: if (!ShowDepends) continue; break;
  61. case pkgCache::Dep::Recommends: if (!ShowRecommends) continue; break;
  62. case pkgCache::Dep::Suggests: if (!ShowSuggests) continue; break;
  63. case pkgCache::Dep::Replaces: if (!ShowReplaces) continue; break;
  64. case pkgCache::Dep::Conflicts: if (!ShowConflicts) continue; break;
  65. case pkgCache::Dep::DpkgBreaks: if (!ShowBreaks) continue; break;
  66. case pkgCache::Dep::Enhances: if (!ShowEnhances) continue; break;
  67. }
  68. if (ShowImplicit == false && D.IsImplicit())
  69. continue;
  70. pkgCache::PkgIterator Trg = RevDepends ? D.ParentPkg() : D.TargetPkg();
  71. if((Installed && Trg->CurrentVer != 0) || !Installed)
  72. {
  73. if ((D->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or && ShowOnlyFirstOr == false)
  74. std::cout << " |";
  75. else
  76. std::cout << " ";
  77. // Show the package
  78. if (ShowDepType == true)
  79. std::cout << D.DepType() << ": ";
  80. if (Trg->VersionList == 0)
  81. std::cout << "<" << Trg.FullName(true) << ">";
  82. else
  83. std::cout << Trg.FullName(true);
  84. if (ShowVersion == true && D->Version != 0)
  85. std::cout << " (" << pkgCache::CompTypeDeb(D->CompareOp) << ' ' << D.TargetVer() << ')';
  86. std::cout << std::endl;
  87. if (Recurse == true && Shown[Trg->ID] == false)
  88. {
  89. Shown[Trg->ID] = true;
  90. verset.insert(APT::VersionSet::FromPackage(CacheFile, Trg, APT::CacheSetHelper::CANDIDATE, helper));
  91. }
  92. }
  93. // Display all solutions
  94. std::unique_ptr<pkgCache::Version *[]> List(D.AllTargets());
  95. pkgPrioSortList(*Cache,List.get());
  96. for (pkgCache::Version **I = List.get(); *I != 0; I++)
  97. {
  98. pkgCache::VerIterator V(*Cache,*I);
  99. if (V != Cache->VerP + V.ParentPkg()->VersionList ||
  100. V->ParentPkg == D->Package)
  101. continue;
  102. std::cout << " " << V.ParentPkg().FullName(true) << std::endl;
  103. if (Recurse == true && Shown[V.ParentPkg()->ID] == false)
  104. {
  105. Shown[V.ParentPkg()->ID] = true;
  106. verset.insert(APT::VersionSet::FromPackage(CacheFile, V.ParentPkg(), APT::CacheSetHelper::CANDIDATE, helper));
  107. }
  108. }
  109. if (ShowOnlyFirstOr == true)
  110. while ((D->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or) ++D;
  111. }
  112. }
  113. for (APT::PackageSet::const_iterator Pkg = helper.virtualPkgs.begin();
  114. Pkg != helper.virtualPkgs.end(); ++Pkg)
  115. std::cout << '<' << Pkg.FullName(true) << '>' << std::endl;
  116. return true;
  117. }
  118. /*}}}*/
  119. // Depends - Print out a dependency tree /*{{{*/
  120. bool Depends(CommandLine &CmdL)
  121. {
  122. return ShowDepends(CmdL, false);
  123. }
  124. /*}}}*/
  125. // RDepends - Print out a reverse dependency tree /*{{{*/
  126. bool RDepends(CommandLine &CmdL)
  127. {
  128. return ShowDepends(CmdL, true);
  129. }
  130. /*}}}*/