XRootD
Loading...
Searching...
No Matches
XrdClS3File.cc
Go to the documentation of this file.
1/******************************************************************************/
2/* Copyright (C) 2025, Pelican Project, Morgridge Institute for Research */
3/* */
4/* This file is part of the XrdClS3 client plugin for XRootD. */
5/* */
6/* XRootD is free software: you can redistribute it and/or modify it under */
7/* the terms of the GNU Lesser General Public License as published by the */
8/* Free Software Foundation, either version 3 of the License, or (at your */
9/* option) any later version. */
10/* */
11/* XRootD is distributed in the hope that it will be useful, but WITHOUT */
12/* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
13/* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
14/* License for more details. */
15/* */
16/* The copyright holder's institutional names and contributor's names may not */
17/* be used to endorse or promote products derived from this software without */
18/* specific prior written permission of the institution or contributor. */
19/******************************************************************************/
20
21#include "XrdClS3Factory.hh"
22#include "XrdClS3File.hh"
23
24#include <XrdCl/XrdClLog.hh>
25
26using namespace XrdClS3;
27
28namespace {
29
30class OpenResponseHandler : public XrdCl::ResponseHandler {
31public:
32 OpenResponseHandler(bool *is_opened, XrdCl::ResponseHandler *handler)
33 : m_is_opened(is_opened),
34 m_handler(handler)
35 {
36 }
37
38 virtual void HandleResponse(XrdCl::XRootDStatus *status, XrdCl::AnyObject *response) {
39 // Delete the handler; since we're injecting results a File object, no one owns us
40 std::unique_ptr<OpenResponseHandler> owner(this);
41
42 if (status && status->IsOK()) {
43 if (m_is_opened) *m_is_opened = true;
44 }
45 if (m_handler) m_handler->HandleResponse(status, response);
46 else delete response;
47 }
48
49private:
50 bool *m_is_opened;
51
52 // A reference to the handler we are wrapping. Note we don't own the handler
53 // so this is not a unique_ptr.
54 XrdCl::ResponseHandler *m_handler;
55};
56
57class CloseResponseHandler : public XrdCl::ResponseHandler {
58public:
59 CloseResponseHandler(bool *is_opened, XrdCl::ResponseHandler *handler)
60 : m_is_opened(is_opened),
61 m_handler(handler)
62 {
63 }
64
65 virtual void HandleResponse(XrdCl::XRootDStatus *status, XrdCl::AnyObject *response) {
66 std::unique_ptr<CloseResponseHandler> owner(this);
67
68 if (status && status->IsOK()) {
69 if (m_is_opened) *m_is_opened = false;
70 }
71 if (m_handler) m_handler->HandleResponse(status, response);
72 else delete response;
73 }
74
75private:
76 bool *m_is_opened;
77
78 // A reference to the handler we are wrapping. Note we don't own the handler
79 // so this is not a unique_ptr.
80 XrdCl::ResponseHandler *m_handler;
81};
82
83} // namespace
84
86 m_logger(log),
87 m_wrapped_file()
88{
89}
90
91File::~File() noexcept {}
92
95 time_t timeout)
96{
97 return m_wrapped_file->Close(new CloseResponseHandler(&m_is_opened, handler), timeout);
98}
99
100bool
102{
103 return m_is_opened;
104}
105
106bool
107File::GetProperty(const std::string &name,
108 std::string &value) const
109{
110 std::unique_lock lock(m_properties_mutex);
111 const auto p = m_properties.find(name);
112 if (p == std::end(m_properties)) {
113 return false;
114 }
115
116 value = p->second;
117 return true;
118}
119
120std::tuple<XrdCl::XRootDStatus, std::string, XrdCl::File*>
121File::GetFileHandle(const std::string &s3_url) {
122 if (m_wrapped_file) {
123 return std::make_tuple(XrdCl::XRootDStatus{}, m_url, m_wrapped_file.get());
124 }
125
126 std::string s3_noslash_url;
127 auto schema_loc = s3_url.find("://");
128 if (schema_loc != std::string::npos) {
129 auto path_loc = s3_url.find('/', schema_loc + 3);
130 if (path_loc != std::string::npos) {
131 if (s3_url.size() >= path_loc && s3_url[path_loc + 1] == '/') {
132 s3_noslash_url = s3_url.substr(0, path_loc) + s3_url.substr(path_loc + 1);
133 }
134 }
135 }
136
137 std::string https_url, err_msg;
138 if (!Factory::GenerateHttpUrl(s3_noslash_url.empty() ? s3_url : s3_noslash_url, https_url, nullptr, err_msg)) {
139 return std::make_tuple(XrdCl::XRootDStatus(XrdCl::stError, XrdCl::errInvalidAddr, 0, err_msg), "", nullptr);
140 }
141 auto loc = https_url.find('/', 8); // strlen("https://") -> 8
142 if (loc == std::string::npos) {
143 return std::make_tuple(XrdCl::XRootDStatus(XrdCl::stError, XrdCl::errInvalidAddr, 0, "Invalid generated URL"), "", nullptr);
144 }
145
146 XrdCl::URL url;
147 if (!url.FromString(https_url)) {
148 return std::make_tuple(XrdCl::XRootDStatus(XrdCl::stError, XrdCl::errInvalidAddr, 0, "Invalid generated XrdCl URL"), "", nullptr);
149 }
150 m_url = https_url;
151 std::unique_ptr<XrdCl::File> wrapped_file(new XrdCl::File(https_url));
152
153 std::stringstream ss;
154 ss << std::hex << reinterpret_cast<long long>(&m_header_callout);
155 if (!wrapped_file->SetProperty("XrdClHttpHeaderCallout", ss.str())) {
156 return std::make_tuple(XrdCl::XRootDStatus(XrdCl::stError, XrdCl::errInvalidAddr, 0, "Failed to setup header callout"), "", nullptr);
157 }
158 m_wrapped_file.reset(wrapped_file.release());
159
160 return std::make_tuple(XrdCl::XRootDStatus{}, https_url, m_wrapped_file.get());
161}
162
163XrdCl::XRootDStatus
164File::Open(const std::string &url,
167 XrdCl::ResponseHandler *handler,
168 time_t timeout)
169{
170 if (IsOpen()) {
171 m_logger->Error(kLogXrdClS3, "URL %s already open", url.c_str());
173 }
174
175 auto [st, https_url, fs] = GetFileHandle(url);
176 if (!st.IsOK()) {
177 return st;
178 }
179
180 return fs->Open(https_url, flags, mode, new OpenResponseHandler(&m_is_opened, handler), timeout);
181}
182
184File::PgRead(uint64_t offset,
185 uint32_t size,
186 void *buffer,
187 XrdCl::ResponseHandler *handler,
188 time_t timeout)
189{
190 return m_wrapped_file->PgRead(offset, size, buffer, handler, timeout);
191}
192
194File::Read(uint64_t offset,
195 uint32_t size,
196 void *buffer,
197 XrdCl::ResponseHandler *handler,
198 time_t timeout)
199{
200
201 return m_wrapped_file->Read(offset, size, buffer, handler, timeout);
202}
203
204bool
205File::SetProperty(const std::string &name,
206 const std::string &value)
207{
208 std::unique_lock lock(m_properties_mutex);
209 m_properties[name] = value;
210 return true;
211}
212
214File::Stat(bool force,
215 XrdCl::ResponseHandler *handler,
216 time_t timeout)
217{
218 return m_wrapped_file->Stat(force, handler, timeout);
219}
220
223 void *buffer,
224 XrdCl::ResponseHandler *handler,
225 time_t timeout )
226{
227 return m_wrapped_file->VectorRead(chunks, buffer, handler, timeout);
228}
229
231File::Write(uint64_t offset,
232 uint32_t size,
233 const void *buffer,
234 XrdCl::ResponseHandler *handler,
235 time_t timeout)
236{
237 return m_wrapped_file->Write(offset, size, buffer, handler, timeout);
238}
239
241File::Write(uint64_t offset,
242 XrdCl::Buffer &&buffer,
243 XrdCl::ResponseHandler *handler,
244 time_t timeout)
245{
246 return m_wrapped_file->Write(offset, std::move(buffer), handler, timeout);
247}
248
249std::shared_ptr<XrdClHttp::HeaderCallout::HeaderList>
250File::S3HeaderCallout::GetHeaders(const std::string &verb,
251 const std::string &url,
253{
254 std::string auth_token, err_msg;
255 std::shared_ptr<HeaderList> header_list(new HeaderList(headers));
256 if (Factory::GenerateV4Signature(url, verb, *header_list, auth_token, err_msg)) {
257 header_list->emplace_back("Authorization", auth_token);
258 } else {
259 m_parent.m_logger->Error(kLogXrdClS3, "Failed to generate V4 signature: %s", err_msg.c_str());
260 return nullptr;
261 }
262 return header_list;
263}
std::vector< std::pair< std::string, std::string > > HeaderList
static bool GenerateHttpUrl(const std::string &s3_url, std::string &https_url, std::string *obj_result, std::string &err_msg)
static bool GenerateV4Signature(const std::string &url, const std::string &verb, std::vector< std::pair< std::string, std::string > > &headers, std::string &auth_token, std::string &err_msg)
virtual XrdCl::XRootDStatus Open(const std::string &url, XrdCl::OpenFlags::Flags flags, XrdCl::Access::Mode mode, XrdCl::ResponseHandler *handler, time_t timeout) override
virtual bool SetProperty(const std::string &name, const std::string &value) override
virtual bool IsOpen() const override
virtual XrdCl::XRootDStatus VectorRead(const XrdCl::ChunkList &chunks, void *buffer, XrdCl::ResponseHandler *handler, time_t timeout) override
virtual XrdCl::XRootDStatus Write(uint64_t offset, uint32_t size, const void *buffer, XrdCl::ResponseHandler *handler, time_t timeout) override
virtual ~File() noexcept
virtual bool GetProperty(const std::string &name, std::string &value) const override
File(XrdCl::Log *log)
virtual XrdCl::XRootDStatus PgRead(uint64_t offset, uint32_t size, void *buffer, XrdCl::ResponseHandler *handler, time_t timeout) override
virtual XrdCl::XRootDStatus Read(uint64_t offset, uint32_t size, void *buffer, XrdCl::ResponseHandler *handler, time_t timeout) override
virtual XrdCl::XRootDStatus Stat(bool force, XrdCl::ResponseHandler *handler, time_t timeout) override
virtual XrdCl::XRootDStatus Close(XrdCl::ResponseHandler *handler, time_t timeout) override
Binary blob representation.
Handle diagnostics.
Definition XrdClLog.hh:101
void Error(uint64_t topic, const char *format,...)
Report an error.
Definition XrdClLog.cc:231
Handle an async response.
bool FromString(const std::string &url)
Parse a string and fill the URL fields.
Definition XrdClURL.cc:62
const uint64_t kLogXrdClS3
const uint16_t errInvalidAddr
const uint16_t stError
An error occurred that could potentially be retried.
const uint16_t errInvalidOp
std::vector< ChunkInfo > ChunkList
List of chunks.
Flags
Open flags, may be or'd when appropriate.
bool IsOK() const
We're fine.