GCC Code Coverage Report
Directory: cvmfs/ Exec Total Coverage
File: cvmfs/cache_posix.h Lines: 13 16 81.3 %
Date: 2019-02-03 02:48:13 Branches: 1 2 50.0 %

Line Branch Exec Source
1
/**
2
 * This file is part of the CernVM File System.
3
 */
4
5
#ifndef CVMFS_CACHE_POSIX_H_
6
#define CVMFS_CACHE_POSIX_H_
7
8
#include <stdint.h>
9
#include <sys/types.h>
10
11
#include <map>
12
#include <string>
13
#include <vector>
14
15
#include "atomic.h"
16
#include "backoff.h"
17
#include "cache.h"
18
#include "catalog_mgr.h"
19
#include "file_chunk.h"
20
#include "gtest/gtest_prod.h"
21
#include "manifest_fetch.h"
22
#include "shortstring.h"
23
#include "signature.h"
24
#include "statistics.h"
25
26
namespace catalog {
27
class DirectoryEntry;
28
class Catalog;
29
}
30
31
namespace download {
32
class DownloadManager;
33
}
34
35
/**
36
 * Cache manger implementation using a file system (cache directory) as a
37
 * backing storage.
38
 */
39
class PosixCacheManager : public CacheManager {
40
  FRIEND_TEST(T_CacheManager, CommitTxnQuotaNotifications);
41
  FRIEND_TEST(T_CacheManager, CommitTxnRenameFail);
42
  FRIEND_TEST(T_CacheManager, Open);
43
  FRIEND_TEST(T_CacheManager, OpenFromTxn);
44
  FRIEND_TEST(T_CacheManager, OpenPinned);
45
  FRIEND_TEST(T_CacheManager, Rename);
46
  FRIEND_TEST(T_CacheManager, StartTxn);
47
  FRIEND_TEST(T_CacheManager, TearDown2ReadOnly);
48
49
 public:
50
  enum CacheModes {
51
    kCacheReadWrite = 0,
52
    kCacheReadOnly,
53
  };
54
55
  enum RenameWorkarounds {
56
    kRenameNormal = 0,
57
    kRenameLink,
58
    kRenameSamedir
59
  };
60
61
  /**
62
   * As of 25M, a file is considered a "big file", which means it is dangerous
63
   * to apply asynchronous semantics.  On start of a transaction with a big file
64
   * the cache is cleaned up opportunistically.
65
   */
66
  static const uint64_t kBigFile;
67
68
11
  virtual CacheManagerIds id() { return kPosixCacheManager; }
69
  virtual std::string Describe();
70
71
  static PosixCacheManager *Create(
72
    const std::string &cache_path,
73
    const bool alien_cache,
74
    const RenameWorkarounds rename_workaround = kRenameNormal);
75
466
  virtual ~PosixCacheManager() { }
76
  virtual bool AcquireQuotaManager(QuotaManager *quota_mgr);
77
78
  virtual int Open(const BlessedObject &object);
79
  virtual int64_t GetSize(int fd);
80
  virtual int Close(int fd);
81
  virtual int64_t Pread(int fd, void *buf, uint64_t size, uint64_t offset);
82
  virtual int Dup(int fd);
83
  virtual int Readahead(int fd);
84
85
205
  virtual uint32_t SizeOfTxn() { return sizeof(Transaction); }
86
  virtual int StartTxn(const shash::Any &id, uint64_t size, void *txn);
87
  virtual void CtrlTxn(const ObjectInfo &object_info,
88
                       const int flags,
89
                       void *txn);
90
  virtual int64_t Write(const void *buf, uint64_t size, void *txn);
91
  virtual int Reset(void *txn);
92
  virtual int OpenFromTxn(void *txn);
93
  virtual int AbortTxn(void *txn);
94
  virtual int CommitTxn(void *txn);
95
96
  virtual void Spawn() { }
97
98
88
  virtual std::string GetBackingDirectory() { return cache_path_; }
99
100
  void TearDown2ReadOnly();
101
  CacheModes cache_mode() { return cache_mode_; }
102
  bool alien_cache() { return alien_cache_; }
103
9
  std::string cache_path() { return cache_path_; }
104
105
 protected:
106
  virtual void *DoSaveState();
107
  virtual bool DoRestoreState(void *data);
108
  virtual bool DoFreeState(void *data);
109
110
 private:
111
222
  struct Transaction {
112
222
    Transaction(const shash::Any &id, const std::string &final_path)
113
      : buf_pos(0)
114
      , size(0)
115
      , expected_size(kSizeUnknown)
116
      , fd(-1)
117
      , object_info(kTypeRegular, "")
118
      , tmp_path()
119
      , final_path(final_path)
120
222
      , id(id)
121
222
    { }
122
123
    unsigned char buffer[4096];
124
    unsigned buf_pos;
125
    uint64_t size;
126
    uint64_t expected_size;
127
    int fd;
128
    ObjectInfo object_info;
129
    std::string tmp_path;
130
    std::string final_path;
131
    shash::Any id;
132
  };
133
134
236
  PosixCacheManager(const std::string &cache_path, const bool alien_cache)
135
    : cache_path_(cache_path)
136
    , txn_template_path_(cache_path_ + "/txn/fetchXXXXXX")
137
    , alien_cache_(alien_cache)
138
    , rename_workaround_(kRenameNormal)
139
    , cache_mode_(kCacheReadWrite)
140
236
    , reports_correct_filesize_(true)
141
  {
142
236
    atomic_init32(&no_inflight_txns_);
143
236
  }
144
145
  std::string GetPathInCache(const shash::Any &id);
146
  int Rename(const char *oldpath, const char *newpath);
147
  int Flush(Transaction *transaction);
148
149
  std::string cache_path_;
150
  std::string txn_template_path_;
151
  bool alien_cache_;
152
  RenameWorkarounds rename_workaround_;
153
  CacheModes cache_mode_;
154
155
  /**
156
   * The cache can only degrade to a read-only cache once all writable file
157
   * descriptors from transactions are closed.  This is indicated by a zero
158
   * value in this variable.
159
   */
160
  atomic_int32 no_inflight_txns_;
161
162
  /**
163
   * Hack for HDFS which writes file sizes asynchronously.
164
   */
165
  bool reports_correct_filesize_;
166
};  // class PosixCacheManager
167
168
#endif  // CVMFS_CACHE_POSIX_H_