GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/pathspec/pathspec_pattern.cc
Date: 2026-03-15 02:35:27
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 2932 PathspecElementPattern::PathspecElementPattern(
13 const std::string::const_iterator begin,
14 2932 const std::string::const_iterator &end)
15 2932 : valid_(true) {
16
1/2
✓ Branch 1 taken 2932 times.
✗ Branch 2 not taken.
2932 Parse(begin, end);
17 2932 }
18
19 12133 PathspecElementPattern::PathspecElementPattern(
20 12133 const PathspecElementPattern &other)
21 12133 : valid_(other.valid_) {
22
1/2
✓ Branch 2 taken 12133 times.
✗ Branch 3 not taken.
12133 subpatterns_.reserve(other.subpatterns_.size());
23 12133 SubPatterns::const_iterator i = other.subpatterns_.begin();
24 12133 const SubPatterns::const_iterator iend = other.subpatterns_.end();
25
2/2
✓ Branch 1 taken 12579 times.
✓ Branch 2 taken 12133 times.
24712 for (; i != iend; ++i) {
26
2/4
✓ Branch 2 taken 12579 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 12579 times.
✗ Branch 6 not taken.
12579 subpatterns_.push_back((*i)->Clone());
27 }
28 12133 }
29
30 14 PathspecElementPattern &PathspecElementPattern::operator=(
31 const PathspecElementPattern &other) {
32
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 if (this != &other) {
33 14 valid_ = other.valid_;
34 14 subpatterns_.clear();
35
1/2
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
14 subpatterns_.reserve(other.subpatterns_.size());
36 14 SubPatterns::const_iterator i = other.subpatterns_.begin();
37 14 const SubPatterns::const_iterator iend = other.subpatterns_.end();
38
2/2
✓ Branch 1 taken 22 times.
✓ Branch 2 taken 14 times.
36 for (; i != iend; ++i) {
39
2/4
✓ Branch 2 taken 22 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 22 times.
✗ Branch 6 not taken.
22 subpatterns_.push_back((*i)->Clone());
40 }
41 }
42
43 14 return *this;
44 }
45
46
47 15059 PathspecElementPattern::~PathspecElementPattern() {
48 15059 SubPatterns::const_iterator i = subpatterns_.begin();
49 15059 const SubPatterns::const_iterator iend = subpatterns_.end();
50
2/2
✓ Branch 2 taken 15684 times.
✓ Branch 3 taken 15059 times.
30743 for (; i != iend; ++i) {
51
1/2
✓ Branch 1 taken 15684 times.
✗ Branch 2 not taken.
15684 delete *i;
52 }
53 15059 subpatterns_.clear();
54 15059 }
55
56
57 2932 void PathspecElementPattern::Parse(const std::string::const_iterator &begin,
58 const std::string::const_iterator &end) {
59 2932 std::string::const_iterator i = begin;
60
2/2
✓ Branch 1 taken 3104 times.
✓ Branch 2 taken 2932 times.
6036 while (i != end) {
61
3/4
✓ Branch 2 taken 339 times.
✓ Branch 3 taken 2765 times.
✓ Branch 5 taken 339 times.
✗ Branch 6 not taken.
3104 SubPattern *next = (Pathspec::IsSpecialChar(*i)) ? ParseSpecialChar(end, &i)
62
1/2
✓ Branch 1 taken 2765 times.
✗ Branch 2 not taken.
2765 : ParsePlaintext(end, &i);
63
3/4
✓ Branch 1 taken 3104 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 3103 times.
3104 if (next->IsEmpty()) {
64 1 valid_ = false;
65
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 delete next;
66 } else {
67
1/2
✓ Branch 1 taken 3103 times.
✗ Branch 2 not taken.
3103 subpatterns_.push_back(next);
68 }
69 }
70 2932 }
71
72
73 2765 PathspecElementPattern::SubPattern *PathspecElementPattern::ParsePlaintext(
74 const std::string::const_iterator &end, std::string::const_iterator *i) {
75 2765 PlaintextSubPattern *pattern = new PlaintextSubPattern();
76 2765 bool next_is_escaped = false;
77
78
2/2
✓ Branch 1 taken 12630 times.
✓ Branch 2 taken 2725 times.
15355 while (*i < end) {
79
6/6
✓ Branch 2 taken 60 times.
✓ Branch 3 taken 12570 times.
✓ Branch 4 taken 40 times.
✓ Branch 5 taken 20 times.
✓ Branch 6 taken 40 times.
✓ Branch 7 taken 12590 times.
12630 if (Pathspec::IsSpecialChar(**i) && !next_is_escaped) {
80 40 break;
81 }
82
83
6/6
✓ Branch 1 taken 34 times.
✓ Branch 2 taken 12556 times.
✓ Branch 3 taken 28 times.
✓ Branch 4 taken 6 times.
✓ Branch 5 taken 28 times.
✓ Branch 6 taken 12562 times.
12590 if (**i == Pathspec::kEscaper && !next_is_escaped) {
84 28 next_is_escaped = true;
85
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 12528 times.
12562 } else if (next_is_escaped) {
86
6/6
✓ Branch 2 taken 14 times.
✓ Branch 3 taken 20 times.
✓ Branch 5 taken 6 times.
✓ Branch 6 taken 8 times.
✓ Branch 7 taken 26 times.
✓ Branch 8 taken 8 times.
34 if (Pathspec::IsSpecialChar(**i) || **i == Pathspec::kEscaper) {
87 26 pattern->AddChar(**i);
88 26 next_is_escaped = false;
89 } else {
90 8 valid_ = false;
91 }
92 } else {
93
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 12528 times.
12528 assert(!Pathspec::IsSpecialChar(**i));
94 12528 pattern->AddChar(**i);
95 }
96
97 12590 ++(*i);
98 }
99
100 2765 return pattern;
101 }
102
103 339 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 339 times.
339 assert(Pathspec::IsSpecialChar(**i));
106 339 const char chr = **i;
107 339 ++(*i);
108
109
2/3
✓ Branch 0 taken 283 times.
✓ Branch 1 taken 56 times.
✗ Branch 2 not taken.
339 switch (chr) {
110 283 case Pathspec::kWildcard:
111 283 return new WildcardSubPattern();
112 56 case Pathspec::kPlaceholder:
113 56 return new PlaceholderSubPattern();
114 default:
115 assert(false && "unrecognized special character");
116 }
117 }
118
119 2630 std::string PathspecElementPattern::GenerateRegularExpression(
120 const bool is_relaxed) const {
121 2630 std::string result;
122 2630 SubPatterns::const_iterator i = subpatterns_.begin();
123 2630 const SubPatterns::const_iterator iend = subpatterns_.end();
124
2/2
✓ Branch 1 taken 2787 times.
✓ Branch 2 taken 2630 times.
5417 for (; i != iend; ++i) {
125
2/4
✓ Branch 2 taken 2787 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 2787 times.
✗ Branch 6 not taken.
2787 result += (*i)->GenerateRegularExpression(is_relaxed);
126 }
127 5260 return result;
128 }
129
130 42 std::string PathspecElementPattern::GenerateGlobString() const {
131 42 std::string result;
132 42 SubPatterns::const_iterator i = subpatterns_.begin();
133 42 const SubPatterns::const_iterator iend = subpatterns_.end();
134
2/2
✓ Branch 1 taken 51 times.
✓ Branch 2 taken 42 times.
93 for (; i != iend; ++i) {
135
2/4
✓ Branch 2 taken 51 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 51 times.
✗ Branch 6 not taken.
51 result += (*i)->GenerateGlobString();
136 }
137 84 return result;
138 }
139
140 442 bool PathspecElementPattern::operator==(
141 const PathspecElementPattern &other) const {
142 442 if (subpatterns_.size() != other.subpatterns_.size()
143
5/6
✓ Branch 0 taken 426 times.
✓ Branch 1 taken 16 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 426 times.
✓ Branch 6 taken 16 times.
✓ Branch 7 taken 426 times.
442 || IsValid() != other.IsValid()) {
144 16 return false;
145 }
146
147 426 SubPatterns::const_iterator i = subpatterns_.begin();
148 426 const SubPatterns::const_iterator iend = subpatterns_.end();
149 426 SubPatterns::const_iterator j = other.subpatterns_.begin();
150 426 const SubPatterns::const_iterator jend = other.subpatterns_.end();
151
152
5/6
✓ Branch 3 taken 454 times.
✓ Branch 4 taken 222 times.
✓ Branch 6 taken 454 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 454 times.
✓ Branch 9 taken 222 times.
676 for (; i != iend && j != jend; ++i, ++j) {
153
3/4
✓ Branch 3 taken 454 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 204 times.
✓ Branch 6 taken 250 times.
454 if (!(*i)->Compare(*j)) {
154 204 return false;
155 }
156 }
157
158 222 return true;
159 }
160
161
162 //------------------------------------------------------------------------------
163
164
165 12554 void PathspecElementPattern::PlaintextSubPattern::AddChar(const char chr) {
166 12554 chars_.push_back(chr);
167 12554 }
168
169 std::string
170 2523 PathspecElementPattern::PlaintextSubPattern::GenerateRegularExpression(
171 const bool is_relaxed) const {
172 // Note: strict and relaxed regex are the same!
173 2523 std::string::const_iterator i = chars_.begin();
174 2523 const std::string::const_iterator iend = chars_.end();
175 2523 std::string regex;
176
2/2
✓ Branch 2 taken 11648 times.
✓ Branch 3 taken 2523 times.
14171 for (; i != iend; ++i) {
177
2/2
✓ Branch 2 taken 165 times.
✓ Branch 3 taken 11483 times.
11648 if (IsSpecialRegexCharacter(*i)) {
178
1/2
✓ Branch 1 taken 165 times.
✗ Branch 2 not taken.
165 regex += "\\";
179 }
180
1/2
✓ Branch 2 taken 11648 times.
✗ Branch 3 not taken.
11648 regex += *i;
181 }
182 5046 return regex;
183 }
184
185
186 45 std::string PathspecElementPattern::PlaintextSubPattern::GenerateGlobString()
187 const {
188 45 std::string::const_iterator i = chars_.begin();
189 45 const std::string::const_iterator iend = chars_.end();
190 45 std::string glob_string;
191
2/2
✓ Branch 2 taken 224 times.
✓ Branch 3 taken 45 times.
269 for (; i != iend; ++i) {
192
2/2
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 220 times.
224 if (Pathspec::IsSpecialChar(*i)) {
193
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 glob_string += "\\";
194 }
195
1/2
✓ Branch 2 taken 224 times.
✗ Branch 3 not taken.
224 glob_string += *i;
196 }
197 90 return glob_string;
198 }
199
200 11648 bool PathspecElementPattern::PlaintextSubPattern::IsSpecialRegexCharacter(
201 const char chr) const {
202
8/8
✓ Branch 0 taken 11521 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 11515 times.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 11507 times.
✓ Branch 5 taken 8 times.
✓ Branch 6 taken 11503 times.
✓ Branch 7 taken 4 times.
11525 return (chr == '.' || chr == '\\' || chr == '*' || chr == '?' || chr == '['
203
10/10
✓ Branch 0 taken 11499 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 11497 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 11495 times.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 11493 times.
✓ Branch 7 taken 2 times.
✓ Branch 8 taken 11491 times.
✓ Branch 9 taken 2 times.
11503 || chr == ']' || chr == '(' || chr == ')' || chr == '{' || chr == '}'
204
8/8
✓ Branch 0 taken 11525 times.
✓ Branch 1 taken 123 times.
✓ Branch 2 taken 11489 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 11487 times.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 4 times.
✓ Branch 7 taken 11483 times.
23173 || chr == '^' || chr == '$' || chr == '+');
205 }
206
207 407 bool PathspecElementPattern::PlaintextSubPattern::Compare(
208 const SubPattern *other) const {
209
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 405 times.
407 if (!other->IsPlaintext()) {
210 2 return false;
211 }
212
213 const PlaintextSubPattern
214
1/2
✓ Branch 0 taken 405 times.
✗ Branch 1 not taken.
405 *pt_other = dynamic_cast<const PlaintextSubPattern *>(other);
215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 405 times.
405 assert(pt_other != NULL);
216 405 return chars_ == pt_other->chars_;
217 }
218
219
220 std::string
221 232 PathspecElementPattern::WildcardSubPattern::GenerateRegularExpression(
222 const bool is_relaxed) const {
223 return (is_relaxed) ? std::string(".*")
224
14/27
✓ Branch 0 taken 121 times.
✓ Branch 1 taken 111 times.
✓ Branch 4 taken 121 times.
✗ Branch 5 not taken.
✓ Branch 8 taken 111 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 111 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 111 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 111 times.
✓ Branch 17 taken 121 times.
✗ Branch 18 not taken.
✓ Branch 19 taken 111 times.
✓ Branch 20 taken 121 times.
✓ Branch 22 taken 111 times.
✓ Branch 23 taken 121 times.
✓ Branch 25 taken 121 times.
✓ Branch 26 taken 111 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.
464 : std::string("[^") + Pathspec::kSeparator + "]*";
225 }
226
227
228 3 std::string PathspecElementPattern::WildcardSubPattern::GenerateGlobString()
229 const {
230
1/2
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 return "*";
231 }
232
233
234 23 bool PathspecElementPattern::WildcardSubPattern::Compare(
235 const SubPattern *other) const {
236 23 return other->IsWildcard();
237 }
238
239
240 std::string
241 32 PathspecElementPattern::PlaceholderSubPattern::GenerateRegularExpression(
242 const bool is_relaxed) const {
243 // Note: strict and relaxed regex are the same!
244
3/6
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 32 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 32 times.
✗ Branch 9 not taken.
64 return std::string("[^") + Pathspec::kSeparator + "]";
245 }
246
247 3 std::string PathspecElementPattern::PlaceholderSubPattern::GenerateGlobString()
248 const {
249
1/2
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 return "?";
250 }
251
252 24 bool PathspecElementPattern::PlaceholderSubPattern::Compare(
253 const SubPattern *other) const {
254 24 return other->IsPlaceholder();
255 }
256