F´ Flight Software - C/C++ Documentation  devel
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 {
41  MAX_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 
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(HandleStorage& aligned_placement_new_memory, const FileInterface* to_copy=nullptr);
211  };
212 
213 
214  class File final : public FileInterface {
215  public:
218  File();
221  ~File() final;
222 
224  File(const File& other);
225 
227  File& operator=(const File& other);
228 
232  bool isOpen() const;
233 
234  // ------------------------------------
235  // Functions supplying default values
236  // ------------------------------------
237 
251  Os::FileInterface::Status open(const char* path, Mode mode);
252 
269  Status read(U8* buffer, FwSignedSizeType &size);
270 
286  Status write(const U8* buffer, FwSignedSizeType &size);
287 
288 
289  // ------------------------------------
290  // Functions overrides
291  // ------------------------------------
292 
309  Os::FileInterface::Status open(const char* path, Mode mode, OverwriteType overwrite) override;
310 
316  void close() override;
317 
324  Status size(FwSignedSizeType& size_result) override;
325 
332  Status position(FwSignedSizeType& position_result) override;
333 
346  Status preallocate(FwSignedSizeType offset, FwSignedSizeType length) override;
347 
357  Status seek(FwSignedSizeType offset, SeekType seekType) override;
358 
366  Status flush() override;
367 
386  Status read(U8* buffer, FwSignedSizeType &size, WaitType wait) override;
387 
406  Status write(const U8* buffer, FwSignedSizeType &size, WaitType wait) override;
407 
415  FileHandle* getHandle() override;
416 
440  Status calculateCrc(U32& crc);
441 
458 
470  Status finalizeCrc(U32& crc);
471 
472  private:
473 
474 
475  PRIVATE:
476  static const U32 INITIAL_CRC = 0xFFFFFFFF;
477  Mode m_mode = Mode::OPEN_NO_MODE;
478  const CHAR* m_path = nullptr;
479 
480  U32 m_crc = File::INITIAL_CRC;
481  U8 m_crc_buffer[FW_FILE_CHUNK_SIZE];
482 
483  // This section is used to store the implementation-defined file handle. To Os::File and fprime, this type is
484  // opaque and thus normal allocation cannot be done. Instead, we allow the implementor to store then handle in
485  // the byte-array here and set `handle` to that address for storage.
486  //
487  alignas(FW_HANDLE_ALIGNMENT) HandleStorage m_handle_storage;
488  FileInterface& m_delegate;
489  };
490 }
491 #endif
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:26
char CHAR
Definition: BasicTypes.h:28
#define FW_HANDLE_ALIGNMENT
Alignment of handle storage.
Definition: FpConfig.h:378
PlatformSignedSizeType FwSignedSizeType
Definition: FpConfig.h:25
#define FW_FILE_CHUNK_SIZE
Chunk size for working with files.
Definition: FpConfig.h:382
C++-compatible configuration header for fprime configuration.
U8 HandleStorage[FW_HANDLE_MAX_SIZE]
Storage type for OSAL handles.
Definition: Os.hpp:10
Status seek(FwSignedSizeType offset, SeekType seekType) override
seek the file pointer to the given offset
Definition: File.cpp:117
File()
constructor
Definition: File.cpp:13
Status incrementalCrc(FwSignedSizeType &size)
calculate the CRC32 of the next section of data
Definition: File.cpp:206
Status read(U8 *buffer, FwSignedSizeType &size)
read data from this file into supplied buffer bounded by size
Definition: File.cpp:142
Status flush() override
flush file contents to storage
Definition: File.cpp:130
Status size(FwSignedSizeType &size_result) override
get size of currently open file
Definition: File.cpp:84
void close() override
close the file, if not opened then do nothing
Definition: File.cpp:69
Status finalizeCrc(U32 &crc)
finalize and retrieve the CRC value
Definition: File.cpp:229
FileHandle * getHandle() override
returns the raw file handle
Definition: File.cpp:183
~File() final
destructor
Definition: File.cpp:17
Os::FileInterface::Status open(const char *path, Mode mode)
open file with supplied path and mode
Definition: File.cpp:45
Status preallocate(FwSignedSizeType offset, FwSignedSizeType length) override
pre-allocate file storage
Definition: File.cpp:103
Status position(FwSignedSizeType &position_result) override
get file pointer position of the currently open file
Definition: File.cpp:93
Status calculateCrc(U32 &crc)
calculate the CRC32 of the entire file
Definition: File.cpp:188
bool isOpen() const
determine if the file is open
Definition: File.cpp:78
File & operator=(const File &other)
assignment operator that copies the internal representation
Definition: File.cpp:35
Status write(const U8 *buffer, FwSignedSizeType &size)
write data to this file from the supplied buffer bounded by size
Definition: File.cpp:162
virtual void close()=0
close the file, if not opened then do nothing
virtual Status size(FwSignedSizeType &size_result)=0
get size of currently open file
virtual Status preallocate(FwSignedSizeType offset, FwSignedSizeType length)=0
pre-allocate file storage
@ WAIT
Do wait for read/write operation to finish.
Definition: File.hpp:58
@ NO_WAIT
Do not wait for read/write operation to finish.
Definition: File.hpp:57
virtual Status flush()=0
flush file contents to storage
static FileInterface * getDelegate(HandleStorage &aligned_placement_new_memory, const FileInterface *to_copy=nullptr)
provide a pointer to a file delegate object
Definition: DefaultFile.cpp:10
@ NO_OVERWRITE
Do NOT overwrite existing files.
Definition: File.hpp:45
@ OVERWRITE
Overwrite file when it exists and creation was requested.
Definition: File.hpp:46
@ MAX_OVERWRITE_TYPE
Definition: File.hpp:47
virtual Status write(const U8 *buffer, FwSignedSizeType &size, WaitType wait)=0
read data from this file into supplied buffer bounded by size
virtual Status position(FwSignedSizeType &position_result)=0
get file pointer position of the currently open file
virtual Status open(const char *path, Mode mode, OverwriteType overwrite)=0
open file with supplied path and mode
virtual Status read(U8 *buffer, FwSignedSizeType &size, WaitType wait)=0
read data from this file into supplied buffer bounded by size
@ NO_SPACE
No space left.
Definition: File.hpp:32
@ NOT_SUPPORTED
Kernel or file system does not support operation.
Definition: File.hpp:37
@ INVALID_ARGUMENT
Invalid argument passed in.
Definition: File.hpp:39
@ INVALID_MODE
Mode for file access is invalid for current operation.
Definition: File.hpp:38
@ NO_PERMISSION
No permission to read/write file.
Definition: File.hpp:33
@ MAX_STATUS
Maximum value of status.
Definition: File.hpp:41
@ NOT_OPENED
file hasn't been opened yet
Definition: File.hpp:35
@ OTHER_ERROR
A catch-all for other errors. Have to look in implementation-specific code.
Definition: File.hpp:40
@ BAD_SIZE
Invalid size parameter.
Definition: File.hpp:34
@ OP_OK
Operation was successful.
Definition: File.hpp:30
@ DOESNT_EXIST
File doesn't exist (for read)
Definition: File.hpp:31
@ FILE_EXISTS
file already exist (for CREATE with O_EXCL enabled)
Definition: File.hpp:36
@ ABSOLUTE
Absolute seek from beginning of file.
Definition: File.hpp:52
@ RELATIVE
Relative seek from current file offset.
Definition: File.hpp:51
@ MAX_OPEN_MODE
Maximum value of mode.
Definition: File.hpp:26
@ OPEN_NO_MODE
File mode not yet selected.
Definition: File.hpp:20
@ OPEN_WRITE
Open file for writing.
Definition: File.hpp:23
@ OPEN_CREATE
Open file for writing and truncates file if it exists, ie same flags as creat()
Definition: File.hpp:22
@ OPEN_READ
Open file for reading.
Definition: File.hpp:21
@ OPEN_APPEND
Open file for appending.
Definition: File.hpp:25
@ OPEN_SYNC_WRITE
Open file for writing; writes don't return until data is on disk.
Definition: File.hpp:24
virtual FileHandle * getHandle()=0
returns the raw file handle
virtual Status seek(FwSignedSizeType offset, SeekType seekType)=0
seek the file pointer to the given offset
virtual ~FileInterface()=default
base implementation of FileHandle
Definition: File.hpp:14