CernVM-FS  2.12.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
gc_aux_impl.h
Go to the documentation of this file.
1 
5 #ifndef CVMFS_GARBAGE_COLLECTION_GC_AUX_IMPL_H_
6 #define CVMFS_GARBAGE_COLLECTION_GC_AUX_IMPL_H_
7 
8 #include <cstdio>
9 #include <string>
10 #include <vector>
11 
12 #include "util/logging.h"
13 #include "util/string.h"
14 
15 template <class CatalogTraversalT, class HashFilterT>
17  const ConfigurationTN &config)
18  : config_(config)
19 {
20  assert(config_.uploader != NULL);
21 }
22 
23 
24 template <class CatalogTraversalT, class HashFilterT>
26  uint64_t timestamp,
27  const HashFilterT &preserved_objects)
28 {
29  if (config_.verbose) {
31  "Sweeping auxiliary objects older than %s",
32  StringifyTime(timestamp, true).c_str());
33  }
34  std::vector<SqlReflog::ReferenceType> aux_types;
35  aux_types.push_back(SqlReflog::kRefCertificate);
36  aux_types.push_back(SqlReflog::kRefHistory);
37  aux_types.push_back(SqlReflog::kRefMetainfo);
38  for (unsigned i = 0; i < aux_types.size(); ++i) {
39  std::vector<shash::Any> hashes;
40  bool retval =
41  config_.reflog->ListOlderThan(aux_types[i], timestamp, &hashes);
42  if (!retval) {
43  LogCvmfs(kLogGc, kLogStderr, "failed to enumerate %s objects",
44  PrintAuxType(aux_types[i]).c_str());
45  return 1;
46  }
47  if (config_.verbose) {
48  LogCvmfs(kLogGc, kLogStdout | kLogDebug, "Scanning %lu %s objects",
49  hashes.size(), PrintAuxType(aux_types[i]).c_str());
50  }
51 
52  for (unsigned iter = 0; iter < hashes.size(); ++iter) {
53  if (preserved_objects.Contains(hashes[iter])) {
54  if (config_.verbose) {
55  LogCvmfs(kLogGc, kLogStdout | kLogDebug, " preserving: %s",
56  hashes[iter].ToStringWithSuffix().c_str());
57  }
58  continue;
59  }
60 
61  if (!Sweep(hashes[iter]))
62  return false;
63  }
64  }
65 
66  config_.uploader->WaitForUpload();
67  return config_.uploader->GetNumberOfErrors() == 0;
68 }
69 
70 
71 template <class CatalogTraversalT, class HashFilterT>
74 {
75  switch (type) {
77  return "file catalog";
79  return "certificate";
81  return "tag database";
83  return "repository meta information";
84  }
85  // Never here
86  return "UNKNOWN";
87 }
88 
89 
90 template <class CatalogTraversalT, class HashFilterT>
92  const shash::Any &hash)
93 {
94  if (config_.verbose) {
96  " sweep: %s", hash.ToStringWithSuffix().c_str());
97  }
98 
99  if (!config_.dry_run) {
100  config_.uploader->RemoveAsync(hash);
101  bool retval = config_.reflog->Remove(hash);
102  if (!retval) {
103  LogCvmfs(kLogGc, kLogStderr, "failed to remove %s from reference log",
104  hash.ToStringWithSuffix().c_str());
105  return false;
106  }
107  }
108 
109  if (config_.has_deletion_log()) {
110  const int written = fprintf(config_.deleted_objects_logfile,
111  "%s\n", hash.ToStringWithSuffix().c_str());
112  if (written < 0) {
113  LogCvmfs(kLogGc, kLogStderr, "failed to write to deleted objects log");
114  return false;
115  }
116  }
117 
118  return true;
119 }
120 
121 #endif // CVMFS_GARBAGE_COLLECTION_GC_AUX_IMPL_H_
std::string PrintAuxType(SqlReflog::ReferenceType type)
Definition: gc_aux_impl.h:72
GarbageCollectorAux(const ConfigurationTN &config)
Definition: gc_aux_impl.h:16
std::string ToStringWithSuffix() const
Definition: hash.h:304
assert((mem||(size==0))&&"Out Of Memory")
string StringifyTime(const time_t seconds, const bool utc)
Definition: string.cc:105
bool Sweep(const shash::Any &hash)
Definition: gc_aux_impl.h:91
upload::AbstractUploader * uploader
bool CollectOlderThan(uint64_t timestamp, const HashFilterT &preserved_objects)
Definition: gc_aux_impl.h:25
const ConfigurationTN config_
Definition: gc_aux.h:51
CVMFS_EXPORT void LogCvmfs(const LogSource source, const int mask, const char *format,...)
Definition: logging.cc:528