29constexpr int kDefaultBlockSize = 4096;
33 :
RRawFile(url, options), fFileDes(-1)
45 return std::make_unique<RRawFileUnix>(fUrl, fOptions);
51 int res = fstat(fFileDes, &info);
53 throw std::runtime_error(
"Cannot call fstat on '" + fUrl +
"', error: " + std::string(strerror(errno)));
59 static std::uint64_t szPageBitmap = sysconf(_SC_PAGESIZE) - 1;
60 mapdOffset = offset & ~szPageBitmap;
61 nbytes += offset & szPageBitmap;
63 void *result = mmap(
nullptr, nbytes, PROT_READ, MAP_PRIVATE, fFileDes, mapdOffset);
64 if (result == MAP_FAILED)
65 throw std::runtime_error(std::string(
"Cannot perform memory mapping: ") + strerror(errno));
71 fFileDes =
open(GetLocation(fUrl).c_str(), O_RDONLY);
73 throw std::runtime_error(
"Cannot open '" + fUrl +
"', error: " + std::string(strerror(errno)));
76 if (fOptions.fBlockSize >= 0)
80 int res = fstat(fFileDes, &info);
82 throw std::runtime_error(
"Cannot call fstat on '" + fUrl +
"', error: " + std::string(strerror(errno)));
84 if (info.st_blksize > 0) {
85 fOptions.fBlockSize = info.st_blksize;
87 fOptions.fBlockSize = kDefaultBlockSize;
93 size_t total_bytes = 0;
95 ssize_t res = pread(fFileDes, buffer, nbytes, offset);
99 throw std::runtime_error(
"Cannot read from '" + fUrl +
"', error: " + std::string(strerror(errno)));
100 }
else if (res == 0) {
103 R__ASSERT(
static_cast<size_t>(res) <= nbytes);
104 buffer =
reinterpret_cast<unsigned char *
>(buffer) + res;
114 int rv = munmap(region, nbytes);
116 throw std::runtime_error(std::string(
"Cannot remove memory mapping: ") + strerror(errno));
void UnmapImpl(void *region, size_t nbytes) final
Derived classes with mmap support must be able to unmap the memory area handed out by Map()
std::unique_ptr< RRawFile > Clone() const final
Create a new RawFile that accesses the same resource. The file pointer is reset to zero.
RRawFileUnix(std::string_view url, RRawFile::ROptions options)
std::uint64_t GetSizeImpl() final
Derived classes should return the file size or kUnknownFileSize.
void * MapImpl(size_t nbytes, std::uint64_t offset, std::uint64_t &mapdOffset) final
If a derived class supports mmap, the MapImpl and UnmapImpl calls are supposed to be implemented,...
void OpenImpl() final
OpenImpl() is called at most once and before any call to either DoReadAt or DoGetSize.
size_t ReadAtImpl(void *buffer, size_t nbytes, std::uint64_t offset) final
Derived classes should implement low-level reading without buffering.
The RRawFile provides read-only access to local and remote files.
basic_string_view< char > string_view
On construction, an ROptions parameter can customize the RRawFile behavior.