GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/catalog_rw.cc
Date: 2026-01-11 02:35:46
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/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 2082 WritableCatalog::WritableCatalog(const string &path,
28 const shash::Any &catalog_hash,
29 Catalog *parent,
30 2082 const bool is_not_root)
31 4164 : Catalog(PathString(path.data(), path.length()),
32 catalog_hash, // This is 0 for a newly created catalog!
33 parent,
34 is_not_root)
35 2082 , sql_insert_(NULL)
36 2082 , sql_unlink_(NULL)
37 2082 , sql_touch_(NULL)
38 2082 , sql_update_(NULL)
39 2082 , sql_chunk_insert_(NULL)
40 2082 , sql_chunks_remove_(NULL)
41 2082 , sql_chunks_count_(NULL)
42 2082 , sql_max_link_id_(NULL)
43 2082 , sql_inc_linkcount_(NULL)
44
2/4
✓ Branch 2 taken 2082 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 2082 times.
✗ Branch 6 not taken.
6246 , dirty_(false) {
45 2082 atomic_init32(&dirty_children_);
46 2082 }
47
48
49 982 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 982 times.
✗ Branch 3 not taken.
982 parent, is_not_root);
56 982 const bool successful_init = catalog->InitStandalone(file);
57
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 982 times.
982 if (!successful_init) {
58 delete catalog;
59 return NULL;
60 }
61 982 return catalog;
62 }
63
64
65 7956 WritableCatalog::~WritableCatalog() {
66 // CAUTION HOT!
67 // (see Catalog.h - near the definition of FinalizePreparedStatements)
68 3978 FinalizePreparedStatements();
69 7956 }
70
71
72 1902 void WritableCatalog::Transaction() {
73
1/3
✓ Branch 2 taken 1902 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
1902 LogCvmfs(kLogCatalog, kLogVerboseMsg, "opening SQLite transaction for '%s'",
74 3804 mountpoint().c_str());
75 1902 const bool retval = database().BeginTransaction();
76
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1902 times.
1902 assert(retval == true);
77 1902 }
78
79
80 1842 void WritableCatalog::Commit() {
81
1/3
✓ Branch 2 taken 1842 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
1842 LogCvmfs(kLogCatalog, kLogVerboseMsg, "closing SQLite transaction for '%s'",
82 3684 mountpoint().c_str());
83 1842 const bool retval = database().CommitTransaction();
84
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1842 times.
1842 assert(retval == true);
85 1842 dirty_ = false;
86 1842 }
87
88
89 2082 void WritableCatalog::InitPreparedStatements() {
90 2082 Catalog::InitPreparedStatements(); // polymorphism: up call
91
92
2/4
✓ Branch 2 taken 2082 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 2082 times.
✗ Branch 7 not taken.
4164 const bool retval = SqlCatalog(database(), "PRAGMA foreign_keys = ON;")
93
1/2
✓ Branch 1 taken 2082 times.
✗ Branch 2 not taken.
2082 .Execute();
94
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2082 times.
2082 assert(retval);
95
1/2
✓ Branch 3 taken 2082 times.
✗ Branch 4 not taken.
2082 sql_insert_ = new SqlDirentInsert(database());
96
1/2
✓ Branch 3 taken 2082 times.
✗ Branch 4 not taken.
2082 sql_unlink_ = new SqlDirentUnlink(database());
97
1/2
✓ Branch 3 taken 2082 times.
✗ Branch 4 not taken.
2082 sql_touch_ = new SqlDirentTouch(database());
98
1/2
✓ Branch 3 taken 2082 times.
✗ Branch 4 not taken.
2082 sql_update_ = new SqlDirentUpdate(database());
99
1/2
✓ Branch 3 taken 2082 times.
✗ Branch 4 not taken.
2082 sql_chunk_insert_ = new SqlChunkInsert(database());
100
1/2
✓ Branch 3 taken 2082 times.
✗ Branch 4 not taken.
2082 sql_chunks_remove_ = new SqlChunksRemove(database());
101
1/2
✓ Branch 3 taken 2082 times.
✗ Branch 4 not taken.
2082 sql_chunks_count_ = new SqlChunksCount(database());
102
1/2
✓ Branch 3 taken 2082 times.
✗ Branch 4 not taken.
2082 sql_max_link_id_ = new SqlMaxHardlinkGroup(database());
103
1/2
✓ Branch 3 taken 2082 times.
✗ Branch 4 not taken.
2082 sql_inc_linkcount_ = new SqlIncLinkcount(database());
104 2082 }
105
106
107 1989 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 1989 times.
✗ Branch 1 not taken.
1989 delete sql_insert_;
111
1/2
✓ Branch 0 taken 1989 times.
✗ Branch 1 not taken.
1989 delete sql_unlink_;
112
1/2
✓ Branch 0 taken 1989 times.
✗ Branch 1 not taken.
1989 delete sql_touch_;
113
1/2
✓ Branch 0 taken 1989 times.
✗ Branch 1 not taken.
1989 delete sql_update_;
114
1/2
✓ Branch 0 taken 1989 times.
✗ Branch 1 not taken.
1989 delete sql_chunk_insert_;
115
1/2
✓ Branch 0 taken 1989 times.
✗ Branch 1 not taken.
1989 delete sql_chunks_remove_;
116
1/2
✓ Branch 0 taken 1989 times.
✗ Branch 1 not taken.
1989 delete sql_chunks_count_;
117
1/2
✓ Branch 0 taken 1989 times.
✗ Branch 1 not taken.
1989 delete sql_max_link_id_;
118
1/2
✓ Branch 0 taken 1989 times.
✗ Branch 1 not taken.
1989 delete sql_inc_linkcount_;
119 1989 }
120
121
122 /**
123 * Find out the maximal hardlink group id in this catalog.
124 */
125 55 uint32_t WritableCatalog::GetMaxLinkId() const {
126 55 int result = -1;
127
128
1/2
✓ Branch 1 taken 55 times.
✗ Branch 2 not taken.
55 if (sql_max_link_id_->FetchRow()) {
129 55 result = sql_max_link_id_->GetMaxGroupId();
130 }
131 55 sql_max_link_id_->Reset();
132
133 55 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 9682 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 9682 times.
✗ Branch 2 not taken.
9682 SetDirty();
148
149
1/6
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 9682 times.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
9682 LogCvmfs(kLogCatalog, kLogVerboseMsg, "add entry '%s' to '%s'",
150
1/2
✓ Branch 1 taken 9682 times.
✗ Branch 2 not taken.
19364 entry_path.c_str(), mountpoint().c_str());
151
152
1/2
✓ Branch 2 taken 9682 times.
✗ Branch 3 not taken.
9682 const shash::Md5 path_hash((shash::AsciiPtr(entry_path)));
153
1/2
✓ Branch 2 taken 9682 times.
✗ Branch 3 not taken.
9682 const shash::Md5 parent_hash((shash::AsciiPtr(parent_path)));
154
1/2
✓ Branch 1 taken 9682 times.
✗ Branch 2 not taken.
9682 DirectoryEntry effective_entry(entry);
155 9682 effective_entry.set_has_xattrs(!xattrs.IsEmpty());
156
157
1/2
✓ Branch 1 taken 9682 times.
✗ Branch 2 not taken.
9682 bool retval = sql_insert_->BindPathHash(path_hash)
158
2/4
✓ Branch 1 taken 9682 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 9682 times.
✗ Branch 4 not taken.
9682 && sql_insert_->BindParentPathHash(parent_hash)
159
3/6
✓ Branch 0 taken 9682 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 9682 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 9682 times.
✗ Branch 6 not taken.
19364 && sql_insert_->BindDirent(effective_entry);
160
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9682 times.
9682 assert(retval);
161
1/2
✓ Branch 1 taken 9682 times.
✗ Branch 2 not taken.
9682 if (xattrs.IsEmpty()) {
162
1/2
✓ Branch 1 taken 9682 times.
✗ Branch 2 not taken.
9682 retval = sql_insert_->BindXattrEmpty();
163 } else {
164 retval = sql_insert_->BindXattr(xattrs);
165 }
166
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9682 times.
9682 assert(retval);
167
1/2
✓ Branch 1 taken 9682 times.
✗ Branch 2 not taken.
9682 retval = sql_insert_->Execute();
168
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9682 times.
9682 assert(retval);
169
1/2
✓ Branch 1 taken 9682 times.
✗ Branch 2 not taken.
9682 sql_insert_->Reset();
170
171
1/2
✓ Branch 1 taken 9682 times.
✗ Branch 2 not taken.
9682 delta_counters_.Increment(effective_entry);
172 9682 }
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 1623 void WritableCatalog::RemoveEntry(const string &file_path) {
182
1/2
✓ Branch 1 taken 1623 times.
✗ Branch 2 not taken.
1623 DirectoryEntry entry;
183
2/4
✓ Branch 1 taken 1623 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1623 times.
✗ Branch 5 not taken.
1623 bool retval = LookupPath(PathString(file_path), &entry);
184
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1623 times.
1623 assert(retval);
185
186
1/2
✓ Branch 1 taken 1623 times.
✗ Branch 2 not taken.
1623 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 1623 times.
1623 if (entry.IsChunkedFile()) {
190 RemoveFileChunks(file_path);
191 }
192
193 // remove the entry itself
194
1/2
✓ Branch 2 taken 1623 times.
✗ Branch 3 not taken.
1623 const shash::Md5 path_hash = shash::Md5(shash::AsciiPtr(file_path));
195
4/9
✓ Branch 1 taken 1623 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1623 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 1623 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 1623 times.
✗ Branch 9 not taken.
1623 retval = sql_unlink_->BindPathHash(path_hash) && sql_unlink_->Execute();
196
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1623 times.
1623 assert(retval);
197
1/2
✓ Branch 1 taken 1623 times.
✗ Branch 2 not taken.
1623 sql_unlink_->Reset();
198
199
1/2
✓ Branch 1 taken 1623 times.
✗ Branch 2 not taken.
1623 delta_counters_.Decrement(entry);
200 1623 }
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 58 void WritableCatalog::TouchEntry(const DirectoryEntryBase &entry,
218 const XattrList &xattrs,
219 const shash::Md5 &path_hash) {
220
1/2
✓ Branch 1 taken 58 times.
✗ Branch 2 not taken.
58 SetDirty();
221
222
1/2
✓ Branch 1 taken 58 times.
✗ Branch 2 not taken.
58 catalog::DirectoryEntry prev_entry;
223
1/2
✓ Branch 1 taken 58 times.
✗ Branch 2 not taken.
58 bool retval = LookupMd5Path(path_hash, &prev_entry);
224
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
58 assert(retval);
225
226
1/2
✓ Branch 1 taken 58 times.
✗ Branch 2 not taken.
58 retval = sql_touch_->BindPathHash(path_hash)
227
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);
228
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
58 assert(retval);
229
1/2
✓ Branch 1 taken 58 times.
✗ Branch 2 not taken.
58 if (xattrs.IsEmpty()) {
230
1/2
✓ Branch 1 taken 58 times.
✗ Branch 2 not taken.
58 retval = sql_touch_->BindXattrEmpty();
231
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 58 times.
58 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 58 times.
58 assert(retval);
239
1/2
✓ Branch 1 taken 58 times.
✗ Branch 2 not taken.
58 retval = sql_touch_->Execute();
240
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
58 assert(retval);
241
1/2
✓ Branch 1 taken 58 times.
✗ Branch 2 not taken.
58 sql_touch_->Reset();
242 58 }
243
244
245 2677 void WritableCatalog::UpdateEntry(const DirectoryEntry &entry,
246 const shash::Md5 &path_hash) {
247 2677 SetDirty();
248
249 2677 const bool retval = sql_update_->BindPathHash(path_hash)
250
1/2
✓ Branch 1 taken 2677 times.
✗ Branch 2 not taken.
2677 && sql_update_->BindDirent(entry)
251
2/4
✓ Branch 0 taken 2677 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 2677 times.
✗ Branch 4 not taken.
5354 && sql_update_->Execute();
252
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2677 times.
2677 assert(retval);
253 2677 sql_update_->Reset();
254 2677 }
255
256 446 void WritableCatalog::AddFileChunk(const std::string &entry_path,
257 const FileChunk &chunk) {
258
1/2
✓ Branch 1 taken 446 times.
✗ Branch 2 not taken.
446 SetDirty();
259
260
1/2
✓ Branch 2 taken 446 times.
✗ Branch 3 not taken.
446 const shash::Md5 path_hash((shash::AsciiPtr(entry_path)));
261
262
1/4
✓ Branch 3 taken 446 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
446 LogCvmfs(kLogCatalog, kLogVerboseMsg,
263 "adding chunk for %s from offset %ld "
264 "and chunk size: %ld bytes",
265 446 entry_path.c_str(), chunk.offset(), chunk.offset() + chunk.size());
266
267 446 delta_counters_.self.file_chunks++;
268
269
1/2
✓ Branch 1 taken 446 times.
✗ Branch 2 not taken.
446 const bool retval = sql_chunk_insert_->BindPathHash(path_hash)
270
2/4
✓ Branch 1 taken 446 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 446 times.
✗ Branch 4 not taken.
446 && sql_chunk_insert_->BindFileChunk(chunk)
271
3/6
✓ Branch 0 taken 446 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 446 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 446 times.
✗ Branch 6 not taken.
892 && sql_chunk_insert_->Execute();
272
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 446 times.
446 assert(retval);
273
1/2
✓ Branch 1 taken 446 times.
✗ Branch 2 not taken.
446 sql_chunk_insert_->Reset();
274 446 }
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 5312 void WritableCatalog::UpdateLastModified() {
305
2/4
✓ Branch 4 taken 5312 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 5312 times.
✗ Branch 8 not taken.
5312 database().SetProperty("last_modified", static_cast<uint64_t>(time(NULL)));
306 5312 }
307
308
309 /**
310 * Increments the revision of the catalog in the database.
311 */
312 852 void WritableCatalog::IncrementRevision() { SetRevision(GetRevision() + 1); }
313
314
315 852 void WritableCatalog::SetRevision(const uint64_t new_revision) {
316
2/4
✓ Branch 3 taken 852 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 852 times.
✗ Branch 7 not taken.
852 database().SetProperty("revision", new_revision);
317 852 }
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 852 void WritableCatalog::SetPreviousRevision(const shash::Any &hash) {
339
2/4
✓ Branch 4 taken 852 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 852 times.
✗ Branch 8 not taken.
852 database().SetProperty("previous_revision", hash.ToString());
340 852 }
341
342
343 /**
344 * Moves a subtree from this catalog into a just created nested catalog.
345 */
346 432 void WritableCatalog::Partition(WritableCatalog *new_nested_catalog) {
347 // Create connection between parent and child catalogs
348
3/6
✓ Branch 1 taken 432 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 432 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 432 times.
✗ Branch 8 not taken.
432 MakeTransitionPoint(new_nested_catalog->mountpoint().ToString());
349
1/2
✓ Branch 1 taken 432 times.
✗ Branch 2 not taken.
432 new_nested_catalog->MakeNestedRoot();
350 432 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 432 vector<string> GrandChildMountpoints;
356
3/6
✓ Branch 1 taken 432 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 432 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 432 times.
✗ Branch 8 not taken.
432 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 432 times.
✗ Branch 2 not taken.
432 MoveCatalogsToNested(GrandChildMountpoints, new_nested_catalog);
363 432 }
364
365
366 432 void WritableCatalog::MakeTransitionPoint(const string &mountpoint) {
367 // Find the directory entry to edit
368
1/2
✓ Branch 1 taken 432 times.
✗ Branch 2 not taken.
432 DirectoryEntry transition_entry;
369
1/2
✓ Branch 1 taken 432 times.
✗ Branch 2 not taken.
432 const bool retval = LookupPath(
370
1/2
✓ Branch 3 taken 432 times.
✗ Branch 4 not taken.
864 PathString(mountpoint.data(), mountpoint.length()), &transition_entry);
371
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 432 times.
432 assert(retval);
372
373
2/4
✓ Branch 1 taken 432 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 432 times.
✗ Branch 5 not taken.
432 assert(transition_entry.IsDirectory()
374 && !transition_entry.IsNestedCatalogRoot());
375
376 432 transition_entry.set_is_nested_catalog_mountpoint(true);
377
1/2
✓ Branch 1 taken 432 times.
✗ Branch 2 not taken.
432 UpdateEntry(transition_entry, mountpoint);
378 432 }
379
380
381 432 void WritableCatalog::MakeNestedRoot() {
382
1/2
✓ Branch 1 taken 432 times.
✗ Branch 2 not taken.
432 DirectoryEntry root_entry;
383
2/4
✓ Branch 1 taken 432 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 432 times.
✗ Branch 5 not taken.
432 const bool retval = LookupPath(mountpoint(), &root_entry);
384
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 432 times.
432 assert(retval);
385
386
2/4
✓ Branch 1 taken 432 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 432 times.
✗ Branch 5 not taken.
432 assert(root_entry.IsDirectory() && !root_entry.IsNestedCatalogMountpoint());
387
388 432 root_entry.set_is_nested_catalog_root(true);
389
3/6
✓ Branch 1 taken 432 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 432 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 432 times.
✗ Branch 8 not taken.
432 UpdateEntry(root_entry, mountpoint().ToString());
390 432 }
391
392
393 552 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 552 DirectoryEntryList listing;
400 552 const bool resolve_magic_symlinks = false;
401
2/4
✓ Branch 1 taken 552 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 552 times.
✗ Branch 5 not taken.
552 bool retval = ListingPath(PathString(directory), &listing,
402 resolve_magic_symlinks);
403
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 552 times.
552 assert(retval);
404
405 // Go through the listing
406 552 const XattrList empty_xattrs;
407 552 for (DirectoryEntryList::const_iterator i = listing.begin(),
408 552 iEnd = listing.end();
409
2/2
✓ Branch 1 taken 1329 times.
✓ Branch 2 taken 552 times.
1881 i != iEnd;
410 1329 ++i) {
411
1/2
✓ Branch 2 taken 1329 times.
✗ Branch 3 not taken.
1329 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 1329 times.
1329 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 1329 times.
✗ Branch 3 not taken.
1329 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 1329 times.
1329 if (i->IsNestedCatalogMountpoint()) {
426 grand_child_mountpoints->push_back(full_path);
427
2/2
✓ Branch 2 taken 120 times.
✓ Branch 3 taken 1209 times.
1329 } else if (i->IsDirectory()) {
428 // Recurse deeper into the directory tree
429
2/4
✓ Branch 1 taken 120 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 120 times.
✗ Branch 5 not taken.
120 MoveToNestedRecursively(full_path, new_nested_catalog,
430 grand_child_mountpoints);
431
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 1209 times.
1209 } 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 1329 times.
✗ Branch 2 not taken.
1329 RemoveEntry(full_path);
438 1329 }
439 552 }
440
441
442 432 void WritableCatalog::MoveCatalogsToNested(
443 const vector<string> &nested_catalogs,
444 WritableCatalog *new_nested_catalog) {
445 864 for (vector<string>::const_iterator i = nested_catalogs.begin(),
446 432 iEnd = nested_catalogs.end();
447
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 432 times.
432 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 432 }
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 899 void WritableCatalog::InsertNestedCatalog(const string &mountpoint,
488 Catalog *attached_reference,
489 const shash::Any content_hash,
490 const uint64_t size) {
491 899 const string hash_string = (!content_hash.IsNull()) ? content_hash.ToString()
492
6/12
✓ Branch 0 taken 467 times.
✓ Branch 1 taken 432 times.
✓ Branch 3 taken 467 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 432 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 432 times.
✓ Branch 10 taken 467 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
899 : "";
493
494 899 SqlCatalog stmt(database(), "INSERT INTO nested_catalogs (path, sha1, size) "
495
2/4
✓ Branch 2 taken 899 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 899 times.
✗ Branch 6 not taken.
2697 "VALUES (:p, :sha1, :size);");
496
1/2
✓ Branch 1 taken 899 times.
✗ Branch 2 not taken.
899 const bool retval = stmt.BindText(1, mountpoint)
497
2/4
✓ Branch 1 taken 899 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 899 times.
✗ Branch 4 not taken.
899 && stmt.BindText(2, hash_string)
498
5/11
✓ Branch 0 taken 899 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 899 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 899 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 899 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 899 times.
✗ Branch 11 not taken.
1798 && stmt.BindInt64(3, size) && stmt.Execute();
499
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 899 times.
899 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 899 times.
899 if (attached_reference != NULL)
504 AddChild(attached_reference);
505
506
1/2
✓ Branch 1 taken 899 times.
✗ Branch 2 not taken.
899 ResetNestedCatalogCacheUnprotected();
507
508 899 delta_counters_.self.nested_catalogs++;
509 899 }
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 70 void WritableCatalog::RemoveNestedCatalog(const string &mountpoint,
540 Catalog **attached_reference) {
541
1/2
✓ Branch 1 taken 70 times.
✗ Branch 2 not taken.
70 shash::Any dummy;
542 uint64_t dummy_size;
543
2/4
✓ Branch 3 taken 70 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 70 times.
✗ Branch 7 not taken.
70 bool retval = FindNested(PathString(mountpoint.data(), mountpoint.length()),
544 &dummy, &dummy_size);
545
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 70 times.
70 assert(retval);
546
547
2/4
✓ Branch 2 taken 70 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 70 times.
✗ Branch 7 not taken.
140 SqlCatalog stmt(database(), "DELETE FROM nested_catalogs WHERE path = :p;");
548
4/9
✓ Branch 1 taken 70 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 70 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 70 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 70 times.
✗ Branch 9 not taken.
70 retval = stmt.BindText(1, mountpoint) && stmt.Execute();
549
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 70 times.
70 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 70 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 70 times.
✗ Branch 5 not taken.
70 Catalog *child = FindChild(PathString(mountpoint));
555
2/2
✓ Branch 0 taken 55 times.
✓ Branch 1 taken 15 times.
70 if (child != NULL)
556
1/2
✓ Branch 1 taken 55 times.
✗ Branch 2 not taken.
55 RemoveChild(child);
557
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 70 times.
70 if (attached_reference != NULL)
558 *attached_reference = child;
559
560
1/2
✓ Branch 1 taken 70 times.
✗ Branch 2 not taken.
70 ResetNestedCatalogCacheUnprotected();
561
562 70 delta_counters_.self.nested_catalogs--;
563 70 }
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 417 void WritableCatalog::UpdateNestedCatalog(const std::string &path,
592 const shash::Any &hash,
593 const uint64_t size,
594 const DeltaCounters &child_counters) {
595 417 const MutexLockGuard guard(lock_);
596
1/2
✓ Branch 1 taken 417 times.
✗ Branch 2 not taken.
417 SetDirty();
597
598
1/2
✓ Branch 1 taken 417 times.
✗ Branch 2 not taken.
417 child_counters.PopulateToParent(&delta_counters_);
599
600
1/2
✓ Branch 1 taken 417 times.
✗ Branch 2 not taken.
417 const string hash_str = hash.ToString();
601 const string sql = "UPDATE nested_catalogs SET sha1 = :sha1, size = :size "
602
1/2
✓ Branch 2 taken 417 times.
✗ Branch 3 not taken.
417 "WHERE path = :path;";
603
1/2
✓ Branch 2 taken 417 times.
✗ Branch 3 not taken.
417 SqlCatalog stmt(database(), sql);
604
605
3/7
✓ Branch 1 taken 417 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 417 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 417 times.
✗ Branch 7 not taken.
834 const bool retval = stmt.BindText(1, hash_str) && stmt.BindInt64(2, size)
606
5/11
✓ Branch 0 taken 417 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 417 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 417 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 417 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 417 times.
✗ Branch 11 not taken.
834 && stmt.BindText(3, path) && stmt.Execute();
607
608
1/2
✓ Branch 1 taken 417 times.
✗ Branch 2 not taken.
417 ResetNestedCatalogCacheUnprotected();
609
610
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 417 times.
417 assert(retval);
611 417 }
612
613
614 55 void WritableCatalog::MergeIntoParent() {
615
2/4
✓ Branch 1 taken 55 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 55 times.
✗ Branch 5 not taken.
55 assert(!IsRoot() && HasParent());
616 55 WritableCatalog *parent = GetWritableParent();
617
618 55 CopyToParent();
619
620 // Copy the nested catalog references
621 55 CopyCatalogsToParent();
622
623 // Fix counters in parent
624 55 delta_counters_.PopulateToParent(&parent->delta_counters_);
625 55 Counters &counters = GetWritableCounters();
626 55 counters.ApplyDelta(delta_counters_);
627 55 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 55 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 55 times.
✗ Branch 6 not taken.
55 parent->RemoveNestedCatalog(this->mountpoint().ToString(), NULL);
632 55 }
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 55 void WritableCatalog::CopyCatalogsToParent() {
648
1/2
✓ Branch 1 taken 55 times.
✗ Branch 2 not taken.
55 WritableCatalog *parent = GetWritableParent();
649
650 // Obtain a list of all nested catalog references
651
1/2
✓ Branch 1 taken 55 times.
✗ Branch 2 not taken.
55 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 110 for (NestedCatalogList::const_iterator i = nested_catalog_references.begin(),
657 55 iEnd = nested_catalog_references.end();
658
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 55 times.
55 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 55 }
666
667 55 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 55 times.
✗ Branch 2 not taken.
55 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 55 times.
✗ Branch 2 not taken.
55 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 55 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 55 times.
✗ Branch 5 not taken.
110 + StringifyInt(offset)
685
1/2
✓ Branch 1 taken 55 times.
✗ Branch 2 not taken.
55 + " WHERE hardlinks > (1 << 32);";
686
687
1/2
✓ Branch 2 taken 55 times.
✗ Branch 3 not taken.
55 SqlCatalog sql_update_link_ids(database(), update_link_ids);
688
1/2
✓ Branch 1 taken 55 times.
✗ Branch 2 not taken.
55 bool retval = sql_update_link_ids.Execute();
689
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 55 times.
55 assert(retval);
690
691 // Remove the nested catalog root.
692 // It is already present in the parent.
693
3/6
✓ Branch 1 taken 55 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 55 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 55 times.
✗ Branch 8 not taken.
55 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 55 times.
✗ Branch 1 not taken.
55 if (dirty_)
698
1/2
✓ Branch 1 taken 55 times.
✗ Branch 2 not taken.
55 Commit();
699
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 12 times.
55 if (parent->dirty_)
700
1/2
✓ Branch 1 taken 43 times.
✗ Branch 2 not taken.
43 parent->Commit();
701
2/4
✓ Branch 1 taken 55 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 55 times.
✗ Branch 5 not taken.
165 SqlCatalog sql_attach(database(), "ATTACH '" + parent->database_path()
702
1/2
✓ Branch 1 taken 55 times.
✗ Branch 2 not taken.
110 + "' "
703
1/2
✓ Branch 1 taken 55 times.
✗ Branch 2 not taken.
55 "AS other;");
704
1/2
✓ Branch 1 taken 55 times.
✗ Branch 2 not taken.
55 retval = sql_attach.Execute();
705
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 55 times.
55 assert(retval);
706
2/4
✓ Branch 2 taken 55 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 55 times.
✗ Branch 7 not taken.
110 retval = SqlCatalog(database(), "INSERT INTO other.catalog "
707 "SELECT * FROM main.catalog;")
708
1/2
✓ Branch 1 taken 55 times.
✗ Branch 2 not taken.
55 .Execute();
709
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 55 times.
55 assert(retval);
710
2/4
✓ Branch 2 taken 55 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 55 times.
✗ Branch 7 not taken.
110 retval = SqlCatalog(database(), "INSERT INTO other.chunks "
711 "SELECT * FROM main.chunks;")
712
1/2
✓ Branch 1 taken 55 times.
✗ Branch 2 not taken.
55 .Execute();
713
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 55 times.
55 assert(retval);
714
3/6
✓ Branch 2 taken 55 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 55 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 55 times.
✗ Branch 10 not taken.
55 retval = SqlCatalog(database(), "DETACH other;").Execute();
715
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 55 times.
55 assert(retval);
716
1/2
✓ Branch 1 taken 55 times.
✗ Branch 2 not taken.
55 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 55 times.
✗ Branch 2 not taken.
55 DirectoryEntry old_root_entry;
721
2/4
✓ Branch 1 taken 55 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 55 times.
✗ Branch 5 not taken.
55 retval = parent->LookupPath(this->mountpoint(), &old_root_entry);
722
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 55 times.
55 assert(retval);
723
724
3/6
✓ Branch 1 taken 55 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 55 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 55 times.
✗ Branch 8 not taken.
55 assert(old_root_entry.IsDirectory()
725 && old_root_entry.IsNestedCatalogMountpoint()
726 && !old_root_entry.IsNestedCatalogRoot());
727
728 // Remove the nested catalog root mark
729 55 old_root_entry.set_is_nested_catalog_mountpoint(false);
730
3/6
✓ Branch 1 taken 55 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 55 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 55 times.
✗ Branch 8 not taken.
55 parent->UpdateEntry(old_root_entry, this->mountpoint().ToString());
731 55 }
732
733
734 /**
735 * Writes delta_counters_ to the database.
736 */
737 1284 void WritableCatalog::UpdateCounters() {
738 1284 const bool retval = delta_counters_.WriteToDatabase(database())
739
2/4
✓ Branch 0 taken 1284 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 1284 times.
✗ Branch 4 not taken.
1284 && ReadCatalogCounters();
740
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1284 times.
1284 assert(retval);
741 1284 }
742
743
744 /**
745 * Checks if the database of this catalogs needs cleanup and defragments it
746 * if necessary
747 */
748 852 void WritableCatalog::VacuumDatabaseIfNecessary() {
749 852 const CatalogDatabase &db = database();
750 852 bool needs_defragmentation = false;
751 852 double ratio = 0.0;
752 852 std::string reason;
753 852 const MutexLockGuard m(lock_);
754
755
2/4
✓ Branch 1 taken 852 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 852 times.
852 if ((ratio = db.GetFreePageRatio()) > kMaximalFreePageRatio) {
756 needs_defragmentation = true;
757 reason = "free pages";
758
3/4
✓ Branch 1 taken 852 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 214 times.
✓ Branch 4 taken 638 times.
852 } else if ((ratio = db.GetRowIdWasteRatio()) > kMaximalRowIdWasteRatio) {
759 214 needs_defragmentation = true;
760
1/2
✓ Branch 1 taken 214 times.
✗ Branch 2 not taken.
214 reason = "wasted row IDs";
761 }
762
763
2/2
✓ Branch 0 taken 214 times.
✓ Branch 1 taken 638 times.
852 if (needs_defragmentation) {
764
3/14
✓ Branch 1 taken 208 times.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 214 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.
434 LogCvmfs(kLogCatalog, kLogStdout | kLogNoLinebreak,
765 "Note: Catalog at %s gets defragmented (%.2f%% %s)... ",
766
3/6
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 6 times.
✓ Branch 6 taken 208 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
434 (IsRoot()) ? "/" : mountpoint().c_str(), ratio * 100.0,
767 reason.c_str());
768
2/4
✓ Branch 1 taken 214 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 214 times.
214 if (!db.Vacuum()) {
769 PANIC(kLogStderr, "failed (SQLite: %s)", db.GetLastErrorMsg().c_str());
770 }
771
1/2
✓ Branch 1 taken 214 times.
✗ Branch 2 not taken.
214 LogCvmfs(kLogCatalog, kLogStdout, "done");
772 }
773 852 }
774
775 } // namespace catalog
776