F´ Flight Software - C/C++ Documentation  devel
A framework for building embedded system applications to NASA flight quality standards.
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 
14 namespace Os {
15 
16 struct FileSystemHandle {};
17 
19  public:
20 
21  // Size of file chunks to use for file system operations (e.g. copyFile)
23 
24  enum Status {
35  BUSY,
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 
44  enum PathType {
45  FILE,
48  };
49 
51  FileSystemInterface() = default;
52 
54  virtual ~FileSystemInterface() = default;
55 
57  FileSystemInterface(const FileSystemInterface& other) = delete;
58 
61 
64  virtual FileSystemHandle* getHandle() = 0;
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 
117 class 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.
Definition: FileSystem.hpp:117
static Status moveFile(const char *sourcePath, const char *destPath)
Move a file from sourcePath to destPath.
Definition: FileSystem.cpp:209
static void init()
initialize singleton
Definition: FileSystem.cpp:62
static bool exists(const char *path)
Return true if the path exists, false otherwise.
Definition: FileSystem.cpp:149
static FileSystem & getSingleton()
get a reference to singleton
Definition: FileSystem.cpp:67
~FileSystem() final
Destructor.
Definition: FileSystem.cpp:14
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.
Definition: FileSystem.cpp:97
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.
Definition: FileSystem.cpp:56
Status _changeWorkingDirectory(const char *path) override
Change the current working directory to the specified path.
Definition: FileSystem.cpp:50
static Status appendFile(const char *sourcePath, const char *destPath, bool createMissingDest=false)
Append the source file to the destination file.
Definition: FileSystem.cpp:178
Status _rename(const char *sourcePath, const char *destPath) override
Rename a file from source to destination.
Definition: FileSystem.cpp:36
static Status removeFile(const char *path)
Remove a file at the specified path.
Definition: FileSystem.cpp:81
static Status removeDirectory(const char *path)
Remove a directory at the specified path.
Definition: FileSystem.cpp:77
static Status copyFile(const char *sourcePath, const char *destPath)
Copy a file from the source path to the destination path.
Definition: FileSystem.cpp:153
static PathType getPathType(const char *path)
Return the type of the path (file, directory, or doesn't exist)
Definition: FileSystem.cpp:132
FileSystemHandle * getHandle() override
return the underlying FileSystem handle (implementation specific)
Definition: FileSystem.cpp:19
static Status getWorkingDirectory(char *path, FwSizeType bufferSize)
Get the current working directory.
Definition: FileSystem.cpp:89
static Status rename(const char *sourcePath, const char *destPath)
Rename a file from source to destination.
Definition: FileSystem.cpp:85
static Status getFileSize(const char *path, FwSignedSizeType &size)
Get the size of the file (in bytes) at the specified path.
Definition: FileSystem.cpp:227
static Status changeWorkingDirectory(const char *path)
Change the current working directory to the specified path.
Definition: FileSystem.cpp:93
static Status createDirectory(const char *path, bool errorIfAlreadyExists=false)
Create a new directory at the specified path.
Definition: FileSystem.cpp:106
Status _removeDirectory(const char *path) override
Remove a directory at the specified path.
Definition: FileSystem.cpp:24
Status _removeFile(const char *path) override
Remove a file at the specified path.
Definition: FileSystem.cpp:30
static Status touch(const char *path)
Touch a file at the specified path, creating it if it doesn't exist.
Definition: FileSystem.cpp:120
Status _getWorkingDirectory(char *path, FwSizeType bufferSize) override
Get the current working directory.
Definition: FileSystem.cpp:43
virtual Status _removeDirectory(const char *path)=0
Remove a directory at the specified path.
@ NO_MORE_FILES
Directory stream has no more files.
Definition: FileSystem.hpp:36
@ ALREADY_EXISTS
File already exists.
Definition: FileSystem.hpp:26
@ NO_PERMISSION
No permission to write.
Definition: FileSystem.hpp:28
@ OP_OK
Operation was successful.
Definition: FileSystem.hpp:25
@ NOT_DIR
Path is not a directory.
Definition: FileSystem.hpp:29
@ OTHER_ERROR
other OS-specific error
Definition: FileSystem.hpp:41
@ IS_DIR
Path is a directory.
Definition: FileSystem.hpp:30
@ NOT_SUPPORTED
Operation is not supported by the current implementation.
Definition: FileSystem.hpp:40
@ NOT_EMPTY
directory is not empty
Definition: FileSystem.hpp:31
@ BUFFER_TOO_SMALL
Buffer size is too small to hold full path (for getWorkingDirectory)
Definition: FileSystem.hpp:37
@ DOESNT_EXIST
Path doesn't exist.
Definition: FileSystem.hpp:33
@ BUSY
Operand is in use by the system or by a process.
Definition: FileSystem.hpp:35
@ FILE_LIMIT
Too many files or links.
Definition: FileSystem.hpp:34
@ NO_SPACE
No space left.
Definition: FileSystem.hpp:27
@ INVALID_PATH
Path is too long, too many sym links, etc.
Definition: FileSystem.hpp:32
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.
Definition: FileSystem.hpp:22
static FileSystemInterface * getDelegate(FileSystemHandleStorage &aligned_new_memory)
provide a pointer to a FileSystem delegate object
Definition: DefaultFile.cpp:17
FileSystemInterface & operator=(const FileSystemInterface &other)=delete
assignment operator is forbidden
@ DIRECTORY
Path is a directory.
Definition: FileSystem.hpp:46
@ FILE
Path is a file.
Definition: FileSystem.hpp:45
@ NOT_EXIST
Path does not exist.
Definition: FileSystem.hpp:47
virtual Status _removeFile(const char *path)=0
Remove a file at the specified path.
virtual FileSystemHandle * getHandle()=0
return the underlying FileSystem handle (implementation specific)
FileSystemInterface(const FileSystemInterface &other)=delete
copy constructor 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.