GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/pathspec/pathspec_pattern.cc
Date: 2026-09-13 02:40:16
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 7057 PathspecElementPattern::PathspecElementPattern(
13 const std::string::const_iterator begin,
14 7057 const std::string::const_iterator &end)
15 7057 : valid_(true) {
16
1/2
✓ Branch 1 taken 7057 times.
✗ Branch 2 not taken.
7057 Parse(begin, end);
17 7057 }
18
19 24087 PathspecElementPattern::PathspecElementPattern(
20 24087 const PathspecElementPattern &other)
21 24087 : valid_(other.valid_) {
22
1/2
✓ Branch 2 taken 24087 times.
✗ Branch 3 not taken.
24087 subpatterns_.reserve(other.subpatterns_.size());
23 24087 SubPatterns::const_iterator i = other.subpatterns_.begin();
24 24087 const SubPatterns::const_iterator iend = other.subpatterns_.end();
25
2/2
✓ Branch 1 taken 26023 times.
✓ Branch 2 taken 24087 times.
50110 for (; i != iend; ++i) {
26
2/4
✓ Branch 2 taken 26023 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 26023 times.
✗ Branch 6 not taken.
26023 subpatterns_.push_back((*i)->Clone());
27 }
28 24087 }
29
30 91 PathspecElementPattern &PathspecElementPattern::operator=(
31 const PathspecElementPattern &other) {
32
1/2
✓ Branch 0 taken 91 times.
✗ Branch 1 not taken.
91 if (this != &other) {
33 91 valid_ = other.valid_;
34 91 subpatterns_.clear();
35
1/2
✓ Branch 2 taken 91 times.
✗ Branch 3 not taken.
91 subpatterns_.reserve(other.subpatterns_.size());
36 91 SubPatterns::const_iterator i = other.subpatterns_.begin();
37 91 const SubPatterns::const_iterator iend = other.subpatterns_.end();
38
2/2
✓ Branch 1 taken 143 times.
✓ Branch 2 taken 91 times.
234 for (; i != iend; ++i) {
39
2/4
✓ Branch 2 taken 143 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 143 times.
✗ Branch 6 not taken.
143 subpatterns_.push_back((*i)->Clone());
40 }
41 }
42
43 91 return *this;
44 }
45
46
47 31138 PathspecElementPattern::~PathspecElementPattern() {
48 31138 SubPatterns::const_iterator i = subpatterns_.begin();
49 31138 const SubPatterns::const_iterator iend = subpatterns_.end();
50
2/2
✓ Branch 2 taken 34107 times.
✓ Branch 3 taken 31138 times.
65245 for (; i != iend; ++i) {
51
1/2
✓ Branch 1 taken 34107 times.
✗ Branch 2 not taken.
34107 delete *i;
52 }
53 31138 subpatterns_.clear();
54 31138 }
55
56
57 7057 void PathspecElementPattern::Parse(const std::string::const_iterator &begin,
58 const std::string::const_iterator &end) {
59 7057 std::string::const_iterator i = begin;
60
2/2
✓ Branch 1 taken 8055 times.
✓ Branch 2 taken 7057 times.
15112 while (i != end) {
61
3/4
✓ Branch 2 taken 1530 times.
✓ Branch 3 taken 6525 times.
✓ Branch 5 taken 1530 times.
✗ Branch 6 not taken.
8055 SubPattern *next = (Pathspec::IsSpecialChar(*i)) ? ParseSpecialChar(end, &i)
62
1/2
✓ Branch 1 taken 6525 times.
✗ Branch 2 not taken.
6525 : ParsePlaintext(end, &i);
63
3/4
✓ Branch 1 taken 8055 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 17 times.
✓ Branch 4 taken 8038 times.
8055 if (next->IsEmpty()) {
64 17 valid_ = false;
65
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
17 delete next;
66 } else {
67
1/2
✓ Branch 1 taken 8038 times.
✗ Branch 2 not taken.
8038 subpatterns_.push_back(next);
68 }
69 }
70 7057 }
71
72
73 6525 PathspecElementPattern::SubPattern *PathspecElementPattern::ParsePlaintext(
74 const std::string::const_iterator &end, std::string::const_iterator *i) {
75 6525 PlaintextSubPattern *pattern = new PlaintextSubPattern();
76 6525 bool next_is_escaped = false;
77
78
2/2
✓ Branch 1 taken 28755 times.
✓ Branch 2 taken 6164 times.
34919 while (*i < end) {
79
6/6
✓ Branch 2 taken 526 times.
✓ Branch 3 taken 28229 times.
✓ Branch 4 taken 361 times.
✓ Branch 5 taken 165 times.
✓ Branch 6 taken 361 times.
✓ Branch 7 taken 28394 times.
28755 if (Pathspec::IsSpecialChar(**i) && !next_is_escaped) {
80 361 break;
81 }
82
83
6/6
✓ Branch 1 taken 297 times.
✓ Branch 2 taken 28097 times.
✓ Branch 3 taken 246 times.
✓ Branch 4 taken 51 times.
✓ Branch 5 taken 246 times.
✓ Branch 6 taken 28148 times.
28394 if (**i == Pathspec::kEscaper && !next_is_escaped) {
84 246 next_is_escaped = true;
85
2/2
✓ Branch 0 taken 332 times.
✓ Branch 1 taken 27816 times.
28148 } else if (next_is_escaped) {
86
6/6
✓ Branch 2 taken 167 times.
✓ Branch 3 taken 165 times.
✓ Branch 5 taken 51 times.
✓ Branch 6 taken 116 times.
✓ Branch 7 taken 216 times.
✓ Branch 8 taken 116 times.
332 if (Pathspec::IsSpecialChar(**i) || **i == Pathspec::kEscaper) {
87 216 pattern->AddChar(**i);
88 216 next_is_escaped = false;
89 } else {
90 116 valid_ = false;
91 }
92 } else {
93
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 27816 times.
27816 assert(!Pathspec::IsSpecialChar(**i));
94 27816 pattern->AddChar(**i);
95 }
96
97 28394 ++(*i);
98 }
99
100 6525 return pattern;
101 }
102
103 1530 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 1530 times.
1530 assert(Pathspec::IsSpecialChar(**i));
106 1530 const char chr = **i;
107 1530 ++(*i);
108
109
2/3
✓ Branch 0 taken 987 times.
✓ Branch 1 taken 543 times.
✗ Branch 2 not taken.
1530 switch (chr) {
110 987 case Pathspec::kWildcard:
111 987 return new WildcardSubPattern();
112 543 case Pathspec::kPlaceholder:
113 543 return new PlaceholderSubPattern();
114 default:
115 assert(false && "unrecognized special character");
116 }
117 }
118
119 4965 std::string PathspecElementPattern::GenerateRegularExpression(
120 const bool is_relaxed) const {
121 4965 std::string result;
122 4965 SubPatterns::const_iterator i = subpatterns_.begin();
123 4965 const SubPatterns::const_iterator iend = subpatterns_.end();
124
2/2
✓ Branch 1 taken 5756 times.
✓ Branch 2 taken 4965 times.
10721 for (; i != iend; ++i) {
125
2/4
✓ Branch 2 taken 5756 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 5756 times.
✗ Branch 6 not taken.
5756 result += (*i)->GenerateRegularExpression(is_relaxed);
126 }
127 9930 return result;
128 }
129
130 416 std::string PathspecElementPattern::GenerateGlobString() const {
131 416 std::string result;
132 416 SubPatterns::const_iterator i = subpatterns_.begin();
133 416 const SubPatterns::const_iterator iend = subpatterns_.end();
134
2/2
✓ Branch 1 taken 491 times.
✓ Branch 2 taken 416 times.
907 for (; i != iend; ++i) {
135
2/4
✓ Branch 2 taken 491 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 491 times.
✗ Branch 6 not taken.
491 result += (*i)->GenerateGlobString();
136 }
137 832 return result;
138 }
139
140 1646 bool PathspecElementPattern::operator==(
141 const PathspecElementPattern &other) const {
142 1646 if (subpatterns_.size() != other.subpatterns_.size()
143
5/6
✓ Branch 0 taken 1500 times.
✓ Branch 1 taken 146 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1500 times.
✓ Branch 6 taken 146 times.
✓ Branch 7 taken 1500 times.
1646 || IsValid() != other.IsValid()) {
144 146 return false;
145 }
146
147 1500 SubPatterns::const_iterator i = subpatterns_.begin();
148 1500 const SubPatterns::const_iterator iend = subpatterns_.end();
149 1500 SubPatterns::const_iterator j = other.subpatterns_.begin();
150 1500 const SubPatterns::const_iterator jend = other.subpatterns_.end();
151
152
5/6
✓ Branch 3 taken 1682 times.
✓ Branch 4 taken 1038 times.
✓ Branch 6 taken 1682 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 1682 times.
✓ Branch 9 taken 1038 times.
2720 for (; i != iend && j != jend; ++i, ++j) {
153
3/4
✓ Branch 3 taken 1682 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 462 times.
✓ Branch 6 taken 1220 times.
1682 if (!(*i)->Compare(*j)) {
154 462 return false;
155 }
156 }
157
158 1038 return true;
159 }
160
161
162 //------------------------------------------------------------------------------
163
164
165 28032 void PathspecElementPattern::PlaintextSubPattern::AddChar(const char chr) {
166 28032 chars_.push_back(chr);
167 28032 }
168
169 std::string
170 4788 PathspecElementPattern::PlaintextSubPattern::GenerateRegularExpression(
171 const bool is_relaxed) const {
172 // Note: strict and relaxed regex are the same!
173 4788 std::string::const_iterator i = chars_.begin();
174 4788 const std::string::const_iterator iend = chars_.end();
175 4788 std::string regex;
176
2/2
✓ Branch 2 taken 21459 times.
✓ Branch 3 taken 4788 times.
26247 for (; i != iend; ++i) {
177
2/2
✓ Branch 2 taken 794 times.
✓ Branch 3 taken 20665 times.
21459 if (IsSpecialRegexCharacter(*i)) {
178
1/2
✓ Branch 1 taken 794 times.
✗ Branch 2 not taken.
794 regex += "\\";
179 }
180
1/2
✓ Branch 2 taken 21459 times.
✗ Branch 3 not taken.
21459 regex += *i;
181 }
182 9576 return regex;
183 }
184
185
186 441 std::string PathspecElementPattern::PlaintextSubPattern::GenerateGlobString()
187 const {
188 441 std::string::const_iterator i = chars_.begin();
189 441 const std::string::const_iterator iend = chars_.end();
190 441 std::string glob_string;
191
2/2
✓ Branch 2 taken 2248 times.
✓ Branch 3 taken 441 times.
2689 for (; i != iend; ++i) {
192
2/2
✓ Branch 2 taken 48 times.
✓ Branch 3 taken 2200 times.
2248 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 2248 times.
✗ Branch 3 not taken.
2248 glob_string += *i;
196 }
197 882 return glob_string;
198 }
199
200 21459 bool PathspecElementPattern::PlaintextSubPattern::IsSpecialRegexCharacter(
201 const char chr) const {
202
8/8
✓ Branch 0 taken 20912 times.
✓ Branch 1 taken 26 times.
✓ Branch 2 taken 20873 times.
✓ Branch 3 taken 39 times.
✓ Branch 4 taken 20821 times.
✓ Branch 5 taken 52 times.
✓ Branch 6 taken 20795 times.
✓ Branch 7 taken 26 times.
20938 return (chr == '.' || chr == '\\' || chr == '*' || chr == '?' || chr == '['
203
10/10
✓ Branch 0 taken 20769 times.
✓ Branch 1 taken 26 times.
✓ Branch 2 taken 20756 times.
✓ Branch 3 taken 13 times.
✓ Branch 4 taken 20743 times.
✓ Branch 5 taken 13 times.
✓ Branch 6 taken 20730 times.
✓ Branch 7 taken 13 times.
✓ Branch 8 taken 20717 times.
✓ Branch 9 taken 13 times.
20795 || chr == ']' || chr == '(' || chr == ')' || chr == '{' || chr == '}'
204
8/8
✓ Branch 0 taken 20938 times.
✓ Branch 1 taken 521 times.
✓ Branch 2 taken 20704 times.
✓ Branch 3 taken 13 times.
✓ Branch 4 taken 20691 times.
✓ Branch 5 taken 13 times.
✓ Branch 6 taken 26 times.
✓ Branch 7 taken 20665 times.
42397 || chr == '^' || chr == '$' || chr == '+');
205 }
206
207 1324 bool PathspecElementPattern::PlaintextSubPattern::Compare(
208 const SubPattern *other) const {
209
2/2
✓ Branch 1 taken 13 times.
✓ Branch 2 taken 1311 times.
1324 if (!other->IsPlaintext()) {
210 13 return false;
211 }
212
213 const PlaintextSubPattern
214
1/2
✓ Branch 0 taken 1311 times.
✗ Branch 1 not taken.
1311 *pt_other = dynamic_cast<const PlaintextSubPattern *>(other);
215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1311 times.
1311 assert(pt_other != NULL);
216 1311 return chars_ == pt_other->chars_;
217 }
218
219
220 std::string
221 702 PathspecElementPattern::WildcardSubPattern::GenerateRegularExpression(
222 const bool is_relaxed) const {
223 return (is_relaxed) ? std::string(".*")
224
14/27
✓ Branch 0 taken 278 times.
✓ Branch 1 taken 424 times.
✓ Branch 4 taken 278 times.
✗ Branch 5 not taken.
✓ Branch 8 taken 424 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 424 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 424 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 424 times.
✓ Branch 17 taken 278 times.
✗ Branch 18 not taken.
✓ Branch 19 taken 424 times.
✓ Branch 20 taken 278 times.
✓ Branch 22 taken 424 times.
✓ Branch 23 taken 278 times.
✓ Branch 25 taken 278 times.
✓ Branch 26 taken 424 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.
1404 : std::string("[^") + Pathspec::kSeparator + "]*";
225 }
226
227
228 25 std::string PathspecElementPattern::WildcardSubPattern::GenerateGlobString()
229 const {
230
1/2
✓ Branch 2 taken 25 times.
✗ Branch 3 not taken.
25 return "*";
231 }
232
233
234 202 bool PathspecElementPattern::WildcardSubPattern::Compare(
235 const SubPattern *other) const {
236 202 return other->IsWildcard();
237 }
238
239
240 std::string
241 266 PathspecElementPattern::PlaceholderSubPattern::GenerateRegularExpression(
242 const bool is_relaxed) const {
243 // Note: strict and relaxed regex are the same!
244
3/6
✓ Branch 2 taken 266 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 266 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 266 times.
✗ Branch 9 not taken.
532 return std::string("[^") + Pathspec::kSeparator + "]";
245 }
246
247 25 std::string PathspecElementPattern::PlaceholderSubPattern::GenerateGlobString()
248 const {
249
1/2
✓ Branch 2 taken 25 times.
✗ Branch 3 not taken.
25 return "?";
250 }
251
252 156 bool PathspecElementPattern::PlaceholderSubPattern::Compare(
253 const SubPattern *other) const {
254 156 return other->IsPlaceholder();
255 }
256