-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
FileSystem.hpp
389 lines (325 loc) · 16 KB
/
FileSystem.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
// ======================================================================
// \title Os/FileSystem.hpp
// \brief Os::FileSystem interface definition
// ======================================================================
#ifndef _OS_FILESYSTEM_HPP_
#define _OS_FILESYSTEM_HPP_
#include <FpConfig.hpp>
#include <Os/Os.hpp>
#include <Os/Directory.hpp>
#include <Os/File.hpp>
namespace Os {
struct FileSystemHandle {};
class FileSystemInterface {
public:
// Size of file chunks to use for file system operations (e.g. copyFile)
static constexpr FwSignedSizeType FILE_SYSTEM_FILE_CHUNK_SIZE = FW_FILE_CHUNK_SIZE; //!< Size of file system chunk
enum Status {
OP_OK, //!< Operation was successful
ALREADY_EXISTS, //!< File already exists
NO_SPACE, //!< No space left
NO_PERMISSION, //!< No permission to write
NOT_DIR, //!< Path is not a directory
IS_DIR, //!< Path is a directory
NOT_EMPTY, //!< directory is not empty
INVALID_PATH, //!< Path is too long, too many sym links, etc.
DOESNT_EXIST, //!< Path doesn't exist
FILE_LIMIT, //!< Too many files or links
BUSY, //!< Operand is in use by the system or by a process
NO_MORE_FILES, //!< Directory stream has no more files
BUFFER_TOO_SMALL, //!< Buffer size is too small to hold full path (for getWorkingDirectory)
EXDEV_ERROR, // Operation not supported across devices (e.g. rename)
OVERFLOW_ERROR, // Operation failed due to overflow in calculation of the result
NOT_SUPPORTED, //!< Operation is not supported by the current implementation
OTHER_ERROR, //!< other OS-specific error
};
enum PathType {
FILE, //!< Path is a file
DIRECTORY, //!< Path is a directory
NOT_EXIST, //!< Path does not exist
};
//! \brief default constructor
FileSystemInterface() = default;
//! \brief default virtual destructor
virtual ~FileSystemInterface() = default;
//! \brief copy constructor is forbidden
FileSystemInterface(const FileSystemInterface& other) = delete;
//! \brief assignment operator is forbidden
FileSystemInterface& operator=(const FileSystemInterface& other) = delete;
//! \brief return the underlying FileSystem handle (implementation specific)
//! \return internal FileSystem handle representation
virtual FileSystemHandle* getHandle() = 0;
//! \brief provide a pointer to a FileSystem delegate object
static FileSystemInterface* getDelegate(FileSystemHandleStorage& aligned_new_memory);
// ------------------------------------------------------------------
// FileSystem operations to be implemented by an OSAL implementation
// ------------------------------------------------------------------
// These functions are to be overridden in each OS implementation
// See an example in in Os/Posix/FileSystem.hpp
//! \brief Remove a directory at the specified path
//! \param path The path of the directory to remove
//! \return Status of the operation
virtual Status _removeDirectory(const char* path) = 0;
//! \brief Remove a file at the specified path
//! \param path The path of the file to remove
//! \return Status of the operation
virtual Status _removeFile(const char* path) = 0;
//! \brief Rename (or move) a file from source to destination
//! \param sourcePath The path of the source file
//! \param destPath The path of the destination file
//! \return Status of the operation
virtual Status _rename(const char* sourcePath, const char* destPath) = 0;
//! \brief Get filesystem free and total space in bytes on the filesystem containing the specified path
//! \param path The path on the filesystem to query
//! \param totalBytes Reference to store the total bytes on the filesystem
//! \param freeBytes Reference to store the free bytes on the filesystem
//! \return Status of the operation
virtual Status _getFreeSpace(const char* path, FwSizeType& totalBytes, FwSizeType& freeBytes) = 0;
//! \brief Get the current working directory
//! \param path Buffer to store the current working directory path
//! \param bufferSize Size of the buffer
//! \return Status of the operation
virtual Status _getWorkingDirectory(char* path, FwSizeType bufferSize) = 0;
//! \brief Change the current working directory to the specified path
//! \param path The path of the new working directory
//! \return Status of the operation
virtual Status _changeWorkingDirectory(const char* path) = 0;
};
//! \brief FileSystem class
//!
//! This class provides a common interface for file system operations.
//! This class uses the singleton pattern and should be accessed through
//! its static functions, for example using `Os::FileSystem::removeFile(path)`.
class FileSystem final : public FileSystemInterface {
private:
FileSystem(); //!< Constructor (private because singleton pattern)
public:
~FileSystem() final; //!< Destructor
//! \brief return the underlying FileSystem handle (implementation specific)
//! \return internal FileSystem handle representation
FileSystemHandle* getHandle() override;
// ------------------------------------------------------------
// Implementation-specific FileSystem member functions
// ------------------------------------------------------------
//! \brief Remove a directory at the specified path
//!
//! It is invalid to pass `nullptr` as the path.
//!
//! \param path The path of the directory to remove
//! \return Status of the operation
Status _removeDirectory(const char* path) override;
//! \brief Remove a file at the specified path
//!
//! It is invalid to pass `nullptr` as the path.
//!
//! \param path The path of the file to remove
//! \return Status of the operation
Status _removeFile(const char* path) override;
//! \brief Rename a file from source to destination
//!
//! If the rename fails due to a cross-device operation, this function should return EXDEV_ERROR
//! and moveFile can be used instead to force a copy-and-remove.
//!
//! It is invalid to pass `nullptr` as sourcePath or destPath.
//!
//! \param sourcePath The path of the source file
//! \param destPath The path of the destination file
//! \return Status of the operation
Status _rename(const char* sourcePath, const char* destPath) override;
//! \brief Get filesystem free and total space in bytes on the filesystem containing the specified path
//!
//! It is invalid to pass `nullptr` as the path.
//!
//! \param path The path on the filesystem to query
//! \param totalBytes Reference to store the total bytes on the filesystem
//! \param freeBytes Reference to store the free bytes on the filesystem
//! \return Status of the operation
Status _getFreeSpace(const char* path, FwSizeType& totalBytes, FwSizeType& freeBytes) override;
//! \brief Get the current working directory
//!
//! Writes the current working directory path to the provided buffer of size bufferSize.
//! If the buffer is too small to hold the full path, the function will return BUFFER_TOO_SMALL.
//!
//! It is invalid to pass `nullptr` as the path.
//! It is invalid to pass a bufferSize of 0.
//!
//! \param path Buffer to store the current working directory path
//! \param bufferSize Size of the buffer
//! \return Status of the operation
Status _getWorkingDirectory(char* path, FwSizeType bufferSize) override;
//! \brief Change the current working directory to the specified path
//!
//! It is invalid to pass `nullptr` as the path.
//!
//! \param path The path of the new working directory
//! \return Status of the operation
Status _changeWorkingDirectory(const char* path) override;
// ------------------------------------------------------------
// Implementation-specific FileSystem static functions
// ------------------------------------------------------------
// These are static variants that are exposed to the user, and call the above member functions
//! \brief Remove a directory at the specified path
//!
//! It is invalid to pass `nullptr` as the path.
//!
//! \param path The path of the directory to remove
//! \return Status of the operation
static Status removeDirectory(const char* path);
//! \brief Remove a file at the specified path
//!
//! It is invalid to pass `nullptr` as the path.
//!
//! \param path The path of the file to remove
//! \return Status of the operation
static Status removeFile(const char* path);
//! \brief Rename a file from source to destination
//!
//! If the rename fails due to a cross-device operation, this function should return EXDEV_ERROR
//! and moveFile can be used instead to force a copy-and-remove.
//!
//! It is invalid to pass `nullptr` as sourcePath or destPath.
//!
//! \param sourcePath The path of the source file
//! \param destPath The path of the destination file
//! \return Status of the operation
static Status rename(const char* sourcePath, const char* destPath);
//! \brief Get filesystem free and total space in bytes on the filesystem containing the specified path
//!
//! It is invalid to pass `nullptr` as the path.
//!
//! \param path The path on the filesystem to query
//! \param totalBytes Reference to store the total bytes on the filesystem
//! \param freeBytes Reference to store the free bytes on the filesystem
//! \return Status of the operation
static Status getFreeSpace(const char* path, FwSizeType& totalBytes, FwSizeType& freeBytes);
//! \brief Get the current working directory
//!
//! Writes the current working directory path to the provided buffer of size bufferSize.
//! If the buffer is too small to hold the full path, the function will return BUFFER_TOO_SMALL.
//!
//! It is invalid to pass `nullptr` as the path.
//! It is invalid to pass a bufferSize of 0.
//!
//! \param path Buffer to store the current working directory path
//! \param bufferSize Size of the buffer
//! \return Status of the operation
static Status getWorkingDirectory(char* path, FwSizeType bufferSize);
//! \brief Change the current working directory to the specified path
//!
//! It is invalid to pass `nullptr` as the path.
//!
//! \param path The path of the new working directory
//! \return Status of the operation
static Status changeWorkingDirectory(const char* path);
// ------------------------------------------------------------
// Additional functions built on top of OS-specific operations
// ------------------------------------------------------------
//! \brief Return true if the path exists, false otherwise
//!
//! It is invalid to pass `nullptr` as the path.
//!
//! \param path The path to check for existence
//! \return True if the path exists, false otherwise
static bool exists(const char* path);
//! \brief Return the type of the path (file, directory, or doesn't exist)
//!
//! It is invalid to pass `nullptr` as the path.
//!
//! \param path The path to check for existence
//! \return PathType enum representing the type of the path (FILE, DIRECTORY, NOT_EXIST)
static PathType getPathType(const char* path);
//! \brief Touch a file at the specified path, creating it if it doesn't exist
//!
//! It is invalid to pass `nullptr` as the path.
//!
//! \param path The path of the file to touch
//! \return Status of the operation
static Status touch(const char* path);
//! \brief Create a new directory at the specified path.
//!
//! The optional errorIfAlreadyExists (default=false) parameter can be set to true
//! to return an error status if the directory already exists.
//!
//! It is invalid to pass `nullptr` as the path.
//!
//! \param path The path where the new directory will be created
//! \param errorIfAlreadyExists If true, returns an error if the directory already exists
//! \return Status of the operation
static Status createDirectory(const char* path, bool errorIfAlreadyExists=false);
//! \brief Append the source file to the destination file
//!
//! This function opens both files, and iteratively reads the source by chunks and writes
//! chunks to the destination.
//! If the destination file does not exist and createMissingDest is true, a new file is created.
//!
//! It is invalid to pass `nullptr` as either the source or destination path.
//!
//! \param sourcePath The path of the source file
//! \param destPath The path of the destination file
//! \param createMissingDest If true, creates a new file if the destination doesn't exist
//! \return Status of the operation
static Status appendFile(const char* sourcePath, const char* destPath, bool createMissingDest=false);
//! \brief Copy a file from the source path to the destination path
//!
//! This function opens both files, and iteratively reads the source by chunks and writes
//! chunks to the destination.
//!
//! It is invalid to pass `nullptr` as either the source or destination path.
//!
//! \param sourcePath The path of the source file
//! \param destPath The path of the destination file
//! \return Status of the operation
static Status copyFile(const char* sourcePath, const char* destPath);
//! \brief Move a file from sourcePath to destPath
//!
//! This is done by first trying to rename, and if renaming fails,
//! copy it and then remove the original
//!
//! It is invalid to pass `nullptr` as either the source or destination path.
//!
//! \param sourcePath The path of the source file
//! \param destPath The path of the destination file
//! \return Status of the operation
static Status moveFile(const char* sourcePath, const char* destPath);
//! \brief Get the size of the file (in bytes) at the specified path
//!
//! It is invalid to pass `nullptr` as the path.
//!
//! \param path The path of the file
//! \param size Reference to store the size of the file
//! \return Status of the operation
static Status getFileSize(const char* path, FwSignedSizeType& size);
public:
//! \brief initialize singleton
static void init();
//! \brief get a reference to singleton
//! \return reference to singleton
static FileSystem& getSingleton();
private:
// ------------------------------------------------------------
// Internal helper functions
// ------------------------------------------------------------
//! \brief Convert a File::Status to a FileSystem::Status
static Status handleFileError(File::Status fileStatus);
//! \brief Convert a Directory::Status to a FileSystem::Status
static Status handleDirectoryError(Directory::Status dirStatus);
//! \brief A helper function that writes all the file information in the source
//! file to the destination file (replaces/appends to end/etc. depending
//! on destination file mode).
//!
//! Files must already be open and will remain open after this function
//! completes.
//!
//! @param source File to copy data from
//! @param destination File to copy data to
//! @param size The number of bytes to copy
static Status copyFileData(File& source, File& destination, FwSignedSizeType size);
private:
// This section is used to store the implementation-defined FileSystem handle. To Os::FileSystem and fprime, this type is
// opaque and thus normal allocation cannot be done. Instead, we allow the implementor to store then handle in
// the byte-array here and set `handle` to that address for storage.
alignas(FW_HANDLE_ALIGNMENT) FileSystemHandleStorage m_handle_storage; //!< FileSystem handle storage
FileSystemInterface& m_delegate;
};
}
#endif