]> git.saurik.com Git - apt.git/blame - cmdline/cacheset.cc
fix the gcc warning about the initialisation order of variables caused
[apt.git] / cmdline / cacheset.cc
CommitLineData
ffee1c2b
DK
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
3/* ######################################################################
4
5 Simple wrapper around a std::set to provide a similar interface to
7959c5ed
DK
6 a set of cache structures as to the complete set of all structures
7 in the pkgCache. Currently only Package is supported.
ffee1c2b
DK
8
9 ##################################################################### */
10 /*}}}*/
11// Include Files /*{{{*/
78c32596 12#include <apt-pkg/aptconfiguration.h>
ffee1c2b 13#include <apt-pkg/error.h>
7959c5ed 14#include <apt-pkg/cacheset.h>
ffee1c2b 15#include <apt-pkg/strutl.h>
856d3b06 16#include <apt-pkg/versionmatch.h>
ffee1c2b
DK
17
18#include <apti18n.h>
19
78c32596
DK
20#include <vector>
21
ffee1c2b
DK
22#include <regex.h>
23 /*}}}*/
24namespace APT {
25// FromRegEx - Return all packages in the cache matching a pattern /*{{{*/
856d3b06 26PackageSet PackageSet::FromRegEx(pkgCacheFile &Cache, std::string pattern, std::ostream &out) {
ffee1c2b 27 PackageSet pkgset;
6e235c66
DK
28 std::string arch = "native";
29 static const char * const isregex = ".?+*|[^$";
ffee1c2b 30
6e235c66 31 if (pattern.find_first_of(isregex) == std::string::npos)
ffee1c2b
DK
32 return pkgset;
33
6e235c66
DK
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);
39 else
40 arch = "native";
41 }
42
ffee1c2b
DK
43 regex_t Pattern;
44 int Res;
6e235c66 45 if ((Res = regcomp(&Pattern, pattern.c_str() , REG_EXTENDED | REG_ICASE | REG_NOSUB)) != 0) {
ffee1c2b
DK
46 char Error[300];
47 regerror(Res, &Pattern, Error, sizeof(Error));
48 _error->Error(_("Regex compilation error - %s"), Error);
49 return pkgset;
50 }
51
856d3b06 52 for (pkgCache::GrpIterator Grp = Cache.GetPkgCache()->GrpBegin(); Grp.end() == false; ++Grp)
ffee1c2b
DK
53 {
54 if (regexec(&Pattern, Grp.Name(), 0, 0, 0) != 0)
55 continue;
6e235c66 56 pkgCache::PkgIterator Pkg = Grp.FindPkg(arch);
78c32596 57 if (Pkg.end() == true) {
6e235c66
DK
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);
78c32596
DK
63 }
64 if (Pkg.end() == true)
65 continue;
66 }
ffee1c2b
DK
67
68 ioprintf(out, _("Note, selecting %s for regex '%s'\n"),
6e235c66 69 Pkg.FullName(true).c_str(), pattern.c_str());
ffee1c2b
DK
70
71 pkgset.insert(Pkg);
72 }
73
74 regfree(&Pattern);
75
78c32596
DK
76 return pkgset;
77}
78 /*}}}*/
9cc83a6f
DK
79// GroupedFromCommandLine - Return all versions specified on commandline/*{{{*/
80std::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;
87 std::string str = *I;
88 for (std::list<PackageSet::Modifier>::const_iterator mod = mods.begin();
89 mod != mods.end(); ++mod) {
90 size_t const alength = strlen(mod->Alias);
91 switch(mod->Pos) {
92 case PackageSet::Modifier::POSTFIX:
93 if (str.compare(str.length() - alength, alength,
55c59998 94 mod->Alias, 0, alength) != 0)
9cc83a6f
DK
95 continue;
96 str.erase(str.length() - alength);
97 modID = mod->ID;
98 break;
99 case PackageSet::Modifier::PREFIX:
100 continue;
101 case PackageSet::Modifier::NONE:
102 continue;
103 }
104 break;
105 }
106 PackageSet pset = PackageSet::FromString(Cache, str, out);
107 pkgsets[modID].insert(pset.begin(), pset.end());
108 }
109 return pkgsets;
110}
111 /*}}}*/
78c32596 112// FromCommandLine - Return all packages specified on commandline /*{{{*/
856d3b06 113PackageSet PackageSet::FromCommandLine(pkgCacheFile &Cache, const char **cmdline, std::ostream &out) {
78c32596 114 PackageSet pkgset;
9f1f17cc 115 for (const char **I = cmdline; *I != 0; ++I) {
856d3b06
DK
116 PackageSet pset = FromString(Cache, *I, out);
117 pkgset.insert(pset.begin(), pset.end());
118 }
119 return pkgset;
120}
121 /*}}}*/
122// FromString - Return all packages matching a specific string /*{{{*/
fe870feb
DK
123PackageSet PackageSet::FromString(pkgCacheFile &Cache, std::string const &str, std::ostream &out) {
124 std::string pkg = str;
125 size_t archfound = pkg.find_last_of(':');
126 std::string arch;
127 if (archfound != std::string::npos) {
128 arch = pkg.substr(archfound+1);
129 pkg.erase(archfound);
130 }
131
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();
137 } else
138 Pkg = Cache.GetPkgCache()->FindPkg(pkg, arch);
139
140 if (Pkg.end() == false) {
856d3b06
DK
141 PackageSet pkgset;
142 pkgset.insert(Pkg);
143 return pkgset;
144 }
145 PackageSet regex = FromRegEx(Cache, str, out);
146 if (regex.empty() == true)
fe870feb 147 _error->Warning(_("Unable to locate package %s"), str.c_str());
856d3b06
DK
148 return regex;
149}
150 /*}}}*/
55c59998
DK
151// GroupedFromCommandLine - Return all versions specified on commandline/*{{{*/
152std::map<unsigned short, VersionSet> VersionSet::GroupedFromCommandLine(
153 pkgCacheFile &Cache, const char **cmdline,
154 std::list<VersionSet::Modifier> const &mods,
155 unsigned short const &fallback, std::ostream &out) {
156 std::map<unsigned short, VersionSet> versets;
157 for (const char **I = cmdline; *I != 0; ++I) {
158 unsigned short modID = fallback;
159 VersionSet::Version select = VersionSet::NEWEST;
160 std::string str = *I;
161 for (std::list<VersionSet::Modifier>::const_iterator mod = mods.begin();
162 mod != mods.end(); ++mod) {
163 if (modID == fallback && mod->ID == fallback)
164 select = mod->SelectVersion;
165 size_t const alength = strlen(mod->Alias);
166 switch(mod->Pos) {
167 case VersionSet::Modifier::POSTFIX:
168 if (str.compare(str.length() - alength, alength,
169 mod->Alias, 0, alength) != 0)
170 continue;
171 str.erase(str.length() - alength);
172 modID = mod->ID;
173 select = mod->SelectVersion;
174 break;
175 case VersionSet::Modifier::PREFIX:
176 continue;
177 case VersionSet::Modifier::NONE:
178 continue;
179 }
180 break;
181 }
182 VersionSet vset = VersionSet::FromString(Cache, str, select , out);
183 versets[modID].insert(vset.begin(), vset.end());
184 }
185 return versets;
186}
187 /*}}}*/
856d3b06
DK
188// FromCommandLine - Return all versions specified on commandline /*{{{*/
189APT::VersionSet VersionSet::FromCommandLine(pkgCacheFile &Cache, const char **cmdline,
190 APT::VersionSet::Version const &fallback, std::ostream &out) {
191 VersionSet verset;
9f1f17cc 192 for (const char **I = cmdline; *I != 0; ++I) {
55c59998
DK
193 VersionSet vset = VersionSet::FromString(Cache, *I, fallback, out);
194 verset.insert(vset.begin(), vset.end());
195 }
196 return verset;
197}
198 /*}}}*/
199// FromString - Returns all versions spedcified by a string /*{{{*/
200APT::VersionSet VersionSet::FromString(pkgCacheFile &Cache, std::string pkg,
201 APT::VersionSet::Version const &fallback, std::ostream &out) {
202 std::string ver;
203 bool verIsRel = false;
204 size_t const vertag = pkg.find_last_of("/=");
205 if (vertag != string::npos) {
206 ver = pkg.substr(vertag+1);
207 verIsRel = (pkg[vertag] == '/');
208 pkg.erase(vertag);
209 }
210 PackageSet pkgset = PackageSet::FromString(Cache, pkg.c_str(), out);
211 VersionSet verset;
212 for (PackageSet::const_iterator P = pkgset.begin();
213 P != pkgset.end(); ++P) {
214 if (vertag == string::npos) {
215 AddSelectedVersion(Cache, verset, P, fallback);
216 continue;
856d3b06 217 }
55c59998
DK
218 pkgCache::VerIterator V;
219 if (ver == "installed")
220 V = getInstalledVer(Cache, P);
221 else if (ver == "candidate")
222 V = getCandidateVer(Cache, P);
223 else {
224 pkgVersionMatch Match(ver, (verIsRel == true ? pkgVersionMatch::Release :
225 pkgVersionMatch::Version));
226 V = Match.Find(P);
227 if (V.end() == true) {
228 if (verIsRel == true)
229 _error->Error(_("Release '%s' for '%s' was not found"),
230 ver.c_str(), P.FullName(true).c_str());
231 else
232 _error->Error(_("Version '%s' for '%s' was not found"),
233 ver.c_str(), P.FullName(true).c_str());
84910ad5
DK
234 continue;
235 }
78c32596 236 }
55c59998
DK
237 if (V.end() == true)
238 continue;
239 if (ver == V.VerStr())
240 ioprintf(out, _("Selected version '%s' (%s) for '%s'\n"),
241 V.VerStr(), V.RelStr().c_str(), P.FullName(true).c_str());
242 verset.insert(V);
78c32596 243 }
856d3b06
DK
244 return verset;
245}
246 /*}}}*/
84910ad5
DK
247// AddSelectedVersion - add version from package based on fallback /*{{{*/
248bool VersionSet::AddSelectedVersion(pkgCacheFile &Cache, VersionSet &verset,
249 pkgCache::PkgIterator const &P, VersionSet::Version const &fallback,
250 bool const &AllowError) {
251 pkgCache::VerIterator V;
252 switch(fallback) {
253 case VersionSet::ALL:
254 if (P->VersionList != 0)
255 for (V = P.VersionList(); V.end() != true; ++V)
256 verset.insert(V);
257 else if (AllowError == false)
258 return _error->Error(_("Can't select versions from package '%s' as it purely virtual"), P.FullName(true).c_str());
259 else
260 return false;
261 break;
262 case VersionSet::CANDANDINST:
263 verset.insert(getInstalledVer(Cache, P, AllowError));
264 verset.insert(getCandidateVer(Cache, P, AllowError));
265 break;
266 case VersionSet::CANDIDATE:
267 verset.insert(getCandidateVer(Cache, P, AllowError));
268 break;
269 case VersionSet::INSTALLED:
270 verset.insert(getInstalledVer(Cache, P, AllowError));
271 break;
272 case VersionSet::CANDINST:
273 V = getCandidateVer(Cache, P, true);
274 if (V.end() == true)
275 V = getInstalledVer(Cache, P, true);
276 if (V.end() == false)
277 verset.insert(V);
278 else if (AllowError == false)
279 return _error->Error(_("Can't select installed nor candidate version from package '%s' as it has neither of them"), P.FullName(true).c_str());
280 else
281 return false;
282 break;
283 case VersionSet::INSTCAND:
284 V = getInstalledVer(Cache, P, true);
285 if (V.end() == true)
286 V = getCandidateVer(Cache, P, true);
287 if (V.end() == false)
288 verset.insert(V);
289 else if (AllowError == false)
290 return _error->Error(_("Can't select installed nor candidate version from package '%s' as it has neither of them"), P.FullName(true).c_str());
291 else
292 return false;
293 break;
294 case VersionSet::NEWEST:
295 if (P->VersionList != 0)
296 verset.insert(P.VersionList());
297 else if (AllowError == false)
298 return _error->Error(_("Can't select newest version from package '%s' as it is purely virtual"), P.FullName(true).c_str());
299 else
300 return false;
301 break;
302 }
303 return true;
304}
305 /*}}}*/
856d3b06
DK
306// getCandidateVer - Returns the candidate version of the given package /*{{{*/
307pkgCache::VerIterator VersionSet::getCandidateVer(pkgCacheFile &Cache,
308 pkgCache::PkgIterator const &Pkg, bool const &AllowError) {
a8ef7efd
DK
309 pkgCache::VerIterator Cand;
310 if (Cache.IsDepCacheBuilt() == true)
311 Cand = Cache[Pkg].CandidateVerIter(Cache);
312 else {
313 if (unlikely(Cache.BuildPolicy() == false))
314 return pkgCache::VerIterator(*Cache);
315 Cand = Cache.GetPolicy()->GetCandidateVer(Pkg);
316 }
856d3b06
DK
317 if (AllowError == false && Cand.end() == true)
318 _error->Error(_("Can't select candidate version from package %s as it has no candidate"), Pkg.FullName(true).c_str());
319 return Cand;
320}
321 /*}}}*/
322// getInstalledVer - Returns the installed version of the given package /*{{{*/
323pkgCache::VerIterator VersionSet::getInstalledVer(pkgCacheFile &Cache,
324 pkgCache::PkgIterator const &Pkg, bool const &AllowError) {
325 if (AllowError == false && Pkg->CurrentVer == 0)
326 _error->Error(_("Can't select installed version from package %s as it is not installed"), Pkg.FullName(true).c_str());
327 return Pkg.CurrentVer();
ffee1c2b
DK
328}
329 /*}}}*/
330}