| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/path_filters/dirtab.cc |
| Date: | 2026-03-22 02:40:38 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 87 | 95 | 91.6% |
| Branches: | 68 | 92 | 73.9% |
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** | ||
| 2 | * This file is part of the CernVM File System. | ||
| 3 | */ | ||
| 4 | |||
| 5 | #include "dirtab.h" | ||
| 6 | |||
| 7 | #include <cassert> | ||
| 8 | #include <cerrno> | ||
| 9 | #include <cstdio> | ||
| 10 | #include <cstdlib> | ||
| 11 | |||
| 12 | #include "util/logging.h" | ||
| 13 | #include "util/posix.h" | ||
| 14 | #include "util/string.h" | ||
| 15 | |||
| 16 | namespace catalog { | ||
| 17 | |||
| 18 | 268 | Dirtab::Dirtab() : valid_(true) { } | |
| 19 | |||
| 20 | |||
| 21 | 3 | bool Dirtab::Open(const std::string &dirtab_path) { | |
| 22 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
|
3 | if (!FileExists(dirtab_path)) { |
| 23 | ✗ | LogCvmfs(kLogCatalog, kLogStderr, "Cannot find dirtab at '%s'", | |
| 24 | dirtab_path.c_str()); | ||
| 25 | ✗ | valid_ = false; | |
| 26 | ✗ | return valid_; | |
| 27 | } | ||
| 28 | |||
| 29 | 3 | FILE *dirtab_file = fopen(dirtab_path.c_str(), "r"); | |
| 30 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (dirtab_file == NULL) { |
| 31 | ✗ | LogCvmfs(kLogCatalog, kLogStderr, | |
| 32 | "Cannot open dirtab for reading at '%s' " | ||
| 33 | "(errno: %d)", | ||
| 34 | ✗ | dirtab_path.c_str(), errno); | |
| 35 | ✗ | valid_ = false; | |
| 36 | ✗ | return valid_; | |
| 37 | } | ||
| 38 | |||
| 39 | 3 | valid_ = Parse(dirtab_file); | |
| 40 | 3 | fclose(dirtab_file); | |
| 41 | 3 | return valid_; | |
| 42 | } | ||
| 43 | |||
| 44 | 256 | bool Dirtab::Parse(const std::string &dirtab) { | |
| 45 | 256 | valid_ = true; | |
| 46 | 256 | off_t line_offset = 0; | |
| 47 |
2/2✓ Branch 1 taken 1150 times.
✓ Branch 2 taken 256 times.
|
1406 | while (line_offset < static_cast<off_t>(dirtab.size())) { |
| 48 | 2300 | const std::string line = GetLineMem(dirtab.c_str() + line_offset, | |
| 49 |
1/2✓ Branch 3 taken 1150 times.
✗ Branch 4 not taken.
|
1150 | dirtab.size() - line_offset); |
| 50 | 1150 | line_offset += line.size() + 1; // +1 == skipped \n | |
| 51 |
3/4✓ Branch 1 taken 1150 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 34 times.
✓ Branch 4 taken 1116 times.
|
1150 | if (!ParseLine(line)) { |
| 52 | 34 | valid_ = false; | |
| 53 | } | ||
| 54 | 1150 | } | |
| 55 |
4/4✓ Branch 0 taken 222 times.
✓ Branch 1 taken 34 times.
✓ Branch 3 taken 205 times.
✓ Branch 4 taken 17 times.
|
256 | valid_ = valid_ && CheckRuleValidity(); |
| 56 | 256 | return valid_; | |
| 57 | } | ||
| 58 | |||
| 59 | |||
| 60 | 6 | bool Dirtab::Parse(FILE *dirtab_file) { | |
| 61 | 6 | valid_ = true; | |
| 62 | 6 | std::string line; | |
| 63 |
3/4✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 6 times.
|
12 | while (GetLineFile(dirtab_file, &line)) { |
| 64 |
2/4✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
|
6 | if (!ParseLine(line)) { |
| 65 | ✗ | valid_ = false; | |
| 66 | } | ||
| 67 | } | ||
| 68 |
3/6✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 6 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 6 times.
✗ Branch 6 not taken.
|
6 | valid_ = valid_ && CheckRuleValidity(); |
| 69 | 6 | return valid_; | |
| 70 | 6 | } | |
| 71 | |||
| 72 | |||
| 73 | 1156 | bool Dirtab::ParseLine(const std::string &line) { | |
| 74 | // line parsing is done using std::string iterators. Each parsing method ex- | ||
| 75 | // pects an iterator and the end iterator. While parsing itr is constantly | ||
| 76 | // incremented to walk through the given .cvmfsdirtab line. | ||
| 77 | 1156 | std::string::const_iterator itr = line.begin(); | |
| 78 | 1156 | const std::string::const_iterator iend = line.end(); | |
| 79 | 1156 | bool negation = false; | |
| 80 | |||
| 81 | // parse preamble | ||
| 82 | 1156 | SkipWhitespace(iend, &itr); | |
| 83 |
2/2✓ Branch 1 taken 325 times.
✓ Branch 2 taken 831 times.
|
1156 | if (*itr == Dirtab::kCommentMarker) { |
| 84 | 325 | return true; | |
| 85 |
2/2✓ Branch 1 taken 336 times.
✓ Branch 2 taken 495 times.
|
831 | } else if (*itr == Dirtab::kNegationMarker) { |
| 86 | 336 | negation = true; | |
| 87 | 336 | ++itr; | |
| 88 | 336 | SkipWhitespace(iend, &itr); | |
| 89 | } | ||
| 90 | |||
| 91 | // extract and parse pathspec | ||
| 92 |
2/4✓ Branch 2 taken 831 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 831 times.
✗ Branch 6 not taken.
|
1662 | const std::string pathspec_str(Trim(std::string(itr, iend))); |
| 93 |
1/2✓ Branch 1 taken 831 times.
✗ Branch 2 not taken.
|
831 | return this->ParsePathspec(pathspec_str, negation); |
| 94 | 831 | } | |
| 95 | |||
| 96 | 861 | bool Dirtab::ParsePathspec(const std::string &pathspec_str, bool negation) { | |
| 97 |
2/2✓ Branch 1 taken 170 times.
✓ Branch 2 taken 691 times.
|
861 | if (pathspec_str.empty()) { |
| 98 | 170 | return true; | |
| 99 | } | ||
| 100 |
1/2✓ Branch 1 taken 691 times.
✗ Branch 2 not taken.
|
691 | const Pathspec pathspec(pathspec_str); |
| 101 | |||
| 102 | // all generated Pathspecs need to be valid and positive rules must be | ||
| 103 | // absolute. Otherwise the .cvmfsdirtab is not valid. | ||
| 104 |
8/8✓ Branch 1 taken 674 times.
✓ Branch 2 taken 17 times.
✓ Branch 3 taken 355 times.
✓ Branch 4 taken 319 times.
✓ Branch 6 taken 17 times.
✓ Branch 7 taken 338 times.
✓ Branch 8 taken 34 times.
✓ Branch 9 taken 657 times.
|
691 | if (!pathspec.IsValid() || (!negation && !pathspec.IsAbsolute())) { |
| 105 | 34 | return false; | |
| 106 | } | ||
| 107 | |||
| 108 | // create a new dirtab rule | ||
| 109 |
1/2✓ Branch 1 taken 657 times.
✗ Branch 2 not taken.
|
657 | const Rule rule(pathspec, negation); |
| 110 |
1/2✓ Branch 1 taken 657 times.
✗ Branch 2 not taken.
|
657 | AddRule(rule); |
| 111 | 657 | return true; | |
| 112 | 691 | } | |
| 113 | |||
| 114 | |||
| 115 | 657 | void Dirtab::AddRule(const Rule &rule) { | |
| 116 |
2/2✓ Branch 0 taken 319 times.
✓ Branch 1 taken 338 times.
|
657 | if (rule.is_negation) { |
| 117 | 319 | negative_rules_.push_back(rule); | |
| 118 | } else { | ||
| 119 | 338 | positive_rules_.push_back(rule); | |
| 120 | } | ||
| 121 | 657 | } | |
| 122 | |||
| 123 | |||
| 124 | 228 | bool Dirtab::CheckRuleValidity() const { | |
| 125 | // check if there are contradicting positive and negative rules | ||
| 126 | 228 | Rules::const_iterator p = positive_rules_.begin(); | |
| 127 | 228 | const Rules::const_iterator pend = positive_rules_.end(); | |
| 128 |
2/2✓ Branch 2 taken 321 times.
✓ Branch 3 taken 211 times.
|
532 | for (; p != pend; ++p) { |
| 129 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 321 times.
|
321 | assert(!p->is_negation); |
| 130 | 321 | Rules::const_iterator n = negative_rules_.begin(); | |
| 131 | 321 | const Rules::const_iterator nend = negative_rules_.end(); | |
| 132 |
2/2✓ Branch 2 taken 529 times.
✓ Branch 3 taken 304 times.
|
833 | for (; n != nend; ++n) { |
| 133 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 529 times.
|
529 | assert(n->is_negation); |
| 134 |
3/4✓ Branch 3 taken 529 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 17 times.
✓ Branch 6 taken 512 times.
|
529 | if (p->pathspec == n->pathspec) { |
| 135 | 17 | return false; | |
| 136 | } | ||
| 137 | } | ||
| 138 | } | ||
| 139 | |||
| 140 | 211 | return true; | |
| 141 | } | ||
| 142 | |||
| 143 | |||
| 144 | 725 | bool Dirtab::IsMatching(const std::string &path) const { | |
| 145 | // check if path has a positive match | ||
| 146 | 725 | bool has_positive_match = false; | |
| 147 | 725 | Rules::const_iterator p = positive_rules_.begin(); | |
| 148 | 725 | const Rules::const_iterator pend = positive_rules_.end(); | |
| 149 |
2/2✓ Branch 2 taken 1194 times.
✓ Branch 3 taken 274 times.
|
1468 | for (; p != pend; ++p) { |
| 150 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1194 times.
|
1194 | assert(!p->is_negation); |
| 151 |
3/4✓ Branch 2 taken 1194 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 451 times.
✓ Branch 5 taken 743 times.
|
1194 | if (p->pathspec.IsMatching(path)) { |
| 152 | 451 | has_positive_match = true; | |
| 153 | 451 | break; | |
| 154 | } | ||
| 155 | } | ||
| 156 | |||
| 157 |
5/6✓ Branch 0 taken 451 times.
✓ Branch 1 taken 274 times.
✓ Branch 3 taken 451 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 332 times.
✓ Branch 6 taken 119 times.
|
725 | return has_positive_match && !IsOpposing(path); |
| 158 | } | ||
| 159 | |||
| 160 | |||
| 161 | 1205 | bool Dirtab::IsOpposing(const std::string &path) const { | |
| 162 | 1205 | Rules::const_iterator n = negative_rules_.begin(); | |
| 163 | 1205 | const Rules::const_iterator nend = negative_rules_.end(); | |
| 164 |
2/2✓ Branch 2 taken 2430 times.
✓ Branch 3 taken 813 times.
|
3243 | for (; n != nend; ++n) { |
| 165 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2430 times.
|
2430 | assert(n->is_negation); |
| 166 |
3/4✓ Branch 2 taken 2430 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 392 times.
✓ Branch 5 taken 2038 times.
|
2430 | if (n->pathspec.IsMatchingRelaxed(path)) { |
| 167 | 392 | return true; | |
| 168 | } | ||
| 169 | } | ||
| 170 | |||
| 171 | 813 | return false; | |
| 172 | } | ||
| 173 | |||
| 174 | } // namespace catalog | ||
| 175 |