Logo ROOT  
Reference Guide
RRawFileDavix.cxx
Go to the documentation of this file.
1// @(#)root/io:$Id$
2// Author: Jakob Blomer
3
4/*************************************************************************
5 * Copyright (C) 1995-2018, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
13#include "ROOT/RMakeUnique.hxx"
14
15#include <stdexcept>
16
17#include <davix.hpp>
18#include <sys/stat.h>
19
20namespace {
21constexpr int kDefaultBlockSize = 128 * 1024; // Read in relatively large 128k blocks for better network utilization
22} // anonymous namespace
23
24namespace ROOT {
25namespace Internal {
26
27struct RDavixFileDes {
28 RDavixFileDes() : fd(nullptr), pos(&ctx) {}
29 RDavixFileDes(const RDavixFileDes &) = delete;
30 RDavixFileDes &operator=(const RDavixFileDes &) = delete;
31 ~RDavixFileDes() = default;
32
33 DAVIX_FD *fd;
34 Davix::Context ctx;
35 Davix::DavPosix pos;
36};
37
38} // namespace Internal
39} // namespace ROOT
40
41
43 : RRawFile(url, options), fFileDes(new RDavixFileDes())
44{
45}
46
48{
49 if (fFileDes->fd != nullptr)
50 fFileDes->pos.close(fFileDes->fd, nullptr);
51}
52
53std::unique_ptr<ROOT::Internal::RRawFile> ROOT::Internal::RRawFileDavix::Clone() const
54{
55 return std::make_unique<RRawFileDavix>(fUrl, fOptions);
56}
57
59{
60 struct stat buf;
61 Davix::DavixError *err = nullptr;
62 if (fFileDes->pos.stat(nullptr, fUrl, &buf, &err) == -1) {
63 throw std::runtime_error("Cannot determine size of '" + fUrl + "', error: " + err->getErrMsg());
64 }
65 return buf.st_size;
66}
67
69{
70 Davix::DavixError *err = nullptr;
71 fFileDes->fd = fFileDes->pos.open(nullptr, fUrl, O_RDONLY, &err);
72 if (fFileDes->fd == nullptr) {
73 throw std::runtime_error("Cannot open '" + fUrl + "', error: " + err->getErrMsg());
74 }
75 if (fOptions.fBlockSize < 0)
76 fOptions.fBlockSize = kDefaultBlockSize;
77}
78
79size_t ROOT::Internal::RRawFileDavix::ReadAtImpl(void *buffer, size_t nbytes, std::uint64_t offset)
80{
81 Davix::DavixError *err = nullptr;
82 auto retval = fFileDes->pos.pread(fFileDes->fd, buffer, nbytes, offset, &err);
83 if (retval < 0) {
84 throw std::runtime_error("Cannot read from '" + fUrl + "', error: " + err->getErrMsg());
85 }
86 return static_cast<size_t>(retval);
87}
Binding & operator=(OUT(*fun)(void))
size_t ReadAtImpl(void *buffer, size_t nbytes, std::uint64_t offset) final
Derived classes should implement low-level reading without buffering.
std::unique_ptr< RRawFile > Clone() const final
Create a new RawFile that accesses the same resource. The file pointer is reset to zero.
RRawFileDavix(std::string_view url, RRawFile::ROptions options)
std::uint64_t GetSizeImpl() final
Derived classes should return the file size or kUnknownFileSize.
void OpenImpl() final
OpenImpl() is called at most once and before any call to either DoReadAt or DoGetSize.
The RRawFile provides read-only access to local and remote files.
Definition: RRawFile.hxx:40
basic_string_view< char > string_view
VSD Structures.
Definition: StringConv.hxx:21
On construction, an ROptions parameter can customize the RRawFile behavior.
Definition: RRawFile.hxx:54