GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/swissknife_history.cc
Date: 2026-06-28 02:36:10
Exec Total Coverage
Lines: 0 691 0.0%
Branches: 0 1651 0.0%

Line Branch Exec Source
1 /**
2 * This file is part of the CernVM File System
3 */
4
5 #include "swissknife_history.h"
6
7 #include <algorithm>
8 #include <cassert>
9 #include <ctime>
10
11 #include "catalog_rw.h"
12 #include "crypto/hash.h"
13 #include "history_sqlite.h"
14 #include "manifest_fetch.h"
15 #include "network/download.h"
16 #include "network/sink_path.h"
17 #include "repository_tag.h"
18 #include "upload.h"
19
20 using namespace std; // NOLINT
21 using namespace swissknife; // NOLINT
22
23 const std::string CommandTag::kHeadTag = "trunk";
24 const std::string CommandTag::kPreviousHeadTag = "trunk-previous";
25
26 const std::string CommandTag::kHeadTagDescription = "current HEAD";
27 const std::string
28 CommandTag::kPreviousHeadTagDescription = "default undo target";
29
30 static void InsertCommonParameters(ParameterList *r) {
31 r->push_back(Parameter::Mandatory('w', "repository directory / url"));
32 r->push_back(Parameter::Mandatory('t', "temporary scratch directory"));
33 r->push_back(Parameter::Optional('p', "public key of the repository"));
34 r->push_back(Parameter::Optional('f', "fully qualified repository name"));
35 r->push_back(Parameter::Optional('r', "spooler definition string"));
36 r->push_back(Parameter::Optional('m', "(unsigned) manifest file to edit"));
37 r->push_back(Parameter::Optional('b', "mounted repository base hash"));
38 r->push_back(
39 Parameter::Optional('e', "hash algorithm to use (default SHA1)"));
40 r->push_back(Parameter::Switch('L', "follow HTTP redirects"));
41 r->push_back(Parameter::Optional('P', "session_token_file"));
42 r->push_back(Parameter::Optional('@', "proxy url"));
43 }
44
45 CommandTag::Environment *CommandTag::InitializeEnvironment(
46 const ArgumentList &args, const bool read_write) {
47 const string repository_url = MakeCanonicalPath(*args.find('w')->second);
48 const string tmp_path = MakeCanonicalPath(*args.find('t')->second);
49 const string spl_definition = (args.find('r') == args.end())
50 ? ""
51 : MakeCanonicalPath(
52 *args.find('r')->second);
53 const string manifest_path = (args.find('m') == args.end())
54 ? ""
55 : MakeCanonicalPath(*args.find('m')->second);
56 const shash::Algorithms hash_algo = (args.find('e') == args.end())
57 ? shash::kSha1
58 : shash::ParseHashAlgorithm(
59 *args.find('e')->second);
60 const string pubkey_path = (args.find('p') == args.end())
61 ? ""
62 : MakeCanonicalPath(*args.find('p')->second);
63 const shash::Any base_hash = (args.find('b') == args.end())
64 ? shash::Any()
65 : shash::MkFromHexPtr(
66 shash::HexPtr(*args.find('b')->second),
67 shash::kSuffixCatalog);
68 const string repo_name = (args.find('f') == args.end())
69 ? ""
70 : *args.find('f')->second;
71
72 string session_token_file;
73 if (args.find('P') != args.end()) {
74 session_token_file = *args.find('P')->second;
75 }
76
77 // Sanity checks
78 if (hash_algo == shash::kAny) {
79 LogCvmfs(kLogCvmfs, kLogStderr, "failed to parse hash algorithm to use");
80 return NULL;
81 }
82
83 if (read_write && spl_definition.empty()) {
84 LogCvmfs(kLogCvmfs, kLogStderr, "no upstream storage provided (-r)");
85 return NULL;
86 }
87
88 if (read_write && manifest_path.empty()) {
89 LogCvmfs(kLogCvmfs, kLogStderr, "no (unsigned) manifest provided (-m)");
90 return NULL;
91 }
92
93 if (!read_write && pubkey_path.empty()) {
94 LogCvmfs(kLogCvmfs, kLogStderr, "no public key provided (-p)");
95 return NULL;
96 }
97
98 if (!read_write && repo_name.empty()) {
99 LogCvmfs(kLogCvmfs, kLogStderr, "no repository name provided (-f)");
100 return NULL;
101 }
102
103 if (HasPrefix(spl_definition, "gw", false)) {
104 if (session_token_file.empty()) {
105 PrintError("Session token file has to be provided "
106 "when upstream type is gw.");
107 return NULL;
108 }
109 }
110
111 // create new environment
112 // Note: We use this encapsulation because we cannot be sure that the
113 // Command object gets deleted properly. With the Environment object at
114 // hand we have full control and can make heavy and safe use of RAII
115 UniquePtr<Environment> env(new Environment(repository_url, tmp_path));
116 env->manifest_path.Set(manifest_path);
117 env->history_path.Set(CreateTempPath(tmp_path + "/history", 0600));
118
119 // initialize the (swissknife global) download manager
120 const bool follow_redirects = (args.count('L') > 0);
121 const std::string &proxy = (args.count('@') > 0) ? *args.find('@')->second
122 : "";
123 if (!this->InitDownloadManager(follow_redirects, proxy)) {
124 return NULL;
125 }
126
127 // initialize the (swissknife global) signature manager (if possible)
128 if (!pubkey_path.empty() && !this->InitSignatureManager(pubkey_path)) {
129 return NULL;
130 }
131
132 // open the (yet unsigned) manifest file if it is there, otherwise load the
133 // latest manifest from the server
134 env->manifest = (FileExists(env->manifest_path.path()))
135 ? OpenLocalManifest(env->manifest_path.path())
136 : FetchRemoteManifest(env->repository_url, repo_name,
137 base_hash);
138
139 if (!env->manifest.IsValid()) {
140 LogCvmfs(kLogCvmfs, kLogStderr, "failed to load manifest file");
141 return NULL;
142 }
143
144 // figure out the hash of the history from the previous revision if needed
145 if (read_write && env->manifest->history().IsNull() && !base_hash.IsNull()) {
146 env->previous_manifest = FetchRemoteManifest(env->repository_url, repo_name,
147 base_hash);
148 if (!env->previous_manifest.IsValid()) {
149 LogCvmfs(kLogCvmfs, kLogStderr, "failed to load previous manifest");
150 return NULL;
151 }
152
153 LogCvmfs(kLogCvmfs, kLogDebug,
154 "using history database '%s' from previous "
155 "manifest (%s) as basis",
156 env->previous_manifest->history().ToString().c_str(),
157 env->previous_manifest->repository_name().c_str());
158 env->manifest->set_history(env->previous_manifest->history());
159 env->manifest->set_repository_name(
160 env->previous_manifest->repository_name());
161 }
162
163 // download the history database referenced in the manifest
164 env->history = GetHistory(env->manifest.weak_ref(), env->repository_url,
165 env->history_path.path(), read_write);
166 if (!env->history.IsValid()) {
167 return NULL;
168 }
169
170 // if the using Command is expected to change the history database, we
171 // need
172 // to initialize the upload spooler for potential later history upload
173 if (read_write) {
174 const bool use_file_chunking = false;
175 const bool generate_legacy_bulk_chunks = false;
176 const upload::SpoolerDefinition sd(
177 spl_definition, hash_algo, zlib::kZlibDefault,
178 generate_legacy_bulk_chunks, use_file_chunking, 0, 0, 0,
179 session_token_file);
180 env->spooler = upload::Spooler::Construct(sd);
181 if (!env->spooler.IsValid()) {
182 LogCvmfs(kLogCvmfs, kLogStderr, "failed to initialize upload spooler");
183 return NULL;
184 }
185 }
186
187 // return the pointer of the Environment (passing the ownership along)
188 return env.Release();
189 }
190
191 bool CommandTag::CloseAndPublishHistory(Environment *env) {
192 assert(env->spooler.IsValid());
193
194 // set the previous revision pointer of the history database
195 env->history->SetPreviousRevision(env->manifest->history());
196
197 // close the history database
198 history::History *weak_history = env->history.Release();
199 delete weak_history;
200
201 // compress and upload the new history database
202 Future<shash::Any> history_hash;
203 upload::Spooler::CallbackPtr callback = env->spooler->RegisterListener(
204 &CommandTag::UploadClosure, this, &history_hash);
205 env->spooler->ProcessHistory(env->history_path.path());
206 env->spooler->WaitForUpload();
207 const shash::Any new_history_hash = history_hash.Get();
208 env->spooler->UnregisterListener(callback);
209
210 // retrieve the (async) uploader result
211 if (new_history_hash.IsNull()) {
212 return false;
213 }
214
215 // update the (yet unsigned) manifest file
216 env->manifest->set_history(new_history_hash);
217 if (!env->manifest->Export(env->manifest_path.path())) {
218 LogCvmfs(kLogCvmfs, kLogStderr, "failed to export the new manifest '%s'",
219 env->manifest_path.path().c_str());
220 return false;
221 }
222
223 // disable the unlink guard in order to keep the newly exported manifest file
224 env->manifest_path.Disable();
225 LogCvmfs(kLogCvmfs, kLogVerboseMsg,
226 "exported manifest (%" PRIu64 ") with new history '%s'",
227 env->manifest->revision(), new_history_hash.ToString().c_str());
228
229 return true;
230 }
231
232
233 bool CommandTag::UploadCatalogAndUpdateManifest(
234 CommandTag::Environment *env, catalog::WritableCatalog *catalog) {
235 assert(env->spooler.IsValid());
236
237 // gather information about catalog to be uploaded and update manifest
238 UniquePtr<catalog::WritableCatalog> wr_catalog(catalog);
239 const std::string catalog_path = wr_catalog->database_path();
240 env->manifest->set_ttl(wr_catalog->GetTTL());
241 env->manifest->set_revision(wr_catalog->GetRevision());
242 env->manifest->set_publish_timestamp(wr_catalog->GetLastModified());
243
244 // close the catalog
245 catalog::WritableCatalog *weak_catalog = wr_catalog.Release();
246 delete weak_catalog;
247
248 // upload the catalog
249 Future<shash::Any> catalog_hash;
250 upload::Spooler::CallbackPtr callback = env->spooler->RegisterListener(
251 &CommandTag::UploadClosure, this, &catalog_hash);
252 env->spooler->ProcessCatalog(catalog_path);
253 env->spooler->WaitForUpload();
254 const shash::Any new_catalog_hash = catalog_hash.Get();
255 env->spooler->UnregisterListener(callback);
256
257 // check if the upload succeeded
258 if (new_catalog_hash.IsNull()) {
259 LogCvmfs(kLogCvmfs, kLogStderr, "failed to upload catalog '%s'",
260 catalog_path.c_str());
261 return false;
262 }
263
264 // update the catalog size and hash in the manifest
265 const size_t catalog_size = GetFileSize(catalog_path);
266 env->manifest->set_catalog_size(catalog_size);
267 env->manifest->set_catalog_hash(new_catalog_hash);
268
269 LogCvmfs(kLogCvmfs, kLogVerboseMsg, "uploaded new catalog (%lu bytes) '%s'",
270 catalog_size, new_catalog_hash.ToString().c_str());
271
272 return true;
273 }
274
275 void CommandTag::UploadClosure(const upload::SpoolerResult &result,
276 Future<shash::Any> *hash) {
277 assert(!result.IsChunked());
278 if (result.return_code != 0) {
279 LogCvmfs(kLogCvmfs, kLogStderr, "failed to upload history database (%d)",
280 result.return_code);
281 hash->Set(shash::Any());
282 } else {
283 hash->Set(result.content_hash);
284 }
285 }
286
287 bool CommandTag::UpdateUndoTags(
288 Environment *env, const history::History::Tag &current_head_template,
289 const bool undo_rollback) {
290 assert(env->history.IsValid());
291
292 history::History::Tag current_head;
293 history::History::Tag current_old_head;
294
295 // remove previous HEAD tag
296 if (!env->history->Remove(CommandTag::kPreviousHeadTag)) {
297 LogCvmfs(kLogCvmfs, kLogVerboseMsg, "didn't find a previous HEAD tag");
298 }
299
300 // check if we have a current HEAD tag that needs to renamed to previous
301 // HEAD
302 if (env->history->GetByName(CommandTag::kHeadTag, &current_head)) {
303 // remove current HEAD tag
304 if (!env->history->Remove(CommandTag::kHeadTag)) {
305 LogCvmfs(kLogCvmfs, kLogStderr, "failed to remove current HEAD tag");
306 return false;
307 }
308
309 // set previous HEAD tag where current HEAD used to be
310 if (!undo_rollback) {
311 current_old_head = current_head;
312 current_old_head.name = CommandTag::kPreviousHeadTag;
313 current_old_head.description = CommandTag::kPreviousHeadTagDescription;
314 if (!env->history->Insert(current_old_head)) {
315 LogCvmfs(kLogCvmfs, kLogStderr, "failed to set previous HEAD tag");
316 return false;
317 }
318 }
319 }
320
321 // set the current HEAD to the catalog provided by the template HEAD
322 current_head = current_head_template;
323 current_head.name = CommandTag::kHeadTag;
324 current_head.description = CommandTag::kHeadTagDescription;
325 if (!env->history->Insert(current_head)) {
326 LogCvmfs(kLogCvmfs, kLogStderr, "failed to set new current HEAD");
327 return false;
328 }
329
330 return true;
331 }
332
333 bool CommandTag::FetchObject(const std::string &repository_url,
334 const shash::Any &object_hash,
335 const std::string &destination_path) const {
336 assert(!object_hash.IsNull());
337
338 download::Failures dl_retval;
339 const std::string url = repository_url + "/data/" + object_hash.MakePath();
340
341 cvmfs::PathSink pathsink(destination_path);
342 download::JobInfo download_object(&url, true, false, &object_hash, &pathsink);
343 dl_retval = download_manager()->Fetch(&download_object);
344
345 if (dl_retval != download::kFailOk) {
346 LogCvmfs(kLogCvmfs, kLogStderr, "failed to download object '%s' (%d - %s)",
347 object_hash.ToStringWithSuffix().c_str(), dl_retval,
348 download::Code2Ascii(dl_retval));
349 return false;
350 }
351
352 return true;
353 }
354
355 history::History *CommandTag::GetHistory(const manifest::Manifest *manifest,
356 const std::string &repository_url,
357 const std::string &history_path,
358 const bool read_write) const {
359 const shash::Any history_hash = manifest->history();
360 history::History *history;
361
362 if (history_hash.IsNull()) {
363 history = history::SqliteHistory::Create(history_path,
364 manifest->repository_name());
365 if (NULL == history) {
366 LogCvmfs(kLogCvmfs, kLogStderr, "failed to create history database");
367 return NULL;
368 }
369 } else {
370 if (!FetchObject(repository_url, history_hash, history_path)) {
371 return NULL;
372 }
373
374 history = (read_write) ? history::SqliteHistory::OpenWritable(history_path)
375 : history::SqliteHistory::Open(history_path);
376 if (NULL == history) {
377 LogCvmfs(kLogCvmfs, kLogStderr, "failed to open history database (%s)",
378 history_path.c_str());
379 unlink(history_path.c_str());
380 return NULL;
381 }
382
383 assert(history->fqrn() == manifest->repository_name());
384 }
385
386 return history;
387 }
388
389 catalog::Catalog *CommandTag::GetCatalog(const std::string &repository_url,
390 const shash::Any &catalog_hash,
391 const std::string catalog_path,
392 const bool read_write) const {
393 assert(shash::kSuffixCatalog == catalog_hash.suffix);
394 if (!FetchObject(repository_url, catalog_hash, catalog_path)) {
395 return NULL;
396 }
397
398 const std::string catalog_root_path = "";
399 return (read_write) ? catalog::WritableCatalog::AttachFreely(
400 catalog_root_path, catalog_path, catalog_hash)
401 : catalog::Catalog::AttachFreely(
402 catalog_root_path, catalog_path, catalog_hash);
403 }
404
405 void CommandTag::PrintTagMachineReadable(
406 const history::History::Tag &tag) const {
407 LogCvmfs(kLogCvmfs, kLogStdout, "%s %s %" PRIu64 " %" PRIu64 " %ld %s %s",
408 tag.name.c_str(), tag.root_hash.ToString().c_str(), tag.size,
409 tag.revision, tag.timestamp,
410 (tag.branch == "") ? "(default)" : tag.branch.c_str(),
411 tag.description.c_str());
412 }
413
414 std::string CommandTag::AddPadding(const std::string &str, const size_t padding,
415 const bool align_right,
416 const std::string &fill_char) const {
417 assert(str.size() <= padding);
418 std::string result(str);
419 result.resize(padding);
420 const size_t pos = (align_right) ? 0 : str.size();
421 const size_t padding_width = padding - str.size();
422 for (size_t i = 0; i < padding_width; ++i)
423 result.insert(pos, fill_char);
424 return result;
425 }
426
427 bool CommandTag::IsUndoTagName(const std::string &tag_name) const {
428 return tag_name == CommandTag::kHeadTag
429 || tag_name == CommandTag::kPreviousHeadTag;
430 }
431
432 //------------------------------------------------------------------------------
433
434 ParameterList CommandEditTag::GetParams() const {
435 ParameterList r;
436 InsertCommonParameters(&r);
437
438 r.push_back(Parameter::Optional('d', "space separated tags to be deleted"));
439 r.push_back(Parameter::Optional('a', "name of the new tag"));
440 r.push_back(Parameter::Optional('D', "description of the tag"));
441 r.push_back(Parameter::Optional('B', "branch of the new tag"));
442 r.push_back(Parameter::Optional('P', "predecessor branch"));
443 r.push_back(Parameter::Optional('h', "root hash of the new tag"));
444 r.push_back(Parameter::Switch('x', "maintain undo tags"));
445 r.push_back(Parameter::Optional('c',
446 "cleanup auto tags older than this Unix timestamp"));
447 return r;
448 }
449
450 int CommandEditTag::Main(const ArgumentList &args) {
451 if ((args.find('d') == args.end()) && (args.find('a') == args.end())
452 && (args.find('x') == args.end()) && (args.find('c') == args.end())) {
453 LogCvmfs(kLogCvmfs, kLogStderr, "nothing to do");
454 return 1;
455 }
456
457 // initialize the Environment (taking ownership)
458 const bool history_read_write = true;
459 const UniquePtr<Environment> env(
460 InitializeEnvironment(args, history_read_write));
461 if (!env.IsValid()) {
462 LogCvmfs(kLogCvmfs, kLogStderr, "failed to init environment");
463 return 1;
464 }
465
466 int retval;
467 if (args.find('d') != args.end()) {
468 retval = RemoveTags(args, env.weak_ref());
469 if (retval != 0)
470 return retval;
471 }
472
473 // Cleanup old auto-generated tags if -c is specified. This must run *before*
474 // adding the new tag below, so that the freshly created auto tag is never
475 // itself a cleanup candidate: with a cutoff at (or in) the future (e.g.
476 // CVMFS_AUTO_TAG_TIMESPAN="tomorrow") the new tag would otherwise be born
477 // older than the threshold and immediately removed. This matches the
478 // non-gateway publish ordering (cleanup before tagging), so the latest auto
479 // tag always survives.
480 if (args.find('c') != args.end()) {
481 retval = CleanupOldAutoTags(args, env.weak_ref());
482 if (retval != 0)
483 return retval;
484 }
485
486 if ((args.find('a') != args.end()) || (args.find('x') != args.end())) {
487 retval = AddNewTag(args, env.weak_ref());
488 if (retval != 0)
489 return retval;
490 }
491
492 // finalize processing and upload new history database
493 if (!CloseAndPublishHistory(env.weak_ref())) {
494 return 1;
495 }
496 return 0;
497 }
498
499 int CommandEditTag::CleanupOldAutoTags(const ArgumentList &args,
500 Environment *env) {
501 const time_t threshold = String2Int64(*args.find('c')->second);
502 if (threshold <= 0) {
503 LogCvmfs(kLogCvmfs, kLogStderr, "invalid cleanup threshold timestamp");
504 return 1;
505 }
506
507 // List all tags
508 typedef std::vector<history::History::Tag> TagList;
509 TagList tags;
510 if (!env->history->List(&tags)) {
511 LogCvmfs(kLogCvmfs, kLogStderr, "failed to list tags for auto cleanup");
512 return 1;
513 }
514
515 // Filter for auto-generated tags that are older than the threshold
516 std::vector<std::string> condemned_tags;
517 for (TagList::const_iterator i = tags.begin(); i != tags.end(); ++i) {
518 if (RepositoryTag::IsAutoGeneratedName(i->name) &&
519 i->timestamp < threshold) {
520 condemned_tags.push_back(i->name);
521 }
522 }
523
524 if (condemned_tags.empty()) {
525 LogCvmfs(kLogCvmfs, kLogDebug, "no outdated auto tags to clean up");
526 return 0;
527 }
528
529 LogCvmfs(kLogCvmfs, kLogStdout, "cleaning up %lu outdated auto tags",
530 condemned_tags.size());
531
532 // Delete the old auto tags
533 env->history->BeginTransaction();
534 for (std::vector<std::string>::const_iterator i = condemned_tags.begin();
535 i != condemned_tags.end(); ++i) {
536 LogCvmfs(kLogCvmfs, kLogStdout, "removing auto tag '%s'", i->c_str());
537 if (!env->history->Remove(*i)) {
538 LogCvmfs(kLogCvmfs, kLogStderr,
539 "failed to remove auto tag '%s' from history", i->c_str());
540 return 1;
541 }
542 }
543 const bool retval = env->history->PruneBranches();
544 if (!retval) {
545 LogCvmfs(kLogCvmfs, kLogStderr,
546 "failed to prune unused branches from history");
547 return 1;
548 }
549 env->history->CommitTransaction();
550 if (!env->history->Vacuum()) {
551 LogCvmfs(kLogCvmfs, kLogStderr, "failed to vacuum history after cleanup");
552 return 1;
553 }
554
555 return 0;
556 }
557
558 int CommandEditTag::AddNewTag(const ArgumentList &args, Environment *env) {
559 const std::string tag_name = (args.find('a') != args.end())
560 ? *args.find('a')->second
561 : "";
562 const std::string tag_description = (args.find('D') != args.end())
563 ? *args.find('D')->second
564 : "";
565 const bool undo_tags = (args.find('x') != args.end());
566 const std::string root_hash_string = (args.find('h') != args.end())
567 ? *args.find('h')->second
568 : "";
569 const std::string branch_name = (args.find('B') != args.end())
570 ? *args.find('B')->second
571 : "";
572 const std::string previous_branch_name = (args.find('P') != args.end())
573 ? *args.find('P')->second
574 : "";
575
576 if (tag_name.find(" ") != std::string::npos) {
577 LogCvmfs(kLogCvmfs, kLogStderr, "tag names must not contain spaces");
578 return 1;
579 }
580
581 assert(!tag_name.empty() || undo_tags);
582
583 if (IsUndoTagName(tag_name)) {
584 LogCvmfs(kLogCvmfs, kLogStderr, "undo tags are managed internally");
585 return 1;
586 }
587
588 // set the root hash to be tagged to the current HEAD if no other hash was
589 // given by the user
590 const shash::Any root_hash = GetTagRootHash(env, root_hash_string);
591 if (root_hash.IsNull()) {
592 return 1;
593 }
594
595 // open the catalog to be tagged (to check for existence and for meta info)
596 const UnlinkGuard catalog_path(
597 CreateTempPath(env->tmp_path + "/catalog", 0600));
598 const bool catalog_read_write = false;
599 const UniquePtr<catalog::Catalog> catalog(GetCatalog(
600 env->repository_url, root_hash, catalog_path.path(), catalog_read_write));
601 if (!catalog.IsValid()) {
602 LogCvmfs(kLogCvmfs, kLogStderr, "catalog with hash '%s' does not exist",
603 root_hash.ToString().c_str());
604 return 1;
605 }
606
607 // check if the catalog is a root catalog
608 if (!catalog->root_prefix().IsEmpty()) {
609 LogCvmfs(kLogCvmfs, kLogStderr,
610 "cannot tag catalog '%s' that is not a "
611 "root catalog.",
612 root_hash.ToString().c_str());
613 return 1;
614 }
615
616 // create a template for the new tag to be created, moved or used as undo tag
617 history::History::Tag tag_template;
618 tag_template.name = "<template>";
619 tag_template.root_hash = root_hash;
620 tag_template.size = GetFileSize(catalog_path.path());
621 tag_template.revision = catalog->GetRevision();
622 tag_template.timestamp = catalog->GetLastModified();
623 tag_template.branch = branch_name;
624 tag_template.description = tag_description;
625
626 // manipulate the tag database by creating a new tag or moving an existing one
627 if (!tag_name.empty()) {
628 tag_template.name = tag_name;
629 const bool user_provided_hash = (!root_hash_string.empty());
630
631 if (!env->history->ExistsBranch(tag_template.branch)) {
632 const history::History::Branch branch(
633 tag_template.branch, previous_branch_name, tag_template.revision);
634 if (!env->history->InsertBranch(branch)) {
635 LogCvmfs(kLogCvmfs, kLogStderr, "cannot insert branch '%s'",
636 tag_template.branch.c_str());
637 return 1;
638 }
639 }
640
641 if (!ManipulateTag(env, tag_template, user_provided_hash)) {
642 return 1;
643 }
644 }
645
646 // handle undo tags ('trunk' and 'trunk-previous') if necessary
647 if (undo_tags && !UpdateUndoTags(env, tag_template)) {
648 return 1;
649 }
650
651 return 0;
652 }
653
654 shash::Any CommandEditTag::GetTagRootHash(
655 Environment *env, const std::string &root_hash_string) const {
656 shash::Any root_hash;
657
658 if (root_hash_string.empty()) {
659 LogCvmfs(kLogCvmfs, kLogVerboseMsg,
660 "no catalog hash provided, using hash"
661 "of current HEAD catalog (%s)",
662 env->manifest->catalog_hash().ToString().c_str());
663 root_hash = env->manifest->catalog_hash();
664 } else {
665 root_hash = shash::MkFromHexPtr(shash::HexPtr(root_hash_string),
666 shash::kSuffixCatalog);
667 if (root_hash.IsNull()) {
668 LogCvmfs(kLogCvmfs, kLogStderr,
669 "failed to read provided catalog hash '%s'",
670 root_hash_string.c_str());
671 }
672 }
673
674 return root_hash;
675 }
676
677 bool CommandEditTag::ManipulateTag(Environment *env,
678 const history::History::Tag &tag_template,
679 const bool user_provided_hash) {
680 const std::string &tag_name = tag_template.name;
681
682 // check if the tag already exists, otherwise create it and return
683 if (!env->history->Exists(tag_name)) {
684 return CreateTag(env, tag_template);
685 }
686
687 // tag does exist already, now we need to see if we can move it
688 if (!user_provided_hash) {
689 LogCvmfs(kLogCvmfs, kLogStderr,
690 "a tag with the name '%s' already exists. Do you want to move it? "
691 "(-h <root hash>)",
692 tag_name.c_str());
693 return false;
694 }
695
696 // move the already existing tag and return
697 return MoveTag(env, tag_template);
698 }
699
700 bool CommandEditTag::MoveTag(Environment *env,
701 const history::History::Tag &tag_template) {
702 const std::string &tag_name = tag_template.name;
703 history::History::Tag new_tag = tag_template;
704
705 // get the already existent tag
706 history::History::Tag old_tag;
707 if (!env->history->GetByName(tag_name, &old_tag)) {
708 LogCvmfs(kLogCvmfs, kLogStderr, "failed to retrieve tag '%s' for moving",
709 tag_name.c_str());
710 return false;
711 }
712
713 // check if we would move the tag to the same hash
714 if (old_tag.root_hash == new_tag.root_hash) {
715 LogCvmfs(kLogCvmfs, kLogStderr, "tag '%s' already points to '%s'",
716 tag_name.c_str(), old_tag.root_hash.ToString().c_str());
717 return false;
718 }
719
720 // copy over old description if no new description was given
721 if (new_tag.description.empty()) {
722 new_tag.description = old_tag.description;
723 }
724 new_tag.branch = old_tag.branch;
725
726 // remove the old tag from the database
727 if (!env->history->Remove(tag_name)) {
728 LogCvmfs(kLogCvmfs, kLogStderr, "removing old tag '%s' before move failed",
729 tag_name.c_str());
730 return false;
731 }
732 if (!env->history->PruneBranches()) {
733 LogCvmfs(kLogCvmfs, kLogStderr, "could not prune unused branches");
734 return false;
735 }
736 const bool retval = env->history->Vacuum();
737 assert(retval);
738
739 LogCvmfs(kLogCvmfs, kLogStdout, "moving tag '%s' from '%s' to '%s'",
740 tag_name.c_str(), old_tag.root_hash.ToString().c_str(),
741 tag_template.root_hash.ToString().c_str());
742
743 // re-create the moved tag
744 return CreateTag(env, new_tag);
745 }
746
747 bool CommandEditTag::CreateTag(Environment *env,
748 const history::History::Tag &new_tag) {
749 if (!env->history->Insert(new_tag)) {
750 LogCvmfs(kLogCvmfs, kLogStderr, "failed to insert new tag '%s'",
751 new_tag.name.c_str());
752 return false;
753 }
754
755 return true;
756 }
757
758 int CommandEditTag::RemoveTags(const ArgumentList &args, Environment *env) {
759 typedef std::vector<std::string> TagNames;
760 const std::string tags_to_delete = *args.find('d')->second;
761
762 const TagNames condemned_tags = SplitString(tags_to_delete, ' ');
763
764 // check if user tries to remove a magic undo tag
765 TagNames::const_iterator i = condemned_tags.begin();
766 const TagNames::const_iterator iend = condemned_tags.end();
767 for (; i != iend; ++i) {
768 if (IsUndoTagName(*i)) {
769 LogCvmfs(kLogCvmfs, kLogStderr,
770 "undo tags are handled internally and cannot be deleted");
771 return 1;
772 }
773 }
774
775 LogCvmfs(kLogCvmfs, kLogDebug, "proceeding to delete %lu tags",
776 condemned_tags.size());
777
778 // check if the tags to be deleted exist
779 bool all_exist = true;
780 for (i = condemned_tags.begin(); i != iend; ++i) {
781 if (!env->history->Exists(*i)) {
782 LogCvmfs(kLogCvmfs, kLogStderr, "tag '%s' does not exist", i->c_str());
783 all_exist = false;
784 }
785 }
786 if (!all_exist) {
787 return 1;
788 }
789
790 // delete the tags from the tag database and print their root hashes
791 i = condemned_tags.begin();
792 env->history->BeginTransaction();
793 for (; i != iend; ++i) {
794 // print some information about the tag to be deleted
795 history::History::Tag condemned_tag;
796 const bool found_tag = env->history->GetByName(*i, &condemned_tag);
797 assert(found_tag);
798 LogCvmfs(kLogCvmfs, kLogStdout, "deleting '%s' (%s)",
799 condemned_tag.name.c_str(),
800 condemned_tag.root_hash.ToString().c_str());
801
802 // remove the tag
803 if (!env->history->Remove(*i)) {
804 LogCvmfs(kLogCvmfs, kLogStderr, "failed to remove tag '%s' from history",
805 i->c_str());
806 return 1;
807 }
808 }
809 bool retval = env->history->PruneBranches();
810 if (!retval) {
811 LogCvmfs(kLogCvmfs, kLogStderr,
812 "failed to prune unused branches from history");
813 return 1;
814 }
815 env->history->CommitTransaction();
816 retval = env->history->Vacuum();
817 assert(retval);
818
819 return 0;
820 }
821
822 //------------------------------------------------------------------------------
823
824
825 ParameterList CommandListTags::GetParams() const {
826 ParameterList r;
827 InsertCommonParameters(&r);
828 r.push_back(Parameter::Switch('x', "machine readable output"));
829 r.push_back(Parameter::Switch('B', "print branch hierarchy"));
830 return r;
831 }
832
833 void CommandListTags::PrintHumanReadableTagList(
834 const CommandListTags::TagList &tags) const {
835 // go through the list of tags and figure out the column widths
836 const std::string name_label = "Name";
837 const std::string rev_label = "Revision";
838 const std::string time_label = "Timestamp";
839 const std::string branch_label = "Branch";
840 const std::string desc_label = "Description";
841
842 // figure out the maximal lengths of the fields in the lists
843 TagList::const_reverse_iterator i = tags.rbegin();
844 const TagList::const_reverse_iterator iend = tags.rend();
845 size_t max_name_len = name_label.size();
846 size_t max_rev_len = rev_label.size();
847 size_t max_time_len = desc_label.size();
848 size_t max_branch_len = branch_label.size();
849 for (; i != iend; ++i) {
850 max_name_len = std::max(max_name_len, i->name.size());
851 max_rev_len = std::max(max_rev_len, StringifyInt(i->revision).size());
852 max_time_len = std::max(max_time_len,
853 StringifyTime(i->timestamp, true).size());
854 max_branch_len = std::max(max_branch_len, i->branch.size());
855 }
856
857 // print the list header
858 LogCvmfs(kLogCvmfs, kLogStdout, "%s \u2502 %s \u2502 %s \u2502 %s \u2502 %s",
859 AddPadding(name_label, max_name_len).c_str(),
860 AddPadding(rev_label, max_rev_len).c_str(),
861 AddPadding(time_label, max_time_len).c_str(),
862 AddPadding(branch_label, max_branch_len).c_str(),
863 desc_label.c_str());
864 LogCvmfs(kLogCvmfs, kLogStdout,
865 "%s\u2500\u253C\u2500%s\u2500\u253C\u2500%s"
866 "\u2500\u253C\u2500%s\u2500\u253C\u2500%s",
867 AddPadding("", max_name_len, false, "\u2500").c_str(),
868 AddPadding("", max_rev_len, false, "\u2500").c_str(),
869 AddPadding("", max_time_len, false, "\u2500").c_str(),
870 AddPadding("", max_branch_len, false, "\u2500").c_str(),
871 AddPadding("", desc_label.size() + 1, false, "\u2500").c_str());
872
873 // print the rows of the list
874 i = tags.rbegin();
875 for (; i != iend; ++i) {
876 LogCvmfs(
877 kLogCvmfs, kLogStdout, "%s \u2502 %s \u2502 %s \u2502 %s \u2502 %s",
878 AddPadding(i->name, max_name_len).c_str(),
879 AddPadding(StringifyInt(i->revision), max_rev_len, true).c_str(),
880 AddPadding(StringifyTime(i->timestamp, true), max_time_len).c_str(),
881 AddPadding(i->branch, max_branch_len).c_str(), i->description.c_str());
882 }
883
884 // print the list footer
885 LogCvmfs(kLogCvmfs, kLogStdout,
886 "%s\u2500\u2534\u2500%s\u2500\u2534\u2500%s"
887 "\u2500\u2534\u2500%s\u2500\u2534\u2500%s",
888 AddPadding("", max_name_len, false, "\u2500").c_str(),
889 AddPadding("", max_rev_len, false, "\u2500").c_str(),
890 AddPadding("", max_time_len, false, "\u2500").c_str(),
891 AddPadding("", max_branch_len, false, "\u2500").c_str(),
892 AddPadding("", desc_label.size() + 1, false, "\u2500").c_str());
893
894 // print the number of tags listed
895 LogCvmfs(kLogCvmfs, kLogStdout, "listing contains %lu tags", tags.size());
896 }
897
898 void CommandListTags::PrintMachineReadableTagList(const TagList &tags) const {
899 TagList::const_iterator i = tags.begin();
900 const TagList::const_iterator iend = tags.end();
901 for (; i != iend; ++i) {
902 PrintTagMachineReadable(*i);
903 }
904 }
905
906
907 void CommandListTags::PrintHumanReadableBranchList(
908 const BranchHierarchy &branches) const {
909 const unsigned N = branches.size();
910 for (unsigned i = 0; i < N; ++i) {
911 for (unsigned l = 0; l < branches[i].level; ++l) {
912 LogCvmfs(kLogCvmfs, kLogStdout | kLogNoLinebreak, "%s",
913 ((l + 1) == branches[i].level) ? "\u251c " : "\u2502 ");
914 }
915 LogCvmfs(kLogCvmfs, kLogStdout, "%s @%" PRIu64,
916 branches[i].branch.branch.c_str(),
917 branches[i].branch.initial_revision);
918 }
919 }
920
921
922 void CommandListTags::PrintMachineReadableBranchList(
923 const BranchHierarchy &branches) const {
924 const unsigned N = branches.size();
925 for (unsigned i = 0; i < N; ++i) {
926 LogCvmfs(kLogCvmfs, kLogStdout, "[%u] %s%s @%" PRIu64, branches[i].level,
927 AddPadding("", branches[i].level, false, " ").c_str(),
928 branches[i].branch.branch.c_str(),
929 branches[i].branch.initial_revision);
930 }
931 }
932
933
934 void CommandListTags::SortBranchesRecursively(
935 unsigned level,
936 const string &parent_branch,
937 const BranchList &branches,
938 BranchHierarchy *hierarchy) const {
939 // For large numbers of branches, this should be turned into the O(n) version
940 // using a linked list
941 const unsigned N = branches.size();
942 for (unsigned i = 0; i < N; ++i) {
943 if (branches[i].branch == "")
944 continue;
945 if (branches[i].parent == parent_branch) {
946 hierarchy->push_back(BranchLevel(branches[i], level));
947 SortBranchesRecursively(level + 1, branches[i].branch, branches,
948 hierarchy);
949 }
950 }
951 }
952
953
954 CommandListTags::BranchHierarchy CommandListTags::SortBranches(
955 const BranchList &branches) const {
956 BranchHierarchy hierarchy;
957 hierarchy.push_back(
958 BranchLevel(history::History::Branch("(default)", "", 0), 0));
959 SortBranchesRecursively(1, "", branches, &hierarchy);
960 return hierarchy;
961 }
962
963
964 int CommandListTags::Main(const ArgumentList &args) {
965 const bool machine_readable = (args.find('x') != args.end());
966 const bool branch_hierarchy = (args.find('B') != args.end());
967
968 // initialize the Environment (taking ownership)
969 const bool history_read_write = false;
970 const UniquePtr<Environment> env(
971 InitializeEnvironment(args, history_read_write));
972 if (!env.IsValid()) {
973 LogCvmfs(kLogCvmfs, kLogStderr, "failed to init environment");
974 return 1;
975 }
976
977 if (branch_hierarchy) {
978 BranchList branch_list;
979 if (!env->history->ListBranches(&branch_list)) {
980 LogCvmfs(kLogCvmfs, kLogStderr,
981 "failed to list branches in history database");
982 return 1;
983 }
984 const BranchHierarchy branch_hierarchy = SortBranches(branch_list);
985
986 if (machine_readable) {
987 PrintMachineReadableBranchList(branch_hierarchy);
988 } else {
989 PrintHumanReadableBranchList(branch_hierarchy);
990 }
991 } else {
992 // obtain a full list of all tags
993 TagList tags;
994 if (!env->history->List(&tags)) {
995 LogCvmfs(kLogCvmfs, kLogStderr,
996 "failed to list tags in history database");
997 return 1;
998 }
999
1000 if (machine_readable) {
1001 PrintMachineReadableTagList(tags);
1002 } else {
1003 PrintHumanReadableTagList(tags);
1004 }
1005 }
1006
1007 return 0;
1008 }
1009
1010 //------------------------------------------------------------------------------
1011
1012 ParameterList CommandInfoTag::GetParams() const {
1013 ParameterList r;
1014 InsertCommonParameters(&r);
1015
1016 r.push_back(Parameter::Mandatory('n', "name of the tag to be inspected"));
1017 r.push_back(Parameter::Switch('x', "machine readable output"));
1018 return r;
1019 }
1020
1021 std::string CommandInfoTag::HumanReadableFilesize(const size_t filesize) const {
1022 const size_t kiB = 1024;
1023 const size_t MiB = kiB * 1024;
1024 const size_t GiB = MiB * 1024;
1025
1026 if (filesize > GiB) {
1027 return StringifyDouble(static_cast<double>(filesize) / GiB) + " GiB";
1028 } else if (filesize > MiB) {
1029 return StringifyDouble(static_cast<double>(filesize) / MiB) + " MiB";
1030 } else if (filesize > kiB) {
1031 return StringifyDouble(static_cast<double>(filesize) / kiB) + " kiB";
1032 } else {
1033 return StringifyInt(filesize) + " Byte";
1034 }
1035 }
1036
1037 void CommandInfoTag::PrintHumanReadableInfo(
1038 const history::History::Tag &tag) const {
1039 LogCvmfs(kLogCvmfs, kLogStdout,
1040 "Name: %s\n"
1041 "Revision: %" PRIu64 "\n"
1042 "Timestamp: %s\n"
1043 "Branch: %s\n"
1044 "Root Hash: %s\n"
1045 "Catalog Size: %s\n"
1046 "%s",
1047 tag.name.c_str(), tag.revision,
1048 StringifyTime(tag.timestamp, true /* utc */).c_str(),
1049 tag.branch.c_str(), tag.root_hash.ToString().c_str(),
1050 HumanReadableFilesize(tag.size).c_str(), tag.description.c_str());
1051 }
1052
1053 int CommandInfoTag::Main(const ArgumentList &args) {
1054 const std::string tag_name = *args.find('n')->second;
1055 const bool machine_readable = (args.find('x') != args.end());
1056
1057 // initialize the Environment (taking ownership)
1058 const bool history_read_write = false;
1059 const UniquePtr<Environment> env(
1060 InitializeEnvironment(args, history_read_write));
1061 if (!env.IsValid()) {
1062 LogCvmfs(kLogCvmfs, kLogStderr, "failed to init environment");
1063 return 1;
1064 }
1065
1066 history::History::Tag tag;
1067 const bool found = env->history->GetByName(tag_name, &tag);
1068 if (!found) {
1069 LogCvmfs(kLogCvmfs, kLogStderr, "tag '%s' does not exist",
1070 tag_name.c_str());
1071 return 1;
1072 }
1073
1074 if (machine_readable) {
1075 PrintTagMachineReadable(tag);
1076 } else {
1077 PrintHumanReadableInfo(tag);
1078 }
1079
1080 return 0;
1081 }
1082
1083 //------------------------------------------------------------------------------
1084
1085 ParameterList CommandRollbackTag::GetParams() const {
1086 ParameterList r;
1087 InsertCommonParameters(&r);
1088
1089 r.push_back(Parameter::Optional('n', "name of the tag to be republished"));
1090 return r;
1091 }
1092
1093 int CommandRollbackTag::Main(const ArgumentList &args) {
1094 const bool undo_rollback = (args.find('n') == args.end());
1095 const std::string tag_name = (!undo_rollback) ? *args.find('n')->second
1096 : CommandTag::kPreviousHeadTag;
1097
1098 // initialize the Environment (taking ownership)
1099 const bool history_read_write = true;
1100 const UniquePtr<Environment> env(
1101 InitializeEnvironment(args, history_read_write));
1102 if (!env.IsValid()) {
1103 LogCvmfs(kLogCvmfs, kLogStderr, "failed to init environment");
1104 return 1;
1105 }
1106
1107 // find tag to be rolled back to
1108 history::History::Tag target_tag;
1109 const bool found = env->history->GetByName(tag_name, &target_tag);
1110 if (!found) {
1111 if (undo_rollback) {
1112 LogCvmfs(kLogCvmfs, kLogStderr,
1113 "only one anonymous rollback supported - "
1114 "perhaps you want to provide a tag name?");
1115 } else {
1116 LogCvmfs(kLogCvmfs, kLogStderr, "tag '%s' does not exist",
1117 tag_name.c_str());
1118 }
1119 return 1;
1120 }
1121 if (target_tag.branch != "") {
1122 LogCvmfs(kLogCvmfs, kLogStderr,
1123 "rollback is only supported on the default branch");
1124 return 1;
1125 }
1126
1127 // list the tags that will be deleted
1128 TagList affected_tags;
1129 if (!env->history->ListTagsAffectedByRollback(tag_name, &affected_tags)) {
1130 LogCvmfs(kLogCvmfs, kLogStderr,
1131 "failed to list condemned tags prior to rollback to '%s'",
1132 tag_name.c_str());
1133 return 1;
1134 }
1135
1136 // check if tag is valid to be rolled back to
1137 const uint64_t current_revision = env->manifest->revision();
1138 assert(target_tag.revision <= current_revision);
1139 if (target_tag.revision == current_revision) {
1140 LogCvmfs(kLogCvmfs, kLogStderr,
1141 "not rolling back to current head (%" PRIu64 ")",
1142 current_revision);
1143 return 1;
1144 }
1145
1146 // open the catalog to be rolled back to
1147 const UnlinkGuard catalog_path(
1148 CreateTempPath(env->tmp_path + "/catalog", 0600));
1149 const bool catalog_read_write = true;
1150 UniquePtr<catalog::WritableCatalog> catalog(
1151 dynamic_cast<catalog::WritableCatalog *>(
1152 GetCatalog(env->repository_url, target_tag.root_hash,
1153 catalog_path.path(), catalog_read_write)));
1154 if (!catalog.IsValid()) {
1155 LogCvmfs(kLogCvmfs, kLogStderr, "failed to open catalog with hash '%s'",
1156 target_tag.root_hash.ToString().c_str());
1157 return 1;
1158 }
1159
1160 // check if the catalog has a supported schema version
1161 if (catalog->schema() < catalog::CatalogDatabase::kLatestSupportedSchema
1162 - catalog::CatalogDatabase::kSchemaEpsilon) {
1163 LogCvmfs(kLogCvmfs, kLogStderr,
1164 "not rolling back to outdated and "
1165 "incompatible catalog schema (%.1f < %.1f)",
1166 catalog->schema(),
1167 catalog::CatalogDatabase::kLatestSupportedSchema);
1168 return 1;
1169 }
1170
1171 // update the catalog to be republished
1172 catalog->Transaction();
1173 catalog->UpdateLastModified();
1174 catalog->SetRevision(current_revision + 1);
1175 catalog->SetPreviousRevision(env->manifest->catalog_hash());
1176 catalog->Commit();
1177
1178 // Upload catalog (handing over ownership of catalog pointer)
1179 if (!UploadCatalogAndUpdateManifest(env.weak_ref(), catalog.Release())) {
1180 LogCvmfs(kLogCvmfs, kLogStderr, "catalog upload failed");
1181 return 1;
1182 }
1183
1184 // update target tag with newly published root catalog information
1185 history::History::Tag updated_target_tag(target_tag);
1186 updated_target_tag.root_hash = env->manifest->catalog_hash();
1187 updated_target_tag.size = env->manifest->catalog_size();
1188 updated_target_tag.revision = env->manifest->revision();
1189 updated_target_tag.timestamp = env->manifest->publish_timestamp();
1190 if (!env->history->Rollback(updated_target_tag)) {
1191 LogCvmfs(kLogCvmfs, kLogStderr, "failed to rollback history to '%s'",
1192 updated_target_tag.name.c_str());
1193 return 1;
1194 }
1195 const bool retval = env->history->Vacuum();
1196 assert(retval);
1197
1198 // set the magic undo tags
1199 if (!UpdateUndoTags(env.weak_ref(), updated_target_tag, undo_rollback)) {
1200 LogCvmfs(kLogCvmfs, kLogStderr, "failed to update magic undo tags");
1201 return 1;
1202 }
1203
1204 // finalize the history and upload it
1205 if (!CloseAndPublishHistory(env.weak_ref())) {
1206 return 1;
1207 }
1208
1209 // print the tags that have been removed by the rollback
1210 PrintDeletedTagList(affected_tags);
1211
1212 return 0;
1213 }
1214
1215 void CommandRollbackTag::PrintDeletedTagList(const TagList &tags) const {
1216 size_t longest_name = 0;
1217 TagList::const_iterator i = tags.begin();
1218 const TagList::const_iterator iend = tags.end();
1219 for (; i != iend; ++i) {
1220 longest_name = std::max(i->name.size(), longest_name);
1221 }
1222
1223 i = tags.begin();
1224 for (; i != iend; ++i) {
1225 LogCvmfs(kLogCvmfs, kLogStdout, "removed tag %s (%s)",
1226 AddPadding(i->name, longest_name).c_str(),
1227 i->root_hash.ToString().c_str());
1228 }
1229 }
1230
1231 //------------------------------------------------------------------------------
1232
1233 ParameterList CommandEmptyRecycleBin::GetParams() const {
1234 ParameterList r;
1235 InsertCommonParameters(&r);
1236 return r;
1237 }
1238
1239 int CommandEmptyRecycleBin::Main(const ArgumentList &args) {
1240 // initialize the Environment (taking ownership)
1241 const bool history_read_write = true;
1242 const UniquePtr<Environment> env(
1243 InitializeEnvironment(args, history_read_write));
1244 if (!env.IsValid()) {
1245 LogCvmfs(kLogCvmfs, kLogStderr, "failed to init environment");
1246 return 1;
1247 }
1248
1249 if (!env->history->EmptyRecycleBin()) {
1250 LogCvmfs(kLogCvmfs, kLogStderr, "failed to empty recycle bin");
1251 return 1;
1252 }
1253
1254 // finalize the history and upload it
1255 if (!CloseAndPublishHistory(env.weak_ref())) {
1256 return 1;
1257 }
1258
1259 return 0;
1260 }
1261