GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/pathspec/pathspec_pattern.cc
Date: 2026-10-18 02:40:11
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 14916 PathspecElementPattern::PathspecElementPattern(
13 const std::string::const_iterator begin,
14 14916 const std::string::const_iterator &end)
15 14916 : valid_(true) {
16
1/2
✓ Branch 1 taken 14916 times.
✗ Branch 2 not taken.
14916 Parse(begin, end);
17 14916 }
18
19 46442 PathspecElementPattern::PathspecElementPattern(
20 46442 const PathspecElementPattern &other)
21 46442 : valid_(other.valid_) {
22
1/2
✓ Branch 2 taken 46442 times.
✗ Branch 3 not taken.
46442 subpatterns_.reserve(other.subpatterns_.size());
23 46442 SubPatterns::const_iterator i = other.subpatterns_.begin();
24 46442 const SubPatterns::const_iterator iend = other.subpatterns_.end();
25
2/2
✓ Branch 1 taken 50607 times.
✓ Branch 2 taken 46442 times.
97049 for (; i != iend; ++i) {
26
2/4
✓ Branch 2 taken 50607 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 50607 times.
✗ Branch 6 not taken.
50607 subpatterns_.push_back((*i)->Clone());
27 }
28 46442 }
29
30 245 PathspecElementPattern &PathspecElementPattern::operator=(
31 const PathspecElementPattern &other) {
32
1/2
✓ Branch 0 taken 245 times.
✗ Branch 1 not taken.
245 if (this != &other) {
33 245 valid_ = other.valid_;
34 245 subpatterns_.clear();
35
1/2
✓ Branch 2 taken 245 times.
✗ Branch 3 not taken.
245 subpatterns_.reserve(other.subpatterns_.size());
36 245 SubPatterns::const_iterator i = other.subpatterns_.begin();
37 245 const SubPatterns::const_iterator iend = other.subpatterns_.end();
38
2/2
✓ Branch 1 taken 385 times.
✓ Branch 2 taken 245 times.
630 for (; i != iend; ++i) {
39
2/4
✓ Branch 2 taken 385 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 385 times.
✗ Branch 6 not taken.
385 subpatterns_.push_back((*i)->Clone());
40 }
41 }
42
43 245 return *this;
44 }
45
46
47 61352 PathspecElementPattern::~PathspecElementPattern() {
48 61352 SubPatterns::const_iterator i = subpatterns_.begin();
49 61352 const SubPatterns::const_iterator iend = subpatterns_.end();
50
2/2
✓ Branch 2 taken 68065 times.
✓ Branch 3 taken 61352 times.
129417 for (; i != iend; ++i) {
51
1/2
✓ Branch 1 taken 68065 times.
✗ Branch 2 not taken.
68065 delete *i;
52 }
53 61352 subpatterns_.clear();
54 61352 }
55
56
57 14916 void PathspecElementPattern::Parse(const std::string::const_iterator &begin,
58 const std::string::const_iterator &end) {
59 14916 std::string::const_iterator i = begin;
60
2/2
✓ Branch 1 taken 17357 times.
✓ Branch 2 taken 14916 times.
32273 while (i != end) {
61
3/4
✓ Branch 2 taken 3319 times.
✓ Branch 3 taken 14038 times.
✓ Branch 5 taken 3319 times.
✗ Branch 6 not taken.
17357 SubPattern *next = (Pathspec::IsSpecialChar(*i)) ? ParseSpecialChar(end, &i)
62
1/2
✓ Branch 1 taken 14038 times.
✗ Branch 2 not taken.
14038 : ParsePlaintext(end, &i);
63
3/4
✓ Branch 1 taken 17357 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 33 times.
✓ Branch 4 taken 17324 times.
17357 if (next->IsEmpty()) {
64 33 valid_ = false;
65
1/2
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
33 delete next;
66 } else {
67
1/2
✓ Branch 1 taken 17324 times.
✗ Branch 2 not taken.
17324 subpatterns_.push_back(next);
68 }
69 }
70 14916 }
71
72
73 14038 PathspecElementPattern::SubPattern *PathspecElementPattern::ParsePlaintext(
74 const std::string::const_iterator &end, std::string::const_iterator *i) {
75 14038 PlaintextSubPattern *pattern = new PlaintextSubPattern();
76 14038 bool next_is_escaped = false;
77
78
2/2
✓ Branch 1 taken 65497 times.
✓ Branch 2 taken 13092 times.
78589 while (*i < end) {
79
6/6
✓ Branch 2 taken 1404 times.
✓ Branch 3 taken 64093 times.
✓ Branch 4 taken 946 times.
✓ Branch 5 taken 458 times.
✓ Branch 6 taken 946 times.
✓ Branch 7 taken 64551 times.
65497 if (Pathspec::IsSpecialChar(**i) && !next_is_escaped) {
80 946 break;
81 }
82
83
6/6
✓ Branch 1 taken 808 times.
✓ Branch 2 taken 63743 times.
✓ Branch 3 taken 667 times.
✓ Branch 4 taken 141 times.
✓ Branch 5 taken 667 times.
✓ Branch 6 taken 63884 times.
64551 if (**i == Pathspec::kEscaper && !next_is_escaped) {
84 667 next_is_escaped = true;
85
2/2
✓ Branch 0 taken 873 times.
✓ Branch 1 taken 63011 times.
63884 } else if (next_is_escaped) {
86
6/6
✓ Branch 2 taken 415 times.
✓ Branch 3 taken 458 times.
✓ Branch 5 taken 141 times.
✓ Branch 6 taken 274 times.
✓ Branch 7 taken 599 times.
✓ Branch 8 taken 274 times.
873 if (Pathspec::IsSpecialChar(**i) || **i == Pathspec::kEscaper) {
87 599 pattern->AddChar(**i);
88 599 next_is_escaped = false;
89 } else {
90 274 valid_ = false;
91 }
92 } else {
93
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 63011 times.
63011 assert(!Pathspec::IsSpecialChar(**i));
94 63011 pattern->AddChar(**i);
95 }
96
97 64551 ++(*i);
98 }
99
100 14038 return pattern;
101 }
102
103 3319 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 3319 times.
3319 assert(Pathspec::IsSpecialChar(**i));
106 3319 const char chr = **i;
107 3319 ++(*i);
108
109
2/3
✓ Branch 0 taken 1923 times.
✓ Branch 1 taken 1396 times.
✗ Branch 2 not taken.
3319 switch (chr) {
110 1923 case Pathspec::kWildcard:
111 1923 return new WildcardSubPattern();
112 1396 case Pathspec::kPlaceholder:
113 1396 return new PlaceholderSubPattern();
114 ✗ default:
115 ✗ assert(false && "unrecognized special character");
116 }
117 }
118
119 9580 std::string PathspecElementPattern::GenerateRegularExpression(
120 const bool is_relaxed) const {
121 9580 std::string result;
122 9580 SubPatterns::const_iterator i = subpatterns_.begin();
123 9580 const SubPatterns::const_iterator iend = subpatterns_.end();
124
2/2
✓ Branch 1 taken 11521 times.
✓ Branch 2 taken 9580 times.
21101 for (; i != iend; ++i) {
125
2/4
✓ Branch 2 taken 11521 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 11521 times.
✗ Branch 6 not taken.
11521 result += (*i)->GenerateRegularExpression(is_relaxed);
126 }
127 19160 return result;
128 }
129
130 1190 std::string PathspecElementPattern::GenerateGlobString() const {
131 1190 std::string result;
132 1190 SubPatterns::const_iterator i = subpatterns_.begin();
133 1190 const SubPatterns::const_iterator iend = subpatterns_.end();
134
2/2
✓ Branch 1 taken 1400 times.
✓ Branch 2 taken 1190 times.
2590 for (; i != iend; ++i) {
135
2/4
✓ Branch 2 taken 1400 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1400 times.
✗ Branch 6 not taken.
1400 result += (*i)->GenerateGlobString();
136 }
137 2380 return result;
138 }
139
140 3342 bool PathspecElementPattern::operator==(
141 const PathspecElementPattern &other) const {
142 3342 if (subpatterns_.size() != other.subpatterns_.size()
143
5/6
✓ Branch 0 taken 3000 times.
✓ Branch 1 taken 342 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 3000 times.
✓ Branch 6 taken 342 times.
✓ Branch 7 taken 3000 times.
3342 || IsValid() != other.IsValid()) {
144 342 return false;
145 }
146
147 3000 SubPatterns::const_iterator i = subpatterns_.begin();
148 3000 const SubPatterns::const_iterator iend = subpatterns_.end();
149 3000 SubPatterns::const_iterator j = other.subpatterns_.begin();
150 3000 const SubPatterns::const_iterator jend = other.subpatterns_.end();
151
152
5/6
✓ Branch 3 taken 3490 times.
✓ Branch 4 taken 2319 times.
✓ Branch 6 taken 3490 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 3490 times.
✓ Branch 9 taken 2319 times.
5809 for (; i != iend && j != jend; ++i, ++j) {
153
3/4
✓ Branch 3 taken 3490 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 681 times.
✓ Branch 6 taken 2809 times.
3490 if (!(*i)->Compare(*j)) {
154 681 return false;
155 }
156 }
157
158 2319 return true;
159 }
160
161
162 //------------------------------------------------------------------------------
163
164
165 63610 void PathspecElementPattern::PlaintextSubPattern::AddChar(const char chr) {
166 63610 chars_.push_back(chr);
167 63610 }
168
169 std::string
170 9386 PathspecElementPattern::PlaintextSubPattern::GenerateRegularExpression(
171 const bool is_relaxed) const {
172 // Note: strict and relaxed regex are the same!
173 9386 std::string::const_iterator i = chars_.begin();
174 9386 const std::string::const_iterator iend = chars_.end();
175 9386 std::string regex;
176
2/2
✓ Branch 2 taken 45634 times.
✓ Branch 3 taken 9386 times.
55020 for (; i != iend; ++i) {
177
2/2
✓ Branch 2 taken 1910 times.
✓ Branch 3 taken 43724 times.
45634 if (IsSpecialRegexCharacter(*i)) {
178
1/2
✓ Branch 1 taken 1910 times.
✗ Branch 2 not taken.
1910 regex += "\\";
179 }
180
1/2
✓ Branch 2 taken 45634 times.
✗ Branch 3 not taken.
45634 regex += *i;
181 }
182 18772 return regex;
183 }
184
185
186 1260 std::string PathspecElementPattern::PlaintextSubPattern::GenerateGlobString()
187 const {
188 1260 std::string::const_iterator i = chars_.begin();
189 1260 const std::string::const_iterator iend = chars_.end();
190 1260 std::string glob_string;
191
2/2
✓ Branch 2 taken 6440 times.
✓ Branch 3 taken 1260 times.
7700 for (; i != iend; ++i) {
192
2/2
✓ Branch 2 taken 140 times.
✓ Branch 3 taken 6300 times.
6440 if (Pathspec::IsSpecialChar(*i)) {
193
1/2
✓ Branch 1 taken 140 times.
✗ Branch 2 not taken.
140 glob_string += "\\";
194 }
195
1/2
✓ Branch 2 taken 6440 times.
✗ Branch 3 not taken.
6440 glob_string += *i;
196 }
197 2520 return glob_string;
198 }
199
200 45634 bool PathspecElementPattern::PlaintextSubPattern::IsSpecialRegexCharacter(
201 const char chr) const {
202
8/8
✓ Branch 0 taken 44392 times.
✓ Branch 1 taken 71 times.
✓ Branch 2 taken 44286 times.
✓ Branch 3 taken 106 times.
✓ Branch 4 taken 44144 times.
✓ Branch 5 taken 142 times.
✓ Branch 6 taken 44074 times.
✓ Branch 7 taken 70 times.
44463 return (chr == '.' || chr == '\\' || chr == '*' || chr == '?' || chr == '['
203
10/10
✓ Branch 0 taken 44004 times.
✓ Branch 1 taken 70 times.
✓ Branch 2 taken 43969 times.
✓ Branch 3 taken 35 times.
✓ Branch 4 taken 43934 times.
✓ Branch 5 taken 35 times.
✓ Branch 6 taken 43899 times.
✓ Branch 7 taken 35 times.
✓ Branch 8 taken 43864 times.
✓ Branch 9 taken 35 times.
44074 || chr == ']' || chr == '(' || chr == ')' || chr == '{' || chr == '}'
204
8/8
✓ Branch 0 taken 44463 times.
✓ Branch 1 taken 1171 times.
✓ Branch 2 taken 43829 times.
✓ Branch 3 taken 35 times.
✓ Branch 4 taken 43794 times.
✓ Branch 5 taken 35 times.
✓ Branch 6 taken 70 times.
✓ Branch 7 taken 43724 times.
90097 || chr == '^' || chr == '$' || chr == '+');
205 }
206
207 2590 bool PathspecElementPattern::PlaintextSubPattern::Compare(
208 const SubPattern *other) const {
209
2/2
✓ Branch 1 taken 35 times.
✓ Branch 2 taken 2555 times.
2590 if (!other->IsPlaintext()) {
210 35 return false;
211 }
212
213 const PlaintextSubPattern
214
1/2
✓ Branch 0 taken 2555 times.
✗ Branch 1 not taken.
2555 *pt_other = dynamic_cast<const PlaintextSubPattern *>(other);
215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2555 times.
2555 assert(pt_other != NULL);
216 2555 return chars_ == pt_other->chars_;
217 }
218
219
220 std::string
221 1402 PathspecElementPattern::WildcardSubPattern::GenerateRegularExpression(
222 const bool is_relaxed) const {
223 return (is_relaxed) ? std::string(".*")
224
14/27
✓ Branch 0 taken 414 times.
✓ Branch 1 taken 988 times.
✓ Branch 4 taken 414 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 414 times.
✗ Branch 18 not taken.
✓ Branch 19 taken 988 times.
✓ Branch 20 taken 414 times.
✓ Branch 22 taken 988 times.
✓ Branch 23 taken 414 times.
✓ Branch 25 taken 414 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.
2804 : std::string("[^") + Pathspec::kSeparator + "]*";
225 }
226
227
228 70 std::string PathspecElementPattern::WildcardSubPattern::GenerateGlobString()
229 const {
230
1/2
✓ Branch 2 taken 70 times.
✗ Branch 3 not taken.
70 return "*";
231 }
232
233
234 480 bool PathspecElementPattern::WildcardSubPattern::Compare(
235 const SubPattern *other) const {
236 480 return other->IsWildcard();
237 }
238
239
240 std::string
241 733 PathspecElementPattern::PlaceholderSubPattern::GenerateRegularExpression(
242 const bool is_relaxed) const {
243 // Note: strict and relaxed regex are the same!
244
3/6
✓ Branch 2 taken 733 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 733 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 733 times.
✗ Branch 9 not taken.
1466 return std::string("[^") + Pathspec::kSeparator + "]";
245 }
246
247 70 std::string PathspecElementPattern::PlaceholderSubPattern::GenerateGlobString()
248 const {
249
1/2
✓ Branch 2 taken 70 times.
✗ Branch 3 not taken.
70 return "?";
250 }
251
252 420 bool PathspecElementPattern::PlaceholderSubPattern::Compare(
253 const SubPattern *other) const {
254 420 return other->IsPlaceholder();
255 }
256