GCC Code Coverage Report


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