GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/pathspec/pathspec_pattern.cc
Date: 2026-10-25 02:40:14
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 19296 PathspecElementPattern::PathspecElementPattern(
13 const std::string::const_iterator begin,
14 19296 const std::string::const_iterator &end)
15 19296 : valid_(true) {
16
1/2
✓ Branch 1 taken 19296 times.
✗ Branch 2 not taken.
19296 Parse(begin, end);
17 19296 }
18
19 57900 PathspecElementPattern::PathspecElementPattern(
20 57900 const PathspecElementPattern &other)
21 57900 : valid_(other.valid_) {
22
1/2
✓ Branch 2 taken 57900 times.
✗ Branch 3 not taken.
57900 subpatterns_.reserve(other.subpatterns_.size());
23 57900 SubPatterns::const_iterator i = other.subpatterns_.begin();
24 57900 const SubPatterns::const_iterator iend = other.subpatterns_.end();
25
2/2
✓ Branch 1 taken 62901 times.
✓ Branch 2 taken 57900 times.
120801 for (; i != iend; ++i) {
26
2/4
✓ Branch 2 taken 62901 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 62901 times.
✗ Branch 6 not taken.
62901 subpatterns_.push_back((*i)->Clone());
27 }
28 57900 }
29
30 336 PathspecElementPattern &PathspecElementPattern::operator=(
31 const PathspecElementPattern &other) {
32
1/2
✓ Branch 0 taken 336 times.
✗ Branch 1 not taken.
336 if (this != &other) {
33 336 valid_ = other.valid_;
34 336 subpatterns_.clear();
35
1/2
✓ Branch 2 taken 336 times.
✗ Branch 3 not taken.
336 subpatterns_.reserve(other.subpatterns_.size());
36 336 SubPatterns::const_iterator i = other.subpatterns_.begin();
37 336 const SubPatterns::const_iterator iend = other.subpatterns_.end();
38
2/2
✓ Branch 1 taken 528 times.
✓ Branch 2 taken 336 times.
864 for (; i != iend; ++i) {
39
2/4
✓ Branch 2 taken 528 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 528 times.
✗ Branch 6 not taken.
528 subpatterns_.push_back((*i)->Clone());
40 }
41 }
42
43 336 return *this;
44 }
45
46
47 77190 PathspecElementPattern::~PathspecElementPattern() {
48 77190 SubPatterns::const_iterator i = subpatterns_.begin();
49 77190 const SubPatterns::const_iterator iend = subpatterns_.end();
50
2/2
✓ Branch 2 taken 85529 times.
✓ Branch 3 taken 77190 times.
162719 for (; i != iend; ++i) {
51
1/2
✓ Branch 1 taken 85529 times.
✗ Branch 2 not taken.
85529 delete *i;
52 }
53 77190 subpatterns_.clear();
54 77190 }
55
56
57 19296 void PathspecElementPattern::Parse(const std::string::const_iterator &begin,
58 const std::string::const_iterator &end) {
59 19296 std::string::const_iterator i = begin;
60
2/2
✓ Branch 1 taken 22451 times.
✓ Branch 2 taken 19296 times.
41747 while (i != end) {
61
3/4
✓ Branch 2 taken 3686 times.
✓ Branch 3 taken 18765 times.
✓ Branch 5 taken 3686 times.
✗ Branch 6 not taken.
22451 SubPattern *next = (Pathspec::IsSpecialChar(*i)) ? ParseSpecialChar(end, &i)
62
1/2
✓ Branch 1 taken 18765 times.
✗ Branch 2 not taken.
18765 : ParsePlaintext(end, &i);
63
3/4
✓ Branch 1 taken 22451 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 9 times.
✓ Branch 4 taken 22442 times.
22451 if (next->IsEmpty()) {
64 9 valid_ = false;
65
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 delete next;
66 } else {
67
1/2
✓ Branch 1 taken 22442 times.
✗ Branch 2 not taken.
22442 subpatterns_.push_back(next);
68 }
69 }
70 19296 }
71
72
73 18765 PathspecElementPattern::SubPattern *PathspecElementPattern::ParsePlaintext(
74 const std::string::const_iterator &end, std::string::const_iterator *i) {
75 18765 PlaintextSubPattern *pattern = new PlaintextSubPattern();
76 18765 bool next_is_escaped = false;
77
78
2/2
✓ Branch 1 taken 85047 times.
✓ Branch 2 taken 17563 times.
102610 while (*i < end) {
79
6/6
✓ Branch 2 taken 1836 times.
✓ Branch 3 taken 83211 times.
✓ Branch 4 taken 1202 times.
✓ Branch 5 taken 634 times.
✓ Branch 6 taken 1202 times.
✓ Branch 7 taken 83845 times.
85047 if (Pathspec::IsSpecialChar(**i) && !next_is_escaped) {
80 1202 break;
81 }
82
83
6/6
✓ Branch 1 taken 1082 times.
✓ Branch 2 taken 82763 times.
✓ Branch 3 taken 887 times.
✓ Branch 4 taken 195 times.
✓ Branch 5 taken 887 times.
✓ Branch 6 taken 82958 times.
83845 if (**i == Pathspec::kEscaper && !next_is_escaped) {
84 887 next_is_escaped = true;
85
2/2
✓ Branch 0 taken 1101 times.
✓ Branch 1 taken 81857 times.
82958 } else if (next_is_escaped) {
86
6/6
✓ Branch 2 taken 467 times.
✓ Branch 3 taken 634 times.
✓ Branch 5 taken 195 times.
✓ Branch 6 taken 272 times.
✓ Branch 7 taken 829 times.
✓ Branch 8 taken 272 times.
1101 if (Pathspec::IsSpecialChar(**i) || **i == Pathspec::kEscaper) {
87 829 pattern->AddChar(**i);
88 829 next_is_escaped = false;
89 } else {
90 272 valid_ = false;
91 }
92 } else {
93
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 81857 times.
81857 assert(!Pathspec::IsSpecialChar(**i));
94 81857 pattern->AddChar(**i);
95 }
96
97 83845 ++(*i);
98 }
99
100 18765 return pattern;
101 }
102
103 3686 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 3686 times.
3686 assert(Pathspec::IsSpecialChar(**i));
106 3686 const char chr = **i;
107 3686 ++(*i);
108
109
2/3
✓ Branch 0 taken 2007 times.
✓ Branch 1 taken 1679 times.
✗ Branch 2 not taken.
3686 switch (chr) {
110 2007 case Pathspec::kWildcard:
111 2007 return new WildcardSubPattern();
112 1679 case Pathspec::kPlaceholder:
113 1679 return new PlaceholderSubPattern();
114 ✗ default:
115 ✗ assert(false && "unrecognized special character");
116 }
117 }
118
119 13649 std::string PathspecElementPattern::GenerateRegularExpression(
120 const bool is_relaxed) const {
121 13649 std::string result;
122 13649 SubPatterns::const_iterator i = subpatterns_.begin();
123 13649 const SubPatterns::const_iterator iend = subpatterns_.end();
124
2/2
✓ Branch 1 taken 16232 times.
✓ Branch 2 taken 13649 times.
29881 for (; i != iend; ++i) {
125
2/4
✓ Branch 2 taken 16232 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 16232 times.
✗ Branch 6 not taken.
16232 result += (*i)->GenerateRegularExpression(is_relaxed);
126 }
127 27298 return result;
128 }
129
130 1658 std::string PathspecElementPattern::GenerateGlobString() const {
131 1658 std::string result;
132 1658 SubPatterns::const_iterator i = subpatterns_.begin();
133 1658 const SubPatterns::const_iterator iend = subpatterns_.end();
134
2/2
✓ Branch 1 taken 1949 times.
✓ Branch 2 taken 1658 times.
3607 for (; i != iend; ++i) {
135
2/4
✓ Branch 2 taken 1949 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1949 times.
✗ Branch 6 not taken.
1949 result += (*i)->GenerateGlobString();
136 }
137 3316 return result;
138 }
139
140 3958 bool PathspecElementPattern::operator==(
141 const PathspecElementPattern &other) const {
142 3958 if (subpatterns_.size() != other.subpatterns_.size()
143
5/6
✓ Branch 0 taken 3628 times.
✓ Branch 1 taken 330 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 3628 times.
✓ Branch 6 taken 330 times.
✓ Branch 7 taken 3628 times.
3958 || IsValid() != other.IsValid()) {
144 330 return false;
145 }
146
147 3628 SubPatterns::const_iterator i = subpatterns_.begin();
148 3628 const SubPatterns::const_iterator iend = subpatterns_.end();
149 3628 SubPatterns::const_iterator j = other.subpatterns_.begin();
150 3628 const SubPatterns::const_iterator jend = other.subpatterns_.end();
151
152
5/6
✓ Branch 3 taken 4314 times.
✓ Branch 4 taken 2781 times.
✓ Branch 6 taken 4314 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 4314 times.
✓ Branch 9 taken 2781 times.
7095 for (; i != iend && j != jend; ++i, ++j) {
153
3/4
✓ Branch 3 taken 4314 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 847 times.
✓ Branch 6 taken 3467 times.
4314 if (!(*i)->Compare(*j)) {
154 847 return false;
155 }
156 }
157
158 2781 return true;
159 }
160
161
162 //------------------------------------------------------------------------------
163
164
165 82686 void PathspecElementPattern::PlaintextSubPattern::AddChar(const char chr) {
166 82686 chars_.push_back(chr);
167 82686 }
168
169 std::string
170 13585 PathspecElementPattern::PlaintextSubPattern::GenerateRegularExpression(
171 const bool is_relaxed) const {
172 // Note: strict and relaxed regex are the same!
173 13585 std::string::const_iterator i = chars_.begin();
174 13585 const std::string::const_iterator iend = chars_.end();
175 13585 std::string regex;
176
2/2
✓ Branch 2 taken 62824 times.
✓ Branch 3 taken 13585 times.
76409 for (; i != iend; ++i) {
177
2/2
✓ Branch 2 taken 2494 times.
✓ Branch 3 taken 60330 times.
62824 if (IsSpecialRegexCharacter(*i)) {
178
1/2
✓ Branch 1 taken 2494 times.
✗ Branch 2 not taken.
2494 regex += "\\";
179 }
180
1/2
✓ Branch 2 taken 62824 times.
✗ Branch 3 not taken.
62824 regex += *i;
181 }
182 27170 return regex;
183 }
184
185
186 1755 std::string PathspecElementPattern::PlaintextSubPattern::GenerateGlobString()
187 const {
188 1755 std::string::const_iterator i = chars_.begin();
189 1755 const std::string::const_iterator iend = chars_.end();
190 1755 std::string glob_string;
191
2/2
✓ Branch 2 taken 8976 times.
✓ Branch 3 taken 1755 times.
10731 for (; i != iend; ++i) {
192
2/2
✓ Branch 2 taken 196 times.
✓ Branch 3 taken 8780 times.
8976 if (Pathspec::IsSpecialChar(*i)) {
193
1/2
✓ Branch 1 taken 196 times.
✗ Branch 2 not taken.
196 glob_string += "\\";
194 }
195
1/2
✓ Branch 2 taken 8976 times.
✗ Branch 3 not taken.
8976 glob_string += *i;
196 }
197 3510 return glob_string;
198 }
199
200 62824 bool PathspecElementPattern::PlaintextSubPattern::IsSpecialRegexCharacter(
201 const char chr) const {
202
8/8
✓ Branch 0 taken 61258 times.
✓ Branch 1 taken 97 times.
✓ Branch 2 taken 61112 times.
✓ Branch 3 taken 146 times.
✓ Branch 4 taken 60918 times.
✓ Branch 5 taken 194 times.
✓ Branch 6 taken 60820 times.
✓ Branch 7 taken 98 times.
61355 return (chr == '.' || chr == '\\' || chr == '*' || chr == '?' || chr == '['
203
10/10
✓ Branch 0 taken 60722 times.
✓ Branch 1 taken 98 times.
✓ Branch 2 taken 60673 times.
✓ Branch 3 taken 49 times.
✓ Branch 4 taken 60624 times.
✓ Branch 5 taken 49 times.
✓ Branch 6 taken 60575 times.
✓ Branch 7 taken 49 times.
✓ Branch 8 taken 60526 times.
✓ Branch 9 taken 49 times.
60820 || chr == ']' || chr == '(' || chr == ')' || chr == '{' || chr == '}'
204
8/8
✓ Branch 0 taken 61355 times.
✓ Branch 1 taken 1469 times.
✓ Branch 2 taken 60477 times.
✓ Branch 3 taken 49 times.
✓ Branch 4 taken 60428 times.
✓ Branch 5 taken 49 times.
✓ Branch 6 taken 98 times.
✓ Branch 7 taken 60330 times.
124179 || chr == '^' || chr == '$' || chr == '+');
205 }
206
207 3240 bool PathspecElementPattern::PlaintextSubPattern::Compare(
208 const SubPattern *other) const {
209
2/2
✓ Branch 1 taken 49 times.
✓ Branch 2 taken 3191 times.
3240 if (!other->IsPlaintext()) {
210 49 return false;
211 }
212
213 const PlaintextSubPattern
214
1/2
✓ Branch 0 taken 3191 times.
✗ Branch 1 not taken.
3191 *pt_other = dynamic_cast<const PlaintextSubPattern *>(other);
215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3191 times.
3191 assert(pt_other != NULL);
216 3191 return chars_ == pt_other->chars_;
217 }
218
219
220 std::string
221 1661 PathspecElementPattern::WildcardSubPattern::GenerateRegularExpression(
222 const bool is_relaxed) const {
223 return (is_relaxed) ? std::string(".*")
224
14/27
✓ Branch 0 taken 485 times.
✓ Branch 1 taken 1176 times.
✓ Branch 4 taken 485 times.
✗ Branch 5 not taken.
✓ Branch 8 taken 1176 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 1176 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 1176 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 1176 times.
✓ Branch 17 taken 485 times.
✗ Branch 18 not taken.
✓ Branch 19 taken 1176 times.
✓ Branch 20 taken 485 times.
✓ Branch 22 taken 1176 times.
✓ Branch 23 taken 485 times.
✓ Branch 25 taken 485 times.
✓ Branch 26 taken 1176 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.
3322 : std::string("[^") + Pathspec::kSeparator + "]*";
225 }
226
227
228 97 std::string PathspecElementPattern::WildcardSubPattern::GenerateGlobString()
229 const {
230
1/2
✓ Branch 2 taken 97 times.
✗ Branch 3 not taken.
97 return "*";
231 }
232
233
234 486 bool PathspecElementPattern::WildcardSubPattern::Compare(
235 const SubPattern *other) const {
236 486 return other->IsWildcard();
237 }
238
239
240 std::string
241 986 PathspecElementPattern::PlaceholderSubPattern::GenerateRegularExpression(
242 const bool is_relaxed) const {
243 // Note: strict and relaxed regex are the same!
244
3/6
✓ Branch 2 taken 986 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 986 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 986 times.
✗ Branch 9 not taken.
1972 return std::string("[^") + Pathspec::kSeparator + "]";
245 }
246
247 97 std::string PathspecElementPattern::PlaceholderSubPattern::GenerateGlobString()
248 const {
249
1/2
✓ Branch 2 taken 97 times.
✗ Branch 3 not taken.
97 return "?";
250 }
251
252 588 bool PathspecElementPattern::PlaceholderSubPattern::Compare(
253 const SubPattern *other) const {
254 588 return other->IsPlaceholder();
255 }
256