GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/pathspec/pathspec_pattern.cc
Date: 2025-10-19 02:35:28
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 13900 PathspecElementPattern::PathspecElementPattern(
13 const std::string::const_iterator begin,
14 13900 const std::string::const_iterator &end)
15 13900 : valid_(true) {
16
1/2
✓ Branch 1 taken 13900 times.
✗ Branch 2 not taken.
13900 Parse(begin, end);
17 13900 }
18
19 37356 PathspecElementPattern::PathspecElementPattern(
20 37356 const PathspecElementPattern &other)
21 37356 : valid_(other.valid_) {
22
1/2
✓ Branch 2 taken 37356 times.
✗ Branch 3 not taken.
37356 subpatterns_.reserve(other.subpatterns_.size());
23 37356 SubPatterns::const_iterator i = other.subpatterns_.begin();
24 37356 const SubPatterns::const_iterator iend = other.subpatterns_.end();
25
2/2
✓ Branch 1 taken 42497 times.
✓ Branch 2 taken 37356 times.
79853 for (; i != iend; ++i) {
26
2/4
✓ Branch 2 taken 42497 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 42497 times.
✗ Branch 6 not taken.
42497 subpatterns_.push_back((*i)->Clone());
27 }
28 37356 }
29
30 308 PathspecElementPattern &PathspecElementPattern::operator=(
31 const PathspecElementPattern &other) {
32
1/2
✓ Branch 0 taken 308 times.
✗ Branch 1 not taken.
308 if (this != &other) {
33 308 valid_ = other.valid_;
34 308 subpatterns_.clear();
35
1/2
✓ Branch 2 taken 308 times.
✗ Branch 3 not taken.
308 subpatterns_.reserve(other.subpatterns_.size());
36 308 SubPatterns::const_iterator i = other.subpatterns_.begin();
37 308 const SubPatterns::const_iterator iend = other.subpatterns_.end();
38
2/2
✓ Branch 1 taken 484 times.
✓ Branch 2 taken 308 times.
792 for (; i != iend; ++i) {
39
2/4
✓ Branch 2 taken 484 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 484 times.
✗ Branch 6 not taken.
484 subpatterns_.push_back((*i)->Clone());
40 }
41 }
42
43 308 return *this;
44 }
45
46
47 51250 PathspecElementPattern::~PathspecElementPattern() {
48 51250 SubPatterns::const_iterator i = subpatterns_.begin();
49 51250 const SubPatterns::const_iterator iend = subpatterns_.end();
50
2/2
✓ Branch 2 taken 59530 times.
✓ Branch 3 taken 51250 times.
110780 for (; i != iend; ++i) {
51
1/2
✓ Branch 1 taken 59530 times.
✗ Branch 2 not taken.
59530 delete *i;
52 }
53 51250 subpatterns_.clear();
54 51250 }
55
56
57 13900 void PathspecElementPattern::Parse(const std::string::const_iterator &begin,
58 const std::string::const_iterator &end) {
59 13900 std::string::const_iterator i = begin;
60
2/2
✓ Branch 1 taken 16903 times.
✓ Branch 2 taken 13900 times.
30803 while (i != end) {
61
3/4
✓ Branch 2 taken 3973 times.
✓ Branch 3 taken 12930 times.
✓ Branch 5 taken 3973 times.
✗ Branch 6 not taken.
16903 SubPattern *next = (Pathspec::IsSpecialChar(*i)) ? ParseSpecialChar(end, &i)
62
1/2
✓ Branch 1 taken 12930 times.
✗ Branch 2 not taken.
12930 : ParsePlaintext(end, &i);
63
3/4
✓ Branch 1 taken 16903 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 40 times.
✓ Branch 4 taken 16863 times.
16903 if (next->IsEmpty()) {
64 40 valid_ = false;
65
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 delete next;
66 } else {
67
1/2
✓ Branch 1 taken 16863 times.
✗ Branch 2 not taken.
16863 subpatterns_.push_back(next);
68 }
69 }
70 13900 }
71
72
73 12930 PathspecElementPattern::SubPattern *PathspecElementPattern::ParsePlaintext(
74 const std::string::const_iterator &end, std::string::const_iterator *i) {
75 12930 PlaintextSubPattern *pattern = new PlaintextSubPattern();
76 12930 bool next_is_escaped = false;
77
78
2/2
✓ Branch 1 taken 56031 times.
✓ Branch 2 taken 11772 times.
67803 while (*i < end) {
79
6/6
✓ Branch 2 taken 1721 times.
✓ Branch 3 taken 54310 times.
✓ Branch 4 taken 1158 times.
✓ Branch 5 taken 563 times.
✓ Branch 6 taken 1158 times.
✓ Branch 7 taken 54873 times.
56031 if (Pathspec::IsSpecialChar(**i) && !next_is_escaped) {
80 1158 break;
81 }
82
83
6/6
✓ Branch 1 taken 992 times.
✓ Branch 2 taken 53881 times.
✓ Branch 3 taken 819 times.
✓ Branch 4 taken 173 times.
✓ Branch 5 taken 819 times.
✓ Branch 6 taken 54054 times.
54873 if (**i == Pathspec::kEscaper && !next_is_escaped) {
84 819 next_is_escaped = true;
85
2/2
✓ Branch 0 taken 1071 times.
✓ Branch 1 taken 52983 times.
54054 } else if (next_is_escaped) {
86
6/6
✓ Branch 2 taken 508 times.
✓ Branch 3 taken 563 times.
✓ Branch 5 taken 173 times.
✓ Branch 6 taken 335 times.
✓ Branch 7 taken 736 times.
✓ Branch 8 taken 335 times.
1071 if (Pathspec::IsSpecialChar(**i) || **i == Pathspec::kEscaper) {
87 736 pattern->AddChar(**i);
88 736 next_is_escaped = false;
89 } else {
90 335 valid_ = false;
91 }
92 } else {
93
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 52983 times.
52983 assert(!Pathspec::IsSpecialChar(**i));
94 52983 pattern->AddChar(**i);
95 }
96
97 54873 ++(*i);
98 }
99
100 12930 return pattern;
101 }
102
103 3973 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 3973 times.
3973 assert(Pathspec::IsSpecialChar(**i));
106 3973 const char chr = **i;
107 3973 ++(*i);
108
109
2/3
✓ Branch 0 taken 2267 times.
✓ Branch 1 taken 1706 times.
✗ Branch 2 not taken.
3973 switch (chr) {
110 2267 case Pathspec::kWildcard:
111 2267 return new WildcardSubPattern();
112 1706 case Pathspec::kPlaceholder:
113 1706 return new PlaceholderSubPattern();
114 default:
115 assert(false && "unrecognized special character");
116 }
117 }
118
119 8282 std::string PathspecElementPattern::GenerateRegularExpression(
120 const bool is_relaxed) const {
121 8282 std::string result;
122 8282 SubPatterns::const_iterator i = subpatterns_.begin();
123 8282 const SubPatterns::const_iterator iend = subpatterns_.end();
124
2/2
✓ Branch 1 taken 10694 times.
✓ Branch 2 taken 8282 times.
18976 for (; i != iend; ++i) {
125
2/4
✓ Branch 2 taken 10694 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 10694 times.
✗ Branch 6 not taken.
10694 result += (*i)->GenerateRegularExpression(is_relaxed);
126 }
127 16564 return result;
128 }
129
130 1470 std::string PathspecElementPattern::GenerateGlobString() const {
131 1470 std::string result;
132 1470 SubPatterns::const_iterator i = subpatterns_.begin();
133 1470 const SubPatterns::const_iterator iend = subpatterns_.end();
134
2/2
✓ Branch 1 taken 1731 times.
✓ Branch 2 taken 1470 times.
3201 for (; i != iend; ++i) {
135
2/4
✓ Branch 2 taken 1731 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1731 times.
✗ Branch 6 not taken.
1731 result += (*i)->GenerateGlobString();
136 }
137 2940 return result;
138 }
139
140 4140 bool PathspecElementPattern::operator==(
141 const PathspecElementPattern &other) const {
142 4140 if (subpatterns_.size() != other.subpatterns_.size()
143
5/6
✓ Branch 0 taken 3722 times.
✓ Branch 1 taken 418 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 3722 times.
✓ Branch 6 taken 418 times.
✓ Branch 7 taken 3722 times.
4140 || IsValid() != other.IsValid()) {
144 418 return false;
145 }
146
147 3722 SubPatterns::const_iterator i = subpatterns_.begin();
148 3722 const SubPatterns::const_iterator iend = subpatterns_.end();
149 3722 SubPatterns::const_iterator j = other.subpatterns_.begin();
150 3722 const SubPatterns::const_iterator jend = other.subpatterns_.end();
151
152
5/6
✓ Branch 3 taken 4324 times.
✓ Branch 4 taken 2859 times.
✓ Branch 6 taken 4324 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 4324 times.
✓ Branch 9 taken 2859 times.
7183 for (; i != iend && j != jend; ++i, ++j) {
153
3/4
✓ Branch 3 taken 4324 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 863 times.
✓ Branch 6 taken 3461 times.
4324 if (!(*i)->Compare(*j)) {
154 863 return false;
155 }
156 }
157
158 2859 return true;
159 }
160
161
162 //------------------------------------------------------------------------------
163
164
165 53719 void PathspecElementPattern::PlaintextSubPattern::AddChar(const char chr) {
166 53719 chars_.push_back(chr);
167 53719 }
168
169 std::string
170 8155 PathspecElementPattern::PlaintextSubPattern::GenerateRegularExpression(
171 const bool is_relaxed) const {
172 // Note: strict and relaxed regex are the same!
173 8155 std::string::const_iterator i = chars_.begin();
174 8155 const std::string::const_iterator iend = chars_.end();
175 8155 std::string regex;
176
2/2
✓ Branch 2 taken 35386 times.
✓ Branch 3 taken 8155 times.
43541 for (; i != iend; ++i) {
177
2/2
✓ Branch 2 taken 2374 times.
✓ Branch 3 taken 33012 times.
35386 if (IsSpecialRegexCharacter(*i)) {
178
1/2
✓ Branch 1 taken 2374 times.
✗ Branch 2 not taken.
2374 regex += "\\";
179 }
180
1/2
✓ Branch 2 taken 35386 times.
✗ Branch 3 not taken.
35386 regex += *i;
181 }
182 16310 return regex;
183 }
184
185
186 1557 std::string PathspecElementPattern::PlaintextSubPattern::GenerateGlobString()
187 const {
188 1557 std::string::const_iterator i = chars_.begin();
189 1557 const std::string::const_iterator iend = chars_.end();
190 1557 std::string glob_string;
191
2/2
✓ Branch 2 taken 7952 times.
✓ Branch 3 taken 1557 times.
9509 for (; i != iend; ++i) {
192
2/2
✓ Branch 2 taken 172 times.
✓ Branch 3 taken 7780 times.
7952 if (Pathspec::IsSpecialChar(*i)) {
193
1/2
✓ Branch 1 taken 172 times.
✗ Branch 2 not taken.
172 glob_string += "\\";
194 }
195
1/2
✓ Branch 2 taken 7952 times.
✗ Branch 3 not taken.
7952 glob_string += *i;
196 }
197 3114 return glob_string;
198 }
199
200 35386 bool PathspecElementPattern::PlaintextSubPattern::IsSpecialRegexCharacter(
201 const char chr) const {
202
8/8
✓ Branch 0 taken 33845 times.
✓ Branch 1 taken 87 times.
✓ Branch 2 taken 33714 times.
✓ Branch 3 taken 131 times.
✓ Branch 4 taken 33540 times.
✓ Branch 5 taken 174 times.
✓ Branch 6 taken 33452 times.
✓ Branch 7 taken 88 times.
33932 return (chr == '.' || chr == '\\' || chr == '*' || chr == '?' || chr == '['
203
10/10
✓ Branch 0 taken 33364 times.
✓ Branch 1 taken 88 times.
✓ Branch 2 taken 33320 times.
✓ Branch 3 taken 44 times.
✓ Branch 4 taken 33276 times.
✓ Branch 5 taken 44 times.
✓ Branch 6 taken 33232 times.
✓ Branch 7 taken 44 times.
✓ Branch 8 taken 33188 times.
✓ Branch 9 taken 44 times.
33452 || chr == ']' || chr == '(' || chr == ')' || chr == '{' || chr == '}'
204
8/8
✓ Branch 0 taken 33932 times.
✓ Branch 1 taken 1454 times.
✓ Branch 2 taken 33144 times.
✓ Branch 3 taken 44 times.
✓ Branch 4 taken 33100 times.
✓ Branch 5 taken 44 times.
✓ Branch 6 taken 88 times.
✓ Branch 7 taken 33012 times.
69318 || chr == '^' || chr == '$' || chr == '+');
205 }
206
207 3221 bool PathspecElementPattern::PlaintextSubPattern::Compare(
208 const SubPattern *other) const {
209
2/2
✓ Branch 1 taken 43 times.
✓ Branch 2 taken 3178 times.
3221 if (!other->IsPlaintext()) {
210 43 return false;
211 }
212
213 const PlaintextSubPattern
214
1/2
✓ Branch 0 taken 3178 times.
✗ Branch 1 not taken.
3178 *pt_other = dynamic_cast<const PlaintextSubPattern *>(other);
215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3178 times.
3178 assert(pt_other != NULL);
216 3178 return chars_ == pt_other->chars_;
217 }
218
219
220 std::string
221 1630 PathspecElementPattern::WildcardSubPattern::GenerateRegularExpression(
222 const bool is_relaxed) const {
223 return (is_relaxed) ? std::string(".*")
224
14/27
✓ Branch 0 taken 525 times.
✓ Branch 1 taken 1105 times.
✓ Branch 4 taken 525 times.
✗ Branch 5 not taken.
✓ Branch 8 taken 1105 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 1105 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 1105 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 1105 times.
✓ Branch 17 taken 525 times.
✗ Branch 18 not taken.
✓ Branch 19 taken 1105 times.
✓ Branch 20 taken 525 times.
✓ Branch 22 taken 1105 times.
✓ Branch 23 taken 525 times.
✓ Branch 25 taken 525 times.
✓ Branch 26 taken 1105 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.
3260 : std::string("[^") + Pathspec::kSeparator + "]*";
225 }
226
227
228 87 std::string PathspecElementPattern::WildcardSubPattern::GenerateGlobString()
229 const {
230
1/2
✓ Branch 2 taken 87 times.
✗ Branch 3 not taken.
87 return "*";
231 }
232
233
234 587 bool PathspecElementPattern::WildcardSubPattern::Compare(
235 const SubPattern *other) const {
236 587 return other->IsWildcard();
237 }
238
239
240 std::string
241 909 PathspecElementPattern::PlaceholderSubPattern::GenerateRegularExpression(
242 const bool is_relaxed) const {
243 // Note: strict and relaxed regex are the same!
244
3/6
✓ Branch 2 taken 909 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 909 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 909 times.
✗ Branch 9 not taken.
1818 return std::string("[^") + Pathspec::kSeparator + "]";
245 }
246
247 87 std::string PathspecElementPattern::PlaceholderSubPattern::GenerateGlobString()
248 const {
249
1/2
✓ Branch 2 taken 87 times.
✗ Branch 3 not taken.
87 return "?";
250 }
251
252 516 bool PathspecElementPattern::PlaceholderSubPattern::Compare(
253 const SubPattern *other) const {
254 516 return other->IsPlaceholder();
255 }
256