GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/pathspec/pathspec_pattern.cc
Date: 2026-09-27 02:40: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 6160 PathspecElementPattern::PathspecElementPattern(
13 const std::string::const_iterator begin,
14 6160 const std::string::const_iterator &end)
15 6160 : valid_(true) {
16
1/2
✓ Branch 1 taken 6160 times.
✗ Branch 2 not taken.
6160 Parse(begin, end);
17 6160 }
18
19 22310 PathspecElementPattern::PathspecElementPattern(
20 22310 const PathspecElementPattern &other)
21 22310 : valid_(other.valid_) {
22
1/2
✓ Branch 2 taken 22310 times.
✗ Branch 3 not taken.
22310 subpatterns_.reserve(other.subpatterns_.size());
23 22310 SubPatterns::const_iterator i = other.subpatterns_.begin();
24 22310 const SubPatterns::const_iterator iend = other.subpatterns_.end();
25
2/2
✓ Branch 1 taken 24001 times.
✓ Branch 2 taken 22310 times.
46311 for (; i != iend; ++i) {
26
2/4
✓ Branch 2 taken 24001 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 24001 times.
✗ Branch 6 not taken.
24001 subpatterns_.push_back((*i)->Clone());
27 }
28 22310 }
29
30 63 PathspecElementPattern &PathspecElementPattern::operator=(
31 const PathspecElementPattern &other) {
32
1/2
✓ Branch 0 taken 63 times.
✗ Branch 1 not taken.
63 if (this != &other) {
33 63 valid_ = other.valid_;
34 63 subpatterns_.clear();
35
1/2
✓ Branch 2 taken 63 times.
✗ Branch 3 not taken.
63 subpatterns_.reserve(other.subpatterns_.size());
36 63 SubPatterns::const_iterator i = other.subpatterns_.begin();
37 63 const SubPatterns::const_iterator iend = other.subpatterns_.end();
38
2/2
✓ Branch 1 taken 99 times.
✓ Branch 2 taken 63 times.
162 for (; i != iend; ++i) {
39
2/4
✓ Branch 2 taken 99 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 99 times.
✗ Branch 6 not taken.
99 subpatterns_.push_back((*i)->Clone());
40 }
41 }
42
43 63 return *this;
44 }
45
46
47 28464 PathspecElementPattern::~PathspecElementPattern() {
48 28464 SubPatterns::const_iterator i = subpatterns_.begin();
49 28464 const SubPatterns::const_iterator iend = subpatterns_.end();
50
2/2
✓ Branch 2 taken 30942 times.
✓ Branch 3 taken 28464 times.
59406 for (; i != iend; ++i) {
51
1/2
✓ Branch 1 taken 30942 times.
✗ Branch 2 not taken.
30942 delete *i;
52 }
53 28464 subpatterns_.clear();
54 28464 }
55
56
57 6160 void PathspecElementPattern::Parse(const std::string::const_iterator &begin,
58 const std::string::const_iterator &end) {
59 6160 std::string::const_iterator i = begin;
60
2/2
✓ Branch 1 taken 6937 times.
✓ Branch 2 taken 6160 times.
13097 while (i != end) {
61
3/4
✓ Branch 2 taken 1447 times.
✓ Branch 3 taken 5490 times.
✓ Branch 5 taken 1447 times.
✗ Branch 6 not taken.
6937 SubPattern *next = (Pathspec::IsSpecialChar(*i)) ? ParseSpecialChar(end, &i)
62
1/2
✓ Branch 1 taken 5490 times.
✗ Branch 2 not taken.
5490 : ParsePlaintext(end, &i);
63
3/4
✓ Branch 1 taken 6937 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 26 times.
✓ Branch 4 taken 6911 times.
6937 if (next->IsEmpty()) {
64 26 valid_ = false;
65
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
26 delete next;
66 } else {
67
1/2
✓ Branch 1 taken 6911 times.
✗ Branch 2 not taken.
6911 subpatterns_.push_back(next);
68 }
69 }
70 6160 }
71
72
73 5490 PathspecElementPattern::SubPattern *PathspecElementPattern::ParsePlaintext(
74 const std::string::const_iterator &end, std::string::const_iterator *i) {
75 5490 PlaintextSubPattern *pattern = new PlaintextSubPattern();
76 5490 bool next_is_escaped = false;
77
78
2/2
✓ Branch 1 taken 23763 times.
✓ Branch 2 taken 5210 times.
28973 while (*i < end) {
79
6/6
✓ Branch 2 taken 393 times.
✓ Branch 3 taken 23370 times.
✓ Branch 4 taken 280 times.
✓ Branch 5 taken 113 times.
✓ Branch 6 taken 280 times.
✓ Branch 7 taken 23483 times.
23763 if (Pathspec::IsSpecialChar(**i) && !next_is_escaped) {
80 280 break;
81 }
82
83
6/6
✓ Branch 1 taken 218 times.
✓ Branch 2 taken 23265 times.
✓ Branch 3 taken 183 times.
✓ Branch 4 taken 35 times.
✓ Branch 5 taken 183 times.
✓ Branch 6 taken 23300 times.
23483 if (**i == Pathspec::kEscaper && !next_is_escaped) {
84 183 next_is_escaped = true;
85
2/2
✓ Branch 0 taken 271 times.
✓ Branch 1 taken 23029 times.
23300 } else if (next_is_escaped) {
86
6/6
✓ Branch 2 taken 158 times.
✓ Branch 3 taken 113 times.
✓ Branch 5 taken 35 times.
✓ Branch 6 taken 123 times.
✓ Branch 7 taken 148 times.
✓ Branch 8 taken 123 times.
271 if (Pathspec::IsSpecialChar(**i) || **i == Pathspec::kEscaper) {
87 148 pattern->AddChar(**i);
88 148 next_is_escaped = false;
89 } else {
90 123 valid_ = false;
91 }
92 } else {
93
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 23029 times.
23029 assert(!Pathspec::IsSpecialChar(**i));
94 23029 pattern->AddChar(**i);
95 }
96
97 23483 ++(*i);
98 }
99
100 5490 return pattern;
101 }
102
103 1447 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 1447 times.
1447 assert(Pathspec::IsSpecialChar(**i));
106 1447 const char chr = **i;
107 1447 ++(*i);
108
109
2/3
✓ Branch 0 taken 985 times.
✓ Branch 1 taken 462 times.
✗ Branch 2 not taken.
1447 switch (chr) {
110 985 case Pathspec::kWildcard:
111 985 return new WildcardSubPattern();
112 462 case Pathspec::kPlaceholder:
113 462 return new PlaceholderSubPattern();
114 ✗ default:
115 ✗ assert(false && "unrecognized special character");
116 }
117 }
118
119 3966 std::string PathspecElementPattern::GenerateRegularExpression(
120 const bool is_relaxed) const {
121 3966 std::string result;
122 3966 SubPatterns::const_iterator i = subpatterns_.begin();
123 3966 const SubPatterns::const_iterator iend = subpatterns_.end();
124
2/2
✓ Branch 1 taken 4572 times.
✓ Branch 2 taken 3966 times.
8538 for (; i != iend; ++i) {
125
2/4
✓ Branch 2 taken 4572 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 4572 times.
✗ Branch 6 not taken.
4572 result += (*i)->GenerateRegularExpression(is_relaxed);
126 }
127 7932 return result;
128 }
129
130 302 std::string PathspecElementPattern::GenerateGlobString() const {
131 302 std::string result;
132 302 SubPatterns::const_iterator i = subpatterns_.begin();
133 302 const SubPatterns::const_iterator iend = subpatterns_.end();
134
2/2
✓ Branch 1 taken 353 times.
✓ Branch 2 taken 302 times.
655 for (; i != iend; ++i) {
135
2/4
✓ Branch 2 taken 353 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 353 times.
✗ Branch 6 not taken.
353 result += (*i)->GenerateGlobString();
136 }
137 604 return result;
138 }
139
140 1496 bool PathspecElementPattern::operator==(
141 const PathspecElementPattern &other) const {
142 1496 if (subpatterns_.size() != other.subpatterns_.size()
143
5/6
✓ Branch 0 taken 1338 times.
✓ Branch 1 taken 158 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1338 times.
✓ Branch 6 taken 158 times.
✓ Branch 7 taken 1338 times.
1496 || IsValid() != other.IsValid()) {
144 158 return false;
145 }
146
147 1338 SubPatterns::const_iterator i = subpatterns_.begin();
148 1338 const SubPatterns::const_iterator iend = subpatterns_.end();
149 1338 SubPatterns::const_iterator j = other.subpatterns_.begin();
150 1338 const SubPatterns::const_iterator jend = other.subpatterns_.end();
151
152
5/6
✓ Branch 3 taken 1464 times.
✓ Branch 4 taken 927 times.
✓ Branch 6 taken 1464 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 1464 times.
✓ Branch 9 taken 927 times.
2391 for (; i != iend && j != jend; ++i, ++j) {
153
3/4
✓ Branch 3 taken 1464 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 411 times.
✓ Branch 6 taken 1053 times.
1464 if (!(*i)->Compare(*j)) {
154 411 return false;
155 }
156 }
157
158 927 return true;
159 }
160
161
162 //------------------------------------------------------------------------------
163
164
165 23177 void PathspecElementPattern::PlaintextSubPattern::AddChar(const char chr) {
166 23177 chars_.push_back(chr);
167 23177 }
168
169 std::string
170 3740 PathspecElementPattern::PlaintextSubPattern::GenerateRegularExpression(
171 const bool is_relaxed) const {
172 // Note: strict and relaxed regex are the same!
173 3740 std::string::const_iterator i = chars_.begin();
174 3740 const std::string::const_iterator iend = chars_.end();
175 3740 std::string regex;
176
2/2
✓ Branch 2 taken 16613 times.
✓ Branch 3 taken 3740 times.
20353 for (; i != iend; ++i) {
177
2/2
✓ Branch 2 taken 610 times.
✓ Branch 3 taken 16003 times.
16613 if (IsSpecialRegexCharacter(*i)) {
178
1/2
✓ Branch 1 taken 610 times.
✗ Branch 2 not taken.
610 regex += "\\";
179 }
180
1/2
✓ Branch 2 taken 16613 times.
✗ Branch 3 not taken.
16613 regex += *i;
181 }
182 7480 return regex;
183 }
184
185
186 319 std::string PathspecElementPattern::PlaintextSubPattern::GenerateGlobString()
187 const {
188 319 std::string::const_iterator i = chars_.begin();
189 319 const std::string::const_iterator iend = chars_.end();
190 319 std::string glob_string;
191
2/2
✓ Branch 2 taken 1636 times.
✓ Branch 3 taken 319 times.
1955 for (; i != iend; ++i) {
192
2/2
✓ Branch 2 taken 36 times.
✓ Branch 3 taken 1600 times.
1636 if (Pathspec::IsSpecialChar(*i)) {
193
1/2
✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
36 glob_string += "\\";
194 }
195
1/2
✓ Branch 2 taken 1636 times.
✗ Branch 3 not taken.
1636 glob_string += *i;
196 }
197 638 return glob_string;
198 }
199
200 16613 bool PathspecElementPattern::PlaintextSubPattern::IsSpecialRegexCharacter(
201 const char chr) const {
202
8/8
✓ Branch 0 taken 16158 times.
✓ Branch 1 taken 17 times.
✓ Branch 2 taken 16133 times.
✓ Branch 3 taken 25 times.
✓ Branch 4 taken 16099 times.
✓ Branch 5 taken 34 times.
✓ Branch 6 taken 16083 times.
✓ Branch 7 taken 16 times.
16175 return (chr == '.' || chr == '\\' || chr == '*' || chr == '?' || chr == '['
203
10/10
✓ Branch 0 taken 16067 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 16059 times.
✓ Branch 3 taken 8 times.
✓ Branch 4 taken 16051 times.
✓ Branch 5 taken 8 times.
✓ Branch 6 taken 16043 times.
✓ Branch 7 taken 8 times.
✓ Branch 8 taken 16035 times.
✓ Branch 9 taken 8 times.
16083 || chr == ']' || chr == '(' || chr == ')' || chr == '{' || chr == '}'
204
8/8
✓ Branch 0 taken 16175 times.
✓ Branch 1 taken 438 times.
✓ Branch 2 taken 16027 times.
✓ Branch 3 taken 8 times.
✓ Branch 4 taken 16019 times.
✓ Branch 5 taken 8 times.
✓ Branch 6 taken 16 times.
✓ Branch 7 taken 16003 times.
32788 || chr == '^' || chr == '$' || chr == '+');
205 }
206
207 1145 bool PathspecElementPattern::PlaintextSubPattern::Compare(
208 const SubPattern *other) const {
209
2/2
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 1136 times.
1145 if (!other->IsPlaintext()) {
210 9 return false;
211 }
212
213 const PlaintextSubPattern
214
1/2
✓ Branch 0 taken 1136 times.
✗ Branch 1 not taken.
1136 *pt_other = dynamic_cast<const PlaintextSubPattern *>(other);
215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1136 times.
1136 assert(pt_other != NULL);
216 1136 return chars_ == pt_other->chars_;
217 }
218
219
220 std::string
221 635 PathspecElementPattern::WildcardSubPattern::GenerateRegularExpression(
222 const bool is_relaxed) const {
223 return (is_relaxed) ? std::string(".*")
224
14/27
✓ Branch 0 taken 261 times.
✓ Branch 1 taken 374 times.
✓ Branch 4 taken 261 times.
✗ Branch 5 not taken.
✓ Branch 8 taken 374 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 374 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 374 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 374 times.
✓ Branch 17 taken 261 times.
✗ Branch 18 not taken.
✓ Branch 19 taken 374 times.
✓ Branch 20 taken 261 times.
✓ Branch 22 taken 374 times.
✓ Branch 23 taken 261 times.
✓ Branch 25 taken 261 times.
✓ Branch 26 taken 374 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.
1270 : std::string("[^") + Pathspec::kSeparator + "]*";
225 }
226
227
228 17 std::string PathspecElementPattern::WildcardSubPattern::GenerateGlobString()
229 const {
230
1/2
✓ Branch 2 taken 17 times.
✗ Branch 3 not taken.
17 return "*";
231 }
232
233
234 211 bool PathspecElementPattern::WildcardSubPattern::Compare(
235 const SubPattern *other) const {
236 211 return other->IsWildcard();
237 }
238
239
240 std::string
241 197 PathspecElementPattern::PlaceholderSubPattern::GenerateRegularExpression(
242 const bool is_relaxed) const {
243 // Note: strict and relaxed regex are the same!
244
3/6
✓ Branch 2 taken 197 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 197 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 197 times.
✗ Branch 9 not taken.
394 return std::string("[^") + Pathspec::kSeparator + "]";
245 }
246
247 17 std::string PathspecElementPattern::PlaceholderSubPattern::GenerateGlobString()
248 const {
249
1/2
✓ Branch 2 taken 17 times.
✗ Branch 3 not taken.
17 return "?";
250 }
251
252 108 bool PathspecElementPattern::PlaceholderSubPattern::Compare(
253 const SubPattern *other) const {
254 108 return other->IsPlaceholder();
255 }
256