cacheset.h 16 KB

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