GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/pathspec/pathspec_pattern.cc
Date: 2026-06-21 02:37:04
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 6684 PathspecElementPattern::PathspecElementPattern(
13 const std::string::const_iterator begin,
14 6684 const std::string::const_iterator &end)
15 6684 : valid_(true) {
16
1/2
✓ Branch 1 taken 6684 times.
✗ Branch 2 not taken.
6684 Parse(begin, end);
17 6684 }
18
19 21172 PathspecElementPattern::PathspecElementPattern(
20 21172 const PathspecElementPattern &other)
21 21172 : valid_(other.valid_) {
22
1/2
✓ Branch 2 taken 21172 times.
✗ Branch 3 not taken.
21172 subpatterns_.reserve(other.subpatterns_.size());
23 21172 SubPatterns::const_iterator i = other.subpatterns_.begin();
24 21172 const SubPatterns::const_iterator iend = other.subpatterns_.end();
25
2/2
✓ Branch 1 taken 23379 times.
✓ Branch 2 taken 21172 times.
44551 for (; i != iend; ++i) {
26
2/4
✓ Branch 2 taken 23379 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 23379 times.
✗ Branch 6 not taken.
23379 subpatterns_.push_back((*i)->Clone());
27 }
28 21172 }
29
30 105 PathspecElementPattern &PathspecElementPattern::operator=(
31 const PathspecElementPattern &other) {
32
1/2
✓ Branch 0 taken 105 times.
✗ Branch 1 not taken.
105 if (this != &other) {
33 105 valid_ = other.valid_;
34 105 subpatterns_.clear();
35
1/2
✓ Branch 2 taken 105 times.
✗ Branch 3 not taken.
105 subpatterns_.reserve(other.subpatterns_.size());
36 105 SubPatterns::const_iterator i = other.subpatterns_.begin();
37 105 const SubPatterns::const_iterator iend = other.subpatterns_.end();
38
2/2
✓ Branch 1 taken 165 times.
✓ Branch 2 taken 105 times.
270 for (; i != iend; ++i) {
39
2/4
✓ Branch 2 taken 165 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 165 times.
✗ Branch 6 not taken.
165 subpatterns_.push_back((*i)->Clone());
40 }
41 }
42
43 105 return *this;
44 }
45
46
47 27850 PathspecElementPattern::~PathspecElementPattern() {
48 27850 SubPatterns::const_iterator i = subpatterns_.begin();
49 27850 const SubPatterns::const_iterator iend = subpatterns_.end();
50
2/2
✓ Branch 2 taken 31248 times.
✓ Branch 3 taken 27850 times.
59098 for (; i != iend; ++i) {
51
1/2
✓ Branch 1 taken 31248 times.
✗ Branch 2 not taken.
31248 delete *i;
52 }
53 27850 subpatterns_.clear();
54 27850 }
55
56
57 6684 void PathspecElementPattern::Parse(const std::string::const_iterator &begin,
58 const std::string::const_iterator &end) {
59 6684 std::string::const_iterator i = begin;
60
2/2
✓ Branch 1 taken 7841 times.
✓ Branch 2 taken 6684 times.
14525 while (i != end) {
61
3/4
✓ Branch 2 taken 1795 times.
✓ Branch 3 taken 6046 times.
✓ Branch 5 taken 1795 times.
✗ Branch 6 not taken.
7841 SubPattern *next = (Pathspec::IsSpecialChar(*i)) ? ParseSpecialChar(end, &i)
62
1/2
✓ Branch 1 taken 6046 times.
✗ Branch 2 not taken.
6046 : ParsePlaintext(end, &i);
63
3/4
✓ Branch 1 taken 7841 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 26 times.
✓ Branch 4 taken 7815 times.
7841 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 7815 times.
✗ Branch 2 not taken.
7815 subpatterns_.push_back(next);
68 }
69 }
70 6684 }
71
72
73 6046 PathspecElementPattern::SubPattern *PathspecElementPattern::ParsePlaintext(
74 const std::string::const_iterator &end, std::string::const_iterator *i) {
75 6046 PlaintextSubPattern *pattern = new PlaintextSubPattern();
76 6046 bool next_is_escaped = false;
77
78
2/2
✓ Branch 1 taken 25915 times.
✓ Branch 2 taken 5609 times.
31524 while (*i < end) {
79
6/6
✓ Branch 2 taken 632 times.
✓ Branch 3 taken 25283 times.
✓ Branch 4 taken 437 times.
✓ Branch 5 taken 195 times.
✓ Branch 6 taken 437 times.
✓ Branch 7 taken 25478 times.
25915 if (Pathspec::IsSpecialChar(**i) && !next_is_escaped) {
80 437 break;
81 }
82
83
6/6
✓ Branch 1 taken 356 times.
✓ Branch 2 taken 25122 times.
✓ Branch 3 taken 296 times.
✓ Branch 4 taken 60 times.
✓ Branch 5 taken 296 times.
✓ Branch 6 taken 25182 times.
25478 if (**i == Pathspec::kEscaper && !next_is_escaped) {
84 296 next_is_escaped = true;
85
2/2
✓ Branch 0 taken 408 times.
✓ Branch 1 taken 24774 times.
25182 } else if (next_is_escaped) {
86
6/6
✓ Branch 2 taken 213 times.
✓ Branch 3 taken 195 times.
✓ Branch 5 taken 60 times.
✓ Branch 6 taken 153 times.
✓ Branch 7 taken 255 times.
✓ Branch 8 taken 153 times.
408 if (Pathspec::IsSpecialChar(**i) || **i == Pathspec::kEscaper) {
87 255 pattern->AddChar(**i);
88 255 next_is_escaped = false;
89 } else {
90 153 valid_ = false;
91 }
92 } else {
93
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 24774 times.
24774 assert(!Pathspec::IsSpecialChar(**i));
94 24774 pattern->AddChar(**i);
95 }
96
97 25478 ++(*i);
98 }
99
100 6046 return pattern;
101 }
102
103 1795 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 1795 times.
1795 assert(Pathspec::IsSpecialChar(**i));
106 1795 const char chr = **i;
107 1795 ++(*i);
108
109
2/3
✓ Branch 0 taken 1119 times.
✓ Branch 1 taken 676 times.
✗ Branch 2 not taken.
1795 switch (chr) {
110 1119 case Pathspec::kWildcard:
111 1119 return new WildcardSubPattern();
112 676 case Pathspec::kPlaceholder:
113 676 return new PlaceholderSubPattern();
114 default:
115 assert(false && "unrecognized special character");
116 }
117 }
118
119 4066 std::string PathspecElementPattern::GenerateRegularExpression(
120 const bool is_relaxed) const {
121 4066 std::string result;
122 4066 SubPatterns::const_iterator i = subpatterns_.begin();
123 4066 const SubPatterns::const_iterator iend = subpatterns_.end();
124
2/2
✓ Branch 1 taken 4974 times.
✓ Branch 2 taken 4066 times.
9040 for (; i != iend; ++i) {
125
2/4
✓ Branch 2 taken 4974 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 4974 times.
✗ Branch 6 not taken.
4974 result += (*i)->GenerateRegularExpression(is_relaxed);
126 }
127 8132 return result;
128 }
129
130 506 std::string PathspecElementPattern::GenerateGlobString() const {
131 506 std::string result;
132 506 SubPatterns::const_iterator i = subpatterns_.begin();
133 506 const SubPatterns::const_iterator iend = subpatterns_.end();
134
2/2
✓ Branch 1 taken 593 times.
✓ Branch 2 taken 506 times.
1099 for (; i != iend; ++i) {
135
2/4
✓ Branch 2 taken 593 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 593 times.
✗ Branch 6 not taken.
593 result += (*i)->GenerateGlobString();
136 }
137 1012 return result;
138 }
139
140 1848 bool PathspecElementPattern::operator==(
141 const PathspecElementPattern &other) const {
142 1848 if (subpatterns_.size() != other.subpatterns_.size()
143
5/6
✓ Branch 0 taken 1654 times.
✓ Branch 1 taken 194 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1654 times.
✓ Branch 6 taken 194 times.
✓ Branch 7 taken 1654 times.
1848 || IsValid() != other.IsValid()) {
144 194 return false;
145 }
146
147 1654 SubPatterns::const_iterator i = subpatterns_.begin();
148 1654 const SubPatterns::const_iterator iend = subpatterns_.end();
149 1654 SubPatterns::const_iterator j = other.subpatterns_.begin();
150 1654 const SubPatterns::const_iterator jend = other.subpatterns_.end();
151
152
5/6
✓ Branch 3 taken 1864 times.
✓ Branch 4 taken 1212 times.
✓ Branch 6 taken 1864 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 1864 times.
✓ Branch 9 taken 1212 times.
3076 for (; i != iend && j != jend; ++i, ++j) {
153
3/4
✓ Branch 3 taken 1864 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 442 times.
✓ Branch 6 taken 1422 times.
1864 if (!(*i)->Compare(*j)) {
154 442 return false;
155 }
156 }
157
158 1212 return true;
159 }
160
161
162 //------------------------------------------------------------------------------
163
164
165 25029 void PathspecElementPattern::PlaintextSubPattern::AddChar(const char chr) {
166 25029 chars_.push_back(chr);
167 25029 }
168
169 std::string
170 3899 PathspecElementPattern::PlaintextSubPattern::GenerateRegularExpression(
171 const bool is_relaxed) const {
172 // Note: strict and relaxed regex are the same!
173 3899 std::string::const_iterator i = chars_.begin();
174 3899 const std::string::const_iterator iend = chars_.end();
175 3899 std::string regex;
176
2/2
✓ Branch 2 taken 16897 times.
✓ Branch 3 taken 3899 times.
20796 for (; i != iend; ++i) {
177
2/2
✓ Branch 2 taken 910 times.
✓ Branch 3 taken 15987 times.
16897 if (IsSpecialRegexCharacter(*i)) {
178
1/2
✓ Branch 1 taken 910 times.
✗ Branch 2 not taken.
910 regex += "\\";
179 }
180
1/2
✓ Branch 2 taken 16897 times.
✗ Branch 3 not taken.
16897 regex += *i;
181 }
182 7798 return regex;
183 }
184
185
186 535 std::string PathspecElementPattern::PlaintextSubPattern::GenerateGlobString()
187 const {
188 535 std::string::const_iterator i = chars_.begin();
189 535 const std::string::const_iterator iend = chars_.end();
190 535 std::string glob_string;
191
2/2
✓ Branch 2 taken 2740 times.
✓ Branch 3 taken 535 times.
3275 for (; i != iend; ++i) {
192
2/2
✓ Branch 2 taken 60 times.
✓ Branch 3 taken 2680 times.
2740 if (Pathspec::IsSpecialChar(*i)) {
193
1/2
✓ Branch 1 taken 60 times.
✗ Branch 2 not taken.
60 glob_string += "\\";
194 }
195
1/2
✓ Branch 2 taken 2740 times.
✗ Branch 3 not taken.
2740 glob_string += *i;
196 }
197 1070 return glob_string;
198 }
199
200 16897 bool PathspecElementPattern::PlaintextSubPattern::IsSpecialRegexCharacter(
201 const char chr) const {
202
8/8
✓ Branch 0 taken 16272 times.
✓ Branch 1 taken 30 times.
✓ Branch 2 taken 16227 times.
✓ Branch 3 taken 45 times.
✓ Branch 4 taken 16167 times.
✓ Branch 5 taken 60 times.
✓ Branch 6 taken 16137 times.
✓ Branch 7 taken 30 times.
16302 return (chr == '.' || chr == '\\' || chr == '*' || chr == '?' || chr == '['
203
10/10
✓ Branch 0 taken 16107 times.
✓ Branch 1 taken 30 times.
✓ Branch 2 taken 16092 times.
✓ Branch 3 taken 15 times.
✓ Branch 4 taken 16077 times.
✓ Branch 5 taken 15 times.
✓ Branch 6 taken 16062 times.
✓ Branch 7 taken 15 times.
✓ Branch 8 taken 16047 times.
✓ Branch 9 taken 15 times.
16137 || chr == ']' || chr == '(' || chr == ')' || chr == '{' || chr == '}'
204
8/8
✓ Branch 0 taken 16302 times.
✓ Branch 1 taken 595 times.
✓ Branch 2 taken 16032 times.
✓ Branch 3 taken 15 times.
✓ Branch 4 taken 16017 times.
✓ Branch 5 taken 15 times.
✓ Branch 6 taken 30 times.
✓ Branch 7 taken 15987 times.
33199 || chr == '^' || chr == '$' || chr == '+');
205 }
206
207 1419 bool PathspecElementPattern::PlaintextSubPattern::Compare(
208 const SubPattern *other) const {
209
2/2
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 1404 times.
1419 if (!other->IsPlaintext()) {
210 15 return false;
211 }
212
213 const PlaintextSubPattern
214
1/2
✓ Branch 0 taken 1404 times.
✗ Branch 1 not taken.
1404 *pt_other = dynamic_cast<const PlaintextSubPattern *>(other);
215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1404 times.
1404 assert(pt_other != NULL);
216 1404 return chars_ == pt_other->chars_;
217 }
218
219
220 std::string
221 752 PathspecElementPattern::WildcardSubPattern::GenerateRegularExpression(
222 const bool is_relaxed) const {
223 return (is_relaxed) ? std::string(".*")
224
14/27
✓ Branch 0 taken 276 times.
✓ Branch 1 taken 476 times.
✓ Branch 4 taken 276 times.
✗ Branch 5 not taken.
✓ Branch 8 taken 476 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 476 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 476 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 476 times.
✓ Branch 17 taken 276 times.
✗ Branch 18 not taken.
✓ Branch 19 taken 476 times.
✓ Branch 20 taken 276 times.
✓ Branch 22 taken 476 times.
✓ Branch 23 taken 276 times.
✓ Branch 25 taken 276 times.
✓ Branch 26 taken 476 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.
1504 : std::string("[^") + Pathspec::kSeparator + "]*";
225 }
226
227
228 29 std::string PathspecElementPattern::WildcardSubPattern::GenerateGlobString()
229 const {
230
1/2
✓ Branch 2 taken 29 times.
✗ Branch 3 not taken.
29 return "*";
231 }
232
233
234 265 bool PathspecElementPattern::WildcardSubPattern::Compare(
235 const SubPattern *other) const {
236 265 return other->IsWildcard();
237 }
238
239
240 std::string
241 323 PathspecElementPattern::PlaceholderSubPattern::GenerateRegularExpression(
242 const bool is_relaxed) const {
243 // Note: strict and relaxed regex are the same!
244
3/6
✓ Branch 2 taken 323 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 323 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 323 times.
✗ Branch 9 not taken.
646 return std::string("[^") + Pathspec::kSeparator + "]";
245 }
246
247 29 std::string PathspecElementPattern::PlaceholderSubPattern::GenerateGlobString()
248 const {
249
1/2
✓ Branch 2 taken 29 times.
✗ Branch 3 not taken.
29 return "?";
250 }
251
252 180 bool PathspecElementPattern::PlaceholderSubPattern::Compare(
253 const SubPattern *other) const {
254 180 return other->IsPlaceholder();
255 }
256