Directory: | cvmfs/ |
---|---|
File: | cvmfs/pathspec/pathspec_pattern.cc |
Date: | 2025-07-27 02:42:09 |
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 | 8136 | PathspecElementPattern::PathspecElementPattern( | |
13 | const std::string::const_iterator begin, | ||
14 | 8136 | const std::string::const_iterator &end) | |
15 | 8136 | : valid_(true) { | |
16 |
1/2✓ Branch 1 taken 8136 times.
✗ Branch 2 not taken.
|
8136 | Parse(begin, end); |
17 | 8136 | } | |
18 | |||
19 | 14828 | PathspecElementPattern::PathspecElementPattern( | |
20 | 14828 | const PathspecElementPattern &other) | |
21 | 14828 | : valid_(other.valid_) { | |
22 |
1/2✓ Branch 2 taken 14828 times.
✗ Branch 3 not taken.
|
14828 | subpatterns_.reserve(other.subpatterns_.size()); |
23 | 14828 | SubPatterns::const_iterator i = other.subpatterns_.begin(); | |
24 | 14828 | const SubPatterns::const_iterator iend = other.subpatterns_.end(); | |
25 |
2/2✓ Branch 1 taken 18325 times.
✓ Branch 2 taken 14828 times.
|
33153 | for (; i != iend; ++i) { |
26 |
2/4✓ Branch 2 taken 18325 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 18325 times.
✗ Branch 6 not taken.
|
18325 | subpatterns_.push_back((*i)->Clone()); |
27 | } | ||
28 | 14828 | } | |
29 | |||
30 | 266 | PathspecElementPattern &PathspecElementPattern::operator=( | |
31 | const PathspecElementPattern &other) { | ||
32 |
1/2✓ Branch 0 taken 266 times.
✗ Branch 1 not taken.
|
266 | if (this != &other) { |
33 | 266 | valid_ = other.valid_; | |
34 | 266 | subpatterns_.clear(); | |
35 |
1/2✓ Branch 2 taken 266 times.
✗ Branch 3 not taken.
|
266 | subpatterns_.reserve(other.subpatterns_.size()); |
36 | 266 | SubPatterns::const_iterator i = other.subpatterns_.begin(); | |
37 | 266 | const SubPatterns::const_iterator iend = other.subpatterns_.end(); | |
38 |
2/2✓ Branch 1 taken 418 times.
✓ Branch 2 taken 266 times.
|
684 | for (; i != iend; ++i) { |
39 |
2/4✓ Branch 2 taken 418 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 418 times.
✗ Branch 6 not taken.
|
418 | subpatterns_.push_back((*i)->Clone()); |
40 | } | ||
41 | } | ||
42 | |||
43 | 266 | return *this; | |
44 | } | ||
45 | |||
46 | |||
47 | 22958 | PathspecElementPattern::~PathspecElementPattern() { | |
48 | 22958 | SubPatterns::const_iterator i = subpatterns_.begin(); | |
49 | 22958 | const SubPatterns::const_iterator iend = subpatterns_.end(); | |
50 |
2/2✓ Branch 2 taken 28967 times.
✓ Branch 3 taken 22958 times.
|
51925 | for (; i != iend; ++i) { |
51 |
1/2✓ Branch 1 taken 28967 times.
✗ Branch 2 not taken.
|
28967 | delete *i; |
52 | } | ||
53 | 22958 | subpatterns_.clear(); | |
54 | 22958 | } | |
55 | |||
56 | |||
57 | 8136 | void PathspecElementPattern::Parse(const std::string::const_iterator &begin, | |
58 | const std::string::const_iterator &end) { | ||
59 | 8136 | std::string::const_iterator i = begin; | |
60 |
2/2✓ Branch 1 taken 10499 times.
✓ Branch 2 taken 8136 times.
|
18635 | while (i != end) { |
61 |
3/4✓ Branch 2 taken 2469 times.
✓ Branch 3 taken 8030 times.
✓ Branch 5 taken 2469 times.
✗ Branch 6 not taken.
|
10499 | SubPattern *next = (Pathspec::IsSpecialChar(*i)) ? ParseSpecialChar(end, &i) |
62 |
1/2✓ Branch 1 taken 8030 times.
✗ Branch 2 not taken.
|
8030 | : ParsePlaintext(end, &i); |
63 |
3/4✓ Branch 1 taken 10499 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 10496 times.
|
10499 | if (next->IsEmpty()) { |
64 | 3 | valid_ = false; | |
65 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | delete next; |
66 | } else { | ||
67 |
1/2✓ Branch 1 taken 10496 times.
✗ Branch 2 not taken.
|
10496 | subpatterns_.push_back(next); |
68 | } | ||
69 | } | ||
70 | 8136 | } | |
71 | |||
72 | |||
73 | 8030 | PathspecElementPattern::SubPattern *PathspecElementPattern::ParsePlaintext( | |
74 | const std::string::const_iterator &end, std::string::const_iterator *i) { | ||
75 | 8030 | PlaintextSubPattern *pattern = new PlaintextSubPattern(); | |
76 | 8030 | bool next_is_escaped = false; | |
77 | |||
78 |
2/2✓ Branch 1 taken 35766 times.
✓ Branch 2 taken 7103 times.
|
42869 | while (*i < end) { |
79 |
6/6✓ Branch 2 taken 1430 times.
✓ Branch 3 taken 34336 times.
✓ Branch 4 taken 927 times.
✓ Branch 5 taken 503 times.
✓ Branch 6 taken 927 times.
✓ Branch 7 taken 34839 times.
|
35766 | if (Pathspec::IsSpecialChar(**i) && !next_is_escaped) { |
80 | 927 | break; | |
81 | } | ||
82 | |||
83 |
6/6✓ Branch 1 taken 855 times.
✓ Branch 2 taken 33984 times.
✓ Branch 3 taken 700 times.
✓ Branch 4 taken 155 times.
✓ Branch 5 taken 700 times.
✓ Branch 6 taken 34139 times.
|
34839 | if (**i == Pathspec::kEscaper && !next_is_escaped) { |
84 | 700 | next_is_escaped = true; | |
85 |
2/2✓ Branch 0 taken 862 times.
✓ Branch 1 taken 33277 times.
|
34139 | } else if (next_is_escaped) { |
86 |
6/6✓ Branch 2 taken 359 times.
✓ Branch 3 taken 503 times.
✓ Branch 5 taken 155 times.
✓ Branch 6 taken 204 times.
✓ Branch 7 taken 658 times.
✓ Branch 8 taken 204 times.
|
862 | if (Pathspec::IsSpecialChar(**i) || **i == Pathspec::kEscaper) { |
87 | 658 | pattern->AddChar(**i); | |
88 | 658 | next_is_escaped = false; | |
89 | } else { | ||
90 | 204 | valid_ = false; | |
91 | } | ||
92 | } else { | ||
93 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 33277 times.
|
33277 | assert(!Pathspec::IsSpecialChar(**i)); |
94 | 33277 | pattern->AddChar(**i); | |
95 | } | ||
96 | |||
97 | 34839 | ++(*i); | |
98 | } | ||
99 | |||
100 | 8030 | return pattern; | |
101 | } | ||
102 | |||
103 | 2469 | 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 2469 times.
|
2469 | assert(Pathspec::IsSpecialChar(**i)); |
106 | 2469 | const char chr = **i; | |
107 | 2469 | ++(*i); | |
108 | |||
109 |
2/3✓ Branch 0 taken 1184 times.
✓ Branch 1 taken 1285 times.
✗ Branch 2 not taken.
|
2469 | switch (chr) { |
110 | 1184 | case Pathspec::kWildcard: | |
111 | 1184 | return new WildcardSubPattern(); | |
112 | 1285 | case Pathspec::kPlaceholder: | |
113 | 1285 | return new PlaceholderSubPattern(); | |
114 | ✗ | default: | |
115 | ✗ | assert(false && "unrecognized special character"); | |
116 | } | ||
117 | } | ||
118 | |||
119 | 4874 | std::string PathspecElementPattern::GenerateRegularExpression( | |
120 | const bool is_relaxed) const { | ||
121 | 4874 | std::string result; | |
122 | 4874 | SubPatterns::const_iterator i = subpatterns_.begin(); | |
123 | 4874 | const SubPatterns::const_iterator iend = subpatterns_.end(); | |
124 |
2/2✓ Branch 1 taken 6813 times.
✓ Branch 2 taken 4874 times.
|
11687 | for (; i != iend; ++i) { |
125 |
2/4✓ Branch 2 taken 6813 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 6813 times.
✗ Branch 6 not taken.
|
6813 | result += (*i)->GenerateRegularExpression(is_relaxed); |
126 | } | ||
127 | 9748 | return result; | |
128 | } | ||
129 | |||
130 | 1292 | std::string PathspecElementPattern::GenerateGlobString() const { | |
131 | 1292 | std::string result; | |
132 | 1292 | SubPatterns::const_iterator i = subpatterns_.begin(); | |
133 | 1292 | const SubPatterns::const_iterator iend = subpatterns_.end(); | |
134 |
2/2✓ Branch 1 taken 1520 times.
✓ Branch 2 taken 1292 times.
|
2812 | for (; i != iend; ++i) { |
135 |
2/4✓ Branch 2 taken 1520 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1520 times.
✗ Branch 6 not taken.
|
1520 | result += (*i)->GenerateGlobString(); |
136 | } | ||
137 | 2584 | return result; | |
138 | } | ||
139 | |||
140 | 2670 | bool PathspecElementPattern::operator==( | |
141 | const PathspecElementPattern &other) const { | ||
142 | 2670 | if (subpatterns_.size() != other.subpatterns_.size() | |
143 |
5/6✓ Branch 0 taken 2430 times.
✓ Branch 1 taken 240 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 2430 times.
✓ Branch 6 taken 240 times.
✓ Branch 7 taken 2430 times.
|
2670 | || IsValid() != other.IsValid()) { |
144 | 240 | return false; | |
145 | } | ||
146 | |||
147 | 2430 | SubPatterns::const_iterator i = subpatterns_.begin(); | |
148 | 2430 | const SubPatterns::const_iterator iend = subpatterns_.end(); | |
149 | 2430 | SubPatterns::const_iterator j = other.subpatterns_.begin(); | |
150 | 2430 | const SubPatterns::const_iterator jend = other.subpatterns_.end(); | |
151 | |||
152 |
5/6✓ Branch 3 taken 2962 times.
✓ Branch 4 taken 1986 times.
✓ Branch 6 taken 2962 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2962 times.
✓ Branch 9 taken 1986 times.
|
4948 | for (; i != iend && j != jend; ++i, ++j) { |
153 |
3/4✓ Branch 3 taken 2962 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 444 times.
✓ Branch 6 taken 2518 times.
|
2962 | if (!(*i)->Compare(*j)) { |
154 | 444 | return false; | |
155 | } | ||
156 | } | ||
157 | |||
158 | 1986 | return true; | |
159 | } | ||
160 | |||
161 | |||
162 | //------------------------------------------------------------------------------ | ||
163 | |||
164 | |||
165 | 33935 | void PathspecElementPattern::PlaintextSubPattern::AddChar(const char chr) { | |
166 | 33935 | chars_.push_back(chr); | |
167 | 33935 | } | |
168 | |||
169 | std::string | ||
170 | 5039 | PathspecElementPattern::PlaintextSubPattern::GenerateRegularExpression( | |
171 | const bool is_relaxed) const { | ||
172 | // Note: strict and relaxed regex are the same! | ||
173 | 5039 | std::string::const_iterator i = chars_.begin(); | |
174 | 5039 | const std::string::const_iterator iend = chars_.end(); | |
175 | 5039 | std::string regex; | |
176 |
2/2✓ Branch 2 taken 22088 times.
✓ Branch 3 taken 5039 times.
|
27127 | for (; i != iend; ++i) { |
177 |
2/2✓ Branch 2 taken 1878 times.
✓ Branch 3 taken 20210 times.
|
22088 | if (IsSpecialRegexCharacter(*i)) { |
178 |
1/2✓ Branch 1 taken 1878 times.
✗ Branch 2 not taken.
|
1878 | regex += "\\"; |
179 | } | ||
180 |
1/2✓ Branch 2 taken 22088 times.
✗ Branch 3 not taken.
|
22088 | regex += *i; |
181 | } | ||
182 | 10078 | return regex; | |
183 | } | ||
184 | |||
185 | |||
186 | 1368 | std::string PathspecElementPattern::PlaintextSubPattern::GenerateGlobString() | |
187 | const { | ||
188 | 1368 | std::string::const_iterator i = chars_.begin(); | |
189 | 1368 | const std::string::const_iterator iend = chars_.end(); | |
190 | 1368 | std::string glob_string; | |
191 |
2/2✓ Branch 2 taken 6992 times.
✓ Branch 3 taken 1368 times.
|
8360 | for (; i != iend; ++i) { |
192 |
2/2✓ Branch 2 taken 152 times.
✓ Branch 3 taken 6840 times.
|
6992 | if (Pathspec::IsSpecialChar(*i)) { |
193 |
1/2✓ Branch 1 taken 152 times.
✗ Branch 2 not taken.
|
152 | glob_string += "\\"; |
194 | } | ||
195 |
1/2✓ Branch 2 taken 6992 times.
✗ Branch 3 not taken.
|
6992 | glob_string += *i; |
196 | } | ||
197 | 2736 | return glob_string; | |
198 | } | ||
199 | |||
200 | 22088 | bool PathspecElementPattern::PlaintextSubPattern::IsSpecialRegexCharacter( | |
201 | const char chr) const { | ||
202 |
8/8✓ Branch 0 taken 20951 times.
✓ Branch 1 taken 78 times.
✓ Branch 2 taken 20834 times.
✓ Branch 3 taken 117 times.
✓ Branch 4 taken 20678 times.
✓ Branch 5 taken 156 times.
✓ Branch 6 taken 20600 times.
✓ Branch 7 taken 78 times.
|
21029 | return (chr == '.' || chr == '\\' || chr == '*' || chr == '?' || chr == '[' |
203 |
10/10✓ Branch 0 taken 20522 times.
✓ Branch 1 taken 78 times.
✓ Branch 2 taken 20483 times.
✓ Branch 3 taken 39 times.
✓ Branch 4 taken 20444 times.
✓ Branch 5 taken 39 times.
✓ Branch 6 taken 20405 times.
✓ Branch 7 taken 39 times.
✓ Branch 8 taken 20366 times.
✓ Branch 9 taken 39 times.
|
20600 | || chr == ']' || chr == '(' || chr == ')' || chr == '{' || chr == '}' |
204 |
8/8✓ Branch 0 taken 21029 times.
✓ Branch 1 taken 1059 times.
✓ Branch 2 taken 20327 times.
✓ Branch 3 taken 39 times.
✓ Branch 4 taken 20288 times.
✓ Branch 5 taken 39 times.
✓ Branch 6 taken 78 times.
✓ Branch 7 taken 20210 times.
|
43117 | || chr == '^' || chr == '$' || chr == '+'); |
205 | } | ||
206 | |||
207 | 2149 | bool PathspecElementPattern::PlaintextSubPattern::Compare( | |
208 | const SubPattern *other) const { | ||
209 |
2/2✓ Branch 1 taken 38 times.
✓ Branch 2 taken 2111 times.
|
2149 | if (!other->IsPlaintext()) { |
210 | 38 | return false; | |
211 | } | ||
212 | |||
213 | const PlaintextSubPattern | ||
214 |
1/2✓ Branch 0 taken 2111 times.
✗ Branch 1 not taken.
|
2111 | *pt_other = dynamic_cast<const PlaintextSubPattern *>(other); |
215 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2111 times.
|
2111 | assert(pt_other != NULL); |
216 | 2111 | return chars_ == pt_other->chars_; | |
217 | } | ||
218 | |||
219 | |||
220 | std::string | ||
221 | 997 | PathspecElementPattern::WildcardSubPattern::GenerateRegularExpression( | |
222 | const bool is_relaxed) const { | ||
223 | return (is_relaxed) ? std::string(".*") | ||
224 |
14/27✓ Branch 0 taken 250 times.
✓ Branch 1 taken 747 times.
✓ Branch 4 taken 250 times.
✗ Branch 5 not taken.
✓ Branch 8 taken 747 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 747 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 747 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 747 times.
✓ Branch 17 taken 250 times.
✗ Branch 18 not taken.
✓ Branch 19 taken 747 times.
✓ Branch 20 taken 250 times.
✓ Branch 22 taken 747 times.
✓ Branch 23 taken 250 times.
✓ Branch 25 taken 250 times.
✓ Branch 26 taken 747 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.
|
1994 | : std::string("[^") + Pathspec::kSeparator + "]*"; |
225 | } | ||
226 | |||
227 | |||
228 | 76 | std::string PathspecElementPattern::WildcardSubPattern::GenerateGlobString() | |
229 | const { | ||
230 |
1/2✓ Branch 2 taken 76 times.
✗ Branch 3 not taken.
|
76 | return "*"; |
231 | } | ||
232 | |||
233 | |||
234 | 357 | bool PathspecElementPattern::WildcardSubPattern::Compare( | |
235 | const SubPattern *other) const { | ||
236 | 357 | return other->IsWildcard(); | |
237 | } | ||
238 | |||
239 | |||
240 | std::string | ||
241 | 777 | PathspecElementPattern::PlaceholderSubPattern::GenerateRegularExpression( | |
242 | const bool is_relaxed) const { | ||
243 | // Note: strict and relaxed regex are the same! | ||
244 |
3/6✓ Branch 2 taken 777 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 777 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 777 times.
✗ Branch 9 not taken.
|
1554 | return std::string("[^") + Pathspec::kSeparator + "]"; |
245 | } | ||
246 | |||
247 | 76 | std::string PathspecElementPattern::PlaceholderSubPattern::GenerateGlobString() | |
248 | const { | ||
249 |
1/2✓ Branch 2 taken 76 times.
✗ Branch 3 not taken.
|
76 | return "?"; |
250 | } | ||
251 | |||
252 | 456 | bool PathspecElementPattern::PlaceholderSubPattern::Compare( | |
253 | const SubPattern *other) const { | ||
254 | 456 | return other->IsPlaceholder(); | |
255 | } | ||
256 |