GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/pathspec/pathspec.cc
Date: 2026-03-08 02:37:57
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 4236 Pathspec::Pathspec(const std::string &spec)
13 4236 : regex_(NULL)
14 4236 , relaxed_regex_(NULL)
15 4236 , prefix_regex_(NULL)
16 4236 , regex_compiled_(false)
17 4236 , relaxed_regex_compiled_(false)
18 4236 , prefix_regex_compiled_(false)
19 4236 , glob_string_compiled_(false)
20 4236 , glob_string_sequence_compiled_(false)
21 4236 , valid_(true)
22 4236 , absolute_(false) {
23
1/2
✓ Branch 1 taken 4236 times.
✗ Branch 2 not taken.
4236 Parse(spec);
24
2/2
✓ Branch 1 taken 26 times.
✓ Branch 2 taken 4210 times.
4236 if (patterns_.size() == 0) {
25 26 valid_ = false;
26 }
27
28 4236 ElementPatterns::const_iterator i = patterns_.begin();
29 4236 const ElementPatterns::const_iterator iend = patterns_.end();
30
2/2
✓ Branch 2 taken 9303 times.
✓ Branch 3 taken 4236 times.
13539 for (; i != iend; ++i) {
31
2/2
✓ Branch 2 taken 51 times.
✓ Branch 3 taken 9252 times.
9303 if (!i->IsValid()) {
32 51 valid_ = false;
33 }
34 }
35 4236 }
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 4528 Pathspec::Pathspec(const Pathspec &other)
40 4528 : patterns_(other.patterns_)
41 4528 , regex_(NULL)
42 4528 , relaxed_regex_(NULL)
43 4528 , prefix_regex_(NULL)
44
1/2
✓ Branch 1 taken 4528 times.
✗ Branch 2 not taken.
4528 , glob_string_(other.glob_string_)
45
1/2
✓ Branch 1 taken 4528 times.
✗ Branch 2 not taken.
4528 , glob_string_sequence_(other.glob_string_sequence_)
46 4528 , regex_compiled_(false)
47 4528 , relaxed_regex_compiled_(false)
48 4528 , prefix_regex_compiled_(false)
49 4528 , glob_string_compiled_(other.glob_string_compiled_)
50 4528 , glob_string_sequence_compiled_(other.glob_string_sequence_compiled_)
51 4528 , valid_(other.valid_)
52 4528 , absolute_(other.absolute_) { }
53
54 8761 Pathspec::~Pathspec() { DestroyRegularExpressions(); }
55
56 78 Pathspec &Pathspec::operator=(const Pathspec &other) {
57
1/2
✓ Branch 0 taken 78 times.
✗ Branch 1 not taken.
78 if (this != &other) {
58 78 DestroyRegularExpressions(); // see: copy c'tor for details
59 78 patterns_ = other.patterns_;
60
61 78 glob_string_compiled_ = other.glob_string_compiled_;
62 78 glob_string_ = other.glob_string_;
63
64 78 glob_string_sequence_compiled_ = other.glob_string_sequence_compiled_;
65 78 glob_string_sequence_ = other.glob_string_sequence_;
66
67 78 valid_ = other.valid_;
68 78 absolute_ = other.absolute_;
69 }
70
71 78 return *this;
72 }
73
74
75 4236 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 4236 std::string::const_iterator itr = spec.begin();
80 4236 const std::string::const_iterator end = spec.end();
81
82 4236 absolute_ = (*itr == kSeparator);
83
2/2
✓ Branch 1 taken 17639 times.
✓ Branch 2 taken 4236 times.
21875 while (itr != end) {
84
2/2
✓ Branch 1 taken 8336 times.
✓ Branch 2 taken 9303 times.
17639 if (*itr == kSeparator) {
85 8336 ++itr;
86 8336 continue;
87 }
88
1/2
✓ Branch 1 taken 9303 times.
✗ Branch 2 not taken.
9303 ParsePathElement(end, &itr);
89 }
90 4236 }
91
92 9303 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 9303 const std::string::const_iterator begin_element = *itr;
96
6/6
✓ Branch 1 taken 44706 times.
✓ Branch 2 taken 4034 times.
✓ Branch 4 taken 39437 times.
✓ Branch 5 taken 5269 times.
✓ Branch 6 taken 39437 times.
✓ Branch 7 taken 9303 times.
48740 while (*itr != end && **itr != kSeparator) {
97 39437 ++(*itr);
98 }
99 9303 const std::string::const_iterator end_element = *itr;
100
101 // create a PathspecElementPattern out of this directory description
102
2/4
✓ Branch 1 taken 9303 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 9303 times.
✗ Branch 5 not taken.
9303 patterns_.push_back(PathspecElementPattern(begin_element, end_element));
103 9303 }
104
105 10434 bool Pathspec::IsMatching(const std::string &query_path) const {
106
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10434 times.
10434 assert(IsValid());
107
108
2/2
✓ Branch 1 taken 429 times.
✓ Branch 2 taken 10005 times.
10434 if (query_path.empty()) {
109 429 return false;
110 }
111
112 10005 const bool query_is_absolute = (query_path[0] == kSeparator);
113
2/2
✓ Branch 1 taken 7344 times.
✓ Branch 2 taken 340 times.
7684 return (!query_is_absolute || this->IsAbsolute())
114
4/4
✓ Branch 0 taken 7684 times.
✓ Branch 1 taken 2321 times.
✓ Branch 3 taken 3844 times.
✓ Branch 4 taken 5821 times.
17689 && IsPathspecMatching(query_path);
115 }
116
117 324 bool Pathspec::IsPrefixMatching(const std::string &query_path) const {
118
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 324 times.
324 assert(IsValid());
119
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 324 times.
324 assert(IsAbsolute());
120
121
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 324 times.
324 if (query_path.empty()) {
122 return false;
123 }
124
125 324 const bool query_is_absolute = (query_path[0] == kSeparator);
126
4/4
✓ Branch 0 taken 297 times.
✓ Branch 1 taken 27 times.
✓ Branch 3 taken 189 times.
✓ Branch 4 taken 108 times.
324 return (query_is_absolute && IsPathspecPrefixMatching(query_path));
127 }
128
129 7926 bool Pathspec::IsMatchingRelaxed(const std::string &query_path) const {
130
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7926 times.
7926 assert(IsValid());
131
132
2/2
✓ Branch 1 taken 900 times.
✓ Branch 2 taken 7026 times.
7926 if (query_path.empty()) {
133 900 return false;
134 }
135
136 7026 return IsPathspecMatchingRelaxed(query_path);
137 }
138
139 9665 bool Pathspec::IsPathspecMatching(const std::string &query_path) const {
140 9665 return ApplyRegularExpression(query_path, GetRegularExpression());
141 }
142
143 297 bool Pathspec::IsPathspecPrefixMatching(const std::string &query_path) const {
144 297 return ApplyRegularExpression(query_path, GetPrefixRegularExpression());
145 }
146
147 7026 bool Pathspec::IsPathspecMatchingRelaxed(const std::string &query_path) const {
148 7026 return ApplyRegularExpression(query_path, GetRelaxedRegularExpression());
149 }
150
151 16988 bool Pathspec::ApplyRegularExpression(const std::string &query_path,
152 regex_t *regex) const {
153 16988 const char *path = query_path.c_str();
154 16988 const int retval = regexec(regex, path, 0, NULL, 0);
155
156
3/4
✓ Branch 0 taken 11631 times.
✓ Branch 1 taken 5357 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 11631 times.
16988 if (retval != 0 && retval != REG_NOMATCH) {
157 PrintRegularExpressionError(retval);
158 }
159
160 16988 return (retval == 0);
161 }
162
163 9665 regex_t *Pathspec::GetRegularExpression() const {
164
2/2
✓ Branch 0 taken 1926 times.
✓ Branch 1 taken 7739 times.
9665 if (!regex_compiled_) {
165 1926 const bool is_relaxed = false;
166
1/2
✓ Branch 1 taken 1926 times.
✗ Branch 2 not taken.
1926 const std::string regex = GenerateRegularExpression(is_relaxed);
167
1/2
✓ Branch 2 taken 1926 times.
✗ Branch 3 not taken.
1926 LogCvmfs(kLogPathspec, kLogDebug, "compiled regex: %s", regex.c_str());
168
169
1/2
✓ Branch 1 taken 1926 times.
✗ Branch 2 not taken.
1926 regex_ = CompileRegularExpression(regex);
170 1926 regex_compiled_ = true;
171 1926 }
172
173 9665 return regex_;
174 }
175
176 297 regex_t *Pathspec::GetPrefixRegularExpression() const {
177
2/2
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 243 times.
297 if (!prefix_regex_compiled_) {
178 54 const bool is_relaxed = false;
179 54 const bool is_prefix = true;
180
1/2
✓ Branch 1 taken 54 times.
✗ Branch 2 not taken.
54 const std::string regex = GenerateRegularExpression(is_relaxed, is_prefix);
181
1/2
✓ Branch 2 taken 54 times.
✗ Branch 3 not taken.
54 LogCvmfs(kLogPathspec, kLogDebug, "compiled regex: %s", regex.c_str());
182
183
1/2
✓ Branch 1 taken 54 times.
✗ Branch 2 not taken.
54 prefix_regex_ = CompileRegularExpression(regex);
184 54 prefix_regex_compiled_ = true;
185 54 }
186
187 297 return prefix_regex_;
188 }
189
190 7026 regex_t *Pathspec::GetRelaxedRegularExpression() const {
191
2/2
✓ Branch 0 taken 710 times.
✓ Branch 1 taken 6316 times.
7026 if (!relaxed_regex_compiled_) {
192 710 const bool is_relaxed = true;
193
1/2
✓ Branch 1 taken 710 times.
✗ Branch 2 not taken.
710 const std::string regex = GenerateRegularExpression(is_relaxed);
194
1/2
✓ Branch 2 taken 710 times.
✗ Branch 3 not taken.
710 LogCvmfs(kLogPathspec, kLogDebug, "compiled relaxed regex: %s",
195 regex.c_str());
196
197
1/2
✓ Branch 1 taken 710 times.
✗ Branch 2 not taken.
710 relaxed_regex_ = CompileRegularExpression(regex);
198 710 relaxed_regex_compiled_ = true;
199 710 }
200
201 7026 return relaxed_regex_;
202 }
203
204 2690 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 2690 times.
✗ Branch 3 not taken.
2690 std::string regex = "^";
208
209 // absolute paths require a / in the beginning
210
2/2
✓ Branch 1 taken 2199 times.
✓ Branch 2 taken 491 times.
2690 if (IsAbsolute()) {
211
1/2
✓ Branch 1 taken 2199 times.
✗ Branch 2 not taken.
2199 regex += kSeparator;
212 }
213
214 // concatenate the regular expressions of the compiled path elements
215 2690 ElementPatterns::const_iterator i = patterns_.begin();
216 2690 const ElementPatterns::const_iterator iend = patterns_.end();
217
2/2
✓ Branch 2 taken 5809 times.
✓ Branch 3 taken 2690 times.
8499 for (; i != iend; ++i) {
218
2/4
✓ Branch 2 taken 5809 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 5809 times.
✗ Branch 6 not taken.
5809 regex += i->GenerateRegularExpression(is_relaxed);
219
2/2
✓ Branch 2 taken 3119 times.
✓ Branch 3 taken 2690 times.
5809 if (i + 1 != iend) {
220
1/2
✓ Branch 1 taken 3119 times.
✗ Branch 2 not taken.
3119 regex += kSeparator;
221 }
222 }
223
224
2/2
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 2636 times.
2690 if (is_prefix) {
225
1/2
✓ Branch 1 taken 54 times.
✗ Branch 2 not taken.
54 regex += "($|";
226
1/2
✓ Branch 1 taken 54 times.
✗ Branch 2 not taken.
54 regex += kSeparator;
227
1/2
✓ Branch 1 taken 54 times.
✗ Branch 2 not taken.
54 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 2636 times.
✗ Branch 2 not taken.
2636 regex += kSeparator;
232
1/2
✓ Branch 1 taken 2636 times.
✗ Branch 2 not taken.
2636 regex += "?$";
233 }
234
235 5380 return regex;
236 }
237
238 2690 regex_t *Pathspec::CompileRegularExpression(const std::string &regex) const {
239 2690 regex_t *result = reinterpret_cast<regex_t *>(smalloc(sizeof(regex_t)));
240 2690 const int flags = REG_NOSUB | REG_NEWLINE | REG_EXTENDED;
241 2690 const int retval = regcomp(result, regex.c_str(), flags);
242
243
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2690 times.
2690 if (retval != 0) {
244 PrintRegularExpressionError(retval);
245 assert(false && "failed to compile regex");
246 }
247
248 2690 return result;
249 }
250
251 8839 void Pathspec::DestroyRegularExpressions() {
252
2/2
✓ Branch 0 taken 1926 times.
✓ Branch 1 taken 6913 times.
8839 if (regex_compiled_) {
253
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1926 times.
1926 assert(regex_ != NULL);
254 1926 regfree(regex_);
255 1926 regex_ = NULL;
256 1926 regex_compiled_ = false;
257 }
258
259
2/2
✓ Branch 0 taken 710 times.
✓ Branch 1 taken 8129 times.
8839 if (relaxed_regex_compiled_) {
260
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 710 times.
710 assert(relaxed_regex_ != NULL);
261 710 regfree(relaxed_regex_);
262 710 relaxed_regex_ = NULL;
263 710 relaxed_regex_compiled_ = false;
264 }
265 8839 }
266
267 2285 bool Pathspec::operator==(const Pathspec &other) const {
268
1/2
✓ Branch 4 taken 1383 times.
✗ Branch 5 not taken.
3668 if (patterns_.size() != other.patterns_.size() || IsValid() != other.IsValid()
269
6/6
✓ Branch 0 taken 1383 times.
✓ Branch 1 taken 902 times.
✓ Branch 4 taken 126 times.
✓ Branch 5 taken 1257 times.
✓ Branch 6 taken 1028 times.
✓ Branch 7 taken 1257 times.
3668 || IsAbsolute() != other.IsAbsolute()) {
270 1028 return false;
271 }
272
273 1257 ElementPatterns::const_iterator i = patterns_.begin();
274 1257 const ElementPatterns::const_iterator iend = patterns_.end();
275 1257 ElementPatterns::const_iterator j = other.patterns_.begin();
276 1257 const ElementPatterns::const_iterator jend = other.patterns_.end();
277
278
5/6
✓ Branch 3 taken 2618 times.
✓ Branch 4 taken 415 times.
✓ Branch 6 taken 2618 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2618 times.
✓ Branch 9 taken 415 times.
3033 for (; i != iend && j != jend; ++i, ++j) {
279
3/4
✓ Branch 3 taken 2618 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 842 times.
✓ Branch 6 taken 1776 times.
2618 if (*i != *j) {
280 842 return false;
281 }
282 }
283
284 415 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 442 const Pathspec::GlobStringSequence &Pathspec::GetGlobStringSequence() const {
296
1/2
✓ Branch 0 taken 442 times.
✗ Branch 1 not taken.
442 if (!glob_string_sequence_compiled_) {
297 442 GenerateGlobStringSequence();
298 442 glob_string_sequence_compiled_ = true;
299 }
300 442 return glob_string_sequence_;
301 }
302
303
304 442 void Pathspec::GenerateGlobStringSequence() const {
305
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 442 times.
442 assert(glob_string_sequence_.empty());
306 442 ElementPatterns::const_iterator i = patterns_.begin();
307 442 const ElementPatterns::const_iterator iend = patterns_.end();
308
2/2
✓ Branch 2 taken 884 times.
✓ Branch 3 taken 442 times.
1326 for (; i != iend; ++i) {
309
1/2
✓ Branch 2 taken 884 times.
✗ Branch 3 not taken.
884 const std::string glob_string = i->GenerateGlobString();
310
1/2
✓ Branch 1 taken 884 times.
✗ Branch 2 not taken.
884 glob_string_sequence_.push_back(glob_string);
311 884 }
312 442 }
313
314
315 598 const std::string &Pathspec::GetGlobString() const {
316
2/2
✓ Branch 0 taken 442 times.
✓ Branch 1 taken 156 times.
598 if (!glob_string_compiled_) {
317 442 GenerateGlobString();
318 442 glob_string_compiled_ = true;
319 }
320 598 return glob_string_;
321 }
322
323
324 442 void Pathspec::GenerateGlobString() const {
325
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 442 times.
442 assert(glob_string_.empty());
326
327 442 bool is_first = true;
328
1/2
✓ Branch 1 taken 442 times.
✗ Branch 2 not taken.
442 const GlobStringSequence &seq = GetGlobStringSequence();
329 442 GlobStringSequence::const_iterator i = seq.begin();
330 442 const GlobStringSequence::const_iterator iend = seq.end();
331
2/2
✓ Branch 1 taken 884 times.
✓ Branch 2 taken 442 times.
1326 for (; i != iend; ++i) {
332
6/6
✓ Branch 0 taken 442 times.
✓ Branch 1 taken 442 times.
✓ Branch 3 taken 286 times.
✓ Branch 4 taken 156 times.
✓ Branch 5 taken 728 times.
✓ Branch 6 taken 156 times.
884 if (!is_first || IsAbsolute()) {
333
1/2
✓ Branch 1 taken 728 times.
✗ Branch 2 not taken.
728 glob_string_ += kSeparator;
334 }
335
1/2
✓ Branch 2 taken 884 times.
✗ Branch 3 not taken.
884 glob_string_ += *i;
336 884 is_first = false;
337 }
338 442 }
339