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