GCC Code Coverage Report


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