CernVM-FS  2.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
swissknife_list_reflog.cc
Go to the documentation of this file.
1 
8 #include "cvmfs_config.h"
10 
11 #include "manifest.h"
12 #include "object_fetcher.h"
13 #include "reflog.h"
14 #include "util/posix.h"
15 #include "util/string.h"
16 
17 using namespace std; // NOLINT
18 
19 namespace swissknife {
20 
21 ParameterList CommandListReflog::GetParams() const {
22  ParameterList r;
23  r.push_back(Parameter::Mandatory('r', "repository url / local storage path"));
24  r.push_back(Parameter::Mandatory('n', "fully qualified repository name"));
25  r.push_back(Parameter::Optional('R', "path to reflog.chksum file"));
26  r.push_back(Parameter::Optional('k', "repository master key(s) / dir"));
27  r.push_back(Parameter::Optional('t', "temporary directory"));
28  r.push_back(Parameter::Optional('o', "output file"));
29  return r;
30 }
31 
32 int CommandListReflog::Main(const ArgumentList &args) {
33  const string &repo_url = *args.find('r')->second;
34  const string &repo_name = *args.find('n')->second;
35  const std::string &reflog_chksum_path = (args.count('R') > 0) ?
36  *args.find('R')->second : "";
37  string repo_keys = (args.count('k') > 0) ?
38  *args.find('k')->second : "";
39  if (DirectoryExists(repo_keys))
40  repo_keys = JoinStrings(FindFilesBySuffix(repo_keys, ".pub"), ":");
41  const string temp_directory = (args.count('t') > 0) ?
42  *args.find('t')->second : "/tmp";
43  const string output_path = (args.count('o') > 0) ?
44  *args.find('o')->second : "";
45 
46  shash::Any reflog_hash;
47  if (reflog_chksum_path != "") {
48  if (!manifest::Reflog::ReadChecksum(reflog_chksum_path, &reflog_hash)) {
49  LogCvmfs(kLogCvmfs, kLogStderr, "Could not read reflog checksum");
50  return 1;
51  }
52  }
53 
54  const bool follow_redirects = false;
55  if (!this->InitDownloadManager(follow_redirects) ||
56  !this->InitVerifyingSignatureManager(repo_keys)) {
57  LogCvmfs(kLogCvmfs, kLogStderr, "failed to init repo connection");
58  return 1;
59  }
60 
61  bool success;
62  if (IsHttpUrl(repo_url)) {
63  HttpObjectFetcher<> object_fetcher(repo_name,
64  repo_url,
65  temp_directory,
66  download_manager(),
67  signature_manager());
68  if (reflog_hash.IsNull()) {
71  switch (failure = object_fetcher.FetchManifest(&manifest)) {
73  reflog_hash = manifest->reflog_hash();
74  break;
75  default:
76  LogCvmfs(kLogCvmfs, kLogStderr, "Failed to fetch manifest: %s",
77  Code2Ascii(failure));
78  return 1;
79  }
80  }
81  success = Run(&object_fetcher, repo_name, output_path, reflog_hash);
82  } else {
83  LocalObjectFetcher<> object_fetcher(repo_url, temp_directory);
84  success = Run(&object_fetcher, repo_name, output_path, reflog_hash);
85  }
86 
87  return (success) ? 0 : 1;
88 }
89 
90 template <class ObjectFetcherT>
91 bool CommandListReflog::Run(ObjectFetcherT *object_fetcher, string repo_name,
92  string output_path, shash::Any reflog_hash)
93 {
94  typename ObjectFetcherT::ReflogTN *reflog;
95  reflog = FetchReflog(object_fetcher, repo_name, reflog_hash);
96 
97  shash::Any null_hash = shash::Any(reflog_hash.algorithm);
99  objects_->Init(1024, null_hash, hasher);
100 
101  // Traverse through catalogs and regular objects
102  vector<shash::Any> catalogs;
103  if (NULL == reflog || !reflog->List(SqlReflog::kRefCatalog, &catalogs)) {
104  LogCvmfs(kLogCvmfs, kLogStderr, "Failed to list catalog reference log");
105  return false;
106  }
107  typename CatalogTraversal<ObjectFetcherT>::Parameters traversal_params;
108  traversal_params.object_fetcher = object_fetcher;
109  CatalogTraversal<ObjectFetcherT> traversal(traversal_params);
110  traversal.RegisterListener(
112  this);
113  bool success = true;
114  vector<shash::Any>::iterator i = catalogs.begin();
115  const vector<shash::Any>::const_iterator iend = catalogs.end();
116  for (; i != iend && success; i++) {
117  success &= traversal.TraverseRevision(*i,
119  }
120 
121  if (!success) {
123  "Catalog traversal aborted due to an error");
124  return false;
125  }
126 
127  // Add history, certificate, metainfo objects from reflog
128  vector<shash::Any> histories, certificates, metainfos;
129  if (!reflog->List(SqlReflog::kRefHistory, &histories)) {
131  "Failed to fetch history objects from reflog");
132  return false;
133  }
134  if (!reflog->List(SqlReflog::kRefCertificate, &certificates)) {
136  "Failed to fetch certificate objects from reflog");
137  return false;
138  }
139  if (!reflog->List(SqlReflog::kRefMetainfo, &metainfos)) {
141  "Failed to fetch metainfo objects from reflog");
142  return false;
143  }
144  InsertObjects(histories);
145  InsertObjects(certificates);
146  InsertObjects(metainfos);
147 
148  // Clean up reflog file
149  delete reflog;
150 
151  LogCvmfs(kLogCvmfs, kLogStderr, "Number of objects: %lu", objects_->size());
152 
153  if (output_path == "") {
154  DumpObjects(stdout);
155  } else {
156  int fd = open(output_path.c_str(), O_WRONLY | O_CREAT, 0644);
157  assert(fd);
158  FILE *stream = fdopen(fd, "w");
159  DumpObjects(stream);
160  fclose(stream); // no need to call close after fclose
161  }
162 
163  return success;
164 }
165 
166 void CommandListReflog::CatalogCallback(
168 {
169  LogCvmfs(kLogCvmfs, kLogStderr, "Processing catalog \"%s\"",
170  data.catalog->mountpoint().c_str());
171  const catalog::Catalog::HashVector &referenced_hashes =
173  InsertObjects(referenced_hashes);
174  if (data.catalog->hash() != objects_->empty_key())
175  objects_->Insert(data.catalog->hash(), true);
176 }
177 
178 void CommandListReflog::InsertObjects(const vector<shash::Any> &list) {
179  vector<shash::Any>::const_iterator i = list.begin();
180  const vector<shash::Any>::const_iterator iend = list.end();
181  for (; i != iend; ++i) {
182  if ((*i) != objects_->empty_key())
183  objects_->Insert(*i, true);
184  }
185 }
186 
187 void CommandListReflog::DumpObjects(FILE *stream)
188 {
189  shash::Any empty_key = objects_->empty_key();
190  shash::Any *hashes = objects_->keys();
191  for (uint32_t i = 0; i < objects_->capacity(); ++i) {
192  if (hashes[i] != empty_key) {
193  fprintf(stream, "%s\n", hashes[i].ToString().c_str());
194  }
195  }
196 }
197 
198 } // namespace swissknife
#define LogCvmfs(source, mask,...)
Definition: logging.h:20
const char * Code2Ascii(const ObjectFetcherFailures::Failures error)
CallbackPtr RegisterListener(typename BoundClosure< CatalogTraversalData< ObjectFetcherT::CatalogTN >, DelegateT, ClosureDataT >::CallbackMethod method, DelegateT *delegate, ClosureDataT data)
bool IsNull() const
Definition: hash.h:379
const manifest::Manifest * manifest() const
Definition: repository.h:123
void CatalogCallback(const CatalogTraversalData< catalog::Catalog > &data)
static bool ReadChecksum(const std::string &path, shash::Any *checksum)
Definition: reflog.cc:47
std::vector< Parameter > ParameterList
Definition: swissknife.h:71
string JoinStrings(const vector< string > &strings, const string &joint)
Definition: string.cc:318
bool IsHttpUrl(const std::string &path)
Definition: posix.cc:204
assert((mem||(size==0))&&"Out Of Memory")
Algorithms algorithm
Definition: hash.h:123
std::vector< shash::Any > HashVector
Definition: catalog.h:97
const HashVector & GetReferencedObjects() const
Definition: catalog.cc:464
PathString mountpoint() const
Definition: catalog.h:179
bool DirectoryExists(const std::string &path)
Definition: posix.cc:838
bool TraverseRevision(const shash::Any &root_catalog_hash, const TraversalType type=Base::kBreadthFirst)
std::map< char, SharedPtr< std::string > > ArgumentList
Definition: swissknife.h:72
Failures FetchManifest(manifest::Manifest **manifest)
shash::Any reflog_hash() const
Definition: manifest.h:132
shash::Any hash() const
Definition: catalog.h:186
void Init(uint32_t expected_size, Key empty, uint32_t(*hasher)(const Key &key))
Definition: smallhash.h:59
const char * c_str() const
Definition: shortstring.h:118
std::vector< std::string > FindFilesBySuffix(const std::string &dir, const std::string &suffix)
Definition: posix.cc:1149