GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/pathspec/pathspec_pattern.cc
Date: 2026-05-24 02:35:55
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 8665 PathspecElementPattern::PathspecElementPattern(
13 const std::string::const_iterator begin,
14 8665 const std::string::const_iterator &end)
15 8665 : valid_(true) {
16
1/2
✓ Branch 1 taken 8665 times.
✗ Branch 2 not taken.
8665 Parse(begin, end);
17 8665 }
18
19 23618 PathspecElementPattern::PathspecElementPattern(
20 23618 const PathspecElementPattern &other)
21 23618 : valid_(other.valid_) {
22
1/2
✓ Branch 2 taken 23618 times.
✗ Branch 3 not taken.
23618 subpatterns_.reserve(other.subpatterns_.size());
23 23618 SubPatterns::const_iterator i = other.subpatterns_.begin();
24 23618 const SubPatterns::const_iterator iend = other.subpatterns_.end();
25
2/2
✓ Branch 1 taken 26639 times.
✓ Branch 2 taken 23618 times.
50257 for (; i != iend; ++i) {
26
2/4
✓ Branch 2 taken 26639 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 26639 times.
✗ Branch 6 not taken.
26639 subpatterns_.push_back((*i)->Clone());
27 }
28 23618 }
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 32277 PathspecElementPattern::~PathspecElementPattern() {
48 32277 SubPatterns::const_iterator i = subpatterns_.begin();
49 32277 const SubPatterns::const_iterator iend = subpatterns_.end();
50
2/2
✓ Branch 2 taken 37200 times.
✓ Branch 3 taken 32277 times.
69477 for (; i != iend; ++i) {
51
1/2
✓ Branch 1 taken 37200 times.
✗ Branch 2 not taken.
37200 delete *i;
52 }
53 32277 subpatterns_.clear();
54 32277 }
55
56
57 8665 void PathspecElementPattern::Parse(const std::string::const_iterator &begin,
58 const std::string::const_iterator &end) {
59 8665 std::string::const_iterator i = begin;
60
2/2
✓ Branch 1 taken 10474 times.
✓ Branch 2 taken 8665 times.
19139 while (i != end) {
61
3/4
✓ Branch 2 taken 2261 times.
✓ Branch 3 taken 8213 times.
✓ Branch 5 taken 2261 times.
✗ Branch 6 not taken.
10474 SubPattern *next = (Pathspec::IsSpecialChar(*i)) ? ParseSpecialChar(end, &i)
62
1/2
✓ Branch 1 taken 8213 times.
✗ Branch 2 not taken.
8213 : ParsePlaintext(end, &i);
63
3/4
✓ Branch 1 taken 10474 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 15 times.
✓ Branch 4 taken 10459 times.
10474 if (next->IsEmpty()) {
64 15 valid_ = false;
65
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 delete next;
66 } else {
67
1/2
✓ Branch 1 taken 10459 times.
✗ Branch 2 not taken.
10459 subpatterns_.push_back(next);
68 }
69 }
70 8665 }
71
72
73 8213 PathspecElementPattern::SubPattern *PathspecElementPattern::ParsePlaintext(
74 const std::string::const_iterator &end, std::string::const_iterator *i) {
75 8213 PlaintextSubPattern *pattern = new PlaintextSubPattern();
76 8213 bool next_is_escaped = false;
77
78
2/2
✓ Branch 1 taken 36151 times.
✓ Branch 2 taken 7525 times.
43676 while (*i < end) {
79
6/6
✓ Branch 2 taken 1026 times.
✓ Branch 3 taken 35125 times.
✓ Branch 4 taken 688 times.
✓ Branch 5 taken 338 times.
✓ Branch 6 taken 688 times.
✓ Branch 7 taken 35463 times.
36151 if (Pathspec::IsSpecialChar(**i) && !next_is_escaped) {
80 688 break;
81 }
82
83
6/6
✓ Branch 1 taken 587 times.
✓ Branch 2 taken 34876 times.
✓ Branch 3 taken 483 times.
✓ Branch 4 taken 104 times.
✓ Branch 5 taken 483 times.
✓ Branch 6 taken 34980 times.
35463 if (**i == Pathspec::kEscaper && !next_is_escaped) {
84 483 next_is_escaped = true;
85
2/2
✓ Branch 0 taken 617 times.
✓ Branch 1 taken 34363 times.
34980 } else if (next_is_escaped) {
86
6/6
✓ Branch 2 taken 279 times.
✓ Branch 3 taken 338 times.
✓ Branch 5 taken 104 times.
✓ Branch 6 taken 175 times.
✓ Branch 7 taken 442 times.
✓ Branch 8 taken 175 times.
617 if (Pathspec::IsSpecialChar(**i) || **i == Pathspec::kEscaper) {
87 442 pattern->AddChar(**i);
88 442 next_is_escaped = false;
89 } else {
90 175 valid_ = false;
91 }
92 } else {
93
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 34363 times.
34363 assert(!Pathspec::IsSpecialChar(**i));
94 34363 pattern->AddChar(**i);
95 }
96
97 35463 ++(*i);
98 }
99
100 8213 return pattern;
101 }
102
103 2261 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 2261 times.
2261 assert(Pathspec::IsSpecialChar(**i));
106 2261 const char chr = **i;
107 2261 ++(*i);
108
109
2/3
✓ Branch 0 taken 1275 times.
✓ Branch 1 taken 986 times.
✗ Branch 2 not taken.
2261 switch (chr) {
110 1275 case Pathspec::kWildcard:
111 1275 return new WildcardSubPattern();
112 986 case Pathspec::kPlaceholder:
113 986 return new PlaceholderSubPattern();
114 default:
115 assert(false && "unrecognized special character");
116 }
117 }
118
119 5647 std::string PathspecElementPattern::GenerateRegularExpression(
120 const bool is_relaxed) const {
121 5647 std::string result;
122 5647 SubPatterns::const_iterator i = subpatterns_.begin();
123 5647 const SubPatterns::const_iterator iend = subpatterns_.end();
124
2/2
✓ Branch 1 taken 7126 times.
✓ Branch 2 taken 5647 times.
12773 for (; i != iend; ++i) {
125
2/4
✓ Branch 2 taken 7126 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 7126 times.
✗ Branch 6 not taken.
7126 result += (*i)->GenerateRegularExpression(is_relaxed);
126 }
127 11294 return result;
128 }
129
130 896 std::string PathspecElementPattern::GenerateGlobString() const {
131 896 std::string result;
132 896 SubPatterns::const_iterator i = subpatterns_.begin();
133 896 const SubPatterns::const_iterator iend = subpatterns_.end();
134
2/2
✓ Branch 1 taken 1058 times.
✓ Branch 2 taken 896 times.
1954 for (; i != iend; ++i) {
135
2/4
✓ Branch 2 taken 1058 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1058 times.
✗ Branch 6 not taken.
1058 result += (*i)->GenerateGlobString();
136 }
137 1792 return result;
138 }
139
140 2442 bool PathspecElementPattern::operator==(
141 const PathspecElementPattern &other) const {
142 2442 if (subpatterns_.size() != other.subpatterns_.size()
143
5/6
✓ Branch 0 taken 2220 times.
✓ Branch 1 taken 222 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 2220 times.
✓ Branch 6 taken 222 times.
✓ Branch 7 taken 2220 times.
2442 || IsValid() != other.IsValid()) {
144 222 return false;
145 }
146
147 2220 SubPatterns::const_iterator i = subpatterns_.begin();
148 2220 const SubPatterns::const_iterator iend = subpatterns_.end();
149 2220 SubPatterns::const_iterator j = other.subpatterns_.begin();
150 2220 const SubPatterns::const_iterator jend = other.subpatterns_.end();
151
152
5/6
✓ Branch 3 taken 2598 times.
✓ Branch 4 taken 1683 times.
✓ Branch 6 taken 2598 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2598 times.
✓ Branch 9 taken 1683 times.
4281 for (; i != iend && j != jend; ++i, ++j) {
153
3/4
✓ Branch 3 taken 2598 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 537 times.
✓ Branch 6 taken 2061 times.
2598 if (!(*i)->Compare(*j)) {
154 537 return false;
155 }
156 }
157
158 1683 return true;
159 }
160
161
162 //------------------------------------------------------------------------------
163
164
165 34805 void PathspecElementPattern::PlaintextSubPattern::AddChar(const char chr) {
166 34805 chars_.push_back(chr);
167 34805 }
168
169 std::string
170 5596 PathspecElementPattern::PlaintextSubPattern::GenerateRegularExpression(
171 const bool is_relaxed) const {
172 // Note: strict and relaxed regex are the same!
173 5596 std::string::const_iterator i = chars_.begin();
174 5596 const std::string::const_iterator iend = chars_.end();
175 5596 std::string regex;
176
2/2
✓ Branch 2 taken 24683 times.
✓ Branch 3 taken 5596 times.
30279 for (; i != iend; ++i) {
177
2/2
✓ Branch 2 taken 1413 times.
✓ Branch 3 taken 23270 times.
24683 if (IsSpecialRegexCharacter(*i)) {
178
1/2
✓ Branch 1 taken 1413 times.
✗ Branch 2 not taken.
1413 regex += "\\";
179 }
180
1/2
✓ Branch 2 taken 24683 times.
✗ Branch 3 not taken.
24683 regex += *i;
181 }
182 11192 return regex;
183 }
184
185
186 950 std::string PathspecElementPattern::PlaintextSubPattern::GenerateGlobString()
187 const {
188 950 std::string::const_iterator i = chars_.begin();
189 950 const std::string::const_iterator iend = chars_.end();
190 950 std::string glob_string;
191
2/2
✓ Branch 2 taken 4844 times.
✓ Branch 3 taken 950 times.
5794 for (; i != iend; ++i) {
192
2/2
✓ Branch 2 taken 104 times.
✓ Branch 3 taken 4740 times.
4844 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 4844 times.
✗ Branch 3 not taken.
4844 glob_string += *i;
196 }
197 1900 return glob_string;
198 }
199
200 24683 bool PathspecElementPattern::PlaintextSubPattern::IsSpecialRegexCharacter(
201 const char chr) const {
202
8/8
✓ Branch 0 taken 23764 times.
✓ Branch 1 taken 52 times.
✓ Branch 2 taken 23686 times.
✓ Branch 3 taken 78 times.
✓ Branch 4 taken 23582 times.
✓ Branch 5 taken 104 times.
✓ Branch 6 taken 23530 times.
✓ Branch 7 taken 52 times.
23816 return (chr == '.' || chr == '\\' || chr == '*' || chr == '?' || chr == '['
203
10/10
✓ Branch 0 taken 23478 times.
✓ Branch 1 taken 52 times.
✓ Branch 2 taken 23452 times.
✓ Branch 3 taken 26 times.
✓ Branch 4 taken 23426 times.
✓ Branch 5 taken 26 times.
✓ Branch 6 taken 23400 times.
✓ Branch 7 taken 26 times.
✓ Branch 8 taken 23374 times.
✓ Branch 9 taken 26 times.
23530 || chr == ']' || chr == '(' || chr == ')' || chr == '{' || chr == '}'
204
8/8
✓ Branch 0 taken 23816 times.
✓ Branch 1 taken 867 times.
✓ Branch 2 taken 23348 times.
✓ Branch 3 taken 26 times.
✓ Branch 4 taken 23322 times.
✓ Branch 5 taken 26 times.
✓ Branch 6 taken 52 times.
✓ Branch 7 taken 23270 times.
48499 || chr == '^' || chr == '$' || chr == '+');
205 }
206
207 1956 bool PathspecElementPattern::PlaintextSubPattern::Compare(
208 const SubPattern *other) const {
209
2/2
✓ Branch 1 taken 27 times.
✓ Branch 2 taken 1929 times.
1956 if (!other->IsPlaintext()) {
210 27 return false;
211 }
212
213 const PlaintextSubPattern
214
1/2
✓ Branch 0 taken 1929 times.
✗ Branch 1 not taken.
1929 *pt_other = dynamic_cast<const PlaintextSubPattern *>(other);
215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1929 times.
1929 assert(pt_other != NULL);
216 1929 return chars_ == pt_other->chars_;
217 }
218
219
220 std::string
221 975 PathspecElementPattern::WildcardSubPattern::GenerateRegularExpression(
222 const bool is_relaxed) const {
223 return (is_relaxed) ? std::string(".*")
224
14/27
✓ Branch 0 taken 318 times.
✓ Branch 1 taken 657 times.
✓ Branch 4 taken 318 times.
✗ Branch 5 not taken.
✓ Branch 8 taken 657 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 657 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 657 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 657 times.
✓ Branch 17 taken 318 times.
✗ Branch 18 not taken.
✓ Branch 19 taken 657 times.
✓ Branch 20 taken 318 times.
✓ Branch 22 taken 657 times.
✓ Branch 23 taken 318 times.
✓ Branch 25 taken 318 times.
✓ Branch 26 taken 657 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.
1950 : std::string("[^") + Pathspec::kSeparator + "]*";
225 }
226
227
228 54 std::string PathspecElementPattern::WildcardSubPattern::GenerateGlobString()
229 const {
230
1/2
✓ Branch 2 taken 54 times.
✗ Branch 3 not taken.
54 return "*";
231 }
232
233
234 318 bool PathspecElementPattern::WildcardSubPattern::Compare(
235 const SubPattern *other) const {
236 318 return other->IsWildcard();
237 }
238
239
240 std::string
241 555 PathspecElementPattern::PlaceholderSubPattern::GenerateRegularExpression(
242 const bool is_relaxed) const {
243 // Note: strict and relaxed regex are the same!
244
3/6
✓ Branch 2 taken 555 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 555 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 555 times.
✗ Branch 9 not taken.
1110 return std::string("[^") + Pathspec::kSeparator + "]";
245 }
246
247 54 std::string PathspecElementPattern::PlaceholderSubPattern::GenerateGlobString()
248 const {
249
1/2
✓ Branch 2 taken 54 times.
✗ Branch 3 not taken.
54 return "?";
250 }
251
252 324 bool PathspecElementPattern::PlaceholderSubPattern::Compare(
253 const SubPattern *other) const {
254 324 return other->IsPlaceholder();
255 }
256