cacheset.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 helper responsible for error and message handling */
  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 helper responsible for error and message handling */
  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 helper responsible for error and message handling */
  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 helper responsible for error and message handling */
  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 helper responsible for error and message handling */
  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. /** \brief group packages by a action modifiers
  161. At some point it is needed to get from the same commandline
  162. different package sets grouped by a modifier. Take
  163. apt-get install apt awesome-
  164. as an example.
  165. \param Cache the packages are in
  166. \param cmdline Command line the package names should be extracted from
  167. \param mods list of modifiers the method should accept
  168. \param fallback the default modifier group for a package
  169. \param helper responsible for error and message handling */
  170. static std::map<unsigned short, PackageSet> GroupedFromCommandLine(
  171. pkgCacheFile &Cache, const char **cmdline,
  172. std::list<PackageSet::Modifier> const &mods,
  173. unsigned short const &fallback, CacheSetHelper &helper);
  174. static std::map<unsigned short, PackageSet> GroupedFromCommandLine(
  175. pkgCacheFile &Cache, const char **cmdline,
  176. std::list<PackageSet::Modifier> const &mods,
  177. unsigned short const &fallback) {
  178. CacheSetHelper helper;
  179. return APT::PackageSet::GroupedFromCommandLine(Cache, cmdline,
  180. mods, fallback, helper);
  181. }
  182. enum Constructor { UNKNOWN, REGEX, TASK };
  183. Constructor getConstructor() const { return ConstructedBy; };
  184. PackageSet() : ConstructedBy(UNKNOWN) {};
  185. PackageSet(Constructor const &by) : ConstructedBy(by) {};
  186. /*}}}*/
  187. private: /*{{{*/
  188. Constructor ConstructedBy;
  189. /*}}}*/
  190. }; /*}}}*/
  191. class VersionSet : public std::set<pkgCache::VerIterator> { /*{{{*/
  192. /** \class APT::VersionSet
  193. Simple wrapper around a std::set to provide a similar interface to
  194. a set of versions as to the complete set of all versions in the
  195. pkgCache. */
  196. public: /*{{{*/
  197. /** \brief smell like a pkgCache::VerIterator */
  198. class const_iterator : public std::set<pkgCache::VerIterator>::const_iterator {/*{{{*/
  199. public:
  200. const_iterator(std::set<pkgCache::VerIterator>::const_iterator x) :
  201. std::set<pkgCache::VerIterator>::const_iterator(x) {}
  202. operator pkgCache::VerIterator(void) { return **this; }
  203. inline pkgCache *Cache() const { return (**this).Cache(); };
  204. inline unsigned long Index() const {return (**this).Index();};
  205. // we have only valid iterators here
  206. inline bool end() const { return false; };
  207. inline pkgCache::Version const * operator->() const {
  208. return &***this;
  209. };
  210. inline int CompareVer(const pkgCache::VerIterator &B) const { return (**this).CompareVer(B); };
  211. inline const char *VerStr() const { return (**this).VerStr(); };
  212. inline const char *Section() const { return (**this).Section(); };
  213. inline const char *Arch() const { return (**this).Arch(); };
  214. inline const char *Arch(bool const pseudo) const { return (**this).Arch(pseudo); };
  215. inline pkgCache::PkgIterator ParentPkg() const { return (**this).ParentPkg(); };
  216. inline pkgCache::DescIterator DescriptionList() const { return (**this).DescriptionList(); };
  217. inline pkgCache::DescIterator TranslatedDescription() const { return (**this).TranslatedDescription(); };
  218. inline pkgCache::DepIterator DependsList() const { return (**this).DependsList(); };
  219. inline pkgCache::PrvIterator ProvidesList() const { return (**this).ProvidesList(); };
  220. inline pkgCache::VerFileIterator FileList() const { return (**this).FileList(); };
  221. inline bool Downloadable() const { return (**this).Downloadable(); };
  222. inline const char *PriorityType() const { return (**this).PriorityType(); };
  223. inline string RelStr() const { return (**this).RelStr(); };
  224. inline bool Automatic() const { return (**this).Automatic(); };
  225. inline bool Pseudo() const { return (**this).Pseudo(); };
  226. inline pkgCache::VerFileIterator NewestFile() const { return (**this).NewestFile(); };
  227. };
  228. /*}}}*/
  229. // 103. set::iterator is required to be modifiable, but this allows modification of keys
  230. typedef APT::VersionSet::const_iterator iterator;
  231. using std::set<pkgCache::VerIterator>::insert;
  232. inline void insert(pkgCache::VerIterator const &V) { if (V.end() == false) std::set<pkgCache::VerIterator>::insert(V); };
  233. inline void insert(VersionSet const &verset) { insert(verset.begin(), verset.end()); };
  234. /** \brief specifies which version(s) will be returned if non is given */
  235. enum Version {
  236. /** All versions */
  237. ALL,
  238. /** Candidate and installed version */
  239. CANDANDINST,
  240. /** Candidate version */
  241. CANDIDATE,
  242. /** Installed version */
  243. INSTALLED,
  244. /** Candidate or if non installed version */
  245. CANDINST,
  246. /** Installed or if non candidate version */
  247. INSTCAND,
  248. /** Newest version */
  249. NEWEST
  250. };
  251. /** \brief returns all versions specified on the commandline
  252. Get all versions from the commandline, uses given default version if
  253. non specifically requested and executes regex's if needed on names.
  254. \param Cache the packages and versions are in
  255. \param cmdline Command line the versions should be extracted from
  256. \param helper responsible for error and message handling */
  257. static APT::VersionSet FromCommandLine(pkgCacheFile &Cache, const char **cmdline,
  258. APT::VersionSet::Version const &fallback, CacheSetHelper &helper);
  259. static APT::VersionSet FromCommandLine(pkgCacheFile &Cache, const char **cmdline,
  260. APT::VersionSet::Version const &fallback) {
  261. CacheSetHelper helper;
  262. return APT::VersionSet::FromCommandLine(Cache, cmdline, fallback, helper);
  263. }
  264. static APT::VersionSet FromCommandLine(pkgCacheFile &Cache, const char **cmdline) {
  265. return APT::VersionSet::FromCommandLine(Cache, cmdline, CANDINST);
  266. }
  267. static APT::VersionSet FromString(pkgCacheFile &Cache, std::string pkg,
  268. APT::VersionSet::Version const &fallback, CacheSetHelper &helper,
  269. bool const &onlyFromName = false);
  270. static APT::VersionSet FromString(pkgCacheFile &Cache, std::string pkg,
  271. APT::VersionSet::Version const &fallback) {
  272. CacheSetHelper helper;
  273. return APT::VersionSet::FromString(Cache, pkg, fallback, helper);
  274. }
  275. static APT::VersionSet FromString(pkgCacheFile &Cache, std::string pkg) {
  276. return APT::VersionSet::FromString(Cache, pkg, CANDINST);
  277. }
  278. /** \brief returns all versions specified for the package
  279. \param Cache the package and versions are in
  280. \param P the package in question
  281. \param fallback the version(s) you want to get
  282. \param helper the helper used for display and error handling */
  283. static APT::VersionSet FromPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &P,
  284. VersionSet::Version const &fallback, CacheSetHelper &helper);
  285. static APT::VersionSet FromPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &P,
  286. APT::VersionSet::Version const &fallback) {
  287. CacheSetHelper helper;
  288. return APT::VersionSet::FromPackage(Cache, P, fallback, helper);
  289. }
  290. static APT::VersionSet FromPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &P) {
  291. return APT::VersionSet::FromPackage(Cache, P, CANDINST);
  292. }
  293. struct Modifier {
  294. enum Position { NONE, PREFIX, POSTFIX };
  295. unsigned short ID;
  296. const char * const Alias;
  297. Position Pos;
  298. VersionSet::Version SelectVersion;
  299. Modifier (unsigned short const &id, const char * const alias, Position const &pos,
  300. VersionSet::Version const &select) : ID(id), Alias(alias), Pos(pos),
  301. SelectVersion(select) {};
  302. };
  303. static std::map<unsigned short, VersionSet> GroupedFromCommandLine(
  304. pkgCacheFile &Cache, const char **cmdline,
  305. std::list<VersionSet::Modifier> const &mods,
  306. unsigned short const &fallback, CacheSetHelper &helper);
  307. static std::map<unsigned short, VersionSet> GroupedFromCommandLine(
  308. pkgCacheFile &Cache, const char **cmdline,
  309. std::list<VersionSet::Modifier> const &mods,
  310. unsigned short const &fallback) {
  311. CacheSetHelper helper;
  312. return APT::VersionSet::GroupedFromCommandLine(Cache, cmdline,
  313. mods, fallback, helper);
  314. }
  315. /*}}}*/
  316. protected: /*{{{*/
  317. /** \brief returns the candidate version of the package
  318. \param Cache to be used to query for information
  319. \param Pkg we want the candidate version from this package */
  320. static pkgCache::VerIterator getCandidateVer(pkgCacheFile &Cache,
  321. pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper);
  322. /** \brief returns the installed version of the package
  323. \param Cache to be used to query for information
  324. \param Pkg we want the installed version from this package */
  325. static pkgCache::VerIterator getInstalledVer(pkgCacheFile &Cache,
  326. pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper);
  327. /*}}}*/
  328. }; /*}}}*/
  329. }
  330. #endif