GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/pathspec/pathspec_pattern.cc
Date: 2026-06-14 02:36:34
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 9355 PathspecElementPattern::PathspecElementPattern(
13 const std::string::const_iterator begin,
14 9355 const std::string::const_iterator &end)
15 9355 : valid_(true) {
16
1/2
✓ Branch 1 taken 9355 times.
✗ Branch 2 not taken.
9355 Parse(begin, end);
17 9355 }
18
19 34278 PathspecElementPattern::PathspecElementPattern(
20 34278 const PathspecElementPattern &other)
21 34278 : valid_(other.valid_) {
22
1/2
✓ Branch 2 taken 34278 times.
✗ Branch 3 not taken.
34278 subpatterns_.reserve(other.subpatterns_.size());
23 34278 SubPatterns::const_iterator i = other.subpatterns_.begin();
24 34278 const SubPatterns::const_iterator iend = other.subpatterns_.end();
25
2/2
✓ Branch 1 taken 36871 times.
✓ Branch 2 taken 34278 times.
71149 for (; i != iend; ++i) {
26
2/4
✓ Branch 2 taken 36871 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 36871 times.
✗ Branch 6 not taken.
36871 subpatterns_.push_back((*i)->Clone());
27 }
28 34278 }
29
30 84 PathspecElementPattern &PathspecElementPattern::operator=(
31 const PathspecElementPattern &other) {
32
1/2
✓ Branch 0 taken 84 times.
✗ Branch 1 not taken.
84 if (this != &other) {
33 84 valid_ = other.valid_;
34 84 subpatterns_.clear();
35
1/2
✓ Branch 2 taken 84 times.
✗ Branch 3 not taken.
84 subpatterns_.reserve(other.subpatterns_.size());
36 84 SubPatterns::const_iterator i = other.subpatterns_.begin();
37 84 const SubPatterns::const_iterator iend = other.subpatterns_.end();
38
2/2
✓ Branch 1 taken 132 times.
✓ Branch 2 taken 84 times.
216 for (; i != iend; ++i) {
39
2/4
✓ Branch 2 taken 132 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 132 times.
✗ Branch 6 not taken.
132 subpatterns_.push_back((*i)->Clone());
40 }
41 }
42
43 84 return *this;
44 }
45
46
47 43627 PathspecElementPattern::~PathspecElementPattern() {
48 43627 SubPatterns::const_iterator i = subpatterns_.begin();
49 43627 const SubPatterns::const_iterator iend = subpatterns_.end();
50
2/2
✓ Branch 2 taken 47399 times.
✓ Branch 3 taken 43627 times.
91026 for (; i != iend; ++i) {
51
1/2
✓ Branch 1 taken 47399 times.
✗ Branch 2 not taken.
47399 delete *i;
52 }
53 43627 subpatterns_.clear();
54 43627 }
55
56
57 9355 void PathspecElementPattern::Parse(const std::string::const_iterator &begin,
58 const std::string::const_iterator &end) {
59 9355 std::string::const_iterator i = begin;
60
2/2
✓ Branch 1 taken 10526 times.
✓ Branch 2 taken 9355 times.
19881 while (i != end) {
61
3/4
✓ Branch 2 taken 2205 times.
✓ Branch 3 taken 8321 times.
✓ Branch 5 taken 2205 times.
✗ Branch 6 not taken.
10526 SubPattern *next = (Pathspec::IsSpecialChar(*i)) ? ParseSpecialChar(end, &i)
62
1/2
✓ Branch 1 taken 8321 times.
✗ Branch 2 not taken.
8321 : ParsePlaintext(end, &i);
63
3/4
✓ Branch 1 taken 10526 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 40 times.
✓ Branch 4 taken 10486 times.
10526 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 10486 times.
✗ Branch 2 not taken.
10486 subpatterns_.push_back(next);
68 }
69 }
70 9355 }
71
72
73 8321 PathspecElementPattern::SubPattern *PathspecElementPattern::ParsePlaintext(
74 const std::string::const_iterator &end, std::string::const_iterator *i) {
75 8321 PlaintextSubPattern *pattern = new PlaintextSubPattern();
76 8321 bool next_is_escaped = false;
77
78
2/2
✓ Branch 1 taken 35641 times.
✓ Branch 2 taken 7904 times.
43545 while (*i < end) {
79
6/6
✓ Branch 2 taken 582 times.
✓ Branch 3 taken 35059 times.
✓ Branch 4 taken 417 times.
✓ Branch 5 taken 165 times.
✓ Branch 6 taken 417 times.
✓ Branch 7 taken 35224 times.
35641 if (Pathspec::IsSpecialChar(**i) && !next_is_escaped) {
80 417 break;
81 }
82
83
6/6
✓ Branch 1 taken 320 times.
✓ Branch 2 taken 34904 times.
✓ Branch 3 taken 269 times.
✓ Branch 4 taken 51 times.
✓ Branch 5 taken 269 times.
✓ Branch 6 taken 34955 times.
35224 if (**i == Pathspec::kEscaper && !next_is_escaped) {
84 269 next_is_escaped = true;
85
2/2
✓ Branch 0 taken 401 times.
✓ Branch 1 taken 34554 times.
34955 } else if (next_is_escaped) {
86
6/6
✓ Branch 2 taken 236 times.
✓ Branch 3 taken 165 times.
✓ Branch 5 taken 51 times.
✓ Branch 6 taken 185 times.
✓ Branch 7 taken 216 times.
✓ Branch 8 taken 185 times.
401 if (Pathspec::IsSpecialChar(**i) || **i == Pathspec::kEscaper) {
87 216 pattern->AddChar(**i);
88 216 next_is_escaped = false;
89 } else {
90 185 valid_ = false;
91 }
92 } else {
93
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 34554 times.
34554 assert(!Pathspec::IsSpecialChar(**i));
94 34554 pattern->AddChar(**i);
95 }
96
97 35224 ++(*i);
98 }
99
100 8321 return pattern;
101 }
102
103 2205 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 2205 times.
2205 assert(Pathspec::IsSpecialChar(**i));
106 2205 const char chr = **i;
107 2205 ++(*i);
108
109
2/3
✓ Branch 0 taken 1509 times.
✓ Branch 1 taken 696 times.
✗ Branch 2 not taken.
2205 switch (chr) {
110 1509 case Pathspec::kWildcard:
111 1509 return new WildcardSubPattern();
112 696 case Pathspec::kPlaceholder:
113 696 return new PlaceholderSubPattern();
114 default:
115 assert(false && "unrecognized special character");
116 }
117 }
118
119 6139 std::string PathspecElementPattern::GenerateRegularExpression(
120 const bool is_relaxed) const {
121 6139 std::string result;
122 6139 SubPatterns::const_iterator i = subpatterns_.begin();
123 6139 const SubPatterns::const_iterator iend = subpatterns_.end();
124
2/2
✓ Branch 1 taken 7053 times.
✓ Branch 2 taken 6139 times.
13192 for (; i != iend; ++i) {
125
2/4
✓ Branch 2 taken 7053 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 7053 times.
✗ Branch 6 not taken.
7053 result += (*i)->GenerateRegularExpression(is_relaxed);
126 }
127 12278 return result;
128 }
129
130 408 std::string PathspecElementPattern::GenerateGlobString() const {
131 408 std::string result;
132 408 SubPatterns::const_iterator i = subpatterns_.begin();
133 408 const SubPatterns::const_iterator iend = subpatterns_.end();
134
2/2
✓ Branch 1 taken 480 times.
✓ Branch 2 taken 408 times.
888 for (; i != iend; ++i) {
135
2/4
✓ Branch 2 taken 480 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 480 times.
✗ Branch 6 not taken.
480 result += (*i)->GenerateGlobString();
136 }
137 816 return result;
138 }
139
140 2224 bool PathspecElementPattern::operator==(
141 const PathspecElementPattern &other) const {
142 2224 if (subpatterns_.size() != other.subpatterns_.size()
143
5/6
✓ Branch 0 taken 1992 times.
✓ Branch 1 taken 232 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1992 times.
✓ Branch 6 taken 232 times.
✓ Branch 7 taken 1992 times.
2224 || IsValid() != other.IsValid()) {
144 232 return false;
145 }
146
147 1992 SubPatterns::const_iterator i = subpatterns_.begin();
148 1992 const SubPatterns::const_iterator iend = subpatterns_.end();
149 1992 SubPatterns::const_iterator j = other.subpatterns_.begin();
150 1992 const SubPatterns::const_iterator jend = other.subpatterns_.end();
151
152
5/6
✓ Branch 3 taken 2160 times.
✓ Branch 4 taken 1350 times.
✓ Branch 6 taken 2160 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2160 times.
✓ Branch 9 taken 1350 times.
3510 for (; i != iend && j != jend; ++i, ++j) {
153
3/4
✓ Branch 3 taken 2160 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 642 times.
✓ Branch 6 taken 1518 times.
2160 if (!(*i)->Compare(*j)) {
154 642 return false;
155 }
156 }
157
158 1350 return true;
159 }
160
161
162 //------------------------------------------------------------------------------
163
164
165 34770 void PathspecElementPattern::PlaintextSubPattern::AddChar(const char chr) {
166 34770 chars_.push_back(chr);
167 34770 }
168
169 std::string
170 5780 PathspecElementPattern::PlaintextSubPattern::GenerateRegularExpression(
171 const bool is_relaxed) const {
172 // Note: strict and relaxed regex are the same!
173 5780 std::string::const_iterator i = chars_.begin();
174 5780 const std::string::const_iterator iend = chars_.end();
175 5780 std::string regex;
176
2/2
✓ Branch 2 taken 25314 times.
✓ Branch 3 taken 5780 times.
31094 for (; i != iend; ++i) {
177
2/2
✓ Branch 2 taken 944 times.
✓ Branch 3 taken 24370 times.
25314 if (IsSpecialRegexCharacter(*i)) {
178
1/2
✓ Branch 1 taken 944 times.
✗ Branch 2 not taken.
944 regex += "\\";
179 }
180
1/2
✓ Branch 2 taken 25314 times.
✗ Branch 3 not taken.
25314 regex += *i;
181 }
182 11560 return regex;
183 }
184
185
186 432 std::string PathspecElementPattern::PlaintextSubPattern::GenerateGlobString()
187 const {
188 432 std::string::const_iterator i = chars_.begin();
189 432 const std::string::const_iterator iend = chars_.end();
190 432 std::string glob_string;
191
2/2
✓ Branch 2 taken 2208 times.
✓ Branch 3 taken 432 times.
2640 for (; i != iend; ++i) {
192
2/2
✓ Branch 2 taken 48 times.
✓ Branch 3 taken 2160 times.
2208 if (Pathspec::IsSpecialChar(*i)) {
193
1/2
✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
48 glob_string += "\\";
194 }
195
1/2
✓ Branch 2 taken 2208 times.
✗ Branch 3 not taken.
2208 glob_string += *i;
196 }
197 864 return glob_string;
198 }
199
200 25314 bool PathspecElementPattern::PlaintextSubPattern::IsSpecialRegexCharacter(
201 const char chr) const {
202
8/8
✓ Branch 0 taken 24617 times.
✓ Branch 1 taken 26 times.
✓ Branch 2 taken 24578 times.
✓ Branch 3 taken 39 times.
✓ Branch 4 taken 24526 times.
✓ Branch 5 taken 52 times.
✓ Branch 6 taken 24500 times.
✓ Branch 7 taken 26 times.
24643 return (chr == '.' || chr == '\\' || chr == '*' || chr == '?' || chr == '['
203
10/10
✓ Branch 0 taken 24474 times.
✓ Branch 1 taken 26 times.
✓ Branch 2 taken 24461 times.
✓ Branch 3 taken 13 times.
✓ Branch 4 taken 24448 times.
✓ Branch 5 taken 13 times.
✓ Branch 6 taken 24435 times.
✓ Branch 7 taken 13 times.
✓ Branch 8 taken 24422 times.
✓ Branch 9 taken 13 times.
24500 || chr == ']' || chr == '(' || chr == ')' || chr == '{' || chr == '}'
204
8/8
✓ Branch 0 taken 24643 times.
✓ Branch 1 taken 671 times.
✓ Branch 2 taken 24409 times.
✓ Branch 3 taken 13 times.
✓ Branch 4 taken 24396 times.
✓ Branch 5 taken 13 times.
✓ Branch 6 taken 26 times.
✓ Branch 7 taken 24370 times.
49957 || chr == '^' || chr == '$' || chr == '+');
205 }
206
207 1708 bool PathspecElementPattern::PlaintextSubPattern::Compare(
208 const SubPattern *other) const {
209
2/2
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 1696 times.
1708 if (!other->IsPlaintext()) {
210 12 return false;
211 }
212
213 const PlaintextSubPattern
214
1/2
✓ Branch 0 taken 1696 times.
✗ Branch 1 not taken.
1696 *pt_other = dynamic_cast<const PlaintextSubPattern *>(other);
215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1696 times.
1696 assert(pt_other != NULL);
216 1696 return chars_ == pt_other->chars_;
217 }
218
219
220 std::string
221 979 PathspecElementPattern::WildcardSubPattern::GenerateRegularExpression(
222 const bool is_relaxed) const {
223 return (is_relaxed) ? std::string(".*")
224
14/27
✓ Branch 0 taken 414 times.
✓ Branch 1 taken 565 times.
✓ Branch 4 taken 414 times.
✗ Branch 5 not taken.
✓ Branch 8 taken 565 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 565 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 565 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 565 times.
✓ Branch 17 taken 414 times.
✗ Branch 18 not taken.
✓ Branch 19 taken 565 times.
✓ Branch 20 taken 414 times.
✓ Branch 22 taken 565 times.
✓ Branch 23 taken 414 times.
✓ Branch 25 taken 414 times.
✓ Branch 26 taken 565 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.
1958 : std::string("[^") + Pathspec::kSeparator + "]*";
225 }
226
227
228 24 std::string PathspecElementPattern::WildcardSubPattern::GenerateGlobString()
229 const {
230
1/2
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
24 return "*";
231 }
232
233
234 308 bool PathspecElementPattern::WildcardSubPattern::Compare(
235 const SubPattern *other) const {
236 308 return other->IsWildcard();
237 }
238
239
240 std::string
241 294 PathspecElementPattern::PlaceholderSubPattern::GenerateRegularExpression(
242 const bool is_relaxed) const {
243 // Note: strict and relaxed regex are the same!
244
3/6
✓ Branch 2 taken 294 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 294 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 294 times.
✗ Branch 9 not taken.
588 return std::string("[^") + Pathspec::kSeparator + "]";
245 }
246
247 24 std::string PathspecElementPattern::PlaceholderSubPattern::GenerateGlobString()
248 const {
249
1/2
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
24 return "?";
250 }
251
252 144 bool PathspecElementPattern::PlaceholderSubPattern::Compare(
253 const SubPattern *other) const {
254 144 return other->IsPlaceholder();
255 }
256