XRootD
XrdOssCsiConfig.hh
Go to the documentation of this file.
1 #ifndef _XRDOSSCSICONFIG_H
2 #define _XRDOSSCSICONFIG_H
3 /******************************************************************************/
4 /* */
5 /* X r d O s s C s i C o n f i g . h h */
6 /* */
7 /* (C) Copyright 2021 CERN. */
8 /* */
9 /* This file is part of the XRootD software suite. */
10 /* */
11 /* XRootD is free software: you can redistribute it and/or modify it under */
12 /* the terms of the GNU Lesser General Public License as published by the */
13 /* Free Software Foundation, either version 3 of the License, or (at your */
14 /* option) any later version. */
15 /* */
16 /* In applying this licence, CERN does not waive the privileges and */
17 /* immunities granted to it by virtue of its status as an Intergovernmental */
18 /* Organization or submit itself to any jurisdiction. */
19 /* */
20 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */
21 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
22 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
23 /* License for more details. */
24 /* */
25 /* You should have received a copy of the GNU Lesser General Public License */
26 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
27 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
28 /* */
29 /* The copyright holder's institutional names and contributor's names may not */
30 /* be used to endorse or promote products derived from this software without */
31 /* specific prior written permission of the institution or contributor. */
32 /******************************************************************************/
33 
34 #include "XrdOss/XrdOss.hh"
35 #include "XrdOuc/XrdOucStream.hh"
36 #include "XrdOuc/XrdOucEnv.hh"
37 #include "XrdSys/XrdSysLogger.hh"
38 
39 #include <string>
40 
41 class TagPath
42 {
43 public:
44 
45  TagPath() : prefix_("/.xrdt"), suffix_(".xrdt") { calcPrefixElements(); }
46  ~TagPath() { }
47 
48  //
49  // path may be absolute or last element from a directory listing
50  bool isTagFile(const char *path)
51  {
52  if (!path || !*path) return false;
53  std::string s(path);
54  simplePath(s);
55  // if prefix_ set, the test is to match if "path" is equal to or a subpath of perfix_
56  if (!prefix_.empty())
57  {
58  if (s.find(prefix_) == 0)
59  {
60  if (prefix_.length() == s.length()) return true;
61  if (s[prefix_.length()] == '/') return true;
62  }
63  return false;
64  }
65  // prefix_ not set, test is if "path" ends with suffix_
66  const size_t haystack = s.length();
67  const size_t needle = suffix_.length();
68  if (haystack >= needle && s.substr(haystack-needle, std::string::npos) == suffix_) return true;
69  return false;
70  }
71 
72  int SetPrefix(XrdSysError &Eroute, const std::string &v)
73  {
74  if (!v.empty() && v[0] != '/')
75  {
76  Eroute.Emsg("Config","prefix must be empty or start with /");
77  return 1;
78  }
79  prefix_ = v;
80  calcPrefixElements();
81  return XrdOssOK;
82  }
83 
84  bool hasPrefix() { return !prefix_.empty(); }
85 
86  //
87  // to convert an absolute data directory to corresponding tag directory
88  std::string makeBaseDirname(const char *path)
89  {
90  if (!path || *path != '/' || prefix_.empty()) return std::string();
91  std::string p(path);
92  simplePath(p);
93  if (p.length()>1) return prefix_ + p;
94  return prefix_;
95  }
96 
97  //
98  // test if absolute path is the directory containing the
99  // base directory of the tags
100  bool matchPrefixDir(const char *path)
101  {
102  if (!path || *path != '/' || prefix_.empty()) return false;
103  std::string p(path);
104  simplePath(p);
105  if (prefixstart_ == p) return true;
106  return false;
107  }
108 
109  // the name (not path) of directory containing the tags
110  std::string getPrefixName()
111  {
112  return prefixend_;
113  }
114 
115  // take datafile name at absolute path and convert it to filename of tagfile
116  std::string makeTagFilename(const char *path)
117  {
118  if (!path || *path != '/') return std::string();
119  std::string p(path);
120  simplePath(p);
121  return prefix_ + p + suffix_;
122  }
123 
124  std::string prefix_;
125 
126 private:
127  void calcPrefixElements()
128  {
129  prefixstart_.clear();
130  prefixend_.clear();
131  if (prefix_.empty()) return;
132  simplePath(prefix_);
133  const size_t idx = prefix_.rfind("/");
134  prefixstart_ = prefix_.substr(0,idx);
135  if (prefixstart_.empty()) prefixstart_ = std::string("/");
136  prefixend_ = prefix_.substr(idx+1,std::string::npos);
137  }
138 
139  void simplePath(std::string &str)
140  {
141  // replace double slashes with single
142  size_t i=0;
143  do {
144  i = str.find("//", i);
145  if (i == std::string::npos) break;
146  str.erase(i, 1);
147  } while (!str.empty());
148 
149  // remove trailing /
150  if (str.length()>1 && str[str.length()-1] == '/')
151  {
152  str.erase( str.end()-1 );
153  }
154  }
155 
156  std::string prefixstart_;
157  std::string prefixend_;
158  std::string suffix_;
159 };
160 
162 {
163 public:
164 
165  XrdOssCsiConfig() : fillFileHole_(true), xrdtSpaceName_("public"), allowMissingTags_(true), disablePgExtend_(false), disableLooseWrite_(false) { }
167 
168  int Init(XrdSysError &, const char *, const char *, XrdOucEnv *);
169 
170  bool fillFileHole() const { return fillFileHole_; }
171 
172  std::string xrdtSpaceName() const { return xrdtSpaceName_; }
173 
174  bool allowMissingTags() const { return allowMissingTags_; }
175 
176  bool disablePgExtend() const { return disablePgExtend_; }
177 
178  bool disableLooseWrite() const { return disableLooseWrite_; }
179 
181 
182 private:
183  int readConfig(XrdSysError &, const char *);
184 
185  int ConfigXeq(char *, XrdOucStream &, XrdSysError &);
186 
187  int xtrace(XrdOucStream &, XrdSysError &);
188 
189  bool fillFileHole_;
190  std::string xrdtSpaceName_;
191  bool allowMissingTags_;
192  bool disablePgExtend_;
193  bool disableLooseWrite_;
194 };
195 
196 #endif
#define XrdOssOK
Definition: XrdOss.hh:50
std::string getPrefixName()
bool matchPrefixDir(const char *path)
std::string makeBaseDirname(const char *path)
std::string makeTagFilename(const char *path)
bool hasPrefix()
std::string prefix_
bool isTagFile(const char *path)
int SetPrefix(XrdSysError &Eroute, const std::string &v)
bool fillFileHole() const
std::string xrdtSpaceName() const
int Init(XrdSysError &, const char *, const char *, XrdOucEnv *)
bool disableLooseWrite() const
bool disablePgExtend() const
bool allowMissingTags() const
int Emsg(const char *esfx, int ecode, const char *text1, const char *text2=0)
Definition: XrdSysError.cc:95