edsp.cc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. /* ######################################################################
  4. Set of methods to help writing and reading everything needed for EDSP
  5. ##################################################################### */
  6. /*}}}*/
  7. // Include Files /*{{{*/
  8. #include <apt-pkg/edsp.h>
  9. #include <apt-pkg/error.h>
  10. #include <apt-pkg/configuration.h>
  11. #include <apt-pkg/version.h>
  12. #include <apt-pkg/policy.h>
  13. #include <apti18n.h>
  14. #include <limits>
  15. #include <stdio.h>
  16. /*}}}*/
  17. // EDSP::WriteScenario - to the given file descriptor /*{{{*/
  18. bool EDSP::WriteScenario(pkgDepCache &Cache, FILE* output)
  19. {
  20. // we could use pkgCache::DepType and ::Priority, but these would be lokalized strings…
  21. const char * const PrioMap[] = {0, "important", "required", "standard",
  22. "optional", "extra"};
  23. const char * const DepMap[] = {"", "Depends", "PreDepends", "Suggests",
  24. "Recommends" , "Conflicts", "Replaces",
  25. "Obsoletes", "Breaks", "Enhances"};
  26. for (pkgCache::PkgIterator Pkg = Cache.PkgBegin(); Pkg.end() == false; ++Pkg)
  27. {
  28. for (pkgCache::VerIterator Ver = Pkg.VersionList(); Ver.end() == false; ++Ver)
  29. {
  30. fprintf(output, "Package: %s\n", Pkg.Name());
  31. fprintf(output, "Architecture: %s\n", Ver.Arch());
  32. fprintf(output, "Version: %s\n", Ver.VerStr());
  33. if (Pkg.CurrentVer() == Ver)
  34. fprintf(output, "Installed: yes\n");
  35. if (Pkg->SelectedState == pkgCache::State::Hold)
  36. fprintf(output, "Hold: yes\n");
  37. fprintf(output, "APT-ID: %u\n", Ver->ID);
  38. fprintf(output, "Priority: %s\n", PrioMap[Ver->Priority]);
  39. if ((Pkg->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential)
  40. fprintf(output, "Essential: yes\n");
  41. fprintf(output, "Section: %s\n", Ver.Section());
  42. if (Ver->MultiArch == pkgCache::Version::Allowed || Ver->MultiArch == pkgCache::Version::AllAllowed)
  43. fprintf(output, "Multi-Arch: allowed\n");
  44. else if (Ver->MultiArch == pkgCache::Version::Foreign || Ver->MultiArch == pkgCache::Version::AllForeign)
  45. fprintf(output, "Multi-Arch: foreign\n");
  46. else if (Ver->MultiArch == pkgCache::Version::Same)
  47. fprintf(output, "Multi-Arch: same\n");
  48. signed short Pin = std::numeric_limits<signed short>::min();
  49. for (pkgCache::VerFileIterator File = Ver.FileList(); File.end() == false; ++File) {
  50. signed short const p = Cache.GetPolicy().GetPriority(File.File());
  51. if (Pin < p)
  52. Pin = p;
  53. }
  54. fprintf(output, "APT-Pin: %d\n", Pin);
  55. if (Cache.GetCandidateVer(Pkg) == Ver)
  56. fprintf(output, "APT-Candidate: yes\n");
  57. if ((Cache[Pkg].Flags & pkgCache::Flag::Auto) == pkgCache::Flag::Auto)
  58. fprintf(output, "APT-Automatic: yes\n");
  59. std::string dependencies[pkgCache::Dep::Enhances + 1];
  60. bool orGroup = false;
  61. for (pkgCache::DepIterator Dep = Ver.DependsList(); Dep.end() == false; ++Dep)
  62. {
  63. // Ignore implicit dependencies for multiarch here
  64. if (strcmp(Pkg.Arch(), Dep.TargetPkg().Arch()) != 0)
  65. continue;
  66. if (orGroup == false)
  67. dependencies[Dep->Type].append(", ");
  68. dependencies[Dep->Type].append(Dep.TargetPkg().Name());
  69. if (Dep->Version != 0)
  70. dependencies[Dep->Type].append(" (").append(pkgCache::CompTypeDeb(Dep->CompareOp)).append(" ").append(Dep.TargetVer()).append(")");
  71. if ((Dep->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or)
  72. {
  73. dependencies[Dep->Type].append(" | ");
  74. orGroup = true;
  75. }
  76. else
  77. orGroup = false;
  78. }
  79. for (int i = 1; i < pkgCache::Dep::Enhances + 1; ++i)
  80. if (dependencies[i].empty() == false)
  81. fprintf(output, "%s: %s\n", DepMap[i], dependencies[i].c_str()+2);
  82. string provides;
  83. for (pkgCache::PrvIterator Prv = Ver.ProvidesList(); Prv.end() == false; ++Prv)
  84. {
  85. // Ignore implicit provides for multiarch here
  86. if (strcmp(Pkg.Arch(), Prv.ParentPkg().Arch()) != 0 || strcmp(Pkg.Name(),Prv.Name()) == 0)
  87. continue;
  88. provides.append(", ").append(Prv.Name());
  89. }
  90. if (provides.empty() == false)
  91. fprintf(output, "Provides: %s\n", provides.c_str()+2);
  92. fprintf(output, "\n");
  93. }
  94. }
  95. return true;
  96. }
  97. /*}}}*/
  98. // EDSP::WriteRequest - to the given file descriptor /*{{{*/
  99. bool EDSP::WriteRequest(pkgDepCache &Cache, FILE* output, bool const Upgrade,
  100. bool const DistUpgrade, bool const AutoRemove)
  101. {
  102. string del, inst;
  103. for (pkgCache::PkgIterator Pkg = Cache.PkgBegin(); Pkg.end() == false; ++Pkg)
  104. {
  105. string* req;
  106. if (Cache[Pkg].Delete() == true)
  107. req = &del;
  108. else if (Cache[Pkg].NewInstall() == true || Cache[Pkg].Upgrade() == true)
  109. req = &inst;
  110. else
  111. continue;
  112. req->append(", ").append(Pkg.FullName());
  113. }
  114. fprintf(output, "Request: EDSP 0.2\n");
  115. if (del.empty() == false)
  116. fprintf(output, "Remove: %s\n", del.c_str()+2);
  117. if (inst.empty() == false)
  118. fprintf(output, "Install: %s\n", inst.c_str()+2);
  119. if (Upgrade == true)
  120. fprintf(output, "Upgrade: yes\n");
  121. if (DistUpgrade == true)
  122. fprintf(output, "Dist-Upgrade: yes\n");
  123. if (AutoRemove == true)
  124. fprintf(output, "Autoremove: yes\n");
  125. if (_config->FindB("APT::Solver::Strict-Pinning", true) == false)
  126. fprintf(output, "Strict-Pinning: no\n");
  127. string solverpref("APT::Solver::");
  128. solverpref.append(_config->Find("APT::Solver::Name", "internal")).append("::Preferences");
  129. if (_config->Exists(solverpref) == false)
  130. fprintf(output, "Preferences: %s\n", _config->Find(solverpref,"").c_str());
  131. return true;
  132. }
  133. /*}}}*/
  134. bool EDSP::ReadResponse(FILE* input, pkgDepCache &Cache) { return false; }
  135. bool EDSP::ReadRequest(FILE* input, std::list<std::string> &install,
  136. std::list<std::string> &remove)
  137. { return false; }
  138. bool EDSP::ApplyRequest(std::list<std::string> const &install,
  139. std::list<std::string> const &remove,
  140. pkgDepCache &Cache)
  141. { return false; }
  142. // EDSP::WriteSolution - to the given file descriptor /*{{{*/
  143. bool EDSP::WriteSolution(pkgDepCache &Cache, FILE* output)
  144. {
  145. bool const Debug = _config->FindB("Debug::EDSPWriter::WriteSolution", false);
  146. for (pkgCache::PkgIterator Pkg = Cache.PkgBegin(); Pkg.end() == false; ++Pkg)
  147. {
  148. if (Cache[Pkg].Delete() == true)
  149. fprintf(output, "Remove: %d\n", Cache.GetCandidateVer(Pkg)->ID);
  150. else if (Cache[Pkg].NewInstall() == true || Cache[Pkg].Upgrade() == true)
  151. fprintf(output, "Install: %d\n", Cache.GetCandidateVer(Pkg)->ID);
  152. else
  153. continue;
  154. if (Debug == true)
  155. fprintf(output, "Package: %s\nVersion: %s\n", Pkg.FullName().c_str(), Cache.GetCandidateVer(Pkg).VerStr());
  156. fprintf(output, "\n");
  157. }
  158. return true;
  159. }
  160. /*}}}*/
  161. bool EDSP::WriteError(std::string const &message, FILE* output) { return false; }