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