cacheset.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. /** \file cacheset.h
  4. Wrappers around std::set to have set::iterators which behave
  5. similar to the Iterators of the cache structures.
  6. Provides also a few helper methods which work with these sets */
  7. /*}}}*/
  8. #ifndef APT_CACHESET_H
  9. #define APT_CACHESET_H
  10. // Include Files /*{{{*/
  11. #include <iostream>
  12. #include <fstream>
  13. #include <list>
  14. #include <map>
  15. #include <set>
  16. #include <string>
  17. #include <apt-pkg/cachefile.h>
  18. #include <apt-pkg/pkgcache.h>
  19. /*}}}*/
  20. namespace APT {
  21. class PackageSet;
  22. class VersionSet;
  23. class CacheSetHelper { /*{{{*/
  24. /** \class APT::CacheSetHelper
  25. Simple base class with a lot of virtual methods which can be overridden
  26. to alter the behavior or the output of the CacheSets.
  27. This helper is passed around by the static methods in the CacheSets and
  28. used every time they hit an error condition or something could be
  29. printed out.
  30. */
  31. public: /*{{{*/
  32. CacheSetHelper(bool const &ShowError = true) : ShowError(ShowError) {};
  33. virtual ~CacheSetHelper() {};
  34. virtual void showTaskSelection(PackageSet const &pkgset, string const &pattern) {};
  35. virtual void showRegExSelection(PackageSet const &pkgset, string const &pattern) {};
  36. virtual void showSelectedVersion(pkgCache::PkgIterator const &Pkg, pkgCache::VerIterator const Ver,
  37. string const &ver, bool const &verIsRel) {};
  38. virtual pkgCache::PkgIterator canNotFindPkgName(pkgCacheFile &Cache, std::string const &str);
  39. virtual PackageSet canNotFindTask(pkgCacheFile &Cache, std::string pattern);
  40. virtual PackageSet canNotFindRegEx(pkgCacheFile &Cache, std::string pattern);
  41. virtual PackageSet canNotFindPackage(pkgCacheFile &Cache, std::string const &str);
  42. virtual VersionSet canNotFindAllVer(pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg);
  43. virtual VersionSet canNotFindInstCandVer(pkgCacheFile &Cache,
  44. pkgCache::PkgIterator const &Pkg);
  45. virtual VersionSet canNotFindCandInstVer(pkgCacheFile &Cache,
  46. pkgCache::PkgIterator const &Pkg);
  47. virtual pkgCache::VerIterator canNotFindNewestVer(pkgCacheFile &Cache,
  48. pkgCache::PkgIterator const &Pkg);
  49. virtual pkgCache::VerIterator canNotFindCandidateVer(pkgCacheFile &Cache,
  50. pkgCache::PkgIterator const &Pkg);
  51. virtual pkgCache::VerIterator canNotFindInstalledVer(pkgCacheFile &Cache,
  52. pkgCache::PkgIterator const &Pkg);
  53. bool showErrors() const { return ShowError; };
  54. bool showErrors(bool const &newValue) { if (ShowError == newValue) return ShowError; else return ((ShowError = newValue) == false); };
  55. /*}}}*/
  56. protected:
  57. bool ShowError;
  58. }; /*}}}*/
  59. class PackageSet : public std::set<pkgCache::PkgIterator> { /*{{{*/
  60. /** \class APT::PackageSet
  61. Simple wrapper around a std::set to provide a similar interface to
  62. a set of packages as to the complete set of all packages in the
  63. pkgCache. */
  64. public: /*{{{*/
  65. /** \brief smell like a pkgCache::PkgIterator */
  66. class const_iterator : public std::set<pkgCache::PkgIterator>::const_iterator {/*{{{*/
  67. public:
  68. const_iterator(std::set<pkgCache::PkgIterator>::const_iterator x) :
  69. std::set<pkgCache::PkgIterator>::const_iterator(x) {}
  70. operator pkgCache::PkgIterator(void) { return **this; }
  71. inline const char *Name() const {return (**this).Name(); }
  72. inline std::string FullName(bool const &Pretty) const { return (**this).FullName(Pretty); }
  73. inline std::string FullName() const { return (**this).FullName(); }
  74. inline const char *Section() const {return (**this).Section(); }
  75. inline bool Purge() const {return (**this).Purge(); }
  76. inline const char *Arch() const {return (**this).Arch(); }
  77. inline pkgCache::GrpIterator Group() const { return (**this).Group(); }
  78. inline pkgCache::VerIterator VersionList() const { return (**this).VersionList(); }
  79. inline pkgCache::VerIterator CurrentVer() const { return (**this).CurrentVer(); }
  80. inline pkgCache::DepIterator RevDependsList() const { return (**this).RevDependsList(); }
  81. inline pkgCache::PrvIterator ProvidesList() const { return (**this).ProvidesList(); }
  82. inline pkgCache::PkgIterator::OkState State() const { return (**this).State(); }
  83. inline const char *CandVersion() const { return (**this).CandVersion(); }
  84. inline const char *CurVersion() const { return (**this).CurVersion(); }
  85. inline pkgCache *Cache() const { return (**this).Cache(); };
  86. inline unsigned long Index() const {return (**this).Index();};
  87. // we have only valid iterators here
  88. inline bool end() const { return false; };
  89. friend std::ostream& operator<<(std::ostream& out, const_iterator i) { return operator<<(out, (*i)); }
  90. inline pkgCache::Package const * operator->() const {
  91. return &***this;
  92. };
  93. };
  94. // 103. set::iterator is required to be modifiable, but this allows modification of keys
  95. typedef APT::PackageSet::const_iterator iterator;
  96. /*}}}*/
  97. using std::set<pkgCache::PkgIterator>::insert;
  98. inline void insert(pkgCache::PkgIterator const &P) { if (P.end() == false) std::set<pkgCache::PkgIterator>::insert(P); };
  99. inline void insert(PackageSet const &pkgset) { insert(pkgset.begin(), pkgset.end()); };
  100. /** \brief returns all packages in the cache who belong to the given task
  101. A simple helper responsible for search for all members of a task
  102. in the cache. Optional it prints a a notice about the
  103. packages chosen cause of the given task.
  104. \param Cache the packages are in
  105. \param pattern name of the task
  106. \param out stream to print the notice to */
  107. static APT::PackageSet FromTask(pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper);
  108. static APT::PackageSet FromTask(pkgCacheFile &Cache, std::string const &pattern) {
  109. CacheSetHelper helper;
  110. return APT::PackageSet::FromTask(Cache, pattern, helper);
  111. }
  112. /** \brief returns all packages in the cache whose name matchs a given pattern
  113. A simple helper responsible for executing a regular expression on all
  114. package names in the cache. Optional it prints a a notice about the
  115. packages chosen cause of the given package.
  116. \param Cache the packages are in
  117. \param pattern regular expression for package names
  118. \param out stream to print the notice to */
  119. static APT::PackageSet FromRegEx(pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper);
  120. static APT::PackageSet FromRegEx(pkgCacheFile &Cache, std::string const &pattern) {
  121. CacheSetHelper helper;
  122. return APT::PackageSet::FromRegEx(Cache, pattern, helper);
  123. }
  124. /** \brief returns all packages specified by a string
  125. \param Cache the packages are in
  126. \param string String the package name(s) should be extracted from
  127. \param out stream to print various notices to */
  128. static APT::PackageSet FromString(pkgCacheFile &Cache, std::string const &string, CacheSetHelper &helper);
  129. static APT::PackageSet FromString(pkgCacheFile &Cache, std::string const &string) {
  130. CacheSetHelper helper;
  131. return APT::PackageSet::FromString(Cache, string, helper);
  132. }
  133. /** \brief returns a package specified by a string
  134. \param Cache the package is in
  135. \param string String the package name should be extracted from
  136. \param out stream to print various notices to */
  137. static pkgCache::PkgIterator FromName(pkgCacheFile &Cache, std::string const &string, CacheSetHelper &helper);
  138. static pkgCache::PkgIterator FromName(pkgCacheFile &Cache, std::string const &string) {
  139. CacheSetHelper helper;
  140. return APT::PackageSet::FromName(Cache, string, helper);
  141. }
  142. /** \brief returns all packages specified on the commandline
  143. Get all package names from the commandline and executes regex's if needed.
  144. No special package command is supported, just plain names.
  145. \param Cache the packages are in
  146. \param cmdline Command line the package names should be extracted from
  147. \param out stream to print various notices to */
  148. static APT::PackageSet FromCommandLine(pkgCacheFile &Cache, const char **cmdline, CacheSetHelper &helper);
  149. static APT::PackageSet FromCommandLine(pkgCacheFile &Cache, const char **cmdline) {
  150. CacheSetHelper helper;
  151. return APT::PackageSet::FromCommandLine(Cache, cmdline, helper);
  152. }
  153. struct Modifier {
  154. enum Position { NONE, PREFIX, POSTFIX };
  155. unsigned short ID;
  156. const char * const Alias;
  157. Position Pos;
  158. Modifier (unsigned short const &id, const char * const alias, Position const &pos) : ID(id), Alias(alias), Pos(pos) {};
  159. };
  160. static std::map<unsigned short, PackageSet> GroupedFromCommandLine(
  161. pkgCacheFile &Cache, const char **cmdline,
  162. std::list<PackageSet::Modifier> const &mods,
  163. unsigned short const &fallback, CacheSetHelper &helper);
  164. static std::map<unsigned short, PackageSet> GroupedFromCommandLine(
  165. pkgCacheFile &Cache, const char **cmdline,
  166. std::list<PackageSet::Modifier> const &mods,
  167. unsigned short const &fallback) {
  168. CacheSetHelper helper;
  169. return APT::PackageSet::GroupedFromCommandLine(Cache, cmdline,
  170. mods, fallback, helper);
  171. }
  172. /*}}}*/
  173. }; /*}}}*/
  174. class VersionSet : public std::set<pkgCache::VerIterator> { /*{{{*/
  175. /** \class APT::VersionSet
  176. Simple wrapper around a std::set to provide a similar interface to
  177. a set of versions as to the complete set of all versions in the
  178. pkgCache. */
  179. public: /*{{{*/
  180. /** \brief smell like a pkgCache::VerIterator */
  181. class const_iterator : public std::set<pkgCache::VerIterator>::const_iterator {/*{{{*/
  182. public:
  183. const_iterator(std::set<pkgCache::VerIterator>::const_iterator x) :
  184. std::set<pkgCache::VerIterator>::const_iterator(x) {}
  185. operator pkgCache::VerIterator(void) { return **this; }
  186. inline pkgCache *Cache() const { return (**this).Cache(); };
  187. inline unsigned long Index() const {return (**this).Index();};
  188. // we have only valid iterators here
  189. inline bool end() const { return false; };
  190. inline pkgCache::Version const * operator->() const {
  191. return &***this;
  192. };
  193. inline int CompareVer(const pkgCache::VerIterator &B) const { return (**this).CompareVer(B); };
  194. inline const char *VerStr() const { return (**this).VerStr(); };
  195. inline const char *Section() const { return (**this).Section(); };
  196. inline const char *Arch() const { return (**this).Arch(); };
  197. inline const char *Arch(bool const pseudo) const { return (**this).Arch(pseudo); };
  198. inline pkgCache::PkgIterator ParentPkg() const { return (**this).ParentPkg(); };
  199. inline pkgCache::DescIterator DescriptionList() const { return (**this).DescriptionList(); };
  200. inline pkgCache::DescIterator TranslatedDescription() const { return (**this).TranslatedDescription(); };
  201. inline pkgCache::DepIterator DependsList() const { return (**this).DependsList(); };
  202. inline pkgCache::PrvIterator ProvidesList() const { return (**this).ProvidesList(); };
  203. inline pkgCache::VerFileIterator FileList() const { return (**this).FileList(); };
  204. inline bool Downloadable() const { return (**this).Downloadable(); };
  205. inline const char *PriorityType() const { return (**this).PriorityType(); };
  206. inline string RelStr() const { return (**this).RelStr(); };
  207. inline bool Automatic() const { return (**this).Automatic(); };
  208. inline bool Pseudo() const { return (**this).Pseudo(); };
  209. inline pkgCache::VerFileIterator NewestFile() const { return (**this).NewestFile(); };
  210. };
  211. /*}}}*/
  212. // 103. set::iterator is required to be modifiable, but this allows modification of keys
  213. typedef APT::VersionSet::const_iterator iterator;
  214. using std::set<pkgCache::VerIterator>::insert;
  215. inline void insert(pkgCache::VerIterator const &V) { if (V.end() == false) std::set<pkgCache::VerIterator>::insert(V); };
  216. inline void insert(VersionSet const &verset) { insert(verset.begin(), verset.end()); };
  217. /** \brief specifies which version(s) will be returned if non is given */
  218. enum Version {
  219. /** All versions */
  220. ALL,
  221. /** Candidate and installed version */
  222. CANDANDINST,
  223. /** Candidate version */
  224. CANDIDATE,
  225. /** Installed version */
  226. INSTALLED,
  227. /** Candidate or if non installed version */
  228. CANDINST,
  229. /** Installed or if non candidate version */
  230. INSTCAND,
  231. /** Newest version */
  232. NEWEST
  233. };
  234. /** \brief returns all versions specified on the commandline
  235. Get all versions from the commandline, uses given default version if
  236. non specifically requested and executes regex's if needed on names.
  237. \param Cache the packages and versions are in
  238. \param cmdline Command line the versions should be extracted from
  239. \param out stream to print various notices to */
  240. static APT::VersionSet FromCommandLine(pkgCacheFile &Cache, const char **cmdline,
  241. APT::VersionSet::Version const &fallback, CacheSetHelper &helper);
  242. static APT::VersionSet FromCommandLine(pkgCacheFile &Cache, const char **cmdline,
  243. APT::VersionSet::Version const &fallback) {
  244. CacheSetHelper helper;
  245. return APT::VersionSet::FromCommandLine(Cache, cmdline, fallback, helper);
  246. }
  247. static APT::VersionSet FromCommandLine(pkgCacheFile &Cache, const char **cmdline) {
  248. return APT::VersionSet::FromCommandLine(Cache, cmdline, CANDINST);
  249. }
  250. static APT::VersionSet FromString(pkgCacheFile &Cache, std::string pkg,
  251. APT::VersionSet::Version const &fallback, CacheSetHelper &helper,
  252. bool const &onlyFromName = false);
  253. static APT::VersionSet FromString(pkgCacheFile &Cache, std::string pkg,
  254. APT::VersionSet::Version const &fallback) {
  255. CacheSetHelper helper;
  256. return APT::VersionSet::FromString(Cache, pkg, fallback, helper);
  257. }
  258. static APT::VersionSet FromString(pkgCacheFile &Cache, std::string pkg) {
  259. return APT::VersionSet::FromString(Cache, pkg, CANDINST);
  260. }
  261. /** \brief returns all versions specified for the package
  262. \param Cache the package and versions are in
  263. \param P the package in question
  264. \param fallback the version(s) you want to get
  265. \param helper the helper used for display and error handling */
  266. static VersionSet FromPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &P,
  267. VersionSet::Version const &fallback, CacheSetHelper &helper);
  268. struct Modifier {
  269. enum Position { NONE, PREFIX, POSTFIX };
  270. unsigned short ID;
  271. const char * const Alias;
  272. Position Pos;
  273. VersionSet::Version SelectVersion;
  274. Modifier (unsigned short const &id, const char * const alias, Position const &pos,
  275. VersionSet::Version const &select) : ID(id), Alias(alias), Pos(pos),
  276. SelectVersion(select) {};
  277. };
  278. static std::map<unsigned short, VersionSet> GroupedFromCommandLine(
  279. pkgCacheFile &Cache, const char **cmdline,
  280. std::list<VersionSet::Modifier> const &mods,
  281. unsigned short const &fallback, CacheSetHelper &helper);
  282. static std::map<unsigned short, VersionSet> GroupedFromCommandLine(
  283. pkgCacheFile &Cache, const char **cmdline,
  284. std::list<VersionSet::Modifier> const &mods,
  285. unsigned short const &fallback) {
  286. CacheSetHelper helper;
  287. return APT::VersionSet::GroupedFromCommandLine(Cache, cmdline,
  288. mods, fallback, helper);
  289. }
  290. /*}}}*/
  291. protected: /*{{{*/
  292. /** \brief returns the candidate version of the package
  293. \param Cache to be used to query for information
  294. \param Pkg we want the candidate version from this package */
  295. static pkgCache::VerIterator getCandidateVer(pkgCacheFile &Cache,
  296. pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper);
  297. /** \brief returns the installed version of the package
  298. \param Cache to be used to query for information
  299. \param Pkg we want the installed version from this package */
  300. static pkgCache::VerIterator getInstalledVer(pkgCacheFile &Cache,
  301. pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper);
  302. /*}}}*/
  303. }; /*}}}*/
  304. }
  305. #endif