private-cacheset.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. #ifndef APT_PRIVATE_CACHESET_H
  2. #define APT_PRIVATE_CACHESET_H
  3. #include <apt-pkg/aptconfiguration.h>
  4. #include <apt-pkg/cachefile.h>
  5. #include <apt-pkg/cacheset.h>
  6. #include <apt-pkg/sptr.h>
  7. #include <apt-pkg/strutl.h>
  8. #include <apt-pkg/depcache.h>
  9. #include <apt-pkg/error.h>
  10. #include <apt-pkg/pkgcache.h>
  11. #include <apt-pkg/cacheiterators.h>
  12. #include <apt-pkg/macros.h>
  13. #include <algorithm>
  14. #include <vector>
  15. #include <string.h>
  16. #include <list>
  17. #include <ostream>
  18. #include <set>
  19. #include <string>
  20. #include <utility>
  21. #include "private-output.h"
  22. #include <apti18n.h>
  23. class OpProgress;
  24. struct VersionSortDescriptionLocality
  25. {
  26. bool operator () (const pkgCache::VerIterator &v_lhs,
  27. const pkgCache::VerIterator &v_rhs)
  28. {
  29. pkgCache::DescFile *A = v_lhs.TranslatedDescription().FileList();
  30. pkgCache::DescFile *B = v_rhs.TranslatedDescription().FileList();
  31. if (A == 0 && B == 0)
  32. return false;
  33. if (A == 0)
  34. return true;
  35. if (B == 0)
  36. return false;
  37. if (A->File == B->File)
  38. return A->Offset < B->Offset;
  39. return A->File < B->File;
  40. }
  41. };
  42. // sorted by locality which makes iterating much faster
  43. typedef APT::VersionContainer<
  44. std::set<pkgCache::VerIterator,
  45. VersionSortDescriptionLocality> > LocalitySortedVersionSet;
  46. class Matcher {
  47. public:
  48. virtual bool operator () (const pkgCache::PkgIterator &/*P*/) {
  49. return true;}
  50. };
  51. // FIXME: add default argument for OpProgress (or overloaded function)
  52. bool GetLocalitySortedVersionSet(pkgCacheFile &CacheFile,
  53. APT::VersionContainerInterface * const vci,
  54. Matcher &matcher,
  55. OpProgress * const progress);
  56. bool GetLocalitySortedVersionSet(pkgCacheFile &CacheFile,
  57. APT::VersionContainerInterface * const vci,
  58. OpProgress * const progress);
  59. // CacheSetHelper saving virtual packages /*{{{*/
  60. class CacheSetHelperVirtuals: public APT::CacheSetHelper {
  61. public:
  62. APT::PackageSet virtualPkgs;
  63. virtual pkgCache::VerIterator canNotGetVersion(enum CacheSetHelper::VerSelector const select, pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg) {
  64. if (select == NEWEST || select == CANDIDATE || select == ALL)
  65. virtualPkgs.insert(Pkg);
  66. return CacheSetHelper::canNotGetVersion(select, Cache, Pkg);
  67. }
  68. virtual void canNotFindVersion(enum CacheSetHelper::VerSelector const select, APT::VersionContainerInterface * vci, pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg) {
  69. if (select == NEWEST || select == CANDIDATE || select == ALL)
  70. virtualPkgs.insert(Pkg);
  71. return CacheSetHelper::canNotFindVersion(select, vci, Cache, Pkg);
  72. }
  73. CacheSetHelperVirtuals(bool const ShowErrors = true, GlobalError::MsgType const &ErrorType = GlobalError::NOTICE) : CacheSetHelper(ShowErrors, ErrorType) {}
  74. };
  75. /*}}}*/
  76. // CacheSetHelperAPTGet - responsible for message telling from the CacheSets/*{{{*/
  77. class CacheSetHelperAPTGet : public APT::CacheSetHelper {
  78. /** \brief stream message should be printed to */
  79. std::ostream &out;
  80. /** \brief were things like Task or RegEx used to select packages? */
  81. bool explicitlyNamed;
  82. APT::PackageSet virtualPkgs;
  83. public:
  84. std::list<std::pair<pkgCache::VerIterator, std::string> > selectedByRelease;
  85. CacheSetHelperAPTGet(std::ostream &out) : APT::CacheSetHelper(true), out(out) {
  86. explicitlyNamed = true;
  87. }
  88. virtual void showTaskSelection(pkgCache::PkgIterator const &Pkg, std::string const &pattern) {
  89. ioprintf(out, _("Note, selecting '%s' for task '%s'\n"),
  90. Pkg.FullName(true).c_str(), pattern.c_str());
  91. explicitlyNamed = false;
  92. }
  93. virtual void showFnmatchSelection(pkgCache::PkgIterator const &Pkg, std::string const &pattern) {
  94. ioprintf(out, _("Note, selecting '%s' for glob '%s'\n"),
  95. Pkg.FullName(true).c_str(), pattern.c_str());
  96. explicitlyNamed = false;
  97. }
  98. virtual void showRegExSelection(pkgCache::PkgIterator const &Pkg, std::string const &pattern) {
  99. ioprintf(out, _("Note, selecting '%s' for regex '%s'\n"),
  100. Pkg.FullName(true).c_str(), pattern.c_str());
  101. explicitlyNamed = false;
  102. }
  103. virtual void showSelectedVersion(pkgCache::PkgIterator const &/*Pkg*/, pkgCache::VerIterator const Ver,
  104. std::string const &ver, bool const /*verIsRel*/) {
  105. if (ver == Ver.VerStr())
  106. return;
  107. selectedByRelease.push_back(make_pair(Ver, ver));
  108. }
  109. bool showVirtualPackageErrors(pkgCacheFile &Cache) {
  110. if (virtualPkgs.empty() == true)
  111. return true;
  112. for (APT::PackageSet::const_iterator Pkg = virtualPkgs.begin();
  113. Pkg != virtualPkgs.end(); ++Pkg) {
  114. if (Pkg->ProvidesList != 0) {
  115. ioprintf(c1out,_("Package %s is a virtual package provided by:\n"),
  116. Pkg.FullName(true).c_str());
  117. pkgCache::PrvIterator I = Pkg.ProvidesList();
  118. unsigned short provider = 0;
  119. for (; I.end() == false; ++I) {
  120. pkgCache::PkgIterator Pkg = I.OwnerPkg();
  121. if (Cache[Pkg].CandidateVerIter(Cache) == I.OwnerVer()) {
  122. c1out << " " << Pkg.FullName(true) << " " << I.OwnerVer().VerStr();
  123. if (Cache[Pkg].Install() == true && Cache[Pkg].NewInstall() == false)
  124. c1out << _(" [Installed]");
  125. c1out << std::endl;
  126. ++provider;
  127. }
  128. }
  129. // if we found no candidate which provide this package, show non-candidates
  130. if (provider == 0)
  131. for (I = Pkg.ProvidesList(); I.end() == false; ++I)
  132. c1out << " " << I.OwnerPkg().FullName(true) << " " << I.OwnerVer().VerStr()
  133. << _(" [Not candidate version]") << std::endl;
  134. else
  135. out << _("You should explicitly select one to install.") << std::endl;
  136. } else {
  137. ioprintf(c1out,
  138. _("Package %s is not available, but is referred to by another package.\n"
  139. "This may mean that the package is missing, has been obsoleted, or\n"
  140. "is only available from another source\n"),Pkg.FullName(true).c_str());
  141. std::string List;
  142. std::string VersionsList;
  143. SPtrArray<bool> Seen = new bool[Cache.GetPkgCache()->Head().PackageCount];
  144. memset(Seen,0,Cache.GetPkgCache()->Head().PackageCount*sizeof(*Seen));
  145. for (pkgCache::DepIterator Dep = Pkg.RevDependsList();
  146. Dep.end() == false; ++Dep) {
  147. if (Dep->Type != pkgCache::Dep::Replaces)
  148. continue;
  149. if (Seen[Dep.ParentPkg()->ID] == true)
  150. continue;
  151. Seen[Dep.ParentPkg()->ID] = true;
  152. List += Dep.ParentPkg().FullName(true) + " ";
  153. //VersionsList += std::string(Dep.ParentPkg().CurVersion) + "\n"; ???
  154. }
  155. ShowList(c1out,_("However the following packages replace it:"),List,VersionsList);
  156. }
  157. c1out << std::endl;
  158. }
  159. return false;
  160. }
  161. virtual pkgCache::VerIterator canNotFindCandidateVer(pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg) {
  162. APT::VersionSet const verset = tryVirtualPackage(Cache, Pkg, CacheSetHelper::CANDIDATE);
  163. if (verset.empty() == false)
  164. return *(verset.begin());
  165. else if (ShowError == true) {
  166. _error->Error(_("Package '%s' has no installation candidate"),Pkg.FullName(true).c_str());
  167. virtualPkgs.insert(Pkg);
  168. }
  169. return pkgCache::VerIterator(Cache, 0);
  170. }
  171. virtual pkgCache::VerIterator canNotFindNewestVer(pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg) {
  172. if (Pkg->ProvidesList != 0)
  173. {
  174. APT::VersionSet const verset = tryVirtualPackage(Cache, Pkg, CacheSetHelper::NEWEST);
  175. if (verset.empty() == false)
  176. return *(verset.begin());
  177. if (ShowError == true)
  178. ioprintf(out, _("Virtual packages like '%s' can't be removed\n"), Pkg.FullName(true).c_str());
  179. }
  180. else
  181. {
  182. pkgCache::GrpIterator Grp = Pkg.Group();
  183. pkgCache::PkgIterator P = Grp.PackageList();
  184. for (; P.end() != true; P = Grp.NextPkg(P))
  185. {
  186. if (P == Pkg)
  187. continue;
  188. if (P->CurrentVer != 0) {
  189. // TRANSLATORS: Note, this is not an interactive question
  190. ioprintf(c1out,_("Package '%s' is not installed, so not removed. Did you mean '%s'?\n"),
  191. Pkg.FullName(true).c_str(), P.FullName(true).c_str());
  192. break;
  193. }
  194. }
  195. if (P.end() == true)
  196. ioprintf(c1out,_("Package '%s' is not installed, so not removed\n"),Pkg.FullName(true).c_str());
  197. }
  198. return pkgCache::VerIterator(Cache, 0);
  199. }
  200. APT::VersionSet tryVirtualPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg,
  201. CacheSetHelper::VerSelector const select) {
  202. /* This is a pure virtual package and there is a single available
  203. candidate providing it. */
  204. if (unlikely(Cache[Pkg].CandidateVer != 0) || Pkg->ProvidesList == 0)
  205. return APT::VersionSet();
  206. pkgCache::PkgIterator Prov;
  207. bool found_one = false;
  208. for (pkgCache::PrvIterator P = Pkg.ProvidesList(); P; ++P) {
  209. pkgCache::VerIterator const PVer = P.OwnerVer();
  210. pkgCache::PkgIterator const PPkg = PVer.ParentPkg();
  211. /* Ignore versions that are not a candidate. */
  212. if (Cache[PPkg].CandidateVer != PVer)
  213. continue;
  214. if (found_one == false) {
  215. Prov = PPkg;
  216. found_one = true;
  217. } else if (PPkg != Prov) {
  218. // same group, so it's a foreign package
  219. if (PPkg->Group == Prov->Group) {
  220. // do we already have the requested arch?
  221. if (strcmp(Pkg.Arch(), Prov.Arch()) == 0 ||
  222. strcmp(Prov.Arch(), "all") == 0 ||
  223. unlikely(strcmp(PPkg.Arch(), Prov.Arch()) == 0)) // packages have only on candidate, but just to be sure
  224. continue;
  225. // see which architecture we prefer more and switch to it
  226. std::vector<std::string> archs = APT::Configuration::getArchitectures();
  227. if (std::find(archs.begin(), archs.end(), PPkg.Arch()) < std::find(archs.begin(), archs.end(), Prov.Arch()))
  228. Prov = PPkg;
  229. continue;
  230. }
  231. found_one = false; // we found at least two
  232. break;
  233. }
  234. }
  235. if (found_one == true) {
  236. ioprintf(out, _("Note, selecting '%s' instead of '%s'\n"),
  237. Prov.FullName(true).c_str(), Pkg.FullName(true).c_str());
  238. return APT::VersionSet::FromPackage(Cache, Prov, select, *this);
  239. }
  240. return APT::VersionSet();
  241. }
  242. inline bool allPkgNamedExplicitly() const { return explicitlyNamed; }
  243. };
  244. /*}}}*/
  245. #endif