CernVM-FS  2.12.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
directory_entry.h
Go to the documentation of this file.
1 
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.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;
30 }
31 namespace swissknife {
32 class CommandMigrate;
33 }
34 
35 namespace catalog {
36 
37 // Create DirectoryEntries for unit test purposes.
38 class DirectoryEntryTestFactory;
39 
40 class MockCatalogManager;
41 class Catalog;
42 class WritableCatalogManager;
43 
44 template <class CatalogMgrT>
46 typedef uint64_t inode_t;
47 
51 };
52 
60  // For testing the catalog balancing
61  friend class CatalogBalancer<MockCatalogManager>;
62  // Create .cvmfscatalog and .cvmfsautocatalog files
64  // Simplify creation of DirectoryEntry objects for write back
65  friend class publish::SyncItem;
67  friend class publish::SyncItemTar;
70  // Simplify file system like _touch_ of DirectoryEntry objects
71  friend class SqlDirentTouch;
72  // Allow creation of virtual directories and files
73  friend class VirtualCatalog;
74 
75  public:
76  static const inode_t kInvalidInode = 0;
77 
82  struct Difference {
83  static const unsigned int kIdentical = 0x000;
84  static const unsigned int kName = 0x001;
85  static const unsigned int kLinkcount = 0x002;
86  static const unsigned int kSize = 0x004;
87  static const unsigned int kMode = 0x008;
88  static const unsigned int kMtime = 0x010;
89  static const unsigned int kSymlink = 0x020;
90  static const unsigned int kChecksum = 0x040;
91  static const unsigned int kHardlinkGroup = 0x080;
92  static const unsigned int kNestedCatalogTransitionFlags = 0x100;
93  static const unsigned int kChunkedFileFlag = 0x200;
94  static const unsigned int kHasXattrsFlag = 0x400;
95  static const unsigned int kExternalFileFlag = 0x800;
96  static const unsigned int kBindMountpointFlag = 0x1000;
97  static const unsigned int kHiddenFlag = 0x2000;
98  static const unsigned int kDirectIoFlag = 0x4000;
99  };
100  typedef unsigned int Differences;
101 
107  , mode_(0)
108  , uid_(0)
109  , gid_(0)
110  , size_(0)
111  , mtime_(0)
112  , linkcount_(1) // generally a normal file has linkcount 1 -> default
113  , has_xattrs_(false)
114  , is_external_file_(false)
115  , is_direct_io_(false)
117  { }
118 
119  inline bool IsRegular() const { return S_ISREG(mode_); }
120  inline bool IsLink() const { return S_ISLNK(mode_); }
121  inline bool IsDirectory() const { return S_ISDIR(mode_); }
122  inline bool IsFifo() const { return S_ISFIFO(mode_); }
123  inline bool IsSocket() const { return S_ISSOCK(mode_); }
124  inline bool IsCharDev() const { return S_ISCHR(mode_); }
125  inline bool IsBlockDev() const { return S_ISBLK(mode_); }
126  inline bool IsSpecial() const {
127  return IsFifo() || IsSocket() || IsCharDev() || IsBlockDev();
128  }
129  inline bool IsExternalFile() const { return is_external_file_; }
130  inline bool IsDirectIo() const { return is_direct_io_; }
131  inline bool HasXattrs() const { return has_xattrs_; }
132 
133  inline inode_t inode() const { return inode_; }
134  inline uint32_t linkcount() const { return linkcount_; }
135  inline NameString name() const { return name_; }
136  inline LinkString symlink() const { return symlink_; }
137  inline time_t mtime() const { return mtime_; }
138  inline unsigned int mode() const { return mode_; }
139  inline uid_t uid() const { return uid_; }
140  inline gid_t gid() const { return gid_; }
141  inline shash::Any checksum() const { return checksum_; }
142  inline const shash::Any *checksum_ptr() const { return &checksum_; }
144  return checksum_.algorithm;
145  }
146  inline uint64_t size() const {
147  if (IsLink())
148  return symlink().GetLength();
149  if (IsBlockDev() || IsCharDev())
150  return 0;
151  return size_;
152  }
153  inline dev_t rdev() const {
154  if (IsBlockDev() || IsCharDev())
155  return size_;
156  return 1;
157  }
158  inline std::string GetFullPath(const std::string &parent_directory) const {
159  std::string file_path = parent_directory + "/";
160  file_path.append(name().GetChars(), name().GetLength());
161  return file_path;
162  }
163 
164  inline void set_inode(const inode_t inode) { inode_ = inode; }
165  inline void set_linkcount(const uint32_t linkcount) {
166  assert(linkcount > 0);
168  }
169  inline void set_symlink(const LinkString &symlink) {
170  symlink_ = symlink;
171  }
172  inline void set_has_xattrs(const bool has_xattrs) {
173  has_xattrs_ = has_xattrs;
174  }
175 
177  return compression_algorithm_;
178  }
179 
184  inline struct stat GetStatStructure() const {
185  struct stat s;
186  memset(&s, 0, sizeof(s));
187  s.st_dev = 1;
188  s.st_ino = inode_;
189  s.st_mode = mode_;
190  s.st_nlink = linkcount();
191  s.st_uid = uid();
192  s.st_gid = gid();
193  s.st_rdev = rdev();
194  s.st_size = static_cast<off_t>(size());
195  s.st_blksize = 4096; // will be ignored by Fuse
196  s.st_blocks = static_cast<blkcnt_t>(1 + size() / 512);
197  s.st_atime = mtime_;
198  s.st_mtime = mtime_;
199  s.st_ctime = mtime_;
200  return s;
201  }
202 
203  Differences CompareTo(const DirectoryEntryBase &other) const;
204  inline bool operator ==(const DirectoryEntryBase &other) const {
205  return CompareTo(other) == Difference::kIdentical;
206  }
207  inline bool operator !=(const DirectoryEntryBase &other) const {
208  return !(*this == other);
209  }
210 
211  protected:
212  // Inodes are generated based on the rowid of the entry in the file catalog.
214 
215  // Data from struct stat
217  unsigned int mode_;
218  uid_t uid_;
219  gid_t gid_;
220  uint64_t size_;
221  time_t mtime_;
223  uint32_t linkcount_;
224  // In order to save memory, we only indicate if a directory entry has custom
225  // extended attributes. Another call to the file catalog is necessary to
226  // get them.
228 
229  // The cryptographic hash is not part of the file system intrinsics, though
230  // it can be computed just using the file contents. We therefore put it in
231  // this base class.
233 
236 
237  // The compression algorithm
239 };
240 
241 
253  // Simplify creation of DirectoryEntry objects
254  friend class SqlLookup;
255  // Simplify write of DirectoryEntry objects in database
256  friend class SqlDirentWrite;
257  // For fixing DirectoryEntry glitches
259  // TODO(rmeusel): remove this dependency
261  // Create DirectoryEntries for unit test purposes.
263 
264  public:
272  inline explicit DirectoryEntry(const DirectoryEntryBase& base)
273  : DirectoryEntryBase(base)
274  , hardlink_group_(0)
275  , is_nested_catalog_root_(false)
277  , is_bind_mountpoint_(false)
278  , is_chunked_file_(false)
279  , is_hidden_(false)
280  , is_negative_(false) { }
281 
282  inline DirectoryEntry()
283  : hardlink_group_(0)
284  , is_nested_catalog_root_(false)
286  , is_bind_mountpoint_(false)
287  , is_chunked_file_(false)
288  , is_hidden_(false)
289  , is_negative_(false) { }
290 
291  inline explicit DirectoryEntry(SpecialDirents special_type)
292  : hardlink_group_(0)
293  , is_nested_catalog_root_(false)
295  , is_bind_mountpoint_(false)
296  , is_chunked_file_(false)
297  , is_hidden_(false)
298  , is_negative_(true) { assert(special_type == kDirentNegative); }
299 
300  inline SpecialDirents GetSpecial() const {
302  }
303 
304  Differences CompareTo(const DirectoryEntry &other) const;
305  inline bool operator ==(const DirectoryEntry &other) const {
306  return CompareTo(other) == Difference::kIdentical;
307  }
308  inline bool operator !=(const DirectoryEntry &other) const {
309  return !(*this == other);
310  }
311 
312  inline bool IsNegative() const { return is_negative_; }
313  inline bool IsNestedCatalogRoot() const { return is_nested_catalog_root_; }
314  inline bool IsNestedCatalogMountpoint() const {
316  }
317  inline bool IsBindMountpoint() const { return is_bind_mountpoint_; }
318  inline bool IsChunkedFile() const { return is_chunked_file_; }
319  inline bool IsHidden() const { return is_hidden_; }
320  inline uint32_t hardlink_group() const { return hardlink_group_; }
321 
322  inline void set_hardlink_group(const uint32_t group) {
323  hardlink_group_ = group;
324  }
325  inline void set_is_nested_catalog_mountpoint(const bool val) {
327  }
328  inline void set_is_nested_catalog_root(const bool val) {
330  }
331  inline void set_is_bind_mountpoint(const bool val) {
332  is_bind_mountpoint_ = val;
333  }
334  inline void set_is_chunked_file(const bool val) {
335  is_chunked_file_ = val;
336  }
337  inline void set_is_hidden(const bool val) {
338  is_hidden_ = val;
339  }
340 
341  private:
346  uint32_t hardlink_group_;
347 
348  // TODO(jblomer): transform into bitfield to save memory
355 };
356 
357 
361 struct StatEntry {
363  struct stat info;
364 
365  StatEntry() { memset(&info, 0, sizeof(info)); }
366  StatEntry(const NameString &n, const struct stat &i) : name(n), info(i) { }
367 };
368 
369 
370 typedef std::vector<DirectoryEntry> DirectoryEntryList;
371 typedef std::vector<DirectoryEntryBase> DirectoryEntryBaseList;
372 // TODO(jblomer): use mmap for large listings
374 
375 } // namespace catalog
376 
377 #endif // CVMFS_DIRECTORY_ENTRY_H_
uint32_t linkcount() const
static const unsigned int kHardlinkGroup
bool IsExternalFile() const
void set_is_bind_mountpoint(const bool val)
struct stat info
DirectoryEntry(SpecialDirents special_type)
bool IsSpecial() const
Differences CompareTo(const DirectoryEntry &other) const
BigVector< StatEntry > StatEntryList
static const unsigned int kHiddenFlag
time_t mtime() const
void set_is_chunked_file(const bool val)
void set_inode(const inode_t inode)
bool IsDirectory() const
static const unsigned int kChecksum
bool IsSocket() const
bool IsHidden() const
bool IsChunkedFile() const
inode_t inode_
bool is_hidden_
SpecialDirents GetSpecial() const
uint64_t size() const
void set_linkcount(const uint32_t linkcount)
DirectoryEntryBase()
void set_is_nested_catalog_root(const bool val)
gid_t gid_
void set_symlink(const LinkString &symlink)
NameString name
static const unsigned int kDirectIoFlag
inode_t inode() const
uint64_t inode_t
assert((mem||(size==0))&&"Out Of Memory")
bool IsDirectIo() const
Algorithms algorithm
Definition: hash.h:125
shash::Any checksum() const
bool has_xattrs_
unsigned int mode() const
uint64_t size_
bool IsNestedCatalogMountpoint() const
Algorithms
Definition: hash.h:41
bool IsNestedCatalogRoot() const
NameString name_
std::vector< DirectoryEntry > DirectoryEntryList
zlib::Algorithms compression_algorithm_
NameString name() const
StatEntry()
bool IsLink() const
Algorithms
Definition: compression.h:44
static const unsigned int kExternalFileFlag
bool HasXattrs() const
friend class DirectoryEntryTestFactory
bool IsRegular() const
bool IsBlockDev() const
static const unsigned int kHasXattrsFlag
static const unsigned int kName
bool IsFifo() const
DirectoryEntry()
uint32_t linkcount_
bool is_nested_catalog_mountpoint_
bool is_external_file_
zlib::Algorithms compression_algorithm() const
void set_has_xattrs(const bool has_xattrs)
Differences CompareTo(const DirectoryEntryBase &other) const
bool is_negative_
bool operator!=(const DirectoryEntry &other) const
LinkString symlink() const
uint32_t hardlink_group_
time_t mtime_
static const unsigned int kNestedCatalogTransitionFlags
static const unsigned int kLinkcount
static const inode_t kInvalidInode
static const unsigned int kMtime
bool IsBindMountpoint() const
shash::Algorithms hash_algorithm() const
bool is_chunked_file_
bool IsCharDev() const
DirectoryEntry(const DirectoryEntryBase &base)
void set_is_hidden(const bool val)
StatEntry(const NameString &n, const struct stat &i)
unsigned int mode_
static const unsigned int kChunkedFileFlag
void set_hardlink_group(const uint32_t group)
static const unsigned int kSymlink
bool operator==(const DirectoryEntryBase &other) const
static const unsigned int kSize
std::string GetFullPath(const std::string &parent_directory) const
bool operator==(const DirectoryEntry &other) const
std::vector< DirectoryEntryBase > DirectoryEntryBaseList
uid_t uid() const
gid_t gid() const
unsigned GetLength() const
Definition: shortstring.h:131
static const unsigned int kMode
shash::Any checksum_
bool is_direct_io_
bool IsNegative() const
bool operator!=(const DirectoryEntryBase &other) const
static const unsigned int kBindMountpointFlag
bool is_nested_catalog_root_
bool is_bind_mountpoint_
LinkString symlink_
uint32_t hardlink_group() const
static const unsigned int kIdentical
void set_is_nested_catalog_mountpoint(const bool val)
unsigned int Differences
struct stat GetStatStructure() const
dev_t rdev() const
const shash::Any * checksum_ptr() const
uid_t uid_