]>
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
) {
29 if (Cache
.BuildCaches() == false || Cache
.BuildDepCache() == false)
32 size_t archfound
= pattern
.find_last_of(':');
33 std::string arch
= "native";
34 if (archfound
!= std::string::npos
) {
35 arch
= pattern
.substr(archfound
+1);
36 pattern
.erase(archfound
);
39 if (pattern
[pattern
.length() -1] != '^')
41 pattern
.erase(pattern
.length()-1);
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
) {
87 static const char * const isregex
= ".?+*|[^$";
89 if (pattern
.find_first_of(isregex
) == std::string::npos
)
92 size_t archfound
= pattern
.find_last_of(':');
93 std::string arch
= "native";
94 if (archfound
!= std::string::npos
) {
95 arch
= pattern
.substr(archfound
+1);
96 if (arch
.find_first_of(isregex
) == std::string::npos
)
97 pattern
.erase(archfound
);
104 if ((Res
= regcomp(&Pattern
, pattern
.c_str() , REG_EXTENDED
| REG_ICASE
| REG_NOSUB
)) != 0) {
106 regerror(Res
, &Pattern
, Error
, sizeof(Error
));
107 _error
->Error(_("Regex compilation error - %s"), Error
);
111 for (pkgCache::GrpIterator Grp
= Cache
.GetPkgCache()->GrpBegin(); Grp
.end() == false; ++Grp
)
113 if (regexec(&Pattern
, Grp
.Name(), 0, 0, 0) != 0)
115 pkgCache::PkgIterator Pkg
= Grp
.FindPkg(arch
);
116 if (Pkg
.end() == true) {
117 if (archfound
== std::string::npos
) {
118 std::vector
<std::string
> archs
= APT::Configuration::getArchitectures();
119 for (std::vector
<std::string
>::const_iterator a
= archs
.begin();
120 a
!= archs
.end() && Pkg
.end() != true; ++a
)
121 Pkg
= Grp
.FindPkg(*a
);
123 if (Pkg
.end() == true)
131 if (pkgset
.empty() == true)
132 return helper
.canNotFindRegEx(Cache
, pattern
);
134 helper
.showRegExSelection(pkgset
, pattern
);
138 // GroupedFromCommandLine - Return all versions specified on commandline/*{{{*/
139 std::map
<unsigned short, PackageSet
> PackageSet::GroupedFromCommandLine(
140 pkgCacheFile
&Cache
, const char **cmdline
,
141 std::list
<PackageSet::Modifier
> const &mods
,
142 unsigned short const &fallback
, CacheSetHelper
&helper
) {
143 std::map
<unsigned short, PackageSet
> pkgsets
;
144 for (const char **I
= cmdline
; *I
!= 0; ++I
) {
145 unsigned short modID
= fallback
;
146 std::string str
= *I
;
147 for (std::list
<PackageSet::Modifier
>::const_iterator mod
= mods
.begin();
148 mod
!= mods
.end(); ++mod
) {
149 size_t const alength
= strlen(mod
->Alias
);
151 case PackageSet::Modifier::POSTFIX
:
152 if (str
.compare(str
.length() - alength
, alength
,
153 mod
->Alias
, 0, alength
) != 0)
155 str
.erase(str
.length() - alength
);
158 case PackageSet::Modifier::PREFIX
:
160 case PackageSet::Modifier::NONE
:
165 pkgsets
[modID
].insert(PackageSet::FromString(Cache
, str
, helper
));
170 // FromCommandLine - Return all packages specified on commandline /*{{{*/
171 PackageSet
PackageSet::FromCommandLine(pkgCacheFile
&Cache
, const char **cmdline
, CacheSetHelper
&helper
) {
173 for (const char **I
= cmdline
; *I
!= 0; ++I
) {
174 PackageSet pset
= FromString(Cache
, *I
, helper
);
175 pkgset
.insert(pset
.begin(), pset
.end());
180 // FromString - Return all packages matching a specific string /*{{{*/
181 PackageSet
PackageSet::FromString(pkgCacheFile
&Cache
, std::string
const &str
, CacheSetHelper
&helper
) {
182 std::string pkg
= str
;
183 size_t archfound
= pkg
.find_last_of(':');
185 if (archfound
!= std::string::npos
) {
186 arch
= pkg
.substr(archfound
+1);
187 pkg
.erase(archfound
);
190 pkgCache::PkgIterator Pkg
;
191 if (arch
.empty() == true) {
192 pkgCache::GrpIterator Grp
= Cache
.GetPkgCache()->FindGrp(pkg
);
193 if (Grp
.end() == false)
194 Pkg
= Grp
.FindPreferredPkg();
196 Pkg
= Cache
.GetPkgCache()->FindPkg(pkg
, arch
);
198 if (Pkg
.end() == false) {
204 _error
->PushToStack();
206 PackageSet pset
= FromTask(Cache
, str
, helper
);
207 if (pset
.empty() == true) {
208 pset
= FromRegEx(Cache
, str
, helper
);
209 if (pset
.empty() == true)
210 pset
= helper
.canNotFindPackage(Cache
, str
);
213 if (pset
.empty() == false)
214 _error
->RevertToStack();
216 _error
->MergeWithStack();
220 // GroupedFromCommandLine - Return all versions specified on commandline/*{{{*/
221 std::map
<unsigned short, VersionSet
> VersionSet::GroupedFromCommandLine(
222 pkgCacheFile
&Cache
, const char **cmdline
,
223 std::list
<VersionSet::Modifier
> const &mods
,
224 unsigned short const &fallback
, CacheSetHelper
&helper
) {
225 std::map
<unsigned short, VersionSet
> versets
;
226 for (const char **I
= cmdline
; *I
!= 0; ++I
) {
227 unsigned short modID
= fallback
;
228 VersionSet::Version select
= VersionSet::NEWEST
;
229 std::string str
= *I
;
230 for (std::list
<VersionSet::Modifier
>::const_iterator mod
= mods
.begin();
231 mod
!= mods
.end(); ++mod
) {
232 if (modID
== fallback
&& mod
->ID
== fallback
)
233 select
= mod
->SelectVersion
;
234 size_t const alength
= strlen(mod
->Alias
);
236 case VersionSet::Modifier::POSTFIX
:
237 if (str
.compare(str
.length() - alength
, alength
,
238 mod
->Alias
, 0, alength
) != 0)
240 str
.erase(str
.length() - alength
);
242 select
= mod
->SelectVersion
;
244 case VersionSet::Modifier::PREFIX
:
246 case VersionSet::Modifier::NONE
:
251 versets
[modID
].insert(VersionSet::FromString(Cache
, str
, select
, helper
));
256 // FromCommandLine - Return all versions specified on commandline /*{{{*/
257 APT::VersionSet
VersionSet::FromCommandLine(pkgCacheFile
&Cache
, const char **cmdline
,
258 APT::VersionSet::Version
const &fallback
, CacheSetHelper
&helper
) {
260 for (const char **I
= cmdline
; *I
!= 0; ++I
) {
261 VersionSet vset
= VersionSet::FromString(Cache
, *I
, fallback
, helper
);
262 verset
.insert(vset
.begin(), vset
.end());
267 // FromString - Returns all versions spedcified by a string /*{{{*/
268 APT::VersionSet
VersionSet::FromString(pkgCacheFile
&Cache
, std::string pkg
,
269 APT::VersionSet::Version
const &fallback
, CacheSetHelper
&helper
) {
271 bool verIsRel
= false;
272 size_t const vertag
= pkg
.find_last_of("/=");
273 if (vertag
!= string::npos
) {
274 ver
= pkg
.substr(vertag
+1);
275 verIsRel
= (pkg
[vertag
] == '/');
278 PackageSet pkgset
= PackageSet::FromString(Cache
, pkg
.c_str(), helper
);
280 for (PackageSet::const_iterator P
= pkgset
.begin();
281 P
!= pkgset
.end(); ++P
) {
282 if (vertag
== string::npos
) {
283 AddSelectedVersion(Cache
, verset
, P
, fallback
, helper
);
286 pkgCache::VerIterator V
;
287 if (ver
== "installed")
288 V
= getInstalledVer(Cache
, P
, helper
);
289 else if (ver
== "candidate")
290 V
= getCandidateVer(Cache
, P
, helper
);
292 pkgVersionMatch
Match(ver
, (verIsRel
== true ? pkgVersionMatch::Release
:
293 pkgVersionMatch::Version
));
295 if (V
.end() == true) {
296 if (verIsRel
== true)
297 _error
->Error(_("Release '%s' for '%s' was not found"),
298 ver
.c_str(), P
.FullName(true).c_str());
300 _error
->Error(_("Version '%s' for '%s' was not found"),
301 ver
.c_str(), P
.FullName(true).c_str());
307 helper
.showSelectedVersion(P
, V
, ver
, verIsRel
);
313 // AddSelectedVersion - add version from package based on fallback /*{{{*/
314 void VersionSet::AddSelectedVersion(pkgCacheFile
&Cache
, VersionSet
&verset
,
315 pkgCache::PkgIterator
const &P
, VersionSet::Version
const &fallback
,
316 CacheSetHelper
&helper
) {
317 pkgCache::VerIterator V
;
320 case VersionSet::ALL
:
321 if (P
->VersionList
!= 0)
322 for (V
= P
.VersionList(); V
.end() != true; ++V
)
325 verset
.insert(helper
.canNotFindAllVer(Cache
, P
));
327 case VersionSet::CANDANDINST
:
328 verset
.insert(getInstalledVer(Cache
, P
, helper
));
329 verset
.insert(getCandidateVer(Cache
, P
, helper
));
331 case VersionSet::CANDIDATE
:
332 verset
.insert(getCandidateVer(Cache
, P
, helper
));
334 case VersionSet::INSTALLED
:
335 verset
.insert(getInstalledVer(Cache
, P
, helper
));
337 case VersionSet::CANDINST
:
338 showErrors
= helper
.showErrors(false);
339 V
= getCandidateVer(Cache
, P
, helper
);
341 V
= getInstalledVer(Cache
, P
, helper
);
342 helper
.showErrors(showErrors
);
343 if (V
.end() == false)
346 verset
.insert(helper
.canNotFindInstCandVer(Cache
, P
));
348 case VersionSet::INSTCAND
:
349 showErrors
= helper
.showErrors(false);
350 V
= getInstalledVer(Cache
, P
, helper
);
352 V
= getCandidateVer(Cache
, P
, helper
);
353 helper
.showErrors(showErrors
);
354 if (V
.end() == false)
357 verset
.insert(helper
.canNotFindInstCandVer(Cache
, P
));
359 case VersionSet::NEWEST
:
360 if (P
->VersionList
!= 0)
361 verset
.insert(P
.VersionList());
363 verset
.insert(helper
.canNotFindNewestVer(Cache
, P
));
368 // getCandidateVer - Returns the candidate version of the given package /*{{{*/
369 pkgCache::VerIterator
VersionSet::getCandidateVer(pkgCacheFile
&Cache
,
370 pkgCache::PkgIterator
const &Pkg
, CacheSetHelper
&helper
) {
371 pkgCache::VerIterator Cand
;
372 if (Cache
.IsDepCacheBuilt() == true)
373 Cand
= Cache
[Pkg
].CandidateVerIter(Cache
);
375 if (unlikely(Cache
.BuildPolicy() == false))
376 return pkgCache::VerIterator(*Cache
);
377 Cand
= Cache
.GetPolicy()->GetCandidateVer(Pkg
);
379 if (Cand
.end() == true)
380 return helper
.canNotFindCandidateVer(Cache
, Pkg
);
384 // getInstalledVer - Returns the installed version of the given package /*{{{*/
385 pkgCache::VerIterator
VersionSet::getInstalledVer(pkgCacheFile
&Cache
,
386 pkgCache::PkgIterator
const &Pkg
, CacheSetHelper
&helper
) {
387 if (Pkg
->CurrentVer
== 0)
388 return helper
.canNotFindInstalledVer(Cache
, Pkg
);
389 return Pkg
.CurrentVer();
392 // canNotFindTask - handle the case no package is found for a task /*{{{*/
393 PackageSet
CacheSetHelper::canNotFindTask(pkgCacheFile
&Cache
, std::string pattern
) {
394 if (ShowError
== true)
395 _error
->Error(_("Couldn't find task '%s'"), pattern
.c_str());
399 // canNotFindRegEx - handle the case no package is found by a regex /*{{{*/
400 PackageSet
CacheSetHelper::canNotFindRegEx(pkgCacheFile
&Cache
, std::string pattern
) {
401 if (ShowError
== true)
402 _error
->Error(_("Couldn't find any package by regex '%s'"), pattern
.c_str());
406 // canNotFindPackage - handle the case no package is found from a string/*{{{*/
407 PackageSet
CacheSetHelper::canNotFindPackage(pkgCacheFile
&Cache
, std::string
const &str
) {
408 if (ShowError
== true)
409 _error
->Error(_("Unable to locate package %s"), str
.c_str());
413 // canNotFindAllVer /*{{{*/
414 VersionSet
CacheSetHelper::canNotFindAllVer(pkgCacheFile
&Cache
,
415 pkgCache::PkgIterator
const &Pkg
) {
416 if (ShowError
== true)
417 _error
->Error(_("Can't select versions from package '%s' as it purely virtual"), Pkg
.FullName(true).c_str());
421 // canNotFindInstCandVer /*{{{*/
422 VersionSet
CacheSetHelper::canNotFindInstCandVer(pkgCacheFile
&Cache
,
423 pkgCache::PkgIterator
const &Pkg
) {
424 if (ShowError
== true)
425 _error
->Error(_("Can't select installed nor candidate version from package '%s' as it has neither of them"), Pkg
.FullName(true).c_str());
429 // canNotFindNewestVer /*{{{*/
430 pkgCache::VerIterator
CacheSetHelper::canNotFindNewestVer(pkgCacheFile
&Cache
,
431 pkgCache::PkgIterator
const &Pkg
) {
432 if (ShowError
== true)
433 _error
->Error(_("Can't select newest version from package '%s' as it is purely virtual"), Pkg
.FullName(true).c_str());
434 return pkgCache::VerIterator(*Cache
);
437 // canNotFindCandidateVer /*{{{*/
438 pkgCache::VerIterator
CacheSetHelper::canNotFindCandidateVer(pkgCacheFile
&Cache
,
439 pkgCache::PkgIterator
const &Pkg
) {
440 if (ShowError
== true)
441 _error
->Error(_("Can't select candidate version from package %s as it has no candidate"), Pkg
.FullName(true).c_str());
442 return pkgCache::VerIterator(*Cache
);
445 // canNotFindInstalledVer /*{{{*/
446 pkgCache::VerIterator
CacheSetHelper::canNotFindInstalledVer(pkgCacheFile
&Cache
,
447 pkgCache::PkgIterator
const &Pkg
) {
448 if (ShowError
== true)
449 _error
->Error(_("Can't select installed version from package %s as it is not installed"), Pkg
.FullName(true).c_str());
450 return pkgCache::VerIterator(*Cache
);