GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/swissknife_sync.cc
Date: 2026-04-19 02:41:37
Exec Total Coverage
Lines: 0 495 0.0%
Branches: 0 322 0.0%

Line Branch Exec Source
1 /**
2 * This file is part of the CernVM File System
3 *
4 * This tool figures out the changes made to a cvmfs repository by means
5 * of a union file system mounted on top of a cvmfs volume.
6 * We take all three volumes (namely union, overlay and repository) into
7 * account to sync the changes back into the repository.
8 *
9 * On the repository side we have a catalogs directory that mimics the
10 * shadow directory structure and stores compressed and uncompressed
11 * versions of all catalogs. The raw data are stored in the data
12 * subdirectory in zlib-compressed form. They are named with their SHA-1
13 * hash of the compressed file (like in CVMFS client cache, but with a
14 * 2-level cache hierarchy). Symlinks from the catalog directory to the
15 * data directory form the connection. If necessary, add a .htaccess file
16 * to allow Apache to follow the symlinks.
17 */
18
19 // NOLINTNEXTLINE
20 #define _FILE_OFFSET_BITS 64
21 // NOLINTNEXTLINE
22 #define __STDC_FORMAT_MACROS
23
24 #include "swissknife_sync.h"
25
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <glob.h>
29 #include <inttypes.h>
30 #include <limits.h>
31
32 #include <cstdio>
33 #include <cstdlib>
34 #include <string>
35 #include <vector>
36
37 #include "catalog_mgr_ro.h"
38 #include "catalog_mgr_rw.h"
39 #include "catalog_virtual.h"
40 #include "manifest.h"
41 #include "monitor.h"
42 #include "path_filters/dirtab.h"
43 #include "reflog.h"
44 #include "sanitizer.h"
45 #include "statistics.h"
46 #include "statistics_database.h"
47 #include "sync_mediator.h"
48 #include "sync_union.h"
49 #include "sync_union_aufs.h"
50 #include "sync_union_overlayfs.h"
51 #include "util/capabilities.h"
52 #include "util/logging.h"
53 #include "util/string.h"
54
55 using namespace std; // NOLINT
56
57 bool swissknife::CommandSync::CheckParams(const SyncParameters &p) {
58 if (!DirectoryExists(p.dir_scratch)) {
59 PrintError("overlay (copy on write) directory does not exist");
60 return false;
61 }
62 if (!DirectoryExists(p.dir_union)) {
63 PrintError("union volume does not exist");
64 return false;
65 }
66 if (!DirectoryExists(p.dir_rdonly)) {
67 PrintError("cvmfs read/only repository does not exist");
68 return false;
69 }
70 if (p.stratum0 == "") {
71 PrintError("Stratum0 url missing");
72 return false;
73 }
74
75 if (p.manifest_path == "") {
76 PrintError("manifest output required");
77 return false;
78 }
79 if (!DirectoryExists(p.dir_temp)) {
80 PrintError("data store directory does not exist");
81 return false;
82 }
83
84 if (p.min_file_chunk_size >= p.avg_file_chunk_size
85 || p.avg_file_chunk_size >= p.max_file_chunk_size) {
86 PrintError("file chunk size values are not sane");
87 return false;
88 }
89
90 if (HasPrefix(p.spooler_definition, "gw", false)) {
91 if (p.session_token_file.empty()) {
92 PrintError("Session token file has to be provided "
93 "when upstream type is gw.");
94 return false;
95 }
96 }
97
98 return true;
99 }
100
101 int swissknife::CommandCreate::Main(const swissknife::ArgumentList &args) {
102 const string manifest_path = *args.find('o')->second;
103 const string dir_temp = *args.find('t')->second;
104 const string spooler_definition = *args.find('r')->second;
105 const string repo_name = *args.find('n')->second;
106 const string reflog_chksum_path = *args.find('R')->second;
107 if (args.find('l') != args.end()) {
108 const unsigned log_level = kLogLevel0
109 << String2Uint64(*args.find('l')->second);
110 if (log_level > kLogNone) {
111 LogCvmfs(kLogCvmfs, kLogStderr, "invalid log level");
112 return 1;
113 }
114 SetLogVerbosity(static_cast<LogLevels>(log_level));
115 }
116 shash::Algorithms hash_algorithm = shash::kSha1;
117 if (args.find('a') != args.end()) {
118 hash_algorithm = shash::ParseHashAlgorithm(*args.find('a')->second);
119 if (hash_algorithm == shash::kAny) {
120 PrintError("unknown hash algorithm");
121 return 1;
122 }
123 }
124
125 const bool volatile_content = (args.count('v') > 0);
126 const bool garbage_collectable = (args.count('z') > 0);
127 std::string voms_authz;
128 if (args.find('V') != args.end()) {
129 voms_authz = *args.find('V')->second;
130 }
131
132 const upload::SpoolerDefinition sd(spooler_definition, hash_algorithm,
133 zlib::kZlibDefault);
134 const UniquePtr<upload::Spooler> spooler(upload::Spooler::Construct(sd));
135 assert(spooler.IsValid());
136
137 const UniquePtr<manifest::Manifest> manifest(
138 catalog::WritableCatalogManager::CreateRepository(
139 dir_temp, volatile_content, voms_authz, spooler.weak_ref()));
140 if (!manifest.IsValid()) {
141 PrintError("Swissknife Sync: Failed to create new repository");
142 return 1;
143 }
144
145 UniquePtr<manifest::Reflog> reflog(CreateEmptyReflog(dir_temp, repo_name));
146 if (!reflog.IsValid()) {
147 PrintError("Swissknife Sync: Failed to create fresh Reflog");
148 return 1;
149 }
150
151 reflog->DropDatabaseFileOwnership();
152 const string reflog_path = reflog->database_file();
153 reflog.Destroy();
154 shash::Any reflog_hash(hash_algorithm);
155 manifest::Reflog::HashDatabase(reflog_path, &reflog_hash);
156 spooler->UploadReflog(reflog_path);
157 spooler->WaitForUpload();
158 unlink(reflog_path.c_str());
159 if (spooler->GetNumberOfErrors()) {
160 LogCvmfs(kLogCvmfs, kLogStderr, "Swissknife Sync: Failed to upload reflog");
161 return 4;
162 }
163 assert(!reflog_chksum_path.empty());
164 manifest::Reflog::WriteChecksum(reflog_chksum_path, reflog_hash);
165
166 // set optional manifest fields
167 const bool needs_bootstrap_shortcuts = !voms_authz.empty();
168 manifest->set_garbage_collectability(garbage_collectable);
169 manifest->set_has_alt_catalog_path(needs_bootstrap_shortcuts);
170
171 if (!manifest->Export(manifest_path)) {
172 PrintError("Swissknife Sync: Failed to create new repository");
173 return 5;
174 }
175
176 return 0;
177 }
178
179 int swissknife::CommandUpload::Main(const swissknife::ArgumentList &args) {
180 const string source = *args.find('i')->second;
181 const string dest = *args.find('o')->second;
182 const string spooler_definition = *args.find('r')->second;
183 shash::Algorithms hash_algorithm = shash::kSha1;
184 if (args.find('a') != args.end()) {
185 hash_algorithm = shash::ParseHashAlgorithm(*args.find('a')->second);
186 if (hash_algorithm == shash::kAny) {
187 PrintError("Swissknife Sync: Unknown hash algorithm");
188 return 1;
189 }
190 }
191
192 const upload::SpoolerDefinition sd(spooler_definition, hash_algorithm);
193 upload::Spooler *spooler = upload::Spooler::Construct(sd);
194 assert(spooler);
195 spooler->Upload(source, dest);
196 spooler->WaitForUpload();
197
198 if (spooler->GetNumberOfErrors() > 0) {
199 LogCvmfs(kLogCatalog, kLogStderr, "Swissknife Sync: failed to upload %s",
200 source.c_str());
201 return 1;
202 }
203
204 delete spooler;
205
206 return 0;
207 }
208
209 int swissknife::CommandPeek::Main(const swissknife::ArgumentList &args) {
210 const string file_to_peek = *args.find('d')->second;
211 const string spooler_definition = *args.find('r')->second;
212
213 // Hash doesn't matter
214 const upload::SpoolerDefinition sd(spooler_definition, shash::kAny);
215 upload::Spooler *spooler = upload::Spooler::Construct(sd);
216 assert(spooler);
217 const bool success = spooler->Peek(file_to_peek);
218
219 if (spooler->GetNumberOfErrors() > 0) {
220 LogCvmfs(kLogCatalog, kLogStderr, "Swissknife Sync: failed to peek for %s",
221 file_to_peek.c_str());
222 return 2;
223 }
224 if (!success) {
225 LogCvmfs(kLogCatalog, kLogStdout, "Swissknife Sync: %s not found",
226 file_to_peek.c_str());
227 return 1;
228 }
229 LogCvmfs(kLogCatalog, kLogStdout, "Swissknife Sync: %s available",
230 file_to_peek.c_str());
231
232 delete spooler;
233
234 return 0;
235 }
236
237 int swissknife::CommandRemove::Main(const ArgumentList &args) {
238 const string file_to_delete = *args.find('o')->second;
239 const string spooler_definition = *args.find('r')->second;
240
241 // Hash doesn't matter
242 const upload::SpoolerDefinition sd(spooler_definition, shash::kAny);
243 upload::Spooler *spooler = upload::Spooler::Construct(sd);
244 assert(spooler);
245 spooler->RemoveAsync(file_to_delete);
246 spooler->WaitForUpload();
247
248 if (spooler->GetNumberOfErrors() > 0) {
249 LogCvmfs(kLogCatalog, kLogStderr, "Swissknife Sync: failed to delete %s",
250 file_to_delete.c_str());
251 return 1;
252 }
253
254 delete spooler;
255
256 return 0;
257 }
258
259 int swissknife::CommandApplyDirtab::Main(const ArgumentList &args) {
260 const string dirtab_file = *args.find('d')->second;
261 union_dir_ = MakeCanonicalPath(*args.find('u')->second);
262 scratch_dir_ = MakeCanonicalPath(*args.find('s')->second);
263 const shash::Any base_hash = shash::MkFromHexPtr(
264 shash::HexPtr(*args.find('b')->second), shash::kSuffixCatalog);
265 const string stratum0 = *args.find('w')->second;
266 const string dir_temp = *args.find('t')->second;
267 verbose_ = (args.find('x') != args.end());
268
269 // check if there is a dirtab file
270 if (!FileExists(dirtab_file)) {
271 LogCvmfs(kLogCatalog, kLogVerboseMsg,
272 "Swissknife Sync: Didn't find a dirtab at '%s'. Skipping...",
273 dirtab_file.c_str());
274 return 0;
275 }
276
277 // parse dirtab file
278 catalog::Dirtab *dirtab = catalog::Dirtab::Create(dirtab_file);
279 if (!dirtab->IsValid()) {
280 LogCvmfs(kLogCatalog, kLogStderr,
281 "Swissknife Sync: Invalid or not readable dirtab '%s'",
282 dirtab_file.c_str());
283 return 1;
284 }
285 LogCvmfs(kLogCatalog, kLogVerboseMsg,
286 "Swissknife Sync: Found %lu rules in dirtab '%s'",
287 dirtab->RuleCount(), dirtab_file.c_str());
288
289 // initialize catalog infrastructure
290 const bool auto_manage_catalog_files = true;
291 const bool follow_redirects = (args.count('L') > 0);
292 const string proxy = (args.count('@') > 0) ? *args.find('@')->second : "";
293 if (!InitDownloadManager(follow_redirects, proxy)) {
294 return 1;
295 }
296 catalog::SimpleCatalogManager catalog_manager(
297 base_hash, stratum0, dir_temp, download_manager(), statistics(),
298 auto_manage_catalog_files);
299 catalog_manager.Init();
300
301 vector<string> new_nested_catalogs;
302 DetermineNestedCatalogCandidates(*dirtab, &catalog_manager,
303 &new_nested_catalogs);
304 const bool success = CreateCatalogMarkers(new_nested_catalogs);
305 delete dirtab;
306
307 return (success) ? 0 : 1;
308 }
309
310
311 namespace {
312
313 // Overwrite directory traversal in the globbing in order to avoid breaking out
314 // the repository tree
315
316 std::string *g_glob_uniondir = NULL;
317
318 bool GlobCheckPath(const char *name) {
319 char resolved_cstr[PATH_MAX];
320 char *retval = realpath(name, resolved_cstr);
321 if (retval == NULL)
322 return false;
323
324 const std::string resolved(resolved_cstr);
325 if (resolved == *g_glob_uniondir)
326 return true;
327 if (!HasPrefix(resolved, (*g_glob_uniondir) + "/", false /*ignore_case*/)) {
328 errno = EACCES;
329 return false;
330 }
331 return true;
332 }
333
334 void *GlobOpendir(const char *name) {
335 if (!GlobCheckPath(name))
336 return NULL;
337 return opendir(name);
338 }
339
340 void GlobClosedir(void *dirp) { closedir(static_cast<DIR *>(dirp)); }
341
342 struct dirent *GlobReaddir(void *dirp) {
343 return readdir(static_cast<DIR *>(dirp));
344 }
345
346 int GlobLstat(const char *name, struct stat *st) {
347 if (!GlobCheckPath(name))
348 return -1;
349 return lstat(name, st);
350 }
351
352 int GlobStat(const char *name, struct stat *st) {
353 if (!GlobCheckPath(name))
354 return -1;
355 return stat(name, st);
356 }
357
358
359 } // anonymous namespace
360
361 void swissknife::CommandApplyDirtab::DetermineNestedCatalogCandidates(
362 const catalog::Dirtab &dirtab,
363 catalog::SimpleCatalogManager *catalog_manager,
364 vector<string> *nested_catalog_candidates) {
365 // find possible new nested catalog locations
366 const catalog::Dirtab::Rules &lookup_rules = dirtab.positive_rules();
367 catalog::Dirtab::Rules::const_iterator i = lookup_rules.begin();
368 const catalog::Dirtab::Rules::const_iterator iend = lookup_rules.end();
369 for (; i != iend; ++i) {
370 assert(!i->is_negation);
371
372 // run a glob using the current dirtab rule on the current repository
373 // state
374 const std::string &glob_string = i->pathspec.GetGlobString();
375 const std::string &glob_string_abs = union_dir_ + glob_string;
376 const int glob_flags = GLOB_ONLYDIR | GLOB_NOSORT | GLOB_PERIOD
377 | GLOB_ALTDIRFUNC;
378 glob_t glob_res;
379 g_glob_uniondir = new std::string(union_dir_);
380 glob_res.gl_opendir = GlobOpendir;
381 glob_res.gl_readdir = GlobReaddir;
382 glob_res.gl_closedir = GlobClosedir;
383 glob_res.gl_lstat = GlobLstat;
384 glob_res.gl_stat = GlobStat;
385 const int glob_retval = glob(glob_string_abs.c_str(), glob_flags, NULL,
386 &glob_res);
387 delete g_glob_uniondir;
388 g_glob_uniondir = NULL;
389
390 if (glob_retval == 0) {
391 // found some candidates... filtering by cvmfs catalog structure
392 LogCvmfs(kLogCatalog, kLogDebug,
393 "Swissknife Sync: Found %lu entries for pathspec (%s)",
394 glob_res.gl_pathc, glob_string.c_str());
395 FilterCandidatesFromGlobResult(dirtab, glob_res.gl_pathv,
396 glob_res.gl_pathc, catalog_manager,
397 nested_catalog_candidates);
398 } else if (glob_retval == GLOB_NOMATCH) {
399 LogCvmfs(kLogCvmfs, kLogStderr,
400 "Swissknife Sync: WARNING: cannot apply pathspec %s",
401 glob_string.c_str());
402 } else {
403 LogCvmfs(kLogCvmfs, kLogStderr,
404 "Swissknife Sync: Failed to run glob matching (%s)",
405 glob_string.c_str());
406 }
407
408 globfree(&glob_res);
409 }
410 }
411
412 void swissknife::CommandApplyDirtab::FilterCandidatesFromGlobResult(
413 const catalog::Dirtab &dirtab, char **paths, const size_t npaths,
414 catalog::SimpleCatalogManager *catalog_manager,
415 std::vector<std::string> *nested_catalog_candidates) {
416 // go through the paths produced by glob() and filter them
417 for (size_t i = 0; i < npaths; ++i) {
418 // process candidate paths
419 const std::string candidate(paths[i]);
420 const std::string candidate_rel = candidate.substr(union_dir_.size());
421
422 // check if path points to a directory
423 platform_stat64 candidate_info;
424 const int lstat_retval = platform_lstat(candidate.c_str(), &candidate_info);
425 if (lstat_retval != 0) {
426 LogCvmfs(kLogCatalog, kLogDebug | kLogStderr | kLogSyslogErr,
427 "Swissknife Sync: "
428 "Error in processing .cvmfsdirtab: cannot access %s (%d)",
429 candidate.c_str(), errno);
430 abort();
431 }
432 assert(lstat_retval == 0);
433 if (!S_ISDIR(candidate_info.st_mode)) {
434 // The GLOB_ONLYDIR flag is only a hint, non-directories can still be
435 // returned
436 LogCvmfs(kLogCatalog, kLogDebug,
437 "Swissknife Sync: "
438 "The '%s' dirtab entry does not point to a directory "
439 "but to a file or a symbolic link",
440 candidate_rel.c_str());
441 continue;
442 }
443
444 // check if the path is a meta-directory (. or ..)
445 assert(candidate_rel.size() >= 2);
446 if (candidate_rel.substr(candidate_rel.size() - 2) == "/."
447 || candidate_rel.substr(candidate_rel.size() - 3) == "/..") {
448 continue;
449 }
450
451 // check that the path isn't excluded in the dirtab
452 if (dirtab.IsOpposing(candidate_rel)) {
453 LogCvmfs(kLogCatalog, kLogDebug,
454 "Swissknife Sync: Candidate '%s' is excluded by dirtab",
455 candidate_rel.c_str());
456 continue;
457 }
458
459 // lookup the path in the catalog structure to find out if it already
460 // points to a nested catalog transition point. Furthermore it could be
461 // a new directory and thus not in any catalog yet.
462 catalog::DirectoryEntry dirent;
463 const bool lookup_success = catalog_manager->LookupPath(
464 candidate_rel, catalog::kLookupDefault, &dirent);
465 if (!lookup_success) {
466 LogCvmfs(kLogCatalog, kLogDebug,
467 "Swissknife Sync: Didn't find '%s' in catalogs, could "
468 "be a new directory and nested catalog.",
469 candidate_rel.c_str());
470 nested_catalog_candidates->push_back(candidate);
471 } else if (!dirent.IsNestedCatalogMountpoint()
472 && !dirent.IsNestedCatalogRoot()) {
473 LogCvmfs(kLogCatalog, kLogDebug,
474 "Swissknife Sync: Found '%s' in catalogs but is not a "
475 "nested catalog yet.",
476 candidate_rel.c_str());
477 nested_catalog_candidates->push_back(candidate);
478 } else {
479 // check if the nested catalog marker is still there, we might need to
480 // recreate the catalog after manual marker removal
481 // Note: First we check if the parent directory shows up in the scratch
482 // space to verify that it was touched (copy-on-write)
483 // Otherwise we would force the cvmfs client behind the union
484 // file-
485 // system to (potentially) unnecessarily fetch catalogs
486 if (DirectoryExists(scratch_dir_ + candidate_rel)
487 && !FileExists(union_dir_ + candidate_rel + "/.cvmfscatalog")) {
488 LogCvmfs(kLogCatalog, kLogStdout,
489 "Swissknife Sync: WARNING: '%s' should be a nested "
490 "catalog according to the dirtab. "
491 "Recreating...",
492 candidate_rel.c_str());
493 nested_catalog_candidates->push_back(candidate);
494 } else {
495 LogCvmfs(kLogCatalog, kLogDebug,
496 "Swissknife Sync: "
497 "Found '%s' in catalogs and it already is a nested catalog.",
498 candidate_rel.c_str());
499 }
500 }
501 }
502 }
503
504 bool swissknife::CommandApplyDirtab::CreateCatalogMarkers(
505 const std::vector<std::string> &new_nested_catalogs) {
506 // go through the new nested catalog paths and create .cvmfscatalog markers
507 // where necessary
508 bool success = true;
509 std::vector<std::string>::const_iterator k = new_nested_catalogs.begin();
510 const std::vector<std::string>::const_iterator kend = new_nested_catalogs
511 .end();
512 for (; k != kend; ++k) {
513 assert(!k->empty() && k->size() > union_dir_.size());
514
515 // was the marker already created by hand?
516 const std::string marker_path = *k + "/.cvmfscatalog";
517 if (FileExists(marker_path)) {
518 continue;
519 }
520
521 // create a nested catalog marker
522 const mode_t mode = kDefaultFileMode;
523 const int fd = open(marker_path.c_str(), O_CREAT, mode);
524 if (fd < 0) {
525 LogCvmfs(kLogCvmfs, kLogStderr,
526 "Swissknife Sync: Failed to create nested catalog marker "
527 "at '%s' (errno: %d)",
528 marker_path.c_str(), errno);
529 success = false;
530 continue;
531 }
532 close(fd);
533
534 // inform the user if requested
535 if (verbose_) {
536 LogCvmfs(kLogCvmfs, kLogStdout,
537 "Swissknife Sync: Auto-creating nested catalog in %s",
538 k->c_str());
539 }
540 }
541
542 return success;
543 }
544
545 struct chunk_arg {
546 chunk_arg(char param, size_t *save_to) : param(param), save_to(save_to) { }
547 char param;
548 size_t *save_to;
549 };
550
551 bool swissknife::CommandSync::ReadFileChunkingArgs(
552 const swissknife::ArgumentList &args, SyncParameters *params) {
553 typedef std::vector<chunk_arg> ChunkArgs;
554
555 // define where to store the value of which file chunk argument
556 ChunkArgs chunk_args;
557 chunk_args.push_back(chunk_arg('a', &params->avg_file_chunk_size));
558 chunk_args.push_back(chunk_arg('l', &params->min_file_chunk_size));
559 chunk_args.push_back(chunk_arg('h', &params->max_file_chunk_size));
560
561 // read the arguments
562 ChunkArgs::const_iterator i = chunk_args.begin();
563 const ChunkArgs::const_iterator iend = chunk_args.end();
564 for (; i != iend; ++i) {
565 const swissknife::ArgumentList::const_iterator arg = args.find(i->param);
566
567 if (arg != args.end()) {
568 const size_t arg_value = static_cast<size_t>(String2Uint64(*arg->second));
569 if (arg_value > 0) {
570 *i->save_to = arg_value;
571 } else {
572 return false;
573 }
574 }
575 }
576
577 // check if argument values are sane
578 return true;
579 }
580
581 int swissknife::CommandSync::Main(const swissknife::ArgumentList &args) {
582 const string start_time = GetGMTimestamp();
583
584 // Spawn monitoring process (watchdog)
585 const std::string watchdog_dir = "/tmp";
586 char watchdog_path[PATH_MAX];
587 const std::string timestamp = GetGMTimestamp("%Y.%m.%d-%H.%M.%S");
588 const int path_size = snprintf(watchdog_path, sizeof(watchdog_path),
589 "%s/cvmfs-swissknife-sync-stacktrace.%s.%d",
590 watchdog_dir.c_str(), timestamp.c_str(),
591 getpid());
592 assert(path_size > 0);
593 assert(path_size < PATH_MAX);
594 const UniquePtr<Watchdog> watchdog(Watchdog::Create(NULL, false /* needs_read_environ */));
595 watchdog->Spawn(std::string(watchdog_path));
596
597 SyncParameters params;
598
599 // Initialization
600 params.dir_union = MakeCanonicalPath(*args.find('u')->second);
601 params.dir_scratch = MakeCanonicalPath(*args.find('s')->second);
602 params.dir_rdonly = MakeCanonicalPath(*args.find('c')->second);
603 params.dir_temp = MakeCanonicalPath(*args.find('t')->second);
604 params.base_hash = shash::MkFromHexPtr(shash::HexPtr(*args.find('b')->second),
605 shash::kSuffixCatalog);
606 params.stratum0 = *args.find('w')->second;
607 params.manifest_path = *args.find('o')->second;
608 params.spooler_definition = *args.find('r')->second;
609
610 params.public_keys = *args.find('K')->second;
611 params.repo_name = *args.find('N')->second;
612
613 params.ttl_seconds = catalog::Catalog::kDefaultTTL;
614
615 if (args.find('f') != args.end())
616 params.union_fs_type = *args.find('f')->second;
617 if (args.find('A') != args.end())
618 params.is_balanced = true;
619 if (args.find('x') != args.end())
620 params.print_changeset = true;
621 if (args.find('y') != args.end())
622 params.dry_run = true;
623 if (args.find('m') != args.end())
624 params.mucatalogs = true;
625 if (args.find('i') != args.end())
626 params.ignore_xdir_hardlinks = true;
627 if (args.find('d') != args.end())
628 params.stop_for_catalog_tweaks = true;
629 if (args.find('V') != args.end())
630 params.voms_authz = true;
631 if (args.find('F') != args.end())
632 params.authz_file = *args.find('F')->second;
633 if (args.find('k') != args.end())
634 params.include_xattrs = true;
635 if (args.find('j') != args.end())
636 params.enable_mtime_ns = true;
637 if (args.find('Y') != args.end())
638 params.external_data = true;
639 if (args.find('W') != args.end())
640 params.direct_io = true;
641 if (args.find('S') != args.end()) {
642 const bool retval = catalog::VirtualCatalog::ParseActions(
643 *args.find('S')->second, &params.virtual_dir_actions);
644 if (!retval) {
645 LogCvmfs(kLogCvmfs, kLogStderr,
646 "Swissknife Sync: Invalid virtual catalog options: %s",
647 args.find('S')->second->c_str());
648 return 1;
649 }
650 }
651 if (args.find('z') != args.end()) {
652 const unsigned log_level = 1 << (kLogLevel0
653 + String2Uint64(*args.find('z')->second));
654 if (log_level > kLogNone) {
655 LogCvmfs(kLogCvmfs, kLogStderr, "Swissknife Sync: invalid log level");
656 return 1;
657 }
658 SetLogVerbosity(static_cast<LogLevels>(log_level));
659 }
660
661 if (args.find('X') != args.end())
662 params.max_weight = String2Uint64(*args.find('X')->second);
663 if (args.find('M') != args.end())
664 params.min_weight = String2Uint64(*args.find('M')->second);
665
666 if (args.find('p') != args.end()) {
667 params.use_file_chunking = true;
668 if (!ReadFileChunkingArgs(args, &params)) {
669 PrintError("Swissknife Sync: Failed to read file chunk size values");
670 return 2;
671 }
672 }
673 if (args.find('O') != args.end()) {
674 params.generate_legacy_bulk_chunks = true;
675 }
676 shash::Algorithms hash_algorithm = shash::kSha1;
677 if (args.find('e') != args.end()) {
678 hash_algorithm = shash::ParseHashAlgorithm(*args.find('e')->second);
679 if (hash_algorithm == shash::kAny) {
680 PrintError("Swissknife Sync: Unknown hash algorithm");
681 return 1;
682 }
683 }
684 if (args.find('Z') != args.end()) {
685 params.compression_alg = zlib::ParseCompressionAlgorithm(
686 *args.find('Z')->second);
687 }
688
689 if (args.find('E') != args.end())
690 params.enforce_limits = true;
691 if (args.find('Q') != args.end()) {
692 params.nested_kcatalog_limit = String2Uint64(*args.find('Q')->second);
693 } else {
694 params.nested_kcatalog_limit = SyncParameters::kDefaultNestedKcatalogLimit;
695 }
696 if (args.find('R') != args.end()) {
697 params.root_kcatalog_limit = String2Uint64(*args.find('R')->second);
698 } else {
699 params.root_kcatalog_limit = SyncParameters::kDefaultRootKcatalogLimit;
700 }
701 if (args.find('U') != args.end()) {
702 params.file_mbyte_limit = String2Uint64(*args.find('U')->second);
703 } else {
704 params.file_mbyte_limit = SyncParameters::kDefaultFileMbyteLimit;
705 }
706
707 if (args.find('v') != args.end()) {
708 const sanitizer::IntegerSanitizer sanitizer;
709 if (!sanitizer.IsValid(*args.find('v')->second)) {
710 PrintError("Swissknife Sync: Invalid revision number");
711 return 1;
712 }
713 params.manual_revision = String2Uint64(*args.find('v')->second);
714 }
715
716 params.branched_catalog = args.find('B') != args.end();
717
718 if (args.find('q') != args.end()) {
719 params.max_concurrent_write_jobs = String2Uint64(*args.find('q')->second);
720 }
721
722 if (args.find('0') != args.end()) {
723 params.num_upload_tasks = String2Uint64(*args.find('0')->second);
724 }
725
726 if (args.find('T') != args.end()) {
727 params.ttl_seconds = String2Uint64(*args.find('T')->second);
728 }
729
730 if (args.find('g') != args.end()) {
731 params.ignore_special_files = true;
732 }
733
734 if (args.find('P') != args.end()) {
735 params.session_token_file = *args.find('P')->second;
736 }
737
738 if (args.find('H') != args.end()) {
739 params.key_file = *args.find('H')->second;
740 }
741
742 if (args.find('D') != args.end()) {
743 params.repo_tag.SetName(*args.find('D')->second);
744 }
745
746 if (args.find('J') != args.end()) {
747 params.repo_tag.SetDescription(*args.find('J')->second);
748 }
749
750 if (args.find('G') != args.end()) {
751 params.cache_dir = "/var/spool/cvmfs/" + params.repo_name + "/cache.server";
752 }
753
754 const bool upload_statsdb = (args.count('I') > 0);
755
756 if (!CheckParams(params))
757 return 2;
758 // This may fail, in which case a warning is printed and the process continues
759 ObtainDacReadSearchCapability();
760
761 perf::StatisticsTemplate publish_statistics("publish", this->statistics());
762
763 // Start spooler
764 upload::SpoolerDefinition spooler_definition(
765 params.spooler_definition, hash_algorithm, params.compression_alg,
766 params.generate_legacy_bulk_chunks, params.use_file_chunking,
767 params.min_file_chunk_size, params.avg_file_chunk_size,
768 params.max_file_chunk_size, params.session_token_file, params.key_file);
769 if (params.max_concurrent_write_jobs > 0) {
770 spooler_definition
771 .number_of_concurrent_uploads = params.max_concurrent_write_jobs;
772 }
773 spooler_definition.num_upload_tasks = params.num_upload_tasks;
774
775 const upload::SpoolerDefinition spooler_definition_catalogs(
776 spooler_definition.Dup2DefaultCompression());
777
778 params.spooler = upload::Spooler::Construct(spooler_definition,
779 &publish_statistics);
780 if (NULL == params.spooler)
781 return 3;
782 const UniquePtr<upload::Spooler> spooler_catalogs(upload::Spooler::Construct(
783 spooler_definition_catalogs, &publish_statistics));
784 if (!spooler_catalogs.IsValid())
785 return 3;
786
787 const bool follow_redirects = (args.count('L') > 0);
788 const string proxy = (args.count('@') > 0) ? *args.find('@')->second : "";
789 if (!InitDownloadManager(follow_redirects, proxy)) {
790 return 3;
791 }
792
793 if (!InitSignatureManager(params.public_keys)) {
794 return 3;
795 }
796
797 /*
798 * Note: If the upstream is of type gateway, due to the possibility of
799 * concurrent release managers, it's possible to have a different local and
800 * remote root hashes. We proceed by loading the remote manifest but we give
801 * an empty base hash.
802 */
803 UniquePtr<manifest::Manifest> manifest;
804 if (params.branched_catalog) {
805 // Throw-away manifest
806 manifest = new manifest::Manifest(shash::Any(), 0, "");
807 } else if (params.virtual_dir_actions
808 != catalog::VirtualCatalog::kActionNone) {
809 manifest = this->OpenLocalManifest(params.manifest_path);
810 params.base_hash = manifest->catalog_hash();
811 } else {
812 // TODO(jblomer): revert to params.base_hash if spooler driver type is not
813 // upload::SpoolerDefinition::Gateway
814 manifest = FetchRemoteManifest(params.stratum0, params.repo_name,
815 shash::Any());
816 }
817 if (!manifest.IsValid()) {
818 return 3;
819 }
820
821 StatisticsDatabase *stats_db = StatisticsDatabase::OpenStandardDB(
822 params.repo_name);
823
824 const std::string old_root_hash = manifest->catalog_hash().ToString(true);
825
826 catalog::WritableCatalogManager catalog_manager(
827 params.base_hash, params.stratum0, params.dir_temp,
828 spooler_catalogs.weak_ref(), download_manager(), params.enforce_limits,
829 params.nested_kcatalog_limit, params.root_kcatalog_limit,
830 params.file_mbyte_limit, statistics(), params.is_balanced,
831 params.max_weight, params.min_weight, params.cache_dir);
832 catalog_manager.Init();
833
834 publish::SyncMediator mediator(&catalog_manager, &params, publish_statistics);
835 LogCvmfs(kLogPublish, kLogStdout, "Swissknife Sync: Processing changes...");
836
837 // Should be before the synchronization starts to avoid race of GetTTL with
838 // other sqlite operations
839 if ((params.ttl_seconds > 0)
840 && ((params.ttl_seconds != catalog_manager.GetTTL())
841 || !catalog_manager.HasExplicitTTL())) {
842 LogCvmfs(kLogCvmfs, kLogStdout,
843 "Swissknife Sync: Setting repository TTL to %" PRIu64 "s",
844 params.ttl_seconds);
845 catalog_manager.SetTTL(params.ttl_seconds);
846 }
847
848 // Either real catalogs or virtual catalog
849 if (params.virtual_dir_actions == catalog::VirtualCatalog::kActionNone) {
850 publish::SyncUnion *sync;
851 if (params.union_fs_type == "overlayfs") {
852 sync = new publish::SyncUnionOverlayfs(
853 &mediator, params.dir_rdonly, params.dir_union, params.dir_scratch);
854 } else if (params.union_fs_type == "aufs") {
855 sync = new publish::SyncUnionAufs(&mediator, params.dir_rdonly,
856 params.dir_union, params.dir_scratch);
857 } else {
858 LogCvmfs(kLogCvmfs, kLogStderr,
859 "Swissknife Sync: unknown union file system: %s",
860 params.union_fs_type.c_str());
861 return 3;
862 }
863
864 if (!sync->Initialize()) {
865 LogCvmfs(kLogCvmfs, kLogStderr,
866 "Swissknife Sync: Initialization of the synchronisation "
867 "engine failed");
868 return 4;
869 }
870
871 sync->Traverse();
872 } else {
873 assert(!manifest->history().IsNull());
874 catalog::VirtualCatalog virtual_catalog(
875 manifest.weak_ref(), download_manager(), &catalog_manager, &params);
876 virtual_catalog.Generate(params.virtual_dir_actions);
877 }
878
879 if (!params.authz_file.empty()) {
880 LogCvmfs(kLogCvmfs, kLogDebug,
881 "Swissknife Sync: Adding contents of authz file %s to"
882 " root catalog.",
883 params.authz_file.c_str());
884 const int fd = open(params.authz_file.c_str(), O_RDONLY);
885 if (fd == -1) {
886 LogCvmfs(kLogCvmfs, kLogStderr,
887 "Swissknife Sync: Unable to open authz file (%s)"
888 "from the publication process: %s",
889 params.authz_file.c_str(), strerror(errno));
890 return 7;
891 }
892
893 std::string new_authz;
894 const bool read_successful = SafeReadToString(fd, &new_authz);
895 close(fd);
896
897 if (!read_successful) {
898 LogCvmfs(kLogCvmfs, kLogStderr,
899 "Swissknife Sync: Failed to read authz file (%s): %s",
900 params.authz_file.c_str(), strerror(errno));
901 return 8;
902 }
903
904 catalog_manager.SetVOMSAuthz(new_authz);
905 }
906
907 if (!mediator.Commit(manifest.weak_ref())) {
908 PrintError("Swissknife Sync: Something went wrong during sync");
909 if (!params.dry_run) {
910 stats_db->StorePublishStatistics(this->statistics(), start_time, false);
911 if (upload_statsdb) {
912 stats_db->UploadStatistics(params.spooler);
913 }
914 }
915 return 5;
916 }
917
918 perf::Counter *revision_counter = statistics()->Register(
919 "publish.revision", "Published revision number");
920 revision_counter->Set(
921 static_cast<int64_t>(catalog_manager.GetRootCatalog()->revision()));
922
923 // finalize the spooler
924 LogCvmfs(kLogCvmfs, kLogStdout,
925 "Swissknife Sync: Wait for all uploads to finish");
926 params.spooler->WaitForUpload();
927 spooler_catalogs->WaitForUpload();
928 params.spooler->FinalizeSession(false);
929
930 LogCvmfs(kLogCvmfs, kLogStdout,
931 "Swissknife Sync: Exporting repository manifest");
932
933 // We call FinalizeSession(true) this time, to also trigger the commit
934 // operation on the gateway machine (if the upstream is of type "gw").
935
936 // Get the path of the new root catalog
937 const std::string new_root_hash = manifest->catalog_hash().ToString(true);
938
939 if (!spooler_catalogs->FinalizeSession(true, old_root_hash, new_root_hash,
940 params.repo_tag)) {
941 PrintError("Swissknife Sync: Failed to commit transaction.");
942 if (!params.dry_run) {
943 stats_db->StorePublishStatistics(this->statistics(), start_time, false);
944 if (upload_statsdb) {
945 stats_db->UploadStatistics(params.spooler);
946 }
947 }
948 return 9;
949 }
950
951 if (!params.dry_run) {
952 stats_db->StorePublishStatistics(this->statistics(), start_time, true);
953 if (upload_statsdb) {
954 stats_db->UploadStatistics(params.spooler);
955 }
956 }
957
958 delete params.spooler;
959
960 if (!manifest->Export(params.manifest_path)) {
961 PrintError("Swissknife Sync: Failed to create new repository");
962 return 6;
963 }
964
965 return 0;
966 }
967