GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/pathspec/pathspec_pattern.cc
Date: 2026-02-15 02:35:50
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 11324 PathspecElementPattern::PathspecElementPattern(
13 const std::string::const_iterator begin,
14 11324 const std::string::const_iterator &end)
15 11324 : valid_(true) {
16
1/2
✓ Branch 1 taken 11324 times.
✗ Branch 2 not taken.
11324 Parse(begin, end);
17 11324 }
18
19 25183 PathspecElementPattern::PathspecElementPattern(
20 25183 const PathspecElementPattern &other)
21 25183 : valid_(other.valid_) {
22
1/2
✓ Branch 2 taken 25183 times.
✗ Branch 3 not taken.
25183 subpatterns_.reserve(other.subpatterns_.size());
23 25183 SubPatterns::const_iterator i = other.subpatterns_.begin();
24 25183 const SubPatterns::const_iterator iend = other.subpatterns_.end();
25
2/2
✓ Branch 1 taken 29590 times.
✓ Branch 2 taken 25183 times.
54773 for (; i != iend; ++i) {
26
2/4
✓ Branch 2 taken 29590 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 29590 times.
✗ Branch 6 not taken.
29590 subpatterns_.push_back((*i)->Clone());
27 }
28 25183 }
29
30 322 PathspecElementPattern &PathspecElementPattern::operator=(
31 const PathspecElementPattern &other) {
32
1/2
✓ Branch 0 taken 322 times.
✗ Branch 1 not taken.
322 if (this != &other) {
33 322 valid_ = other.valid_;
34 322 subpatterns_.clear();
35
1/2
✓ Branch 2 taken 322 times.
✗ Branch 3 not taken.
322 subpatterns_.reserve(other.subpatterns_.size());
36 322 SubPatterns::const_iterator i = other.subpatterns_.begin();
37 322 const SubPatterns::const_iterator iend = other.subpatterns_.end();
38
2/2
✓ Branch 1 taken 506 times.
✓ Branch 2 taken 322 times.
828 for (; i != iend; ++i) {
39
2/4
✓ Branch 2 taken 506 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 506 times.
✗ Branch 6 not taken.
506 subpatterns_.push_back((*i)->Clone());
40 }
41 }
42
43 322 return *this;
44 }
45
46
47 36501 PathspecElementPattern::~PathspecElementPattern() {
48 36501 SubPatterns::const_iterator i = subpatterns_.begin();
49 36501 const SubPatterns::const_iterator iend = subpatterns_.end();
50
2/2
✓ Branch 2 taken 43949 times.
✓ Branch 3 taken 36501 times.
80450 for (; i != iend; ++i) {
51
1/2
✓ Branch 1 taken 43949 times.
✗ Branch 2 not taken.
43949 delete *i;
52 }
53 36501 subpatterns_.clear();
54 36501 }
55
56
57 11324 void PathspecElementPattern::Parse(const std::string::const_iterator &begin,
58 const std::string::const_iterator &end) {
59 11324 std::string::const_iterator i = begin;
60
2/2
✓ Branch 1 taken 14189 times.
✓ Branch 2 taken 11324 times.
25513 while (i != end) {
61
3/4
✓ Branch 2 taken 3157 times.
✓ Branch 3 taken 11032 times.
✓ Branch 5 taken 3157 times.
✗ Branch 6 not taken.
14189 SubPattern *next = (Pathspec::IsSpecialChar(*i)) ? ParseSpecialChar(end, &i)
62
1/2
✓ Branch 1 taken 11032 times.
✗ Branch 2 not taken.
11032 : ParsePlaintext(end, &i);
63
3/4
✓ Branch 1 taken 14189 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
✓ Branch 4 taken 14181 times.
14189 if (next->IsEmpty()) {
64 8 valid_ = false;
65
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 delete next;
66 } else {
67
1/2
✓ Branch 1 taken 14181 times.
✗ Branch 2 not taken.
14181 subpatterns_.push_back(next);
68 }
69 }
70 11324 }
71
72
73 11032 PathspecElementPattern::SubPattern *PathspecElementPattern::ParsePlaintext(
74 const std::string::const_iterator &end, std::string::const_iterator *i) {
75 11032 PlaintextSubPattern *pattern = new PlaintextSubPattern();
76 11032 bool next_is_escaped = false;
77
78
2/2
✓ Branch 1 taken 49082 times.
✓ Branch 2 taken 9922 times.
59004 while (*i < end) {
79
6/6
✓ Branch 2 taken 1695 times.
✓ Branch 3 taken 47387 times.
✓ Branch 4 taken 1110 times.
✓ Branch 5 taken 585 times.
✓ Branch 6 taken 1110 times.
✓ Branch 7 taken 47972 times.
49082 if (Pathspec::IsSpecialChar(**i) && !next_is_escaped) {
80 1110 break;
81 }
82
83
6/6
✓ Branch 1 taken 998 times.
✓ Branch 2 taken 46974 times.
✓ Branch 3 taken 818 times.
✓ Branch 4 taken 180 times.
✓ Branch 5 taken 818 times.
✓ Branch 6 taken 47154 times.
47972 if (**i == Pathspec::kEscaper && !next_is_escaped) {
84 818 next_is_escaped = true;
85
2/2
✓ Branch 0 taken 1014 times.
✓ Branch 1 taken 46140 times.
47154 } else if (next_is_escaped) {
86
6/6
✓ Branch 2 taken 429 times.
✓ Branch 3 taken 585 times.
✓ Branch 5 taken 180 times.
✓ Branch 6 taken 249 times.
✓ Branch 7 taken 765 times.
✓ Branch 8 taken 249 times.
1014 if (Pathspec::IsSpecialChar(**i) || **i == Pathspec::kEscaper) {
87 765 pattern->AddChar(**i);
88 765 next_is_escaped = false;
89 } else {
90 249 valid_ = false;
91 }
92 } else {
93
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 46140 times.
46140 assert(!Pathspec::IsSpecialChar(**i));
94 46140 pattern->AddChar(**i);
95 }
96
97 47972 ++(*i);
98 }
99
100 11032 return pattern;
101 }
102
103 3157 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 3157 times.
3157 assert(Pathspec::IsSpecialChar(**i));
106 3157 const char chr = **i;
107 3157 ++(*i);
108
109
2/3
✓ Branch 0 taken 1609 times.
✓ Branch 1 taken 1548 times.
✗ Branch 2 not taken.
3157 switch (chr) {
110 1609 case Pathspec::kWildcard:
111 1609 return new WildcardSubPattern();
112 1548 case Pathspec::kPlaceholder:
113 1548 return new PlaceholderSubPattern();
114 default:
115 assert(false && "unrecognized special character");
116 }
117 }
118
119 7184 std::string PathspecElementPattern::GenerateRegularExpression(
120 const bool is_relaxed) const {
121 7184 std::string result;
122 7184 SubPatterns::const_iterator i = subpatterns_.begin();
123 7184 const SubPatterns::const_iterator iend = subpatterns_.end();
124
2/2
✓ Branch 1 taken 9532 times.
✓ Branch 2 taken 7184 times.
16716 for (; i != iend; ++i) {
125
2/4
✓ Branch 2 taken 9532 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 9532 times.
✗ Branch 6 not taken.
9532 result += (*i)->GenerateRegularExpression(is_relaxed);
126 }
127 14368 return result;
128 }
129
130 1538 std::string PathspecElementPattern::GenerateGlobString() const {
131 1538 std::string result;
132 1538 SubPatterns::const_iterator i = subpatterns_.begin();
133 1538 const SubPatterns::const_iterator iend = subpatterns_.end();
134
2/2
✓ Branch 1 taken 1811 times.
✓ Branch 2 taken 1538 times.
3349 for (; i != iend; ++i) {
135
2/4
✓ Branch 2 taken 1811 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1811 times.
✗ Branch 6 not taken.
1811 result += (*i)->GenerateGlobString();
136 }
137 3076 return result;
138 }
139
140 3428 bool PathspecElementPattern::operator==(
141 const PathspecElementPattern &other) const {
142 3428 if (subpatterns_.size() != other.subpatterns_.size()
143
5/6
✓ Branch 0 taken 3126 times.
✓ Branch 1 taken 302 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 3126 times.
✓ Branch 6 taken 302 times.
✓ Branch 7 taken 3126 times.
3428 || IsValid() != other.IsValid()) {
144 302 return false;
145 }
146
147 3126 SubPatterns::const_iterator i = subpatterns_.begin();
148 3126 const SubPatterns::const_iterator iend = subpatterns_.end();
149 3126 SubPatterns::const_iterator j = other.subpatterns_.begin();
150 3126 const SubPatterns::const_iterator jend = other.subpatterns_.end();
151
152
5/6
✓ Branch 3 taken 3756 times.
✓ Branch 4 taken 2475 times.
✓ Branch 6 taken 3756 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 3756 times.
✓ Branch 9 taken 2475 times.
6231 for (; i != iend && j != jend; ++i, ++j) {
153
3/4
✓ Branch 3 taken 3756 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 651 times.
✓ Branch 6 taken 3105 times.
3756 if (!(*i)->Compare(*j)) {
154 651 return false;
155 }
156 }
157
158 2475 return true;
159 }
160
161
162 //------------------------------------------------------------------------------
163
164
165 46905 void PathspecElementPattern::PlaintextSubPattern::AddChar(const char chr) {
166 46905 chars_.push_back(chr);
167 46905 }
168
169 std::string
170 7297 PathspecElementPattern::PlaintextSubPattern::GenerateRegularExpression(
171 const bool is_relaxed) const {
172 // Note: strict and relaxed regex are the same!
173 7297 std::string::const_iterator i = chars_.begin();
174 7297 const std::string::const_iterator iend = chars_.end();
175 7297 std::string regex;
176
2/2
✓ Branch 2 taken 32222 times.
✓ Branch 3 taken 7297 times.
39519 for (; i != iend; ++i) {
177
2/2
✓ Branch 2 taken 2253 times.
✓ Branch 3 taken 29969 times.
32222 if (IsSpecialRegexCharacter(*i)) {
178
1/2
✓ Branch 1 taken 2253 times.
✗ Branch 2 not taken.
2253 regex += "\\";
179 }
180
1/2
✓ Branch 2 taken 32222 times.
✗ Branch 3 not taken.
32222 regex += *i;
181 }
182 14594 return regex;
183 }
184
185
186 1629 std::string PathspecElementPattern::PlaintextSubPattern::GenerateGlobString()
187 const {
188 1629 std::string::const_iterator i = chars_.begin();
189 1629 const std::string::const_iterator iend = chars_.end();
190 1629 std::string glob_string;
191
2/2
✓ Branch 2 taken 8320 times.
✓ Branch 3 taken 1629 times.
9949 for (; i != iend; ++i) {
192
2/2
✓ Branch 2 taken 180 times.
✓ Branch 3 taken 8140 times.
8320 if (Pathspec::IsSpecialChar(*i)) {
193
1/2
✓ Branch 1 taken 180 times.
✗ Branch 2 not taken.
180 glob_string += "\\";
194 }
195
1/2
✓ Branch 2 taken 8320 times.
✗ Branch 3 not taken.
8320 glob_string += *i;
196 }
197 3258 return glob_string;
198 }
199
200 32222 bool PathspecElementPattern::PlaintextSubPattern::IsSpecialRegexCharacter(
201 const char chr) const {
202
8/8
✓ Branch 0 taken 30824 times.
✓ Branch 1 taken 90 times.
✓ Branch 2 taken 30689 times.
✓ Branch 3 taken 135 times.
✓ Branch 4 taken 30509 times.
✓ Branch 5 taken 180 times.
✓ Branch 6 taken 30419 times.
✓ Branch 7 taken 90 times.
30914 return (chr == '.' || chr == '\\' || chr == '*' || chr == '?' || chr == '['
203
10/10
✓ Branch 0 taken 30329 times.
✓ Branch 1 taken 90 times.
✓ Branch 2 taken 30284 times.
✓ Branch 3 taken 45 times.
✓ Branch 4 taken 30239 times.
✓ Branch 5 taken 45 times.
✓ Branch 6 taken 30194 times.
✓ Branch 7 taken 45 times.
✓ Branch 8 taken 30149 times.
✓ Branch 9 taken 45 times.
30419 || chr == ']' || chr == '(' || chr == ')' || chr == '{' || chr == '}'
204
8/8
✓ Branch 0 taken 30914 times.
✓ Branch 1 taken 1308 times.
✓ Branch 2 taken 30104 times.
✓ Branch 3 taken 45 times.
✓ Branch 4 taken 30059 times.
✓ Branch 5 taken 45 times.
✓ Branch 6 taken 90 times.
✓ Branch 7 taken 29969 times.
63136 || chr == '^' || chr == '$' || chr == '+');
205 }
206
207 2771 bool PathspecElementPattern::PlaintextSubPattern::Compare(
208 const SubPattern *other) const {
209
2/2
✓ Branch 1 taken 45 times.
✓ Branch 2 taken 2726 times.
2771 if (!other->IsPlaintext()) {
210 45 return false;
211 }
212
213 const PlaintextSubPattern
214
1/2
✓ Branch 0 taken 2726 times.
✗ Branch 1 not taken.
2726 *pt_other = dynamic_cast<const PlaintextSubPattern *>(other);
215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2726 times.
2726 assert(pt_other != NULL);
216 2726 return chars_ == pt_other->chars_;
217 }
218
219
220 std::string
221 1318 PathspecElementPattern::WildcardSubPattern::GenerateRegularExpression(
222 const bool is_relaxed) const {
223 return (is_relaxed) ? std::string(".*")
224
14/27
✓ Branch 0 taken 371 times.
✓ Branch 1 taken 947 times.
✓ Branch 4 taken 371 times.
✗ Branch 5 not taken.
✓ Branch 8 taken 947 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 947 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 947 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 947 times.
✓ Branch 17 taken 371 times.
✗ Branch 18 not taken.
✓ Branch 19 taken 947 times.
✓ Branch 20 taken 371 times.
✓ Branch 22 taken 947 times.
✓ Branch 23 taken 371 times.
✓ Branch 25 taken 371 times.
✓ Branch 26 taken 947 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.
2636 : std::string("[^") + Pathspec::kSeparator + "]*";
225 }
226
227
228 91 std::string PathspecElementPattern::WildcardSubPattern::GenerateGlobString()
229 const {
230
1/2
✓ Branch 2 taken 91 times.
✗ Branch 3 not taken.
91 return "*";
231 }
232
233
234 445 bool PathspecElementPattern::WildcardSubPattern::Compare(
235 const SubPattern *other) const {
236 445 return other->IsWildcard();
237 }
238
239
240 std::string
241 917 PathspecElementPattern::PlaceholderSubPattern::GenerateRegularExpression(
242 const bool is_relaxed) const {
243 // Note: strict and relaxed regex are the same!
244
3/6
✓ Branch 2 taken 917 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 917 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 917 times.
✗ Branch 9 not taken.
1834 return std::string("[^") + Pathspec::kSeparator + "]";
245 }
246
247 91 std::string PathspecElementPattern::PlaceholderSubPattern::GenerateGlobString()
248 const {
249
1/2
✓ Branch 2 taken 91 times.
✗ Branch 3 not taken.
91 return "?";
250 }
251
252 540 bool PathspecElementPattern::PlaceholderSubPattern::Compare(
253 const SubPattern *other) const {
254 540 return other->IsPlaceholder();
255 }
256