GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/pathspec/pathspec_pattern.cc
Date: 2025-12-21 02:39: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 9891 PathspecElementPattern::PathspecElementPattern(
13 const std::string::const_iterator begin,
14 9891 const std::string::const_iterator &end)
15 9891 : valid_(true) {
16
1/2
✓ Branch 1 taken 9891 times.
✗ Branch 2 not taken.
9891 Parse(begin, end);
17 9891 }
18
19 20830 PathspecElementPattern::PathspecElementPattern(
20 20830 const PathspecElementPattern &other)
21 20830 : valid_(other.valid_) {
22
1/2
✓ Branch 2 taken 20830 times.
✗ Branch 3 not taken.
20830 subpatterns_.reserve(other.subpatterns_.size());
23 20830 SubPatterns::const_iterator i = other.subpatterns_.begin();
24 20830 const SubPatterns::const_iterator iend = other.subpatterns_.end();
25
2/2
✓ Branch 1 taken 24884 times.
✓ Branch 2 taken 20830 times.
45714 for (; i != iend; ++i) {
26
2/4
✓ Branch 2 taken 24884 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 24884 times.
✗ Branch 6 not taken.
24884 subpatterns_.push_back((*i)->Clone());
27 }
28 20830 }
29
30 294 PathspecElementPattern &PathspecElementPattern::operator=(
31 const PathspecElementPattern &other) {
32
1/2
✓ Branch 0 taken 294 times.
✗ Branch 1 not taken.
294 if (this != &other) {
33 294 valid_ = other.valid_;
34 294 subpatterns_.clear();
35
1/2
✓ Branch 2 taken 294 times.
✗ Branch 3 not taken.
294 subpatterns_.reserve(other.subpatterns_.size());
36 294 SubPatterns::const_iterator i = other.subpatterns_.begin();
37 294 const SubPatterns::const_iterator iend = other.subpatterns_.end();
38
2/2
✓ Branch 1 taken 462 times.
✓ Branch 2 taken 294 times.
756 for (; i != iend; ++i) {
39
2/4
✓ Branch 2 taken 462 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 462 times.
✗ Branch 6 not taken.
462 subpatterns_.push_back((*i)->Clone());
40 }
41 }
42
43 294 return *this;
44 }
45
46
47 30715 PathspecElementPattern::~PathspecElementPattern() {
48 30715 SubPatterns::const_iterator i = subpatterns_.begin();
49 30715 const SubPatterns::const_iterator iend = subpatterns_.end();
50
2/2
✓ Branch 2 taken 37565 times.
✓ Branch 3 taken 30715 times.
68280 for (; i != iend; ++i) {
51
1/2
✓ Branch 1 taken 37565 times.
✗ Branch 2 not taken.
37565 delete *i;
52 }
53 30715 subpatterns_.clear();
54 30715 }
55
56
57 9891 void PathspecElementPattern::Parse(const std::string::const_iterator &begin,
58 const std::string::const_iterator &end) {
59 9891 std::string::const_iterator i = begin;
60
2/2
✓ Branch 1 taken 12529 times.
✓ Branch 2 taken 9891 times.
22420 while (i != end) {
61
3/4
✓ Branch 2 taken 2925 times.
✓ Branch 3 taken 9604 times.
✓ Branch 5 taken 2925 times.
✗ Branch 6 not taken.
12529 SubPattern *next = (Pathspec::IsSpecialChar(*i)) ? ParseSpecialChar(end, &i)
62
1/2
✓ Branch 1 taken 9604 times.
✗ Branch 2 not taken.
9604 : ParsePlaintext(end, &i);
63
3/4
✓ Branch 1 taken 12529 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 10 times.
✓ Branch 4 taken 12519 times.
12529 if (next->IsEmpty()) {
64 10 valid_ = false;
65
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 delete next;
66 } else {
67
1/2
✓ Branch 1 taken 12519 times.
✗ Branch 2 not taken.
12519 subpatterns_.push_back(next);
68 }
69 }
70 9891 }
71
72
73 9604 PathspecElementPattern::SubPattern *PathspecElementPattern::ParsePlaintext(
74 const std::string::const_iterator &end, std::string::const_iterator *i) {
75 9604 PlaintextSubPattern *pattern = new PlaintextSubPattern();
76 9604 bool next_is_escaped = false;
77
78
2/2
✓ Branch 1 taken 42463 times.
✓ Branch 2 taken 8575 times.
51038 while (*i < end) {
79
6/6
✓ Branch 2 taken 1565 times.
✓ Branch 3 taken 40898 times.
✓ Branch 4 taken 1029 times.
✓ Branch 5 taken 536 times.
✓ Branch 6 taken 1029 times.
✓ Branch 7 taken 41434 times.
42463 if (Pathspec::IsSpecialChar(**i) && !next_is_escaped) {
80 1029 break;
81 }
82
83
6/6
✓ Branch 1 taken 917 times.
✓ Branch 2 taken 40517 times.
✓ Branch 3 taken 752 times.
✓ Branch 4 taken 165 times.
✓ Branch 5 taken 752 times.
✓ Branch 6 taken 40682 times.
41434 if (**i == Pathspec::kEscaper && !next_is_escaped) {
84 752 next_is_escaped = true;
85
2/2
✓ Branch 0 taken 936 times.
✓ Branch 1 taken 39746 times.
40682 } else if (next_is_escaped) {
86
6/6
✓ Branch 2 taken 400 times.
✓ Branch 3 taken 536 times.
✓ Branch 5 taken 165 times.
✓ Branch 6 taken 235 times.
✓ Branch 7 taken 701 times.
✓ Branch 8 taken 235 times.
936 if (Pathspec::IsSpecialChar(**i) || **i == Pathspec::kEscaper) {
87 701 pattern->AddChar(**i);
88 701 next_is_escaped = false;
89 } else {
90 235 valid_ = false;
91 }
92 } else {
93
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 39746 times.
39746 assert(!Pathspec::IsSpecialChar(**i));
94 39746 pattern->AddChar(**i);
95 }
96
97 41434 ++(*i);
98 }
99
100 9604 return pattern;
101 }
102
103 2925 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 2925 times.
2925 assert(Pathspec::IsSpecialChar(**i));
106 2925 const char chr = **i;
107 2925 ++(*i);
108
109
2/3
✓ Branch 0 taken 1476 times.
✓ Branch 1 taken 1449 times.
✗ Branch 2 not taken.
2925 switch (chr) {
110 1476 case Pathspec::kWildcard:
111 1476 return new WildcardSubPattern();
112 1449 case Pathspec::kPlaceholder:
113 1449 return new PlaceholderSubPattern();
114 default:
115 assert(false && "unrecognized special character");
116 }
117 }
118
119 5975 std::string PathspecElementPattern::GenerateRegularExpression(
120 const bool is_relaxed) const {
121 5975 std::string result;
122 5975 SubPatterns::const_iterator i = subpatterns_.begin();
123 5975 const SubPatterns::const_iterator iend = subpatterns_.end();
124
2/2
✓ Branch 1 taken 8115 times.
✓ Branch 2 taken 5975 times.
14090 for (; i != iend; ++i) {
125
2/4
✓ Branch 2 taken 8115 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 8115 times.
✗ Branch 6 not taken.
8115 result += (*i)->GenerateRegularExpression(is_relaxed);
126 }
127 11950 return result;
128 }
129
130 1402 std::string PathspecElementPattern::GenerateGlobString() const {
131 1402 std::string result;
132 1402 SubPatterns::const_iterator i = subpatterns_.begin();
133 1402 const SubPatterns::const_iterator iend = subpatterns_.end();
134
2/2
✓ Branch 1 taken 1651 times.
✓ Branch 2 taken 1402 times.
3053 for (; i != iend; ++i) {
135
2/4
✓ Branch 2 taken 1651 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1651 times.
✗ Branch 6 not taken.
1651 result += (*i)->GenerateGlobString();
136 }
137 2804 return result;
138 }
139
140 3180 bool PathspecElementPattern::operator==(
141 const PathspecElementPattern &other) const {
142 3180 if (subpatterns_.size() != other.subpatterns_.size()
143
5/6
✓ Branch 0 taken 2888 times.
✓ Branch 1 taken 292 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 2888 times.
✓ Branch 6 taken 292 times.
✓ Branch 7 taken 2888 times.
3180 || IsValid() != other.IsValid()) {
144 292 return false;
145 }
146
147 2888 SubPatterns::const_iterator i = subpatterns_.begin();
148 2888 const SubPatterns::const_iterator iend = subpatterns_.end();
149 2888 SubPatterns::const_iterator j = other.subpatterns_.begin();
150 2888 const SubPatterns::const_iterator jend = other.subpatterns_.end();
151
152
5/6
✓ Branch 3 taken 3476 times.
✓ Branch 4 taken 2316 times.
✓ Branch 6 taken 3476 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 3476 times.
✓ Branch 9 taken 2316 times.
5792 for (; i != iend && j != jend; ++i, ++j) {
153
3/4
✓ Branch 3 taken 3476 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 572 times.
✓ Branch 6 taken 2904 times.
3476 if (!(*i)->Compare(*j)) {
154 572 return false;
155 }
156 }
157
158 2316 return true;
159 }
160
161
162 //------------------------------------------------------------------------------
163
164
165 40447 void PathspecElementPattern::PlaintextSubPattern::AddChar(const char chr) {
166 40447 chars_.push_back(chr);
167 40447 }
168
169 std::string
170 6086 PathspecElementPattern::PlaintextSubPattern::GenerateRegularExpression(
171 const bool is_relaxed) const {
172 // Note: strict and relaxed regex are the same!
173 6086 std::string::const_iterator i = chars_.begin();
174 6086 const std::string::const_iterator iend = chars_.end();
175 6086 std::string regex;
176
2/2
✓ Branch 2 taken 26660 times.
✓ Branch 3 taken 6086 times.
32746 for (; i != iend; ++i) {
177
2/2
✓ Branch 2 taken 2060 times.
✓ Branch 3 taken 24600 times.
26660 if (IsSpecialRegexCharacter(*i)) {
178
1/2
✓ Branch 1 taken 2060 times.
✗ Branch 2 not taken.
2060 regex += "\\";
179 }
180
1/2
✓ Branch 2 taken 26660 times.
✗ Branch 3 not taken.
26660 regex += *i;
181 }
182 12172 return regex;
183 }
184
185
186 1485 std::string PathspecElementPattern::PlaintextSubPattern::GenerateGlobString()
187 const {
188 1485 std::string::const_iterator i = chars_.begin();
189 1485 const std::string::const_iterator iend = chars_.end();
190 1485 std::string glob_string;
191
2/2
✓ Branch 2 taken 7584 times.
✓ Branch 3 taken 1485 times.
9069 for (; i != iend; ++i) {
192
2/2
✓ Branch 2 taken 164 times.
✓ Branch 3 taken 7420 times.
7584 if (Pathspec::IsSpecialChar(*i)) {
193
1/2
✓ Branch 1 taken 164 times.
✗ Branch 2 not taken.
164 glob_string += "\\";
194 }
195
1/2
✓ Branch 2 taken 7584 times.
✗ Branch 3 not taken.
7584 glob_string += *i;
196 }
197 2970 return glob_string;
198 }
199
200 26660 bool PathspecElementPattern::PlaintextSubPattern::IsSpecialRegexCharacter(
201 const char chr) const {
202
8/8
✓ Branch 0 taken 25382 times.
✓ Branch 1 taken 83 times.
✓ Branch 2 taken 25258 times.
✓ Branch 3 taken 124 times.
✓ Branch 4 taken 25092 times.
✓ Branch 5 taken 166 times.
✓ Branch 6 taken 25010 times.
✓ Branch 7 taken 82 times.
25465 return (chr == '.' || chr == '\\' || chr == '*' || chr == '?' || chr == '['
203
10/10
✓ Branch 0 taken 24928 times.
✓ Branch 1 taken 82 times.
✓ Branch 2 taken 24887 times.
✓ Branch 3 taken 41 times.
✓ Branch 4 taken 24846 times.
✓ Branch 5 taken 41 times.
✓ Branch 6 taken 24805 times.
✓ Branch 7 taken 41 times.
✓ Branch 8 taken 24764 times.
✓ Branch 9 taken 41 times.
25010 || chr == ']' || chr == '(' || chr == ')' || chr == '{' || chr == '}'
204
8/8
✓ Branch 0 taken 25465 times.
✓ Branch 1 taken 1195 times.
✓ Branch 2 taken 24723 times.
✓ Branch 3 taken 41 times.
✓ Branch 4 taken 24682 times.
✓ Branch 5 taken 41 times.
✓ Branch 6 taken 82 times.
✓ Branch 7 taken 24600 times.
52125 || chr == '^' || chr == '$' || chr == '+');
205 }
206
207 2544 bool PathspecElementPattern::PlaintextSubPattern::Compare(
208 const SubPattern *other) const {
209
2/2
✓ Branch 1 taken 42 times.
✓ Branch 2 taken 2502 times.
2544 if (!other->IsPlaintext()) {
210 42 return false;
211 }
212
213 const PlaintextSubPattern
214
1/2
✓ Branch 0 taken 2502 times.
✗ Branch 1 not taken.
2502 *pt_other = dynamic_cast<const PlaintextSubPattern *>(other);
215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2502 times.
2502 assert(pt_other != NULL);
216 2502 return chars_ == pt_other->chars_;
217 }
218
219
220 std::string
221 1188 PathspecElementPattern::WildcardSubPattern::GenerateRegularExpression(
222 const bool is_relaxed) const {
223 return (is_relaxed) ? std::string(".*")
224
14/27
✓ Branch 0 taken 325 times.
✓ Branch 1 taken 863 times.
✓ Branch 4 taken 325 times.
✗ Branch 5 not taken.
✓ Branch 8 taken 863 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 863 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 863 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 863 times.
✓ Branch 17 taken 325 times.
✗ Branch 18 not taken.
✓ Branch 19 taken 863 times.
✓ Branch 20 taken 325 times.
✓ Branch 22 taken 863 times.
✓ Branch 23 taken 325 times.
✓ Branch 25 taken 325 times.
✓ Branch 26 taken 863 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.
2376 : std::string("[^") + Pathspec::kSeparator + "]*";
225 }
226
227
228 83 std::string PathspecElementPattern::WildcardSubPattern::GenerateGlobString()
229 const {
230
1/2
✓ Branch 2 taken 83 times.
✗ Branch 3 not taken.
83 return "*";
231 }
232
233
234 428 bool PathspecElementPattern::WildcardSubPattern::Compare(
235 const SubPattern *other) const {
236 428 return other->IsWildcard();
237 }
238
239
240 std::string
241 841 PathspecElementPattern::PlaceholderSubPattern::GenerateRegularExpression(
242 const bool is_relaxed) const {
243 // Note: strict and relaxed regex are the same!
244
3/6
✓ Branch 2 taken 841 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 841 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 841 times.
✗ Branch 9 not taken.
1682 return std::string("[^") + Pathspec::kSeparator + "]";
245 }
246
247 83 std::string PathspecElementPattern::PlaceholderSubPattern::GenerateGlobString()
248 const {
249
1/2
✓ Branch 2 taken 83 times.
✗ Branch 3 not taken.
83 return "?";
250 }
251
252 504 bool PathspecElementPattern::PlaceholderSubPattern::Compare(
253 const SubPattern *other) const {
254 504 return other->IsPlaceholder();
255 }
256