]>
git.saurik.com Git - apt.git/blob - cmdline/cacheset.cc
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 /*{{{*/
12 #include <apt-pkg/aptconfiguration.h>
13 #include <apt-pkg/error.h>
14 #include <apt-pkg/strutl.h>
15 #include <apt-pkg/versionmatch.h>
26 // FromTask - Return all packages in the cache from a specific task /*{{{*/
27 PackageSet
PackageSet::FromTask(pkgCacheFile
&Cache
, std::string pattern
, CacheSetHelper
&helper
) {
28 size_t const archfound
= pattern
.find_last_of(':');
29 std::string arch
= "native";
30 if (archfound
!= std::string::npos
) {
31 arch
= pattern
.substr(archfound
+1);
32 pattern
.erase(archfound
);
35 if (pattern
[pattern
.length() -1] != '^')
36 return APT::PackageSet(TASK
);
37 pattern
.erase(pattern
.length()-1);
39 if (unlikely(Cache
.GetPkgCache() == 0 || Cache
.GetDepCache() == 0))
40 return APT::PackageSet(TASK
);
42 PackageSet
pkgset(TASK
);
44 pkgRecords
Recs(Cache
);
46 // build regexp for the task
49 snprintf(S
, sizeof(S
), "^Task:.*[, ]%s([, ]|$)", pattern
.c_str());
50 if(regcomp(&Pattern
,S
, REG_EXTENDED
| REG_NOSUB
| REG_NEWLINE
) != 0) {
51 _error
->Error("Failed to compile task regexp");
55 for (pkgCache::GrpIterator Grp
= Cache
->GrpBegin(); Grp
.end() == false; ++Grp
) {
56 pkgCache::PkgIterator Pkg
= Grp
.FindPkg(arch
);
57 if (Pkg
.end() == true)
59 pkgCache::VerIterator ver
= Cache
[Pkg
].CandidateVerIter(Cache
);
63 pkgRecords::Parser
&parser
= Recs
.Lookup(ver
.FileList());
64 const char *start
, *end
;
65 parser
.GetRec(start
,end
);
66 unsigned int const length
= end
- start
;
68 strncpy(buf
, start
, length
);
70 if (regexec(&Pattern
, buf
, 0, 0, 0) != 0)
77 if (pkgset
.empty() == true)
78 return helper
.canNotFindTask(Cache
, pattern
);
80 helper
.showTaskSelection(pkgset
, pattern
);
84 // FromRegEx - Return all packages in the cache matching a pattern /*{{{*/
85 PackageSet
PackageSet::FromRegEx(pkgCacheFile
&Cache
, std::string pattern
, CacheSetHelper
&helper
) {
86 static const char * const isregex
= ".?+*|[^$";
87 if (pattern
.find_first_of(isregex
) == std::string::npos
)
88 return PackageSet(REGEX
);
90 size_t archfound
= pattern
.find_last_of(':');
91 std::string arch
= "native";
92 if (archfound
!= std::string::npos
) {
93 arch
= pattern
.substr(archfound
+1);
94 if (arch
.find_first_of(isregex
) == std::string::npos
)
95 pattern
.erase(archfound
);
102 if ((Res
= regcomp(&Pattern
, pattern
.c_str() , REG_EXTENDED
| REG_ICASE
| REG_NOSUB
)) != 0) {
104 regerror(Res
, &Pattern
, Error
, sizeof(Error
));
105 _error
->Error(_("Regex compilation error - %s"), Error
);
106 return PackageSet(REGEX
);
109 if (unlikely(Cache
.GetPkgCache() == 0))
110 return PackageSet(REGEX
);
112 PackageSet
pkgset(REGEX
);
113 for (pkgCache::GrpIterator Grp
= Cache
.GetPkgCache()->GrpBegin(); Grp
.end() == false; ++Grp
)
115 if (regexec(&Pattern
, Grp
.Name(), 0, 0, 0) != 0)
117 pkgCache::PkgIterator Pkg
= Grp
.FindPkg(arch
);
118 if (Pkg
.end() == true) {
119 if (archfound
== std::string::npos
) {
120 std::vector
<std::string
> archs
= APT::Configuration::getArchitectures();
121 for (std::vector
<std::string
>::const_iterator a
= archs
.begin();
122 a
!= archs
.end() && Pkg
.end() != true; ++a
)
123 Pkg
= Grp
.FindPkg(*a
);
125 if (Pkg
.end() == true)
133 if (pkgset
.empty() == true)
134 return helper
.canNotFindRegEx(Cache
, pattern
);
136 helper
.showRegExSelection(pkgset
, pattern
);
140 // FromName - Returns the package defined by this string /*{{{*/
141 pkgCache::PkgIterator
PackageSet::FromName(pkgCacheFile
&Cache
,
142 std::string
const &str
, CacheSetHelper
&helper
) {
143 std::string pkg
= str
;
144 size_t archfound
= pkg
.find_last_of(':');
146 if (archfound
!= std::string::npos
) {
147 arch
= pkg
.substr(archfound
+1);
148 pkg
.erase(archfound
);
151 if (Cache
.GetPkgCache() == 0)
152 return pkgCache::PkgIterator(Cache
, 0);
154 pkgCache::PkgIterator
Pkg(Cache
, 0);
155 if (arch
.empty() == true) {
156 pkgCache::GrpIterator Grp
= Cache
.GetPkgCache()->FindGrp(pkg
);
157 if (Grp
.end() == false)
158 Pkg
= Grp
.FindPreferredPkg();
160 Pkg
= Cache
.GetPkgCache()->FindPkg(pkg
, arch
);
162 if (Pkg
.end() == true)
163 return helper
.canNotFindPkgName(Cache
, str
);
167 // GroupedFromCommandLine - Return all versions specified on commandline/*{{{*/
168 std::map
<unsigned short, PackageSet
> PackageSet::GroupedFromCommandLine(
169 pkgCacheFile
&Cache
, const char **cmdline
,
170 std::list
<PackageSet::Modifier
> const &mods
,
171 unsigned short const &fallback
, CacheSetHelper
&helper
) {
172 std::map
<unsigned short, PackageSet
> pkgsets
;
173 for (const char **I
= cmdline
; *I
!= 0; ++I
) {
174 unsigned short modID
= fallback
;
175 std::string str
= *I
;
176 bool modifierPresent
= false;
177 for (std::list
<PackageSet::Modifier
>::const_iterator mod
= mods
.begin();
178 mod
!= mods
.end(); ++mod
) {
179 size_t const alength
= strlen(mod
->Alias
);
181 case PackageSet::Modifier::POSTFIX
:
182 if (str
.compare(str
.length() - alength
, alength
,
183 mod
->Alias
, 0, alength
) != 0)
185 str
.erase(str
.length() - alength
);
188 case PackageSet::Modifier::PREFIX
:
190 case PackageSet::Modifier::NONE
:
193 modifierPresent
= true;
196 if (modifierPresent
== true) {
197 bool const errors
= helper
.showErrors(false);
198 pkgCache::PkgIterator Pkg
= FromName(Cache
, *I
, helper
);
199 helper
.showErrors(errors
);
200 if (Pkg
.end() == false) {
201 pkgsets
[fallback
].insert(Pkg
);
205 pkgsets
[modID
].insert(PackageSet::FromString(Cache
, str
, helper
));
210 // FromCommandLine - Return all packages specified on commandline /*{{{*/
211 PackageSet
PackageSet::FromCommandLine(pkgCacheFile
&Cache
, const char **cmdline
, CacheSetHelper
&helper
) {
213 for (const char **I
= cmdline
; *I
!= 0; ++I
) {
214 PackageSet pset
= FromString(Cache
, *I
, helper
);
215 pkgset
.insert(pset
.begin(), pset
.end());
220 // FromString - Return all packages matching a specific string /*{{{*/
221 PackageSet
PackageSet::FromString(pkgCacheFile
&Cache
, std::string
const &str
, CacheSetHelper
&helper
) {
222 _error
->PushToStack();
225 pkgCache::PkgIterator Pkg
= FromName(Cache
, str
, helper
);
226 if (Pkg
.end() == false)
229 pkgset
= FromTask(Cache
, str
, helper
);
230 if (pkgset
.empty() == true) {
231 pkgset
= FromRegEx(Cache
, str
, helper
);
232 if (pkgset
.empty() == true)
233 pkgset
= helper
.canNotFindPackage(Cache
, str
);
237 if (pkgset
.empty() == false)
238 _error
->RevertToStack();
240 _error
->MergeWithStack();
244 // GroupedFromCommandLine - Return all versions specified on commandline/*{{{*/
245 std::map
<unsigned short, VersionSet
> VersionSet::GroupedFromCommandLine(
246 pkgCacheFile
&Cache
, const char **cmdline
,
247 std::list
<VersionSet::Modifier
> const &mods
,
248 unsigned short const &fallback
, CacheSetHelper
&helper
) {
249 std::map
<unsigned short, VersionSet
> versets
;
250 for (const char **I
= cmdline
; *I
!= 0; ++I
) {
251 unsigned short modID
= fallback
;
252 VersionSet::Version select
= VersionSet::NEWEST
;
253 std::string str
= *I
;
254 bool modifierPresent
= false;
255 for (std::list
<VersionSet::Modifier
>::const_iterator mod
= mods
.begin();
256 mod
!= mods
.end(); ++mod
) {
257 if (modID
== fallback
&& mod
->ID
== fallback
)
258 select
= mod
->SelectVersion
;
259 size_t const alength
= strlen(mod
->Alias
);
261 case VersionSet::Modifier::POSTFIX
:
262 if (str
.compare(str
.length() - alength
, alength
,
263 mod
->Alias
, 0, alength
) != 0)
265 str
.erase(str
.length() - alength
);
267 select
= mod
->SelectVersion
;
269 case VersionSet::Modifier::PREFIX
:
271 case VersionSet::Modifier::NONE
:
274 modifierPresent
= true;
278 if (modifierPresent
== true) {
279 bool const errors
= helper
.showErrors(false);
280 VersionSet
const vset
= VersionSet::FromString(Cache
, std::string(*I
), select
, helper
, true);
281 helper
.showErrors(errors
);
282 if (vset
.empty() == false) {
283 versets
[fallback
].insert(vset
);
287 versets
[modID
].insert(VersionSet::FromString(Cache
, str
, select
, helper
));
292 // FromCommandLine - Return all versions specified on commandline /*{{{*/
293 APT::VersionSet
VersionSet::FromCommandLine(pkgCacheFile
&Cache
, const char **cmdline
,
294 APT::VersionSet::Version
const &fallback
, CacheSetHelper
&helper
) {
296 for (const char **I
= cmdline
; *I
!= 0; ++I
)
297 verset
.insert(VersionSet::FromString(Cache
, *I
, fallback
, helper
));
301 // FromString - Returns all versions spedcified by a string /*{{{*/
302 APT::VersionSet
VersionSet::FromString(pkgCacheFile
&Cache
, std::string pkg
,
303 APT::VersionSet::Version
const &fallback
, CacheSetHelper
&helper
,
304 bool const &onlyFromName
) {
306 bool verIsRel
= false;
307 size_t const vertag
= pkg
.find_last_of("/=");
308 if (vertag
!= string::npos
) {
309 ver
= pkg
.substr(vertag
+1);
310 verIsRel
= (pkg
[vertag
] == '/');
314 if (onlyFromName
== false)
315 pkgset
= PackageSet::FromString(Cache
, pkg
, helper
);
317 pkgset
.insert(PackageSet::FromName(Cache
, pkg
, helper
));
322 if (pkgset
.getConstructor() != PackageSet::UNKNOWN
)
323 errors
= helper
.showErrors(false);
324 for (PackageSet::const_iterator P
= pkgset
.begin();
325 P
!= pkgset
.end(); ++P
) {
326 if (vertag
== string::npos
) {
327 verset
.insert(VersionSet::FromPackage(Cache
, P
, fallback
, helper
));
330 pkgCache::VerIterator V
;
331 if (ver
== "installed")
332 V
= getInstalledVer(Cache
, P
, helper
);
333 else if (ver
== "candidate")
334 V
= getCandidateVer(Cache
, P
, helper
);
336 pkgVersionMatch
Match(ver
, (verIsRel
== true ? pkgVersionMatch::Release
:
337 pkgVersionMatch::Version
));
339 if (V
.end() == true) {
340 if (verIsRel
== true)
341 _error
->Error(_("Release '%s' for '%s' was not found"),
342 ver
.c_str(), P
.FullName(true).c_str());
344 _error
->Error(_("Version '%s' for '%s' was not found"),
345 ver
.c_str(), P
.FullName(true).c_str());
351 helper
.showSelectedVersion(P
, V
, ver
, verIsRel
);
354 if (pkgset
.getConstructor() != PackageSet::UNKNOWN
)
355 helper
.showErrors(errors
);
359 // FromPackage - versions from package based on fallback /*{{{*/
360 VersionSet
VersionSet::FromPackage(pkgCacheFile
&Cache
, pkgCache::PkgIterator
const &P
,
361 VersionSet::Version
const &fallback
, CacheSetHelper
&helper
) {
363 pkgCache::VerIterator V
;
366 case VersionSet::ALL
:
367 if (P
->VersionList
!= 0)
368 for (V
= P
.VersionList(); V
.end() != true; ++V
)
371 verset
.insert(helper
.canNotFindAllVer(Cache
, P
));
373 case VersionSet::CANDANDINST
:
374 verset
.insert(getInstalledVer(Cache
, P
, helper
));
375 verset
.insert(getCandidateVer(Cache
, P
, helper
));
377 case VersionSet::CANDIDATE
:
378 verset
.insert(getCandidateVer(Cache
, P
, helper
));
380 case VersionSet::INSTALLED
:
381 verset
.insert(getInstalledVer(Cache
, P
, helper
));
383 case VersionSet::CANDINST
:
384 showErrors
= helper
.showErrors(false);
385 V
= getCandidateVer(Cache
, P
, helper
);
387 V
= getInstalledVer(Cache
, P
, helper
);
388 helper
.showErrors(showErrors
);
389 if (V
.end() == false)
392 verset
.insert(helper
.canNotFindInstCandVer(Cache
, P
));
394 case VersionSet::INSTCAND
:
395 showErrors
= helper
.showErrors(false);
396 V
= getInstalledVer(Cache
, P
, helper
);
398 V
= getCandidateVer(Cache
, P
, helper
);
399 helper
.showErrors(showErrors
);
400 if (V
.end() == false)
403 verset
.insert(helper
.canNotFindInstCandVer(Cache
, P
));
405 case VersionSet::NEWEST
:
406 if (P
->VersionList
!= 0)
407 verset
.insert(P
.VersionList());
409 verset
.insert(helper
.canNotFindNewestVer(Cache
, P
));
415 // getCandidateVer - Returns the candidate version of the given package /*{{{*/
416 pkgCache::VerIterator
VersionSet::getCandidateVer(pkgCacheFile
&Cache
,
417 pkgCache::PkgIterator
const &Pkg
, CacheSetHelper
&helper
) {
418 pkgCache::VerIterator Cand
;
419 if (Cache
.IsPolicyBuilt() == true || Cache
.IsDepCacheBuilt() == false)
421 if (unlikely(Cache
.GetPolicy() == 0))
422 return pkgCache::VerIterator(Cache
);
423 Cand
= Cache
.GetPolicy()->GetCandidateVer(Pkg
);
425 Cand
= Cache
[Pkg
].CandidateVerIter(Cache
);
427 if (Cand
.end() == true)
428 return helper
.canNotFindCandidateVer(Cache
, Pkg
);
432 // getInstalledVer - Returns the installed version of the given package /*{{{*/
433 pkgCache::VerIterator
VersionSet::getInstalledVer(pkgCacheFile
&Cache
,
434 pkgCache::PkgIterator
const &Pkg
, CacheSetHelper
&helper
) {
435 if (Pkg
->CurrentVer
== 0)
436 return helper
.canNotFindInstalledVer(Cache
, Pkg
);
437 return Pkg
.CurrentVer();
440 // canNotFindPkgName - handle the case no package has this name /*{{{*/
441 pkgCache::PkgIterator
CacheSetHelper::canNotFindPkgName(pkgCacheFile
&Cache
,
442 std::string
const &str
) {
443 if (ShowError
== true)
444 _error
->Error(_("Unable to locate package %s"), str
.c_str());
445 return pkgCache::PkgIterator(Cache
, 0);
448 // canNotFindTask - handle the case no package is found for a task /*{{{*/
449 PackageSet
CacheSetHelper::canNotFindTask(pkgCacheFile
&Cache
, std::string pattern
) {
450 if (ShowError
== true)
451 _error
->Error(_("Couldn't find task '%s'"), pattern
.c_str());
455 // canNotFindRegEx - handle the case no package is found by a regex /*{{{*/
456 PackageSet
CacheSetHelper::canNotFindRegEx(pkgCacheFile
&Cache
, std::string pattern
) {
457 if (ShowError
== true)
458 _error
->Error(_("Couldn't find any package by regex '%s'"), pattern
.c_str());
462 // canNotFindPackage - handle the case no package is found from a string/*{{{*/
463 PackageSet
CacheSetHelper::canNotFindPackage(pkgCacheFile
&Cache
, std::string
const &str
) {
467 // canNotFindAllVer /*{{{*/
468 VersionSet
CacheSetHelper::canNotFindAllVer(pkgCacheFile
&Cache
,
469 pkgCache::PkgIterator
const &Pkg
) {
470 if (ShowError
== true)
471 _error
->Error(_("Can't select versions from package '%s' as it purely virtual"), Pkg
.FullName(true).c_str());
475 // canNotFindInstCandVer /*{{{*/
476 VersionSet
CacheSetHelper::canNotFindInstCandVer(pkgCacheFile
&Cache
,
477 pkgCache::PkgIterator
const &Pkg
) {
478 if (ShowError
== true)
479 _error
->Error(_("Can't select installed nor candidate version from package '%s' as it has neither of them"), Pkg
.FullName(true).c_str());
483 // canNotFindInstCandVer /*{{{*/
484 VersionSet
CacheSetHelper::canNotFindCandInstVer(pkgCacheFile
&Cache
,
485 pkgCache::PkgIterator
const &Pkg
) {
486 if (ShowError
== true)
487 _error
->Error(_("Can't select installed nor candidate version from package '%s' as it has neither of them"), Pkg
.FullName(true).c_str());
491 // canNotFindNewestVer /*{{{*/
492 pkgCache::VerIterator
CacheSetHelper::canNotFindNewestVer(pkgCacheFile
&Cache
,
493 pkgCache::PkgIterator
const &Pkg
) {
494 if (ShowError
== true)
495 _error
->Error(_("Can't select newest version from package '%s' as it is purely virtual"), Pkg
.FullName(true).c_str());
496 return pkgCache::VerIterator(Cache
, 0);
499 // canNotFindCandidateVer /*{{{*/
500 pkgCache::VerIterator
CacheSetHelper::canNotFindCandidateVer(pkgCacheFile
&Cache
,
501 pkgCache::PkgIterator
const &Pkg
) {
502 if (ShowError
== true)
503 _error
->Error(_("Can't select candidate version from package %s as it has no candidate"), Pkg
.FullName(true).c_str());
504 return pkgCache::VerIterator(Cache
, 0);
507 // canNotFindInstalledVer /*{{{*/
508 pkgCache::VerIterator
CacheSetHelper::canNotFindInstalledVer(pkgCacheFile
&Cache
,
509 pkgCache::PkgIterator
const &Pkg
) {
510 if (ShowError
== true)
511 _error
->Error(_("Can't select installed version from package %s as it is not installed"), Pkg
.FullName(true).c_str());
512 return pkgCache::VerIterator(Cache
, 0);