GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/catalog.cc
Date: 2026-09-06 02:40:30
Exec Total Coverage
Lines: 407 439 92.7%
Branches: 324 606 53.5%

Line Branch Exec Source
1 /**
2 * This file is part of the CernVM File System.
3 */
4
5 #include "catalog.h"
6
7 #include <alloca.h>
8
9 #include <cassert>
10
11 #include "catalog_mgr.h"
12 #include "util/logging.h"
13 #include "util/smalloc.h"
14
15 using namespace std; // NOLINT
16
17 namespace catalog {
18
19 const shash::Md5 Catalog::kMd5PathEmpty("", 0);
20
21
22 /**
23 * Open a catalog outside the framework of a catalog manager.
24 */
25 941 Catalog *Catalog::AttachFreely(const string &imaginary_mountpoint,
26 const string &file,
27 const shash::Any &catalog_hash,
28 Catalog *parent,
29 const bool is_nested) {
30 Catalog *catalog = new Catalog(
31 941 PathString(imaginary_mountpoint.data(), imaginary_mountpoint.length()),
32 catalog_hash,
33 parent,
34
2/4
✓ Branch 1 taken 941 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 941 times.
✗ Branch 5 not taken.
941 is_nested);
35 941 const bool successful_init = catalog->InitStandalone(file);
36
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 899 times.
941 if (!successful_init) {
37
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 delete catalog;
38 42 return NULL;
39 }
40 899 return catalog;
41 }
42
43
44 6759 Catalog::Catalog(const PathString &mountpoint,
45 const shash::Any &catalog_hash,
46 Catalog *parent,
47 6759 const bool is_nested)
48 6759 : catalog_hash_(catalog_hash)
49
1/2
✓ Branch 1 taken 6759 times.
✗ Branch 2 not taken.
6759 , mountpoint_(mountpoint)
50
1/2
✓ Branch 1 taken 6759 times.
✗ Branch 2 not taken.
6759 , is_regular_mountpoint_(mountpoint_ == root_prefix_)
51 6759 , volatile_flag_(false)
52
3/4
✓ Branch 0 taken 4296 times.
✓ Branch 1 taken 2463 times.
✓ Branch 2 taken 4296 times.
✗ Branch 3 not taken.
6759 , is_root_(parent == NULL && !is_nested)
53 6759 , managed_database_(false)
54 6759 , parent_(parent)
55 6759 , nested_catalog_cache_dirty_(true)
56 6759 , voms_authz_status_(kVomsUnknown)
57
1/2
✓ Branch 8 taken 6759 times.
✗ Branch 9 not taken.
13518 , initialized_(false) {
58 6759 max_row_id_ = 0;
59 6759 inode_annotation_ = NULL;
60 6759 lock_ = reinterpret_cast<pthread_mutex_t *>(smalloc(sizeof(pthread_mutex_t)));
61 6759 const int retval = pthread_mutex_init(lock_, NULL);
62
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6759 times.
6759 assert(retval == 0);
63
64 6759 database_ = NULL;
65 6759 uid_map_ = NULL;
66 6759 gid_map_ = NULL;
67 6759 sql_listing_ = NULL;
68 6759 sql_lookup_md5path_ = NULL;
69 6759 sql_lookup_nested_ = NULL;
70 6759 sql_list_nested_ = NULL;
71 6759 sql_own_list_nested_ = NULL;
72 6759 sql_all_chunks_ = NULL;
73 6759 sql_chunks_listing_ = NULL;
74 6759 sql_lookup_xattrs_ = NULL;
75 6759 }
76
77
78 17734 Catalog::~Catalog() {
79 13424 pthread_mutex_destroy(lock_);
80 13424 free(lock_);
81 13424 FinalizePreparedStatements();
82
2/2
✓ Branch 0 taken 6378 times.
✓ Branch 1 taken 334 times.
13424 delete database_;
83 17734 }
84
85
86 /**
87 * InitPreparedStatement uses polymorphism in case of a r/w catalog.
88 * FinalizePreparedStatements is called in the destructor where
89 * polymorphism does not work any more and has to be called both in
90 * the WritableCatalog and the Catalog destructor
91 */
92 6423 void Catalog::InitPreparedStatements() {
93
1/2
✓ Branch 3 taken 6423 times.
✗ Branch 4 not taken.
6423 sql_listing_ = new SqlListing(database());
94
1/2
✓ Branch 3 taken 6423 times.
✗ Branch 4 not taken.
6423 sql_lookup_md5path_ = new SqlLookupPathHash(database());
95
1/2
✓ Branch 3 taken 6423 times.
✗ Branch 4 not taken.
6423 sql_lookup_nested_ = new SqlNestedCatalogLookup(database());
96
1/2
✓ Branch 3 taken 6423 times.
✗ Branch 4 not taken.
6423 sql_list_nested_ = new SqlNestedCatalogListing(database());
97
1/2
✓ Branch 3 taken 6423 times.
✗ Branch 4 not taken.
6423 sql_own_list_nested_ = new SqlOwnNestedCatalogListing(database());
98
1/2
✓ Branch 3 taken 6423 times.
✗ Branch 4 not taken.
6423 sql_all_chunks_ = new SqlAllChunks(database());
99
1/2
✓ Branch 3 taken 6423 times.
✗ Branch 4 not taken.
6423 sql_chunks_listing_ = new SqlChunksListing(database());
100
1/2
✓ Branch 3 taken 6423 times.
✗ Branch 4 not taken.
6423 sql_lookup_xattrs_ = new SqlLookupXattrs(database());
101 6423 }
102
103
104 6712 void Catalog::FinalizePreparedStatements() {
105
2/2
✓ Branch 0 taken 6378 times.
✓ Branch 1 taken 334 times.
6712 delete sql_lookup_xattrs_;
106
2/2
✓ Branch 0 taken 6378 times.
✓ Branch 1 taken 334 times.
6712 delete sql_chunks_listing_;
107
2/2
✓ Branch 0 taken 6378 times.
✓ Branch 1 taken 334 times.
6712 delete sql_all_chunks_;
108
2/2
✓ Branch 0 taken 6378 times.
✓ Branch 1 taken 334 times.
6712 delete sql_listing_;
109
2/2
✓ Branch 0 taken 6378 times.
✓ Branch 1 taken 334 times.
6712 delete sql_lookup_md5path_;
110
2/2
✓ Branch 0 taken 6378 times.
✓ Branch 1 taken 334 times.
6712 delete sql_lookup_nested_;
111
2/2
✓ Branch 0 taken 6378 times.
✓ Branch 1 taken 334 times.
6712 delete sql_list_nested_;
112
2/2
✓ Branch 0 taken 6378 times.
✓ Branch 1 taken 334 times.
6712 delete sql_own_list_nested_;
113 6712 }
114
115
116 2033 bool Catalog::InitStandalone(const std::string &database_file) {
117
1/2
✓ Branch 1 taken 2033 times.
✗ Branch 2 not taken.
2033 const bool retval = OpenDatabase(database_file);
118
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 1991 times.
2033 if (!retval) {
119 42 return false;
120 }
121
122 1991 InodeRange inode_range;
123 1991 inode_range.MakeDummy();
124 1991 set_inode_range(inode_range);
125 1991 return true;
126 }
127
128
129 10077 bool Catalog::ReadCatalogCounters() {
130
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10077 times.
10077 assert(database_ != NULL);
131 bool statistics_loaded;
132 10077 if (database().schema_version() < CatalogDatabase::kLatestSupportedSchema
133
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 9993 times.
10077 - CatalogDatabase::kSchemaEpsilon) {
134 84 statistics_loaded = counters_.ReadFromDatabase(database(),
135 LegacyMode::kLegacy);
136
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 9993 times.
9993 } else if (database().schema_revision() < 2) {
137 statistics_loaded = counters_.ReadFromDatabase(database(),
138 LegacyMode::kNoXattrs);
139
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 9993 times.
9993 } else if (database().schema_revision() < 3) {
140 statistics_loaded = counters_.ReadFromDatabase(database(),
141 LegacyMode::kNoExternals);
142
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 9993 times.
9993 } else if (database().schema_revision() < 5) {
143 statistics_loaded = counters_.ReadFromDatabase(database(),
144 LegacyMode::kNoSpecials);
145 } else {
146 9993 statistics_loaded = counters_.ReadFromDatabase(database());
147 }
148 10077 return statistics_loaded;
149 }
150
151
152 /**
153 * Establishes the database structures and opens the sqlite database file.
154 * @param db_path the absolute path to the database file on local file system
155 * @return true on successful initialization otherwise false
156 */
157 6507 bool Catalog::OpenDatabase(const string &db_path) {
158
2/4
✓ Branch 1 taken 6507 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 6507 times.
✗ Branch 5 not taken.
6507 database_ = CatalogDatabase::Open(db_path, DatabaseOpenMode());
159
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 6423 times.
6507 if (NULL == database_) {
160 84 return false;
161 }
162
163
2/2
✓ Branch 2 taken 84 times.
✓ Branch 3 taken 6339 times.
6423 if (database_->IsEqualSchema(database_->schema_version(), 1.0)) {
164 // Possible fix-up for database layout lacking the content hash of
165 // nested catalogs
166 SqlCatalog sql_has_nested_sha1(
167 84 database(),
168 "SELECT count(*) FROM sqlite_master "
169
2/4
✓ Branch 2 taken 84 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 84 times.
✗ Branch 6 not taken.
252 "WHERE type='table' AND name='nested_catalogs' AND sql LIKE '%sha1%';");
170
1/2
✓ Branch 1 taken 84 times.
✗ Branch 2 not taken.
84 const bool retval = sql_has_nested_sha1.FetchRow();
171
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 84 times.
84 assert(retval == true);
172
1/2
✓ Branch 1 taken 84 times.
✗ Branch 2 not taken.
84 const bool has_nested_sha1 = sql_has_nested_sha1.RetrieveInt64(0);
173
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 42 times.
84 if (!has_nested_sha1) {
174 42 database_->EnforceSchema(0.9, 0);
175 }
176 84 }
177
178
1/2
✓ Branch 1 taken 6423 times.
✗ Branch 2 not taken.
6423 InitPreparedStatements();
179
180 // Set the database file ownership if requested
181
2/2
✓ Branch 0 taken 471 times.
✓ Branch 1 taken 5952 times.
6423 if (managed_database_) {
182
1/2
✓ Branch 1 taken 471 times.
✗ Branch 2 not taken.
471 database_->TakeFileOwnership();
183 }
184
185 // Find out the maximum row id of this database file
186
2/4
✓ Branch 2 taken 6423 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 6423 times.
✗ Branch 7 not taken.
12846 SqlCatalog sql_max_row_id(database(), "SELECT MAX(rowid) FROM catalog;");
187
2/4
✓ Branch 1 taken 6423 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 6423 times.
6423 if (!sql_max_row_id.FetchRow()) {
188 LogCvmfs(kLogCatalog, kLogDebug,
189 "Cannot retrieve maximal row id for database file %s "
190 "(SqliteErrorcode: %d)",
191 db_path.c_str(), sql_max_row_id.GetLastError());
192 return false;
193 }
194
1/2
✓ Branch 1 taken 6423 times.
✗ Branch 2 not taken.
6423 max_row_id_ = sql_max_row_id.RetrieveInt64(0);
195
196 // Get root prefix
197
4/6
✓ Branch 2 taken 6423 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 6423 times.
✗ Branch 6 not taken.
✓ Branch 9 taken 2863 times.
✓ Branch 10 taken 3560 times.
6423 if (database_->HasProperty("root_prefix")) {
198 2863 const std::string root_prefix = database_->GetProperty<std::string>(
199
2/4
✓ Branch 2 taken 2863 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 2863 times.
✗ Branch 6 not taken.
5726 "root_prefix");
200
1/2
✓ Branch 3 taken 2863 times.
✗ Branch 4 not taken.
2863 root_prefix_.Assign(root_prefix.data(), root_prefix.size());
201
1/2
✓ Branch 3 taken 2863 times.
✗ Branch 4 not taken.
2863 LogCvmfs(kLogCatalog, kLogDebug,
202 "found root prefix %s in root catalog file %s",
203 root_prefix_.c_str(), db_path.c_str());
204
1/2
✓ Branch 1 taken 2863 times.
✗ Branch 2 not taken.
2863 is_regular_mountpoint_ = (root_prefix_ == mountpoint_);
205 2863 } else {
206
1/2
✓ Branch 2 taken 3560 times.
✗ Branch 3 not taken.
3560 LogCvmfs(kLogCatalog, kLogDebug, "no root prefix for root catalog file %s",
207 db_path.c_str());
208 }
209
210 // Get volatile content flag
211
2/4
✓ Branch 1 taken 6423 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 6423 times.
✗ Branch 5 not taken.
6423 volatile_flag_ = database_->GetPropertyDefault<bool>("volatile",
212 6423 volatile_flag_);
213
214 // Read Catalog Counter Statistics
215
2/4
✓ Branch 1 taken 6423 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 6423 times.
6423 if (!ReadCatalogCounters()) {
216 LogCvmfs(kLogCatalog, kLogStderr,
217 "failed to load statistics counters for catalog %s (file %s)",
218 mountpoint_.c_str(), db_path.c_str());
219 return false;
220 }
221
222
2/2
✓ Branch 1 taken 2463 times.
✓ Branch 2 taken 3960 times.
6423 if (HasParent()) {
223
1/2
✓ Branch 1 taken 2463 times.
✗ Branch 2 not taken.
2463 parent_->AddChild(this);
224 }
225
226 6423 initialized_ = true;
227 6423 return true;
228 6423 }
229
230
231 /**
232 * Removes the mountpoint and prepends the root prefix to path
233 */
234 30229 shash::Md5 Catalog::NormalizePath(const PathString &path) const {
235
2/2
✓ Branch 0 taken 29901 times.
✓ Branch 1 taken 328 times.
30229 if (is_regular_mountpoint_)
236
1/2
✓ Branch 3 taken 29901 times.
✗ Branch 4 not taken.
29901 return shash::Md5(path.GetChars(), path.GetLength());
237
238
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 328 times.
328 assert(path.GetLength() >= mountpoint_.GetLength());
239 // Piecewise hash calculation: root_prefix plus tail of path
240
1/2
✓ Branch 1 taken 328 times.
✗ Branch 2 not taken.
328 shash::Any result(shash::kMd5);
241
1/2
✓ Branch 1 taken 328 times.
✗ Branch 2 not taken.
328 shash::ContextPtr ctx(shash::kMd5);
242 328 ctx.buffer = alloca(ctx.size);
243
1/2
✓ Branch 1 taken 328 times.
✗ Branch 2 not taken.
328 shash::Init(ctx);
244
1/2
✓ Branch 2 taken 328 times.
✗ Branch 3 not taken.
656 shash::Update(
245 328 reinterpret_cast<const unsigned char *>(root_prefix_.GetChars()),
246 root_prefix_.GetLength(),
247 ctx);
248 328 shash::Update(reinterpret_cast<const unsigned char *>(path.GetChars())
249
1/2
✓ Branch 2 taken 328 times.
✗ Branch 3 not taken.
328 + mountpoint_.GetLength(),
250 328 path.GetLength() - mountpoint_.GetLength(),
251 ctx);
252
1/2
✓ Branch 1 taken 328 times.
✗ Branch 2 not taken.
328 shash::Final(ctx, &result);
253
1/2
✓ Branch 1 taken 328 times.
✗ Branch 2 not taken.
328 return result.CastToMd5();
254 }
255
256
257 /**
258 * Same as NormalizePath but returns a PathString instead of an Md5 hash.
259 */
260 2668 PathString Catalog::NormalizePath2(const PathString &path) const {
261
2/2
✓ Branch 0 taken 2500 times.
✓ Branch 1 taken 168 times.
2668 if (is_regular_mountpoint_)
262
1/2
✓ Branch 1 taken 2500 times.
✗ Branch 2 not taken.
2500 return path;
263
264
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 168 times.
168 assert(path.GetLength() >= mountpoint_.GetLength());
265
1/2
✓ Branch 1 taken 168 times.
✗ Branch 2 not taken.
168 PathString result = root_prefix_;
266
1/2
✓ Branch 2 taken 168 times.
✗ Branch 3 not taken.
168 const PathString suffix = path.Suffix(mountpoint_.GetLength());
267
1/2
✓ Branch 3 taken 168 times.
✗ Branch 4 not taken.
168 result.Append(suffix.GetChars(), suffix.GetLength());
268
1/2
✓ Branch 1 taken 168 times.
✗ Branch 2 not taken.
168 return result;
269 168 }
270
271
272 /**
273 * The opposite of NormalizePath: from a full path remove the root prefix and
274 * add the catalogs current mountpoint. Needed for normalized paths from the
275 * SQlite tables, such as nested catalog entry points.
276 */
277 2496 PathString Catalog::PlantPath(const PathString &path) const {
278
2/2
✓ Branch 0 taken 2328 times.
✓ Branch 1 taken 168 times.
2496 if (is_regular_mountpoint_)
279
1/2
✓ Branch 1 taken 2328 times.
✗ Branch 2 not taken.
2328 return path;
280
281
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 168 times.
168 assert(path.GetLength() >= root_prefix_.GetLength());
282
1/2
✓ Branch 1 taken 168 times.
✗ Branch 2 not taken.
168 PathString result = mountpoint_;
283
1/2
✓ Branch 2 taken 168 times.
✗ Branch 3 not taken.
168 const PathString suffix = path.Suffix(root_prefix_.GetLength());
284
1/2
✓ Branch 3 taken 168 times.
✗ Branch 4 not taken.
168 result.Append(suffix.GetChars(), suffix.GetLength());
285
1/2
✓ Branch 1 taken 168 times.
✗ Branch 2 not taken.
168 return result;
286 168 }
287
288
289 /**
290 * Performs a lookup on this Catalog for a given MD5 path hash.
291 * @param md5path the MD5 hash of the searched path
292 * @param expand_symlink indicates if variables in symlink should be resolved
293 * @param dirent will be set to the found DirectoryEntry
294 * @return true if DirectoryEntry was successfully found, false otherwise
295 */
296 26108 bool Catalog::LookupEntry(const shash::Md5 &md5path, const bool expand_symlink,
297 DirectoryEntry *dirent) const {
298
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 26108 times.
26108 assert(IsInitialized());
299
300 26108 const MutexLockGuard m(lock_);
301
1/2
✓ Branch 1 taken 26108 times.
✗ Branch 2 not taken.
26108 sql_lookup_md5path_->BindPathHash(md5path);
302
1/2
✓ Branch 1 taken 26108 times.
✗ Branch 2 not taken.
26108 const bool found = sql_lookup_md5path_->FetchRow();
303
4/4
✓ Branch 0 taken 25231 times.
✓ Branch 1 taken 877 times.
✓ Branch 2 taken 25191 times.
✓ Branch 3 taken 40 times.
26108 if (found && (dirent != NULL)) {
304
2/4
✓ Branch 1 taken 25191 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 25191 times.
✗ Branch 5 not taken.
25191 *dirent = sql_lookup_md5path_->GetDirent(this, expand_symlink);
305
1/2
✓ Branch 1 taken 25191 times.
✗ Branch 2 not taken.
25191 FixTransitionPoint(md5path, dirent);
306 }
307
1/2
✓ Branch 1 taken 26108 times.
✗ Branch 2 not taken.
26108 sql_lookup_md5path_->Reset();
308
309 26108 return found;
310 26108 }
311
312
313 /**
314 * Performs a lookup on this Catalog for a given MD5 path hash.
315 * @param md5path the MD5 hash of the searched path
316 * @param dirent will be set to the found DirectoryEntry
317 * @return true if DirectoryEntry was successfully found, false otherwise
318 */
319 26068 bool Catalog::LookupMd5Path(const shash::Md5 &md5path,
320 DirectoryEntry *dirent) const {
321 26068 return LookupEntry(md5path, true, dirent);
322 }
323
324
325 40 bool Catalog::LookupRawSymlink(const PathString &path,
326 LinkString *raw_symlink) const {
327
1/2
✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
40 DirectoryEntry dirent;
328
2/4
✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 40 times.
✗ Branch 5 not taken.
40 const bool result = (LookupEntry(NormalizePath(path), false, &dirent));
329
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 if (result)
330
2/4
✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 40 times.
✗ Branch 5 not taken.
40 raw_symlink->Assign(dirent.symlink());
331 40 return result;
332 40 }
333
334
335 40 bool Catalog::LookupXattrsMd5Path(const shash::Md5 &md5path,
336 XattrList *xattrs) const {
337
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 40 times.
40 assert(IsInitialized());
338
339 40 const MutexLockGuard m(lock_);
340
1/2
✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
40 sql_lookup_xattrs_->BindPathHash(md5path);
341
1/2
✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
40 const bool found = sql_lookup_xattrs_->FetchRow();
342
2/4
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 40 times.
✗ Branch 3 not taken.
40 if (found && (xattrs != NULL)) {
343
1/2
✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
40 *xattrs = sql_lookup_xattrs_->GetXattrs();
344 }
345
1/2
✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
40 sql_lookup_xattrs_->Reset();
346
347 40 return found;
348 40 }
349
350
351 /**
352 * Perform a listing of the directory with the given MD5 path hash.
353 * @param path_hash the MD5 hash of the path of the directory to list
354 * @param listing will be set to the resulting DirectoryEntryList
355 * @return true on successful listing, false otherwise
356 */
357 186 bool Catalog::ListingMd5PathStat(const shash::Md5 &md5path,
358 StatEntryList *listing) const {
359
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 186 times.
186 assert(IsInitialized());
360
361
1/2
✓ Branch 1 taken 186 times.
✗ Branch 2 not taken.
186 DirectoryEntry dirent;
362
1/2
✓ Branch 1 taken 186 times.
✗ Branch 2 not taken.
186 StatEntry entry;
363
364 186 const MutexLockGuard m(lock_);
365
1/2
✓ Branch 1 taken 186 times.
✗ Branch 2 not taken.
186 sql_listing_->BindPathHash(md5path);
366
3/4
✓ Branch 1 taken 618 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 432 times.
✓ Branch 4 taken 186 times.
618 while (sql_listing_->FetchRow()) {
367
2/4
✓ Branch 1 taken 432 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 432 times.
✗ Branch 5 not taken.
432 dirent = sql_listing_->GetDirent(this);
368
2/2
✓ Branch 1 taken 42 times.
✓ Branch 2 taken 390 times.
432 if (dirent.IsHidden())
369 42 continue;
370
1/2
✓ Branch 1 taken 390 times.
✗ Branch 2 not taken.
390 FixTransitionPoint(md5path, &dirent);
371
2/4
✓ Branch 1 taken 390 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 390 times.
✗ Branch 5 not taken.
390 entry.name = dirent.name();
372
1/2
✓ Branch 1 taken 390 times.
✗ Branch 2 not taken.
390 entry.info = dirent.GetStatStructure();
373
1/2
✓ Branch 1 taken 390 times.
✗ Branch 2 not taken.
390 listing->PushBack(entry);
374 }
375
1/2
✓ Branch 1 taken 186 times.
✗ Branch 2 not taken.
186 sql_listing_->Reset();
376
377 186 return true;
378 186 }
379
380
381 /**
382 * Perform a listing of the directory with the given MD5 path hash.
383 * Returns only struct stat values
384 * @param path_hash the MD5 hash of the path of the directory to list
385 * @param listing will be set to the resulting DirectoryEntryList
386 * @param expand_symlink defines if magic symlinks should be resolved
387 * @return true on successful listing, false otherwise
388 */
389 4796 bool Catalog::ListingMd5Path(const shash::Md5 &md5path,
390 DirectoryEntryList *listing,
391 const bool expand_symlink) const {
392
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4796 times.
4796 assert(IsInitialized());
393
394 4796 const MutexLockGuard m(lock_);
395
396
1/2
✓ Branch 1 taken 4796 times.
✗ Branch 2 not taken.
4796 sql_listing_->BindPathHash(md5path);
397
3/4
✓ Branch 1 taken 12694 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 7898 times.
✓ Branch 4 taken 4796 times.
12694 while (sql_listing_->FetchRow()) {
398
1/2
✓ Branch 1 taken 7898 times.
✗ Branch 2 not taken.
7898 DirectoryEntry dirent = sql_listing_->GetDirent(this, expand_symlink);
399
1/2
✓ Branch 1 taken 7898 times.
✗ Branch 2 not taken.
7898 FixTransitionPoint(md5path, &dirent);
400
1/2
✓ Branch 1 taken 7898 times.
✗ Branch 2 not taken.
7898 listing->push_back(dirent);
401 7898 }
402
1/2
✓ Branch 1 taken 4796 times.
✗ Branch 2 not taken.
4796 sql_listing_->Reset();
403
404 4796 return true;
405 4796 }
406
407
408 84 bool Catalog::AllChunksBegin() { return sql_all_chunks_->Open(); }
409
410
411 420 bool Catalog::AllChunksNext(shash::Any *hash,
412 zlib::Algorithms *compression_alg) {
413 420 return sql_all_chunks_->Next(hash, compression_alg);
414 }
415
416
417 84 bool Catalog::AllChunksEnd() { return sql_all_chunks_->Close(); }
418
419
420 /**
421 * Hash algorithm is given by the unchunked file.
422 * Could be figured out by a join but it is faster if the user of this
423 * method tells us.
424 */
425 84 bool Catalog::ListMd5PathChunks(const shash::Md5 &md5path,
426 const shash::Algorithms interpret_hashes_as,
427 FileChunkList *chunks) const {
428
2/4
✓ Branch 1 taken 84 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 84 times.
✗ Branch 5 not taken.
84 assert(IsInitialized() && chunks->IsEmpty());
429
430 84 const MutexLockGuard m(lock_);
431
432
1/2
✓ Branch 1 taken 84 times.
✗ Branch 2 not taken.
84 sql_chunks_listing_->BindPathHash(md5path);
433
3/4
✓ Branch 1 taken 168 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 84 times.
✓ Branch 4 taken 84 times.
168 while (sql_chunks_listing_->FetchRow()) {
434
2/4
✓ Branch 1 taken 84 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 84 times.
✗ Branch 5 not taken.
84 chunks->PushBack(sql_chunks_listing_->GetFileChunk(interpret_hashes_as));
435 }
436
1/2
✓ Branch 1 taken 84 times.
✗ Branch 2 not taken.
84 sql_chunks_listing_->Reset();
437
438 84 return true;
439 84 }
440
441
442 /**
443 * Only used by the garbage collection
444 */
445 84 const Catalog::HashVector &Catalog::GetReferencedObjects() const {
446
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 84 times.
84 if (!referenced_hashes_.empty()) {
447 return referenced_hashes_;
448 }
449
450 // retrieve all referenced content hashes of both files and file chunks
451
1/2
✓ Branch 2 taken 84 times.
✗ Branch 3 not taken.
84 SqlListContentHashes list_content_hashes(database());
452
3/4
✓ Branch 1 taken 630 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 546 times.
✓ Branch 4 taken 84 times.
630 while (list_content_hashes.FetchRow()) {
453
2/4
✓ Branch 1 taken 546 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 546 times.
✗ Branch 5 not taken.
546 referenced_hashes_.push_back(list_content_hashes.GetHash());
454 }
455
456 84 return referenced_hashes_;
457 84 }
458
459
460 4378 void Catalog::TakeDatabaseFileOwnership() {
461 4378 managed_database_ = true;
462
2/2
✓ Branch 0 taken 3907 times.
✓ Branch 1 taken 471 times.
4378 if (NULL != database_) {
463 3907 database_->TakeFileOwnership();
464 }
465 4378 }
466
467
468 42 void Catalog::DropDatabaseFileOwnership() {
469 42 managed_database_ = false;
470
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 if (NULL != database_) {
471 42 database_->DropFileOwnership();
472 }
473 42 }
474
475
476 1158 uint64_t Catalog::GetTTL() const {
477 1158 const MutexLockGuard m(lock_);
478
2/4
✓ Branch 3 taken 1158 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 1158 times.
✗ Branch 7 not taken.
2316 return database().GetPropertyDefault<uint64_t>("TTL", kDefaultTTL);
479 1158 }
480
481
482 bool Catalog::HasExplicitTTL() const {
483 const MutexLockGuard m(lock_);
484 return database().HasProperty("TTL");
485 }
486
487
488 2473 bool Catalog::GetVOMSAuthz(string *authz) const {
489 bool result;
490 2473 const MutexLockGuard m(lock_);
491
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2473 times.
2473 if (voms_authz_status_ == kVomsPresent) {
492 if (authz) {
493 *authz = voms_authz_;
494 }
495 result = true;
496
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2473 times.
2473 } else if (voms_authz_status_ == kVomsNone) {
497 result = false;
498 } else {
499
3/6
✓ Branch 3 taken 2473 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 2473 times.
✗ Branch 7 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 2473 times.
2473 if (database().HasProperty("voms_authz")) {
500 voms_authz_ = database().GetProperty<string>("voms_authz");
501 if (authz) {
502 *authz = voms_authz_;
503 }
504 voms_authz_status_ = kVomsPresent;
505 } else {
506 2473 voms_authz_status_ = kVomsNone;
507 }
508 2473 result = (voms_authz_status_ == kVomsPresent);
509 }
510 2473 return result;
511 2473 }
512
513 6094 uint64_t Catalog::GetRevision() const {
514 6094 const MutexLockGuard m(lock_);
515
2/4
✓ Branch 3 taken 6094 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 6094 times.
✗ Branch 7 not taken.
12188 return database().GetPropertyDefault<uint64_t>("revision", 0);
516 6094 }
517
518 2557 uint64_t Catalog::GetLastModified() const {
519
1/2
✓ Branch 2 taken 2557 times.
✗ Branch 3 not taken.
2557 const std::string prop_name = "last_modified";
520
1/2
✓ Branch 2 taken 2557 times.
✗ Branch 3 not taken.
2557 return (database().HasProperty(prop_name))
521
2/4
✓ Branch 0 taken 2557 times.
✗ Branch 1 not taken.
✓ Branch 4 taken 2557 times.
✗ Branch 5 not taken.
2557 ? database().GetProperty<int>(prop_name)
522 2557 : 0u;
523 2557 }
524
525
526 uint64_t Catalog::GetNumChunks() const {
527 return counters_.Get("self_regular") + counters_.Get("self_chunks");
528 }
529
530
531 84 uint64_t Catalog::GetNumEntries() const {
532
1/2
✓ Branch 2 taken 84 times.
✗ Branch 3 not taken.
84 const string sql = "SELECT count(*) FROM catalog;";
533
534 84 const MutexLockGuard m(lock_);
535
1/2
✓ Branch 2 taken 84 times.
✗ Branch 3 not taken.
84 SqlCatalog stmt(database(), sql);
536
3/6
✓ Branch 1 taken 84 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 84 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 84 times.
✗ Branch 7 not taken.
168 return (stmt.FetchRow()) ? stmt.RetrieveInt64(0) : 0;
537 84 }
538
539
540 84 shash::Any Catalog::GetPreviousRevision() const {
541 84 const MutexLockGuard m(lock_);
542 84 const std::string hash_string = database().GetPropertyDefault<std::string>(
543
3/6
✓ Branch 2 taken 84 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 84 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 84 times.
✗ Branch 10 not taken.
168 "previous_revision", "");
544 84 return (!hash_string.empty())
545
1/2
✓ Branch 2 taken 42 times.
✗ Branch 3 not taken.
42 ? shash::MkFromHexPtr(shash::HexPtr(hash_string),
546 shash::kSuffixCatalog)
547
3/4
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 42 times.
✓ Branch 3 taken 42 times.
✗ Branch 4 not taken.
168 : shash::Any();
548 84 }
549
550
551 42 string Catalog::PrintMemStatistics() const {
552 42 sqlite::MemStatistics stats;
553 {
554 42 const MutexLockGuard m(lock_);
555
1/2
✓ Branch 2 taken 42 times.
✗ Branch 3 not taken.
42 database().GetMemStatistics(&stats);
556 42 }
557
4/8
✓ Branch 2 taken 42 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 42 times.
✗ Branch 7 not taken.
✓ Branch 10 taken 42 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 42 times.
✗ Branch 14 not taken.
84 return string(mountpoint().GetChars(), mountpoint().GetLength()) + ": "
558
3/6
✓ Branch 1 taken 42 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 42 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 42 times.
✗ Branch 8 not taken.
168 + StringifyInt(stats.lookaside_slots_used) + " / "
559
3/6
✓ Branch 1 taken 42 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 42 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 42 times.
✗ Branch 8 not taken.
168 + StringifyInt(stats.lookaside_slots_max) + " slots -- "
560
3/6
✓ Branch 1 taken 42 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 42 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 42 times.
✗ Branch 8 not taken.
168 + StringifyInt(stats.lookaside_hit) + " hits, "
561
3/6
✓ Branch 1 taken 42 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 42 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 42 times.
✗ Branch 8 not taken.
168 + StringifyInt(stats.lookaside_miss_size) + " misses-size, "
562
3/6
✓ Branch 1 taken 42 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 42 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 42 times.
✗ Branch 8 not taken.
168 + StringifyInt(stats.lookaside_miss_full) + " misses-full -- "
563
3/6
✓ Branch 1 taken 42 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 42 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 42 times.
✗ Branch 8 not taken.
168 + StringifyInt(stats.page_cache_used / 1024) + " kB pages -- "
564
3/6
✓ Branch 1 taken 42 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 42 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 42 times.
✗ Branch 8 not taken.
168 + StringifyInt(stats.page_cache_hit) + " hits, "
565
3/6
✓ Branch 1 taken 42 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 42 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 42 times.
✗ Branch 8 not taken.
168 + StringifyInt(stats.page_cache_miss) + " misses -- "
566
3/6
✓ Branch 1 taken 42 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 42 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 42 times.
✗ Branch 8 not taken.
168 + StringifyInt(stats.schema_used / 1024) + " kB schema -- "
567
3/6
✓ Branch 1 taken 42 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 42 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 42 times.
✗ Branch 8 not taken.
168 + StringifyInt(stats.stmt_used / 1024) + " kB statements";
568 }
569
570
571 /**
572 * Determine the actual inode of a DirectoryEntry.
573 * The first used entry from a hardlink group deterimines the inode of the
574 * others.
575 * @param row_id the row id of a read row in the sqlite database
576 * @param hardlink_group the id of a possibly present hardlink group
577 * @return the assigned inode number
578 */
579 33521 inode_t Catalog::GetMangledInode(const uint64_t row_id,
580 const uint64_t hardlink_group) const {
581
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 33521 times.
33521 assert(IsInitialized());
582
583
2/2
✓ Branch 1 taken 1244 times.
✓ Branch 2 taken 32277 times.
33521 if (inode_range_.IsDummy()) {
584 1244 return DirectoryEntry::kInvalidInode;
585 }
586
587 32277 inode_t inode = row_id + inode_range_.offset;
588
589 // Hardlinks are encoded in catalog-wide unique hard link group ids.
590 // These ids must be resolved to actual inode relationships at runtime.
591
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32277 times.
32277 if (hardlink_group > 0) {
592 const HardlinkGroupMap::const_iterator inode_iter = hardlink_groups_.find(
593 hardlink_group);
594
595 // Use cached entry if possible
596 if (inode_iter == hardlink_groups_.end()) {
597 hardlink_groups_[hardlink_group] = inode;
598 } else {
599 inode = inode_iter->second;
600 }
601 }
602
603
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32277 times.
32277 if (inode_annotation_) {
604 inode = inode_annotation_->Annotate(inode);
605 }
606
607 32277 return inode;
608 }
609
610
611 /**
612 * Get a list of all registered nested catalogs and bind mountpoints in this
613 * catalog.
614 * @return a list of all nested catalog and bind mountpoints.
615 */
616 18652 const Catalog::NestedCatalogList &Catalog::ListNestedCatalogs() const {
617 18652 const MutexLockGuard m(lock_);
618
619
2/2
✓ Branch 0 taken 4494 times.
✓ Branch 1 taken 14158 times.
18652 if (nested_catalog_cache_dirty_) {
620
1/2
✓ Branch 2 taken 4494 times.
✗ Branch 3 not taken.
4494 LogCvmfs(kLogCatalog, kLogDebug, "refreshing nested catalog cache of '%s'",
621
1/2
✓ Branch 1 taken 4494 times.
✗ Branch 2 not taken.
8988 mountpoint().c_str());
622
3/4
✓ Branch 1 taken 6576 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2082 times.
✓ Branch 4 taken 4494 times.
6576 while (sql_list_nested_->FetchRow()) {
623
1/2
✓ Branch 1 taken 2082 times.
✗ Branch 2 not taken.
2082 NestedCatalog nested;
624
3/6
✓ Branch 1 taken 2082 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2082 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 2082 times.
✗ Branch 8 not taken.
2082 nested.mountpoint = PlantPath(sql_list_nested_->GetPath());
625
1/2
✓ Branch 1 taken 2082 times.
✗ Branch 2 not taken.
2082 nested.hash = sql_list_nested_->GetContentHash();
626
1/2
✓ Branch 1 taken 2082 times.
✗ Branch 2 not taken.
2082 nested.size = sql_list_nested_->GetSize();
627
1/2
✓ Branch 1 taken 2082 times.
✗ Branch 2 not taken.
2082 nested_catalog_cache_.push_back(nested);
628 2082 }
629
1/2
✓ Branch 1 taken 4494 times.
✗ Branch 2 not taken.
4494 sql_list_nested_->Reset();
630 4494 nested_catalog_cache_dirty_ = false;
631 }
632
633 18652 return nested_catalog_cache_;
634 18652 }
635
636
637 /**
638 * Get a list of all registered nested catalogs without bind mountpoints. Used
639 * for replication and garbage collection.
640 * @return a list of all nested catalogs.
641 */
642 1634 const Catalog::NestedCatalogList Catalog::ListOwnNestedCatalogs() const {
643 1634 NestedCatalogList result;
644
645 1634 const MutexLockGuard m(lock_);
646
647
3/4
✓ Branch 1 taken 1796 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 162 times.
✓ Branch 4 taken 1634 times.
1796 while (sql_own_list_nested_->FetchRow()) {
648
1/2
✓ Branch 1 taken 162 times.
✗ Branch 2 not taken.
162 NestedCatalog nested;
649
3/6
✓ Branch 1 taken 162 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 162 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 162 times.
✗ Branch 8 not taken.
162 nested.mountpoint = PlantPath(sql_own_list_nested_->GetPath());
650
1/2
✓ Branch 1 taken 162 times.
✗ Branch 2 not taken.
162 nested.hash = sql_own_list_nested_->GetContentHash();
651
1/2
✓ Branch 1 taken 162 times.
✗ Branch 2 not taken.
162 nested.size = sql_own_list_nested_->GetSize();
652
1/2
✓ Branch 1 taken 162 times.
✗ Branch 2 not taken.
162 result.push_back(nested);
653 162 }
654
1/2
✓ Branch 1 taken 1634 times.
✗ Branch 2 not taken.
1634 sql_own_list_nested_->Reset();
655
656 3268 return result;
657 1634 }
658
659
660 /**
661 * Drops the nested catalog cache. Usually this is only useful in subclasses
662 * that implement writable catalogs.
663 *
664 * Note: this action is _not_ secured by the catalog's mutex. If serialisation
665 * is required the subclass needs to ensure that.
666 */
667 3483 void Catalog::ResetNestedCatalogCacheUnprotected() {
668 3483 nested_catalog_cache_.clear();
669 3483 nested_catalog_cache_dirty_ = true;
670 3483 }
671
672
673 /**
674 * Looks for a specific registered nested catalog based on a path.
675 */
676 2416 bool Catalog::FindNested(const PathString &mountpoint, shash::Any *hash,
677 uint64_t *size) const {
678 2416 const MutexLockGuard m(lock_);
679
1/2
✓ Branch 1 taken 2416 times.
✗ Branch 2 not taken.
2416 const PathString normalized_mountpoint = NormalizePath2(mountpoint);
680
1/2
✓ Branch 1 taken 2416 times.
✗ Branch 2 not taken.
2416 sql_lookup_nested_->BindSearchPath(normalized_mountpoint);
681
1/2
✓ Branch 1 taken 2416 times.
✗ Branch 2 not taken.
2416 const bool found = sql_lookup_nested_->FetchRow();
682
3/4
✓ Branch 0 taken 2334 times.
✓ Branch 1 taken 82 times.
✓ Branch 2 taken 2334 times.
✗ Branch 3 not taken.
2416 if (found && (hash != NULL)) {
683
1/2
✓ Branch 1 taken 2334 times.
✗ Branch 2 not taken.
2334 *hash = sql_lookup_nested_->GetContentHash();
684
1/2
✓ Branch 1 taken 2334 times.
✗ Branch 2 not taken.
2334 *size = sql_lookup_nested_->GetSize();
685 }
686
1/2
✓ Branch 1 taken 2416 times.
✗ Branch 2 not taken.
2416 sql_lookup_nested_->Reset();
687
688 2416 return found;
689 2416 }
690
691
692 /**
693 * Sets a new object to do inode annotations (or set to NULL)
694 * The annotation object is not owned by the catalog.
695 */
696 4390 void Catalog::SetInodeAnnotation(InodeAnnotation *new_annotation) {
697 4390 const MutexLockGuard m(lock_);
698 // Since annotated inodes could come back to the catalog in order to
699 // get stripped, exchanging the annotation is not allowed
700
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4390 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
4390 assert((inode_annotation_ == NULL) || (inode_annotation_ == new_annotation));
701 4390 inode_annotation_ = new_annotation;
702 4390 }
703
704
705 4390 void Catalog::SetOwnerMaps(const OwnerMap *uid_map, const OwnerMap *gid_map) {
706
2/4
✓ Branch 0 taken 4390 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 4390 times.
4390 uid_map_ = (uid_map && uid_map->HasEffect()) ? uid_map : NULL;
707
2/4
✓ Branch 0 taken 4390 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 4390 times.
4390 gid_map_ = (gid_map && gid_map->HasEffect()) ? gid_map : NULL;
708 4390 }
709
710
711 /**
712 * Add a Catalog as child to this Catalog.
713 * @param child the Catalog to define as child
714 */
715 2463 void Catalog::AddChild(Catalog *child) {
716
3/7
✓ Branch 1 taken 2463 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 2463 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 2463 times.
2463 assert(NULL == FindChild(child->mountpoint()));
717
718 2463 const MutexLockGuard m(lock_);
719
2/4
✓ Branch 1 taken 2463 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2463 times.
✗ Branch 5 not taken.
2463 children_[child->mountpoint()] = child;
720 2463 child->set_parent(this);
721 2463 }
722
723
724 /**
725 * Removes a Catalog from the children list of this Catalog
726 * @param child the Catalog to delete as child
727 */
728 1957 void Catalog::RemoveChild(Catalog *child) {
729
3/7
✓ Branch 1 taken 1957 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1957 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 1957 times.
1957 assert(NULL != FindChild(child->mountpoint()));
730
731 1957 const MutexLockGuard m(lock_);
732 1957 child->set_parent(NULL);
733
2/4
✓ Branch 1 taken 1957 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1957 times.
✗ Branch 5 not taken.
1957 children_.erase(child->mountpoint());
734 1957 }
735
736
737 6833 CatalogList Catalog::GetChildren() const {
738 6833 CatalogList result;
739
740 6833 const MutexLockGuard m(lock_);
741 13666 for (NestedCatalogMap::const_iterator i = children_.begin(),
742 6833 iEnd = children_.end();
743
2/2
✓ Branch 1 taken 3005 times.
✓ Branch 2 taken 6833 times.
9838 i != iEnd;
744 3005 ++i) {
745
1/2
✓ Branch 2 taken 3005 times.
✗ Branch 3 not taken.
3005 result.push_back(i->second);
746 }
747
748 13666 return result;
749 6833 }
750
751
752 /**
753 * Find the nested catalog that serves the given path.
754 * It might be possible that the path is in fact served by a child of the found
755 * nested catalog.
756 * @param path the path to find a best fitting catalog for
757 * @return a pointer to the best fitting child or NULL if it does not fit at all
758 */
759 19968 Catalog *Catalog::FindSubtree(const PathString &path) const {
760 // Check if this catalog fits the beginning of the path.
761
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 19968 times.
19968 if (!path.StartsWith(mountpoint_))
762 return NULL;
763
764
1/2
✓ Branch 2 taken 19968 times.
✗ Branch 3 not taken.
19968 PathString remaining(path.Suffix(mountpoint_.GetLength()));
765
1/2
✓ Branch 1 taken 19968 times.
✗ Branch 2 not taken.
19968 remaining.Append("/", 1);
766
767 // now we recombine the path elements successively
768 // in order to find a child which serves a part of the path
769
1/2
✓ Branch 1 taken 19968 times.
✗ Branch 2 not taken.
19968 PathString path_prefix(mountpoint_);
770 19968 Catalog *result = NULL;
771 // Skip the first '/'
772
1/2
✓ Branch 1 taken 19968 times.
✗ Branch 2 not taken.
19968 path_prefix.Append("/", 1);
773 19968 const char *c = remaining.GetChars() + 1;
774
2/2
✓ Branch 1 taken 185270 times.
✓ Branch 2 taken 16158 times.
201428 for (unsigned i = 1; i < remaining.GetLength(); ++i, ++c) {
775
2/2
✓ Branch 0 taken 41316 times.
✓ Branch 1 taken 143954 times.
185270 if (*c == '/') {
776
1/2
✓ Branch 1 taken 41316 times.
✗ Branch 2 not taken.
41316 result = FindChild(path_prefix);
777
778 // If we found a child serving a part of the path we can stop searching.
779 // Remaining sub path elements are possibly served by a grand child.
780
2/2
✓ Branch 0 taken 3810 times.
✓ Branch 1 taken 37506 times.
41316 if (result != NULL)
781 3810 break;
782 }
783
1/2
✓ Branch 1 taken 181460 times.
✗ Branch 2 not taken.
181460 path_prefix.Append(c, 1);
784 }
785
786 19968 return result;
787 19968 }
788
789
790 /**
791 * Looks for a child catalog, which is a subset of all registered nested
792 * catalogs.
793 */
794 46257 Catalog *Catalog::FindChild(const PathString &mountpoint) const {
795 46257 NestedCatalogMap::const_iterator nested_iter;
796
797 46257 const MutexLockGuard m(lock_);
798
1/2
✓ Branch 1 taken 46257 times.
✗ Branch 2 not taken.
46257 nested_iter = children_.find(mountpoint);
799
2/2
✓ Branch 2 taken 40289 times.
✓ Branch 3 taken 5968 times.
52225 Catalog *result = (nested_iter == children_.end()) ? NULL
800 5968 : nested_iter->second;
801
802 46257 return result;
803 46257 }
804
805
806 /**
807 * For the transition points for nested catalogs and bind mountpoints, the inode
808 * is ambiguous. It has to be set to the parent inode because nested catalogs
809 * are lazily loaded.
810 * @param md5path the MD5 hash of the entry to check
811 * @param dirent the DirectoryEntry to perform coherence fixes on
812 */
813 33479 void Catalog::FixTransitionPoint(const shash::Md5 &md5path,
814 DirectoryEntry *dirent) const {
815
2/2
✓ Branch 1 taken 25291 times.
✓ Branch 2 taken 8188 times.
33479 if (!HasParent())
816 25291 return;
817
818
2/2
✓ Branch 1 taken 1078 times.
✓ Branch 2 taken 7110 times.
8188 if (dirent->IsNestedCatalogRoot()) {
819 // Normal nested catalog
820
1/2
✓ Branch 1 taken 1078 times.
✗ Branch 2 not taken.
1078 DirectoryEntry parent_dirent;
821
1/2
✓ Branch 1 taken 1078 times.
✗ Branch 2 not taken.
1078 const bool retval = parent_->LookupMd5Path(md5path, &parent_dirent);
822
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1078 times.
1078 assert(retval);
823 1078 dirent->set_inode(parent_dirent.inode());
824
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 7110 times.
8188 } else if (md5path == kMd5PathEmpty) {
825 // Bind mountpoint
826 DirectoryEntry parent_dirent;
827 const bool retval = parent_->LookupPath(mountpoint_, &parent_dirent);
828 assert(retval);
829 dirent->set_inode(parent_dirent.inode());
830 }
831 }
832
833 } // namespace catalog
834