GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/pathspec/pathspec_pattern.cc
Date: 2026-07-12 02:35:48
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 14747 PathspecElementPattern::PathspecElementPattern(
13 const std::string::const_iterator begin,
14 14747 const std::string::const_iterator &end)
15 14747 : valid_(true) {
16
1/2
✓ Branch 1 taken 14747 times.
✗ Branch 2 not taken.
14747 Parse(begin, end);
17 14747 }
18
19 47871 PathspecElementPattern::PathspecElementPattern(
20 47871 const PathspecElementPattern &other)
21 47871 : valid_(other.valid_) {
22
1/2
✓ Branch 2 taken 47871 times.
✗ Branch 3 not taken.
47871 subpatterns_.reserve(other.subpatterns_.size());
23 47871 SubPatterns::const_iterator i = other.subpatterns_.begin();
24 47871 const SubPatterns::const_iterator iend = other.subpatterns_.end();
25
2/2
✓ Branch 1 taken 52026 times.
✓ Branch 2 taken 47871 times.
99897 for (; i != iend; ++i) {
26
2/4
✓ Branch 2 taken 52026 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 52026 times.
✗ Branch 6 not taken.
52026 subpatterns_.push_back((*i)->Clone());
27 }
28 47871 }
29
30 217 PathspecElementPattern &PathspecElementPattern::operator=(
31 const PathspecElementPattern &other) {
32
1/2
✓ Branch 0 taken 217 times.
✗ Branch 1 not taken.
217 if (this != &other) {
33 217 valid_ = other.valid_;
34 217 subpatterns_.clear();
35
1/2
✓ Branch 2 taken 217 times.
✗ Branch 3 not taken.
217 subpatterns_.reserve(other.subpatterns_.size());
36 217 SubPatterns::const_iterator i = other.subpatterns_.begin();
37 217 const SubPatterns::const_iterator iend = other.subpatterns_.end();
38
2/2
✓ Branch 1 taken 341 times.
✓ Branch 2 taken 217 times.
558 for (; i != iend; ++i) {
39
2/4
✓ Branch 2 taken 341 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 341 times.
✗ Branch 6 not taken.
341 subpatterns_.push_back((*i)->Clone());
40 }
41 }
42
43 217 return *this;
44 }
45
46
47 62612 PathspecElementPattern::~PathspecElementPattern() {
48 62612 SubPatterns::const_iterator i = subpatterns_.begin();
49 62612 const SubPatterns::const_iterator iend = subpatterns_.end();
50
2/2
✓ Branch 2 taken 69131 times.
✓ Branch 3 taken 62612 times.
131743 for (; i != iend; ++i) {
51
1/2
✓ Branch 1 taken 69131 times.
✗ Branch 2 not taken.
69131 delete *i;
52 }
53 62612 subpatterns_.clear();
54 62612 }
55
56
57 14747 void PathspecElementPattern::Parse(const std::string::const_iterator &begin,
58 const std::string::const_iterator &end) {
59 14747 std::string::const_iterator i = begin;
60
2/2
✓ Branch 1 taken 17036 times.
✓ Branch 2 taken 14747 times.
31783 while (i != end) {
61
3/4
✓ Branch 2 taken 3460 times.
✓ Branch 3 taken 13576 times.
✓ Branch 5 taken 3460 times.
✗ Branch 6 not taken.
17036 SubPattern *next = (Pathspec::IsSpecialChar(*i)) ? ParseSpecialChar(end, &i)
62
1/2
✓ Branch 1 taken 13576 times.
✗ Branch 2 not taken.
13576 : ParsePlaintext(end, &i);
63
3/4
✓ Branch 1 taken 17036 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 49 times.
✓ Branch 4 taken 16987 times.
17036 if (next->IsEmpty()) {
64 49 valid_ = false;
65
1/2
✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
49 delete next;
66 } else {
67
1/2
✓ Branch 1 taken 16987 times.
✗ Branch 2 not taken.
16987 subpatterns_.push_back(next);
68 }
69 }
70 14747 }
71
72
73 13576 PathspecElementPattern::SubPattern *PathspecElementPattern::ParsePlaintext(
74 const std::string::const_iterator &end, std::string::const_iterator *i) {
75 13576 PlaintextSubPattern *pattern = new PlaintextSubPattern();
76 13576 bool next_is_escaped = false;
77
78
2/2
✓ Branch 1 taken 62572 times.
✓ Branch 2 taken 12685 times.
75257 while (*i < end) {
79
6/6
✓ Branch 2 taken 1294 times.
✓ Branch 3 taken 61278 times.
✓ Branch 4 taken 891 times.
✓ Branch 5 taken 403 times.
✓ Branch 6 taken 891 times.
✓ Branch 7 taken 61681 times.
62572 if (Pathspec::IsSpecialChar(**i) && !next_is_escaped) {
80 891 break;
81 }
82
83
6/6
✓ Branch 1 taken 731 times.
✓ Branch 2 taken 60950 times.
✓ Branch 3 taken 607 times.
✓ Branch 4 taken 124 times.
✓ Branch 5 taken 607 times.
✓ Branch 6 taken 61074 times.
61681 if (**i == Pathspec::kEscaper && !next_is_escaped) {
84 607 next_is_escaped = true;
85
2/2
✓ Branch 0 taken 829 times.
✓ Branch 1 taken 60245 times.
61074 } else if (next_is_escaped) {
86
6/6
✓ Branch 2 taken 426 times.
✓ Branch 3 taken 403 times.
✓ Branch 5 taken 124 times.
✓ Branch 6 taken 302 times.
✓ Branch 7 taken 527 times.
✓ Branch 8 taken 302 times.
829 if (Pathspec::IsSpecialChar(**i) || **i == Pathspec::kEscaper) {
87 527 pattern->AddChar(**i);
88 527 next_is_escaped = false;
89 } else {
90 302 valid_ = false;
91 }
92 } else {
93
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 60245 times.
60245 assert(!Pathspec::IsSpecialChar(**i));
94 60245 pattern->AddChar(**i);
95 }
96
97 61681 ++(*i);
98 }
99
100 13576 return pattern;
101 }
102
103 3460 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 3460 times.
3460 assert(Pathspec::IsSpecialChar(**i));
106 3460 const char chr = **i;
107 3460 ++(*i);
108
109
2/3
✓ Branch 0 taken 2094 times.
✓ Branch 1 taken 1366 times.
✗ Branch 2 not taken.
3460 switch (chr) {
110 2094 case Pathspec::kWildcard:
111 2094 return new WildcardSubPattern();
112 1366 case Pathspec::kPlaceholder:
113 1366 return new PlaceholderSubPattern();
114 default:
115 assert(false && "unrecognized special character");
116 }
117 }
118
119 8989 std::string PathspecElementPattern::GenerateRegularExpression(
120 const bool is_relaxed) const {
121 8989 std::string result;
122 8989 SubPatterns::const_iterator i = subpatterns_.begin();
123 8989 const SubPatterns::const_iterator iend = subpatterns_.end();
124
2/2
✓ Branch 1 taken 10790 times.
✓ Branch 2 taken 8989 times.
19779 for (; i != iend; ++i) {
125
2/4
✓ Branch 2 taken 10790 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 10790 times.
✗ Branch 6 not taken.
10790 result += (*i)->GenerateRegularExpression(is_relaxed);
126 }
127 17978 return result;
128 }
129
130 1054 std::string PathspecElementPattern::GenerateGlobString() const {
131 1054 std::string result;
132 1054 SubPatterns::const_iterator i = subpatterns_.begin();
133 1054 const SubPatterns::const_iterator iend = subpatterns_.end();
134
2/2
✓ Branch 1 taken 1240 times.
✓ Branch 2 taken 1054 times.
2294 for (; i != iend; ++i) {
135
2/4
✓ Branch 2 taken 1240 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1240 times.
✗ Branch 6 not taken.
1240 result += (*i)->GenerateGlobString();
136 }
137 2108 return result;
138 }
139
140 3406 bool PathspecElementPattern::operator==(
141 const PathspecElementPattern &other) const {
142 3406 if (subpatterns_.size() != other.subpatterns_.size()
143
5/6
✓ Branch 0 taken 3024 times.
✓ Branch 1 taken 382 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 3024 times.
✓ Branch 6 taken 382 times.
✓ Branch 7 taken 3024 times.
3406 || IsValid() != other.IsValid()) {
144 382 return false;
145 }
146
147 3024 SubPatterns::const_iterator i = subpatterns_.begin();
148 3024 const SubPatterns::const_iterator iend = subpatterns_.end();
149 3024 SubPatterns::const_iterator j = other.subpatterns_.begin();
150 3024 const SubPatterns::const_iterator jend = other.subpatterns_.end();
151
152
5/6
✓ Branch 3 taken 3458 times.
✓ Branch 4 taken 2325 times.
✓ Branch 6 taken 3458 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 3458 times.
✓ Branch 9 taken 2325 times.
5783 for (; i != iend && j != jend; ++i, ++j) {
153
3/4
✓ Branch 3 taken 3458 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 699 times.
✓ Branch 6 taken 2759 times.
3458 if (!(*i)->Compare(*j)) {
154 699 return false;
155 }
156 }
157
158 2325 return true;
159 }
160
161
162 //------------------------------------------------------------------------------
163
164
165 60772 void PathspecElementPattern::PlaintextSubPattern::AddChar(const char chr) {
166 60772 chars_.push_back(chr);
167 60772 }
168
169 std::string
170 8693 PathspecElementPattern::PlaintextSubPattern::GenerateRegularExpression(
171 const bool is_relaxed) const {
172 // Note: strict and relaxed regex are the same!
173 8693 std::string::const_iterator i = chars_.begin();
174 8693 const std::string::const_iterator iend = chars_.end();
175 8693 std::string regex;
176
2/2
✓ Branch 2 taken 42085 times.
✓ Branch 3 taken 8693 times.
50778 for (; i != iend; ++i) {
177
2/2
✓ Branch 2 taken 1788 times.
✓ Branch 3 taken 40297 times.
42085 if (IsSpecialRegexCharacter(*i)) {
178
1/2
✓ Branch 1 taken 1788 times.
✗ Branch 2 not taken.
1788 regex += "\\";
179 }
180
1/2
✓ Branch 2 taken 42085 times.
✗ Branch 3 not taken.
42085 regex += *i;
181 }
182 17386 return regex;
183 }
184
185
186 1116 std::string PathspecElementPattern::PlaintextSubPattern::GenerateGlobString()
187 const {
188 1116 std::string::const_iterator i = chars_.begin();
189 1116 const std::string::const_iterator iend = chars_.end();
190 1116 std::string glob_string;
191
2/2
✓ Branch 2 taken 5704 times.
✓ Branch 3 taken 1116 times.
6820 for (; i != iend; ++i) {
192
2/2
✓ Branch 2 taken 124 times.
✓ Branch 3 taken 5580 times.
5704 if (Pathspec::IsSpecialChar(*i)) {
193
1/2
✓ Branch 1 taken 124 times.
✗ Branch 2 not taken.
124 glob_string += "\\";
194 }
195
1/2
✓ Branch 2 taken 5704 times.
✗ Branch 3 not taken.
5704 glob_string += *i;
196 }
197 2232 return glob_string;
198 }
199
200 42085 bool PathspecElementPattern::PlaintextSubPattern::IsSpecialRegexCharacter(
201 const char chr) const {
202
8/8
✓ Branch 0 taken 40886 times.
✓ Branch 1 taken 62 times.
✓ Branch 2 taken 40793 times.
✓ Branch 3 taken 93 times.
✓ Branch 4 taken 40669 times.
✓ Branch 5 taken 124 times.
✓ Branch 6 taken 40607 times.
✓ Branch 7 taken 62 times.
40948 return (chr == '.' || chr == '\\' || chr == '*' || chr == '?' || chr == '['
203
10/10
✓ Branch 0 taken 40545 times.
✓ Branch 1 taken 62 times.
✓ Branch 2 taken 40514 times.
✓ Branch 3 taken 31 times.
✓ Branch 4 taken 40483 times.
✓ Branch 5 taken 31 times.
✓ Branch 6 taken 40452 times.
✓ Branch 7 taken 31 times.
✓ Branch 8 taken 40421 times.
✓ Branch 9 taken 31 times.
40607 || chr == ']' || chr == '(' || chr == ')' || chr == '{' || chr == '}'
204
8/8
✓ Branch 0 taken 40948 times.
✓ Branch 1 taken 1137 times.
✓ Branch 2 taken 40390 times.
✓ Branch 3 taken 31 times.
✓ Branch 4 taken 40359 times.
✓ Branch 5 taken 31 times.
✓ Branch 6 taken 62 times.
✓ Branch 7 taken 40297 times.
83033 || chr == '^' || chr == '$' || chr == '+');
205 }
206
207 2562 bool PathspecElementPattern::PlaintextSubPattern::Compare(
208 const SubPattern *other) const {
209
2/2
✓ Branch 1 taken 31 times.
✓ Branch 2 taken 2531 times.
2562 if (!other->IsPlaintext()) {
210 31 return false;
211 }
212
213 const PlaintextSubPattern
214
1/2
✓ Branch 0 taken 2531 times.
✗ Branch 1 not taken.
2531 *pt_other = dynamic_cast<const PlaintextSubPattern *>(other);
215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2531 times.
2531 assert(pt_other != NULL);
216 2531 return chars_ == pt_other->chars_;
217 }
218
219
220 std::string
221 1428 PathspecElementPattern::WildcardSubPattern::GenerateRegularExpression(
222 const bool is_relaxed) const {
223 return (is_relaxed) ? std::string(".*")
224
14/27
✓ Branch 0 taken 440 times.
✓ Branch 1 taken 988 times.
✓ Branch 4 taken 440 times.
✗ Branch 5 not taken.
✓ Branch 8 taken 988 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 988 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 988 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 988 times.
✓ Branch 17 taken 440 times.
✗ Branch 18 not taken.
✓ Branch 19 taken 988 times.
✓ Branch 20 taken 440 times.
✓ Branch 22 taken 988 times.
✓ Branch 23 taken 440 times.
✓ Branch 25 taken 440 times.
✓ Branch 26 taken 988 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.
2856 : std::string("[^") + Pathspec::kSeparator + "]*";
225 }
226
227
228 62 std::string PathspecElementPattern::WildcardSubPattern::GenerateGlobString()
229 const {
230
1/2
✓ Branch 2 taken 62 times.
✗ Branch 3 not taken.
62 return "*";
231 }
232
233
234 524 bool PathspecElementPattern::WildcardSubPattern::Compare(
235 const SubPattern *other) const {
236 524 return other->IsWildcard();
237 }
238
239
240 std::string
241 669 PathspecElementPattern::PlaceholderSubPattern::GenerateRegularExpression(
242 const bool is_relaxed) const {
243 // Note: strict and relaxed regex are the same!
244
3/6
✓ Branch 2 taken 669 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 669 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 669 times.
✗ Branch 9 not taken.
1338 return std::string("[^") + Pathspec::kSeparator + "]";
245 }
246
247 62 std::string PathspecElementPattern::PlaceholderSubPattern::GenerateGlobString()
248 const {
249
1/2
✓ Branch 2 taken 62 times.
✗ Branch 3 not taken.
62 return "?";
250 }
251
252 372 bool PathspecElementPattern::PlaceholderSubPattern::Compare(
253 const SubPattern *other) const {
254 372 return other->IsPlaceholder();
255 }
256