Directory: | cvmfs/ |
---|---|
File: | cvmfs/directory_entry.h |
Date: | 2025-06-29 02:35:41 |
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 | 26851338 | inline DirectoryEntryBase() | |
112 | 26851338 | : inode_(kInvalidInode) | |
113 | 26851338 | , mode_(0) | |
114 | 26851338 | , uid_(0) | |
115 | 26851338 | , gid_(0) | |
116 | 26851338 | , size_(0) | |
117 | 26851338 | , mtime_(0) | |
118 | 26851338 | , mtime_ns_(-1) | |
119 | 26851338 | , linkcount_(1) // generally a normal file has linkcount 1 -> default | |
120 | 26851338 | , has_xattrs_(false) | |
121 | 26851338 | , is_external_file_(false) | |
122 | 26851338 | , is_direct_io_(false) | |
123 |
1/2✓ Branch 3 taken 26851338 times.
✗ Branch 4 not taken.
|
26851338 | , compression_algorithm_(zlib::kZlibDefault) { } |
124 | |||
125 | 21877 | inline bool IsRegular() const { return S_ISREG(mode_); } | |
126 | 24588 | inline bool IsLink() const { return S_ISLNK(mode_); } | |
127 | 22806 | inline bool IsDirectory() const { return S_ISDIR(mode_); } | |
128 | 9220 | inline bool IsFifo() const { return S_ISFIFO(mode_); } | |
129 | 9164 | inline bool IsSocket() const { return S_ISSOCK(mode_); } | |
130 | 22916 | inline bool IsCharDev() const { return S_ISCHR(mode_); } | |
131 | 22916 | inline bool IsBlockDev() const { return S_ISBLK(mode_); } | |
132 | 9220 | inline bool IsSpecial() const { | |
133 |
5/8✓ Branch 1 taken 9164 times.
✓ Branch 2 taken 56 times.
✓ Branch 4 taken 9164 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 9164 times.
✗ Branch 8 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 9164 times.
|
9220 | return IsFifo() || IsSocket() || IsCharDev() || IsBlockDev(); |
134 | } | ||
135 | 12236 | inline bool IsExternalFile() const { return is_external_file_; } | |
136 | 5333 | inline bool IsDirectIo() const { return is_direct_io_; } | |
137 | 14222 | inline bool HasXattrs() const { return has_xattrs_; } | |
138 | 14210 | inline bool HasMtimeNs() const { return mtime_ns_ >= 0; } | |
139 | |||
140 | 1052 | inline inode_t inode() const { return inode_; } | |
141 | 3803 | inline uint32_t linkcount() const { return linkcount_; } | |
142 | 26147 | inline NameString name() const { return name_; } | |
143 | 308 | inline LinkString symlink() const { return symlink_; } | |
144 | 170 | inline time_t mtime() const { return mtime_; } | |
145 | 88 | inline int32_t mtime_ns() const { return mtime_ns_; } | |
146 | 170 | inline unsigned int mode() const { return mode_; } | |
147 | 1419 | inline uid_t uid() const { return uid_; } | |
148 | 1419 | inline gid_t gid() const { return gid_; } | |
149 | 3092 | inline shash::Any checksum() const { return checksum_; } | |
150 | 17888 | inline const shash::Any *checksum_ptr() const { return &checksum_; } | |
151 | ✗ | inline shash::Algorithms hash_algorithm() const { | |
152 | ✗ | return checksum_.algorithm; | |
153 | } | ||
154 | 12564 | inline uint64_t size() const { | |
155 |
2/2✓ Branch 1 taken 112 times.
✓ Branch 2 taken 12452 times.
|
12564 | if (IsLink()) |
156 | 112 | return symlink().GetLength(); | |
157 |
3/6✓ Branch 1 taken 12452 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 12452 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 12452 times.
|
12452 | if (IsBlockDev() || IsCharDev()) |
158 | ✗ | return 0; | |
159 | 12452 | return size_; | |
160 | } | ||
161 | 1300 | inline dev_t rdev() const { | |
162 |
3/6✓ Branch 1 taken 1300 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1300 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 1300 times.
|
1300 | if (IsBlockDev() || IsCharDev()) |
163 | ✗ | return size_; | |
164 | 1300 | return 1; | |
165 | } | ||
166 | 9797 | inline std::string GetFullPath(const std::string &parent_directory) const { | |
167 | 9797 | std::string file_path = parent_directory + "/"; | |
168 |
3/6✓ Branch 1 taken 9797 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 9797 times.
✗ Branch 6 not taken.
✓ Branch 9 taken 9797 times.
✗ Branch 10 not taken.
|
9797 | file_path.append(name().GetChars(), name().GetLength()); |
169 | 9797 | return file_path; | |
170 | } | ||
171 | |||
172 | 274 | inline void set_inode(const inode_t inode) { inode_ = inode; } | |
173 | 4616 | inline void set_linkcount(const uint32_t linkcount) { | |
174 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4616 times.
|
4616 | assert(linkcount > 0); |
175 | 4616 | linkcount_ = linkcount; | |
176 | 4616 | } | |
177 | ✗ | inline void set_symlink(const LinkString &symlink) { symlink_ = symlink; } | |
178 | 8346 | inline void set_has_xattrs(const bool has_xattrs) { | |
179 | 8346 | has_xattrs_ = has_xattrs; | |
180 | 8346 | } | |
181 | |||
182 | 5245 | inline zlib::Algorithms compression_algorithm() const { | |
183 | 5245 | 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 | 1218 | inline struct stat GetStatStructure() const { | |
191 | struct stat s; | ||
192 | 1218 | memset(&s, 0, sizeof(s)); | |
193 | 1218 | s.st_dev = 1; | |
194 | 1218 | s.st_ino = inode_; | |
195 | 1218 | s.st_mode = mode_; | |
196 | 1218 | s.st_nlink = linkcount(); | |
197 | 1218 | s.st_uid = uid(); | |
198 | 1218 | s.st_gid = gid(); | |
199 | 1218 | s.st_rdev = rdev(); | |
200 | 1218 | s.st_size = static_cast<off_t>(size()); | |
201 | 1218 | s.st_blksize = 4096; // will be ignored by Fuse | |
202 | 1218 | s.st_blocks = static_cast<blkcnt_t>(1 + size() / 512); | |
203 | 1218 | s.st_atime = mtime_; | |
204 | 1218 | s.st_mtime = mtime_; | |
205 | 1218 | s.st_ctime = mtime_; | |
206 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1218 times.
|
1218 | 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 | 1218 | 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 | 4912 | inline explicit DirectoryEntry(const DirectoryEntryBase &base) | |
292 | 4912 | : DirectoryEntryBase(base) | |
293 | 4912 | , hardlink_group_(0) | |
294 | 4912 | , is_nested_catalog_root_(false) | |
295 | 4912 | , is_nested_catalog_mountpoint_(false) | |
296 | 4912 | , is_bind_mountpoint_(false) | |
297 | 4912 | , is_chunked_file_(false) | |
298 | 4912 | , is_hidden_(false) | |
299 | 4912 | , is_negative_(false) { } | |
300 | |||
301 | 26849333 | inline DirectoryEntry() | |
302 | 53698666 | : hardlink_group_(0) | |
303 | 26849333 | , is_nested_catalog_root_(false) | |
304 | 26849333 | , is_nested_catalog_mountpoint_(false) | |
305 | 26849333 | , is_bind_mountpoint_(false) | |
306 | 26849333 | , is_chunked_file_(false) | |
307 | 26849333 | , is_hidden_(false) | |
308 | 26849333 | , is_negative_(false) { } | |
309 | |||
310 | 1985 | inline explicit DirectoryEntry(SpecialDirents special_type) | |
311 | 3970 | : hardlink_group_(0) | |
312 | 1985 | , is_nested_catalog_root_(false) | |
313 | 1985 | , is_nested_catalog_mountpoint_(false) | |
314 | 1985 | , is_bind_mountpoint_(false) | |
315 | 1985 | , is_chunked_file_(false) | |
316 | 1985 | , is_hidden_(false) | |
317 | 1985 | , is_negative_(true) { | |
318 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1985 times.
|
1985 | assert(special_type == kDirentNegative); |
319 | 1985 | } | |
320 | |||
321 | 563 | inline SpecialDirents GetSpecial() const { | |
322 |
2/2✓ Branch 0 taken 81 times.
✓ Branch 1 taken 482 times.
|
563 | 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 | 1521 | inline bool IsNegative() const { return is_negative_; } | |
334 | 19347 | inline bool IsNestedCatalogRoot() const { return is_nested_catalog_root_; } | |
335 | 15458 | inline bool IsNestedCatalogMountpoint() const { | |
336 | 15458 | return is_nested_catalog_mountpoint_; | |
337 | } | ||
338 | 12028 | inline bool IsBindMountpoint() const { return is_bind_mountpoint_; } | |
339 | 26927 | inline bool IsChunkedFile() const { return is_chunked_file_; } | |
340 | 13487 | inline bool IsHidden() const { return is_hidden_; } | |
341 | 88 | inline uint32_t hardlink_group() const { return hardlink_group_; } | |
342 | |||
343 | 90 | inline void set_hardlink_group(const uint32_t group) { | |
344 | 90 | hardlink_group_ = group; | |
345 | 90 | } | |
346 | 674 | inline void set_is_nested_catalog_mountpoint(const bool val) { | |
347 | 674 | is_nested_catalog_mountpoint_ = val; | |
348 | 674 | } | |
349 | 653 | inline void set_is_nested_catalog_root(const bool val) { | |
350 | 653 | is_nested_catalog_root_ = val; | |
351 | 653 | } | |
352 | ✗ | inline void set_is_bind_mountpoint(const bool val) { | |
353 | ✗ | is_bind_mountpoint_ = val; | |
354 | } | ||
355 | 1200 | 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 | 113 | 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 |