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 
9 #include <Os/Os.hpp>
10 
11 // Forward declaration for UTs
12 namespace Os {
13 namespace Test {
14 namespace FileTest {
15 struct Tester;
16 }
17 } // namespace Test
18 } // namespace Os
19 
20 namespace Os {
21 
24 struct FileHandle {};
25 
26 // This class encapsulates a very simple file interface that has the most often-used features
28  public:
29  enum Mode {
37  };
38 
39  enum Status {
53  };
54 
59  };
60 
61  enum SeekType {
65  };
66 
67  enum WaitType {
69  WAIT,
71  };
72 
73  virtual ~FileInterface() = default;
74 
91  virtual Status open(const char* path, Mode mode, OverwriteType overwrite) = 0;
92 
98  virtual void close() = 0;
99 
106  virtual Status size(FwSizeType& size_result) = 0;
107 
114  virtual Status position(FwSizeType& position_result) = 0;
115 
128  virtual Status preallocate(FwSizeType offset, FwSizeType length) = 0;
129 
139  virtual Status seek(FwSignedSizeType offset, SeekType seekType) = 0;
140 
148  virtual Status flush() = 0;
149 
168  virtual Status read(U8* buffer, FwSizeType& size, WaitType wait) = 0;
169 
188  virtual Status write(const U8* buffer, FwSizeType& size, WaitType wait) = 0;
189 
197  virtual FileHandle* getHandle() = 0;
198 
221  static FileInterface* getDelegate(FileHandleStorage& aligned_placement_new_memory,
222  const FileInterface* to_copy = nullptr);
223 };
224 
225 class File final : public FileInterface {
227 
228  public:
231  File();
235  ~File() final;
236 
238  File(const File& other);
239 
241  File& operator=(const File& other);
242 
246  bool isOpen() const;
247 
248  // ------------------------------------
249  // Functions supplying default values
250  // ------------------------------------
251 
265  Os::FileInterface::Status open(const char* path, Mode mode);
266 
283  Status read(U8* buffer, FwSizeType& size);
284 
300  Status write(const U8* buffer, FwSizeType& size);
301 
302  // ------------------------------------
303  // Functions overrides
304  // ------------------------------------
305 
322  Os::FileInterface::Status open(const char* path, Mode mode, OverwriteType overwrite) override;
323 
329  void close() override;
330 
337  Status size(FwSizeType& size_result) override;
338 
345  Status position(FwSizeType& position_result) override;
346 
359  Status preallocate(FwSizeType offset, FwSizeType length) override;
360 
370  Status seek(FwSignedSizeType offset, SeekType seekType) override;
371 
383  Status seek_absolute(FwSizeType offset_unsigned);
384 
392  Status flush() override;
393 
410 
412  Status read(U8* buffer, FwSizeType& size, WaitType wait) override;
413 
430  Status readline(U8* buffer, FwSizeType& size, WaitType wait);
431 
450  Status write(const U8* buffer, FwSizeType& size, WaitType wait) override;
451 
459  FileHandle* getHandle() override;
460 
486  Status calculateCrc(U32& crc);
487 
504 
516  Status finalizeCrc(U32& crc);
517 
518  private:
519  static const U32 INITIAL_CRC = 0xFFFFFFFF;
520 
521  Mode m_mode = Mode::OPEN_NO_MODE;
522  const CHAR* m_path = nullptr;
523 
524  U32 m_crc = File::INITIAL_CRC;
525  U8 m_crc_buffer[FW_FILE_CHUNK_SIZE];
526 
527  // This section is used to store the implementation-defined file handle. To Os::File and fprime, this type is
528  // opaque and thus normal allocation cannot be done. Instead, we allow the implementor to store then handle in
529  // the byte-array here and set `handle` to that address for storage.
530  //
531  alignas(FW_HANDLE_ALIGNMENT) FileHandleStorage m_handle_storage;
532  FileInterface& m_delegate;
533 };
534 } // namespace Os
535 #endif
virtual Status open(const char *path, Mode mode, OverwriteType overwrite)=0
open file with supplied path and mode
Status incrementalCrc(FwSizeType &size)
calculate the CRC32 of the next section of data
Definition: File.cpp:229
base implementation of FileHandle
Definition: File.hpp:24
friend struct Os::Test::FileTest::Tester
Definition: File.hpp:226
Status calculateCrc(U32 &crc)
calculate the CRC32 of the entire file
Definition: File.cpp:211
PlatformSizeType FwSizeType
File mode not yet selected.
Definition: File.hpp:30
No more available resources.
Definition: File.hpp:50
Status preallocate(FwSizeType offset, FwSizeType length) override
pre-allocate file storage
Definition: File.cpp:104
Status position(FwSizeType &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:34
Status seek_absolute(FwSizeType offset_unsigned)
seek the file pointer to the given offset absolutely with the full range
Definition: File.cpp:129
virtual void close()=0
close the file, if not opened then do nothing
Status size(FwSizeType &size_result) override
get size of currently open file
Definition: File.cpp:85
No permission to read/write file.
Definition: File.hpp:43
Overwrite file when it exists and creation was requested.
Definition: File.hpp:57
Open file for writing.
Definition: File.hpp:33
PlatformSignedSizeType FwSignedSizeType
Maximum value of status.
Definition: File.hpp:52
Os::FileInterface::Status open(const char *path, Mode mode)
open file with supplied path and mode
char CHAR
Definition: BasicTypes.h:59
virtual FileHandle * getHandle()=0
returns the raw file handle
#define FW_HANDLE_ALIGNMENT
Alignment of handle storage.
Definition: FpConfig.h:322
virtual ~FileInterface()=default
#define FW_FILE_CHUNK_SIZE
Chunk size for working with files in the OSAL layer.
Definition: FpConfig.h:327
virtual Status size(FwSizeType &size_result)=0
get size of currently open file
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:35
No space left.
Definition: File.hpp:42
Do not wait for read/write operation to finish.
Definition: File.hpp:68
Do wait for read/write operation to finish.
Definition: File.hpp:69
Invalid argument passed in.
Definition: File.hpp:49
Maximum value of mode.
Definition: File.hpp:36
Absolute seek from beginning of file.
Definition: File.hpp:63
Status seek(FwSignedSizeType offset, SeekType seekType) override
seek the file pointer to the given offset
Definition: File.cpp:116
File doesn&#39;t exist (for read)
Definition: File.hpp:41
FileHandle * getHandle() override
returns the raw file handle
Definition: File.cpp:206
Mode for file access is invalid for current operation.
Definition: File.hpp:48
Kernel or file system does not support operation.
Definition: File.hpp:47
void close() override
close the file, if not opened then do nothing
Definition: File.cpp:70
Status write(const U8 *buffer, FwSizeType &size)
write data to this file from the supplied buffer bounded by size
Definition: File.cpp:187
Relative seek from current file offset.
Definition: File.hpp:62
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:53
Status read(U8 *buffer, FwSizeType &size)
read data from this file into supplied buffer bounded by size
Definition: File.cpp:168
Status flush() override
flush file contents to storage
Definition: File.cpp:156
U8 FileHandleStorage[FW_FILE_HANDLE_MAX_SIZE]
Definition: Os.hpp:13
Do NOT overwrite existing files.
Definition: File.hpp:56
virtual Status write(const U8 *buffer, FwSizeType &size, WaitType wait)=0
read data from this file into supplied buffer bounded by size
Status readline(U8 *buffer, FwSizeType &size, WaitType wait)
read a line from the file using \n as the delimiter
Definition: File.cpp:255
file hasn&#39;t been opened yet
Definition: File.hpp:45
Operation was successful.
Definition: File.hpp:40
A catch-all for other errors. Have to look in implementation-specific code.
Definition: File.hpp:51
Status finalizeCrc(U32 &crc)
finalize and retrieve the CRC value
Definition: File.cpp:248
Open file for reading.
Definition: File.hpp:31
~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
virtual Status preallocate(FwSizeType offset, FwSizeType length)=0
pre-allocate file storage
Invalid size parameter.
Definition: File.hpp:44
file already exist (for CREATE with O_EXCL enabled)
Definition: File.hpp:46
virtual Status position(FwSizeType &position_result)=0
get file pointer position of the currently open file
bool isOpen() const
determine if the file is open
Definition: File.cpp:79
virtual Status read(U8 *buffer, FwSizeType &size, WaitType wait)=0
read data from this file into supplied buffer bounded by size
Open file for writing and truncates file if it exists, ie same flags as creat()
Definition: File.hpp:32