21#include <vrs/WriteFileHandler.h>
22#include <vrs/os/Platform.h>
32template <
class FileChunk>
35 static const std::string& staticName();
41 std::unique_ptr<FileHandler>
makeNew()
const override;
43 const string& getFileHandlerName()
const override;
52 int create(
const string& newFilePath,
const map<string, string>& options = {})
override;
58 vector<std::pair<string, int64_t>>
getFileChunks()
const override;
65 int setPos(int64_t offset)
override;
68 int read(
void* buffer,
size_t length)
override;
70 template <typename T, std::enable_if_t<std::is_trivially_copyable<T>::value,
int> = 0>
72 return read(&
object,
sizeof(
object));
84 int write(
const void* buffer,
size_t length)
override;
86 template <typename T, std::enable_if_t<std::is_trivially_copyable<T>::value,
int> = 0>
88 return write(&
object,
sizeof(
object));
92 int overwrite(
const void* buffer,
size_t length)
override;
94 template <typename T, std::enable_if_t<std::is_trivially_copyable<T>::value,
int> = 0>
96 return overwrite(&
object,
sizeof(
object));
106 bool isEof()
const override;
108 int64_t
getPos()
const override;
112 int getChunkRange(int64_t& outChunkOffset, int64_t& outChunkSize)
const override;
114 bool getCurrentChunk(
string& outChunkPath,
size_t& outChunkIndex)
const override;
120 static int writeZstdFile(
const string& path,
const void* data,
size_t dataSize);
121 static int writeZstdFile(
const string& path,
const string&
string);
122 template <typename T, std::enable_if_t<std::is_trivially_copyable<T>::value,
int> = 0>
126 template <typename T, std::enable_if_t<std::is_trivially_copyable<T>::value,
int> = 0>
131 static int readZstdFile(
const string& path, vector<char>& outContent);
132 static int readZstdFile(
const string& path,
string& outString);
133 template <typename T, std::enable_if_t<std::is_trivially_copyable<T>::value,
int> = 0>
138 static int readZstdFile(
const string& path,
void* data,
size_t dataSize);
150 static int writeTextFile(
const string& path,
const string& text);
152 int parseUri(FileSpec& inOutFileSpec,
size_t colonIndex)
const override;
155 int checkChunks(
const vector<string>& chunks);
156 int openChunk(FileChunk* chunk);
157 int closeChunk(FileChunk* chunk);
158 int addChunk(
const string& chunkFilePath);
159 bool isLastChunk()
const;
160 bool trySetPosInCurrentChunk(int64_t offset);
162 map<string, string> options_;
163 std::unique_ptr<vector<FileChunk>> chunks_;
164 FileChunk* currentChunk_{};
165 int filesOpenCount_{};
167 size_t lastRWSize_{};
169 bool readOnly_{
true};
172using DiskFile = DiskFileT<DiskFileChunk>;
174#if (IS_LINUX_PLATFORM() && IS_X86_PLATFORM()) || (IS_WINDOWS_PLATFORM() && IS_VRS_FB_INTERNAL())
175#define VRS_ASYNC_DISKFILE_SUPPORTED() true
177class AsyncDiskFileChunk;
178using AsyncDiskFile = DiskFileT<AsyncDiskFileChunk>;
181#define VRS_ASYNC_DISKFILE_SUPPORTED() false
204 int create(
const string& newFilePath,
const map<string, string>& options = {})
override;
205 int close()
override;
Definition DiskFile.h:200
int close() override
Definition DiskFile.cpp:638
int create(const string &newFilePath, const map< string, string > &options={}) override
Definition DiskFile.cpp:633
FileHandler implementation for disk files, with chunked file support.
Definition DiskFile.h:33
int read(T &object)
Helper to read trivially copyable objects, in a chunk aware way.
Definition DiskFile.h:71
int close() override
Close the file.
Definition DiskFile.cpp:69
static int writeTextFile(const string &path, const string &text)
Definition DiskFile.cpp:580
int create(const string &newFilePath, const map< string, string > &options={}) override
Create a new file.
Definition DiskFile.cpp:112
bool isEof() const override
Tell if we are at the end of the last chunk.
Definition DiskFile.cpp:338
static int writeZstdFile(const string &path, const void *data, size_t dataSize)
Definition DiskFile.cpp:494
int write(const void *buffer, size_t length) override
Write to the current chunk, possibly expanding it.
Definition DiskFile.cpp:262
int overwrite(const T &object)
Helper for trivially copyable objects.
Definition DiskFile.h:95
int openSpec(const FileSpec &fileSpec) override
Open a file in read-only mode.
Definition DiskFile.cpp:92
int64_t getTotalSize() const override
Get the total size of all the chunks considered.
Definition DiskFile.cpp:179
int getLastError() const override
Get the last error code. 0 means no error.
Definition DiskFile.cpp:333
std::unique_ptr< FileHandler > makeNew() const override
Make a new DiskFile object, with a default state.
Definition DiskFile.cpp:59
static string readTextFile(const string &path)
Definition DiskFile.cpp:564
int64_t getChunkPos() const override
Get position in the current chunk.
Definition DiskFile.cpp:457
bool isOpened() const override
Tell if a file is actually open.
Definition DiskFile.cpp:107
int truncate() override
Truncate chunk to the current file position. Use with care.
Definition DiskFile.cpp:313
int reopenForUpdates() override
Switch from read-only to read-write mode.
Definition DiskFile.cpp:240
bool reopenForUpdatesSupported() const override
Tell if modifying files is supported by this FileHandler implementation.
Definition DiskFile.cpp:235
int addChunk() override
Append a new chunk to the current file.
Definition DiskFile.cpp:376
int write(const T &object)
Helper for trivially copyable objects.
Definition DiskFile.h:87
int read(void *buffer, size_t length) override
Read a number of bytes, in a chunk aware way.
Definition DiskFile.cpp:198
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:479
int setPos(int64_t offset) override
Set the file position at an arbitrary position, in a chunk aware way.
Definition DiskFile.cpp:144
int overwrite(const void *buffer, size_t length) override
Definition DiskFile.cpp:279
int64_t getPos() const override
Get the absolute position in the file, in a chunk aware way.
Definition DiskFile.cpp:450
size_t getLastRWSize() const override
Get the number of bytes actually moved by the last read or write operation.
Definition DiskFile.cpp:230
int skipForward(int64_t offset) override
Skip a number of bytes further in the file, in a chunk aware way.
Definition DiskFile.cpp:131
bool isRemoteFileSystem() const override
Definition DiskFile.cpp:489
bool isReadOnly() const override
Find out if the file is in read-only mode.
Definition DiskFile.cpp:257
vector< std::pair< string, int64_t > > getFileChunks() const override
Get the list of chunks, path + size.
Definition DiskFile.cpp:188
static int readZstdFile(const string &path, vector< char > &outContent)
Read a compressed buffer or a string (automatically adjusts the size)
Definition DiskFile.cpp:535
void forgetFurtherChunks(int64_t fileSize) override
Call this method to forget any chunk beyond this file size.
Definition DiskFile.cpp:120
int getChunkRange(int64_t &outChunkOffset, int64_t &outChunkSize) const override
Get range of the current chunk.
Definition DiskFile.cpp:464
The WriteFileHandler interface adds write operations to the FileHandler interface.
Definition WriteFileHandler.h:45
Definition Compressor.cpp:112
Generalized file descriptor class, allowing the efficient representation of complex file objects,...
Definition FileSpec.h:37