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/strutl.h>
20 #include <apt-pkg/versionmatch.h>
21 #include <apt-pkg/pkgrecords.h>
22 #include <apt-pkg/policy.h>
31 // FromTask - Return all packages in the cache from a specific task /*{{{*/
32 bool PackageContainerInterface::FromTask(PackageContainerInterface
* const pci
, pkgCacheFile
&Cache
, std::string pattern
, CacheSetHelper
&helper
) {
33 size_t const archfound
= pattern
.find_last_of(':');
34 std::string arch
= "native";
35 if (archfound
!= std::string::npos
) {
36 arch
= pattern
.substr(archfound
+1);
37 pattern
.erase(archfound
);
40 if (pattern
[pattern
.length() -1] != '^')
42 pattern
.erase(pattern
.length()-1);
44 if (unlikely(Cache
.GetPkgCache() == 0 || Cache
.GetDepCache() == 0))
47 bool const wasEmpty
= pci
->empty();
49 pci
->setConstructor(TASK
);
52 pkgRecords
Recs(Cache
);
54 // build regexp for the task
57 snprintf(S
, sizeof(S
), "^Task:.*[, ]%s([, ]|$)", pattern
.c_str());
58 if(regcomp(&Pattern
,S
, REG_EXTENDED
| REG_NOSUB
| REG_NEWLINE
) != 0) {
59 _error
->Error("Failed to compile task regexp");
64 for (pkgCache::GrpIterator Grp
= Cache
->GrpBegin(); Grp
.end() == false; ++Grp
) {
65 pkgCache::PkgIterator Pkg
= Grp
.FindPkg(arch
);
66 if (Pkg
.end() == true)
68 pkgCache::VerIterator ver
= Cache
[Pkg
].CandidateVerIter(Cache
);
72 pkgRecords::Parser
&parser
= Recs
.Lookup(ver
.FileList());
73 const char *start
, *end
;
74 parser
.GetRec(start
,end
);
75 unsigned int const length
= end
- start
;
77 strncpy(buf
, start
, length
);
79 if (regexec(&Pattern
, buf
, 0, 0, 0) != 0)
83 helper
.showTaskSelection(Pkg
, pattern
);
89 helper
.canNotFindTask(pci
, Cache
, pattern
);
90 pci
->setConstructor(UNKNOWN
);
94 if (wasEmpty
== false && pci
->getConstructor() != UNKNOWN
)
95 pci
->setConstructor(UNKNOWN
);
100 // FromRegEx - Return all packages in the cache matching a pattern /*{{{*/
101 bool PackageContainerInterface::FromRegEx(PackageContainerInterface
* const pci
, pkgCacheFile
&Cache
, std::string pattern
, CacheSetHelper
&helper
) {
102 static const char * const isregex
= ".?+*|[^$";
103 if (pattern
.find_first_of(isregex
) == std::string::npos
)
106 bool const wasEmpty
= pci
->empty();
107 if (wasEmpty
== true)
108 pci
->setConstructor(REGEX
);
110 size_t archfound
= pattern
.find_last_of(':');
111 std::string arch
= "native";
112 if (archfound
!= std::string::npos
) {
113 arch
= pattern
.substr(archfound
+1);
114 if (arch
.find_first_of(isregex
) == std::string::npos
)
115 pattern
.erase(archfound
);
120 if (unlikely(Cache
.GetPkgCache() == 0))
123 APT::CacheFilter::PackageNameMatchesRegEx
regexfilter(pattern
);
126 for (pkgCache::GrpIterator Grp
= Cache
.GetPkgCache()->GrpBegin(); Grp
.end() == false; ++Grp
) {
127 if (regexfilter(Grp
) == false)
129 pkgCache::PkgIterator Pkg
= Grp
.FindPkg(arch
);
130 if (Pkg
.end() == true) {
131 if (archfound
== std::string::npos
) {
132 std::vector
<std::string
> archs
= APT::Configuration::getArchitectures();
133 for (std::vector
<std::string
>::const_iterator a
= archs
.begin();
134 a
!= archs
.end() && Pkg
.end() != true; ++a
)
135 Pkg
= Grp
.FindPkg(*a
);
137 if (Pkg
.end() == true)
142 helper
.showRegExSelection(Pkg
, pattern
);
146 if (found
== false) {
147 helper
.canNotFindRegEx(pci
, Cache
, pattern
);
148 pci
->setConstructor(UNKNOWN
);
152 if (wasEmpty
== false && pci
->getConstructor() != UNKNOWN
)
153 pci
->setConstructor(UNKNOWN
);
158 // FromFnmatch - Returns the package defined by this fnmatch /*{{{*/
160 PackageContainerInterface::FromFnmatch(PackageContainerInterface
* const pci
,
163 CacheSetHelper
&helper
)
165 static const char * const isfnmatch
= ".?*[]!";
166 if (pattern
.find_first_of(isfnmatch
) == std::string::npos
)
169 bool const wasEmpty
= pci
->empty();
170 if (wasEmpty
== true)
171 pci
->setConstructor(FNMATCH
);
173 size_t archfound
= pattern
.find_last_of(':');
174 std::string arch
= "native";
175 if (archfound
!= std::string::npos
) {
176 arch
= pattern
.substr(archfound
+1);
177 if (arch
.find_first_of(isfnmatch
) == std::string::npos
)
178 pattern
.erase(archfound
);
183 if (unlikely(Cache
.GetPkgCache() == 0))
186 APT::CacheFilter::PackageNameMatchesFnmatch
filter(pattern
);
189 for (pkgCache::GrpIterator Grp
= Cache
.GetPkgCache()->GrpBegin(); Grp
.end() == false; ++Grp
) {
190 if (filter(Grp
) == false)
192 pkgCache::PkgIterator Pkg
= Grp
.FindPkg(arch
);
193 if (Pkg
.end() == true) {
194 if (archfound
== std::string::npos
) {
195 std::vector
<std::string
> archs
= APT::Configuration::getArchitectures();
196 for (std::vector
<std::string
>::const_iterator a
= archs
.begin();
197 a
!= archs
.end() && Pkg
.end() != true; ++a
)
198 Pkg
= Grp
.FindPkg(*a
);
200 if (Pkg
.end() == true)
205 helper
.showRegExSelection(Pkg
, pattern
);
209 if (found
== false) {
210 helper
.canNotFindRegEx(pci
, Cache
, pattern
);
211 pci
->setConstructor(UNKNOWN
);
215 if (wasEmpty
== false && pci
->getConstructor() != UNKNOWN
)
216 pci
->setConstructor(UNKNOWN
);
221 // FromName - Returns the package defined by this string /*{{{*/
222 pkgCache::PkgIterator
PackageContainerInterface::FromName(pkgCacheFile
&Cache
,
223 std::string
const &str
, CacheSetHelper
&helper
) {
224 std::string pkg
= str
;
225 size_t archfound
= pkg
.find_last_of(':');
227 if (archfound
!= std::string::npos
) {
228 arch
= pkg
.substr(archfound
+1);
229 pkg
.erase(archfound
);
232 if (Cache
.GetPkgCache() == 0)
233 return pkgCache::PkgIterator(Cache
, 0);
235 pkgCache::PkgIterator
Pkg(Cache
, 0);
236 if (arch
.empty() == true) {
237 pkgCache::GrpIterator Grp
= Cache
.GetPkgCache()->FindGrp(pkg
);
238 if (Grp
.end() == false)
239 Pkg
= Grp
.FindPreferredPkg();
241 Pkg
= Cache
.GetPkgCache()->FindPkg(pkg
, arch
);
243 if (Pkg
.end() == true)
244 return helper
.canNotFindPkgName(Cache
, str
);
248 // FromGroup - Returns the package defined by this string /*{{{*/
249 bool PackageContainerInterface::FromGroup(PackageContainerInterface
* const pci
, pkgCacheFile
&Cache
,
250 std::string pkg
, CacheSetHelper
&helper
) {
251 if (unlikely(Cache
.GetPkgCache() == 0))
254 size_t const archfound
= pkg
.find_last_of(':');
256 if (archfound
!= std::string::npos
) {
257 arch
= pkg
.substr(archfound
+1);
258 pkg
.erase(archfound
);
259 if (arch
== "all" || arch
== "native")
260 arch
= _config
->Find("APT::Architecture");
263 pkgCache::GrpIterator Grp
= Cache
.GetPkgCache()->FindGrp(pkg
);
264 if (Grp
.end() == false) {
265 if (arch
.empty() == true) {
266 pkgCache::PkgIterator Pkg
= Grp
.FindPreferredPkg();
267 if (Pkg
.end() == false)
274 // for 'linux-any' return the first package matching, for 'linux-*' return all matches
275 bool const isGlobal
= arch
.find('*') != std::string::npos
;
276 APT::CacheFilter::PackageArchitectureMatchesSpecification
pams(arch
);
277 for (pkgCache::PkgIterator Pkg
= Grp
.PackageList(); Pkg
.end() == false; Pkg
= Grp
.NextPkg(Pkg
)) {
278 if (pams(Pkg
) == false)
282 if (isGlobal
== false)
290 pkgCache::PkgIterator Pkg
= helper
.canNotFindPkgName(Cache
, pkg
);
291 if (Pkg
.end() == true)
298 // FromString - Return all packages matching a specific string /*{{{*/
299 bool PackageContainerInterface::FromString(PackageContainerInterface
* const pci
, pkgCacheFile
&Cache
, std::string
const &str
, CacheSetHelper
&helper
) {
301 _error
->PushToStack();
303 if (FromGroup(pci
, Cache
, str
, helper
) == false &&
304 FromTask(pci
, Cache
, str
, helper
) == false &&
305 FromFnmatch(pci
, Cache
, str
, helper
) == false &&
306 FromRegEx(pci
, Cache
, str
, helper
) == false)
308 helper
.canNotFindPackage(pci
, Cache
, str
);
313 _error
->RevertToStack();
315 _error
->MergeWithStack();
319 // FromCommandLine - Return all packages specified on commandline /*{{{*/
320 bool PackageContainerInterface::FromCommandLine(PackageContainerInterface
* const pci
, pkgCacheFile
&Cache
, const char **cmdline
, CacheSetHelper
&helper
) {
322 for (const char **I
= cmdline
; *I
!= 0; ++I
)
323 found
|= PackageContainerInterface::FromString(pci
, Cache
, *I
, helper
);
327 // FromModifierCommandLine - helper doing the work for PKG:GroupedFromCommandLine /*{{{*/
328 bool PackageContainerInterface::FromModifierCommandLine(unsigned short &modID
, PackageContainerInterface
* const pci
,
329 pkgCacheFile
&Cache
, const char * cmdline
,
330 std::list
<Modifier
> const &mods
, CacheSetHelper
&helper
) {
331 std::string str
= cmdline
;
332 unsigned short fallback
= modID
;
333 bool modifierPresent
= false;
334 for (std::list
<Modifier
>::const_iterator mod
= mods
.begin();
335 mod
!= mods
.end(); ++mod
) {
336 size_t const alength
= strlen(mod
->Alias
);
338 case Modifier::POSTFIX
:
339 if (str
.compare(str
.length() - alength
, alength
,
340 mod
->Alias
, 0, alength
) != 0)
342 str
.erase(str
.length() - alength
);
345 case Modifier::PREFIX
:
350 modifierPresent
= true;
353 if (modifierPresent
== true) {
354 bool const errors
= helper
.showErrors(false);
355 pkgCache::PkgIterator Pkg
= FromName(Cache
, cmdline
, helper
);
356 helper
.showErrors(errors
);
357 if (Pkg
.end() == false) {
363 return FromString(pci
, Cache
, str
, helper
);
366 // FromModifierCommandLine - helper doing the work for VER:GroupedFromCommandLine /*{{{*/
367 bool VersionContainerInterface::FromModifierCommandLine(unsigned short &modID
,
368 VersionContainerInterface
* const vci
,
369 pkgCacheFile
&Cache
, const char * cmdline
,
370 std::list
<Modifier
> const &mods
,
371 CacheSetHelper
&helper
) {
372 Version select
= NEWEST
;
373 std::string str
= cmdline
;
374 bool modifierPresent
= false;
375 unsigned short fallback
= modID
;
376 for (std::list
<Modifier
>::const_iterator mod
= mods
.begin();
377 mod
!= mods
.end(); ++mod
) {
378 if (modID
== fallback
&& mod
->ID
== fallback
)
379 select
= mod
->SelectVersion
;
380 size_t const alength
= strlen(mod
->Alias
);
382 case Modifier::POSTFIX
:
383 if (str
.compare(str
.length() - alength
, alength
,
384 mod
->Alias
, 0, alength
) != 0)
386 str
.erase(str
.length() - alength
);
388 select
= mod
->SelectVersion
;
390 case Modifier::PREFIX
:
395 modifierPresent
= true;
398 if (modifierPresent
== true) {
399 bool const errors
= helper
.showErrors(false);
400 bool const found
= VersionContainerInterface::FromString(vci
, Cache
, cmdline
, select
, helper
, true);
401 helper
.showErrors(errors
);
407 return FromString(vci
, Cache
, str
, select
, helper
);
410 // FromCommandLine - Return all versions specified on commandline /*{{{*/
411 bool VersionContainerInterface::FromCommandLine(VersionContainerInterface
* const vci
,
412 pkgCacheFile
&Cache
, const char **cmdline
,
413 Version
const &fallback
, CacheSetHelper
&helper
) {
415 for (const char **I
= cmdline
; *I
!= 0; ++I
)
416 found
|= VersionContainerInterface::FromString(vci
, Cache
, *I
, fallback
, helper
);
420 // FromString - Returns all versions spedcified by a string /*{{{*/
421 bool VersionContainerInterface::FromString(VersionContainerInterface
* const vci
,
422 pkgCacheFile
&Cache
, std::string pkg
,
423 Version
const &fallback
, CacheSetHelper
&helper
,
424 bool const onlyFromName
) {
426 bool verIsRel
= false;
427 size_t const vertag
= pkg
.find_last_of("/=");
428 if (vertag
!= std::string::npos
) {
429 ver
= pkg
.substr(vertag
+1);
430 verIsRel
= (pkg
[vertag
] == '/');
434 if (onlyFromName
== false)
435 PackageContainerInterface::FromString(&pkgset
, Cache
, pkg
, helper
);
437 pkgset
.insert(PackageContainerInterface::FromName(Cache
, pkg
, helper
));
441 if (pkgset
.getConstructor() != PackageSet::UNKNOWN
)
442 errors
= helper
.showErrors(false);
445 for (PackageSet::const_iterator P
= pkgset
.begin();
446 P
!= pkgset
.end(); ++P
) {
447 if (vertag
== std::string::npos
) {
448 found
|= VersionContainerInterface::FromPackage(vci
, Cache
, P
, fallback
, helper
);
451 pkgCache::VerIterator V
;
452 if (ver
== "installed")
453 V
= getInstalledVer(Cache
, P
, helper
);
454 else if (ver
== "candidate")
455 V
= getCandidateVer(Cache
, P
, helper
);
456 else if (ver
== "newest") {
457 if (P
->VersionList
!= 0)
460 V
= helper
.canNotFindNewestVer(Cache
, P
);
462 pkgVersionMatch
Match(ver
, (verIsRel
== true ? pkgVersionMatch::Release
:
463 pkgVersionMatch::Version
));
465 if (V
.end() == true) {
466 if (verIsRel
== true)
467 _error
->Error(_("Release '%s' for '%s' was not found"),
468 ver
.c_str(), P
.FullName(true).c_str());
470 _error
->Error(_("Version '%s' for '%s' was not found"),
471 ver
.c_str(), P
.FullName(true).c_str());
477 helper
.showSelectedVersion(P
, V
, ver
, verIsRel
);
481 if (pkgset
.getConstructor() != PackageSet::UNKNOWN
)
482 helper
.showErrors(errors
);
486 // FromPackage - versions from package based on fallback /*{{{*/
487 bool VersionContainerInterface::FromPackage(VersionContainerInterface
* const vci
,
489 pkgCache::PkgIterator
const &P
,
490 Version
const &fallback
,
491 CacheSetHelper
&helper
) {
492 pkgCache::VerIterator V
;
497 if (P
->VersionList
!= 0)
498 for (V
= P
.VersionList(); V
.end() != true; ++V
)
499 found
|= vci
->insert(V
);
501 helper
.canNotFindAllVer(vci
, Cache
, P
);
504 found
|= vci
->insert(getInstalledVer(Cache
, P
, helper
));
505 found
|= vci
->insert(getCandidateVer(Cache
, P
, helper
));
508 found
|= vci
->insert(getCandidateVer(Cache
, P
, helper
));
511 found
|= vci
->insert(getInstalledVer(Cache
, P
, helper
));
514 showErrors
= helper
.showErrors(false);
515 V
= getCandidateVer(Cache
, P
, helper
);
517 V
= getInstalledVer(Cache
, P
, helper
);
518 helper
.showErrors(showErrors
);
519 if (V
.end() == false)
520 found
|= vci
->insert(V
);
522 helper
.canNotFindInstCandVer(vci
, Cache
, P
);
525 showErrors
= helper
.showErrors(false);
526 V
= getInstalledVer(Cache
, P
, helper
);
528 V
= getCandidateVer(Cache
, P
, helper
);
529 helper
.showErrors(showErrors
);
530 if (V
.end() == false)
531 found
|= vci
->insert(V
);
533 helper
.canNotFindInstCandVer(vci
, Cache
, P
);
536 if (P
->VersionList
!= 0)
537 found
|= vci
->insert(P
.VersionList());
539 helper
.canNotFindNewestVer(Cache
, P
);
545 // getCandidateVer - Returns the candidate version of the given package /*{{{*/
546 pkgCache::VerIterator
VersionContainerInterface::getCandidateVer(pkgCacheFile
&Cache
,
547 pkgCache::PkgIterator
const &Pkg
, CacheSetHelper
&helper
) {
548 pkgCache::VerIterator Cand
;
549 if (Cache
.IsPolicyBuilt() == true || Cache
.IsDepCacheBuilt() == false) {
550 if (unlikely(Cache
.GetPolicy() == 0))
551 return pkgCache::VerIterator(Cache
);
552 Cand
= Cache
.GetPolicy()->GetCandidateVer(Pkg
);
554 Cand
= Cache
[Pkg
].CandidateVerIter(Cache
);
556 if (Cand
.end() == true)
557 return helper
.canNotFindCandidateVer(Cache
, Pkg
);
561 // getInstalledVer - Returns the installed version of the given package /*{{{*/
562 pkgCache::VerIterator
VersionContainerInterface::getInstalledVer(pkgCacheFile
&Cache
,
563 pkgCache::PkgIterator
const &Pkg
, CacheSetHelper
&helper
) {
564 if (Pkg
->CurrentVer
== 0)
565 return helper
.canNotFindInstalledVer(Cache
, Pkg
);
566 return Pkg
.CurrentVer();
570 // canNotFindPkgName - handle the case no package has this name /*{{{*/
571 pkgCache::PkgIterator
CacheSetHelper::canNotFindPkgName(pkgCacheFile
&Cache
,
572 std::string
const &str
) {
573 if (ShowError
== true)
574 _error
->Insert(ErrorType
, _("Unable to locate package %s"), str
.c_str());
575 return pkgCache::PkgIterator(Cache
, 0);
578 // canNotFindTask - handle the case no package is found for a task /*{{{*/
579 void CacheSetHelper::canNotFindTask(PackageContainerInterface
* const pci
, pkgCacheFile
&Cache
, std::string pattern
) {
580 if (ShowError
== true)
581 _error
->Insert(ErrorType
, _("Couldn't find task '%s'"), pattern
.c_str());
584 // canNotFindRegEx - handle the case no package is found by a regex /*{{{*/
585 void CacheSetHelper::canNotFindRegEx(PackageContainerInterface
* const pci
, pkgCacheFile
&Cache
, std::string pattern
) {
586 if (ShowError
== true)
587 _error
->Insert(ErrorType
, _("Couldn't find any package by regex '%s'"), pattern
.c_str());
590 // canNotFindPackage - handle the case no package is found from a string/*{{{*/
591 void CacheSetHelper::canNotFindPackage(PackageContainerInterface
* const pci
, pkgCacheFile
&Cache
, std::string
const &str
) {
594 // canNotFindAllVer /*{{{*/
595 void CacheSetHelper::canNotFindAllVer(VersionContainerInterface
* const vci
, pkgCacheFile
&Cache
,
596 pkgCache::PkgIterator
const &Pkg
) {
597 if (ShowError
== true)
598 _error
->Insert(ErrorType
, _("Can't select versions from package '%s' as it is purely virtual"), Pkg
.FullName(true).c_str());
601 // canNotFindInstCandVer /*{{{*/
602 void CacheSetHelper::canNotFindInstCandVer(VersionContainerInterface
* const vci
, pkgCacheFile
&Cache
,
603 pkgCache::PkgIterator
const &Pkg
) {
604 if (ShowError
== true)
605 _error
->Insert(ErrorType
, _("Can't select installed nor candidate version from package '%s' as it has neither of them"), Pkg
.FullName(true).c_str());
608 // canNotFindInstCandVer /*{{{*/
609 void CacheSetHelper::canNotFindCandInstVer(VersionContainerInterface
* const vci
, pkgCacheFile
&Cache
,
610 pkgCache::PkgIterator
const &Pkg
) {
611 if (ShowError
== true)
612 _error
->Insert(ErrorType
, _("Can't select installed nor candidate version from package '%s' as it has neither of them"), Pkg
.FullName(true).c_str());
615 // canNotFindNewestVer /*{{{*/
616 pkgCache::VerIterator
CacheSetHelper::canNotFindNewestVer(pkgCacheFile
&Cache
,
617 pkgCache::PkgIterator
const &Pkg
) {
618 if (ShowError
== true)
619 _error
->Insert(ErrorType
, _("Can't select newest version from package '%s' as it is purely virtual"), Pkg
.FullName(true).c_str());
620 return pkgCache::VerIterator(Cache
, 0);
623 // canNotFindCandidateVer /*{{{*/
624 pkgCache::VerIterator
CacheSetHelper::canNotFindCandidateVer(pkgCacheFile
&Cache
,
625 pkgCache::PkgIterator
const &Pkg
) {
626 if (ShowError
== true)
627 _error
->Insert(ErrorType
, _("Can't select candidate version from package %s as it has no candidate"), Pkg
.FullName(true).c_str());
628 return pkgCache::VerIterator(Cache
, 0);
631 // canNotFindInstalledVer /*{{{*/
632 pkgCache::VerIterator
CacheSetHelper::canNotFindInstalledVer(pkgCacheFile
&Cache
,
633 pkgCache::PkgIterator
const &Pkg
) {
634 if (ShowError
== true)
635 _error
->Insert(ErrorType
, _("Can't select installed version from package %s as it is not installed"), Pkg
.FullName(true).c_str());
636 return pkgCache::VerIterator(Cache
, 0);
639 // showTaskSelection /*{{{*/
640 void CacheSetHelper::showTaskSelection(pkgCache::PkgIterator
const &pkg
,
641 std::string
const &pattern
) {
644 // showRegExSelection /*{{{*/
645 void CacheSetHelper::showRegExSelection(pkgCache::PkgIterator
const &pkg
,
646 std::string
const &pattern
) {
649 // showSelectedVersion /*{{{*/
650 void CacheSetHelper::showSelectedVersion(pkgCache::PkgIterator
const &Pkg
,
651 pkgCache::VerIterator
const Ver
,
652 std::string
const &ver
,
653 bool const verIsRel
) {