GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/pathspec/pathspec_pattern.cc
Date: 2026-11-01 02:41:19
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 19534 PathspecElementPattern::PathspecElementPattern(
13 const std::string::const_iterator begin,
14 19534 const std::string::const_iterator &end)
15 19534 : valid_(true) {
16
1/2
✓ Branch 1 taken 19534 times.
✗ Branch 2 not taken.
19534 Parse(begin, end);
17 19534 }
18
19 58520 PathspecElementPattern::PathspecElementPattern(
20 58520 const PathspecElementPattern &other)
21 58520 : valid_(other.valid_) {
22
1/2
✓ Branch 2 taken 58520 times.
✗ Branch 3 not taken.
58520 subpatterns_.reserve(other.subpatterns_.size());
23 58520 SubPatterns::const_iterator i = other.subpatterns_.begin();
24 58520 const SubPatterns::const_iterator iend = other.subpatterns_.end();
25
2/2
✓ Branch 1 taken 63918 times.
✓ Branch 2 taken 58520 times.
122438 for (; i != iend; ++i) {
26
2/4
✓ Branch 2 taken 63918 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 63918 times.
✗ Branch 6 not taken.
63918 subpatterns_.push_back((*i)->Clone());
27 }
28 58520 }
29
30 343 PathspecElementPattern &PathspecElementPattern::operator=(
31 const PathspecElementPattern &other) {
32
1/2
✓ Branch 0 taken 343 times.
✗ Branch 1 not taken.
343 if (this != &other) {
33 343 valid_ = other.valid_;
34 343 subpatterns_.clear();
35
1/2
✓ Branch 2 taken 343 times.
✗ Branch 3 not taken.
343 subpatterns_.reserve(other.subpatterns_.size());
36 343 SubPatterns::const_iterator i = other.subpatterns_.begin();
37 343 const SubPatterns::const_iterator iend = other.subpatterns_.end();
38
2/2
✓ Branch 1 taken 539 times.
✓ Branch 2 taken 343 times.
882 for (; i != iend; ++i) {
39
2/4
✓ Branch 2 taken 539 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 539 times.
✗ Branch 6 not taken.
539 subpatterns_.push_back((*i)->Clone());
40 }
41 }
42
43 343 return *this;
44 }
45
46
47 78048 PathspecElementPattern::~PathspecElementPattern() {
48 78048 SubPatterns::const_iterator i = subpatterns_.begin();
49 78048 const SubPatterns::const_iterator iend = subpatterns_.end();
50
2/2
✓ Branch 2 taken 86878 times.
✓ Branch 3 taken 78048 times.
164926 for (; i != iend; ++i) {
51
1/2
✓ Branch 1 taken 86878 times.
✗ Branch 2 not taken.
86878 delete *i;
52 }
53 78048 subpatterns_.clear();
54 78048 }
55
56
57 19534 void PathspecElementPattern::Parse(const std::string::const_iterator &begin,
58 const std::string::const_iterator &end) {
59 19534 std::string::const_iterator i = begin;
60
2/2
✓ Branch 1 taken 22796 times.
✓ Branch 2 taken 19534 times.
42330 while (i != end) {
61
3/4
✓ Branch 2 taken 4088 times.
✓ Branch 3 taken 18708 times.
✓ Branch 5 taken 4088 times.
✗ Branch 6 not taken.
22796 SubPattern *next = (Pathspec::IsSpecialChar(*i)) ? ParseSpecialChar(end, &i)
62
1/2
✓ Branch 1 taken 18708 times.
✗ Branch 2 not taken.
18708 : ParsePlaintext(end, &i);
63
3/4
✓ Branch 1 taken 22796 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 26 times.
✓ Branch 4 taken 22770 times.
22796 if (next->IsEmpty()) {
64 26 valid_ = false;
65
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
26 delete next;
66 } else {
67
1/2
✓ Branch 1 taken 22770 times.
✗ Branch 2 not taken.
22770 subpatterns_.push_back(next);
68 }
69 }
70 19534 }
71
72
73 18708 PathspecElementPattern::SubPattern *PathspecElementPattern::ParsePlaintext(
74 const std::string::const_iterator &end, std::string::const_iterator *i) {
75 18708 PlaintextSubPattern *pattern = new PlaintextSubPattern();
76 18708 bool next_is_escaped = false;
77
78
2/2
✓ Branch 1 taken 80783 times.
✓ Branch 2 taken 17460 times.
98243 while (*i < end) {
79
6/6
✓ Branch 2 taken 1877 times.
✓ Branch 3 taken 78906 times.
✓ Branch 4 taken 1248 times.
✓ Branch 5 taken 629 times.
✓ Branch 6 taken 1248 times.
✓ Branch 7 taken 79535 times.
80783 if (Pathspec::IsSpecialChar(**i) && !next_is_escaped) {
80 1248 break;
81 }
82
83
6/6
✓ Branch 1 taken 1092 times.
✓ Branch 2 taken 78443 times.
✓ Branch 3 taken 898 times.
✓ Branch 4 taken 194 times.
✓ Branch 5 taken 898 times.
✓ Branch 6 taken 78637 times.
79535 if (**i == Pathspec::kEscaper && !next_is_escaped) {
84 898 next_is_escaped = true;
85
2/2
✓ Branch 0 taken 1146 times.
✓ Branch 1 taken 77491 times.
78637 } else if (next_is_escaped) {
86
6/6
✓ Branch 2 taken 517 times.
✓ Branch 3 taken 629 times.
✓ Branch 5 taken 194 times.
✓ Branch 6 taken 323 times.
✓ Branch 7 taken 823 times.
✓ Branch 8 taken 323 times.
1146 if (Pathspec::IsSpecialChar(**i) || **i == Pathspec::kEscaper) {
87 823 pattern->AddChar(**i);
88 823 next_is_escaped = false;
89 } else {
90 323 valid_ = false;
91 }
92 } else {
93
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 77491 times.
77491 assert(!Pathspec::IsSpecialChar(**i));
94 77491 pattern->AddChar(**i);
95 }
96
97 79535 ++(*i);
98 }
99
100 18708 return pattern;
101 }
102
103 4088 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 4088 times.
4088 assert(Pathspec::IsSpecialChar(**i));
106 4088 const char chr = **i;
107 4088 ++(*i);
108
109
2/3
✓ Branch 0 taken 2303 times.
✓ Branch 1 taken 1785 times.
✗ Branch 2 not taken.
4088 switch (chr) {
110 2303 case Pathspec::kWildcard:
111 2303 return new WildcardSubPattern();
112 1785 case Pathspec::kPlaceholder:
113 1785 return new PlaceholderSubPattern();
114 ✗ default:
115 ✗ assert(false && "unrecognized special character");
116 }
117 }
118
119 13194 std::string PathspecElementPattern::GenerateRegularExpression(
120 const bool is_relaxed) const {
121 13194 std::string result;
122 13194 SubPatterns::const_iterator i = subpatterns_.begin();
123 13194 const SubPatterns::const_iterator iend = subpatterns_.end();
124
2/2
✓ Branch 1 taken 15844 times.
✓ Branch 2 taken 13194 times.
29038 for (; i != iend; ++i) {
125
2/4
✓ Branch 2 taken 15844 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 15844 times.
✗ Branch 6 not taken.
15844 result += (*i)->GenerateRegularExpression(is_relaxed);
126 }
127 26388 return result;
128 }
129
130 1644 std::string PathspecElementPattern::GenerateGlobString() const {
131 1644 std::string result;
132 1644 SubPatterns::const_iterator i = subpatterns_.begin();
133 1644 const SubPatterns::const_iterator iend = subpatterns_.end();
134
2/2
✓ Branch 1 taken 1938 times.
✓ Branch 2 taken 1644 times.
3582 for (; i != iend; ++i) {
135
2/4
✓ Branch 2 taken 1938 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1938 times.
✗ Branch 6 not taken.
1938 result += (*i)->GenerateGlobString();
136 }
137 3288 return result;
138 }
139
140 4344 bool PathspecElementPattern::operator==(
141 const PathspecElementPattern &other) const {
142 4344 if (subpatterns_.size() != other.subpatterns_.size()
143
5/6
✓ Branch 0 taken 3946 times.
✓ Branch 1 taken 398 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 3946 times.
✓ Branch 6 taken 398 times.
✓ Branch 7 taken 3946 times.
4344 || IsValid() != other.IsValid()) {
144 398 return false;
145 }
146
147 3946 SubPatterns::const_iterator i = subpatterns_.begin();
148 3946 const SubPatterns::const_iterator iend = subpatterns_.end();
149 3946 SubPatterns::const_iterator j = other.subpatterns_.begin();
150 3946 const SubPatterns::const_iterator jend = other.subpatterns_.end();
151
152
5/6
✓ Branch 3 taken 4632 times.
✓ Branch 4 taken 3015 times.
✓ Branch 6 taken 4632 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 4632 times.
✓ Branch 9 taken 3015 times.
7647 for (; i != iend && j != jend; ++i, ++j) {
153
3/4
✓ Branch 3 taken 4632 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 931 times.
✓ Branch 6 taken 3701 times.
4632 if (!(*i)->Compare(*j)) {
154 931 return false;
155 }
156 }
157
158 3015 return true;
159 }
160
161
162 //------------------------------------------------------------------------------
163
164
165 78314 void PathspecElementPattern::PlaintextSubPattern::AddChar(const char chr) {
166 78314 chars_.push_back(chr);
167 78314 }
168
169 std::string
170 13071 PathspecElementPattern::PlaintextSubPattern::GenerateRegularExpression(
171 const bool is_relaxed) const {
172 // Note: strict and relaxed regex are the same!
173 13071 std::string::const_iterator i = chars_.begin();
174 13071 const std::string::const_iterator iend = chars_.end();
175 13071 std::string regex;
176
2/2
✓ Branch 2 taken 57241 times.
✓ Branch 3 taken 13071 times.
70312 for (; i != iend; ++i) {
177
2/2
✓ Branch 2 taken 2563 times.
✓ Branch 3 taken 54678 times.
57241 if (IsSpecialRegexCharacter(*i)) {
178
1/2
✓ Branch 1 taken 2563 times.
✗ Branch 2 not taken.
2563 regex += "\\";
179 }
180
1/2
✓ Branch 2 taken 57241 times.
✗ Branch 3 not taken.
57241 regex += *i;
181 }
182 26142 return regex;
183 }
184
185
186 1742 std::string PathspecElementPattern::PlaintextSubPattern::GenerateGlobString()
187 const {
188 1742 std::string::const_iterator i = chars_.begin();
189 1742 const std::string::const_iterator iend = chars_.end();
190 1742 std::string glob_string;
191
2/2
✓ Branch 2 taken 8892 times.
✓ Branch 3 taken 1742 times.
10634 for (; i != iend; ++i) {
192
2/2
✓ Branch 2 taken 192 times.
✓ Branch 3 taken 8700 times.
8892 if (Pathspec::IsSpecialChar(*i)) {
193
1/2
✓ Branch 1 taken 192 times.
✗ Branch 2 not taken.
192 glob_string += "\\";
194 }
195
1/2
✓ Branch 2 taken 8892 times.
✗ Branch 3 not taken.
8892 glob_string += *i;
196 }
197 3484 return glob_string;
198 }
199
200 57241 bool PathspecElementPattern::PlaintextSubPattern::IsSpecialRegexCharacter(
201 const char chr) const {
202
8/8
✓ Branch 0 taken 55593 times.
✓ Branch 1 taken 97 times.
✓ Branch 2 taken 55448 times.
✓ Branch 3 taken 145 times.
✓ Branch 4 taken 55254 times.
✓ Branch 5 taken 194 times.
✓ Branch 6 taken 55158 times.
✓ Branch 7 taken 96 times.
55690 return (chr == '.' || chr == '\\' || chr == '*' || chr == '?' || chr == '['
203
10/10
✓ Branch 0 taken 55062 times.
✓ Branch 1 taken 96 times.
✓ Branch 2 taken 55014 times.
✓ Branch 3 taken 48 times.
✓ Branch 4 taken 54966 times.
✓ Branch 5 taken 48 times.
✓ Branch 6 taken 54918 times.
✓ Branch 7 taken 48 times.
✓ Branch 8 taken 54870 times.
✓ Branch 9 taken 48 times.
55158 || chr == ']' || chr == '(' || chr == ')' || chr == '{' || chr == '}'
204
8/8
✓ Branch 0 taken 55690 times.
✓ Branch 1 taken 1551 times.
✓ Branch 2 taken 54822 times.
✓ Branch 3 taken 48 times.
✓ Branch 4 taken 54774 times.
✓ Branch 5 taken 48 times.
✓ Branch 6 taken 96 times.
✓ Branch 7 taken 54678 times.
112931 || chr == '^' || chr == '$' || chr == '+');
205 }
206
207 3473 bool PathspecElementPattern::PlaintextSubPattern::Compare(
208 const SubPattern *other) const {
209
2/2
✓ Branch 1 taken 49 times.
✓ Branch 2 taken 3424 times.
3473 if (!other->IsPlaintext()) {
210 49 return false;
211 }
212
213 const PlaintextSubPattern
214
1/2
✓ Branch 0 taken 3424 times.
✗ Branch 1 not taken.
3424 *pt_other = dynamic_cast<const PlaintextSubPattern *>(other);
215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3424 times.
3424 assert(pt_other != NULL);
216 3424 return chars_ == pt_other->chars_;
217 }
218
219
220 std::string
221 1775 PathspecElementPattern::WildcardSubPattern::GenerateRegularExpression(
222 const bool is_relaxed) const {
223 return (is_relaxed) ? std::string(".*")
224
14/27
✓ Branch 0 taken 546 times.
✓ Branch 1 taken 1229 times.
✓ Branch 4 taken 546 times.
✗ Branch 5 not taken.
✓ Branch 8 taken 1229 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 1229 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 1229 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 1229 times.
✓ Branch 17 taken 546 times.
✗ Branch 18 not taken.
✓ Branch 19 taken 1229 times.
✓ Branch 20 taken 546 times.
✓ Branch 22 taken 1229 times.
✓ Branch 23 taken 546 times.
✓ Branch 25 taken 546 times.
✓ Branch 26 taken 1229 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.
3550 : std::string("[^") + Pathspec::kSeparator + "]*";
225 }
226
227
228 98 std::string PathspecElementPattern::WildcardSubPattern::GenerateGlobString()
229 const {
230
1/2
✓ Branch 2 taken 98 times.
✗ Branch 3 not taken.
98 return "*";
231 }
232
233
234 571 bool PathspecElementPattern::WildcardSubPattern::Compare(
235 const SubPattern *other) const {
236 571 return other->IsWildcard();
237 }
238
239
240 std::string
241 998 PathspecElementPattern::PlaceholderSubPattern::GenerateRegularExpression(
242 const bool is_relaxed) const {
243 // Note: strict and relaxed regex are the same!
244
3/6
✓ Branch 2 taken 998 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 998 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 998 times.
✗ Branch 9 not taken.
1996 return std::string("[^") + Pathspec::kSeparator + "]";
245 }
246
247 98 std::string PathspecElementPattern::PlaceholderSubPattern::GenerateGlobString()
248 const {
249
1/2
✓ Branch 2 taken 98 times.
✗ Branch 3 not taken.
98 return "?";
250 }
251
252 588 bool PathspecElementPattern::PlaceholderSubPattern::Compare(
253 const SubPattern *other) const {
254 588 return other->IsPlaceholder();
255 }
256