GCC Code Coverage Report


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