GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/directory_entry.h
Date: 2026-04-05 02:35:23
Exec Total Coverage
Lines: 124 135 91.9%
Branches: 22 38 57.9%

Line Branch Exec Source
1 /**
2 * This file is part of the CernVM File System.
3 *
4 * Data wrappers for single dentries. In addition to the normal file meta data
5 * it manages bookkeeping data specific to cvmfs such as the associated catalog.
6 */
7
8 #ifndef CVMFS_DIRECTORY_ENTRY_H_
9 #define CVMFS_DIRECTORY_ENTRY_H_
10
11 #include <sys/types.h>
12
13 #include <cassert>
14 #include <cstring>
15 #include <string>
16 #include <vector>
17
18 #include "bigvector.h"
19 #include "compression/compression.h"
20 #include "crypto/hash.h"
21 #include "shortstring.h"
22 #include "util/platform.h"
23
24 namespace publish {
25 class SyncItem;
26 class SyncItemNative;
27 class SyncItemTar;
28 class SyncItemDummyDir;
29 class SyncItemDummyCatalog;
30 } // namespace publish
31 namespace swissknife {
32 class CommandMigrate;
33 class CommandOverlay;
34 class IngestSQL;
35 } // namespace swissknife
36
37 namespace catalog {
38
39 // Create DirectoryEntries for unit test purposes.
40 class DirectoryEntryTestFactory;
41
42 class MockCatalogManager;
43 class Catalog;
44 class WritableCatalogManager;
45
46 template<class CatalogMgrT>
47 class CatalogBalancer;
48 typedef uint64_t inode_t;
49
50 enum SpecialDirents {
51 kDirentNormal = 0,
52 kDirentNegative,
53 };
54
55 /**
56 * Wrapper around struct dirent. Only contains file system related meta data
57 * for a directory entry.
58 * TODO(jblomer): separation to DirectoryEntry not quite clear: this one also
59 * contains hash, compression algorithm and external flag
60 */
61 class DirectoryEntryBase {
62 // For testing the catalog balancing
63 friend class CatalogBalancer<MockCatalogManager>;
64 // Create .cvmfscatalog and .cvmfsautocatalog files
65 friend class CatalogBalancer<WritableCatalogManager>;
66 // Simplify creation of DirectoryEntry objects for write back
67 friend class publish::SyncItem;
68 friend class publish::SyncItemNative;
69 friend class publish::SyncItemTar;
70 friend class publish::SyncItemDummyDir;
71 friend class publish::SyncItemDummyCatalog;
72 friend class swissknife::CommandOverlay;
73 friend class swissknife::IngestSQL; // TODO(vvolkl): can probably avoided
74 // with new setters
75 // Simplify file system like _touch_ of DirectoryEntry objects
76 friend class SqlDirentTouch;
77 // Allow creation of virtual directories and files
78 friend class VirtualCatalog;
79
80 public:
81 static const inode_t kInvalidInode = 0;
82
83 /**
84 * Used in the swissknife for sanity checks and catalog migration. If
85 * anything is added, also adjust PrintDifferences in swissknife::CommandDiff
86 * and CommandCheck::CompareEntries
87 */
88 struct Difference {
89 static const unsigned int kIdentical = 0x00000;
90 static const unsigned int kName = 0x00001;
91 static const unsigned int kLinkcount = 0x00002;
92 static const unsigned int kSize = 0x00004;
93 static const unsigned int kMode = 0x00008;
94 static const unsigned int kMtime = 0x00010;
95 static const unsigned int kSymlink = 0x00020;
96 static const unsigned int kChecksum = 0x00040;
97 static const unsigned int kHardlinkGroup = 0x00080;
98 static const unsigned int kNestedCatalogTransitionFlags = 0x00100;
99 static const unsigned int kChunkedFileFlag = 0x00200;
100 static const unsigned int kHasXattrsFlag = 0x00400;
101 static const unsigned int kExternalFileFlag = 0x00800;
102 static const unsigned int kBindMountpointFlag = 0x01000;
103 static const unsigned int kHiddenFlag = 0x02000;
104 static const unsigned int kDirectIoFlag = 0x04000;
105 static const unsigned int kUid = 0x08000;
106 static const unsigned int kGid = 0x10000;
107 };
108 typedef unsigned int Differences;
109
110 /**
111 * Zero-constructed DirectoryEntry objects are unusable as such.
112 */
113 40481522 inline DirectoryEntryBase()
114 40481522 : inode_(kInvalidInode)
115 40481522 , mode_(0)
116 40481522 , uid_(0)
117 40481522 , gid_(0)
118 40481522 , size_(0)
119 40481522 , mtime_(0)
120 40481522 , mtime_ns_(-1)
121 40481522 , linkcount_(1) // generally a normal file has linkcount 1 -> default
122 40481522 , has_xattrs_(false)
123 40481522 , is_external_file_(false)
124 40481522 , is_direct_io_(false)
125
1/2
✓ Branch 3 taken 40481522 times.
✗ Branch 4 not taken.
40481522 , compression_algorithm_(zlib::kZlibDefault) { }
126
127 12755 inline bool IsRegular() const { return S_ISREG(mode_); }
128 14024 inline bool IsLink() const { return S_ISLNK(mode_); }
129 14806 inline bool IsDirectory() const { return S_ISDIR(mode_); }
130 5265 inline bool IsFifo() const { return S_ISFIFO(mode_); }
131 5249 inline bool IsSocket() const { return S_ISSOCK(mode_); }
132 12652 inline bool IsCharDev() const { return S_ISCHR(mode_); }
133 12652 inline bool IsBlockDev() const { return S_ISBLK(mode_); }
134 5265 inline bool IsSpecial() const {
135
5/8
✓ Branch 1 taken 5249 times.
✓ Branch 2 taken 16 times.
✓ Branch 4 taken 5249 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 5249 times.
✗ Branch 8 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 5249 times.
5265 return IsFifo() || IsSocket() || IsCharDev() || IsBlockDev();
136 }
137 7603 inline bool IsExternalFile() const { return is_external_file_; }
138 3618 inline bool IsDirectIo() const { return is_direct_io_; }
139 9078 inline bool HasXattrs() const { return has_xattrs_; }
140 8317 inline bool HasMtimeNs() const { return mtime_ns_ >= 0; }
141
142 4127 inline inode_t inode() const { return inode_; }
143 2919 inline uint32_t linkcount() const { return linkcount_; }
144 17997 inline NameString name() const { return name_; }
145 610 inline LinkString symlink() const { return symlink_; }
146 510 inline time_t mtime() const { return mtime_; }
147 484 inline int32_t mtime_ns() const { return mtime_ns_; }
148 510 inline unsigned int mode() const { return mode_; }
149 913 inline uid_t uid() const { return uid_; }
150 913 inline gid_t gid() const { return gid_; }
151 2281 inline shash::Any checksum() const { return checksum_; }
152 10679 inline const shash::Any *checksum_ptr() const { return &checksum_; }
153 inline shash::Algorithms hash_algorithm() const {
154 return checksum_.algorithm;
155 }
156 7075 inline uint64_t size() const {
157
2/2
✓ Branch 1 taken 76 times.
✓ Branch 2 taken 6999 times.
7075 if (IsLink())
158 76 return symlink().GetLength();
159
3/6
✓ Branch 1 taken 6999 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 6999 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 6999 times.
6999 if (IsBlockDev() || IsCharDev())
160 return 0;
161 6999 return size_;
162 }
163 404 inline dev_t rdev() const {
164
3/6
✓ Branch 1 taken 404 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 404 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 404 times.
404 if (IsBlockDev() || IsCharDev())
165 return size_;
166 404 return 1;
167 }
168 6130 inline std::string GetFullPath(const std::string &parent_directory) const {
169 6130 std::string file_path = parent_directory + "/";
170
3/6
✓ Branch 1 taken 6130 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 6130 times.
✗ Branch 6 not taken.
✓ Branch 9 taken 6130 times.
✗ Branch 10 not taken.
6130 file_path.append(name().GetChars(), name().GetLength());
171 6130 return file_path;
172 }
173
174 481 inline void set_inode(const inode_t inode) { inode_ = inode; }
175 2784 inline void set_linkcount(const uint32_t linkcount) {
176
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2784 times.
2784 assert(linkcount > 0);
177 2784 linkcount_ = linkcount;
178 2784 }
179 inline void set_symlink(const LinkString &symlink) { symlink_ = symlink; }
180 4544 inline void set_has_xattrs(const bool has_xattrs) {
181 4544 has_xattrs_ = has_xattrs;
182 4544 }
183
184 3134 inline zlib::Algorithms compression_algorithm() const {
185 3134 return compression_algorithm_;
186 }
187
188 /**
189 * Converts to a stat struct as required by many Fuse callbacks.
190 * @return the struct stat for this DirectoryEntry
191 */
192 378 inline struct stat GetStatStructure() const {
193 struct stat s;
194 378 memset(&s, 0, sizeof(s));
195 378 s.st_dev = 1;
196 378 s.st_ino = inode_;
197 378 s.st_mode = mode_;
198 378 s.st_nlink = linkcount();
199 378 s.st_uid = uid();
200 378 s.st_gid = gid();
201 378 s.st_rdev = rdev();
202 378 s.st_size = static_cast<off_t>(size());
203 378 s.st_blksize = 4096; // will be ignored by Fuse
204 378 s.st_blocks = static_cast<blkcnt_t>(1 + size() / 512);
205 378 s.st_atime = mtime_;
206 378 s.st_mtime = mtime_;
207 378 s.st_ctime = mtime_;
208
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 378 times.
378 if (HasMtimeNs()) {
209 #ifdef __APPLE__
210 s.st_atimespec.tv_nsec = mtime_ns_;
211 s.st_mtimespec.tv_nsec = mtime_ns_;
212 s.st_ctimespec.tv_nsec = mtime_ns_;
213 #else
214 s.st_atim.tv_nsec = mtime_ns_;
215 s.st_mtim.tv_nsec = mtime_ns_;
216 s.st_ctim.tv_nsec = mtime_ns_;
217 #endif
218 }
219 378 return s;
220 }
221
222 Differences CompareTo(const DirectoryEntryBase &other) const;
223 inline bool operator==(const DirectoryEntryBase &other) const {
224 return CompareTo(other) == Difference::kIdentical;
225 }
226 inline bool operator!=(const DirectoryEntryBase &other) const {
227 return !(*this == other);
228 }
229
230 protected:
231 // Inodes are generated based on the rowid of the entry in the file catalog.
232 inode_t inode_;
233
234 // Data from struct stat
235 NameString name_;
236 unsigned int mode_;
237 uid_t uid_;
238 gid_t gid_;
239 uint64_t size_;
240 time_t mtime_;
241 // nanosecond part of the mtime. Only valid if non-negative
242 int32_t mtime_ns_;
243 LinkString symlink_;
244 uint32_t linkcount_;
245 // In order to save memory, we only indicate if a directory entry has custom
246 // extended attributes. Another call to the file catalog is necessary to
247 // get them.
248 bool has_xattrs_;
249
250 // The cryptographic hash is not part of the file system intrinsics, though
251 // it can be computed just using the file contents. We therefore put it in
252 // this base class.
253 shash::Any checksum_;
254
255 bool is_external_file_;
256 bool is_direct_io_;
257
258 // The compression algorithm
259 zlib::Algorithms compression_algorithm_;
260 };
261
262
263 /**
264 * In addition to the file system meta-data covered by DirectoryEntryBase,
265 * DirectoryEntries contain cvmfs-specific meta data. Currently these are the
266 * following things:
267 * - Pointer to the originating catalog
268 * - Markers for nested catalog transition points (mountpoint and root entry)
269 * - Transient marker storing the time of caching (Fuse page caches).
270 * This is required to invalidate caches after a catalog update
271 * - Hardlink group used to emulate hardlinks in cvmfs
272 */
273 class DirectoryEntry : public DirectoryEntryBase {
274 // Simplify creation of DirectoryEntry objects
275 friend class SqlLookup;
276 // Simplify write of DirectoryEntry objects in database
277 friend class SqlDirentWrite;
278 // For fixing DirectoryEntry glitches
279 friend class swissknife::CommandMigrate;
280 // TODO(rmeusel): remove this dependency
281 friend class WritableCatalogManager;
282 // Create DirectoryEntries for unit test purposes.
283 friend class DirectoryEntryTestFactory;
284
285 public:
286 /**
287 * This is _kind of_ a copy constructor allowing us to create
288 * DirectoryEntries directly from DirectoryEntryBase objects. We make it
289 * explicit, to disallow black magic from happening. It uses the copy
290 * constructor of DirectoryEntryBase and initializes the additional fields of
291 * DirectoryEntry.
292 */
293 2868 inline explicit DirectoryEntry(const DirectoryEntryBase &base)
294 2868 : DirectoryEntryBase(base)
295 2868 , hardlink_group_(0)
296 2868 , is_nested_catalog_root_(false)
297 2868 , is_nested_catalog_mountpoint_(false)
298 2868 , is_bind_mountpoint_(false)
299 2868 , is_chunked_file_(false)
300 2868 , is_hidden_(false)
301 2868 , is_negative_(false) { }
302
303 40480082 inline DirectoryEntry()
304 80960164 : hardlink_group_(0)
305 40480082 , is_nested_catalog_root_(false)
306 40480082 , is_nested_catalog_mountpoint_(false)
307 40480082 , is_bind_mountpoint_(false)
308 40480082 , is_chunked_file_(false)
309 40480082 , is_hidden_(false)
310 40480082 , is_negative_(false) { }
311
312 1416 inline explicit DirectoryEntry(SpecialDirents special_type)
313 2832 : hardlink_group_(0)
314 1416 , is_nested_catalog_root_(false)
315 1416 , is_nested_catalog_mountpoint_(false)
316 1416 , is_bind_mountpoint_(false)
317 1416 , is_chunked_file_(false)
318 1416 , is_hidden_(false)
319 1416 , is_negative_(true) {
320
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1416 times.
1416 assert(special_type == kDirentNegative);
321 1416 }
322
323 177 inline SpecialDirents GetSpecial() const {
324
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 151 times.
177 return is_negative_ ? kDirentNegative : kDirentNormal;
325 }
326
327 Differences CompareTo(const DirectoryEntry &other) const;
328 inline bool operator==(const DirectoryEntry &other) const {
329 return CompareTo(other) == Difference::kIdentical;
330 }
331 inline bool operator!=(const DirectoryEntry &other) const {
332 return !(*this == other);
333 }
334
335 1206 inline bool IsNegative() const { return is_negative_; }
336 12585 inline bool IsNestedCatalogRoot() const { return is_nested_catalog_root_; }
337 10467 inline bool IsNestedCatalogMountpoint() const {
338 10467 return is_nested_catalog_mountpoint_;
339 }
340 7604 inline bool IsBindMountpoint() const { return is_bind_mountpoint_; }
341 17034 inline bool IsChunkedFile() const { return is_chunked_file_; }
342 9069 inline bool IsHidden() const { return is_hidden_; }
343 484 inline uint32_t hardlink_group() const { return hardlink_group_; }
344
345 110 inline void set_hardlink_group(const uint32_t group) {
346 110 hardlink_group_ = group;
347 110 }
348 586 inline void set_is_nested_catalog_mountpoint(const bool val) {
349 586 is_nested_catalog_mountpoint_ = val;
350 586 }
351 553 inline void set_is_nested_catalog_root(const bool val) {
352 553 is_nested_catalog_root_ = val;
353 553 }
354 inline void set_is_bind_mountpoint(const bool val) {
355 is_bind_mountpoint_ = val;
356 }
357 220 inline void set_is_chunked_file(const bool val) { is_chunked_file_ = val; }
358 inline void set_is_hidden(const bool val) { is_hidden_ = val; }
359
360 private:
361 /**
362 * Hardlink handling is emulated in CVMFS. Since inodes are allocated on
363 * demand we save hardlink relationships using the same hardlink_group.
364 */
365 uint32_t hardlink_group_;
366
367 // TODO(jblomer): transform into bitfield to save memory
368 bool is_nested_catalog_root_;
369 bool is_nested_catalog_mountpoint_;
370 bool is_bind_mountpoint_;
371 bool is_chunked_file_;
372 bool is_hidden_;
373 bool is_negative_;
374 };
375
376
377 /**
378 * Saves memory for large directory listings.
379 */
380 struct StatEntry {
381 NameString name;
382 struct stat info;
383
384 34 StatEntry() { memset(&info, 0, sizeof(info)); }
385 StatEntry(const NameString &n, const struct stat &i) : name(n), info(i) { }
386 };
387
388
389 typedef std::vector<DirectoryEntry> DirectoryEntryList;
390 typedef std::vector<DirectoryEntryBase> DirectoryEntryBaseList;
391 // TODO(jblomer): use mmap for large listings
392 typedef BigVector<StatEntry> StatEntryList;
393
394 } // namespace catalog
395
396 #endif // CVMFS_DIRECTORY_ENTRY_H_
397