| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/pathspec/pathspec_pattern.cc |
| Date: | 2025-10-26 02:35:25 |
| 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 | 10324 | PathspecElementPattern::PathspecElementPattern( | |
| 13 | const std::string::const_iterator begin, | ||
| 14 | 10324 | const std::string::const_iterator &end) | |
| 15 | 10324 | : valid_(true) { | |
| 16 |
1/2✓ Branch 1 taken 10324 times.
✗ Branch 2 not taken.
|
10324 | Parse(begin, end); |
| 17 | 10324 | } | |
| 18 | |||
| 19 | 32443 | PathspecElementPattern::PathspecElementPattern( | |
| 20 | 32443 | const PathspecElementPattern &other) | |
| 21 | 32443 | : valid_(other.valid_) { | |
| 22 |
1/2✓ Branch 2 taken 32443 times.
✗ Branch 3 not taken.
|
32443 | subpatterns_.reserve(other.subpatterns_.size()); |
| 23 | 32443 | SubPatterns::const_iterator i = other.subpatterns_.begin(); | |
| 24 | 32443 | const SubPatterns::const_iterator iend = other.subpatterns_.end(); | |
| 25 |
2/2✓ Branch 1 taken 35998 times.
✓ Branch 2 taken 32443 times.
|
68441 | for (; i != iend; ++i) { |
| 26 |
2/4✓ Branch 2 taken 35998 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 35998 times.
✗ Branch 6 not taken.
|
35998 | subpatterns_.push_back((*i)->Clone()); |
| 27 | } | ||
| 28 | 32443 | } | |
| 29 | |||
| 30 | 161 | PathspecElementPattern &PathspecElementPattern::operator=( | |
| 31 | const PathspecElementPattern &other) { | ||
| 32 |
1/2✓ Branch 0 taken 161 times.
✗ Branch 1 not taken.
|
161 | if (this != &other) { |
| 33 | 161 | valid_ = other.valid_; | |
| 34 | 161 | subpatterns_.clear(); | |
| 35 |
1/2✓ Branch 2 taken 161 times.
✗ Branch 3 not taken.
|
161 | subpatterns_.reserve(other.subpatterns_.size()); |
| 36 | 161 | SubPatterns::const_iterator i = other.subpatterns_.begin(); | |
| 37 | 161 | const SubPatterns::const_iterator iend = other.subpatterns_.end(); | |
| 38 |
2/2✓ Branch 1 taken 253 times.
✓ Branch 2 taken 161 times.
|
414 | for (; i != iend; ++i) { |
| 39 |
2/4✓ Branch 2 taken 253 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 253 times.
✗ Branch 6 not taken.
|
253 | subpatterns_.push_back((*i)->Clone()); |
| 40 | } | ||
| 41 | } | ||
| 42 | |||
| 43 | 161 | return *this; | |
| 44 | } | ||
| 45 | |||
| 46 | |||
| 47 | 42761 | PathspecElementPattern::~PathspecElementPattern() { | |
| 48 | 42761 | SubPatterns::const_iterator i = subpatterns_.begin(); | |
| 49 | 42761 | const SubPatterns::const_iterator iend = subpatterns_.end(); | |
| 50 |
2/2✓ Branch 2 taken 48204 times.
✓ Branch 3 taken 42761 times.
|
90965 | for (; i != iend; ++i) { |
| 51 |
1/2✓ Branch 1 taken 48204 times.
✗ Branch 2 not taken.
|
48204 | delete *i; |
| 52 | } | ||
| 53 | 42761 | subpatterns_.clear(); | |
| 54 | 42761 | } | |
| 55 | |||
| 56 | |||
| 57 | 10324 | void PathspecElementPattern::Parse(const std::string::const_iterator &begin, | |
| 58 | const std::string::const_iterator &end) { | ||
| 59 | 10324 | std::string::const_iterator i = begin; | |
| 60 |
2/2✓ Branch 1 taken 12167 times.
✓ Branch 2 taken 10324 times.
|
22491 | while (i != end) { |
| 61 |
3/4✓ Branch 2 taken 2931 times.
✓ Branch 3 taken 9236 times.
✓ Branch 5 taken 2931 times.
✗ Branch 6 not taken.
|
12167 | SubPattern *next = (Pathspec::IsSpecialChar(*i)) ? ParseSpecialChar(end, &i) |
| 62 |
1/2✓ Branch 1 taken 9236 times.
✗ Branch 2 not taken.
|
9236 | : ParsePlaintext(end, &i); |
| 63 |
3/4✓ Branch 1 taken 12167 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 47 times.
✓ Branch 4 taken 12120 times.
|
12167 | if (next->IsEmpty()) { |
| 64 | 47 | valid_ = false; | |
| 65 |
1/2✓ Branch 0 taken 47 times.
✗ Branch 1 not taken.
|
47 | delete next; |
| 66 | } else { | ||
| 67 |
1/2✓ Branch 1 taken 12120 times.
✗ Branch 2 not taken.
|
12120 | subpatterns_.push_back(next); |
| 68 | } | ||
| 69 | } | ||
| 70 | 10324 | } | |
| 71 | |||
| 72 | |||
| 73 | 9236 | PathspecElementPattern::SubPattern *PathspecElementPattern::ParsePlaintext( | |
| 74 | const std::string::const_iterator &end, std::string::const_iterator *i) { | ||
| 75 | 9236 | PlaintextSubPattern *pattern = new PlaintextSubPattern(); | |
| 76 | 9236 | bool next_is_escaped = false; | |
| 77 | |||
| 78 |
2/2✓ Branch 1 taken 39127 times.
✓ Branch 2 taken 8533 times.
|
47660 | while (*i < end) { |
| 79 |
6/6✓ Branch 2 taken 1006 times.
✓ Branch 3 taken 38121 times.
✓ Branch 4 taken 703 times.
✓ Branch 5 taken 303 times.
✓ Branch 6 taken 703 times.
✓ Branch 7 taken 38424 times.
|
39127 | if (Pathspec::IsSpecialChar(**i) && !next_is_escaped) { |
| 80 | 703 | break; | |
| 81 | } | ||
| 82 | |||
| 83 |
6/6✓ Branch 1 taken 559 times.
✓ Branch 2 taken 37865 times.
✓ Branch 3 taken 466 times.
✓ Branch 4 taken 93 times.
✓ Branch 5 taken 466 times.
✓ Branch 6 taken 37958 times.
|
38424 | if (**i == Pathspec::kEscaper && !next_is_escaped) { |
| 84 | 466 | next_is_escaped = true; | |
| 85 |
2/2✓ Branch 0 taken 652 times.
✓ Branch 1 taken 37306 times.
|
37958 | } else if (next_is_escaped) { |
| 86 |
6/6✓ Branch 2 taken 349 times.
✓ Branch 3 taken 303 times.
✓ Branch 5 taken 93 times.
✓ Branch 6 taken 256 times.
✓ Branch 7 taken 396 times.
✓ Branch 8 taken 256 times.
|
652 | if (Pathspec::IsSpecialChar(**i) || **i == Pathspec::kEscaper) { |
| 87 | 396 | pattern->AddChar(**i); | |
| 88 | 396 | next_is_escaped = false; | |
| 89 | } else { | ||
| 90 | 256 | valid_ = false; | |
| 91 | } | ||
| 92 | } else { | ||
| 93 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 37306 times.
|
37306 | assert(!Pathspec::IsSpecialChar(**i)); |
| 94 | 37306 | pattern->AddChar(**i); | |
| 95 | } | ||
| 96 | |||
| 97 | 38424 | ++(*i); | |
| 98 | } | ||
| 99 | |||
| 100 | 9236 | return pattern; | |
| 101 | } | ||
| 102 | |||
| 103 | 2931 | 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 2931 times.
|
2931 | assert(Pathspec::IsSpecialChar(**i)); |
| 106 | 2931 | const char chr = **i; | |
| 107 | 2931 | ++(*i); | |
| 108 | |||
| 109 |
2/3✓ Branch 0 taken 1827 times.
✓ Branch 1 taken 1104 times.
✗ Branch 2 not taken.
|
2931 | switch (chr) { |
| 110 | 1827 | case Pathspec::kWildcard: | |
| 111 | 1827 | return new WildcardSubPattern(); | |
| 112 | 1104 | case Pathspec::kPlaceholder: | |
| 113 | 1104 | return new PlaceholderSubPattern(); | |
| 114 | ✗ | default: | |
| 115 | ✗ | assert(false && "unrecognized special character"); | |
| 116 | } | ||
| 117 | } | ||
| 118 | |||
| 119 | 5945 | std::string PathspecElementPattern::GenerateRegularExpression( | |
| 120 | const bool is_relaxed) const { | ||
| 121 | 5945 | std::string result; | |
| 122 | 5945 | SubPatterns::const_iterator i = subpatterns_.begin(); | |
| 123 | 5945 | const SubPatterns::const_iterator iend = subpatterns_.end(); | |
| 124 |
2/2✓ Branch 1 taken 7378 times.
✓ Branch 2 taken 5945 times.
|
13323 | for (; i != iend; ++i) { |
| 125 |
2/4✓ Branch 2 taken 7378 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 7378 times.
✗ Branch 6 not taken.
|
7378 | result += (*i)->GenerateRegularExpression(is_relaxed); |
| 126 | } | ||
| 127 | 11890 | return result; | |
| 128 | } | ||
| 129 | |||
| 130 | 782 | std::string PathspecElementPattern::GenerateGlobString() const { | |
| 131 | 782 | std::string result; | |
| 132 | 782 | SubPatterns::const_iterator i = subpatterns_.begin(); | |
| 133 | 782 | const SubPatterns::const_iterator iend = subpatterns_.end(); | |
| 134 |
2/2✓ Branch 1 taken 920 times.
✓ Branch 2 taken 782 times.
|
1702 | for (; i != iend; ++i) { |
| 135 |
2/4✓ Branch 2 taken 920 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 920 times.
✗ Branch 6 not taken.
|
920 | result += (*i)->GenerateGlobString(); |
| 136 | } | ||
| 137 | 1564 | return result; | |
| 138 | } | ||
| 139 | |||
| 140 | 2946 | bool PathspecElementPattern::operator==( | |
| 141 | const PathspecElementPattern &other) const { | ||
| 142 | 2946 | if (subpatterns_.size() != other.subpatterns_.size() | |
| 143 |
5/6✓ Branch 0 taken 2620 times.
✓ Branch 1 taken 326 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 2620 times.
✓ Branch 6 taken 326 times.
✓ Branch 7 taken 2620 times.
|
2946 | || IsValid() != other.IsValid()) { |
| 144 | 326 | return false; | |
| 145 | } | ||
| 146 | |||
| 147 | 2620 | SubPatterns::const_iterator i = subpatterns_.begin(); | |
| 148 | 2620 | const SubPatterns::const_iterator iend = subpatterns_.end(); | |
| 149 | 2620 | SubPatterns::const_iterator j = other.subpatterns_.begin(); | |
| 150 | 2620 | const SubPatterns::const_iterator jend = other.subpatterns_.end(); | |
| 151 | |||
| 152 |
5/6✓ Branch 3 taken 2942 times.
✓ Branch 4 taken 1938 times.
✓ Branch 6 taken 2942 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2942 times.
✓ Branch 9 taken 1938 times.
|
4880 | for (; i != iend && j != jend; ++i, ++j) { |
| 153 |
3/4✓ Branch 3 taken 2942 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 682 times.
✓ Branch 6 taken 2260 times.
|
2942 | if (!(*i)->Compare(*j)) { |
| 154 | 682 | return false; | |
| 155 | } | ||
| 156 | } | ||
| 157 | |||
| 158 | 1938 | return true; | |
| 159 | } | ||
| 160 | |||
| 161 | |||
| 162 | //------------------------------------------------------------------------------ | ||
| 163 | |||
| 164 | |||
| 165 | 37702 | void PathspecElementPattern::PlaintextSubPattern::AddChar(const char chr) { | |
| 166 | 37702 | chars_.push_back(chr); | |
| 167 | 37702 | } | |
| 168 | |||
| 169 | std::string | ||
| 170 | 5672 | PathspecElementPattern::PlaintextSubPattern::GenerateRegularExpression( | |
| 171 | const bool is_relaxed) const { | ||
| 172 | // Note: strict and relaxed regex are the same! | ||
| 173 | 5672 | std::string::const_iterator i = chars_.begin(); | |
| 174 | 5672 | const std::string::const_iterator iend = chars_.end(); | |
| 175 | 5672 | std::string regex; | |
| 176 |
2/2✓ Branch 2 taken 24285 times.
✓ Branch 3 taken 5672 times.
|
29957 | for (; i != iend; ++i) { |
| 177 |
2/2✓ Branch 2 taken 1450 times.
✓ Branch 3 taken 22835 times.
|
24285 | if (IsSpecialRegexCharacter(*i)) { |
| 178 |
1/2✓ Branch 1 taken 1450 times.
✗ Branch 2 not taken.
|
1450 | regex += "\\"; |
| 179 | } | ||
| 180 |
1/2✓ Branch 2 taken 24285 times.
✗ Branch 3 not taken.
|
24285 | regex += *i; |
| 181 | } | ||
| 182 | 11344 | return regex; | |
| 183 | } | ||
| 184 | |||
| 185 | |||
| 186 | 828 | std::string PathspecElementPattern::PlaintextSubPattern::GenerateGlobString() | |
| 187 | const { | ||
| 188 | 828 | std::string::const_iterator i = chars_.begin(); | |
| 189 | 828 | const std::string::const_iterator iend = chars_.end(); | |
| 190 | 828 | std::string glob_string; | |
| 191 |
2/2✓ Branch 2 taken 4232 times.
✓ Branch 3 taken 828 times.
|
5060 | for (; i != iend; ++i) { |
| 192 |
2/2✓ Branch 2 taken 92 times.
✓ Branch 3 taken 4140 times.
|
4232 | if (Pathspec::IsSpecialChar(*i)) { |
| 193 |
1/2✓ Branch 1 taken 92 times.
✗ Branch 2 not taken.
|
92 | glob_string += "\\"; |
| 194 | } | ||
| 195 |
1/2✓ Branch 2 taken 4232 times.
✗ Branch 3 not taken.
|
4232 | glob_string += *i; |
| 196 | } | ||
| 197 | 1656 | return glob_string; | |
| 198 | } | ||
| 199 | |||
| 200 | 24285 | bool PathspecElementPattern::PlaintextSubPattern::IsSpecialRegexCharacter( | |
| 201 | const char chr) const { | ||
| 202 |
8/8✓ Branch 0 taken 23288 times.
✓ Branch 1 taken 47 times.
✓ Branch 2 taken 23217 times.
✓ Branch 3 taken 71 times.
✓ Branch 4 taken 23123 times.
✓ Branch 5 taken 94 times.
✓ Branch 6 taken 23075 times.
✓ Branch 7 taken 48 times.
|
23335 | return (chr == '.' || chr == '\\' || chr == '*' || chr == '?' || chr == '[' |
| 203 |
10/10✓ Branch 0 taken 23027 times.
✓ Branch 1 taken 48 times.
✓ Branch 2 taken 23003 times.
✓ Branch 3 taken 24 times.
✓ Branch 4 taken 22979 times.
✓ Branch 5 taken 24 times.
✓ Branch 6 taken 22955 times.
✓ Branch 7 taken 24 times.
✓ Branch 8 taken 22931 times.
✓ Branch 9 taken 24 times.
|
23075 | || chr == ']' || chr == '(' || chr == ')' || chr == '{' || chr == '}' |
| 204 |
8/8✓ Branch 0 taken 23335 times.
✓ Branch 1 taken 950 times.
✓ Branch 2 taken 22907 times.
✓ Branch 3 taken 24 times.
✓ Branch 4 taken 22883 times.
✓ Branch 5 taken 24 times.
✓ Branch 6 taken 48 times.
✓ Branch 7 taken 22835 times.
|
47620 | || chr == '^' || chr == '$' || chr == '+'); |
| 205 | } | ||
| 206 | |||
| 207 | 2224 | bool PathspecElementPattern::PlaintextSubPattern::Compare( | |
| 208 | const SubPattern *other) const { | ||
| 209 |
2/2✓ Branch 1 taken 23 times.
✓ Branch 2 taken 2201 times.
|
2224 | if (!other->IsPlaintext()) { |
| 210 | 23 | return false; | |
| 211 | } | ||
| 212 | |||
| 213 | const PlaintextSubPattern | ||
| 214 |
1/2✓ Branch 0 taken 2201 times.
✗ Branch 1 not taken.
|
2201 | *pt_other = dynamic_cast<const PlaintextSubPattern *>(other); |
| 215 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2201 times.
|
2201 | assert(pt_other != NULL); |
| 216 | 2201 | return chars_ == pt_other->chars_; | |
| 217 | } | ||
| 218 | |||
| 219 | |||
| 220 | std::string | ||
| 221 | 1193 | PathspecElementPattern::WildcardSubPattern::GenerateRegularExpression( | |
| 222 | const bool is_relaxed) const { | ||
| 223 | return (is_relaxed) ? std::string(".*") | ||
| 224 |
14/27✓ Branch 0 taken 433 times.
✓ Branch 1 taken 760 times.
✓ Branch 4 taken 433 times.
✗ Branch 5 not taken.
✓ Branch 8 taken 760 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 760 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 760 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 760 times.
✓ Branch 17 taken 433 times.
✗ Branch 18 not taken.
✓ Branch 19 taken 760 times.
✓ Branch 20 taken 433 times.
✓ Branch 22 taken 760 times.
✓ Branch 23 taken 433 times.
✓ Branch 25 taken 433 times.
✓ Branch 26 taken 760 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.
|
2386 | : std::string("[^") + Pathspec::kSeparator + "]*"; |
| 225 | } | ||
| 226 | |||
| 227 | |||
| 228 | 46 | std::string PathspecElementPattern::WildcardSubPattern::GenerateGlobString() | |
| 229 | const { | ||
| 230 |
1/2✓ Branch 2 taken 46 times.
✗ Branch 3 not taken.
|
46 | return "*"; |
| 231 | } | ||
| 232 | |||
| 233 | |||
| 234 | 442 | bool PathspecElementPattern::WildcardSubPattern::Compare( | |
| 235 | const SubPattern *other) const { | ||
| 236 | 442 | return other->IsWildcard(); | |
| 237 | } | ||
| 238 | |||
| 239 | |||
| 240 | std::string | ||
| 241 | 513 | PathspecElementPattern::PlaceholderSubPattern::GenerateRegularExpression( | |
| 242 | const bool is_relaxed) const { | ||
| 243 | // Note: strict and relaxed regex are the same! | ||
| 244 |
3/6✓ Branch 2 taken 513 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 513 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 513 times.
✗ Branch 9 not taken.
|
1026 | return std::string("[^") + Pathspec::kSeparator + "]"; |
| 245 | } | ||
| 246 | |||
| 247 | 46 | std::string PathspecElementPattern::PlaceholderSubPattern::GenerateGlobString() | |
| 248 | const { | ||
| 249 |
1/2✓ Branch 2 taken 46 times.
✗ Branch 3 not taken.
|
46 | return "?"; |
| 250 | } | ||
| 251 | |||
| 252 | 276 | bool PathspecElementPattern::PlaceholderSubPattern::Compare( | |
| 253 | const SubPattern *other) const { | ||
| 254 | 276 | return other->IsPlaceholder(); | |
| 255 | } | ||
| 256 |