apt  2.2.4
About: Apt (Advanced Package Tool) is a management system for software packages (Debian/Ubuntu). Release series 2.2.
  Fossies Dox: apt-2.2.4.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

cacheset.cc
Go to the documentation of this file.
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 
15 #include <apt-pkg/cachefile.h>
16 #include <apt-pkg/cachefilter.h>
17 #include <apt-pkg/cacheset.h>
18 #include <apt-pkg/configuration.h>
19 #include <apt-pkg/depcache.h>
20 #include <apt-pkg/error.h>
21 #include <apt-pkg/fileutl.h>
22 #include <apt-pkg/macros.h>
23 #include <apt-pkg/pkgcache.h>
24 #include <apt-pkg/pkgrecords.h>
25 #include <apt-pkg/policy.h>
26 #include <apt-pkg/versionmatch.h>
27 
28 #include <list>
29 #include <string>
30 #include <vector>
31 #include <regex.h>
32 #include <stddef.h>
33 #include <stdio.h>
34 #include <string.h>
35 
36 #include <apti18n.h>
37  /*}}}*/
38 namespace APT {
39 // PackageFrom - selecting the appropriate method for package selection /*{{{*/
41  pkgCacheFile &Cache, std::string const &pattern) {
42  switch (select) {
43  case UNKNOWN: return false;
44  case REGEX: return PackageFromRegEx(pci, Cache, pattern);
45  case TASK: return PackageFromTask(pci, Cache, pattern);
46  case FNMATCH: return PackageFromFnmatch(pci, Cache, pattern);
47  case PACKAGENAME: return PackageFromPackageName(pci, Cache, pattern);
48  case STRING: return PackageFromString(pci, Cache, pattern);
49  case PATTERN: return PackageFromPattern(pci, Cache, pattern);
50  }
51  return false;
52 }
53  /*}}}*/
54 // PackageFromTask - Return all packages in the cache from a specific task /*{{{*/
55 bool CacheSetHelper::PackageFromTask(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern) {
56  size_t const archfound = pattern.find_last_of(':');
57  std::string arch = "native";
58  if (archfound != std::string::npos) {
59  arch = pattern.substr(archfound+1);
60  pattern.erase(archfound);
61  }
62 
63  if (pattern[pattern.length() -1] != '^')
64  return false;
65  pattern.erase(pattern.length()-1);
66 
67  if (unlikely(Cache.GetPkgCache() == 0 || Cache.GetDepCache() == 0))
68  return false;
69 
70  bool const wasEmpty = pci->empty();
71  if (wasEmpty == true)
73 
74  // get the records
75  pkgRecords Recs(Cache);
76 
77  // build regexp for the task
78  regex_t Pattern;
79  char S[300];
80  snprintf(S, sizeof(S), "^Task:.*[, ]%s([, ]|$)", pattern.c_str());
81  if(regcomp(&Pattern,S, REG_EXTENDED | REG_NOSUB | REG_NEWLINE) != 0) {
82  _error->Error("Failed to compile task regexp");
83  return false;
84  }
85 
86  bool found = false;
87  for (pkgCache::GrpIterator Grp = Cache->GrpBegin(); Grp.end() == false; ++Grp) {
88  pkgCache::PkgIterator Pkg = Grp.FindPkg(arch);
89  if (Pkg.end() == true)
90  continue;
91  pkgCache::VerIterator ver = Cache[Pkg].CandidateVerIter(Cache);
92  if(ver.end() == true)
93  continue;
94 
95  pkgRecords::Parser &parser = Recs.Lookup(ver.FileList());
96  const char *start, *end;
97  parser.GetRec(start,end);
98  unsigned int const length = end - start;
99  if (unlikely(length == 0))
100  continue;
101  char buf[length];
102  strncpy(buf, start, length);
103  buf[length-1] = '\0';
104  if (regexec(&Pattern, buf, 0, 0, 0) != 0)
105  continue;
106 
107  pci->insert(Pkg);
109  found = true;
110  }
111  regfree(&Pattern);
112 
113  if (found == false) {
114  canNotFindPackage(CacheSetHelper::TASK, pci, Cache, pattern);
116  return false;
117  }
118 
119  if (wasEmpty == false && pci->getConstructor() != CacheSetHelper::UNKNOWN)
121 
122  return true;
123 }
124  /*}}}*/
125 // PackageFromRegEx - Return all packages in the cache matching a pattern /*{{{*/
126 bool CacheSetHelper::PackageFromRegEx(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern) {
127  static const char * const isregex = ".?+*|[^$";
128 
129  if (_config->FindB("APT::Cmd::Pattern-Only", false))
130  {
131  // Only allow explicit regexp pattern.
132  if (pattern.size() == 0 || (pattern[0] != '^' && pattern[pattern.size() - 1] != '$'))
133  return false;
134  } else {
135  if (pattern.find_first_of(isregex) == std::string::npos)
136  return false;
137  }
138 
139  bool const wasEmpty = pci->empty();
140  if (wasEmpty == true)
142 
143  size_t archfound = pattern.find_last_of(':');
144  std::string arch = "native";
145  if (archfound != std::string::npos) {
146  arch = pattern.substr(archfound+1);
147  if (arch.find_first_of(isregex) == std::string::npos)
148  pattern.erase(archfound);
149  else
150  arch = "native";
151  }
152 
153  if (unlikely(Cache.GetPkgCache() == 0))
154  return false;
155 
156  APT::CacheFilter::PackageNameMatchesRegEx regexfilter(pattern);
157 
158  bool found = false;
159  for (pkgCache::GrpIterator Grp = Cache.GetPkgCache()->GrpBegin(); Grp.end() == false; ++Grp) {
160  if (regexfilter(Grp) == false)
161  continue;
162  pkgCache::PkgIterator Pkg = Grp.FindPkg(arch);
163  if (Pkg.end() == true) {
164  if (archfound == std::string::npos)
165  Pkg = Grp.FindPreferredPkg(true);
166  if (Pkg.end() == true)
167  continue;
168  }
169 
170  pci->insert(Pkg);
172  found = true;
173  }
174 
175  if (found == false) {
176  canNotFindPackage(CacheSetHelper::REGEX, pci, Cache, pattern);
178  return false;
179  }
180 
181  if (wasEmpty == false && pci->getConstructor() != CacheSetHelper::UNKNOWN)
183 
184  return true;
185 }
186  /*}}}*/
187 // PackageFromFnmatch - Returns the package defined by this fnmatch /*{{{*/
189  pkgCacheFile &Cache, std::string pattern)
190 {
191  static const char * const isfnmatch = ".?*[]!";
192  // Whitelist approach: Anything not in here is not a valid pattern
193  static const char *const isfnmatch_strict = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-.:*";
194 
195  if (_config->FindB("APT::Cmd::Pattern-Only", false) && pattern.find_first_not_of(isfnmatch_strict) != std::string::npos)
196  return false;
197  if (pattern.find_first_of(isfnmatch) == std::string::npos)
198  return false;
199 
200  bool const wasEmpty = pci->empty();
201  if (wasEmpty == true)
203 
204  size_t archfound = pattern.find_last_of(':');
205  std::string arch = "native";
206  if (archfound != std::string::npos) {
207  arch = pattern.substr(archfound+1);
208  if (arch.find_first_of(isfnmatch) == std::string::npos)
209  pattern.erase(archfound);
210  else
211  arch = "native";
212  }
213 
214  if (unlikely(Cache.GetPkgCache() == 0))
215  return false;
216 
218 
219  bool found = false;
220  for (pkgCache::GrpIterator Grp = Cache.GetPkgCache()->GrpBegin(); Grp.end() == false; ++Grp) {
221  if (filter(Grp) == false)
222  continue;
223  pkgCache::PkgIterator Pkg = Grp.FindPkg(arch);
224  if (Pkg.end() == true) {
225  if (archfound == std::string::npos)
226  Pkg = Grp.FindPreferredPkg(true);
227  if (Pkg.end() == true)
228  continue;
229  }
230 
231  pci->insert(Pkg);
233  found = true;
234  }
235 
236  if (found == false) {
237  canNotFindPackage(CacheSetHelper::FNMATCH, pci, Cache, pattern);
239  return false;
240  }
241 
242  if (wasEmpty == false && pci->getConstructor() != CacheSetHelper::UNKNOWN)
244 
245  return true;
246 }
247  /*}}}*/
248 // PackageFromPackageName - Returns the package defined by this string /*{{{*/
250  std::string pkg) {
251  if (unlikely(Cache.GetPkgCache() == 0))
252  return false;
253 
254  std::string const pkgstring = pkg;
255  size_t const archfound = pkg.find_last_of(':');
256  std::string arch;
257  if (archfound != std::string::npos) {
258  arch = pkg.substr(archfound+1);
259  pkg.erase(archfound);
260  if (arch == "all" || arch == "native")
261  arch = _config->Find("APT::Architecture");
262  }
263 
264  pkgCache::GrpIterator Grp = Cache.GetPkgCache()->FindGrp(pkg);
265  if (Grp.end() == false) {
266  if (arch.empty() == true) {
267  pkgCache::PkgIterator Pkg = Grp.FindPreferredPkg();
268  if (Pkg.end() == false)
269  {
270  pci->insert(Pkg);
271  return true;
272  }
273  } else {
274  bool found = false;
275  // for 'linux-any' return the first package matching, for 'linux-*' return all matches
276  bool const isGlobal = arch.find('*') != std::string::npos;
278  for (pkgCache::PkgIterator Pkg = Grp.PackageList(); Pkg.end() == false; Pkg = Grp.NextPkg(Pkg)) {
279  if (pams(Pkg) == false)
280  continue;
281  pci->insert(Pkg);
282  found = true;
283  if (isGlobal == false)
284  break;
285  }
286  if (found == true)
287  return true;
288  }
289  }
290 
291  pkgCache::PkgIterator Pkg = canNotFindPkgName(Cache, pkgstring);
292  if (Pkg.end() == true)
293  return false;
294 
295  pci->insert(Pkg);
296  return true;
297 }
298 
299 bool CacheSetHelper::PackageFromPattern(PackageContainerInterface *const pci, pkgCacheFile &Cache, std::string const &pattern)
300 {
301  if (pattern.size() < 1 || (pattern[0] != '?' && pattern[0] != '~'))
302  return false;
303 
304  auto compiledPattern = APT::CacheFilter::ParsePattern(pattern, &Cache);
305  if (!compiledPattern)
306  return false;
307 
308  for (pkgCache::PkgIterator Pkg = Cache->PkgBegin(); Pkg.end() == false; ++Pkg)
309  {
310  if ((*compiledPattern)(Pkg) == false)
311  continue;
312 
313  pci->insert(Pkg);
314  }
315  return true;
316 }
317  /*}}}*/
318 // PackageFromString - Return all packages matching a specific string /*{{{*/
319 bool CacheSetHelper::PackageFromString(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string const &str) {
320  bool found = true;
321  _error->PushToStack();
322 
323  if (PackageFrom(CacheSetHelper::PATTERN, pci, Cache, str) == false &&
324  PackageFrom(CacheSetHelper::PACKAGENAME, pci, Cache, str) == false &&
325  PackageFrom(CacheSetHelper::TASK, pci, Cache, str) == false &&
326  // FIXME: hm, hm, regexp/fnmatch incompatible?
327  PackageFrom(CacheSetHelper::FNMATCH, pci, Cache, str) == false &&
328  PackageFrom(CacheSetHelper::REGEX, pci, Cache, str) == false)
329  {
331  found = false;
332  }
333 
334  if (found == true)
335  _error->RevertToStack();
336  else
337  _error->MergeWithStack();
338  return found;
339 }
340  /*}}}*/
341 // PackageFromCommandLine - Return all packages specified on commandline /*{{{*/
342 bool CacheSetHelper::PackageFromCommandLine(PackageContainerInterface * const pci, pkgCacheFile &Cache, const char **cmdline) {
343  bool found = false;
344  for (const char **I = cmdline; *I != 0; ++I)
345  found |= PackageFrom(CacheSetHelper::STRING, pci, Cache, *I);
346  return found;
347 }
348  /*}}}*/
349 // FromModifierCommandLine - helper doing the work for PKG:GroupedFromCommandLine /*{{{*/
351  pkgCacheFile &Cache, const char * cmdline,
352  std::list<PkgModifier> const &mods) {
353  std::string str = cmdline;
354  unsigned short fallback = modID;
355  bool modifierPresent = false;
356  for (std::list<PkgModifier>::const_iterator mod = mods.begin();
357  mod != mods.end(); ++mod) {
358  size_t const alength = strlen(mod->Alias);
359  switch(mod->Pos) {
361  if (str.compare(str.length() - alength, alength,
362  mod->Alias, 0, alength) != 0)
363  continue;
364  str.erase(str.length() - alength);
365  modID = mod->ID;
366  break;
367  case PkgModifier::PREFIX:
368  continue;
369  case PkgModifier::NONE:
370  continue;
371  }
372  modifierPresent = true;
373  break;
374  }
375  if (modifierPresent == true) {
376  bool const errors = showErrors(false);
377  bool const found = PackageFrom(PACKAGENAME, pci, Cache, cmdline);
378  showErrors(errors);
379  if (found == true) {
380  modID = fallback;
381  return true;
382  }
383  }
384  return PackageFrom(CacheSetHelper::PACKAGENAME, pci, Cache, str);
385 }
386  /*}}}*/
387 // FromModifierCommandLine - helper doing the work for VER:GroupedFromCommandLine /*{{{*/
389  VersionContainerInterface * const vci,
390  pkgCacheFile &Cache, const char * cmdline,
391  std::list<Modifier> const &mods,
392  CacheSetHelper &helper) {
394  std::string str = cmdline;
395  if (unlikely(str.empty() == true))
396  return false;
397  bool modifierPresent = false;
398  unsigned short fallback = modID;
399  for (std::list<Modifier>::const_iterator mod = mods.begin();
400  mod != mods.end(); ++mod) {
401  if (modID == fallback && mod->ID == fallback)
402  select = mod->SelectVersion;
403  size_t const alength = strlen(mod->Alias);
404  switch(mod->Pos) {
405  case Modifier::POSTFIX:
406  if (str.length() <= alength ||
407  str.compare(str.length() - alength, alength, mod->Alias, 0, alength) != 0)
408  continue;
409  str.erase(str.length() - alength);
410  modID = mod->ID;
411  select = mod->SelectVersion;
412  break;
413  case Modifier::PREFIX:
414  continue;
415  case Modifier::NONE:
416  continue;
417  }
418  modifierPresent = true;
419  break;
420  }
421  if (modifierPresent == true) {
422  bool const errors = helper.showErrors(false);
423  bool const found = VersionContainerInterface::FromString(vci, Cache, cmdline, select, helper, true);
424  helper.showErrors(errors);
425  if (found == true) {
426  modID = fallback;
427  return true;
428  }
429  }
430  return FromString(vci, Cache, str, select, helper);
431 }
432  /*}}}*/
433 // FromCommandLine - Return all versions specified on commandline /*{{{*/
435  pkgCacheFile &Cache, const char **cmdline,
436  CacheSetHelper::VerSelector const fallback,
437  CacheSetHelper &helper) {
438  bool found = false;
439  for (const char **I = cmdline; *I != 0; ++I)
440  found |= VersionContainerInterface::FromString(vci, Cache, *I, fallback, helper);
441  return found;
442 }
443  /*}}}*/
444 // FromString - Returns all versions spedcified by a string /*{{{*/
446  pkgCacheFile &Cache, std::string pkg,
447  CacheSetHelper::VerSelector const fallback,
448  CacheSetHelper &helper,
449  bool const onlyFromName) {
450  std::string ver;
451  bool verIsRel = false;
452  size_t const vertag = pkg.find_last_of("/=");
453  if (vertag != std::string::npos) {
454  ver = pkg.substr(vertag+1);
455  verIsRel = (pkg[vertag] == '/');
456  pkg.erase(vertag);
457  }
458 
459  PackageSet pkgset;
460  if (onlyFromName == false)
461  helper.PackageFrom(CacheSetHelper::STRING, &pkgset, Cache, pkg);
462  else {
463  helper.PackageFrom(CacheSetHelper::PACKAGENAME, &pkgset, Cache, pkg);
464  }
465 
466  bool errors = true;
467  if (pkgset.getConstructor() != CacheSetHelper::UNKNOWN)
468  errors = helper.showErrors(false);
469 
470  bool found = false;
471  for (PackageSet::const_iterator P = pkgset.begin();
472  P != pkgset.end(); ++P) {
473  if (vertag == std::string::npos) {
474  found |= VersionContainerInterface::FromPackage(vci, Cache, P, fallback, helper);
475  continue;
476  }
478  if (ver == "installed")
479  V = getInstalledVer(Cache, P, helper);
480  else if (ver == "candidate")
481  V = getCandidateVer(Cache, P, helper);
482  else if (ver == "newest") {
483  if (P->VersionList != 0)
484  V = P.VersionList();
485  else
486  V = helper.canNotGetVersion(CacheSetHelper::NEWEST, Cache, P);
487  } else {
488  pkgVersionMatch Match(ver, (verIsRel == true ? pkgVersionMatch::Release :
490  V = Match.Find(P);
491  if (V.end() == true) {
492  if (verIsRel == true)
493  _error->Error(_("Release '%s' for '%s' was not found"),
494  ver.c_str(), P.FullName(true).c_str());
495  else
496  _error->Error(_("Version '%s' for '%s' was not found"),
497  ver.c_str(), P.FullName(true).c_str());
498  continue;
499  }
500  }
501  if (V.end() == true)
502  continue;
503  if (verIsRel == true)
505  else
507  vci->insert(V);
508  found = true;
509  }
510  if (pkgset.getConstructor() != CacheSetHelper::UNKNOWN)
511  helper.showErrors(errors);
512  return found;
513 }
514  /*}}}*/
515 // FromPackage - versions from package based on fallback /*{{{*/
517  pkgCacheFile &Cache,
518  pkgCache::PkgIterator const &P,
519  CacheSetHelper::VerSelector const fallback,
520  CacheSetHelper &helper) {
522  bool showErrors;
523  bool found = false;
524  switch(fallback) {
525  case CacheSetHelper::ALL:
526  if (P->VersionList != 0)
527  for (V = P.VersionList(); V.end() != true; ++V)
528  found |= vci->insert(V);
529  else
530  helper.canNotFindVersion(CacheSetHelper::ALL, vci, Cache, P);
531  break;
533  found |= vci->insert(getInstalledVer(Cache, P, helper));
534  found |= vci->insert(getCandidateVer(Cache, P, helper));
535  break;
537  found |= vci->insert(getCandidateVer(Cache, P, helper));
538  break;
540  found |= vci->insert(getInstalledVer(Cache, P, helper));
541  break;
543  showErrors = helper.showErrors(false);
544  V = getCandidateVer(Cache, P, helper);
545  if (V.end() == true)
546  V = getInstalledVer(Cache, P, helper);
547  helper.showErrors(showErrors);
548  if (V.end() == false)
549  found |= vci->insert(V);
550  else
551  helper.canNotFindVersion(CacheSetHelper::CANDINST, vci, Cache, P);
552  break;
554  showErrors = helper.showErrors(false);
555  V = getInstalledVer(Cache, P, helper);
556  if (V.end() == true)
557  V = getCandidateVer(Cache, P, helper);
558  helper.showErrors(showErrors);
559  if (V.end() == false)
560  found |= vci->insert(V);
561  else
562  helper.canNotFindVersion(CacheSetHelper::INSTCAND, vci, Cache, P);
563  break;
565  if (P->VersionList != 0)
566  found |= vci->insert(P.VersionList());
567  else
568  helper.canNotFindVersion(CacheSetHelper::NEWEST, vci, Cache, P);
569  break;
572  // both make no sense here, so always false
573  return false;
574  }
575  return found;
576 }
577  /*}}}*/
578 // FromDependency - versions satisfying a given dependency /*{{{*/
580  pkgCacheFile &Cache,
581  pkgCache::DepIterator const &D,
582  CacheSetHelper::VerSelector const selector,
583  CacheSetHelper &helper)
584 {
585  bool found = false;
586  auto const insertVersion = [&](pkgCache::PkgIterator const &TP, pkgCache::VerIterator const &TV) {
587  if (not TV.end() && not D.IsIgnorable(TP) && D.IsSatisfied(TV))
588  {
589  vci->insert(TV);
590  found = true;
591  }
592  };
593  pkgCache::PkgIterator const T = D.TargetPkg();
594  auto const insertAllTargetVersions = [&](auto const &getTargetVersion) {
595  insertVersion(T, getTargetVersion(T));
596  for (auto Prv = T.ProvidesList(); not Prv.end(); ++Prv)
597  {
598  if (D.IsIgnorable(Prv))
599  continue;
600  auto const OP = Prv.OwnerPkg();
601  auto const TV = getTargetVersion(OP);
602  if (Prv.OwnerVer() == TV && D.IsSatisfied(Prv))
603  {
604  vci->insert(TV);
605  found = true;
606  }
607  }
608  return found;
609  };
610  switch(selector) {
611  case CacheSetHelper::ALL:
612  for (auto Ver = T.VersionList(); not Ver.end(); ++Ver)
613  {
614  insertVersion(T, Ver);
615  for (pkgCache::PrvIterator Prv = T.ProvidesList(); not Prv.end(); ++Prv)
616  if (not D.IsIgnorable(Prv))
617  {
618  vci->insert(Prv.OwnerVer());
619  found = true;
620  }
621  }
622  return found;
624  found = FromDependency(vci, Cache, D, CacheSetHelper::CANDIDATE, helper);
625  found &= FromDependency(vci, Cache, D, CacheSetHelper::INSTALLED, helper);
626  return found;
628  // skip looking if we have already cached that we will find nothing
629  if (((Cache[D] & pkgDepCache::DepCVer) == 0) != D.IsNegative())
630  return found;
631  return insertAllTargetVersions([&](pkgCache::PkgIterator const &OP) { return Cache[OP].CandidateVerIter(Cache); });
633  return insertAllTargetVersions([&](pkgCache::PkgIterator const &OP) { return OP.CurrentVer(); });
635  return FromDependency(vci, Cache, D, CacheSetHelper::CANDIDATE, helper) ||
636  FromDependency(vci, Cache, D, CacheSetHelper::INSTALLED, helper);
638  return FromDependency(vci, Cache, D, CacheSetHelper::INSTALLED, helper) ||
639  FromDependency(vci, Cache, D, CacheSetHelper::CANDIDATE, helper);
641  return insertAllTargetVersions([&](pkgCache::PkgIterator const &OP) { return OP.VersionList(); });
644  // both make no sense here, so always false
645  return false;
646  }
647  return found;
648 }
649  /*}}}*/
650 // getCandidateVer - Returns the candidate version of the given package /*{{{*/
652  pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper) {
654  if (Cache.IsDepCacheBuilt() == true) {
655  Cand = Cache[Pkg].CandidateVerIter(Cache);
656  } else if (unlikely(Cache.GetPolicy() == nullptr)) {
657  return pkgCache::VerIterator(Cache);
658  } else {
659  Cand = Cache.GetPolicy()->GetCandidateVer(Pkg);
660  }
661  if (Cand.end() == true)
662  return helper.canNotGetVersion(CacheSetHelper::CANDIDATE, Cache, Pkg);
663  return Cand;
664 }
665  /*}}}*/
666 // getInstalledVer - Returns the installed version of the given package /*{{{*/
668  pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper) {
669  if (Pkg->CurrentVer == 0)
670  return helper.canNotGetVersion(CacheSetHelper::INSTALLED, Cache, Pkg);
671  return Pkg.CurrentVer();
672 }
673  /*}}}*/
674 
675 // canNotFindPackage - with the given selector and pattern /*{{{*/
677  PackageContainerInterface * const pci, pkgCacheFile &Cache,
678  std::string const &pattern) {
679  switch (select) {
680  case REGEX: canNotFindRegEx(pci, Cache, pattern); break;
681  case TASK: canNotFindTask(pci, Cache, pattern); break;
682  case FNMATCH: canNotFindFnmatch(pci, Cache, pattern); break;
683  case PACKAGENAME: canNotFindPackage(pci, Cache, pattern); break;
684  case STRING: canNotFindPackage(pci, Cache, pattern); break;
685  case PATTERN: canNotFindPackage(pci, Cache, pattern); break;
686  case UNKNOWN: break;
687  }
688 }
689 // canNotFindTask - handle the case no package is found for a task /*{{{*/
690 void CacheSetHelper::canNotFindTask(PackageContainerInterface * const /*pci*/, pkgCacheFile &/*Cache*/, std::string pattern) {
691  if (ShowError == true)
692  _error->Insert(ErrorType, _("Couldn't find task '%s'"), pattern.c_str());
693 }
694  /*}}}*/
695 // canNotFindRegEx - handle the case no package is found by a regex /*{{{*/
696 void CacheSetHelper::canNotFindRegEx(PackageContainerInterface * const /*pci*/, pkgCacheFile &/*Cache*/, std::string pattern) {
697  if (ShowError == true)
698  _error->Insert(ErrorType, _("Couldn't find any package by regex '%s'"), pattern.c_str());
699 }
700  /*}}}*/
701 // canNotFindFnmatch - handle the case no package is found by a fnmatch /*{{{*/
702  void CacheSetHelper::canNotFindFnmatch(PackageContainerInterface * const /*pci*/, pkgCacheFile &/*Cache*/, std::string pattern) {
703  if (ShowError == true)
704  _error->Insert(ErrorType, _("Couldn't find any package by glob '%s'"), pattern.c_str());
705 }
706  /*}}}*/
707 // canNotFindPackage - handle the case no package is found from a string/*{{{*/
708 void CacheSetHelper::canNotFindPackage(PackageContainerInterface * const /*pci*/, pkgCacheFile &/*Cache*/, std::string const &/*str*/) {
709 }
710  /*}}}*/
711  /*}}}*/
712 // canNotFindPkgName - handle the case no package has this name /*{{{*/
714  std::string const &str) {
715  if (ShowError == true)
716  _error->Insert(ErrorType, _("Unable to locate package %s"), str.c_str());
717  return pkgCache::PkgIterator(Cache, 0);
718 }
719  /*}}}*/
720 // canNotFindVersion - for package by selector /*{{{*/
722 {
723  switch (select) {
724  case ALL: canNotFindAllVer(vci, Cache, Pkg); break;
725  case INSTCAND: canNotFindInstCandVer(vci, Cache, Pkg); break;
726  case CANDINST: canNotFindCandInstVer(vci, Cache, Pkg); break;
727  case NEWEST: canNotFindNewestVer(Cache, Pkg); break;
728  case CANDIDATE: canNotFindCandidateVer(Cache, Pkg); break;
729  case INSTALLED: canNotFindInstalledVer(Cache, Pkg); break;
730  case CANDANDINST: canNotGetCandInstVer(Cache, Pkg); break;
731  case RELEASE:
732  case VERSIONNUMBER:
733  // invalid in this branch
734  break;
735  }
736 }
737 // canNotFindAllVer /*{{{*/
739  pkgCache::PkgIterator const &Pkg) {
740  if (ShowError == true)
741  _error->Insert(ErrorType, _("Can't select versions from package '%s' as it is purely virtual"), Pkg.FullName(true).c_str());
742 }
743  /*}}}*/
744 // canNotFindInstCandVer /*{{{*/
746  pkgCache::PkgIterator const &Pkg) {
747  canNotGetInstCandVer(Cache, Pkg);
748 }
749  /*}}}*/
750 // canNotFindInstCandVer /*{{{*/
752  pkgCache::PkgIterator const &Pkg) {
753  canNotGetCandInstVer(Cache, Pkg);
754 }
755  /*}}}*/
756  /*}}}*/
757 // canNotGetVersion - for package by selector /*{{{*/
759  switch (select) {
760  case NEWEST: return canNotFindNewestVer(Cache, Pkg);
761  case CANDIDATE: return canNotFindCandidateVer(Cache, Pkg);
762  case INSTALLED: return canNotFindInstalledVer(Cache, Pkg);
763  case CANDINST: return canNotGetCandInstVer(Cache, Pkg);
764  case INSTCAND: return canNotGetInstCandVer(Cache, Pkg);
765  case ALL:
766  case CANDANDINST:
767  case RELEASE:
768  case VERSIONNUMBER:
769  // invalid in this branch
770  return pkgCache::VerIterator(Cache, 0);
771  }
772  return pkgCache::VerIterator(Cache, 0);
773 }
774 // canNotFindNewestVer /*{{{*/
776  pkgCache::PkgIterator const &Pkg) {
777  if (ShowError == true)
778  _error->Insert(ErrorType, _("Can't select newest version from package '%s' as it is purely virtual"), Pkg.FullName(true).c_str());
779  return pkgCache::VerIterator(Cache, 0);
780 }
781  /*}}}*/
782 // canNotFindCandidateVer /*{{{*/
784  pkgCache::PkgIterator const &Pkg) {
785  if (ShowError == true)
786  _error->Insert(ErrorType, _("Can't select candidate version from package %s as it has no candidate"), Pkg.FullName(true).c_str());
787  return pkgCache::VerIterator(Cache, 0);
788 }
789  /*}}}*/
790 // canNotFindInstalledVer /*{{{*/
792  pkgCache::PkgIterator const &Pkg) {
793  if (ShowError == true)
794  _error->Insert(ErrorType, _("Can't select installed version from package %s as it is not installed"), Pkg.FullName(true).c_str());
795  return pkgCache::VerIterator(Cache, 0);
796 }
797  /*}}}*/
798 // canNotFindInstCandVer /*{{{*/
800  pkgCache::PkgIterator const &Pkg) {
801  if (ShowError == true)
802  _error->Insert(ErrorType, _("Can't select installed nor candidate version from package '%s' as it has neither of them"), Pkg.FullName(true).c_str());
803  return pkgCache::VerIterator(Cache, 0);
804 }
805  /*}}}*/
806 // canNotFindInstCandVer /*{{{*/
808  pkgCache::PkgIterator const &Pkg) {
809  if (ShowError == true)
810  _error->Insert(ErrorType, _("Can't select installed nor candidate version from package '%s' as it has neither of them"), Pkg.FullName(true).c_str());
811  return pkgCache::VerIterator(Cache, 0);
812 }
813  /*}}}*/
814  /*}}}*/
815 // showPackageSelection - by selector and given pattern /*{{{*/
817  std::string const &pattern) {
818  switch (select) {
819  case REGEX: showRegExSelection(pkg, pattern); break;
820  case TASK: showTaskSelection(pkg, pattern); break;
821  case FNMATCH: showFnmatchSelection(pkg, pattern); break;
822  case PATTERN: showPatternSelection(pkg, pattern); break;
823  case PACKAGENAME: /* no surprises here */ break;
824  case STRING: /* handled by the special cases */ break;
825  case UNKNOWN: break;
826  }
827 }
828 // showTaskSelection /*{{{*/
830  std::string const &/*pattern*/) {
831 }
832  /*}}}*/
833 // showRegExSelection /*{{{*/
835  std::string const &/*pattern*/) {
836 }
837  /*}}}*/
838 // showFnmatchSelection /*{{{*/
840  std::string const &/*pattern*/) {
841 }
842  /*}}}*/
843 // showPatternSelection /*{{{*/
845  std::string const & /*pattern*/)
846 {
847 }
848  /*}}}*/
849  /*}}}*/
850 // showVersionSelection /*{{{*/
852  pkgCache::VerIterator const &Ver, enum VerSelector const select, std::string const &pattern) {
853  switch (select) {
854  case RELEASE:
855  showSelectedVersion(Pkg, Ver, pattern, true);
856  break;
857  case VERSIONNUMBER:
858  showSelectedVersion(Pkg, Ver, pattern, false);
859  break;
860  case NEWEST:
861  case CANDIDATE:
862  case INSTALLED:
863  case CANDINST:
864  case INSTCAND:
865  case ALL:
866  case CANDANDINST:
867  // not really surprises, but in fact: just not implemented
868  break;
869  }
870 }
872  pkgCache::VerIterator const /*Ver*/,
873  std::string const &/*ver*/,
874  bool const /*verIsRel*/) {
875 }
876  /*}}}*/
877 
878 CacheSetHelper::CacheSetHelper(bool const ShowError, GlobalError::MsgType ErrorType) :
879  ShowError(ShowError), ErrorType(ErrorType), d(NULL) {}
881 
886  if (this != &other)
887  this->ConstructedBy = other.ConstructedBy;
888  return *this;
889 }
891 
892 PackageUniverse::PackageUniverse(pkgCache * const Owner) : _cont(Owner), d(NULL) {}
893 PackageUniverse::PackageUniverse(pkgCacheFile * const Owner) : _cont(Owner->GetPkgCache()), d(NULL) {}
895 
898  *this = other;
899 };
901  return *this;
902 }
903 
905 }
static bool std::string const metaIndex const *const pkgAcqMetaClearSig *const pkgAcquire::Item *const I
bool PackageFromPackageName(PackageContainerInterface *const pci, pkgCacheFile &Cache, std::string pattern)
Definition: cacheset.cc:249
virtual bool PackageFromCommandLine(PackageContainerInterface *const pci, pkgCacheFile &Cache, const char **cmdline)
Definition: cacheset.cc:342
pkgCache::VerIterator canNotGetInstCandVer(pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg)
Definition: cacheset.cc:799
bool PackageFromTask(PackageContainerInterface *const pci, pkgCacheFile &Cache, std::string pattern)
Definition: cacheset.cc:55
bool PackageFromRegEx(PackageContainerInterface *const pci, pkgCacheFile &Cache, std::string pattern)
Definition: cacheset.cc:126
void canNotFindRegEx(PackageContainerInterface *const pci, pkgCacheFile &Cache, std::string pattern)
Definition: cacheset.cc:696
VerSelector
specifies which version(s) we want to refer to
Definition: cacheset.h:95
void canNotFindAllVer(VersionContainerInterface *const vci, pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg)
Definition: cacheset.cc:738
virtual void canNotFindPackage(enum PkgSelector const select, PackageContainerInterface *const pci, pkgCacheFile &Cache, std::string const &pattern)
be notified if a package can't be found via pattern
Definition: cacheset.cc:676
void showRegExSelection(pkgCache::PkgIterator const &pkg, std::string const &pattern)
Definition: cacheset.cc:834
pkgCache::VerIterator canNotFindInstalledVer(pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg)
Definition: cacheset.cc:791
virtual ~CacheSetHelper()
Definition: cacheset.cc:880
void canNotFindInstCandVer(VersionContainerInterface *const vci, pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg)
Definition: cacheset.cc:745
void showFnmatchSelection(pkgCache::PkgIterator const &pkg, std::string const &pattern)
Definition: cacheset.cc:839
virtual void showVersionSelection(pkgCache::PkgIterator const &Pkg, pkgCache::VerIterator const &Ver, enum VerSelector const select, std::string const &pattern)
be notified about the version being selected via pattern
Definition: cacheset.cc:851
virtual bool PackageFrom(enum PkgSelector const select, PackageContainerInterface *const pci, pkgCacheFile &Cache, std::string const &pattern)
Definition: cacheset.cc:40
void showTaskSelection(pkgCache::PkgIterator const &pkg, std::string const &pattern)
Definition: cacheset.cc:829
GlobalError::MsgType ErrorType
Definition: cacheset.h:163
void canNotFindFnmatch(PackageContainerInterface *const pci, pkgCacheFile &Cache, std::string pattern)
Definition: cacheset.cc:702
virtual void showPackageSelection(pkgCache::PkgIterator const &pkg, PkgSelector const select, std::string const &pattern)
be notified about the package being selected via pattern
Definition: cacheset.cc:816
void canNotFindCandInstVer(VersionContainerInterface *const vci, pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg)
Definition: cacheset.cc:751
bool showErrors() const
Definition: cacheset.h:147
bool PackageFromFnmatch(PackageContainerInterface *const pci, pkgCacheFile &Cache, std::string pattern)
Definition: cacheset.cc:188
pkgCache::VerIterator canNotFindNewestVer(pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg)
Definition: cacheset.cc:775
bool PackageFromPattern(PackageContainerInterface *const pci, pkgCacheFile &Cache, std::string const &pattern)
Definition: cacheset.cc:299
virtual void canNotFindVersion(enum VerSelector const select, VersionContainerInterface *const vci, pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg)
be notified if a version can't be found for a package
Definition: cacheset.cc:721
pkgCache::VerIterator canNotFindCandidateVer(pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg)
Definition: cacheset.cc:783
virtual bool PackageFromModifierCommandLine(unsigned short &modID, PackageContainerInterface *const pci, pkgCacheFile &Cache, const char *cmdline, std::list< PkgModifier > const &mods)
Definition: cacheset.cc:350
virtual pkgCache::PkgIterator canNotFindPkgName(pkgCacheFile &Cache, std::string const &str)
Definition: cacheset.cc:713
virtual pkgCache::VerIterator canNotGetVersion(enum VerSelector const select, pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg)
Definition: cacheset.cc:758
void canNotFindTask(PackageContainerInterface *const pci, pkgCacheFile &Cache, std::string pattern)
Definition: cacheset.cc:690
void showPatternSelection(pkgCache::PkgIterator const &pkg, std::string const &pattern)
Definition: cacheset.cc:844
CacheSetHelper(bool const ShowError=true, GlobalError::MsgType ErrorType=GlobalError::ERROR)
Definition: cacheset.cc:878
pkgCache::VerIterator canNotGetCandInstVer(pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg)
Definition: cacheset.cc:807
bool PackageFromString(PackageContainerInterface *const pci, pkgCacheFile &Cache, std::string const &pattern)
Definition: cacheset.cc:319
void showSelectedVersion(pkgCache::PkgIterator const &Pkg, pkgCache::VerIterator const Ver, std::string const &ver, bool const verIsRel)
Definition: cacheset.cc:871
virtual bool insert(pkgCache::PkgIterator const &P)=0
void setConstructor(CacheSetHelper::PkgSelector const by)
Definition: cacheset.h:322
PackageContainerInterface & operator=(PackageContainerInterface const &other)
Definition: cacheset.cc:885
CacheSetHelper::PkgSelector ConstructedBy
Definition: cacheset.h:331
CacheSetHelper::PkgSelector getConstructor() const
Definition: cacheset.h:323
virtual bool empty() const =0
const_iterator begin() const
Definition: cacheset.h:371
const_iterator end() const
Definition: cacheset.h:372
PackageUniverse(pkgCache *const Owner)
Definition: cacheset.cc:892
virtual ~PackageUniverse()
Definition: cacheset.cc:894
static bool FromPackage(VersionContainerInterface *const vci, pkgCacheFile &Cache, pkgCache::PkgIterator const &P, CacheSetHelper::VerSelector const fallback, CacheSetHelper &helper)
Definition: cacheset.cc:516
static bool FromString(VersionContainerInterface *const vci, pkgCacheFile &Cache, std::string pkg, CacheSetHelper::VerSelector const fallback, CacheSetHelper &helper, bool const onlyFromName=false)
Definition: cacheset.cc:445
VersionContainerInterface & operator=(VersionContainerInterface const &other)
Definition: cacheset.cc:900
virtual bool insert(pkgCache::VerIterator const &V)=0
static bool FromDependency(VersionContainerInterface *const vci, pkgCacheFile &Cache, pkgCache::DepIterator const &D, CacheSetHelper::VerSelector const selector, CacheSetHelper &helper)
Definition: cacheset.cc:579
static pkgCache::VerIterator getInstalledVer(pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper)
returns the installed version of the package
Definition: cacheset.cc:667
static pkgCache::VerIterator getCandidateVer(pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper)
returns the candidate version of the package
Definition: cacheset.cc:651
static bool FromCommandLine(VersionContainerInterface *const vci, pkgCacheFile &Cache, const char **cmdline, CacheSetHelper::VerSelector const fallback, CacheSetHelper &helper)
Definition: cacheset.cc:434
static bool FromModifierCommandLine(unsigned short &modID, VersionContainerInterface *const vci, pkgCacheFile &Cache, const char *cmdline, std::list< Modifier > const &mods, CacheSetHelper &helper)
Definition: cacheset.cc:388
std::string Find(const char *Name, const char *Default=0) const
bool FindB(const char *Name, bool const &Default=false) const
MsgType
a message can have one of following severity
Definition: error.h:55
pkgPolicy * GetPolicy()
Definition: cachefile.h:75
pkgDepCache * GetDepCache()
Definition: cachefile.h:74
bool IsDepCacheBuilt() const
Definition: cachefile.h:79
pkgCache * GetPkgCache()
Definition: cachefile.h:73
PkgIterator PkgBegin()
Definition: depcache.h:356
GrpIterator GrpBegin()
Definition: depcache.h:355
virtual pkgCache::VerIterator GetCandidateVer(pkgCache::PkgIterator const &Pkg) APT_OVERRIDE
Definition: policy.cc:140
Parser & Lookup(pkgCache::VerFileIterator const &Ver)
Definition: pkgrecords.cc:62
pkgCache::VerIterator Find(pkgCache::PkgIterator Pkg)
Configuration * _config
APT_PUBLIC std::unique_ptr< APT::CacheFilter::Matcher > ParsePattern(APT::StringView pattern, pkgCacheFile *file)
Parse a pattern, return nullptr or pattern.
pkgCache - Structure definitions for the cache file