]>
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) {
203 PackageSet pset
= FromTask(Cache
, str
, helper
);
204 if (pset
.empty() == false)
207 pset
= FromRegEx(Cache
, str
, helper
);
208 if (pset
.empty() == false)
211 return helper
.canNotFindPackage(Cache
, str
);
214 // GroupedFromCommandLine - Return all versions specified on commandline/*{{{*/
215 std::map
<unsigned short, VersionSet
> VersionSet::GroupedFromCommandLine(
216 pkgCacheFile
&Cache
, const char **cmdline
,
217 std::list
<VersionSet::Modifier
> const &mods
,
218 unsigned short const &fallback
, CacheSetHelper
&helper
) {
219 std::map
<unsigned short, VersionSet
> versets
;
220 for (const char **I
= cmdline
; *I
!= 0; ++I
) {
221 unsigned short modID
= fallback
;
222 VersionSet::Version select
= VersionSet::NEWEST
;
223 std::string str
= *I
;
224 for (std::list
<VersionSet::Modifier
>::const_iterator mod
= mods
.begin();
225 mod
!= mods
.end(); ++mod
) {
226 if (modID
== fallback
&& mod
->ID
== fallback
)
227 select
= mod
->SelectVersion
;
228 size_t const alength
= strlen(mod
->Alias
);
230 case VersionSet::Modifier::POSTFIX
:
231 if (str
.compare(str
.length() - alength
, alength
,
232 mod
->Alias
, 0, alength
) != 0)
234 str
.erase(str
.length() - alength
);
236 select
= mod
->SelectVersion
;
238 case VersionSet::Modifier::PREFIX
:
240 case VersionSet::Modifier::NONE
:
245 versets
[modID
].insert(VersionSet::FromString(Cache
, str
, select
, helper
));
250 // FromCommandLine - Return all versions specified on commandline /*{{{*/
251 APT::VersionSet
VersionSet::FromCommandLine(pkgCacheFile
&Cache
, const char **cmdline
,
252 APT::VersionSet::Version
const &fallback
, CacheSetHelper
&helper
) {
254 for (const char **I
= cmdline
; *I
!= 0; ++I
) {
255 VersionSet vset
= VersionSet::FromString(Cache
, *I
, fallback
, helper
);
256 verset
.insert(vset
.begin(), vset
.end());
261 // FromString - Returns all versions spedcified by a string /*{{{*/
262 APT::VersionSet
VersionSet::FromString(pkgCacheFile
&Cache
, std::string pkg
,
263 APT::VersionSet::Version
const &fallback
, CacheSetHelper
&helper
) {
265 bool verIsRel
= false;
266 size_t const vertag
= pkg
.find_last_of("/=");
267 if (vertag
!= string::npos
) {
268 ver
= pkg
.substr(vertag
+1);
269 verIsRel
= (pkg
[vertag
] == '/');
272 PackageSet pkgset
= PackageSet::FromString(Cache
, pkg
.c_str(), helper
);
274 for (PackageSet::const_iterator P
= pkgset
.begin();
275 P
!= pkgset
.end(); ++P
) {
276 if (vertag
== string::npos
) {
277 AddSelectedVersion(Cache
, verset
, P
, fallback
, helper
);
280 pkgCache::VerIterator V
;
281 if (ver
== "installed")
282 V
= getInstalledVer(Cache
, P
, helper
);
283 else if (ver
== "candidate")
284 V
= getCandidateVer(Cache
, P
, helper
);
286 pkgVersionMatch
Match(ver
, (verIsRel
== true ? pkgVersionMatch::Release
:
287 pkgVersionMatch::Version
));
289 if (V
.end() == true) {
290 if (verIsRel
== true)
291 _error
->Error(_("Release '%s' for '%s' was not found"),
292 ver
.c_str(), P
.FullName(true).c_str());
294 _error
->Error(_("Version '%s' for '%s' was not found"),
295 ver
.c_str(), P
.FullName(true).c_str());
301 helper
.showSelectedVersion(P
, V
, ver
, verIsRel
);
307 // AddSelectedVersion - add version from package based on fallback /*{{{*/
308 void VersionSet::AddSelectedVersion(pkgCacheFile
&Cache
, VersionSet
&verset
,
309 pkgCache::PkgIterator
const &P
, VersionSet::Version
const &fallback
,
310 CacheSetHelper
&helper
) {
311 pkgCache::VerIterator V
;
314 case VersionSet::ALL
:
315 if (P
->VersionList
!= 0)
316 for (V
= P
.VersionList(); V
.end() != true; ++V
)
319 verset
.insert(helper
.canNotFindAllVer(Cache
, P
));
321 case VersionSet::CANDANDINST
:
322 verset
.insert(getInstalledVer(Cache
, P
, helper
));
323 verset
.insert(getCandidateVer(Cache
, P
, helper
));
325 case VersionSet::CANDIDATE
:
326 verset
.insert(getCandidateVer(Cache
, P
, helper
));
328 case VersionSet::INSTALLED
:
329 verset
.insert(getInstalledVer(Cache
, P
, helper
));
331 case VersionSet::CANDINST
:
332 showErrors
= helper
.showErrors(false);
333 V
= getCandidateVer(Cache
, P
, helper
);
335 V
= getInstalledVer(Cache
, P
, helper
);
336 helper
.showErrors(showErrors
);
337 if (V
.end() == false)
340 verset
.insert(helper
.canNotFindInstCandVer(Cache
, P
));
342 case VersionSet::INSTCAND
:
343 showErrors
= helper
.showErrors(false);
344 V
= getInstalledVer(Cache
, P
, helper
);
346 V
= getCandidateVer(Cache
, P
, helper
);
347 helper
.showErrors(showErrors
);
348 if (V
.end() == false)
351 verset
.insert(helper
.canNotFindInstCandVer(Cache
, P
));
353 case VersionSet::NEWEST
:
354 if (P
->VersionList
!= 0)
355 verset
.insert(P
.VersionList());
357 verset
.insert(helper
.canNotFindNewestVer(Cache
, P
));
362 // getCandidateVer - Returns the candidate version of the given package /*{{{*/
363 pkgCache::VerIterator
VersionSet::getCandidateVer(pkgCacheFile
&Cache
,
364 pkgCache::PkgIterator
const &Pkg
, CacheSetHelper
&helper
) {
365 pkgCache::VerIterator Cand
;
366 if (Cache
.IsDepCacheBuilt() == true)
367 Cand
= Cache
[Pkg
].CandidateVerIter(Cache
);
369 if (unlikely(Cache
.BuildPolicy() == false))
370 return pkgCache::VerIterator(*Cache
);
371 Cand
= Cache
.GetPolicy()->GetCandidateVer(Pkg
);
373 if (Cand
.end() == true)
374 return helper
.canNotFindCandidateVer(Cache
, Pkg
);
378 // getInstalledVer - Returns the installed version of the given package /*{{{*/
379 pkgCache::VerIterator
VersionSet::getInstalledVer(pkgCacheFile
&Cache
,
380 pkgCache::PkgIterator
const &Pkg
, CacheSetHelper
&helper
) {
381 if (Pkg
->CurrentVer
== 0)
382 return helper
.canNotFindInstalledVer(Cache
, Pkg
);
383 return Pkg
.CurrentVer();
386 // canNotFindTask - handle the case no package is found for a task /*{{{*/
387 PackageSet
CacheSetHelper::canNotFindTask(pkgCacheFile
&Cache
, std::string pattern
) {
388 if (ShowError
== true)
389 _error
->Error(_("Couldn't find task '%s'"), pattern
.c_str());
393 // canNotFindRegEx - handle the case no package is found by a regex /*{{{*/
394 PackageSet
CacheSetHelper::canNotFindRegEx(pkgCacheFile
&Cache
, std::string pattern
) {
395 if (ShowError
== true)
396 _error
->Error(_("Couldn't find any package by regex '%s'"), pattern
.c_str());
400 // canNotFindPackage - handle the case no package is found from a string/*{{{*/
401 PackageSet
CacheSetHelper::canNotFindPackage(pkgCacheFile
&Cache
, std::string
const &str
) {
402 if (ShowError
== true)
403 _error
->Error(_("Unable to locate package %s"), str
.c_str());
407 // canNotFindAllVer /*{{{*/
408 VersionSet
CacheSetHelper::canNotFindAllVer(pkgCacheFile
&Cache
,
409 pkgCache::PkgIterator
const &Pkg
) {
410 if (ShowError
== true)
411 _error
->Error(_("Can't select versions from package '%s' as it purely virtual"), Pkg
.FullName(true).c_str());
415 // canNotFindInstCandVer /*{{{*/
416 VersionSet
CacheSetHelper::canNotFindInstCandVer(pkgCacheFile
&Cache
,
417 pkgCache::PkgIterator
const &Pkg
) {
418 if (ShowError
== true)
419 _error
->Error(_("Can't select installed nor candidate version from package '%s' as it has neither of them"), Pkg
.FullName(true).c_str());
423 // canNotFindNewestVer /*{{{*/
424 pkgCache::VerIterator
CacheSetHelper::canNotFindNewestVer(pkgCacheFile
&Cache
,
425 pkgCache::PkgIterator
const &Pkg
) {
426 if (ShowError
== true)
427 _error
->Error(_("Can't select newest version from package '%s' as it is purely virtual"), Pkg
.FullName(true).c_str());
428 return pkgCache::VerIterator(*Cache
);
431 // canNotFindCandidateVer /*{{{*/
432 pkgCache::VerIterator
CacheSetHelper::canNotFindCandidateVer(pkgCacheFile
&Cache
,
433 pkgCache::PkgIterator
const &Pkg
) {
434 if (ShowError
== true)
435 _error
->Error(_("Can't select candidate version from package %s as it has no candidate"), Pkg
.FullName(true).c_str());
436 return pkgCache::VerIterator(*Cache
);
439 // canNotFindInstalledVer /*{{{*/
440 pkgCache::VerIterator
CacheSetHelper::canNotFindInstalledVer(pkgCacheFile
&Cache
,
441 pkgCache::PkgIterator
const &Pkg
) {
442 if (ShowError
== true)
443 _error
->Error(_("Can't select installed version from package %s as it is not installed"), Pkg
.FullName(true).c_str());
444 return pkgCache::VerIterator(*Cache
);