GCC Code Coverage Report


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