ARC SDK
FileCache.h
1 // -*- indent-tabs-mode: nil -*-
2 
3 #ifndef FILECACHE_H_
4 #define FILECACHE_H_
5 
6 #include <sstream>
7 #include <vector>
8 #include <map>
9 #include <set>
10 #include <arc/DateTime.h>
11 #include <arc/Logger.h>
12 
13 #include "FileCacheHash.h"
14 
15 namespace Arc {
16 
18 
22  struct CacheParameters {
23  std::string cache_path;
24  std::string cache_link_path;
25  };
26 
28 
61  class FileCache {
62  private:
64  std::map <std::string, struct CacheParameters> _cache_map;
67  std::vector<struct CacheParameters> _caches;
69  std::vector<struct CacheParameters> _draining_caches;
71  std::vector<struct CacheParameters> _readonly_caches;
74  std::set<std::string> _urls_unlocked;
76  std::string _id;
79  uid_t _uid;
81  gid_t _gid;
82 
84  static const std::string CACHE_DATA_DIR;
86  static const std::string CACHE_JOB_DIR;
88  static const int CACHE_DIR_LENGTH;
90  static const int CACHE_DIR_LEVELS;
92  static const std::string CACHE_META_SUFFIX;
94  static const int CACHE_DEFAULT_AUTH_VALIDITY;
97  static const int CACHE_LOCK_TIMEOUT;
99  static const int CACHE_META_LOCK_TIMEOUT;
100 
102  bool _init(const std::vector<std::string>& caches,
103  const std::vector<std::string>& draining_caches,
104  const std::vector<std::string>& readonly_caches,
105  const std::string& id,
106  uid_t job_uid,
107  gid_t job_gid);
111  bool _checkMetaFile(const std::string& filename, const std::string& url, bool& is_locked);
114  bool _createMetaFile(const std::string& meta_file, const std::string& content, bool& is_locked);
116  std::string _getMetaFileName(const std::string& url);
118  std::string _getHash(const std::string& url) const;
121  struct CacheParameters _chooseCache(const std::string& url) const;
123  float _getCacheInfo(const std::string& path) const;
125  bool _cleanFilesAndReturnFalse(const std::string& hard_link_file, bool& locked);
126 
128  static Logger logger;
129 
130  public:
132 
145  FileCache(const std::string& cache_path,
146  const std::string& id,
147  uid_t job_uid,
148  gid_t job_gid);
149 
151 
160  FileCache(const std::vector<std::string>& caches,
161  const std::string& id,
162  uid_t job_uid,
163  gid_t job_gid);
164 
166 
179  FileCache(const std::vector<std::string>& caches,
180  const std::vector<std::string>& draining_caches,
181  const std::string& id,
182  uid_t job_uid,
183  gid_t job_gid);
184 
186 
200  FileCache(const std::vector<std::string>& caches,
201  const std::vector<std::string>& draining_caches,
202  const std::vector<std::string>& readonly_caches,
203  const std::string& id,
204  uid_t job_uid,
205  gid_t job_gid);
206 
208  FileCache(): _uid(0),_gid(0) {
209  _caches.clear();
210  }
211 
213 
231  bool Start(const std::string& url,
232  bool& available,
233  bool& is_locked,
234  bool delete_first = false);
235 
237 
250  bool Stop(const std::string& url);
251 
253 
263  bool StopAndDelete(const std::string& url);
264 
266 
271  std::string File(const std::string& url);
272 
274 
311  bool Link(const std::string& link_path,
312  const std::string& url,
313  bool copy,
314  bool executable,
315  bool holding_lock,
316  bool& try_again);
317 
319 
325  bool Release() const;
326 
328 
336  bool AddDN(const std::string& url, const std::string& DN, const Time& expiry_time);
337 
339 
346  bool CheckDN(const std::string& url, const std::string& DN);
347 
349 
355  bool CheckCreated(const std::string& url);
356 
358 
363  Time GetCreated(const std::string& url);
364 
366  operator bool() {
367  return (!_caches.empty());
368  };
369 
371  bool operator==(const FileCache& a);
372 
373  };
374 
375 } // namespace Arc
376 
377 #endif /*FILECACHE_H_*/
Arc namespace contains all core ARC classes.
Definition: ArcConfig.h:11
bool StopAndDelete(const std::string &url)
Stop the cache after a file was downloaded and delete the cache file.
A class for storing and manipulating times.
Definition: DateTime.h:125
bool CheckDN(const std::string &url, const std::string &DN)
Check if a DN exists in the permission cache and is still valid for the given url.
bool AddDN(const std::string &url, const std::string &DN, const Time &expiry_time)
Store a DN in the permissions cache for the given url.
bool Start(const std::string &url, bool &available, bool &is_locked, bool delete_first=false)
Start preparing to cache the file specified by url.
std::string File(const std::string &url)
Get the cache filename for the given URL.
A logger class.
Definition: Logger.h:493
FileCache provides an interface to all cache operations.
Definition: FileCache.h:61
Contains data on the parameters of a cache.
Definition: FileCache.h:22
Time GetCreated(const std::string &url)
Get the creation time of a cached file.
bool operator==(const FileCache &a)
Returns true if all attributes are equal.
bool CheckCreated(const std::string &url)
Check if it is possible to obtain the creation time of a cache file.
bool Release() const
Release cache files used in this cache.
bool Stop(const std::string &url)
Stop the cache after a file was downloaded.
bool Link(const std::string &link_path, const std::string &url, bool copy, bool executable, bool holding_lock, bool &try_again)
Link a cache file to the place it will be used.