GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/path_filters/dirtab.cc
Date: 2026-04-19 02:41:37
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 390 Dirtab::Dirtab() : valid_(true) { }
19
20
21 32 bool Dirtab::Open(const std::string &dirtab_path) {
22
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
32 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 32 FILE *dirtab_file = fopen(dirtab_path.c_str(), "r");
30
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 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 32 valid_ = Parse(dirtab_file);
40 32 fclose(dirtab_file);
41 32 return valid_;
42 }
43
44 262 bool Dirtab::Parse(const std::string &dirtab) {
45 262 valid_ = true;
46 262 off_t line_offset = 0;
47
2/2
✓ Branch 1 taken 1334 times.
✓ Branch 2 taken 262 times.
1596 while (line_offset < static_cast<off_t>(dirtab.size())) {
48 2668 const std::string line = GetLineMem(dirtab.c_str() + line_offset,
49
1/2
✓ Branch 3 taken 1334 times.
✗ Branch 4 not taken.
1334 dirtab.size() - line_offset);
50 1334 line_offset += line.size() + 1; // +1 == skipped \n
51
3/4
✓ Branch 1 taken 1334 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 10 times.
✓ Branch 4 taken 1324 times.
1334 if (!ParseLine(line)) {
52 10 valid_ = false;
53 }
54 1334 }
55
4/4
✓ Branch 0 taken 252 times.
✓ Branch 1 taken 10 times.
✓ Branch 3 taken 247 times.
✓ Branch 4 taken 5 times.
262 valid_ = valid_ && CheckRuleValidity();
56 262 return valid_;
57 }
58
59
60 64 bool Dirtab::Parse(FILE *dirtab_file) {
61 64 valid_ = true;
62 64 std::string line;
63
3/4
✓ Branch 1 taken 128 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 64 times.
✓ Branch 4 taken 64 times.
128 while (GetLineFile(dirtab_file, &line)) {
64
2/4
✓ Branch 1 taken 64 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 64 times.
64 if (!ParseLine(line)) {
65 valid_ = false;
66 }
67 }
68
3/6
✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 64 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 64 times.
✗ Branch 6 not taken.
64 valid_ = valid_ && CheckRuleValidity();
69 64 return valid_;
70 64 }
71
72
73 1398 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 1398 std::string::const_iterator itr = line.begin();
78 1398 const std::string::const_iterator iend = line.end();
79 1398 bool negation = false;
80
81 // parse preamble
82 1398 SkipWhitespace(iend, &itr);
83
2/2
✓ Branch 1 taken 469 times.
✓ Branch 2 taken 929 times.
1398 if (*itr == Dirtab::kCommentMarker) {
84 469 return true;
85
2/2
✓ Branch 1 taken 410 times.
✓ Branch 2 taken 519 times.
929 } else if (*itr == Dirtab::kNegationMarker) {
86 410 negation = true;
87 410 ++itr;
88 410 SkipWhitespace(iend, &itr);
89 }
90
91 // extract and parse pathspec
92
2/4
✓ Branch 2 taken 929 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 929 times.
✗ Branch 6 not taken.
1858 const std::string pathspec_str(Trim(std::string(itr, iend)));
93
1/2
✓ Branch 1 taken 929 times.
✗ Branch 2 not taken.
929 return this->ParsePathspec(pathspec_str, negation);
94 929 }
95
96 1249 bool Dirtab::ParsePathspec(const std::string &pathspec_str, bool negation) {
97
2/2
✓ Branch 1 taken 50 times.
✓ Branch 2 taken 1199 times.
1249 if (pathspec_str.empty()) {
98 50 return true;
99 }
100
1/2
✓ Branch 1 taken 1199 times.
✗ Branch 2 not taken.
1199 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 1194 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 789 times.
✓ Branch 4 taken 405 times.
✓ Branch 6 taken 5 times.
✓ Branch 7 taken 784 times.
✓ Branch 8 taken 10 times.
✓ Branch 9 taken 1189 times.
1199 if (!pathspec.IsValid() || (!negation && !pathspec.IsAbsolute())) {
105 10 return false;
106 }
107
108 // create a new dirtab rule
109
1/2
✓ Branch 1 taken 1189 times.
✗ Branch 2 not taken.
1189 const Rule rule(pathspec, negation);
110
1/2
✓ Branch 1 taken 1189 times.
✗ Branch 2 not taken.
1189 AddRule(rule);
111 1189 return true;
112 1199 }
113
114
115 1189 void Dirtab::AddRule(const Rule &rule) {
116
2/2
✓ Branch 0 taken 405 times.
✓ Branch 1 taken 784 times.
1189 if (rule.is_negation) {
117 405 negative_rules_.push_back(rule);
118 } else {
119 784 positive_rules_.push_back(rule);
120 }
121 1189 }
122
123
124 316 bool Dirtab::CheckRuleValidity() const {
125 // check if there are contradicting positive and negative rules
126 316 Rules::const_iterator p = positive_rules_.begin();
127 316 const Rules::const_iterator pend = positive_rules_.end();
128
2/2
✓ Branch 2 taken 779 times.
✓ Branch 3 taken 311 times.
1090 for (; p != pend; ++p) {
129
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 779 times.
779 assert(!p->is_negation);
130 779 Rules::const_iterator n = negative_rules_.begin();
131 779 const Rules::const_iterator nend = negative_rules_.end();
132
2/2
✓ Branch 2 taken 1058 times.
✓ Branch 3 taken 774 times.
1832 for (; n != nend; ++n) {
133
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1058 times.
1058 assert(n->is_negation);
134
3/4
✓ Branch 3 taken 1058 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 5 times.
✓ Branch 6 taken 1053 times.
1058 if (p->pathspec == n->pathspec) {
135 5 return false;
136 }
137 }
138 }
139
140 311 return true;
141 }
142
143
144 1738 bool Dirtab::IsMatching(const std::string &path) const {
145 // check if path has a positive match
146 1738 bool has_positive_match = false;
147 1738 Rules::const_iterator p = positive_rules_.begin();
148 1738 const Rules::const_iterator pend = positive_rules_.end();
149
2/2
✓ Branch 2 taken 4272 times.
✓ Branch 3 taken 983 times.
5255 for (; p != pend; ++p) {
150
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4272 times.
4272 assert(!p->is_negation);
151
3/4
✓ Branch 2 taken 4272 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 755 times.
✓ Branch 5 taken 3517 times.
4272 if (p->pathspec.IsMatching(path)) {
152 755 has_positive_match = true;
153 755 break;
154 }
155 }
156
157
5/6
✓ Branch 0 taken 755 times.
✓ Branch 1 taken 983 times.
✓ Branch 3 taken 755 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 720 times.
✓ Branch 6 taken 35 times.
1738 return has_positive_match && !IsOpposing(path);
158 }
159
160
161 3684 bool Dirtab::IsOpposing(const std::string &path) const {
162 3684 Rules::const_iterator n = negative_rules_.begin();
163 3684 const Rules::const_iterator nend = negative_rules_.end();
164
2/2
✓ Branch 2 taken 5289 times.
✓ Branch 3 taken 3382 times.
8671 for (; n != nend; ++n) {
165
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5289 times.
5289 assert(n->is_negation);
166
3/4
✓ Branch 2 taken 5289 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 302 times.
✓ Branch 5 taken 4987 times.
5289 if (n->pathspec.IsMatchingRelaxed(path)) {
167 302 return true;
168 }
169 }
170
171 3382 return false;
172 }
173
174 } // namespace catalog
175