You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

291 lines
13 KiB

  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 : public std::set<pkgCache::PkgIterator> { /*{{{*/
  22. /** \class APT::PackageSet
  23. Simple wrapper around a std::set to provide a similar interface to
  24. a set of packages as to the complete set of all packages in the
  25. pkgCache. */
  26. public: /*{{{*/
  27. /** \brief smell like a pkgCache::PkgIterator */
  28. class const_iterator : public std::set<pkgCache::PkgIterator>::const_iterator {/*{{{*/
  29. public:
  30. const_iterator(std::set<pkgCache::PkgIterator>::const_iterator x) :
  31. std::set<pkgCache::PkgIterator>::const_iterator(x) {}
  32. operator pkgCache::PkgIterator(void) { return **this; }
  33. inline const char *Name() const {return (**this).Name(); }
  34. inline std::string FullName(bool const &Pretty) const { return (**this).FullName(Pretty); }
  35. inline std::string FullName() const { return (**this).FullName(); }
  36. inline const char *Section() const {return (**this).Section(); }
  37. inline bool Purge() const {return (**this).Purge(); }
  38. inline const char *Arch() const {return (**this).Arch(); }
  39. inline pkgCache::GrpIterator Group() const { return (**this).Group(); }
  40. inline pkgCache::VerIterator VersionList() const { return (**this).VersionList(); }
  41. inline pkgCache::VerIterator CurrentVer() const { return (**this).CurrentVer(); }
  42. inline pkgCache::DepIterator RevDependsList() const { return (**this).RevDependsList(); }
  43. inline pkgCache::PrvIterator ProvidesList() const { return (**this).ProvidesList(); }
  44. inline pkgCache::PkgIterator::OkState State() const { return (**this).State(); }
  45. inline const char *CandVersion() const { return (**this).CandVersion(); }
  46. inline const char *CurVersion() const { return (**this).CurVersion(); }
  47. inline pkgCache *Cache() const { return (**this).Cache(); };
  48. inline unsigned long Index() const {return (**this).Index();};
  49. // we have only valid iterators here
  50. inline bool end() const { return false; };
  51. friend std::ostream& operator<<(std::ostream& out, const_iterator i) { return operator<<(out, (*i)); }
  52. inline pkgCache::Package const * operator->() const {
  53. return &***this;
  54. };
  55. };
  56. // 103. set::iterator is required to be modifiable, but this allows modification of keys
  57. typedef APT::PackageSet::const_iterator iterator;
  58. /*}}}*/
  59. using std::set<pkgCache::PkgIterator>::insert;
  60. inline void insert(pkgCache::PkgIterator const &P) { if (P.end() == false) std::set<pkgCache::PkgIterator>::insert(P); };
  61. /** \brief returns all packages in the cache who belong to the given task
  62. A simple helper responsible for search for all members of a task
  63. in the cache. Optional it prints a a notice about the
  64. packages chosen cause of the given task.
  65. \param Cache the packages are in
  66. \param pattern name of the task
  67. \param out stream to print the notice to */
  68. static APT::PackageSet FromTask(pkgCacheFile &Cache, std::string pattern, std::ostream &out);
  69. static APT::PackageSet FromTask(pkgCacheFile &Cache, std::string const &pattern) {
  70. std::ostream out (std::ofstream("/dev/null").rdbuf());
  71. return APT::PackageSet::FromTask(Cache, pattern, out);
  72. }
  73. /** \brief returns all packages in the cache whose name matchs a given pattern
  74. A simple helper responsible for executing a regular expression on all
  75. package names in the cache. Optional it prints a a notice about the
  76. packages chosen cause of the given package.
  77. \param Cache the packages are in
  78. \param pattern regular expression for package names
  79. \param out stream to print the notice to */
  80. static APT::PackageSet FromRegEx(pkgCacheFile &Cache, std::string pattern, std::ostream &out);
  81. static APT::PackageSet FromRegEx(pkgCacheFile &Cache, std::string const &pattern) {
  82. std::ostream out (std::ofstream("/dev/null").rdbuf());
  83. return APT::PackageSet::FromRegEx(Cache, pattern, out);
  84. }
  85. /** \brief returns all packages specified by a string
  86. \param Cache the packages are in
  87. \param string String the package name(s) should be extracted from
  88. \param out stream to print various notices to */
  89. static APT::PackageSet FromString(pkgCacheFile &Cache, std::string const &string, std::ostream &out);
  90. static APT::PackageSet FromString(pkgCacheFile &Cache, std::string const &string) {
  91. std::ostream out (std::ofstream("/dev/null").rdbuf());
  92. return APT::PackageSet::FromString(Cache, string, out);
  93. }
  94. /** \brief returns all packages specified on the commandline
  95. Get all package names from the commandline and executes regex's if needed.
  96. No special package command is supported, just plain names.
  97. \param Cache the packages are in
  98. \param cmdline Command line the package names should be extracted from
  99. \param out stream to print various notices to */
  100. static APT::PackageSet FromCommandLine(pkgCacheFile &Cache, const char **cmdline, std::ostream &out);
  101. static APT::PackageSet FromCommandLine(pkgCacheFile &Cache, const char **cmdline) {
  102. std::ostream out (std::ofstream("/dev/null").rdbuf());
  103. return APT::PackageSet::FromCommandLine(Cache, cmdline, out);
  104. }
  105. struct Modifier {
  106. enum Position { NONE, PREFIX, POSTFIX };
  107. unsigned short ID;
  108. const char * const Alias;
  109. Position Pos;
  110. Modifier (unsigned short const &id, const char * const alias, Position const &pos) : ID(id), Alias(alias), Pos(pos) {};
  111. };
  112. static std::map<unsigned short, PackageSet> GroupedFromCommandLine(
  113. pkgCacheFile &Cache, const char **cmdline,
  114. std::list<PackageSet::Modifier> const &mods,
  115. unsigned short const &fallback, std::ostream &out);
  116. static std::map<unsigned short, PackageSet> GroupedFromCommandLine(
  117. pkgCacheFile &Cache, const char **cmdline,
  118. std::list<PackageSet::Modifier> const &mods,
  119. unsigned short const &fallback) {
  120. std::ostream out (std::ofstream("/dev/null").rdbuf());
  121. return APT::PackageSet::GroupedFromCommandLine(Cache, cmdline,
  122. mods, fallback, out);
  123. }
  124. /*}}}*/
  125. }; /*}}}*/
  126. class VersionSet : public std::set<pkgCache::VerIterator> { /*{{{*/
  127. /** \class APT::VersionSet
  128. Simple wrapper around a std::set to provide a similar interface to
  129. a set of versions as to the complete set of all versions in the
  130. pkgCache. */
  131. public: /*{{{*/
  132. /** \brief smell like a pkgCache::VerIterator */
  133. class const_iterator : public std::set<pkgCache::VerIterator>::const_iterator {/*{{{*/
  134. public:
  135. const_iterator(std::set<pkgCache::VerIterator>::const_iterator x) :
  136. std::set<pkgCache::VerIterator>::const_iterator(x) {}
  137. operator pkgCache::VerIterator(void) { return **this; }
  138. inline pkgCache *Cache() const { return (**this).Cache(); };
  139. inline unsigned long Index() const {return (**this).Index();};
  140. // we have only valid iterators here
  141. inline bool end() const { return false; };
  142. inline pkgCache::Version const * operator->() const {
  143. return &***this;
  144. };
  145. inline int CompareVer(const pkgCache::VerIterator &B) const { return (**this).CompareVer(B); };
  146. inline const char *VerStr() const { return (**this).VerStr(); };
  147. inline const char *Section() const { return (**this).Section(); };
  148. inline const char *Arch() const { return (**this).Arch(); };
  149. inline const char *Arch(bool const pseudo) const { return (**this).Arch(pseudo); };
  150. inline pkgCache::PkgIterator ParentPkg() const { return (**this).ParentPkg(); };
  151. inline pkgCache::DescIterator DescriptionList() const { return (**this).DescriptionList(); };
  152. inline pkgCache::DescIterator TranslatedDescription() const { return (**this).TranslatedDescription(); };
  153. inline pkgCache::DepIterator DependsList() const { return (**this).DependsList(); };
  154. inline pkgCache::PrvIterator ProvidesList() const { return (**this).ProvidesList(); };
  155. inline pkgCache::VerFileIterator FileList() const { return (**this).FileList(); };
  156. inline bool Downloadable() const { return (**this).Downloadable(); };
  157. inline const char *PriorityType() const { return (**this).PriorityType(); };
  158. inline string RelStr() const { return (**this).RelStr(); };
  159. inline bool Automatic() const { return (**this).Automatic(); };
  160. inline bool Pseudo() const { return (**this).Pseudo(); };
  161. inline pkgCache::VerFileIterator NewestFile() const { return (**this).NewestFile(); };
  162. };
  163. /*}}}*/
  164. // 103. set::iterator is required to be modifiable, but this allows modification of keys
  165. typedef APT::VersionSet::const_iterator iterator;
  166. using std::set<pkgCache::VerIterator>::insert;
  167. inline void insert(pkgCache::VerIterator const &V) { if (V.end() == false) std::set<pkgCache::VerIterator>::insert(V); };
  168. /** \brief specifies which version(s) will be returned if non is given */
  169. enum Version {
  170. /** All versions */
  171. ALL,
  172. /** Candidate and installed version */
  173. CANDANDINST,
  174. /** Candidate version */
  175. CANDIDATE,
  176. /** Installed version */
  177. INSTALLED,
  178. /** Candidate or if non installed version */
  179. CANDINST,
  180. /** Installed or if non candidate version */
  181. INSTCAND,
  182. /** Newest version */
  183. NEWEST
  184. };
  185. /** \brief returns all versions specified on the commandline
  186. Get all versions from the commandline, uses given default version if
  187. non specifically requested and executes regex's if needed on names.
  188. \param Cache the packages and versions are in
  189. \param cmdline Command line the versions should be extracted from
  190. \param out stream to print various notices to */
  191. static APT::VersionSet FromCommandLine(pkgCacheFile &Cache, const char **cmdline,
  192. APT::VersionSet::Version const &fallback, std::ostream &out);
  193. static APT::VersionSet FromCommandLine(pkgCacheFile &Cache, const char **cmdline,
  194. APT::VersionSet::Version const &fallback) {
  195. std::ostream out (std::ofstream("/dev/null").rdbuf());
  196. return APT::VersionSet::FromCommandLine(Cache, cmdline, fallback, out);
  197. }
  198. static APT::VersionSet FromCommandLine(pkgCacheFile &Cache, const char **cmdline) {
  199. return APT::VersionSet::FromCommandLine(Cache, cmdline, CANDINST);
  200. }
  201. static APT::VersionSet FromString(pkgCacheFile &Cache, std::string pkg,
  202. APT::VersionSet::Version const &fallback, std::ostream &out);
  203. static APT::VersionSet FromString(pkgCacheFile &Cache, std::string pkg,
  204. APT::VersionSet::Version const &fallback) {
  205. std::ostream out (std::ofstream("/dev/null").rdbuf());
  206. return APT::VersionSet::FromString(Cache, pkg, fallback, out);
  207. }
  208. static APT::VersionSet FromString(pkgCacheFile &Cache, std::string pkg) {
  209. return APT::VersionSet::FromString(Cache, pkg, CANDINST);
  210. }
  211. struct Modifier {
  212. enum Position { NONE, PREFIX, POSTFIX };
  213. unsigned short ID;
  214. const char * const Alias;
  215. Position Pos;
  216. VersionSet::Version SelectVersion;
  217. Modifier (unsigned short const &id, const char * const alias, Position const &pos,
  218. VersionSet::Version const &select) : ID(id), Alias(alias), Pos(pos),
  219. SelectVersion(select) {};
  220. };
  221. static std::map<unsigned short, VersionSet> GroupedFromCommandLine(
  222. pkgCacheFile &Cache, const char **cmdline,
  223. std::list<VersionSet::Modifier> const &mods,
  224. unsigned short const &fallback, std::ostream &out);
  225. static std::map<unsigned short, VersionSet> GroupedFromCommandLine(
  226. pkgCacheFile &Cache, const char **cmdline,
  227. std::list<VersionSet::Modifier> const &mods,
  228. unsigned short const &fallback) {
  229. std::ostream out (std::ofstream("/dev/null").rdbuf());
  230. return APT::VersionSet::GroupedFromCommandLine(Cache, cmdline,
  231. mods, fallback, out);
  232. }
  233. /*}}}*/
  234. protected: /*{{{*/
  235. /** \brief returns the candidate version of the package
  236. \param Cache to be used to query for information
  237. \param Pkg we want the candidate version from this package
  238. \param AllowError add an error to the stack if not */
  239. static pkgCache::VerIterator getCandidateVer(pkgCacheFile &Cache,
  240. pkgCache::PkgIterator const &Pkg, bool const &AllowError = false);
  241. /** \brief returns the installed version of the package
  242. \param Cache to be used to query for information
  243. \param Pkg we want the installed version from this package
  244. \param AllowError add an error to the stack if not */
  245. static pkgCache::VerIterator getInstalledVer(pkgCacheFile &Cache,
  246. pkgCache::PkgIterator const &Pkg, bool const &AllowError = false);
  247. static bool AddSelectedVersion(pkgCacheFile &Cache, VersionSet &verset,
  248. pkgCache::PkgIterator const &P, VersionSet::Version const &fallback,
  249. bool const &AllowError = false);
  250. /*}}}*/
  251. }; /*}}}*/
  252. }
  253. #endif