GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/catalog_sql.h
Date: 2026-06-28 02:36:10
Exec Total Coverage
Lines: 19 21 90.5%
Branches: 11 18 61.1%

Line Branch Exec Source
1 /**
2 * This file is part of the CernVM file system.
3 *
4 * This file provides classes to wrap often used catalog SQL statements.
5 * In particular, it wraps around sqlite3 prepared statement syntax.
6 *
7 * Usage example:
8 * SqlLookup statement(<database>);
9 * statement.BindPathHash(<hash>);
10 * if (statement.FetchRow()) {
11 * statement.GetDirectoryEntry(<catalog>);
12 * }
13 * statement.Reset();
14 */
15
16 #ifndef CVMFS_CATALOG_SQL_H_
17 #define CVMFS_CATALOG_SQL_H_
18
19 #include <inttypes.h>
20
21 #include <string>
22
23 #include "compression/compression.h"
24 #include "crypto/hash.h"
25 #include "directory_entry.h"
26 #include "file_chunk.h"
27 #include "shortstring.h"
28 #include "sql.h"
29
30 class XattrList;
31
32 namespace catalog {
33
34 // Set to false via CVMFS_NO_IGNORE_LEGACY_BULKHASHES env var to restore the
35 // pre-2.14 behavior of including bulk hashes for chunked files.
36 extern bool g_ignore_legacy_bulk_hashes;
37
38 class Catalog;
39
40
41 class CatalogDatabase : public sqlite::Database<CatalogDatabase> {
42 public:
43 static const float kLatestSchema;
44 static const float kLatestSupportedSchema; // + 1.X catalogs (r/o)
45 // Backwards-compatible schema changes
46 static const unsigned kLatestSchemaRevision;
47
48 bool CreateEmptyDatabase();
49 bool InsertInitialValues(
50 const std::string &root_path,
51 const bool volatile_content,
52 const std::string &voms_authz,
53 const DirectoryEntry &root_entry = DirectoryEntry(kDirentNegative));
54
55 bool CheckSchemaCompatibility();
56 bool LiveSchemaUpgradeIfNecessary();
57 bool CompactDatabase() const;
58
59 double GetRowIdWasteRatio() const;
60 bool SetVOMSAuthz(const std::string &);
61
62 protected:
63 // TODO(rmeusel): C++11 - constructor inheritance
64 friend class sqlite::Database<CatalogDatabase>;
65 7797 CatalogDatabase(const std::string &filename, const OpenMode open_mode)
66 7797 : sqlite::Database<CatalogDatabase>(filename, open_mode) { }
67 };
68
69
70 //------------------------------------------------------------------------------
71
72
73 /**
74 * Base class for all SQL statement classes. It wraps a single SQL statement
75 * and all necessary calls of the sqlite3 API to deal with this statement.
76 */
77 class SqlCatalog : public sqlite::Sql {
78 public:
79 /**
80 * Basic constructor to use this class for a specific statement.
81 * @param database the database to use the query on
82 * @param statement the statement to prepare
83 */
84 35184 SqlCatalog(const CatalogDatabase &database, const std::string &statement) {
85
2/4
✓ Branch 1 taken 35184 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 35184 times.
✗ Branch 5 not taken.
35184 Init(database.sqlite_db(), statement);
86 35184 }
87
88 /**
89 * Wrapper for retrieving MD5-ified path names.
90 * @param idx_high offset of most significant bits in database query
91 * @param idx_low offset of least significant bits in database query
92 * @result the retrieved MD5 hash
93 */
94 inline shash::Md5 RetrieveMd5(const int idx_high, const int idx_low) const {
95 return shash::Md5(RetrieveInt64(idx_high), RetrieveInt64(idx_low));
96 }
97
98 /**
99 * Wrapper for retrieving a cryptographic hash from a blob field.
100 */
101 33499 inline shash::Any RetrieveHashBlob(
102 const int idx_column,
103 const shash::Algorithms hash_algo,
104 const char hash_suffix = shash::kSuffixNone) const {
105 // Note: SQLite documentation advises to first define the data type of BLOB
106 // by calling sqlite3_column_XXX() on the column and _afterwards_ get
107 // the number of bytes using sqlite3_column_bytes().
108 //
109 // See: https://www.sqlite.org/c3ref/column_blob.html
110 const unsigned char *buffer = static_cast<const unsigned char *>(
111 33499 RetrieveBlob(idx_column));
112 33499 const int byte_count = RetrieveBytes(idx_column);
113 return (byte_count > 0) ? shash::Any(hash_algo, buffer, hash_suffix)
114
2/2
✓ Branch 0 taken 10051 times.
✓ Branch 1 taken 23448 times.
33499 : shash::Any(hash_algo);
115 }
116
117 /**
118 * Wrapper for retrieving a cryptographic hash from a text field.
119 */
120 inline shash::Any RetrieveHashHex(
121 const int idx_column, const char hash_suffix = shash::kSuffixNone) const {
122 const std::string hash_string = std::string(
123 reinterpret_cast<const char *>(RetrieveText(idx_column)));
124 return shash::MkFromHexPtr(shash::HexPtr(hash_string), hash_suffix);
125 }
126
127 /**
128 * Wrapper for binding a MD5-ified path name.
129 * @param idx_high offset of most significant bits in database query
130 * @param idx_low offset of least significant bits in database query
131 * @param hash the hash to bind in the query
132 * @result true on success, false otherwise
133 */
134 86017 inline bool BindMd5(const int idx_high, const int idx_low,
135 const shash::Md5 &hash) {
136 uint64_t high, low;
137
1/2
✓ Branch 1 taken 86017 times.
✗ Branch 2 not taken.
86017 hash.ToIntPair(&high, &low);
138
1/2
✓ Branch 1 taken 86017 times.
✗ Branch 2 not taken.
86017 const bool retval = BindInt64(idx_high, static_cast<int64_t>(high))
139
3/6
✓ Branch 0 taken 86017 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 86017 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 86017 times.
✗ Branch 6 not taken.
86017 && BindInt64(idx_low, static_cast<int64_t>(low));
140 86017 return retval;
141 }
142
143 /**
144 * Wrapper for binding a cryptographic hash. Algorithm of hash has to be
145 * elsewhere (in flags).
146 * @param idx_column offset of the blob field in database query
147 * @param hash the hash to bind in the query
148 * @result true on success, false otherwise
149 */
150 29128 inline bool BindHashBlob(const int idx_column, const shash::Any &hash) {
151
2/2
✓ Branch 1 taken 18285 times.
✓ Branch 2 taken 10843 times.
29128 if (hash.IsNull()) {
152 18285 return BindNull(idx_column);
153 } else {
154 10843 return BindBlob(idx_column, hash.digest, hash.GetDigestSize());
155 }
156 }
157
158 protected:
159 85076 SqlCatalog() : sqlite::Sql() { }
160 };
161
162
163 //------------------------------------------------------------------------------
164
165
166 /**
167 * Common ancestor of SQL statements that deal with directory entries.
168 */
169 class SqlDirent : public SqlCatalog {
170 public:
171 // Definition of bit positions for the flags field of a DirectoryEntry
172 // All other bit positions are unused
173 static const int kFlagDir = 1;
174 // Link in the parent catalog
175 static const int kFlagDirNestedMountpoint = 2;
176 // Link in the child catalog
177 static const int kFlagDirNestedRoot = 32;
178 static const int kFlagFile = 4;
179 static const int kFlagLink = 8;
180 static const int kFlagFileSpecial = 16;
181 static const int kFlagFileChunk = 64;
182 /**
183 * The file is not natively stored in cvmfs but on a different storage system,
184 * for instance on HTTPS data federation services.
185 * NOTE: used as magic number in SqlListContentHashes::SqlListContentHashes
186 */
187 static const int kFlagFileExternal = 128;
188 // as of 2^8: 3 bit for hashes
189 // - 0: SHA-1
190 // - 1: RIPEMD-160
191 // - ...
192 // Corresponds to shash::algorithms with offset in order to support future
193 // hashes
194 static const int kFlagPosHash = 8;
195 // Compression methods, 3 bits starting at 2^11
196 // Corresponds to zlib::Algorithms
197 static const int kFlagPosCompression = 11;
198 /**
199 * A transition point to a root catalog (instead of a nested catalog). Used
200 * to link previous snapshots into the catalog structure.
201 */
202 static const int kFlagDirBindMountpoint = 0x4000; // 2^14
203 /**
204 * An entry that should not appear in listings. Used for the /.cvmfs
205 * directory.
206 */
207 static const int kFlagHidden = 0x8000; // 2^15
208 /**
209 * For regular files, indicates that the file should be opened with direct I/O
210 */
211 static const int kFlagDirectIo = 0x10000; // 2^16
212 /**
213 * For regular files, indicates that this file has an attached file bundle
214 * list, that should be prefetched upon opening the file.
215 */
216 static const int kFlagBundleTrigger = 0x20000; // 2^17
217
218
219 protected:
220 /**
221 * Take the meta data from the DirectoryEntry and transform it
222 * into a valid flags field ready to be saved in the database.
223 * @param entry the DirectoryEntry to encode
224 * @return an integer containing the bitmap of the flags field
225 */
226 unsigned CreateDatabaseFlags(const DirectoryEntry &entry) const;
227 void StoreHashAlgorithm(const shash::Algorithms algo, unsigned *flags) const;
228 shash::Algorithms RetrieveHashAlgorithm(const unsigned flags) const;
229 zlib::Algorithms RetrieveCompressionAlgorithm(const unsigned flags) const;
230
231 /**
232 * The hardlink information (hardlink group ID and linkcount) is saved in one
233 * uint_64t field in the CVMFS Catalogs. Therefore we need to do bitshifting
234 * in these helper methods.
235 */
236 uint32_t Hardlinks2Linkcount(const uint64_t hardlinks) const;
237 uint32_t Hardlinks2HardlinkGroup(const uint64_t hardlinks) const;
238 uint64_t MakeHardlinks(const uint32_t hardlink_group,
239 const uint32_t linkcount) const;
240
241 /**
242 * Replaces place holder variables in a symbolic link by actual path elements.
243 * @param raw_symlink the raw symlink path (may) containing place holders
244 * @return the expanded symlink
245 */
246 void ExpandSymlink(LinkString *raw_symlink) const;
247 };
248
249
250 //------------------------------------------------------------------------------
251
252
253 class SqlDirentWrite : public SqlDirent {
254 public:
255 /**
256 * To bind an entire DirectoryEntry
257 * @param entry the DirectoryEntry to bind in the SQL statement
258 * @return true on success, false otherwise
259 */
260 virtual bool BindDirent(const DirectoryEntry &entry) = 0;
261
262 protected:
263 bool BindDirentFields(const int hash_idx,
264 const int hardlinks_idx,
265 const int size_idx,
266 const int mode_idx,
267 const int mtime_idx,
268 const int mtimens_idx,
269 const int flags_idx,
270 const int name_idx,
271 const int symlink_idx,
272 const int uid_idx,
273 const int gid_idx,
274 const DirectoryEntry &entry);
275 };
276
277
278 //------------------------------------------------------------------------------
279
280
281 class SqlListContentHashes : public SqlDirent {
282 public:
283 explicit SqlListContentHashes(const CatalogDatabase &database);
284 shash::Any GetHash() const;
285 };
286
287
288 //------------------------------------------------------------------------------
289
290
291 class SqlLookup : public SqlDirent {
292 public:
293 /**
294 * Retrieves a DirectoryEntry from a freshly performed SqlLookup statement.
295 * @param catalog the catalog in which the DirectoryEntry resides
296 * @return the retrieved DirectoryEntry
297 */
298 DirectoryEntry GetDirent(const Catalog *catalog,
299 const bool expand_symlink = true) const;
300
301 /**
302 * DirectoryEntrys do not contain their path hash.
303 * This method retrieves the saved path hash from the database
304 * @return the MD5 path hash of a freshly performed lookup
305 */
306 shash::Md5 GetPathHash() const;
307
308 /**
309 * DirectoryEntries do not contain their parent path hash.
310 * This method retrieves the saved parent path hash from the database
311 * @return the MD5 parent path hash of a freshly performed lookup
312 */
313 shash::Md5 GetParentPathHash() const;
314 };
315
316
317 //------------------------------------------------------------------------------
318
319
320 class SqlListing : public SqlLookup {
321 public:
322 explicit SqlListing(const CatalogDatabase &database);
323 bool BindPathHash(const struct shash::Md5 &hash);
324 };
325
326
327 //------------------------------------------------------------------------------
328
329
330 class SqlLookupPathHash : public SqlLookup {
331 public:
332 explicit SqlLookupPathHash(const CatalogDatabase &database);
333 bool BindPathHash(const struct shash::Md5 &hash);
334 };
335
336
337 //------------------------------------------------------------------------------
338
339
340 class SqlLookupInode : public SqlLookup {
341 public:
342 explicit SqlLookupInode(const CatalogDatabase &database);
343 bool BindRowId(const uint64_t inode);
344 };
345
346
347 //------------------------------------------------------------------------------
348
349
350 /**
351 * This SQL statement is only used for legacy catalog migrations and has been
352 * moved here as it needs to use a locally defined macro inside catalog_sql.cc
353 *
354 * Queries a single catalog and looks for DirectoryEntrys that have direct
355 * children in the same catalog but are marked as 'nested catalog mountpoints'.
356 * This is an inconsistent situation, since a mountpoint is supposed to be empty
357 * and it's children are stored in the corresponding referenced nested catalog.
358 *
359 * Note: the user code needs to check if there is a corresponding nested catalog
360 * reference for the found dangling mountpoints. If so, we also have a
361 * bogus state, but it is not reliably fixable automatically. The child-
362 * DirectoryEntrys would be masked by the mounting nested catalog but it
363 * is not clear if we can simply delete them or if this would destroy
364 * data.
365 */
366 class SqlLookupDanglingMountpoints : public catalog::SqlLookup {
367 public:
368 explicit SqlLookupDanglingMountpoints(const CatalogDatabase &database);
369 };
370
371
372 //------------------------------------------------------------------------------
373
374
375 /**
376 * Filesystem like _touch_ of a DirectoryEntry. Only file system specific meta
377 * data will be modified. All CVMFS-specific administrative data stays
378 * unchanged.
379 * NOTE: This is not a subclass of SqlDirent since it works on
380 * DirectoryEntryBase objects, which are restricted to file system meta
381 * data.
382 */
383 class SqlDirentTouch : public SqlCatalog {
384 public:
385 explicit SqlDirentTouch(const CatalogDatabase &database);
386
387 bool BindDirentBase(const DirectoryEntryBase &entry);
388 bool BindPathHash(const shash::Md5 &hash);
389 bool BindXattr(const XattrList &xattrs);
390 bool BindXattrEmpty();
391 };
392
393
394 //------------------------------------------------------------------------------
395
396
397 /**
398 * Nested catalogs and bind mountpoints.
399 */
400 class SqlNestedCatalogLookup : public SqlCatalog {
401 public:
402 explicit SqlNestedCatalogLookup(const CatalogDatabase &database);
403 bool BindSearchPath(const PathString &path);
404 shash::Any GetContentHash() const;
405 uint64_t GetSize() const;
406 };
407
408
409 //------------------------------------------------------------------------------
410
411
412 /**
413 * Nested catalogs and bind mountpoints.
414 */
415 class SqlNestedCatalogListing : public SqlCatalog {
416 public:
417 explicit SqlNestedCatalogListing(const CatalogDatabase &database);
418 PathString GetPath() const;
419 shash::Any GetContentHash() const;
420 uint64_t GetSize() const;
421 };
422
423
424 //------------------------------------------------------------------------------
425
426
427 /**
428 * Only nested catalogs, no bind mountpoints. Used for replication and GC.
429 */
430 class SqlOwnNestedCatalogListing : public SqlCatalog {
431 public:
432 explicit SqlOwnNestedCatalogListing(const CatalogDatabase &database);
433 PathString GetPath() const;
434 shash::Any GetContentHash() const;
435 uint64_t GetSize() const;
436 };
437
438
439 //------------------------------------------------------------------------------
440
441
442 class SqlDirentInsert : public SqlDirentWrite {
443 public:
444 explicit SqlDirentInsert(const CatalogDatabase &database);
445 bool BindPathHash(const shash::Md5 &hash);
446 bool BindParentPathHash(const shash::Md5 &hash);
447 bool BindDirent(const DirectoryEntry &entry);
448 bool BindXattr(const XattrList &xattrs);
449 bool BindXattrEmpty();
450 };
451
452
453 //------------------------------------------------------------------------------
454
455
456 class SqlDirentUpdate : public SqlDirentWrite {
457 public:
458 explicit SqlDirentUpdate(const CatalogDatabase &database);
459 bool BindPathHash(const shash::Md5 &hash);
460 bool BindDirent(const DirectoryEntry &entry);
461 };
462
463
464 //------------------------------------------------------------------------------
465
466
467 class SqlDirentUnlink : public SqlCatalog {
468 public:
469 explicit SqlDirentUnlink(const CatalogDatabase &database);
470 bool BindPathHash(const shash::Md5 &hash);
471 };
472
473
474 //------------------------------------------------------------------------------
475
476
477 /**
478 * Changes the linkcount for all files in a hardlink group.
479 */
480 class SqlIncLinkcount : public SqlCatalog {
481 public:
482 explicit SqlIncLinkcount(const CatalogDatabase &database);
483 bool BindPathHash(const shash::Md5 &hash);
484 bool BindDelta(const int delta);
485 };
486
487
488 //------------------------------------------------------------------------------
489
490
491 class SqlChunkInsert : public SqlCatalog {
492 public:
493 explicit SqlChunkInsert(const CatalogDatabase &database);
494 bool BindPathHash(const shash::Md5 &hash);
495 bool BindFileChunk(const FileChunk &chunk);
496 };
497
498
499 //------------------------------------------------------------------------------
500
501
502 class SqlChunksRemove : public SqlCatalog {
503 public:
504 explicit SqlChunksRemove(const CatalogDatabase &database);
505 bool BindPathHash(const shash::Md5 &hash);
506 };
507
508
509 //------------------------------------------------------------------------------
510
511
512 class SqlChunksListing : public SqlCatalog {
513 public:
514 explicit SqlChunksListing(const CatalogDatabase &database);
515 bool BindPathHash(const shash::Md5 &hash);
516 FileChunk GetFileChunk(const shash::Algorithms interpret_hash_as) const;
517 };
518
519
520 //------------------------------------------------------------------------------
521
522
523 class SqlChunksCount : public SqlCatalog {
524 public:
525 explicit SqlChunksCount(const CatalogDatabase &database);
526 bool BindPathHash(const shash::Md5 &hash);
527 int GetChunkCount() const;
528 };
529
530
531 //------------------------------------------------------------------------------
532
533
534 class SqlMaxHardlinkGroup : public SqlCatalog {
535 public:
536 explicit SqlMaxHardlinkGroup(const CatalogDatabase &database);
537 uint32_t GetMaxGroupId() const;
538 };
539
540
541 //------------------------------------------------------------------------------
542
543
544 class SqlGetCounter : public SqlCatalog {
545 public:
546 explicit SqlGetCounter(const CatalogDatabase &database);
547 bool BindCounter(const std::string &counter);
548 uint64_t GetCounter() const;
549
550 private:
551 bool compat_;
552 };
553
554
555 //------------------------------------------------------------------------------
556
557
558 class SqlUpdateCounter : public SqlCatalog {
559 public:
560 explicit SqlUpdateCounter(const CatalogDatabase &database);
561 bool BindCounter(const std::string &counter);
562 bool BindDelta(const int64_t delta);
563 };
564
565
566 //------------------------------------------------------------------------------
567
568
569 class SqlCreateCounter : public SqlCatalog {
570 public:
571 explicit SqlCreateCounter(const CatalogDatabase &database);
572 bool BindCounter(const std::string &counter);
573 bool BindInitialValue(const int64_t value);
574 };
575
576
577 //------------------------------------------------------------------------------
578
579
580 class SqlAllChunks : public SqlCatalog {
581 public:
582 explicit SqlAllChunks(const CatalogDatabase &database);
583 bool Open();
584 bool Next(shash::Any *hash, zlib::Algorithms *compression_alg);
585 bool Close();
586 };
587
588
589 //------------------------------------------------------------------------------
590
591
592 class SqlLookupXattrs : public SqlCatalog {
593 public:
594 explicit SqlLookupXattrs(const CatalogDatabase &database);
595 bool BindPathHash(const shash::Md5 &hash);
596 XattrList GetXattrs();
597 };
598
599 } // namespace catalog
600
601 #endif // CVMFS_CATALOG_SQL_H_
602