]>
git.saurik.com Git - apt.git/blob - apt-pkg/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/cacheset.h>
15 #include <apt-pkg/strutl.h>
16 #include <apt-pkg/versionmatch.h>
25 // FromRegEx - Return all packages in the cache matching a pattern /*{{{*/
26 PackageSet
PackageSet::FromRegEx(pkgCacheFile
&Cache
, std::string pattern
, std::ostream
&out
) {
28 std::string arch
= "native";
29 static const char * const isregex
= ".?+*|[^$";
31 if (pattern
.find_first_of(isregex
) == std::string::npos
)
34 size_t archfound
= pattern
.find_last_of(':');
35 if (archfound
!= std::string::npos
) {
36 arch
= pattern
.substr(archfound
+1);
37 if (arch
.find_first_of(isregex
) == std::string::npos
)
38 pattern
.erase(archfound
);
45 if ((Res
= regcomp(&Pattern
, pattern
.c_str() , REG_EXTENDED
| REG_ICASE
| REG_NOSUB
)) != 0) {
47 regerror(Res
, &Pattern
, Error
, sizeof(Error
));
48 _error
->Error(_("Regex compilation error - %s"), Error
);
52 for (pkgCache::GrpIterator Grp
= Cache
.GetPkgCache()->GrpBegin(); Grp
.end() == false; ++Grp
)
54 if (regexec(&Pattern
, Grp
.Name(), 0, 0, 0) != 0)
56 pkgCache::PkgIterator Pkg
= Grp
.FindPkg(arch
);
57 if (Pkg
.end() == true) {
58 if (archfound
== std::string::npos
) {
59 std::vector
<std::string
> archs
= APT::Configuration::getArchitectures();
60 for (std::vector
<std::string
>::const_iterator a
= archs
.begin();
61 a
!= archs
.end() && Pkg
.end() != true; ++a
)
62 Pkg
= Grp
.FindPkg(*a
);
64 if (Pkg
.end() == true)
68 ioprintf(out
, _("Note, selecting %s for regex '%s'\n"),
69 Pkg
.FullName(true).c_str(), pattern
.c_str());
79 // GroupedFromCommandLine - Return all versions specified on commandline/*{{{*/
80 std::map
<unsigned short, PackageSet
> PackageSet::GroupedFromCommandLine(
81 pkgCacheFile
&Cache
, const char **cmdline
,
82 std::list
<PackageSet::Modifier
> const &mods
,
83 unsigned short const &fallback
, std::ostream
&out
) {
84 std::map
<unsigned short, PackageSet
> pkgsets
;
85 for (const char **I
= cmdline
; *I
!= 0; ++I
) {
86 unsigned short modID
= fallback
;
88 for (std::list
<PackageSet::Modifier
>::const_iterator mod
= mods
.begin();
89 mod
!= mods
.end(); ++mod
) {
90 size_t const alength
= strlen(mod
->Alias
);
92 case PackageSet::Modifier::POSTFIX
:
93 if (str
.compare(str
.length() - alength
, alength
,
94 mod
->Alias
, 0, alength
) != 0)
96 str
.erase(str
.length() - alength
);
99 case PackageSet::Modifier::PREFIX
:
101 case PackageSet::Modifier::NONE
:
106 PackageSet pset
= PackageSet::FromString(Cache
, str
, out
);
107 pkgsets
[modID
].insert(pset
.begin(), pset
.end());
112 // FromCommandLine - Return all packages specified on commandline /*{{{*/
113 PackageSet
PackageSet::FromCommandLine(pkgCacheFile
&Cache
, const char **cmdline
, std::ostream
&out
) {
115 for (const char **I
= cmdline
; *I
!= 0; ++I
) {
116 PackageSet pset
= FromString(Cache
, *I
, out
);
117 pkgset
.insert(pset
.begin(), pset
.end());
122 // FromString - Return all packages matching a specific string /*{{{*/
123 PackageSet
PackageSet::FromString(pkgCacheFile
&Cache
, std::string
const &str
, std::ostream
&out
) {
124 std::string pkg
= str
;
125 size_t archfound
= pkg
.find_last_of(':');
127 if (archfound
!= std::string::npos
) {
128 arch
= pkg
.substr(archfound
+1);
129 pkg
.erase(archfound
);
132 pkgCache::PkgIterator Pkg
;
133 if (arch
.empty() == true) {
134 pkgCache::GrpIterator Grp
= Cache
.GetPkgCache()->FindGrp(pkg
);
135 if (Grp
.end() == false)
136 Pkg
= Grp
.FindPreferredPkg();
138 Pkg
= Cache
.GetPkgCache()->FindPkg(pkg
, arch
);
140 if (Pkg
.end() == false) {
145 PackageSet regex
= FromRegEx(Cache
, str
, out
);
146 if (regex
.empty() == true)
147 _error
->Warning(_("Unable to locate package %s"), str
.c_str());
151 // FromCommandLine - Return all versions specified on commandline /*{{{*/
152 APT::VersionSet
VersionSet::FromCommandLine(pkgCacheFile
&Cache
, const char **cmdline
,
153 APT::VersionSet::Version
const &fallback
, std::ostream
&out
) {
155 for (const char **I
= cmdline
; *I
!= 0; ++I
) {
156 std::string pkg
= *I
;
158 bool verIsRel
= false;
159 size_t const vertag
= pkg
.find_last_of("/=");
160 if (vertag
!= string::npos
) {
161 ver
= pkg
.substr(vertag
+1);
162 verIsRel
= (pkg
[vertag
] == '/');
165 PackageSet pkgset
= PackageSet::FromString(Cache
, pkg
.c_str(), out
);
166 for (PackageSet::const_iterator P
= pkgset
.begin();
167 P
!= pkgset
.end(); ++P
) {
168 if (vertag
== string::npos
) {
169 AddSelectedVersion(Cache
, verset
, P
, fallback
);
172 pkgCache::VerIterator V
;
173 if (ver
== "installed")
174 V
= getInstalledVer(Cache
, P
);
175 else if (ver
== "candidate")
176 V
= getCandidateVer(Cache
, P
);
178 pkgVersionMatch
Match(ver
, (verIsRel
== true ? pkgVersionMatch::Release
:
179 pkgVersionMatch::Version
));
181 if (V
.end() == true) {
182 if (verIsRel
== true)
183 _error
->Error(_("Release '%s' for '%s' was not found"),
184 ver
.c_str(), P
.FullName(true).c_str());
186 _error
->Error(_("Version '%s' for '%s' was not found"),
187 ver
.c_str(), P
.FullName(true).c_str());
193 if (ver
== V
.VerStr())
194 ioprintf(out
, _("Selected version '%s' (%s) for '%s'\n"),
195 V
.VerStr(), V
.RelStr().c_str(), P
.FullName(true).c_str());
202 // AddSelectedVersion - add version from package based on fallback /*{{{*/
203 bool VersionSet::AddSelectedVersion(pkgCacheFile
&Cache
, VersionSet
&verset
,
204 pkgCache::PkgIterator
const &P
, VersionSet::Version
const &fallback
,
205 bool const &AllowError
) {
206 pkgCache::VerIterator V
;
208 case VersionSet::ALL
:
209 if (P
->VersionList
!= 0)
210 for (V
= P
.VersionList(); V
.end() != true; ++V
)
212 else if (AllowError
== false)
213 return _error
->Error(_("Can't select versions from package '%s' as it purely virtual"), P
.FullName(true).c_str());
217 case VersionSet::CANDANDINST
:
218 verset
.insert(getInstalledVer(Cache
, P
, AllowError
));
219 verset
.insert(getCandidateVer(Cache
, P
, AllowError
));
221 case VersionSet::CANDIDATE
:
222 verset
.insert(getCandidateVer(Cache
, P
, AllowError
));
224 case VersionSet::INSTALLED
:
225 verset
.insert(getInstalledVer(Cache
, P
, AllowError
));
227 case VersionSet::CANDINST
:
228 V
= getCandidateVer(Cache
, P
, true);
230 V
= getInstalledVer(Cache
, P
, true);
231 if (V
.end() == false)
233 else if (AllowError
== false)
234 return _error
->Error(_("Can't select installed nor candidate version from package '%s' as it has neither of them"), P
.FullName(true).c_str());
238 case VersionSet::INSTCAND
:
239 V
= getInstalledVer(Cache
, P
, true);
241 V
= getCandidateVer(Cache
, P
, true);
242 if (V
.end() == false)
244 else if (AllowError
== false)
245 return _error
->Error(_("Can't select installed nor candidate version from package '%s' as it has neither of them"), P
.FullName(true).c_str());
249 case VersionSet::NEWEST
:
250 if (P
->VersionList
!= 0)
251 verset
.insert(P
.VersionList());
252 else if (AllowError
== false)
253 return _error
->Error(_("Can't select newest version from package '%s' as it is purely virtual"), P
.FullName(true).c_str());
261 // getCandidateVer - Returns the candidate version of the given package /*{{{*/
262 pkgCache::VerIterator
VersionSet::getCandidateVer(pkgCacheFile
&Cache
,
263 pkgCache::PkgIterator
const &Pkg
, bool const &AllowError
) {
264 if (unlikely(Cache
.BuildDepCache() == false))
265 return pkgCache::VerIterator(*Cache
);
266 pkgCache::VerIterator Cand
= Cache
[Pkg
].CandidateVerIter(Cache
);
267 if (AllowError
== false && Cand
.end() == true)
268 _error
->Error(_("Can't select candidate version from package %s as it has no candidate"), Pkg
.FullName(true).c_str());
272 // getInstalledVer - Returns the installed version of the given package /*{{{*/
273 pkgCache::VerIterator
VersionSet::getInstalledVer(pkgCacheFile
&Cache
,
274 pkgCache::PkgIterator
const &Pkg
, bool const &AllowError
) {
275 if (AllowError
== false && Pkg
->CurrentVer
== 0)
276 _error
->Error(_("Can't select installed version from package %s as it is not installed"), Pkg
.FullName(true).c_str());
277 return Pkg
.CurrentVer();