GCC Code Coverage Report


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