VRS
A file format for sensor data.
Loading...
Searching...
No Matches
DiskFile.h
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <cstdio>
20
21#include <vrs/WriteFileHandler.h>
22#include <vrs/os/Platform.h>
23
24namespace vrs {
25
26using std::string;
27using std::vector;
28
29class DiskFileChunk;
30
32template <class FileChunk>
34 public:
35 static const string& staticName();
36
37 DiskFileT();
38 ~DiskFileT() override;
39
41 std::unique_ptr<FileHandler> makeNew() const override;
42
43 const string& getFileHandlerName() const override;
44
46 int openSpec(const FileSpec& fileSpec) override;
47
49 bool isOpened() const override;
50
52 int create(const string& newFilePath, const FileSpec::Extras& options = {}) override;
54 void forgetFurtherChunks(int64_t fileSize) override;
56 int64_t getTotalSize() const override;
58 vector<std::pair<string, int64_t>> getFileChunks() const override;
60 int close() override;
61
63 int skipForward(int64_t offset) override;
65 int setPos(int64_t offset) override;
66
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>
71 int read(T& object) {
72 return read(&object, sizeof(object));
73 }
75 template <typename T, std::enable_if_t<std::is_trivially_copyable<T>::value, int> = 0>
76 int read(vector<T>& vec) {
77 return read(vec.data(), vec.size() * sizeof(T));
78 }
80 size_t getLastRWSize() const override;
81
83 bool reopenForUpdatesSupported() const override;
85 int reopenForUpdates() override;
87 bool isReadOnly() const override;
89 int write(const void* buffer, size_t length) override;
91 template <typename T, std::enable_if_t<std::is_trivially_copyable<T>::value, int> = 0>
92 int write(const T& object) {
93 return write(&object, sizeof(object));
94 }
97 int overwrite(const void* buffer, size_t length) override;
99 template <typename T, std::enable_if_t<std::is_trivially_copyable<T>::value, int> = 0>
100 int overwrite(const T& object) {
101 return overwrite(&object, sizeof(object));
102 }
104 int addChunk() override;
106 int truncate() override;
107
109 int getLastError() const override;
111 bool isEof() const override;
113 int64_t getPos() const override;
115 int64_t getChunkPos() const override;
117 int getChunkRange(int64_t& outChunkOffset, int64_t& outChunkSize) const override;
119 bool getCurrentChunk(string& outChunkPath, size_t& outChunkIndex) const override;
120
121 bool isRemoteFileSystem() const override;
122
125 static int writeZstdFile(const string& path, const void* data, size_t dataSize);
126 static int writeZstdFile(const string& path, const string& string);
127 template <typename T, std::enable_if_t<std::is_trivially_copyable<T>::value, int> = 0>
128 int writeZstdFile(const string& path, const T& object) {
129 return writeZstdFile(path, &object, sizeof(T));
130 }
131 template <typename T, std::enable_if_t<std::is_trivially_copyable<T>::value, int> = 0>
132 int writeZstdFile(const string& path, const vector<T>& v) {
133 return writeZstdFile(path, v.data(), v.size() * sizeof(T));
134 }
136 static int readZstdFile(const string& path, vector<char>& outContent);
137 static int readZstdFile(const string& path, string& outString);
138 template <typename T, std::enable_if_t<std::is_trivially_copyable<T>::value, int> = 0>
139 int readZstdFile(const string& path, T& object) {
140 return readZstdFile(path, &object, sizeof(T));
141 }
143 static int readZstdFile(const string& path, void* data, size_t dataSize);
144
150 static string readTextFile(const string& path);
155 static int writeTextFile(const string& path, const string& text);
156
157 int parseUri(FileSpec& inOutFileSpec, size_t colonIndex) const override;
158
159 protected:
160 int checkChunks(const vector<string>& chunks);
161 int openChunk(FileChunk* chunk);
162 int closeChunk(FileChunk* chunk);
163 int addChunk(const string& chunkFilePath);
164 bool isLastChunk() const;
165 bool trySetPosInCurrentChunk(int64_t offset);
166
167 FileSpec::Extras options_; // optional options provided in openSpec or createFile
168 std::unique_ptr<vector<FileChunk>> chunks_; // all the chunks, when a VRS file is opened.
169 FileChunk* currentChunk_{}; // always points to the current chunk within chunks_.
170 int filesOpenCount_{};
171
172 size_t lastRWSize_{};
173 int lastError_{};
174 bool readOnly_{true};
175};
176
177using DiskFile = DiskFileT<DiskFileChunk>;
178
179#if (IS_LINUX_PLATFORM() && IS_X86_PLATFORM()) || (IS_WINDOWS_PLATFORM() && IS_VRS_FB_INTERNAL())
180#define VRS_ASYNC_DISKFILE_SUPPORTED() true
181
182class AsyncDiskFileChunk;
183using AsyncDiskFile = DiskFileT<AsyncDiskFileChunk>;
184
185#else
186#define VRS_ASYNC_DISKFILE_SUPPORTED() false
187
188#endif
189
205class AtomicDiskFile : public DiskFile {
206 public:
207 ~AtomicDiskFile() override;
208
209 int create(const string& newFilePath, const FileSpec::Extras& options = {}) override;
210 int close() override;
211
212 void abort();
213
214 private:
215 string finalName_;
216};
217
218} // namespace vrs
Definition DiskFile.h:205
int close() override
Definition DiskFile.cpp:639
int create(const string &newFilePath, const FileSpec::Extras &options={}) override
Definition DiskFile.cpp:634
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:70
static int writeTextFile(const string &path, const string &text)
Definition DiskFile.cpp:581
bool isEof() const override
Tell if we are at the end of the last chunk.
Definition DiskFile.cpp:339
static int writeZstdFile(const string &path, const void *data, size_t dataSize)
Definition DiskFile.cpp:495
int write(const void *buffer, size_t length) override
Write to the current chunk, possibly expanding it.
Definition DiskFile.cpp:263
int overwrite(const T &object)
Helper for trivially copyable objects.
Definition DiskFile.h:100
int openSpec(const FileSpec &fileSpec) override
Open a file in read-only mode.
Definition DiskFile.cpp:93
int64_t getTotalSize() const override
Get the total size of all the chunks considered.
Definition DiskFile.cpp:180
int getLastError() const override
Get the last error code. 0 means no error.
Definition DiskFile.cpp:334
std::unique_ptr< FileHandler > makeNew() const override
Make a new DiskFile object, with a default state.
Definition DiskFile.cpp:60
static string readTextFile(const string &path)
Definition DiskFile.cpp:565
int64_t getChunkPos() const override
Get position in the current chunk.
Definition DiskFile.cpp:458
bool isOpened() const override
Tell if a file is actually open.
Definition DiskFile.cpp:108
int truncate() override
Truncate chunk to the current file position. Use with care.
Definition DiskFile.cpp:314
int read(vector< T > &vec)
Helper to read a vector of trivially copyable objects, in a chunk aware way.
Definition DiskFile.h:76
int reopenForUpdates() override
Switch from read-only to read-write mode.
Definition DiskFile.cpp:241
bool reopenForUpdatesSupported() const override
Tell if modifying files is supported by this FileHandler implementation.
Definition DiskFile.cpp:236
int addChunk() override
Append a new chunk to the current file.
Definition DiskFile.cpp:377
int write(const T &object)
Helper for trivially copyable objects.
Definition DiskFile.h:92
int read(void *buffer, size_t length) override
Read a number of bytes, in a chunk aware way.
Definition DiskFile.cpp:199
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:480
int setPos(int64_t offset) override
Set the file position at an arbitrary position, in a chunk aware way.
Definition DiskFile.cpp:145
int overwrite(const void *buffer, size_t length) override
Definition DiskFile.cpp:280
int64_t getPos() const override
Get the absolute position in the file, in a chunk aware way.
Definition DiskFile.cpp:451
size_t getLastRWSize() const override
Get the number of bytes actually moved by the last read or write operation.
Definition DiskFile.cpp:231
int create(const string &newFilePath, const FileSpec::Extras &options={}) override
Create a new file.
Definition DiskFile.cpp:113
int skipForward(int64_t offset) override
Skip a number of bytes further in the file, in a chunk aware way.
Definition DiskFile.cpp:132
bool isRemoteFileSystem() const override
Definition DiskFile.cpp:490
bool isReadOnly() const override
Find out if the file is in read-only mode.
Definition DiskFile.cpp:258
vector< std::pair< string, int64_t > > getFileChunks() const override
Get the list of chunks, path + size.
Definition DiskFile.cpp:189
static int readZstdFile(const string &path, vector< char > &outContent)
Read a compressed buffer or a string (automatically adjusts the size)
Definition DiskFile.cpp:536
void forgetFurtherChunks(int64_t fileSize) override
Call this method to forget any chunk beyond this file size.
Definition DiskFile.cpp:121
int getChunkRange(int64_t &outChunkOffset, int64_t &outChunkSize) const override
Get range of the current chunk.
Definition DiskFile.cpp:465
The WriteFileHandler interface adds write operations to the FileHandler interface.
Definition WriteFileHandler.h:42
Definition Compressor.cpp:113
Generalized file descriptor class, allowing the efficient representation of complex file objects,...
Definition FileSpec.h:40
map< string, string, std::less<> > Extras
Definition FileSpec.h:43