cacheiterators.h 16 KB

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