ewmscp  ..
dcapCommon.cpp
Go to the documentation of this file.
1 #include "dcapCommon.h"
2 #include <OptionsChrono.h>
3 #include <forward_list>
4 #include "escapism.h"
5 
6 static const std::string parPrefix("dCap");
7 
9  static std::forward_list<dCapConfigParameter*>& getList() {
10  static std::remove_reference<decltype(getList())>::type list;
11  return list;
12  }
13  protected:
14  virtual void applySingle() = 0;
15  public:
16  virtual void fForbid(options::base& /*opt*/) {};
18  getList().push_front(this);
19  }
20  static void apply() {
21  for (auto par : getList()) {
22  par->applySingle();
23  }
24  }
25 };
26 template <class T> class dCapCfgPar: public dCapConfigParameter {
28  void (* set)(T);
29  public:
30  dCapCfgPar(const std::string& optName,
31  const std::string& description,
32  void (* setter)(T)):
33  option('\0', parPrefix + optName, description),
34  set(setter) {
35  }
36  void fForbid(options::base& opt) override {
37  opt.fForbid(&option);
38  option.fForbid(&opt);
39  };
40  void applySingle() override {
41  if (option.fIsSet()) {
42  set(option);
43  }
44  }
45 };
46 template <> class dCapCfgPar<const char*>: public dCapConfigParameter {
48  void (* set)(const char *);
49  public:
50  dCapCfgPar(const std::string& optName,
51  const std::string& description,
52  void (* setter)(const char*)):
53  option('\0', parPrefix + optName, description),
54  set(setter) {
55  }
56  void applySingle() override {
57  if (option.fIsSet()) {
58  set(option.c_str());
59  }
60  }
61 };
62 template <class T, class U> class dCapCfg2Par: public dCapConfigParameter {
65  void (* set)(T, U);
66  public:
67  dCapCfg2Par(const std::string& opt1Name,
68  const std::string& description1,
69  const std::string& opt2Name,
70  const std::string& description2,
71  void (* setter)(T, U),
72  std::initializer_list<dCapConfigParameter*> forbid = {}):
73  option1('\0', parPrefix + opt1Name, description1),
74  option2('\0', parPrefix + opt2Name, description2),
75  set(setter) {
76  option1.fRequire(&option2);
78  for (auto& other : forbid) {
79  other->fForbid(option1);
80  other->fForbid(option2);
81  }
82  }
83  void applySingle() override {
84  if (option1.fIsSet()) {
86  }
87  }
88 };
89 template <typename T> class dCapCfgTimePar: public dCapConfigParameter {
91  void (* set)(T);
92  public:
93  dCapCfgTimePar(const std::string& optName,
94  const std::string& description,
95  void (* setter)(T)):
96  option('\0', parPrefix + optName, description),
97  set(setter) {
98  }
99  void applySingle() override {
100  if (option.fIsSet()) {
101  set(option.count());
102  }
103  }
104 };
105 
106 static dCapCfgPar<unsigned short> CallbackPort("CallbackPort", "set callback port", dc_setCallbackPort);
108 CallbackPortRange("CallbackPortRangeBegin", "set callback port range begin",
109  "CallbackPortRangeEnd", "set callback port range end",
110  dc_setCallbackPortRange, {&CallbackPort});
111 static dCapCfgPar<const char*> ReplyHostName("ReplyHostName", "set reply host name", dc_setReplyHostName);
112 static dCapCfgPar<const char*> Tunnel("Tunnel", "set tunnel", dc_setTunnel);
113 static dCapCfgPar<const char*> TunnelType("TunnelType", "set tunnel type", dc_setTunnelType);
114 static dCapCfgPar<unsigned int> DebugLevel("DebugLevel", "set debug level", dc_setDebugLevel);
115 static dCapCfgTimePar<time_t> OpenTimeout("OpenTimeout", "set open timeout", dc_setOpenTimeout);
116 static dCapCfgTimePar<unsigned int> CloseTimeout("CloseTimeout", "set close timeout", dc_setCloseTimeout);
117 
118 
121 }
122 
123 
125 
126 
127 std::string dcapCommon::fixPathUrl(const std::string& path) {
128  // example path: dcap://dcache-dot3:22125/pnfs/desy.de/dot/data_volatile/cards/A Display of My Dark Power.full.jpg
129  auto index = path.find("//"); //find host name start
130  if (index == std::remove_reference<decltype(path)>::type::npos) {
131  throw std::runtime_error("can't sanitize path \"" + path + "\"");
132  }
133  index = path.find('/', index + 2); // find start of real path part
134  std::string url(path.substr(0, index + 1));
135  static const auto escaper = escapism::newEscaper("Urlsoft");
136  escaper->escape(path.substr(index + 1), url);
137  if (url.back() == '/') {
138  url.pop_back();
139  }
140  return url;
141 }
142 
143 
144 
145 
146 bool dcapCommon::pathExists(const std::string& path) {
147  dCapLock extraOptionLock;
148  struct dcapStat statBuf;
149  std::string fixedPath(fixPathUrl(path));
150  auto result = dc_stat(fixedPath.c_str(), &statBuf);
151  if (result && errno == ENOENT) {
152  return false;
153  }
154  throwcall::dcap::good0(result, "can't stat ", fixedPath);
155  return true;
156 };
157 std::unique_ptr<const genericStat> dcapCommon::getStat(const std::string& path,
158  bool followLink) {
159  dCapLock extraOptionLock;
160  struct dcapStat statBuf;
161  std::string fixedPath(fixPathUrl(path));
162  int result;
163  if (followLink) {
164  result = dc_stat(fixedPath.c_str(), &statBuf);
165  } else {
166  result = dc_lstat(fixedPath.c_str(), &statBuf);
167  }
168  if (result && errno == ENOENT && (dc_errno == DEOK || dc_errno == DESRVMSG)) {
169  return nullptr;
170  }
171  throwcall::dcap::good0(result, "can't stat ", fixedPath);
172  if (statBuf.st_blksize == 0) { // we got nonsense
173  statBuf.st_blksize = 1024 * 1024; // use 1M as default
174  }
175  return std::unique_ptr<const genericStat>(new genericStat(statBuf, std::chrono::seconds(1)));
176 };
177 
178 
179 
180 
181 dcapIoCommon::dcapIoCommon(const std::string& aPath):
182  path(dcapCommon::fixPathUrl(aPath)) {
183 }
184 
185 
186 
187 
188 
189 std::unique_ptr<const genericStat> dcapIoCommon::getStat() {
190  dCapLock extraOptionLock;
191  struct dcapStat statBuf;
192  throwcall::dcap::good0(dc_fstat(fd, &statBuf), "can't stat", path);
193  return std::unique_ptr<const genericStat>(new genericStat(statBuf, std::chrono::seconds(1)));
194 }
options::single
generic option class with any type that can be used with std::istream and std::ostream
Definition: Options.h:533
Tunnel
static dCapCfgPar< const char * > Tunnel("Tunnel", "set tunnel", dc_setTunnel)
dCapConfigParameter::fForbid
virtual void fForbid(options::base &)
Definition: dcapCommon.cpp:16
DebugLevel
static dCapCfgPar< unsigned int > DebugLevel("DebugLevel", "set debug level", dc_setDebugLevel)
dCapLock
Definition: dcapCommon.h:40
genericStat
generic stat abstraction class Used to abstract the variants of the stat structure.
Definition: genericStat.h:12
dcapCommon::dcapCommon
dcapCommon()
Definition: dcapCommon.cpp:119
dCapCfg2Par::option2
options::single< U > option2
Definition: dcapCommon.cpp:64
dCapCfgTimePar::applySingle
void applySingle() override
Definition: dcapCommon.cpp:99
dCapConfigParameter::dCapConfigParameter
dCapConfigParameter()
Definition: dcapCommon.cpp:17
dCapCfgPar< const char * >
Definition: dcapCommon.cpp:46
dcapStat
#define dcapStat
Definition: dcapCommon.h:7
options::base::fIsSet
virtual bool fIsSet() const
check if this option was set, regardless of from command line or config file
Definition: Options.h:263
dCapCfgPar::set
void(* set)(T)
Definition: dcapCommon.cpp:28
options::base
base class for options
Definition: Options.h:193
dcapIoCommon::path
const std::string path
Definition: dcapCommon.h:76
dCapCfgPar::dCapCfgPar
dCapCfgPar(const std::string &optName, const std::string &description, void(*setter)(T))
Definition: dcapCommon.cpp:30
dcapIoCommon::getStat
std::unique_ptr< const genericStat > getStat() override
Definition: dcapCommon.cpp:189
dCapCfg2Par::set
void(* set)(T, U)
Definition: dcapCommon.cpp:65
CloseTimeout
static dCapCfgTimePar< unsigned int > CloseTimeout("CloseTimeout", "set close timeout", dc_setCloseTimeout)
options::base::fForbid
virtual void fForbid(const base *aOtherOption)
forbid aOtherOption when this option is set
Definition: Options.cpp:617
dCapCfgTimePar::option
options::single< std::chrono::seconds > option
Definition: dcapCommon.cpp:90
options::base::fRequire
virtual void fRequire(const base *aOtherOption)
require aOtherOption when this option is set
Definition: Options.cpp:611
escapism.h
OptionsChrono.h
dCapConfigParameter::getList
static std::forward_list< dCapConfigParameter * > & getList()
Definition: dcapCommon.cpp:9
dcapCommon::fixPathUrl
static std::string fixPathUrl(const std::string &path)
Definition: dcapCommon.cpp:127
dCapCfg2Par::option1
options::single< T > option1
Definition: dcapCommon.cpp:63
dCapCfgTimePar
Definition: dcapCommon.cpp:89
dCapConfigParameter::apply
static void apply()
Definition: dcapCommon.cpp:20
dCapCfgPar::fForbid
void fForbid(options::base &opt) override
Definition: dcapCommon.cpp:36
parPrefix
static const std::string parPrefix("dCap")
dcapIoCommon::dcapIoCommon
dcapIoCommon(const std::string &aPath)
Definition: dcapCommon.cpp:181
CallbackPort
static dCapCfgPar< unsigned short > CallbackPort("CallbackPort", "set callback port", dc_setCallbackPort)
dCapConfigParameter
Definition: dcapCommon.cpp:8
dCapCfgTimePar::dCapCfgTimePar
dCapCfgTimePar(const std::string &optName, const std::string &description, void(*setter)(T))
Definition: dcapCommon.cpp:93
dCapConfigParameter::applySingle
virtual void applySingle()=0
dCapCfg2Par::dCapCfg2Par
dCapCfg2Par(const std::string &opt1Name, const std::string &description1, const std::string &opt2Name, const std::string &description2, void(*setter)(T, U), std::initializer_list< dCapConfigParameter * > forbid={})
Definition: dcapCommon.cpp:67
dCapLock::extraOptionMutex
static std::mutex extraOptionMutex
Definition: dcapCommon.h:44
dCapCfgTimePar::set
void(* set)(T)
Definition: dcapCommon.cpp:91
escapism::newEscaper
static const escapism * newEscaper(const std::string &name)
Definition: escapism.cpp:25
dcapCommon::pathExists
bool pathExists(const std::string &path) override
Definition: dcapCommon.cpp:146
OpenTimeout
static dCapCfgTimePar< time_t > OpenTimeout("OpenTimeout", "set open timeout", dc_setOpenTimeout)
ReplyHostName
static dCapCfgPar< const char * > ReplyHostName("ReplyHostName", "set reply host name", dc_setReplyHostName)
dCapCfgPar< const char * >::option
options::single< std::string > option
Definition: dcapCommon.cpp:47
throwcall::dcap::good0
void good0(T call, const Args &... args)
Definition: dcapCommon.h:21
dCapCfgPar< const char * >::applySingle
void applySingle() override
Definition: dcapCommon.cpp:56
dCapCfgPar< const char * >::dCapCfgPar
dCapCfgPar(const std::string &optName, const std::string &description, void(*setter)(const char *))
Definition: dcapCommon.cpp:50
dCapCfg2Par::applySingle
void applySingle() override
Definition: dcapCommon.cpp:83
dcapCommon.h
dcapIoCommon::fd
int fd
Definition: dcapCommon.h:77
dcapCommon
Definition: dcapCommon.h:65
dCapCfg2Par
Definition: dcapCommon.cpp:62
dCapCfgPar::option
options::single< T > option
Definition: dcapCommon.cpp:27
TunnelType
static dCapCfgPar< const char * > TunnelType("TunnelType", "set tunnel type", dc_setTunnelType)
CallbackPortRange
static dCapCfg2Par< unsigned short, unsigned short > CallbackPortRange("CallbackPortRangeBegin", "set callback port range begin", "CallbackPortRangeEnd", "set callback port range end", dc_setCallbackPortRange, {&CallbackPort})
dcapCommon::getStat
std::unique_ptr< const genericStat > getStat(const std::string &path, bool followLink) override
Definition: dcapCommon.cpp:157
dCapCfgPar::applySingle
void applySingle() override
Definition: dcapCommon.cpp:40
dCapCfgPar
Definition: dcapCommon.cpp:26