Directory: | cvmfs/ |
---|---|
File: | cvmfs/pathspec/pathspec_pattern.cc |
Date: | 2025-08-31 02:39:21 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 136 | 138 | 98.6% |
Branches: | 134 | 182 | 73.6% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /** | ||
2 | * This file is part of the CernVM File System. | ||
3 | */ | ||
4 | |||
5 | |||
6 | #include "pathspec_pattern.h" | ||
7 | |||
8 | #include <cassert> | ||
9 | |||
10 | #include "pathspec/pathspec.h" | ||
11 | |||
12 | 1856 | PathspecElementPattern::PathspecElementPattern( | |
13 | const std::string::const_iterator begin, | ||
14 | 1856 | const std::string::const_iterator &end) | |
15 | 1856 | : valid_(true) { | |
16 |
1/2✓ Branch 1 taken 1856 times.
✗ Branch 2 not taken.
|
1856 | Parse(begin, end); |
17 | 1856 | } | |
18 | |||
19 | 6262 | PathspecElementPattern::PathspecElementPattern( | |
20 | 6262 | const PathspecElementPattern &other) | |
21 | 6262 | : valid_(other.valid_) { | |
22 |
1/2✓ Branch 2 taken 6262 times.
✗ Branch 3 not taken.
|
6262 | subpatterns_.reserve(other.subpatterns_.size()); |
23 | 6262 | SubPatterns::const_iterator i = other.subpatterns_.begin(); | |
24 | 6262 | const SubPatterns::const_iterator iend = other.subpatterns_.end(); | |
25 |
2/2✓ Branch 1 taken 6779 times.
✓ Branch 2 taken 6262 times.
|
13041 | for (; i != iend; ++i) { |
26 |
2/4✓ Branch 2 taken 6779 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 6779 times.
✗ Branch 6 not taken.
|
6779 | subpatterns_.push_back((*i)->Clone()); |
27 | } | ||
28 | 6262 | } | |
29 | |||
30 | 28 | PathspecElementPattern &PathspecElementPattern::operator=( | |
31 | const PathspecElementPattern &other) { | ||
32 |
1/2✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
|
28 | if (this != &other) { |
33 | 28 | valid_ = other.valid_; | |
34 | 28 | subpatterns_.clear(); | |
35 |
1/2✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
|
28 | subpatterns_.reserve(other.subpatterns_.size()); |
36 | 28 | SubPatterns::const_iterator i = other.subpatterns_.begin(); | |
37 | 28 | const SubPatterns::const_iterator iend = other.subpatterns_.end(); | |
38 |
2/2✓ Branch 1 taken 44 times.
✓ Branch 2 taken 28 times.
|
72 | for (; i != iend; ++i) { |
39 |
2/4✓ Branch 2 taken 44 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 44 times.
✗ Branch 6 not taken.
|
44 | subpatterns_.push_back((*i)->Clone()); |
40 | } | ||
41 | } | ||
42 | |||
43 | 28 | return *this; | |
44 | } | ||
45 | |||
46 | |||
47 | 8112 | PathspecElementPattern::~PathspecElementPattern() { | |
48 | 8112 | SubPatterns::const_iterator i = subpatterns_.begin(); | |
49 | 8112 | const SubPatterns::const_iterator iend = subpatterns_.end(); | |
50 |
2/2✓ Branch 2 taken 8912 times.
✓ Branch 3 taken 8112 times.
|
17024 | for (; i != iend; ++i) { |
51 |
1/2✓ Branch 1 taken 8912 times.
✗ Branch 2 not taken.
|
8912 | delete *i; |
52 | } | ||
53 | 8112 | subpatterns_.clear(); | |
54 | 8112 | } | |
55 | |||
56 | |||
57 | 1856 | void PathspecElementPattern::Parse(const std::string::const_iterator &begin, | |
58 | const std::string::const_iterator &end) { | ||
59 | 1856 | std::string::const_iterator i = begin; | |
60 |
2/2✓ Branch 1 taken 2127 times.
✓ Branch 2 taken 1856 times.
|
3983 | while (i != end) { |
61 |
3/4✓ Branch 2 taken 404 times.
✓ Branch 3 taken 1723 times.
✓ Branch 5 taken 404 times.
✗ Branch 6 not taken.
|
2127 | SubPattern *next = (Pathspec::IsSpecialChar(*i)) ? ParseSpecialChar(end, &i) |
62 |
1/2✓ Branch 1 taken 1723 times.
✗ Branch 2 not taken.
|
1723 | : ParsePlaintext(end, &i); |
63 |
3/4✓ Branch 1 taken 2127 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 2123 times.
|
2127 | if (next->IsEmpty()) { |
64 | 4 | valid_ = false; | |
65 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | delete next; |
66 | } else { | ||
67 |
1/2✓ Branch 1 taken 2123 times.
✗ Branch 2 not taken.
|
2123 | subpatterns_.push_back(next); |
68 | } | ||
69 | } | ||
70 | 1856 | } | |
71 | |||
72 | |||
73 | 1723 | PathspecElementPattern::SubPattern *PathspecElementPattern::ParsePlaintext( | |
74 | const std::string::const_iterator &end, std::string::const_iterator *i) { | ||
75 | 1723 | PlaintextSubPattern *pattern = new PlaintextSubPattern(); | |
76 | 1723 | bool next_is_escaped = false; | |
77 | |||
78 |
2/2✓ Branch 1 taken 7563 times.
✓ Branch 2 taken 1624 times.
|
9187 | while (*i < end) { |
79 |
6/6✓ Branch 2 taken 138 times.
✓ Branch 3 taken 7425 times.
✓ Branch 4 taken 99 times.
✓ Branch 5 taken 39 times.
✓ Branch 6 taken 99 times.
✓ Branch 7 taken 7464 times.
|
7563 | if (Pathspec::IsSpecialChar(**i) && !next_is_escaped) { |
80 | 99 | break; | |
81 | } | ||
82 | |||
83 |
6/6✓ Branch 1 taken 70 times.
✓ Branch 2 taken 7394 times.
✓ Branch 3 taken 58 times.
✓ Branch 4 taken 12 times.
✓ Branch 5 taken 58 times.
✓ Branch 6 taken 7406 times.
|
7464 | if (**i == Pathspec::kEscaper && !next_is_escaped) { |
84 | 58 | next_is_escaped = true; | |
85 |
2/2✓ Branch 0 taken 78 times.
✓ Branch 1 taken 7328 times.
|
7406 | } else if (next_is_escaped) { |
86 |
6/6✓ Branch 2 taken 39 times.
✓ Branch 3 taken 39 times.
✓ Branch 5 taken 12 times.
✓ Branch 6 taken 27 times.
✓ Branch 7 taken 51 times.
✓ Branch 8 taken 27 times.
|
78 | if (Pathspec::IsSpecialChar(**i) || **i == Pathspec::kEscaper) { |
87 | 51 | pattern->AddChar(**i); | |
88 | 51 | next_is_escaped = false; | |
89 | } else { | ||
90 | 27 | valid_ = false; | |
91 | } | ||
92 | } else { | ||
93 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 7328 times.
|
7328 | assert(!Pathspec::IsSpecialChar(**i)); |
94 | 7328 | pattern->AddChar(**i); | |
95 | } | ||
96 | |||
97 | 7464 | ++(*i); | |
98 | } | ||
99 | |||
100 | 1723 | return pattern; | |
101 | } | ||
102 | |||
103 | 404 | PathspecElementPattern::SubPattern *PathspecElementPattern::ParseSpecialChar( | |
104 | const std::string::const_iterator &end, std::string::const_iterator *i) { | ||
105 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 404 times.
|
404 | assert(Pathspec::IsSpecialChar(**i)); |
106 | 404 | const char chr = **i; | |
107 | 404 | ++(*i); | |
108 | |||
109 |
2/3✓ Branch 0 taken 259 times.
✓ Branch 1 taken 145 times.
✗ Branch 2 not taken.
|
404 | switch (chr) { |
110 | 259 | case Pathspec::kWildcard: | |
111 | 259 | return new WildcardSubPattern(); | |
112 | 145 | case Pathspec::kPlaceholder: | |
113 | 145 | return new PlaceholderSubPattern(); | |
114 | ✗ | default: | |
115 | ✗ | assert(false && "unrecognized special character"); | |
116 | } | ||
117 | } | ||
118 | |||
119 | 1318 | std::string PathspecElementPattern::GenerateRegularExpression( | |
120 | const bool is_relaxed) const { | ||
121 | 1318 | std::string result; | |
122 | 1318 | SubPatterns::const_iterator i = subpatterns_.begin(); | |
123 | 1318 | const SubPatterns::const_iterator iend = subpatterns_.end(); | |
124 |
2/2✓ Branch 1 taken 1536 times.
✓ Branch 2 taken 1318 times.
|
2854 | for (; i != iend; ++i) { |
125 |
2/4✓ Branch 2 taken 1536 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1536 times.
✗ Branch 6 not taken.
|
1536 | result += (*i)->GenerateRegularExpression(is_relaxed); |
126 | } | ||
127 | 2636 | return result; | |
128 | } | ||
129 | |||
130 | 110 | std::string PathspecElementPattern::GenerateGlobString() const { | |
131 | 110 | std::string result; | |
132 | 110 | SubPatterns::const_iterator i = subpatterns_.begin(); | |
133 | 110 | const SubPatterns::const_iterator iend = subpatterns_.end(); | |
134 |
2/2✓ Branch 1 taken 131 times.
✓ Branch 2 taken 110 times.
|
241 | for (; i != iend; ++i) { |
135 |
2/4✓ Branch 2 taken 131 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 131 times.
✗ Branch 6 not taken.
|
131 | result += (*i)->GenerateGlobString(); |
136 | } | ||
137 | 220 | return result; | |
138 | } | ||
139 | |||
140 | 464 | bool PathspecElementPattern::operator==( | |
141 | const PathspecElementPattern &other) const { | ||
142 | 464 | if (subpatterns_.size() != other.subpatterns_.size() | |
143 |
5/6✓ Branch 0 taken 424 times.
✓ Branch 1 taken 40 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 424 times.
✓ Branch 6 taken 40 times.
✓ Branch 7 taken 424 times.
|
464 | || IsValid() != other.IsValid()) { |
144 | 40 | return false; | |
145 | } | ||
146 | |||
147 | 424 | SubPatterns::const_iterator i = subpatterns_.begin(); | |
148 | 424 | const SubPatterns::const_iterator iend = subpatterns_.end(); | |
149 | 424 | SubPatterns::const_iterator j = other.subpatterns_.begin(); | |
150 | 424 | const SubPatterns::const_iterator jend = other.subpatterns_.end(); | |
151 | |||
152 |
5/6✓ Branch 3 taken 480 times.
✓ Branch 4 taken 297 times.
✓ Branch 6 taken 480 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 480 times.
✓ Branch 9 taken 297 times.
|
777 | for (; i != iend && j != jend; ++i, ++j) { |
153 |
3/4✓ Branch 3 taken 480 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 127 times.
✓ Branch 6 taken 353 times.
|
480 | if (!(*i)->Compare(*j)) { |
154 | 127 | return false; | |
155 | } | ||
156 | } | ||
157 | |||
158 | 297 | return true; | |
159 | } | ||
160 | |||
161 | |||
162 | //------------------------------------------------------------------------------ | ||
163 | |||
164 | |||
165 | 7379 | void PathspecElementPattern::PlaintextSubPattern::AddChar(const char chr) { | |
166 | 7379 | chars_.push_back(chr); | |
167 | 7379 | } | |
168 | |||
169 | std::string | ||
170 | 1276 | PathspecElementPattern::PlaintextSubPattern::GenerateRegularExpression( | |
171 | const bool is_relaxed) const { | ||
172 | // Note: strict and relaxed regex are the same! | ||
173 | 1276 | std::string::const_iterator i = chars_.begin(); | |
174 | 1276 | const std::string::const_iterator iend = chars_.end(); | |
175 | 1276 | std::string regex; | |
176 |
2/2✓ Branch 2 taken 5690 times.
✓ Branch 3 taken 1276 times.
|
6966 | for (; i != iend; ++i) { |
177 |
2/2✓ Branch 2 taken 201 times.
✓ Branch 3 taken 5489 times.
|
5690 | if (IsSpecialRegexCharacter(*i)) { |
178 |
1/2✓ Branch 1 taken 201 times.
✗ Branch 2 not taken.
|
201 | regex += "\\"; |
179 | } | ||
180 |
1/2✓ Branch 2 taken 5690 times.
✗ Branch 3 not taken.
|
5690 | regex += *i; |
181 | } | ||
182 | 2552 | return regex; | |
183 | } | ||
184 | |||
185 | |||
186 | 117 | std::string PathspecElementPattern::PlaintextSubPattern::GenerateGlobString() | |
187 | const { | ||
188 | 117 | std::string::const_iterator i = chars_.begin(); | |
189 | 117 | const std::string::const_iterator iend = chars_.end(); | |
190 | 117 | std::string glob_string; | |
191 |
2/2✓ Branch 2 taken 592 times.
✓ Branch 3 taken 117 times.
|
709 | for (; i != iend; ++i) { |
192 |
2/2✓ Branch 2 taken 12 times.
✓ Branch 3 taken 580 times.
|
592 | if (Pathspec::IsSpecialChar(*i)) { |
193 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | glob_string += "\\"; |
194 | } | ||
195 |
1/2✓ Branch 2 taken 592 times.
✗ Branch 3 not taken.
|
592 | glob_string += *i; |
196 | } | ||
197 | 234 | return glob_string; | |
198 | } | ||
199 | |||
200 | 5690 | bool PathspecElementPattern::PlaintextSubPattern::IsSpecialRegexCharacter( | |
201 | const char chr) const { | ||
202 |
8/8✓ Branch 0 taken 5546 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 5537 times.
✓ Branch 3 taken 9 times.
✓ Branch 4 taken 5525 times.
✓ Branch 5 taken 12 times.
✓ Branch 6 taken 5519 times.
✓ Branch 7 taken 6 times.
|
5552 | return (chr == '.' || chr == '\\' || chr == '*' || chr == '?' || chr == '[' |
203 |
10/10✓ Branch 0 taken 5513 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 5510 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 5507 times.
✓ Branch 5 taken 3 times.
✓ Branch 6 taken 5504 times.
✓ Branch 7 taken 3 times.
✓ Branch 8 taken 5501 times.
✓ Branch 9 taken 3 times.
|
5519 | || chr == ']' || chr == '(' || chr == ')' || chr == '{' || chr == '}' |
204 |
8/8✓ Branch 0 taken 5552 times.
✓ Branch 1 taken 138 times.
✓ Branch 2 taken 5498 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 5495 times.
✓ Branch 5 taken 3 times.
✓ Branch 6 taken 6 times.
✓ Branch 7 taken 5489 times.
|
11242 | || chr == '^' || chr == '$' || chr == '+'); |
205 | } | ||
206 | |||
207 | 376 | bool PathspecElementPattern::PlaintextSubPattern::Compare( | |
208 | const SubPattern *other) const { | ||
209 |
2/2✓ Branch 1 taken 4 times.
✓ Branch 2 taken 372 times.
|
376 | if (!other->IsPlaintext()) { |
210 | 4 | return false; | |
211 | } | ||
212 | |||
213 | const PlaintextSubPattern | ||
214 |
1/2✓ Branch 0 taken 372 times.
✗ Branch 1 not taken.
|
372 | *pt_other = dynamic_cast<const PlaintextSubPattern *>(other); |
215 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 372 times.
|
372 | assert(pt_other != NULL); |
216 | 372 | return chars_ == pt_other->chars_; | |
217 | } | ||
218 | |||
219 | |||
220 | std::string | ||
221 | 187 | PathspecElementPattern::WildcardSubPattern::GenerateRegularExpression( | |
222 | const bool is_relaxed) const { | ||
223 | return (is_relaxed) ? std::string(".*") | ||
224 |
14/27✓ Branch 0 taken 72 times.
✓ Branch 1 taken 115 times.
✓ Branch 4 taken 72 times.
✗ Branch 5 not taken.
✓ Branch 8 taken 115 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 115 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 115 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 115 times.
✓ Branch 17 taken 72 times.
✗ Branch 18 not taken.
✓ Branch 19 taken 115 times.
✓ Branch 20 taken 72 times.
✓ Branch 22 taken 115 times.
✓ Branch 23 taken 72 times.
✓ Branch 25 taken 72 times.
✓ Branch 26 taken 115 times.
✗ Branch 28 not taken.
✗ Branch 29 not taken.
✗ Branch 31 not taken.
✗ Branch 32 not taken.
✗ Branch 34 not taken.
✗ Branch 35 not taken.
✗ Branch 37 not taken.
✗ Branch 38 not taken.
|
374 | : std::string("[^") + Pathspec::kSeparator + "]*"; |
225 | } | ||
226 | |||
227 | |||
228 | 7 | std::string PathspecElementPattern::WildcardSubPattern::GenerateGlobString() | |
229 | const { | ||
230 |
1/2✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
|
7 | return "*"; |
231 | } | ||
232 | |||
233 | |||
234 | 56 | bool PathspecElementPattern::WildcardSubPattern::Compare( | |
235 | const SubPattern *other) const { | ||
236 | 56 | return other->IsWildcard(); | |
237 | } | ||
238 | |||
239 | |||
240 | std::string | ||
241 | 73 | PathspecElementPattern::PlaceholderSubPattern::GenerateRegularExpression( | |
242 | const bool is_relaxed) const { | ||
243 | // Note: strict and relaxed regex are the same! | ||
244 |
3/6✓ Branch 2 taken 73 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 73 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 73 times.
✗ Branch 9 not taken.
|
146 | return std::string("[^") + Pathspec::kSeparator + "]"; |
245 | } | ||
246 | |||
247 | 7 | std::string PathspecElementPattern::PlaceholderSubPattern::GenerateGlobString() | |
248 | const { | ||
249 |
1/2✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
|
7 | return "?"; |
250 | } | ||
251 | |||
252 | 48 | bool PathspecElementPattern::PlaceholderSubPattern::Compare( | |
253 | const SubPattern *other) const { | ||
254 | 48 | return other->IsPlaceholder(); | |
255 | } | ||
256 |