cacheiterators.h 17 KB

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