8
8
#pragma once
9
9
10
10
#include < functional>
11
- #include < inmemory_filesystem_metadata.hpp>
12
11
#include < memory>
13
- #include < memory_buffer.hpp>
14
12
#include < optional>
15
13
#include < stdio.h>
16
14
#include < string>
17
15
#include < system_error>
18
16
17
+ #include " inmemory_filesystem_metadata.hpp"
18
+ #include " memory_buffer.hpp"
19
+
19
20
namespace inmemoryfs {
20
21
21
22
// / A class representing an in-memory file system.
@@ -29,36 +30,36 @@ class InMemoryFileSystem final {
29
30
DirectoryExpected, // If path is not a directory.
30
31
FileExpected, // If the path is not a file.
31
32
};
32
-
33
+
33
34
// / Options for loading file content.
34
35
enum class FileLoadOption : int8_t {
35
36
Malloc = 1 , // Copy file contents into memory.
36
37
MMap, // Memory map file contents.
37
38
LazyMMap // Memory map file contents but lazily.
38
39
};
39
-
40
+
40
41
// / The error category for `InMemoryFileSystem`.
41
42
struct ErrorCategory final : public std::error_category {
42
43
public:
43
44
inline const char * name () const noexcept override {
44
45
return " InMemoryFileSystem" ;
45
46
}
46
-
47
+
47
48
std::string message (int code) const override ;
48
49
};
49
-
50
+
50
51
struct Attributes {
51
52
time_t modificationTime;
52
-
53
+
53
54
inline Attributes () noexcept :
54
55
modificationTime(time(0 ))
55
56
{}
56
57
};
57
-
58
+
58
59
using MetadataWriter = std::function<bool (const InMemoryFileSystemMetadata&, std::ostream&)>;
59
60
using MetadataWriterInMemory = std::function<size_t (const InMemoryFileSystemMetadata&, void *)>;
60
61
using MetadataReader = std::function<std::optional<InMemoryFileSystemMetadata>(std::istream&)>;
61
-
62
+
62
63
// / A class representing an in-memory node. This could either be a file node or a directory node.
63
64
class InMemoryNode {
64
65
public:
@@ -67,7 +68,7 @@ class InMemoryFileSystem final {
67
68
File = 0 , // / Node is a File.
68
69
Directory // / Node is a Directory.
69
70
};
70
-
71
+
71
72
// / Constructs an in-memory node instance.
72
73
// /
73
74
// / @param name The name of the Node. It must be unique in the enclosing Directory.
@@ -78,38 +79,38 @@ class InMemoryFileSystem final {
78
79
attributes_(std::move(attributes)),
79
80
kind_(kind)
80
81
{}
81
-
82
+
82
83
InMemoryNode (InMemoryNode const &) = delete ;
83
84
InMemoryNode& operator =(InMemoryNode const &) = delete ;
84
-
85
+
85
86
inline virtual ~InMemoryNode () {}
86
-
87
+
87
88
// / Returns the node attributes.
88
89
inline Attributes attributes () const noexcept {
89
90
return attributes_;
90
91
}
91
-
92
+
92
93
// / Sets the node attributes.
93
94
// /
94
95
// / @param attributes The node attributes.
95
96
inline void set_attributes (Attributes attributes) noexcept {
96
97
attributes_ = std::move (attributes);
97
98
}
98
-
99
+
99
100
// / Returns the node kind, possible values are `File` and `Directory`.
100
101
inline Kind kind () const noexcept {
101
102
return kind_;
102
103
}
103
-
104
+
104
105
// / Returns the name of the node.
105
106
inline const std::string& name () const noexcept {
106
107
return name_;
107
108
}
108
-
109
+
109
110
inline void set_name (std::string name) noexcept {
110
111
std::swap (name_, name);
111
112
}
112
-
113
+
113
114
// / Returns `true` if the node is a directory otherwise `false`.
114
115
inline bool isDirectory () const noexcept {
115
116
switch (kind_) {
@@ -119,58 +120,58 @@ class InMemoryFileSystem final {
119
120
return false ;
120
121
}
121
122
}
122
-
123
+
123
124
// / Returns `true` if the node is a file otherwise `false`.
124
125
inline bool isFile () const noexcept {
125
126
return !isDirectory ();
126
127
}
127
-
128
+
128
129
private:
129
130
std::string name_;
130
131
InMemoryFileSystem::Attributes attributes_;
131
132
const Kind kind_;
132
133
};
133
-
134
+
134
135
// / Constructs an`InMemoryFileSystem` instance with an empty root and the specified name.
135
136
// /
136
137
// / @param rootName The name of the root node.
137
138
explicit InMemoryFileSystem (std::string rootName = " root" ) noexcept ;
138
-
139
+
139
140
// / Constructs an`InMemoryFileSystem` instance with the specified root.
140
141
// /
141
142
// / @param root The root node.
142
143
explicit InMemoryFileSystem (std::unique_ptr<InMemoryNode> root) noexcept
143
144
:root_(std::move(root))
144
145
{}
145
-
146
+
146
147
InMemoryFileSystem (InMemoryFileSystem const &) = delete ;
147
148
InMemoryFileSystem& operator =(InMemoryFileSystem const &) = delete ;
148
-
149
+
149
150
virtual ~InMemoryFileSystem () {}
150
-
151
+
151
152
// / Returns the root.
152
153
InMemoryNode *root () const noexcept {
153
154
return root_.get ();
154
155
}
155
-
156
+
156
157
// / Checks if the node at the specified path is a directory.
157
158
// /
158
159
// / @param canonical_path The path components from the root.
159
160
// / @retval `true` if the node at the specified path is a directory otherwise `false`.
160
161
bool is_directory (const std::vector<std::string>& canonical_path) noexcept ;
161
-
162
+
162
163
// / Checks if the node at the specified path is a file.
163
164
// /
164
165
// / @param canonical_path The path components from the root.
165
166
// / @retval `true` if the node at the specified path is a file otherwise `false`.
166
167
bool is_file (const std::vector<std::string>& canonical_path) noexcept ;
167
-
168
+
168
169
// / Checks if the node at the specified path exists.
169
170
// /
170
171
// / @param canonical_path The path components from the root.
171
172
// / @retval `true` if the node at the specified path exists.
172
173
bool exists (const std::vector<std::string>& canonical_path) const noexcept ;
173
-
174
+
174
175
// / Retrieves the canonical path of all the child nodes at the specified path. The node
175
176
// / at the specified path must be a directory otherwise it returns an empty vector with the `error`
176
177
// / populated.
@@ -180,23 +181,23 @@ class InMemoryFileSystem final {
180
181
// / @retval paths to all the items at the specified path.
181
182
std::vector<std::vector<std::string>> get_item_paths (const std::vector<std::string>& canonical_path,
182
183
std::error_code& error) const noexcept ;
183
-
184
+
184
185
// / Retrieves the attributes of the item at the specified path.
185
186
// /
186
187
// / @param canonical_path The path components from the root.
187
188
// / @param error On failure, error is populated with the failure reason.
188
189
// / @retval The item attributes at the specified path.
189
190
std::optional<Attributes> get_attributes (const std::vector<std::string>& canonical_path,
190
191
std::error_code& error) const noexcept ;
191
-
192
+
192
193
// / Retrieves the contents of the file at the specified path.
193
194
// /
194
195
// / @param canonical_path The path components from the root.
195
196
// / @param error On failure, error is populated with the failure reason.
196
197
// / @retval The file contents or `nullptr` if the item at the specified path is not a file.
197
198
std::shared_ptr<MemoryBuffer> get_file_content (const std::vector<std::string>& canonical_path,
198
199
std::error_code& error) const noexcept ;
199
-
200
+
200
201
// / Creates an in-memory directory at the specified path.
201
202
// /
202
203
// / @param canonical_path The path components from the root.
@@ -208,7 +209,7 @@ class InMemoryFileSystem final {
208
209
Attributes attributes,
209
210
bool create_intermediate_directories,
210
211
std::error_code& error) noexcept ;
211
-
212
+
212
213
// / Creates an in-memory file at the specified path.
213
214
// /
214
215
// / @param canonical_path The path components from the root.
@@ -222,15 +223,15 @@ class InMemoryFileSystem final {
222
223
Attributes attributes,
223
224
bool overwrite,
224
225
std::error_code& error) noexcept ;
225
-
226
+
226
227
// / Removes the item at the specified path.
227
228
// /
228
229
// / @param canonical_path The path components from the root.
229
230
// / @param error On failure, error is populated with the failure reason.
230
231
// / @retval `true` if the item is removed otherwise `false`.
231
232
bool remove_item (const std::vector<std::string>& canonical_path,
232
233
std::error_code& error) noexcept ;
233
-
234
+
234
235
// / Sets the attributes at the specified path.
235
236
// /
236
237
// / @param canonical_path The path components from the root.
@@ -239,7 +240,7 @@ class InMemoryFileSystem final {
239
240
bool set_attributes (const std::vector<std::string>& canonical_path,
240
241
Attributes attributes,
241
242
std::error_code& error) noexcept ;
242
-
243
+
243
244
// / Writes the item at the specified path to the filesystem.
244
245
// /
245
246
// / @param canonical_path The path components from the root.
@@ -251,7 +252,7 @@ class InMemoryFileSystem final {
251
252
const std::string& dst_path,
252
253
bool recursive,
253
254
std::error_code& error) const noexcept ;
254
-
255
+
255
256
// / Renames the item at the specified path, if there is already an item with the same name then
256
257
// / the rename would fail.
257
258
// /
@@ -262,7 +263,7 @@ class InMemoryFileSystem final {
262
263
bool rename_item (const std::vector<std::string>& canonical_path,
263
264
const std::string& name,
264
265
std::error_code& error) noexcept ;
265
-
266
+
266
267
// / Creates an`InMemoryFileSystem` from the filesystem path.
267
268
// /
268
269
// / The structure of the `InMemoryFileSystem` is identical to the structure of the filesystem at the
@@ -275,7 +276,7 @@ class InMemoryFileSystem final {
275
276
static std::unique_ptr<InMemoryFileSystem> make_from_directory (const std::string& path,
276
277
FileLoadOption option,
277
278
std::error_code& error) noexcept ;
278
-
279
+
279
280
// / Serializes the item at the specified path and writes it to the stream.
280
281
// /
281
282
// / The structure of the `InMemoryFileSystem` is identical to the structure of the filesystem at the
@@ -292,7 +293,7 @@ class InMemoryFileSystem final {
292
293
const MetadataWriter& metadata_writer,
293
294
std::ostream& ostream,
294
295
std::error_code& error) const noexcept ;
295
-
296
+
296
297
// / Serializes the item at the specified path and writes it to the stream.
297
298
// /
298
299
// / The structure of the `InMemoryFileSystem` is identical to the structure of the filesystem at the
@@ -309,7 +310,7 @@ class InMemoryFileSystem final {
309
310
const MetadataWriterInMemory& metadata_writer,
310
311
void *dst,
311
312
std::error_code& error) const noexcept ;
312
-
313
+
313
314
// / Computes the size of the buffer that would be needed to serialized the item at the specified path.
314
315
// /
315
316
// / @param canonical_path The path components from the root.
@@ -319,15 +320,15 @@ class InMemoryFileSystem final {
319
320
size_t get_buffer_size_for_serialization (const std::vector<std::string>& canonical_path,
320
321
size_t alignment,
321
322
const MetadataWriter& metadata_writer) const noexcept ;
322
-
323
+
323
324
// / Constructs an `InMemoryFileSystem` instance from the buffer contents.
324
325
// /
325
326
// / @param buffer The memory buffer.
326
327
// / @param metadata_reader The function to use when deserializing the filesystem metadata.
327
328
// / @retval The constructed `InMemoryFileSystem` or `nullptr` if the deserialization failed.
328
329
static std::unique_ptr<InMemoryFileSystem> make_from_buffer (const std::shared_ptr<MemoryBuffer>& buffer,
329
330
const MetadataReader& metadata_reader) noexcept ;
330
-
331
+
331
332
private:
332
333
const std::unique_ptr<InMemoryNode> root_;
333
334
};
0 commit comments