21#include <vrs/VrsExport.h>
22#include <vrs/WriteFileHandler.h>
23#include <vrs/os/Platform.h>
33template <
class FileChunk>
34class VRS_API DiskFileT :
public WriteFileHandler {
36 static const string& staticName();
39 ~DiskFileT()
override;
42 std::unique_ptr<FileHandler>
makeNew()
const override;
44 const string& getFileHandlerName()
const override;
69 int read(
void* buffer,
size_t length)
override;
71 template <typename T, std::enable_if_t<std::is_trivially_copyable<T>::value,
int> = 0>
73 return read(&
object,
sizeof(
object));
76 template <typename T, std::enable_if_t<std::is_trivially_copyable<T>::value,
int> = 0>
78 return read(vec.data(), vec.size() *
sizeof(T));
90 int write(
const void* buffer,
size_t length)
override;
92 template <typename T, std::enable_if_t<std::is_trivially_copyable<T>::value,
int> = 0>
94 return write(&
object,
sizeof(
object));
98 int overwrite(
const void* buffer,
size_t length)
override;
100 template <typename T, std::enable_if_t<std::is_trivially_copyable<T>::value,
int> = 0>
102 return overwrite(&
object,
sizeof(
object));
118 int getChunkRange(int64_t& outChunkOffset, int64_t& outChunkSize)
const override;
126 static int writeZstdFile(
const string& path,
const void* data,
size_t dataSize);
127 static int writeZstdFile(
const string& path,
const string&
string);
128 template <typename T, std::enable_if_t<std::is_trivially_copyable<T>::value,
int> = 0>
132 template <typename T, std::enable_if_t<std::is_trivially_copyable<T>::value,
int> = 0>
133 int writeZstdFile(
const string& path,
const vector<T>& v) {
134 return writeZstdFile(path, v.data(), v.size() *
sizeof(T));
138 static int readZstdFile(
const string& path,
string& outString);
139 template <typename T, std::enable_if_t<std::is_trivially_copyable<T>::value,
int> = 0>
144 static int readZstdFile(
const string& path,
void* data,
size_t dataSize);
158 int parseUri(
FileSpec& inOutFileSpec,
size_t colonIndex)
const override;
161 int checkChunks(
const vector<string>& chunks);
162 int openChunk(FileChunk* chunk);
163 int closeChunk(FileChunk* chunk);
164 int addChunk(
const string& chunkFilePath);
165 bool isLastChunk()
const;
166 bool trySetPosInCurrentChunk(int64_t offset);
169 std::unique_ptr<vector<FileChunk>> chunks_;
170 FileChunk* currentChunk_{};
171 int filesOpenCount_{};
173 size_t lastRWSize_{};
175 bool readOnly_{
true};
180#if (IS_LINUX_PLATFORM() && IS_X86_PLATFORM()) || IS_ANDROID_PLATFORM() || \
181 (IS_WINDOWS_PLATFORM() && IS_VRS_FB_INTERNAL())
182#define VRS_ASYNC_DISKFILE_SUPPORTED() true
184class AsyncDiskFileChunk;
188#define VRS_ASYNC_DISKFILE_SUPPORTED() false
212 int close()
override;
214 void abort()
override;
Definition DiskFile.h:207
int close() override
Definition DiskFile.cpp:641
int create(const string &newFilePath, const FileSpec::Extras &options={}) override
Definition DiskFile.cpp:636
void abort() override
Definition DiskFile.cpp:656
Definition DiskFileChunk.hpp:30
FileHandler implementation for disk files, with chunked file support.
Definition DiskFile.h:34
int read(T &object)
Helper to read trivially copyable objects, in a chunk aware way.
Definition DiskFile.h:72
int close() override
Close the file.
Definition DiskFile.cpp:72
static int writeTextFile(const string &path, const string &text)
Definition DiskFile.cpp:583
bool isEof() const override
Tell if we are at the end of the last chunk.
Definition DiskFile.cpp:341
static int writeZstdFile(const string &path, const void *data, size_t dataSize)
Definition DiskFile.cpp:497
int write(const void *buffer, size_t length) override
Write to the current chunk, possibly expanding it.
Definition DiskFile.cpp:265
int overwrite(const T &object)
Helper for trivially copyable objects.
Definition DiskFile.h:101
int openSpec(const FileSpec &fileSpec) override
Open a file in read-only mode.
Definition DiskFile.cpp:95
int64_t getTotalSize() const override
Get the total size of all the chunks considered.
Definition DiskFile.cpp:182
int getLastError() const override
Get the last error code. 0 means no error.
Definition DiskFile.cpp:336
std::unique_ptr< FileHandler > makeNew() const override
Make a new DiskFile object, with a default state.
Definition DiskFile.cpp:62
static string readTextFile(const string &path)
Definition DiskFile.cpp:567
int64_t getChunkPos() const override
Get position in the current chunk.
Definition DiskFile.cpp:460
bool isOpened() const override
Tell if a file is actually open.
Definition DiskFile.cpp:110
int truncate() override
Truncate chunk to the current file position. Use with care.
Definition DiskFile.cpp:316
int read(vector< T > &vec)
Helper to read a vector of trivially copyable objects, in a chunk aware way.
Definition DiskFile.h:77
int reopenForUpdates() override
Switch from read-only to read-write mode.
Definition DiskFile.cpp:243
bool reopenForUpdatesSupported() const override
Tell if modifying files is supported by this FileHandler implementation.
Definition DiskFile.cpp:238
int addChunk() override
Append a new chunk to the current file.
Definition DiskFile.cpp:379
int write(const T &object)
Helper for trivially copyable objects.
Definition DiskFile.h:93
int read(void *buffer, size_t length) override
Read a number of bytes, in a chunk aware way.
Definition DiskFile.cpp:201
static int readZstdFile(const string &path, void *data, size_t dataSize)
Read a compressed buffer of an exact size, fails if the size isn't perfectly right.
Definition DiskFile.cpp:548
bool getCurrentChunk(string &outChunkPath, size_t &outChunkIndex) const override
Get the path of the current chunk, or an empty string if no chunk is open.
Definition DiskFile.cpp:482
int setPos(int64_t offset) override
Set the file position at an arbitrary position, in a chunk aware way.
Definition DiskFile.cpp:147
int overwrite(const void *buffer, size_t length) override
Definition DiskFile.cpp:282
int64_t getPos() const override
Get the absolute position in the file, in a chunk aware way.
Definition DiskFile.cpp:453
size_t getLastRWSize() const override
Get the number of bytes actually moved by the last read or write operation.
Definition DiskFile.cpp:233
int create(const string &newFilePath, const FileSpec::Extras &options={}) override
Create a new file.
Definition DiskFile.cpp:115
int skipForward(int64_t offset) override
Skip a number of bytes further in the file, in a chunk aware way.
Definition DiskFile.cpp:134
bool isRemoteFileSystem() const override
Definition DiskFile.cpp:492
bool isReadOnly() const override
Find out if the file is in read-only mode.
Definition DiskFile.cpp:260
vector< std::pair< string, int64_t > > getFileChunks() const override
Get the list of chunks, path + size.
Definition DiskFile.cpp:191
static int readZstdFile(const string &path, vector< char > &outContent)
Read a compressed buffer or a string (automatically adjusts the size).
Definition DiskFile.cpp:538
void forgetFurtherChunks(int64_t fileSize) override
Call this method to forget any chunk beyond this file size.
Definition DiskFile.cpp:123
int getChunkRange(int64_t &outChunkOffset, int64_t &outChunkSize) const override
Get range of the current chunk.
Definition DiskFile.cpp:467
Definition Compressor.cpp:130
Generalized file descriptor class, allowing the efficient representation of complex file objects,...
Definition FileSpec.h:43
map< string, string, std::less<> > Extras
Definition FileSpec.h:46