CernVM-FS  2.12.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 "crypto/hash.h"
19 #include "directory_entry.h"
20 #include "duplex_libarchive.h"
21 #include "file_chunk.h"
22 #include "util/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 
147  bool enable_mtime_ns) const = 0;
148 
149  inline std::string GetRelativePath() const {
150  return (relative_parent_path_.empty()) ?
151  filename_ :
152  relative_parent_path_ + (filename_.empty() ? "" : ("/" + filename_));
153  }
154 
155  std::string GetRdOnlyPath() const;
156  std::string GetUnionPath() const;
157  std::string GetScratchPath() const;
158 
159  void MarkAsWhiteout(const std::string &actual_filename);
160  void MarkAsOpaqueDirectory();
161 
168  inline void MaskHardlink() { masked_hardlink_ = true; }
169  inline bool HasHardlinks() const {
170  return !masked_hardlink_ && GetUnionLinkcount() > 1;
171  }
172 
173  unsigned int GetRdOnlyLinkcount() const;
174  uint64_t GetRdOnlyInode() const;
175  unsigned int GetUnionLinkcount() const;
176  uint64_t GetUnionInode() const;
177  uint64_t GetScratchSize() const;
178  uint64_t GetRdOnlySize() const;
179 
180  inline std::string filename() const { return filename_; }
181  inline std::string relative_parent_path() const {
182  return relative_parent_path_;
183  }
184 
185  virtual IngestionSource *CreateIngestionSource() const = 0;
186  virtual void MakePlaceholderDirectory() const = 0;
188 
189  bool operator==(const SyncItem &other) const {
190  return ((relative_parent_path_ == other.relative_parent_path_) &&
191  (filename_ == other.filename_));
192  }
193 
194  protected:
205  SyncItem(const std::string &relative_parent_path,
206  const std::string &filename,
207  const SyncUnion *union_engine,
208  const SyncItemType entry_type);
209 
210  inline platform_stat64 GetUnionStat() const {
211  StatUnion();
212  return union_stat_.stat;
213  }
214 
217 
218  virtual SyncItemType GetScratchFiletype() const = 0;
219 
227  virtual bool IsType(const SyncItemType expected_type) const = 0;
228 
236  inline bool WasType(const SyncItemType expected_type) const {
237  if (rdonly_type_ == kItemUnknown) {
239  }
240  return rdonly_type_ == expected_type;
241  }
242 
246  struct EntryStat {
247  EntryStat() : obtained(false), error_code(0) {
248  memset(&stat, 0, sizeof(stat));
249  }
250 
251  inline SyncItemType GetSyncItemType() const {
252  assert(obtained);
253  if (S_ISREG(stat.st_mode)) return kItemFile;
254  if (S_ISLNK(stat.st_mode)) return kItemSymlink;
255  if (S_ISDIR(stat.st_mode)) return kItemDir;
256  if (S_ISFIFO(stat.st_mode)) return kItemFifo;
257  if (S_ISSOCK(stat.st_mode)) return kItemSocket;
258  if (S_ISCHR(stat.st_mode)) return kItemCharacterDevice;
259  if (S_ISBLK(stat.st_mode)) return kItemBlockDevice;
260  return kItemUnknown;
261  }
262 
263  bool obtained;
266  };
267 
268  static void StatGeneric(const std::string &path,
269  EntryStat *info,
270  const bool refresh);
271  SyncItemType GetGenericFiletype(const EntryStat &stat) const;
272  void CheckMarkerFiles();
273 
276 
277  ssize_t graft_size_;
278 
279  // The hash of regular file's content
281 
283 
284  private:
285  void CheckCatalogMarker();
286 
287  std::string filename_;
288 
289  std::string GetGraftMarkerPath() const;
290  void CheckGraft();
291 
296 
297  bool whiteout_;
298  bool opaque_;
307 
312 
313  // The compression algorithm for the file
315  // The compression algorithm has been set explicitly
317 
318  // Lazy evaluation and caching of results of file stats
319  inline void StatRdOnly(const bool refresh = false) const {
320  StatGeneric(GetRdOnlyPath(), &rdonly_stat_, refresh);
321  }
322  inline void StatUnion(const bool refresh = false) const {
323  StatGeneric(GetUnionPath(), &union_stat_, refresh);
324  }
325  virtual void StatScratch(const bool refresh) const = 0;
326 };
327 
328 typedef std::map<std::string, SharedPtr<SyncItem> > SyncItemList;
329 
330 class SyncItemNative : public SyncItem {
331  friend class SyncUnion;
333  bool enable_mtime_ns) const;
334  virtual IngestionSource *CreateIngestionSource() const;
335  virtual void MakePlaceholderDirectory() const { assert(false); }
336  virtual SyncItemType GetScratchFiletype() const;
337  virtual bool IsType(const SyncItemType expected_type) const;
338  virtual void StatScratch(const bool refresh) const {
340  }
341 
342  protected:
344  const std::string &filename, const SyncUnion *union_engine,
345  const SyncItemType entry_type)
346  : SyncItem(relative_parent_path, filename, union_engine, entry_type) {
348  }
349 };
350 
351 } // namespace publish
352 
353 #endif // CVMFS_SYNC_ITEM_H_
EntryStat union_stat_
Definition: sync_item.h:295
bool WasType(const SyncItemType expected_type) const
Definition: sync_item.h:236
SyncItemType GetGenericFiletype(const EntryStat &stat) const
Definition: sync_item.cc:71
bool operator==(const SyncItem &other) const
Definition: sync_item.h:189
bool IsNull() const
Definition: hash.h:383
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:274
ssize_t graft_size_
Definition: sync_item.h:277
bool HasCatalogMarker() const
Definition: sync_item.h:114
unsigned int GetRdevMajor() const
Definition: sync_item.h:104
virtual catalog::DirectoryEntryBase CreateBasicCatalogDirent(bool enable_mtime_ns) const
Definition: sync_item.cc:204
const SyncUnion * union_engine_
Definition: sync_item.h:292
std::string relative_parent_path_
Definition: sync_item.h:306
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:343
bool IsBlockDevice() const
Definition: sync_item.h:77
bool IsDirectory() const
Definition: sync_item.h:64
bool IsCharacterDevice() const
Definition: sync_item.h:76
EntryStat()
Definition: sync_item.h:247
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:168
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:328
void CheckCatalogMarker()
Definition: sync_item.cc:282
SyncItemType scratch_type_
Definition: sync_item.h:282
void CheckMarkerFiles()
Definition: sync_item.cc:274
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:265
virtual catalog::DirectoryEntryBase CreateBasicCatalogDirent(bool enable_mtime_ns) const =0
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:294
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:210
std::string GetUnionPath() const
Definition: sync_item.cc:261
bool WasSymlink() const
Definition: sync_item.h:69
std::string GetRdOnlyPath() const
Definition: sync_item.cc:255
void StatRdOnly(const bool refresh=false) const
Definition: sync_item.h:319
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:267
std::string relative_parent_path() const
Definition: sync_item.h:181
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:311
bool WasSpecialFile() const
Definition: sync_item.h:91
EntryStat scratch_stat_
Definition: sync_item.h:275
virtual ~SyncItem()
Definition: sync_item.cc:66
unsigned int GetRdOnlyLinkcount() const
Definition: sync_item.cc:157
Definition: sync_item.h:246
shash::Any content_hash_
Definition: sync_item.h:280
void SetCatalogMarker()
Definition: sync_item.h:187
std::string filename_
Definition: sync_item.h:287
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:316
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:335
std::string GetGraftMarkerPath() const
Definition: sync_item.cc:298
virtual void StatScratch(const bool refresh) const
Definition: sync_item.h:338
bool has_catalog_marker_
Definition: sync_item.h:300
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:302
bool IsSymlink() const
Definition: sync_item.h:68
std::string filename() const
Definition: sync_item.h:180
bool IsExternalData() const
Definition: sync_item.h:81
bool WasRegularFile() const
Definition: sync_item.h:67
bool obtained
Definition: sync_item.h:263
bool IsCatalogMarker() const
Definition: sync_item.h:85
SyncItemType GetSyncItemType() const
Definition: sync_item.h:251
void StatUnion(const bool refresh=false) const
Definition: sync_item.h:322
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:149
int error_code
Definition: sync_item.h:264
zlib::Algorithms compression_algorithm_
Definition: sync_item.h:314
uint64_t GetRdOnlySize() const
Definition: sync_item.cc:185
bool HasHardlinks() const
Definition: sync_item.h:169
SyncItemType GetRdOnlyFiletype() const
Definition: sync_item.cc:83