Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
TCurlFile.cxx
Go to the documentation of this file.
1// @(#)root/net:$Id$
2// Author: Jakob Blomer
3
4/*************************************************************************
5 * Copyright (C) 1995-2025, 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 "TCurlFile.h"
13#include "TError.h"
14
15#include <cstdint>
16#include <cstring>
17#include <vector>
18
19TCurlFile::TCurlFile(const char *url, Option_t *opt)
20 : TFile(url, strstr(opt, "_WITHOUT_GLOBALREGISTRATION") != nullptr ? "WEB_WITHOUT_GLOBALREGISTRATION" : "WEB"),
21 fConnection(new ROOT::Internal::RCurlConnection(url))
22{
23 fConnection->SetCredentialsFromEnvironment();
25 fOffset = 0;
26 fD = -2; // so TFile::IsOpen() will return true when in ~TFile
27}
28
30{
31 std::uint64_t size;
32 auto status = fConnection->SendHeadReq(size);
33 if (!status)
34 return -1;
35 return size;
36}
37
39{
40 switch (pos) {
41 case kBeg: fOffset = offset + fArchiveOffset; break;
42 case kCur: fOffset += offset; break;
43 case kEnd:
44 // this option is not used currently in the ROOT code
46 Error("Seek", "seeking from end in archive is not (yet) supported");
47 fOffset = fEND - offset; // is fEND really EOF or logical EOF?
48 break;
49 }
50}
51
53{
55 range.fDestination = reinterpret_cast<unsigned char *>(buf);
56 range.fOffset = fOffset;
57 range.fLength = len;
58 auto status = fConnection->SendRangesReq(1, &range);
59 if (!status) {
60 Error("TCurlFile", "can not read data: %s", status.fStatusMsg.c_str());
61 return kTRUE;
62 }
63
64 fOffset += range.fNBytesRecv;
65 fBytesRead += range.fNBytesRecv;
66 fReadCalls++;
67#ifdef R__WIN32
68 SetFileBytesRead(GetFileBytesRead() + range.fNBytesRecv);
70#else
71 fgBytesRead += range.fNBytesRecv;
73#endif
74 return kFALSE;
75}
76
78{
80 range.fDestination = reinterpret_cast<unsigned char *>(buf);
81 range.fOffset = pos;
82 range.fLength = len;
83 auto status = fConnection->SendRangesReq(1, &range);
84 if (!status) {
85 Error("TCurlFile", "can not read data: %s", status.fStatusMsg.c_str());
86 return kTRUE;
87 }
88 fBytesRead += range.fNBytesRecv;
89 fReadCalls++;
90#ifdef R__WIN32
91 SetFileBytesRead(GetFileBytesRead() + range.fNBytesRecv);
93#else
94 fgBytesRead += range.fNBytesRecv;
96#endif
97 return kFALSE;
98}
99
101{
102 if (nbuf == 0)
103 return kFALSE;
104
105 std::vector<ROOT::Internal::RCurlConnection::RUserRange> ranges;
106 ranges.reserve(nbuf);
107 std::size_t bufPos = 0;
108 for (Int_t i = 0; i < nbuf; ++i) {
110 r.fDestination = reinterpret_cast<unsigned char *>(&buf[bufPos]);
111 r.fOffset = pos[i];
112 r.fLength = len[i];
113 ranges.emplace_back(r);
114 bufPos += len[i];
115 fBytesRead += r.fNBytesRecv;
116#ifdef R__WIN32
117 SetFileBytesRead(GetFileBytesRead() + r.fNBytesRecv);
118#else
119 fgBytesRead += r.fNBytesRecv;
120#endif
121 }
122
123 auto status = fConnection->SendRangesReq(nbuf, &ranges[0]);
124 if (!status) {
125 Error("TCurlFile", "can not read data: %s", status.fStatusMsg.c_str());
126 return kTRUE;
127 }
128 return kFALSE;
129}
ROOT::R::TRInterface & r
Definition Object.C:4
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
int Int_t
Signed integer 4 bytes (int).
Definition RtypesCore.h:59
bool Bool_t
Boolean (0=false, 1=true) (bool).
Definition RtypesCore.h:77
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
long long Long64_t
Portable signed long integer 8 bytes.
Definition RtypesCore.h:83
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
const char Option_t
Option string (const char).
Definition RtypesCore.h:80
Error("WriteTObject","The current directory (%s) is not associated with a file. The object (%s) has not been written.", GetName(), objname)
TCurlFile(const char *url, Option_t *option="")
Definition TCurlFile.cxx:19
std::unique_ptr< ROOT::Internal::RCurlConnection > fConnection
Definition TCurlFile.h:21
Bool_t ReadBuffer(char *buf, Int_t len) override
Read a buffer from the file.
Definition TCurlFile.cxx:52
Long64_t GetSize() const override
Returns the current file size.
Definition TCurlFile.cxx:29
Bool_t ReadBuffers(char *buf, Long64_t *pos, Int_t *len, Int_t nbuf) override
Read the nbuf blocks described in arrays pos and len.
void Seek(Long64_t offset, ERelativeTo pos=kBeg) override
Seek to a specific position in the file. Pos it either kBeg, kCur or kEnd.
Definition TCurlFile.cxx:38
static std::atomic< Long64_t > fgBytesRead
Number of bytes read by all TFile objects.
Definition TFile.h:183
Int_t fReadCalls
Number of read calls ( not counting the cache calls ).
Definition TFile.h:167
Long64_t fBytesRead
Number of bytes read from this file.
Definition TFile.h:154
static void SetFileBytesRead(Long64_t bytes=0)
Definition TFile.cxx:4311
static void SetFileReadCalls(Int_t readcalls=0)
Definition TFile.cxx:4317
static Long64_t GetFileBytesRead()
Static function returning the total number of bytes read from all files.
Definition TFile.cxx:4277
Long64_t fArchiveOffset
!Offset at which file starts in archive
Definition TFile.h:179
virtual void Init(Bool_t create)
Initialize a TFile object.
Definition TFile.cxx:640
TFile(const TFile &)=delete
ERelativeTo
Definition TFile.h:277
@ kCur
Definition TFile.h:277
@ kBeg
Definition TFile.h:277
@ kEnd
Definition TFile.h:277
Int_t fD
File descriptor.
Definition TFile.h:160
Long64_t fOffset
!Seek offset cache
Definition TFile.h:174
Long64_t fEND
Last used byte in file.
Definition TFile.h:157
static std::atomic< Int_t > fgReadCalls
Number of bytes read from all TFile objects.
Definition TFile.h:186
static Int_t GetFileReadCalls()
Static function returning the total number of read calls from all files.
Definition TFile.cxx:4294
Small utility to parse cmdline options.
Definition RExports.h:71
Caller-provided byte-range of the remote resource together with a pointer to a buffer.