GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/pathspec/pathspec_pattern.cc
Date: 2026-05-03 02:36:16
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 12531 PathspecElementPattern::PathspecElementPattern(
13 const std::string::const_iterator begin,
14 12531 const std::string::const_iterator &end)
15 12531 : valid_(true) {
16
1/2
✓ Branch 1 taken 12531 times.
✗ Branch 2 not taken.
12531 Parse(begin, end);
17 12531 }
18
19 34823 PathspecElementPattern::PathspecElementPattern(
20 34823 const PathspecElementPattern &other)
21 34823 : valid_(other.valid_) {
22
1/2
✓ Branch 2 taken 34823 times.
✗ Branch 3 not taken.
34823 subpatterns_.reserve(other.subpatterns_.size());
23 34823 SubPatterns::const_iterator i = other.subpatterns_.begin();
24 34823 const SubPatterns::const_iterator iend = other.subpatterns_.end();
25
2/2
✓ Branch 1 taken 39498 times.
✓ Branch 2 taken 34823 times.
74321 for (; i != iend; ++i) {
26
2/4
✓ Branch 2 taken 39498 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 39498 times.
✗ Branch 6 not taken.
39498 subpatterns_.push_back((*i)->Clone());
27 }
28 34823 }
29
30 252 PathspecElementPattern &PathspecElementPattern::operator=(
31 const PathspecElementPattern &other) {
32
1/2
✓ Branch 0 taken 252 times.
✗ Branch 1 not taken.
252 if (this != &other) {
33 252 valid_ = other.valid_;
34 252 subpatterns_.clear();
35
1/2
✓ Branch 2 taken 252 times.
✗ Branch 3 not taken.
252 subpatterns_.reserve(other.subpatterns_.size());
36 252 SubPatterns::const_iterator i = other.subpatterns_.begin();
37 252 const SubPatterns::const_iterator iend = other.subpatterns_.end();
38
2/2
✓ Branch 1 taken 396 times.
✓ Branch 2 taken 252 times.
648 for (; i != iend; ++i) {
39
2/4
✓ Branch 2 taken 396 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 396 times.
✗ Branch 6 not taken.
396 subpatterns_.push_back((*i)->Clone());
40 }
41 }
42
43 252 return *this;
44 }
45
46
47 47348 PathspecElementPattern::~PathspecElementPattern() {
48 47348 SubPatterns::const_iterator i = subpatterns_.begin();
49 47348 const SubPatterns::const_iterator iend = subpatterns_.end();
50
2/2
✓ Branch 2 taken 54758 times.
✓ Branch 3 taken 47348 times.
102106 for (; i != iend; ++i) {
51
1/2
✓ Branch 1 taken 54758 times.
✗ Branch 2 not taken.
54758 delete *i;
52 }
53 47348 subpatterns_.clear();
54 47348 }
55
56
57 12531 void PathspecElementPattern::Parse(const std::string::const_iterator &begin,
58 const std::string::const_iterator &end) {
59 12531 std::string::const_iterator i = begin;
60
2/2
✓ Branch 1 taken 15168 times.
✓ Branch 2 taken 12531 times.
27699 while (i != end) {
61
3/4
✓ Branch 2 taken 3696 times.
✓ Branch 3 taken 11472 times.
✓ Branch 5 taken 3696 times.
✗ Branch 6 not taken.
15168 SubPattern *next = (Pathspec::IsSpecialChar(*i)) ? ParseSpecialChar(end, &i)
62
1/2
✓ Branch 1 taken 11472 times.
✗ Branch 2 not taken.
11472 : ParsePlaintext(end, &i);
63
3/4
✓ Branch 1 taken 15168 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 46 times.
✓ Branch 4 taken 15122 times.
15168 if (next->IsEmpty()) {
64 46 valid_ = false;
65
1/2
✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
46 delete next;
66 } else {
67
1/2
✓ Branch 1 taken 15122 times.
✗ Branch 2 not taken.
15122 subpatterns_.push_back(next);
68 }
69 }
70 12531 }
71
72
73 11472 PathspecElementPattern::SubPattern *PathspecElementPattern::ParsePlaintext(
74 const std::string::const_iterator &end, std::string::const_iterator *i) {
75 11472 PlaintextSubPattern *pattern = new PlaintextSubPattern();
76 11472 bool next_is_escaped = false;
77
78
2/2
✓ Branch 1 taken 49148 times.
✓ Branch 2 taken 10454 times.
59602 while (*i < end) {
79
6/6
✓ Branch 2 taken 1499 times.
✓ Branch 3 taken 47649 times.
✓ Branch 4 taken 1018 times.
✓ Branch 5 taken 481 times.
✓ Branch 6 taken 1018 times.
✓ Branch 7 taken 48130 times.
49148 if (Pathspec::IsSpecialChar(**i) && !next_is_escaped) {
80 1018 break;
81 }
82
83
6/6
✓ Branch 1 taken 860 times.
✓ Branch 2 taken 47270 times.
✓ Branch 3 taken 712 times.
✓ Branch 4 taken 148 times.
✓ Branch 5 taken 712 times.
✓ Branch 6 taken 47418 times.
48130 if (**i == Pathspec::kEscaper && !next_is_escaped) {
84 712 next_is_escaped = true;
85
2/2
✓ Branch 0 taken 952 times.
✓ Branch 1 taken 46466 times.
47418 } else if (next_is_escaped) {
86
6/6
✓ Branch 2 taken 471 times.
✓ Branch 3 taken 481 times.
✓ Branch 5 taken 148 times.
✓ Branch 6 taken 323 times.
✓ Branch 7 taken 629 times.
✓ Branch 8 taken 323 times.
952 if (Pathspec::IsSpecialChar(**i) || **i == Pathspec::kEscaper) {
87 629 pattern->AddChar(**i);
88 629 next_is_escaped = false;
89 } else {
90 323 valid_ = false;
91 }
92 } else {
93
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 46466 times.
46466 assert(!Pathspec::IsSpecialChar(**i));
94 46466 pattern->AddChar(**i);
95 }
96
97 48130 ++(*i);
98 }
99
100 11472 return pattern;
101 }
102
103 3696 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 3696 times.
3696 assert(Pathspec::IsSpecialChar(**i));
106 3696 const char chr = **i;
107 3696 ++(*i);
108
109
2/3
✓ Branch 0 taken 2154 times.
✓ Branch 1 taken 1542 times.
✗ Branch 2 not taken.
3696 switch (chr) {
110 2154 case Pathspec::kWildcard:
111 2154 return new WildcardSubPattern();
112 1542 case Pathspec::kPlaceholder:
113 1542 return new PlaceholderSubPattern();
114 default:
115 assert(false && "unrecognized special character");
116 }
117 }
118
119 7130 std::string PathspecElementPattern::GenerateRegularExpression(
120 const bool is_relaxed) const {
121 7130 std::string result;
122 7130 SubPatterns::const_iterator i = subpatterns_.begin();
123 7130 const SubPatterns::const_iterator iend = subpatterns_.end();
124
2/2
✓ Branch 1 taken 9220 times.
✓ Branch 2 taken 7130 times.
16350 for (; i != iend; ++i) {
125
2/4
✓ Branch 2 taken 9220 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 9220 times.
✗ Branch 6 not taken.
9220 result += (*i)->GenerateRegularExpression(is_relaxed);
126 }
127 14260 return result;
128 }
129
130 1250 std::string PathspecElementPattern::GenerateGlobString() const {
131 1250 std::string result;
132 1250 SubPatterns::const_iterator i = subpatterns_.begin();
133 1250 const SubPatterns::const_iterator iend = subpatterns_.end();
134
2/2
✓ Branch 1 taken 1469 times.
✓ Branch 2 taken 1250 times.
2719 for (; i != iend; ++i) {
135
2/4
✓ Branch 2 taken 1469 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1469 times.
✗ Branch 6 not taken.
1469 result += (*i)->GenerateGlobString();
136 }
137 2500 return result;
138 }
139
140 3824 bool PathspecElementPattern::operator==(
141 const PathspecElementPattern &other) const {
142 3824 if (subpatterns_.size() != other.subpatterns_.size()
143
5/6
✓ Branch 0 taken 3418 times.
✓ Branch 1 taken 406 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 3418 times.
✓ Branch 6 taken 406 times.
✓ Branch 7 taken 3418 times.
3824 || IsValid() != other.IsValid()) {
144 406 return false;
145 }
146
147 3418 SubPatterns::const_iterator i = subpatterns_.begin();
148 3418 const SubPatterns::const_iterator iend = subpatterns_.end();
149 3418 SubPatterns::const_iterator j = other.subpatterns_.begin();
150 3418 const SubPatterns::const_iterator jend = other.subpatterns_.end();
151
152
5/6
✓ Branch 3 taken 3936 times.
✓ Branch 4 taken 2619 times.
✓ Branch 6 taken 3936 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 3936 times.
✓ Branch 9 taken 2619 times.
6555 for (; i != iend && j != jend; ++i, ++j) {
153
3/4
✓ Branch 3 taken 3936 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 799 times.
✓ Branch 6 taken 3137 times.
3936 if (!(*i)->Compare(*j)) {
154 799 return false;
155 }
156 }
157
158 2619 return true;
159 }
160
161
162 //------------------------------------------------------------------------------
163
164
165 47095 void PathspecElementPattern::PlaintextSubPattern::AddChar(const char chr) {
166 47095 chars_.push_back(chr);
167 47095 }
168
169 std::string
170 6948 PathspecElementPattern::PlaintextSubPattern::GenerateRegularExpression(
171 const bool is_relaxed) const {
172 // Note: strict and relaxed regex are the same!
173 6948 std::string::const_iterator i = chars_.begin();
174 6948 const std::string::const_iterator iend = chars_.end();
175 6948 std::string regex;
176
2/2
✓ Branch 2 taken 29800 times.
✓ Branch 3 taken 6948 times.
36748 for (; i != iend; ++i) {
177
2/2
✓ Branch 2 taken 2072 times.
✓ Branch 3 taken 27728 times.
29800 if (IsSpecialRegexCharacter(*i)) {
178
1/2
✓ Branch 1 taken 2072 times.
✗ Branch 2 not taken.
2072 regex += "\\";
179 }
180
1/2
✓ Branch 2 taken 29800 times.
✗ Branch 3 not taken.
29800 regex += *i;
181 }
182 13896 return regex;
183 }
184
185
186 1323 std::string PathspecElementPattern::PlaintextSubPattern::GenerateGlobString()
187 const {
188 1323 std::string::const_iterator i = chars_.begin();
189 1323 const std::string::const_iterator iend = chars_.end();
190 1323 std::string glob_string;
191
2/2
✓ Branch 2 taken 6768 times.
✓ Branch 3 taken 1323 times.
8091 for (; i != iend; ++i) {
192
2/2
✓ Branch 2 taken 148 times.
✓ Branch 3 taken 6620 times.
6768 if (Pathspec::IsSpecialChar(*i)) {
193
1/2
✓ Branch 1 taken 148 times.
✗ Branch 2 not taken.
148 glob_string += "\\";
194 }
195
1/2
✓ Branch 2 taken 6768 times.
✗ Branch 3 not taken.
6768 glob_string += *i;
196 }
197 2646 return glob_string;
198 }
199
200 29800 bool PathspecElementPattern::PlaintextSubPattern::IsSpecialRegexCharacter(
201 const char chr) const {
202
8/8
✓ Branch 0 taken 28431 times.
✓ Branch 1 taken 74 times.
✓ Branch 2 taken 28320 times.
✓ Branch 3 taken 111 times.
✓ Branch 4 taken 28172 times.
✓ Branch 5 taken 148 times.
✓ Branch 6 taken 28098 times.
✓ Branch 7 taken 74 times.
28505 return (chr == '.' || chr == '\\' || chr == '*' || chr == '?' || chr == '['
203
10/10
✓ Branch 0 taken 28024 times.
✓ Branch 1 taken 74 times.
✓ Branch 2 taken 27987 times.
✓ Branch 3 taken 37 times.
✓ Branch 4 taken 27950 times.
✓ Branch 5 taken 37 times.
✓ Branch 6 taken 27913 times.
✓ Branch 7 taken 37 times.
✓ Branch 8 taken 27876 times.
✓ Branch 9 taken 37 times.
28098 || chr == ']' || chr == '(' || chr == ')' || chr == '{' || chr == '}'
204
8/8
✓ Branch 0 taken 28505 times.
✓ Branch 1 taken 1295 times.
✓ Branch 2 taken 27839 times.
✓ Branch 3 taken 37 times.
✓ Branch 4 taken 27802 times.
✓ Branch 5 taken 37 times.
✓ Branch 6 taken 74 times.
✓ Branch 7 taken 27728 times.
58305 || chr == '^' || chr == '$' || chr == '+');
205 }
206
207 2929 bool PathspecElementPattern::PlaintextSubPattern::Compare(
208 const SubPattern *other) const {
209
2/2
✓ Branch 1 taken 37 times.
✓ Branch 2 taken 2892 times.
2929 if (!other->IsPlaintext()) {
210 37 return false;
211 }
212
213 const PlaintextSubPattern
214
1/2
✓ Branch 0 taken 2892 times.
✗ Branch 1 not taken.
2892 *pt_other = dynamic_cast<const PlaintextSubPattern *>(other);
215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2892 times.
2892 assert(pt_other != NULL);
216 2892 return chars_ == pt_other->chars_;
217 }
218
219
220 std::string
221 1489 PathspecElementPattern::WildcardSubPattern::GenerateRegularExpression(
222 const bool is_relaxed) const {
223 return (is_relaxed) ? std::string(".*")
224
14/27
✓ Branch 0 taken 493 times.
✓ Branch 1 taken 996 times.
✓ Branch 4 taken 493 times.
✗ Branch 5 not taken.
✓ Branch 8 taken 996 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 996 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 996 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 996 times.
✓ Branch 17 taken 493 times.
✗ Branch 18 not taken.
✓ Branch 19 taken 996 times.
✓ Branch 20 taken 493 times.
✓ Branch 22 taken 996 times.
✓ Branch 23 taken 493 times.
✓ Branch 25 taken 493 times.
✓ Branch 26 taken 996 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.
2978 : std::string("[^") + Pathspec::kSeparator + "]*";
225 }
226
227
228 73 std::string PathspecElementPattern::WildcardSubPattern::GenerateGlobString()
229 const {
230
1/2
✓ Branch 2 taken 73 times.
✗ Branch 3 not taken.
73 return "*";
231 }
232
233
234 563 bool PathspecElementPattern::WildcardSubPattern::Compare(
235 const SubPattern *other) const {
236 563 return other->IsWildcard();
237 }
238
239
240 std::string
241 783 PathspecElementPattern::PlaceholderSubPattern::GenerateRegularExpression(
242 const bool is_relaxed) const {
243 // Note: strict and relaxed regex are the same!
244
3/6
✓ Branch 2 taken 783 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 783 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 783 times.
✗ Branch 9 not taken.
1566 return std::string("[^") + Pathspec::kSeparator + "]";
245 }
246
247 73 std::string PathspecElementPattern::PlaceholderSubPattern::GenerateGlobString()
248 const {
249
1/2
✓ Branch 2 taken 73 times.
✗ Branch 3 not taken.
73 return "?";
250 }
251
252 444 bool PathspecElementPattern::PlaceholderSubPattern::Compare(
253 const SubPattern *other) const {
254 444 return other->IsPlaceholder();
255 }
256