GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/catalog_sql.h
Date: 2025-06-22 02:36:02
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 #ifndef __STDC_FORMAT_MACROS
20 #define __STDC_FORMAT_MACROS
21 #endif
22
23 #include <inttypes.h>
24
25 #include <string>
26
27 #include "compression/compression.h"
28 #include "crypto/hash.h"
29 #include "directory_entry.h"
30 #include "file_chunk.h"
31 #include "shortstring.h"
32 #include "sql.h"
33
34 class XattrList;
35
36 namespace catalog {
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 7328 CatalogDatabase(const std::string &filename, const OpenMode open_mode)
66 7328 : 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 31079 SqlCatalog(const CatalogDatabase &database, const std::string &statement) {
85
2/4
✓ Branch 1 taken 31079 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 31079 times.
✗ Branch 5 not taken.
31079 Init(database.sqlite_db(), statement);
86 31079 }
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 25218 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 25218 RetrieveBlob(idx_column));
112 25218 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 7415 times.
✓ Branch 1 taken 17803 times.
25218 : 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 67299 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 67299 times.
✗ Branch 2 not taken.
67299 hash.ToIntPair(&high, &low);
138
1/2
✓ Branch 1 taken 67299 times.
✗ Branch 2 not taken.
67299 const bool retval = BindInt64(idx_high, static_cast<int64_t>(high))
139
3/6
✓ Branch 0 taken 67299 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 67299 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 67299 times.
✗ Branch 6 not taken.
67299 && BindInt64(idx_low, static_cast<int64_t>(low));
140 67299 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 23378 inline bool BindHashBlob(const int idx_column, const shash::Any &hash) {
151
2/2
✓ Branch 1 taken 14642 times.
✓ Branch 2 taken 8736 times.
23378 if (hash.IsNull()) {
152 14642 return BindNull(idx_column);
153 } else {
154 8736 return BindBlob(idx_column, hash.digest, hash.GetDigestSize());
155 }
156 }
157
158 protected:
159 75304 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
214 protected:
215 /**
216 * Take the meta data from the DirectoryEntry and transform it
217 * into a valid flags field ready to be saved in the database.
218 * @param entry the DirectoryEntry to encode
219 * @return an integer containing the bitmap of the flags field
220 */
221 unsigned CreateDatabaseFlags(const DirectoryEntry &entry) const;
222 void StoreHashAlgorithm(const shash::Algorithms algo, unsigned *flags) const;
223 shash::Algorithms RetrieveHashAlgorithm(const unsigned flags) const;
224 zlib::Algorithms RetrieveCompressionAlgorithm(const unsigned flags) const;
225
226 /**
227 * The hardlink information (hardlink group ID and linkcount) is saved in one
228 * uint_64t field in the CVMFS Catalogs. Therefore we need to do bitshifting
229 * in these helper methods.
230 */
231 uint32_t Hardlinks2Linkcount(const uint64_t hardlinks) const;
232 uint32_t Hardlinks2HardlinkGroup(const uint64_t hardlinks) const;
233 uint64_t MakeHardlinks(const uint32_t hardlink_group,
234 const uint32_t linkcount) const;
235
236 /**
237 * Replaces place holder variables in a symbolic link by actual path elements.
238 * @param raw_symlink the raw symlink path (may) containing place holders
239 * @return the expanded symlink
240 */
241 void ExpandSymlink(LinkString *raw_symlink) const;
242 };
243
244
245 //------------------------------------------------------------------------------
246
247
248 class SqlDirentWrite : public SqlDirent {
249 public:
250 /**
251 * To bind an entire DirectoryEntry
252 * @param entry the DirectoryEntry to bind in the SQL statement
253 * @return true on success, false otherwise
254 */
255 virtual bool BindDirent(const DirectoryEntry &entry) = 0;
256
257 protected:
258 bool BindDirentFields(const int hash_idx,
259 const int hardlinks_idx,
260 const int size_idx,
261 const int mode_idx,
262 const int mtime_idx,
263 const int mtimens_idx,
264 const int flags_idx,
265 const int name_idx,
266 const int symlink_idx,
267 const int uid_idx,
268 const int gid_idx,
269 const DirectoryEntry &entry);
270 };
271
272
273 //------------------------------------------------------------------------------
274
275
276 class SqlListContentHashes : public SqlDirent {
277 public:
278 explicit SqlListContentHashes(const CatalogDatabase &database);
279 shash::Any GetHash() const;
280 };
281
282
283 //------------------------------------------------------------------------------
284
285
286 class SqlLookup : public SqlDirent {
287 public:
288 /**
289 * Retrieves a DirectoryEntry from a freshly performed SqlLookup statement.
290 * @param catalog the catalog in which the DirectoryEntry resides
291 * @return the retrieved DirectoryEntry
292 */
293 DirectoryEntry GetDirent(const Catalog *catalog,
294 const bool expand_symlink = true) const;
295
296 /**
297 * DirectoryEntrys do not contain their path hash.
298 * This method retrieves the saved path hash from the database
299 * @return the MD5 path hash of a freshly performed lookup
300 */
301 shash::Md5 GetPathHash() const;
302
303 /**
304 * DirectoryEntries do not contain their parent path hash.
305 * This method retrieves the saved parent path hash from the database
306 * @return the MD5 parent path hash of a freshly performed lookup
307 */
308 shash::Md5 GetParentPathHash() const;
309 };
310
311
312 //------------------------------------------------------------------------------
313
314
315 class SqlListing : public SqlLookup {
316 public:
317 explicit SqlListing(const CatalogDatabase &database);
318 bool BindPathHash(const struct shash::Md5 &hash);
319 };
320
321
322 //------------------------------------------------------------------------------
323
324
325 class SqlLookupPathHash : public SqlLookup {
326 public:
327 explicit SqlLookupPathHash(const CatalogDatabase &database);
328 bool BindPathHash(const struct shash::Md5 &hash);
329 };
330
331
332 //------------------------------------------------------------------------------
333
334
335 class SqlLookupInode : public SqlLookup {
336 public:
337 explicit SqlLookupInode(const CatalogDatabase &database);
338 bool BindRowId(const uint64_t inode);
339 };
340
341
342 //------------------------------------------------------------------------------
343
344
345 /**
346 * This SQL statement is only used for legacy catalog migrations and has been
347 * moved here as it needs to use a locally defined macro inside catalog_sql.cc
348 *
349 * Queries a single catalog and looks for DirectoryEntrys that have direct
350 * children in the same catalog but are marked as 'nested catalog mountpoints'.
351 * This is an inconsistent situation, since a mountpoint is supposed to be empty
352 * and it's children are stored in the corresponding referenced nested catalog.
353 *
354 * Note: the user code needs to check if there is a corresponding nested catalog
355 * reference for the found dangling mountpoints. If so, we also have a
356 * bogus state, but it is not reliably fixable automatically. The child-
357 * DirectoryEntrys would be masked by the mounting nested catalog but it
358 * is not clear if we can simply delete them or if this would destroy
359 * data.
360 */
361 class SqlLookupDanglingMountpoints : public catalog::SqlLookup {
362 public:
363 explicit SqlLookupDanglingMountpoints(const CatalogDatabase &database);
364 };
365
366
367 //------------------------------------------------------------------------------
368
369
370 /**
371 * Filesystem like _touch_ of a DirectoryEntry. Only file system specific meta
372 * data will be modified. All CVMFS-specific administrative data stays
373 * unchanged.
374 * NOTE: This is not a subclass of SqlDirent since it works on
375 * DirectoryEntryBase objects, which are restricted to file system meta
376 * data.
377 */
378 class SqlDirentTouch : public SqlCatalog {
379 public:
380 explicit SqlDirentTouch(const CatalogDatabase &database);
381
382 bool BindDirentBase(const DirectoryEntryBase &entry);
383 bool BindPathHash(const shash::Md5 &hash);
384 bool BindXattr(const XattrList &xattrs);
385 bool BindXattrEmpty();
386 };
387
388
389 //------------------------------------------------------------------------------
390
391
392 /**
393 * Nested catalogs and bind mountpoints.
394 */
395 class SqlNestedCatalogLookup : public SqlCatalog {
396 public:
397 explicit SqlNestedCatalogLookup(const CatalogDatabase &database);
398 bool BindSearchPath(const PathString &path);
399 shash::Any GetContentHash() const;
400 uint64_t GetSize() const;
401 };
402
403
404 //------------------------------------------------------------------------------
405
406
407 /**
408 * Nested catalogs and bind mountpoints.
409 */
410 class SqlNestedCatalogListing : public SqlCatalog {
411 public:
412 explicit SqlNestedCatalogListing(const CatalogDatabase &database);
413 PathString GetPath() const;
414 shash::Any GetContentHash() const;
415 uint64_t GetSize() const;
416 };
417
418
419 //------------------------------------------------------------------------------
420
421
422 /**
423 * Only nested catalogs, no bind mountpoints. Used for replication and GC.
424 */
425 class SqlOwnNestedCatalogListing : public SqlCatalog {
426 public:
427 explicit SqlOwnNestedCatalogListing(const CatalogDatabase &database);
428 PathString GetPath() const;
429 shash::Any GetContentHash() const;
430 uint64_t GetSize() const;
431 };
432
433
434 //------------------------------------------------------------------------------
435
436
437 class SqlDirentInsert : public SqlDirentWrite {
438 public:
439 explicit SqlDirentInsert(const CatalogDatabase &database);
440 bool BindPathHash(const shash::Md5 &hash);
441 bool BindParentPathHash(const shash::Md5 &hash);
442 bool BindDirent(const DirectoryEntry &entry);
443 bool BindXattr(const XattrList &xattrs);
444 bool BindXattrEmpty();
445 };
446
447
448 //------------------------------------------------------------------------------
449
450
451 class SqlDirentUpdate : public SqlDirentWrite {
452 public:
453 explicit SqlDirentUpdate(const CatalogDatabase &database);
454 bool BindPathHash(const shash::Md5 &hash);
455 bool BindDirent(const DirectoryEntry &entry);
456 };
457
458
459 //------------------------------------------------------------------------------
460
461
462 class SqlDirentUnlink : public SqlCatalog {
463 public:
464 explicit SqlDirentUnlink(const CatalogDatabase &database);
465 bool BindPathHash(const shash::Md5 &hash);
466 };
467
468
469 //------------------------------------------------------------------------------
470
471
472 /**
473 * Changes the linkcount for all files in a hardlink group.
474 */
475 class SqlIncLinkcount : public SqlCatalog {
476 public:
477 explicit SqlIncLinkcount(const CatalogDatabase &database);
478 bool BindPathHash(const shash::Md5 &hash);
479 bool BindDelta(const int delta);
480 };
481
482
483 //------------------------------------------------------------------------------
484
485
486 class SqlChunkInsert : public SqlCatalog {
487 public:
488 explicit SqlChunkInsert(const CatalogDatabase &database);
489 bool BindPathHash(const shash::Md5 &hash);
490 bool BindFileChunk(const FileChunk &chunk);
491 };
492
493
494 //------------------------------------------------------------------------------
495
496
497 class SqlChunksRemove : public SqlCatalog {
498 public:
499 explicit SqlChunksRemove(const CatalogDatabase &database);
500 bool BindPathHash(const shash::Md5 &hash);
501 };
502
503
504 //------------------------------------------------------------------------------
505
506
507 class SqlChunksListing : public SqlCatalog {
508 public:
509 explicit SqlChunksListing(const CatalogDatabase &database);
510 bool BindPathHash(const shash::Md5 &hash);
511 FileChunk GetFileChunk(const shash::Algorithms interpret_hash_as) const;
512 };
513
514
515 //------------------------------------------------------------------------------
516
517
518 class SqlChunksCount : public SqlCatalog {
519 public:
520 explicit SqlChunksCount(const CatalogDatabase &database);
521 bool BindPathHash(const shash::Md5 &hash);
522 int GetChunkCount() const;
523 };
524
525
526 //------------------------------------------------------------------------------
527
528
529 class SqlMaxHardlinkGroup : public SqlCatalog {
530 public:
531 explicit SqlMaxHardlinkGroup(const CatalogDatabase &database);
532 uint32_t GetMaxGroupId() const;
533 };
534
535
536 //------------------------------------------------------------------------------
537
538
539 class SqlGetCounter : public SqlCatalog {
540 public:
541 explicit SqlGetCounter(const CatalogDatabase &database);
542 bool BindCounter(const std::string &counter);
543 uint64_t GetCounter() const;
544
545 private:
546 bool compat_;
547 };
548
549
550 //------------------------------------------------------------------------------
551
552
553 class SqlUpdateCounter : public SqlCatalog {
554 public:
555 explicit SqlUpdateCounter(const CatalogDatabase &database);
556 bool BindCounter(const std::string &counter);
557 bool BindDelta(const int64_t delta);
558 };
559
560
561 //------------------------------------------------------------------------------
562
563
564 class SqlCreateCounter : public SqlCatalog {
565 public:
566 explicit SqlCreateCounter(const CatalogDatabase &database);
567 bool BindCounter(const std::string &counter);
568 bool BindInitialValue(const int64_t value);
569 };
570
571
572 //------------------------------------------------------------------------------
573
574
575 class SqlAllChunks : public SqlCatalog {
576 public:
577 explicit SqlAllChunks(const CatalogDatabase &database);
578 bool Open();
579 bool Next(shash::Any *hash, zlib::Algorithms *compression_alg);
580 bool Close();
581 };
582
583
584 //------------------------------------------------------------------------------
585
586
587 class SqlLookupXattrs : public SqlCatalog {
588 public:
589 explicit SqlLookupXattrs(const CatalogDatabase &database);
590 bool BindPathHash(const shash::Md5 &hash);
591 XattrList GetXattrs();
592 };
593
594 } // namespace catalog
595
596 #endif // CVMFS_CATALOG_SQL_H_
597