GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/pathspec/pathspec.cc
Date: 2026-11-08 02:41:18
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 3619 Pathspec::Pathspec(const std::string &spec)
13 3619 : regex_(NULL)
14 3619 , relaxed_regex_(NULL)
15 3619 , prefix_regex_(NULL)
16 3619 , regex_compiled_(false)
17 3619 , relaxed_regex_compiled_(false)
18 3619 , prefix_regex_compiled_(false)
19 3619 , glob_string_compiled_(false)
20 3619 , glob_string_sequence_compiled_(false)
21 3619 , valid_(true)
22 3619 , absolute_(false) {
23
1/2
✓ Branch 1 taken 3619 times.
✗ Branch 2 not taken.
3619 Parse(spec);
24
2/2
✓ Branch 1 taken 17 times.
✓ Branch 2 taken 3602 times.
3619 if (patterns_.size() == 0) {
25 17 valid_ = false;
26 }
27
28 3619 ElementPatterns::const_iterator i = patterns_.begin();
29 3619 const ElementPatterns::const_iterator iend = patterns_.end();
30
2/2
✓ Branch 2 taken 7806 times.
✓ Branch 3 taken 3619 times.
11425 for (; i != iend; ++i) {
31
2/2
✓ Branch 2 taken 31 times.
✓ Branch 3 taken 7775 times.
7806 if (!i->IsValid()) {
32 31 valid_ = false;
33 }
34 }
35 3619 }
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 4880 Pathspec::Pathspec(const Pathspec &other)
40 4880 : patterns_(other.patterns_)
41 4880 , regex_(NULL)
42 4880 , relaxed_regex_(NULL)
43 4880 , prefix_regex_(NULL)
44
1/2
✓ Branch 1 taken 4880 times.
✗ Branch 2 not taken.
4880 , glob_string_(other.glob_string_)
45
1/2
✓ Branch 1 taken 4880 times.
✗ Branch 2 not taken.
4880 , glob_string_sequence_(other.glob_string_sequence_)
46 4880 , regex_compiled_(false)
47 4880 , relaxed_regex_compiled_(false)
48 4880 , prefix_regex_compiled_(false)
49 4880 , glob_string_compiled_(other.glob_string_compiled_)
50 4880 , glob_string_sequence_compiled_(other.glob_string_sequence_compiled_)
51 4880 , valid_(other.valid_)
52 4880 , absolute_(other.absolute_) { }
53
54 8496 Pathspec::~Pathspec() { DestroyRegularExpressions(); }
55
56 54 Pathspec &Pathspec::operator=(const Pathspec &other) {
57
1/2
✓ Branch 0 taken 54 times.
✗ Branch 1 not taken.
54 if (this != &other) {
58 54 DestroyRegularExpressions(); // see: copy c'tor for details
59 54 patterns_ = other.patterns_;
60
61 54 glob_string_compiled_ = other.glob_string_compiled_;
62 54 glob_string_ = other.glob_string_;
63
64 54 glob_string_sequence_compiled_ = other.glob_string_sequence_compiled_;
65 54 glob_string_sequence_ = other.glob_string_sequence_;
66
67 54 valid_ = other.valid_;
68 54 absolute_ = other.absolute_;
69 }
70
71 54 return *this;
72 }
73
74
75 3619 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 3619 std::string::const_iterator itr = spec.begin();
80 3619 const std::string::const_iterator end = spec.end();
81
82 3619 absolute_ = (*itr == kSeparator);
83
2/2
✓ Branch 1 taken 14985 times.
✓ Branch 2 taken 3619 times.
18604 while (itr != end) {
84
2/2
✓ Branch 1 taken 7179 times.
✓ Branch 2 taken 7806 times.
14985 if (*itr == kSeparator) {
85 7179 ++itr;
86 7179 continue;
87 }
88
1/2
✓ Branch 1 taken 7806 times.
✗ Branch 2 not taken.
7806 ParsePathElement(end, &itr);
89 }
90 3619 }
91
92 7806 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 7806 const std::string::const_iterator begin_element = *itr;
96
6/6
✓ Branch 1 taken 37788 times.
✓ Branch 2 taken 3472 times.
✓ Branch 4 taken 33454 times.
✓ Branch 5 taken 4334 times.
✓ Branch 6 taken 33454 times.
✓ Branch 7 taken 7806 times.
41260 while (*itr != end && **itr != kSeparator) {
97 33454 ++(*itr);
98 }
99 7806 const std::string::const_iterator end_element = *itr;
100
101 // create a PathspecElementPattern out of this directory description
102
2/4
✓ Branch 1 taken 7806 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 7806 times.
✗ Branch 5 not taken.
7806 patterns_.push_back(PathspecElementPattern(begin_element, end_element));
103 7806 }
104
105 14014 bool Pathspec::IsMatching(const std::string &query_path) const {
106
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14014 times.
14014 assert(IsValid());
107
108
2/2
✓ Branch 1 taken 1254 times.
✓ Branch 2 taken 12760 times.
14014 if (query_path.empty()) {
109 1254 return false;
110 }
111
112 12760 const bool query_is_absolute = (query_path[0] == kSeparator);
113
2/2
✓ Branch 1 taken 10980 times.
✓ Branch 2 taken 226 times.
11206 return (!query_is_absolute || this->IsAbsolute())
114
4/4
✓ Branch 0 taken 11206 times.
✓ Branch 1 taken 1554 times.
✓ Branch 3 taken 4241 times.
✓ Branch 4 taken 8293 times.
23966 && IsPathspecMatching(query_path);
115 }
116
117 216 bool Pathspec::IsPrefixMatching(const std::string &query_path) const {
118
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 216 times.
216 assert(IsValid());
119
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 216 times.
216 assert(IsAbsolute());
120
121
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 216 times.
216 if (query_path.empty()) {
122 ✗ return false;
123 }
124
125 216 const bool query_is_absolute = (query_path[0] == kSeparator);
126
4/4
✓ Branch 0 taken 198 times.
✓ Branch 1 taken 18 times.
✓ Branch 3 taken 126 times.
✓ Branch 4 taken 72 times.
216 return (query_is_absolute && IsPathspecPrefixMatching(query_path));
127 }
128
129 7076 bool Pathspec::IsMatchingRelaxed(const std::string &query_path) const {
130
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7076 times.
7076 assert(IsValid());
131
132
2/2
✓ Branch 1 taken 1239 times.
✓ Branch 2 taken 5837 times.
7076 if (query_path.empty()) {
133 1239 return false;
134 }
135
136 5837 return IsPathspecMatchingRelaxed(query_path);
137 }
138
139 12534 bool Pathspec::IsPathspecMatching(const std::string &query_path) const {
140 12534 return ApplyRegularExpression(query_path, GetRegularExpression());
141 }
142
143 198 bool Pathspec::IsPathspecPrefixMatching(const std::string &query_path) const {
144 198 return ApplyRegularExpression(query_path, GetPrefixRegularExpression());
145 }
146
147 5837 bool Pathspec::IsPathspecMatchingRelaxed(const std::string &query_path) const {
148 5837 return ApplyRegularExpression(query_path, GetRelaxedRegularExpression());
149 }
150
151 18569 bool Pathspec::ApplyRegularExpression(const std::string &query_path,
152 regex_t *regex) const {
153 18569 const char *path = query_path.c_str();
154 18569 const int retval = regexec(regex, path, 0, NULL, 0);
155
156
3/4
✓ Branch 0 taken 13249 times.
✓ Branch 1 taken 5320 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 13249 times.
18569 if (retval != 0 && retval != REG_NOMATCH) {
157 ✗ PrintRegularExpressionError(retval);
158 }
159
160 18569 return (retval == 0);
161 }
162
163 12534 regex_t *Pathspec::GetRegularExpression() const {
164
2/2
✓ Branch 0 taken 1836 times.
✓ Branch 1 taken 10698 times.
12534 if (!regex_compiled_) {
165 1836 const bool is_relaxed = false;
166
1/2
✓ Branch 1 taken 1836 times.
✗ Branch 2 not taken.
1836 const std::string regex = GenerateRegularExpression(is_relaxed);
167
1/2
✓ Branch 2 taken 1836 times.
✗ Branch 3 not taken.
1836 LogCvmfs(kLogPathspec, kLogDebug, "compiled regex: %s", regex.c_str());
168
169
1/2
✓ Branch 1 taken 1836 times.
✗ Branch 2 not taken.
1836 regex_ = CompileRegularExpression(regex);
170 1836 regex_compiled_ = true;
171 1836 }
172
173 12534 return regex_;
174 }
175
176 198 regex_t *Pathspec::GetPrefixRegularExpression() const {
177
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 162 times.
198 if (!prefix_regex_compiled_) {
178 36 const bool is_relaxed = false;
179 36 const bool is_prefix = true;
180
1/2
✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
36 const std::string regex = GenerateRegularExpression(is_relaxed, is_prefix);
181
1/2
✓ Branch 2 taken 36 times.
✗ Branch 3 not taken.
36 LogCvmfs(kLogPathspec, kLogDebug, "compiled regex: %s", regex.c_str());
182
183
1/2
✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
36 prefix_regex_ = CompileRegularExpression(regex);
184 36 prefix_regex_compiled_ = true;
185 36 }
186
187 198 return prefix_regex_;
188 }
189
190 5837 regex_t *Pathspec::GetRelaxedRegularExpression() const {
191
2/2
✓ Branch 0 taken 513 times.
✓ Branch 1 taken 5324 times.
5837 if (!relaxed_regex_compiled_) {
192 513 const bool is_relaxed = true;
193
1/2
✓ Branch 1 taken 513 times.
✗ Branch 2 not taken.
513 const std::string regex = GenerateRegularExpression(is_relaxed);
194
1/2
✓ Branch 2 taken 513 times.
✗ Branch 3 not taken.
513 LogCvmfs(kLogPathspec, kLogDebug, "compiled relaxed regex: %s",
195 regex.c_str());
196
197
1/2
✓ Branch 1 taken 513 times.
✗ Branch 2 not taken.
513 relaxed_regex_ = CompileRegularExpression(regex);
198 513 relaxed_regex_compiled_ = true;
199 513 }
200
201 5837 return relaxed_regex_;
202 }
203
204 2385 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 2385 times.
✗ Branch 3 not taken.
2385 std::string regex = "^";
208
209 // absolute paths require a / in the beginning
210
2/2
✓ Branch 1 taken 2067 times.
✓ Branch 2 taken 318 times.
2385 if (IsAbsolute()) {
211
1/2
✓ Branch 1 taken 2067 times.
✗ Branch 2 not taken.
2067 regex += kSeparator;
212 }
213
214 // concatenate the regular expressions of the compiled path elements
215 2385 ElementPatterns::const_iterator i = patterns_.begin();
216 2385 const ElementPatterns::const_iterator iend = patterns_.end();
217
2/2
✓ Branch 2 taken 5199 times.
✓ Branch 3 taken 2385 times.
7584 for (; i != iend; ++i) {
218
2/4
✓ Branch 2 taken 5199 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 5199 times.
✗ Branch 6 not taken.
5199 regex += i->GenerateRegularExpression(is_relaxed);
219
2/2
✓ Branch 2 taken 2814 times.
✓ Branch 3 taken 2385 times.
5199 if (i + 1 != iend) {
220
1/2
✓ Branch 1 taken 2814 times.
✗ Branch 2 not taken.
2814 regex += kSeparator;
221 }
222 }
223
224
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 2349 times.
2385 if (is_prefix) {
225
1/2
✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
36 regex += "($|";
226
1/2
✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
36 regex += kSeparator;
227
1/2
✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
36 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 2349 times.
✗ Branch 2 not taken.
2349 regex += kSeparator;
232
1/2
✓ Branch 1 taken 2349 times.
✗ Branch 2 not taken.
2349 regex += "?$";
233 }
234
235 4770 return regex;
236 }
237
238 2385 regex_t *Pathspec::CompileRegularExpression(const std::string &regex) const {
239 2385 regex_t *result = reinterpret_cast<regex_t *>(smalloc(sizeof(regex_t)));
240 2385 const int flags = REG_NOSUB | REG_NEWLINE | REG_EXTENDED;
241 2385 const int retval = regcomp(result, regex.c_str(), flags);
242
243
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2385 times.
2385 if (retval != 0) {
244 ✗ PrintRegularExpressionError(retval);
245 ✗ assert(false && "failed to compile regex");
246 }
247
248 2385 return result;
249 }
250
251 8550 void Pathspec::DestroyRegularExpressions() {
252
2/2
✓ Branch 0 taken 1836 times.
✓ Branch 1 taken 6714 times.
8550 if (regex_compiled_) {
253
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1836 times.
1836 assert(regex_ != NULL);
254 1836 regfree(regex_);
255 1836 regex_ = NULL;
256 1836 regex_compiled_ = false;
257 }
258
259
2/2
✓ Branch 0 taken 513 times.
✓ Branch 1 taken 8037 times.
8550 if (relaxed_regex_compiled_) {
260
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 513 times.
513 assert(relaxed_regex_ != NULL);
261 513 regfree(relaxed_regex_);
262 513 relaxed_regex_ = NULL;
263 513 relaxed_regex_compiled_ = false;
264 }
265 8550 }
266
267 1521 bool Pathspec::operator==(const Pathspec &other) const {
268
1/2
✓ Branch 4 taken 867 times.
✗ Branch 5 not taken.
2388 if (patterns_.size() != other.patterns_.size() || IsValid() != other.IsValid()
269
6/6
✓ Branch 0 taken 867 times.
✓ Branch 1 taken 654 times.
✓ Branch 4 taken 70 times.
✓ Branch 5 taken 797 times.
✓ Branch 6 taken 724 times.
✓ Branch 7 taken 797 times.
2388 || IsAbsolute() != other.IsAbsolute()) {
270 724 return false;
271 }
272
273 797 ElementPatterns::const_iterator i = patterns_.begin();
274 797 const ElementPatterns::const_iterator iend = patterns_.end();
275 797 ElementPatterns::const_iterator j = other.patterns_.begin();
276 797 const ElementPatterns::const_iterator jend = other.patterns_.end();
277
278
5/6
✓ Branch 3 taken 1666 times.
✓ Branch 4 taken 283 times.
✓ Branch 6 taken 1666 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 1666 times.
✓ Branch 9 taken 283 times.
1949 for (; i != iend && j != jend; ++i, ++j) {
279
3/4
✓ Branch 3 taken 1666 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 514 times.
✓ Branch 6 taken 1152 times.
1666 if (*i != *j) {
280 514 return false;
281 }
282 }
283
284 283 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 304 const Pathspec::GlobStringSequence &Pathspec::GetGlobStringSequence() const {
296
1/2
✓ Branch 0 taken 304 times.
✗ Branch 1 not taken.
304 if (!glob_string_sequence_compiled_) {
297 304 GenerateGlobStringSequence();
298 304 glob_string_sequence_compiled_ = true;
299 }
300 304 return glob_string_sequence_;
301 }
302
303
304 304 void Pathspec::GenerateGlobStringSequence() const {
305
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 304 times.
304 assert(glob_string_sequence_.empty());
306 304 ElementPatterns::const_iterator i = patterns_.begin();
307 304 const ElementPatterns::const_iterator iend = patterns_.end();
308
2/2
✓ Branch 2 taken 608 times.
✓ Branch 3 taken 304 times.
912 for (; i != iend; ++i) {
309
1/2
✓ Branch 2 taken 608 times.
✗ Branch 3 not taken.
608 const std::string glob_string = i->GenerateGlobString();
310
1/2
✓ Branch 1 taken 608 times.
✗ Branch 2 not taken.
608 glob_string_sequence_.push_back(glob_string);
311 608 }
312 304 }
313
314
315 409 const std::string &Pathspec::GetGlobString() const {
316
2/2
✓ Branch 0 taken 304 times.
✓ Branch 1 taken 105 times.
409 if (!glob_string_compiled_) {
317 304 GenerateGlobString();
318 304 glob_string_compiled_ = true;
319 }
320 409 return glob_string_;
321 }
322
323
324 304 void Pathspec::GenerateGlobString() const {
325
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 304 times.
304 assert(glob_string_.empty());
326
327 304 bool is_first = true;
328
1/2
✓ Branch 1 taken 304 times.
✗ Branch 2 not taken.
304 const GlobStringSequence &seq = GetGlobStringSequence();
329 304 GlobStringSequence::const_iterator i = seq.begin();
330 304 const GlobStringSequence::const_iterator iend = seq.end();
331
2/2
✓ Branch 1 taken 608 times.
✓ Branch 2 taken 304 times.
912 for (; i != iend; ++i) {
332
6/6
✓ Branch 0 taken 304 times.
✓ Branch 1 taken 304 times.
✓ Branch 3 taken 196 times.
✓ Branch 4 taken 108 times.
✓ Branch 5 taken 500 times.
✓ Branch 6 taken 108 times.
608 if (!is_first || IsAbsolute()) {
333
1/2
✓ Branch 1 taken 500 times.
✗ Branch 2 not taken.
500 glob_string_ += kSeparator;
334 }
335
1/2
✓ Branch 2 taken 608 times.
✗ Branch 3 not taken.
608 glob_string_ += *i;
336 608 is_first = false;
337 }
338 304 }
339