CernVM-FS  2.13.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
reflog.cc
Go to the documentation of this file.
1 
5 #include "reflog.h"
6 
7 #include <fcntl.h>
8 #include <unistd.h>
9 
10 #include <cassert>
11 
12 #include "util/posix.h"
13 #include "util/string.h"
14 
15 namespace manifest {
16 
17 Reflog *Reflog::Open(const std::string &database_path) {
18  Reflog *reflog = new Reflog();
19  if (NULL == reflog || !reflog->OpenDatabase(database_path)) {
20  delete reflog;
21  return NULL;
22  }
23 
25  "opened Reflog database '%s' for repository '%s'",
26  database_path.c_str(), reflog->fqrn().c_str());
27 
28  return reflog;
29 }
30 
31 
32 Reflog *Reflog::Create(const std::string &database_path,
33  const std::string &repo_name) {
34  Reflog *reflog = new Reflog();
35  if (NULL == reflog || !reflog->CreateDatabase(database_path, repo_name)) {
36  delete reflog;
37  return NULL;
38  }
39 
41  "created empty reflog database '%s' for "
42  "repository '%s'",
43  database_path.c_str(), repo_name.c_str());
44  return reflog;
45 }
46 
47 
48 bool Reflog::ReadChecksum(const std::string &path, shash::Any *checksum) {
49  const int fd = open(path.c_str(), O_RDONLY);
50  if (fd < 0) {
51  return false;
52  }
53  std::string hex_hash;
54  const bool retval = GetLineFd(fd, &hex_hash);
55  if (retval == 0) {
56  close(fd);
57  return false;
58  }
59  close(fd);
60  *checksum = shash::MkFromHexPtr(shash::HexPtr(Trim(hex_hash)));
61  return true;
62 }
63 
64 
65 bool Reflog::WriteChecksum(const std::string &path, const shash::Any &value) {
66  const int fd =
67  open(path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, kDefaultFileMode);
68  if (fd < 0) {
69  return false;
70  }
71  std::string hex_hash = value.ToString();
72  const bool retval = SafeWrite(fd, hex_hash.data(), hex_hash.length());
73  if (retval == 0) {
74  close(fd);
75  return false;
76  }
77  close(fd);
78  return true;
79 }
80 
81 
82 bool Reflog::CreateDatabase(const std::string &database_path,
83  const std::string &repo_name) {
85  database_ = ReflogDatabase::Create(database_path);
86  if (!database_.IsValid() || !database_->InsertInitialValues(repo_name)) {
87  LogCvmfs(kLogReflog, kLogDebug, "failed to initialize empty database '%s'",
88  database_path.c_str());
89  return false;
90  }
91 
93  return true;
94 }
95 
96 
97 bool Reflog::OpenDatabase(const std::string &database_path) {
99 
101  database_ = ReflogDatabase::Open(database_path, mode);
102  if (!database_.IsValid()) {
103  return false;
104  }
105 
106  PrepareQueries();
107  return true;
108 }
109 
110 
119 }
120 
121 
122 bool Reflog::AddCertificate(const shash::Any &certificate) {
123  assert(certificate.HasSuffix()
124  && certificate.suffix == shash::kSuffixCertificate);
125  return AddReference(certificate, SqlReflog::kRefCertificate);
126 }
127 
128 
129 bool Reflog::AddCatalog(const shash::Any &catalog) {
130  assert(catalog.HasSuffix() && catalog.suffix == shash::kSuffixCatalog);
131  return AddReference(catalog, SqlReflog::kRefCatalog);
132 }
133 
134 
136  assert(history.HasSuffix() && history.suffix == shash::kSuffixHistory);
137  return AddReference(history, SqlReflog::kRefHistory);
138 }
139 
140 
141 bool Reflog::AddMetainfo(const shash::Any &metainfo) {
142  assert(metainfo.HasSuffix() && metainfo.suffix == shash::kSuffixMetainfo);
143  return AddReference(metainfo, SqlReflog::kRefMetainfo);
144 }
145 
146 
149  const bool success_exec = count_references_->Execute();
150  assert(success_exec);
151  const uint64_t count = count_references_->RetrieveCount();
152  const bool success_reset = count_references_->Reset();
153  assert(success_reset);
154  return count;
155 }
156 
157 
159  std::vector<shash::Any> *hashes) const {
160  return ListOlderThan(type, static_cast<uint64_t>(-1), hashes);
161 }
162 
163 
165  uint64_t timestamp,
166  std::vector<shash::Any> *hashes) const {
168  assert(NULL != hashes);
169 
170  hashes->clear();
171 
172  bool success_bind = list_references_->BindType(type);
173  assert(success_bind);
174  success_bind = list_references_->BindOlderThan(timestamp);
175  assert(success_bind);
176  while (list_references_->FetchRow()) {
177  hashes->push_back(list_references_->RetrieveHash());
178  }
179 
180  return list_references_->Reset();
181 }
182 
183 
184 bool Reflog::Remove(const shash::Any &hash) {
186 
188  switch (hash.suffix) {
190  type = SqlReflog::kRefCatalog;
191  break;
193  type = SqlReflog::kRefHistory;
194  break;
197  break;
200  break;
201  default:
202  return false;
203  }
204 
205  return remove_reference_->BindReference(hash, type)
206  && remove_reference_->Execute() && remove_reference_->Reset();
207 }
208 
209 
210 bool Reflog::ContainsCertificate(const shash::Any &certificate) const {
211  assert(certificate.HasSuffix()
212  && certificate.suffix == shash::kSuffixCertificate);
213  return ContainsReference(certificate, SqlReflog::kRefCertificate);
214 }
215 
216 
217 bool Reflog::ContainsCatalog(const shash::Any &catalog) const {
218  assert(catalog.HasSuffix() && catalog.suffix == shash::kSuffixCatalog);
219  return ContainsReference(catalog, SqlReflog::kRefCatalog);
220 }
221 
222 
224  uint64_t *timestamp) const {
225  assert(catalog.HasSuffix() && catalog.suffix == shash::kSuffixCatalog);
226  const bool result =
227  GetReferenceTimestamp(catalog, SqlReflog::kRefCatalog, timestamp);
228  return result;
229 }
230 
231 
233  assert(history.HasSuffix() && history.suffix == shash::kSuffixHistory);
234  return ContainsReference(history, SqlReflog::kRefHistory);
235 }
236 
237 
238 bool Reflog::ContainsMetainfo(const shash::Any &metainfo) const {
239  assert(metainfo.HasSuffix() && metainfo.suffix == shash::kSuffixMetainfo);
240  return ContainsReference(metainfo, SqlReflog::kRefMetainfo);
241 }
242 
243 
246  return insert_reference_->BindReference(hash, type)
247  && insert_reference_->Execute() && insert_reference_->Reset();
248 }
249 
250 
252  const SqlReflog::ReferenceType type) const {
253  const bool fetching = contains_reference_->BindReference(hash, type)
254  && contains_reference_->FetchRow();
255  assert(fetching);
256 
257  const bool answer = contains_reference_->RetrieveAnswer();
258  const bool reset = contains_reference_->Reset();
259  assert(reset);
260 
261  return answer;
262 }
263 
264 
267  uint64_t *timestamp) const {
268  const bool retval =
269  get_timestamp_->BindReference(hash, type) && get_timestamp_->FetchRow();
270 
271  if (retval) {
272  *timestamp = get_timestamp_->RetrieveTimestamp();
273  }
274 
275  const bool reset = get_timestamp_->Reset();
276  assert(reset);
277 
278  return retval;
279 }
280 
281 
284  database_->BeginTransaction();
285 }
286 
287 
290  database_->CommitTransaction();
291 }
292 
293 
296  database_->TakeFileOwnership();
297 }
298 
299 
302  database_->DropFileOwnership();
303 }
304 
305 
309 void Reflog::HashDatabase(const std::string &database_path,
310  shash::Any *hash_reflog) {
311  const bool retval = HashFile(database_path, hash_reflog);
312  assert(retval);
313 }
314 
315 
316 std::string Reflog::fqrn() const {
318  return database_->GetProperty<std::string>(ReflogDatabase::kFqrnKey);
319 }
320 
321 
322 std::string Reflog::database_file() const {
324  return database_->filename();
325 }
326 
327 } // namespace manifest
bool ContainsReference(const shash::Any &hash, const SqlReflog::ReferenceType type) const
Definition: reflog.cc:251
void TakeDatabaseFileOwnership()
Definition: reflog.cc:294
void PrepareQueries()
Definition: reflog.cc:111
std::string database_file() const
Definition: reflog.cc:322
bool ContainsHistory(const shash::Any &history) const
Definition: reflog.cc:232
const manifest::Manifest * manifest() const
Definition: repository.h:125
bool AddHistory(const shash::Any &history)
Definition: reflog.cc:135
UniquePtr< ReflogDatabase > database_
Definition: reflog.h:101
bool GetCatalogTimestamp(const shash::Any &catalog, uint64_t *timestamp) const
Definition: reflog.cc:223
UniquePtr< SqlListReferences > list_references_
Definition: reflog.h:105
bool HashFile(const std::string &filename, Any *any_digest)
Definition: hash.cc:339
T * weak_ref() const
Definition: pointer.h:46
static const std::string kFqrnKey
Definition: reflog_sql.h:20
const int kDefaultFileMode
Definition: posix.h:32
static bool ReadChecksum(const std::string &path, shash::Any *checksum)
Definition: reflog.cc:48
string Trim(const string &raw, bool trim_newline)
Definition: string.cc:466
const char kSuffixCertificate
Definition: hash.h:59
std::string ToString(const bool with_suffix=false) const
Definition: hash.h:241
const history::History * history() const
bool AddReference(const shash::Any &hash, const SqlReflog::ReferenceType type)
Definition: reflog.cc:244
bool HasSuffix() const
Definition: hash.h:231
bool SafeWrite(int fd, const void *buf, size_t nbyte)
Definition: posix.cc:2036
assert((mem||(size==0))&&"Out Of Memory")
bool AddCatalog(const shash::Any &catalog)
Definition: reflog.cc:129
static Reflog * Create(const std::string &database_path, const std::string &repo_name)
Definition: reflog.cc:32
static ReflogDatabase * Open(const std::string &filename, const OpenMode open_mode)
uint64_t CountEntries()
Definition: reflog.cc:147
bool ContainsMetainfo(const shash::Any &metainfo) const
Definition: reflog.cc:238
UniquePtr< SqlRemoveReference > remove_reference_
Definition: reflog.h:106
static Reflog * Open(const std::string &database_path)
Definition: reflog.cc:17
bool OpenDatabase(const std::string &database_path)
Definition: reflog.cc:97
bool AddMetainfo(const shash::Any &metainfo)
Definition: reflog.cc:141
bool Remove(const shash::Any &hash)
Definition: reflog.cc:184
const char kSuffixCatalog
Definition: hash.h:54
UniquePtr< SqlCountReferences > count_references_
Definition: reflog.h:104
bool List(SqlReflog::ReferenceType type, std::vector< shash::Any > *hashes) const
Definition: reflog.cc:158
void BeginTransaction()
Definition: reflog.cc:282
static void HashDatabase(const std::string &database_path, shash::Any *hash_reflog)
Definition: reflog.cc:309
bool GetReferenceTimestamp(const shash::Any &hash, const SqlReflog::ReferenceType type, uint64_t *timestamp) const
Definition: reflog.cc:265
const char kSuffixMetainfo
Definition: hash.h:60
bool ListOlderThan(SqlReflog::ReferenceType type, uint64_t timestamp, std::vector< shash::Any > *hashes) const
Definition: reflog.cc:164
bool IsValid() const
Definition: pointer.h:47
void DropDatabaseFileOwnership()
Definition: reflog.cc:300
const char kSuffixHistory
Definition: hash.h:55
bool AddCertificate(const shash::Any &certificate)
Definition: reflog.cc:122
bool GetLineFd(const int fd, std::string *line)
Definition: string.cc:441
UniquePtr< SqlInsertReference > insert_reference_
Definition: reflog.h:103
bool ContainsCatalog(const shash::Any &catalog) const
Definition: reflog.cc:217
std::string fqrn() const
Definition: reflog.cc:316
void CommitTransaction()
Definition: reflog.cc:288
Any MkFromHexPtr(const HexPtr hex, const char suffix)
Definition: hash.cc:82
UniquePtr< SqlContainsReference > contains_reference_
Definition: reflog.h:107
Suffix suffix
Definition: hash.h:123
static bool WriteChecksum(const std::string &path, const shash::Any &value)
Definition: reflog.cc:65
bool ContainsCertificate(const shash::Any &certificate) const
Definition: reflog.cc:210
static ReflogDatabase * Create(const std::string &filename)
bool CreateDatabase(const std::string &database_path, const std::string &repo_name)
Definition: reflog.cc:82
UniquePtr< SqlGetTimestamp > get_timestamp_
Definition: reflog.h:108
CVMFS_EXPORT void LogCvmfs(const LogSource source, const int mask, const char *format,...)
Definition: logging.cc:545