GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/pathspec/pathspec_pattern.cc
Date: 2025-11-23 02:35:30
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 11139 PathspecElementPattern::PathspecElementPattern(
13 const std::string::const_iterator begin,
14 11139 const std::string::const_iterator &end)
15 11139 : valid_(true) {
16
1/2
✓ Branch 1 taken 11139 times.
✗ Branch 2 not taken.
11139 Parse(begin, end);
17 11139 }
18
19 37249 PathspecElementPattern::PathspecElementPattern(
20 37249 const PathspecElementPattern &other)
21 37249 : valid_(other.valid_) {
22
1/2
✓ Branch 2 taken 37249 times.
✗ Branch 3 not taken.
37249 subpatterns_.reserve(other.subpatterns_.size());
23 37249 SubPatterns::const_iterator i = other.subpatterns_.begin();
24 37249 const SubPatterns::const_iterator iend = other.subpatterns_.end();
25
2/2
✓ Branch 1 taken 40770 times.
✓ Branch 2 taken 37249 times.
78019 for (; i != iend; ++i) {
26
2/4
✓ Branch 2 taken 40770 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 40770 times.
✗ Branch 6 not taken.
40770 subpatterns_.push_back((*i)->Clone());
27 }
28 37249 }
29
30 147 PathspecElementPattern &PathspecElementPattern::operator=(
31 const PathspecElementPattern &other) {
32
1/2
✓ Branch 0 taken 147 times.
✗ Branch 1 not taken.
147 if (this != &other) {
33 147 valid_ = other.valid_;
34 147 subpatterns_.clear();
35
1/2
✓ Branch 2 taken 147 times.
✗ Branch 3 not taken.
147 subpatterns_.reserve(other.subpatterns_.size());
36 147 SubPatterns::const_iterator i = other.subpatterns_.begin();
37 147 const SubPatterns::const_iterator iend = other.subpatterns_.end();
38
2/2
✓ Branch 1 taken 231 times.
✓ Branch 2 taken 147 times.
378 for (; i != iend; ++i) {
39
2/4
✓ Branch 2 taken 231 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 231 times.
✗ Branch 6 not taken.
231 subpatterns_.push_back((*i)->Clone());
40 }
41 }
42
43 147 return *this;
44 }
45
46
47 48382 PathspecElementPattern::~PathspecElementPattern() {
48 48382 SubPatterns::const_iterator i = subpatterns_.begin();
49 48382 const SubPatterns::const_iterator iend = subpatterns_.end();
50
2/2
✓ Branch 2 taken 53681 times.
✓ Branch 3 taken 48382 times.
102063 for (; i != iend; ++i) {
51
1/2
✓ Branch 1 taken 53681 times.
✗ Branch 2 not taken.
53681 delete *i;
52 }
53 48382 subpatterns_.clear();
54 48382 }
55
56
57 11139 void PathspecElementPattern::Parse(const std::string::const_iterator &begin,
58 const std::string::const_iterator &end) {
59 11139 std::string::const_iterator i = begin;
60
2/2
✓ Branch 1 taken 12882 times.
✓ Branch 2 taken 11139 times.
24021 while (i != end) {
61
3/4
✓ Branch 2 taken 2934 times.
✓ Branch 3 taken 9948 times.
✓ Branch 5 taken 2934 times.
✗ Branch 6 not taken.
12882 SubPattern *next = (Pathspec::IsSpecialChar(*i)) ? ParseSpecialChar(end, &i)
62
1/2
✓ Branch 1 taken 9948 times.
✗ Branch 2 not taken.
9948 : ParsePlaintext(end, &i);
63
3/4
✓ Branch 1 taken 12882 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 49 times.
✓ Branch 4 taken 12833 times.
12882 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 12833 times.
✗ Branch 2 not taken.
12833 subpatterns_.push_back(next);
68 }
69 }
70 11139 }
71
72
73 9948 PathspecElementPattern::SubPattern *PathspecElementPattern::ParsePlaintext(
74 const std::string::const_iterator &end, std::string::const_iterator *i) {
75 9948 PlaintextSubPattern *pattern = new PlaintextSubPattern();
76 9948 bool next_is_escaped = false;
77
78
2/2
✓ Branch 1 taken 42400 times.
✓ Branch 2 taken 9297 times.
51697 while (*i < end) {
79
6/6
✓ Branch 2 taken 931 times.
✓ Branch 3 taken 41469 times.
✓ Branch 4 taken 651 times.
✓ Branch 5 taken 280 times.
✓ Branch 6 taken 651 times.
✓ Branch 7 taken 41749 times.
42400 if (Pathspec::IsSpecialChar(**i) && !next_is_escaped) {
80 651 break;
81 }
82
83
6/6
✓ Branch 1 taken 522 times.
✓ Branch 2 taken 41227 times.
✓ Branch 3 taken 436 times.
✓ Branch 4 taken 86 times.
✓ Branch 5 taken 436 times.
✓ Branch 6 taken 41313 times.
41749 if (**i == Pathspec::kEscaper && !next_is_escaped) {
84 436 next_is_escaped = true;
85
2/2
✓ Branch 0 taken 618 times.
✓ Branch 1 taken 40695 times.
41313 } else if (next_is_escaped) {
86
6/6
✓ Branch 2 taken 338 times.
✓ Branch 3 taken 280 times.
✓ Branch 5 taken 86 times.
✓ Branch 6 taken 252 times.
✓ Branch 7 taken 366 times.
✓ Branch 8 taken 252 times.
618 if (Pathspec::IsSpecialChar(**i) || **i == Pathspec::kEscaper) {
87 366 pattern->AddChar(**i);
88 366 next_is_escaped = false;
89 } else {
90 252 valid_ = false;
91 }
92 } else {
93
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 40695 times.
40695 assert(!Pathspec::IsSpecialChar(**i));
94 40695 pattern->AddChar(**i);
95 }
96
97 41749 ++(*i);
98 }
99
100 9948 return pattern;
101 }
102
103 2934 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 2934 times.
2934 assert(Pathspec::IsSpecialChar(**i));
106 2934 const char chr = **i;
107 2934 ++(*i);
108
109
2/3
✓ Branch 0 taken 1898 times.
✓ Branch 1 taken 1036 times.
✗ Branch 2 not taken.
2934 switch (chr) {
110 1898 case Pathspec::kWildcard:
111 1898 return new WildcardSubPattern();
112 1036 case Pathspec::kPlaceholder:
113 1036 return new PlaceholderSubPattern();
114 default:
115 assert(false && "unrecognized special character");
116 }
117 }
118
119 6790 std::string PathspecElementPattern::GenerateRegularExpression(
120 const bool is_relaxed) const {
121 6790 std::string result;
122 6790 SubPatterns::const_iterator i = subpatterns_.begin();
123 6790 const SubPatterns::const_iterator iend = subpatterns_.end();
124
2/2
✓ Branch 1 taken 8155 times.
✓ Branch 2 taken 6790 times.
14945 for (; i != iend; ++i) {
125
2/4
✓ Branch 2 taken 8155 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 8155 times.
✗ Branch 6 not taken.
8155 result += (*i)->GenerateRegularExpression(is_relaxed);
126 }
127 13580 return result;
128 }
129
130 714 std::string PathspecElementPattern::GenerateGlobString() const {
131 714 std::string result;
132 714 SubPatterns::const_iterator i = subpatterns_.begin();
133 714 const SubPatterns::const_iterator iend = subpatterns_.end();
134
2/2
✓ Branch 1 taken 840 times.
✓ Branch 2 taken 714 times.
1554 for (; i != iend; ++i) {
135
2/4
✓ Branch 2 taken 840 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 840 times.
✗ Branch 6 not taken.
840 result += (*i)->GenerateGlobString();
136 }
137 1428 return result;
138 }
139
140 2982 bool PathspecElementPattern::operator==(
141 const PathspecElementPattern &other) const {
142 2982 if (subpatterns_.size() != other.subpatterns_.size()
143
5/6
✓ Branch 0 taken 2660 times.
✓ Branch 1 taken 322 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 2660 times.
✓ Branch 6 taken 322 times.
✓ Branch 7 taken 2660 times.
2982 || IsValid() != other.IsValid()) {
144 322 return false;
145 }
146
147 2660 SubPatterns::const_iterator i = subpatterns_.begin();
148 2660 const SubPatterns::const_iterator iend = subpatterns_.end();
149 2660 SubPatterns::const_iterator j = other.subpatterns_.begin();
150 2660 const SubPatterns::const_iterator jend = other.subpatterns_.end();
151
152
5/6
✓ Branch 3 taken 2954 times.
✓ Branch 4 taken 1911 times.
✓ Branch 6 taken 2954 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2954 times.
✓ Branch 9 taken 1911 times.
4865 for (; i != iend && j != jend; ++i, ++j) {
153
3/4
✓ Branch 3 taken 2954 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 749 times.
✓ Branch 6 taken 2205 times.
2954 if (!(*i)->Compare(*j)) {
154 749 return false;
155 }
156 }
157
158 1911 return true;
159 }
160
161
162 //------------------------------------------------------------------------------
163
164
165 41061 void PathspecElementPattern::PlaintextSubPattern::AddChar(const char chr) {
166 41061 chars_.push_back(chr);
167 41061 }
168
169 std::string
170 6446 PathspecElementPattern::PlaintextSubPattern::GenerateRegularExpression(
171 const bool is_relaxed) const {
172 // Note: strict and relaxed regex are the same!
173 6446 std::string::const_iterator i = chars_.begin();
174 6446 const std::string::const_iterator iend = chars_.end();
175 6446 std::string regex;
176
2/2
✓ Branch 2 taken 27910 times.
✓ Branch 3 taken 6446 times.
34356 for (; i != iend; ++i) {
177
2/2
✓ Branch 2 taken 1397 times.
✓ Branch 3 taken 26513 times.
27910 if (IsSpecialRegexCharacter(*i)) {
178
1/2
✓ Branch 1 taken 1397 times.
✗ Branch 2 not taken.
1397 regex += "\\";
179 }
180
1/2
✓ Branch 2 taken 27910 times.
✗ Branch 3 not taken.
27910 regex += *i;
181 }
182 12892 return regex;
183 }
184
185
186 756 std::string PathspecElementPattern::PlaintextSubPattern::GenerateGlobString()
187 const {
188 756 std::string::const_iterator i = chars_.begin();
189 756 const std::string::const_iterator iend = chars_.end();
190 756 std::string glob_string;
191
2/2
✓ Branch 2 taken 3864 times.
✓ Branch 3 taken 756 times.
4620 for (; i != iend; ++i) {
192
2/2
✓ Branch 2 taken 84 times.
✓ Branch 3 taken 3780 times.
3864 if (Pathspec::IsSpecialChar(*i)) {
193
1/2
✓ Branch 1 taken 84 times.
✗ Branch 2 not taken.
84 glob_string += "\\";
194 }
195
1/2
✓ Branch 2 taken 3864 times.
✗ Branch 3 not taken.
3864 glob_string += *i;
196 }
197 1512 return glob_string;
198 }
199
200 27910 bool PathspecElementPattern::PlaintextSubPattern::IsSpecialRegexCharacter(
201 const char chr) const {
202
8/8
✓ Branch 0 taken 26931 times.
✓ Branch 1 taken 44 times.
✓ Branch 2 taken 26865 times.
✓ Branch 3 taken 66 times.
✓ Branch 4 taken 26777 times.
✓ Branch 5 taken 88 times.
✓ Branch 6 taken 26733 times.
✓ Branch 7 taken 44 times.
26975 return (chr == '.' || chr == '\\' || chr == '*' || chr == '?' || chr == '['
203
10/10
✓ Branch 0 taken 26689 times.
✓ Branch 1 taken 44 times.
✓ Branch 2 taken 26667 times.
✓ Branch 3 taken 22 times.
✓ Branch 4 taken 26645 times.
✓ Branch 5 taken 22 times.
✓ Branch 6 taken 26623 times.
✓ Branch 7 taken 22 times.
✓ Branch 8 taken 26601 times.
✓ Branch 9 taken 22 times.
26733 || chr == ']' || chr == '(' || chr == ')' || chr == '{' || chr == '}'
204
8/8
✓ Branch 0 taken 26975 times.
✓ Branch 1 taken 935 times.
✓ Branch 2 taken 26579 times.
✓ Branch 3 taken 22 times.
✓ Branch 4 taken 26557 times.
✓ Branch 5 taken 22 times.
✓ Branch 6 taken 44 times.
✓ Branch 7 taken 26513 times.
54885 || chr == '^' || chr == '$' || chr == '+');
205 }
206
207 2268 bool PathspecElementPattern::PlaintextSubPattern::Compare(
208 const SubPattern *other) const {
209
2/2
✓ Branch 1 taken 21 times.
✓ Branch 2 taken 2247 times.
2268 if (!other->IsPlaintext()) {
210 21 return false;
211 }
212
213 const PlaintextSubPattern
214
1/2
✓ Branch 0 taken 2247 times.
✗ Branch 1 not taken.
2247 *pt_other = dynamic_cast<const PlaintextSubPattern *>(other);
215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2247 times.
2247 assert(pt_other != NULL);
216 2247 return chars_ == pt_other->chars_;
217 }
218
219
220 std::string
221 1240 PathspecElementPattern::WildcardSubPattern::GenerateRegularExpression(
222 const bool is_relaxed) const {
223 return (is_relaxed) ? std::string(".*")
224
14/27
✓ Branch 0 taken 476 times.
✓ Branch 1 taken 764 times.
✓ Branch 4 taken 476 times.
✗ Branch 5 not taken.
✓ Branch 8 taken 764 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 764 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 764 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 764 times.
✓ Branch 17 taken 476 times.
✗ Branch 18 not taken.
✓ Branch 19 taken 764 times.
✓ Branch 20 taken 476 times.
✓ Branch 22 taken 764 times.
✓ Branch 23 taken 476 times.
✓ Branch 25 taken 476 times.
✓ Branch 26 taken 764 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.
2480 : std::string("[^") + Pathspec::kSeparator + "]*";
225 }
226
227
228 42 std::string PathspecElementPattern::WildcardSubPattern::GenerateGlobString()
229 const {
230
1/2
✓ Branch 2 taken 42 times.
✗ Branch 3 not taken.
42 return "*";
231 }
232
233
234 434 bool PathspecElementPattern::WildcardSubPattern::Compare(
235 const SubPattern *other) const {
236 434 return other->IsWildcard();
237 }
238
239
240 std::string
241 469 PathspecElementPattern::PlaceholderSubPattern::GenerateRegularExpression(
242 const bool is_relaxed) const {
243 // Note: strict and relaxed regex are the same!
244
3/6
✓ Branch 2 taken 469 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 469 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 469 times.
✗ Branch 9 not taken.
938 return std::string("[^") + Pathspec::kSeparator + "]";
245 }
246
247 42 std::string PathspecElementPattern::PlaceholderSubPattern::GenerateGlobString()
248 const {
249
1/2
✓ Branch 2 taken 42 times.
✗ Branch 3 not taken.
42 return "?";
250 }
251
252 252 bool PathspecElementPattern::PlaceholderSubPattern::Compare(
253 const SubPattern *other) const {
254 252 return other->IsPlaceholder();
255 }
256