cacheiterators.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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 stucture 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. refering 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. std::string RelStr() const;
  187. bool Automatic() const;
  188. VerFileIterator NewestFile() const;
  189. inline VerIterator(pkgCache &Owner,Version *Trg = 0) : Iterator<Version, VerIterator>(Owner, Trg) {
  190. if (S == 0)
  191. S = OwnerPointer();
  192. };
  193. inline VerIterator() : Iterator<Version, VerIterator>() {};
  194. };
  195. /*}}}*/
  196. // Description Iterator /*{{{*/
  197. class pkgCache::DescIterator : public Iterator<Description, DescIterator> {
  198. protected:
  199. inline Description* OwnerPointer() const {
  200. return (Owner != 0) ? Owner->DescP : 0;
  201. };
  202. public:
  203. // Iteration
  204. void operator ++(int) {if (S != Owner->DescP) S = Owner->DescP + S->NextDesc;};
  205. inline void operator ++() {operator ++(0);};
  206. // Comparison
  207. int CompareDesc(const DescIterator &B) const;
  208. // Accessors
  209. inline const char *LanguageCode() const {return Owner->StrP + S->language_code;};
  210. inline const char *md5() const {return Owner->StrP + S->md5sum;};
  211. inline DescFileIterator FileList() const;
  212. inline DescIterator() : Iterator<Description, DescIterator>() {};
  213. inline DescIterator(pkgCache &Owner,Description *Trg = 0) : Iterator<Description, DescIterator>(Owner, Trg) {
  214. if (S == 0)
  215. S = Owner.DescP;
  216. };
  217. };
  218. /*}}}*/
  219. // Dependency iterator /*{{{*/
  220. class pkgCache::DepIterator : public Iterator<Dependency, DepIterator> {
  221. enum {DepVer, DepRev} Type;
  222. protected:
  223. inline Dependency* OwnerPointer() const {
  224. return (Owner != 0) ? Owner->DepP : 0;
  225. };
  226. public:
  227. // Iteration
  228. void operator ++(int) {if (S != Owner->DepP) S = Owner->DepP +
  229. (Type == DepVer ? S->NextDepends : S->NextRevDepends);};
  230. inline void operator ++() {operator ++(0);};
  231. // Accessors
  232. inline const char *TargetVer() const {return S->Version == 0?0:Owner->StrP + S->Version;};
  233. inline PkgIterator TargetPkg() const {return PkgIterator(*Owner,Owner->PkgP + S->Package);};
  234. inline PkgIterator SmartTargetPkg() const {PkgIterator R(*Owner,0);SmartTargetPkg(R);return R;};
  235. inline VerIterator ParentVer() const {return VerIterator(*Owner,Owner->VerP + S->ParentVer);};
  236. inline PkgIterator ParentPkg() const {return PkgIterator(*Owner,Owner->PkgP + Owner->VerP[S->ParentVer].ParentPkg);};
  237. inline bool Reverse() const {return Type == DepRev;};
  238. bool IsCritical() const;
  239. bool IsNegative() const;
  240. bool IsIgnorable(PrvIterator const &Prv) const;
  241. bool IsIgnorable(PkgIterator const &Pkg) const;
  242. bool IsMultiArchImplicit() const;
  243. void GlobOr(DepIterator &Start,DepIterator &End);
  244. Version **AllTargets() const;
  245. bool SmartTargetPkg(PkgIterator &Result) const;
  246. inline const char *CompType() const {return Owner->CompType(S->CompareOp);};
  247. inline const char *DepType() const {return Owner->DepType(S->Type);};
  248. //Nice printable representation
  249. friend std::ostream& operator <<(std::ostream& out, DepIterator D);
  250. inline DepIterator(pkgCache &Owner, Dependency *Trg, Version* = 0) :
  251. Iterator<Dependency, DepIterator>(Owner, Trg), Type(DepVer) {
  252. if (S == 0)
  253. S = Owner.DepP;
  254. };
  255. inline DepIterator(pkgCache &Owner, Dependency *Trg, Package*) :
  256. Iterator<Dependency, DepIterator>(Owner, Trg), Type(DepRev) {
  257. if (S == 0)
  258. S = Owner.DepP;
  259. };
  260. inline DepIterator() : Iterator<Dependency, DepIterator>(), Type(DepVer) {};
  261. };
  262. /*}}}*/
  263. // Provides iterator /*{{{*/
  264. class pkgCache::PrvIterator : public Iterator<Provides, PrvIterator> {
  265. enum {PrvVer, PrvPkg} Type;
  266. protected:
  267. inline Provides* OwnerPointer() const {
  268. return (Owner != 0) ? Owner->ProvideP : 0;
  269. };
  270. public:
  271. // Iteration
  272. void operator ++(int) {if (S != Owner->ProvideP) S = Owner->ProvideP +
  273. (Type == PrvVer?S->NextPkgProv:S->NextProvides);};
  274. inline void operator ++() {operator ++(0);};
  275. // Accessors
  276. inline const char *Name() const {return Owner->StrP + Owner->PkgP[S->ParentPkg].Name;};
  277. inline const char *ProvideVersion() const {return S->ProvideVersion == 0?0:Owner->StrP + S->ProvideVersion;};
  278. inline PkgIterator ParentPkg() const {return PkgIterator(*Owner,Owner->PkgP + S->ParentPkg);};
  279. inline VerIterator OwnerVer() const {return VerIterator(*Owner,Owner->VerP + S->Version);};
  280. inline PkgIterator OwnerPkg() const {return PkgIterator(*Owner,Owner->PkgP + Owner->VerP[S->Version].ParentPkg);};
  281. bool IsMultiArchImplicit() const;
  282. inline PrvIterator() : Iterator<Provides, PrvIterator>(), Type(PrvVer) {};
  283. inline PrvIterator(pkgCache &Owner, Provides *Trg, Version*) :
  284. Iterator<Provides, PrvIterator>(Owner, Trg), Type(PrvVer) {
  285. if (S == 0)
  286. S = Owner.ProvideP;
  287. };
  288. inline PrvIterator(pkgCache &Owner, Provides *Trg, Package*) :
  289. Iterator<Provides, PrvIterator>(Owner, Trg), Type(PrvPkg) {
  290. if (S == 0)
  291. S = Owner.ProvideP;
  292. };
  293. };
  294. /*}}}*/
  295. // Package file /*{{{*/
  296. class pkgCache::PkgFileIterator : public Iterator<PackageFile, PkgFileIterator> {
  297. protected:
  298. inline PackageFile* OwnerPointer() const {
  299. return (Owner != 0) ? Owner->PkgFileP : 0;
  300. };
  301. public:
  302. // Iteration
  303. void operator ++(int) {if (S != Owner->PkgFileP) S = Owner->PkgFileP + S->NextFile;};
  304. inline void operator ++() {operator ++(0);};
  305. // Accessors
  306. inline const char *FileName() const {return S->FileName == 0?0:Owner->StrP + S->FileName;};
  307. inline const char *Archive() const {return S->Archive == 0?0:Owner->StrP + S->Archive;};
  308. inline const char *Component() const {return S->Component == 0?0:Owner->StrP + S->Component;};
  309. inline const char *Version() const {return S->Version == 0?0:Owner->StrP + S->Version;};
  310. inline const char *Origin() const {return S->Origin == 0?0:Owner->StrP + S->Origin;};
  311. inline const char *Codename() const {return S->Codename ==0?0:Owner->StrP + S->Codename;};
  312. inline const char *Label() const {return S->Label == 0?0:Owner->StrP + S->Label;};
  313. inline const char *Site() const {return S->Site == 0?0:Owner->StrP + S->Site;};
  314. inline const char *Architecture() const {return S->Architecture == 0?0:Owner->StrP + S->Architecture;};
  315. inline const char *IndexType() const {return S->IndexType == 0?0:Owner->StrP + S->IndexType;};
  316. bool IsOk();
  317. std::string RelStr();
  318. // Constructors
  319. inline PkgFileIterator() : Iterator<PackageFile, PkgFileIterator>() {};
  320. inline PkgFileIterator(pkgCache &Owner) : Iterator<PackageFile, PkgFileIterator>(Owner, Owner.PkgFileP) {};
  321. inline PkgFileIterator(pkgCache &Owner,PackageFile *Trg) : Iterator<PackageFile, PkgFileIterator>(Owner, Trg) {};
  322. };
  323. /*}}}*/
  324. // Version File /*{{{*/
  325. class pkgCache::VerFileIterator : public pkgCache::Iterator<VerFile, VerFileIterator> {
  326. protected:
  327. inline VerFile* OwnerPointer() const {
  328. return (Owner != 0) ? Owner->VerFileP : 0;
  329. };
  330. public:
  331. // Iteration
  332. void operator ++(int) {if (S != Owner->VerFileP) S = Owner->VerFileP + S->NextFile;};
  333. inline void operator ++() {operator ++(0);};
  334. // Accessors
  335. inline PkgFileIterator File() const {return PkgFileIterator(*Owner,S->File + Owner->PkgFileP);};
  336. inline VerFileIterator() : Iterator<VerFile, VerFileIterator>() {};
  337. inline VerFileIterator(pkgCache &Owner,VerFile *Trg) : Iterator<VerFile, VerFileIterator>(Owner, Trg) {};
  338. };
  339. /*}}}*/
  340. // Description File /*{{{*/
  341. class pkgCache::DescFileIterator : public Iterator<DescFile, DescFileIterator> {
  342. protected:
  343. inline DescFile* OwnerPointer() const {
  344. return (Owner != 0) ? Owner->DescFileP : 0;
  345. };
  346. public:
  347. // Iteration
  348. void operator ++(int) {if (S != Owner->DescFileP) S = Owner->DescFileP + S->NextFile;};
  349. inline void operator ++() {operator ++(0);};
  350. // Accessors
  351. inline PkgFileIterator File() const {return PkgFileIterator(*Owner,S->File + Owner->PkgFileP);};
  352. inline DescFileIterator() : Iterator<DescFile, DescFileIterator>() {};
  353. inline DescFileIterator(pkgCache &Owner,DescFile *Trg) : Iterator<DescFile, DescFileIterator>(Owner, Trg) {};
  354. };
  355. /*}}}*/
  356. // Inlined Begin functions cant be in the class because of order problems /*{{{*/
  357. inline pkgCache::PkgIterator pkgCache::GrpIterator::PackageList() const
  358. {return PkgIterator(*Owner,Owner->PkgP + S->FirstPackage);};
  359. inline pkgCache::VerIterator pkgCache::PkgIterator::VersionList() const
  360. {return VerIterator(*Owner,Owner->VerP + S->VersionList);};
  361. inline pkgCache::VerIterator pkgCache::PkgIterator::CurrentVer() const
  362. {return VerIterator(*Owner,Owner->VerP + S->CurrentVer);};
  363. inline pkgCache::DepIterator pkgCache::PkgIterator::RevDependsList() const
  364. {return DepIterator(*Owner,Owner->DepP + S->RevDepends,S);};
  365. inline pkgCache::PrvIterator pkgCache::PkgIterator::ProvidesList() const
  366. {return PrvIterator(*Owner,Owner->ProvideP + S->ProvidesList,S);};
  367. inline pkgCache::DescIterator pkgCache::VerIterator::DescriptionList() const
  368. {return DescIterator(*Owner,Owner->DescP + S->DescriptionList);};
  369. inline pkgCache::PrvIterator pkgCache::VerIterator::ProvidesList() const
  370. {return PrvIterator(*Owner,Owner->ProvideP + S->ProvidesList,S);};
  371. inline pkgCache::DepIterator pkgCache::VerIterator::DependsList() const
  372. {return DepIterator(*Owner,Owner->DepP + S->DependsList,S);};
  373. inline pkgCache::VerFileIterator pkgCache::VerIterator::FileList() const
  374. {return VerFileIterator(*Owner,Owner->VerFileP + S->FileList);};
  375. inline pkgCache::DescFileIterator pkgCache::DescIterator::FileList() const
  376. {return DescFileIterator(*Owner,Owner->DescFileP + S->FileList);};
  377. /*}}}*/
  378. #endif