XRootD
XrdClHttpFileSystemPlugIn.cc
Go to the documentation of this file.
1 
6 
7 #include <mutex>
8 
9 #include "davix.hpp"
10 
11 #include "XrdCl/XrdClDefaultEnv.hh"
12 #include "XrdCl/XrdClLog.hh"
14 
18 
19 namespace XrdCl {
20 
21 Davix::Context *root_ctx_ = NULL;
22 Davix::DavPosix *root_davix_client_ = NULL;
23 
25  : url_(url), logger_(DefaultEnv::GetLog()) {
26  SetUpLogging(logger_);
27  logger_->Debug(kLogXrdClHttp,
28  "HttpFileSystemPlugIn constructed with URL: %s.",
29  url_.GetURL().c_str());
30  std::string origin = getenv("XRDXROOTD_PROXY")? getenv("XRDXROOTD_PROXY") : "";
31  if ( origin.empty() || origin.find("=") == 0) {
32  ctx_ = new Davix::Context();
33  davix_client_ = new Davix::DavPosix(ctx_);
34  }
35  else {
36  if (root_ctx_ == NULL) {
37  root_ctx_ = new Davix::Context();
38  root_davix_client_ = new Davix::DavPosix(root_ctx_);
39  }
40  ctx_ = root_ctx_;
41  davix_client_ = root_davix_client_;
42  }
43 }
44 
45 // destructor of davix_client_ or ctx_ will call something in ssl3 lib
46 // which reset errno. We need to preserve errno so that XrdPssSys::Stat
47 // will see it.
49  int rc = errno;
50  if (root_ctx_ == NULL) {
51  delete davix_client_;
52  delete ctx_;
53  }
54  errno = rc;
55 }
56 
57 XRootDStatus HttpFileSystemPlugIn::Mv(const std::string &source,
58  const std::string &dest,
59  ResponseHandler *handler,
60  uint16_t timeout) {
61  //const auto full_source_path = url_.GetLocation() + source;
62  //const auto full_dest_path = url_.GetLocation() + dest;
63  const auto full_source_path = url_.GetProtocol() + "://"
64  + url_.GetHostName() + ":"
65  + std::to_string(url_.GetPort())
66  + source;
67  const auto full_dest_path = url_.GetProtocol() + "://"
68  + url_.GetHostName() + ":"
69  + std::to_string(url_.GetPort())
70  + dest;
71 
72  logger_->Debug(kLogXrdClHttp,
73  "HttpFileSystemPlugIn::Mv - src = %s, dest = %s, timeout = %d",
74  full_source_path.c_str(), full_dest_path.c_str(), timeout);
75 
76  auto status =
77  Posix::Rename(*davix_client_, full_source_path, full_dest_path, timeout);
78 
79  if (status.IsError()) {
80  logger_->Error(kLogXrdClHttp, "Mv failed: %s", status.ToStr().c_str());
81  return status;
82  }
83 
84  handler->HandleResponse(new XRootDStatus(status), nullptr);
85 
86  return XRootDStatus();
87 }
88 
89 XRootDStatus HttpFileSystemPlugIn::Rm(const std::string &path,
90  ResponseHandler *handler,
91  uint16_t timeout) {
92  auto url = url_;
93  url.SetPath(path);
94 
95  logger_->Debug(kLogXrdClHttp,
96  "HttpFileSystemPlugIn::Rm - path = %s, timeout = %d",
97  url.GetURL().c_str(), timeout);
98 
99  auto status = Posix::Unlink(*davix_client_, url.GetURL(), timeout);
100 
101  if (status.IsError()) {
102  logger_->Error(kLogXrdClHttp, "Rm failed: %s", status.ToStr().c_str());
103  return status;
104  }
105 
106  handler->HandleResponse(new XRootDStatus(status), nullptr);
107 
108  return XRootDStatus();
109 }
110 
112  MkDirFlags::Flags flags,
113  Access::Mode mode,
114  ResponseHandler *handler,
115  uint16_t timeout) {
116  auto url = url_;
117  url.SetPath(path);
118 
119  logger_->Debug(
121  "HttpFileSystemPlugIn::MkDir - path = %s, flags = %d, timeout = %d",
122  url.GetURL().c_str(), flags, timeout);
123 
124  auto status = Posix::MkDir(*davix_client_, url.GetURL(), flags, mode, timeout);
125  if (status.IsError()) {
126  logger_->Error(kLogXrdClHttp, "MkDir failed: %s", status.ToStr().c_str());
127  return status;
128  }
129 
130  handler->HandleResponse(new XRootDStatus(status), nullptr);
131 
132  return XRootDStatus();
133 }
134 
136  ResponseHandler *handler,
137  uint16_t timeout) {
138  auto url = url_;
139  url.SetPath(path);
140 
141  logger_->Debug(kLogXrdClHttp,
142  "HttpFileSystemPlugIn::RmDir - path = %s, timeout = %d",
143  url.GetURL().c_str(), timeout);
144 
145  auto status = Posix::RmDir(*davix_client_, url.GetURL(), timeout);
146  if (status.IsError()) {
147  logger_->Error(kLogXrdClHttp, "RmDir failed: %s", status.ToStr().c_str());
148  return status;
149  }
150 
151  handler->HandleResponse(new XRootDStatus(status), nullptr);
152  return XRootDStatus();
153 }
154 
156  DirListFlags::Flags flags,
157  ResponseHandler *handler,
158  uint16_t timeout) {
159  auto url = url_;
160  url.SetPath(path);
161  const auto full_path = url.GetLocation();
162 
163  logger_->Debug(
165  "HttpFileSystemPlugIn::DirList - path = %s, flags = %d, timeout = %d",
166  full_path.c_str(), flags, timeout);
167 
168  const bool details = flags & DirListFlags::Stat;
169  const bool recursive = flags & DirListFlags::Recursive;
170 
171  // res == std::pair<DirectoryList*, XRootDStatus>
172  auto res =
173  Posix::DirList(*davix_client_, full_path, details, recursive, timeout);
174  if (res.second.IsError()) {
175  logger_->Error(kLogXrdClHttp, "Could not list dir: %s, error: %s",
176  full_path.c_str(), res.second.ToStr().c_str());
177  return res.second;
178  }
179 
180  auto obj = new AnyObject();
181  obj->Set(res.first);
182 
183  handler->HandleResponse(new XRootDStatus(), obj);
184  return XRootDStatus();
185 }
186 
187 XRootDStatus HttpFileSystemPlugIn::Stat(const std::string &path,
188  ResponseHandler *handler,
189  uint16_t timeout) {
190  //const auto full_path = url_.GetLocation() + path;
191  const auto full_path = url_.GetProtocol() + "://" +
192  url_.GetHostName() + ":" +
193  std::to_string(url_.GetPort()) + "/" + path;
194 
195  logger_->Debug(kLogXrdClHttp,
196  "HttpFileSystemPlugIn::Stat - path = %s, timeout = %d",
197  full_path.c_str(), timeout);
198 
199  auto stat_info = new StatInfo();
200  //XRootDStatus status;
201  auto status = Posix::Stat(*davix_client_, full_path, timeout, stat_info);
202 
203  if (status.IsError()) {
204  logger_->Error(kLogXrdClHttp, "Stat failed: %s", status.ToStr().c_str());
205  return status;
206  }
207 
208  auto obj = new AnyObject();
209  obj->Set(stat_info);
210 
211  handler->HandleResponse(new XRootDStatus(), obj);
212 
213  return XRootDStatus();
214 }
215 
216 bool HttpFileSystemPlugIn::SetProperty(const std::string &name,
217  const std::string &value) {
218  properties_[name] = value;
219  return true;
220 }
221 
222 bool HttpFileSystemPlugIn::GetProperty(const std::string &name,
223  std::string &value) const {
224  const auto p = properties_.find(name);
225  if (p == std::end(properties_)) {
226  return false;
227  }
228 
229  value = p->second;
230  return true;
231 }
232 
233 } // namespace XrdCl
virtual XRootDStatus Rm(const std::string &path, ResponseHandler *handler, uint16_t timeout) override
virtual bool GetProperty(const std::string &name, std::string &value) const override
virtual XRootDStatus DirList(const std::string &path, DirListFlags::Flags flags, ResponseHandler *handler, uint16_t timeout) override
virtual XRootDStatus Mv(const std::string &source, const std::string &dest, ResponseHandler *handler, uint16_t timeout) override
virtual XRootDStatus Stat(const std::string &path, ResponseHandler *handler, uint16_t timeout) override
virtual XRootDStatus RmDir(const std::string &path, ResponseHandler *handler, uint16_t timeout) override
virtual XRootDStatus MkDir(const std::string &path, MkDirFlags::Flags flags, Access::Mode mode, ResponseHandler *handler, uint16_t timeout) override
HttpFileSystemPlugIn(const std::string &url)
virtual bool SetProperty(const std::string &name, const std::string &value) override
void Error(uint64_t topic, const char *format,...)
Report an error.
Definition: XrdClLog.cc:231
void Debug(uint64_t topic, const char *format,...)
Print a debug message.
Definition: XrdClLog.cc:282
Handle an async response.
virtual void HandleResponse(XRootDStatus *status, AnyObject *response)
Object stat info.
const std::string & GetHostName() const
Get the name of the target host.
Definition: XrdClURL.hh:165
const std::string & GetProtocol() const
Get the protocol.
Definition: XrdClURL.hh:113
std::string GetURL() const
Get the URL.
Definition: XrdClURL.hh:86
void SetPath(const std::string &path)
Set the path.
Definition: XrdClURL.hh:220
int GetPort() const
Get the target port.
Definition: XrdClURL.hh:183
XRootDStatus Unlink(Davix::DavPosix &davix_client, const std::string &url, uint16_t timeout)
XRootDStatus RmDir(Davix::DavPosix &davix_client, const std::string &path, uint16_t timeout)
std::pair< XrdCl::DirectoryList *, XrdCl::XRootDStatus > DirList(Davix::DavPosix &davix_client, const std::string &path, bool details, bool, uint16_t timeout)
XRootDStatus MkDir(Davix::DavPosix &davix_client, const std::string &path, XrdCl::MkDirFlags::Flags flags, XrdCl::Access::Mode, uint16_t timeout)
XRootDStatus Rename(Davix::DavPosix &davix_client, const std::string &source, const std::string &dest, uint16_t timeout)
XRootDStatus Stat(Davix::DavPosix &davix_client, const std::string &url, uint16_t timeout, StatInfo *stat_info)
Davix::Context * root_ctx_
Davix::DavPosix * root_davix_client_
void SetUpLogging(Log *logger)
static const uint64_t kLogXrdClHttp
Mode
Access mode.
@ Stat
Stat each entry.
@ Recursive
Do a recursive listing.