]> git.saurik.com Git - apt.git/blob - cmdline/cacheset.cc
- only print errors if all tries to get a package by string failed
[apt.git] / cmdline / cacheset.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 /* ######################################################################
4
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.
8
9 ##################################################################### */
10 /*}}}*/
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>
16
17 #include <apti18n.h>
18
19 #include "cacheset.h"
20
21 #include <vector>
22
23 #include <regex.h>
24 /*}}}*/
25 namespace APT {
26 // FromTask - Return all packages in the cache from a specific task /*{{{*/
27 PackageSet PackageSet::FromTask(pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper) {
28 PackageSet pkgset;
29 if (Cache.BuildCaches() == false || Cache.BuildDepCache() == false)
30 return pkgset;
31
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);
37 }
38
39 if (pattern[pattern.length() -1] != '^')
40 return pkgset;
41 pattern.erase(pattern.length()-1);
42
43 // get the records
44 pkgRecords Recs(Cache);
45
46 // build regexp for the task
47 regex_t Pattern;
48 char S[300];
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");
52 return pkgset;
53 }
54
55 for (pkgCache::GrpIterator Grp = Cache->GrpBegin(); Grp.end() == false; ++Grp) {
56 pkgCache::PkgIterator Pkg = Grp.FindPkg(arch);
57 if (Pkg.end() == true)
58 continue;
59 pkgCache::VerIterator ver = Cache[Pkg].CandidateVerIter(Cache);
60 if(ver.end() == true)
61 continue;
62
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;
67 char buf[length];
68 strncpy(buf, start, length);
69 buf[length-1] = '\0';
70 if (regexec(&Pattern, buf, 0, 0, 0) != 0)
71 continue;
72
73 pkgset.insert(Pkg);
74 }
75 regfree(&Pattern);
76
77 if (pkgset.empty() == true)
78 return helper.canNotFindTask(Cache, pattern);
79
80 helper.showTaskSelection(pkgset, pattern);
81 return pkgset;
82 }
83 /*}}}*/
84 // FromRegEx - Return all packages in the cache matching a pattern /*{{{*/
85 PackageSet PackageSet::FromRegEx(pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper) {
86 PackageSet pkgset;
87 static const char * const isregex = ".?+*|[^$";
88
89 if (pattern.find_first_of(isregex) == std::string::npos)
90 return pkgset;
91
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);
98 else
99 arch = "native";
100 }
101
102 regex_t Pattern;
103 int Res;
104 if ((Res = regcomp(&Pattern, pattern.c_str() , REG_EXTENDED | REG_ICASE | REG_NOSUB)) != 0) {
105 char Error[300];
106 regerror(Res, &Pattern, Error, sizeof(Error));
107 _error->Error(_("Regex compilation error - %s"), Error);
108 return pkgset;
109 }
110
111 for (pkgCache::GrpIterator Grp = Cache.GetPkgCache()->GrpBegin(); Grp.end() == false; ++Grp)
112 {
113 if (regexec(&Pattern, Grp.Name(), 0, 0, 0) != 0)
114 continue;
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);
122 }
123 if (Pkg.end() == true)
124 continue;
125 }
126
127 pkgset.insert(Pkg);
128 }
129 regfree(&Pattern);
130
131 if (pkgset.empty() == true)
132 return helper.canNotFindRegEx(Cache, pattern);
133
134 helper.showRegExSelection(pkgset, pattern);
135 return pkgset;
136 }
137 /*}}}*/
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);
150 switch(mod->Pos) {
151 case PackageSet::Modifier::POSTFIX:
152 if (str.compare(str.length() - alength, alength,
153 mod->Alias, 0, alength) != 0)
154 continue;
155 str.erase(str.length() - alength);
156 modID = mod->ID;
157 break;
158 case PackageSet::Modifier::PREFIX:
159 continue;
160 case PackageSet::Modifier::NONE:
161 continue;
162 }
163 break;
164 }
165 pkgsets[modID].insert(PackageSet::FromString(Cache, str, helper));
166 }
167 return pkgsets;
168 }
169 /*}}}*/
170 // FromCommandLine - Return all packages specified on commandline /*{{{*/
171 PackageSet PackageSet::FromCommandLine(pkgCacheFile &Cache, const char **cmdline, CacheSetHelper &helper) {
172 PackageSet pkgset;
173 for (const char **I = cmdline; *I != 0; ++I) {
174 PackageSet pset = FromString(Cache, *I, helper);
175 pkgset.insert(pset.begin(), pset.end());
176 }
177 return pkgset;
178 }
179 /*}}}*/
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(':');
184 std::string arch;
185 if (archfound != std::string::npos) {
186 arch = pkg.substr(archfound+1);
187 pkg.erase(archfound);
188 }
189
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();
195 } else
196 Pkg = Cache.GetPkgCache()->FindPkg(pkg, arch);
197
198 if (Pkg.end() == false) {
199 PackageSet pkgset;
200 pkgset.insert(Pkg);
201 return pkgset;
202 }
203
204 _error->PushToStack();
205
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);
211 }
212
213 if (pset.empty() == false)
214 _error->RevertToStack();
215 else
216 _error->MergeWithStack();
217 return pset;
218 }
219 /*}}}*/
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);
235 switch(mod->Pos) {
236 case VersionSet::Modifier::POSTFIX:
237 if (str.compare(str.length() - alength, alength,
238 mod->Alias, 0, alength) != 0)
239 continue;
240 str.erase(str.length() - alength);
241 modID = mod->ID;
242 select = mod->SelectVersion;
243 break;
244 case VersionSet::Modifier::PREFIX:
245 continue;
246 case VersionSet::Modifier::NONE:
247 continue;
248 }
249 break;
250 }
251 versets[modID].insert(VersionSet::FromString(Cache, str, select , helper));
252 }
253 return versets;
254 }
255 /*}}}*/
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) {
259 VersionSet verset;
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());
263 }
264 return verset;
265 }
266 /*}}}*/
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) {
270 std::string ver;
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] == '/');
276 pkg.erase(vertag);
277 }
278 PackageSet pkgset = PackageSet::FromString(Cache, pkg.c_str(), helper);
279 VersionSet verset;
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);
284 continue;
285 }
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);
291 else {
292 pkgVersionMatch Match(ver, (verIsRel == true ? pkgVersionMatch::Release :
293 pkgVersionMatch::Version));
294 V = Match.Find(P);
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());
299 else
300 _error->Error(_("Version '%s' for '%s' was not found"),
301 ver.c_str(), P.FullName(true).c_str());
302 continue;
303 }
304 }
305 if (V.end() == true)
306 continue;
307 helper.showSelectedVersion(P, V, ver, verIsRel);
308 verset.insert(V);
309 }
310 return verset;
311 }
312 /*}}}*/
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;
318 bool showErrors;
319 switch(fallback) {
320 case VersionSet::ALL:
321 if (P->VersionList != 0)
322 for (V = P.VersionList(); V.end() != true; ++V)
323 verset.insert(V);
324 else
325 verset.insert(helper.canNotFindAllVer(Cache, P));
326 break;
327 case VersionSet::CANDANDINST:
328 verset.insert(getInstalledVer(Cache, P, helper));
329 verset.insert(getCandidateVer(Cache, P, helper));
330 break;
331 case VersionSet::CANDIDATE:
332 verset.insert(getCandidateVer(Cache, P, helper));
333 break;
334 case VersionSet::INSTALLED:
335 verset.insert(getInstalledVer(Cache, P, helper));
336 break;
337 case VersionSet::CANDINST:
338 showErrors = helper.showErrors(false);
339 V = getCandidateVer(Cache, P, helper);
340 if (V.end() == true)
341 V = getInstalledVer(Cache, P, helper);
342 helper.showErrors(showErrors);
343 if (V.end() == false)
344 verset.insert(V);
345 else
346 verset.insert(helper.canNotFindInstCandVer(Cache, P));
347 break;
348 case VersionSet::INSTCAND:
349 showErrors = helper.showErrors(false);
350 V = getInstalledVer(Cache, P, helper);
351 if (V.end() == true)
352 V = getCandidateVer(Cache, P, helper);
353 helper.showErrors(showErrors);
354 if (V.end() == false)
355 verset.insert(V);
356 else
357 verset.insert(helper.canNotFindInstCandVer(Cache, P));
358 break;
359 case VersionSet::NEWEST:
360 if (P->VersionList != 0)
361 verset.insert(P.VersionList());
362 else
363 verset.insert(helper.canNotFindNewestVer(Cache, P));
364 break;
365 }
366 }
367 /*}}}*/
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);
374 else {
375 if (unlikely(Cache.BuildPolicy() == false))
376 return pkgCache::VerIterator(*Cache);
377 Cand = Cache.GetPolicy()->GetCandidateVer(Pkg);
378 }
379 if (Cand.end() == true)
380 return helper.canNotFindCandidateVer(Cache, Pkg);
381 return Cand;
382 }
383 /*}}}*/
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();
390 }
391 /*}}}*/
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());
396 return PackageSet();
397 }
398 /*}}}*/
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());
403 return PackageSet();
404 }
405 /*}}}*/
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());
410 return PackageSet();
411 }
412 /*}}}*/
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());
418 return VersionSet();
419 }
420 /*}}}*/
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());
426 return VersionSet();
427 }
428 /*}}}*/
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);
435 }
436 /*}}}*/
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);
443 }
444 /*}}}*/
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);
451 }
452 /*}}}*/
453 }