]>
git.saurik.com Git - apt.git/blob - cmdline/cacheset.cc
42bc79693521634aecaaca2a7265607493ea95b0
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();
37 pattern
.erase(pattern
.length()-1);
39 if (unlikely(Cache
.GetPkgCache() == 0 || Cache
.GetDepCache() == 0))
40 return APT::PackageSet();
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
)
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
);
109 if (unlikely(Cache
.GetPkgCache() == 0))
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
));
321 for (PackageSet::const_iterator P
= pkgset
.begin();
322 P
!= pkgset
.end(); ++P
) {
323 if (vertag
== string::npos
) {
324 AddSelectedVersion(Cache
, verset
, P
, fallback
, helper
);
327 pkgCache::VerIterator V
;
328 if (ver
== "installed")
329 V
= getInstalledVer(Cache
, P
, helper
);
330 else if (ver
== "candidate")
331 V
= getCandidateVer(Cache
, P
, helper
);
333 pkgVersionMatch
Match(ver
, (verIsRel
== true ? pkgVersionMatch::Release
:
334 pkgVersionMatch::Version
));
336 if (V
.end() == true) {
337 if (verIsRel
== true)
338 _error
->Error(_("Release '%s' for '%s' was not found"),
339 ver
.c_str(), P
.FullName(true).c_str());
341 _error
->Error(_("Version '%s' for '%s' was not found"),
342 ver
.c_str(), P
.FullName(true).c_str());
348 helper
.showSelectedVersion(P
, V
, ver
, verIsRel
);
354 // AddSelectedVersion - add version from package based on fallback /*{{{*/
355 void VersionSet::AddSelectedVersion(pkgCacheFile
&Cache
, VersionSet
&verset
,
356 pkgCache::PkgIterator
const &P
, VersionSet::Version
const &fallback
,
357 CacheSetHelper
&helper
) {
358 pkgCache::VerIterator V
;
361 case VersionSet::ALL
:
362 if (P
->VersionList
!= 0)
363 for (V
= P
.VersionList(); V
.end() != true; ++V
)
366 verset
.insert(helper
.canNotFindAllVer(Cache
, P
));
368 case VersionSet::CANDANDINST
:
369 verset
.insert(getInstalledVer(Cache
, P
, helper
));
370 verset
.insert(getCandidateVer(Cache
, P
, helper
));
372 case VersionSet::CANDIDATE
:
373 verset
.insert(getCandidateVer(Cache
, P
, helper
));
375 case VersionSet::INSTALLED
:
376 verset
.insert(getInstalledVer(Cache
, P
, helper
));
378 case VersionSet::CANDINST
:
379 showErrors
= helper
.showErrors(false);
380 V
= getCandidateVer(Cache
, P
, helper
);
382 V
= getInstalledVer(Cache
, P
, helper
);
383 helper
.showErrors(showErrors
);
384 if (V
.end() == false)
387 verset
.insert(helper
.canNotFindInstCandVer(Cache
, P
));
389 case VersionSet::INSTCAND
:
390 showErrors
= helper
.showErrors(false);
391 V
= getInstalledVer(Cache
, P
, helper
);
393 V
= getCandidateVer(Cache
, P
, helper
);
394 helper
.showErrors(showErrors
);
395 if (V
.end() == false)
398 verset
.insert(helper
.canNotFindInstCandVer(Cache
, P
));
400 case VersionSet::NEWEST
:
401 if (P
->VersionList
!= 0)
402 verset
.insert(P
.VersionList());
404 verset
.insert(helper
.canNotFindNewestVer(Cache
, P
));
409 // getCandidateVer - Returns the candidate version of the given package /*{{{*/
410 pkgCache::VerIterator
VersionSet::getCandidateVer(pkgCacheFile
&Cache
,
411 pkgCache::PkgIterator
const &Pkg
, CacheSetHelper
&helper
) {
412 pkgCache::VerIterator Cand
;
413 if (Cache
.IsDepCacheBuilt() == true)
414 Cand
= Cache
[Pkg
].CandidateVerIter(Cache
);
416 if (unlikely(Cache
.GetPolicy() == 0))
417 return pkgCache::VerIterator(Cache
);
418 Cand
= Cache
.GetPolicy()->GetCandidateVer(Pkg
);
420 if (Cand
.end() == true)
421 return helper
.canNotFindCandidateVer(Cache
, Pkg
);
425 // getInstalledVer - Returns the installed version of the given package /*{{{*/
426 pkgCache::VerIterator
VersionSet::getInstalledVer(pkgCacheFile
&Cache
,
427 pkgCache::PkgIterator
const &Pkg
, CacheSetHelper
&helper
) {
428 if (Pkg
->CurrentVer
== 0)
429 return helper
.canNotFindInstalledVer(Cache
, Pkg
);
430 return Pkg
.CurrentVer();
433 // canNotFindPkgName - handle the case no package has this name /*{{{*/
434 pkgCache::PkgIterator
CacheSetHelper::canNotFindPkgName(pkgCacheFile
&Cache
,
435 std::string
const &str
) {
436 if (ShowError
== true)
437 _error
->Error(_("Unable to locate package %s"), str
.c_str());
438 return pkgCache::PkgIterator(Cache
, 0);
441 // canNotFindTask - handle the case no package is found for a task /*{{{*/
442 PackageSet
CacheSetHelper::canNotFindTask(pkgCacheFile
&Cache
, std::string pattern
) {
443 if (ShowError
== true)
444 _error
->Error(_("Couldn't find task '%s'"), pattern
.c_str());
448 // canNotFindRegEx - handle the case no package is found by a regex /*{{{*/
449 PackageSet
CacheSetHelper::canNotFindRegEx(pkgCacheFile
&Cache
, std::string pattern
) {
450 if (ShowError
== true)
451 _error
->Error(_("Couldn't find any package by regex '%s'"), pattern
.c_str());
455 // canNotFindPackage - handle the case no package is found from a string/*{{{*/
456 PackageSet
CacheSetHelper::canNotFindPackage(pkgCacheFile
&Cache
, std::string
const &str
) {
460 // canNotFindAllVer /*{{{*/
461 VersionSet
CacheSetHelper::canNotFindAllVer(pkgCacheFile
&Cache
,
462 pkgCache::PkgIterator
const &Pkg
) {
463 if (ShowError
== true)
464 _error
->Error(_("Can't select versions from package '%s' as it purely virtual"), Pkg
.FullName(true).c_str());
468 // canNotFindInstCandVer /*{{{*/
469 VersionSet
CacheSetHelper::canNotFindInstCandVer(pkgCacheFile
&Cache
,
470 pkgCache::PkgIterator
const &Pkg
) {
471 if (ShowError
== true)
472 _error
->Error(_("Can't select installed nor candidate version from package '%s' as it has neither of them"), Pkg
.FullName(true).c_str());
476 // canNotFindNewestVer /*{{{*/
477 pkgCache::VerIterator
CacheSetHelper::canNotFindNewestVer(pkgCacheFile
&Cache
,
478 pkgCache::PkgIterator
const &Pkg
) {
479 if (ShowError
== true)
480 _error
->Error(_("Can't select newest version from package '%s' as it is purely virtual"), Pkg
.FullName(true).c_str());
481 return pkgCache::VerIterator(Cache
);
484 // canNotFindCandidateVer /*{{{*/
485 pkgCache::VerIterator
CacheSetHelper::canNotFindCandidateVer(pkgCacheFile
&Cache
,
486 pkgCache::PkgIterator
const &Pkg
) {
487 if (ShowError
== true)
488 _error
->Error(_("Can't select candidate version from package %s as it has no candidate"), Pkg
.FullName(true).c_str());
489 return pkgCache::VerIterator(Cache
);
492 // canNotFindInstalledVer /*{{{*/
493 pkgCache::VerIterator
CacheSetHelper::canNotFindInstalledVer(pkgCacheFile
&Cache
,
494 pkgCache::PkgIterator
const &Pkg
) {
495 if (ShowError
== true)
496 _error
->Error(_("Can't select installed version from package %s as it is not installed"), Pkg
.FullName(true).c_str());
497 return pkgCache::VerIterator(Cache
);