GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/pathspec/pathspec.cc
Date: 2026-06-21 02:37:04
Exec Total Coverage
Lines: 198 207 95.7%
Branches: 120 172 69.8%

Line Branch Exec Source
1 /**
2 * This file is part of the CernVM File System.
3 */
4
5 #include "pathspec.h"
6
7 #include <cassert>
8
9 #include "util/logging.h"
10 #include "util/smalloc.h"
11
12 2979 Pathspec::Pathspec(const std::string &spec)
13 2979 : regex_(NULL)
14 2979 , relaxed_regex_(NULL)
15 2979 , prefix_regex_(NULL)
16 2979 , regex_compiled_(false)
17 2979 , relaxed_regex_compiled_(false)
18 2979 , prefix_regex_compiled_(false)
19 2979 , glob_string_compiled_(false)
20 2979 , glob_string_sequence_compiled_(false)
21 2979 , valid_(true)
22 2979 , absolute_(false) {
23
1/2
✓ Branch 1 taken 2979 times.
✗ Branch 2 not taken.
2979 Parse(spec);
24
2/2
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 2964 times.
2979 if (patterns_.size() == 0) {
25 15 valid_ = false;
26 }
27
28 2979 ElementPatterns::const_iterator i = patterns_.begin();
29 2979 const ElementPatterns::const_iterator iend = patterns_.end();
30
2/2
✓ Branch 2 taken 6684 times.
✓ Branch 3 taken 2979 times.
9663 for (; i != iend; ++i) {
31
2/2
✓ Branch 2 taken 41 times.
✓ Branch 3 taken 6643 times.
6684 if (!i->IsValid()) {
32 41 valid_ = false;
33 }
34 }
35 2979 }
36
37 // Compiled regex structure cannot be duplicated and needs to be re-compiled
38 // Note: the copy-constructed object will perform a lazy evaluation again
39 4040 Pathspec::Pathspec(const Pathspec &other)
40 4040 : patterns_(other.patterns_)
41 4040 , regex_(NULL)
42 4040 , relaxed_regex_(NULL)
43 4040 , prefix_regex_(NULL)
44
1/2
✓ Branch 1 taken 4040 times.
✗ Branch 2 not taken.
4040 , glob_string_(other.glob_string_)
45
1/2
✓ Branch 1 taken 4040 times.
✗ Branch 2 not taken.
4040 , glob_string_sequence_(other.glob_string_sequence_)
46 4040 , regex_compiled_(false)
47 4040 , relaxed_regex_compiled_(false)
48 4040 , prefix_regex_compiled_(false)
49 4040 , glob_string_compiled_(other.glob_string_compiled_)
50 4040 , glob_string_sequence_compiled_(other.glob_string_sequence_compiled_)
51 4040 , valid_(other.valid_)
52 4040 , absolute_(other.absolute_) { }
53
54 7016 Pathspec::~Pathspec() { DestroyRegularExpressions(); }
55
56 45 Pathspec &Pathspec::operator=(const Pathspec &other) {
57
1/2
✓ Branch 0 taken 45 times.
✗ Branch 1 not taken.
45 if (this != &other) {
58 45 DestroyRegularExpressions(); // see: copy c'tor for details
59 45 patterns_ = other.patterns_;
60
61 45 glob_string_compiled_ = other.glob_string_compiled_;
62 45 glob_string_ = other.glob_string_;
63
64 45 glob_string_sequence_compiled_ = other.glob_string_sequence_compiled_;
65 45 glob_string_sequence_ = other.glob_string_sequence_;
66
67 45 valid_ = other.valid_;
68 45 absolute_ = other.absolute_;
69 }
70
71 45 return *this;
72 }
73
74
75 2979 void Pathspec::Parse(const std::string &spec) {
76 // parsing is done using std::string iterators to walk through the entire
77 // pathspec parameter. Thus, all parsing methods receive references to these
78 // iterators and increment itr as they pass along.
79 2979 std::string::const_iterator itr = spec.begin();
80 2979 const std::string::const_iterator end = spec.end();
81
82 2979 absolute_ = (*itr == kSeparator);
83
2/2
✓ Branch 1 taken 12790 times.
✓ Branch 2 taken 2979 times.
15769 while (itr != end) {
84
2/2
✓ Branch 1 taken 6106 times.
✓ Branch 2 taken 6684 times.
12790 if (*itr == kSeparator) {
85 6106 ++itr;
86 6106 continue;
87 }
88
1/2
✓ Branch 1 taken 6684 times.
✗ Branch 2 not taken.
6684 ParsePathElement(end, &itr);
89 }
90 2979 }
91
92 6684 void Pathspec::ParsePathElement(const std::string::const_iterator &end,
93 std::string::const_iterator *itr) {
94 // find the end of the current pattern element (next directory boundary)
95 6684 const std::string::const_iterator begin_element = *itr;
96
6/6
✓ Branch 1 taken 31122 times.
✓ Branch 2 taken 2835 times.
✓ Branch 4 taken 27273 times.
✓ Branch 5 taken 3849 times.
✓ Branch 6 taken 27273 times.
✓ Branch 7 taken 6684 times.
33957 while (*itr != end && **itr != kSeparator) {
97 27273 ++(*itr);
98 }
99 6684 const std::string::const_iterator end_element = *itr;
100
101 // create a PathspecElementPattern out of this directory description
102
2/4
✓ Branch 1 taken 6684 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 6684 times.
✗ Branch 5 not taken.
6684 patterns_.push_back(PathspecElementPattern(begin_element, end_element));
103 6684 }
104
105 7127 bool Pathspec::IsMatching(const std::string &query_path) const {
106
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7127 times.
7127 assert(IsValid());
107
108
2/2
✓ Branch 1 taken 307 times.
✓ Branch 2 taken 6820 times.
7127 if (query_path.empty()) {
109 307 return false;
110 }
111
112 6820 const bool query_is_absolute = (query_path[0] == kSeparator);
113
2/2
✓ Branch 1 taken 5248 times.
✓ Branch 2 taken 194 times.
5442 return (!query_is_absolute || this->IsAbsolute())
114
4/4
✓ Branch 0 taken 5442 times.
✓ Branch 1 taken 1378 times.
✓ Branch 3 taken 2561 times.
✓ Branch 4 taken 4065 times.
12262 && IsPathspecMatching(query_path);
115 }
116
117 180 bool Pathspec::IsPrefixMatching(const std::string &query_path) const {
118
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 180 times.
180 assert(IsValid());
119
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 180 times.
180 assert(IsAbsolute());
120
121
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 180 times.
180 if (query_path.empty()) {
122 return false;
123 }
124
125 180 const bool query_is_absolute = (query_path[0] == kSeparator);
126
4/4
✓ Branch 0 taken 165 times.
✓ Branch 1 taken 15 times.
✓ Branch 3 taken 105 times.
✓ Branch 4 taken 60 times.
180 return (query_is_absolute && IsPathspecPrefixMatching(query_path));
127 }
128
129 6595 bool Pathspec::IsMatchingRelaxed(const std::string &query_path) const {
130
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6595 times.
6595 assert(IsValid());
131
132
2/2
✓ Branch 1 taken 684 times.
✓ Branch 2 taken 5911 times.
6595 if (query_path.empty()) {
133 684 return false;
134 }
135
136 5911 return IsPathspecMatchingRelaxed(query_path);
137 }
138
139 6626 bool Pathspec::IsPathspecMatching(const std::string &query_path) const {
140 6626 return ApplyRegularExpression(query_path, GetRegularExpression());
141 }
142
143 165 bool Pathspec::IsPathspecPrefixMatching(const std::string &query_path) const {
144 165 return ApplyRegularExpression(query_path, GetPrefixRegularExpression());
145 }
146
147 5911 bool Pathspec::IsPathspecMatchingRelaxed(const std::string &query_path) const {
148 5911 return ApplyRegularExpression(query_path, GetRelaxedRegularExpression());
149 }
150
151 12702 bool Pathspec::ApplyRegularExpression(const std::string &query_path,
152 regex_t *regex) const {
153 12702 const char *path = query_path.c_str();
154 12702 const int retval = regexec(regex, path, 0, NULL, 0);
155
156
3/4
✓ Branch 0 taken 8993 times.
✓ Branch 1 taken 3709 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 8993 times.
12702 if (retval != 0 && retval != REG_NOMATCH) {
157 PrintRegularExpressionError(retval);
158 }
159
160 12702 return (retval == 0);
161 }
162
163 6626 regex_t *Pathspec::GetRegularExpression() const {
164
2/2
✓ Branch 0 taken 1273 times.
✓ Branch 1 taken 5353 times.
6626 if (!regex_compiled_) {
165 1273 const bool is_relaxed = false;
166
1/2
✓ Branch 1 taken 1273 times.
✗ Branch 2 not taken.
1273 const std::string regex = GenerateRegularExpression(is_relaxed);
167
1/2
✓ Branch 2 taken 1273 times.
✗ Branch 3 not taken.
1273 LogCvmfs(kLogPathspec, kLogDebug, "compiled regex: %s", regex.c_str());
168
169
1/2
✓ Branch 1 taken 1273 times.
✗ Branch 2 not taken.
1273 regex_ = CompileRegularExpression(regex);
170 1273 regex_compiled_ = true;
171 1273 }
172
173 6626 return regex_;
174 }
175
176 165 regex_t *Pathspec::GetPrefixRegularExpression() const {
177
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 135 times.
165 if (!prefix_regex_compiled_) {
178 30 const bool is_relaxed = false;
179 30 const bool is_prefix = true;
180
1/2
✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
30 const std::string regex = GenerateRegularExpression(is_relaxed, is_prefix);
181
1/2
✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
30 LogCvmfs(kLogPathspec, kLogDebug, "compiled regex: %s", regex.c_str());
182
183
1/2
✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
30 prefix_regex_ = CompileRegularExpression(regex);
184 30 prefix_regex_compiled_ = true;
185 30 }
186
187 165 return prefix_regex_;
188 }
189
190 5911 regex_t *Pathspec::GetRelaxedRegularExpression() const {
191
2/2
✓ Branch 0 taken 553 times.
✓ Branch 1 taken 5358 times.
5911 if (!relaxed_regex_compiled_) {
192 553 const bool is_relaxed = true;
193
1/2
✓ Branch 1 taken 553 times.
✗ Branch 2 not taken.
553 const std::string regex = GenerateRegularExpression(is_relaxed);
194
1/2
✓ Branch 2 taken 553 times.
✗ Branch 3 not taken.
553 LogCvmfs(kLogPathspec, kLogDebug, "compiled relaxed regex: %s",
195 regex.c_str());
196
197
1/2
✓ Branch 1 taken 553 times.
✗ Branch 2 not taken.
553 relaxed_regex_ = CompileRegularExpression(regex);
198 553 relaxed_regex_compiled_ = true;
199 553 }
200
201 5911 return relaxed_regex_;
202 }
203
204 1856 std::string Pathspec::GenerateRegularExpression(const bool is_relaxed,
205 const bool is_prefix) const {
206 // start matching at the first character
207
1/2
✓ Branch 2 taken 1856 times.
✗ Branch 3 not taken.
1856 std::string regex = "^";
208
209 // absolute paths require a / in the beginning
210
2/2
✓ Branch 1 taken 1541 times.
✓ Branch 2 taken 315 times.
1856 if (IsAbsolute()) {
211
1/2
✓ Branch 1 taken 1541 times.
✗ Branch 2 not taken.
1541 regex += kSeparator;
212 }
213
214 // concatenate the regular expressions of the compiled path elements
215 1856 ElementPatterns::const_iterator i = patterns_.begin();
216 1856 const ElementPatterns::const_iterator iend = patterns_.end();
217
2/2
✓ Branch 2 taken 4066 times.
✓ Branch 3 taken 1856 times.
5922 for (; i != iend; ++i) {
218
2/4
✓ Branch 2 taken 4066 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 4066 times.
✗ Branch 6 not taken.
4066 regex += i->GenerateRegularExpression(is_relaxed);
219
2/2
✓ Branch 2 taken 2210 times.
✓ Branch 3 taken 1856 times.
4066 if (i + 1 != iend) {
220
1/2
✓ Branch 1 taken 2210 times.
✗ Branch 2 not taken.
2210 regex += kSeparator;
221 }
222 }
223
224
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 1826 times.
1856 if (is_prefix) {
225
1/2
✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
30 regex += "($|";
226
1/2
✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
30 regex += kSeparator;
227
1/2
✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
30 regex += ".*$)";
228 } else {
229 // a path might end with a trailing slash
230 // (pathspec does not distinguish files and directories)
231
1/2
✓ Branch 1 taken 1826 times.
✗ Branch 2 not taken.
1826 regex += kSeparator;
232
1/2
✓ Branch 1 taken 1826 times.
✗ Branch 2 not taken.
1826 regex += "?$";
233 }
234
235 3712 return regex;
236 }
237
238 1856 regex_t *Pathspec::CompileRegularExpression(const std::string &regex) const {
239 1856 regex_t *result = reinterpret_cast<regex_t *>(smalloc(sizeof(regex_t)));
240 1856 const int flags = REG_NOSUB | REG_NEWLINE | REG_EXTENDED;
241 1856 const int retval = regcomp(result, regex.c_str(), flags);
242
243
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1856 times.
1856 if (retval != 0) {
244 PrintRegularExpressionError(retval);
245 assert(false && "failed to compile regex");
246 }
247
248 1856 return result;
249 }
250
251 7061 void Pathspec::DestroyRegularExpressions() {
252
2/2
✓ Branch 0 taken 1273 times.
✓ Branch 1 taken 5788 times.
7061 if (regex_compiled_) {
253
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1273 times.
1273 assert(regex_ != NULL);
254 1273 regfree(regex_);
255 1273 regex_ = NULL;
256 1273 regex_compiled_ = false;
257 }
258
259
2/2
✓ Branch 0 taken 553 times.
✓ Branch 1 taken 6508 times.
7061 if (relaxed_regex_compiled_) {
260
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 553 times.
553 assert(relaxed_regex_ != NULL);
261 553 regfree(relaxed_regex_);
262 553 relaxed_regex_ = NULL;
263 553 relaxed_regex_compiled_ = false;
264 }
265 7061 }
266
267 1752 bool Pathspec::operator==(const Pathspec &other) const {
268
1/2
✓ Branch 4 taken 999 times.
✗ Branch 5 not taken.
2751 if (patterns_.size() != other.patterns_.size() || IsValid() != other.IsValid()
269
6/6
✓ Branch 0 taken 999 times.
✓ Branch 1 taken 753 times.
✓ Branch 4 taken 112 times.
✓ Branch 5 taken 887 times.
✓ Branch 6 taken 865 times.
✓ Branch 7 taken 887 times.
2751 || IsAbsolute() != other.IsAbsolute()) {
270 865 return false;
271 }
272
273 887 ElementPatterns::const_iterator i = patterns_.begin();
274 887 const ElementPatterns::const_iterator iend = patterns_.end();
275 887 ElementPatterns::const_iterator j = other.patterns_.begin();
276 887 const ElementPatterns::const_iterator jend = other.patterns_.end();
277
278
5/6
✓ Branch 3 taken 1848 times.
✓ Branch 4 taken 251 times.
✓ Branch 6 taken 1848 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 1848 times.
✓ Branch 9 taken 251 times.
2099 for (; i != iend && j != jend; ++i, ++j) {
279
3/4
✓ Branch 3 taken 1848 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 636 times.
✓ Branch 6 taken 1212 times.
1848 if (*i != *j) {
280 636 return false;
281 }
282 }
283
284 251 return true;
285 }
286
287 void Pathspec::PrintRegularExpressionError(const int error_code) const {
288 assert(regex_compiled_);
289 const size_t errbuf_size = 1024;
290 char error[errbuf_size];
291 regerror(error_code, regex_, error, errbuf_size);
292 LogCvmfs(kLogPathspec, kLogStderr, "RegEx Error: %d - %s", error_code, error);
293 }
294
295 253 const Pathspec::GlobStringSequence &Pathspec::GetGlobStringSequence() const {
296
1/2
✓ Branch 0 taken 253 times.
✗ Branch 1 not taken.
253 if (!glob_string_sequence_compiled_) {
297 253 GenerateGlobStringSequence();
298 253 glob_string_sequence_compiled_ = true;
299 }
300 253 return glob_string_sequence_;
301 }
302
303
304 253 void Pathspec::GenerateGlobStringSequence() const {
305
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 253 times.
253 assert(glob_string_sequence_.empty());
306 253 ElementPatterns::const_iterator i = patterns_.begin();
307 253 const ElementPatterns::const_iterator iend = patterns_.end();
308
2/2
✓ Branch 2 taken 506 times.
✓ Branch 3 taken 253 times.
759 for (; i != iend; ++i) {
309
1/2
✓ Branch 2 taken 506 times.
✗ Branch 3 not taken.
506 const std::string glob_string = i->GenerateGlobString();
310
1/2
✓ Branch 1 taken 506 times.
✗ Branch 2 not taken.
506 glob_string_sequence_.push_back(glob_string);
311 506 }
312 253 }
313
314
315 340 const std::string &Pathspec::GetGlobString() const {
316
2/2
✓ Branch 0 taken 253 times.
✓ Branch 1 taken 87 times.
340 if (!glob_string_compiled_) {
317 253 GenerateGlobString();
318 253 glob_string_compiled_ = true;
319 }
320 340 return glob_string_;
321 }
322
323
324 253 void Pathspec::GenerateGlobString() const {
325
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 253 times.
253 assert(glob_string_.empty());
326
327 253 bool is_first = true;
328
1/2
✓ Branch 1 taken 253 times.
✗ Branch 2 not taken.
253 const GlobStringSequence &seq = GetGlobStringSequence();
329 253 GlobStringSequence::const_iterator i = seq.begin();
330 253 const GlobStringSequence::const_iterator iend = seq.end();
331
2/2
✓ Branch 1 taken 506 times.
✓ Branch 2 taken 253 times.
759 for (; i != iend; ++i) {
332
6/6
✓ Branch 0 taken 253 times.
✓ Branch 1 taken 253 times.
✓ Branch 3 taken 163 times.
✓ Branch 4 taken 90 times.
✓ Branch 5 taken 416 times.
✓ Branch 6 taken 90 times.
506 if (!is_first || IsAbsolute()) {
333
1/2
✓ Branch 1 taken 416 times.
✗ Branch 2 not taken.
416 glob_string_ += kSeparator;
334 }
335
1/2
✓ Branch 2 taken 506 times.
✗ Branch 3 not taken.
506 glob_string_ += *i;
336 506 is_first = false;
337 }
338 253 }
339