CernVM-FS  2.12.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 
40  LogCvmfs(kLogReflog, kLogDebug, "created empty reflog database '%s' for "
41  "repository '%s'",
42  database_path.c_str(), repo_name.c_str());
43  return reflog;
44 }
45 
46 
47 bool Reflog::ReadChecksum(const std::string &path, shash::Any* checksum) {
48  int fd = open(path.c_str(), O_RDONLY);
49  if (fd < 0) {
50  return false;
51  }
52  std::string hex_hash;
53  bool retval = GetLineFd(fd, &hex_hash);
54  if (retval == 0) {
55  close(fd);
56  return false;
57  }
58  close(fd);
59  *checksum = shash::MkFromHexPtr(shash::HexPtr(Trim(hex_hash)));
60  return true;
61 }
62 
63 
64 bool Reflog::WriteChecksum(const std::string &path, const shash::Any &value) {
65  int fd = open(path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, kDefaultFileMode);
66  if (fd < 0) {
67  return false;
68  }
69  std::string hex_hash = value.ToString();
70  bool retval = SafeWrite(fd, hex_hash.data(), hex_hash.length());
71  if (retval == 0) {
72  close(fd);
73  return false;
74  }
75  close(fd);
76  return true;
77 }
78 
79 
80 bool Reflog::CreateDatabase(const std::string &database_path,
81  const std::string &repo_name) {
83  database_ = ReflogDatabase::Create(database_path);
84  if (!database_.IsValid() || !database_->InsertInitialValues(repo_name)) {
86  "failed to initialize empty database '%s'",
87  database_path.c_str());
88  return false;
89  }
90 
92  return true;
93 }
94 
95 
96 bool Reflog::OpenDatabase(const std::string &database_path) {
98 
100  database_ = ReflogDatabase::Open(database_path, mode);
101  if (!database_.IsValid()) {
102  return false;
103  }
104 
105  PrepareQueries();
106  return true;
107 }
108 
109 
118 }
119 
120 
121 bool Reflog::AddCertificate(const shash::Any &certificate) {
122  assert(certificate.HasSuffix() &&
123  certificate.suffix == shash::kSuffixCertificate);
124  return AddReference(certificate, SqlReflog::kRefCertificate);
125 }
126 
127 
128 bool Reflog::AddCatalog(const shash::Any &catalog) {
129  assert(catalog.HasSuffix() && catalog.suffix == shash::kSuffixCatalog);
130  return AddReference(catalog, SqlReflog::kRefCatalog);
131 }
132 
133 
135  assert(history.HasSuffix() && history.suffix == shash::kSuffixHistory);
136  return AddReference(history, SqlReflog::kRefHistory);
137 }
138 
139 
140 bool Reflog::AddMetainfo(const shash::Any &metainfo) {
141  assert(metainfo.HasSuffix() && metainfo.suffix == shash::kSuffixMetainfo);
142  return AddReference(metainfo, SqlReflog::kRefMetainfo);
143 }
144 
145 
148  const bool success_exec = count_references_->Execute();
149  assert(success_exec);
150  const uint64_t count = count_references_->RetrieveCount();
151  const bool success_reset = count_references_->Reset();
152  assert(success_reset);
153  return count;
154 }
155 
156 
159  std::vector<shash::Any> *hashes) const
160 {
161  return ListOlderThan(type, static_cast<uint64_t>(-1), hashes);
162 }
163 
164 
167  uint64_t timestamp,
168  std::vector<shash::Any> *hashes) const
169 {
171  assert(NULL != hashes);
172 
173  hashes->clear();
174 
175  bool success_bind = list_references_->BindType(type);
176  assert(success_bind);
177  success_bind = list_references_->BindOlderThan(timestamp);
178  assert(success_bind);
179  while (list_references_->FetchRow()) {
180  hashes->push_back(list_references_->RetrieveHash());
181  }
182 
183  return list_references_->Reset();
184 }
185 
186 
187 bool Reflog::Remove(const shash::Any &hash) {
189 
191  switch (hash.suffix) {
193  type = SqlReflog::kRefCatalog;
194  break;
196  type = SqlReflog::kRefHistory;
197  break;
200  break;
203  break;
204  default:
205  return false;
206  }
207 
208  return
209  remove_reference_->BindReference(hash, type) &&
210  remove_reference_->Execute() &&
211  remove_reference_->Reset();
212 }
213 
214 
215 bool Reflog::ContainsCertificate(const shash::Any &certificate) const {
216  assert(certificate.HasSuffix() &&
217  certificate.suffix == shash::kSuffixCertificate);
218  return ContainsReference(certificate, SqlReflog::kRefCertificate);
219 }
220 
221 
222 bool Reflog::ContainsCatalog(const shash::Any &catalog) const {
223  assert(catalog.HasSuffix() && catalog.suffix == shash::kSuffixCatalog);
224  return ContainsReference(catalog, SqlReflog::kRefCatalog);
225 }
226 
227 
229  const shash::Any &catalog,
230  uint64_t *timestamp) const
231 {
232  assert(catalog.HasSuffix() && catalog.suffix == shash::kSuffixCatalog);
233  bool result = GetReferenceTimestamp(catalog, SqlReflog::kRefCatalog,
234  timestamp);
235  return result;
236 }
237 
238 
240  assert(history.HasSuffix() && history.suffix == shash::kSuffixHistory);
241  return ContainsReference(history, SqlReflog::kRefHistory);
242 }
243 
244 
245 bool Reflog::ContainsMetainfo(const shash::Any &metainfo) const {
246  assert(metainfo.HasSuffix() && metainfo.suffix == shash::kSuffixMetainfo);
247  return ContainsReference(metainfo, SqlReflog::kRefMetainfo);
248 }
249 
250 
253  return
254  insert_reference_->BindReference(hash, type) &&
255  insert_reference_->Execute() &&
256  insert_reference_->Reset();
257 }
258 
259 
261  const SqlReflog::ReferenceType type) const {
262  const bool fetching =
263  contains_reference_->BindReference(hash, type) &&
264  contains_reference_->FetchRow();
265  assert(fetching);
266 
267  const bool answer = contains_reference_->RetrieveAnswer();
268  const bool reset = contains_reference_->Reset();
269  assert(reset);
270 
271  return answer;
272 }
273 
274 
276  const shash::Any &hash,
278  uint64_t *timestamp) const
279 {
280  bool retval =
281  get_timestamp_->BindReference(hash, type) &&
282  get_timestamp_->FetchRow();
283 
284  if (retval) {
285  *timestamp = get_timestamp_->RetrieveTimestamp();
286  }
287 
288  const bool reset = get_timestamp_->Reset();
289  assert(reset);
290 
291  return retval;
292 }
293 
294 
297  database_->BeginTransaction();
298 }
299 
300 
303  database_->CommitTransaction();
304 }
305 
306 
309  database_->TakeFileOwnership();
310 }
311 
312 
315  database_->DropFileOwnership();
316 }
317 
318 
323  const std::string &database_path,
324  shash::Any *hash_reflog)
325 {
326  bool retval = HashFile(database_path, hash_reflog);
327  assert(retval);
328 }
329 
330 
331 std::string Reflog::fqrn() const {
333  return database_->GetProperty<std::string>(ReflogDatabase::kFqrnKey);
334 }
335 
336 
337 std::string Reflog::database_file() const {
339  return database_->filename();
340 }
341 
342 } // namespace manifest
bool ContainsReference(const shash::Any &hash, const SqlReflog::ReferenceType type) const
Definition: reflog.cc:260
void TakeDatabaseFileOwnership()
Definition: reflog.cc:307
void PrepareQueries()
Definition: reflog.cc:110
std::string database_file() const
Definition: reflog.cc:337
bool ContainsHistory(const shash::Any &history) const
Definition: reflog.cc:239
const manifest::Manifest * manifest() const
Definition: repository.h:125
bool AddHistory(const shash::Any &history)
Definition: reflog.cc:134
UniquePtr< ReflogDatabase > database_
Definition: reflog.h:101
bool GetCatalogTimestamp(const shash::Any &catalog, uint64_t *timestamp) const
Definition: reflog.cc:228
UniquePtr< SqlListReferences > list_references_
Definition: reflog.h:105
bool HashFile(const std::string &filename, Any *any_digest)
Definition: hash.cc:342
T * weak_ref() const
Definition: pointer.h:42
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:47
string Trim(const string &raw, bool trim_newline)
Definition: string.cc:428
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 AddReference(const shash::Any &hash, const SqlReflog::ReferenceType type)
Definition: reflog.cc:251
bool HasSuffix() const
Definition: hash.h:239
bool SafeWrite(int fd, const void *buf, size_t nbyte)
Definition: posix.cc:1983
assert((mem||(size==0))&&"Out Of Memory")
bool AddCatalog(const shash::Any &catalog)
Definition: reflog.cc:128
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:146
bool ContainsMetainfo(const shash::Any &metainfo) const
Definition: reflog.cc:245
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:96
bool AddMetainfo(const shash::Any &metainfo)
Definition: reflog.cc:140
bool Remove(const shash::Any &hash)
Definition: reflog.cc:187
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:157
void BeginTransaction()
Definition: reflog.cc:295
static void HashDatabase(const std::string &database_path, shash::Any *hash_reflog)
Definition: reflog.cc:322
bool GetReferenceTimestamp(const shash::Any &hash, const SqlReflog::ReferenceType type, uint64_t *timestamp) const
Definition: reflog.cc:275
const char kSuffixMetainfo
Definition: hash.h:60
bool ListOlderThan(SqlReflog::ReferenceType type, uint64_t timestamp, std::vector< shash::Any > *hashes) const
Definition: reflog.cc:165
bool IsValid() const
Definition: pointer.h:43
void DropDatabaseFileOwnership()
Definition: reflog.cc:313
const char kSuffixHistory
Definition: hash.h:55
bool AddCertificate(const shash::Any &certificate)
Definition: reflog.cc:121
bool GetLineFd(const int fd, std::string *line)
Definition: string.cc:404
UniquePtr< SqlInsertReference > insert_reference_
Definition: reflog.h:103
bool ContainsCatalog(const shash::Any &catalog) const
Definition: reflog.cc:222
std::string fqrn() const
Definition: reflog.cc:331
void CommitTransaction()
Definition: reflog.cc:301
Any MkFromHexPtr(const HexPtr hex, const char suffix)
Definition: hash.cc:83
UniquePtr< SqlContainsReference > contains_reference_
Definition: reflog.h:107
Suffix suffix
Definition: hash.h:126
static bool WriteChecksum(const std::string &path, const shash::Any &value)
Definition: reflog.cc:64
bool ContainsCertificate(const shash::Any &certificate) const
Definition: reflog.cc:215
static ReflogDatabase * Create(const std::string &filename)
bool CreateDatabase(const std::string &database_path, const std::string &repo_name)
Definition: reflog.cc:80
UniquePtr< SqlGetTimestamp > get_timestamp_
Definition: reflog.h:108
CVMFS_EXPORT void LogCvmfs(const LogSource source, const int mask, const char *format,...)
Definition: logging.cc:528