GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/swissknife_scrub.cc
Date: 2024-04-21 02:33:16
Exec Total Coverage
Lines: 0 118 0.0%
Branches: 0 80 0.0%

Line Branch Exec Source
1 /**
2 * This file is part of the CernVM File System.
3 */
4
5 #define __STDC_FORMAT_MACROS
6
7 #include "swissknife_scrub.h"
8 #include "cvmfs_config.h"
9
10 #include "util/fs_traversal.h"
11 #include "util/logging.h"
12 #include "util/posix.h"
13 #include "util/smalloc.h"
14 #include "util/string.h"
15
16 using namespace std; // NOLINT
17
18 namespace swissknife {
19
20 const size_t kHashSubtreeLength = 2;
21 const std::string kTxnDirectoryName = "txn";
22
23 CommandScrub::CommandScrub()
24 : machine_readable_output_(false)
25 , alerts_(0)
26 {
27 int retval = pthread_mutex_init(&alerts_mutex_, NULL);
28 assert(retval == 0);
29 }
30
31
32 CommandScrub::~CommandScrub() {
33 pthread_mutex_destroy(&alerts_mutex_);
34 }
35
36
37 swissknife::ParameterList CommandScrub::GetParams() const {
38 swissknife::ParameterList r;
39 r.push_back(Parameter::Mandatory('r', "repository directory"));
40 r.push_back(Parameter::Switch('m', "machine readable output"));
41 return r;
42 }
43
44 const char *CommandScrub::Alerts::ToString(const CommandScrub::Alerts::Type t) {
45 switch (t) {
46 case Alerts::kUnexpectedFile:
47 return "unexpected regular file";
48 case Alerts::kUnexpectedSymlink:
49 return "unexpected symlink";
50 case Alerts::kUnexpectedSubdir:
51 return "unexpected subdir in CAS subdir";
52 case Alerts::kUnexpectedModifier:
53 return "unknown object modifier";
54 case Alerts::kMalformedHash:
55 return "malformed content hash";
56 case Alerts::kMalformedCasSubdir:
57 return "malformed CAS subdir length";
58 case Alerts::kContentHashMismatch:
59 return "mismatch of file name and content hash";
60 default:
61 return "unknown alert";
62 }
63 }
64
65 void CommandScrub::FileCallback(
66 const std::string &relative_path,
67 const std::string &file_name)
68 {
69 assert(!file_name.empty());
70
71 if (relative_path.empty()) {
72 PrintAlert(Alerts::kUnexpectedFile, repo_path_ + "/" + file_name);
73 return;
74 }
75 if (relative_path == kTxnDirectoryName) {
76 // transaction directory should be ignored
77 return;
78 }
79
80 const string full_path = MakeFullPath(relative_path, file_name);
81 const std::string hash_string =
82 CheckPathAndExtractHash(relative_path, file_name, full_path);
83 if (hash_string.empty()) {
84 return;
85 }
86
87 if (!shash::HexPtr(hash_string).IsValid()) {
88 PrintAlert(Alerts::kMalformedHash, full_path, hash_string);
89 return;
90 }
91
92 shash::Any hash_from_name =
93 shash::MkFromSuffixedHexPtr(shash::HexPtr(hash_string));
94 IngestionSource* full_path_source = new FileIngestionSource(full_path);
95 pipeline_scrubbing_.Process(
96 full_path_source,
97 hash_from_name.algorithm,
98 hash_from_name.suffix);
99 }
100
101
102 void CommandScrub::DirCallback(
103 const std::string &relative_path,
104 const std::string &dir_name)
105 {
106 const string full_path = MakeFullPath(relative_path, dir_name);
107
108 // The directory "/srv/cvmfs/<REPO_NAME>/data/txn/receiver" is whitelisted
109 if (HasSuffix(full_path, "data/txn/receiver", false)) {
110 return;
111 }
112
113 // Check for nested subdirs
114 if (relative_path.size() > 0) {
115 PrintAlert(Alerts::kUnexpectedSubdir, full_path);
116 return;
117 }
118
119 // Check CAS hash subdirectory name length
120 if (!dir_name.empty() && dir_name.size() != kHashSubtreeLength &&
121 dir_name != kTxnDirectoryName) {
122 PrintAlert(Alerts::kMalformedCasSubdir, full_path);
123 }
124 }
125
126 void CommandScrub::SymlinkCallback(const std::string &relative_path,
127 const std::string &symlink_name) {
128 const string full_path = MakeFullPath(relative_path, symlink_name);
129 PrintAlert(Alerts::kUnexpectedSymlink, full_path);
130 }
131
132 void CommandScrub::OnFileHashed(const ScrubbingResult &scrubbing_result) {
133 const string full_path = scrubbing_result.path;
134 const string file_name = GetFileName(full_path);
135 const string parent_path = GetParentPath(full_path);
136 const string relative_path = MakeRelativePath(parent_path);
137 assert(!file_name.empty());
138
139 const std::string hash_string =
140 CheckPathAndExtractHash(relative_path, file_name, full_path);
141 assert(!hash_string.empty());
142 assert(shash::HexPtr(hash_string).IsValid());
143
144
145 if (scrubbing_result.hash !=
146 shash::MkFromSuffixedHexPtr(shash::HexPtr(hash_string)))
147 {
148 PrintAlert(Alerts::kContentHashMismatch, full_path,
149 scrubbing_result.hash.ToString());
150 }
151 }
152
153 std::string CommandScrub::CheckPathAndExtractHash(
154 const std::string &relative_path,
155 const std::string &file_name,
156 const std::string &full_path) const
157 {
158 // check for a valid object modifier on the end of the file name
159 const char last_character = *(file_name.end() - 1);
160 bool has_object_modifier = false;
161 if (std::isupper(last_character)) {
162 has_object_modifier = true;
163 }
164 if (has_object_modifier && last_character != shash::kSuffixHistory &&
165 last_character != shash::kSuffixCatalog &&
166 last_character != shash::kSuffixPartial &&
167 last_character != shash::kSuffixCertificate &&
168 last_character != shash::kSuffixMicroCatalog &&
169 last_character != shash::kSuffixMetainfo) {
170 PrintAlert(Alerts::kUnexpectedModifier, full_path);
171 return "";
172 }
173
174 const string hash_string =
175 GetFileName(GetParentPath(full_path)) +
176 (has_object_modifier ? file_name.substr(0, file_name.length() - 1)
177 : file_name);
178 return hash_string;
179 }
180
181
182 int CommandScrub::Main(const swissknife::ArgumentList &args) {
183 repo_path_ = MakeCanonicalPath(*args.find('r')->second);
184 machine_readable_output_ = (args.find('m') != args.end());
185
186 pipeline_scrubbing_.RegisterListener(&CommandScrub::OnFileHashed, this);
187 pipeline_scrubbing_.Spawn();
188
189 // initialize file system recursion engine
190 FileSystemTraversal<CommandScrub> traverser(this, repo_path_, true);
191 traverser.fn_new_file = &CommandScrub::FileCallback;
192 traverser.fn_enter_dir = &CommandScrub::DirCallback;
193 traverser.fn_new_symlink = &CommandScrub::SymlinkCallback;
194 traverser.Recurse(repo_path_);
195
196 // wait for reader to finish all jobs
197 pipeline_scrubbing_.WaitFor();
198
199 return (alerts_ == 0) ? 0 : 1;
200 }
201
202 void CommandScrub::PrintAlert(
203 const Alerts::Type type,
204 const std::string &path,
205 const std::string &affected_hash) const
206 {
207 MutexLockGuard l(alerts_mutex_);
208
209 const char *msg = Alerts::ToString(type);
210 if (machine_readable_output_) {
211 LogCvmfs(kLogUtility, kLogStderr, "%d %s %s", type,
212 ((affected_hash.empty()) ? "-" : affected_hash.c_str()),
213 path.c_str());
214 } else {
215 LogCvmfs(kLogUtility, kLogStderr, "%s | at: %s", msg, path.c_str());
216 }
217
218 ++alerts_;
219 }
220
221 std::string CommandScrub::MakeFullPath(const std::string &relative_path,
222 const std::string &file_name) const {
223 return (relative_path.empty())
224 ? repo_path_ + "/" + file_name
225 : repo_path_ + "/" + relative_path + "/" + file_name;
226 }
227
228 std::string CommandScrub::MakeRelativePath(const std::string &full_path) {
229 assert(HasPrefix(full_path, repo_path_ + "/", false));
230 return full_path.substr(repo_path_.length() + 1);
231 }
232
233 void CommandScrub::ShowAlertsHelpMessage() const {
234 LogCvmfs(kLogUtility, kLogStdout, "to come...");
235 }
236
237 } // namespace swissknife
238