Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 
12 #include "ROOT/RRawFileDavix.hxx"
13 #include "ROOT/RMakeUnique.hxx"
14 
15 #include <stdexcept>
16 
17 #include <davix.hpp>
18 #include <sys/stat.h>
19 
20 namespace {
21 constexpr int kDefaultBlockSize = 128 * 1024; // Read in relatively large 128k blocks for better network utilization
22 } // anonymous namespace
23 
24 namespace ROOT {
25 namespace Internal {
26 
27 struct 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 
42 ROOT::Internal::RRawFileDavix::RRawFileDavix(std::string_view url, ROptions options)
43  : RRawFile(url, options), fFileDes(new RDavixFileDes())
44 {
45 }
46 
47 ROOT::Internal::RRawFileDavix::~RRawFileDavix()
48 {
49  if (fFileDes->fd != nullptr)
50  fFileDes->pos.close(fFileDes->fd, nullptr);
51 }
52 
53 std::unique_ptr<ROOT::Internal::RRawFile> ROOT::Internal::RRawFileDavix::Clone() const
54 {
55  return std::make_unique<RRawFileDavix>(fUrl, fOptions);
56 }
57 
58 std::uint64_t ROOT::Internal::RRawFileDavix::GetSizeImpl()
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 
68 void ROOT::Internal::RRawFileDavix::OpenImpl()
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 
79 size_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 }