GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/pathspec/pathspec_pattern.cc
Date: 2026-08-16 02:40:26
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 11028 PathspecElementPattern::PathspecElementPattern(
13 const std::string::const_iterator begin,
14 11028 const std::string::const_iterator &end)
15 11028 : valid_(true) {
16
1/2
✓ Branch 1 taken 11028 times.
✗ Branch 2 not taken.
11028 Parse(begin, end);
17 11028 }
18
19 33590 PathspecElementPattern::PathspecElementPattern(
20 33590 const PathspecElementPattern &other)
21 33590 : valid_(other.valid_) {
22
1/2
✓ Branch 2 taken 33590 times.
✗ Branch 3 not taken.
33590 subpatterns_.reserve(other.subpatterns_.size());
23 33590 SubPatterns::const_iterator i = other.subpatterns_.begin();
24 33590 const SubPatterns::const_iterator iend = other.subpatterns_.end();
25
2/2
✓ Branch 1 taken 37130 times.
✓ Branch 2 taken 33590 times.
70720 for (; i != iend; ++i) {
26
2/4
✓ Branch 2 taken 37130 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 37130 times.
✗ Branch 6 not taken.
37130 subpatterns_.push_back((*i)->Clone());
27 }
28 33590 }
29
30 196 PathspecElementPattern &PathspecElementPattern::operator=(
31 const PathspecElementPattern &other) {
32
1/2
✓ Branch 0 taken 196 times.
✗ Branch 1 not taken.
196 if (this != &other) {
33 196 valid_ = other.valid_;
34 196 subpatterns_.clear();
35
1/2
✓ Branch 2 taken 196 times.
✗ Branch 3 not taken.
196 subpatterns_.reserve(other.subpatterns_.size());
36 196 SubPatterns::const_iterator i = other.subpatterns_.begin();
37 196 const SubPatterns::const_iterator iend = other.subpatterns_.end();
38
2/2
✓ Branch 1 taken 308 times.
✓ Branch 2 taken 196 times.
504 for (; i != iend; ++i) {
39
2/4
✓ Branch 2 taken 308 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 308 times.
✗ Branch 6 not taken.
308 subpatterns_.push_back((*i)->Clone());
40 }
41 }
42
43 196 return *this;
44 }
45
46
47 44612 PathspecElementPattern::~PathspecElementPattern() {
48 44612 SubPatterns::const_iterator i = subpatterns_.begin();
49 44612 const SubPatterns::const_iterator iend = subpatterns_.end();
50
2/2
✓ Branch 2 taken 50219 times.
✓ Branch 3 taken 44612 times.
94831 for (; i != iend; ++i) {
51
1/2
✓ Branch 1 taken 50219 times.
✗ Branch 2 not taken.
50219 delete *i;
52 }
53 44612 subpatterns_.clear();
54 44612 }
55
56
57 11028 void PathspecElementPattern::Parse(const std::string::const_iterator &begin,
58 const std::string::const_iterator &end) {
59 11028 std::string::const_iterator i = begin;
60
2/2
✓ Branch 1 taken 13010 times.
✓ Branch 2 taken 11028 times.
24038 while (i != end) {
61
3/4
✓ Branch 2 taken 2744 times.
✓ Branch 3 taken 10266 times.
✓ Branch 5 taken 2744 times.
✗ Branch 6 not taken.
13010 SubPattern *next = (Pathspec::IsSpecialChar(*i)) ? ParseSpecialChar(end, &i)
62
1/2
✓ Branch 1 taken 10266 times.
✗ Branch 2 not taken.
10266 : ParsePlaintext(end, &i);
63
3/4
✓ Branch 1 taken 13010 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 27 times.
✓ Branch 4 taken 12983 times.
13010 if (next->IsEmpty()) {
64 27 valid_ = false;
65
1/2
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
27 delete next;
66 } else {
67
1/2
✓ Branch 1 taken 12983 times.
✗ Branch 2 not taken.
12983 subpatterns_.push_back(next);
68 }
69 }
70 11028 }
71
72
73 10266 PathspecElementPattern::SubPattern *PathspecElementPattern::ParsePlaintext(
74 const std::string::const_iterator &end, std::string::const_iterator *i) {
75 10266 PlaintextSubPattern *pattern = new PlaintextSubPattern();
76 10266 bool next_is_escaped = false;
77
78
2/2
✓ Branch 1 taken 44974 times.
✓ Branch 2 taken 9525 times.
54499 while (*i < end) {
79
6/6
✓ Branch 2 taken 1092 times.
✓ Branch 3 taken 43882 times.
✓ Branch 4 taken 741 times.
✓ Branch 5 taken 351 times.
✓ Branch 6 taken 741 times.
✓ Branch 7 taken 44233 times.
44974 if (Pathspec::IsSpecialChar(**i) && !next_is_escaped) {
80 741 break;
81 }
82
83
6/6
✓ Branch 1 taken 621 times.
✓ Branch 2 taken 43612 times.
✓ Branch 3 taken 513 times.
✓ Branch 4 taken 108 times.
✓ Branch 5 taken 513 times.
✓ Branch 6 taken 43720 times.
44233 if (**i == Pathspec::kEscaper && !next_is_escaped) {
84 513 next_is_escaped = true;
85
2/2
✓ Branch 0 taken 675 times.
✓ Branch 1 taken 43045 times.
43720 } else if (next_is_escaped) {
86
6/6
✓ Branch 2 taken 324 times.
✓ Branch 3 taken 351 times.
✓ Branch 5 taken 108 times.
✓ Branch 6 taken 216 times.
✓ Branch 7 taken 459 times.
✓ Branch 8 taken 216 times.
675 if (Pathspec::IsSpecialChar(**i) || **i == Pathspec::kEscaper) {
87 459 pattern->AddChar(**i);
88 459 next_is_escaped = false;
89 } else {
90 216 valid_ = false;
91 }
92 } else {
93
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 43045 times.
43045 assert(!Pathspec::IsSpecialChar(**i));
94 43045 pattern->AddChar(**i);
95 }
96
97 44233 ++(*i);
98 }
99
100 10266 return pattern;
101 }
102
103 2744 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 2744 times.
2744 assert(Pathspec::IsSpecialChar(**i));
106 2744 const char chr = **i;
107 2744 ++(*i);
108
109
2/3
✓ Branch 0 taken 1643 times.
✓ Branch 1 taken 1101 times.
✗ Branch 2 not taken.
2744 switch (chr) {
110 1643 case Pathspec::kWildcard:
111 1643 return new WildcardSubPattern();
112 1101 case Pathspec::kPlaceholder:
113 1101 return new PlaceholderSubPattern();
114 default:
115 assert(false && "unrecognized special character");
116 }
117 }
118
119 7271 std::string PathspecElementPattern::GenerateRegularExpression(
120 const bool is_relaxed) const {
121 7271 std::string result;
122 7271 SubPatterns::const_iterator i = subpatterns_.begin();
123 7271 const SubPatterns::const_iterator iend = subpatterns_.end();
124
2/2
✓ Branch 1 taken 8872 times.
✓ Branch 2 taken 7271 times.
16143 for (; i != iend; ++i) {
125
2/4
✓ Branch 2 taken 8872 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 8872 times.
✗ Branch 6 not taken.
8872 result += (*i)->GenerateRegularExpression(is_relaxed);
126 }
127 14542 return result;
128 }
129
130 926 std::string PathspecElementPattern::GenerateGlobString() const {
131 926 std::string result;
132 926 SubPatterns::const_iterator i = subpatterns_.begin();
133 926 const SubPatterns::const_iterator iend = subpatterns_.end();
134
2/2
✓ Branch 1 taken 1091 times.
✓ Branch 2 taken 926 times.
2017 for (; i != iend; ++i) {
135
2/4
✓ Branch 2 taken 1091 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1091 times.
✗ Branch 6 not taken.
1091 result += (*i)->GenerateGlobString();
136 }
137 1852 return result;
138 }
139
140 2874 bool PathspecElementPattern::operator==(
141 const PathspecElementPattern &other) const {
142 2874 if (subpatterns_.size() != other.subpatterns_.size()
143
5/6
✓ Branch 0 taken 2604 times.
✓ Branch 1 taken 270 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 2604 times.
✓ Branch 6 taken 270 times.
✓ Branch 7 taken 2604 times.
2874 || IsValid() != other.IsValid()) {
144 270 return false;
145 }
146
147 2604 SubPatterns::const_iterator i = subpatterns_.begin();
148 2604 const SubPatterns::const_iterator iend = subpatterns_.end();
149 2604 SubPatterns::const_iterator j = other.subpatterns_.begin();
150 2604 const SubPatterns::const_iterator jend = other.subpatterns_.end();
151
152
5/6
✓ Branch 3 taken 2982 times.
✓ Branch 4 taken 1908 times.
✓ Branch 6 taken 2982 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2982 times.
✓ Branch 9 taken 1908 times.
4890 for (; i != iend && j != jend; ++i, ++j) {
153
3/4
✓ Branch 3 taken 2982 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 696 times.
✓ Branch 6 taken 2286 times.
2982 if (!(*i)->Compare(*j)) {
154 696 return false;
155 }
156 }
157
158 1908 return true;
159 }
160
161
162 //------------------------------------------------------------------------------
163
164
165 43504 void PathspecElementPattern::PlaintextSubPattern::AddChar(const char chr) {
166 43504 chars_.push_back(chr);
167 43504 }
168
169 std::string
170 7091 PathspecElementPattern::PlaintextSubPattern::GenerateRegularExpression(
171 const bool is_relaxed) const {
172 // Note: strict and relaxed regex are the same!
173 7091 std::string::const_iterator i = chars_.begin();
174 7091 const std::string::const_iterator iend = chars_.end();
175 7091 std::string regex;
176
2/2
✓ Branch 2 taken 31358 times.
✓ Branch 3 taken 7091 times.
38449 for (; i != iend; ++i) {
177
2/2
✓ Branch 2 taken 1555 times.
✓ Branch 3 taken 29803 times.
31358 if (IsSpecialRegexCharacter(*i)) {
178
1/2
✓ Branch 1 taken 1555 times.
✗ Branch 2 not taken.
1555 regex += "\\";
179 }
180
1/2
✓ Branch 2 taken 31358 times.
✗ Branch 3 not taken.
31358 regex += *i;
181 }
182 14182 return regex;
183 }
184
185
186 981 std::string PathspecElementPattern::PlaintextSubPattern::GenerateGlobString()
187 const {
188 981 std::string::const_iterator i = chars_.begin();
189 981 const std::string::const_iterator iend = chars_.end();
190 981 std::string glob_string;
191
2/2
✓ Branch 2 taken 5008 times.
✓ Branch 3 taken 981 times.
5989 for (; i != iend; ++i) {
192
2/2
✓ Branch 2 taken 108 times.
✓ Branch 3 taken 4900 times.
5008 if (Pathspec::IsSpecialChar(*i)) {
193
1/2
✓ Branch 1 taken 108 times.
✗ Branch 2 not taken.
108 glob_string += "\\";
194 }
195
1/2
✓ Branch 2 taken 5008 times.
✗ Branch 3 not taken.
5008 glob_string += *i;
196 }
197 1962 return glob_string;
198 }
199
200 31358 bool PathspecElementPattern::PlaintextSubPattern::IsSpecialRegexCharacter(
201 const char chr) const {
202
8/8
✓ Branch 0 taken 30316 times.
✓ Branch 1 taken 54 times.
✓ Branch 2 taken 30235 times.
✓ Branch 3 taken 81 times.
✓ Branch 4 taken 30127 times.
✓ Branch 5 taken 108 times.
✓ Branch 6 taken 30073 times.
✓ Branch 7 taken 54 times.
30370 return (chr == '.' || chr == '\\' || chr == '*' || chr == '?' || chr == '['
203
10/10
✓ Branch 0 taken 30019 times.
✓ Branch 1 taken 54 times.
✓ Branch 2 taken 29992 times.
✓ Branch 3 taken 27 times.
✓ Branch 4 taken 29965 times.
✓ Branch 5 taken 27 times.
✓ Branch 6 taken 29938 times.
✓ Branch 7 taken 27 times.
✓ Branch 8 taken 29911 times.
✓ Branch 9 taken 27 times.
30073 || chr == ']' || chr == '(' || chr == ')' || chr == '{' || chr == '}'
204
8/8
✓ Branch 0 taken 30370 times.
✓ Branch 1 taken 988 times.
✓ Branch 2 taken 29884 times.
✓ Branch 3 taken 27 times.
✓ Branch 4 taken 29857 times.
✓ Branch 5 taken 27 times.
✓ Branch 6 taken 54 times.
✓ Branch 7 taken 29803 times.
61728 || chr == '^' || chr == '$' || chr == '+');
205 }
206
207 2280 bool PathspecElementPattern::PlaintextSubPattern::Compare(
208 const SubPattern *other) const {
209
2/2
✓ Branch 1 taken 27 times.
✓ Branch 2 taken 2253 times.
2280 if (!other->IsPlaintext()) {
210 27 return false;
211 }
212
213 const PlaintextSubPattern
214
1/2
✓ Branch 0 taken 2253 times.
✗ Branch 1 not taken.
2253 *pt_other = dynamic_cast<const PlaintextSubPattern *>(other);
215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2253 times.
2253 assert(pt_other != NULL);
216 2253 return chars_ == pt_other->chars_;
217 }
218
219
220 std::string
221 1197 PathspecElementPattern::WildcardSubPattern::GenerateRegularExpression(
222 const bool is_relaxed) const {
223 return (is_relaxed) ? std::string(".*")
224
14/27
✓ Branch 0 taken 428 times.
✓ Branch 1 taken 769 times.
✓ Branch 4 taken 428 times.
✗ Branch 5 not taken.
✓ Branch 8 taken 769 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 769 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 769 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 769 times.
✓ Branch 17 taken 428 times.
✗ Branch 18 not taken.
✓ Branch 19 taken 769 times.
✓ Branch 20 taken 428 times.
✓ Branch 22 taken 769 times.
✓ Branch 23 taken 428 times.
✓ Branch 25 taken 428 times.
✓ Branch 26 taken 769 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.
2394 : std::string("[^") + Pathspec::kSeparator + "]*";
225 }
226
227
228 55 std::string PathspecElementPattern::WildcardSubPattern::GenerateGlobString()
229 const {
230
1/2
✓ Branch 2 taken 55 times.
✗ Branch 3 not taken.
55 return "*";
231 }
232
233
234 378 bool PathspecElementPattern::WildcardSubPattern::Compare(
235 const SubPattern *other) const {
236 378 return other->IsWildcard();
237 }
238
239
240 std::string
241 584 PathspecElementPattern::PlaceholderSubPattern::GenerateRegularExpression(
242 const bool is_relaxed) const {
243 // Note: strict and relaxed regex are the same!
244
3/6
✓ Branch 2 taken 584 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 584 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 584 times.
✗ Branch 9 not taken.
1168 return std::string("[^") + Pathspec::kSeparator + "]";
245 }
246
247 55 std::string PathspecElementPattern::PlaceholderSubPattern::GenerateGlobString()
248 const {
249
1/2
✓ Branch 2 taken 55 times.
✗ Branch 3 not taken.
55 return "?";
250 }
251
252 324 bool PathspecElementPattern::PlaceholderSubPattern::Compare(
253 const SubPattern *other) const {
254 324 return other->IsPlaceholder();
255 }
256