GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/directory_entry.h
Date: 2026-05-24 02:35:55
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 32556197 inline DirectoryEntryBase()
114 32556197 : inode_(kInvalidInode)
115 32556197 , mode_(0)
116 32556197 , uid_(0)
117 32556197 , gid_(0)
118 32556197 , size_(0)
119 32556197 , mtime_(0)
120 32556197 , mtime_ns_(-1)
121 32556197 , linkcount_(1) // generally a normal file has linkcount 1 -> default
122 32556197 , has_xattrs_(false)
123 32556197 , is_external_file_(false)
124 32556197 , is_direct_io_(false)
125
1/2
✓ Branch 3 taken 32556197 times.
✗ Branch 4 not taken.
32556197 , compression_algorithm_(zlib::kZlibDefault) { }
126
127 44505 inline bool IsRegular() const { return S_ISREG(mode_); }
128 48607 inline bool IsLink() const { return S_ISLNK(mode_); }
129 48535 inline bool IsDirectory() const { return S_ISDIR(mode_); }
130 19203 inline bool IsFifo() const { return S_ISFIFO(mode_); }
131 19011 inline bool IsSocket() const { return S_ISSOCK(mode_); }
132 44056 inline bool IsCharDev() const { return S_ISCHR(mode_); }
133 44056 inline bool IsBlockDev() const { return S_ISBLK(mode_); }
134 19203 inline bool IsSpecial() const {
135
5/8
✓ Branch 1 taken 19011 times.
✓ Branch 2 taken 192 times.
✓ Branch 4 taken 19011 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 19011 times.
✗ Branch 8 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 19011 times.
19203 return IsFifo() || IsSocket() || IsCharDev() || IsBlockDev();
136 }
137 26928 inline bool IsExternalFile() const { return is_external_file_; }
138 11371 inline bool IsDirectIo() const { return is_direct_io_; }
139 32874 inline bool HasXattrs() const { return has_xattrs_; }
140 28112 inline bool HasMtimeNs() const { return mtime_ns_ >= 0; }
141
142 4810 inline inode_t inode() const { return inode_; }
143 6314 inline uint32_t linkcount() const { return linkcount_; }
144 51781 inline NameString name() const { return name_; }
145 708 inline LinkString symlink() const { return symlink_; }
146 490 inline time_t mtime() const { return mtime_; }
147 440 inline int32_t mtime_ns() const { return mtime_ns_; }
148 490 inline unsigned int mode() const { return mode_; }
149 1404 inline uid_t uid() const { return uid_; }
150 1404 inline gid_t gid() const { return gid_; }
151 6212 inline shash::Any checksum() const { return checksum_; }
152 37663 inline const shash::Any *checksum_ptr() const { return &checksum_; }
153 inline shash::Algorithms hash_algorithm() const {
154 return checksum_.algorithm;
155 }
156 24256 inline uint64_t size() const {
157
2/2
✓ Branch 1 taken 144 times.
✓ Branch 2 taken 24112 times.
24256 if (IsLink())
158 144 return symlink().GetLength();
159
3/6
✓ Branch 1 taken 24112 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 24112 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 24112 times.
24112 if (IsBlockDev() || IsCharDev())
160 return 0;
161 24112 return size_;
162 }
163 933 inline dev_t rdev() const {
164
3/6
✓ Branch 1 taken 933 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 933 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 933 times.
933 if (IsBlockDev() || IsCharDev())
165 return size_;
166 933 return 1;
167 }
168 19241 inline std::string GetFullPath(const std::string &parent_directory) const {
169 19241 std::string file_path = parent_directory + "/";
170
3/6
✓ Branch 1 taken 19241 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 19241 times.
✗ Branch 6 not taken.
✓ Branch 9 taken 19241 times.
✗ Branch 10 not taken.
19241 file_path.append(name().GetChars(), name().GetLength());
171 19241 return file_path;
172 }
173
174 1454 inline void set_inode(const inode_t inode) { inode_ = inode; }
175 8902 inline void set_linkcount(const uint32_t linkcount) {
176
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8902 times.
8902 assert(linkcount > 0);
177 8902 linkcount_ = linkcount;
178 8902 }
179 inline void set_symlink(const LinkString &symlink) { symlink_ = symlink; }
180 17332 inline void set_has_xattrs(const bool has_xattrs) {
181 17332 has_xattrs_ = has_xattrs;
182 17332 }
183
184 10931 inline zlib::Algorithms compression_algorithm() const {
185 10931 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 883 inline struct stat GetStatStructure() const {
193 struct stat s;
194 883 memset(&s, 0, sizeof(s));
195 883 s.st_dev = 1;
196 883 s.st_ino = inode_;
197 883 s.st_mode = mode_;
198 883 s.st_nlink = linkcount();
199 883 s.st_uid = uid();
200 883 s.st_gid = gid();
201 883 s.st_rdev = rdev();
202 883 s.st_size = static_cast<off_t>(size());
203 883 s.st_blksize = 4096; // will be ignored by Fuse
204 883 s.st_blocks = static_cast<blkcnt_t>(1 + size() / 512);
205 883 s.st_atime = mtime_;
206 883 s.st_mtime = mtime_;
207 883 s.st_ctime = mtime_;
208
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 883 times.
883 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 883 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 9018 inline explicit DirectoryEntry(const DirectoryEntryBase &base)
294 9018 : DirectoryEntryBase(base)
295 9018 , hardlink_group_(0)
296 9018 , is_nested_catalog_root_(false)
297 9018 , is_nested_catalog_mountpoint_(false)
298 9018 , is_bind_mountpoint_(false)
299 9018 , is_chunked_file_(false)
300 9018 , is_hidden_(false)
301 9018 , is_negative_(false) { }
302
303 32553261 inline DirectoryEntry()
304 65106522 : hardlink_group_(0)
305 32553261 , is_nested_catalog_root_(false)
306 32553261 , is_nested_catalog_mountpoint_(false)
307 32553261 , is_bind_mountpoint_(false)
308 32553261 , is_chunked_file_(false)
309 32553261 , is_hidden_(false)
310 32553261 , is_negative_(false) { }
311
312 2932 inline explicit DirectoryEntry(SpecialDirents special_type)
313 5864 : hardlink_group_(0)
314 2932 , is_nested_catalog_root_(false)
315 2932 , is_nested_catalog_mountpoint_(false)
316 2932 , is_bind_mountpoint_(false)
317 2932 , is_chunked_file_(false)
318 2932 , is_hidden_(false)
319 2932 , is_negative_(true) {
320
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2932 times.
2932 assert(special_type == kDirentNegative);
321 2932 }
322
323 360 inline SpecialDirents GetSpecial() const {
324
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 309 times.
360 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 2928 inline bool IsNegative() const { return is_negative_; }
336 42756 inline bool IsNestedCatalogRoot() const { return is_nested_catalog_root_; }
337 33023 inline bool IsNestedCatalogMountpoint() const {
338 33023 return is_nested_catalog_mountpoint_;
339 }
340 24595 inline bool IsBindMountpoint() const { return is_bind_mountpoint_; }
341 58413 inline bool IsChunkedFile() const { return is_chunked_file_; }
342 28424 inline bool IsHidden() const { return is_hidden_; }
343 440 inline uint32_t hardlink_group() const { return hardlink_group_; }
344
345 490 inline void set_hardlink_group(const uint32_t group) {
346 490 hardlink_group_ = group;
347 490 }
348 2315 inline void set_is_nested_catalog_mountpoint(const bool val) {
349 2315 is_nested_catalog_mountpoint_ = val;
350 2315 }
351 2207 inline void set_is_nested_catalog_root(const bool val) {
352 2207 is_nested_catalog_root_ = val;
353 2207 }
354 inline void set_is_bind_mountpoint(const bool val) {
355 is_bind_mountpoint_ = val;
356 }
357 2880 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 133 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