GCC Code Coverage Report


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