]> git.saurik.com Git - apt.git/blob - apt-pkg/cacheset.h
DropPrivs: Do not use an invalid return check for setgroups()
[apt.git] / apt-pkg / cacheset.h
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 <fstream>
13 #include <map>
14 #include <set>
15 #include <list>
16 #include <string>
17 #include <iterator>
18
19 #include <stddef.h>
20
21 #include <apt-pkg/error.h>
22 #include <apt-pkg/pkgcache.h>
23 #include <apt-pkg/cacheiterators.h>
24
25 #ifndef APT_8_CLEANER_HEADERS
26 #include <apt-pkg/cachefile.h>
27 #endif
28 #ifndef APT_10_CLEANER_HEADERS
29 #include <iostream>
30 #endif
31 /*}}}*/
32
33 class pkgCacheFile;
34
35 namespace APT {
36 class PackageContainerInterface;
37 class VersionContainerInterface;
38
39 class CacheSetHelper { /*{{{*/
40 /** \class APT::CacheSetHelper
41 Simple base class with a lot of virtual methods which can be overridden
42 to alter the behavior or the output of the CacheSets.
43
44 This helper is passed around by the static methods in the CacheSets and
45 used every time they hit an error condition or something could be
46 printed out.
47 */
48 public: /*{{{*/
49 CacheSetHelper(bool const ShowError = true,
50 GlobalError::MsgType ErrorType = GlobalError::ERROR) :
51 ShowError(ShowError), ErrorType(ErrorType) {}
52 virtual ~CacheSetHelper() {}
53
54 virtual void showTaskSelection(pkgCache::PkgIterator const &pkg, std::string const &pattern);
55 virtual void showRegExSelection(pkgCache::PkgIterator const &pkg, std::string const &pattern);
56 #if (APT_PKG_MAJOR >= 4 && APT_PKG_MINOR >= 13)
57 virtual void showFnmatchSelection(pkgCache::PkgIterator const &pkg, std::string const &pattern);
58 #endif
59 virtual void showSelectedVersion(pkgCache::PkgIterator const &Pkg, pkgCache::VerIterator const Ver,
60 std::string const &ver, bool const verIsRel);
61
62 virtual void canNotFindTask(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern);
63 virtual void canNotFindRegEx(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern);
64 #if (APT_PKG_MAJOR >= 4 && APT_PKG_MINOR >= 13)
65 virtual void canNotFindFnmatch(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern);
66 #endif
67 virtual void canNotFindPackage(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string const &str);
68
69 virtual void canNotFindAllVer(VersionContainerInterface * const vci, pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg);
70 virtual void canNotFindInstCandVer(VersionContainerInterface * const vci, pkgCacheFile &Cache,
71 pkgCache::PkgIterator const &Pkg);
72 virtual void canNotFindCandInstVer(VersionContainerInterface * const vci,
73 pkgCacheFile &Cache,
74 pkgCache::PkgIterator const &Pkg);
75
76 virtual pkgCache::PkgIterator canNotFindPkgName(pkgCacheFile &Cache, std::string const &str);
77 virtual pkgCache::VerIterator canNotFindNewestVer(pkgCacheFile &Cache,
78 pkgCache::PkgIterator const &Pkg);
79 virtual pkgCache::VerIterator canNotFindCandidateVer(pkgCacheFile &Cache,
80 pkgCache::PkgIterator const &Pkg);
81 virtual pkgCache::VerIterator canNotFindInstalledVer(pkgCacheFile &Cache,
82 pkgCache::PkgIterator const &Pkg);
83
84 bool showErrors() const { return ShowError; }
85 bool showErrors(bool const newValue) { if (ShowError == newValue) return ShowError; else return ((ShowError = newValue) == false); }
86 GlobalError::MsgType errorType() const { return ErrorType; }
87 GlobalError::MsgType errorType(GlobalError::MsgType const &newValue)
88 {
89 if (ErrorType == newValue) return ErrorType;
90 else {
91 GlobalError::MsgType const &oldValue = ErrorType;
92 ErrorType = newValue;
93 return oldValue;
94 }
95 }
96
97 /*}}}*/
98 protected:
99 bool ShowError;
100 GlobalError::MsgType ErrorType;
101 }; /*}}}*/
102 class PackageContainerInterface { /*{{{*/
103 /** \class PackageContainerInterface
104
105 * Interface ensuring that all operations can be executed on the yet to
106 * define concrete PackageContainer - access to all methods is possible,
107 * but in general the wrappers provided by the PackageContainer template
108 * are nicer to use.
109
110 * This class mostly protects use from the need to write all implementation
111 * of the methods working on containers in the template */
112 public:
113 class const_iterator { /*{{{*/
114 public:
115 virtual pkgCache::PkgIterator getPkg() const = 0;
116 operator pkgCache::PkgIterator(void) const { return getPkg(); }
117
118 inline const char *Name() const {return getPkg().Name(); }
119 inline std::string FullName(bool const Pretty) const { return getPkg().FullName(Pretty); }
120 inline std::string FullName() const { return getPkg().FullName(); }
121 APT_DEPRECATED inline const char *Section() const {
122 #if __GNUC__ >= 4
123 #pragma GCC diagnostic push
124 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
125 #endif
126 return getPkg().Section();
127 #if __GNUC__ >= 4
128 #pragma GCC diagnostic pop
129 #endif
130 }
131 inline bool Purge() const {return getPkg().Purge(); }
132 inline const char *Arch() const {return getPkg().Arch(); }
133 inline pkgCache::GrpIterator Group() const { return getPkg().Group(); }
134 inline pkgCache::VerIterator VersionList() const { return getPkg().VersionList(); }
135 inline pkgCache::VerIterator CurrentVer() const { return getPkg().CurrentVer(); }
136 inline pkgCache::DepIterator RevDependsList() const { return getPkg().RevDependsList(); }
137 inline pkgCache::PrvIterator ProvidesList() const { return getPkg().ProvidesList(); }
138 inline pkgCache::PkgIterator::OkState State() const { return getPkg().State(); }
139 inline const char *CandVersion() const { return getPkg().CandVersion(); }
140 inline const char *CurVersion() const { return getPkg().CurVersion(); }
141 inline pkgCache *Cache() const { return getPkg().Cache(); }
142 inline unsigned long Index() const {return getPkg().Index();}
143 // we have only valid iterators here
144 inline bool end() const { return false; }
145
146 inline pkgCache::Package const * operator->() const {return &*getPkg();}
147 };
148 /*}}}*/
149
150 virtual bool insert(pkgCache::PkgIterator const &P) = 0;
151 virtual bool empty() const = 0;
152 virtual void clear() = 0;
153
154 enum Constructor { UNKNOWN, REGEX, TASK, FNMATCH };
155 virtual void setConstructor(Constructor const &con) = 0;
156 virtual Constructor getConstructor() const = 0;
157
158 static bool FromTask(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper);
159 static bool FromRegEx(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper);
160 static pkgCache::PkgIterator FromName(pkgCacheFile &Cache, std::string const &pattern, CacheSetHelper &helper);
161 static bool FromFnmatch(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper);
162 static bool FromGroup(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper);
163 static bool FromString(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string const &pattern, CacheSetHelper &helper);
164 static bool FromCommandLine(PackageContainerInterface * const pci, pkgCacheFile &Cache, const char **cmdline, CacheSetHelper &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
174 static bool FromModifierCommandLine(unsigned short &modID, PackageContainerInterface * const pci,
175 pkgCacheFile &Cache, const char * cmdline,
176 std::list<Modifier> const &mods, CacheSetHelper &helper);
177 };
178 /*}}}*/
179 template<class Container> class PackageContainer : public PackageContainerInterface {/*{{{*/
180 /** \class APT::PackageContainer
181
182 Simple wrapper around a container class like std::set to provide a similar
183 interface to a set of packages as to the complete set of all packages in the
184 pkgCache. */
185 Container _cont;
186 public: /*{{{*/
187 /** \brief smell like a pkgCache::PkgIterator */
188 class const_iterator : public PackageContainerInterface::const_iterator,/*{{{*/
189 public std::iterator<std::forward_iterator_tag, typename Container::const_iterator> {
190 typename Container::const_iterator _iter;
191 public:
192 const_iterator(typename Container::const_iterator i) : _iter(i) {}
193 pkgCache::PkgIterator getPkg(void) const { return *_iter; }
194 inline pkgCache::PkgIterator operator*(void) const { return *_iter; }
195 operator typename Container::const_iterator(void) const { return _iter; }
196 inline const_iterator& operator++() { ++_iter; return *this; }
197 inline const_iterator operator++(int) { const_iterator tmp(*this); operator++(); return tmp; }
198 inline bool operator!=(const_iterator const &i) const { return _iter != i._iter; }
199 inline bool operator==(const_iterator const &i) const { return _iter == i._iter; }
200 friend std::ostream& operator<<(std::ostream& out, const_iterator i) { return operator<<(out, *i); }
201 };
202 class iterator : public PackageContainerInterface::const_iterator,
203 public std::iterator<std::forward_iterator_tag, typename Container::iterator> {
204 typename Container::iterator _iter;
205 public:
206 iterator(typename Container::iterator i) : _iter(i) {}
207 pkgCache::PkgIterator getPkg(void) const { return *_iter; }
208 inline pkgCache::PkgIterator operator*(void) const { return *_iter; }
209 operator typename Container::iterator(void) const { return _iter; }
210 operator typename PackageContainer<Container>::const_iterator() { return typename PackageContainer<Container>::const_iterator(_iter); }
211 inline iterator& operator++() { ++_iter; return *this; }
212 inline iterator operator++(int) { iterator tmp(*this); operator++(); return tmp; }
213 inline bool operator!=(iterator const &i) const { return _iter != i._iter; }
214 inline bool operator==(iterator const &i) const { return _iter == i._iter; }
215 inline iterator& operator=(iterator const &i) { _iter = i._iter; return *this; }
216 inline iterator& operator=(typename Container::iterator const &i) { _iter = i; return *this; }
217 friend std::ostream& operator<<(std::ostream& out, iterator i) { return operator<<(out, *i); }
218 };
219 /*}}}*/
220
221 bool insert(pkgCache::PkgIterator const &P) { if (P.end() == true) return false; _cont.insert(P); return true; }
222 template<class Cont> void insert(PackageContainer<Cont> const &pkgcont) { _cont.insert((typename Cont::const_iterator)pkgcont.begin(), (typename Cont::const_iterator)pkgcont.end()); }
223 void insert(const_iterator begin, const_iterator end) { _cont.insert(begin, end); }
224
225 bool empty() const { return _cont.empty(); }
226 void clear() { return _cont.clear(); }
227 //FIXME: on ABI break, replace the first with the second without bool
228 void erase(iterator position) { _cont.erase((typename Container::iterator)position); }
229 iterator& erase(iterator &position, bool) { return position = _cont.erase((typename Container::iterator)position); }
230 size_t erase(const pkgCache::PkgIterator x) { return _cont.erase(x); }
231 void erase(iterator first, iterator last) { _cont.erase(first, last); }
232 size_t size() const { return _cont.size(); }
233
234 const_iterator begin() const { return const_iterator(_cont.begin()); }
235 const_iterator end() const { return const_iterator(_cont.end()); }
236 iterator begin() { return iterator(_cont.begin()); }
237 iterator end() { return iterator(_cont.end()); }
238 const_iterator find(pkgCache::PkgIterator const &P) const { return const_iterator(_cont.find(P)); }
239
240 void setConstructor(Constructor const &by) { ConstructedBy = by; }
241 Constructor getConstructor() const { return ConstructedBy; }
242
243 PackageContainer() : ConstructedBy(UNKNOWN) {}
244 PackageContainer(Constructor const &by) : ConstructedBy(by) {}
245
246 /** \brief returns all packages in the cache who belong to the given task
247
248 A simple helper responsible for search for all members of a task
249 in the cache. Optional it prints a a notice about the
250 packages chosen cause of the given task.
251 \param Cache the packages are in
252 \param pattern name of the task
253 \param helper responsible for error and message handling */
254 static PackageContainer FromTask(pkgCacheFile &Cache, std::string const &pattern, CacheSetHelper &helper) {
255 PackageContainer cont(TASK);
256 PackageContainerInterface::FromTask(&cont, Cache, pattern, helper);
257 return cont;
258 }
259 static PackageContainer FromTask(pkgCacheFile &Cache, std::string const &pattern) {
260 CacheSetHelper helper;
261 return FromTask(Cache, pattern, helper);
262 }
263
264 /** \brief returns all packages in the cache whose name matchs a given pattern
265
266 A simple helper responsible for executing a regular expression on all
267 package names in the cache. Optional it prints a a notice about the
268 packages chosen cause of the given package.
269 \param Cache the packages are in
270 \param pattern regular expression for package names
271 \param helper responsible for error and message handling */
272 static PackageContainer FromRegEx(pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper) {
273 PackageContainer cont(REGEX);
274 PackageContainerInterface::FromRegEx(&cont, Cache, pattern, helper);
275 return cont;
276 }
277
278 static PackageContainer FromRegEx(pkgCacheFile &Cache, std::string const &pattern) {
279 CacheSetHelper helper;
280 return FromRegEx(Cache, pattern, helper);
281 }
282
283 static PackageContainer FromFnmatch(pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper) {
284 PackageContainer cont(FNMATCH);
285 PackageContainerInterface::FromFnmatch(&cont, Cache, pattern, helper);
286 return cont;
287 }
288 static PackageContainer FromFnMatch(pkgCacheFile &Cache, std::string const &pattern) {
289 CacheSetHelper helper;
290 return FromFnmatch(Cache, pattern, helper);
291 }
292
293 /** \brief returns a package specified by a string
294
295 \param Cache the package is in
296 \param pattern String the package name should be extracted from
297 \param helper responsible for error and message handling */
298 static pkgCache::PkgIterator FromName(pkgCacheFile &Cache, std::string const &pattern, CacheSetHelper &helper) {
299 return PackageContainerInterface::FromName(Cache, pattern, helper);
300 }
301 static pkgCache::PkgIterator FromName(pkgCacheFile &Cache, std::string const &pattern) {
302 CacheSetHelper helper;
303 return PackageContainerInterface::FromName(Cache, pattern, helper);
304 }
305
306 /** \brief returns all packages specified by a string
307
308 \param Cache the packages are in
309 \param pattern String the package name(s) should be extracted from
310 \param helper responsible for error and message handling */
311 static PackageContainer FromString(pkgCacheFile &Cache, std::string const &pattern, CacheSetHelper &helper) {
312 PackageContainer cont;
313 PackageContainerInterface::FromString(&cont, Cache, pattern, helper);
314 return cont;
315 }
316 static PackageContainer FromString(pkgCacheFile &Cache, std::string const &pattern) {
317 CacheSetHelper helper;
318 return FromString(Cache, pattern, helper);
319 }
320
321 /** \brief returns all packages specified on the commandline
322
323 Get all package names from the commandline and executes regex's if needed.
324 No special package command is supported, just plain names.
325 \param Cache the packages are in
326 \param cmdline Command line the package names should be extracted from
327 \param helper responsible for error and message handling */
328 static PackageContainer FromCommandLine(pkgCacheFile &Cache, const char **cmdline, CacheSetHelper &helper) {
329 PackageContainer cont;
330 PackageContainerInterface::FromCommandLine(&cont, Cache, cmdline, helper);
331 return cont;
332 }
333 static PackageContainer FromCommandLine(pkgCacheFile &Cache, const char **cmdline) {
334 CacheSetHelper helper;
335 return FromCommandLine(Cache, cmdline, helper);
336 }
337
338 /** \brief group packages by a action modifiers
339
340 At some point it is needed to get from the same commandline
341 different package sets grouped by a modifier. Take
342 apt-get install apt awesome-
343 as an example.
344 \param Cache the packages are in
345 \param cmdline Command line the package names should be extracted from
346 \param mods list of modifiers the method should accept
347 \param fallback the default modifier group for a package
348 \param helper responsible for error and message handling */
349 static std::map<unsigned short, PackageContainer> GroupedFromCommandLine(
350 pkgCacheFile &Cache,
351 const char **cmdline,
352 std::list<Modifier> const &mods,
353 unsigned short const &fallback,
354 CacheSetHelper &helper) {
355 std::map<unsigned short, PackageContainer> pkgsets;
356 for (const char **I = cmdline; *I != 0; ++I) {
357 unsigned short modID = fallback;
358 PackageContainer pkgset;
359 PackageContainerInterface::FromModifierCommandLine(modID, &pkgset, Cache, *I, mods, helper);
360 pkgsets[modID].insert(pkgset);
361 }
362 return pkgsets;
363 }
364 static std::map<unsigned short, PackageContainer> GroupedFromCommandLine(
365 pkgCacheFile &Cache,
366 const char **cmdline,
367 std::list<Modifier> const &mods,
368 unsigned short const &fallback) {
369 CacheSetHelper helper;
370 return GroupedFromCommandLine(Cache, cmdline,
371 mods, fallback, helper);
372 }
373 /*}}}*/
374 private: /*{{{*/
375 Constructor ConstructedBy;
376 /*}}}*/
377 }; /*}}}*/
378
379 template<> template<class Cont> void PackageContainer<std::list<pkgCache::PkgIterator> >::insert(PackageContainer<Cont> const &pkgcont) {
380 for (typename PackageContainer<Cont>::const_iterator p = pkgcont.begin(); p != pkgcont.end(); ++p)
381 _cont.push_back(*p);
382 }
383 // these two are 'inline' as otherwise the linker has problems with seeing these untemplated
384 // specializations again and again - but we need to see them, so that library users can use them
385 template<> inline bool PackageContainer<std::list<pkgCache::PkgIterator> >::insert(pkgCache::PkgIterator const &P) {
386 if (P.end() == true)
387 return false;
388 _cont.push_back(P);
389 return true;
390 }
391 template<> inline void PackageContainer<std::list<pkgCache::PkgIterator> >::insert(const_iterator begin, const_iterator end) {
392 for (const_iterator p = begin; p != end; ++p)
393 _cont.push_back(*p);
394 }
395 typedef PackageContainer<std::set<pkgCache::PkgIterator> > PackageSet;
396 typedef PackageContainer<std::list<pkgCache::PkgIterator> > PackageList;
397
398 class VersionContainerInterface { /*{{{*/
399 /** \class APT::VersionContainerInterface
400
401 Same as APT::PackageContainerInterface, just for Versions */
402 public:
403 /** \brief smell like a pkgCache::VerIterator */
404 class const_iterator { /*{{{*/
405 public:
406 virtual pkgCache::VerIterator getVer() const = 0;
407 operator pkgCache::VerIterator(void) { return getVer(); }
408
409 inline pkgCache *Cache() const { return getVer().Cache(); }
410 inline unsigned long Index() const {return getVer().Index();}
411 inline int CompareVer(const pkgCache::VerIterator &B) const { return getVer().CompareVer(B); }
412 inline const char *VerStr() const { return getVer().VerStr(); }
413 inline const char *Section() const { return getVer().Section(); }
414 inline const char *Arch() const { return getVer().Arch(); }
415 inline pkgCache::PkgIterator ParentPkg() const { return getVer().ParentPkg(); }
416 inline pkgCache::DescIterator DescriptionList() const { return getVer().DescriptionList(); }
417 inline pkgCache::DescIterator TranslatedDescription() const { return getVer().TranslatedDescription(); }
418 inline pkgCache::DepIterator DependsList() const { return getVer().DependsList(); }
419 inline pkgCache::PrvIterator ProvidesList() const { return getVer().ProvidesList(); }
420 inline pkgCache::VerFileIterator FileList() const { return getVer().FileList(); }
421 inline bool Downloadable() const { return getVer().Downloadable(); }
422 inline const char *PriorityType() const { return getVer().PriorityType(); }
423 inline std::string RelStr() const { return getVer().RelStr(); }
424 inline bool Automatic() const { return getVer().Automatic(); }
425 inline pkgCache::VerFileIterator NewestFile() const { return getVer().NewestFile(); }
426 // we have only valid iterators here
427 inline bool end() const { return false; }
428
429 inline pkgCache::Version const * operator->() const { return &*getVer(); }
430 };
431 /*}}}*/
432
433 virtual bool insert(pkgCache::VerIterator const &V) = 0;
434 virtual bool empty() const = 0;
435 virtual void clear() = 0;
436
437 /** \brief specifies which version(s) will be returned if non is given */
438 enum Version {
439 /** All versions */
440 ALL,
441 /** Candidate and installed version */
442 CANDANDINST,
443 /** Candidate version */
444 CANDIDATE,
445 /** Installed version */
446 INSTALLED,
447 /** Candidate or if non installed version */
448 CANDINST,
449 /** Installed or if non candidate version */
450 INSTCAND,
451 /** Newest version */
452 NEWEST
453 };
454
455 struct Modifier {
456 enum Position { NONE, PREFIX, POSTFIX };
457 unsigned short ID;
458 const char * const Alias;
459 Position Pos;
460 Version SelectVersion;
461 Modifier (unsigned short const &id, const char * const alias, Position const &pos,
462 Version const &select) : ID(id), Alias(alias), Pos(pos),
463 SelectVersion(select) {}
464 };
465
466 static bool FromCommandLine(VersionContainerInterface * const vci, pkgCacheFile &Cache,
467 const char **cmdline, Version const &fallback,
468 CacheSetHelper &helper);
469
470 static bool FromString(VersionContainerInterface * const vci, pkgCacheFile &Cache,
471 std::string pkg, Version const &fallback, CacheSetHelper &helper,
472 bool const onlyFromName = false);
473
474 static bool FromPackage(VersionContainerInterface * const vci, pkgCacheFile &Cache,
475 pkgCache::PkgIterator const &P, Version const &fallback,
476 CacheSetHelper &helper);
477
478 static bool FromModifierCommandLine(unsigned short &modID,
479 VersionContainerInterface * const vci,
480 pkgCacheFile &Cache, const char * cmdline,
481 std::list<Modifier> const &mods,
482 CacheSetHelper &helper);
483
484
485 static bool FromDependency(VersionContainerInterface * const vci,
486 pkgCacheFile &Cache,
487 pkgCache::DepIterator const &D,
488 Version const &selector,
489 CacheSetHelper &helper);
490
491 protected: /*{{{*/
492
493 /** \brief returns the candidate version of the package
494
495 \param Cache to be used to query for information
496 \param Pkg we want the candidate version from this package
497 \param helper used in this container instance */
498 static pkgCache::VerIterator getCandidateVer(pkgCacheFile &Cache,
499 pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper);
500
501 /** \brief returns the installed version of the package
502
503 \param Cache to be used to query for information
504 \param Pkg we want the installed version from this package
505 \param helper used in this container instance */
506 static pkgCache::VerIterator getInstalledVer(pkgCacheFile &Cache,
507 pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper);
508 /*}}}*/
509 };
510 /*}}}*/
511 template<class Container> class VersionContainer : public VersionContainerInterface {/*{{{*/
512 /** \class APT::VersionContainer
513
514 Simple wrapper around a container class like std::set to provide a similar
515 interface to a set of versions as to the complete set of all versions in the
516 pkgCache. */
517 Container _cont;
518 public: /*{{{*/
519 /** \brief smell like a pkgCache::VerIterator */
520 class const_iterator : public VersionContainerInterface::const_iterator,
521 public std::iterator<std::forward_iterator_tag, typename Container::const_iterator> {/*{{{*/
522 typename Container::const_iterator _iter;
523 public:
524 const_iterator(typename Container::const_iterator i) : _iter(i) {}
525 pkgCache::VerIterator getVer(void) const { return *_iter; }
526 inline pkgCache::VerIterator operator*(void) const { return *_iter; }
527 operator typename Container::const_iterator(void) const { return _iter; }
528 inline const_iterator& operator++() { ++_iter; return *this; }
529 inline const_iterator operator++(int) { const_iterator tmp(*this); operator++(); return tmp; }
530 inline bool operator!=(const_iterator const &i) const { return _iter != i._iter; }
531 inline bool operator==(const_iterator const &i) const { return _iter == i._iter; }
532 friend std::ostream& operator<<(std::ostream& out, const_iterator i) { return operator<<(out, *i); }
533 };
534 class iterator : public VersionContainerInterface::const_iterator,
535 public std::iterator<std::forward_iterator_tag, typename Container::iterator> {
536 typename Container::iterator _iter;
537 public:
538 iterator(typename Container::iterator i) : _iter(i) {}
539 pkgCache::VerIterator getVer(void) const { return *_iter; }
540 inline pkgCache::VerIterator operator*(void) const { return *_iter; }
541 operator typename Container::iterator(void) const { return _iter; }
542 operator typename VersionContainer<Container>::const_iterator() { return typename VersionContainer<Container>::const_iterator(_iter); }
543 inline iterator& operator++() { ++_iter; return *this; }
544 inline iterator operator++(int) { iterator tmp(*this); operator++(); return tmp; }
545 inline bool operator!=(iterator const &i) const { return _iter != i._iter; }
546 inline bool operator==(iterator const &i) const { return _iter == i._iter; }
547 inline iterator& operator=(iterator const &i) { _iter = i._iter; return *this; }
548 inline iterator& operator=(typename Container::iterator const &i) { _iter = i; return *this; }
549 friend std::ostream& operator<<(std::ostream& out, iterator i) { return operator<<(out, *i); }
550 };
551 /*}}}*/
552
553 bool insert(pkgCache::VerIterator const &V) { if (V.end() == true) return false; _cont.insert(V); return true; }
554 template<class Cont> void insert(VersionContainer<Cont> const &vercont) { _cont.insert((typename Cont::const_iterator)vercont.begin(), (typename Cont::const_iterator)vercont.end()); }
555 void insert(const_iterator begin, const_iterator end) { _cont.insert(begin, end); }
556 bool empty() const { return _cont.empty(); }
557 void clear() { return _cont.clear(); }
558 //FIXME: on ABI break, replace the first with the second without bool
559 void erase(iterator position) { _cont.erase((typename Container::iterator)position); }
560 iterator& erase(iterator &position, bool) { return position = _cont.erase((typename Container::iterator)position); }
561 size_t erase(const pkgCache::VerIterator x) { return _cont.erase(x); }
562 void erase(iterator first, iterator last) { _cont.erase(first, last); }
563 size_t size() const { return _cont.size(); }
564
565 const_iterator begin() const { return const_iterator(_cont.begin()); }
566 const_iterator end() const { return const_iterator(_cont.end()); }
567 iterator begin() { return iterator(_cont.begin()); }
568 iterator end() { return iterator(_cont.end()); }
569 const_iterator find(pkgCache::VerIterator const &V) const { return const_iterator(_cont.find(V)); }
570
571 /** \brief returns all versions specified on the commandline
572
573 Get all versions from the commandline, uses given default version if
574 non specifically requested and executes regex's if needed on names.
575 \param Cache the packages and versions are in
576 \param cmdline Command line the versions should be extracted from
577 \param fallback version specification
578 \param helper responsible for error and message handling */
579 static VersionContainer FromCommandLine(pkgCacheFile &Cache, const char **cmdline,
580 Version const &fallback, CacheSetHelper &helper) {
581 VersionContainer vercon;
582 VersionContainerInterface::FromCommandLine(&vercon, Cache, cmdline, fallback, helper);
583 return vercon;
584 }
585 static VersionContainer FromCommandLine(pkgCacheFile &Cache, const char **cmdline,
586 Version const &fallback) {
587 CacheSetHelper helper;
588 return FromCommandLine(Cache, cmdline, fallback, helper);
589 }
590 static VersionContainer FromCommandLine(pkgCacheFile &Cache, const char **cmdline) {
591 return FromCommandLine(Cache, cmdline, CANDINST);
592 }
593
594 static VersionContainer FromString(pkgCacheFile &Cache, std::string const &pkg,
595 Version const &fallback, CacheSetHelper &helper,
596 bool const /*onlyFromName = false*/) {
597 VersionContainer vercon;
598 VersionContainerInterface::FromString(&vercon, Cache, pkg, fallback, helper);
599 return vercon;
600 }
601 static VersionContainer FromString(pkgCacheFile &Cache, std::string pkg,
602 Version const &fallback) {
603 CacheSetHelper helper;
604 return FromString(Cache, pkg, fallback, helper);
605 }
606 static VersionContainer FromString(pkgCacheFile &Cache, std::string pkg) {
607 return FromString(Cache, pkg, CANDINST);
608 }
609
610 /** \brief returns all versions specified for the package
611
612 \param Cache the package and versions are in
613 \param P the package in question
614 \param fallback the version(s) you want to get
615 \param helper the helper used for display and error handling */
616 static VersionContainer FromPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &P,
617 Version const &fallback, CacheSetHelper &helper) {
618 VersionContainer vercon;
619 VersionContainerInterface::FromPackage(&vercon, Cache, P, fallback, helper);
620 return vercon;
621 }
622 static VersionContainer FromPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &P,
623 Version const &fallback) {
624 CacheSetHelper helper;
625 return FromPackage(Cache, P, fallback, helper);
626 }
627 static VersionContainer FromPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &P) {
628 return FromPackage(Cache, P, CANDIDATE);
629 }
630
631 static std::map<unsigned short, VersionContainer> GroupedFromCommandLine(
632 pkgCacheFile &Cache,
633 const char **cmdline,
634 std::list<Modifier> const &mods,
635 unsigned short const fallback,
636 CacheSetHelper &helper) {
637 std::map<unsigned short, VersionContainer> versets;
638 for (const char **I = cmdline; *I != 0; ++I) {
639 unsigned short modID = fallback;
640 VersionContainer verset;
641 VersionContainerInterface::FromModifierCommandLine(modID, &verset, Cache, *I, mods, helper);
642 versets[modID].insert(verset);
643 }
644 return versets;
645
646 }
647 static std::map<unsigned short, VersionContainer> GroupedFromCommandLine(
648 pkgCacheFile &Cache, const char **cmdline,
649 std::list<Modifier> const &mods,
650 unsigned short const fallback) {
651 CacheSetHelper helper;
652 return GroupedFromCommandLine(Cache, cmdline,
653 mods, fallback, helper);
654 }
655
656 static VersionContainer FromDependency(pkgCacheFile &Cache, pkgCache::DepIterator const &D,
657 Version const &selector, CacheSetHelper &helper) {
658 VersionContainer vercon;
659 VersionContainerInterface::FromDependency(&vercon, Cache, D, selector, helper);
660 return vercon;
661 }
662 static VersionContainer FromDependency(pkgCacheFile &Cache, pkgCache::DepIterator const &D,
663 Version const &selector) {
664 CacheSetHelper helper;
665 return FromPackage(Cache, D, selector, helper);
666 }
667 static VersionContainer FromDependency(pkgCacheFile &Cache, pkgCache::DepIterator const &D) {
668 return FromPackage(Cache, D, CANDIDATE);
669 }
670 /*}}}*/
671 }; /*}}}*/
672
673 template<> template<class Cont> void VersionContainer<std::list<pkgCache::VerIterator> >::insert(VersionContainer<Cont> const &vercont) {
674 for (typename VersionContainer<Cont>::const_iterator v = vercont.begin(); v != vercont.end(); ++v)
675 _cont.push_back(*v);
676 }
677 // these two are 'inline' as otherwise the linker has problems with seeing these untemplated
678 // specializations again and again - but we need to see them, so that library users can use them
679 template<> inline bool VersionContainer<std::list<pkgCache::VerIterator> >::insert(pkgCache::VerIterator const &V) {
680 if (V.end() == true)
681 return false;
682 _cont.push_back(V);
683 return true;
684 }
685 template<> inline void VersionContainer<std::list<pkgCache::VerIterator> >::insert(const_iterator begin, const_iterator end) {
686 for (const_iterator v = begin; v != end; ++v)
687 _cont.push_back(*v);
688 }
689 typedef VersionContainer<std::set<pkgCache::VerIterator> > VersionSet;
690 typedef VersionContainer<std::list<pkgCache::VerIterator> > VersionList;
691 }
692 #endif