cacheiterators.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. /* ######################################################################
  4. Cache Iterators - Iterators for navigating the cache structure
  5. The iterators all provides ++,==,!=,->,* and end for their type.
  6. The end function can be used to tell if the list has been fully
  7. traversed.
  8. Unlike STL iterators these contain helper functions to access the data
  9. that is being iterated over. This is because the data structures can't
  10. be formed in a manner that is intuitive to use and also mmapable.
  11. For each variable in the target structure that would need a translation
  12. to be accessed correctly a translating function of the same name is
  13. present in the iterator. If applicable the translating function will
  14. return an iterator.
  15. The DepIterator can iterate over two lists, a list of 'version depends'
  16. or a list of 'package reverse depends'. The type is determined by the
  17. structure passed to the constructor, which should be the structure
  18. that has the depends pointer as a member. The provide iterator has the
  19. same system.
  20. This header is not user includable, please use apt-pkg/pkgcache.h
  21. ##################################################################### */
  22. /*}}}*/
  23. #ifndef PKGLIB_CACHEITERATORS_H
  24. #define PKGLIB_CACHEITERATORS_H
  25. #include<apt-pkg/pkgcache.h>
  26. #include<apt-pkg/macros.h>
  27. #include<iterator>
  28. #include <iosfwd>
  29. #include <string>
  30. #include<string.h>
  31. // abstract Iterator template /*{{{*/
  32. /* This template provides the very basic iterator methods we
  33. need to have for doing some walk-over-the-cache magic */
  34. template<typename Str, typename Itr> class pkgCache::Iterator :
  35. public std::iterator<std::forward_iterator_tag, Str> {
  36. protected:
  37. Str *S;
  38. pkgCache *Owner;
  39. /** \brief Returns the Pointer for this struct in the owner
  40. * The implementation of this method should be pretty short
  41. * as it will only return the Pointer into the mmap stored
  42. * in the owner but the name of this pointer is different for
  43. * each structure and we want to abstract here at least for the
  44. * basic methods from the actual structure.
  45. * \return Pointer to the first structure of this type
  46. */
  47. virtual Str* OwnerPointer() const = 0;
  48. public:
  49. // Iteration
  50. virtual void operator ++(int) = 0;
  51. virtual void operator ++() = 0; // Should be {operator ++(0);}
  52. inline bool end() const {return Owner == 0 || S == OwnerPointer();}
  53. // Comparison
  54. inline bool operator ==(const Itr &B) const {return S == B.S;}
  55. inline bool operator !=(const Itr &B) const {return S != B.S;}
  56. // Accessors
  57. inline Str *operator ->() {return S;}
  58. inline Str const *operator ->() const {return S;}
  59. inline operator Str *() {return S == OwnerPointer() ? 0 : S;}
  60. inline operator Str const *() const {return S == OwnerPointer() ? 0 : S;}
  61. inline Str &operator *() {return *S;}
  62. inline Str const &operator *() const {return *S;}
  63. inline pkgCache *Cache() const {return Owner;}
  64. // Mixed stuff
  65. inline void operator =(const Itr &B) {S = B.S; Owner = B.Owner;}
  66. inline bool IsGood() const { return S && Owner && ! end();}
  67. inline unsigned long Index() const {return S - OwnerPointer();}
  68. void ReMap(void const * const oldMap, void const * const newMap) {
  69. if (Owner == 0 || S == 0)
  70. return;
  71. S += (Str const * const)(newMap) - (Str const * const)(oldMap);
  72. }
  73. // Constructors - look out for the variable assigning
  74. inline Iterator() : S(0), Owner(0) {}
  75. inline Iterator(pkgCache &Owner,Str *T = 0) : S(T), Owner(&Owner) {}
  76. };
  77. /*}}}*/
  78. // Group Iterator /*{{{*/
  79. /* Packages with the same name are collected in a Group so someone only
  80. interest in package names can iterate easily over the names, so the
  81. different architectures can be treated as of the "same" package
  82. (apt internally treat them as totally different packages) */
  83. class pkgCache::GrpIterator: public Iterator<Group, GrpIterator> {
  84. long HashIndex;
  85. protected:
  86. inline Group* OwnerPointer() const {
  87. return (Owner != 0) ? Owner->GrpP : 0;
  88. }
  89. public:
  90. // This constructor is the 'begin' constructor, never use it.
  91. inline GrpIterator(pkgCache &Owner) : Iterator<Group, GrpIterator>(Owner), HashIndex(-1) {
  92. S = OwnerPointer();
  93. operator ++(0);
  94. }
  95. virtual void operator ++(int);
  96. virtual void operator ++() {operator ++(0);}
  97. inline const char *Name() const {return S->Name == 0?0:Owner->StrP + S->Name;}
  98. inline PkgIterator PackageList() const;
  99. PkgIterator FindPkg(std::string Arch = "any") const;
  100. /** \brief find the package with the "best" architecture
  101. The best architecture is either the "native" or the first
  102. in the list of Architectures which is not an end-Pointer
  103. \param PreferNonVirtual tries to respond with a non-virtual package
  104. and only if this fails returns the best virtual package */
  105. PkgIterator FindPreferredPkg(bool const &PreferNonVirtual = true) const;
  106. PkgIterator NextPkg(PkgIterator const &Pkg) const;
  107. // Constructors
  108. inline GrpIterator(pkgCache &Owner, Group *Trg) : Iterator<Group, GrpIterator>(Owner, Trg), HashIndex(0) {
  109. if (S == 0)
  110. S = OwnerPointer();
  111. }
  112. inline GrpIterator() : Iterator<Group, GrpIterator>(), HashIndex(0) {}
  113. };
  114. /*}}}*/
  115. // Package Iterator /*{{{*/
  116. class pkgCache::PkgIterator: public Iterator<Package, PkgIterator> {
  117. long HashIndex;
  118. protected:
  119. inline Package* OwnerPointer() const {
  120. return (Owner != 0) ? Owner->PkgP : 0;
  121. }
  122. public:
  123. // This constructor is the 'begin' constructor, never use it.
  124. inline PkgIterator(pkgCache &Owner) : Iterator<Package, PkgIterator>(Owner), HashIndex(-1) {
  125. S = OwnerPointer();
  126. operator ++(0);
  127. }
  128. virtual void operator ++(int);
  129. virtual void operator ++() {operator ++(0);}
  130. enum OkState {NeedsNothing,NeedsUnpack,NeedsConfigure};
  131. // Accessors
  132. inline const char *Name() const {return S->Name == 0?0:Owner->StrP + S->Name;}
  133. // Versions have sections - and packages can have different versions with different sections
  134. // so this interface is broken by design. It used to return the section of the "first parsed
  135. // package stanza", but as this can potentially be anything it now returns the section of the
  136. // newest version instead (if any). aka: Run as fast as you can to Version.Section().
  137. APT_DEPRECATED const char *Section() const;
  138. inline bool Purge() const {return S->CurrentState == pkgCache::State::Purge ||
  139. (S->CurrentVer == 0 && S->CurrentState == pkgCache::State::NotInstalled);}
  140. inline const char *Arch() const {return S->Arch == 0?0:Owner->StrP + S->Arch;}
  141. inline APT_PURE GrpIterator Group() const { return GrpIterator(*Owner, Owner->GrpP + S->Group);}
  142. inline VerIterator VersionList() const APT_PURE;
  143. inline VerIterator CurrentVer() const APT_PURE;
  144. inline DepIterator RevDependsList() const APT_PURE;
  145. inline PrvIterator ProvidesList() const APT_PURE;
  146. OkState State() const APT_PURE;
  147. const char *CandVersion() const APT_PURE;
  148. const char *CurVersion() const APT_PURE;
  149. //Nice printable representation
  150. friend std::ostream& operator <<(std::ostream& out, PkgIterator i);
  151. std::string FullName(bool const &Pretty = false) const;
  152. // Constructors
  153. inline PkgIterator(pkgCache &Owner,Package *Trg) : Iterator<Package, PkgIterator>(Owner, Trg), HashIndex(0) {
  154. if (S == 0)
  155. S = OwnerPointer();
  156. }
  157. inline PkgIterator() : Iterator<Package, PkgIterator>(), HashIndex(0) {}
  158. };
  159. /*}}}*/
  160. // Version Iterator /*{{{*/
  161. class pkgCache::VerIterator : public Iterator<Version, VerIterator> {
  162. protected:
  163. inline Version* OwnerPointer() const {
  164. return (Owner != 0) ? Owner->VerP : 0;
  165. }
  166. public:
  167. // Iteration
  168. void operator ++(int) {if (S != Owner->VerP) S = Owner->VerP + S->NextVer;}
  169. inline void operator ++() {operator ++(0);}
  170. // Comparison
  171. int CompareVer(const VerIterator &B) const;
  172. /** \brief compares two version and returns if they are similar
  173. This method should be used to identify if two pseudo versions are
  174. referring to the same "real" version */
  175. inline bool SimilarVer(const VerIterator &B) const {
  176. return (B.end() == false && S->Hash == B->Hash && strcmp(VerStr(), B.VerStr()) == 0);
  177. }
  178. // Accessors
  179. inline const char *VerStr() const {return S->VerStr == 0?0:Owner->StrP + S->VerStr;}
  180. inline const char *Section() const {return S->Section == 0?0:Owner->StrP + S->Section;}
  181. inline const char *Arch() const {
  182. if ((S->MultiArch & pkgCache::Version::All) == pkgCache::Version::All)
  183. return "all";
  184. return S->ParentPkg == 0?0:Owner->StrP + ParentPkg()->Arch;
  185. }
  186. inline PkgIterator ParentPkg() const {return PkgIterator(*Owner,Owner->PkgP + S->ParentPkg);}
  187. inline DescIterator DescriptionList() const;
  188. DescIterator TranslatedDescription() const;
  189. inline DepIterator DependsList() const;
  190. inline PrvIterator ProvidesList() const;
  191. inline VerFileIterator FileList() const;
  192. bool Downloadable() const;
  193. inline const char *PriorityType() const {return Owner->Priority(S->Priority);}
  194. const char *MultiArchType() const APT_PURE;
  195. std::string RelStr() const;
  196. bool Automatic() const;
  197. VerFileIterator NewestFile() const;
  198. inline VerIterator(pkgCache &Owner,Version *Trg = 0) : Iterator<Version, VerIterator>(Owner, Trg) {
  199. if (S == 0)
  200. S = OwnerPointer();
  201. }
  202. inline VerIterator() : Iterator<Version, VerIterator>() {}
  203. };
  204. /*}}}*/
  205. // Description Iterator /*{{{*/
  206. class pkgCache::DescIterator : public Iterator<Description, DescIterator> {
  207. protected:
  208. inline Description* OwnerPointer() const {
  209. return (Owner != 0) ? Owner->DescP : 0;
  210. }
  211. public:
  212. // Iteration
  213. void operator ++(int) {if (S != Owner->DescP) S = Owner->DescP + S->NextDesc;}
  214. inline void operator ++() {operator ++(0);}
  215. // Comparison
  216. int CompareDesc(const DescIterator &B) const;
  217. // Accessors
  218. inline const char *LanguageCode() const {return Owner->StrP + S->language_code;}
  219. inline const char *md5() const {return Owner->StrP + S->md5sum;}
  220. inline DescFileIterator FileList() const;
  221. inline DescIterator() : Iterator<Description, DescIterator>() {}
  222. inline DescIterator(pkgCache &Owner,Description *Trg = 0) : Iterator<Description, DescIterator>(Owner, Trg) {
  223. if (S == 0)
  224. S = Owner.DescP;
  225. }
  226. };
  227. /*}}}*/
  228. // Dependency iterator /*{{{*/
  229. class pkgCache::DepIterator : public Iterator<Dependency, DepIterator> {
  230. enum {DepVer, DepRev} Type;
  231. protected:
  232. inline Dependency* OwnerPointer() const {
  233. return (Owner != 0) ? Owner->DepP : 0;
  234. }
  235. public:
  236. // Iteration
  237. void operator ++(int) {if (S != Owner->DepP) S = Owner->DepP +
  238. (Type == DepVer ? S->NextDepends : S->NextRevDepends);}
  239. inline void operator ++() {operator ++(0);}
  240. // Accessors
  241. inline const char *TargetVer() const {return S->Version == 0?0:Owner->StrP + S->Version;}
  242. inline PkgIterator TargetPkg() const {return PkgIterator(*Owner,Owner->PkgP + S->Package);}
  243. inline PkgIterator SmartTargetPkg() const {PkgIterator R(*Owner,0);SmartTargetPkg(R);return R;}
  244. inline VerIterator ParentVer() const {return VerIterator(*Owner,Owner->VerP + S->ParentVer);}
  245. inline PkgIterator ParentPkg() const {return PkgIterator(*Owner,Owner->PkgP + Owner->VerP[S->ParentVer].ParentPkg);}
  246. inline bool Reverse() const {return Type == DepRev;}
  247. bool IsCritical() const APT_PURE;
  248. bool IsNegative() const APT_PURE;
  249. bool IsIgnorable(PrvIterator const &Prv) const APT_PURE;
  250. bool IsIgnorable(PkgIterator const &Pkg) const APT_PURE;
  251. bool IsMultiArchImplicit() const APT_PURE;
  252. bool IsSatisfied(VerIterator const &Ver) const APT_PURE;
  253. bool IsSatisfied(PrvIterator const &Prv) const APT_PURE;
  254. void GlobOr(DepIterator &Start,DepIterator &End);
  255. Version **AllTargets() const;
  256. bool SmartTargetPkg(PkgIterator &Result) const;
  257. inline const char *CompType() const {return Owner->CompType(S->CompareOp);}
  258. inline const char *DepType() const {return Owner->DepType(S->Type);}
  259. //Nice printable representation
  260. friend std::ostream& operator <<(std::ostream& out, DepIterator D);
  261. inline DepIterator(pkgCache &Owner, Dependency *Trg, Version* = 0) :
  262. Iterator<Dependency, DepIterator>(Owner, Trg), Type(DepVer) {
  263. if (S == 0)
  264. S = Owner.DepP;
  265. }
  266. inline DepIterator(pkgCache &Owner, Dependency *Trg, Package*) :
  267. Iterator<Dependency, DepIterator>(Owner, Trg), Type(DepRev) {
  268. if (S == 0)
  269. S = Owner.DepP;
  270. }
  271. inline DepIterator() : Iterator<Dependency, DepIterator>(), Type(DepVer) {}
  272. };
  273. /*}}}*/
  274. // Provides iterator /*{{{*/
  275. class pkgCache::PrvIterator : public Iterator<Provides, PrvIterator> {
  276. enum {PrvVer, PrvPkg} Type;
  277. protected:
  278. inline Provides* OwnerPointer() const {
  279. return (Owner != 0) ? Owner->ProvideP : 0;
  280. }
  281. public:
  282. // Iteration
  283. void operator ++(int) {if (S != Owner->ProvideP) S = Owner->ProvideP +
  284. (Type == PrvVer?S->NextPkgProv:S->NextProvides);}
  285. inline void operator ++() {operator ++(0);}
  286. // Accessors
  287. inline const char *Name() const {return Owner->StrP + Owner->PkgP[S->ParentPkg].Name;}
  288. inline const char *ProvideVersion() const {return S->ProvideVersion == 0?0:Owner->StrP + S->ProvideVersion;}
  289. inline PkgIterator ParentPkg() const {return PkgIterator(*Owner,Owner->PkgP + S->ParentPkg);}
  290. inline VerIterator OwnerVer() const {return VerIterator(*Owner,Owner->VerP + S->Version);}
  291. inline PkgIterator OwnerPkg() const {return PkgIterator(*Owner,Owner->PkgP + Owner->VerP[S->Version].ParentPkg);}
  292. bool IsMultiArchImplicit() const APT_PURE;
  293. inline PrvIterator() : Iterator<Provides, PrvIterator>(), Type(PrvVer) {}
  294. inline PrvIterator(pkgCache &Owner, Provides *Trg, Version*) :
  295. Iterator<Provides, PrvIterator>(Owner, Trg), Type(PrvVer) {
  296. if (S == 0)
  297. S = Owner.ProvideP;
  298. }
  299. inline PrvIterator(pkgCache &Owner, Provides *Trg, Package*) :
  300. Iterator<Provides, PrvIterator>(Owner, Trg), Type(PrvPkg) {
  301. if (S == 0)
  302. S = Owner.ProvideP;
  303. }
  304. };
  305. /*}}}*/
  306. // Package file /*{{{*/
  307. class pkgCache::PkgFileIterator : public Iterator<PackageFile, PkgFileIterator> {
  308. protected:
  309. inline PackageFile* OwnerPointer() const {
  310. return (Owner != 0) ? Owner->PkgFileP : 0;
  311. }
  312. public:
  313. // Iteration
  314. void operator ++(int) {if (S != Owner->PkgFileP) S = Owner->PkgFileP + S->NextFile;}
  315. inline void operator ++() {operator ++(0);}
  316. // Accessors
  317. inline const char *FileName() const {return S->FileName == 0?0:Owner->StrP + S->FileName;}
  318. inline const char *Archive() const {return S->Archive == 0?0:Owner->StrP + S->Archive;}
  319. inline const char *Component() const {return S->Component == 0?0:Owner->StrP + S->Component;}
  320. inline const char *Version() const {return S->Version == 0?0:Owner->StrP + S->Version;}
  321. inline const char *Origin() const {return S->Origin == 0?0:Owner->StrP + S->Origin;}
  322. inline const char *Codename() const {return S->Codename ==0?0:Owner->StrP + S->Codename;}
  323. inline const char *Label() const {return S->Label == 0?0:Owner->StrP + S->Label;}
  324. inline const char *Site() const {return S->Site == 0?0:Owner->StrP + S->Site;}
  325. inline const char *Architecture() const {return S->Architecture == 0?0:Owner->StrP + S->Architecture;}
  326. inline const char *IndexType() const {return S->IndexType == 0?0:Owner->StrP + S->IndexType;}
  327. bool IsOk();
  328. std::string RelStr();
  329. // Constructors
  330. inline PkgFileIterator() : Iterator<PackageFile, PkgFileIterator>() {}
  331. inline PkgFileIterator(pkgCache &Owner) : Iterator<PackageFile, PkgFileIterator>(Owner, Owner.PkgFileP) {}
  332. inline PkgFileIterator(pkgCache &Owner,PackageFile *Trg) : Iterator<PackageFile, PkgFileIterator>(Owner, Trg) {}
  333. };
  334. /*}}}*/
  335. // Version File /*{{{*/
  336. class pkgCache::VerFileIterator : public pkgCache::Iterator<VerFile, VerFileIterator> {
  337. protected:
  338. inline VerFile* OwnerPointer() const {
  339. return (Owner != 0) ? Owner->VerFileP : 0;
  340. }
  341. public:
  342. // Iteration
  343. void operator ++(int) {if (S != Owner->VerFileP) S = Owner->VerFileP + S->NextFile;}
  344. inline void operator ++() {operator ++(0);}
  345. // Accessors
  346. inline PkgFileIterator File() const {return PkgFileIterator(*Owner,S->File + Owner->PkgFileP);}
  347. inline VerFileIterator() : Iterator<VerFile, VerFileIterator>() {}
  348. inline VerFileIterator(pkgCache &Owner,VerFile *Trg) : Iterator<VerFile, VerFileIterator>(Owner, Trg) {}
  349. };
  350. /*}}}*/
  351. // Description File /*{{{*/
  352. class pkgCache::DescFileIterator : public Iterator<DescFile, DescFileIterator> {
  353. protected:
  354. inline DescFile* OwnerPointer() const {
  355. return (Owner != 0) ? Owner->DescFileP : 0;
  356. }
  357. public:
  358. // Iteration
  359. void operator ++(int) {if (S != Owner->DescFileP) S = Owner->DescFileP + S->NextFile;}
  360. inline void operator ++() {operator ++(0);}
  361. // Accessors
  362. inline PkgFileIterator File() const {return PkgFileIterator(*Owner,S->File + Owner->PkgFileP);}
  363. inline DescFileIterator() : Iterator<DescFile, DescFileIterator>() {}
  364. inline DescFileIterator(pkgCache &Owner,DescFile *Trg) : Iterator<DescFile, DescFileIterator>(Owner, Trg) {}
  365. };
  366. /*}}}*/
  367. // Inlined Begin functions can't be in the class because of order problems /*{{{*/
  368. inline pkgCache::PkgIterator pkgCache::GrpIterator::PackageList() const
  369. {return PkgIterator(*Owner,Owner->PkgP + S->FirstPackage);}
  370. inline pkgCache::VerIterator pkgCache::PkgIterator::VersionList() const
  371. {return VerIterator(*Owner,Owner->VerP + S->VersionList);}
  372. inline pkgCache::VerIterator pkgCache::PkgIterator::CurrentVer() const
  373. {return VerIterator(*Owner,Owner->VerP + S->CurrentVer);}
  374. inline pkgCache::DepIterator pkgCache::PkgIterator::RevDependsList() const
  375. {return DepIterator(*Owner,Owner->DepP + S->RevDepends,S);}
  376. inline pkgCache::PrvIterator pkgCache::PkgIterator::ProvidesList() const
  377. {return PrvIterator(*Owner,Owner->ProvideP + S->ProvidesList,S);}
  378. inline pkgCache::DescIterator pkgCache::VerIterator::DescriptionList() const
  379. {return DescIterator(*Owner,Owner->DescP + S->DescriptionList);}
  380. inline pkgCache::PrvIterator pkgCache::VerIterator::ProvidesList() const
  381. {return PrvIterator(*Owner,Owner->ProvideP + S->ProvidesList,S);}
  382. inline pkgCache::DepIterator pkgCache::VerIterator::DependsList() const
  383. {return DepIterator(*Owner,Owner->DepP + S->DependsList,S);}
  384. inline pkgCache::VerFileIterator pkgCache::VerIterator::FileList() const
  385. {return VerFileIterator(*Owner,Owner->VerFileP + S->FileList);}
  386. inline pkgCache::DescFileIterator pkgCache::DescIterator::FileList() const
  387. {return DescFileIterator(*Owner,Owner->DescFileP + S->FileList);}
  388. /*}}}*/
  389. #endif