GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/pathspec/pathspec_pattern.cc
Date: 2026-01-04 02:35:37
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 9923 PathspecElementPattern::PathspecElementPattern(
13 const std::string::const_iterator begin,
14 9923 const std::string::const_iterator &end)
15 9923 : valid_(true) {
16
1/2
✓ Branch 1 taken 9923 times.
✗ Branch 2 not taken.
9923 Parse(begin, end);
17 9923 }
18
19 25028 PathspecElementPattern::PathspecElementPattern(
20 25028 const PathspecElementPattern &other)
21 25028 : valid_(other.valid_) {
22
1/2
✓ Branch 2 taken 25028 times.
✗ Branch 3 not taken.
25028 subpatterns_.reserve(other.subpatterns_.size());
23 25028 SubPatterns::const_iterator i = other.subpatterns_.begin();
24 25028 const SubPatterns::const_iterator iend = other.subpatterns_.end();
25
2/2
✓ Branch 1 taken 28988 times.
✓ Branch 2 taken 25028 times.
54016 for (; i != iend; ++i) {
26
2/4
✓ Branch 2 taken 28988 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 28988 times.
✗ Branch 6 not taken.
28988 subpatterns_.push_back((*i)->Clone());
27 }
28 25028 }
29
30 238 PathspecElementPattern &PathspecElementPattern::operator=(
31 const PathspecElementPattern &other) {
32
1/2
✓ Branch 0 taken 238 times.
✗ Branch 1 not taken.
238 if (this != &other) {
33 238 valid_ = other.valid_;
34 238 subpatterns_.clear();
35
1/2
✓ Branch 2 taken 238 times.
✗ Branch 3 not taken.
238 subpatterns_.reserve(other.subpatterns_.size());
36 238 SubPatterns::const_iterator i = other.subpatterns_.begin();
37 238 const SubPatterns::const_iterator iend = other.subpatterns_.end();
38
2/2
✓ Branch 1 taken 374 times.
✓ Branch 2 taken 238 times.
612 for (; i != iend; ++i) {
39
2/4
✓ Branch 2 taken 374 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 374 times.
✗ Branch 6 not taken.
374 subpatterns_.push_back((*i)->Clone());
40 }
41 }
42
43 238 return *this;
44 }
45
46
47 34945 PathspecElementPattern::~PathspecElementPattern() {
48 34945 SubPatterns::const_iterator i = subpatterns_.begin();
49 34945 const SubPatterns::const_iterator iend = subpatterns_.end();
50
2/2
✓ Branch 2 taken 41341 times.
✓ Branch 3 taken 34945 times.
76286 for (; i != iend; ++i) {
51
1/2
✓ Branch 1 taken 41341 times.
✗ Branch 2 not taken.
41341 delete *i;
52 }
53 34945 subpatterns_.clear();
54 34945 }
55
56
57 9923 void PathspecElementPattern::Parse(const std::string::const_iterator &begin,
58 const std::string::const_iterator &end) {
59 9923 std::string::const_iterator i = begin;
60
2/2
✓ Branch 1 taken 12257 times.
✓ Branch 2 taken 9923 times.
22180 while (i != end) {
61
3/4
✓ Branch 2 taken 3082 times.
✓ Branch 3 taken 9175 times.
✓ Branch 5 taken 3082 times.
✗ Branch 6 not taken.
12257 SubPattern *next = (Pathspec::IsSpecialChar(*i)) ? ParseSpecialChar(end, &i)
62
1/2
✓ Branch 1 taken 9175 times.
✗ Branch 2 not taken.
9175 : ParsePlaintext(end, &i);
63
3/4
✓ Branch 1 taken 12257 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 34 times.
✓ Branch 4 taken 12223 times.
12257 if (next->IsEmpty()) {
64 34 valid_ = false;
65
1/2
✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
34 delete next;
66 } else {
67
1/2
✓ Branch 1 taken 12223 times.
✗ Branch 2 not taken.
12223 subpatterns_.push_back(next);
68 }
69 }
70 9923 }
71
72
73 9175 PathspecElementPattern::SubPattern *PathspecElementPattern::ParsePlaintext(
74 const std::string::const_iterator &end, std::string::const_iterator *i) {
75 9175 PlaintextSubPattern *pattern = new PlaintextSubPattern();
76 9175 bool next_is_escaped = false;
77
78
2/2
✓ Branch 1 taken 39362 times.
✓ Branch 2 taken 8263 times.
47625 while (*i < end) {
79
6/6
✓ Branch 2 taken 1354 times.
✓ Branch 3 taken 38008 times.
✓ Branch 4 taken 912 times.
✓ Branch 5 taken 442 times.
✓ Branch 6 taken 912 times.
✓ Branch 7 taken 38450 times.
39362 if (Pathspec::IsSpecialChar(**i) && !next_is_escaped) {
80 912 break;
81 }
82
83
6/6
✓ Branch 1 taken 782 times.
✓ Branch 2 taken 37668 times.
✓ Branch 3 taken 646 times.
✓ Branch 4 taken 136 times.
✓ Branch 5 taken 646 times.
✓ Branch 6 taken 37804 times.
38450 if (**i == Pathspec::kEscaper && !next_is_escaped) {
84 646 next_is_escaped = true;
85
2/2
✓ Branch 0 taken 850 times.
✓ Branch 1 taken 36954 times.
37804 } else if (next_is_escaped) {
86
6/6
✓ Branch 2 taken 408 times.
✓ Branch 3 taken 442 times.
✓ Branch 5 taken 136 times.
✓ Branch 6 taken 272 times.
✓ Branch 7 taken 578 times.
✓ Branch 8 taken 272 times.
850 if (Pathspec::IsSpecialChar(**i) || **i == Pathspec::kEscaper) {
87 578 pattern->AddChar(**i);
88 578 next_is_escaped = false;
89 } else {
90 272 valid_ = false;
91 }
92 } else {
93
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 36954 times.
36954 assert(!Pathspec::IsSpecialChar(**i));
94 36954 pattern->AddChar(**i);
95 }
96
97 38450 ++(*i);
98 }
99
100 9175 return pattern;
101 }
102
103 3082 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 3082 times.
3082 assert(Pathspec::IsSpecialChar(**i));
106 3082 const char chr = **i;
107 3082 ++(*i);
108
109
2/3
✓ Branch 0 taken 1733 times.
✓ Branch 1 taken 1349 times.
✗ Branch 2 not taken.
3082 switch (chr) {
110 1733 case Pathspec::kWildcard:
111 1733 return new WildcardSubPattern();
112 1349 case Pathspec::kPlaceholder:
113 1349 return new PlaceholderSubPattern();
114 default:
115 assert(false && "unrecognized special character");
116 }
117 }
118
119 5474 std::string PathspecElementPattern::GenerateRegularExpression(
120 const bool is_relaxed) const {
121 5474 std::string result;
122 5474 SubPatterns::const_iterator i = subpatterns_.begin();
123 5474 const SubPatterns::const_iterator iend = subpatterns_.end();
124
2/2
✓ Branch 1 taken 7346 times.
✓ Branch 2 taken 5474 times.
12820 for (; i != iend; ++i) {
125
2/4
✓ Branch 2 taken 7346 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 7346 times.
✗ Branch 6 not taken.
7346 result += (*i)->GenerateRegularExpression(is_relaxed);
126 }
127 10948 return result;
128 }
129
130 1156 std::string PathspecElementPattern::GenerateGlobString() const {
131 1156 std::string result;
132 1156 SubPatterns::const_iterator i = subpatterns_.begin();
133 1156 const SubPatterns::const_iterator iend = subpatterns_.end();
134
2/2
✓ Branch 1 taken 1360 times.
✓ Branch 2 taken 1156 times.
2516 for (; i != iend; ++i) {
135
2/4
✓ Branch 2 taken 1360 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1360 times.
✗ Branch 6 not taken.
1360 result += (*i)->GenerateGlobString();
136 }
137 2312 return result;
138 }
139
140 3136 bool PathspecElementPattern::operator==(
141 const PathspecElementPattern &other) const {
142 3136 if (subpatterns_.size() != other.subpatterns_.size()
143
5/6
✓ Branch 0 taken 2802 times.
✓ Branch 1 taken 334 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 2802 times.
✓ Branch 6 taken 334 times.
✓ Branch 7 taken 2802 times.
3136 || IsValid() != other.IsValid()) {
144 334 return false;
145 }
146
147 2802 SubPatterns::const_iterator i = subpatterns_.begin();
148 2802 const SubPatterns::const_iterator iend = subpatterns_.end();
149 2802 SubPatterns::const_iterator j = other.subpatterns_.begin();
150 2802 const SubPatterns::const_iterator jend = other.subpatterns_.end();
151
152
5/6
✓ Branch 3 taken 3264 times.
✓ Branch 4 taken 2196 times.
✓ Branch 6 taken 3264 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 3264 times.
✓ Branch 9 taken 2196 times.
5460 for (; i != iend && j != jend; ++i, ++j) {
153
3/4
✓ Branch 3 taken 3264 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 606 times.
✓ Branch 6 taken 2658 times.
3264 if (!(*i)->Compare(*j)) {
154 606 return false;
155 }
156 }
157
158 2196 return true;
159 }
160
161
162 //------------------------------------------------------------------------------
163
164
165 37532 void PathspecElementPattern::PlaintextSubPattern::AddChar(const char chr) {
166 37532 chars_.push_back(chr);
167 37532 }
168
169 std::string
170 5403 PathspecElementPattern::PlaintextSubPattern::GenerateRegularExpression(
171 const bool is_relaxed) const {
172 // Note: strict and relaxed regex are the same!
173 5403 std::string::const_iterator i = chars_.begin();
174 5403 const std::string::const_iterator iend = chars_.end();
175 5403 std::string regex;
176
2/2
✓ Branch 2 taken 23048 times.
✓ Branch 3 taken 5403 times.
28451 for (; i != iend; ++i) {
177
2/2
✓ Branch 2 taken 1838 times.
✓ Branch 3 taken 21210 times.
23048 if (IsSpecialRegexCharacter(*i)) {
178
1/2
✓ Branch 1 taken 1838 times.
✗ Branch 2 not taken.
1838 regex += "\\";
179 }
180
1/2
✓ Branch 2 taken 23048 times.
✗ Branch 3 not taken.
23048 regex += *i;
181 }
182 10806 return regex;
183 }
184
185
186 1224 std::string PathspecElementPattern::PlaintextSubPattern::GenerateGlobString()
187 const {
188 1224 std::string::const_iterator i = chars_.begin();
189 1224 const std::string::const_iterator iend = chars_.end();
190 1224 std::string glob_string;
191
2/2
✓ Branch 2 taken 6256 times.
✓ Branch 3 taken 1224 times.
7480 for (; i != iend; ++i) {
192
2/2
✓ Branch 2 taken 136 times.
✓ Branch 3 taken 6120 times.
6256 if (Pathspec::IsSpecialChar(*i)) {
193
1/2
✓ Branch 1 taken 136 times.
✗ Branch 2 not taken.
136 glob_string += "\\";
194 }
195
1/2
✓ Branch 2 taken 6256 times.
✗ Branch 3 not taken.
6256 glob_string += *i;
196 }
197 2448 return glob_string;
198 }
199
200 23048 bool PathspecElementPattern::PlaintextSubPattern::IsSpecialRegexCharacter(
201 const char chr) const {
202
8/8
✓ Branch 0 taken 21856 times.
✓ Branch 1 taken 68 times.
✓ Branch 2 taken 21754 times.
✓ Branch 3 taken 102 times.
✓ Branch 4 taken 21618 times.
✓ Branch 5 taken 136 times.
✓ Branch 6 taken 21550 times.
✓ Branch 7 taken 68 times.
21924 return (chr == '.' || chr == '\\' || chr == '*' || chr == '?' || chr == '['
203
10/10
✓ Branch 0 taken 21482 times.
✓ Branch 1 taken 68 times.
✓ Branch 2 taken 21448 times.
✓ Branch 3 taken 34 times.
✓ Branch 4 taken 21414 times.
✓ Branch 5 taken 34 times.
✓ Branch 6 taken 21380 times.
✓ Branch 7 taken 34 times.
✓ Branch 8 taken 21346 times.
✓ Branch 9 taken 34 times.
21550 || chr == ']' || chr == '(' || chr == ')' || chr == '{' || chr == '}'
204
8/8
✓ Branch 0 taken 21924 times.
✓ Branch 1 taken 1124 times.
✓ Branch 2 taken 21312 times.
✓ Branch 3 taken 34 times.
✓ Branch 4 taken 21278 times.
✓ Branch 5 taken 34 times.
✓ Branch 6 taken 68 times.
✓ Branch 7 taken 21210 times.
44972 || chr == '^' || chr == '$' || chr == '+');
205 }
206
207 2401 bool PathspecElementPattern::PlaintextSubPattern::Compare(
208 const SubPattern *other) const {
209
2/2
✓ Branch 1 taken 33 times.
✓ Branch 2 taken 2368 times.
2401 if (!other->IsPlaintext()) {
210 33 return false;
211 }
212
213 const PlaintextSubPattern
214
1/2
✓ Branch 0 taken 2368 times.
✗ Branch 1 not taken.
2368 *pt_other = dynamic_cast<const PlaintextSubPattern *>(other);
215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2368 times.
2368 assert(pt_other != NULL);
216 2368 return chars_ == pt_other->chars_;
217 }
218
219
220 std::string
221 1229 PathspecElementPattern::WildcardSubPattern::GenerateRegularExpression(
222 const bool is_relaxed) const {
223 return (is_relaxed) ? std::string(".*")
224
14/27
✓ Branch 0 taken 377 times.
✓ Branch 1 taken 852 times.
✓ Branch 4 taken 377 times.
✗ Branch 5 not taken.
✓ Branch 8 taken 852 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 852 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 852 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 852 times.
✓ Branch 17 taken 377 times.
✗ Branch 18 not taken.
✓ Branch 19 taken 852 times.
✓ Branch 20 taken 377 times.
✓ Branch 22 taken 852 times.
✓ Branch 23 taken 377 times.
✓ Branch 25 taken 377 times.
✓ Branch 26 taken 852 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.
2458 : std::string("[^") + Pathspec::kSeparator + "]*";
225 }
226
227
228 68 std::string PathspecElementPattern::WildcardSubPattern::GenerateGlobString()
229 const {
230
1/2
✓ Branch 2 taken 68 times.
✗ Branch 3 not taken.
68 return "*";
231 }
232
233
234 467 bool PathspecElementPattern::WildcardSubPattern::Compare(
235 const SubPattern *other) const {
236 467 return other->IsWildcard();
237 }
238
239
240 std::string
241 714 PathspecElementPattern::PlaceholderSubPattern::GenerateRegularExpression(
242 const bool is_relaxed) const {
243 // Note: strict and relaxed regex are the same!
244
3/6
✓ Branch 2 taken 714 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 714 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 714 times.
✗ Branch 9 not taken.
1428 return std::string("[^") + Pathspec::kSeparator + "]";
245 }
246
247 68 std::string PathspecElementPattern::PlaceholderSubPattern::GenerateGlobString()
248 const {
249
1/2
✓ Branch 2 taken 68 times.
✗ Branch 3 not taken.
68 return "?";
250 }
251
252 396 bool PathspecElementPattern::PlaceholderSubPattern::Compare(
253 const SubPattern *other) const {
254 396 return other->IsPlaceholder();
255 }
256