GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/swissknife_ingest.cc
Date: 2024-04-21 02:33:16
Exec Total Coverage
Lines: 0 123 0.0%
Branches: 0 66 0.0%

Line Branch Exec Source
1 /**
2 * This file is part of the CernVM File System
3 */
4
5 #include "swissknife_ingest.h"
6
7 #include <fcntl.h>
8 #include <unistd.h>
9
10 #include "catalog_virtual.h"
11 #include "manifest.h"
12 #include "statistics.h"
13 #include "statistics_database.h"
14 #include "swissknife_capabilities.h"
15 #include "sync_mediator.h"
16 #include "sync_union.h"
17 #include "sync_union_tarball.h"
18 #include "util/logging.h"
19 #include "util/pointer.h"
20 #include "util/posix.h"
21
22 /*
23 * Many of the options possible to set in the ArgumentList are not actually used
24 * by the ingest command since they are not part of its interface, hence those
25 * unused options cannot be set by the shell script. Of course if there is the
26 * necessitty those parameters can be added and managed.
27 * At the moment this approach worked fine and didn't add much complexity,
28 * however if yet another command will need to use a similar approach it would
29 * be good to consider creating different options handler for each command.
30 */
31 int swissknife::Ingest::Main(const swissknife::ArgumentList &args) {
32 std::string start_time = GetGMTimestamp();
33
34 SyncParameters params;
35 params.dir_rdonly = MakeCanonicalPath(*args.find('c')->second);
36 params.dir_temp = MakeCanonicalPath(*args.find('t')->second);
37 params.base_hash = shash::MkFromHexPtr(shash::HexPtr(*args.find('b')->second),
38 shash::kSuffixCatalog);
39 params.stratum0 = *args.find('w')->second;
40 params.manifest_path = *args.find('o')->second;
41 params.spooler_definition = *args.find('r')->second;
42
43 params.public_keys = *args.find('K')->second;
44 params.repo_name = *args.find('N')->second;
45
46 if (args.find('T') != args.end()) {
47 params.tar_file = *args.find('T')->second;
48 }
49 if (args.find('B') != args.end()) {
50 params.base_directory = *args.find('B')->second;
51 }
52 if (args.find('D') != args.end()) {
53 params.to_delete = *args.find('D')->second;
54 }
55
56 if (args.find('O') != args.end()) {
57 params.generate_legacy_bulk_chunks = true;
58 }
59 shash::Algorithms hash_algorithm = shash::kSha1;
60 if (args.find('e') != args.end()) {
61 hash_algorithm = shash::ParseHashAlgorithm(*args.find('e')->second);
62 if (hash_algorithm == shash::kAny) {
63 PrintError("unknown hash algorithm");
64 return 1;
65 }
66 }
67 if (args.find('Z') != args.end()) {
68 params.compression_alg =
69 zlib::ParseCompressionAlgorithm(*args.find('Z')->second);
70 }
71 if (args.find('U') != args.end()) {
72 params.uid = static_cast<uid_t>(String2Int64(*args.find('U')->second));
73 }
74 if (args.find('G') != args.end()) {
75 params.gid = static_cast<gid_t>(String2Int64(*args.find('G')->second));
76 }
77
78 bool create_catalog = args.find('C') != args.end();
79
80 params.nested_kcatalog_limit = SyncParameters::kDefaultNestedKcatalogLimit;
81 params.root_kcatalog_limit = SyncParameters::kDefaultRootKcatalogLimit;
82 params.file_mbyte_limit = SyncParameters::kDefaultFileMbyteLimit;
83
84 params.branched_catalog = false; // could be true?
85
86 if (args.find('P') != args.end()) {
87 params.session_token_file = *args.find('P')->second;
88 }
89
90 if (args.find('H') != args.end()) {
91 params.key_file = *args.find('H')->second;
92 }
93
94 const bool upload_statsdb = (args.count('I') > 0);
95
96 perf::StatisticsTemplate publish_statistics("publish", this->statistics());
97 StatisticsDatabase *stats_db =
98 StatisticsDatabase::OpenStandardDB(params.repo_name);
99
100 upload::SpoolerDefinition spooler_definition(
101 params.spooler_definition, hash_algorithm, params.compression_alg,
102 params.generate_legacy_bulk_chunks, params.use_file_chunking,
103 params.min_file_chunk_size, params.avg_file_chunk_size,
104 params.max_file_chunk_size, params.session_token_file, params.key_file);
105 if (params.max_concurrent_write_jobs > 0) {
106 spooler_definition.number_of_concurrent_uploads =
107 params.max_concurrent_write_jobs;
108 }
109
110 // Sanitize base_directory, removing any leading or trailing slashes
111 // from non-root (!= "/") paths
112 params.base_directory = TrimString(params.base_directory, "/", kTrimAll);
113
114 upload::SpoolerDefinition spooler_definition_catalogs(
115 spooler_definition.Dup2DefaultCompression());
116
117 params.spooler = upload::Spooler::Construct(spooler_definition,
118 &publish_statistics);
119 if (NULL == params.spooler) return 3;
120 UniquePtr<upload::Spooler> spooler_catalogs(
121 upload::Spooler::Construct(spooler_definition_catalogs,
122 &publish_statistics));
123 if (!spooler_catalogs.IsValid()) return 3;
124
125 const bool follow_redirects = (args.count('L') > 0);
126 const string proxy = (args.count('@') > 0) ? *args.find('@')->second : "";
127 if (!InitDownloadManager(follow_redirects, proxy)) {
128 return 3;
129 }
130
131 if (!InitVerifyingSignatureManager(params.public_keys)) {
132 return 3;
133 }
134
135 bool with_gateway =
136 spooler_definition.driver_type == upload::SpoolerDefinition::Gateway;
137
138 // This may fail, in which case a warning is printed and the process continues
139 ObtainDacReadSearchCapability();
140
141 UniquePtr<manifest::Manifest> manifest;
142 if (params.branched_catalog) {
143 // Throw-away manifest
144 manifest = new manifest::Manifest(shash::Any(), 0, "");
145 } else {
146 if (with_gateway) {
147 manifest =
148 FetchRemoteManifest(params.stratum0, params.repo_name, shash::Any());
149 } else {
150 manifest = FetchRemoteManifest(params.stratum0, params.repo_name,
151 params.base_hash);
152 }
153 }
154 if (!manifest.IsValid()) {
155 return 3;
156 }
157
158 const std::string old_root_hash = manifest->catalog_hash().ToString(true);
159
160 catalog::WritableCatalogManager catalog_manager(
161 params.base_hash, params.stratum0, params.dir_temp,
162 spooler_catalogs.weak_ref(),
163 download_manager(), params.enforce_limits, params.nested_kcatalog_limit,
164 params.root_kcatalog_limit, params.file_mbyte_limit, statistics(),
165 params.is_balanced, params.max_weight, params.min_weight);
166 catalog_manager.Init();
167
168 publish::SyncMediator mediator(&catalog_manager, &params, publish_statistics);
169 LogCvmfs(kLogPublish, kLogStdout, "Processing changes...");
170
171 publish::SyncUnion *sync = new publish::SyncUnionTarball(
172 &mediator, params.dir_rdonly, params.tar_file, params.base_directory,
173 params.uid, params.gid, params.to_delete, create_catalog);
174
175 if (!sync->Initialize()) {
176 LogCvmfs(kLogCvmfs, kLogStderr,
177 "Initialization of the synchronisation "
178 "engine failed");
179 return 4;
180 }
181
182 sync->Traverse();
183
184 if (!params.authz_file.empty()) {
185 LogCvmfs(kLogCvmfs, kLogDebug,
186 "Adding contents of authz file %s to"
187 " root catalog.",
188 params.authz_file.c_str());
189 int fd = open(params.authz_file.c_str(), O_RDONLY);
190 if (fd == -1) {
191 LogCvmfs(kLogCvmfs, kLogStderr,
192 "Unable to open authz file (%s)"
193 "from the publication process: %s",
194 params.authz_file.c_str(), strerror(errno));
195 return 7;
196 }
197
198 std::string new_authz;
199 const bool read_successful = SafeReadToString(fd, &new_authz);
200 close(fd);
201
202 if (!read_successful) {
203 LogCvmfs(kLogCvmfs, kLogStderr, "Failed to read authz file (%s): %s",
204 params.authz_file.c_str(), strerror(errno));
205 return 8;
206 }
207
208 catalog_manager.SetVOMSAuthz(new_authz);
209 }
210
211 if (!mediator.Commit(manifest.weak_ref())) {
212 PrintError("something went wrong during sync");
213 stats_db->StorePublishStatistics(this->statistics(), start_time, false);
214 if (upload_statsdb) {
215 stats_db->UploadStatistics(params.spooler);
216 }
217 return 5;
218 }
219
220 perf::Counter *revision_counter = statistics()->Register("publish.revision",
221 "Published revision number");
222 revision_counter->Set(catalog_manager.GetRootCatalog()->revision());
223
224 // finalize the spooler
225 LogCvmfs(kLogCvmfs, kLogStdout, "Wait for all uploads to finish");
226 params.spooler->WaitForUpload();
227 spooler_catalogs->WaitForUpload();
228 params.spooler->FinalizeSession(false);
229
230 LogCvmfs(kLogCvmfs, kLogStdout, "Exporting repository manifest");
231
232 // We call FinalizeSession(true) this time, to also trigger the commit
233 // operation on the gateway machine (if the upstream is of type "gw").
234
235 // Get the path of the new root catalog
236 const std::string new_root_hash = manifest->catalog_hash().ToString(true);
237
238 if (!spooler_catalogs->FinalizeSession(true, old_root_hash, new_root_hash,
239 params.repo_tag)) {
240 PrintError("Failed to commit the transaction.");
241 stats_db->StorePublishStatistics(this->statistics(), start_time, false);
242 if (upload_statsdb) {
243 stats_db->UploadStatistics(params.spooler);
244 }
245 return 9;
246 }
247
248 stats_db->StorePublishStatistics(this->statistics(), start_time, true);
249 if (upload_statsdb) {
250 stats_db->UploadStatistics(params.spooler);
251 }
252
253 delete params.spooler;
254
255 if (!manifest->Export(params.manifest_path)) {
256 PrintError("Failed to create new repository");
257 return 6;
258 }
259
260 return 0;
261 }
262