F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
File.hpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title Os/File.hpp
3 // \brief common function definitions for Os::File
4 // ======================================================================
5 #ifndef Os_File_hpp_
6 #define Os_File_hpp_
7 
8 #include <FpConfig.hpp>
9 #include <Os/Os.hpp>
10 
11 namespace Os {
14  struct FileHandle {};
15 
16  // This class encapsulates a very simple file interface that has the most often-used features
17  class FileInterface {
18  public:
19  enum Mode {
27  };
28 
29  enum Status {
42  };
43 
48  };
49 
50  enum SeekType {
54  };
55 
56  enum WaitType {
58  WAIT,
60  };
61 
62  virtual ~FileInterface() = default;
63 
80  virtual Status open(const char* path, Mode mode, OverwriteType overwrite) = 0;
81 
87  virtual void close() = 0;
88 
95  virtual Status size(FwSignedSizeType& size_result) = 0;
96 
103  virtual Status position(FwSignedSizeType& position_result) = 0;
104 
117  virtual Status preallocate(FwSignedSizeType offset, FwSignedSizeType length) = 0;
118 
128  virtual Status seek(FwSignedSizeType offset, SeekType seekType) = 0;
129 
137  virtual Status flush() = 0;
138 
157  virtual Status read(U8* buffer, FwSignedSizeType &size, WaitType wait) = 0;
158 
177  virtual Status write(const U8* buffer, FwSignedSizeType &size, WaitType wait) = 0;
178 
186  virtual FileHandle* getHandle() = 0;
187 
210  static FileInterface* getDelegate(FileHandleStorage& aligned_placement_new_memory, const FileInterface* to_copy=nullptr);
211  };
212 
213 
214  class File final : public FileInterface {
215  public:
218  File();
222  ~File() final;
223 
225  File(const File& other);
226 
228  File& operator=(const File& other);
229 
233  bool isOpen() const;
234 
235  // ------------------------------------
236  // Functions supplying default values
237  // ------------------------------------
238 
252  Os::FileInterface::Status open(const char* path, Mode mode);
253 
270  Status read(U8* buffer, FwSignedSizeType &size);
271 
287  Status write(const U8* buffer, FwSignedSizeType &size);
288 
289 
290  // ------------------------------------
291  // Functions overrides
292  // ------------------------------------
293 
310  Os::FileInterface::Status open(const char* path, Mode mode, OverwriteType overwrite) override;
311 
317  void close() override;
318 
325  Status size(FwSignedSizeType& size_result) override;
326 
333  Status position(FwSignedSizeType& position_result) override;
334 
347  Status preallocate(FwSignedSizeType offset, FwSignedSizeType length) override;
348 
358  Status seek(FwSignedSizeType offset, SeekType seekType) override;
359 
367  Status flush() override;
368 
385 
387  Status read(U8* buffer, FwSignedSizeType &size, WaitType wait) override;
388 
405  Status readline(U8* buffer, FwSignedSizeType &size, WaitType wait);
406 
425  Status write(const U8* buffer, FwSignedSizeType &size, WaitType wait) override;
426 
434  FileHandle* getHandle() override;
435 
459  Status calculateCrc(U32& crc);
460 
477 
489  Status finalizeCrc(U32& crc);
490 
491  private:
492 
493 
494  PRIVATE:
495  static const U32 INITIAL_CRC = 0xFFFFFFFF;
496  Mode m_mode = Mode::OPEN_NO_MODE;
497  const CHAR* m_path = nullptr;
498 
499  U32 m_crc = File::INITIAL_CRC;
500  U8 m_crc_buffer[FW_FILE_CHUNK_SIZE];
501 
502  // This section is used to store the implementation-defined file handle. To Os::File and fprime, this type is
503  // opaque and thus normal allocation cannot be done. Instead, we allow the implementor to store then handle in
504  // the byte-array here and set `handle` to that address for storage.
505  //
506  alignas(FW_HANDLE_ALIGNMENT) FileHandleStorage m_handle_storage;
507  FileInterface& m_delegate;
508  };
509 }
510 #endif
virtual Status read(U8 *buffer, FwSignedSizeType &size, WaitType wait)=0
read data from this file into supplied buffer bounded by size
Status readline(U8 *buffer, FwSignedSizeType &size, WaitType wait)
read a line from the file using \n as the delimiter
Definition: File.cpp:237
virtual Status open(const char *path, Mode mode, OverwriteType overwrite)=0
open file with supplied path and mode
base implementation of FileHandle
Definition: File.hpp:14
Status size(FwSignedSizeType &size_result) override
get size of currently open file
Definition: File.cpp:85
Status calculateCrc(U32 &crc)
calculate the CRC32 of the entire file
Definition: File.cpp:189
virtual Status write(const U8 *buffer, FwSignedSizeType &size, WaitType wait)=0
read data from this file into supplied buffer bounded by size
File mode not yet selected.
Definition: File.hpp:20
Status preallocate(FwSignedSizeType offset, FwSignedSizeType length) override
pre-allocate file storage
Definition: File.cpp:104
Status read(U8 *buffer, FwSignedSizeType &size)
read data from this file into supplied buffer bounded by size
Definition: File.cpp:143
Status position(FwSignedSizeType &position_result) override
get file pointer position of the currently open file
Definition: File.cpp:94
Open file for writing; writes don&#39;t return until data is on disk.
Definition: File.hpp:24
virtual void close()=0
close the file, if not opened then do nothing
No permission to read/write file.
Definition: File.hpp:33
Overwrite file when it exists and creation was requested.
Definition: File.hpp:46
Open file for writing.
Definition: File.hpp:23
Maximum value of status.
Definition: File.hpp:41
Os::FileInterface::Status open(const char *path, Mode mode)
open file with supplied path and mode
char CHAR
Definition: BasicTypes.h:32
virtual FileHandle * getHandle()=0
returns the raw file handle
virtual ~FileInterface()=default
virtual Status flush()=0
flush file contents to storage
virtual Status seek(FwSignedSizeType offset, SeekType seekType)=0
seek the file pointer to the given offset
Open file for appending.
Definition: File.hpp:25
No space left.
Definition: File.hpp:32
Do not wait for read/write operation to finish.
Definition: File.hpp:57
Do wait for read/write operation to finish.
Definition: File.hpp:58
Invalid argument passed in.
Definition: File.hpp:39
Maximum value of mode.
Definition: File.hpp:26
Absolute seek from beginning of file.
Definition: File.hpp:52
Status seek(FwSignedSizeType offset, SeekType seekType) override
seek the file pointer to the given offset
Definition: File.cpp:118
virtual Status size(FwSignedSizeType &size_result)=0
get size of currently open file
File doesn&#39;t exist (for read)
Definition: File.hpp:31
virtual Status preallocate(FwSignedSizeType offset, FwSignedSizeType length)=0
pre-allocate file storage
FileHandle * getHandle() override
returns the raw file handle
Definition: File.cpp:184
Mode for file access is invalid for current operation.
Definition: File.hpp:38
Kernel or file system does not support operation.
Definition: File.hpp:37
void close() override
close the file, if not opened then do nothing
Definition: File.cpp:70
Relative seek from current file offset.
Definition: File.hpp:51
Status incrementalCrc(FwSignedSizeType &size)
calculate the CRC32 of the next section of data
Definition: File.cpp:207
C++-compatible configuration header for fprime configuration.
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:30
Status flush() override
flush file contents to storage
Definition: File.cpp:131
U8 FileHandleStorage[FW_FILE_HANDLE_MAX_SIZE]
Definition: Os.hpp:13
Do NOT overwrite existing files.
Definition: File.hpp:45
#define FW_FILE_CHUNK_SIZE
Chunk size for working with files in the OSAL layer.
Definition: FpConfig.h:445
file hasn&#39;t been opened yet
Definition: File.hpp:35
Operation was successful.
Definition: File.hpp:30
A catch-all for other errors. Have to look in implementation-specific code.
Definition: File.hpp:40
Status finalizeCrc(U32 &crc)
finalize and retrieve the CRC value
Definition: File.cpp:230
Open file for reading.
Definition: File.hpp:21
~File() final
destructor
Definition: File.cpp:17
File()
constructor
Definition: File.cpp:13
static FileInterface * getDelegate(FileHandleStorage &aligned_placement_new_memory, const FileInterface *to_copy=nullptr)
provide a pointer to a file delegate object
Definition: DefaultFile.cpp:14
PlatformSignedSizeType FwSignedSizeType
Definition: FpConfig.h:30
Invalid size parameter.
Definition: File.hpp:34
file already exist (for CREATE with O_EXCL enabled)
Definition: File.hpp:36
File & operator=(const File &other)
assignment operator that copies the internal representation
Definition: File.cpp:35
#define FW_HANDLE_ALIGNMENT
Alignment of handle storage.
Definition: FpConfig.h:440
virtual Status position(FwSignedSizeType &position_result)=0
get file pointer position of the currently open file
Status write(const U8 *buffer, FwSignedSizeType &size)
write data to this file from the supplied buffer bounded by size
Definition: File.cpp:163
bool isOpen() const
determine if the file is open
Definition: File.cpp:79
Open file for writing and truncates file if it exists, ie same flags as creat()
Definition: File.hpp:22