GCC Code Coverage Report


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