| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/directory_entry.h |
| Date: | 2025-11-09 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 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 | 23043092 | inline DirectoryEntryBase() | |
| 112 | 23043092 | : inode_(kInvalidInode) | |
| 113 | 23043092 | , mode_(0) | |
| 114 | 23043092 | , uid_(0) | |
| 115 | 23043092 | , gid_(0) | |
| 116 | 23043092 | , size_(0) | |
| 117 | 23043092 | , mtime_(0) | |
| 118 | 23043092 | , mtime_ns_(-1) | |
| 119 | 23043092 | , linkcount_(1) // generally a normal file has linkcount 1 -> default | |
| 120 | 23043092 | , has_xattrs_(false) | |
| 121 | 23043092 | , is_external_file_(false) | |
| 122 | 23043092 | , is_direct_io_(false) | |
| 123 |
1/2✓ Branch 3 taken 23043092 times.
✗ Branch 4 not taken.
|
23043092 | , compression_algorithm_(zlib::kZlibDefault) { } |
| 124 | |||
| 125 | 44187 | inline bool IsRegular() const { return S_ISREG(mode_); } | |
| 126 | 46859 | inline bool IsLink() const { return S_ISLNK(mode_); } | |
| 127 | 48595 | inline bool IsDirectory() const { return S_ISDIR(mode_); } | |
| 128 | 18102 | inline bool IsFifo() const { return S_ISFIFO(mode_); } | |
| 129 | 17906 | inline bool IsSocket() const { return S_ISSOCK(mode_); } | |
| 130 | 42069 | inline bool IsCharDev() const { return S_ISCHR(mode_); } | |
| 131 | 42069 | inline bool IsBlockDev() const { return S_ISBLK(mode_); } | |
| 132 | 18102 | inline bool IsSpecial() const { | |
| 133 |
5/8✓ Branch 1 taken 17906 times.
✓ Branch 2 taken 196 times.
✓ Branch 4 taken 17906 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 17906 times.
✗ Branch 8 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 17906 times.
|
18102 | return IsFifo() || IsSocket() || IsCharDev() || IsBlockDev(); |
| 134 | } | ||
| 135 | 26637 | inline bool IsExternalFile() const { return is_external_file_; } | |
| 136 | 11491 | inline bool IsDirectIo() const { return is_direct_io_; } | |
| 137 | 32318 | inline bool HasXattrs() const { return has_xattrs_; } | |
| 138 | 26790 | inline bool HasMtimeNs() const { return mtime_ns_ >= 0; } | |
| 139 | |||
| 140 | 7897 | inline inode_t inode() const { return inode_; } | |
| 141 | 7000 | inline uint32_t linkcount() const { return linkcount_; } | |
| 142 | 55572 | inline NameString name() const { return name_; } | |
| 143 | 1050 | inline LinkString symlink() const { return symlink_; } | |
| 144 | 870 | inline time_t mtime() const { return mtime_; } | |
| 145 | 836 | inline int32_t mtime_ns() const { return mtime_ns_; } | |
| 146 | 870 | inline unsigned int mode() const { return mode_; } | |
| 147 | 1530 | inline uid_t uid() const { return uid_; } | |
| 148 | 1530 | inline gid_t gid() const { return gid_; } | |
| 149 | 6848 | inline shash::Any checksum() const { return checksum_; } | |
| 150 | 36042 | inline const shash::Any *checksum_ptr() const { return &checksum_; } | |
| 151 | ✗ | inline shash::Algorithms hash_algorithm() const { | |
| 152 | ✗ | return checksum_.algorithm; | |
| 153 | } | ||
| 154 | 23700 | inline uint64_t size() const { | |
| 155 |
2/2✓ Branch 1 taken 128 times.
✓ Branch 2 taken 23572 times.
|
23700 | if (IsLink()) |
| 156 | 128 | return symlink().GetLength(); | |
| 157 |
3/6✓ Branch 1 taken 23572 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 23572 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 23572 times.
|
23572 | if (IsBlockDev() || IsCharDev()) |
| 158 | ✗ | return 0; | |
| 159 | 23572 | return size_; | |
| 160 | } | ||
| 161 | 591 | inline dev_t rdev() const { | |
| 162 |
3/6✓ Branch 1 taken 591 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 591 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 591 times.
|
591 | if (IsBlockDev() || IsCharDev()) |
| 163 | ✗ | return size_; | |
| 164 | 591 | return 1; | |
| 165 | } | ||
| 166 | 20225 | inline std::string GetFullPath(const std::string &parent_directory) const { | |
| 167 | 20225 | std::string file_path = parent_directory + "/"; | |
| 168 |
3/6✓ Branch 1 taken 20225 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 20225 times.
✗ Branch 6 not taken.
✓ Branch 9 taken 20225 times.
✗ Branch 10 not taken.
|
20225 | file_path.append(name().GetChars(), name().GetLength()); |
| 169 | 20225 | return file_path; | |
| 170 | } | ||
| 171 | |||
| 172 | 1613 | inline void set_inode(const inode_t inode) { inode_ = inode; } | |
| 173 | 9108 | inline void set_linkcount(const uint32_t linkcount) { | |
| 174 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9108 times.
|
9108 | assert(linkcount > 0); |
| 175 | 9108 | linkcount_ = linkcount; | |
| 176 | 9108 | } | |
| 177 | ✗ | inline void set_symlink(const LinkString &symlink) { symlink_ = symlink; } | |
| 178 | 15730 | inline void set_has_xattrs(const bool has_xattrs) { | |
| 179 | 15730 | has_xattrs_ = has_xattrs; | |
| 180 | 15730 | } | |
| 181 | |||
| 182 | 10655 | inline zlib::Algorithms compression_algorithm() const { | |
| 183 | 10655 | 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 | 557 | inline struct stat GetStatStructure() const { | |
| 191 | struct stat s; | ||
| 192 | 557 | memset(&s, 0, sizeof(s)); | |
| 193 | 557 | s.st_dev = 1; | |
| 194 | 557 | s.st_ino = inode_; | |
| 195 | 557 | s.st_mode = mode_; | |
| 196 | 557 | s.st_nlink = linkcount(); | |
| 197 | 557 | s.st_uid = uid(); | |
| 198 | 557 | s.st_gid = gid(); | |
| 199 | 557 | s.st_rdev = rdev(); | |
| 200 | 557 | s.st_size = static_cast<off_t>(size()); | |
| 201 | 557 | s.st_blksize = 4096; // will be ignored by Fuse | |
| 202 | 557 | s.st_blocks = static_cast<blkcnt_t>(1 + size() / 512); | |
| 203 | 557 | s.st_atime = mtime_; | |
| 204 | 557 | s.st_mtime = mtime_; | |
| 205 | 557 | s.st_ctime = mtime_; | |
| 206 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 557 times.
|
557 | 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 | 557 | 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 | 9379 | inline explicit DirectoryEntry(const DirectoryEntryBase &base) | |
| 292 | 9379 | : DirectoryEntryBase(base) | |
| 293 | 9379 | , hardlink_group_(0) | |
| 294 | 9379 | , is_nested_catalog_root_(false) | |
| 295 | 9379 | , is_nested_catalog_mountpoint_(false) | |
| 296 | 9379 | , is_bind_mountpoint_(false) | |
| 297 | 9379 | , is_chunked_file_(false) | |
| 298 | 9379 | , is_hidden_(false) | |
| 299 | 9379 | , is_negative_(false) { } | |
| 300 | |||
| 301 | 23040282 | inline DirectoryEntry() | |
| 302 | 46080564 | : hardlink_group_(0) | |
| 303 | 23040282 | , is_nested_catalog_root_(false) | |
| 304 | 23040282 | , is_nested_catalog_mountpoint_(false) | |
| 305 | 23040282 | , is_bind_mountpoint_(false) | |
| 306 | 23040282 | , is_chunked_file_(false) | |
| 307 | 23040282 | , is_hidden_(false) | |
| 308 | 23040282 | , is_negative_(false) { } | |
| 309 | |||
| 310 | 2714 | inline explicit DirectoryEntry(SpecialDirents special_type) | |
| 311 | 5428 | : hardlink_group_(0) | |
| 312 | 2714 | , is_nested_catalog_root_(false) | |
| 313 | 2714 | , is_nested_catalog_mountpoint_(false) | |
| 314 | 2714 | , is_bind_mountpoint_(false) | |
| 315 | 2714 | , is_chunked_file_(false) | |
| 316 | 2714 | , is_hidden_(false) | |
| 317 | 2714 | , is_negative_(true) { | |
| 318 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2714 times.
|
2714 | assert(special_type == kDirentNegative); |
| 319 | 2714 | } | |
| 320 | |||
| 321 | 246 | inline SpecialDirents GetSpecial() const { | |
| 322 |
2/2✓ Branch 0 taken 35 times.
✓ Branch 1 taken 211 times.
|
246 | 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 | 2842 | inline bool IsNegative() const { return is_negative_; } | |
| 334 | 42080 | inline bool IsNestedCatalogRoot() const { return is_nested_catalog_root_; } | |
| 335 | 33620 | inline bool IsNestedCatalogMountpoint() const { | |
| 336 | 33620 | return is_nested_catalog_mountpoint_; | |
| 337 | } | ||
| 338 | 23832 | inline bool IsBindMountpoint() const { return is_bind_mountpoint_; } | |
| 339 | 58283 | inline bool IsChunkedFile() const { return is_chunked_file_; } | |
| 340 | 28105 | inline bool IsHidden() const { return is_hidden_; } | |
| 341 | 836 | inline uint32_t hardlink_group() const { return hardlink_group_; } | |
| 342 | |||
| 343 | 490 | inline void set_hardlink_group(const uint32_t group) { | |
| 344 | 490 | hardlink_group_ = group; | |
| 345 | 490 | } | |
| 346 | 2391 | inline void set_is_nested_catalog_mountpoint(const bool val) { | |
| 347 | 2391 | is_nested_catalog_mountpoint_ = val; | |
| 348 | 2391 | } | |
| 349 | 2274 | inline void set_is_nested_catalog_root(const bool val) { | |
| 350 | 2274 | is_nested_catalog_root_ = val; | |
| 351 | 2274 | } | |
| 352 | ✗ | inline void set_is_bind_mountpoint(const bool val) { | |
| 353 | ✗ | is_bind_mountpoint_ = val; | |
| 354 | } | ||
| 355 | 840 | 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 | 63 | 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 |