cachefilter.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. /** \file cachefilter.h
  4. Collection of functor classes */
  5. /*}}}*/
  6. // Include Files /*{{{*/
  7. #include <config.h>
  8. #include <apt-pkg/cachefilter.h>
  9. #include <apt-pkg/error.h>
  10. #include <apt-pkg/pkgcache.h>
  11. #include <apt-pkg/cacheiterators.h>
  12. #include <apt-pkg/strutl.h>
  13. #include <apt-pkg/macros.h>
  14. #include <string>
  15. #include <string.h>
  16. #include <regex.h>
  17. #include <fnmatch.h>
  18. #include <apti18n.h>
  19. /*}}}*/
  20. namespace APT {
  21. namespace CacheFilter {
  22. PackageNameMatchesRegEx::PackageNameMatchesRegEx(std::string const &Pattern) : d(NULL) {/*{{{*/
  23. pattern = new regex_t;
  24. int const Res = regcomp(pattern, Pattern.c_str(), REG_EXTENDED | REG_ICASE | REG_NOSUB);
  25. if (Res == 0)
  26. return;
  27. delete pattern;
  28. pattern = NULL;
  29. char Error[300];
  30. regerror(Res, pattern, Error, sizeof(Error));
  31. _error->Error(_("Regex compilation error - %s"), Error);
  32. }
  33. /*}}}*/
  34. bool PackageNameMatchesRegEx::operator() (pkgCache::PkgIterator const &Pkg) {/*{{{*/
  35. if (unlikely(pattern == NULL))
  36. return false;
  37. else
  38. return regexec(pattern, Pkg.Name(), 0, 0, 0) == 0;
  39. }
  40. /*}}}*/
  41. bool PackageNameMatchesRegEx::operator() (pkgCache::GrpIterator const &Grp) {/*{{{*/
  42. if (unlikely(pattern == NULL))
  43. return false;
  44. else
  45. return regexec(pattern, Grp.Name(), 0, 0, 0) == 0;
  46. }
  47. /*}}}*/
  48. PackageNameMatchesRegEx::~PackageNameMatchesRegEx() { /*{{{*/
  49. if (pattern == NULL)
  50. return;
  51. regfree(pattern);
  52. delete pattern;
  53. }
  54. /*}}}*/
  55. // Fnmatch support /*{{{*/
  56. //----------------------------------------------------------------------
  57. bool PackageNameMatchesFnmatch::operator() (pkgCache::PkgIterator const &Pkg) {/*{{{*/
  58. return fnmatch(Pattern.c_str(), Pkg.Name(), FNM_CASEFOLD) == 0;
  59. }
  60. /*}}}*/
  61. bool PackageNameMatchesFnmatch::operator() (pkgCache::GrpIterator const &Grp) {/*{{{*/
  62. return fnmatch(Pattern.c_str(), Grp.Name(), FNM_CASEFOLD) == 0;
  63. }
  64. /*}}}*/
  65. // CompleteArch to <kernel>-<cpu> tuple /*{{{*/
  66. //----------------------------------------------------------------------
  67. /* The complete architecture, consisting of <kernel>-<cpu>. */
  68. static std::string CompleteArch(std::string const &arch) {
  69. if (arch.find('-') != std::string::npos) {
  70. // ensure that only -any- is replaced and not something like company-
  71. std::string complete = std::string("-").append(arch).append("-");
  72. complete = SubstVar(complete, "-any-", "-*-");
  73. complete = complete.substr(1, complete.size()-2);
  74. return complete;
  75. }
  76. else if (arch == "any") return "*-*";
  77. else return "linux-" + arch;
  78. }
  79. /*}}}*/
  80. PackageArchitectureMatchesSpecification::PackageArchitectureMatchesSpecification(std::string const &pattern, bool const isPattern) :/*{{{*/
  81. literal(pattern), complete(CompleteArch(pattern)), isPattern(isPattern), d(NULL) {
  82. }
  83. /*}}}*/
  84. bool PackageArchitectureMatchesSpecification::operator() (char const * const &arch) {/*{{{*/
  85. if (strcmp(literal.c_str(), arch) == 0 ||
  86. strcmp(complete.c_str(), arch) == 0)
  87. return true;
  88. std::string const pkgarch = CompleteArch(arch);
  89. if (isPattern == true)
  90. return fnmatch(complete.c_str(), pkgarch.c_str(), 0) == 0;
  91. return fnmatch(pkgarch.c_str(), complete.c_str(), 0) == 0;
  92. }
  93. /*}}}*/
  94. bool PackageArchitectureMatchesSpecification::operator() (pkgCache::PkgIterator const &Pkg) {/*{{{*/
  95. return (*this)(Pkg.Arch());
  96. }
  97. /*}}}*/
  98. bool PackageArchitectureMatchesSpecification::operator() (pkgCache::VerIterator const &Ver) {/*{{{*/
  99. return (*this)(Ver.ParentPkg());
  100. }
  101. /*}}}*/
  102. PackageArchitectureMatchesSpecification::~PackageArchitectureMatchesSpecification() { /*{{{*/
  103. }
  104. /*}}}*/
  105. }
  106. }