| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/catalog_rw.cc |
| Date: | 2026-04-26 02:35:59 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 308 | 393 | 78.4% |
| Branches: | 266 | 723 | 36.8% |
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** | ||
| 2 | * This file is part of the CernVM File System. | ||
| 3 | */ | ||
| 4 | |||
| 5 | #define __STDC_FORMAT_MACROS | ||
| 6 | |||
| 7 | #include "catalog_rw.h" | ||
| 8 | |||
| 9 | #include <inttypes.h> | ||
| 10 | |||
| 11 | #include <cstdio> | ||
| 12 | #include <cstdlib> | ||
| 13 | |||
| 14 | #include "util/exception.h" | ||
| 15 | #include "util/logging.h" | ||
| 16 | #include "xattr.h" | ||
| 17 | |||
| 18 | using namespace std; // NOLINT | ||
| 19 | |||
| 20 | namespace catalog { | ||
| 21 | |||
| 22 | const double WritableCatalog::kMaximalFreePageRatio = 0.20; | ||
| 23 | const double WritableCatalog::kMaximalRowIdWasteRatio = 0.25; | ||
| 24 | |||
| 25 | |||
| 26 | 2725 | WritableCatalog::WritableCatalog(const string &path, | |
| 27 | const shash::Any &catalog_hash, | ||
| 28 | Catalog *parent, | ||
| 29 | 2725 | const bool is_not_root) | |
| 30 | 5450 | : Catalog(PathString(path.data(), path.length()), | |
| 31 | catalog_hash, // This is 0 for a newly created catalog! | ||
| 32 | parent, | ||
| 33 | is_not_root) | ||
| 34 | 2725 | , sql_insert_(NULL) | |
| 35 | 2725 | , sql_unlink_(NULL) | |
| 36 | 2725 | , sql_touch_(NULL) | |
| 37 | 2725 | , sql_update_(NULL) | |
| 38 | 2725 | , sql_chunk_insert_(NULL) | |
| 39 | 2725 | , sql_chunks_remove_(NULL) | |
| 40 | 2725 | , sql_chunks_count_(NULL) | |
| 41 | 2725 | , sql_max_link_id_(NULL) | |
| 42 | 2725 | , sql_inc_linkcount_(NULL) | |
| 43 |
2/4✓ Branch 2 taken 2725 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 2725 times.
✗ Branch 6 not taken.
|
8175 | , dirty_(false) { |
| 44 | 2725 | atomic_init32(&dirty_children_); | |
| 45 | 2725 | } | |
| 46 | |||
| 47 | |||
| 48 | 1046 | WritableCatalog *WritableCatalog::AttachFreely(const string &root_path, | |
| 49 | const string &file, | ||
| 50 | const shash::Any &catalog_hash, | ||
| 51 | Catalog *parent, | ||
| 52 | const bool is_not_root) { | ||
| 53 | WritableCatalog *catalog = new WritableCatalog(root_path, catalog_hash, | ||
| 54 |
1/2✓ Branch 2 taken 1046 times.
✗ Branch 3 not taken.
|
1046 | parent, is_not_root); |
| 55 | 1046 | const bool successful_init = catalog->InitStandalone(file); | |
| 56 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1046 times.
|
1046 | if (!successful_init) { |
| 57 | ✗ | delete catalog; | |
| 58 | ✗ | return NULL; | |
| 59 | } | ||
| 60 | 1046 | return catalog; | |
| 61 | } | ||
| 62 | |||
| 63 | |||
| 64 | 10704 | WritableCatalog::~WritableCatalog() { | |
| 65 | // CAUTION HOT! | ||
| 66 | // (see Catalog.h - near the definition of FinalizePreparedStatements) | ||
| 67 | 5352 | FinalizePreparedStatements(); | |
| 68 | 10704 | } | |
| 69 | |||
| 70 | |||
| 71 | 2330 | void WritableCatalog::Transaction() { | |
| 72 |
1/3✓ Branch 2 taken 2330 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
2330 | LogCvmfs(kLogCatalog, kLogVerboseMsg, "opening SQLite transaction for '%s'", |
| 73 | 4660 | mountpoint().c_str()); | |
| 74 | 2330 | const bool retval = database().BeginTransaction(); | |
| 75 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2330 times.
|
2330 | assert(retval == true); |
| 76 | 2330 | } | |
| 77 | |||
| 78 | |||
| 79 | 2288 | void WritableCatalog::Commit() { | |
| 80 |
1/3✓ Branch 2 taken 2288 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
2288 | LogCvmfs(kLogCatalog, kLogVerboseMsg, "closing SQLite transaction for '%s'", |
| 81 | 4576 | mountpoint().c_str()); | |
| 82 | 2288 | const bool retval = database().CommitTransaction(); | |
| 83 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2288 times.
|
2288 | assert(retval == true); |
| 84 | 2288 | dirty_ = false; | |
| 85 | 2288 | } | |
| 86 | |||
| 87 | |||
| 88 | 2725 | void WritableCatalog::InitPreparedStatements() { | |
| 89 | 2725 | Catalog::InitPreparedStatements(); // polymorphism: up call | |
| 90 | |||
| 91 |
2/4✓ Branch 2 taken 2725 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 2725 times.
✗ Branch 7 not taken.
|
5450 | const bool retval = SqlCatalog(database(), "PRAGMA foreign_keys = ON;") |
| 92 |
1/2✓ Branch 1 taken 2725 times.
✗ Branch 2 not taken.
|
2725 | .Execute(); |
| 93 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2725 times.
|
2725 | assert(retval); |
| 94 |
1/2✓ Branch 3 taken 2725 times.
✗ Branch 4 not taken.
|
2725 | sql_insert_ = new SqlDirentInsert(database()); |
| 95 |
1/2✓ Branch 3 taken 2725 times.
✗ Branch 4 not taken.
|
2725 | sql_unlink_ = new SqlDirentUnlink(database()); |
| 96 |
1/2✓ Branch 3 taken 2725 times.
✗ Branch 4 not taken.
|
2725 | sql_touch_ = new SqlDirentTouch(database()); |
| 97 |
1/2✓ Branch 3 taken 2725 times.
✗ Branch 4 not taken.
|
2725 | sql_update_ = new SqlDirentUpdate(database()); |
| 98 |
1/2✓ Branch 3 taken 2725 times.
✗ Branch 4 not taken.
|
2725 | sql_chunk_insert_ = new SqlChunkInsert(database()); |
| 99 |
1/2✓ Branch 3 taken 2725 times.
✗ Branch 4 not taken.
|
2725 | sql_chunks_remove_ = new SqlChunksRemove(database()); |
| 100 |
1/2✓ Branch 3 taken 2725 times.
✗ Branch 4 not taken.
|
2725 | sql_chunks_count_ = new SqlChunksCount(database()); |
| 101 |
1/2✓ Branch 3 taken 2725 times.
✗ Branch 4 not taken.
|
2725 | sql_max_link_id_ = new SqlMaxHardlinkGroup(database()); |
| 102 |
1/2✓ Branch 3 taken 2725 times.
✗ Branch 4 not taken.
|
2725 | sql_inc_linkcount_ = new SqlIncLinkcount(database()); |
| 103 | 2725 | } | |
| 104 | |||
| 105 | |||
| 106 | 2676 | void WritableCatalog::FinalizePreparedStatements() { | |
| 107 | // no polymorphism: no up call (see Catalog.h - | ||
| 108 | // near the definition of this method) | ||
| 109 |
1/2✓ Branch 0 taken 2676 times.
✗ Branch 1 not taken.
|
2676 | delete sql_insert_; |
| 110 |
1/2✓ Branch 0 taken 2676 times.
✗ Branch 1 not taken.
|
2676 | delete sql_unlink_; |
| 111 |
1/2✓ Branch 0 taken 2676 times.
✗ Branch 1 not taken.
|
2676 | delete sql_touch_; |
| 112 |
1/2✓ Branch 0 taken 2676 times.
✗ Branch 1 not taken.
|
2676 | delete sql_update_; |
| 113 |
1/2✓ Branch 0 taken 2676 times.
✗ Branch 1 not taken.
|
2676 | delete sql_chunk_insert_; |
| 114 |
1/2✓ Branch 0 taken 2676 times.
✗ Branch 1 not taken.
|
2676 | delete sql_chunks_remove_; |
| 115 |
1/2✓ Branch 0 taken 2676 times.
✗ Branch 1 not taken.
|
2676 | delete sql_chunks_count_; |
| 116 |
1/2✓ Branch 0 taken 2676 times.
✗ Branch 1 not taken.
|
2676 | delete sql_max_link_id_; |
| 117 |
1/2✓ Branch 0 taken 2676 times.
✗ Branch 1 not taken.
|
2676 | delete sql_inc_linkcount_; |
| 118 | 2676 | } | |
| 119 | |||
| 120 | |||
| 121 | /** | ||
| 122 | * Find out the maximal hardlink group id in this catalog. | ||
| 123 | */ | ||
| 124 | 57 | uint32_t WritableCatalog::GetMaxLinkId() const { | |
| 125 | 57 | int result = -1; | |
| 126 | |||
| 127 |
1/2✓ Branch 1 taken 57 times.
✗ Branch 2 not taken.
|
57 | if (sql_max_link_id_->FetchRow()) { |
| 128 | 57 | result = sql_max_link_id_->GetMaxGroupId(); | |
| 129 | } | ||
| 130 | 57 | sql_max_link_id_->Reset(); | |
| 131 | |||
| 132 | 57 | return result; | |
| 133 | } | ||
| 134 | |||
| 135 | |||
| 136 | /** | ||
| 137 | * Adds a directory entry. | ||
| 138 | * @param entry the DirectoryEntry to add to the catalog | ||
| 139 | * @param entry_path the full path of the DirectoryEntry to add | ||
| 140 | * @param parent_path the full path of the containing directory | ||
| 141 | */ | ||
| 142 | 13327 | void WritableCatalog::AddEntry(const DirectoryEntry &entry, | |
| 143 | const XattrList &xattrs, | ||
| 144 | const string &entry_path, | ||
| 145 | const string &parent_path) { | ||
| 146 |
1/2✓ Branch 1 taken 13327 times.
✗ Branch 2 not taken.
|
13327 | SetDirty(); |
| 147 | |||
| 148 |
1/6✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 13327 times.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
13327 | LogCvmfs(kLogCatalog, kLogVerboseMsg, "add entry '%s' to '%s'", |
| 149 |
1/2✓ Branch 1 taken 13327 times.
✗ Branch 2 not taken.
|
26654 | entry_path.c_str(), mountpoint().c_str()); |
| 150 | |||
| 151 |
1/2✓ Branch 2 taken 13327 times.
✗ Branch 3 not taken.
|
13327 | const shash::Md5 path_hash((shash::AsciiPtr(entry_path))); |
| 152 |
1/2✓ Branch 2 taken 13327 times.
✗ Branch 3 not taken.
|
13327 | const shash::Md5 parent_hash((shash::AsciiPtr(parent_path))); |
| 153 |
1/2✓ Branch 1 taken 13327 times.
✗ Branch 2 not taken.
|
13327 | DirectoryEntry effective_entry(entry); |
| 154 | 13327 | effective_entry.set_has_xattrs(!xattrs.IsEmpty()); | |
| 155 | |||
| 156 |
1/2✓ Branch 1 taken 13327 times.
✗ Branch 2 not taken.
|
13327 | bool retval = sql_insert_->BindPathHash(path_hash) |
| 157 |
2/4✓ Branch 1 taken 13327 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 13327 times.
✗ Branch 4 not taken.
|
13327 | && sql_insert_->BindParentPathHash(parent_hash) |
| 158 |
3/6✓ Branch 0 taken 13327 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 13327 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 13327 times.
✗ Branch 6 not taken.
|
26654 | && sql_insert_->BindDirent(effective_entry); |
| 159 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13327 times.
|
13327 | assert(retval); |
| 160 |
1/2✓ Branch 1 taken 13327 times.
✗ Branch 2 not taken.
|
13327 | if (xattrs.IsEmpty()) { |
| 161 |
1/2✓ Branch 1 taken 13327 times.
✗ Branch 2 not taken.
|
13327 | retval = sql_insert_->BindXattrEmpty(); |
| 162 | } else { | ||
| 163 | ✗ | retval = sql_insert_->BindXattr(xattrs); | |
| 164 | } | ||
| 165 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13327 times.
|
13327 | assert(retval); |
| 166 |
1/2✓ Branch 1 taken 13327 times.
✗ Branch 2 not taken.
|
13327 | retval = sql_insert_->Execute(); |
| 167 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13327 times.
|
13327 | assert(retval); |
| 168 |
1/2✓ Branch 1 taken 13327 times.
✗ Branch 2 not taken.
|
13327 | sql_insert_->Reset(); |
| 169 | |||
| 170 |
1/2✓ Branch 1 taken 13327 times.
✗ Branch 2 not taken.
|
13327 | delta_counters_.Increment(effective_entry); |
| 171 | 13327 | } | |
| 172 | |||
| 173 | |||
| 174 | /** | ||
| 175 | * Removes the specified entry from the catalog. | ||
| 176 | * Note: removing a directory which is non-empty results in dangling entries. | ||
| 177 | * (this should be treated in upper layers) | ||
| 178 | * @param entry_path the full path of the DirectoryEntry to delete | ||
| 179 | */ | ||
| 180 | 2503 | void WritableCatalog::RemoveEntry(const string &file_path) { | |
| 181 |
1/2✓ Branch 1 taken 2503 times.
✗ Branch 2 not taken.
|
2503 | DirectoryEntry entry; |
| 182 |
2/4✓ Branch 1 taken 2503 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2503 times.
✗ Branch 5 not taken.
|
2503 | bool retval = LookupPath(PathString(file_path), &entry); |
| 183 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2503 times.
|
2503 | assert(retval); |
| 184 | |||
| 185 |
1/2✓ Branch 1 taken 2503 times.
✗ Branch 2 not taken.
|
2503 | SetDirty(); |
| 186 | |||
| 187 | // If the entry used to be a chunked file... remove the chunks | ||
| 188 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2503 times.
|
2503 | if (entry.IsChunkedFile()) { |
| 189 | ✗ | RemoveFileChunks(file_path); | |
| 190 | } | ||
| 191 | |||
| 192 | // remove the entry itself | ||
| 193 |
1/2✓ Branch 2 taken 2503 times.
✗ Branch 3 not taken.
|
2503 | const shash::Md5 path_hash = shash::Md5(shash::AsciiPtr(file_path)); |
| 194 |
4/9✓ Branch 1 taken 2503 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2503 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 2503 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2503 times.
✗ Branch 9 not taken.
|
2503 | retval = sql_unlink_->BindPathHash(path_hash) && sql_unlink_->Execute(); |
| 195 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2503 times.
|
2503 | assert(retval); |
| 196 |
1/2✓ Branch 1 taken 2503 times.
✗ Branch 2 not taken.
|
2503 | sql_unlink_->Reset(); |
| 197 | |||
| 198 |
1/2✓ Branch 1 taken 2503 times.
✗ Branch 2 not taken.
|
2503 | delta_counters_.Decrement(entry); |
| 199 | 2503 | } | |
| 200 | |||
| 201 | |||
| 202 | ✗ | void WritableCatalog::IncLinkcount(const string &path_within_group, | |
| 203 | const int delta) { | ||
| 204 | ✗ | SetDirty(); | |
| 205 | |||
| 206 | ✗ | const shash::Md5 path_hash = shash::Md5(shash::AsciiPtr(path_within_group)); | |
| 207 | |||
| 208 | ✗ | const bool retval = sql_inc_linkcount_->BindPathHash(path_hash) | |
| 209 | ✗ | && sql_inc_linkcount_->BindDelta(delta) | |
| 210 | ✗ | && sql_inc_linkcount_->Execute(); | |
| 211 | ✗ | assert(retval); | |
| 212 | ✗ | sql_inc_linkcount_->Reset(); | |
| 213 | } | ||
| 214 | |||
| 215 | |||
| 216 | 58 | void WritableCatalog::TouchEntry(const DirectoryEntryBase &entry, | |
| 217 | const XattrList &xattrs, | ||
| 218 | const shash::Md5 &path_hash) { | ||
| 219 |
1/2✓ Branch 1 taken 58 times.
✗ Branch 2 not taken.
|
58 | SetDirty(); |
| 220 | |||
| 221 |
1/2✓ Branch 1 taken 58 times.
✗ Branch 2 not taken.
|
58 | catalog::DirectoryEntry prev_entry; |
| 222 |
1/2✓ Branch 1 taken 58 times.
✗ Branch 2 not taken.
|
58 | bool retval = LookupMd5Path(path_hash, &prev_entry); |
| 223 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
|
58 | assert(retval); |
| 224 | |||
| 225 |
1/2✓ Branch 1 taken 58 times.
✗ Branch 2 not taken.
|
58 | retval = sql_touch_->BindPathHash(path_hash) |
| 226 |
3/6✓ Branch 0 taken 58 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 58 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 58 times.
✗ Branch 6 not taken.
|
58 | && sql_touch_->BindDirentBase(entry); |
| 227 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
|
58 | assert(retval); |
| 228 |
1/2✓ Branch 1 taken 58 times.
✗ Branch 2 not taken.
|
58 | if (xattrs.IsEmpty()) { |
| 229 |
1/2✓ Branch 1 taken 58 times.
✗ Branch 2 not taken.
|
58 | retval = sql_touch_->BindXattrEmpty(); |
| 230 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 58 times.
|
58 | if (prev_entry.HasXattrs()) |
| 231 | ✗ | delta_counters_.self.xattrs--; | |
| 232 | } else { | ||
| 233 | ✗ | retval = sql_touch_->BindXattr(xattrs); | |
| 234 | ✗ | if (!prev_entry.HasXattrs()) | |
| 235 | ✗ | delta_counters_.self.xattrs++; | |
| 236 | } | ||
| 237 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
|
58 | assert(retval); |
| 238 |
1/2✓ Branch 1 taken 58 times.
✗ Branch 2 not taken.
|
58 | retval = sql_touch_->Execute(); |
| 239 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
|
58 | assert(retval); |
| 240 |
1/2✓ Branch 1 taken 58 times.
✗ Branch 2 not taken.
|
58 | sql_touch_->Reset(); |
| 241 | 58 | } | |
| 242 | |||
| 243 | |||
| 244 | 4144 | void WritableCatalog::UpdateEntry(const DirectoryEntry &entry, | |
| 245 | const shash::Md5 &path_hash) { | ||
| 246 | 4144 | SetDirty(); | |
| 247 | |||
| 248 | 4144 | const bool retval = sql_update_->BindPathHash(path_hash) | |
| 249 |
1/2✓ Branch 1 taken 4144 times.
✗ Branch 2 not taken.
|
4144 | && sql_update_->BindDirent(entry) |
| 250 |
2/4✓ Branch 0 taken 4144 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 4144 times.
✗ Branch 4 not taken.
|
8288 | && sql_update_->Execute(); |
| 251 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4144 times.
|
4144 | assert(retval); |
| 252 | 4144 | sql_update_->Reset(); | |
| 253 | 4144 | } | |
| 254 | |||
| 255 | 476 | void WritableCatalog::AddFileChunk(const std::string &entry_path, | |
| 256 | const FileChunk &chunk) { | ||
| 257 |
1/2✓ Branch 1 taken 476 times.
✗ Branch 2 not taken.
|
476 | SetDirty(); |
| 258 | |||
| 259 |
1/2✓ Branch 2 taken 476 times.
✗ Branch 3 not taken.
|
476 | const shash::Md5 path_hash((shash::AsciiPtr(entry_path))); |
| 260 | |||
| 261 |
1/4✓ Branch 3 taken 476 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
|
476 | LogCvmfs(kLogCatalog, kLogVerboseMsg, |
| 262 | "adding chunk for %s from offset %ld " | ||
| 263 | "and chunk size: %ld bytes", | ||
| 264 | 476 | entry_path.c_str(), chunk.offset(), chunk.offset() + chunk.size()); | |
| 265 | |||
| 266 | 476 | delta_counters_.self.file_chunks++; | |
| 267 | |||
| 268 |
1/2✓ Branch 1 taken 476 times.
✗ Branch 2 not taken.
|
476 | const bool retval = sql_chunk_insert_->BindPathHash(path_hash) |
| 269 |
2/4✓ Branch 1 taken 476 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 476 times.
✗ Branch 4 not taken.
|
476 | && sql_chunk_insert_->BindFileChunk(chunk) |
| 270 |
3/6✓ Branch 0 taken 476 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 476 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 476 times.
✗ Branch 6 not taken.
|
952 | && sql_chunk_insert_->Execute(); |
| 271 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 476 times.
|
476 | assert(retval); |
| 272 |
1/2✓ Branch 1 taken 476 times.
✗ Branch 2 not taken.
|
476 | sql_chunk_insert_->Reset(); |
| 273 | 476 | } | |
| 274 | |||
| 275 | |||
| 276 | /** | ||
| 277 | * Removes the file chunks for a given file path | ||
| 278 | * @param entry_path the file path to clear from it's file chunks | ||
| 279 | */ | ||
| 280 | ✗ | void WritableCatalog::RemoveFileChunks(const std::string &entry_path) { | |
| 281 | ✗ | const shash::Md5 path_hash((shash::AsciiPtr(entry_path))); | |
| 282 | bool retval; | ||
| 283 | |||
| 284 | // subtract the number of chunks from the statistics counters | ||
| 285 | ✗ | retval = sql_chunks_count_->BindPathHash(path_hash) | |
| 286 | ✗ | && sql_chunks_count_->Execute(); | |
| 287 | ✗ | assert(retval); | |
| 288 | ✗ | const int chunks_count = sql_chunks_count_->GetChunkCount(); | |
| 289 | ✗ | delta_counters_.self.file_chunks -= chunks_count; | |
| 290 | ✗ | sql_chunks_count_->Reset(); | |
| 291 | |||
| 292 | // remove the chunks associated to `entry_path` | ||
| 293 | ✗ | retval = sql_chunks_remove_->BindPathHash(path_hash) | |
| 294 | ✗ | && sql_chunks_remove_->Execute(); | |
| 295 | ✗ | assert(retval); | |
| 296 | ✗ | sql_chunks_remove_->Reset(); | |
| 297 | } | ||
| 298 | |||
| 299 | |||
| 300 | /** | ||
| 301 | * Sets the last modified time stamp of this catalog to current time. | ||
| 302 | */ | ||
| 303 | 5990 | void WritableCatalog::UpdateLastModified() { | |
| 304 |
2/4✓ Branch 4 taken 5990 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 5990 times.
✗ Branch 8 not taken.
|
5990 | database().SetProperty("last_modified", static_cast<uint64_t>(time(NULL))); |
| 305 | 5990 | } | |
| 306 | |||
| 307 | |||
| 308 | /** | ||
| 309 | * Increments the revision of the catalog in the database. | ||
| 310 | */ | ||
| 311 | 1230 | void WritableCatalog::IncrementRevision() { SetRevision(GetRevision() + 1); } | |
| 312 | |||
| 313 | |||
| 314 | 1230 | void WritableCatalog::SetRevision(const uint64_t new_revision) { | |
| 315 |
2/4✓ Branch 3 taken 1230 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 1230 times.
✗ Branch 7 not taken.
|
1230 | database().SetProperty("revision", new_revision); |
| 316 | 1230 | } | |
| 317 | |||
| 318 | |||
| 319 | ✗ | void WritableCatalog::SetBranch(const std::string &branch_name) { | |
| 320 | ✗ | database().SetProperty("branch", branch_name); | |
| 321 | } | ||
| 322 | |||
| 323 | |||
| 324 | ✗ | void WritableCatalog::SetTTL(const uint64_t new_ttl) { | |
| 325 | ✗ | database().SetProperty("TTL", new_ttl); | |
| 326 | } | ||
| 327 | |||
| 328 | |||
| 329 | ✗ | bool WritableCatalog::SetVOMSAuthz(const std::string &voms_authz) { | |
| 330 | ✗ | return database().SetVOMSAuthz(voms_authz); | |
| 331 | } | ||
| 332 | |||
| 333 | |||
| 334 | /** | ||
| 335 | * Sets the content hash of the previous catalog revision. | ||
| 336 | */ | ||
| 337 | 1230 | void WritableCatalog::SetPreviousRevision(const shash::Any &hash) { | |
| 338 |
2/4✓ Branch 4 taken 1230 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1230 times.
✗ Branch 8 not taken.
|
1230 | database().SetProperty("previous_revision", hash.ToString()); |
| 339 | 1230 | } | |
| 340 | |||
| 341 | |||
| 342 | /** | ||
| 343 | * Moves a subtree from this catalog into a just created nested catalog. | ||
| 344 | */ | ||
| 345 | 579 | void WritableCatalog::Partition(WritableCatalog *new_nested_catalog) { | |
| 346 | // Create connection between parent and child catalogs | ||
| 347 |
3/6✓ Branch 1 taken 579 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 579 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 579 times.
✗ Branch 8 not taken.
|
579 | MakeTransitionPoint(new_nested_catalog->mountpoint().ToString()); |
| 348 |
1/2✓ Branch 1 taken 579 times.
✗ Branch 2 not taken.
|
579 | new_nested_catalog->MakeNestedRoot(); |
| 349 | 579 | delta_counters_.subtree.directories++; // Root directory in nested catalog | |
| 350 | |||
| 351 | // Move the present directory tree into the newly created nested catalog | ||
| 352 | // if we hit nested catalog mountpoints on the way, we return them through | ||
| 353 | // the passed list | ||
| 354 | 579 | vector<string> GrandChildMountpoints; | |
| 355 |
3/6✓ Branch 1 taken 579 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 579 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 579 times.
✗ Branch 8 not taken.
|
579 | MoveToNested(new_nested_catalog->mountpoint().ToString(), new_nested_catalog, |
| 356 | &GrandChildMountpoints); | ||
| 357 | |||
| 358 | // Nested catalog mountpoints found in the moved directory structure are now | ||
| 359 | // links to nested catalogs of the newly created nested catalog. | ||
| 360 | // Move these references into the new nested catalog | ||
| 361 |
1/2✓ Branch 1 taken 579 times.
✗ Branch 2 not taken.
|
579 | MoveCatalogsToNested(GrandChildMountpoints, new_nested_catalog); |
| 362 | 579 | } | |
| 363 | |||
| 364 | |||
| 365 | 579 | void WritableCatalog::MakeTransitionPoint(const string &mountpoint) { | |
| 366 | // Find the directory entry to edit | ||
| 367 |
1/2✓ Branch 1 taken 579 times.
✗ Branch 2 not taken.
|
579 | DirectoryEntry transition_entry; |
| 368 |
1/2✓ Branch 1 taken 579 times.
✗ Branch 2 not taken.
|
579 | const bool retval = LookupPath( |
| 369 |
1/2✓ Branch 3 taken 579 times.
✗ Branch 4 not taken.
|
1158 | PathString(mountpoint.data(), mountpoint.length()), &transition_entry); |
| 370 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 579 times.
|
579 | assert(retval); |
| 371 | |||
| 372 |
2/4✓ Branch 1 taken 579 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 579 times.
✗ Branch 5 not taken.
|
579 | assert(transition_entry.IsDirectory() |
| 373 | && !transition_entry.IsNestedCatalogRoot()); | ||
| 374 | |||
| 375 | 579 | transition_entry.set_is_nested_catalog_mountpoint(true); | |
| 376 |
1/2✓ Branch 1 taken 579 times.
✗ Branch 2 not taken.
|
579 | UpdateEntry(transition_entry, mountpoint); |
| 377 | 579 | } | |
| 378 | |||
| 379 | |||
| 380 | 579 | void WritableCatalog::MakeNestedRoot() { | |
| 381 |
1/2✓ Branch 1 taken 579 times.
✗ Branch 2 not taken.
|
579 | DirectoryEntry root_entry; |
| 382 |
2/4✓ Branch 1 taken 579 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 579 times.
✗ Branch 5 not taken.
|
579 | const bool retval = LookupPath(mountpoint(), &root_entry); |
| 383 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 579 times.
|
579 | assert(retval); |
| 384 | |||
| 385 |
2/4✓ Branch 1 taken 579 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 579 times.
✗ Branch 5 not taken.
|
579 | assert(root_entry.IsDirectory() && !root_entry.IsNestedCatalogMountpoint()); |
| 386 | |||
| 387 | 579 | root_entry.set_is_nested_catalog_root(true); | |
| 388 |
3/6✓ Branch 1 taken 579 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 579 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 579 times.
✗ Branch 8 not taken.
|
579 | UpdateEntry(root_entry, mountpoint().ToString()); |
| 389 | 579 | } | |
| 390 | |||
| 391 | |||
| 392 | 963 | void WritableCatalog::MoveToNestedRecursively( | |
| 393 | const string directory, | ||
| 394 | WritableCatalog *new_nested_catalog, | ||
| 395 | vector<string> *grand_child_mountpoints) { | ||
| 396 | // After creating a new nested catalog we have to move all elements | ||
| 397 | // now contained by the new one. List and move them recursively. | ||
| 398 | 963 | DirectoryEntryList listing; | |
| 399 | 963 | const bool resolve_magic_symlinks = false; | |
| 400 |
2/4✓ Branch 1 taken 963 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 963 times.
✗ Branch 5 not taken.
|
963 | bool retval = ListingPath(PathString(directory), &listing, |
| 401 | resolve_magic_symlinks); | ||
| 402 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 963 times.
|
963 | assert(retval); |
| 403 | |||
| 404 | // Go through the listing | ||
| 405 | 963 | const XattrList empty_xattrs; | |
| 406 | 963 | for (DirectoryEntryList::const_iterator i = listing.begin(), | |
| 407 | 963 | iEnd = listing.end(); | |
| 408 |
2/2✓ Branch 1 taken 2185 times.
✓ Branch 2 taken 963 times.
|
3148 | i != iEnd; |
| 409 | 2185 | ++i) { | |
| 410 |
1/2✓ Branch 2 taken 2185 times.
✗ Branch 3 not taken.
|
2185 | const string full_path = i->GetFullPath(directory); |
| 411 | |||
| 412 | // The entries are first inserted into the new catalog | ||
| 413 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 2185 times.
|
2185 | if (i->HasXattrs()) { |
| 414 | ✗ | XattrList xattrs; | |
| 415 | ✗ | retval = LookupXattrsPath(PathString(full_path), &xattrs); | |
| 416 | ✗ | assert(retval); | |
| 417 | ✗ | assert(!xattrs.IsEmpty()); | |
| 418 | ✗ | new_nested_catalog->AddEntry(*i, xattrs, full_path); | |
| 419 | ✗ | } else { | |
| 420 |
1/2✓ Branch 2 taken 2185 times.
✗ Branch 3 not taken.
|
2185 | new_nested_catalog->AddEntry(*i, empty_xattrs, full_path); |
| 421 | } | ||
| 422 | |||
| 423 | // Then we check if we have some special cases: | ||
| 424 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 2185 times.
|
2185 | if (i->IsNestedCatalogMountpoint()) { |
| 425 | ✗ | grand_child_mountpoints->push_back(full_path); | |
| 426 |
2/2✓ Branch 2 taken 384 times.
✓ Branch 3 taken 1801 times.
|
2185 | } else if (i->IsDirectory()) { |
| 427 | // Recurse deeper into the directory tree | ||
| 428 |
2/4✓ Branch 1 taken 384 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 384 times.
✗ Branch 5 not taken.
|
384 | MoveToNestedRecursively(full_path, new_nested_catalog, |
| 429 | grand_child_mountpoints); | ||
| 430 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 1801 times.
|
1801 | } else if (i->IsChunkedFile()) { |
| 431 | ✗ | MoveFileChunksToNested(full_path, i->hash_algorithm(), | |
| 432 | new_nested_catalog); | ||
| 433 | } | ||
| 434 | |||
| 435 | // Remove the entry from the current catalog | ||
| 436 |
1/2✓ Branch 1 taken 2185 times.
✗ Branch 2 not taken.
|
2185 | RemoveEntry(full_path); |
| 437 | 2185 | } | |
| 438 | 963 | } | |
| 439 | |||
| 440 | |||
| 441 | 579 | void WritableCatalog::MoveCatalogsToNested( | |
| 442 | const vector<string> &nested_catalogs, | ||
| 443 | WritableCatalog *new_nested_catalog) { | ||
| 444 | 1158 | for (vector<string>::const_iterator i = nested_catalogs.begin(), | |
| 445 | 579 | iEnd = nested_catalogs.end(); | |
| 446 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 579 times.
|
579 | i != iEnd; |
| 447 | ✗ | ++i) { | |
| 448 | ✗ | shash::Any hash_nested; | |
| 449 | uint64_t size_nested; | ||
| 450 | ✗ | const bool retval = FindNested(PathString(*i), &hash_nested, &size_nested); | |
| 451 | ✗ | assert(retval); | |
| 452 | |||
| 453 | ✗ | Catalog *attached_reference = NULL; | |
| 454 | ✗ | RemoveNestedCatalog(*i, &attached_reference); | |
| 455 | |||
| 456 | ✗ | new_nested_catalog->InsertNestedCatalog(*i, attached_reference, hash_nested, | |
| 457 | size_nested); | ||
| 458 | } | ||
| 459 | 579 | } | |
| 460 | |||
| 461 | |||
| 462 | ✗ | void WritableCatalog::MoveFileChunksToNested( | |
| 463 | const std::string &full_path, | ||
| 464 | const shash::Algorithms algorithm, | ||
| 465 | WritableCatalog *new_nested_catalog) { | ||
| 466 | ✗ | FileChunkList chunks; | |
| 467 | ✗ | ListPathChunks(PathString(full_path), algorithm, &chunks); | |
| 468 | ✗ | assert(chunks.size() > 0); | |
| 469 | |||
| 470 | ✗ | for (unsigned i = 0; i < chunks.size(); ++i) { | |
| 471 | ✗ | new_nested_catalog->AddFileChunk(full_path, *chunks.AtPtr(i)); | |
| 472 | } | ||
| 473 | } | ||
| 474 | |||
| 475 | |||
| 476 | /** | ||
| 477 | * Insert a nested catalog reference into this catalog. | ||
| 478 | * The attached catalog object of this mountpoint can be specified (optional) | ||
| 479 | * This way, the in-memory representation of the catalog tree is updated, too | ||
| 480 | * @param mountpoint the path to the catalog to add a reference to | ||
| 481 | * @param attached_reference can contain a reference to the attached catalog | ||
| 482 | * object of mountpoint | ||
| 483 | * @param content_hash can be set to safe a content hash together with the | ||
| 484 | * reference | ||
| 485 | */ | ||
| 486 | 1068 | void WritableCatalog::InsertNestedCatalog(const string &mountpoint, | |
| 487 | Catalog *attached_reference, | ||
| 488 | const shash::Any content_hash, | ||
| 489 | const uint64_t size) { | ||
| 490 | 1068 | const string hash_string = (!content_hash.IsNull()) ? content_hash.ToString() | |
| 491 |
6/12✓ Branch 0 taken 489 times.
✓ Branch 1 taken 579 times.
✓ Branch 3 taken 489 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 579 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 579 times.
✓ Branch 10 taken 489 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
|
1068 | : ""; |
| 492 | |||
| 493 | 1068 | SqlCatalog stmt(database(), "INSERT INTO nested_catalogs (path, sha1, size) " | |
| 494 |
2/4✓ Branch 2 taken 1068 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1068 times.
✗ Branch 6 not taken.
|
3204 | "VALUES (:p, :sha1, :size);"); |
| 495 |
1/2✓ Branch 1 taken 1068 times.
✗ Branch 2 not taken.
|
1068 | const bool retval = stmt.BindText(1, mountpoint) |
| 496 |
2/4✓ Branch 1 taken 1068 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1068 times.
✗ Branch 4 not taken.
|
1068 | && stmt.BindText(2, hash_string) |
| 497 |
5/11✓ Branch 0 taken 1068 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 1068 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1068 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 1068 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 1068 times.
✗ Branch 11 not taken.
|
2136 | && stmt.BindInt64(3, size) && stmt.Execute(); |
| 498 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1068 times.
|
1068 | assert(retval); |
| 499 | |||
| 500 | // If a reference of the in-memory object of the newly referenced | ||
| 501 | // catalog was passed, we add this to our own children | ||
| 502 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1068 times.
|
1068 | if (attached_reference != NULL) |
| 503 | ✗ | AddChild(attached_reference); | |
| 504 | |||
| 505 |
1/2✓ Branch 1 taken 1068 times.
✗ Branch 2 not taken.
|
1068 | ResetNestedCatalogCacheUnprotected(); |
| 506 | |||
| 507 | 1068 | delta_counters_.self.nested_catalogs++; | |
| 508 | 1068 | } | |
| 509 | |||
| 510 | |||
| 511 | /** | ||
| 512 | * Registers a snapshot in /.cvmfs/snapshots. Note that bind mountpoints are | ||
| 513 | * not universally handled: in Partition and MergeIntoParent, bind mountpoint | ||
| 514 | * handling is missing! | ||
| 515 | */ | ||
| 516 | ✗ | void WritableCatalog::InsertBindMountpoint(const string &mountpoint, | |
| 517 | const shash::Any content_hash, | ||
| 518 | const uint64_t size) { | ||
| 519 | ✗ | SqlCatalog stmt(database(), | |
| 520 | "INSERT INTO bind_mountpoints (path, sha1, size) " | ||
| 521 | ✗ | "VALUES (:p, :sha1, :size);"); | |
| 522 | ✗ | const bool retval = stmt.BindText(1, mountpoint) | |
| 523 | ✗ | && stmt.BindText(2, content_hash.ToString()) | |
| 524 | ✗ | && stmt.BindInt64(3, size) && stmt.Execute(); | |
| 525 | ✗ | assert(retval); | |
| 526 | } | ||
| 527 | |||
| 528 | |||
| 529 | /** | ||
| 530 | * Remove a nested catalog reference from the database. | ||
| 531 | * If the catalog 'mountpoint' is currently attached as a child, it will be | ||
| 532 | * removed, too (but not detached). | ||
| 533 | * @param[in] mountpoint the mountpoint of the nested catalog to dereference in | ||
| 534 | the database | ||
| 535 | * @param[out] attached_reference is set to the object of the attached child or | ||
| 536 | * to NULL | ||
| 537 | */ | ||
| 538 | 66 | void WritableCatalog::RemoveNestedCatalog(const string &mountpoint, | |
| 539 | Catalog **attached_reference) { | ||
| 540 |
1/2✓ Branch 1 taken 66 times.
✗ Branch 2 not taken.
|
66 | shash::Any dummy; |
| 541 | uint64_t dummy_size; | ||
| 542 |
2/4✓ Branch 3 taken 66 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 66 times.
✗ Branch 7 not taken.
|
66 | bool retval = FindNested(PathString(mountpoint.data(), mountpoint.length()), |
| 543 | &dummy, &dummy_size); | ||
| 544 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 66 times.
|
66 | assert(retval); |
| 545 | |||
| 546 |
2/4✓ Branch 2 taken 66 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 66 times.
✗ Branch 7 not taken.
|
132 | SqlCatalog stmt(database(), "DELETE FROM nested_catalogs WHERE path = :p;"); |
| 547 |
4/9✓ Branch 1 taken 66 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 66 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 66 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 66 times.
✗ Branch 9 not taken.
|
66 | retval = stmt.BindText(1, mountpoint) && stmt.Execute(); |
| 548 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 66 times.
|
66 | assert(retval); |
| 549 | |||
| 550 | // If the reference was successfully deleted, we also have to check whether | ||
| 551 | // there is also an attached reference in our in-memory data. | ||
| 552 | // In this case we remove the child and return it through **attached_reference | ||
| 553 |
2/4✓ Branch 1 taken 66 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 66 times.
✗ Branch 5 not taken.
|
66 | Catalog *child = FindChild(PathString(mountpoint)); |
| 554 |
2/2✓ Branch 0 taken 57 times.
✓ Branch 1 taken 9 times.
|
66 | if (child != NULL) |
| 555 |
1/2✓ Branch 1 taken 57 times.
✗ Branch 2 not taken.
|
57 | RemoveChild(child); |
| 556 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 66 times.
|
66 | if (attached_reference != NULL) |
| 557 | ✗ | *attached_reference = child; | |
| 558 | |||
| 559 |
1/2✓ Branch 1 taken 66 times.
✗ Branch 2 not taken.
|
66 | ResetNestedCatalogCacheUnprotected(); |
| 560 | |||
| 561 | 66 | delta_counters_.self.nested_catalogs--; | |
| 562 | 66 | } | |
| 563 | |||
| 564 | |||
| 565 | /** | ||
| 566 | * Unregisters a snapshot from /.cvmfs/snapshots. Note that bind mountpoints | ||
| 567 | * are not universally handled: in Partition and MergeIntoParent, bind | ||
| 568 | * mountpoint handling is missing! | ||
| 569 | */ | ||
| 570 | ✗ | void WritableCatalog::RemoveBindMountpoint(const std::string &mountpoint) { | |
| 571 | ✗ | shash::Any dummy; | |
| 572 | uint64_t dummy_size; | ||
| 573 | ✗ | bool retval = FindNested(PathString(mountpoint.data(), mountpoint.length()), | |
| 574 | &dummy, &dummy_size); | ||
| 575 | ✗ | assert(retval); | |
| 576 | |||
| 577 | ✗ | SqlCatalog stmt(database(), "DELETE FROM bind_mountpoints WHERE path = :p;"); | |
| 578 | ✗ | retval = stmt.BindText(1, mountpoint) && stmt.Execute(); | |
| 579 | ✗ | assert(retval); | |
| 580 | } | ||
| 581 | |||
| 582 | |||
| 583 | /** | ||
| 584 | * Updates the link to a nested catalog in the database. | ||
| 585 | * @param path the path of the nested catalog to update | ||
| 586 | * @param hash the hash to set the given nested catalog link to | ||
| 587 | * @param size the uncompressed catalog database file size | ||
| 588 | * @param child_counters the statistics counters of the nested catalog | ||
| 589 | */ | ||
| 590 | 567 | void WritableCatalog::UpdateNestedCatalog(const std::string &path, | |
| 591 | const shash::Any &hash, | ||
| 592 | const uint64_t size, | ||
| 593 | const DeltaCounters &child_counters) { | ||
| 594 | 567 | const MutexLockGuard guard(lock_); | |
| 595 |
1/2✓ Branch 1 taken 567 times.
✗ Branch 2 not taken.
|
567 | SetDirty(); |
| 596 | |||
| 597 |
1/2✓ Branch 1 taken 567 times.
✗ Branch 2 not taken.
|
567 | child_counters.PopulateToParent(&delta_counters_); |
| 598 | |||
| 599 |
1/2✓ Branch 1 taken 567 times.
✗ Branch 2 not taken.
|
567 | const string hash_str = hash.ToString(); |
| 600 | const string sql = "UPDATE nested_catalogs SET sha1 = :sha1, size = :size " | ||
| 601 |
1/2✓ Branch 2 taken 567 times.
✗ Branch 3 not taken.
|
567 | "WHERE path = :path;"; |
| 602 |
1/2✓ Branch 2 taken 567 times.
✗ Branch 3 not taken.
|
567 | SqlCatalog stmt(database(), sql); |
| 603 | |||
| 604 |
3/7✓ Branch 1 taken 567 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 567 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 567 times.
✗ Branch 7 not taken.
|
1134 | const bool retval = stmt.BindText(1, hash_str) && stmt.BindInt64(2, size) |
| 605 |
5/11✓ Branch 0 taken 567 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 567 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 567 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 567 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 567 times.
✗ Branch 11 not taken.
|
1134 | && stmt.BindText(3, path) && stmt.Execute(); |
| 606 | |||
| 607 |
1/2✓ Branch 1 taken 567 times.
✗ Branch 2 not taken.
|
567 | ResetNestedCatalogCacheUnprotected(); |
| 608 | |||
| 609 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 567 times.
|
567 | assert(retval); |
| 610 | 567 | } | |
| 611 | |||
| 612 | |||
| 613 | 57 | void WritableCatalog::MergeIntoParent() { | |
| 614 |
2/4✓ Branch 1 taken 57 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 57 times.
✗ Branch 5 not taken.
|
57 | assert(!IsRoot() && HasParent()); |
| 615 | 57 | WritableCatalog *parent = GetWritableParent(); | |
| 616 | |||
| 617 | 57 | CopyToParent(); | |
| 618 | |||
| 619 | // Copy the nested catalog references | ||
| 620 | 57 | CopyCatalogsToParent(); | |
| 621 | |||
| 622 | // Fix counters in parent | ||
| 623 | 57 | delta_counters_.PopulateToParent(&parent->delta_counters_); | |
| 624 | 57 | Counters &counters = GetWritableCounters(); | |
| 625 | 57 | counters.ApplyDelta(delta_counters_); | |
| 626 | 57 | counters.MergeIntoParent(&parent->delta_counters_); | |
| 627 | |||
| 628 | // Remove the nested catalog reference for this nested catalog. | ||
| 629 | // From now on this catalog will be dangling! | ||
| 630 |
2/4✓ Branch 2 taken 57 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 57 times.
✗ Branch 6 not taken.
|
57 | parent->RemoveNestedCatalog(this->mountpoint().ToString(), NULL); |
| 631 | 57 | } | |
| 632 | |||
| 633 | |||
| 634 | ✗ | void WritableCatalog::RemoveFromParent() { | |
| 635 | ✗ | assert(!IsRoot() && HasParent()); | |
| 636 | ✗ | WritableCatalog *parent = GetWritableParent(); | |
| 637 | |||
| 638 | // Remove the nested catalog reference for this nested catalog. | ||
| 639 | // From now on this catalog will be dangling! | ||
| 640 | ✗ | parent->RemoveNestedCatalog(this->mountpoint().ToString(), NULL); | |
| 641 | ✗ | parent->delta_counters_.RemoveFromSubtree( | |
| 642 | ✗ | Counters::Diff(Counters(), GetCounters())); | |
| 643 | } | ||
| 644 | |||
| 645 | |||
| 646 | 57 | void WritableCatalog::CopyCatalogsToParent() { | |
| 647 |
1/2✓ Branch 1 taken 57 times.
✗ Branch 2 not taken.
|
57 | WritableCatalog *parent = GetWritableParent(); |
| 648 | |||
| 649 | // Obtain a list of all nested catalog references | ||
| 650 |
1/2✓ Branch 1 taken 57 times.
✗ Branch 2 not taken.
|
57 | const NestedCatalogList nested_catalog_references = ListOwnNestedCatalogs(); |
| 651 | |||
| 652 | // Go through the list and update the databases | ||
| 653 | // simultaneously we are checking if the referenced catalogs are currently | ||
| 654 | // attached and update the in-memory data structures as well | ||
| 655 | 114 | for (NestedCatalogList::const_iterator i = nested_catalog_references.begin(), | |
| 656 | 57 | iEnd = nested_catalog_references.end(); | |
| 657 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 57 times.
|
57 | i != iEnd; |
| 658 | ✗ | ++i) { | |
| 659 | ✗ | Catalog *child = FindChild(i->mountpoint); | |
| 660 | ✗ | parent->InsertNestedCatalog(i->mountpoint.ToString(), child, i->hash, | |
| 661 | ✗ | i->size); | |
| 662 | ✗ | parent->delta_counters_.self.nested_catalogs--; // Will be fixed later | |
| 663 | } | ||
| 664 | 57 | } | |
| 665 | |||
| 666 | 57 | void WritableCatalog::CopyToParent() { | |
| 667 | // We could simply copy all entries from this database to the 'other' database | ||
| 668 | // BUT: 1. this would create collisions in hardlink group IDs. | ||
| 669 | // therefore we first update all hardlink group IDs to fit behind the | ||
| 670 | // ones in the 'other' database | ||
| 671 | // 2. the root entry of the nested catalog is present twice: | ||
| 672 | // 1. in the parent directory (as mount point) and | ||
| 673 | // 2. in the nested catalog (as root entry) | ||
| 674 | // therefore we delete the mount point from the parent before merging | ||
| 675 | |||
| 676 |
1/2✓ Branch 1 taken 57 times.
✗ Branch 2 not taken.
|
57 | WritableCatalog *parent = GetWritableParent(); |
| 677 | |||
| 678 | // Update hardlink group IDs in this nested catalog. | ||
| 679 | // To avoid collisions we add the maximal present hardlink group ID in parent | ||
| 680 | // to all hardlink group IDs in the nested catalog. | ||
| 681 |
1/2✓ Branch 1 taken 57 times.
✗ Branch 2 not taken.
|
57 | const uint64_t offset = static_cast<uint64_t>(parent->GetMaxLinkId()) << 32; |
| 682 | const string update_link_ids = "UPDATE catalog SET hardlinks = hardlinks + " | ||
| 683 |
2/4✓ Branch 1 taken 57 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 57 times.
✗ Branch 5 not taken.
|
114 | + StringifyInt(offset) |
| 684 |
1/2✓ Branch 1 taken 57 times.
✗ Branch 2 not taken.
|
57 | + " WHERE hardlinks > (1 << 32);"; |
| 685 | |||
| 686 |
1/2✓ Branch 2 taken 57 times.
✗ Branch 3 not taken.
|
57 | SqlCatalog sql_update_link_ids(database(), update_link_ids); |
| 687 |
1/2✓ Branch 1 taken 57 times.
✗ Branch 2 not taken.
|
57 | bool retval = sql_update_link_ids.Execute(); |
| 688 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 57 times.
|
57 | assert(retval); |
| 689 | |||
| 690 | // Remove the nested catalog root. | ||
| 691 | // It is already present in the parent. | ||
| 692 |
3/6✓ Branch 1 taken 57 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 57 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 57 times.
✗ Branch 8 not taken.
|
57 | RemoveEntry(this->mountpoint().ToString()); |
| 693 | |||
| 694 | // Now copy all DirectoryEntries to the 'other' catalog. | ||
| 695 | // There will be no data collisions, as we resolved them beforehand | ||
| 696 |
1/2✓ Branch 0 taken 57 times.
✗ Branch 1 not taken.
|
57 | if (dirty_) |
| 697 |
1/2✓ Branch 1 taken 57 times.
✗ Branch 2 not taken.
|
57 | Commit(); |
| 698 |
2/2✓ Branch 0 taken 49 times.
✓ Branch 1 taken 8 times.
|
57 | if (parent->dirty_) |
| 699 |
1/2✓ Branch 1 taken 49 times.
✗ Branch 2 not taken.
|
49 | parent->Commit(); |
| 700 |
2/4✓ Branch 1 taken 57 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 57 times.
✗ Branch 5 not taken.
|
171 | SqlCatalog sql_attach(database(), "ATTACH '" + parent->database_path() |
| 701 |
1/2✓ Branch 1 taken 57 times.
✗ Branch 2 not taken.
|
114 | + "' " |
| 702 |
1/2✓ Branch 1 taken 57 times.
✗ Branch 2 not taken.
|
57 | "AS other;"); |
| 703 |
1/2✓ Branch 1 taken 57 times.
✗ Branch 2 not taken.
|
57 | retval = sql_attach.Execute(); |
| 704 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 57 times.
|
57 | assert(retval); |
| 705 |
2/4✓ Branch 2 taken 57 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 57 times.
✗ Branch 7 not taken.
|
114 | retval = SqlCatalog(database(), "INSERT INTO other.catalog " |
| 706 | "SELECT * FROM main.catalog;") | ||
| 707 |
1/2✓ Branch 1 taken 57 times.
✗ Branch 2 not taken.
|
57 | .Execute(); |
| 708 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 57 times.
|
57 | assert(retval); |
| 709 |
2/4✓ Branch 2 taken 57 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 57 times.
✗ Branch 7 not taken.
|
114 | retval = SqlCatalog(database(), "INSERT INTO other.chunks " |
| 710 | "SELECT * FROM main.chunks;") | ||
| 711 |
1/2✓ Branch 1 taken 57 times.
✗ Branch 2 not taken.
|
57 | .Execute(); |
| 712 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 57 times.
|
57 | assert(retval); |
| 713 |
3/6✓ Branch 2 taken 57 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 57 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 57 times.
✗ Branch 10 not taken.
|
57 | retval = SqlCatalog(database(), "DETACH other;").Execute(); |
| 714 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 57 times.
|
57 | assert(retval); |
| 715 |
1/2✓ Branch 1 taken 57 times.
✗ Branch 2 not taken.
|
57 | parent->SetDirty(); |
| 716 | |||
| 717 | // Change the just copied nested catalog root to an ordinary directory | ||
| 718 | // (the nested catalog is merged into it's parent) | ||
| 719 |
1/2✓ Branch 1 taken 57 times.
✗ Branch 2 not taken.
|
57 | DirectoryEntry old_root_entry; |
| 720 |
2/4✓ Branch 1 taken 57 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 57 times.
✗ Branch 5 not taken.
|
57 | retval = parent->LookupPath(this->mountpoint(), &old_root_entry); |
| 721 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 57 times.
|
57 | assert(retval); |
| 722 | |||
| 723 |
3/6✓ Branch 1 taken 57 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 57 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 57 times.
✗ Branch 8 not taken.
|
57 | assert(old_root_entry.IsDirectory() |
| 724 | && old_root_entry.IsNestedCatalogMountpoint() | ||
| 725 | && !old_root_entry.IsNestedCatalogRoot()); | ||
| 726 | |||
| 727 | // Remove the nested catalog root mark | ||
| 728 | 57 | old_root_entry.set_is_nested_catalog_mountpoint(false); | |
| 729 |
3/6✓ Branch 1 taken 57 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 57 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 57 times.
✗ Branch 8 not taken.
|
57 | parent->UpdateEntry(old_root_entry, this->mountpoint().ToString()); |
| 730 | 57 | } | |
| 731 | |||
| 732 | |||
| 733 | /** | ||
| 734 | * Writes delta_counters_ to the database. | ||
| 735 | */ | ||
| 736 | 1809 | void WritableCatalog::UpdateCounters() { | |
| 737 | 1809 | const bool retval = delta_counters_.WriteToDatabase(database()) | |
| 738 |
2/4✓ Branch 0 taken 1809 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 1809 times.
✗ Branch 4 not taken.
|
1809 | && ReadCatalogCounters(); |
| 739 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1809 times.
|
1809 | assert(retval); |
| 740 | 1809 | } | |
| 741 | |||
| 742 | |||
| 743 | /** | ||
| 744 | * Checks if the database of this catalogs needs cleanup and defragments it | ||
| 745 | * if necessary | ||
| 746 | */ | ||
| 747 | 1230 | void WritableCatalog::VacuumDatabaseIfNecessary() { | |
| 748 | 1230 | const CatalogDatabase &db = database(); | |
| 749 | 1230 | bool needs_defragmentation = false; | |
| 750 | 1230 | double ratio = 0.0; | |
| 751 | 1230 | std::string reason; | |
| 752 | 1230 | const MutexLockGuard m(lock_); | |
| 753 | |||
| 754 |
2/4✓ Branch 1 taken 1230 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1230 times.
|
1230 | if ((ratio = db.GetFreePageRatio()) > kMaximalFreePageRatio) { |
| 755 | ✗ | needs_defragmentation = true; | |
| 756 | ✗ | reason = "free pages"; | |
| 757 |
3/4✓ Branch 1 taken 1230 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 314 times.
✓ Branch 4 taken 916 times.
|
1230 | } else if ((ratio = db.GetRowIdWasteRatio()) > kMaximalRowIdWasteRatio) { |
| 758 | 314 | needs_defragmentation = true; | |
| 759 |
1/2✓ Branch 1 taken 314 times.
✗ Branch 2 not taken.
|
314 | reason = "wasted row IDs"; |
| 760 | } | ||
| 761 | |||
| 762 |
2/2✓ Branch 0 taken 314 times.
✓ Branch 1 taken 916 times.
|
1230 | if (needs_defragmentation) { |
| 763 |
3/14✓ Branch 1 taken 209 times.
✓ Branch 2 taken 105 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 314 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
|
733 | LogCvmfs(kLogCatalog, kLogStdout | kLogNoLinebreak, |
| 764 | "Note: Catalog at %s gets defragmented (%.2f%% %s)... ", | ||
| 765 |
3/6✓ Branch 2 taken 105 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 105 times.
✓ Branch 6 taken 209 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
|
733 | (IsRoot()) ? "/" : mountpoint().c_str(), ratio * 100.0, |
| 766 | reason.c_str()); | ||
| 767 |
2/4✓ Branch 1 taken 314 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 314 times.
|
314 | if (!db.Vacuum()) { |
| 768 | ✗ | PANIC(kLogStderr, "failed (SQLite: %s)", db.GetLastErrorMsg().c_str()); | |
| 769 | } | ||
| 770 |
1/2✓ Branch 1 taken 314 times.
✗ Branch 2 not taken.
|
314 | LogCvmfs(kLogCatalog, kLogStdout, "done"); |
| 771 | } | ||
| 772 | 1230 | } | |
| 773 | |||
| 774 | } // namespace catalog | ||
| 775 |