0b099f4426df31aba0742511c1869aea5dd2f787
[ntk/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/cachefilter.h>
14 #include <apt-pkg/error.h>
15 #include <apt-pkg/strutl.h>
16 #include <apt-pkg/versionmatch.h>
17
18 #include <apti18n.h>
19
20 #include "cacheset.h"
21
22 #include <vector>
23
24 #include <regex.h>
25 /*}}}*/
26 namespace APT {
27 // FromTask - Return all packages in the cache from a specific task /*{{{*/
28 PackageSet PackageSet::FromTask(pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper) {
29 size_t const archfound = pattern.find_last_of(':');
30 std::string arch = "native";
31 if (archfound != std::string::npos) {
32 arch = pattern.substr(archfound+1);
33 pattern.erase(archfound);
34 }
35
36 if (pattern[pattern.length() -1] != '^')
37 return APT::PackageSet(TASK);
38 pattern.erase(pattern.length()-1);
39
40 if (unlikely(Cache.GetPkgCache() == 0 || Cache.GetDepCache() == 0))
41 return APT::PackageSet(TASK);
42
43 PackageSet pkgset(TASK);
44 // get the records
45 pkgRecords Recs(Cache);
46
47 // build regexp for the task
48 regex_t Pattern;
49 char S[300];
50 snprintf(S, sizeof(S), "^Task:.*[, ]%s([, ]|$)", pattern.c_str());
51 if(regcomp(&Pattern,S, REG_EXTENDED | REG_NOSUB | REG_NEWLINE) != 0) {
52 _error->Error("Failed to compile task regexp");
53 return pkgset;
54 }
55
56 for (pkgCache::GrpIterator Grp = Cache->GrpBegin(); Grp.end() == false; ++Grp) {
57 pkgCache::PkgIterator Pkg = Grp.FindPkg(arch);
58 if (Pkg.end() == true)
59 continue;
60 pkgCache::VerIterator ver = Cache[Pkg].CandidateVerIter(Cache);
61 if(ver.end() == true)
62 continue;
63
64 pkgRecords::Parser &parser = Recs.Lookup(ver.FileList());
65 const char *start, *end;
66 parser.GetRec(start,end);
67 unsigned int const length = end - start;
68 char buf[length];
69 strncpy(buf, start, length);
70 buf[length-1] = '\0';
71 if (regexec(&Pattern, buf, 0, 0, 0) != 0)
72 continue;
73
74 pkgset.insert(Pkg);
75 }
76 regfree(&Pattern);
77
78 if (pkgset.empty() == true)
79 return helper.canNotFindTask(Cache, pattern);
80
81 helper.showTaskSelection(pkgset, pattern);
82 return pkgset;
83 }
84 /*}}}*/
85 // FromRegEx - Return all packages in the cache matching a pattern /*{{{*/
86 PackageSet PackageSet::FromRegEx(pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper) {
87 static const char * const isregex = ".?+*|[^$";
88 if (pattern.find_first_of(isregex) == std::string::npos)
89 return PackageSet(REGEX);
90
91 size_t archfound = pattern.find_last_of(':');
92 std::string arch = "native";
93 if (archfound != std::string::npos) {
94 arch = pattern.substr(archfound+1);
95 if (arch.find_first_of(isregex) == std::string::npos)
96 pattern.erase(archfound);
97 else
98 arch = "native";
99 }
100
101 if (unlikely(Cache.GetPkgCache() == 0))
102 return PackageSet(REGEX);
103
104 APT::CacheFilter::PackageNameMatchesRegEx regexfilter(pattern);
105
106 PackageSet pkgset(REGEX);
107 for (pkgCache::GrpIterator Grp = Cache.GetPkgCache()->GrpBegin(); Grp.end() == false; ++Grp) {
108 if (regexfilter(Grp) == false)
109 continue;
110 pkgCache::PkgIterator Pkg = Grp.FindPkg(arch);
111 if (Pkg.end() == true) {
112 if (archfound == std::string::npos) {
113 std::vector<std::string> archs = APT::Configuration::getArchitectures();
114 for (std::vector<std::string>::const_iterator a = archs.begin();
115 a != archs.end() && Pkg.end() != true; ++a)
116 Pkg = Grp.FindPkg(*a);
117 }
118 if (Pkg.end() == true)
119 continue;
120 }
121
122 pkgset.insert(Pkg);
123 }
124
125 if (pkgset.empty() == true)
126 return helper.canNotFindRegEx(Cache, pattern);
127
128 helper.showRegExSelection(pkgset, pattern);
129 return pkgset;
130 }
131 /*}}}*/
132 // FromName - Returns the package defined by this string /*{{{*/
133 pkgCache::PkgIterator PackageSet::FromName(pkgCacheFile &Cache,
134 std::string const &str, CacheSetHelper &helper) {
135 std::string pkg = str;
136 size_t archfound = pkg.find_last_of(':');
137 std::string arch;
138 if (archfound != std::string::npos) {
139 arch = pkg.substr(archfound+1);
140 pkg.erase(archfound);
141 }
142
143 if (Cache.GetPkgCache() == 0)
144 return pkgCache::PkgIterator(Cache, 0);
145
146 pkgCache::PkgIterator Pkg(Cache, 0);
147 if (arch.empty() == true) {
148 pkgCache::GrpIterator Grp = Cache.GetPkgCache()->FindGrp(pkg);
149 if (Grp.end() == false)
150 Pkg = Grp.FindPreferredPkg();
151 } else
152 Pkg = Cache.GetPkgCache()->FindPkg(pkg, arch);
153
154 if (Pkg.end() == true)
155 return helper.canNotFindPkgName(Cache, str);
156 return Pkg;
157 }
158 /*}}}*/
159 // GroupedFromCommandLine - Return all versions specified on commandline/*{{{*/
160 std::map<unsigned short, PackageSet> PackageSet::GroupedFromCommandLine(
161 pkgCacheFile &Cache, const char **cmdline,
162 std::list<PackageSet::Modifier> const &mods,
163 unsigned short const &fallback, CacheSetHelper &helper) {
164 std::map<unsigned short, PackageSet> pkgsets;
165 for (const char **I = cmdline; *I != 0; ++I) {
166 unsigned short modID = fallback;
167 std::string str = *I;
168 bool modifierPresent = false;
169 for (std::list<PackageSet::Modifier>::const_iterator mod = mods.begin();
170 mod != mods.end(); ++mod) {
171 size_t const alength = strlen(mod->Alias);
172 switch(mod->Pos) {
173 case PackageSet::Modifier::POSTFIX:
174 if (str.compare(str.length() - alength, alength,
175 mod->Alias, 0, alength) != 0)
176 continue;
177 str.erase(str.length() - alength);
178 modID = mod->ID;
179 break;
180 case PackageSet::Modifier::PREFIX:
181 continue;
182 case PackageSet::Modifier::NONE:
183 continue;
184 }
185 modifierPresent = true;
186 break;
187 }
188 if (modifierPresent == true) {
189 bool const errors = helper.showErrors(false);
190 pkgCache::PkgIterator Pkg = FromName(Cache, *I, helper);
191 helper.showErrors(errors);
192 if (Pkg.end() == false) {
193 pkgsets[fallback].insert(Pkg);
194 continue;
195 }
196 }
197 pkgsets[modID].insert(PackageSet::FromString(Cache, str, helper));
198 }
199 return pkgsets;
200 }
201 /*}}}*/
202 // FromCommandLine - Return all packages specified on commandline /*{{{*/
203 PackageSet PackageSet::FromCommandLine(pkgCacheFile &Cache, const char **cmdline, CacheSetHelper &helper) {
204 PackageSet pkgset;
205 for (const char **I = cmdline; *I != 0; ++I) {
206 PackageSet pset = FromString(Cache, *I, helper);
207 pkgset.insert(pset.begin(), pset.end());
208 }
209 return pkgset;
210 }
211 /*}}}*/
212 // FromString - Return all packages matching a specific string /*{{{*/
213 PackageSet PackageSet::FromString(pkgCacheFile &Cache, std::string const &str, CacheSetHelper &helper) {
214 _error->PushToStack();
215
216 PackageSet pkgset;
217 pkgCache::PkgIterator Pkg = FromName(Cache, str, helper);
218 if (Pkg.end() == false)
219 pkgset.insert(Pkg);
220 else {
221 pkgset = FromTask(Cache, str, helper);
222 if (pkgset.empty() == true) {
223 pkgset = FromRegEx(Cache, str, helper);
224 if (pkgset.empty() == true)
225 pkgset = helper.canNotFindPackage(Cache, str);
226 }
227 }
228
229 if (pkgset.empty() == false)
230 _error->RevertToStack();
231 else
232 _error->MergeWithStack();
233 return pkgset;
234 }
235 /*}}}*/
236 // GroupedFromCommandLine - Return all versions specified on commandline/*{{{*/
237 std::map<unsigned short, VersionSet> VersionSet::GroupedFromCommandLine(
238 pkgCacheFile &Cache, const char **cmdline,
239 std::list<VersionSet::Modifier> const &mods,
240 unsigned short const &fallback, CacheSetHelper &helper) {
241 std::map<unsigned short, VersionSet> versets;
242 for (const char **I = cmdline; *I != 0; ++I) {
243 unsigned short modID = fallback;
244 VersionSet::Version select = VersionSet::NEWEST;
245 std::string str = *I;
246 bool modifierPresent = false;
247 for (std::list<VersionSet::Modifier>::const_iterator mod = mods.begin();
248 mod != mods.end(); ++mod) {
249 if (modID == fallback && mod->ID == fallback)
250 select = mod->SelectVersion;
251 size_t const alength = strlen(mod->Alias);
252 switch(mod->Pos) {
253 case VersionSet::Modifier::POSTFIX:
254 if (str.compare(str.length() - alength, alength,
255 mod->Alias, 0, alength) != 0)
256 continue;
257 str.erase(str.length() - alength);
258 modID = mod->ID;
259 select = mod->SelectVersion;
260 break;
261 case VersionSet::Modifier::PREFIX:
262 continue;
263 case VersionSet::Modifier::NONE:
264 continue;
265 }
266 modifierPresent = true;
267 break;
268 }
269
270 if (modifierPresent == true) {
271 bool const errors = helper.showErrors(false);
272 VersionSet const vset = VersionSet::FromString(Cache, std::string(*I), select, helper, true);
273 helper.showErrors(errors);
274 if (vset.empty() == false) {
275 versets[fallback].insert(vset);
276 continue;
277 }
278 }
279 versets[modID].insert(VersionSet::FromString(Cache, str, select , helper));
280 }
281 return versets;
282 }
283 /*}}}*/
284 // FromCommandLine - Return all versions specified on commandline /*{{{*/
285 APT::VersionSet VersionSet::FromCommandLine(pkgCacheFile &Cache, const char **cmdline,
286 APT::VersionSet::Version const &fallback, CacheSetHelper &helper) {
287 VersionSet verset;
288 for (const char **I = cmdline; *I != 0; ++I)
289 verset.insert(VersionSet::FromString(Cache, *I, fallback, helper));
290 return verset;
291 }
292 /*}}}*/
293 // FromString - Returns all versions spedcified by a string /*{{{*/
294 APT::VersionSet VersionSet::FromString(pkgCacheFile &Cache, std::string pkg,
295 APT::VersionSet::Version const &fallback, CacheSetHelper &helper,
296 bool const &onlyFromName) {
297 std::string ver;
298 bool verIsRel = false;
299 size_t const vertag = pkg.find_last_of("/=");
300 if (vertag != string::npos) {
301 ver = pkg.substr(vertag+1);
302 verIsRel = (pkg[vertag] == '/');
303 pkg.erase(vertag);
304 }
305 PackageSet pkgset;
306 if (onlyFromName == false)
307 pkgset = PackageSet::FromString(Cache, pkg, helper);
308 else {
309 pkgset.insert(PackageSet::FromName(Cache, pkg, helper));
310 }
311
312 VersionSet verset;
313 bool errors = true;
314 if (pkgset.getConstructor() != PackageSet::UNKNOWN)
315 errors = helper.showErrors(false);
316 for (PackageSet::const_iterator P = pkgset.begin();
317 P != pkgset.end(); ++P) {
318 if (vertag == string::npos) {
319 verset.insert(VersionSet::FromPackage(Cache, P, fallback, helper));
320 continue;
321 }
322 pkgCache::VerIterator V;
323 if (ver == "installed")
324 V = getInstalledVer(Cache, P, helper);
325 else if (ver == "candidate")
326 V = getCandidateVer(Cache, P, helper);
327 else {
328 pkgVersionMatch Match(ver, (verIsRel == true ? pkgVersionMatch::Release :
329 pkgVersionMatch::Version));
330 V = Match.Find(P);
331 if (V.end() == true) {
332 if (verIsRel == true)
333 _error->Error(_("Release '%s' for '%s' was not found"),
334 ver.c_str(), P.FullName(true).c_str());
335 else
336 _error->Error(_("Version '%s' for '%s' was not found"),
337 ver.c_str(), P.FullName(true).c_str());
338 continue;
339 }
340 }
341 if (V.end() == true)
342 continue;
343 helper.showSelectedVersion(P, V, ver, verIsRel);
344 verset.insert(V);
345 }
346 if (pkgset.getConstructor() != PackageSet::UNKNOWN)
347 helper.showErrors(errors);
348 return verset;
349 }
350 /*}}}*/
351 // FromPackage - versions from package based on fallback /*{{{*/
352 VersionSet VersionSet::FromPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &P,
353 VersionSet::Version const &fallback, CacheSetHelper &helper) {
354 VersionSet verset;
355 pkgCache::VerIterator V;
356 bool showErrors;
357 switch(fallback) {
358 case VersionSet::ALL:
359 if (P->VersionList != 0)
360 for (V = P.VersionList(); V.end() != true; ++V)
361 verset.insert(V);
362 else
363 verset.insert(helper.canNotFindAllVer(Cache, P));
364 break;
365 case VersionSet::CANDANDINST:
366 verset.insert(getInstalledVer(Cache, P, helper));
367 verset.insert(getCandidateVer(Cache, P, helper));
368 break;
369 case VersionSet::CANDIDATE:
370 verset.insert(getCandidateVer(Cache, P, helper));
371 break;
372 case VersionSet::INSTALLED:
373 verset.insert(getInstalledVer(Cache, P, helper));
374 break;
375 case VersionSet::CANDINST:
376 showErrors = helper.showErrors(false);
377 V = getCandidateVer(Cache, P, helper);
378 if (V.end() == true)
379 V = getInstalledVer(Cache, P, helper);
380 helper.showErrors(showErrors);
381 if (V.end() == false)
382 verset.insert(V);
383 else
384 verset.insert(helper.canNotFindInstCandVer(Cache, P));
385 break;
386 case VersionSet::INSTCAND:
387 showErrors = helper.showErrors(false);
388 V = getInstalledVer(Cache, P, helper);
389 if (V.end() == true)
390 V = getCandidateVer(Cache, P, helper);
391 helper.showErrors(showErrors);
392 if (V.end() == false)
393 verset.insert(V);
394 else
395 verset.insert(helper.canNotFindInstCandVer(Cache, P));
396 break;
397 case VersionSet::NEWEST:
398 if (P->VersionList != 0)
399 verset.insert(P.VersionList());
400 else
401 verset.insert(helper.canNotFindNewestVer(Cache, P));
402 break;
403 }
404 return verset;
405 }
406 /*}}}*/
407 // getCandidateVer - Returns the candidate version of the given package /*{{{*/
408 pkgCache::VerIterator VersionSet::getCandidateVer(pkgCacheFile &Cache,
409 pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper) {
410 pkgCache::VerIterator Cand;
411 if (Cache.IsPolicyBuilt() == true || Cache.IsDepCacheBuilt() == false)
412 {
413 if (unlikely(Cache.GetPolicy() == 0))
414 return pkgCache::VerIterator(Cache);
415 Cand = Cache.GetPolicy()->GetCandidateVer(Pkg);
416 } else {
417 Cand = Cache[Pkg].CandidateVerIter(Cache);
418 }
419 if (Cand.end() == true)
420 return helper.canNotFindCandidateVer(Cache, Pkg);
421 return Cand;
422 }
423 /*}}}*/
424 // getInstalledVer - Returns the installed version of the given package /*{{{*/
425 pkgCache::VerIterator VersionSet::getInstalledVer(pkgCacheFile &Cache,
426 pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper) {
427 if (Pkg->CurrentVer == 0)
428 return helper.canNotFindInstalledVer(Cache, Pkg);
429 return Pkg.CurrentVer();
430 }
431 /*}}}*/
432 // canNotFindPkgName - handle the case no package has this name /*{{{*/
433 pkgCache::PkgIterator CacheSetHelper::canNotFindPkgName(pkgCacheFile &Cache,
434 std::string const &str) {
435 if (ShowError == true)
436 _error->Error(_("Unable to locate package %s"), str.c_str());
437 return pkgCache::PkgIterator(Cache, 0);
438 }
439 /*}}}*/
440 // canNotFindTask - handle the case no package is found for a task /*{{{*/
441 PackageSet CacheSetHelper::canNotFindTask(pkgCacheFile &Cache, std::string pattern) {
442 if (ShowError == true)
443 _error->Error(_("Couldn't find task '%s'"), pattern.c_str());
444 return PackageSet();
445 }
446 /*}}}*/
447 // canNotFindRegEx - handle the case no package is found by a regex /*{{{*/
448 PackageSet CacheSetHelper::canNotFindRegEx(pkgCacheFile &Cache, std::string pattern) {
449 if (ShowError == true)
450 _error->Error(_("Couldn't find any package by regex '%s'"), pattern.c_str());
451 return PackageSet();
452 }
453 /*}}}*/
454 // canNotFindPackage - handle the case no package is found from a string/*{{{*/
455 PackageSet CacheSetHelper::canNotFindPackage(pkgCacheFile &Cache, std::string const &str) {
456 return PackageSet();
457 }
458 /*}}}*/
459 // canNotFindAllVer /*{{{*/
460 VersionSet CacheSetHelper::canNotFindAllVer(pkgCacheFile &Cache,
461 pkgCache::PkgIterator const &Pkg) {
462 if (ShowError == true)
463 _error->Error(_("Can't select versions from package '%s' as it purely virtual"), Pkg.FullName(true).c_str());
464 return VersionSet();
465 }
466 /*}}}*/
467 // canNotFindInstCandVer /*{{{*/
468 VersionSet CacheSetHelper::canNotFindInstCandVer(pkgCacheFile &Cache,
469 pkgCache::PkgIterator const &Pkg) {
470 if (ShowError == true)
471 _error->Error(_("Can't select installed nor candidate version from package '%s' as it has neither of them"), Pkg.FullName(true).c_str());
472 return VersionSet();
473 }
474 /*}}}*/
475 // canNotFindInstCandVer /*{{{*/
476 VersionSet CacheSetHelper::canNotFindCandInstVer(pkgCacheFile &Cache,
477 pkgCache::PkgIterator const &Pkg) {
478 if (ShowError == true)
479 _error->Error(_("Can't select installed nor candidate version from package '%s' as it has neither of them"), Pkg.FullName(true).c_str());
480 return VersionSet();
481 }
482 /*}}}*/
483 // canNotFindNewestVer /*{{{*/
484 pkgCache::VerIterator CacheSetHelper::canNotFindNewestVer(pkgCacheFile &Cache,
485 pkgCache::PkgIterator const &Pkg) {
486 if (ShowError == true)
487 _error->Error(_("Can't select newest version from package '%s' as it is purely virtual"), Pkg.FullName(true).c_str());
488 return pkgCache::VerIterator(Cache, 0);
489 }
490 /*}}}*/
491 // canNotFindCandidateVer /*{{{*/
492 pkgCache::VerIterator CacheSetHelper::canNotFindCandidateVer(pkgCacheFile &Cache,
493 pkgCache::PkgIterator const &Pkg) {
494 if (ShowError == true)
495 _error->Error(_("Can't select candidate version from package %s as it has no candidate"), Pkg.FullName(true).c_str());
496 return pkgCache::VerIterator(Cache, 0);
497 }
498 /*}}}*/
499 // canNotFindInstalledVer /*{{{*/
500 pkgCache::VerIterator CacheSetHelper::canNotFindInstalledVer(pkgCacheFile &Cache,
501 pkgCache::PkgIterator const &Pkg) {
502 if (ShowError == true)
503 _error->Error(_("Can't select installed version from package %s as it is not installed"), Pkg.FullName(true).c_str());
504 return pkgCache::VerIterator(Cache, 0);
505 }
506 /*}}}*/
507 }