1 // -*- mode: cpp; mode: fold -*-
3 /* ######################################################################
5 Simple wrapper around a std::set to provide a similar interface to
6 a set of cache structures as to the complete set of all structures
7 in the pkgCache. Currently only Package is supported.
9 ##################################################################### */
11 // Include Files /*{{{*/
14 #include <apt-pkg/aptconfiguration.h>
15 #include <apt-pkg/cachefile.h>
16 #include <apt-pkg/cachefilter.h>
17 #include <apt-pkg/cacheset.h>
18 #include <apt-pkg/error.h>
19 #include <apt-pkg/versionmatch.h>
20 #include <apt-pkg/pkgrecords.h>
21 #include <apt-pkg/policy.h>
22 #include <apt-pkg/cacheiterators.h>
23 #include <apt-pkg/configuration.h>
24 #include <apt-pkg/depcache.h>
25 #include <apt-pkg/macros.h>
26 #include <apt-pkg/pkgcache.h>
27 #include <apt-pkg/fileutl.h>
40 // PackageFrom - selecting the appropriate method for package selection /*{{{*/
41 bool CacheSetHelper::PackageFrom(enum PkgSelector
const select
, PackageContainerInterface
* const pci
,
42 pkgCacheFile
&Cache
, std::string
const &pattern
) {
44 case UNKNOWN
: return false;
45 case REGEX
: return PackageFromRegEx(pci
, Cache
, pattern
);
46 case TASK
: return PackageFromTask(pci
, Cache
, pattern
);
47 case FNMATCH
: return PackageFromFnmatch(pci
, Cache
, pattern
);
48 case PACKAGENAME
: return PackageFromPackageName(pci
, Cache
, pattern
);
49 case STRING
: return PackageFromString(pci
, Cache
, pattern
);
54 // PackageFromTask - Return all packages in the cache from a specific task /*{{{*/
55 bool CacheSetHelper::PackageFromTask(PackageContainerInterface
* const pci
, pkgCacheFile
&Cache
, std::string pattern
) {
56 size_t const archfound
= pattern
.find_last_of(':');
57 std::string arch
= "native";
58 if (archfound
!= std::string::npos
) {
59 arch
= pattern
.substr(archfound
+1);
60 pattern
.erase(archfound
);
63 if (pattern
[pattern
.length() -1] != '^')
65 pattern
.erase(pattern
.length()-1);
67 if (unlikely(Cache
.GetPkgCache() == 0 || Cache
.GetDepCache() == 0))
70 bool const wasEmpty
= pci
->empty();
72 pci
->setConstructor(CacheSetHelper::TASK
);
75 pkgRecords
Recs(Cache
);
77 // build regexp for the task
80 snprintf(S
, sizeof(S
), "^Task:.*[, ]%s([, ]|$)", pattern
.c_str());
81 if(regcomp(&Pattern
,S
, REG_EXTENDED
| REG_NOSUB
| REG_NEWLINE
) != 0) {
82 _error
->Error("Failed to compile task regexp");
87 for (pkgCache::GrpIterator Grp
= Cache
->GrpBegin(); Grp
.end() == false; ++Grp
) {
88 pkgCache::PkgIterator Pkg
= Grp
.FindPkg(arch
);
89 if (Pkg
.end() == true)
91 pkgCache::VerIterator ver
= Cache
[Pkg
].CandidateVerIter(Cache
);
95 pkgRecords::Parser
&parser
= Recs
.Lookup(ver
.FileList());
96 const char *start
, *end
;
97 parser
.GetRec(start
,end
);
98 unsigned int const length
= end
- start
;
99 if (unlikely(length
== 0))
102 strncpy(buf
, start
, length
);
103 buf
[length
-1] = '\0';
104 if (regexec(&Pattern
, buf
, 0, 0, 0) != 0)
108 showPackageSelection(Pkg
, CacheSetHelper::TASK
, pattern
);
113 if (found
== false) {
114 canNotFindPackage(CacheSetHelper::TASK
, pci
, Cache
, pattern
);
115 pci
->setConstructor(CacheSetHelper::UNKNOWN
);
119 if (wasEmpty
== false && pci
->getConstructor() != CacheSetHelper::UNKNOWN
)
120 pci
->setConstructor(CacheSetHelper::UNKNOWN
);
125 // PackageFromRegEx - Return all packages in the cache matching a pattern /*{{{*/
126 bool CacheSetHelper::PackageFromRegEx(PackageContainerInterface
* const pci
, pkgCacheFile
&Cache
, std::string pattern
) {
127 static const char * const isregex
= ".?+*|[^$";
128 if (pattern
.find_first_of(isregex
) == std::string::npos
)
131 bool const wasEmpty
= pci
->empty();
132 if (wasEmpty
== true)
133 pci
->setConstructor(CacheSetHelper::REGEX
);
135 size_t archfound
= pattern
.find_last_of(':');
136 std::string arch
= "native";
137 if (archfound
!= std::string::npos
) {
138 arch
= pattern
.substr(archfound
+1);
139 if (arch
.find_first_of(isregex
) == std::string::npos
)
140 pattern
.erase(archfound
);
145 if (unlikely(Cache
.GetPkgCache() == 0))
148 APT::CacheFilter::PackageNameMatchesRegEx
regexfilter(pattern
);
151 for (pkgCache::GrpIterator Grp
= Cache
.GetPkgCache()->GrpBegin(); Grp
.end() == false; ++Grp
) {
152 if (regexfilter(Grp
) == false)
154 pkgCache::PkgIterator Pkg
= Grp
.FindPkg(arch
);
155 if (Pkg
.end() == true) {
156 if (archfound
== std::string::npos
)
157 Pkg
= Grp
.FindPreferredPkg(true);
158 if (Pkg
.end() == true)
163 showPackageSelection(Pkg
, CacheSetHelper::REGEX
, pattern
);
167 if (found
== false) {
168 canNotFindPackage(CacheSetHelper::REGEX
, pci
, Cache
, pattern
);
169 pci
->setConstructor(CacheSetHelper::UNKNOWN
);
173 if (wasEmpty
== false && pci
->getConstructor() != CacheSetHelper::UNKNOWN
)
174 pci
->setConstructor(CacheSetHelper::UNKNOWN
);
179 // PackageFromFnmatch - Returns the package defined by this fnmatch /*{{{*/
180 bool CacheSetHelper::PackageFromFnmatch(PackageContainerInterface
* const pci
,
181 pkgCacheFile
&Cache
, std::string pattern
)
183 static const char * const isfnmatch
= ".?*[]!";
184 if (pattern
.find_first_of(isfnmatch
) == std::string::npos
)
187 bool const wasEmpty
= pci
->empty();
188 if (wasEmpty
== true)
189 pci
->setConstructor(CacheSetHelper::FNMATCH
);
191 size_t archfound
= pattern
.find_last_of(':');
192 std::string arch
= "native";
193 if (archfound
!= std::string::npos
) {
194 arch
= pattern
.substr(archfound
+1);
195 if (arch
.find_first_of(isfnmatch
) == std::string::npos
)
196 pattern
.erase(archfound
);
201 if (unlikely(Cache
.GetPkgCache() == 0))
204 APT::CacheFilter::PackageNameMatchesFnmatch
filter(pattern
);
207 for (pkgCache::GrpIterator Grp
= Cache
.GetPkgCache()->GrpBegin(); Grp
.end() == false; ++Grp
) {
208 if (filter(Grp
) == false)
210 pkgCache::PkgIterator Pkg
= Grp
.FindPkg(arch
);
211 if (Pkg
.end() == true) {
212 if (archfound
== std::string::npos
)
213 Pkg
= Grp
.FindPreferredPkg(true);
214 if (Pkg
.end() == true)
219 showPackageSelection(Pkg
, CacheSetHelper::FNMATCH
, pattern
);
223 if (found
== false) {
224 canNotFindPackage(CacheSetHelper::FNMATCH
, pci
, Cache
, pattern
);
225 pci
->setConstructor(CacheSetHelper::UNKNOWN
);
229 if (wasEmpty
== false && pci
->getConstructor() != CacheSetHelper::UNKNOWN
)
230 pci
->setConstructor(CacheSetHelper::UNKNOWN
);
235 // PackageFromName - Returns the package defined by this string /*{{{*/
236 pkgCache::PkgIterator
CacheSetHelper::PackageFromName(pkgCacheFile
&Cache
,
237 std::string
const &str
) {
238 std::string pkg
= str
;
239 size_t archfound
= pkg
.find_last_of(':');
241 if (archfound
!= std::string::npos
) {
242 arch
= pkg
.substr(archfound
+1);
243 pkg
.erase(archfound
);
246 if (Cache
.GetPkgCache() == 0)
247 return pkgCache::PkgIterator(Cache
, 0);
249 pkgCache::PkgIterator
Pkg(Cache
, 0);
250 if (arch
.empty() == true) {
251 pkgCache::GrpIterator Grp
= Cache
.GetPkgCache()->FindGrp(pkg
);
252 if (Grp
.end() == false)
253 Pkg
= Grp
.FindPreferredPkg();
255 Pkg
= Cache
.GetPkgCache()->FindPkg(pkg
, arch
);
257 if (Pkg
.end() == true)
258 return canNotFindPkgName(Cache
, str
);
262 // PackageFromPackageName - Returns the package defined by this string /*{{{*/
263 bool CacheSetHelper::PackageFromPackageName(PackageContainerInterface
* const pci
, pkgCacheFile
&Cache
,
265 if (unlikely(Cache
.GetPkgCache() == 0))
268 std::string
const pkgstring
= pkg
;
269 size_t const archfound
= pkg
.find_last_of(':');
271 if (archfound
!= std::string::npos
) {
272 arch
= pkg
.substr(archfound
+1);
273 pkg
.erase(archfound
);
274 if (arch
== "all" || arch
== "native")
275 arch
= _config
->Find("APT::Architecture");
278 pkgCache::GrpIterator Grp
= Cache
.GetPkgCache()->FindGrp(pkg
);
279 if (Grp
.end() == false) {
280 if (arch
.empty() == true) {
281 pkgCache::PkgIterator Pkg
= Grp
.FindPreferredPkg();
282 if (Pkg
.end() == false)
289 // for 'linux-any' return the first package matching, for 'linux-*' return all matches
290 bool const isGlobal
= arch
.find('*') != std::string::npos
;
291 APT::CacheFilter::PackageArchitectureMatchesSpecification
pams(arch
);
292 for (pkgCache::PkgIterator Pkg
= Grp
.PackageList(); Pkg
.end() == false; Pkg
= Grp
.NextPkg(Pkg
)) {
293 if (pams(Pkg
) == false)
297 if (isGlobal
== false)
305 pkgCache::PkgIterator Pkg
= canNotFindPkgName(Cache
, pkgstring
);
306 if (Pkg
.end() == true)
313 // PackageFromString - Return all packages matching a specific string /*{{{*/
314 bool CacheSetHelper::PackageFromString(PackageContainerInterface
* const pci
, pkgCacheFile
&Cache
, std::string
const &str
) {
316 _error
->PushToStack();
318 if (PackageFrom(CacheSetHelper::PACKAGENAME
, pci
, Cache
, str
) == false &&
319 PackageFrom(CacheSetHelper::TASK
, pci
, Cache
, str
) == false &&
320 // FIXME: hm, hm, regexp/fnmatch incompatible?
321 PackageFrom(CacheSetHelper::FNMATCH
, pci
, Cache
, str
) == false &&
322 PackageFrom(CacheSetHelper::REGEX
, pci
, Cache
, str
) == false)
324 canNotFindPackage(CacheSetHelper::PACKAGENAME
, pci
, Cache
, str
);
329 _error
->RevertToStack();
331 _error
->MergeWithStack();
335 // PackageFromCommandLine - Return all packages specified on commandline /*{{{*/
336 bool CacheSetHelper::PackageFromCommandLine(PackageContainerInterface
* const pci
, pkgCacheFile
&Cache
, const char **cmdline
) {
338 for (const char **I
= cmdline
; *I
!= 0; ++I
)
339 found
|= PackageFrom(CacheSetHelper::PACKAGENAME
, pci
, Cache
, *I
);
343 // FromModifierCommandLine - helper doing the work for PKG:GroupedFromCommandLine /*{{{*/
344 bool CacheSetHelper::PackageFromModifierCommandLine(unsigned short &modID
, PackageContainerInterface
* const pci
,
345 pkgCacheFile
&Cache
, const char * cmdline
,
346 std::list
<PkgModifier
> const &mods
) {
347 std::string str
= cmdline
;
348 unsigned short fallback
= modID
;
349 bool modifierPresent
= false;
350 for (std::list
<PkgModifier
>::const_iterator mod
= mods
.begin();
351 mod
!= mods
.end(); ++mod
) {
352 size_t const alength
= strlen(mod
->Alias
);
354 case PkgModifier::POSTFIX
:
355 if (str
.compare(str
.length() - alength
, alength
,
356 mod
->Alias
, 0, alength
) != 0)
358 str
.erase(str
.length() - alength
);
361 case PkgModifier::PREFIX
:
363 case PkgModifier::NONE
:
366 modifierPresent
= true;
369 if (modifierPresent
== true) {
370 bool const errors
= showErrors(false);
371 bool const found
= PackageFrom(PACKAGENAME
, pci
, Cache
, cmdline
);
378 return PackageFrom(CacheSetHelper::PACKAGENAME
, pci
, Cache
, str
);
381 // FromModifierCommandLine - helper doing the work for VER:GroupedFromCommandLine /*{{{*/
382 bool VersionContainerInterface::FromModifierCommandLine(unsigned short &modID
,
383 VersionContainerInterface
* const vci
,
384 pkgCacheFile
&Cache
, const char * cmdline
,
385 std::list
<Modifier
> const &mods
,
386 CacheSetHelper
&helper
) {
387 CacheSetHelper::VerSelector select
= CacheSetHelper::NEWEST
;
388 std::string str
= cmdline
;
389 if (unlikely(str
.empty() == true))
391 bool modifierPresent
= false;
392 unsigned short fallback
= modID
;
393 for (std::list
<Modifier
>::const_iterator mod
= mods
.begin();
394 mod
!= mods
.end(); ++mod
) {
395 if (modID
== fallback
&& mod
->ID
== fallback
)
396 select
= mod
->SelectVersion
;
397 size_t const alength
= strlen(mod
->Alias
);
399 case Modifier::POSTFIX
:
400 if (str
.length() <= alength
||
401 str
.compare(str
.length() - alength
, alength
, mod
->Alias
, 0, alength
) != 0)
403 str
.erase(str
.length() - alength
);
405 select
= mod
->SelectVersion
;
407 case Modifier::PREFIX
:
412 modifierPresent
= true;
415 if (modifierPresent
== true) {
416 bool const errors
= helper
.showErrors(false);
417 bool const found
= VersionContainerInterface::FromString(vci
, Cache
, cmdline
, select
, helper
, true);
418 helper
.showErrors(errors
);
424 return FromString(vci
, Cache
, str
, select
, helper
);
427 // FromCommandLine - Return all versions specified on commandline /*{{{*/
428 bool VersionContainerInterface::FromCommandLine(VersionContainerInterface
* const vci
,
429 pkgCacheFile
&Cache
, const char **cmdline
,
430 CacheSetHelper::VerSelector
const fallback
,
431 CacheSetHelper
&helper
) {
433 for (const char **I
= cmdline
; *I
!= 0; ++I
)
434 found
|= VersionContainerInterface::FromString(vci
, Cache
, *I
, fallback
, helper
);
438 // FromString - Returns all versions spedcified by a string /*{{{*/
439 bool VersionContainerInterface::FromString(VersionContainerInterface
* const vci
,
440 pkgCacheFile
&Cache
, std::string pkg
,
441 CacheSetHelper::VerSelector
const fallback
,
442 CacheSetHelper
&helper
,
443 bool const onlyFromName
) {
445 if(FileExists(pkg
)) {
446 helper
.PackageFrom(CacheSetHelper::STRING
, &pkgset
, Cache
, pkg
);
447 if(pkgset
.empty() == true)
449 return VersionContainerInterface::FromPackage(vci
, Cache
, pkgset
.begin(), fallback
, helper
);
453 bool verIsRel
= false;
454 size_t const vertag
= pkg
.find_last_of("/=");
455 if (vertag
!= std::string::npos
) {
456 ver
= pkg
.substr(vertag
+1);
457 verIsRel
= (pkg
[vertag
] == '/');
460 if (onlyFromName
== false)
461 helper
.PackageFrom(CacheSetHelper::STRING
, &pkgset
, Cache
, pkg
);
463 helper
.PackageFrom(CacheSetHelper::PACKAGENAME
, &pkgset
, Cache
, pkg
);
467 if (pkgset
.getConstructor() != CacheSetHelper::UNKNOWN
)
468 errors
= helper
.showErrors(false);
471 for (PackageSet::const_iterator P
= pkgset
.begin();
472 P
!= pkgset
.end(); ++P
) {
473 if (vertag
== std::string::npos
) {
474 found
|= VersionContainerInterface::FromPackage(vci
, Cache
, P
, fallback
, helper
);
477 pkgCache::VerIterator V
;
478 if (ver
== "installed")
479 V
= getInstalledVer(Cache
, P
, helper
);
480 else if (ver
== "candidate")
481 V
= getCandidateVer(Cache
, P
, helper
);
482 else if (ver
== "newest") {
483 if (P
->VersionList
!= 0)
486 V
= helper
.canNotGetVersion(CacheSetHelper::NEWEST
, Cache
, P
);
488 pkgVersionMatch
Match(ver
, (verIsRel
== true ? pkgVersionMatch::Release
:
489 pkgVersionMatch::Version
));
491 if (V
.end() == true) {
492 if (verIsRel
== true)
493 _error
->Error(_("Release '%s' for '%s' was not found"),
494 ver
.c_str(), P
.FullName(true).c_str());
496 _error
->Error(_("Version '%s' for '%s' was not found"),
497 ver
.c_str(), P
.FullName(true).c_str());
503 if (verIsRel
== true)
504 helper
.showVersionSelection(P
, V
, CacheSetHelper::RELEASE
, ver
);
506 helper
.showVersionSelection(P
, V
, CacheSetHelper::VERSIONNUMBER
, ver
);
510 if (pkgset
.getConstructor() != CacheSetHelper::UNKNOWN
)
511 helper
.showErrors(errors
);
515 // FromPackage - versions from package based on fallback /*{{{*/
516 bool VersionContainerInterface::FromPackage(VersionContainerInterface
* const vci
,
518 pkgCache::PkgIterator
const &P
,
519 CacheSetHelper::VerSelector
const fallback
,
520 CacheSetHelper
&helper
) {
521 pkgCache::VerIterator V
;
525 case CacheSetHelper::ALL
:
526 if (P
->VersionList
!= 0)
527 for (V
= P
.VersionList(); V
.end() != true; ++V
)
528 found
|= vci
->insert(V
);
530 helper
.canNotFindVersion(CacheSetHelper::ALL
, vci
, Cache
, P
);
532 case CacheSetHelper::CANDANDINST
:
533 found
|= vci
->insert(getInstalledVer(Cache
, P
, helper
));
534 found
|= vci
->insert(getCandidateVer(Cache
, P
, helper
));
536 case CacheSetHelper::CANDIDATE
:
537 found
|= vci
->insert(getCandidateVer(Cache
, P
, helper
));
539 case CacheSetHelper::INSTALLED
:
540 found
|= vci
->insert(getInstalledVer(Cache
, P
, helper
));
542 case CacheSetHelper::CANDINST
:
543 showErrors
= helper
.showErrors(false);
544 V
= getCandidateVer(Cache
, P
, helper
);
546 V
= getInstalledVer(Cache
, P
, helper
);
547 helper
.showErrors(showErrors
);
548 if (V
.end() == false)
549 found
|= vci
->insert(V
);
551 helper
.canNotFindVersion(CacheSetHelper::CANDINST
, vci
, Cache
, P
);
553 case CacheSetHelper::INSTCAND
:
554 showErrors
= helper
.showErrors(false);
555 V
= getInstalledVer(Cache
, P
, helper
);
557 V
= getCandidateVer(Cache
, P
, helper
);
558 helper
.showErrors(showErrors
);
559 if (V
.end() == false)
560 found
|= vci
->insert(V
);
562 helper
.canNotFindVersion(CacheSetHelper::INSTCAND
, vci
, Cache
, P
);
564 case CacheSetHelper::NEWEST
:
565 if (P
->VersionList
!= 0)
566 found
|= vci
->insert(P
.VersionList());
568 helper
.canNotFindVersion(CacheSetHelper::NEWEST
, vci
, Cache
, P
);
570 case CacheSetHelper::RELEASE
:
571 case CacheSetHelper::VERSIONNUMBER
:
572 // both make no sense here, so always false
578 // FromDependency - versions satisfying a given dependency /*{{{*/
579 bool VersionContainerInterface::FromDependency(VersionContainerInterface
* const vci
,
581 pkgCache::DepIterator
const &D
,
582 CacheSetHelper::VerSelector
const selector
,
583 CacheSetHelper
&helper
)
587 case CacheSetHelper::ALL
:
589 pkgCache::PkgIterator
const T
= D
.TargetPkg();
590 for (pkgCache::VerIterator Ver
= T
.VersionList(); Ver
.end() == false; ++Ver
)
592 if (D
.IsSatisfied(Ver
) == true)
597 for (pkgCache::PrvIterator Prv
= T
.ProvidesList(); Prv
.end() != true; ++Prv
)
599 pkgCache::VerIterator
const V
= Prv
.OwnerVer();
600 if (unlikely(V
.end() == true) || D
.IsSatisfied(Prv
) == false)
608 case CacheSetHelper::CANDANDINST
:
610 found
= FromDependency(vci
, Cache
, D
, CacheSetHelper::CANDIDATE
, helper
);
611 found
&= FromDependency(vci
, Cache
, D
, CacheSetHelper::INSTALLED
, helper
);
614 case CacheSetHelper::CANDIDATE
:
616 pkgCache::PkgIterator
const T
= D
.TargetPkg();
617 pkgCache::VerIterator
const Cand
= Cache
[T
].CandidateVerIter(Cache
);
618 if (Cand
.end() == false && D
.IsSatisfied(Cand
) == true)
623 for (pkgCache::PrvIterator Prv
= T
.ProvidesList(); Prv
.end() != true; ++Prv
)
625 pkgCache::VerIterator
const V
= Prv
.OwnerVer();
626 pkgCache::VerIterator
const Cand
= Cache
[Prv
.OwnerPkg()].CandidateVerIter(Cache
);
627 if (Cand
.end() == true || V
!= Cand
|| D
.IsSatisfied(Prv
) == false)
634 case CacheSetHelper::INSTALLED
:
636 pkgCache::PkgIterator
const T
= D
.TargetPkg();
637 pkgCache::VerIterator
const Cand
= T
.CurrentVer();
638 if (Cand
.end() == false && D
.IsSatisfied(Cand
) == true)
643 for (pkgCache::PrvIterator Prv
= T
.ProvidesList(); Prv
.end() != true; ++Prv
)
645 pkgCache::VerIterator
const V
= Prv
.OwnerVer();
646 pkgCache::VerIterator
const Cand
= Prv
.OwnerPkg().CurrentVer();
647 if (Cand
.end() == true || V
!= Cand
|| D
.IsSatisfied(Prv
) == false)
654 case CacheSetHelper::CANDINST
:
655 return FromDependency(vci
, Cache
, D
, CacheSetHelper::CANDIDATE
, helper
) ||
656 FromDependency(vci
, Cache
, D
, CacheSetHelper::INSTALLED
, helper
);
657 case CacheSetHelper::INSTCAND
:
658 return FromDependency(vci
, Cache
, D
, CacheSetHelper::INSTALLED
, helper
) ||
659 FromDependency(vci
, Cache
, D
, CacheSetHelper::CANDIDATE
, helper
);
660 case CacheSetHelper::NEWEST
:
662 pkgCache::PkgIterator
const T
= D
.TargetPkg();
663 pkgCache::VerIterator
const Cand
= T
.VersionList();
664 if (Cand
.end() == false && D
.IsSatisfied(Cand
) == true)
669 for (pkgCache::PrvIterator Prv
= T
.ProvidesList(); Prv
.end() != true; ++Prv
)
671 pkgCache::VerIterator
const V
= Prv
.OwnerVer();
672 pkgCache::VerIterator
const Cand
= Prv
.OwnerPkg().VersionList();
673 if (Cand
.end() == true || V
!= Cand
|| D
.IsSatisfied(Prv
) == false)
680 case CacheSetHelper::RELEASE
:
681 case CacheSetHelper::VERSIONNUMBER
:
682 // both make no sense here, so always false
688 // getCandidateVer - Returns the candidate version of the given package /*{{{*/
689 pkgCache::VerIterator
VersionContainerInterface::getCandidateVer(pkgCacheFile
&Cache
,
690 pkgCache::PkgIterator
const &Pkg
, CacheSetHelper
&helper
) {
691 pkgCache::VerIterator Cand
;
692 if (Cache
.IsDepCacheBuilt() == true) {
693 Cand
= Cache
[Pkg
].CandidateVerIter(Cache
);
694 } else if (unlikely(Cache
.GetPolicy() == nullptr)) {
695 return pkgCache::VerIterator(Cache
);
697 Cand
= Cache
.GetPolicy()->GetCandidateVer(Pkg
);
699 if (Cand
.end() == true)
700 return helper
.canNotGetVersion(CacheSetHelper::CANDIDATE
, Cache
, Pkg
);
704 // getInstalledVer - Returns the installed version of the given package /*{{{*/
705 pkgCache::VerIterator
VersionContainerInterface::getInstalledVer(pkgCacheFile
&Cache
,
706 pkgCache::PkgIterator
const &Pkg
, CacheSetHelper
&helper
) {
707 if (Pkg
->CurrentVer
== 0)
708 return helper
.canNotGetVersion(CacheSetHelper::INSTALLED
, Cache
, Pkg
);
709 return Pkg
.CurrentVer();
713 // canNotFindPackage - with the given selector and pattern /*{{{*/
714 void CacheSetHelper::canNotFindPackage(enum PkgSelector
const select
,
715 PackageContainerInterface
* const pci
, pkgCacheFile
&Cache
,
716 std::string
const &pattern
) {
718 APT_IGNORE_DEPRECATED_PUSH
719 case REGEX
: canNotFindRegEx(pci
, Cache
, pattern
); break;
720 case TASK
: canNotFindTask(pci
, Cache
, pattern
); break;
721 case FNMATCH
: canNotFindFnmatch(pci
, Cache
, pattern
); break;
722 case PACKAGENAME
: canNotFindPackage(pci
, Cache
, pattern
); break;
723 case STRING
: canNotFindPackage(pci
, Cache
, pattern
); break;
725 APT_IGNORE_DEPRECATED_POP
728 // canNotFindTask - handle the case no package is found for a task /*{{{*/
729 void CacheSetHelper::canNotFindTask(PackageContainerInterface
* const /*pci*/, pkgCacheFile
&/*Cache*/, std::string pattern
) {
730 if (ShowError
== true)
731 _error
->Insert(ErrorType
, _("Couldn't find task '%s'"), pattern
.c_str());
734 // canNotFindRegEx - handle the case no package is found by a regex /*{{{*/
735 void CacheSetHelper::canNotFindRegEx(PackageContainerInterface
* const /*pci*/, pkgCacheFile
&/*Cache*/, std::string pattern
) {
736 if (ShowError
== true)
737 _error
->Insert(ErrorType
, _("Couldn't find any package by regex '%s'"), pattern
.c_str());
740 // canNotFindFnmatch - handle the case no package is found by a fnmatch /*{{{*/
741 void CacheSetHelper::canNotFindFnmatch(PackageContainerInterface
* const /*pci*/, pkgCacheFile
&/*Cache*/, std::string pattern
) {
742 if (ShowError
== true)
743 _error
->Insert(ErrorType
, _("Couldn't find any package by glob '%s'"), pattern
.c_str());
746 // canNotFindPackage - handle the case no package is found from a string/*{{{*/
747 APT_CONST
void CacheSetHelper::canNotFindPackage(PackageContainerInterface
* const /*pci*/, pkgCacheFile
&/*Cache*/, std::string
const &/*str*/) {
751 // canNotFindPkgName - handle the case no package has this name /*{{{*/
752 pkgCache::PkgIterator
CacheSetHelper::canNotFindPkgName(pkgCacheFile
&Cache
,
753 std::string
const &str
) {
754 if (ShowError
== true)
755 _error
->Insert(ErrorType
, _("Unable to locate package %s"), str
.c_str());
756 return pkgCache::PkgIterator(Cache
, 0);
759 // canNotFindVersion - for package by selector /*{{{*/
760 void CacheSetHelper::canNotFindVersion(enum VerSelector
const select
, VersionContainerInterface
* const vci
, pkgCacheFile
&Cache
, pkgCache::PkgIterator
const &Pkg
)
763 APT_IGNORE_DEPRECATED_PUSH
764 case ALL
: canNotFindAllVer(vci
, Cache
, Pkg
); break;
765 case INSTCAND
: canNotFindInstCandVer(vci
, Cache
, Pkg
); break;
766 case CANDINST
: canNotFindCandInstVer(vci
, Cache
, Pkg
); break;
767 case NEWEST
: canNotFindNewestVer(Cache
, Pkg
); break;
768 case CANDIDATE
: canNotFindCandidateVer(Cache
, Pkg
); break;
769 case INSTALLED
: canNotFindInstalledVer(Cache
, Pkg
); break;
770 APT_IGNORE_DEPRECATED_POP
771 case CANDANDINST
: canNotGetCandInstVer(Cache
, Pkg
); break;
774 // invalid in this branch
778 // canNotFindAllVer /*{{{*/
779 void CacheSetHelper::canNotFindAllVer(VersionContainerInterface
* const /*vci*/, pkgCacheFile
&/*Cache*/,
780 pkgCache::PkgIterator
const &Pkg
) {
781 if (ShowError
== true)
782 _error
->Insert(ErrorType
, _("Can't select versions from package '%s' as it is purely virtual"), Pkg
.FullName(true).c_str());
785 // canNotFindInstCandVer /*{{{*/
786 void CacheSetHelper::canNotFindInstCandVer(VersionContainerInterface
* const /*vci*/, pkgCacheFile
&Cache
,
787 pkgCache::PkgIterator
const &Pkg
) {
788 canNotGetInstCandVer(Cache
, Pkg
);
791 // canNotFindInstCandVer /*{{{*/
792 void CacheSetHelper::canNotFindCandInstVer(VersionContainerInterface
* const /*vci*/, pkgCacheFile
&Cache
,
793 pkgCache::PkgIterator
const &Pkg
) {
794 canNotGetCandInstVer(Cache
, Pkg
);
798 // canNotGetVersion - for package by selector /*{{{*/
799 pkgCache::VerIterator
CacheSetHelper::canNotGetVersion(enum VerSelector
const select
, pkgCacheFile
&Cache
, pkgCache::PkgIterator
const &Pkg
) {
801 APT_IGNORE_DEPRECATED_PUSH
802 case NEWEST
: return canNotFindNewestVer(Cache
, Pkg
);
803 case CANDIDATE
: return canNotFindCandidateVer(Cache
, Pkg
);
804 case INSTALLED
: return canNotFindInstalledVer(Cache
, Pkg
);
805 APT_IGNORE_DEPRECATED_POP
806 case CANDINST
: return canNotGetCandInstVer(Cache
, Pkg
);
807 case INSTCAND
: return canNotGetInstCandVer(Cache
, Pkg
);
812 // invalid in this branch
813 return pkgCache::VerIterator(Cache
, 0);
815 return pkgCache::VerIterator(Cache
, 0);
817 // canNotFindNewestVer /*{{{*/
818 pkgCache::VerIterator
CacheSetHelper::canNotFindNewestVer(pkgCacheFile
&Cache
,
819 pkgCache::PkgIterator
const &Pkg
) {
820 if (ShowError
== true)
821 _error
->Insert(ErrorType
, _("Can't select newest version from package '%s' as it is purely virtual"), Pkg
.FullName(true).c_str());
822 return pkgCache::VerIterator(Cache
, 0);
825 // canNotFindCandidateVer /*{{{*/
826 pkgCache::VerIterator
CacheSetHelper::canNotFindCandidateVer(pkgCacheFile
&Cache
,
827 pkgCache::PkgIterator
const &Pkg
) {
828 if (ShowError
== true)
829 _error
->Insert(ErrorType
, _("Can't select candidate version from package %s as it has no candidate"), Pkg
.FullName(true).c_str());
830 return pkgCache::VerIterator(Cache
, 0);
833 // canNotFindInstalledVer /*{{{*/
834 pkgCache::VerIterator
CacheSetHelper::canNotFindInstalledVer(pkgCacheFile
&Cache
,
835 pkgCache::PkgIterator
const &Pkg
) {
836 if (ShowError
== true)
837 _error
->Insert(ErrorType
, _("Can't select installed version from package %s as it is not installed"), Pkg
.FullName(true).c_str());
838 return pkgCache::VerIterator(Cache
, 0);
841 // canNotFindInstCandVer /*{{{*/
842 pkgCache::VerIterator
CacheSetHelper::canNotGetInstCandVer(pkgCacheFile
&Cache
,
843 pkgCache::PkgIterator
const &Pkg
) {
844 if (ShowError
== true)
845 _error
->Insert(ErrorType
, _("Can't select installed nor candidate version from package '%s' as it has neither of them"), Pkg
.FullName(true).c_str());
846 return pkgCache::VerIterator(Cache
, 0);
849 // canNotFindInstCandVer /*{{{*/
850 pkgCache::VerIterator
CacheSetHelper::canNotGetCandInstVer(pkgCacheFile
&Cache
,
851 pkgCache::PkgIterator
const &Pkg
) {
852 if (ShowError
== true)
853 _error
->Insert(ErrorType
, _("Can't select installed nor candidate version from package '%s' as it has neither of them"), Pkg
.FullName(true).c_str());
854 return pkgCache::VerIterator(Cache
, 0);
858 // showPackageSelection - by selector and given pattern /*{{{*/
859 void CacheSetHelper::showPackageSelection(pkgCache::PkgIterator
const &pkg
, enum PkgSelector
const select
,
860 std::string
const &pattern
) {
862 APT_IGNORE_DEPRECATED_PUSH
863 case REGEX
: showRegExSelection(pkg
, pattern
); break;
864 case TASK
: showTaskSelection(pkg
, pattern
); break;
865 case FNMATCH
: showFnmatchSelection(pkg
, pattern
); break;
866 APT_IGNORE_DEPRECATED_POP
867 case PACKAGENAME
: /* no suprises here */ break;
868 case STRING
: /* handled by the special cases */ break;
872 // showTaskSelection /*{{{*/
873 APT_CONST
void CacheSetHelper::showTaskSelection(pkgCache::PkgIterator
const &/*pkg*/,
874 std::string
const &/*pattern*/) {
877 // showRegExSelection /*{{{*/
878 APT_CONST
void CacheSetHelper::showRegExSelection(pkgCache::PkgIterator
const &/*pkg*/,
879 std::string
const &/*pattern*/) {
882 // showFnmatchSelection /*{{{*/
883 APT_CONST
void CacheSetHelper::showFnmatchSelection(pkgCache::PkgIterator
const &/*pkg*/,
884 std::string
const &/*pattern*/) {
888 // showVersionSelection /*{{{*/
889 void CacheSetHelper::showVersionSelection(pkgCache::PkgIterator
const &Pkg
,
890 pkgCache::VerIterator
const &Ver
, enum VerSelector
const select
, std::string
const &pattern
) {
892 APT_IGNORE_DEPRECATED_PUSH
894 showSelectedVersion(Pkg
, Ver
, pattern
, true);
897 showSelectedVersion(Pkg
, Ver
, pattern
, false);
899 APT_IGNORE_DEPRECATED_POP
907 // not really suprises, but in fact: just not implemented
911 APT_CONST
void CacheSetHelper::showSelectedVersion(pkgCache::PkgIterator
const &/*Pkg*/,
912 pkgCache::VerIterator
const /*Ver*/,
913 std::string
const &/*ver*/,
914 bool const /*verIsRel*/) {
918 CacheSetHelper::CacheSetHelper(bool const ShowError
, GlobalError::MsgType ErrorType
) :
919 ShowError(ShowError
), ErrorType(ErrorType
), d(NULL
) {}
920 CacheSetHelper::~CacheSetHelper() {}
922 PackageContainerInterface::PackageContainerInterface() : ConstructedBy(CacheSetHelper::UNKNOWN
), d(NULL
) {}
923 PackageContainerInterface::PackageContainerInterface(CacheSetHelper::PkgSelector
const by
) : ConstructedBy(by
), d(NULL
) {}
924 PackageContainerInterface
& PackageContainerInterface::operator=(PackageContainerInterface
const &other
) {
926 this->ConstructedBy
= other
.ConstructedBy
;
929 PackageContainerInterface::~PackageContainerInterface() {}
931 PackageUniverse::PackageUniverse(pkgCache
* const Owner
) : _cont(Owner
), d(NULL
) {}
932 PackageUniverse::PackageUniverse(pkgCacheFile
* const Owner
) : _cont(Owner
->GetPkgCache()), d(NULL
) {}
933 PackageUniverse::~PackageUniverse() {}
935 VersionContainerInterface::VersionContainerInterface() : d(NULL
) {}
936 VersionContainerInterface
& VersionContainerInterface::operator=(VersionContainerInterface
const &) {
940 VersionContainerInterface::~VersionContainerInterface() {}