| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/reflog.cc |
| Date: | 2026-09-13 02:40:16 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 157 | 184 | 85.3% |
| Branches: | 85 | 177 | 48.0% |
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** | ||
| 2 | * This file is part of the CernVM File System. | ||
| 3 | */ | ||
| 4 | |||
| 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 | 49 | Reflog *Reflog::Open(const std::string &database_path) { | |
| 18 | 49 | Reflog *reflog = new Reflog(); | |
| 19 |
3/6✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 49 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 49 times.
|
49 | if (NULL == reflog || !reflog->OpenDatabase(database_path)) { |
| 20 | ✗ | delete reflog; | |
| 21 | ✗ | return NULL; | |
| 22 | } | ||
| 23 | |||
| 24 |
1/2✓ Branch 3 taken 49 times.
✗ Branch 4 not taken.
|
49 | LogCvmfs(kLogReflog, kLogDebug, |
| 25 | "opened Reflog database '%s' for repository '%s'", | ||
| 26 | 98 | database_path.c_str(), reflog->fqrn().c_str()); | |
| 27 | |||
| 28 | 49 | return reflog; | |
| 29 | } | ||
| 30 | |||
| 31 | |||
| 32 | 228 | Reflog *Reflog::Create(const std::string &database_path, | |
| 33 | const std::string &repo_name) { | ||
| 34 | 228 | Reflog *reflog = new Reflog(); | |
| 35 |
3/6✓ Branch 0 taken 228 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 228 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 228 times.
|
228 | if (NULL == reflog || !reflog->CreateDatabase(database_path, repo_name)) { |
| 36 | ✗ | delete reflog; | |
| 37 | ✗ | return NULL; | |
| 38 | } | ||
| 39 | |||
| 40 | 228 | LogCvmfs(kLogReflog, kLogDebug, | |
| 41 | "created empty reflog database '%s' for " | ||
| 42 | "repository '%s'", | ||
| 43 | database_path.c_str(), repo_name.c_str()); | ||
| 44 | 228 | return reflog; | |
| 45 | } | ||
| 46 | |||
| 47 | |||
| 48 | 17 | bool Reflog::ReadChecksum(const std::string &path, shash::Any *checksum) { | |
| 49 |
1/2✓ Branch 2 taken 17 times.
✗ Branch 3 not taken.
|
17 | const int fd = open(path.c_str(), O_RDONLY); |
| 50 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
|
17 | if (fd < 0) { |
| 51 | ✗ | return false; | |
| 52 | } | ||
| 53 | 17 | std::string hex_hash; | |
| 54 |
1/2✓ Branch 1 taken 17 times.
✗ Branch 2 not taken.
|
17 | const bool retval = GetLineFd(fd, &hex_hash); |
| 55 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
|
17 | if (retval == 0) { |
| 56 | ✗ | close(fd); | |
| 57 | ✗ | return false; | |
| 58 | } | ||
| 59 |
1/2✓ Branch 1 taken 17 times.
✗ Branch 2 not taken.
|
17 | close(fd); |
| 60 |
2/4✓ Branch 1 taken 17 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 17 times.
✗ Branch 6 not taken.
|
17 | *checksum = shash::MkFromHexPtr(shash::HexPtr(Trim(hex_hash))); |
| 61 | 17 | return true; | |
| 62 | 17 | } | |
| 63 | |||
| 64 | |||
| 65 | 17 | bool Reflog::WriteChecksum(const std::string &path, const shash::Any &value) { | |
| 66 |
1/2✓ Branch 2 taken 17 times.
✗ Branch 3 not taken.
|
17 | const int fd = open(path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, |
| 67 | kDefaultFileMode); | ||
| 68 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
|
17 | if (fd < 0) { |
| 69 | ✗ | return false; | |
| 70 | } | ||
| 71 |
1/2✓ Branch 1 taken 17 times.
✗ Branch 2 not taken.
|
17 | std::string hex_hash = value.ToString(); |
| 72 |
1/2✓ Branch 3 taken 17 times.
✗ Branch 4 not taken.
|
17 | const bool retval = SafeWrite(fd, hex_hash.data(), hex_hash.length()); |
| 73 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
|
17 | if (retval == 0) { |
| 74 | ✗ | close(fd); | |
| 75 | ✗ | return false; | |
| 76 | } | ||
| 77 |
1/2✓ Branch 1 taken 17 times.
✗ Branch 2 not taken.
|
17 | close(fd); |
| 78 | 17 | return true; | |
| 79 | 17 | } | |
| 80 | |||
| 81 | |||
| 82 | 228 | bool Reflog::CreateDatabase(const std::string &database_path, | |
| 83 | const std::string &repo_name) { | ||
| 84 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 228 times.
|
228 | assert(database_.get() == nullptr); |
| 85 | 456 | database_ = std::unique_ptr<ReflogDatabase>( | |
| 86 | 228 | ReflogDatabase::Create(database_path)); | |
| 87 | 228 | if (database_.get() == nullptr | |
| 88 |
3/6✓ Branch 0 taken 228 times.
✗ Branch 1 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 228 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 228 times.
|
228 | || !database_->InsertInitialValues(repo_name)) { |
| 89 | ✗ | LogCvmfs(kLogReflog, kLogDebug, "failed to initialize empty database '%s'", | |
| 90 | database_path.c_str()); | ||
| 91 | ✗ | return false; | |
| 92 | } | ||
| 93 | |||
| 94 | 228 | PrepareQueries(); | |
| 95 | 228 | return true; | |
| 96 | } | ||
| 97 | |||
| 98 | |||
| 99 | 49 | bool Reflog::OpenDatabase(const std::string &database_path) { | |
| 100 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 49 times.
|
49 | assert(database_.get() == nullptr); |
| 101 | |||
| 102 | 49 | const ReflogDatabase::OpenMode mode = ReflogDatabase::kOpenReadWrite; | |
| 103 | 98 | database_ = std::unique_ptr<ReflogDatabase>( | |
| 104 | 49 | ReflogDatabase::Open(database_path, mode)); | |
| 105 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 49 times.
|
49 | if (database_.get() == nullptr) { |
| 106 | ✗ | return false; | |
| 107 | } | ||
| 108 | |||
| 109 | 49 | PrepareQueries(); | |
| 110 | 49 | return true; | |
| 111 | } | ||
| 112 | |||
| 113 | |||
| 114 | 277 | void Reflog::PrepareQueries() { | |
| 115 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 277 times.
|
277 | assert(database_.get() != nullptr); |
| 116 | 554 | insert_reference_ = std::unique_ptr<SqlInsertReference>( | |
| 117 |
1/2✓ Branch 3 taken 277 times.
✗ Branch 4 not taken.
|
554 | new SqlInsertReference(database_.get())); |
| 118 | 554 | count_references_ = std::unique_ptr<SqlCountReferences>( | |
| 119 |
1/2✓ Branch 3 taken 277 times.
✗ Branch 4 not taken.
|
554 | new SqlCountReferences(database_.get())); |
| 120 | 554 | list_references_ = std::unique_ptr<SqlListReferences>( | |
| 121 |
1/2✓ Branch 3 taken 277 times.
✗ Branch 4 not taken.
|
554 | new SqlListReferences(database_.get())); |
| 122 | 554 | remove_reference_ = std::unique_ptr<SqlRemoveReference>( | |
| 123 |
1/2✓ Branch 3 taken 277 times.
✗ Branch 4 not taken.
|
554 | new SqlRemoveReference(database_.get())); |
| 124 | 554 | contains_reference_ = std::unique_ptr<SqlContainsReference>( | |
| 125 |
1/2✓ Branch 3 taken 277 times.
✗ Branch 4 not taken.
|
554 | new SqlContainsReference(database_.get())); |
| 126 | 554 | get_timestamp_ = std::unique_ptr<SqlGetTimestamp>( | |
| 127 |
1/2✓ Branch 3 taken 277 times.
✗ Branch 4 not taken.
|
554 | new SqlGetTimestamp(database_.get())); |
| 128 | 277 | } | |
| 129 | |||
| 130 | |||
| 131 | 7 | bool Reflog::AddCertificate(const shash::Any &certificate) { | |
| 132 |
2/4✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 7 times.
✗ Branch 4 not taken.
|
7 | assert(certificate.HasSuffix() |
| 133 | && certificate.suffix == shash::kSuffixCertificate); | ||
| 134 | 7 | return AddReference(certificate, SqlReflog::kRefCertificate); | |
| 135 | } | ||
| 136 | |||
| 137 | |||
| 138 | 10 | bool Reflog::AddCatalog(const shash::Any &catalog) { | |
| 139 |
2/4✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 10 times.
✗ Branch 4 not taken.
|
10 | assert(catalog.HasSuffix() && catalog.suffix == shash::kSuffixCatalog); |
| 140 | 10 | return AddReference(catalog, SqlReflog::kRefCatalog); | |
| 141 | } | ||
| 142 | |||
| 143 | |||
| 144 | 4 | bool Reflog::AddHistory(const shash::Any &history) { | |
| 145 |
2/4✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
|
4 | assert(history.HasSuffix() && history.suffix == shash::kSuffixHistory); |
| 146 | 4 | return AddReference(history, SqlReflog::kRefHistory); | |
| 147 | } | ||
| 148 | |||
| 149 | |||
| 150 | 7 | bool Reflog::AddMetainfo(const shash::Any &metainfo) { | |
| 151 |
2/4✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 7 times.
✗ Branch 4 not taken.
|
7 | assert(metainfo.HasSuffix() && metainfo.suffix == shash::kSuffixMetainfo); |
| 152 | 7 | return AddReference(metainfo, SqlReflog::kRefMetainfo); | |
| 153 | } | ||
| 154 | |||
| 155 | |||
| 156 | 8 | uint64_t Reflog::CountEntries() { | |
| 157 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
|
8 | assert(database_.get() != nullptr); |
| 158 | 8 | const bool success_exec = count_references_->Execute(); | |
| 159 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 | assert(success_exec); |
| 160 | 8 | const uint64_t count = count_references_->RetrieveCount(); | |
| 161 | 8 | const bool success_reset = count_references_->Reset(); | |
| 162 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 | assert(success_reset); |
| 163 | 8 | return count; | |
| 164 | } | ||
| 165 | |||
| 166 | |||
| 167 | 4 | bool Reflog::List(SqlReflog::ReferenceType type, | |
| 168 | std::vector<shash::Any> *hashes) const { | ||
| 169 | 4 | return ListOlderThan(type, static_cast<uint64_t>(-1), hashes); | |
| 170 | } | ||
| 171 | |||
| 172 | |||
| 173 | 7 | bool Reflog::ListOlderThan(SqlReflog::ReferenceType type, | |
| 174 | uint64_t timestamp, | ||
| 175 | std::vector<shash::Any> *hashes) const { | ||
| 176 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
|
7 | assert(database_.get() != nullptr); |
| 177 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
|
7 | assert(NULL != hashes); |
| 178 | |||
| 179 | 7 | hashes->clear(); | |
| 180 | |||
| 181 | 7 | bool success_bind = list_references_->BindType(type); | |
| 182 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
|
7 | assert(success_bind); |
| 183 | 7 | success_bind = list_references_->BindOlderThan(timestamp); | |
| 184 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
|
7 | assert(success_bind); |
| 185 |
2/2✓ Branch 2 taken 13 times.
✓ Branch 3 taken 7 times.
|
20 | while (list_references_->FetchRow()) { |
| 186 |
1/2✓ Branch 3 taken 13 times.
✗ Branch 4 not taken.
|
13 | hashes->push_back(list_references_->RetrieveHash()); |
| 187 | } | ||
| 188 | |||
| 189 | 7 | return list_references_->Reset(); | |
| 190 | } | ||
| 191 | |||
| 192 | |||
| 193 | 4 | bool Reflog::Remove(const shash::Any &hash) { | |
| 194 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
|
4 | assert(database_.get() != nullptr); |
| 195 | |||
| 196 | SqlReflog::ReferenceType type; | ||
| 197 |
4/5✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
|
4 | switch (hash.suffix) { |
| 198 | 1 | case shash::kSuffixCatalog: | |
| 199 | 1 | type = SqlReflog::kRefCatalog; | |
| 200 | 1 | break; | |
| 201 | 1 | case shash::kSuffixHistory: | |
| 202 | 1 | type = SqlReflog::kRefHistory; | |
| 203 | 1 | break; | |
| 204 | 1 | case shash::kSuffixCertificate: | |
| 205 | 1 | type = SqlReflog::kRefCertificate; | |
| 206 | 1 | break; | |
| 207 | 1 | case shash::kSuffixMetainfo: | |
| 208 | 1 | type = SqlReflog::kRefMetainfo; | |
| 209 | 1 | break; | |
| 210 | ✗ | default: | |
| 211 | ✗ | return false; | |
| 212 | } | ||
| 213 | |||
| 214 | 4 | return remove_reference_->BindReference(hash, type) | |
| 215 |
3/6✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✓ Branch 8 taken 4 times.
✗ Branch 9 not taken.
|
4 | && remove_reference_->Execute() && remove_reference_->Reset(); |
| 216 | } | ||
| 217 | |||
| 218 | |||
| 219 | 4 | bool Reflog::ContainsCertificate(const shash::Any &certificate) const { | |
| 220 |
2/4✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
|
4 | assert(certificate.HasSuffix() |
| 221 | && certificate.suffix == shash::kSuffixCertificate); | ||
| 222 | 4 | return ContainsReference(certificate, SqlReflog::kRefCertificate); | |
| 223 | } | ||
| 224 | |||
| 225 | |||
| 226 | 4 | bool Reflog::ContainsCatalog(const shash::Any &catalog) const { | |
| 227 |
2/4✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
|
4 | assert(catalog.HasSuffix() && catalog.suffix == shash::kSuffixCatalog); |
| 228 | 4 | return ContainsReference(catalog, SqlReflog::kRefCatalog); | |
| 229 | } | ||
| 230 | |||
| 231 | |||
| 232 | 2 | bool Reflog::GetCatalogTimestamp(const shash::Any &catalog, | |
| 233 | uint64_t *timestamp) const { | ||
| 234 |
2/4✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
|
2 | assert(catalog.HasSuffix() && catalog.suffix == shash::kSuffixCatalog); |
| 235 | 2 | const bool result = GetReferenceTimestamp(catalog, SqlReflog::kRefCatalog, | |
| 236 | timestamp); | ||
| 237 | 2 | return result; | |
| 238 | } | ||
| 239 | |||
| 240 | |||
| 241 | 4 | bool Reflog::ContainsHistory(const shash::Any &history) const { | |
| 242 |
2/4✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
|
4 | assert(history.HasSuffix() && history.suffix == shash::kSuffixHistory); |
| 243 | 4 | return ContainsReference(history, SqlReflog::kRefHistory); | |
| 244 | } | ||
| 245 | |||
| 246 | |||
| 247 | 4 | bool Reflog::ContainsMetainfo(const shash::Any &metainfo) const { | |
| 248 |
2/4✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
|
4 | assert(metainfo.HasSuffix() && metainfo.suffix == shash::kSuffixMetainfo); |
| 249 | 4 | return ContainsReference(metainfo, SqlReflog::kRefMetainfo); | |
| 250 | } | ||
| 251 | |||
| 252 | |||
| 253 | 28 | bool Reflog::AddReference(const shash::Any &hash, | |
| 254 | const SqlReflog::ReferenceType type) { | ||
| 255 | 28 | return insert_reference_->BindReference(hash, type) | |
| 256 |
3/6✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 4 taken 28 times.
✗ Branch 5 not taken.
✓ Branch 8 taken 28 times.
✗ Branch 9 not taken.
|
28 | && insert_reference_->Execute() && insert_reference_->Reset(); |
| 257 | } | ||
| 258 | |||
| 259 | |||
| 260 | 16 | bool Reflog::ContainsReference(const shash::Any &hash, | |
| 261 | const SqlReflog::ReferenceType type) const { | ||
| 262 | 16 | const bool fetching = contains_reference_->BindReference(hash, type) | |
| 263 |
2/4✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✓ Branch 4 taken 16 times.
✗ Branch 5 not taken.
|
16 | && contains_reference_->FetchRow(); |
| 264 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
|
16 | assert(fetching); |
| 265 | |||
| 266 | 16 | const bool answer = contains_reference_->RetrieveAnswer(); | |
| 267 | 16 | const bool reset = contains_reference_->Reset(); | |
| 268 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
|
16 | assert(reset); |
| 269 | |||
| 270 | 16 | return answer; | |
| 271 | } | ||
| 272 | |||
| 273 | |||
| 274 | 2 | bool Reflog::GetReferenceTimestamp(const shash::Any &hash, | |
| 275 | const SqlReflog::ReferenceType type, | ||
| 276 | uint64_t *timestamp) const { | ||
| 277 | 2 | const bool retval = get_timestamp_->BindReference(hash, type) | |
| 278 |
3/4✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 1 times.
|
2 | && get_timestamp_->FetchRow(); |
| 279 | |||
| 280 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (retval) { |
| 281 | 1 | *timestamp = get_timestamp_->RetrieveTimestamp(); | |
| 282 | } | ||
| 283 | |||
| 284 | 2 | const bool reset = get_timestamp_->Reset(); | |
| 285 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | assert(reset); |
| 286 | |||
| 287 | 2 | return retval; | |
| 288 | } | ||
| 289 | |||
| 290 | |||
| 291 | ✗ | void Reflog::BeginTransaction() { | |
| 292 | ✗ | assert(database_.get() != nullptr); | |
| 293 | ✗ | database_->BeginTransaction(); | |
| 294 | } | ||
| 295 | |||
| 296 | |||
| 297 | ✗ | void Reflog::CommitTransaction() { | |
| 298 | ✗ | assert(database_.get() != nullptr); | |
| 299 | ✗ | database_->CommitTransaction(); | |
| 300 | } | ||
| 301 | |||
| 302 | |||
| 303 | 44 | void Reflog::TakeDatabaseFileOwnership() { | |
| 304 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 44 times.
|
44 | assert(database_.get() != nullptr); |
| 305 | 44 | database_->TakeFileOwnership(); | |
| 306 | 44 | } | |
| 307 | |||
| 308 | |||
| 309 | ✗ | void Reflog::DropDatabaseFileOwnership() { | |
| 310 | ✗ | assert(database_.get() != nullptr); | |
| 311 | ✗ | database_->DropFileOwnership(); | |
| 312 | } | ||
| 313 | |||
| 314 | |||
| 315 | /** | ||
| 316 | * Use only once the database was closed. | ||
| 317 | */ | ||
| 318 | 286 | void Reflog::HashDatabase(const std::string &database_path, | |
| 319 | shash::Any *hash_reflog) { | ||
| 320 | 286 | const bool retval = HashFile(database_path, hash_reflog); | |
| 321 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 286 times.
|
286 | assert(retval); |
| 322 | 286 | } | |
| 323 | |||
| 324 | |||
| 325 | 60 | std::string Reflog::fqrn() const { | |
| 326 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 60 times.
|
60 | assert(database_.get() != nullptr); |
| 327 | 60 | return database_->GetProperty<std::string>(ReflogDatabase::kFqrnKey); | |
| 328 | } | ||
| 329 | |||
| 330 | |||
| 331 | ✗ | std::string Reflog::database_file() const { | |
| 332 | ✗ | assert(database_.get() != nullptr); | |
| 333 | ✗ | return database_->filename(); | |
| 334 | } | ||
| 335 | |||
| 336 | } // namespace manifest | ||
| 337 |