1 |
|
|
/** |
2 |
|
|
* This file is part of the CernVM File System. |
3 |
|
|
*/ |
4 |
|
|
|
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 "logging.h" |
13 |
|
|
#include "util/string.h" |
14 |
|
|
|
15 |
|
|
template <class CatalogTraversalT, class HashFilterT> |
16 |
|
|
GarbageCollectorAux<CatalogTraversalT, HashFilterT>::GarbageCollectorAux( |
17 |
|
|
const ConfigurationTN &config) |
18 |
|
|
: config_(config) |
19 |
|
|
{ |
20 |
|
|
assert(config_.uploader != NULL); |
21 |
|
|
} |
22 |
|
|
|
23 |
|
|
|
24 |
|
|
template <class CatalogTraversalT, class HashFilterT> |
25 |
|
|
bool GarbageCollectorAux<CatalogTraversalT, HashFilterT>::CollectOlderThan( |
26 |
|
|
uint64_t timestamp, |
27 |
|
|
const HashFilterT &preserved_objects) |
28 |
|
|
{ |
29 |
|
|
if (config_.verbose) { |
30 |
|
|
LogCvmfs(kLogGc, kLogStdout | kLogDebug, |
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 %u %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> |
72 |
|
|
std::string GarbageCollectorAux<CatalogTraversalT, HashFilterT>::PrintAuxType( |
73 |
|
|
SqlReflog::ReferenceType type) |
74 |
|
|
{ |
75 |
|
|
switch (type) { |
76 |
|
|
case SqlReflog::kRefCatalog: |
77 |
|
|
return "file catalog"; |
78 |
|
|
case SqlReflog::kRefCertificate: |
79 |
|
|
return "certificate"; |
80 |
|
|
case SqlReflog::kRefHistory: |
81 |
|
|
return "tag database"; |
82 |
|
|
case SqlReflog::kRefMetainfo: |
83 |
|
|
return "repository meta information"; |
84 |
|
|
} |
85 |
|
|
// Never here |
86 |
|
|
return "UNKNOWN"; |
87 |
|
|
} |
88 |
|
|
|
89 |
|
|
|
90 |
|
|
template <class CatalogTraversalT, class HashFilterT> |
91 |
|
|
bool GarbageCollectorAux<CatalogTraversalT, HashFilterT>::Sweep( |
92 |
|
|
const shash::Any &hash) |
93 |
|
|
{ |
94 |
|
|
if (config_.verbose) { |
95 |
|
|
LogCvmfs(kLogGc, kLogStdout | kLogDebug, |
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_ |