GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/path_filters/dirtab.cc
Date: 2026-10-25 02:40:14
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 2692 Dirtab::Dirtab() : valid_(true) { }
19
20
21 49 bool Dirtab::Open(const std::string &dirtab_path) {
22
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 49 times.
49 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 49 FILE *dirtab_file = fopen(dirtab_path.c_str(), "r");
30
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49 times.
49 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 49 valid_ = Parse(dirtab_file);
40 49 fclose(dirtab_file);
41 49 return valid_;
42 }
43
44 1632 bool Dirtab::Parse(const std::string &dirtab) {
45 1632 valid_ = true;
46 1632 off_t line_offset = 0;
47
2/2
✓ Branch 1 taken 4762 times.
✓ Branch 2 taken 1632 times.
6394 while (line_offset < static_cast<off_t>(dirtab.size())) {
48 9524 const std::string line = GetLineMem(dirtab.c_str() + line_offset,
49
1/2
✓ Branch 3 taken 4762 times.
✗ Branch 4 not taken.
4762 dirtab.size() - line_offset);
50 4762 line_offset += line.size() + 1; // +1 == skipped \n
51
3/4
✓ Branch 1 taken 4762 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 18 times.
✓ Branch 4 taken 4744 times.
4762 if (!ParseLine(line)) {
52 18 valid_ = false;
53 }
54 4762 }
55
4/4
✓ Branch 0 taken 1614 times.
✓ Branch 1 taken 18 times.
✓ Branch 3 taken 1605 times.
✓ Branch 4 taken 9 times.
1632 valid_ = valid_ && CheckRuleValidity();
56 1632 return valid_;
57 }
58
59
60 98 bool Dirtab::Parse(FILE *dirtab_file) {
61 98 valid_ = true;
62 98 std::string line;
63
3/4
✓ Branch 1 taken 196 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 98 times.
✓ Branch 4 taken 98 times.
196 while (GetLineFile(dirtab_file, &line)) {
64
2/4
✓ Branch 1 taken 98 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 98 times.
98 if (!ParseLine(line)) {
65 ✗ valid_ = false;
66 }
67 }
68
3/6
✓ Branch 0 taken 98 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 98 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 98 times.
✗ Branch 6 not taken.
98 valid_ = valid_ && CheckRuleValidity();
69 98 return valid_;
70 98 }
71
72
73 4860 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 4860 std::string::const_iterator itr = line.begin();
78 4860 const std::string::const_iterator iend = line.end();
79 4860 bool negation = false;
80
81 // parse preamble
82 4860 SkipWhitespace(iend, &itr);
83
2/2
✓ Branch 1 taken 741 times.
✓ Branch 2 taken 4119 times.
4860 if (*itr == Dirtab::kCommentMarker) {
84 741 return true;
85
2/2
✓ Branch 1 taken 864 times.
✓ Branch 2 taken 3255 times.
4119 } else if (*itr == Dirtab::kNegationMarker) {
86 864 negation = true;
87 864 ++itr;
88 864 SkipWhitespace(iend, &itr);
89 }
90
91 // extract and parse pathspec
92
2/4
✓ Branch 2 taken 4119 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 4119 times.
✗ Branch 6 not taken.
8238 const std::string pathspec_str(Trim(std::string(itr, iend)));
93
1/2
✓ Branch 1 taken 4119 times.
✗ Branch 2 not taken.
4119 return this->ParsePathspec(pathspec_str, negation);
94 4119 }
95
96 4945 bool Dirtab::ParsePathspec(const std::string &pathspec_str, bool negation) {
97
2/2
✓ Branch 1 taken 696 times.
✓ Branch 2 taken 4249 times.
4945 if (pathspec_str.empty()) {
98 696 return true;
99 }
100
1/2
✓ Branch 1 taken 4249 times.
✗ Branch 2 not taken.
4249 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 4240 times.
✓ Branch 2 taken 9 times.
✓ Branch 3 taken 3385 times.
✓ Branch 4 taken 855 times.
✓ Branch 6 taken 9 times.
✓ Branch 7 taken 3376 times.
✓ Branch 8 taken 18 times.
✓ Branch 9 taken 4231 times.
4249 if (!pathspec.IsValid() || (!negation && !pathspec.IsAbsolute())) {
105 18 return false;
106 }
107
108 // create a new dirtab rule
109
1/2
✓ Branch 1 taken 4231 times.
✗ Branch 2 not taken.
4231 const Rule rule(pathspec, negation);
110
1/2
✓ Branch 1 taken 4231 times.
✗ Branch 2 not taken.
4231 AddRule(rule);
111 4231 return true;
112 4249 }
113
114
115 4231 void Dirtab::AddRule(const Rule &rule) {
116
2/2
✓ Branch 0 taken 855 times.
✓ Branch 1 taken 3376 times.
4231 if (rule.is_negation) {
117 855 negative_rules_.push_back(rule);
118 } else {
119 3376 positive_rules_.push_back(rule);
120 }
121 4231 }
122
123
124 1712 bool Dirtab::CheckRuleValidity() const {
125 // check if there are contradicting positive and negative rules
126 1712 Rules::const_iterator p = positive_rules_.begin();
127 1712 const Rules::const_iterator pend = positive_rules_.end();
128
2/2
✓ Branch 2 taken 3367 times.
✓ Branch 3 taken 1703 times.
5070 for (; p != pend; ++p) {
129
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3367 times.
3367 assert(!p->is_negation);
130 3367 Rules::const_iterator n = negative_rules_.begin();
131 3367 const Rules::const_iterator nend = negative_rules_.end();
132
2/2
✓ Branch 2 taken 2021 times.
✓ Branch 3 taken 3358 times.
5379 for (; n != nend; ++n) {
133
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2021 times.
2021 assert(n->is_negation);
134
3/4
✓ Branch 3 taken 2021 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 9 times.
✓ Branch 6 taken 2012 times.
2021 if (p->pathspec == n->pathspec) {
135 9 return false;
136 }
137 }
138 }
139
140 1703 return true;
141 }
142
143
144 12271 bool Dirtab::IsMatching(const std::string &path) const {
145 // check if path has a positive match
146 12271 bool has_positive_match = false;
147 12271 Rules::const_iterator p = positive_rules_.begin();
148 12271 const Rules::const_iterator pend = positive_rules_.end();
149
2/2
✓ Branch 2 taken 21090 times.
✓ Branch 3 taken 7832 times.
28922 for (; p != pend; ++p) {
150
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 21090 times.
21090 assert(!p->is_negation);
151
3/4
✓ Branch 2 taken 21090 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4439 times.
✓ Branch 5 taken 16651 times.
21090 if (p->pathspec.IsMatching(path)) {
152 4439 has_positive_match = true;
153 4439 break;
154 }
155 }
156
157
5/6
✓ Branch 0 taken 4439 times.
✓ Branch 1 taken 7832 times.
✓ Branch 3 taken 4439 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 4376 times.
✓ Branch 6 taken 63 times.
12271 return has_positive_match && !IsOpposing(path);
158 }
159
160
161 24071 bool Dirtab::IsOpposing(const std::string &path) const {
162 24071 Rules::const_iterator n = negative_rules_.begin();
163 24071 const Rules::const_iterator nend = negative_rules_.end();
164
2/2
✓ Branch 2 taken 13112 times.
✓ Branch 3 taken 23251 times.
36363 for (; n != nend; ++n) {
165
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 13112 times.
13112 assert(n->is_negation);
166
3/4
✓ Branch 2 taken 13112 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 820 times.
✓ Branch 5 taken 12292 times.
13112 if (n->pathspec.IsMatchingRelaxed(path)) {
167 820 return true;
168 }
169 }
170
171 23251 return false;
172 }
173
174 } // namespace catalog
175