private-cacheset.h 10 KB

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