GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/pathspec/pathspec_pattern.cc
Date: 2025-11-09 02:35:23
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 2315 PathspecElementPattern::PathspecElementPattern(
13 const std::string::const_iterator begin,
14 2315 const std::string::const_iterator &end)
15 2315 : valid_(true) {
16
1/2
✓ Branch 1 taken 2315 times.
✗ Branch 2 not taken.
2315 Parse(begin, end);
17 2315 }
18
19 9188 PathspecElementPattern::PathspecElementPattern(
20 9188 const PathspecElementPattern &other)
21 9188 : valid_(other.valid_) {
22
1/2
✓ Branch 2 taken 9188 times.
✗ Branch 3 not taken.
9188 subpatterns_.reserve(other.subpatterns_.size());
23 9188 SubPatterns::const_iterator i = other.subpatterns_.begin();
24 9188 const SubPatterns::const_iterator iend = other.subpatterns_.end();
25
2/2
✓ Branch 1 taken 9822 times.
✓ Branch 2 taken 9188 times.
19010 for (; i != iend; ++i) {
26
2/4
✓ Branch 2 taken 9822 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 9822 times.
✗ Branch 6 not taken.
9822 subpatterns_.push_back((*i)->Clone());
27 }
28 9188 }
29
30 14 PathspecElementPattern &PathspecElementPattern::operator=(
31 const PathspecElementPattern &other) {
32
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 if (this != &other) {
33 14 valid_ = other.valid_;
34 14 subpatterns_.clear();
35
1/2
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
14 subpatterns_.reserve(other.subpatterns_.size());
36 14 SubPatterns::const_iterator i = other.subpatterns_.begin();
37 14 const SubPatterns::const_iterator iend = other.subpatterns_.end();
38
2/2
✓ Branch 1 taken 22 times.
✓ Branch 2 taken 14 times.
36 for (; i != iend; ++i) {
39
2/4
✓ Branch 2 taken 22 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 22 times.
✗ Branch 6 not taken.
22 subpatterns_.push_back((*i)->Clone());
40 }
41 }
42
43 14 return *this;
44 }
45
46
47 11497 PathspecElementPattern::~PathspecElementPattern() {
48 11497 SubPatterns::const_iterator i = subpatterns_.begin();
49 11497 const SubPatterns::const_iterator iend = subpatterns_.end();
50
2/2
✓ Branch 2 taken 12362 times.
✓ Branch 3 taken 11497 times.
23859 for (; i != iend; ++i) {
51
1/2
✓ Branch 1 taken 12362 times.
✗ Branch 2 not taken.
12362 delete *i;
52 }
53 11497 subpatterns_.clear();
54 11497 }
55
56
57 2315 void PathspecElementPattern::Parse(const std::string::const_iterator &begin,
58 const std::string::const_iterator &end) {
59 2315 std::string::const_iterator i = begin;
60
2/2
✓ Branch 1 taken 2553 times.
✓ Branch 2 taken 2315 times.
4868 while (i != end) {
61
3/4
✓ Branch 2 taken 589 times.
✓ Branch 3 taken 1964 times.
✓ Branch 5 taken 589 times.
✗ Branch 6 not taken.
2553 SubPattern *next = (Pathspec::IsSpecialChar(*i)) ? ParseSpecialChar(end, &i)
62
1/2
✓ Branch 1 taken 1964 times.
✗ Branch 2 not taken.
1964 : ParsePlaintext(end, &i);
63
3/4
✓ Branch 1 taken 2553 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 15 times.
✓ Branch 4 taken 2538 times.
2553 if (next->IsEmpty()) {
64 15 valid_ = false;
65
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 delete next;
66 } else {
67
1/2
✓ Branch 1 taken 2538 times.
✗ Branch 2 not taken.
2538 subpatterns_.push_back(next);
68 }
69 }
70 2315 }
71
72
73 1964 PathspecElementPattern::SubPattern *PathspecElementPattern::ParsePlaintext(
74 const std::string::const_iterator &end, std::string::const_iterator *i) {
75 1964 PlaintextSubPattern *pattern = new PlaintextSubPattern();
76 1964 bool next_is_escaped = false;
77
78
2/2
✓ Branch 1 taken 8143 times.
✓ Branch 2 taken 1880 times.
10023 while (*i < end) {
79
6/6
✓ Branch 2 taken 108 times.
✓ Branch 3 taken 8035 times.
✓ Branch 4 taken 84 times.
✓ Branch 5 taken 24 times.
✓ Branch 6 taken 84 times.
✓ Branch 7 taken 8059 times.
8143 if (Pathspec::IsSpecialChar(**i) && !next_is_escaped) {
80 84 break;
81 }
82
83
6/6
✓ Branch 1 taken 54 times.
✓ Branch 2 taken 8005 times.
✓ Branch 3 taken 47 times.
✓ Branch 4 taken 7 times.
✓ Branch 5 taken 47 times.
✓ Branch 6 taken 8012 times.
8059 if (**i == Pathspec::kEscaper && !next_is_escaped) {
84 47 next_is_escaped = true;
85
2/2
✓ Branch 0 taken 81 times.
✓ Branch 1 taken 7931 times.
8012 } else if (next_is_escaped) {
86
6/6
✓ Branch 2 taken 57 times.
✓ Branch 3 taken 24 times.
✓ Branch 5 taken 7 times.
✓ Branch 6 taken 50 times.
✓ Branch 7 taken 31 times.
✓ Branch 8 taken 50 times.
81 if (Pathspec::IsSpecialChar(**i) || **i == Pathspec::kEscaper) {
87 31 pattern->AddChar(**i);
88 31 next_is_escaped = false;
89 } else {
90 50 valid_ = false;
91 }
92 } else {
93
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 7931 times.
7931 assert(!Pathspec::IsSpecialChar(**i));
94 7931 pattern->AddChar(**i);
95 }
96
97 8059 ++(*i);
98 }
99
100 1964 return pattern;
101 }
102
103 589 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 589 times.
589 assert(Pathspec::IsSpecialChar(**i));
106 589 const char chr = **i;
107 589 ++(*i);
108
109
2/3
✓ Branch 0 taken 435 times.
✓ Branch 1 taken 154 times.
✗ Branch 2 not taken.
589 switch (chr) {
110 435 case Pathspec::kWildcard:
111 435 return new WildcardSubPattern();
112 154 case Pathspec::kPlaceholder:
113 154 return new PlaceholderSubPattern();
114 default:
115 assert(false && "unrecognized special character");
116 }
117 }
118
119 1404 std::string PathspecElementPattern::GenerateRegularExpression(
120 const bool is_relaxed) const {
121 1404 std::string result;
122 1404 SubPatterns::const_iterator i = subpatterns_.begin();
123 1404 const SubPatterns::const_iterator iend = subpatterns_.end();
124
2/2
✓ Branch 1 taken 1589 times.
✓ Branch 2 taken 1404 times.
2993 for (; i != iend; ++i) {
125
2/4
✓ Branch 2 taken 1589 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1589 times.
✗ Branch 6 not taken.
1589 result += (*i)->GenerateRegularExpression(is_relaxed);
126 }
127 2808 return result;
128 }
129
130 68 std::string PathspecElementPattern::GenerateGlobString() const {
131 68 std::string result;
132 68 SubPatterns::const_iterator i = subpatterns_.begin();
133 68 const SubPatterns::const_iterator iend = subpatterns_.end();
134
2/2
✓ Branch 1 taken 80 times.
✓ Branch 2 taken 68 times.
148 for (; i != iend; ++i) {
135
2/4
✓ Branch 2 taken 80 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 80 times.
✗ Branch 6 not taken.
80 result += (*i)->GenerateGlobString();
136 }
137 136 return result;
138 }
139
140 530 bool PathspecElementPattern::operator==(
141 const PathspecElementPattern &other) const {
142 530 if (subpatterns_.size() != other.subpatterns_.size()
143
5/6
✓ Branch 0 taken 464 times.
✓ Branch 1 taken 66 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 464 times.
✓ Branch 6 taken 66 times.
✓ Branch 7 taken 464 times.
530 || IsValid() != other.IsValid()) {
144 66 return false;
145 }
146
147 464 SubPatterns::const_iterator i = subpatterns_.begin();
148 464 const SubPatterns::const_iterator iend = subpatterns_.end();
149 464 SubPatterns::const_iterator j = other.subpatterns_.begin();
150 464 const SubPatterns::const_iterator jend = other.subpatterns_.end();
151
152
5/6
✓ Branch 3 taken 478 times.
✓ Branch 4 taken 303 times.
✓ Branch 6 taken 478 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 478 times.
✓ Branch 9 taken 303 times.
781 for (; i != iend && j != jend; ++i, ++j) {
153
3/4
✓ Branch 3 taken 478 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 161 times.
✓ Branch 6 taken 317 times.
478 if (!(*i)->Compare(*j)) {
154 161 return false;
155 }
156 }
157
158 303 return true;
159 }
160
161
162 //------------------------------------------------------------------------------
163
164
165 7962 void PathspecElementPattern::PlaintextSubPattern::AddChar(const char chr) {
166 7962 chars_.push_back(chr);
167 7962 }
168
169 std::string
170 1280 PathspecElementPattern::PlaintextSubPattern::GenerateRegularExpression(
171 const bool is_relaxed) const {
172 // Note: strict and relaxed regex are the same!
173 1280 std::string::const_iterator i = chars_.begin();
174 1280 const std::string::const_iterator iend = chars_.end();
175 1280 std::string regex;
176
2/2
✓ Branch 2 taken 5474 times.
✓ Branch 3 taken 1280 times.
6754 for (; i != iend; ++i) {
177
2/2
✓ Branch 2 taken 203 times.
✓ Branch 3 taken 5271 times.
5474 if (IsSpecialRegexCharacter(*i)) {
178
1/2
✓ Branch 1 taken 203 times.
✗ Branch 2 not taken.
203 regex += "\\";
179 }
180
1/2
✓ Branch 2 taken 5474 times.
✗ Branch 3 not taken.
5474 regex += *i;
181 }
182 2560 return regex;
183 }
184
185
186 72 std::string PathspecElementPattern::PlaintextSubPattern::GenerateGlobString()
187 const {
188 72 std::string::const_iterator i = chars_.begin();
189 72 const std::string::const_iterator iend = chars_.end();
190 72 std::string glob_string;
191
2/2
✓ Branch 2 taken 368 times.
✓ Branch 3 taken 72 times.
440 for (; i != iend; ++i) {
192
2/2
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 360 times.
368 if (Pathspec::IsSpecialChar(*i)) {
193
1/2
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
8 glob_string += "\\";
194 }
195
1/2
✓ Branch 2 taken 368 times.
✗ Branch 3 not taken.
368 glob_string += *i;
196 }
197 144 return glob_string;
198 }
199
200 5474 bool PathspecElementPattern::PlaintextSubPattern::IsSpecialRegexCharacter(
201 const char chr) const {
202
8/8
✓ Branch 0 taken 5309 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 5303 times.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 5295 times.
✓ Branch 5 taken 8 times.
✓ Branch 6 taken 5291 times.
✓ Branch 7 taken 4 times.
5313 return (chr == '.' || chr == '\\' || chr == '*' || chr == '?' || chr == '['
203
10/10
✓ Branch 0 taken 5287 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 5285 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 5283 times.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 5281 times.
✓ Branch 7 taken 2 times.
✓ Branch 8 taken 5279 times.
✓ Branch 9 taken 2 times.
5291 || chr == ']' || chr == '(' || chr == ')' || chr == '{' || chr == '}'
204
8/8
✓ Branch 0 taken 5313 times.
✓ Branch 1 taken 161 times.
✓ Branch 2 taken 5277 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 5275 times.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 4 times.
✓ Branch 7 taken 5271 times.
10787 || chr == '^' || chr == '$' || chr == '+');
205 }
206
207 382 bool PathspecElementPattern::PlaintextSubPattern::Compare(
208 const SubPattern *other) const {
209
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 381 times.
382 if (!other->IsPlaintext()) {
210 1 return false;
211 }
212
213 const PlaintextSubPattern
214
1/2
✓ Branch 0 taken 381 times.
✗ Branch 1 not taken.
381 *pt_other = dynamic_cast<const PlaintextSubPattern *>(other);
215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 381 times.
381 assert(pt_other != NULL);
216 381 return chars_ == pt_other->chars_;
217 }
218
219
220 std::string
221 260 PathspecElementPattern::WildcardSubPattern::GenerateRegularExpression(
222 const bool is_relaxed) const {
223 return (is_relaxed) ? std::string(".*")
224
14/27
✓ Branch 0 taken 114 times.
✓ Branch 1 taken 146 times.
✓ Branch 4 taken 114 times.
✗ Branch 5 not taken.
✓ Branch 8 taken 146 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 146 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 146 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 146 times.
✓ Branch 17 taken 114 times.
✗ Branch 18 not taken.
✓ Branch 19 taken 146 times.
✓ Branch 20 taken 114 times.
✓ Branch 22 taken 146 times.
✓ Branch 23 taken 114 times.
✓ Branch 25 taken 114 times.
✓ Branch 26 taken 146 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.
520 : std::string("[^") + Pathspec::kSeparator + "]*";
225 }
226
227
228 4 std::string PathspecElementPattern::WildcardSubPattern::GenerateGlobString()
229 const {
230
1/2
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 return "*";
231 }
232
233
234 84 bool PathspecElementPattern::WildcardSubPattern::Compare(
235 const SubPattern *other) const {
236 84 return other->IsWildcard();
237 }
238
239
240 std::string
241 49 PathspecElementPattern::PlaceholderSubPattern::GenerateRegularExpression(
242 const bool is_relaxed) const {
243 // Note: strict and relaxed regex are the same!
244
3/6
✓ Branch 2 taken 49 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 49 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 49 times.
✗ Branch 9 not taken.
98 return std::string("[^") + Pathspec::kSeparator + "]";
245 }
246
247 4 std::string PathspecElementPattern::PlaceholderSubPattern::GenerateGlobString()
248 const {
249
1/2
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 return "?";
250 }
251
252 12 bool PathspecElementPattern::PlaceholderSubPattern::Compare(
253 const SubPattern *other) const {
254 12 return other->IsPlaceholder();
255 }
256