CernVM-FS  2.12.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
cache.h
Go to the documentation of this file.
1 
5 #ifndef CVMFS_CACHE_H_
6 #define CVMFS_CACHE_H_
7 
8 #ifndef __STDC_FORMAT_MACROS
9 #define __STDC_FORMAT_MACROS
10 #endif
11 
12 #include <stdint.h>
13 
14 #include <string>
15 
17 #include "crypto/hash.h"
18 #include "manifest.h"
19 #include "util/pointer.h"
20 
21 
22 class QuotaManager;
23 
31 };
32 
33 enum CacheModes {
36 };
37 
38 
68  public:
74  static const uint64_t kSizeUnknown;
75 
80  static const int kLabelCatalog = 0x01;
81  static const int kLabelPinned = 0x02;
82  static const int kLabelVolatile = 0x04;
83  static const int kLabelExternal = 0x08;
84  static const int kLabelChunked = 0x10;
85  static const int kLabelCertificate = 0x20;
86  static const int kLabelMetainfo = 0x40;
87  static const int kLabelHistory = 0x80;
88 
95  struct Label {
96  Label() : flags(0)
99  , range_offset(-1)
100  {}
101 
102  bool IsCatalog() const { return flags & kLabelCatalog; }
103  bool IsPinned() const { return flags & kLabelPinned; }
104  bool IsExternal() const { return flags & kLabelExternal; }
105  bool IsCertificate() const { return flags & kLabelCertificate; }
106 
110  std::string GetDescription() const {
111  if (flags & kLabelCatalog)
112  return "file catalog at " + path;
113  if (flags & kLabelCertificate)
114  return "certificate for " + path;
115  if (flags & kLabelMetainfo)
116  return "metainfo for " + path;
117  if (flags & kLabelHistory)
118  return "tag database for " + path;
119  if (flags & kLabelChunked)
120  return "Part of " + path;
121  return path;
122  }
123 
124  int flags;
125  uint64_t size;
133  std::string path;
134  };
135 
139  struct LabeledObject {
140  explicit LabeledObject(const shash::Any &id) : id(id), label() { }
141  LabeledObject(const shash::Any &id, const Label &l)
142  : id(id)
143  , label(l) { }
144 
147  };
148 
149  virtual CacheManagerIds id() = 0;
154  virtual std::string Describe() = 0;
155 
156  virtual bool AcquireQuotaManager(QuotaManager *quota_mgr) = 0;
157 
158  virtual ~CacheManager();
165  virtual int Open(const LabeledObject &object) = 0;
166  virtual int64_t GetSize(int fd) = 0;
167  virtual int Close(int fd) = 0;
168  virtual int64_t Pread(int fd, void *buf, uint64_t size, uint64_t offset) = 0;
169  virtual int Dup(int fd) = 0;
170  virtual int Readahead(int fd) = 0;
171 
172  virtual uint32_t SizeOfTxn() = 0;
173  virtual int StartTxn(const shash::Any &id, uint64_t size, void *txn) = 0;
174  virtual void CtrlTxn(const Label &label,
175  const int flags, // reserved for future use
176  void *txn) = 0;
177  virtual int64_t Write(const void *buf, uint64_t sz, void *txn) = 0;
178  virtual int Reset(void *txn) = 0;
179  virtual int AbortTxn(void *txn) = 0;
180  virtual int OpenFromTxn(void *txn) = 0;
181  virtual int CommitTxn(void *txn) = 0;
182 
183  virtual void Spawn() = 0;
184 
185  int ChecksumFd(int fd, shash::Any *id);
186  int OpenPinned(const LabeledObject &object);
187  bool Open2Mem(const LabeledObject &object,
188  unsigned char **buffer, uint64_t *size);
189  bool CommitFromMem(const LabeledObject &object,
190  const unsigned char *buffer,
191  const uint64_t size);
192 
194 
195  // Rescue the open file table during reload of the fuse module. For the
196  // POSIX cache, nothing needs to be done because the table is keep in the
197  // kernel for the process. Other cache managers need to do it manually.
198  void *SaveState(const int fd_progress);
205  int RestoreState(const int fd_progress, void *state);
206  void FreeState(const int fd_progress, void *state);
207  CacheManagerIds PeekState(void *state) {
208  return static_cast<State *>(state)->manager_type;
209  }
210 
217  virtual manifest::Breadcrumb LoadBreadcrumb(const std::string & /*fqrn*/) {
218  return manifest::Breadcrumb();
219  }
220  virtual bool StoreBreadcrumb(const manifest::Manifest &/*manifest*/) {
221  return false;
222  }
223 
224  protected:
225  CacheManager();
226 
227  // Unless overwritten, Saving/Restoring states will crash the Fuse module
228  virtual void *DoSaveState() { return NULL; }
229  virtual int DoRestoreState(void *data) { return false; }
230  virtual bool DoFreeState(void *data) { return false; }
231 
236 
237  private:
238  static const unsigned kStateVersion = 0;
239 
244  struct State : SingleCopy {
248  , concrete_state(NULL)
249  { }
250 
251  unsigned version;
254  };
255 }; // class CacheManager
256 
257 #endif // CVMFS_CACHE_H_
LabeledObject(const shash::Any &id, const Label &l)
Definition: cache.h:141
int ChecksumFd(int fd, shash::Any *id)
Definition: cache.cc:39
virtual int64_t GetSize(int fd)=0
std::string GetDescription() const
Definition: cache.h:110
off_t range_offset
Definition: cache.h:127
int RestoreState(const int fd_progress, void *state)
Definition: cache.cc:188
static const int kLabelCatalog
Definition: cache.h:80
static const int kLabelMetainfo
Definition: cache.h:86
virtual void Spawn()=0
bool IsCertificate() const
Definition: cache.h:105
int OpenPinned(const LabeledObject &object)
Definition: cache.cc:168
virtual ~CacheManager()
Definition: cache.cc:30
CacheManagerIds manager_type
Definition: cache.h:252
CacheModes
Definition: cache.h:33
virtual manifest::Breadcrumb LoadBreadcrumb(const std::string &)
Definition: cache.h:217
void * SaveState(const int fd_progress)
Definition: cache.cc:215
unsigned version
Definition: cache.h:251
LabeledObject(const shash::Any &id)
Definition: cache.h:140
virtual int AbortTxn(void *txn)=0
static const int kLabelHistory
Definition: cache.h:87
virtual bool StoreBreadcrumb(const manifest::Manifest &)
Definition: cache.h:220
virtual void * DoSaveState()
Definition: cache.h:228
virtual bool DoFreeState(void *data)
Definition: cache.h:230
virtual CacheManagerIds id()=0
void * concrete_state
Definition: cache.h:253
bool IsPinned() const
Definition: cache.h:103
virtual int Open(const LabeledObject &object)=0
virtual int DoRestoreState(void *data)
Definition: cache.h:229
Algorithms
Definition: compression.h:44
virtual int CommitTxn(void *txn)=0
uint64_t size
unzipped size, if known
Definition: cache.h:125
virtual int OpenFromTxn(void *txn)=0
static const int kLabelCertificate
Definition: cache.h:85
bool CommitFromMem(const LabeledObject &object, const unsigned char *buffer, const uint64_t size)
Definition: cache.cc:79
CacheManagerIds
Definition: cache.h:24
bool IsCatalog() const
Definition: cache.h:102
static const int kLabelPinned
Definition: cache.h:81
virtual int Close(int fd)=0
CacheManagerIds PeekState(void *state)
Definition: cache.h:207
static const int kLabelVolatile
Definition: cache.h:82
virtual int Reset(void *txn)=0
static const unsigned kStateVersion
Definition: cache.h:238
void FreeState(const int fd_progress, void *state)
Definition: cache.cc:99
zlib::Algorithms zip_algorithm
Definition: cache.h:126
CacheManager()
Definition: cache.cc:27
virtual void CtrlTxn(const Label &label, const int flags, void *txn)=0
QuotaManager * quota_mgr()
Definition: cache.h:193
bool IsExternal() const
Definition: cache.h:104
QuotaManager * quota_mgr_
Definition: cache.h:235
virtual uint32_t SizeOfTxn()=0
virtual int Dup(int fd)=0
virtual int Readahead(int fd)=0
std::string path
Definition: cache.h:133
bool Open2Mem(const LabeledObject &object, unsigned char **buffer, uint64_t *size)
Definition: cache.cc:126
static const int kLabelExternal
Definition: cache.h:83
virtual bool AcquireQuotaManager(QuotaManager *quota_mgr)=0
static const int kLabelChunked
Definition: cache.h:84
virtual int StartTxn(const shash::Any &id, uint64_t size, void *txn)=0
static void size_t size
Definition: smalloc.h:54
virtual int64_t Pread(int fd, void *buf, uint64_t size, uint64_t offset)=0
virtual std::string Describe()=0
virtual int64_t Write(const void *buf, uint64_t sz, void *txn)=0
static const uint64_t kSizeUnknown
Definition: cache.h:74