GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/pathspec/pathspec_pattern.cc
Date: 2026-08-30 02:40:36
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 8672 PathspecElementPattern::PathspecElementPattern(
13 const std::string::const_iterator begin,
14 8672 const std::string::const_iterator &end)
15 8672 : valid_(true) {
16
1/2
✓ Branch 1 taken 8672 times.
✗ Branch 2 not taken.
8672 Parse(begin, end);
17 8672 }
18
19 34937 PathspecElementPattern::PathspecElementPattern(
20 34937 const PathspecElementPattern &other)
21 34937 : valid_(other.valid_) {
22
1/2
✓ Branch 2 taken 34937 times.
✗ Branch 3 not taken.
34937 subpatterns_.reserve(other.subpatterns_.size());
23 34937 SubPatterns::const_iterator i = other.subpatterns_.begin();
24 34937 const SubPatterns::const_iterator iend = other.subpatterns_.end();
25
2/2
✓ Branch 1 taken 37050 times.
✓ Branch 2 taken 34937 times.
71987 for (; i != iend; ++i) {
26
2/4
✓ Branch 2 taken 37050 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 37050 times.
✗ Branch 6 not taken.
37050 subpatterns_.push_back((*i)->Clone());
27 }
28 34937 }
29
30 42 PathspecElementPattern &PathspecElementPattern::operator=(
31 const PathspecElementPattern &other) {
32
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 if (this != &other) {
33 42 valid_ = other.valid_;
34 42 subpatterns_.clear();
35
1/2
✓ Branch 2 taken 42 times.
✗ Branch 3 not taken.
42 subpatterns_.reserve(other.subpatterns_.size());
36 42 SubPatterns::const_iterator i = other.subpatterns_.begin();
37 42 const SubPatterns::const_iterator iend = other.subpatterns_.end();
38
2/2
✓ Branch 1 taken 66 times.
✓ Branch 2 taken 42 times.
108 for (; i != iend; ++i) {
39
2/4
✓ Branch 2 taken 66 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 66 times.
✗ Branch 6 not taken.
66 subpatterns_.push_back((*i)->Clone());
40 }
41 }
42
43 42 return *this;
44 }
45
46
47 43603 PathspecElementPattern::~PathspecElementPattern() {
48 43603 SubPatterns::const_iterator i = subpatterns_.begin();
49 43603 const SubPatterns::const_iterator iend = subpatterns_.end();
50
2/2
✓ Branch 2 taken 46493 times.
✓ Branch 3 taken 43603 times.
90096 for (; i != iend; ++i) {
51
1/2
✓ Branch 1 taken 46493 times.
✗ Branch 2 not taken.
46493 delete *i;
52 }
53 43603 subpatterns_.clear();
54 43603 }
55
56
57 8672 void PathspecElementPattern::Parse(const std::string::const_iterator &begin,
58 const std::string::const_iterator &end) {
59 8672 std::string::const_iterator i = begin;
60
2/2
✓ Branch 1 taken 9469 times.
✓ Branch 2 taken 8672 times.
18141 while (i != end) {
61
3/4
✓ Branch 2 taken 1915 times.
✓ Branch 3 taken 7554 times.
✓ Branch 5 taken 1915 times.
✗ Branch 6 not taken.
9469 SubPattern *next = (Pathspec::IsSpecialChar(*i)) ? ParseSpecialChar(end, &i)
62
1/2
✓ Branch 1 taken 7554 times.
✗ Branch 2 not taken.
7554 : ParsePlaintext(end, &i);
63
3/4
✓ Branch 1 taken 9469 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 44 times.
✓ Branch 4 taken 9425 times.
9469 if (next->IsEmpty()) {
64 44 valid_ = false;
65
1/2
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
44 delete next;
66 } else {
67
1/2
✓ Branch 1 taken 9425 times.
✗ Branch 2 not taken.
9425 subpatterns_.push_back(next);
68 }
69 }
70 8672 }
71
72
73 7554 PathspecElementPattern::SubPattern *PathspecElementPattern::ParsePlaintext(
74 const std::string::const_iterator &end, std::string::const_iterator *i) {
75 7554 PlaintextSubPattern *pattern = new PlaintextSubPattern();
76 7554 bool next_is_escaped = false;
77
78
2/2
✓ Branch 1 taken 32421 times.
✓ Branch 2 taken 7281 times.
39702 while (*i < end) {
79
6/6
✓ Branch 2 taken 344 times.
✓ Branch 3 taken 32077 times.
✓ Branch 4 taken 273 times.
✓ Branch 5 taken 71 times.
✓ Branch 6 taken 273 times.
✓ Branch 7 taken 32148 times.
32421 if (Pathspec::IsSpecialChar(**i) && !next_is_escaped) {
80 273 break;
81 }
82
83
6/6
✓ Branch 1 taken 165 times.
✓ Branch 2 taken 31983 times.
✓ Branch 3 taken 143 times.
✓ Branch 4 taken 22 times.
✓ Branch 5 taken 143 times.
✓ Branch 6 taken 32005 times.
32148 if (**i == Pathspec::kEscaper && !next_is_escaped) {
84 143 next_is_escaped = true;
85
2/2
✓ Branch 0 taken 255 times.
✓ Branch 1 taken 31750 times.
32005 } else if (next_is_escaped) {
86
6/6
✓ Branch 2 taken 184 times.
✓ Branch 3 taken 71 times.
✓ Branch 5 taken 22 times.
✓ Branch 6 taken 162 times.
✓ Branch 7 taken 93 times.
✓ Branch 8 taken 162 times.
255 if (Pathspec::IsSpecialChar(**i) || **i == Pathspec::kEscaper) {
87 93 pattern->AddChar(**i);
88 93 next_is_escaped = false;
89 } else {
90 162 valid_ = false;
91 }
92 } else {
93
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 31750 times.
31750 assert(!Pathspec::IsSpecialChar(**i));
94 31750 pattern->AddChar(**i);
95 }
96
97 32148 ++(*i);
98 }
99
100 7554 return pattern;
101 }
102
103 1915 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 1915 times.
1915 assert(Pathspec::IsSpecialChar(**i));
106 1915 const char chr = **i;
107 1915 ++(*i);
108
109
2/3
✓ Branch 0 taken 1413 times.
✓ Branch 1 taken 502 times.
✗ Branch 2 not taken.
1915 switch (chr) {
110 1413 case Pathspec::kWildcard:
111 1413 return new WildcardSubPattern();
112 502 case Pathspec::kPlaceholder:
113 502 return new PlaceholderSubPattern();
114 default:
115 assert(false && "unrecognized special character");
116 }
117 }
118
119 5719 std::string PathspecElementPattern::GenerateRegularExpression(
120 const bool is_relaxed) const {
121 5719 std::string result;
122 5719 SubPatterns::const_iterator i = subpatterns_.begin();
123 5719 const SubPatterns::const_iterator iend = subpatterns_.end();
124
2/2
✓ Branch 1 taken 6315 times.
✓ Branch 2 taken 5719 times.
12034 for (; i != iend; ++i) {
125
2/4
✓ Branch 2 taken 6315 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 6315 times.
✗ Branch 6 not taken.
6315 result += (*i)->GenerateRegularExpression(is_relaxed);
126 }
127 11438 return result;
128 }
129
130 182 std::string PathspecElementPattern::GenerateGlobString() const {
131 182 std::string result;
132 182 SubPatterns::const_iterator i = subpatterns_.begin();
133 182 const SubPatterns::const_iterator iend = subpatterns_.end();
134
2/2
✓ Branch 1 taken 218 times.
✓ Branch 2 taken 182 times.
400 for (; i != iend; ++i) {
135
2/4
✓ Branch 2 taken 218 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 218 times.
✗ Branch 6 not taken.
218 result += (*i)->GenerateGlobString();
136 }
137 364 return result;
138 }
139
140 1912 bool PathspecElementPattern::operator==(
141 const PathspecElementPattern &other) const {
142 1912 if (subpatterns_.size() != other.subpatterns_.size()
143
5/6
✓ Branch 0 taken 1700 times.
✓ Branch 1 taken 212 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1700 times.
✓ Branch 6 taken 212 times.
✓ Branch 7 taken 1700 times.
1912 || IsValid() != other.IsValid()) {
144 212 return false;
145 }
146
147 1700 SubPatterns::const_iterator i = subpatterns_.begin();
148 1700 const SubPatterns::const_iterator iend = subpatterns_.end();
149 1700 SubPatterns::const_iterator j = other.subpatterns_.begin();
150 1700 const SubPatterns::const_iterator jend = other.subpatterns_.end();
151
152
5/6
✓ Branch 3 taken 1784 times.
✓ Branch 4 taken 1101 times.
✓ Branch 6 taken 1784 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 1784 times.
✓ Branch 9 taken 1101 times.
2885 for (; i != iend && j != jend; ++i, ++j) {
153
3/4
✓ Branch 3 taken 1784 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 599 times.
✓ Branch 6 taken 1185 times.
1784 if (!(*i)->Compare(*j)) {
154 599 return false;
155 }
156 }
157
158 1101 return true;
159 }
160
161
162 //------------------------------------------------------------------------------
163
164
165 31843 void PathspecElementPattern::PlaintextSubPattern::AddChar(const char chr) {
166 31843 chars_.push_back(chr);
167 31843 }
168
169 std::string
170 5298 PathspecElementPattern::PlaintextSubPattern::GenerateRegularExpression(
171 const bool is_relaxed) const {
172 // Note: strict and relaxed regex are the same!
173 5298 std::string::const_iterator i = chars_.begin();
174 5298 const std::string::const_iterator iend = chars_.end();
175 5298 std::string regex;
176
2/2
✓ Branch 2 taken 23615 times.
✓ Branch 3 taken 5298 times.
28913 for (; i != iend; ++i) {
177
2/2
✓ Branch 2 taken 630 times.
✓ Branch 3 taken 22985 times.
23615 if (IsSpecialRegexCharacter(*i)) {
178
1/2
✓ Branch 1 taken 630 times.
✗ Branch 2 not taken.
630 regex += "\\";
179 }
180
1/2
✓ Branch 2 taken 23615 times.
✗ Branch 3 not taken.
23615 regex += *i;
181 }
182 10596 return regex;
183 }
184
185
186 194 std::string PathspecElementPattern::PlaintextSubPattern::GenerateGlobString()
187 const {
188 194 std::string::const_iterator i = chars_.begin();
189 194 const std::string::const_iterator iend = chars_.end();
190 194 std::string glob_string;
191
2/2
✓ Branch 2 taken 980 times.
✓ Branch 3 taken 194 times.
1174 for (; i != iend; ++i) {
192
2/2
✓ Branch 2 taken 20 times.
✓ Branch 3 taken 960 times.
980 if (Pathspec::IsSpecialChar(*i)) {
193
1/2
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
20 glob_string += "\\";
194 }
195
1/2
✓ Branch 2 taken 980 times.
✗ Branch 3 not taken.
980 glob_string += *i;
196 }
197 388 return glob_string;
198 }
199
200 23615 bool PathspecElementPattern::PlaintextSubPattern::IsSpecialRegexCharacter(
201 const char chr) const {
202
8/8
✓ Branch 0 taken 23096 times.
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 23079 times.
✓ Branch 3 taken 17 times.
✓ Branch 4 taken 23057 times.
✓ Branch 5 taken 22 times.
✓ Branch 6 taken 23045 times.
✓ Branch 7 taken 12 times.
23107 return (chr == '.' || chr == '\\' || chr == '*' || chr == '?' || chr == '['
203
10/10
✓ Branch 0 taken 23033 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 23027 times.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 23021 times.
✓ Branch 5 taken 6 times.
✓ Branch 6 taken 23015 times.
✓ Branch 7 taken 6 times.
✓ Branch 8 taken 23009 times.
✓ Branch 9 taken 6 times.
23045 || chr == ']' || chr == '(' || chr == ')' || chr == '{' || chr == '}'
204
8/8
✓ Branch 0 taken 23107 times.
✓ Branch 1 taken 508 times.
✓ Branch 2 taken 23003 times.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 22997 times.
✓ Branch 5 taken 6 times.
✓ Branch 6 taken 12 times.
✓ Branch 7 taken 22985 times.
46722 || chr == '^' || chr == '$' || chr == '+');
205 }
206
207 1438 bool PathspecElementPattern::PlaintextSubPattern::Compare(
208 const SubPattern *other) const {
209
2/2
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 1432 times.
1438 if (!other->IsPlaintext()) {
210 6 return false;
211 }
212
213 const PlaintextSubPattern
214
1/2
✓ Branch 0 taken 1432 times.
✗ Branch 1 not taken.
1432 *pt_other = dynamic_cast<const PlaintextSubPattern *>(other);
215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1432 times.
1432 assert(pt_other != NULL);
216 1432 return chars_ == pt_other->chars_;
217 }
218
219
220 std::string
221 861 PathspecElementPattern::WildcardSubPattern::GenerateRegularExpression(
222 const bool is_relaxed) const {
223 return (is_relaxed) ? std::string(".*")
224
14/27
✓ Branch 0 taken 387 times.
✓ Branch 1 taken 474 times.
✓ Branch 4 taken 387 times.
✗ Branch 5 not taken.
✓ Branch 8 taken 474 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 474 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 474 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 474 times.
✓ Branch 17 taken 387 times.
✗ Branch 18 not taken.
✓ Branch 19 taken 474 times.
✓ Branch 20 taken 387 times.
✓ Branch 22 taken 474 times.
✓ Branch 23 taken 387 times.
✓ Branch 25 taken 387 times.
✓ Branch 26 taken 474 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.
1722 : std::string("[^") + Pathspec::kSeparator + "]*";
225 }
226
227
228 12 std::string PathspecElementPattern::WildcardSubPattern::GenerateGlobString()
229 const {
230
1/2
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
12 return "*";
231 }
232
233
234 274 bool PathspecElementPattern::WildcardSubPattern::Compare(
235 const SubPattern *other) const {
236 274 return other->IsWildcard();
237 }
238
239
240 std::string
241 156 PathspecElementPattern::PlaceholderSubPattern::GenerateRegularExpression(
242 const bool is_relaxed) const {
243 // Note: strict and relaxed regex are the same!
244
3/6
✓ Branch 2 taken 156 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 156 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 156 times.
✗ Branch 9 not taken.
312 return std::string("[^") + Pathspec::kSeparator + "]";
245 }
246
247 12 std::string PathspecElementPattern::PlaceholderSubPattern::GenerateGlobString()
248 const {
249
1/2
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
12 return "?";
250 }
251
252 72 bool PathspecElementPattern::PlaceholderSubPattern::Compare(
253 const SubPattern *other) const {
254 72 return other->IsPlaceholder();
255 }
256