]>
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
.IsDepCacheBuilt() == true)
420 Cand
= Cache
[Pkg
].CandidateVerIter(Cache
);
422 if (unlikely(Cache
.GetPolicy() == 0))
423 return pkgCache::VerIterator(Cache
);
424 Cand
= Cache
.GetPolicy()->GetCandidateVer(Pkg
);
426 if (Cand
.end() == true)
427 return helper
.canNotFindCandidateVer(Cache
, Pkg
);
431 // getInstalledVer - Returns the installed version of the given package /*{{{*/
432 pkgCache::VerIterator
VersionSet::getInstalledVer(pkgCacheFile
&Cache
,
433 pkgCache::PkgIterator
const &Pkg
, CacheSetHelper
&helper
) {
434 if (Pkg
->CurrentVer
== 0)
435 return helper
.canNotFindInstalledVer(Cache
, Pkg
);
436 return Pkg
.CurrentVer();
439 // canNotFindPkgName - handle the case no package has this name /*{{{*/
440 pkgCache::PkgIterator
CacheSetHelper::canNotFindPkgName(pkgCacheFile
&Cache
,
441 std::string
const &str
) {
442 if (ShowError
== true)
443 _error
->Error(_("Unable to locate package %s"), str
.c_str());
444 return pkgCache::PkgIterator(Cache
, 0);
447 // canNotFindTask - handle the case no package is found for a task /*{{{*/
448 PackageSet
CacheSetHelper::canNotFindTask(pkgCacheFile
&Cache
, std::string pattern
) {
449 if (ShowError
== true)
450 _error
->Error(_("Couldn't find task '%s'"), pattern
.c_str());
454 // canNotFindRegEx - handle the case no package is found by a regex /*{{{*/
455 PackageSet
CacheSetHelper::canNotFindRegEx(pkgCacheFile
&Cache
, std::string pattern
) {
456 if (ShowError
== true)
457 _error
->Error(_("Couldn't find any package by regex '%s'"), pattern
.c_str());
461 // canNotFindPackage - handle the case no package is found from a string/*{{{*/
462 PackageSet
CacheSetHelper::canNotFindPackage(pkgCacheFile
&Cache
, std::string
const &str
) {
466 // canNotFindAllVer /*{{{*/
467 VersionSet
CacheSetHelper::canNotFindAllVer(pkgCacheFile
&Cache
,
468 pkgCache::PkgIterator
const &Pkg
) {
469 if (ShowError
== true)
470 _error
->Error(_("Can't select versions from package '%s' as it purely virtual"), Pkg
.FullName(true).c_str());
474 // canNotFindInstCandVer /*{{{*/
475 VersionSet
CacheSetHelper::canNotFindInstCandVer(pkgCacheFile
&Cache
,
476 pkgCache::PkgIterator
const &Pkg
) {
477 if (ShowError
== true)
478 _error
->Error(_("Can't select installed nor candidate version from package '%s' as it has neither of them"), Pkg
.FullName(true).c_str());
482 // canNotFindInstCandVer /*{{{*/
483 VersionSet
CacheSetHelper::canNotFindCandInstVer(pkgCacheFile
&Cache
,
484 pkgCache::PkgIterator
const &Pkg
) {
485 if (ShowError
== true)
486 _error
->Error(_("Can't select installed nor candidate version from package '%s' as it has neither of them"), Pkg
.FullName(true).c_str());
490 // canNotFindNewestVer /*{{{*/
491 pkgCache::VerIterator
CacheSetHelper::canNotFindNewestVer(pkgCacheFile
&Cache
,
492 pkgCache::PkgIterator
const &Pkg
) {
493 if (ShowError
== true)
494 _error
->Error(_("Can't select newest version from package '%s' as it is purely virtual"), Pkg
.FullName(true).c_str());
495 return pkgCache::VerIterator(Cache
, 0);
498 // canNotFindCandidateVer /*{{{*/
499 pkgCache::VerIterator
CacheSetHelper::canNotFindCandidateVer(pkgCacheFile
&Cache
,
500 pkgCache::PkgIterator
const &Pkg
) {
501 if (ShowError
== true)
502 _error
->Error(_("Can't select candidate version from package %s as it has no candidate"), Pkg
.FullName(true).c_str());
503 return pkgCache::VerIterator(Cache
, 0);
506 // canNotFindInstalledVer /*{{{*/
507 pkgCache::VerIterator
CacheSetHelper::canNotFindInstalledVer(pkgCacheFile
&Cache
,
508 pkgCache::PkgIterator
const &Pkg
) {
509 if (ShowError
== true)
510 _error
->Error(_("Can't select installed version from package %s as it is not installed"), Pkg
.FullName(true).c_str());
511 return pkgCache::VerIterator(Cache
, 0);