Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
RRawFileCurl.cxx
Go to the documentation of this file.
1// @(#)root/net:$Id$
2// Author: Jakob Blomer
3
5#include "ROOT/RError.hxx"
7
8#include <utility>
9
10ROOT::Internal::RRawFileCurl::RRawFileCurl(std::string_view url, ROptions options) : RRawFile(url, options) {}
11
13
14std::unique_ptr<ROOT::Internal::RRawFile> ROOT::Internal::RRawFileCurl::Clone() const
15{
16 return std::make_unique<RRawFileCurl>(fUrl, fOptions);
17}
18
20{
21 fConnection = std::make_unique<RCurlConnection>(GetUrl());
22 fConnection->SetCredentialsFromEnvironment();
23 if (fOptions.fBlockSize == ROptions::kUseDefaultBlockSize)
24 fOptions.fBlockSize = kDefaultBlockSize;
25}
26
28{
29 std::uint64_t size;
30 auto status = fConnection->SendHeadReq(size);
31 if (!status)
32 throw RException(R__FAIL("cannot determine file size of " + GetUrl() + ": " + status.fStatusMsg));
33 return size;
34}
35
36std::size_t ROOT::Internal::RRawFileCurl::ReadAtImpl(void *buffer, std::size_t nbytes, std::uint64_t offset)
37{
39 range.fDestination = reinterpret_cast<unsigned char *>(buffer);
40 range.fOffset = offset;
41 range.fLength = nbytes;
42 auto status = fConnection->SendRangesReq(1, &range);
43 if (!status)
44 throw RException(R__FAIL("cannot read from " + GetUrl() + ": " + status.fStatusMsg));
45 return range.fNBytesRecv;
46}
47
48void ROOT::Internal::RRawFileCurl::ReadVImpl(RIOVec *ioVec, unsigned int nReq)
49{
50 if (nReq == 0)
51 return;
52
53 std::vector<RCurlConnection::RUserRange> ranges;
54 ranges.reserve(nReq);
55
56 for (unsigned int i = 0; i < nReq; ++i) {
58 range.fDestination = reinterpret_cast<unsigned char *>(ioVec[i].fBuffer);
59 range.fOffset = ioVec[i].fOffset;
60 range.fLength = ioVec[i].fSize;
61 ranges.emplace_back(range);
62 }
63
64 auto status = fConnection->SendRangesReq(ranges.size(), &ranges[0]);
65 if (!status)
66 throw RException(R__FAIL("cannot read from " + GetUrl() + ": " + status.fStatusMsg));
67
68 for (unsigned int i = 0; i < nReq; ++i) {
69 ioVec[i].fOutBytes = ranges[i].fNBytesRecv;
70 }
71}
72
#define R__FAIL(msg)
Short-hand to return an RResult<T> in an error state; the RError is implicitly converted into RResult...
Definition RError.hxx:299
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
Encapsulates a curl easy handle and provides an interface to send HTTP HEAD and (multi-)range queries...
std::unique_ptr< RCurlConnection > fConnection
static constexpr int kDefaultBlockSize
std::uint64_t GetSizeImpl() final
Derived classes should return the file size.
size_t ReadAtImpl(void *buffer, size_t nbytes, std::uint64_t offset) final
Derived classes should implement low-level reading without buffering.
void ReadVImpl(RIOVec *ioVec, unsigned int nReq) final
By default implemented as a loop of ReadAt calls but can be overwritten, e.g. XRootD or DAVIX impleme...
void OpenImpl() final
OpenImpl() is called at most once and before any call to either DoReadAt or DoGetSize.
RCurlConnection & GetConnection()
RRawFileCurl(std::string_view url, RRawFile::ROptions options)
std::unique_ptr< RRawFile > Clone() const final
Create a new RawFile that accesses the same resource. The file pointer is reset to zero.
RRawFile(std::string_view url, ROptions options)
Definition RRawFile.cxx:61
void EnsureOpen()
Open the file if not already open. Otherwise noop.
Definition RRawFile.cxx:88
std::string GetUrl() const
Returns the url of the file.
Definition RRawFile.cxx:123
Base class for all ROOT issued exceptions.
Definition RError.hxx:78
Caller-provided byte-range of the remote resource together with a pointer to a buffer.
Used for vector reads from multiple offsets into multiple buffers.
Definition RRawFile.hxx:61
std::size_t fOutBytes
The number of actually read bytes, set by ReadV().
Definition RRawFile.hxx:69
std::size_t fSize
The number of desired bytes.
Definition RRawFile.hxx:67
void * fBuffer
The destination for reading.
Definition RRawFile.hxx:63
std::uint64_t fOffset
The file offset.
Definition RRawFile.hxx:65
On construction, an ROptions parameter can customize the RRawFile behavior.
Definition RRawFile.hxx:49