GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/pathspec/pathspec_pattern.cc
Date: 2026-11-08 02:41:18
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 7806 PathspecElementPattern::PathspecElementPattern(
13 const std::string::const_iterator begin,
14 7806 const std::string::const_iterator &end)
15 7806 : valid_(true) {
16
1/2
✓ Branch 1 taken 7806 times.
✗ Branch 2 not taken.
7806 Parse(begin, end);
17 7806 }
18
19 24267 PathspecElementPattern::PathspecElementPattern(
20 24267 const PathspecElementPattern &other)
21 24267 : valid_(other.valid_) {
22
1/2
✓ Branch 2 taken 24267 times.
✗ Branch 3 not taken.
24267 subpatterns_.reserve(other.subpatterns_.size());
23 24267 SubPatterns::const_iterator i = other.subpatterns_.begin();
24 24267 const SubPatterns::const_iterator iend = other.subpatterns_.end();
25
2/2
✓ Branch 1 taken 26316 times.
✓ Branch 2 taken 24267 times.
50583 for (; i != iend; ++i) {
26
2/4
✓ Branch 2 taken 26316 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 26316 times.
✗ Branch 6 not taken.
26316 subpatterns_.push_back((*i)->Clone());
27 }
28 24267 }
29
30 126 PathspecElementPattern &PathspecElementPattern::operator=(
31 const PathspecElementPattern &other) {
32
1/2
✓ Branch 0 taken 126 times.
✗ Branch 1 not taken.
126 if (this != &other) {
33 126 valid_ = other.valid_;
34 126 subpatterns_.clear();
35
1/2
✓ Branch 2 taken 126 times.
✗ Branch 3 not taken.
126 subpatterns_.reserve(other.subpatterns_.size());
36 126 SubPatterns::const_iterator i = other.subpatterns_.begin();
37 126 const SubPatterns::const_iterator iend = other.subpatterns_.end();
38
2/2
✓ Branch 1 taken 198 times.
✓ Branch 2 taken 126 times.
324 for (; i != iend; ++i) {
39
2/4
✓ Branch 2 taken 198 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 198 times.
✗ Branch 6 not taken.
198 subpatterns_.push_back((*i)->Clone());
40 }
41 }
42
43 126 return *this;
44 }
45
46
47 32067 PathspecElementPattern::~PathspecElementPattern() {
48 32067 SubPatterns::const_iterator i = subpatterns_.begin();
49 32067 const SubPatterns::const_iterator iend = subpatterns_.end();
50
2/2
✓ Branch 2 taken 35388 times.
✓ Branch 3 taken 32067 times.
67455 for (; i != iend; ++i) {
51
1/2
✓ Branch 1 taken 35388 times.
✗ Branch 2 not taken.
35388 delete *i;
52 }
53 32067 subpatterns_.clear();
54 32067 }
55
56
57 7806 void PathspecElementPattern::Parse(const std::string::const_iterator &begin,
58 const std::string::const_iterator &end) {
59 7806 std::string::const_iterator i = begin;
60
2/2
✓ Branch 1 taken 9019 times.
✓ Branch 2 taken 7806 times.
16825 while (i != end) {
61
3/4
✓ Branch 2 taken 1589 times.
✓ Branch 3 taken 7430 times.
✓ Branch 5 taken 1589 times.
✗ Branch 6 not taken.
9019 SubPattern *next = (Pathspec::IsSpecialChar(*i)) ? ParseSpecialChar(end, &i)
62
1/2
✓ Branch 1 taken 7430 times.
✗ Branch 2 not taken.
7430 : ParsePlaintext(end, &i);
63
3/4
✓ Branch 1 taken 9019 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 13 times.
✓ Branch 4 taken 9006 times.
9019 if (next->IsEmpty()) {
64 13 valid_ = false;
65
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 delete next;
66 } else {
67
1/2
✓ Branch 1 taken 9006 times.
✗ Branch 2 not taken.
9006 subpatterns_.push_back(next);
68 }
69 }
70 7806 }
71
72
73 7430 PathspecElementPattern::SubPattern *PathspecElementPattern::ParsePlaintext(
74 const std::string::const_iterator &end, std::string::const_iterator *i) {
75 7430 PlaintextSubPattern *pattern = new PlaintextSubPattern();
76 7430 bool next_is_escaped = false;
77
78
2/2
✓ Branch 1 taken 32332 times.
✓ Branch 2 taken 6963 times.
39295 while (*i < end) {
79
6/6
✓ Branch 2 taken 701 times.
✓ Branch 3 taken 31631 times.
✓ Branch 4 taken 467 times.
✓ Branch 5 taken 234 times.
✓ Branch 6 taken 467 times.
✓ Branch 7 taken 31865 times.
32332 if (Pathspec::IsSpecialChar(**i) && !next_is_escaped) {
80 467 break;
81 }
82
83
6/6
✓ Branch 1 taken 409 times.
✓ Branch 2 taken 31456 times.
✓ Branch 3 taken 337 times.
✓ Branch 4 taken 72 times.
✓ Branch 5 taken 337 times.
✓ Branch 6 taken 31528 times.
31865 if (**i == Pathspec::kEscaper && !next_is_escaped) {
84 337 next_is_escaped = true;
85
2/2
✓ Branch 0 taken 435 times.
✓ Branch 1 taken 31093 times.
31528 } else if (next_is_escaped) {
86
6/6
✓ Branch 2 taken 201 times.
✓ Branch 3 taken 234 times.
✓ Branch 5 taken 72 times.
✓ Branch 6 taken 129 times.
✓ Branch 7 taken 306 times.
✓ Branch 8 taken 129 times.
435 if (Pathspec::IsSpecialChar(**i) || **i == Pathspec::kEscaper) {
87 306 pattern->AddChar(**i);
88 306 next_is_escaped = false;
89 } else {
90 129 valid_ = false;
91 }
92 } else {
93
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 31093 times.
31093 assert(!Pathspec::IsSpecialChar(**i));
94 31093 pattern->AddChar(**i);
95 }
96
97 31865 ++(*i);
98 }
99
100 7430 return pattern;
101 }
102
103 1589 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 1589 times.
1589 assert(Pathspec::IsSpecialChar(**i));
106 1589 const char chr = **i;
107 1589 ++(*i);
108
109
2/3
✓ Branch 0 taken 909 times.
✓ Branch 1 taken 680 times.
✗ Branch 2 not taken.
1589 switch (chr) {
110 909 case Pathspec::kWildcard:
111 909 return new WildcardSubPattern();
112 680 case Pathspec::kPlaceholder:
113 680 return new PlaceholderSubPattern();
114 ✗ default:
115 ✗ assert(false && "unrecognized special character");
116 }
117 }
118
119 5199 std::string PathspecElementPattern::GenerateRegularExpression(
120 const bool is_relaxed) const {
121 5199 std::string result;
122 5199 SubPatterns::const_iterator i = subpatterns_.begin();
123 5199 const SubPatterns::const_iterator iend = subpatterns_.end();
124
2/2
✓ Branch 1 taken 6166 times.
✓ Branch 2 taken 5199 times.
11365 for (; i != iend; ++i) {
125
2/4
✓ Branch 2 taken 6166 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 6166 times.
✗ Branch 6 not taken.
6166 result += (*i)->GenerateRegularExpression(is_relaxed);
126 }
127 10398 return result;
128 }
129
130 608 std::string PathspecElementPattern::GenerateGlobString() const {
131 608 std::string result;
132 608 SubPatterns::const_iterator i = subpatterns_.begin();
133 608 const SubPatterns::const_iterator iend = subpatterns_.end();
134
2/2
✓ Branch 1 taken 713 times.
✓ Branch 2 taken 608 times.
1321 for (; i != iend; ++i) {
135
2/4
✓ Branch 2 taken 713 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 713 times.
✗ Branch 6 not taken.
713 result += (*i)->GenerateGlobString();
136 }
137 1216 return result;
138 }
139
140 1666 bool PathspecElementPattern::operator==(
141 const PathspecElementPattern &other) const {
142 1666 if (subpatterns_.size() != other.subpatterns_.size()
143
5/6
✓ Branch 0 taken 1506 times.
✓ Branch 1 taken 160 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1506 times.
✓ Branch 6 taken 160 times.
✓ Branch 7 taken 1506 times.
1666 || IsValid() != other.IsValid()) {
144 160 return false;
145 }
146
147 1506 SubPatterns::const_iterator i = subpatterns_.begin();
148 1506 const SubPatterns::const_iterator iend = subpatterns_.end();
149 1506 SubPatterns::const_iterator j = other.subpatterns_.begin();
150 1506 const SubPatterns::const_iterator jend = other.subpatterns_.end();
151
152
5/6
✓ Branch 3 taken 1758 times.
✓ Branch 4 taken 1152 times.
✓ Branch 6 taken 1758 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 1758 times.
✓ Branch 9 taken 1152 times.
2910 for (; i != iend && j != jend; ++i, ++j) {
153
3/4
✓ Branch 3 taken 1758 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 354 times.
✓ Branch 6 taken 1404 times.
1758 if (!(*i)->Compare(*j)) {
154 354 return false;
155 }
156 }
157
158 1152 return true;
159 }
160
161
162 //------------------------------------------------------------------------------
163
164
165 31399 void PathspecElementPattern::PlaintextSubPattern::AddChar(const char chr) {
166 31399 chars_.push_back(chr);
167 31399 }
168
169 std::string
170 5124 PathspecElementPattern::PlaintextSubPattern::GenerateRegularExpression(
171 const bool is_relaxed) const {
172 // Note: strict and relaxed regex are the same!
173 5124 std::string::const_iterator i = chars_.begin();
174 5124 const std::string::const_iterator iend = chars_.end();
175 5124 std::string regex;
176
2/2
✓ Branch 2 taken 22782 times.
✓ Branch 3 taken 5124 times.
27906 for (; i != iend; ++i) {
177
2/2
✓ Branch 2 taken 954 times.
✓ Branch 3 taken 21828 times.
22782 if (IsSpecialRegexCharacter(*i)) {
178
1/2
✓ Branch 1 taken 954 times.
✗ Branch 2 not taken.
954 regex += "\\";
179 }
180
1/2
✓ Branch 2 taken 22782 times.
✗ Branch 3 not taken.
22782 regex += *i;
181 }
182 10248 return regex;
183 }
184
185
186 643 std::string PathspecElementPattern::PlaintextSubPattern::GenerateGlobString()
187 const {
188 643 std::string::const_iterator i = chars_.begin();
189 643 const std::string::const_iterator iend = chars_.end();
190 643 std::string glob_string;
191
2/2
✓ Branch 2 taken 3292 times.
✓ Branch 3 taken 643 times.
3935 for (; i != iend; ++i) {
192
2/2
✓ Branch 2 taken 72 times.
✓ Branch 3 taken 3220 times.
3292 if (Pathspec::IsSpecialChar(*i)) {
193
1/2
✓ Branch 1 taken 72 times.
✗ Branch 2 not taken.
72 glob_string += "\\";
194 }
195
1/2
✓ Branch 2 taken 3292 times.
✗ Branch 3 not taken.
3292 glob_string += *i;
196 }
197 1286 return glob_string;
198 }
199
200 22782 bool PathspecElementPattern::PlaintextSubPattern::IsSpecialRegexCharacter(
201 const char chr) const {
202
8/8
✓ Branch 0 taken 22170 times.
✓ Branch 1 taken 36 times.
✓ Branch 2 taken 22116 times.
✓ Branch 3 taken 54 times.
✓ Branch 4 taken 22044 times.
✓ Branch 5 taken 72 times.
✓ Branch 6 taken 22008 times.
✓ Branch 7 taken 36 times.
22206 return (chr == '.' || chr == '\\' || chr == '*' || chr == '?' || chr == '['
203
10/10
✓ Branch 0 taken 21972 times.
✓ Branch 1 taken 36 times.
✓ Branch 2 taken 21954 times.
✓ Branch 3 taken 18 times.
✓ Branch 4 taken 21936 times.
✓ Branch 5 taken 18 times.
✓ Branch 6 taken 21918 times.
✓ Branch 7 taken 18 times.
✓ Branch 8 taken 21900 times.
✓ Branch 9 taken 18 times.
22008 || chr == ']' || chr == '(' || chr == ')' || chr == '{' || chr == '}'
204
8/8
✓ Branch 0 taken 22206 times.
✓ Branch 1 taken 576 times.
✓ Branch 2 taken 21882 times.
✓ Branch 3 taken 18 times.
✓ Branch 4 taken 21864 times.
✓ Branch 5 taken 18 times.
✓ Branch 6 taken 36 times.
✓ Branch 7 taken 21828 times.
44988 || chr == '^' || chr == '$' || chr == '+');
205 }
206
207 1315 bool PathspecElementPattern::PlaintextSubPattern::Compare(
208 const SubPattern *other) const {
209
2/2
✓ Branch 1 taken 18 times.
✓ Branch 2 taken 1297 times.
1315 if (!other->IsPlaintext()) {
210 18 return false;
211 }
212
213 const PlaintextSubPattern
214
1/2
✓ Branch 0 taken 1297 times.
✗ Branch 1 not taken.
1297 *pt_other = dynamic_cast<const PlaintextSubPattern *>(other);
215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1297 times.
1297 assert(pt_other != NULL);
216 1297 return chars_ == pt_other->chars_;
217 }
218
219
220 std::string
221 680 PathspecElementPattern::WildcardSubPattern::GenerateRegularExpression(
222 const bool is_relaxed) const {
223 return (is_relaxed) ? std::string(".*")
224
14/27
✓ Branch 0 taken 207 times.
✓ Branch 1 taken 473 times.
✓ Branch 4 taken 207 times.
✗ Branch 5 not taken.
✓ Branch 8 taken 473 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 473 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 473 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 473 times.
✓ Branch 17 taken 207 times.
✗ Branch 18 not taken.
✓ Branch 19 taken 473 times.
✓ Branch 20 taken 207 times.
✓ Branch 22 taken 473 times.
✓ Branch 23 taken 207 times.
✓ Branch 25 taken 207 times.
✓ Branch 26 taken 473 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.
1360 : std::string("[^") + Pathspec::kSeparator + "]*";
225 }
226
227
228 35 std::string PathspecElementPattern::WildcardSubPattern::GenerateGlobString()
229 const {
230
1/2
✓ Branch 2 taken 35 times.
✗ Branch 3 not taken.
35 return "*";
231 }
232
233
234 227 bool PathspecElementPattern::WildcardSubPattern::Compare(
235 const SubPattern *other) const {
236 227 return other->IsWildcard();
237 }
238
239
240 std::string
241 362 PathspecElementPattern::PlaceholderSubPattern::GenerateRegularExpression(
242 const bool is_relaxed) const {
243 // Note: strict and relaxed regex are the same!
244
3/6
✓ Branch 2 taken 362 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 362 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 362 times.
✗ Branch 9 not taken.
724 return std::string("[^") + Pathspec::kSeparator + "]";
245 }
246
247 35 std::string PathspecElementPattern::PlaceholderSubPattern::GenerateGlobString()
248 const {
249
1/2
✓ Branch 2 taken 35 times.
✗ Branch 3 not taken.
35 return "?";
250 }
251
252 216 bool PathspecElementPattern::PlaceholderSubPattern::Compare(
253 const SubPattern *other) const {
254 216 return other->IsPlaceholder();
255 }
256