CernVM-FS  2.10.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
sync_item.h
Go to the documentation of this file.
1 
5 #ifndef CVMFS_SYNC_ITEM_H_
6 #define CVMFS_SYNC_ITEM_H_
7 
8 #include <sys/types.h>
9 
10 #if !defined(__APPLE__)
11 #include <sys/sysmacros.h>
12 #endif // __APPLE__
13 
14 #include <cstring>
15 #include <map>
16 #include <string>
17 
18 #include "directory_entry.h"
19 #include "duplex_libarchive.h"
20 #include "file_chunk.h"
21 #include "hash.h"
22 #include "platform.h"
23 #include "util/shared_ptr.h"
24 
25 class IngestionSource;
26 
27 namespace publish {
28 
40 };
41 
42 class SyncUnion;
56 class SyncItem {
57  // only SyncUnion can create SyncItems (see SyncUnion::CreateSyncItem).
58  // SyncUnionTarball can create SyncItemTar and SyncItemDummyDir.
59 
60  public:
61  SyncItem();
62  virtual ~SyncItem();
63 
64  inline bool IsDirectory() const { return IsType(kItemDir); }
65  inline bool WasDirectory() const { return WasType(kItemDir); }
66  inline bool IsRegularFile() const { return IsType(kItemFile); }
67  inline bool WasRegularFile() const { return WasType(kItemFile); }
68  inline bool IsSymlink() const { return IsType(kItemSymlink); }
69  inline bool WasSymlink() const { return WasType(kItemSymlink); }
70  inline bool IsNew() const { return WasType(kItemNew); }
71  inline bool IsTouched() const {
72  return (GetRdOnlyFiletype() == GetUnionFiletype()) &&
75  }
76  inline bool IsCharacterDevice() const { return IsType(kItemCharacterDevice); }
77  inline bool IsBlockDevice() const { return IsType(kItemBlockDevice); }
78  inline bool IsFifo() const { return IsType(kItemFifo); }
79  inline bool IsSocket() const { return IsType(kItemSocket); }
80  inline bool IsGraftMarker() const { return IsType(kItemMarker); }
81  inline bool IsExternalData() const { return external_data_; }
82  inline bool IsDirectIo() const { return direct_io_; }
83 
84  inline bool IsWhiteout() const { return whiteout_; }
85  inline bool IsCatalogMarker() const { return filename_ == ".cvmfscatalog"; }
86  inline bool IsOpaqueDirectory() const { return IsDirectory() && opaque_; }
87 
88  inline bool IsSpecialFile() const {
89  return IsCharacterDevice() || IsBlockDevice() || IsFifo() || IsSocket();
90  }
91  inline bool WasSpecialFile() const {
92  return WasType(kItemCharacterDevice) ||
94  WasType(kItemFifo) ||
96  }
97  inline bool IsBundleSpec() const {
98  return filename_ == ".cvmfsbundles";
99  }
100  inline bool WasBundleSpec() const {
101  return filename_ == ".cvmfsbundles";
102  }
103 
104  inline unsigned int GetRdevMajor() const {
106  StatUnion(true); return major(union_stat_.stat.st_rdev);
107  }
108 
109  inline unsigned int GetRdevMinor() const {
111  StatUnion(true); return minor(union_stat_.stat.st_rdev);
112  }
113 
114  bool HasCatalogMarker() const { return has_catalog_marker_; }
115  bool HasGraftMarker() const { return graft_marker_present_; }
117  bool IsValidGraft() const { return valid_graft_; }
118  bool IsChunkedGraft() const { return graft_chunklist_; }
119 
120  inline const FileChunkList* GetGraftChunks() const {return graft_chunklist_;}
121  inline shash::Any GetContentHash() const { return content_hash_; }
122  inline void SetContentHash(const shash::Any &hash) { content_hash_ = hash; }
123  inline bool HasContentHash() const { return !content_hash_.IsNull(); }
124  void SetExternalData(bool val) {external_data_ = val;}
125  void SetDirectIo(bool val) {direct_io_ = val;}
126 
128  return compression_algorithm_;
129  }
130  inline void SetCompressionAlgorithm(const zlib::Algorithms &alg) {
133  }
134 
142 
143  inline std::string GetRelativePath() const {
144  return (relative_parent_path_.empty()) ?
145  filename_ :
146  relative_parent_path_ + (filename_.empty() ? "" : ("/" + filename_));
147  }
148 
149  std::string GetRdOnlyPath() const;
150  std::string GetUnionPath() const;
151  std::string GetScratchPath() const;
152 
153  void MarkAsWhiteout(const std::string &actual_filename);
154  void MarkAsOpaqueDirectory();
155 
162  inline void MaskHardlink() { masked_hardlink_ = true; }
163  inline bool HasHardlinks() const {
164  return !masked_hardlink_ && GetUnionLinkcount() > 1;
165  }
166 
167  unsigned int GetRdOnlyLinkcount() const;
168  uint64_t GetRdOnlyInode() const;
169  unsigned int GetUnionLinkcount() const;
170  uint64_t GetUnionInode() const;
171  uint64_t GetScratchSize() const;
172  uint64_t GetRdOnlySize() const;
173 
174  inline std::string filename() const { return filename_; }
175  inline std::string relative_parent_path() const {
176  return relative_parent_path_;
177  }
178 
179  virtual IngestionSource *CreateIngestionSource() const = 0;
180  virtual void MakePlaceholderDirectory() const = 0;
182 
183  bool operator==(const SyncItem &other) const {
184  return ((relative_parent_path_ == other.relative_parent_path_) &&
185  (filename_ == other.filename_));
186  }
187 
188  protected:
199  SyncItem(const std::string &relative_parent_path,
200  const std::string &filename,
201  const SyncUnion *union_engine,
202  const SyncItemType entry_type);
203 
204  inline platform_stat64 GetUnionStat() const {
205  StatUnion();
206  return union_stat_.stat;
207  }
208 
211 
212  virtual SyncItemType GetScratchFiletype() const = 0;
213 
221  virtual bool IsType(const SyncItemType expected_type) const = 0;
222 
230  inline bool WasType(const SyncItemType expected_type) const {
231  if (rdonly_type_ == kItemUnknown) {
233  }
234  return rdonly_type_ == expected_type;
235  }
236 
240  struct EntryStat {
241  EntryStat() : obtained(false), error_code(0) {
242  memset(&stat, 0, sizeof(stat));
243  }
244 
245  inline SyncItemType GetSyncItemType() const {
246  assert(obtained);
247  if (S_ISREG(stat.st_mode)) return kItemFile;
248  if (S_ISLNK(stat.st_mode)) return kItemSymlink;
249  if (S_ISDIR(stat.st_mode)) return kItemDir;
250  if (S_ISFIFO(stat.st_mode)) return kItemFifo;
251  if (S_ISSOCK(stat.st_mode)) return kItemSocket;
252  if (S_ISCHR(stat.st_mode)) return kItemCharacterDevice;
253  if (S_ISBLK(stat.st_mode)) return kItemBlockDevice;
254  return kItemUnknown;
255  }
256 
257  bool obtained;
260  };
261 
262  static void StatGeneric(const std::string &path,
263  EntryStat *info,
264  const bool refresh);
265  SyncItemType GetGenericFiletype(const EntryStat &stat) const;
266  void CheckMarkerFiles();
267 
270 
271  ssize_t graft_size_;
272 
273  // The hash of regular file's content
275 
277 
278  private:
279  void CheckCatalogMarker();
280 
281  std::string filename_;
282 
283  std::string GetGraftMarkerPath() const;
284  void CheckGraft();
285 
290 
291  bool whiteout_;
292  bool opaque_;
301 
306 
307  // The compression algorithm for the file
309  // The compression algorithm has been set explicitly
311 
312  // Lazy evaluation and caching of results of file stats
313  inline void StatRdOnly(const bool refresh = false) const {
314  StatGeneric(GetRdOnlyPath(), &rdonly_stat_, refresh);
315  }
316  inline void StatUnion(const bool refresh = false) const {
317  StatGeneric(GetUnionPath(), &union_stat_, refresh);
318  }
319  virtual void StatScratch(const bool refresh) const = 0;
320 };
321 
322 typedef std::map<std::string, SharedPtr<SyncItem> > SyncItemList;
323 
324 class SyncItemNative : public SyncItem {
325  friend class SyncUnion;
327  virtual IngestionSource *CreateIngestionSource() const;
328  virtual void MakePlaceholderDirectory() const { assert(false); }
329  virtual SyncItemType GetScratchFiletype() const;
330  virtual bool IsType(const SyncItemType expected_type) const;
331  virtual void StatScratch(const bool refresh) const {
333  }
334 
335  protected:
337  const std::string &filename, const SyncUnion *union_engine,
338  const SyncItemType entry_type)
339  : SyncItem(relative_parent_path, filename, union_engine, entry_type) {
341  }
342 };
343 
344 } // namespace publish
345 
346 #endif // CVMFS_SYNC_ITEM_H_
EntryStat union_stat_
Definition: sync_item.h:289
bool WasType(const SyncItemType expected_type) const
Definition: sync_item.h:230
SyncItemType GetGenericFiletype(const EntryStat &stat) const
Definition: sync_item.cc:71
bool operator==(const SyncItem &other) const
Definition: sync_item.h:183
bool IsNull() const
Definition: hash.h:382
unsigned int GetRdevMinor() const
Definition: sync_item.h:109
struct stat64 platform_stat64
shash::Any GetContentHash() const
Definition: sync_item.h:121
SyncItemType rdonly_type_
Definition: sync_item.h:268
ssize_t graft_size_
Definition: sync_item.h:271
bool HasCatalogMarker() const
Definition: sync_item.h:114
unsigned int GetRdevMajor() const
Definition: sync_item.h:104
virtual catalog::DirectoryEntryBase CreateBasicCatalogDirent() const
Definition: sync_item.cc:204
const SyncUnion * union_engine_
Definition: sync_item.h:286
std::string relative_parent_path_
Definition: sync_item.h:300
bool IsSocket() const
Definition: sync_item.h:79
SyncItemNative(const std::string &relative_parent_path, const std::string &filename, const SyncUnion *union_engine, const SyncItemType entry_type)
Definition: sync_item.h:336
bool IsBlockDevice() const
Definition: sync_item.h:77
bool IsDirectory() const
Definition: sync_item.h:64
bool IsCharacterDevice() const
Definition: sync_item.h:76
virtual catalog::DirectoryEntryBase CreateBasicCatalogDirent() const =0
EntryStat()
Definition: sync_item.h:241
assert((mem||(size==0))&&"Out Of Memory")
void SetExternalData(bool val)
Definition: sync_item.h:124
void SetDirectIo(bool val)
Definition: sync_item.h:125
bool IsTouched() const
Definition: sync_item.h:71
bool IsGraftMarker() const
Definition: sync_item.h:80
void MaskHardlink()
Definition: sync_item.h:162
SyncItemType GetUnionFiletype() const
Definition: sync_item.cc:108
bool IsWhiteout() const
Definition: sync_item.h:84
bool IsValidGraft() const
Definition: sync_item.h:117
bool IsOpaqueDirectory() const
Definition: sync_item.h:86
uint64_t GetUnionInode() const
Definition: sync_item.cc:175
std::map< std::string, SharedPtr< SyncItem > > SyncItemList
Definition: sync_item.h:322
void CheckCatalogMarker()
Definition: sync_item.cc:270
SyncItemType scratch_type_
Definition: sync_item.h:276
void CheckMarkerFiles()
Definition: sync_item.cc:262
uint64_t GetRdOnlyInode() const
Definition: sync_item.cc:163
bool IsFifo() const
Definition: sync_item.h:78
virtual bool IsType(const SyncItemType expected_type) const
Definition: sync_item.cc:115
platform_stat64 stat
Definition: sync_item.h:259
Algorithms
Definition: compression.h:44
bool WasBundleSpec() const
Definition: sync_item.h:100
bool HasGraftMarker() const
Definition: sync_item.h:115
EntryStat rdonly_stat_
Definition: sync_item.h:288
bool HasContentHash() const
Definition: sync_item.h:123
virtual void MakePlaceholderDirectory() const =0
bool IsNew() const
Definition: sync_item.h:70
uint64_t GetScratchSize() const
Definition: sync_item.cc:180
bool IsRegularFile() const
Definition: sync_item.h:66
virtual bool IsType(const SyncItemType expected_type) const =0
platform_stat64 GetUnionStat() const
Definition: sync_item.h:204
std::string GetUnionPath() const
Definition: sync_item.cc:249
bool WasSymlink() const
Definition: sync_item.h:69
std::string GetRdOnlyPath() const
Definition: sync_item.cc:243
void StatRdOnly(const bool refresh=false) const
Definition: sync_item.h:313
zlib::Algorithms GetCompressionAlgorithm() const
Definition: sync_item.h:127
bool HasCompressionAlgorithm() const
Definition: sync_item.h:116
std::string GetScratchPath() const
Definition: sync_item.cc:255
std::string relative_parent_path() const
Definition: sync_item.h:175
void SetCompressionAlgorithm(const zlib::Algorithms &alg)
Definition: sync_item.h:130
virtual SyncItemType GetScratchFiletype() const =0
const FileChunkList * GetGraftChunks() const
Definition: sync_item.h:120
FileChunkList * graft_chunklist_
Definition: sync_item.h:305
bool WasSpecialFile() const
Definition: sync_item.h:91
EntryStat scratch_stat_
Definition: sync_item.h:269
virtual ~SyncItem()
Definition: sync_item.cc:66
unsigned int GetRdOnlyLinkcount() const
Definition: sync_item.cc:157
Definition: sync_item.h:240
shash::Any content_hash_
Definition: sync_item.h:274
void SetCatalogMarker()
Definition: sync_item.h:181
std::string filename_
Definition: sync_item.h:281
virtual SyncItemType GetScratchFiletype() const
Definition: sync_item.cc:98
bool IsDirectIo() const
Definition: sync_item.h:82
void MarkAsOpaqueDirectory()
Definition: sync_item.cc:151
bool IsBundleSpec() const
Definition: sync_item.h:97
bool has_compression_algorithm_
Definition: sync_item.h:310
SyncItemType
Definition: sync_item.h:29
virtual void StatScratch(const bool refresh) const =0
void SetContentHash(const shash::Any &hash)
Definition: sync_item.h:122
static void StatGeneric(const std::string &path, EntryStat *info, const bool refresh)
Definition: sync_item.cc:194
virtual void MakePlaceholderDirectory() const
Definition: sync_item.h:328
std::string GetGraftMarkerPath() const
Definition: sync_item.cc:286
virtual void StatScratch(const bool refresh) const
Definition: sync_item.h:331
bool has_catalog_marker_
Definition: sync_item.h:294
virtual IngestionSource * CreateIngestionSource() const =0
virtual IngestionSource * CreateIngestionSource() const
Definition: sync_item.cc:190
void MarkAsWhiteout(const std::string &actual_filename)
Definition: sync_item.cc:124
unsigned int GetUnionLinkcount() const
Definition: sync_item.cc:169
bool graft_marker_present_
Definition: sync_item.h:296
bool IsSymlink() const
Definition: sync_item.h:68
std::string filename() const
Definition: sync_item.h:174
bool IsExternalData() const
Definition: sync_item.h:81
bool WasRegularFile() const
Definition: sync_item.h:67
bool obtained
Definition: sync_item.h:257
bool IsCatalogMarker() const
Definition: sync_item.h:85
SyncItemType GetSyncItemType() const
Definition: sync_item.h:245
void StatUnion(const bool refresh=false) const
Definition: sync_item.h:316
bool IsSpecialFile() const
Definition: sync_item.h:88
bool IsChunkedGraft() const
Definition: sync_item.h:118
bool WasDirectory() const
Definition: sync_item.h:65
std::string GetRelativePath() const
Definition: sync_item.h:143
int error_code
Definition: sync_item.h:258
zlib::Algorithms compression_algorithm_
Definition: sync_item.h:308
uint64_t GetRdOnlySize() const
Definition: sync_item.cc:185
bool HasHardlinks() const
Definition: sync_item.h:163
SyncItemType GetRdOnlyFiletype() const
Definition: sync_item.cc:83