| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/pathspec/pathspec_pattern.cc |
| Date: | 2025-12-07 02:35:36 |
| 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 | 3591 | PathspecElementPattern::PathspecElementPattern( | |
| 13 | const std::string::const_iterator begin, | ||
| 14 | 3591 | const std::string::const_iterator &end) | |
| 15 | 3591 | : valid_(true) { | |
| 16 |
1/2✓ Branch 1 taken 3591 times.
✗ Branch 2 not taken.
|
3591 | Parse(begin, end); |
| 17 | 3591 | } | |
| 18 | |||
| 19 | 12121 | PathspecElementPattern::PathspecElementPattern( | |
| 20 | 12121 | const PathspecElementPattern &other) | |
| 21 | 12121 | : valid_(other.valid_) { | |
| 22 |
1/2✓ Branch 2 taken 12121 times.
✗ Branch 3 not taken.
|
12121 | subpatterns_.reserve(other.subpatterns_.size()); |
| 23 | 12121 | SubPatterns::const_iterator i = other.subpatterns_.begin(); | |
| 24 | 12121 | const SubPatterns::const_iterator iend = other.subpatterns_.end(); | |
| 25 |
2/2✓ Branch 1 taken 13024 times.
✓ Branch 2 taken 12121 times.
|
25145 | for (; i != iend; ++i) { |
| 26 |
2/4✓ Branch 2 taken 13024 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 13024 times.
✗ Branch 6 not taken.
|
13024 | subpatterns_.push_back((*i)->Clone()); |
| 27 | } | ||
| 28 | 12121 | } | |
| 29 | |||
| 30 | 49 | PathspecElementPattern &PathspecElementPattern::operator=( | |
| 31 | const PathspecElementPattern &other) { | ||
| 32 |
1/2✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
|
49 | if (this != &other) { |
| 33 | 49 | valid_ = other.valid_; | |
| 34 | 49 | subpatterns_.clear(); | |
| 35 |
1/2✓ Branch 2 taken 49 times.
✗ Branch 3 not taken.
|
49 | subpatterns_.reserve(other.subpatterns_.size()); |
| 36 | 49 | SubPatterns::const_iterator i = other.subpatterns_.begin(); | |
| 37 | 49 | const SubPatterns::const_iterator iend = other.subpatterns_.end(); | |
| 38 |
2/2✓ Branch 1 taken 77 times.
✓ Branch 2 taken 49 times.
|
126 | for (; i != iend; ++i) { |
| 39 |
2/4✓ Branch 2 taken 77 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 77 times.
✗ Branch 6 not taken.
|
77 | subpatterns_.push_back((*i)->Clone()); |
| 40 | } | ||
| 41 | } | ||
| 42 | |||
| 43 | 49 | return *this; | |
| 44 | } | ||
| 45 | |||
| 46 | |||
| 47 | 15706 | PathspecElementPattern::~PathspecElementPattern() { | |
| 48 | 15706 | SubPatterns::const_iterator i = subpatterns_.begin(); | |
| 49 | 15706 | const SubPatterns::const_iterator iend = subpatterns_.end(); | |
| 50 |
2/2✓ Branch 2 taken 17134 times.
✓ Branch 3 taken 15706 times.
|
32840 | for (; i != iend; ++i) { |
| 51 |
1/2✓ Branch 1 taken 17134 times.
✗ Branch 2 not taken.
|
17134 | delete *i; |
| 52 | } | ||
| 53 | 15706 | subpatterns_.clear(); | |
| 54 | 15706 | } | |
| 55 | |||
| 56 | |||
| 57 | 3591 | void PathspecElementPattern::Parse(const std::string::const_iterator &begin, | |
| 58 | const std::string::const_iterator &end) { | ||
| 59 | 3591 | std::string::const_iterator i = begin; | |
| 60 |
2/2✓ Branch 1 taken 4090 times.
✓ Branch 2 taken 3591 times.
|
7681 | while (i != end) { |
| 61 |
3/4✓ Branch 2 taken 660 times.
✓ Branch 3 taken 3430 times.
✓ Branch 5 taken 660 times.
✗ Branch 6 not taken.
|
4090 | SubPattern *next = (Pathspec::IsSpecialChar(*i)) ? ParseSpecialChar(end, &i) |
| 62 |
1/2✓ Branch 1 taken 3430 times.
✗ Branch 2 not taken.
|
3430 | : ParsePlaintext(end, &i); |
| 63 |
3/4✓ Branch 1 taken 4090 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 4088 times.
|
4090 | if (next->IsEmpty()) { |
| 64 | 2 | valid_ = false; | |
| 65 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | delete next; |
| 66 | } else { | ||
| 67 |
1/2✓ Branch 1 taken 4088 times.
✗ Branch 2 not taken.
|
4088 | subpatterns_.push_back(next); |
| 68 | } | ||
| 69 | } | ||
| 70 | 3591 | } | |
| 71 | |||
| 72 | |||
| 73 | 3430 | PathspecElementPattern::SubPattern *PathspecElementPattern::ParsePlaintext( | |
| 74 | const std::string::const_iterator &end, std::string::const_iterator *i) { | ||
| 75 | 3430 | PlaintextSubPattern *pattern = new PlaintextSubPattern(); | |
| 76 | 3430 | bool next_is_escaped = false; | |
| 77 | |||
| 78 |
2/2✓ Branch 1 taken 15456 times.
✓ Branch 2 taken 3256 times.
|
18712 | while (*i < end) { |
| 79 |
6/6✓ Branch 2 taken 261 times.
✓ Branch 3 taken 15195 times.
✓ Branch 4 taken 174 times.
✓ Branch 5 taken 87 times.
✓ Branch 6 taken 174 times.
✓ Branch 7 taken 15282 times.
|
15456 | if (Pathspec::IsSpecialChar(**i) && !next_is_escaped) { |
| 80 | 174 | break; | |
| 81 | } | ||
| 82 | |||
| 83 |
6/6✓ Branch 1 taken 150 times.
✓ Branch 2 taken 15132 times.
✓ Branch 3 taken 123 times.
✓ Branch 4 taken 27 times.
✓ Branch 5 taken 123 times.
✓ Branch 6 taken 15159 times.
|
15282 | if (**i == Pathspec::kEscaper && !next_is_escaped) { |
| 84 | 123 | next_is_escaped = true; | |
| 85 |
2/2✓ Branch 0 taken 155 times.
✓ Branch 1 taken 15004 times.
|
15159 | } else if (next_is_escaped) { |
| 86 |
6/6✓ Branch 2 taken 68 times.
✓ Branch 3 taken 87 times.
✓ Branch 5 taken 27 times.
✓ Branch 6 taken 41 times.
✓ Branch 7 taken 114 times.
✓ Branch 8 taken 41 times.
|
155 | if (Pathspec::IsSpecialChar(**i) || **i == Pathspec::kEscaper) { |
| 87 | 114 | pattern->AddChar(**i); | |
| 88 | 114 | next_is_escaped = false; | |
| 89 | } else { | ||
| 90 | 41 | valid_ = false; | |
| 91 | } | ||
| 92 | } else { | ||
| 93 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 15004 times.
|
15004 | assert(!Pathspec::IsSpecialChar(**i)); |
| 94 | 15004 | pattern->AddChar(**i); | |
| 95 | } | ||
| 96 | |||
| 97 | 15282 | ++(*i); | |
| 98 | } | ||
| 99 | |||
| 100 | 3430 | return pattern; | |
| 101 | } | ||
| 102 | |||
| 103 | 660 | 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 660 times.
|
660 | assert(Pathspec::IsSpecialChar(**i)); |
| 106 | 660 | const char chr = **i; | |
| 107 | 660 | ++(*i); | |
| 108 | |||
| 109 |
2/3✓ Branch 0 taken 415 times.
✓ Branch 1 taken 245 times.
✗ Branch 2 not taken.
|
660 | switch (chr) { |
| 110 | 415 | case Pathspec::kWildcard: | |
| 111 | 415 | return new WildcardSubPattern(); | |
| 112 | 245 | case Pathspec::kPlaceholder: | |
| 113 | 245 | return new PlaceholderSubPattern(); | |
| 114 | ✗ | default: | |
| 115 | ✗ | assert(false && "unrecognized special character"); | |
| 116 | } | ||
| 117 | } | ||
| 118 | |||
| 119 | 2810 | std::string PathspecElementPattern::GenerateRegularExpression( | |
| 120 | const bool is_relaxed) const { | ||
| 121 | 2810 | std::string result; | |
| 122 | 2810 | SubPatterns::const_iterator i = subpatterns_.begin(); | |
| 123 | 2810 | const SubPatterns::const_iterator iend = subpatterns_.end(); | |
| 124 |
2/2✓ Branch 1 taken 3226 times.
✓ Branch 2 taken 2810 times.
|
6036 | for (; i != iend; ++i) { |
| 125 |
2/4✓ Branch 2 taken 3226 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 3226 times.
✗ Branch 6 not taken.
|
3226 | result += (*i)->GenerateRegularExpression(is_relaxed); |
| 126 | } | ||
| 127 | 5620 | return result; | |
| 128 | } | ||
| 129 | |||
| 130 | 238 | std::string PathspecElementPattern::GenerateGlobString() const { | |
| 131 | 238 | std::string result; | |
| 132 | 238 | SubPatterns::const_iterator i = subpatterns_.begin(); | |
| 133 | 238 | const SubPatterns::const_iterator iend = subpatterns_.end(); | |
| 134 |
2/2✓ Branch 1 taken 280 times.
✓ Branch 2 taken 238 times.
|
518 | for (; i != iend; ++i) { |
| 135 |
2/4✓ Branch 2 taken 280 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 280 times.
✗ Branch 6 not taken.
|
280 | result += (*i)->GenerateGlobString(); |
| 136 | } | ||
| 137 | 476 | return result; | |
| 138 | } | ||
| 139 | |||
| 140 | 752 | bool PathspecElementPattern::operator==( | |
| 141 | const PathspecElementPattern &other) const { | ||
| 142 | 752 | if (subpatterns_.size() != other.subpatterns_.size() | |
| 143 |
5/6✓ Branch 0 taken 702 times.
✓ Branch 1 taken 50 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 702 times.
✓ Branch 6 taken 50 times.
✓ Branch 7 taken 702 times.
|
752 | || IsValid() != other.IsValid()) { |
| 144 | 50 | return false; | |
| 145 | } | ||
| 146 | |||
| 147 | 702 | SubPatterns::const_iterator i = subpatterns_.begin(); | |
| 148 | 702 | const SubPatterns::const_iterator iend = subpatterns_.end(); | |
| 149 | 702 | SubPatterns::const_iterator j = other.subpatterns_.begin(); | |
| 150 | 702 | const SubPatterns::const_iterator jend = other.subpatterns_.end(); | |
| 151 | |||
| 152 |
5/6✓ Branch 3 taken 800 times.
✓ Branch 4 taken 471 times.
✓ Branch 6 taken 800 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 800 times.
✓ Branch 9 taken 471 times.
|
1271 | for (; i != iend && j != jend; ++i, ++j) { |
| 153 |
3/4✓ Branch 3 taken 800 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 231 times.
✓ Branch 6 taken 569 times.
|
800 | if (!(*i)->Compare(*j)) { |
| 154 | 231 | return false; | |
| 155 | } | ||
| 156 | } | ||
| 157 | |||
| 158 | 471 | return true; | |
| 159 | } | ||
| 160 | |||
| 161 | |||
| 162 | //------------------------------------------------------------------------------ | ||
| 163 | |||
| 164 | |||
| 165 | 15118 | void PathspecElementPattern::PlaintextSubPattern::AddChar(const char chr) { | |
| 166 | 15118 | chars_.push_back(chr); | |
| 167 | 15118 | } | |
| 168 | |||
| 169 | std::string | ||
| 170 | 2747 | PathspecElementPattern::PlaintextSubPattern::GenerateRegularExpression( | |
| 171 | const bool is_relaxed) const { | ||
| 172 | // Note: strict and relaxed regex are the same! | ||
| 173 | 2747 | std::string::const_iterator i = chars_.begin(); | |
| 174 | 2747 | const std::string::const_iterator iend = chars_.end(); | |
| 175 | 2747 | std::string regex; | |
| 176 |
2/2✓ Branch 2 taken 12460 times.
✓ Branch 3 taken 2747 times.
|
15207 | for (; i != iend; ++i) { |
| 177 |
2/2✓ Branch 2 taken 383 times.
✓ Branch 3 taken 12077 times.
|
12460 | if (IsSpecialRegexCharacter(*i)) { |
| 178 |
1/2✓ Branch 1 taken 383 times.
✗ Branch 2 not taken.
|
383 | regex += "\\"; |
| 179 | } | ||
| 180 |
1/2✓ Branch 2 taken 12460 times.
✗ Branch 3 not taken.
|
12460 | regex += *i; |
| 181 | } | ||
| 182 | 5494 | return regex; | |
| 183 | } | ||
| 184 | |||
| 185 | |||
| 186 | 252 | std::string PathspecElementPattern::PlaintextSubPattern::GenerateGlobString() | |
| 187 | const { | ||
| 188 | 252 | std::string::const_iterator i = chars_.begin(); | |
| 189 | 252 | const std::string::const_iterator iend = chars_.end(); | |
| 190 | 252 | std::string glob_string; | |
| 191 |
2/2✓ Branch 2 taken 1288 times.
✓ Branch 3 taken 252 times.
|
1540 | for (; i != iend; ++i) { |
| 192 |
2/2✓ Branch 2 taken 28 times.
✓ Branch 3 taken 1260 times.
|
1288 | if (Pathspec::IsSpecialChar(*i)) { |
| 193 |
1/2✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
|
28 | glob_string += "\\"; |
| 194 | } | ||
| 195 |
1/2✓ Branch 2 taken 1288 times.
✗ Branch 3 not taken.
|
1288 | glob_string += *i; |
| 196 | } | ||
| 197 | 504 | return glob_string; | |
| 198 | } | ||
| 199 | |||
| 200 | 12460 | bool PathspecElementPattern::PlaintextSubPattern::IsSpecialRegexCharacter( | |
| 201 | const char chr) const { | ||
| 202 |
8/8✓ Branch 0 taken 12194 times.
✓ Branch 1 taken 13 times.
✓ Branch 2 taken 12175 times.
✓ Branch 3 taken 19 times.
✓ Branch 4 taken 12149 times.
✓ Branch 5 taken 26 times.
✓ Branch 6 taken 12137 times.
✓ Branch 7 taken 12 times.
|
12207 | return (chr == '.' || chr == '\\' || chr == '*' || chr == '?' || chr == '[' |
| 203 |
10/10✓ Branch 0 taken 12125 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 12119 times.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 12113 times.
✓ Branch 5 taken 6 times.
✓ Branch 6 taken 12107 times.
✓ Branch 7 taken 6 times.
✓ Branch 8 taken 12101 times.
✓ Branch 9 taken 6 times.
|
12137 | || chr == ']' || chr == '(' || chr == ')' || chr == '{' || chr == '}' |
| 204 |
8/8✓ Branch 0 taken 12207 times.
✓ Branch 1 taken 253 times.
✓ Branch 2 taken 12095 times.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 12089 times.
✓ Branch 5 taken 6 times.
✓ Branch 6 taken 12 times.
✓ Branch 7 taken 12077 times.
|
24667 | || chr == '^' || chr == '$' || chr == '+'); |
| 205 | } | ||
| 206 | |||
| 207 | 643 | bool PathspecElementPattern::PlaintextSubPattern::Compare( | |
| 208 | const SubPattern *other) const { | ||
| 209 |
2/2✓ Branch 1 taken 7 times.
✓ Branch 2 taken 636 times.
|
643 | if (!other->IsPlaintext()) { |
| 210 | 7 | return false; | |
| 211 | } | ||
| 212 | |||
| 213 | const PlaintextSubPattern | ||
| 214 |
1/2✓ Branch 0 taken 636 times.
✗ Branch 1 not taken.
|
636 | *pt_other = dynamic_cast<const PlaintextSubPattern *>(other); |
| 215 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 636 times.
|
636 | assert(pt_other != NULL); |
| 216 | 636 | return chars_ == pt_other->chars_; | |
| 217 | } | ||
| 218 | |||
| 219 | |||
| 220 | std::string | ||
| 221 | 337 | PathspecElementPattern::WildcardSubPattern::GenerateRegularExpression( | |
| 222 | const bool is_relaxed) const { | ||
| 223 | return (is_relaxed) ? std::string(".*") | ||
| 224 |
14/27✓ Branch 0 taken 136 times.
✓ Branch 1 taken 201 times.
✓ Branch 4 taken 136 times.
✗ Branch 5 not taken.
✓ Branch 8 taken 201 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 201 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 201 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 201 times.
✓ Branch 17 taken 136 times.
✗ Branch 18 not taken.
✓ Branch 19 taken 201 times.
✓ Branch 20 taken 136 times.
✓ Branch 22 taken 201 times.
✓ Branch 23 taken 136 times.
✓ Branch 25 taken 136 times.
✓ Branch 26 taken 201 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.
|
674 | : std::string("[^") + Pathspec::kSeparator + "]*"; |
| 225 | } | ||
| 226 | |||
| 227 | |||
| 228 | 14 | std::string PathspecElementPattern::WildcardSubPattern::GenerateGlobString() | |
| 229 | const { | ||
| 230 |
1/2✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
|
14 | return "*"; |
| 231 | } | ||
| 232 | |||
| 233 | |||
| 234 | 73 | bool PathspecElementPattern::WildcardSubPattern::Compare( | |
| 235 | const SubPattern *other) const { | ||
| 236 | 73 | return other->IsWildcard(); | |
| 237 | } | ||
| 238 | |||
| 239 | |||
| 240 | std::string | ||
| 241 | 142 | PathspecElementPattern::PlaceholderSubPattern::GenerateRegularExpression( | |
| 242 | const bool is_relaxed) const { | ||
| 243 | // Note: strict and relaxed regex are the same! | ||
| 244 |
3/6✓ Branch 2 taken 142 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 142 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 142 times.
✗ Branch 9 not taken.
|
284 | return std::string("[^") + Pathspec::kSeparator + "]"; |
| 245 | } | ||
| 246 | |||
| 247 | 14 | std::string PathspecElementPattern::PlaceholderSubPattern::GenerateGlobString() | |
| 248 | const { | ||
| 249 |
1/2✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
|
14 | return "?"; |
| 250 | } | ||
| 251 | |||
| 252 | 84 | bool PathspecElementPattern::PlaceholderSubPattern::Compare( | |
| 253 | const SubPattern *other) const { | ||
| 254 | 84 | return other->IsPlaceholder(); | |
| 255 | } | ||
| 256 |