GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/pathspec/pathspec_pattern.cc
Date: 2026-04-12 02:41:08
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 3226 PathspecElementPattern::PathspecElementPattern(
13 const std::string::const_iterator begin,
14 3226 const std::string::const_iterator &end)
15 3226 : valid_(true) {
16
1/2
✓ Branch 1 taken 3226 times.
✗ Branch 2 not taken.
3226 Parse(begin, end);
17 3226 }
18
19 12484 PathspecElementPattern::PathspecElementPattern(
20 12484 const PathspecElementPattern &other)
21 12484 : valid_(other.valid_) {
22
1/2
✓ Branch 2 taken 12484 times.
✗ Branch 3 not taken.
12484 subpatterns_.reserve(other.subpatterns_.size());
23 12484 SubPatterns::const_iterator i = other.subpatterns_.begin();
24 12484 const SubPatterns::const_iterator iend = other.subpatterns_.end();
25
2/2
✓ Branch 1 taken 13305 times.
✓ Branch 2 taken 12484 times.
25789 for (; i != iend; ++i) {
26
2/4
✓ Branch 2 taken 13305 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 13305 times.
✗ Branch 6 not taken.
13305 subpatterns_.push_back((*i)->Clone());
27 }
28 12484 }
29
30 21 PathspecElementPattern &PathspecElementPattern::operator=(
31 const PathspecElementPattern &other) {
32
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 if (this != &other) {
33 21 valid_ = other.valid_;
34 21 subpatterns_.clear();
35
1/2
✓ Branch 2 taken 21 times.
✗ Branch 3 not taken.
21 subpatterns_.reserve(other.subpatterns_.size());
36 21 SubPatterns::const_iterator i = other.subpatterns_.begin();
37 21 const SubPatterns::const_iterator iend = other.subpatterns_.end();
38
2/2
✓ Branch 1 taken 33 times.
✓ Branch 2 taken 21 times.
54 for (; i != iend; ++i) {
39
2/4
✓ Branch 2 taken 33 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 33 times.
✗ Branch 6 not taken.
33 subpatterns_.push_back((*i)->Clone());
40 }
41 }
42
43 21 return *this;
44 }
45
46
47 15704 PathspecElementPattern::~PathspecElementPattern() {
48 15704 SubPatterns::const_iterator i = subpatterns_.begin();
49 15704 const SubPatterns::const_iterator iend = subpatterns_.end();
50
2/2
✓ Branch 2 taken 16864 times.
✓ Branch 3 taken 15704 times.
32568 for (; i != iend; ++i) {
51
1/2
✓ Branch 1 taken 16864 times.
✗ Branch 2 not taken.
16864 delete *i;
52 }
53 15704 subpatterns_.clear();
54 15704 }
55
56
57 3226 void PathspecElementPattern::Parse(const std::string::const_iterator &begin,
58 const std::string::const_iterator &end) {
59 3226 std::string::const_iterator i = begin;
60
2/2
✓ Branch 1 taken 3567 times.
✓ Branch 2 taken 3226 times.
6793 while (i != end) {
61
3/4
✓ Branch 2 taken 715 times.
✓ Branch 3 taken 2852 times.
✓ Branch 5 taken 715 times.
✗ Branch 6 not taken.
3567 SubPattern *next = (Pathspec::IsSpecialChar(*i)) ? ParseSpecialChar(end, &i)
62
1/2
✓ Branch 1 taken 2852 times.
✗ Branch 2 not taken.
2852 : ParsePlaintext(end, &i);
63
3/4
✓ Branch 1 taken 3567 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 14 times.
✓ Branch 4 taken 3553 times.
3567 if (next->IsEmpty()) {
64 14 valid_ = false;
65
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 delete next;
66 } else {
67
1/2
✓ Branch 1 taken 3553 times.
✗ Branch 2 not taken.
3553 subpatterns_.push_back(next);
68 }
69 }
70 3226 }
71
72
73 2852 PathspecElementPattern::SubPattern *PathspecElementPattern::ParsePlaintext(
74 const std::string::const_iterator &end, std::string::const_iterator *i) {
75 2852 PlaintextSubPattern *pattern = new PlaintextSubPattern();
76 2852 bool next_is_escaped = false;
77
78
2/2
✓ Branch 1 taken 12221 times.
✓ Branch 2 taken 2733 times.
14954 while (*i < end) {
79
6/6
✓ Branch 2 taken 158 times.
✓ Branch 3 taken 12063 times.
✓ Branch 4 taken 119 times.
✓ Branch 5 taken 39 times.
✓ Branch 6 taken 119 times.
✓ Branch 7 taken 12102 times.
12221 if (Pathspec::IsSpecialChar(**i) && !next_is_escaped) {
80 119 break;
81 }
82
83
6/6
✓ Branch 1 taken 80 times.
✓ Branch 2 taken 12022 times.
✓ Branch 3 taken 68 times.
✓ Branch 4 taken 12 times.
✓ Branch 5 taken 68 times.
✓ Branch 6 taken 12034 times.
12102 if (**i == Pathspec::kEscaper && !next_is_escaped) {
84 68 next_is_escaped = true;
85
2/2
✓ Branch 0 taken 108 times.
✓ Branch 1 taken 11926 times.
12034 } else if (next_is_escaped) {
86
6/6
✓ Branch 2 taken 69 times.
✓ Branch 3 taken 39 times.
✓ Branch 5 taken 12 times.
✓ Branch 6 taken 57 times.
✓ Branch 7 taken 51 times.
✓ Branch 8 taken 57 times.
108 if (Pathspec::IsSpecialChar(**i) || **i == Pathspec::kEscaper) {
87 51 pattern->AddChar(**i);
88 51 next_is_escaped = false;
89 } else {
90 57 valid_ = false;
91 }
92 } else {
93
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 11926 times.
11926 assert(!Pathspec::IsSpecialChar(**i));
94 11926 pattern->AddChar(**i);
95 }
96
97 12102 ++(*i);
98 }
99
100 2852 return pattern;
101 }
102
103 715 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 715 times.
715 assert(Pathspec::IsSpecialChar(**i));
106 715 const char chr = **i;
107 715 ++(*i);
108
109
2/3
✓ Branch 0 taken 518 times.
✓ Branch 1 taken 197 times.
✗ Branch 2 not taken.
715 switch (chr) {
110 518 case Pathspec::kWildcard:
111 518 return new WildcardSubPattern();
112 197 case Pathspec::kPlaceholder:
113 197 return new PlaceholderSubPattern();
114 default:
115 assert(false && "unrecognized special character");
116 }
117 }
118
119 2192 std::string PathspecElementPattern::GenerateRegularExpression(
120 const bool is_relaxed) const {
121 2192 std::string result;
122 2192 SubPatterns::const_iterator i = subpatterns_.begin();
123 2192 const SubPatterns::const_iterator iend = subpatterns_.end();
124
2/2
✓ Branch 1 taken 2458 times.
✓ Branch 2 taken 2192 times.
4650 for (; i != iend; ++i) {
125
2/4
✓ Branch 2 taken 2458 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 2458 times.
✗ Branch 6 not taken.
2458 result += (*i)->GenerateRegularExpression(is_relaxed);
126 }
127 4384 return result;
128 }
129
130 102 std::string PathspecElementPattern::GenerateGlobString() const {
131 102 std::string result;
132 102 SubPatterns::const_iterator i = subpatterns_.begin();
133 102 const SubPatterns::const_iterator iend = subpatterns_.end();
134
2/2
✓ Branch 1 taken 120 times.
✓ Branch 2 taken 102 times.
222 for (; i != iend; ++i) {
135
2/4
✓ Branch 2 taken 120 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 120 times.
✗ Branch 6 not taken.
120 result += (*i)->GenerateGlobString();
136 }
137 204 return result;
138 }
139
140 720 bool PathspecElementPattern::operator==(
141 const PathspecElementPattern &other) const {
142 720 if (subpatterns_.size() != other.subpatterns_.size()
143
5/6
✓ Branch 0 taken 646 times.
✓ Branch 1 taken 74 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 646 times.
✓ Branch 6 taken 74 times.
✓ Branch 7 taken 646 times.
720 || IsValid() != other.IsValid()) {
144 74 return false;
145 }
146
147 646 SubPatterns::const_iterator i = subpatterns_.begin();
148 646 const SubPatterns::const_iterator iend = subpatterns_.end();
149 646 SubPatterns::const_iterator j = other.subpatterns_.begin();
150 646 const SubPatterns::const_iterator jend = other.subpatterns_.end();
151
152
5/6
✓ Branch 3 taken 688 times.
✓ Branch 4 taken 420 times.
✓ Branch 6 taken 688 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 688 times.
✓ Branch 9 taken 420 times.
1108 for (; i != iend && j != jend; ++i, ++j) {
153
3/4
✓ Branch 3 taken 688 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 226 times.
✓ Branch 6 taken 462 times.
688 if (!(*i)->Compare(*j)) {
154 226 return false;
155 }
156 }
157
158 420 return true;
159 }
160
161
162 //------------------------------------------------------------------------------
163
164
165 11977 void PathspecElementPattern::PlaintextSubPattern::AddChar(const char chr) {
166 11977 chars_.push_back(chr);
167 11977 }
168
169 std::string
170 2051 PathspecElementPattern::PlaintextSubPattern::GenerateRegularExpression(
171 const bool is_relaxed) const {
172 // Note: strict and relaxed regex are the same!
173 2051 std::string::const_iterator i = chars_.begin();
174 2051 const std::string::const_iterator iend = chars_.end();
175 2051 std::string regex;
176
2/2
✓ Branch 2 taken 9018 times.
✓ Branch 3 taken 2051 times.
11069 for (; i != iend; ++i) {
177
2/2
✓ Branch 2 taken 270 times.
✓ Branch 3 taken 8748 times.
9018 if (IsSpecialRegexCharacter(*i)) {
178
1/2
✓ Branch 1 taken 270 times.
✗ Branch 2 not taken.
270 regex += "\\";
179 }
180
1/2
✓ Branch 2 taken 9018 times.
✗ Branch 3 not taken.
9018 regex += *i;
181 }
182 4102 return regex;
183 }
184
185
186 108 std::string PathspecElementPattern::PlaintextSubPattern::GenerateGlobString()
187 const {
188 108 std::string::const_iterator i = chars_.begin();
189 108 const std::string::const_iterator iend = chars_.end();
190 108 std::string glob_string;
191
2/2
✓ Branch 2 taken 552 times.
✓ Branch 3 taken 108 times.
660 for (; i != iend; ++i) {
192
2/2
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 540 times.
552 if (Pathspec::IsSpecialChar(*i)) {
193
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
12 glob_string += "\\";
194 }
195
1/2
✓ Branch 2 taken 552 times.
✗ Branch 3 not taken.
552 glob_string += *i;
196 }
197 216 return glob_string;
198 }
199
200 9018 bool PathspecElementPattern::PlaintextSubPattern::IsSpecialRegexCharacter(
201 const char chr) const {
202
8/8
✓ Branch 0 taken 8805 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 8796 times.
✓ Branch 3 taken 9 times.
✓ Branch 4 taken 8784 times.
✓ Branch 5 taken 12 times.
✓ Branch 6 taken 8778 times.
✓ Branch 7 taken 6 times.
8811 return (chr == '.' || chr == '\\' || chr == '*' || chr == '?' || chr == '['
203
10/10
✓ Branch 0 taken 8772 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 8769 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 8766 times.
✓ Branch 5 taken 3 times.
✓ Branch 6 taken 8763 times.
✓ Branch 7 taken 3 times.
✓ Branch 8 taken 8760 times.
✓ Branch 9 taken 3 times.
8778 || chr == ']' || chr == '(' || chr == ')' || chr == '{' || chr == '}'
204
8/8
✓ Branch 0 taken 8811 times.
✓ Branch 1 taken 207 times.
✓ Branch 2 taken 8757 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 8754 times.
✓ Branch 5 taken 3 times.
✓ Branch 6 taken 6 times.
✓ Branch 7 taken 8748 times.
17829 || chr == '^' || chr == '$' || chr == '+');
205 }
206
207 555 bool PathspecElementPattern::PlaintextSubPattern::Compare(
208 const SubPattern *other) const {
209
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 552 times.
555 if (!other->IsPlaintext()) {
210 3 return false;
211 }
212
213 const PlaintextSubPattern
214
1/2
✓ Branch 0 taken 552 times.
✗ Branch 1 not taken.
552 *pt_other = dynamic_cast<const PlaintextSubPattern *>(other);
215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 552 times.
552 assert(pt_other != NULL);
216 552 return chars_ == pt_other->chars_;
217 }
218
219
220 std::string
221 333 PathspecElementPattern::WildcardSubPattern::GenerateRegularExpression(
222 const bool is_relaxed) const {
223 return (is_relaxed) ? std::string(".*")
224
14/27
✓ Branch 0 taken 145 times.
✓ Branch 1 taken 188 times.
✓ Branch 4 taken 145 times.
✗ Branch 5 not taken.
✓ Branch 8 taken 188 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 188 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 188 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 188 times.
✓ Branch 17 taken 145 times.
✗ Branch 18 not taken.
✓ Branch 19 taken 188 times.
✓ Branch 20 taken 145 times.
✓ Branch 22 taken 188 times.
✓ Branch 23 taken 145 times.
✓ Branch 25 taken 145 times.
✓ Branch 26 taken 188 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.
666 : std::string("[^") + Pathspec::kSeparator + "]*";
225 }
226
227
228 6 std::string PathspecElementPattern::WildcardSubPattern::GenerateGlobString()
229 const {
230
1/2
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
6 return "*";
231 }
232
233
234 97 bool PathspecElementPattern::WildcardSubPattern::Compare(
235 const SubPattern *other) const {
236 97 return other->IsWildcard();
237 }
238
239
240 std::string
241 74 PathspecElementPattern::PlaceholderSubPattern::GenerateRegularExpression(
242 const bool is_relaxed) const {
243 // Note: strict and relaxed regex are the same!
244
3/6
✓ Branch 2 taken 74 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 74 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 74 times.
✗ Branch 9 not taken.
148 return std::string("[^") + Pathspec::kSeparator + "]";
245 }
246
247 6 std::string PathspecElementPattern::PlaceholderSubPattern::GenerateGlobString()
248 const {
249
1/2
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
6 return "?";
250 }
251
252 36 bool PathspecElementPattern::PlaceholderSubPattern::Compare(
253 const SubPattern *other) const {
254 36 return other->IsPlaceholder();
255 }
256