F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
Loading...
Searching...
No Matches
FileSystem.hpp
Go to the documentation of this file.
1// ======================================================================
2// \title Os/FileSystem.hpp
3// \brief Os::FileSystem interface definition
4// ======================================================================
5
6#ifndef _OS_FILESYSTEM_HPP_
7#define _OS_FILESYSTEM_HPP_
8
9#include <FpConfig.hpp>
10#include <Os/Os.hpp>
11#include <Os/Directory.hpp>
12#include <Os/File.hpp>
13
14namespace Os {
15
17
19 public:
20
21 // Size of file chunks to use for file system operations (e.g. copyFile)
23
24 enum Status {
38 EXDEV_ERROR, // Operation not supported across devices (e.g. rename)
39 OVERFLOW_ERROR, // Operation failed due to overflow in calculation of the result
42 };
43
49
52
54 virtual ~FileSystemInterface() = default;
55
58
61
65
67 static FileSystemInterface* getDelegate(FileSystemHandleStorage& aligned_new_memory);
68
69
70 // ------------------------------------------------------------------
71 // FileSystem operations to be implemented by an OSAL implementation
72 // ------------------------------------------------------------------
73 // These functions are to be overridden in each OS implementation
74 // See an example in in Os/Posix/FileSystem.hpp
75
79 virtual Status _removeDirectory(const char* path) = 0;
80
84 virtual Status _removeFile(const char* path) = 0;
85
90 virtual Status _rename(const char* sourcePath, const char* destPath) = 0;
91
97 virtual Status _getFreeSpace(const char* path, FwSizeType& totalBytes, FwSizeType& freeBytes) = 0;
98
103 virtual Status _getWorkingDirectory(char* path, FwSizeType bufferSize) = 0;
104
108 virtual Status _changeWorkingDirectory(const char* path) = 0;
109
110};
111
117class FileSystem final : public FileSystemInterface {
118 private:
119 FileSystem();
120 public:
121 ~FileSystem() final;
122
125 FileSystemHandle* getHandle() override;
126
127
128 // ------------------------------------------------------------
129 // Implementation-specific FileSystem member functions
130 // ------------------------------------------------------------
131
138 Status _removeDirectory(const char* path) override;
139
146 Status _removeFile(const char* path) override;
147
158 Status _rename(const char* sourcePath, const char* destPath) override;
159
168 Status _getFreeSpace(const char* path, FwSizeType& totalBytes, FwSizeType& freeBytes) override;
169
181 Status _getWorkingDirectory(char* path, FwSizeType bufferSize) override;
182
189 Status _changeWorkingDirectory(const char* path) override;
190
191
192 // ------------------------------------------------------------
193 // Implementation-specific FileSystem static functions
194 // ------------------------------------------------------------
195 // These are static variants that are exposed to the user, and call the above member functions
196
203 static Status removeDirectory(const char* path);
204
211 static Status removeFile(const char* path);
212
223 static Status rename(const char* sourcePath, const char* destPath);
224
233 static Status getFreeSpace(const char* path, FwSizeType& totalBytes, FwSizeType& freeBytes);
234
246 static Status getWorkingDirectory(char* path, FwSizeType bufferSize);
247
254 static Status changeWorkingDirectory(const char* path);
255
256
257 // ------------------------------------------------------------
258 // Additional functions built on top of OS-specific operations
259 // ------------------------------------------------------------
260
267 static bool exists(const char* path);
268
275 static PathType getPathType(const char* path);
276
283 static Status touch(const char* path);
284
295 static Status createDirectory(const char* path, bool errorIfAlreadyExists=false);
296
309 static Status appendFile(const char* sourcePath, const char* destPath, bool createMissingDest=false);
310
321 static Status copyFile(const char* sourcePath, const char* destPath);
322
333 static Status moveFile(const char* sourcePath, const char* destPath);
334
342 static Status getFileSize(const char* path, FwSignedSizeType& size);
343
344
345 public:
347 static void init();
348
351 static FileSystem& getSingleton();
352
353 private:
354 // ------------------------------------------------------------
355 // Internal helper functions
356 // ------------------------------------------------------------
357
359 static Status handleFileError(File::Status fileStatus);
360
362 static Status handleDirectoryError(Directory::Status dirStatus);
363
374 static Status copyFileData(File& source, File& destination, FwSignedSizeType size);
375
376 private:
377 // This section is used to store the implementation-defined FileSystem handle. To Os::FileSystem and fprime, this type is
378 // opaque and thus normal allocation cannot be done. Instead, we allow the implementor to store then handle in
379 // the byte-array here and set `handle` to that address for storage.
380
381 alignas(FW_HANDLE_ALIGNMENT) FileSystemHandleStorage m_handle_storage;
382 FileSystemInterface& m_delegate;
383};
384
385
386
387}
388
389#endif
#define FW_HANDLE_ALIGNMENT
Alignment of handle storage.
Definition FpConfig.h:440
PlatformSignedSizeType FwSignedSizeType
Definition FpConfig.h:30
#define FW_FILE_CHUNK_SIZE
Chunk size for working with files in the OSAL layer.
Definition FpConfig.h:445
PlatformSizeType FwSizeType
Definition FpConfig.h:35
C++-compatible configuration header for fprime configuration.
U8 FileSystemHandleStorage[FW_FILESYSTEM_HANDLE_MAX_SIZE]
Definition Os.hpp:16
FileSystem class.
static Status moveFile(const char *sourcePath, const char *destPath)
Move a file from sourcePath to destPath.
static void init()
initialize singleton
static bool exists(const char *path)
Return true if the path exists, false otherwise.
static FileSystem & getSingleton()
get a reference to singleton
~FileSystem() final
Destructor.
static Status getFreeSpace(const char *path, FwSizeType &totalBytes, FwSizeType &freeBytes)
Get filesystem free and total space in bytes on the filesystem containing the specified path.
Status _getFreeSpace(const char *path, FwSizeType &totalBytes, FwSizeType &freeBytes) override
Get filesystem free and total space in bytes on the filesystem containing the specified path.
Status _changeWorkingDirectory(const char *path) override
Change the current working directory to the specified path.
static Status appendFile(const char *sourcePath, const char *destPath, bool createMissingDest=false)
Append the source file to the destination file.
Status _rename(const char *sourcePath, const char *destPath) override
Rename a file from source to destination.
static Status removeFile(const char *path)
Remove a file at the specified path.
static Status removeDirectory(const char *path)
Remove a directory at the specified path.
static Status copyFile(const char *sourcePath, const char *destPath)
Copy a file from the source path to the destination path.
static PathType getPathType(const char *path)
Return the type of the path (file, directory, or doesn't exist)
FileSystemHandle * getHandle() override
return the underlying FileSystem handle (implementation specific)
static Status getWorkingDirectory(char *path, FwSizeType bufferSize)
Get the current working directory.
static Status rename(const char *sourcePath, const char *destPath)
Rename a file from source to destination.
static Status getFileSize(const char *path, FwSignedSizeType &size)
Get the size of the file (in bytes) at the specified path.
static Status changeWorkingDirectory(const char *path)
Change the current working directory to the specified path.
static Status createDirectory(const char *path, bool errorIfAlreadyExists=false)
Create a new directory at the specified path.
Status _removeDirectory(const char *path) override
Remove a directory at the specified path.
Status _removeFile(const char *path) override
Remove a file at the specified path.
static Status touch(const char *path)
Touch a file at the specified path, creating it if it doesn't exist.
Status _getWorkingDirectory(char *path, FwSizeType bufferSize) override
Get the current working directory.
virtual FileSystemHandle * getHandle()=0
return the underlying FileSystem handle (implementation specific)
virtual Status _removeDirectory(const char *path)=0
Remove a directory at the specified path.
@ NO_MORE_FILES
Directory stream has no more files.
@ ALREADY_EXISTS
File already exists.
@ NO_PERMISSION
No permission to write.
@ OP_OK
Operation was successful.
@ NOT_DIR
Path is not a directory.
@ OTHER_ERROR
other OS-specific error
@ IS_DIR
Path is a directory.
@ NOT_SUPPORTED
Operation is not supported by the current implementation.
@ NOT_EMPTY
directory is not empty
@ BUFFER_TOO_SMALL
Buffer size is too small to hold full path (for getWorkingDirectory)
@ DOESNT_EXIST
Path doesn't exist.
@ BUSY
Operand is in use by the system or by a process.
@ FILE_LIMIT
Too many files or links.
@ NO_SPACE
No space left.
@ INVALID_PATH
Path is too long, too many sym links, etc.
virtual Status _getFreeSpace(const char *path, FwSizeType &totalBytes, FwSizeType &freeBytes)=0
Get filesystem free and total space in bytes on the filesystem containing the specified path.
static constexpr FwSignedSizeType FILE_SYSTEM_FILE_CHUNK_SIZE
Size of file system chunk.
static FileSystemInterface * getDelegate(FileSystemHandleStorage &aligned_new_memory)
provide a pointer to a FileSystem delegate object
@ DIRECTORY
Path is a directory.
@ FILE
Path is a file.
@ NOT_EXIST
Path does not exist.
virtual Status _removeFile(const char *path)=0
Remove a file at the specified path.
FileSystemInterface(const FileSystemInterface &other)=delete
copy constructor is forbidden
FileSystemInterface & operator=(const FileSystemInterface &other)=delete
assignment operator is forbidden
virtual ~FileSystemInterface()=default
default virtual destructor
virtual Status _changeWorkingDirectory(const char *path)=0
Change the current working directory to the specified path.
FileSystemInterface()=default
default constructor
virtual Status _rename(const char *sourcePath, const char *destPath)=0
Rename (or move) a file from source to destination.
virtual Status _getWorkingDirectory(char *path, FwSizeType bufferSize)=0
Get the current working directory.