XRootD
XrdPfcFsTraversal.hh
Go to the documentation of this file.
1 #ifndef __XRDPFC_FSTRAVERSAL_HH__
2 #define __XRDPFC_FSTRAVERSAL_HH__
3 
4 #include "XrdOss/XrdOssAt.hh"
5 #include "XrdOuc/XrdOucEnv.hh"
6 
7 #include <functional>
8 #include <string>
9 #include <vector>
10 #include <map>
11 #include <set>
12 #include <sys/stat.h>
13 
14 class XrdOss;
15 class XrdOssDF;
16 
17 namespace XrdPfc {
18 
19 struct DirState;
20 
22 {
23 public:
24  struct FilePairStat {
25  struct stat stat_data, stat_cinfo;
26  bool has_data = false;
27  bool has_cinfo = false;
28 
29  void set_data (const struct stat &s) { stat_data = s; has_data = true; }
30  void set_cinfo(const struct stat &s) { stat_cinfo = s; has_cinfo = true; }
31  bool has_both() const { return has_data && has_cinfo; }
32  };
33 
34 protected:
38 
39  bool m_maintain_dirstate = false;
40  bool m_statret_warned = false; // StatRet() failure reported once per traversal
41 
42 public:
44  DirState *m_dir_state = nullptr; // current DirState
45 
46  int m_rel_dir_level = -1; // dir level relative to root, 0 ~ at root
47  std::string m_current_path; // Includes trailing '/' -- needed for printouts and PurgeCandidate creation.
48 
49  // Hmmh ... need a stack of those ... or not, if doing tail recursion.
50  // Can not, OpenDirAt descend can not be like that, ie, i will need the old handle.
51  std::vector<XrdOssDF*> m_dir_handle_stack;
52 
53  std::vector<std::string> m_current_dirs; // swap out into local scope before recursion
54  std::map<std::string, FilePairStat> m_current_files; // clear when done
55 
56  std::set<std::string> m_protected_top_dirs; // directories that will NOT be traversed at relative level 0.
57 
58  // Optional callback invoked every s_readdir_hook_period entries while a directory
59  // is being read, so long reads of a single huge directory can yield to other work.
60  // The callback must not use this FsTraversal object.
61  std::function<void()> m_readdir_hook;
62  static const int s_readdir_hook_period = 1024;
63 
64  static const char *m_traceID;
65 
66  void slurp_current_dir();
67  void slurp_dir_ll(XrdOssDF &dh, int dir_level, const char *path, const char *trc_pfx);
68 
69 public:
70  FsTraversal(XrdOss &oss);
71  ~FsTraversal();
72 
73  bool begin_traversal(DirState *root, const char *root_path);
74  bool begin_traversal(const char *root_path);
75  void end_traversal();
76 
77  bool cd_down(const std::string &dir_name);
78  void cd_up();
79 
80  int open_at_ro(const char* fname, XrdOssDF *&ossDF) {
81  return m_oss_at.OpenRO(*m_dir_handle_stack.back(), fname, m_env, ossDF);
82  }
83  int unlink_at(const char* fname) {
84  return m_oss_at.Unlink(*m_dir_handle_stack.back(), fname);
85  }
86  int close_delete(XrdOssDF *&ossDF);
87 
88  XrdOucEnv& default_env() { return m_env; }
89 };
90 
91 }
92 
93 #endif
#define stat(a, b)
Definition: XrdPosix.hh:101
int OpenRO(XrdOssDF &atDir, const char *path, XrdOucEnv &env, XrdOssDF *&ossDF)
Definition: XrdOssAt.cc:127
int Unlink(XrdOssDF &atDir, const char *path)
Definition: XrdOssAt.cc:199
std::vector< std::string > m_current_dirs
int open_at_ro(const char *fname, XrdOssDF *&ossDF)
std::vector< XrdOssDF * > m_dir_handle_stack
int close_delete(XrdOssDF *&ossDF)
static const int s_readdir_hook_period
std::function< void()> m_readdir_hook
XrdOucEnv & default_env()
void slurp_dir_ll(XrdOssDF &dh, int dir_level, const char *path, const char *trc_pfx)
bool begin_traversal(DirState *root, const char *root_path)
int unlink_at(const char *fname)
std::set< std::string > m_protected_top_dirs
bool cd_down(const std::string &dir_name)
std::map< std::string, FilePairStat > m_current_files
static const char * m_traceID
Definition: XrdPfc.hh:41
void set_data(const struct stat &s)
void set_cinfo(const struct stat &s)