GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/pathspec/pathspec_pattern.cc
Date: 2026-08-23 02:40:52
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 14673 PathspecElementPattern::PathspecElementPattern(
13 const std::string::const_iterator begin,
14 14673 const std::string::const_iterator &end)
15 14673 : valid_(true) {
16
1/2
✓ Branch 1 taken 14673 times.
✗ Branch 2 not taken.
14673 Parse(begin, end);
17 14673 }
18
19 51673 PathspecElementPattern::PathspecElementPattern(
20 51673 const PathspecElementPattern &other)
21 51673 : valid_(other.valid_) {
22
1/2
✓ Branch 2 taken 51673 times.
✗ Branch 3 not taken.
51673 subpatterns_.reserve(other.subpatterns_.size());
23 51673 SubPatterns::const_iterator i = other.subpatterns_.begin();
24 51673 const SubPatterns::const_iterator iend = other.subpatterns_.end();
25
2/2
✓ Branch 1 taken 55335 times.
✓ Branch 2 taken 51673 times.
107008 for (; i != iend; ++i) {
26
2/4
✓ Branch 2 taken 55335 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 55335 times.
✗ Branch 6 not taken.
55335 subpatterns_.push_back((*i)->Clone());
27 }
28 51673 }
29
30 161 PathspecElementPattern &PathspecElementPattern::operator=(
31 const PathspecElementPattern &other) {
32
1/2
✓ Branch 0 taken 161 times.
✗ Branch 1 not taken.
161 if (this != &other) {
33 161 valid_ = other.valid_;
34 161 subpatterns_.clear();
35
1/2
✓ Branch 2 taken 161 times.
✗ Branch 3 not taken.
161 subpatterns_.reserve(other.subpatterns_.size());
36 161 SubPatterns::const_iterator i = other.subpatterns_.begin();
37 161 const SubPatterns::const_iterator iend = other.subpatterns_.end();
38
2/2
✓ Branch 1 taken 253 times.
✓ Branch 2 taken 161 times.
414 for (; i != iend; ++i) {
39
2/4
✓ Branch 2 taken 253 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 253 times.
✗ Branch 6 not taken.
253 subpatterns_.push_back((*i)->Clone());
40 }
41 }
42
43 161 return *this;
44 }
45
46
47 66340 PathspecElementPattern::~PathspecElementPattern() {
48 66340 SubPatterns::const_iterator i = subpatterns_.begin();
49 66340 const SubPatterns::const_iterator iend = subpatterns_.end();
50
2/2
✓ Branch 2 taken 71929 times.
✓ Branch 3 taken 66340 times.
138269 for (; i != iend; ++i) {
51
1/2
✓ Branch 1 taken 71929 times.
✗ Branch 2 not taken.
71929 delete *i;
52 }
53 66340 subpatterns_.clear();
54 66340 }
55
56
57 14673 void PathspecElementPattern::Parse(const std::string::const_iterator &begin,
58 const std::string::const_iterator &end) {
59 14673 std::string::const_iterator i = begin;
60
2/2
✓ Branch 1 taken 16557 times.
✓ Branch 2 taken 14673 times.
31230 while (i != end) {
61
3/4
✓ Branch 2 taken 3124 times.
✓ Branch 3 taken 13433 times.
✓ Branch 5 taken 3124 times.
✗ Branch 6 not taken.
16557 SubPattern *next = (Pathspec::IsSpecialChar(*i)) ? ParseSpecialChar(end, &i)
62
1/2
✓ Branch 1 taken 13433 times.
✗ Branch 2 not taken.
13433 : ParsePlaintext(end, &i);
63
3/4
✓ Branch 1 taken 16557 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 49 times.
✓ Branch 4 taken 16508 times.
16557 if (next->IsEmpty()) {
64 49 valid_ = false;
65
1/2
✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
49 delete next;
66 } else {
67
1/2
✓ Branch 1 taken 16508 times.
✗ Branch 2 not taken.
16508 subpatterns_.push_back(next);
68 }
69 }
70 14673 }
71
72
73 13433 PathspecElementPattern::SubPattern *PathspecElementPattern::ParsePlaintext(
74 const std::string::const_iterator &end, std::string::const_iterator *i) {
75 13433 PlaintextSubPattern *pattern = new PlaintextSubPattern();
76 13433 bool next_is_escaped = false;
77
78
2/2
✓ Branch 1 taken 62097 times.
✓ Branch 2 taken 12717 times.
74814 while (*i < end) {
79
6/6
✓ Branch 2 taken 1024 times.
✓ Branch 3 taken 61073 times.
✓ Branch 4 taken 716 times.
✓ Branch 5 taken 308 times.
✓ Branch 6 taken 716 times.
✓ Branch 7 taken 61381 times.
62097 if (Pathspec::IsSpecialChar(**i) && !next_is_escaped) {
80 716 break;
81 }
82
83
6/6
✓ Branch 1 taken 571 times.
✓ Branch 2 taken 60810 times.
✓ Branch 3 taken 476 times.
✓ Branch 4 taken 95 times.
✓ Branch 5 taken 476 times.
✓ Branch 6 taken 60905 times.
61381 if (**i == Pathspec::kEscaper && !next_is_escaped) {
84 476 next_is_escaped = true;
85
2/2
✓ Branch 0 taken 670 times.
✓ Branch 1 taken 60235 times.
60905 } else if (next_is_escaped) {
86
6/6
✓ Branch 2 taken 362 times.
✓ Branch 3 taken 308 times.
✓ Branch 5 taken 95 times.
✓ Branch 6 taken 267 times.
✓ Branch 7 taken 403 times.
✓ Branch 8 taken 267 times.
670 if (Pathspec::IsSpecialChar(**i) || **i == Pathspec::kEscaper) {
87 403 pattern->AddChar(**i);
88 403 next_is_escaped = false;
89 } else {
90 267 valid_ = false;
91 }
92 } else {
93
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 60235 times.
60235 assert(!Pathspec::IsSpecialChar(**i));
94 60235 pattern->AddChar(**i);
95 }
96
97 61381 ++(*i);
98 }
99
100 13433 return pattern;
101 }
102
103 3124 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 3124 times.
3124 assert(Pathspec::IsSpecialChar(**i));
106 3124 const char chr = **i;
107 3124 ++(*i);
108
109
2/3
✓ Branch 0 taken 2001 times.
✓ Branch 1 taken 1123 times.
✗ Branch 2 not taken.
3124 switch (chr) {
110 2001 case Pathspec::kWildcard:
111 2001 return new WildcardSubPattern();
112 1123 case Pathspec::kPlaceholder:
113 1123 return new PlaceholderSubPattern();
114 default:
115 assert(false && "unrecognized special character");
116 }
117 }
118
119 9463 std::string PathspecElementPattern::GenerateRegularExpression(
120 const bool is_relaxed) const {
121 9463 std::string result;
122 9463 SubPatterns::const_iterator i = subpatterns_.begin();
123 9463 const SubPatterns::const_iterator iend = subpatterns_.end();
124
2/2
✓ Branch 1 taken 10944 times.
✓ Branch 2 taken 9463 times.
20407 for (; i != iend; ++i) {
125
2/4
✓ Branch 2 taken 10944 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 10944 times.
✗ Branch 6 not taken.
10944 result += (*i)->GenerateRegularExpression(is_relaxed);
126 }
127 18926 return result;
128 }
129
130 786 std::string PathspecElementPattern::GenerateGlobString() const {
131 786 std::string result;
132 786 SubPatterns::const_iterator i = subpatterns_.begin();
133 786 const SubPatterns::const_iterator iend = subpatterns_.end();
134
2/2
✓ Branch 1 taken 927 times.
✓ Branch 2 taken 786 times.
1713 for (; i != iend; ++i) {
135
2/4
✓ Branch 2 taken 927 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 927 times.
✗ Branch 6 not taken.
927 result += (*i)->GenerateGlobString();
136 }
137 1572 return result;
138 }
139
140 3022 bool PathspecElementPattern::operator==(
141 const PathspecElementPattern &other) const {
142 3022 if (subpatterns_.size() != other.subpatterns_.size()
143
5/6
✓ Branch 0 taken 2688 times.
✓ Branch 1 taken 334 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 2688 times.
✓ Branch 6 taken 334 times.
✓ Branch 7 taken 2688 times.
3022 || IsValid() != other.IsValid()) {
144 334 return false;
145 }
146
147 2688 SubPatterns::const_iterator i = subpatterns_.begin();
148 2688 const SubPatterns::const_iterator iend = subpatterns_.end();
149 2688 SubPatterns::const_iterator j = other.subpatterns_.begin();
150 2688 const SubPatterns::const_iterator jend = other.subpatterns_.end();
151
152
5/6
✓ Branch 3 taken 3010 times.
✓ Branch 4 taken 1977 times.
✓ Branch 6 taken 3010 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 3010 times.
✓ Branch 9 taken 1977 times.
4987 for (; i != iend && j != jend; ++i, ++j) {
153
3/4
✓ Branch 3 taken 3010 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 711 times.
✓ Branch 6 taken 2299 times.
3010 if (!(*i)->Compare(*j)) {
154 711 return false;
155 }
156 }
157
158 1977 return true;
159 }
160
161
162 //------------------------------------------------------------------------------
163
164
165 60638 void PathspecElementPattern::PlaintextSubPattern::AddChar(const char chr) {
166 60638 chars_.push_back(chr);
167 60638 }
168
169 std::string
170 9070 PathspecElementPattern::PlaintextSubPattern::GenerateRegularExpression(
171 const bool is_relaxed) const {
172 // Note: strict and relaxed regex are the same!
173 9070 std::string::const_iterator i = chars_.begin();
174 9070 const std::string::const_iterator iend = chars_.end();
175 9070 std::string regex;
176
2/2
✓ Branch 2 taken 44055 times.
✓ Branch 3 taken 9070 times.
53125 for (; i != iend; ++i) {
177
2/2
✓ Branch 2 taken 1488 times.
✓ Branch 3 taken 42567 times.
44055 if (IsSpecialRegexCharacter(*i)) {
178
1/2
✓ Branch 1 taken 1488 times.
✗ Branch 2 not taken.
1488 regex += "\\";
179 }
180
1/2
✓ Branch 2 taken 44055 times.
✗ Branch 3 not taken.
44055 regex += *i;
181 }
182 18140 return regex;
183 }
184
185
186 833 std::string PathspecElementPattern::PlaintextSubPattern::GenerateGlobString()
187 const {
188 833 std::string::const_iterator i = chars_.begin();
189 833 const std::string::const_iterator iend = chars_.end();
190 833 std::string glob_string;
191
2/2
✓ Branch 2 taken 4252 times.
✓ Branch 3 taken 833 times.
5085 for (; i != iend; ++i) {
192
2/2
✓ Branch 2 taken 92 times.
✓ Branch 3 taken 4160 times.
4252 if (Pathspec::IsSpecialChar(*i)) {
193
1/2
✓ Branch 1 taken 92 times.
✗ Branch 2 not taken.
92 glob_string += "\\";
194 }
195
1/2
✓ Branch 2 taken 4252 times.
✗ Branch 3 not taken.
4252 glob_string += *i;
196 }
197 1666 return glob_string;
198 }
199
200 44055 bool PathspecElementPattern::PlaintextSubPattern::IsSpecialRegexCharacter(
201 const char chr) const {
202
8/8
✓ Branch 0 taken 43023 times.
✓ Branch 1 taken 48 times.
✓ Branch 2 taken 42951 times.
✓ Branch 3 taken 72 times.
✓ Branch 4 taken 42855 times.
✓ Branch 5 taken 96 times.
✓ Branch 6 taken 42807 times.
✓ Branch 7 taken 48 times.
43071 return (chr == '.' || chr == '\\' || chr == '*' || chr == '?' || chr == '['
203
10/10
✓ Branch 0 taken 42759 times.
✓ Branch 1 taken 48 times.
✓ Branch 2 taken 42735 times.
✓ Branch 3 taken 24 times.
✓ Branch 4 taken 42711 times.
✓ Branch 5 taken 24 times.
✓ Branch 6 taken 42687 times.
✓ Branch 7 taken 24 times.
✓ Branch 8 taken 42663 times.
✓ Branch 9 taken 24 times.
42807 || chr == ']' || chr == '(' || chr == ')' || chr == '{' || chr == '}'
204
8/8
✓ Branch 0 taken 43071 times.
✓ Branch 1 taken 984 times.
✓ Branch 2 taken 42639 times.
✓ Branch 3 taken 24 times.
✓ Branch 4 taken 42615 times.
✓ Branch 5 taken 24 times.
✓ Branch 6 taken 48 times.
✓ Branch 7 taken 42567 times.
87126 || chr == '^' || chr == '$' || chr == '+');
205 }
206
207 2282 bool PathspecElementPattern::PlaintextSubPattern::Compare(
208 const SubPattern *other) const {
209
2/2
✓ Branch 1 taken 23 times.
✓ Branch 2 taken 2259 times.
2282 if (!other->IsPlaintext()) {
210 23 return false;
211 }
212
213 const PlaintextSubPattern
214
1/2
✓ Branch 0 taken 2259 times.
✗ Branch 1 not taken.
2259 *pt_other = dynamic_cast<const PlaintextSubPattern *>(other);
215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2259 times.
2259 assert(pt_other != NULL);
216 2259 return chars_ == pt_other->chars_;
217 }
218
219
220 std::string
221 1348 PathspecElementPattern::WildcardSubPattern::GenerateRegularExpression(
222 const bool is_relaxed) const {
223 return (is_relaxed) ? std::string(".*")
224
14/27
✓ Branch 0 taken 457 times.
✓ Branch 1 taken 891 times.
✓ Branch 4 taken 457 times.
✗ Branch 5 not taken.
✓ Branch 8 taken 891 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 891 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 891 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 891 times.
✓ Branch 17 taken 457 times.
✗ Branch 18 not taken.
✓ Branch 19 taken 891 times.
✓ Branch 20 taken 457 times.
✓ Branch 22 taken 891 times.
✓ Branch 23 taken 457 times.
✓ Branch 25 taken 457 times.
✓ Branch 26 taken 891 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.
2696 : std::string("[^") + Pathspec::kSeparator + "]*";
225 }
226
227
228 47 std::string PathspecElementPattern::WildcardSubPattern::GenerateGlobString()
229 const {
230
1/2
✓ Branch 2 taken 47 times.
✗ Branch 3 not taken.
47 return "*";
231 }
232
233
234 452 bool PathspecElementPattern::WildcardSubPattern::Compare(
235 const SubPattern *other) const {
236 452 return other->IsWildcard();
237 }
238
239
240 std::string
241 526 PathspecElementPattern::PlaceholderSubPattern::GenerateRegularExpression(
242 const bool is_relaxed) const {
243 // Note: strict and relaxed regex are the same!
244
3/6
✓ Branch 2 taken 526 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 526 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 526 times.
✗ Branch 9 not taken.
1052 return std::string("[^") + Pathspec::kSeparator + "]";
245 }
246
247 47 std::string PathspecElementPattern::PlaceholderSubPattern::GenerateGlobString()
248 const {
249
1/2
✓ Branch 2 taken 47 times.
✗ Branch 3 not taken.
47 return "?";
250 }
251
252 276 bool PathspecElementPattern::PlaceholderSubPattern::Compare(
253 const SubPattern *other) const {
254 276 return other->IsPlaceholder();
255 }
256