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);
117  bool _createMetaFile(const std::string& meta_file, const std::string& content, bool& is_locked);
119  std::string _getMetaFileName(const std::string& url);
121  std::string _getHash(const std::string& url) const;
124  struct CacheParameters _chooseCache(const std::string& url) const;
126  float _getCacheInfo(const std::string& path) const;
128  bool _cleanFilesAndReturnFalse(const std::string& hard_link_file, bool& locked);
129 
131  static Logger logger;
132 
133  public:
135 
148  FileCache(const std::string& cache_path,
149  const std::string& id,
150  uid_t job_uid,
151  gid_t job_gid);
152 
154 
163  FileCache(const std::vector<std::string>& caches,
164  const std::string& id,
165  uid_t job_uid,
166  gid_t job_gid);
167 
169 
183  FileCache(const std::vector<std::string>& caches,
184  const std::vector<std::string>& remote_caches,
185  const std::vector<std::string>& draining_caches,
186  const std::string& id,
187  uid_t job_uid,
188  gid_t job_gid);
189 
191  FileCache(): _uid(0),_gid(0) {
192  _caches.clear();
193  }
194 
196 
217  bool Start(const std::string& url,
218  bool& available,
219  bool& is_locked,
220  bool use_remote = true,
221  bool delete_first = false);
222 
224 
237  bool Stop(const std::string& url);
238 
240 
250  bool StopAndDelete(const std::string& url);
251 
253 
258  std::string File(const std::string& url);
259 
261 
298  bool Link(const std::string& link_path,
299  const std::string& url,
300  bool copy,
301  bool executable,
302  bool holding_lock,
303  bool& try_again);
304 
306 
312  bool Release() const;
313 
315 
323  bool AddDN(const std::string& url, const std::string& DN, const Time& expiry_time);
324 
326 
333  bool CheckDN(const std::string& url, const std::string& DN);
334 
336 
342  bool CheckCreated(const std::string& url);
343 
345 
350  Time GetCreated(const std::string& url);
351 
353 
358  bool CheckValid(const std::string& url);
359 
361 
366  Time GetValid(const std::string& url);
367 
369 
375  bool SetValid(const std::string& url, const Time& val);
376 
378  operator bool() {
379  return (!_caches.empty());
380  };
381 
383  bool operator==(const FileCache& a);
384 
385  };
386 
387 #else
388 
389  class FileCache {
390  public:
391  FileCache(const std::string& cache_path,
392  const std::string& id,
393  int job_uid,
394  int job_gid) {}
395  FileCache(const std::vector<std::string>& caches,
396  const std::string& id,
397  int job_uid,
398  int job_gid) {}
399  FileCache(const std::vector<std::string>& caches,
400  const std::vector<std::string>& remote_caches,
401  const std::vector<std::string>& draining_caches,
402  const std::string& id,
403  int job_uid,
404  int job_gid,
405  int cache_max=100,
406  int cache_min=100) {}
407  FileCache(const FileCache& cache) {}
408  FileCache() {}
409  bool Start(const std::string& url, bool& available, bool& is_locked, bool use_remote=true, bool delete_first=false) { return false; }
410  bool Stop(const std::string& url) { return false; }
411  bool StopAndDelete(const std::string& url) {return false; }
412  std::string File(const std::string& url) { return url; }
413  bool Link(const std::string& link_path, const std::string& url, bool copy, bool executable, bool holding_lock, bool& try_again) { return false; }
414  bool Release() const { return false;}
415  bool AddDN(const std::string& url, const std::string& DN, const Time& expiry_time) { return false;}
416  bool CheckDN(const std::string& url, const std::string& DN) { return false; }
417  bool CheckCreated(const std::string& url){ return false; }
418  Time GetCreated(const std::string& url) { return Time(); }
419  bool CheckValid(const std::string& url) { return false; }
420  Time GetValid(const std::string& url) { return Time(); }
421  bool SetValid(const std::string& url, const Time& val) { return false; }
422  operator bool() {
423  return false;
424  };
425  bool operator==(const FileCache& a) { return false; }
426  };
427 #endif /*WIN32*/
428 
429 
430 } // namespace Arc
431 
432 #endif /*FILECACHE_H_*/