GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/swissknife_assistant.cc
Date: 2025-06-22 02:36:02
Exec Total Coverage
Lines: 0 59 0.0%
Branches: 0 87 0.0%

Line Branch Exec Source
1 /**
2 * This file is part of the CernVM File System
3 */
4
5 #include "swissknife_assistant.h"
6
7 #include <unistd.h>
8
9 #include <cassert>
10 #include <cstdlib>
11
12 #include "catalog.h"
13 #include "catalog_rw.h"
14 #include "history.h"
15 #include "history_sqlite.h"
16 #include "manifest.h"
17 #include "network/download.h"
18 #include "util/logging.h"
19 #include "util/posix.h"
20
21 using namespace std; // NOLINT
22
23 namespace swissknife {
24
25 catalog::Catalog *Assistant::GetCatalog(const shash::Any &catalog_hash,
26 OpenMode open_mode) {
27 assert(shash::kSuffixCatalog == catalog_hash.suffix);
28 const string local_path = CreateTempPath(tmp_dir_ + "/catalog", 0600);
29 assert(!local_path.empty());
30
31 if (!FetchObject(catalog_hash, local_path)) {
32 return NULL;
33 }
34
35 const std::string catalog_root_path = "";
36 catalog::Catalog *catalog;
37 switch (open_mode) {
38 case kOpenReadWrite:
39 catalog = catalog::WritableCatalog::AttachFreely(
40 catalog_root_path, local_path, catalog_hash);
41 break;
42 case kOpenReadOnly:
43 catalog = catalog::Catalog::AttachFreely(
44 catalog_root_path, local_path, catalog_hash);
45 break;
46 default:
47 abort();
48 }
49 assert(catalog != NULL);
50 catalog->TakeDatabaseFileOwnership();
51 return catalog;
52 }
53
54
55 history::History *Assistant::GetHistory(OpenMode open_mode) {
56 const shash::Any history_hash = manifest_->history();
57 history::History *history;
58
59 const string local_path = CreateTempPath(tmp_dir_ + "/history", 0600);
60 assert(!local_path.empty());
61
62 if (history_hash.IsNull()) {
63 history = history::SqliteHistory::Create(local_path,
64 manifest_->repository_name());
65 if (NULL == history) {
66 LogCvmfs(kLogCvmfs, kLogStderr, "failed to create history database");
67 return NULL;
68 }
69 return history;
70 }
71
72 if (!FetchObject(history_hash, local_path))
73 return NULL;
74
75 switch (open_mode) {
76 case kOpenReadWrite:
77 history = history::SqliteHistory::OpenWritable(local_path);
78 break;
79 case kOpenReadOnly:
80 history = history::SqliteHistory::Open(local_path);
81 break;
82 default:
83 abort();
84 }
85
86 if (history == NULL) {
87 LogCvmfs(kLogCvmfs, kLogStderr, "failed to open history database (%s)",
88 local_path.c_str());
89 unlink(local_path.c_str());
90 return NULL;
91 }
92 assert(history->fqrn() == manifest_->repository_name());
93 history->TakeDatabaseFileOwnership();
94 return history;
95 }
96
97
98 bool Assistant::FetchObject(const shash::Any &id, const string &local_path) {
99 assert(!id.IsNull());
100
101 download::Failures dl_retval;
102 const std::string url = repository_url_ + "/data/" + id.MakePath();
103
104 cvmfs::PathSink pathsink(local_path);
105 download::JobInfo download_info(&url, true, false, &id, &pathsink);
106 dl_retval = download_mgr_->Fetch(&download_info);
107
108 if (dl_retval != download::kFailOk) {
109 LogCvmfs(kLogCvmfs, kLogStderr, "failed to download object '%s' (%d - %s)",
110 id.ToStringWithSuffix().c_str(), dl_retval,
111 download::Code2Ascii(dl_retval));
112 return false;
113 }
114 return true;
115 }
116
117 } // namespace swissknife
118