GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/pathspec/pathspec_pattern.cc
Date: 2026-04-05 02:35:23
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 9834 PathspecElementPattern::PathspecElementPattern(
13 const std::string::const_iterator begin,
14 9834 const std::string::const_iterator &end)
15 9834 : valid_(true) {
16
1/2
✓ Branch 1 taken 9834 times.
✗ Branch 2 not taken.
9834 Parse(begin, end);
17 9834 }
18
19 28457 PathspecElementPattern::PathspecElementPattern(
20 28457 const PathspecElementPattern &other)
21 28457 : valid_(other.valid_) {
22
1/2
✓ Branch 2 taken 28457 times.
✗ Branch 3 not taken.
28457 subpatterns_.reserve(other.subpatterns_.size());
23 28457 SubPatterns::const_iterator i = other.subpatterns_.begin();
24 28457 const SubPatterns::const_iterator iend = other.subpatterns_.end();
25
2/2
✓ Branch 1 taken 32163 times.
✓ Branch 2 taken 28457 times.
60620 for (; i != iend; ++i) {
26
2/4
✓ Branch 2 taken 32163 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 32163 times.
✗ Branch 6 not taken.
32163 subpatterns_.push_back((*i)->Clone());
27 }
28 28457 }
29
30 189 PathspecElementPattern &PathspecElementPattern::operator=(
31 const PathspecElementPattern &other) {
32
1/2
✓ Branch 0 taken 189 times.
✗ Branch 1 not taken.
189 if (this != &other) {
33 189 valid_ = other.valid_;
34 189 subpatterns_.clear();
35
1/2
✓ Branch 2 taken 189 times.
✗ Branch 3 not taken.
189 subpatterns_.reserve(other.subpatterns_.size());
36 189 SubPatterns::const_iterator i = other.subpatterns_.begin();
37 189 const SubPatterns::const_iterator iend = other.subpatterns_.end();
38
2/2
✓ Branch 1 taken 297 times.
✓ Branch 2 taken 189 times.
486 for (; i != iend; ++i) {
39
2/4
✓ Branch 2 taken 297 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 297 times.
✗ Branch 6 not taken.
297 subpatterns_.push_back((*i)->Clone());
40 }
41 }
42
43 189 return *this;
44 }
45
46
47 38285 PathspecElementPattern::~PathspecElementPattern() {
48 38285 SubPatterns::const_iterator i = subpatterns_.begin();
49 38285 const SubPatterns::const_iterator iend = subpatterns_.end();
50
2/2
✓ Branch 2 taken 44052 times.
✓ Branch 3 taken 38285 times.
82337 for (; i != iend; ++i) {
51
1/2
✓ Branch 1 taken 44052 times.
✗ Branch 2 not taken.
44052 delete *i;
52 }
53 38285 subpatterns_.clear();
54 38285 }
55
56
57 9834 void PathspecElementPattern::Parse(const std::string::const_iterator &begin,
58 const std::string::const_iterator &end) {
59 9834 std::string::const_iterator i = begin;
60
2/2
✓ Branch 1 taken 11834 times.
✓ Branch 2 taken 9834 times.
21668 while (i != end) {
61
3/4
✓ Branch 2 taken 3034 times.
✓ Branch 3 taken 8800 times.
✓ Branch 5 taken 3034 times.
✗ Branch 6 not taken.
11834 SubPattern *next = (Pathspec::IsSpecialChar(*i)) ? ParseSpecialChar(end, &i)
62
1/2
✓ Branch 1 taken 8800 times.
✗ Branch 2 not taken.
8800 : ParsePlaintext(end, &i);
63
3/4
✓ Branch 1 taken 11834 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 47 times.
✓ Branch 4 taken 11787 times.
11834 if (next->IsEmpty()) {
64 47 valid_ = false;
65
1/2
✓ Branch 0 taken 47 times.
✗ Branch 1 not taken.
47 delete next;
66 } else {
67
1/2
✓ Branch 1 taken 11787 times.
✗ Branch 2 not taken.
11787 subpatterns_.push_back(next);
68 }
69 }
70 9834 }
71
72
73 8800 PathspecElementPattern::SubPattern *PathspecElementPattern::ParsePlaintext(
74 const std::string::const_iterator &end, std::string::const_iterator *i) {
75 8800 PlaintextSubPattern *pattern = new PlaintextSubPattern();
76 8800 bool next_is_escaped = false;
77
78
2/2
✓ Branch 1 taken 37050 times.
✓ Branch 2 taken 8021 times.
45071 while (*i < end) {
79
6/6
✓ Branch 2 taken 1124 times.
✓ Branch 3 taken 35926 times.
✓ Branch 4 taken 779 times.
✓ Branch 5 taken 345 times.
✓ Branch 6 taken 779 times.
✓ Branch 7 taken 36271 times.
37050 if (Pathspec::IsSpecialChar(**i) && !next_is_escaped) {
80 779 break;
81 }
82
83
6/6
✓ Branch 1 taken 630 times.
✓ Branch 2 taken 35641 times.
✓ Branch 3 taken 524 times.
✓ Branch 4 taken 106 times.
✓ Branch 5 taken 524 times.
✓ Branch 6 taken 35747 times.
36271 if (**i == Pathspec::kEscaper && !next_is_escaped) {
84 524 next_is_escaped = true;
85
2/2
✓ Branch 0 taken 722 times.
✓ Branch 1 taken 35025 times.
35747 } else if (next_is_escaped) {
86
6/6
✓ Branch 2 taken 377 times.
✓ Branch 3 taken 345 times.
✓ Branch 5 taken 106 times.
✓ Branch 6 taken 271 times.
✓ Branch 7 taken 451 times.
✓ Branch 8 taken 271 times.
722 if (Pathspec::IsSpecialChar(**i) || **i == Pathspec::kEscaper) {
87 451 pattern->AddChar(**i);
88 451 next_is_escaped = false;
89 } else {
90 271 valid_ = false;
91 }
92 } else {
93
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 35025 times.
35025 assert(!Pathspec::IsSpecialChar(**i));
94 35025 pattern->AddChar(**i);
95 }
96
97 36271 ++(*i);
98 }
99
100 8800 return pattern;
101 }
102
103 3034 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 3034 times.
3034 assert(Pathspec::IsSpecialChar(**i));
106 3034 const char chr = **i;
107 3034 ++(*i);
108
109
2/3
✓ Branch 0 taken 1835 times.
✓ Branch 1 taken 1199 times.
✗ Branch 2 not taken.
3034 switch (chr) {
110 1835 case Pathspec::kWildcard:
111 1835 return new WildcardSubPattern();
112 1199 case Pathspec::kPlaceholder:
113 1199 return new PlaceholderSubPattern();
114 default:
115 assert(false && "unrecognized special character");
116 }
117 }
118
119 5264 std::string PathspecElementPattern::GenerateRegularExpression(
120 const bool is_relaxed) const {
121 5264 std::string result;
122 5264 SubPatterns::const_iterator i = subpatterns_.begin();
123 5264 const SubPatterns::const_iterator iend = subpatterns_.end();
124
2/2
✓ Branch 1 taken 6825 times.
✓ Branch 2 taken 5264 times.
12089 for (; i != iend; ++i) {
125
2/4
✓ Branch 2 taken 6825 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 6825 times.
✗ Branch 6 not taken.
6825 result += (*i)->GenerateRegularExpression(is_relaxed);
126 }
127 10528 return result;
128 }
129
130 892 std::string PathspecElementPattern::GenerateGlobString() const {
131 892 std::string result;
132 892 SubPatterns::const_iterator i = subpatterns_.begin();
133 892 const SubPatterns::const_iterator iend = subpatterns_.end();
134
2/2
✓ Branch 1 taken 1051 times.
✓ Branch 2 taken 892 times.
1943 for (; i != iend; ++i) {
135
2/4
✓ Branch 2 taken 1051 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1051 times.
✗ Branch 6 not taken.
1051 result += (*i)->GenerateGlobString();
136 }
137 1784 return result;
138 }
139
140 3090 bool PathspecElementPattern::operator==(
141 const PathspecElementPattern &other) const {
142 3090 if (subpatterns_.size() != other.subpatterns_.size()
143
5/6
✓ Branch 0 taken 2740 times.
✓ Branch 1 taken 350 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 2740 times.
✓ Branch 6 taken 350 times.
✓ Branch 7 taken 2740 times.
3090 || IsValid() != other.IsValid()) {
144 350 return false;
145 }
146
147 2740 SubPatterns::const_iterator i = subpatterns_.begin();
148 2740 const SubPatterns::const_iterator iend = subpatterns_.end();
149 2740 SubPatterns::const_iterator j = other.subpatterns_.begin();
150 2740 const SubPatterns::const_iterator jend = other.subpatterns_.end();
151
152
5/6
✓ Branch 3 taken 3118 times.
✓ Branch 4 taken 2094 times.
✓ Branch 6 taken 3118 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 3118 times.
✓ Branch 9 taken 2094 times.
5212 for (; i != iend && j != jend; ++i, ++j) {
153
3/4
✓ Branch 3 taken 3118 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 646 times.
✓ Branch 6 taken 2472 times.
3118 if (!(*i)->Compare(*j)) {
154 646 return false;
155 }
156 }
157
158 2094 return true;
159 }
160
161
162 //------------------------------------------------------------------------------
163
164
165 35476 void PathspecElementPattern::PlaintextSubPattern::AddChar(const char chr) {
166 35476 chars_.push_back(chr);
167 35476 }
168
169 std::string
170 5055 PathspecElementPattern::PlaintextSubPattern::GenerateRegularExpression(
171 const bool is_relaxed) const {
172 // Note: strict and relaxed regex are the same!
173 5055 std::string::const_iterator i = chars_.begin();
174 5055 const std::string::const_iterator iend = chars_.end();
175 5055 std::string regex;
176
2/2
✓ Branch 2 taken 21333 times.
✓ Branch 3 taken 5055 times.
26388 for (; i != iend; ++i) {
177
2/2
✓ Branch 2 taken 1573 times.
✓ Branch 3 taken 19760 times.
21333 if (IsSpecialRegexCharacter(*i)) {
178
1/2
✓ Branch 1 taken 1573 times.
✗ Branch 2 not taken.
1573 regex += "\\";
179 }
180
1/2
✓ Branch 2 taken 21333 times.
✗ Branch 3 not taken.
21333 regex += *i;
181 }
182 10110 return regex;
183 }
184
185
186 945 std::string PathspecElementPattern::PlaintextSubPattern::GenerateGlobString()
187 const {
188 945 std::string::const_iterator i = chars_.begin();
189 945 const std::string::const_iterator iend = chars_.end();
190 945 std::string glob_string;
191
2/2
✓ Branch 2 taken 4824 times.
✓ Branch 3 taken 945 times.
5769 for (; i != iend; ++i) {
192
2/2
✓ Branch 2 taken 104 times.
✓ Branch 3 taken 4720 times.
4824 if (Pathspec::IsSpecialChar(*i)) {
193
1/2
✓ Branch 1 taken 104 times.
✗ Branch 2 not taken.
104 glob_string += "\\";
194 }
195
1/2
✓ Branch 2 taken 4824 times.
✗ Branch 3 not taken.
4824 glob_string += *i;
196 }
197 1890 return glob_string;
198 }
199
200 21333 bool PathspecElementPattern::PlaintextSubPattern::IsSpecialRegexCharacter(
201 const char chr) const {
202
8/8
✓ Branch 0 taken 20273 times.
✓ Branch 1 taken 54 times.
✓ Branch 2 taken 20192 times.
✓ Branch 3 taken 81 times.
✓ Branch 4 taken 20084 times.
✓ Branch 5 taken 108 times.
✓ Branch 6 taken 20030 times.
✓ Branch 7 taken 54 times.
20327 return (chr == '.' || chr == '\\' || chr == '*' || chr == '?' || chr == '['
203
10/10
✓ Branch 0 taken 19976 times.
✓ Branch 1 taken 54 times.
✓ Branch 2 taken 19949 times.
✓ Branch 3 taken 27 times.
✓ Branch 4 taken 19922 times.
✓ Branch 5 taken 27 times.
✓ Branch 6 taken 19895 times.
✓ Branch 7 taken 27 times.
✓ Branch 8 taken 19868 times.
✓ Branch 9 taken 27 times.
20030 || chr == ']' || chr == '(' || chr == ')' || chr == '{' || chr == '}'
204
8/8
✓ Branch 0 taken 20327 times.
✓ Branch 1 taken 1006 times.
✓ Branch 2 taken 19841 times.
✓ Branch 3 taken 27 times.
✓ Branch 4 taken 19814 times.
✓ Branch 5 taken 27 times.
✓ Branch 6 taken 54 times.
✓ Branch 7 taken 19760 times.
41660 || chr == '^' || chr == '$' || chr == '+');
205 }
206
207 2316 bool PathspecElementPattern::PlaintextSubPattern::Compare(
208 const SubPattern *other) const {
209
2/2
✓ Branch 1 taken 27 times.
✓ Branch 2 taken 2289 times.
2316 if (!other->IsPlaintext()) {
210 27 return false;
211 }
212
213 const PlaintextSubPattern
214
1/2
✓ Branch 0 taken 2289 times.
✗ Branch 1 not taken.
2289 *pt_other = dynamic_cast<const PlaintextSubPattern *>(other);
215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2289 times.
2289 assert(pt_other != NULL);
216 2289 return chars_ == pt_other->chars_;
217 }
218
219
220 std::string
221 1200 PathspecElementPattern::WildcardSubPattern::GenerateRegularExpression(
222 const bool is_relaxed) const {
223 return (is_relaxed) ? std::string(".*")
224
14/27
✓ Branch 0 taken 404 times.
✓ Branch 1 taken 796 times.
✓ Branch 4 taken 404 times.
✗ Branch 5 not taken.
✓ Branch 8 taken 796 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 796 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 796 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 796 times.
✓ Branch 17 taken 404 times.
✗ Branch 18 not taken.
✓ Branch 19 taken 796 times.
✓ Branch 20 taken 404 times.
✓ Branch 22 taken 796 times.
✓ Branch 23 taken 404 times.
✓ Branch 25 taken 404 times.
✓ Branch 26 taken 796 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.
2400 : std::string("[^") + Pathspec::kSeparator + "]*";
225 }
226
227
228 53 std::string PathspecElementPattern::WildcardSubPattern::GenerateGlobString()
229 const {
230
1/2
✓ Branch 2 taken 53 times.
✗ Branch 3 not taken.
53 return "*";
231 }
232
233
234 478 bool PathspecElementPattern::WildcardSubPattern::Compare(
235 const SubPattern *other) const {
236 478 return other->IsWildcard();
237 }
238
239
240 std::string
241 570 PathspecElementPattern::PlaceholderSubPattern::GenerateRegularExpression(
242 const bool is_relaxed) const {
243 // Note: strict and relaxed regex are the same!
244
3/6
✓ Branch 2 taken 570 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 570 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 570 times.
✗ Branch 9 not taken.
1140 return std::string("[^") + Pathspec::kSeparator + "]";
245 }
246
247 53 std::string PathspecElementPattern::PlaceholderSubPattern::GenerateGlobString()
248 const {
249
1/2
✓ Branch 2 taken 53 times.
✗ Branch 3 not taken.
53 return "?";
250 }
251
252 324 bool PathspecElementPattern::PlaceholderSubPattern::Compare(
253 const SubPattern *other) const {
254 324 return other->IsPlaceholder();
255 }
256