GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/pathspec/pathspec_pattern.cc
Date: 2026-06-28 02:36:10
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 16598 PathspecElementPattern::PathspecElementPattern(
13 const std::string::const_iterator begin,
14 16598 const std::string::const_iterator &end)
15 16598 : valid_(true) {
16
1/2
✓ Branch 1 taken 16598 times.
✗ Branch 2 not taken.
16598 Parse(begin, end);
17 16598 }
18
19 55313 PathspecElementPattern::PathspecElementPattern(
20 55313 const PathspecElementPattern &other)
21 55313 : valid_(other.valid_) {
22
1/2
✓ Branch 2 taken 55313 times.
✗ Branch 3 not taken.
55313 subpatterns_.reserve(other.subpatterns_.size());
23 55313 SubPatterns::const_iterator i = other.subpatterns_.begin();
24 55313 const SubPatterns::const_iterator iend = other.subpatterns_.end();
25
2/2
✓ Branch 1 taken 59716 times.
✓ Branch 2 taken 55313 times.
115029 for (; i != iend; ++i) {
26
2/4
✓ Branch 2 taken 59716 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 59716 times.
✗ Branch 6 not taken.
59716 subpatterns_.push_back((*i)->Clone());
27 }
28 55313 }
29
30 224 PathspecElementPattern &PathspecElementPattern::operator=(
31 const PathspecElementPattern &other) {
32
1/2
✓ Branch 0 taken 224 times.
✗ Branch 1 not taken.
224 if (this != &other) {
33 224 valid_ = other.valid_;
34 224 subpatterns_.clear();
35
1/2
✓ Branch 2 taken 224 times.
✗ Branch 3 not taken.
224 subpatterns_.reserve(other.subpatterns_.size());
36 224 SubPatterns::const_iterator i = other.subpatterns_.begin();
37 224 const SubPatterns::const_iterator iend = other.subpatterns_.end();
38
2/2
✓ Branch 1 taken 352 times.
✓ Branch 2 taken 224 times.
576 for (; i != iend; ++i) {
39
2/4
✓ Branch 2 taken 352 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 352 times.
✗ Branch 6 not taken.
352 subpatterns_.push_back((*i)->Clone());
40 }
41 }
42
43 224 return *this;
44 }
45
46
47 71905 PathspecElementPattern::~PathspecElementPattern() {
48 71905 SubPatterns::const_iterator i = subpatterns_.begin();
49 71905 const SubPatterns::const_iterator iend = subpatterns_.end();
50
2/2
✓ Branch 2 taken 78802 times.
✓ Branch 3 taken 71905 times.
150707 for (; i != iend; ++i) {
51
1/2
✓ Branch 1 taken 78802 times.
✗ Branch 2 not taken.
78802 delete *i;
52 }
53 71905 subpatterns_.clear();
54 71905 }
55
56
57 16598 void PathspecElementPattern::Parse(const std::string::const_iterator &begin,
58 const std::string::const_iterator &end) {
59 16598 std::string::const_iterator i = begin;
60
2/2
✓ Branch 1 taken 19005 times.
✓ Branch 2 taken 16598 times.
35603 while (i != end) {
61
3/4
✓ Branch 2 taken 3558 times.
✓ Branch 3 taken 15447 times.
✓ Branch 5 taken 3558 times.
✗ Branch 6 not taken.
19005 SubPattern *next = (Pathspec::IsSpecialChar(*i)) ? ParseSpecialChar(end, &i)
62
1/2
✓ Branch 1 taken 15447 times.
✗ Branch 2 not taken.
15447 : ParsePlaintext(end, &i);
63
3/4
✓ Branch 1 taken 19005 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 41 times.
✓ Branch 4 taken 18964 times.
19005 if (next->IsEmpty()) {
64 41 valid_ = false;
65
1/2
✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
41 delete next;
66 } else {
67
1/2
✓ Branch 1 taken 18964 times.
✗ Branch 2 not taken.
18964 subpatterns_.push_back(next);
68 }
69 }
70 16598 }
71
72
73 15447 PathspecElementPattern::SubPattern *PathspecElementPattern::ParsePlaintext(
74 const std::string::const_iterator &end, std::string::const_iterator *i) {
75 15447 PlaintextSubPattern *pattern = new PlaintextSubPattern();
76 15447 bool next_is_escaped = false;
77
78
2/2
✓ Branch 1 taken 70971 times.
✓ Branch 2 taken 14539 times.
85510 while (*i < end) {
79
6/6
✓ Branch 2 taken 1331 times.
✓ Branch 3 taken 69640 times.
✓ Branch 4 taken 908 times.
✓ Branch 5 taken 423 times.
✓ Branch 6 taken 908 times.
✓ Branch 7 taken 70063 times.
70971 if (Pathspec::IsSpecialChar(**i) && !next_is_escaped) {
80 908 break;
81 }
82
83
6/6
✓ Branch 1 taken 756 times.
✓ Branch 2 taken 69307 times.
✓ Branch 3 taken 626 times.
✓ Branch 4 taken 130 times.
✓ Branch 5 taken 626 times.
✓ Branch 6 taken 69437 times.
70063 if (**i == Pathspec::kEscaper && !next_is_escaped) {
84 626 next_is_escaped = true;
85
2/2
✓ Branch 0 taken 836 times.
✓ Branch 1 taken 68601 times.
69437 } else if (next_is_escaped) {
86
6/6
✓ Branch 2 taken 413 times.
✓ Branch 3 taken 423 times.
✓ Branch 5 taken 130 times.
✓ Branch 6 taken 283 times.
✓ Branch 7 taken 553 times.
✓ Branch 8 taken 283 times.
836 if (Pathspec::IsSpecialChar(**i) || **i == Pathspec::kEscaper) {
87 553 pattern->AddChar(**i);
88 553 next_is_escaped = false;
89 } else {
90 283 valid_ = false;
91 }
92 } else {
93
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 68601 times.
68601 assert(!Pathspec::IsSpecialChar(**i));
94 68601 pattern->AddChar(**i);
95 }
96
97 70063 ++(*i);
98 }
99
100 15447 return pattern;
101 }
102
103 3558 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 3558 times.
3558 assert(Pathspec::IsSpecialChar(**i));
106 3558 const char chr = **i;
107 3558 ++(*i);
108
109
2/3
✓ Branch 0 taken 2190 times.
✓ Branch 1 taken 1368 times.
✗ Branch 2 not taken.
3558 switch (chr) {
110 2190 case Pathspec::kWildcard:
111 2190 return new WildcardSubPattern();
112 1368 case Pathspec::kPlaceholder:
113 1368 return new PlaceholderSubPattern();
114 default:
115 assert(false && "unrecognized special character");
116 }
117 }
118
119 11074 std::string PathspecElementPattern::GenerateRegularExpression(
120 const bool is_relaxed) const {
121 11074 std::string result;
122 11074 SubPatterns::const_iterator i = subpatterns_.begin();
123 11074 const SubPatterns::const_iterator iend = subpatterns_.end();
124
2/2
✓ Branch 1 taken 12987 times.
✓ Branch 2 taken 11074 times.
24061 for (; i != iend; ++i) {
125
2/4
✓ Branch 2 taken 12987 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 12987 times.
✗ Branch 6 not taken.
12987 result += (*i)->GenerateRegularExpression(is_relaxed);
126 }
127 22148 return result;
128 }
129
130 1088 std::string PathspecElementPattern::GenerateGlobString() const {
131 1088 std::string result;
132 1088 SubPatterns::const_iterator i = subpatterns_.begin();
133 1088 const SubPatterns::const_iterator iend = subpatterns_.end();
134
2/2
✓ Branch 1 taken 1280 times.
✓ Branch 2 taken 1088 times.
2368 for (; i != iend; ++i) {
135
2/4
✓ Branch 2 taken 1280 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1280 times.
✗ Branch 6 not taken.
1280 result += (*i)->GenerateGlobString();
136 }
137 2176 return result;
138 }
139
140 3654 bool PathspecElementPattern::operator==(
141 const PathspecElementPattern &other) const {
142 3654 if (subpatterns_.size() != other.subpatterns_.size()
143
5/6
✓ Branch 0 taken 3292 times.
✓ Branch 1 taken 362 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 3292 times.
✓ Branch 6 taken 362 times.
✓ Branch 7 taken 3292 times.
3654 || IsValid() != other.IsValid()) {
144 362 return false;
145 }
146
147 3292 SubPatterns::const_iterator i = subpatterns_.begin();
148 3292 const SubPatterns::const_iterator iend = subpatterns_.end();
149 3292 SubPatterns::const_iterator j = other.subpatterns_.begin();
150 3292 const SubPatterns::const_iterator jend = other.subpatterns_.end();
151
152
5/6
✓ Branch 3 taken 3754 times.
✓ Branch 4 taken 2427 times.
✓ Branch 6 taken 3754 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 3754 times.
✓ Branch 9 taken 2427 times.
6181 for (; i != iend && j != jend; ++i, ++j) {
153
3/4
✓ Branch 3 taken 3754 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 865 times.
✓ Branch 6 taken 2889 times.
3754 if (!(*i)->Compare(*j)) {
154 865 return false;
155 }
156 }
157
158 2427 return true;
159 }
160
161
162 //------------------------------------------------------------------------------
163
164
165 69154 void PathspecElementPattern::PlaintextSubPattern::AddChar(const char chr) {
166 69154 chars_.push_back(chr);
167 69154 }
168
169 std::string
170 10733 PathspecElementPattern::PlaintextSubPattern::GenerateRegularExpression(
171 const bool is_relaxed) const {
172 // Note: strict and relaxed regex are the same!
173 10733 std::string::const_iterator i = chars_.begin();
174 10733 const std::string::const_iterator iend = chars_.end();
175 10733 std::string regex;
176
2/2
✓ Branch 2 taken 51072 times.
✓ Branch 3 taken 10733 times.
61805 for (; i != iend; ++i) {
177
2/2
✓ Branch 2 taken 1905 times.
✓ Branch 3 taken 49167 times.
51072 if (IsSpecialRegexCharacter(*i)) {
178
1/2
✓ Branch 1 taken 1905 times.
✗ Branch 2 not taken.
1905 regex += "\\";
179 }
180
1/2
✓ Branch 2 taken 51072 times.
✗ Branch 3 not taken.
51072 regex += *i;
181 }
182 21466 return regex;
183 }
184
185
186 1152 std::string PathspecElementPattern::PlaintextSubPattern::GenerateGlobString()
187 const {
188 1152 std::string::const_iterator i = chars_.begin();
189 1152 const std::string::const_iterator iend = chars_.end();
190 1152 std::string glob_string;
191
2/2
✓ Branch 2 taken 5888 times.
✓ Branch 3 taken 1152 times.
7040 for (; i != iend; ++i) {
192
2/2
✓ Branch 2 taken 128 times.
✓ Branch 3 taken 5760 times.
5888 if (Pathspec::IsSpecialChar(*i)) {
193
1/2
✓ Branch 1 taken 128 times.
✗ Branch 2 not taken.
128 glob_string += "\\";
194 }
195
1/2
✓ Branch 2 taken 5888 times.
✗ Branch 3 not taken.
5888 glob_string += *i;
196 }
197 2304 return glob_string;
198 }
199
200 51072 bool PathspecElementPattern::PlaintextSubPattern::IsSpecialRegexCharacter(
201 const char chr) const {
202
8/8
✓ Branch 0 taken 49794 times.
✓ Branch 1 taken 66 times.
✓ Branch 2 taken 49695 times.
✓ Branch 3 taken 99 times.
✓ Branch 4 taken 49563 times.
✓ Branch 5 taken 132 times.
✓ Branch 6 taken 49497 times.
✓ Branch 7 taken 66 times.
49860 return (chr == '.' || chr == '\\' || chr == '*' || chr == '?' || chr == '['
203
10/10
✓ Branch 0 taken 49431 times.
✓ Branch 1 taken 66 times.
✓ Branch 2 taken 49398 times.
✓ Branch 3 taken 33 times.
✓ Branch 4 taken 49365 times.
✓ Branch 5 taken 33 times.
✓ Branch 6 taken 49332 times.
✓ Branch 7 taken 33 times.
✓ Branch 8 taken 49299 times.
✓ Branch 9 taken 33 times.
49497 || chr == ']' || chr == '(' || chr == ')' || chr == '{' || chr == '}'
204
8/8
✓ Branch 0 taken 49860 times.
✓ Branch 1 taken 1212 times.
✓ Branch 2 taken 49266 times.
✓ Branch 3 taken 33 times.
✓ Branch 4 taken 49233 times.
✓ Branch 5 taken 33 times.
✓ Branch 6 taken 66 times.
✓ Branch 7 taken 49167 times.
100932 || chr == '^' || chr == '$' || chr == '+');
205 }
206
207 2856 bool PathspecElementPattern::PlaintextSubPattern::Compare(
208 const SubPattern *other) const {
209
2/2
✓ Branch 1 taken 33 times.
✓ Branch 2 taken 2823 times.
2856 if (!other->IsPlaintext()) {
210 33 return false;
211 }
212
213 const PlaintextSubPattern
214
1/2
✓ Branch 0 taken 2823 times.
✗ Branch 1 not taken.
2823 *pt_other = dynamic_cast<const PlaintextSubPattern *>(other);
215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2823 times.
2823 assert(pt_other != NULL);
216 2823 return chars_ == pt_other->chars_;
217 }
218
219
220 std::string
221 1565 PathspecElementPattern::WildcardSubPattern::GenerateRegularExpression(
222 const bool is_relaxed) const {
223 return (is_relaxed) ? std::string(".*")
224
14/27
✓ Branch 0 taken 530 times.
✓ Branch 1 taken 1035 times.
✓ Branch 4 taken 530 times.
✗ Branch 5 not taken.
✓ Branch 8 taken 1035 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 1035 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 1035 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 1035 times.
✓ Branch 17 taken 530 times.
✗ Branch 18 not taken.
✓ Branch 19 taken 1035 times.
✓ Branch 20 taken 530 times.
✓ Branch 22 taken 1035 times.
✓ Branch 23 taken 530 times.
✓ Branch 25 taken 530 times.
✓ Branch 26 taken 1035 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.
3130 : std::string("[^") + Pathspec::kSeparator + "]*";
225 }
226
227
228 64 std::string PathspecElementPattern::WildcardSubPattern::GenerateGlobString()
229 const {
230
1/2
✓ Branch 2 taken 64 times.
✗ Branch 3 not taken.
64 return "*";
231 }
232
233
234 502 bool PathspecElementPattern::WildcardSubPattern::Compare(
235 const SubPattern *other) const {
236 502 return other->IsWildcard();
237 }
238
239
240 std::string
241 689 PathspecElementPattern::PlaceholderSubPattern::GenerateRegularExpression(
242 const bool is_relaxed) const {
243 // Note: strict and relaxed regex are the same!
244
3/6
✓ Branch 2 taken 689 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 689 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 689 times.
✗ Branch 9 not taken.
1378 return std::string("[^") + Pathspec::kSeparator + "]";
245 }
246
247 64 std::string PathspecElementPattern::PlaceholderSubPattern::GenerateGlobString()
248 const {
249
1/2
✓ Branch 2 taken 64 times.
✗ Branch 3 not taken.
64 return "?";
250 }
251
252 396 bool PathspecElementPattern::PlaceholderSubPattern::Compare(
253 const SubPattern *other) const {
254 396 return other->IsPlaceholder();
255 }
256