GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/directory_entry.h
Date: 2025-08-31 02:39:21
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 18352800 inline DirectoryEntryBase()
112 18352800 : inode_(kInvalidInode)
113 18352800 , mode_(0)
114 18352800 , uid_(0)
115 18352800 , gid_(0)
116 18352800 , size_(0)
117 18352800 , mtime_(0)
118 18352800 , mtime_ns_(-1)
119 18352800 , linkcount_(1) // generally a normal file has linkcount 1 -> default
120 18352800 , has_xattrs_(false)
121 18352800 , is_external_file_(false)
122 18352800 , is_direct_io_(false)
123
1/2
✓ Branch 3 taken 18352800 times.
✗ Branch 4 not taken.
18352800 , compression_algorithm_(zlib::kZlibDefault) { }
124
125 31670 inline bool IsRegular() const { return S_ISREG(mode_); }
126 35386 inline bool IsLink() const { return S_ISLNK(mode_); }
127 36074 inline bool IsDirectory() const { return S_ISDIR(mode_); }
128 12057 inline bool IsFifo() const { return S_ISFIFO(mode_); }
129 12041 inline bool IsSocket() const { return S_ISSOCK(mode_); }
130 31468 inline bool IsCharDev() const { return S_ISCHR(mode_); }
131 31468 inline bool IsBlockDev() const { return S_ISBLK(mode_); }
132 12057 inline bool IsSpecial() const {
133
5/8
✓ Branch 1 taken 12041 times.
✓ Branch 2 taken 16 times.
✓ Branch 4 taken 12041 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 12041 times.
✗ Branch 8 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 12041 times.
12057 return IsFifo() || IsSocket() || IsCharDev() || IsBlockDev();
134 }
135 18822 inline bool IsExternalFile() const { return is_external_file_; }
136 9385 inline bool IsDirectIo() const { return is_direct_io_; }
137 22317 inline bool HasXattrs() const { return has_xattrs_; }
138 19115 inline bool HasMtimeNs() const { return mtime_ns_ >= 0; }
139
140 17025 inline inode_t inode() const { return inode_; }
141 9637 inline uint32_t linkcount() const { return linkcount_; }
142 50417 inline NameString name() const { return name_; }
143 2556 inline LinkString symlink() const { return symlink_; }
144 2186 inline time_t mtime() const { return mtime_; }
145 2112 inline int32_t mtime_ns() const { return mtime_ns_; }
146 2186 inline unsigned int mode() const { return mode_; }
147 3347 inline uid_t uid() const { return uid_; }
148 3347 inline gid_t gid() const { return gid_; }
149 6586 inline shash::Any checksum() const { return checksum_; }
150 24649 inline const shash::Any *checksum_ptr() const { return &checksum_; }
151 inline shash::Algorithms hash_algorithm() const {
152 return checksum_.algorithm;
153 }
154 18537 inline uint64_t size() const {
155
2/2
✓ Branch 1 taken 272 times.
✓ Branch 2 taken 18265 times.
18537 if (IsLink())
156 272 return symlink().GetLength();
157
3/6
✓ Branch 1 taken 18265 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 18265 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 18265 times.
18265 if (IsBlockDev() || IsCharDev())
158 return 0;
159 18265 return size_;
160 }
161 1162 inline dev_t rdev() const {
162
3/6
✓ Branch 1 taken 1162 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1162 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 1162 times.
1162 if (IsBlockDev() || IsCharDev())
163 return size_;
164 1162 return 1;
165 }
166 16207 inline std::string GetFullPath(const std::string &parent_directory) const {
167 16207 std::string file_path = parent_directory + "/";
168
3/6
✓ Branch 1 taken 16207 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 16207 times.
✗ Branch 6 not taken.
✓ Branch 9 taken 16207 times.
✗ Branch 10 not taken.
16207 file_path.append(name().GetChars(), name().GetLength());
169 16207 return file_path;
170 }
171
172 1170 inline void set_inode(const inode_t inode) { inode_ = inode; }
173 6940 inline void set_linkcount(const uint32_t linkcount) {
174
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6940 times.
6940 assert(linkcount > 0);
175 6940 linkcount_ = linkcount;
176 6940 }
177 inline void set_symlink(const LinkString &symlink) { symlink_ = symlink; }
178 10859 inline void set_has_xattrs(const bool has_xattrs) {
179 10859 has_xattrs_ = has_xattrs;
180 10859 }
181
182 7273 inline zlib::Algorithms compression_algorithm() const {
183 7273 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 1088 inline struct stat GetStatStructure() const {
191 struct stat s;
192 1088 memset(&s, 0, sizeof(s));
193 1088 s.st_dev = 1;
194 1088 s.st_ino = inode_;
195 1088 s.st_mode = mode_;
196 1088 s.st_nlink = linkcount();
197 1088 s.st_uid = uid();
198 1088 s.st_gid = gid();
199 1088 s.st_rdev = rdev();
200 1088 s.st_size = static_cast<off_t>(size());
201 1088 s.st_blksize = 4096; // will be ignored by Fuse
202 1088 s.st_blocks = static_cast<blkcnt_t>(1 + size() / 512);
203 1088 s.st_atime = mtime_;
204 1088 s.st_mtime = mtime_;
205 1088 s.st_ctime = mtime_;
206
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1088 times.
1088 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 1088 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 7566 inline explicit DirectoryEntry(const DirectoryEntryBase &base)
292 7566 : DirectoryEntryBase(base)
293 7566 , hardlink_group_(0)
294 7566 , is_nested_catalog_root_(false)
295 7566 , is_nested_catalog_mountpoint_(false)
296 7566 , is_bind_mountpoint_(false)
297 7566 , is_chunked_file_(false)
298 7566 , is_hidden_(false)
299 7566 , is_negative_(false) { }
300
301 18350779 inline DirectoryEntry()
302 36701558 : hardlink_group_(0)
303 18350779 , is_nested_catalog_root_(false)
304 18350779 , is_nested_catalog_mountpoint_(false)
305 18350779 , is_bind_mountpoint_(false)
306 18350779 , is_chunked_file_(false)
307 18350779 , is_hidden_(false)
308 18350779 , is_negative_(false) { }
309
310 1949 inline explicit DirectoryEntry(SpecialDirents special_type)
311 3898 : hardlink_group_(0)
312 1949 , is_nested_catalog_root_(false)
313 1949 , is_nested_catalog_mountpoint_(false)
314 1949 , is_bind_mountpoint_(false)
315 1949 , is_chunked_file_(false)
316 1949 , is_hidden_(false)
317 1949 , is_negative_(true) {
318
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1949 times.
1949 assert(special_type == kDirentNegative);
319 1949 }
320
321 518 inline SpecialDirents GetSpecial() const {
322
2/2
✓ Branch 0 taken 74 times.
✓ Branch 1 taken 444 times.
518 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 1943 inline bool IsNegative() const { return is_negative_; }
334 28563 inline bool IsNestedCatalogRoot() const { return is_nested_catalog_root_; }
335 25960 inline bool IsNestedCatalogMountpoint() const {
336 25960 return is_nested_catalog_mountpoint_;
337 }
338 18428 inline bool IsBindMountpoint() const { return is_bind_mountpoint_; }
339 40310 inline bool IsChunkedFile() const { return is_chunked_file_; }
340 22892 inline bool IsHidden() const { return is_hidden_; }
341 2112 inline uint32_t hardlink_group() const { return hardlink_group_; }
342
343 120 inline void set_hardlink_group(const uint32_t group) {
344 120 hardlink_group_ = group;
345 120 }
346 1066 inline void set_is_nested_catalog_mountpoint(const bool val) {
347 1066 is_nested_catalog_mountpoint_ = val;
348 1066 }
349 994 inline void set_is_nested_catalog_root(const bool val) {
350 994 is_nested_catalog_root_ = val;
351 994 }
352 inline void set_is_bind_mountpoint(const bool val) {
353 is_bind_mountpoint_ = val;
354 }
355 260 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 83 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