GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/cache_stream.h
Date: 2024-04-28 02:33:07
Exec Total Coverage
Lines: 9 31 29.0%
Branches: 5 8 62.5%

Line Branch Exec Source
1 /**
2 * This file is part of the CernVM File System.
3 */
4
5 #ifndef CVMFS_CACHE_STREAM_H_
6 #define CVMFS_CACHE_STREAM_H_
7
8 #include <pthread.h>
9
10 #include <string>
11
12 #include "cache.h"
13 #include "crypto/hash.h"
14 #include "fd_table.h"
15 #include "util/pointer.h"
16
17 namespace download {
18 class DownloadManager;
19 }
20
21 /**
22 * Cache manager that streams regular files using a download manager and stores
23 * file catalogs in an underlying cache manager.
24 */
25 class StreamingCacheManager : public CacheManager {
26 public:
27 StreamingCacheManager(unsigned max_open_fds,
28 CacheManager *cache_mgr,
29 download::DownloadManager *regular_download_mgr,
30 download::DownloadManager *external_download_mgr);
31 virtual ~StreamingCacheManager();
32
33 // In the files system / mountpoint initialization, we create the cache
34 // manager before we know about the download manager. Hence we allow to
35 // patch in the download manager at a later point.
36 void SetRegularDownloadManager(download::DownloadManager *download_mgr) {
37 regular_download_mgr_ = download_mgr;
38 }
39 void SetExternalDownloadManager(download::DownloadManager *download_mgr) {
40 external_download_mgr_ = download_mgr;
41 }
42
43 virtual CacheManagerIds id() { return kStreamingCacheManager; }
44 virtual std::string Describe();
45
46 virtual bool AcquireQuotaManager(QuotaManager *quota_mgr);
47
48 virtual int Open(const LabeledObject &object);
49 virtual int64_t GetSize(int fd);
50 virtual int Close(int fd);
51 virtual int64_t Pread(int fd, void *buf, uint64_t size, uint64_t offset);
52 virtual int Dup(int fd);
53 virtual int Readahead(int fd);
54
55 // Only pinned objects and catalogs are written to the cache. Transactions
56 // are passed through to the backing cache manager.
57 virtual uint32_t SizeOfTxn() { return cache_mgr_->SizeOfTxn(); }
58 virtual int StartTxn(const shash::Any &id, uint64_t size, void *txn) {
59 return cache_mgr_->StartTxn(id, size, txn);
60 }
61 virtual void CtrlTxn(const Label &label, const int flags, void *txn) {
62 cache_mgr_->CtrlTxn(label, flags, txn);
63 }
64 virtual int64_t Write(const void *buf, uint64_t size, void *txn)
65 {
66 return cache_mgr_->Write(buf, size, txn);
67 }
68 virtual int Reset(void *txn) { return cache_mgr_->Reset(txn); }
69 virtual int OpenFromTxn(void *txn);
70 virtual int AbortTxn(void *txn) { return cache_mgr_->AbortTxn(txn); }
71 virtual int CommitTxn(void *txn) { return cache_mgr_->CommitTxn(txn); }
72
73 virtual void Spawn() { cache_mgr_->Spawn(); }
74
75 virtual manifest::Breadcrumb LoadBreadcrumb(const std::string &fqrn) {
76 return cache_mgr_->LoadBreadcrumb(fqrn);
77 }
78 virtual bool StoreBreadcrumb(const manifest::Manifest &manifest) {
79 return cache_mgr_->StoreBreadcrumb(manifest);
80 }
81
82 // Used in cvmfs' RestoreState to switch back from the streaming to the
83 // regular cache manager. At this point, the streaming cache manager has
84 // opened the root file catalog. We need to return the file descriptor in
85 // the wrapped cache manager, too.
86 CacheManager *MoveOutBackingCacheMgr(int *root_fd);
87 // Used in cvmfs' RestoreState to create a virtual file descriptor for the
88 // root catalog fd, that has been already opened in the backing cache manager
89 int PlantFd(int fd_in_cache_mgr);
90
91 protected:
92 virtual void *DoSaveState();
93 virtual int DoRestoreState(void *data);
94 virtual bool DoFreeState(void *data);
95
96 private:
97 struct FdInfo {
98 int fd_in_cache_mgr;
99 shash::Any object_id;
100 CacheManager::Label label;
101
102 4 FdInfo() : fd_in_cache_mgr(-1) {}
103 explicit FdInfo(int fd) : fd_in_cache_mgr(fd) {}
104 1 explicit FdInfo(const CacheManager::LabeledObject &object)
105 1 : fd_in_cache_mgr(-1), object_id(object.id), label(object.label) {}
106
107 6 bool operator ==(const FdInfo &other) const {
108
3/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 5 times.
12 return this->fd_in_cache_mgr == other.fd_in_cache_mgr &&
109 12 this->object_id == other.object_id;
110 }
111 4 bool operator !=(const FdInfo &other) const {
112 4 return !(*this == other);
113 }
114
115
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
3 bool IsValid() const { return fd_in_cache_mgr >= 0 || !object_id.IsNull(); }
116 };
117
118 struct SavedState {
119 SavedState() : version(0), fd_table(NULL), state_backing_cachemgr(NULL) { }
120 unsigned int version;
121 FdTable<FdInfo> *fd_table;
122 void *state_backing_cachemgr;
123 };
124
125 /// Depending on info.flags, selects either the regular or the external
126 /// download manager
127 download::DownloadManager *SelectDownloadManager(const FdInfo &info);
128
129 /// Streams an object using the download manager. The complete object is read
130 /// and its size is returned (-errno on error).
131 /// The given section of the object is copied into the provided buffer,
132 /// which may be NULL if only the size of the object is relevant.
133 int64_t Stream(const FdInfo &info, void *buf, uint64_t size, uint64_t offset);
134
135 UniquePtr<CacheManager> cache_mgr_;
136 download::DownloadManager *regular_download_mgr_;
137 download::DownloadManager *external_download_mgr_;
138
139 pthread_mutex_t *lock_fd_table_;
140 FdTable<FdInfo> fd_table_;
141 }; // class StreamingCacheManager
142
143 #endif // CVMFS_CACHE_STREAM_H_
144