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 
27 #ifndef WIN32
28 
30 
63  class FileCache {
64  private:
66  std::map <std::string, struct CacheParameters> _cache_map;
69  std::vector<struct CacheParameters> _caches;
72  std::vector<struct CacheParameters> _remote_caches;
74  std::vector<struct CacheParameters> _draining_caches;
77  std::set<std::string> _urls_unlocked;
79  std::string _id;
82  uid_t _uid;
84  gid_t _gid;
85 
87  static const std::string CACHE_DATA_DIR;
89  static const std::string CACHE_JOB_DIR;
91  static const int CACHE_DIR_LENGTH;
93  static const int CACHE_DIR_LEVELS;
95  static const std::string CACHE_META_SUFFIX;
97  static const int CACHE_DEFAULT_AUTH_VALIDITY;
100  static const int CACHE_LOCK_TIMEOUT;
102  static const int CACHE_META_LOCK_TIMEOUT;
103 
105  bool _init(const std::vector<std::string>& caches,
106  const std::vector<std::string>& remote_caches,
107  const std::vector<std::string>& draining_caches,
108  const std::string& id,
109  uid_t job_uid,
110  gid_t job_gid);
114  bool _checkMetaFile(const std::string& filename, const std::string& url, 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 
180  FileCache(const std::vector<std::string>& caches,
181  const std::vector<std::string>& remote_caches,
182  const std::vector<std::string>& draining_caches,
183  const std::string& id,
184  uid_t job_uid,
185  gid_t job_gid);
186 
188  FileCache(): _uid(0),_gid(0) {
189  _caches.clear();
190  }
191 
193 
214  bool Start(const std::string& url,
215  bool& available,
216  bool& is_locked,
217  bool use_remote = true,
218  bool delete_first = false);
219 
221 
234  bool Stop(const std::string& url);
235 
237 
247  bool StopAndDelete(const std::string& url);
248 
250 
255  std::string File(const std::string& url);
256 
258 
295  bool Link(const std::string& link_path,
296  const std::string& url,
297  bool copy,
298  bool executable,
299  bool holding_lock,
300  bool& try_again);
301 
303 
309  bool Release() const;
310 
312 
320  bool AddDN(const std::string& url, const std::string& DN, const Time& expiry_time);
321 
323 
330  bool CheckDN(const std::string& url, const std::string& DN);
331 
333 
339  bool CheckCreated(const std::string& url);
340 
342 
347  Time GetCreated(const std::string& url);
348 
350 
355  bool CheckValid(const std::string& url);
356 
358 
363  Time GetValid(const std::string& url);
364 
366 
372  bool SetValid(const std::string& url, const Time& val);
373 
375  operator bool() {
376  return (!_caches.empty());
377  };
378 
380  bool operator==(const FileCache& a);
381 
382  };
383 
384 #else
385 
386  class FileCache {
387  public:
388  FileCache(const std::string& cache_path,
389  const std::string& id,
390  int job_uid,
391  int job_gid) {}
392  FileCache(const std::vector<std::string>& caches,
393  const std::string& id,
394  int job_uid,
395  int job_gid) {}
396  FileCache(const std::vector<std::string>& caches,
397  const std::vector<std::string>& remote_caches,
398  const std::vector<std::string>& draining_caches,
399  const std::string& id,
400  int job_uid,
401  int job_gid,
402  int cache_max=100,
403  int cache_min=100) {}
404  FileCache(const FileCache& cache) {}
405  FileCache() {}
406  bool Start(const std::string& url, bool& available, bool& is_locked, bool use_remote=true, bool delete_first=false) { return false; }
407  bool Stop(const std::string& url) { return false; }
408  bool StopAndDelete(const std::string& url) {return false; }
409  std::string File(const std::string& url) { return url; }
410  bool Link(const std::string& link_path, const std::string& url, bool copy, bool executable, bool holding_lock, bool& try_again) { return false; }
411  bool Release() const { return false;}
412  bool AddDN(const std::string& url, const std::string& DN, const Time& expiry_time) { return false;}
413  bool CheckDN(const std::string& url, const std::string& DN) { return false; }
414  bool CheckCreated(const std::string& url){ return false; }
415  Time GetCreated(const std::string& url) { return Time(); }
416  bool CheckValid(const std::string& url) { return false; }
417  Time GetValid(const std::string& url) { return Time(); }
418  bool SetValid(const std::string& url, const Time& val) { return false; }
419  operator bool() {
420  return false;
421  };
422  bool operator==(const FileCache& a) { return false; }
423  };
424 #endif /*WIN32*/
425 
426 
427 } // namespace Arc
428 
429 #endif /*FILECACHE_H_*/