cacheiterators.h 17 KB

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