]> git.saurik.com Git - apt.git/blame - apt-pkg/cachefilter.cc
avoid 416 response teardown binding to null pointer
[apt.git] / apt-pkg / cachefilter.cc
CommitLineData
9ba5aa3b
DK
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
3/** \file cachefilter.h
4 Collection of functor classes */
5 /*}}}*/
6// Include Files /*{{{*/
ea542140
DK
7#include <config.h>
8
3721cb01 9#include <apt-pkg/cachefile.h>
9ba5aa3b
DK
10#include <apt-pkg/cachefilter.h>
11#include <apt-pkg/error.h>
12#include <apt-pkg/pkgcache.h>
453b82a3 13#include <apt-pkg/cacheiterators.h>
424ff669 14#include <apt-pkg/strutl.h>
453b82a3 15#include <apt-pkg/macros.h>
9ba5aa3b 16
9ba5aa3b 17#include <string>
453b82a3 18#include <string.h>
9ba5aa3b 19#include <regex.h>
424ff669 20#include <fnmatch.h>
a00a9b44
DK
21
22#include <apti18n.h>
9ba5aa3b
DK
23 /*}}}*/
24namespace APT {
25namespace CacheFilter {
3721cb01
DK
26APT_CONST Matcher::~Matcher() {}
27APT_CONST PackageMatcher::~PackageMatcher() {}
28
29// Name matches RegEx /*{{{*/
30PackageNameMatchesRegEx::PackageNameMatchesRegEx(std::string const &Pattern) {
9ba5aa3b
DK
31 pattern = new regex_t;
32 int const Res = regcomp(pattern, Pattern.c_str(), REG_EXTENDED | REG_ICASE | REG_NOSUB);
33 if (Res == 0)
34 return;
35
36 delete pattern;
37 pattern = NULL;
38 char Error[300];
39 regerror(Res, pattern, Error, sizeof(Error));
40 _error->Error(_("Regex compilation error - %s"), Error);
41}
3721cb01 42bool PackageNameMatchesRegEx::operator() (pkgCache::PkgIterator const &Pkg) {
9ba5aa3b
DK
43 if (unlikely(pattern == NULL))
44 return false;
45 else
46 return regexec(pattern, Pkg.Name(), 0, 0, 0) == 0;
47}
3721cb01 48bool PackageNameMatchesRegEx::operator() (pkgCache::GrpIterator const &Grp) {
9ba5aa3b
DK
49 if (unlikely(pattern == NULL))
50 return false;
51 else
52 return regexec(pattern, Grp.Name(), 0, 0, 0) == 0;
53}
3721cb01 54PackageNameMatchesRegEx::~PackageNameMatchesRegEx() {
9ba5aa3b
DK
55 if (pattern == NULL)
56 return;
57 regfree(pattern);
58 delete pattern;
59}
60 /*}}}*/
3721cb01
DK
61// Name matches Fnmatch /*{{{*/
62PackageNameMatchesFnmatch::PackageNameMatchesFnmatch(std::string const &Pattern) :
63 Pattern(Pattern) {}
64bool PackageNameMatchesFnmatch::operator() (pkgCache::PkgIterator const &Pkg) {
b9179170
MV
65 return fnmatch(Pattern.c_str(), Pkg.Name(), FNM_CASEFOLD) == 0;
66}
3721cb01 67bool PackageNameMatchesFnmatch::operator() (pkgCache::GrpIterator const &Grp) {
b9179170
MV
68 return fnmatch(Pattern.c_str(), Grp.Name(), FNM_CASEFOLD) == 0;
69}
70 /*}}}*/
5025879e 71// Architecture matches <libc>-<kernel>-<cpu> specification /*{{{*/
424ff669 72//----------------------------------------------------------------------
5025879e
DK
73/* The complete architecture, consisting of <libc>-<kernel>-<cpu>. */
74static std::string CompleteArch(std::string const &arch, bool const isPattern) {
75 auto const found = arch.find('-');
76 if (found != std::string::npos)
77 {
78 // ensure that only -any- is replaced and not something like company-
79 std::string complete = std::string("-").append(arch).append("-");
80 size_t pos = 0;
81 char const * const search = "-any-";
82 auto const search_len = strlen(search) - 2;
83 while((pos = complete.find(search, pos)) != std::string::npos) {
84 complete.replace(pos + 1, search_len, "*");
85 pos += 2;
86 }
87 complete = complete.substr(1, complete.size()-2);
88 if (arch.find('-', found+1) != std::string::npos)
89 // <libc>-<kernel>-<cpu> format
90 return complete;
91 // <kernel>-<cpu> format
92 else if (isPattern)
93 return "*-" + complete;
94 else
95 return "gnu-" + complete;
96 }
97 else if (arch == "any")
98 return "*-*-*";
99 else if (isPattern)
100 return "*-linux-" + arch;
101 else
102 return "gnu-linux-" + arch;
424ff669 103}
5025879e
DK
104PackageArchitectureMatchesSpecification::PackageArchitectureMatchesSpecification(std::string const &pattern, bool const pisPattern) :
105 literal(pattern), complete(CompleteArch(pattern, pisPattern)), isPattern(pisPattern) {
424ff669 106}
3721cb01 107bool PackageArchitectureMatchesSpecification::operator() (char const * const &arch) {
424ff669
DK
108 if (strcmp(literal.c_str(), arch) == 0 ||
109 strcmp(complete.c_str(), arch) == 0)
110 return true;
5025879e 111 std::string const pkgarch = CompleteArch(arch, !isPattern);
424ff669
DK
112 if (isPattern == true)
113 return fnmatch(complete.c_str(), pkgarch.c_str(), 0) == 0;
114 return fnmatch(pkgarch.c_str(), complete.c_str(), 0) == 0;
115}
3721cb01 116bool PackageArchitectureMatchesSpecification::operator() (pkgCache::PkgIterator const &Pkg) {
424ff669 117 return (*this)(Pkg.Arch());
3721cb01
DK
118}
119PackageArchitectureMatchesSpecification::~PackageArchitectureMatchesSpecification() {
424ff669
DK
120}
121 /*}}}*/
3721cb01
DK
122// Package is new install /*{{{*/
123PackageIsNewInstall::PackageIsNewInstall(pkgCacheFile * const Cache) : Cache(Cache) {}
124APT_PURE bool PackageIsNewInstall::operator() (pkgCache::PkgIterator const &Pkg) {
125 return (*Cache)[Pkg].NewInstall();
424ff669 126}
3721cb01 127PackageIsNewInstall::~PackageIsNewInstall() {}
424ff669 128 /*}}}*/
3721cb01
DK
129// Generica like True, False, NOT, AND, OR /*{{{*/
130APT_CONST bool TrueMatcher::operator() (pkgCache::PkgIterator const &) { return true; }
131APT_CONST bool TrueMatcher::operator() (pkgCache::GrpIterator const &) { return true; }
132APT_CONST bool TrueMatcher::operator() (pkgCache::VerIterator const &) { return true; }
133
134APT_CONST bool FalseMatcher::operator() (pkgCache::PkgIterator const &) { return false; }
135APT_CONST bool FalseMatcher::operator() (pkgCache::GrpIterator const &) { return false; }
136APT_CONST bool FalseMatcher::operator() (pkgCache::VerIterator const &) { return false; }
137
138NOTMatcher::NOTMatcher(Matcher * const matcher) : matcher(matcher) {}
139bool NOTMatcher::operator() (pkgCache::PkgIterator const &Pkg) { return ! (*matcher)(Pkg); }
140bool NOTMatcher::operator() (pkgCache::GrpIterator const &Grp) { return ! (*matcher)(Grp); }
141bool NOTMatcher::operator() (pkgCache::VerIterator const &Ver) { return ! (*matcher)(Ver); }
142NOTMatcher::~NOTMatcher() { delete matcher; }
143
144ANDMatcher::ANDMatcher() {}
145ANDMatcher::ANDMatcher(Matcher * const matcher1) {
146 AND(matcher1);
147}
148ANDMatcher::ANDMatcher(Matcher * const matcher1, Matcher * const matcher2) {
149 AND(matcher1).AND(matcher2);
150}
151ANDMatcher::ANDMatcher(Matcher * const matcher1, Matcher * const matcher2, Matcher * const matcher3) {
152 AND(matcher1).AND(matcher2).AND(matcher3);
153}
154ANDMatcher::ANDMatcher(Matcher * const matcher1, Matcher * const matcher2, Matcher * const matcher3, Matcher * const matcher4) {
155 AND(matcher1).AND(matcher2).AND(matcher3).AND(matcher4);
156}
157ANDMatcher::ANDMatcher(Matcher * const matcher1, Matcher * const matcher2, Matcher * const matcher3, Matcher * const matcher4, Matcher * const matcher5) {
158 AND(matcher1).AND(matcher2).AND(matcher3).AND(matcher4).AND(matcher5);
159}
160ANDMatcher& ANDMatcher::AND(Matcher * const matcher) { matchers.push_back(matcher); return *this; }
161bool ANDMatcher::operator() (pkgCache::PkgIterator const &Pkg) {
162 for (std::vector<Matcher *>::const_iterator M = matchers.begin(); M != matchers.end(); ++M)
163 if ((**M)(Pkg) == false)
164 return false;
165 return true;
166}
167bool ANDMatcher::operator() (pkgCache::GrpIterator const &Grp) {
168 for (std::vector<Matcher *>::const_iterator M = matchers.begin(); M != matchers.end(); ++M)
169 if ((**M)(Grp) == false)
170 return false;
171 return true;
172}
173bool ANDMatcher::operator() (pkgCache::VerIterator const &Ver) {
174 for (std::vector<Matcher *>::const_iterator M = matchers.begin(); M != matchers.end(); ++M)
175 if ((**M)(Ver) == false)
176 return false;
177 return true;
178}
179ANDMatcher::~ANDMatcher() {
180 for (std::vector<Matcher *>::iterator M = matchers.begin(); M != matchers.end(); ++M)
181 delete *M;
182}
183
184ORMatcher::ORMatcher() {}
185ORMatcher::ORMatcher(Matcher * const matcher1) {
186 OR(matcher1);
187}
188ORMatcher::ORMatcher(Matcher * const matcher1, Matcher * const matcher2) {
189 OR(matcher1).OR(matcher2);
190}
191ORMatcher::ORMatcher(Matcher * const matcher1, Matcher * const matcher2, Matcher * const matcher3) {
192 OR(matcher1).OR(matcher2).OR(matcher3);
193}
194ORMatcher::ORMatcher(Matcher * const matcher1, Matcher * const matcher2, Matcher * const matcher3, Matcher * const matcher4) {
195 OR(matcher1).OR(matcher2).OR(matcher3).OR(matcher4);
196}
197ORMatcher::ORMatcher(Matcher * const matcher1, Matcher * const matcher2, Matcher * const matcher3, Matcher * const matcher4, Matcher * const matcher5) {
198 OR(matcher1).OR(matcher2).OR(matcher3).OR(matcher4).OR(matcher5);
199}
200ORMatcher& ORMatcher::OR(Matcher * const matcher) { matchers.push_back(matcher); return *this; }
201bool ORMatcher::operator() (pkgCache::PkgIterator const &Pkg) {
202 for (std::vector<Matcher *>::const_iterator M = matchers.begin(); M != matchers.end(); ++M)
203 if ((**M)(Pkg) == true)
204 return true;
205 return false;
206}
207bool ORMatcher::operator() (pkgCache::GrpIterator const &Grp) {
208 for (std::vector<Matcher *>::const_iterator M = matchers.begin(); M != matchers.end(); ++M)
209 if ((**M)(Grp) == true)
210 return true;
211 return false;
212}
213bool ORMatcher::operator() (pkgCache::VerIterator const &Ver) {
214 for (std::vector<Matcher *>::const_iterator M = matchers.begin(); M != matchers.end(); ++M)
215 if ((**M)(Ver) == true)
216 return true;
217 return false;
218}
219ORMatcher::~ORMatcher() {
220 for (std::vector<Matcher *>::iterator M = matchers.begin(); M != matchers.end(); ++M)
221 delete *M;
424ff669
DK
222}
223 /*}}}*/
224
9ba5aa3b
DK
225}
226}