GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/path_filters/dirtab.cc
Date: 2026-11-08 02:41:18
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 1140 Dirtab::Dirtab() : valid_(true) { }
19
20
21 13 bool Dirtab::Open(const std::string &dirtab_path) {
22
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
13 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 13 FILE *dirtab_file = fopen(dirtab_path.c_str(), "r");
30
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 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 13 valid_ = Parse(dirtab_file);
40 13 fclose(dirtab_file);
41 13 return valid_;
42 }
43
44 782 bool Dirtab::Parse(const std::string &dirtab) {
45 782 valid_ = true;
46 782 off_t line_offset = 0;
47
2/2
✓ Branch 1 taken 2372 times.
✓ Branch 2 taken 782 times.
3154 while (line_offset < static_cast<off_t>(dirtab.size())) {
48 4744 const std::string line = GetLineMem(dirtab.c_str() + line_offset,
49
1/2
✓ Branch 3 taken 2372 times.
✗ Branch 4 not taken.
2372 dirtab.size() - line_offset);
50 2372 line_offset += line.size() + 1; // +1 == skipped \n
51
3/4
✓ Branch 1 taken 2372 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 26 times.
✓ Branch 4 taken 2346 times.
2372 if (!ParseLine(line)) {
52 26 valid_ = false;
53 }
54 2372 }
55
4/4
✓ Branch 0 taken 756 times.
✓ Branch 1 taken 26 times.
✓ Branch 3 taken 743 times.
✓ Branch 4 taken 13 times.
782 valid_ = valid_ && CheckRuleValidity();
56 782 return valid_;
57 }
58
59
60 26 bool Dirtab::Parse(FILE *dirtab_file) {
61 26 valid_ = true;
62 26 std::string line;
63
3/4
✓ Branch 1 taken 52 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 26 times.
✓ Branch 4 taken 26 times.
52 while (GetLineFile(dirtab_file, &line)) {
64
2/4
✓ Branch 1 taken 26 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 26 times.
26 if (!ParseLine(line)) {
65 ✗ valid_ = false;
66 }
67 }
68
3/6
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 26 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 26 times.
✗ Branch 6 not taken.
26 valid_ = valid_ && CheckRuleValidity();
69 26 return valid_;
70 26 }
71
72
73 2398 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 2398 std::string::const_iterator itr = line.begin();
78 2398 const std::string::const_iterator iend = line.end();
79 2398 bool negation = false;
80
81 // parse preamble
82 2398 SkipWhitespace(iend, &itr);
83
2/2
✓ Branch 1 taken 377 times.
✓ Branch 2 taken 2021 times.
2398 if (*itr == Dirtab::kCommentMarker) {
84 377 return true;
85
2/2
✓ Branch 1 taken 470 times.
✓ Branch 2 taken 1551 times.
2021 } else if (*itr == Dirtab::kNegationMarker) {
86 470 negation = true;
87 470 ++itr;
88 470 SkipWhitespace(iend, &itr);
89 }
90
91 // extract and parse pathspec
92
2/4
✓ Branch 2 taken 2021 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 2021 times.
✗ Branch 6 not taken.
4042 const std::string pathspec_str(Trim(std::string(itr, iend)));
93
1/2
✓ Branch 1 taken 2021 times.
✗ Branch 2 not taken.
2021 return this->ParsePathspec(pathspec_str, negation);
94 2021 }
95
96 2270 bool Dirtab::ParsePathspec(const std::string &pathspec_str, bool negation) {
97
2/2
✓ Branch 1 taken 391 times.
✓ Branch 2 taken 1879 times.
2270 if (pathspec_str.empty()) {
98 391 return true;
99 }
100
1/2
✓ Branch 1 taken 1879 times.
✗ Branch 2 not taken.
1879 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 1866 times.
✓ Branch 2 taken 13 times.
✓ Branch 3 taken 1409 times.
✓ Branch 4 taken 457 times.
✓ Branch 6 taken 13 times.
✓ Branch 7 taken 1396 times.
✓ Branch 8 taken 26 times.
✓ Branch 9 taken 1853 times.
1879 if (!pathspec.IsValid() || (!negation && !pathspec.IsAbsolute())) {
105 26 return false;
106 }
107
108 // create a new dirtab rule
109
1/2
✓ Branch 1 taken 1853 times.
✗ Branch 2 not taken.
1853 const Rule rule(pathspec, negation);
110
1/2
✓ Branch 1 taken 1853 times.
✗ Branch 2 not taken.
1853 AddRule(rule);
111 1853 return true;
112 1879 }
113
114
115 1853 void Dirtab::AddRule(const Rule &rule) {
116
2/2
✓ Branch 0 taken 457 times.
✓ Branch 1 taken 1396 times.
1853 if (rule.is_negation) {
117 457 negative_rules_.push_back(rule);
118 } else {
119 1396 positive_rules_.push_back(rule);
120 }
121 1853 }
122
123
124 782 bool Dirtab::CheckRuleValidity() const {
125 // check if there are contradicting positive and negative rules
126 782 Rules::const_iterator p = positive_rules_.begin();
127 782 const Rules::const_iterator pend = positive_rules_.end();
128
2/2
✓ Branch 2 taken 1383 times.
✓ Branch 3 taken 769 times.
2152 for (; p != pend; ++p) {
129
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1383 times.
1383 assert(!p->is_negation);
130 1383 Rules::const_iterator n = negative_rules_.begin();
131 1383 const Rules::const_iterator nend = negative_rules_.end();
132
2/2
✓ Branch 2 taken 891 times.
✓ Branch 3 taken 1370 times.
2261 for (; n != nend; ++n) {
133
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 891 times.
891 assert(n->is_negation);
134
3/4
✓ Branch 3 taken 891 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 13 times.
✓ Branch 6 taken 878 times.
891 if (p->pathspec == n->pathspec) {
135 13 return false;
136 }
137 }
138 }
139
140 769 return true;
141 }
142
143
144 6414 bool Dirtab::IsMatching(const std::string &path) const {
145 // check if path has a positive match
146 6414 bool has_positive_match = false;
147 6414 Rules::const_iterator p = positive_rules_.begin();
148 6414 const Rules::const_iterator pend = positive_rules_.end();
149
2/2
✓ Branch 2 taken 9896 times.
✓ Branch 3 taken 4054 times.
13950 for (; p != pend; ++p) {
150
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9896 times.
9896 assert(!p->is_negation);
151
3/4
✓ Branch 2 taken 9896 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2360 times.
✓ Branch 5 taken 7536 times.
9896 if (p->pathspec.IsMatching(path)) {
152 2360 has_positive_match = true;
153 2360 break;
154 }
155 }
156
157
5/6
✓ Branch 0 taken 2360 times.
✓ Branch 1 taken 4054 times.
✓ Branch 3 taken 2360 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 2269 times.
✓ Branch 6 taken 91 times.
6414 return has_positive_match && !IsOpposing(path);
158 }
159
160
161 12094 bool Dirtab::IsOpposing(const std::string &path) const {
162 12094 Rules::const_iterator n = negative_rules_.begin();
163 12094 const Rules::const_iterator nend = negative_rules_.end();
164
2/2
✓ Branch 2 taken 6203 times.
✓ Branch 3 taken 11552 times.
17755 for (; n != nend; ++n) {
165
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6203 times.
6203 assert(n->is_negation);
166
3/4
✓ Branch 2 taken 6203 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 542 times.
✓ Branch 5 taken 5661 times.
6203 if (n->pathspec.IsMatchingRelaxed(path)) {
167 542 return true;
168 }
169 }
170
171 11552 return false;
172 }
173
174 } // namespace catalog
175