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  int fd = open(path.c_str(), O_RDONLY);
50  if (fd < 0) {
51  return false;
52  }
53  std::string hex_hash;
54  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  int fd = open(path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, kDefaultFileMode);
67  if (fd < 0) {
68  return false;
69  }
70  std::string hex_hash = value.ToString();
71  bool retval = SafeWrite(fd, hex_hash.data(), hex_hash.length());
72  if (retval == 0) {
73  close(fd);
74  return false;
75  }
76  close(fd);
77  return true;
78 }
79 
80 
81 bool Reflog::CreateDatabase(const std::string &database_path,
82  const std::string &repo_name) {
84  database_ = ReflogDatabase::Create(database_path);
85  if (!database_.IsValid() || !database_->InsertInitialValues(repo_name)) {
86  LogCvmfs(kLogReflog, kLogDebug, "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 
158  std::vector<shash::Any> *hashes) const {
159  return ListOlderThan(type, static_cast<uint64_t>(-1), hashes);
160 }
161 
162 
164  uint64_t timestamp,
165  std::vector<shash::Any> *hashes) const {
167  assert(NULL != hashes);
168 
169  hashes->clear();
170 
171  bool success_bind = list_references_->BindType(type);
172  assert(success_bind);
173  success_bind = list_references_->BindOlderThan(timestamp);
174  assert(success_bind);
175  while (list_references_->FetchRow()) {
176  hashes->push_back(list_references_->RetrieveHash());
177  }
178 
179  return list_references_->Reset();
180 }
181 
182 
183 bool Reflog::Remove(const shash::Any &hash) {
185 
187  switch (hash.suffix) {
189  type = SqlReflog::kRefCatalog;
190  break;
192  type = SqlReflog::kRefHistory;
193  break;
196  break;
199  break;
200  default:
201  return false;
202  }
203 
204  return remove_reference_->BindReference(hash, type)
205  && remove_reference_->Execute() && remove_reference_->Reset();
206 }
207 
208 
209 bool Reflog::ContainsCertificate(const shash::Any &certificate) const {
210  assert(certificate.HasSuffix()
211  && certificate.suffix == shash::kSuffixCertificate);
212  return ContainsReference(certificate, SqlReflog::kRefCertificate);
213 }
214 
215 
216 bool Reflog::ContainsCatalog(const shash::Any &catalog) const {
217  assert(catalog.HasSuffix() && catalog.suffix == shash::kSuffixCatalog);
218  return ContainsReference(catalog, SqlReflog::kRefCatalog);
219 }
220 
221 
223  uint64_t *timestamp) const {
224  assert(catalog.HasSuffix() && catalog.suffix == shash::kSuffixCatalog);
225  bool result = GetReferenceTimestamp(catalog, SqlReflog::kRefCatalog,
226  timestamp);
227  return result;
228 }
229 
230 
232  assert(history.HasSuffix() && history.suffix == shash::kSuffixHistory);
233  return ContainsReference(history, SqlReflog::kRefHistory);
234 }
235 
236 
237 bool Reflog::ContainsMetainfo(const shash::Any &metainfo) const {
238  assert(metainfo.HasSuffix() && metainfo.suffix == shash::kSuffixMetainfo);
239  return ContainsReference(metainfo, SqlReflog::kRefMetainfo);
240 }
241 
242 
245  return insert_reference_->BindReference(hash, type)
246  && insert_reference_->Execute() && insert_reference_->Reset();
247 }
248 
249 
251  const SqlReflog::ReferenceType type) const {
252  const bool fetching = contains_reference_->BindReference(hash, type)
253  && contains_reference_->FetchRow();
254  assert(fetching);
255 
256  const bool answer = contains_reference_->RetrieveAnswer();
257  const bool reset = contains_reference_->Reset();
258  assert(reset);
259 
260  return answer;
261 }
262 
263 
266  uint64_t *timestamp) const {
267  bool retval = get_timestamp_->BindReference(hash, type)
268  && get_timestamp_->FetchRow();
269 
270  if (retval) {
271  *timestamp = get_timestamp_->RetrieveTimestamp();
272  }
273 
274  const bool reset = get_timestamp_->Reset();
275  assert(reset);
276 
277  return retval;
278 }
279 
280 
283  database_->BeginTransaction();
284 }
285 
286 
289  database_->CommitTransaction();
290 }
291 
292 
295  database_->TakeFileOwnership();
296 }
297 
298 
301  database_->DropFileOwnership();
302 }
303 
304 
308 void Reflog::HashDatabase(const std::string &database_path,
309  shash::Any *hash_reflog) {
310  bool retval = HashFile(database_path, hash_reflog);
311  assert(retval);
312 }
313 
314 
315 std::string Reflog::fqrn() const {
317  return database_->GetProperty<std::string>(ReflogDatabase::kFqrnKey);
318 }
319 
320 
321 std::string Reflog::database_file() const {
323  return database_->filename();
324 }
325 
326 } // namespace manifest
bool ContainsReference(const shash::Any &hash, const SqlReflog::ReferenceType type) const
Definition: reflog.cc:250
void TakeDatabaseFileOwnership()
Definition: reflog.cc:293
void PrepareQueries()
Definition: reflog.cc:110
std::string database_file() const
Definition: reflog.cc:321
bool ContainsHistory(const shash::Any &history) const
Definition: reflog.cc:231
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:222
UniquePtr< SqlListReferences > list_references_
Definition: reflog.h:105
bool HashFile(const std::string &filename, Any *any_digest)
Definition: hash.cc:341
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:243
bool HasSuffix() const
Definition: hash.h:231
bool SafeWrite(int fd, const void *buf, size_t nbyte)
Definition: posix.cc:2035
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:237
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:183
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:281
static void HashDatabase(const std::string &database_path, shash::Any *hash_reflog)
Definition: reflog.cc:308
bool GetReferenceTimestamp(const shash::Any &hash, const SqlReflog::ReferenceType type, uint64_t *timestamp) const
Definition: reflog.cc:264
const char kSuffixMetainfo
Definition: hash.h:60
bool ListOlderThan(SqlReflog::ReferenceType type, uint64_t timestamp, std::vector< shash::Any > *hashes) const
Definition: reflog.cc:163
bool IsValid() const
Definition: pointer.h:47
void DropDatabaseFileOwnership()
Definition: reflog.cc:299
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:441
UniquePtr< SqlInsertReference > insert_reference_
Definition: reflog.h:103
bool ContainsCatalog(const shash::Any &catalog) const
Definition: reflog.cc:216
std::string fqrn() const
Definition: reflog.cc:315
void CommitTransaction()
Definition: reflog.cc:287
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:209
static ReflogDatabase * Create(const std::string &filename)
bool CreateDatabase(const std::string &database_path, const std::string &repo_name)
Definition: reflog.cc:81
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