F´ Flight Software - C/C++ Documentation
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 
10 #include <Os/Directory.hpp>
11 #include <Os/File.hpp>
12 #include <Os/Os.hpp>
13 
14 namespace Os {
15 
16 struct FileSystemHandle {};
17 
19  public:
20  // Size of file chunks to use for file system operations (e.g. copyFile)
22 
23  enum Status {
34  BUSY,
37  EXDEV_ERROR, // Operation not supported across devices (e.g. rename)
38  OVERFLOW_ERROR, // Operation failed due to overflow in calculation of the result
41  };
42 
43  enum PathType {
44  FILE,
48  };
49 
51  FileSystemInterface() = default;
52 
54  virtual ~FileSystemInterface() = default;
55 
57  FileSystemInterface(const FileSystemInterface& other) = delete;
58 
60  FileSystemInterface& operator=(const FileSystemInterface& other) = delete;
61 
64  virtual FileSystemHandle* getHandle() = 0;
65 
67  static FileSystemInterface* getDelegate(FileSystemHandleStorage& aligned_new_memory);
68 
69  // ------------------------------------------------------------------
70  // FileSystem operations to be implemented by an OSAL implementation
71  // ------------------------------------------------------------------
72  // These functions are to be overridden in each OS implementation
73  // See an example in in Os/Posix/FileSystem.hpp
74 
78  virtual Status _removeDirectory(const char* path) = 0;
79 
83  virtual Status _removeFile(const char* path) = 0;
84 
89  virtual Status _rename(const char* sourcePath, const char* destPath) = 0;
90 
96  virtual Status _getFreeSpace(const char* path, FwSizeType& totalBytes, FwSizeType& freeBytes) = 0;
97 
105  virtual Status _getPathType(const char* path, PathType& pathType) = 0;
106 
111  virtual Status _getWorkingDirectory(char* path, FwSizeType bufferSize) = 0;
112 
116  virtual Status _changeWorkingDirectory(const char* path) = 0;
117 };
118 
124 class FileSystem final : public FileSystemInterface {
125  private:
126  FileSystem();
127  public:
128  ~FileSystem() final;
129 
132  FileSystemHandle* getHandle() override;
133 
134  // ------------------------------------------------------------
135  // Implementation-specific FileSystem member functions
136  // ------------------------------------------------------------
137 
144  Status _removeDirectory(const char* path) override;
145 
152  Status _removeFile(const char* path) override;
153 
164  Status _rename(const char* sourcePath, const char* destPath) override;
165 
174  Status _getFreeSpace(const char* path, FwSizeType& totalBytes, FwSizeType& freeBytes) override;
175 
187  Status _getWorkingDirectory(char* path, FwSizeType bufferSize) override;
188 
195  Status _changeWorkingDirectory(const char* path) override;
196 
204  Status _getPathType(const char* path, PathType& pathType) override;
205 
206  // ------------------------------------------------------------
207  // Implementation-specific FileSystem static functions
208  // ------------------------------------------------------------
209  // These are static variants that are exposed to the user, and call the above member functions
210 
217  static Status removeDirectory(const char* path);
218 
225  static Status removeFile(const char* path);
226 
237  static Status rename(const char* sourcePath, const char* destPath);
238 
247  static Status getFreeSpace(const char* path, FwSizeType& totalBytes, FwSizeType& freeBytes);
248 
260  static Status getWorkingDirectory(char* path, FwSizeType bufferSize);
261 
268  static Status changeWorkingDirectory(const char* path);
269 
270  // ------------------------------------------------------------
271  // Additional functions built on top of OS-specific operations
272  // ------------------------------------------------------------
273 
280  static bool exists(const char* path);
281 
288  static PathType getPathType(const char* path);
289 
296  static Status touch(const char* path);
297 
308  static Status createDirectory(const char* path, bool errorIfAlreadyExists = false);
309 
322  static Status appendFile(const char* sourcePath, const char* destPath, bool createMissingDest = false);
323 
334  static Status copyFile(const char* sourcePath, const char* destPath);
335 
346  static Status moveFile(const char* sourcePath, const char* destPath);
347 
355  static Status getFileSize(const char* path, FwSizeType& size);
356 
357  public:
359  static void init();
360 
363  static FileSystem& getSingleton();
364 
365  private:
366  // ------------------------------------------------------------
367  // Internal helper functions
368  // ------------------------------------------------------------
369 
371  static Status handleFileError(File::Status fileStatus);
372 
374  static Status handleDirectoryError(Directory::Status dirStatus);
375 
386  static Status copyFileData(File& source, File& destination, FwSizeType size);
387 
388  private:
389  // This section is used to store the implementation-defined FileSystem handle. To Os::FileSystem and fprime, this
390  // type is opaque and thus normal allocation cannot be done. Instead, we allow the implementor to store then handle
391  // in the byte-array here and set `handle` to that address for storage.
392 
393  alignas(FW_HANDLE_ALIGNMENT) FileSystemHandleStorage m_handle_storage;
394  FileSystemInterface& m_delegate;
395 };
396 
397 } // namespace Os
398 
399 #endif
FileSystemHandle * getHandle() override
return the underlying FileSystem handle (implementation specific)
Definition: FileSystem.cpp:19
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 Status rename(const char *sourcePath, const char *destPath)
Rename a file from source to destination.
Definition: FileSystem.cpp:90
PlatformSizeType FwSizeType
static FileSystemInterface * getDelegate(FileSystemHandleStorage &aligned_new_memory)
provide a pointer to a FileSystem delegate object
Definition: DefaultFile.cpp:17
static Status moveFile(const char *sourcePath, const char *destPath)
Move a file from sourcePath to destPath.
Definition: FileSystem.cpp:207
FileSystemInterface()=default
default constructor
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:102
Path is a directory.
Definition: FileSystem.hpp:29
directory is not empty
Definition: FileSystem.hpp:30
Status _getWorkingDirectory(char *path, FwSizeType bufferSize) override
Get the current working directory.
Definition: FileSystem.cpp:49
static Status removeDirectory(const char *path)
Remove a directory at the specified path.
Definition: FileSystem.cpp:82
static Status appendFile(const char *sourcePath, const char *destPath, bool createMissingDest=false)
Append the source file to the destination file.
Definition: FileSystem.cpp:176
Path is not a directory.
Definition: FileSystem.hpp:28
static bool exists(const char *path)
Return true if the path exists, false otherwise.
Definition: FileSystem.cpp:147
virtual ~FileSystemInterface()=default
default virtual destructor
Too many files or links.
Definition: FileSystem.hpp:33
#define FW_HANDLE_ALIGNMENT
Alignment of handle storage.
Definition: FpConfig.h:322
static void init()
initialize singleton
Definition: FileSystem.cpp:68
Path is not a file or directory, e.g. a socket.
Definition: FileSystem.hpp:46
#define FW_FILE_CHUNK_SIZE
Chunk size for working with files in the OSAL layer.
Definition: FpConfig.h:327
Status _rename(const char *sourcePath, const char *destPath) override
Rename a file from source to destination.
Definition: FileSystem.cpp:36
virtual Status _rename(const char *sourcePath, const char *destPath)=0
Rename (or move) a file from source to destination.
static FileSystem & getSingleton()
get a reference to singleton
Definition: FileSystem.cpp:73
Directory stream has no more files.
Definition: FileSystem.hpp:35
Status _removeFile(const char *path) override
Remove a file at the specified path.
Definition: FileSystem.cpp:30
U8 FileSystemHandleStorage[FW_FILESYSTEM_HANDLE_MAX_SIZE]
Definition: Os.hpp:16
Status _getPathType(const char *path, PathType &pathType) override
Get the type of the path (file, directory, etc.)
Definition: FileSystem.cpp:43
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:62
Status _removeDirectory(const char *path) override
Remove a directory at the specified path.
Definition: FileSystem.cpp:24
static Status getWorkingDirectory(char *path, FwSizeType bufferSize)
Get the current working directory.
Definition: FileSystem.cpp:94
virtual Status _getPathType(const char *path, PathType &pathType)=0
Get the type of the path (file, directory, etc.)
static Status getFileSize(const char *path, FwSizeType &size)
Get the size of the file (in bytes) at the specified path.
Definition: FileSystem.cpp:225
virtual Status _getWorkingDirectory(char *path, FwSizeType bufferSize)=0
Get the current working directory.
static Status createDirectory(const char *path, bool errorIfAlreadyExists=false)
Create a new directory at the specified path.
Definition: FileSystem.cpp:110
static PathType getPathType(const char *path)
Return the type of the path (file, directory, or doesn&#39;t exist)
Definition: FileSystem.cpp:137
other OS-specific error
Definition: FileSystem.hpp:40
Operand is in use by the system or by a process.
Definition: FileSystem.hpp:34
static constexpr FwSizeType FILE_SYSTEM_FILE_CHUNK_SIZE
Size of file system chunk.
Definition: FileSystem.hpp:21
Path is too long, too many sym links, etc.
Definition: FileSystem.hpp:31
Operation is not supported by the current implementation.
Definition: FileSystem.hpp:39
virtual FileSystemHandle * getHandle()=0
return the underlying FileSystem handle (implementation specific)
FileSystem class.
Definition: FileSystem.hpp:124
FileSystemInterface & operator=(const FileSystemInterface &other)=delete
assignment operator is forbidden
static Status copyFile(const char *sourcePath, const char *destPath)
Copy a file from the source path to the destination path.
Definition: FileSystem.cpp:151
virtual Status _removeFile(const char *path)=0
Remove a file at the specified path.
Operation was successful.
Definition: FileSystem.hpp:24
~FileSystem() final
Destructor.
Definition: FileSystem.cpp:14
virtual Status _removeDirectory(const char *path)=0
Remove a directory at the specified path.
static Status changeWorkingDirectory(const char *path)
Change the current working directory to the specified path.
Definition: FileSystem.cpp:98
Status _changeWorkingDirectory(const char *path) override
Change the current working directory to the specified path.
Definition: FileSystem.cpp:56
Buffer size is too small to hold full path (for getWorkingDirectory)
Definition: FileSystem.hpp:36
static Status removeFile(const char *path)
Remove a file at the specified path.
Definition: FileSystem.cpp:86
virtual Status _changeWorkingDirectory(const char *path)=0
Change the current working directory to the specified path.
Path doesn&#39;t exist.
Definition: FileSystem.hpp:32
static Status touch(const char *path)
Touch a file at the specified path, creating it if it doesn&#39;t exist.
Definition: FileSystem.cpp:125