CernVM-FS  2.12.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
manifest.cc
Go to the documentation of this file.
1 
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 Breadcrumb::Breadcrumb(const std::string &from_string) {
20  timestamp = 0;
21  revision = 0; // for backward compatibility: no revision --> revision = 0
22 
23  if (from_string.empty()) {
24  return;
25  }
26 
27  // Separate hash from timestamp
28  std::vector<std::string> vec_split_timestamp = SplitString(from_string, 'T');
29 
30  catalog_hash = shash::MkFromHexPtr(shash::HexPtr(vec_split_timestamp[0]),
32 
33  if (vec_split_timestamp.size() > 1) {
34  // check if revision number is included
35  std::vector<std::string> vec_split_revision =
36  SplitString(vec_split_timestamp[1], 'R');
37 
38  // Get local last modified time
39  timestamp = String2Uint64(vec_split_revision[0]);
40 
41  // Get local revision
42  if (vec_split_revision.size() > 1) {
43  revision = String2Uint64(vec_split_revision[1]);
44  }
45  }
46 }
47 
48 bool Breadcrumb::Export(const string &fqrn, const string &directory,
49  const int mode) const {
50  string breadcrumb_path = MakeCanonicalPath(directory) +
51  "/cvmfschecksum." + fqrn;
52  string tmp_path;
53  FILE *fbreadcrumb = CreateTempFile(breadcrumb_path, mode, "w", &tmp_path);
54  if (fbreadcrumb == NULL)
55  return false;
56  string str_breadcrumb = ToString();
57  int written = fwrite(&(str_breadcrumb[0]), 1, str_breadcrumb.length(),
58  fbreadcrumb);
59  fclose(fbreadcrumb);
60  if (static_cast<unsigned>(written) != str_breadcrumb.length()) {
61  unlink(tmp_path.c_str());
62  return false;
63  }
64  int retval = rename(tmp_path.c_str(), breadcrumb_path.c_str());
65  if (retval != 0) {
66  unlink(tmp_path.c_str());
67  return false;
68  }
69  return true;
70 }
71 
72 std::string Breadcrumb::ToString() const {
73  return catalog_hash.ToString()
74  + "T" + StringifyInt(static_cast<int64_t>(timestamp))
75  + "R" + StringifyUint(revision);
76 }
77 
78 
79 //------------------------------------------------------------------------------
80 
81 
82 Manifest *Manifest::LoadMem(const unsigned char *buffer,
83  const unsigned length)
84 {
85  map<char, string> content;
86  ParseKeyvalMem(buffer, length, &content);
87 
88  return Load(content);
89 }
90 
91 
92 Manifest *Manifest::LoadFile(const std::string &from_path) {
93  map<char, string> content;
94  if (!ParseKeyvalPath(from_path, &content))
95  return NULL;
96 
97  return Load(content);
98 }
99 
100 
101 Manifest *Manifest::Load(const map<char, string> &content) {
102  map<char, string>::const_iterator iter;
103 
104  // Required keys
105  shash::Any catalog_hash;
106  shash::Md5 root_path;
107  uint32_t ttl;
108  uint64_t revision;
109 
110  iter = content.find('C');
111  if ((iter = content.find('C')) == content.end())
112  return NULL;
113  catalog_hash = MkFromHexPtr(shash::HexPtr(iter->second),
115  if ((iter = content.find('R')) == content.end())
116  return NULL;
117  root_path = shash::Md5(shash::HexPtr(iter->second));
118  if ((iter = content.find('D')) == content.end())
119  return NULL;
120  ttl = String2Uint64(iter->second);
121  if ((iter = content.find('S')) == content.end())
122  return NULL;
123  revision = String2Uint64(iter->second);
124 
125 
126  // Optional keys
127  uint64_t catalog_size = 0;
128  shash::Any micro_catalog_hash;
129  string repository_name;
130  shash::Any certificate;
132  uint64_t publish_timestamp = 0;
133  bool garbage_collectable = false;
134  bool has_alt_catalog_path = false;
136  shash::Any reflog_hash;
137 
138  if ((iter = content.find('B')) != content.end())
139  catalog_size = String2Uint64(iter->second);
140  if ((iter = content.find('L')) != content.end())
141  micro_catalog_hash = MkFromHexPtr(shash::HexPtr(iter->second),
143  if ((iter = content.find('N')) != content.end())
144  repository_name = iter->second;
145  if ((iter = content.find('X')) != content.end())
146  certificate = MkFromHexPtr(shash::HexPtr(iter->second),
148  if ((iter = content.find('H')) != content.end())
149  history = MkFromHexPtr(shash::HexPtr(iter->second),
151  if ((iter = content.find('T')) != content.end())
152  publish_timestamp = String2Uint64(iter->second);
153  if ((iter = content.find('G')) != content.end())
154  garbage_collectable = (iter->second == "yes");
155  if ((iter = content.find('A')) != content.end())
156  has_alt_catalog_path = (iter->second == "yes");
157  if ((iter = content.find('M')) != content.end())
158  meta_info = MkFromHexPtr(shash::HexPtr(iter->second),
160  if ((iter = content.find('Y')) != content.end()) {
161  reflog_hash = MkFromHexPtr(shash::HexPtr(iter->second));
162  }
163 
164  return new Manifest(catalog_hash, catalog_size, root_path, ttl, revision,
165  micro_catalog_hash, repository_name, certificate,
166  history, publish_timestamp, garbage_collectable,
167  has_alt_catalog_path, meta_info, reflog_hash);
168 }
169 
170 
171 Manifest::Manifest(const shash::Any &catalog_hash,
172  const uint64_t catalog_size,
173  const string &root_path)
174  : catalog_hash_(catalog_hash)
175  , catalog_size_(catalog_size)
176  , root_path_(shash::Md5(shash::AsciiPtr(root_path)))
177  , ttl_(catalog::Catalog::kDefaultTTL)
178  , revision_(0)
179  , publish_timestamp_(0)
180  , garbage_collectable_(false)
181  , has_alt_catalog_path_(false)
182 { }
183 
184 
188 string Manifest::ExportString() const {
189  string manifest =
190  "C" + catalog_hash_.ToString() + "\n" +
191  "B" + StringifyInt(catalog_size_) + "\n" +
192  "R" + root_path_.ToString() + "\n" +
193  "D" + StringifyInt(ttl_) + "\n" +
194  "S" + StringifyInt(revision_) + "\n" +
195  "G" + StringifyBool(garbage_collectable_) + "\n" +
196  "A" + StringifyBool(has_alt_catalog_path_) + "\n";
197 
199  manifest += "L" + micro_catalog_hash_.ToString() + "\n";
200  if (repository_name_ != "")
201  manifest += "N" + repository_name_ + "\n";
202  if (!certificate_.IsNull())
203  manifest += "X" + certificate_.ToString() + "\n";
204  if (!history_.IsNull())
205  manifest += "H" + history_.ToString() + "\n";
206  if (publish_timestamp_ > 0)
207  manifest += "T" + StringifyInt(publish_timestamp_) + "\n";
208  if (!meta_info_.IsNull())
209  manifest += "M" + meta_info_.ToString() + "\n";
210  if (!reflog_hash_.IsNull()) {
211  manifest += "Y" + reflog_hash_.ToString() + "\n";
212  }
213  // Reserved: Z -> for identification of channel tips
214 
215  return manifest;
216 }
217 
218 
219 
223 bool Manifest::Export(const std::string &path) const {
224  FILE *fmanifest = fopen(path.c_str(), "w");
225  if (!fmanifest)
226  return false;
227 
228  string manifest = ExportString();
229 
230  if (fwrite(manifest.data(), 1, manifest.length(), fmanifest) !=
231  manifest.length())
232  {
233  fclose(fmanifest);
234  unlink(path.c_str());
235  return false;
236  }
237  fclose(fmanifest);
238 
239  return true;
240 }
241 
242 
246 bool Manifest::ExportBreadcrumb(const string &directory, const int mode) const {
248  Export(repository_name_, directory, mode);
249 }
250 
251 
257  const std::string &repo_name,
258  const std::string &directory)
259 {
260  Breadcrumb breadcrumb;
261  const string breadcrumb_path = directory + "/cvmfschecksum." + repo_name;
262  FILE *fbreadcrumb = fopen(breadcrumb_path.c_str(), "r");
263  if (!fbreadcrumb) {
264  // Return invalid breadcrumb if not found
265  return breadcrumb;
266  }
267  char tmp[164];
268  const size_t read_bytes = fread(tmp, 1, 164, fbreadcrumb);
269  if (read_bytes > 0) {
270  breadcrumb = Breadcrumb(std::string(tmp, read_bytes));
271  }
272  fclose(fbreadcrumb);
273 
274  return breadcrumb;
275 }
276 
277 } // namespace manifest
bool IsNull() const
Definition: hash.h:383
shash::Any micro_catalog_hash_
Definition: manifest.h:159
const manifest::Manifest * manifest() const
Definition: repository.h:125
bool Export(const std::string &path) const
Definition: manifest.cc:223
shash::Any meta_info_
Definition: manifest.h:177
shash::Any certificate_
Definition: manifest.h:161
shash::Md5 root_path_
Definition: manifest.h:156
FILE * CreateTempFile(const std::string &path_prefix, const int mode, const char *open_flags, std::string *final_path)
Definition: posix.cc:1005
const char kSuffixCertificate
Definition: hash.h:59
std::string ToString(const bool with_suffix=false) const
Definition: hash.h:249
const history::History * history() const
bool garbage_collectable_
Definition: manifest.h:164
const char kSuffixMicroCatalog
Definition: hash.h:56
std::string ExportString() const
Definition: manifest.cc:188
shash::Any reflog_hash_
Definition: manifest.h:182
std::string StringifyUint(const uint64_t value)
Definition: string.cc:84
string StringifyBool(const bool value)
Definition: string.cc:76
uint64_t revision_
Definition: manifest.h:158
bool ExportBreadcrumb(const std::string &directory, const int mode) const
Definition: manifest.cc:246
vector< string > SplitString(const string &str, char delim)
Definition: string.cc:290
const char kSuffixCatalog
Definition: hash.h:54
string StringifyInt(const int64_t value)
Definition: string.cc:78
const char kSuffixMetainfo
Definition: hash.h:60
bool has_alt_catalog_path_
Definition: manifest.h:171
std::string repository_name_
Definition: manifest.h:160
const char kSuffixHistory
Definition: hash.h:55
uint64_t String2Uint64(const string &value)
Definition: string.cc:228
bool ParseKeyvalPath(const string &filename, map< char, string > *content)
Definition: string.cc:366
Any MkFromHexPtr(const HexPtr hex, const char suffix)
Definition: hash.cc:83
std::string meta_info() const
Definition: repository.h:128
static Breadcrumb ReadBreadcrumb(const std::string &repo_name, const std::string &directory)
Definition: manifest.cc:256
shash::Any catalog_hash_
Definition: manifest.h:154
shash::Any history_
Definition: manifest.h:162
std::string MakeCanonicalPath(const std::string &path)
Definition: posix.cc:98
uint64_t publish_timestamp_
Definition: manifest.h:163
uint64_t catalog_size_
Definition: manifest.h:155
void ParseKeyvalMem(const unsigned char *buffer, const unsigned buffer_size, map< char, string > *content)
Definition: string.cc:337