GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/pathspec/pathspec_pattern.cc
Date: 2026-02-22 02:35:58
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 6436 PathspecElementPattern::PathspecElementPattern(
13 const std::string::const_iterator begin,
14 6436 const std::string::const_iterator &end)
15 6436 : valid_(true) {
16
1/2
✓ Branch 1 taken 6436 times.
✗ Branch 2 not taken.
6436 Parse(begin, end);
17 6436 }
18
19 16569 PathspecElementPattern::PathspecElementPattern(
20 16569 const PathspecElementPattern &other)
21 16569 : valid_(other.valid_) {
22
1/2
✓ Branch 2 taken 16569 times.
✗ Branch 3 not taken.
16569 subpatterns_.reserve(other.subpatterns_.size());
23 16569 SubPatterns::const_iterator i = other.subpatterns_.begin();
24 16569 const SubPatterns::const_iterator iend = other.subpatterns_.end();
25
2/2
✓ Branch 1 taken 18731 times.
✓ Branch 2 taken 16569 times.
35300 for (; i != iend; ++i) {
26
2/4
✓ Branch 2 taken 18731 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 18731 times.
✗ Branch 6 not taken.
18731 subpatterns_.push_back((*i)->Clone());
27 }
28 16569 }
29
30 154 PathspecElementPattern &PathspecElementPattern::operator=(
31 const PathspecElementPattern &other) {
32
1/2
✓ Branch 0 taken 154 times.
✗ Branch 1 not taken.
154 if (this != &other) {
33 154 valid_ = other.valid_;
34 154 subpatterns_.clear();
35
1/2
✓ Branch 2 taken 154 times.
✗ Branch 3 not taken.
154 subpatterns_.reserve(other.subpatterns_.size());
36 154 SubPatterns::const_iterator i = other.subpatterns_.begin();
37 154 const SubPatterns::const_iterator iend = other.subpatterns_.end();
38
2/2
✓ Branch 1 taken 242 times.
✓ Branch 2 taken 154 times.
396 for (; i != iend; ++i) {
39
2/4
✓ Branch 2 taken 242 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 242 times.
✗ Branch 6 not taken.
242 subpatterns_.push_back((*i)->Clone());
40 }
41 }
42
43 154 return *this;
44 }
45
46
47 22999 PathspecElementPattern::~PathspecElementPattern() {
48 22999 SubPatterns::const_iterator i = subpatterns_.begin();
49 22999 const SubPatterns::const_iterator iend = subpatterns_.end();
50
2/2
✓ Branch 2 taken 26626 times.
✓ Branch 3 taken 22999 times.
49625 for (; i != iend; ++i) {
51
1/2
✓ Branch 1 taken 26626 times.
✗ Branch 2 not taken.
26626 delete *i;
52 }
53 22999 subpatterns_.clear();
54 22999 }
55
56
57 6436 void PathspecElementPattern::Parse(const std::string::const_iterator &begin,
58 const std::string::const_iterator &end) {
59 6436 std::string::const_iterator i = begin;
60
2/2
✓ Branch 1 taken 7814 times.
✓ Branch 2 taken 6436 times.
14250 while (i != end) {
61
3/4
✓ Branch 2 taken 1526 times.
✓ Branch 3 taken 6288 times.
✓ Branch 5 taken 1526 times.
✗ Branch 6 not taken.
7814 SubPattern *next = (Pathspec::IsSpecialChar(*i)) ? ParseSpecialChar(end, &i)
62
1/2
✓ Branch 1 taken 6288 times.
✗ Branch 2 not taken.
6288 : ParsePlaintext(end, &i);
63
3/4
✓ Branch 1 taken 7814 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 7813 times.
7814 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 7813 times.
✗ Branch 2 not taken.
7813 subpatterns_.push_back(next);
68 }
69 }
70 6436 }
71
72
73 6288 PathspecElementPattern::SubPattern *PathspecElementPattern::ParsePlaintext(
74 const std::string::const_iterator &end, std::string::const_iterator *i) {
75 6288 PlaintextSubPattern *pattern = new PlaintextSubPattern();
76 6288 bool next_is_escaped = false;
77
78
2/2
✓ Branch 1 taken 28351 times.
✓ Branch 2 taken 5768 times.
34119 while (*i < end) {
79
6/6
✓ Branch 2 taken 806 times.
✓ Branch 3 taken 27545 times.
✓ Branch 4 taken 520 times.
✓ Branch 5 taken 286 times.
✓ Branch 6 taken 520 times.
✓ Branch 7 taken 27831 times.
28351 if (Pathspec::IsSpecialChar(**i) && !next_is_escaped) {
80 520 break;
81 }
82
83
6/6
✓ Branch 1 taken 485 times.
✓ Branch 2 taken 27346 times.
✓ Branch 3 taken 397 times.
✓ Branch 4 taken 88 times.
✓ Branch 5 taken 397 times.
✓ Branch 6 taken 27434 times.
27831 if (**i == Pathspec::kEscaper && !next_is_escaped) {
84 397 next_is_escaped = true;
85
2/2
✓ Branch 0 taken 487 times.
✓ Branch 1 taken 26947 times.
27434 } else if (next_is_escaped) {
86
6/6
✓ Branch 2 taken 201 times.
✓ Branch 3 taken 286 times.
✓ Branch 5 taken 88 times.
✓ Branch 6 taken 113 times.
✓ Branch 7 taken 374 times.
✓ Branch 8 taken 113 times.
487 if (Pathspec::IsSpecialChar(**i) || **i == Pathspec::kEscaper) {
87 374 pattern->AddChar(**i);
88 374 next_is_escaped = false;
89 } else {
90 113 valid_ = false;
91 }
92 } else {
93
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 26947 times.
26947 assert(!Pathspec::IsSpecialChar(**i));
94 26947 pattern->AddChar(**i);
95 }
96
97 27831 ++(*i);
98 }
99
100 6288 return pattern;
101 }
102
103 1526 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 1526 times.
1526 assert(Pathspec::IsSpecialChar(**i));
106 1526 const char chr = **i;
107 1526 ++(*i);
108
109
2/3
✓ Branch 0 taken 810 times.
✓ Branch 1 taken 716 times.
✗ Branch 2 not taken.
1526 switch (chr) {
110 810 case Pathspec::kWildcard:
111 810 return new WildcardSubPattern();
112 716 case Pathspec::kPlaceholder:
113 716 return new PlaceholderSubPattern();
114 default:
115 assert(false && "unrecognized special character");
116 }
117 }
118
119 4550 std::string PathspecElementPattern::GenerateRegularExpression(
120 const bool is_relaxed) const {
121 4550 std::string result;
122 4550 SubPatterns::const_iterator i = subpatterns_.begin();
123 4550 const SubPatterns::const_iterator iend = subpatterns_.end();
124
2/2
✓ Branch 1 taken 5701 times.
✓ Branch 2 taken 4550 times.
10251 for (; i != iend; ++i) {
125
2/4
✓ Branch 2 taken 5701 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 5701 times.
✗ Branch 6 not taken.
5701 result += (*i)->GenerateRegularExpression(is_relaxed);
126 }
127 9100 return result;
128 }
129
130 748 std::string PathspecElementPattern::GenerateGlobString() const {
131 748 std::string result;
132 748 SubPatterns::const_iterator i = subpatterns_.begin();
133 748 const SubPatterns::const_iterator iend = subpatterns_.end();
134
2/2
✓ Branch 1 taken 880 times.
✓ Branch 2 taken 748 times.
1628 for (; i != iend; ++i) {
135
2/4
✓ Branch 2 taken 880 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 880 times.
✗ Branch 6 not taken.
880 result += (*i)->GenerateGlobString();
136 }
137 1496 return result;
138 }
139
140 1670 bool PathspecElementPattern::operator==(
141 const PathspecElementPattern &other) const {
142 1670 if (subpatterns_.size() != other.subpatterns_.size()
143
5/6
✓ Branch 0 taken 1540 times.
✓ Branch 1 taken 130 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1540 times.
✓ Branch 6 taken 130 times.
✓ Branch 7 taken 1540 times.
1670 || IsValid() != other.IsValid()) {
144 130 return false;
145 }
146
147 1540 SubPatterns::const_iterator i = subpatterns_.begin();
148 1540 const SubPatterns::const_iterator iend = subpatterns_.end();
149 1540 SubPatterns::const_iterator j = other.subpatterns_.begin();
150 1540 const SubPatterns::const_iterator jend = other.subpatterns_.end();
151
152
5/6
✓ Branch 3 taken 1834 times.
✓ Branch 4 taken 1167 times.
✓ Branch 6 taken 1834 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 1834 times.
✓ Branch 9 taken 1167 times.
3001 for (; i != iend && j != jend; ++i, ++j) {
153
3/4
✓ Branch 3 taken 1834 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 373 times.
✓ Branch 6 taken 1461 times.
1834 if (!(*i)->Compare(*j)) {
154 373 return false;
155 }
156 }
157
158 1167 return true;
159 }
160
161
162 //------------------------------------------------------------------------------
163
164
165 27321 void PathspecElementPattern::PlaintextSubPattern::AddChar(const char chr) {
166 27321 chars_.push_back(chr);
167 27321 }
168
169 std::string
170 4570 PathspecElementPattern::PlaintextSubPattern::GenerateRegularExpression(
171 const bool is_relaxed) const {
172 // Note: strict and relaxed regex are the same!
173 4570 std::string::const_iterator i = chars_.begin();
174 4570 const std::string::const_iterator iend = chars_.end();
175 4570 std::string regex;
176
2/2
✓ Branch 2 taken 20516 times.
✓ Branch 3 taken 4570 times.
25086 for (; i != iend; ++i) {
177
2/2
✓ Branch 2 taken 1115 times.
✓ Branch 3 taken 19401 times.
20516 if (IsSpecialRegexCharacter(*i)) {
178
1/2
✓ Branch 1 taken 1115 times.
✗ Branch 2 not taken.
1115 regex += "\\";
179 }
180
1/2
✓ Branch 2 taken 20516 times.
✗ Branch 3 not taken.
20516 regex += *i;
181 }
182 9140 return regex;
183 }
184
185
186 792 std::string PathspecElementPattern::PlaintextSubPattern::GenerateGlobString()
187 const {
188 792 std::string::const_iterator i = chars_.begin();
189 792 const std::string::const_iterator iend = chars_.end();
190 792 std::string glob_string;
191
2/2
✓ Branch 2 taken 4048 times.
✓ Branch 3 taken 792 times.
4840 for (; i != iend; ++i) {
192
2/2
✓ Branch 2 taken 88 times.
✓ Branch 3 taken 3960 times.
4048 if (Pathspec::IsSpecialChar(*i)) {
193
1/2
✓ Branch 1 taken 88 times.
✗ Branch 2 not taken.
88 glob_string += "\\";
194 }
195
1/2
✓ Branch 2 taken 4048 times.
✗ Branch 3 not taken.
4048 glob_string += *i;
196 }
197 1584 return glob_string;
198 }
199
200 20516 bool PathspecElementPattern::PlaintextSubPattern::IsSpecialRegexCharacter(
201 const char chr) const {
202
8/8
✓ Branch 0 taken 19819 times.
✓ Branch 1 taken 44 times.
✓ Branch 2 taken 19753 times.
✓ Branch 3 taken 66 times.
✓ Branch 4 taken 19665 times.
✓ Branch 5 taken 88 times.
✓ Branch 6 taken 19621 times.
✓ Branch 7 taken 44 times.
19863 return (chr == '.' || chr == '\\' || chr == '*' || chr == '?' || chr == '['
203
10/10
✓ Branch 0 taken 19577 times.
✓ Branch 1 taken 44 times.
✓ Branch 2 taken 19555 times.
✓ Branch 3 taken 22 times.
✓ Branch 4 taken 19533 times.
✓ Branch 5 taken 22 times.
✓ Branch 6 taken 19511 times.
✓ Branch 7 taken 22 times.
✓ Branch 8 taken 19489 times.
✓ Branch 9 taken 22 times.
19621 || chr == ']' || chr == '(' || chr == ')' || chr == '{' || chr == '}'
204
8/8
✓ Branch 0 taken 19863 times.
✓ Branch 1 taken 653 times.
✓ Branch 2 taken 19467 times.
✓ Branch 3 taken 22 times.
✓ Branch 4 taken 19445 times.
✓ Branch 5 taken 22 times.
✓ Branch 6 taken 44 times.
✓ Branch 7 taken 19401 times.
40379 || chr == '^' || chr == '$' || chr == '+');
205 }
206
207 1388 bool PathspecElementPattern::PlaintextSubPattern::Compare(
208 const SubPattern *other) const {
209
2/2
✓ Branch 1 taken 21 times.
✓ Branch 2 taken 1367 times.
1388 if (!other->IsPlaintext()) {
210 21 return false;
211 }
212
213 const PlaintextSubPattern
214
1/2
✓ Branch 0 taken 1367 times.
✗ Branch 1 not taken.
1367 *pt_other = dynamic_cast<const PlaintextSubPattern *>(other);
215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1367 times.
1367 assert(pt_other != NULL);
216 1367 return chars_ == pt_other->chars_;
217 }
218
219
220 std::string
221 696 PathspecElementPattern::WildcardSubPattern::GenerateRegularExpression(
222 const bool is_relaxed) const {
223 return (is_relaxed) ? std::string(".*")
224
14/27
✓ Branch 0 taken 218 times.
✓ Branch 1 taken 478 times.
✓ Branch 4 taken 218 times.
✗ Branch 5 not taken.
✓ Branch 8 taken 478 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 478 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 478 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 478 times.
✓ Branch 17 taken 218 times.
✗ Branch 18 not taken.
✓ Branch 19 taken 478 times.
✓ Branch 20 taken 218 times.
✓ Branch 22 taken 478 times.
✓ Branch 23 taken 218 times.
✓ Branch 25 taken 218 times.
✓ Branch 26 taken 478 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.
1392 : std::string("[^") + Pathspec::kSeparator + "]*";
225 }
226
227
228 44 std::string PathspecElementPattern::WildcardSubPattern::GenerateGlobString()
229 const {
230
1/2
✓ Branch 2 taken 44 times.
✗ Branch 3 not taken.
44 return "*";
231 }
232
233
234 194 bool PathspecElementPattern::WildcardSubPattern::Compare(
235 const SubPattern *other) const {
236 194 return other->IsWildcard();
237 }
238
239
240 std::string
241 435 PathspecElementPattern::PlaceholderSubPattern::GenerateRegularExpression(
242 const bool is_relaxed) const {
243 // Note: strict and relaxed regex are the same!
244
3/6
✓ Branch 2 taken 435 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 435 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 435 times.
✗ Branch 9 not taken.
870 return std::string("[^") + Pathspec::kSeparator + "]";
245 }
246
247 44 std::string PathspecElementPattern::PlaceholderSubPattern::GenerateGlobString()
248 const {
249
1/2
✓ Branch 2 taken 44 times.
✗ Branch 3 not taken.
44 return "?";
250 }
251
252 252 bool PathspecElementPattern::PlaceholderSubPattern::Compare(
253 const SubPattern *other) const {
254 252 return other->IsPlaceholder();
255 }
256