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 // FromName - Returns the package defined by this string /*{{{*/
159 pkgCache::PkgIterator
PackageContainerInterface::FromName(pkgCacheFile
&Cache
,
160 std::string
const &str
, CacheSetHelper
&helper
) {
161 std::string pkg
= str
;
162 size_t archfound
= pkg
.find_last_of(':');
164 if (archfound
!= std::string::npos
) {
165 arch
= pkg
.substr(archfound
+1);
166 pkg
.erase(archfound
);
169 if (Cache
.GetPkgCache() == 0)
170 return pkgCache::PkgIterator(Cache
, 0);
172 pkgCache::PkgIterator
Pkg(Cache
, 0);
173 if (arch
.empty() == true) {
174 pkgCache::GrpIterator Grp
= Cache
.GetPkgCache()->FindGrp(pkg
);
175 if (Grp
.end() == false)
176 Pkg
= Grp
.FindPreferredPkg();
178 Pkg
= Cache
.GetPkgCache()->FindPkg(pkg
, arch
);
180 if (Pkg
.end() == true)
181 return helper
.canNotFindPkgName(Cache
, str
);
185 // FromGroup - Returns the package defined by this string /*{{{*/
186 bool PackageContainerInterface::FromGroup(PackageContainerInterface
* const pci
, pkgCacheFile
&Cache
,
187 std::string pkg
, CacheSetHelper
&helper
) {
188 if (unlikely(Cache
.GetPkgCache() == 0))
191 size_t const archfound
= pkg
.find_last_of(':');
193 if (archfound
!= std::string::npos
) {
194 arch
= pkg
.substr(archfound
+1);
195 pkg
.erase(archfound
);
196 if (arch
== "all" || arch
== "native")
197 arch
= _config
->Find("APT::Architecture");
200 pkgCache::GrpIterator Grp
= Cache
.GetPkgCache()->FindGrp(pkg
);
201 if (Grp
.end() == false) {
202 if (arch
.empty() == true) {
203 pkgCache::PkgIterator Pkg
= Grp
.FindPreferredPkg();
204 if (Pkg
.end() == false)
211 // for 'linux-any' return the first package matching, for 'linux-*' return all matches
212 bool const isGlobal
= arch
.find('*') != std::string::npos
;
213 APT::CacheFilter::PackageArchitectureMatchesSpecification
pams(arch
);
214 for (pkgCache::PkgIterator Pkg
= Grp
.PackageList(); Pkg
.end() == false; Pkg
= Grp
.NextPkg(Pkg
)) {
215 if (pams(Pkg
) == false)
219 if (isGlobal
== false)
227 pkgCache::PkgIterator Pkg
= helper
.canNotFindPkgName(Cache
, pkg
);
228 if (Pkg
.end() == true)
235 // FromString - Return all packages matching a specific string /*{{{*/
236 bool PackageContainerInterface::FromString(PackageContainerInterface
* const pci
, pkgCacheFile
&Cache
, std::string
const &str
, CacheSetHelper
&helper
) {
238 _error
->PushToStack();
240 if (FromGroup(pci
, Cache
, str
, helper
) == false &&
241 FromTask(pci
, Cache
, str
, helper
) == false &&
242 FromRegEx(pci
, Cache
, str
, helper
) == false)
244 helper
.canNotFindPackage(pci
, Cache
, str
);
249 _error
->RevertToStack();
251 _error
->MergeWithStack();
255 // FromCommandLine - Return all packages specified on commandline /*{{{*/
256 bool PackageContainerInterface::FromCommandLine(PackageContainerInterface
* const pci
, pkgCacheFile
&Cache
, const char **cmdline
, CacheSetHelper
&helper
) {
258 for (const char **I
= cmdline
; *I
!= 0; ++I
)
259 found
|= PackageContainerInterface::FromString(pci
, Cache
, *I
, helper
);
263 // FromModifierCommandLine - helper doing the work for PKG:GroupedFromCommandLine /*{{{*/
264 bool PackageContainerInterface::FromModifierCommandLine(unsigned short &modID
, PackageContainerInterface
* const pci
,
265 pkgCacheFile
&Cache
, const char * cmdline
,
266 std::list
<Modifier
> const &mods
, CacheSetHelper
&helper
) {
267 std::string str
= cmdline
;
268 unsigned short fallback
= modID
;
269 bool modifierPresent
= false;
270 for (std::list
<Modifier
>::const_iterator mod
= mods
.begin();
271 mod
!= mods
.end(); ++mod
) {
272 size_t const alength
= strlen(mod
->Alias
);
274 case Modifier::POSTFIX
:
275 if (str
.compare(str
.length() - alength
, alength
,
276 mod
->Alias
, 0, alength
) != 0)
278 str
.erase(str
.length() - alength
);
281 case Modifier::PREFIX
:
286 modifierPresent
= true;
289 if (modifierPresent
== true) {
290 bool const errors
= helper
.showErrors(false);
291 pkgCache::PkgIterator Pkg
= FromName(Cache
, cmdline
, helper
);
292 helper
.showErrors(errors
);
293 if (Pkg
.end() == false) {
299 return FromString(pci
, Cache
, str
, helper
);
302 // FromModifierCommandLine - helper doing the work for VER:GroupedFromCommandLine /*{{{*/
303 bool VersionContainerInterface::FromModifierCommandLine(unsigned short &modID
,
304 VersionContainerInterface
* const vci
,
305 pkgCacheFile
&Cache
, const char * cmdline
,
306 std::list
<Modifier
> const &mods
,
307 CacheSetHelper
&helper
) {
308 Version select
= NEWEST
;
309 std::string str
= cmdline
;
310 bool modifierPresent
= false;
311 unsigned short fallback
= modID
;
312 for (std::list
<Modifier
>::const_iterator mod
= mods
.begin();
313 mod
!= mods
.end(); ++mod
) {
314 if (modID
== fallback
&& mod
->ID
== fallback
)
315 select
= mod
->SelectVersion
;
316 size_t const alength
= strlen(mod
->Alias
);
318 case Modifier::POSTFIX
:
319 if (str
.compare(str
.length() - alength
, alength
,
320 mod
->Alias
, 0, alength
) != 0)
322 str
.erase(str
.length() - alength
);
324 select
= mod
->SelectVersion
;
326 case Modifier::PREFIX
:
331 modifierPresent
= true;
334 if (modifierPresent
== true) {
335 bool const errors
= helper
.showErrors(false);
336 bool const found
= VersionContainerInterface::FromString(vci
, Cache
, cmdline
, select
, helper
, true);
337 helper
.showErrors(errors
);
343 return FromString(vci
, Cache
, str
, select
, helper
);
346 // FromCommandLine - Return all versions specified on commandline /*{{{*/
347 bool VersionContainerInterface::FromCommandLine(VersionContainerInterface
* const vci
,
348 pkgCacheFile
&Cache
, const char **cmdline
,
349 Version
const &fallback
, CacheSetHelper
&helper
) {
351 for (const char **I
= cmdline
; *I
!= 0; ++I
)
352 found
|= VersionContainerInterface::FromString(vci
, Cache
, *I
, fallback
, helper
);
356 // FromString - Returns all versions spedcified by a string /*{{{*/
357 bool VersionContainerInterface::FromString(VersionContainerInterface
* const vci
,
358 pkgCacheFile
&Cache
, std::string pkg
,
359 Version
const &fallback
, CacheSetHelper
&helper
,
360 bool const onlyFromName
) {
362 bool verIsRel
= false;
363 size_t const vertag
= pkg
.find_last_of("/=");
364 if (vertag
!= std::string::npos
) {
365 ver
= pkg
.substr(vertag
+1);
366 verIsRel
= (pkg
[vertag
] == '/');
370 if (onlyFromName
== false)
371 PackageContainerInterface::FromString(&pkgset
, Cache
, pkg
, helper
);
373 pkgset
.insert(PackageContainerInterface::FromName(Cache
, pkg
, helper
));
377 if (pkgset
.getConstructor() != PackageSet::UNKNOWN
)
378 errors
= helper
.showErrors(false);
381 for (PackageSet::const_iterator P
= pkgset
.begin();
382 P
!= pkgset
.end(); ++P
) {
383 if (vertag
== std::string::npos
) {
384 found
|= VersionContainerInterface::FromPackage(vci
, Cache
, P
, fallback
, helper
);
387 pkgCache::VerIterator V
;
388 if (ver
== "installed")
389 V
= getInstalledVer(Cache
, P
, helper
);
390 else if (ver
== "candidate")
391 V
= getCandidateVer(Cache
, P
, helper
);
392 else if (ver
== "newest") {
393 if (P
->VersionList
!= 0)
396 V
= helper
.canNotFindNewestVer(Cache
, P
);
398 pkgVersionMatch
Match(ver
, (verIsRel
== true ? pkgVersionMatch::Release
:
399 pkgVersionMatch::Version
));
401 if (V
.end() == true) {
402 if (verIsRel
== true)
403 _error
->Error(_("Release '%s' for '%s' was not found"),
404 ver
.c_str(), P
.FullName(true).c_str());
406 _error
->Error(_("Version '%s' for '%s' was not found"),
407 ver
.c_str(), P
.FullName(true).c_str());
413 helper
.showSelectedVersion(P
, V
, ver
, verIsRel
);
417 if (pkgset
.getConstructor() != PackageSet::UNKNOWN
)
418 helper
.showErrors(errors
);
422 // FromPackage - versions from package based on fallback /*{{{*/
423 bool VersionContainerInterface::FromPackage(VersionContainerInterface
* const vci
,
425 pkgCache::PkgIterator
const &P
,
426 Version
const &fallback
,
427 CacheSetHelper
&helper
) {
428 pkgCache::VerIterator V
;
433 if (P
->VersionList
!= 0)
434 for (V
= P
.VersionList(); V
.end() != true; ++V
)
435 found
|= vci
->insert(V
);
437 helper
.canNotFindAllVer(vci
, Cache
, P
);
440 found
|= vci
->insert(getInstalledVer(Cache
, P
, helper
));
441 found
|= vci
->insert(getCandidateVer(Cache
, P
, helper
));
444 found
|= vci
->insert(getCandidateVer(Cache
, P
, helper
));
447 found
|= vci
->insert(getInstalledVer(Cache
, P
, helper
));
450 showErrors
= helper
.showErrors(false);
451 V
= getCandidateVer(Cache
, P
, helper
);
453 V
= getInstalledVer(Cache
, P
, helper
);
454 helper
.showErrors(showErrors
);
455 if (V
.end() == false)
456 found
|= vci
->insert(V
);
458 helper
.canNotFindInstCandVer(vci
, Cache
, P
);
461 showErrors
= helper
.showErrors(false);
462 V
= getInstalledVer(Cache
, P
, helper
);
464 V
= getCandidateVer(Cache
, P
, helper
);
465 helper
.showErrors(showErrors
);
466 if (V
.end() == false)
467 found
|= vci
->insert(V
);
469 helper
.canNotFindInstCandVer(vci
, Cache
, P
);
472 if (P
->VersionList
!= 0)
473 found
|= vci
->insert(P
.VersionList());
475 helper
.canNotFindNewestVer(Cache
, P
);
481 // getCandidateVer - Returns the candidate version of the given package /*{{{*/
482 pkgCache::VerIterator
VersionContainerInterface::getCandidateVer(pkgCacheFile
&Cache
,
483 pkgCache::PkgIterator
const &Pkg
, CacheSetHelper
&helper
) {
484 pkgCache::VerIterator Cand
;
485 if (Cache
.IsPolicyBuilt() == true || Cache
.IsDepCacheBuilt() == false) {
486 if (unlikely(Cache
.GetPolicy() == 0))
487 return pkgCache::VerIterator(Cache
);
488 Cand
= Cache
.GetPolicy()->GetCandidateVer(Pkg
);
490 Cand
= Cache
[Pkg
].CandidateVerIter(Cache
);
492 if (Cand
.end() == true)
493 return helper
.canNotFindCandidateVer(Cache
, Pkg
);
497 // getInstalledVer - Returns the installed version of the given package /*{{{*/
498 pkgCache::VerIterator
VersionContainerInterface::getInstalledVer(pkgCacheFile
&Cache
,
499 pkgCache::PkgIterator
const &Pkg
, CacheSetHelper
&helper
) {
500 if (Pkg
->CurrentVer
== 0)
501 return helper
.canNotFindInstalledVer(Cache
, Pkg
);
502 return Pkg
.CurrentVer();
506 // canNotFindPkgName - handle the case no package has this name /*{{{*/
507 pkgCache::PkgIterator
CacheSetHelper::canNotFindPkgName(pkgCacheFile
&Cache
,
508 std::string
const &str
) {
509 if (ShowError
== true)
510 _error
->Insert(ErrorType
, _("Unable to locate package %s"), str
.c_str());
511 return pkgCache::PkgIterator(Cache
, 0);
514 // canNotFindTask - handle the case no package is found for a task /*{{{*/
515 void CacheSetHelper::canNotFindTask(PackageContainerInterface
* const pci
, pkgCacheFile
&Cache
, std::string pattern
) {
516 if (ShowError
== true)
517 _error
->Insert(ErrorType
, _("Couldn't find task '%s'"), pattern
.c_str());
520 // canNotFindRegEx - handle the case no package is found by a regex /*{{{*/
521 void CacheSetHelper::canNotFindRegEx(PackageContainerInterface
* const pci
, pkgCacheFile
&Cache
, std::string pattern
) {
522 if (ShowError
== true)
523 _error
->Insert(ErrorType
, _("Couldn't find any package by regex '%s'"), pattern
.c_str());
526 // canNotFindPackage - handle the case no package is found from a string/*{{{*/
527 void CacheSetHelper::canNotFindPackage(PackageContainerInterface
* const pci
, pkgCacheFile
&Cache
, std::string
const &str
) {
530 // canNotFindAllVer /*{{{*/
531 void CacheSetHelper::canNotFindAllVer(VersionContainerInterface
* const vci
, pkgCacheFile
&Cache
,
532 pkgCache::PkgIterator
const &Pkg
) {
533 if (ShowError
== true)
534 _error
->Insert(ErrorType
, _("Can't select versions from package '%s' as it is purely virtual"), Pkg
.FullName(true).c_str());
537 // canNotFindInstCandVer /*{{{*/
538 void CacheSetHelper::canNotFindInstCandVer(VersionContainerInterface
* const vci
, pkgCacheFile
&Cache
,
539 pkgCache::PkgIterator
const &Pkg
) {
540 if (ShowError
== true)
541 _error
->Insert(ErrorType
, _("Can't select installed nor candidate version from package '%s' as it has neither of them"), Pkg
.FullName(true).c_str());
544 // canNotFindInstCandVer /*{{{*/
545 void CacheSetHelper::canNotFindCandInstVer(VersionContainerInterface
* const vci
, pkgCacheFile
&Cache
,
546 pkgCache::PkgIterator
const &Pkg
) {
547 if (ShowError
== true)
548 _error
->Insert(ErrorType
, _("Can't select installed nor candidate version from package '%s' as it has neither of them"), Pkg
.FullName(true).c_str());
551 // canNotFindNewestVer /*{{{*/
552 pkgCache::VerIterator
CacheSetHelper::canNotFindNewestVer(pkgCacheFile
&Cache
,
553 pkgCache::PkgIterator
const &Pkg
) {
554 if (ShowError
== true)
555 _error
->Insert(ErrorType
, _("Can't select newest version from package '%s' as it is purely virtual"), Pkg
.FullName(true).c_str());
556 return pkgCache::VerIterator(Cache
, 0);
559 // canNotFindCandidateVer /*{{{*/
560 pkgCache::VerIterator
CacheSetHelper::canNotFindCandidateVer(pkgCacheFile
&Cache
,
561 pkgCache::PkgIterator
const &Pkg
) {
562 if (ShowError
== true)
563 _error
->Insert(ErrorType
, _("Can't select candidate version from package %s as it has no candidate"), Pkg
.FullName(true).c_str());
564 return pkgCache::VerIterator(Cache
, 0);
567 // canNotFindInstalledVer /*{{{*/
568 pkgCache::VerIterator
CacheSetHelper::canNotFindInstalledVer(pkgCacheFile
&Cache
,
569 pkgCache::PkgIterator
const &Pkg
) {
570 if (ShowError
== true)
571 _error
->Insert(ErrorType
, _("Can't select installed version from package %s as it is not installed"), Pkg
.FullName(true).c_str());
572 return pkgCache::VerIterator(Cache
, 0);
575 // showTaskSelection /*{{{*/
576 void CacheSetHelper::showTaskSelection(pkgCache::PkgIterator
const &pkg
,
577 std::string
const &pattern
) {
580 // showRegExSelection /*{{{*/
581 void CacheSetHelper::showRegExSelection(pkgCache::PkgIterator
const &pkg
,
582 std::string
const &pattern
) {
585 // showSelectedVersion /*{{{*/
586 void CacheSetHelper::showSelectedVersion(pkgCache::PkgIterator
const &Pkg
,
587 pkgCache::VerIterator
const Ver
,
588 std::string
const &ver
,
589 bool const verIsRel
) {