cacheset.h 17 KB

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