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