GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/manifest.cc
Date: 2025-08-31 02:39:21
Exec Total Coverage
Lines: 138 158 87.3%
Branches: 154 340 45.3%

Line Branch Exec Source
1 /**
2 * This file is part of the CernVM File System.
3 */
4
5 #include "manifest.h"
6
7 #include <cstdio>
8 #include <map>
9 #include <vector>
10
11 #include "catalog.h"
12 #include "util/posix.h"
13 #include "util/string.h"
14
15 using namespace std; // NOLINT
16
17 namespace manifest {
18
19 706 Breadcrumb::Breadcrumb(const std::string &from_string) {
20 706 timestamp = 0;
21 706 revision = 0; // for backward compatibility: no revision --> revision = 0
22
23
2/2
✓ Branch 1 taken 48 times.
✓ Branch 2 taken 658 times.
706 if (from_string.empty()) {
24 48 return;
25 }
26
27 // Separate hash from timestamp
28
1/2
✓ Branch 1 taken 658 times.
✗ Branch 2 not taken.
658 std::vector<std::string> vec_split_timestamp = SplitString(from_string, 'T');
29
30
1/2
✓ Branch 3 taken 658 times.
✗ Branch 4 not taken.
658 catalog_hash = shash::MkFromHexPtr(shash::HexPtr(vec_split_timestamp[0]),
31 shash::kSuffixCatalog);
32
33
2/2
✓ Branch 1 taken 562 times.
✓ Branch 2 taken 96 times.
658 if (vec_split_timestamp.size() > 1) {
34 // check if revision number is included
35 std::vector<std::string> vec_split_revision = SplitString(
36
1/2
✓ Branch 2 taken 562 times.
✗ Branch 3 not taken.
562 vec_split_timestamp[1], 'R');
37
38 // Get local last modified time
39
1/2
✓ Branch 2 taken 562 times.
✗ Branch 3 not taken.
562 timestamp = String2Uint64(vec_split_revision[0]);
40
41 // Get local revision
42
2/2
✓ Branch 1 taken 130 times.
✓ Branch 2 taken 432 times.
562 if (vec_split_revision.size() > 1) {
43
1/2
✓ Branch 2 taken 130 times.
✗ Branch 3 not taken.
130 revision = String2Uint64(vec_split_revision[1]);
44 }
45 562 }
46 658 }
47
48 126 bool Breadcrumb::Export(const string &fqrn, const string &directory,
49 const int mode) const {
50
1/2
✓ Branch 1 taken 126 times.
✗ Branch 2 not taken.
126 const string breadcrumb_path = MakeCanonicalPath(directory)
51
2/4
✓ Branch 1 taken 126 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 126 times.
✗ Branch 5 not taken.
252 + "/cvmfschecksum." + fqrn;
52 126 string tmp_path;
53
1/2
✓ Branch 1 taken 126 times.
✗ Branch 2 not taken.
126 FILE *fbreadcrumb = CreateTempFile(breadcrumb_path, mode, "w", &tmp_path);
54
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 126 times.
126 if (fbreadcrumb == NULL)
55 return false;
56
1/2
✓ Branch 1 taken 126 times.
✗ Branch 2 not taken.
126 string str_breadcrumb = ToString();
57
2/4
✓ Branch 2 taken 126 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 126 times.
✗ Branch 6 not taken.
126 const int written = fwrite(&(str_breadcrumb[0]), 1, str_breadcrumb.length(),
58 126 fbreadcrumb);
59
1/2
✓ Branch 1 taken 126 times.
✗ Branch 2 not taken.
126 fclose(fbreadcrumb);
60
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 126 times.
126 if (static_cast<unsigned>(written) != str_breadcrumb.length()) {
61 unlink(tmp_path.c_str());
62 return false;
63 }
64 126 const int retval = rename(tmp_path.c_str(), breadcrumb_path.c_str());
65
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 126 times.
126 if (retval != 0) {
66 unlink(tmp_path.c_str());
67 return false;
68 }
69 126 return true;
70 126 }
71
72 330 std::string Breadcrumb::ToString() const {
73
2/4
✓ Branch 1 taken 330 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 330 times.
✗ Branch 5 not taken.
660 return catalog_hash.ToString() + "T"
74
3/6
✓ Branch 1 taken 330 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 330 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 330 times.
✗ Branch 8 not taken.
1320 + StringifyInt(static_cast<int64_t>(timestamp)) + "R"
75
1/2
✓ Branch 2 taken 330 times.
✗ Branch 3 not taken.
990 + StringifyUint(revision);
76 }
77
78
79 //------------------------------------------------------------------------------
80
81
82 283 Manifest *Manifest::LoadMem(const unsigned char *buffer,
83 const unsigned length) {
84 283 map<char, string> content;
85
1/2
✓ Branch 1 taken 283 times.
✗ Branch 2 not taken.
283 ParseKeyvalMem(buffer, length, &content);
86
87
1/2
✓ Branch 1 taken 283 times.
✗ Branch 2 not taken.
566 return Load(content);
88 283 }
89
90
91 397 Manifest *Manifest::LoadFile(const std::string &from_path) {
92 397 map<char, string> content;
93
2/4
✓ Branch 1 taken 397 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 397 times.
397 if (!ParseKeyvalPath(from_path, &content))
94 return NULL;
95
96
1/2
✓ Branch 1 taken 397 times.
✗ Branch 2 not taken.
397 return Load(content);
97 397 }
98
99
100 680 Manifest *Manifest::Load(const map<char, string> &content) {
101 680 map<char, string>::const_iterator iter;
102
103 // Required keys
104
1/2
✓ Branch 1 taken 680 times.
✗ Branch 2 not taken.
680 shash::Any catalog_hash;
105
1/2
✓ Branch 1 taken 680 times.
✗ Branch 2 not taken.
680 shash::Md5 root_path;
106 uint32_t ttl;
107 uint64_t revision;
108
109
1/2
✓ Branch 1 taken 680 times.
✗ Branch 2 not taken.
680 iter = content.find('C');
110
2/5
✓ Branch 2 taken 680 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 680 times.
680 if ((iter = content.find('C')) == content.end())
111 return NULL;
112
1/2
✓ Branch 3 taken 680 times.
✗ Branch 4 not taken.
680 catalog_hash = MkFromHexPtr(shash::HexPtr(iter->second),
113 shash::kSuffixCatalog);
114
2/5
✓ Branch 2 taken 680 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 680 times.
680 if ((iter = content.find('R')) == content.end())
115 return NULL;
116
1/2
✓ Branch 3 taken 680 times.
✗ Branch 4 not taken.
680 root_path = shash::Md5(shash::HexPtr(iter->second));
117
2/5
✓ Branch 2 taken 680 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 680 times.
680 if ((iter = content.find('D')) == content.end())
118 return NULL;
119
1/2
✓ Branch 2 taken 680 times.
✗ Branch 3 not taken.
680 ttl = String2Uint64(iter->second);
120
2/5
✓ Branch 2 taken 680 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 680 times.
680 if ((iter = content.find('S')) == content.end())
121 return NULL;
122
1/2
✓ Branch 2 taken 680 times.
✗ Branch 3 not taken.
680 revision = String2Uint64(iter->second);
123
124
125 // Optional keys
126 680 uint64_t catalog_size = 0;
127
1/2
✓ Branch 1 taken 680 times.
✗ Branch 2 not taken.
680 shash::Any micro_catalog_hash;
128 680 string repository_name;
129
1/2
✓ Branch 1 taken 680 times.
✗ Branch 2 not taken.
680 shash::Any certificate;
130
1/2
✓ Branch 1 taken 680 times.
✗ Branch 2 not taken.
680 shash::Any history;
131 680 uint64_t publish_timestamp = 0;
132 680 bool garbage_collectable = false;
133 680 bool has_alt_catalog_path = false;
134
1/2
✓ Branch 1 taken 680 times.
✗ Branch 2 not taken.
680 shash::Any meta_info;
135
1/2
✓ Branch 1 taken 680 times.
✗ Branch 2 not taken.
680 shash::Any reflog_hash;
136
137
2/5
✓ Branch 2 taken 680 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 680 times.
✗ Branch 6 not taken.
680 if ((iter = content.find('B')) != content.end())
138
1/2
✓ Branch 2 taken 680 times.
✗ Branch 3 not taken.
680 catalog_size = String2Uint64(iter->second);
139
2/5
✓ Branch 2 taken 680 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 680 times.
680 if ((iter = content.find('L')) != content.end())
140 micro_catalog_hash = MkFromHexPtr(shash::HexPtr(iter->second),
141 shash::kSuffixMicroCatalog);
142
2/5
✓ Branch 2 taken 680 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 680 times.
✗ Branch 6 not taken.
680 if ((iter = content.find('N')) != content.end())
143
1/2
✓ Branch 2 taken 680 times.
✗ Branch 3 not taken.
680 repository_name = iter->second;
144
2/5
✓ Branch 2 taken 680 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 680 times.
✗ Branch 6 not taken.
680 if ((iter = content.find('X')) != content.end())
145
1/2
✓ Branch 3 taken 680 times.
✗ Branch 4 not taken.
680 certificate = MkFromHexPtr(shash::HexPtr(iter->second),
146 shash::kSuffixCertificate);
147
2/5
✓ Branch 2 taken 680 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 680 times.
✗ Branch 6 not taken.
680 if ((iter = content.find('H')) != content.end())
148
1/2
✓ Branch 3 taken 680 times.
✗ Branch 4 not taken.
680 history = MkFromHexPtr(shash::HexPtr(iter->second), shash::kSuffixHistory);
149
3/5
✓ Branch 2 taken 680 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 281 times.
✓ Branch 6 taken 399 times.
680 if ((iter = content.find('T')) != content.end())
150
1/2
✓ Branch 2 taken 281 times.
✗ Branch 3 not taken.
281 publish_timestamp = String2Uint64(iter->second);
151
2/5
✓ Branch 2 taken 680 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 680 times.
✗ Branch 6 not taken.
680 if ((iter = content.find('G')) != content.end())
152 680 garbage_collectable = (iter->second == "yes");
153
2/5
✓ Branch 2 taken 680 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 680 times.
✗ Branch 6 not taken.
680 if ((iter = content.find('A')) != content.end())
154 680 has_alt_catalog_path = (iter->second == "yes");
155
2/5
✓ Branch 2 taken 680 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 680 times.
680 if ((iter = content.find('M')) != content.end())
156 meta_info = MkFromHexPtr(shash::HexPtr(iter->second),
157 shash::kSuffixMetainfo);
158
2/5
✓ Branch 2 taken 680 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 680 times.
680 if ((iter = content.find('Y')) != content.end()) {
159 reflog_hash = MkFromHexPtr(shash::HexPtr(iter->second));
160 }
161
162 return new Manifest(catalog_hash, catalog_size, root_path, ttl, revision,
163 micro_catalog_hash, repository_name, certificate, history,
164 publish_timestamp, garbage_collectable,
165
2/4
✓ Branch 1 taken 680 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 680 times.
✗ Branch 5 not taken.
680 has_alt_catalog_path, meta_info, reflog_hash);
166 680 }
167
168
169 3547 Manifest::Manifest(const shash::Any &catalog_hash,
170 const uint64_t catalog_size,
171 3547 const string &root_path)
172 3547 : catalog_hash_(catalog_hash)
173 3547 , catalog_size_(catalog_size)
174
1/2
✓ Branch 2 taken 3547 times.
✗ Branch 3 not taken.
3547 , root_path_(shash::Md5(shash::AsciiPtr(root_path)))
175 3547 , ttl_(catalog::Catalog::kDefaultTTL)
176 3547 , revision_(0)
177 3547 , publish_timestamp_(0)
178 3547 , garbage_collectable_(false)
179
4/8
✓ Branch 3 taken 3547 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 3547 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 3547 times.
✗ Branch 10 not taken.
✓ Branch 12 taken 3547 times.
✗ Branch 13 not taken.
3547 , has_alt_catalog_path_(false) { }
180
181
182 /**
183 * Creates the manifest string
184 */
185 1362 string Manifest::ExportString() const {
186
4/8
✓ Branch 1 taken 1362 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1362 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1362 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 1362 times.
✗ Branch 11 not taken.
2724 string manifest = "C" + catalog_hash_.ToString() + "\n" + "B"
187
4/8
✓ Branch 1 taken 1362 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1362 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1362 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 1362 times.
✗ Branch 11 not taken.
5448 + StringifyInt(catalog_size_) + "\n" + "R"
188
6/12
✓ Branch 1 taken 1362 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1362 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1362 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 1362 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 1362 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 1362 times.
✗ Branch 17 not taken.
5448 + root_path_.ToString() + "\n" + "D" + StringifyInt(ttl_)
189
6/12
✓ Branch 1 taken 1362 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1362 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1362 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 1362 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 1362 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 1362 times.
✗ Branch 17 not taken.
5448 + "\n" + "S" + StringifyInt(revision_) + "\n" + "G"
190
4/8
✓ Branch 1 taken 1362 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1362 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1362 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 1362 times.
✗ Branch 11 not taken.
5448 + StringifyBool(garbage_collectable_) + "\n" + "A"
191
2/4
✓ Branch 2 taken 1362 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1362 times.
✗ Branch 6 not taken.
4086 + StringifyBool(has_alt_catalog_path_) + "\n";
192
193
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1362 times.
1362 if (!micro_catalog_hash_.IsNull())
194 manifest += "L" + micro_catalog_hash_.ToString() + "\n";
195
1/2
✓ Branch 1 taken 1362 times.
✗ Branch 2 not taken.
1362 if (repository_name_ != "")
196
3/6
✓ Branch 1 taken 1362 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1362 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1362 times.
✗ Branch 8 not taken.
1362 manifest += "N" + repository_name_ + "\n";
197
1/2
✓ Branch 1 taken 1362 times.
✗ Branch 2 not taken.
1362 if (!certificate_.IsNull())
198
4/8
✓ Branch 1 taken 1362 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1362 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1362 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 1362 times.
✗ Branch 11 not taken.
1362 manifest += "X" + certificate_.ToString() + "\n";
199
1/2
✓ Branch 1 taken 1362 times.
✗ Branch 2 not taken.
1362 if (!history_.IsNull())
200
4/8
✓ Branch 1 taken 1362 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1362 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1362 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 1362 times.
✗ Branch 11 not taken.
1362 manifest += "H" + history_.ToString() + "\n";
201
2/2
✓ Branch 0 taken 792 times.
✓ Branch 1 taken 570 times.
1362 if (publish_timestamp_ > 0)
202
4/8
✓ Branch 1 taken 792 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 792 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 792 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 792 times.
✗ Branch 11 not taken.
792 manifest += "T" + StringifyInt(publish_timestamp_) + "\n";
203
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1362 times.
1362 if (!meta_info_.IsNull())
204 manifest += "M" + meta_info_.ToString() + "\n";
205
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1362 times.
1362 if (!reflog_hash_.IsNull()) {
206 manifest += "Y" + reflog_hash_.ToString() + "\n";
207 }
208 // Reserved: Z -> for identification of channel tips
209
210 1362 return manifest;
211 }
212
213
214 /**
215 * Writes the .cvmfspublished file (unsigned).
216 */
217 96 bool Manifest::Export(const std::string &path) const {
218
1/2
✓ Branch 2 taken 96 times.
✗ Branch 3 not taken.
96 FILE *fmanifest = fopen(path.c_str(), "w");
219
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 96 times.
96 if (!fmanifest)
220 return false;
221
222
1/2
✓ Branch 1 taken 96 times.
✗ Branch 2 not taken.
96 string manifest = ExportString();
223
224
1/2
✓ Branch 3 taken 96 times.
✗ Branch 4 not taken.
96 if (fwrite(manifest.data(), 1, manifest.length(), fmanifest)
225
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 96 times.
96 != manifest.length()) {
226 fclose(fmanifest);
227 unlink(path.c_str());
228 return false;
229 }
230
1/2
✓ Branch 1 taken 96 times.
✗ Branch 2 not taken.
96 fclose(fmanifest);
231
232 96 return true;
233 96 }
234
235
236 /**
237 * Writes the cvmfschecksum.$repository file. Atomic store.
238 */
239 126 bool Manifest::ExportBreadcrumb(const string &directory, const int mode) const {
240 252 return Breadcrumb(catalog_hash_, publish_timestamp_, revision_)
241
1/2
✓ Branch 1 taken 126 times.
✗ Branch 2 not taken.
252 .Export(repository_name_, directory, mode);
242 }
243
244
245 /**
246 * Read the hash and the last-modified time stamp from the
247 * cvmfschecksum.$repository file in the given directory.
248 */
249 535 Breadcrumb Manifest::ReadBreadcrumb(const std::string &repo_name,
250 const std::string &directory) {
251
1/2
✓ Branch 1 taken 535 times.
✗ Branch 2 not taken.
535 Breadcrumb breadcrumb;
252
2/4
✓ Branch 1 taken 535 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 535 times.
✗ Branch 5 not taken.
535 const string breadcrumb_path = directory + "/cvmfschecksum." + repo_name;
253
1/2
✓ Branch 2 taken 535 times.
✗ Branch 3 not taken.
535 FILE *fbreadcrumb = fopen(breadcrumb_path.c_str(), "r");
254
2/2
✓ Branch 0 taken 261 times.
✓ Branch 1 taken 274 times.
535 if (!fbreadcrumb) {
255 // Return invalid breadcrumb if not found
256 261 return breadcrumb;
257 }
258 char tmp[164];
259
1/2
✓ Branch 1 taken 274 times.
✗ Branch 2 not taken.
274 const size_t read_bytes = fread(tmp, 1, 164, fbreadcrumb);
260
2/2
✓ Branch 0 taken 226 times.
✓ Branch 1 taken 48 times.
274 if (read_bytes > 0) {
261
2/4
✓ Branch 2 taken 226 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 226 times.
✗ Branch 6 not taken.
226 breadcrumb = Breadcrumb(std::string(tmp, read_bytes));
262 }
263
1/2
✓ Branch 1 taken 274 times.
✗ Branch 2 not taken.
274 fclose(fbreadcrumb);
264
265 274 return breadcrumb;
266 535 }
267
268 } // namespace manifest
269