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 
10 #include <Os/Os.hpp>
11 #include <Utils/Hash/Hash.hpp>
12 
13 // Forward declaration for UTs
14 namespace Os {
15 namespace Test {
16 namespace FileTest {
17 struct Tester;
18 }
19 } // namespace Test
20 } // namespace Os
21 
22 namespace Os {
23 
26 struct FileHandle {};
27 
28 // This class encapsulates a very simple file interface that has the most often-used features
30  public:
31  enum Mode {
39  };
40 
41  enum Status {
56  };
57 
62  };
63 
64  enum SeekType {
68  };
69 
70  enum WaitType {
72  WAIT,
74  };
75 
76  virtual ~FileInterface() = default;
77 
94  virtual Status open(const char* path, Mode mode, OverwriteType overwrite) = 0;
95 
101  virtual void close() = 0;
102 
109  virtual Status size(FwSizeType& size_result) = 0;
110 
117  virtual Status position(FwSizeType& position_result) = 0;
118 
131  virtual Status preallocate(FwSizeType offset, FwSizeType length) = 0;
132 
142  virtual Status seek(FwSignedSizeType offset, SeekType seekType) = 0;
143 
151  virtual Status flush() = 0;
152 
171  virtual Status read(U8* buffer, FwSizeType& size, WaitType wait) = 0;
172 
191  virtual Status write(const U8* buffer, FwSizeType& size, WaitType wait) = 0;
192 
200  virtual FileHandle* getHandle() = 0;
201 
224  static FileInterface* getDelegate(FileHandleStorage& aligned_placement_new_memory,
225  const FileInterface* to_copy = nullptr);
226 };
227 
228 class File final : public FileInterface {
230 
231  public:
234  File();
238  ~File() final;
239 
241  File(const File& other);
242 
244  File& operator=(const File& other);
245 
249  bool isOpen() const;
250 
251  // ------------------------------------
252  // Functions supplying default values
253  // ------------------------------------
254 
268  Os::FileInterface::Status open(const char* path, Mode mode);
269 
285  Os::FileInterface::Status open(const char* path, FwSizeType length, Mode mode);
286 
298  Os::FileInterface::Status open(const Fw::ConstStringBase& path, Mode mode);
299 
313  Os::FileInterface::Status open(const Fw::ConstStringBase& path, Mode mode, OverwriteType overwrite);
314 
331  Status read(U8* buffer, FwSizeType& size);
332 
348  Status write(const U8* buffer, FwSizeType& size);
349 
350  // ------------------------------------
351  // Functions overrides
352  // ------------------------------------
353 
370  Os::FileInterface::Status open(const char* path, Mode mode, OverwriteType overwrite) override;
371 
390  Os::FileInterface::Status open(const char* path, FwSizeType length, Mode mode, OverwriteType overwrite);
391 
397  void close() override;
398 
405  Status size(FwSizeType& size_result) override;
406 
413  Status position(FwSizeType& position_result) override;
414 
427  Status preallocate(FwSizeType offset, FwSizeType length) override;
428 
438  Status seek(FwSignedSizeType offset, SeekType seekType) override;
439 
451  Status seek_absolute(FwSizeType offset_unsigned);
452 
460  Status flush() override;
461 
478 
480  Status read(U8* buffer, FwSizeType& size, WaitType wait) override;
481 
498  Status readline(U8* buffer, FwSizeType& size, WaitType wait);
499 
518  Status write(const U8* buffer, FwSizeType& size, WaitType wait) override;
519 
527  FileHandle* getHandle() override;
528 
554  Status calculateCrc(U32& crc);
555 
572 
584  Status finalizeCrc(U32& crc);
585 
586  private:
587  static const U32 INITIAL_CRC = 0xFFFFFFFF;
588 
589  Mode m_mode = Mode::OPEN_NO_MODE;
590 
591  Utils::Hash m_hash;
592  U8 m_crc_buffer[FW_FILE_CHUNK_SIZE];
593 
594  // This section is used to store the implementation-defined file handle. To Os::File and fprime, this type is
595  // opaque and thus normal allocation cannot be done. Instead, we allow the implementor to store then handle in
596  // the byte-array here and set `handle` to that address for storage.
597  //
598  alignas(FW_HANDLE_ALIGNMENT) FileHandleStorage m_handle_storage;
599  FileInterface& m_delegate;
600 };
601 } // namespace Os
602 #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:248
base implementation of FileHandle
Definition: File.hpp:26
friend struct Os::Test::FileTest::Tester
Definition: File.hpp:229
Path falls outside the configured sandbox directory.
Definition: File.hpp:54
Status calculateCrc(U32 &crc)
calculate the CRC32 of the entire file
Definition: File.cpp:230
PlatformSizeType FwSizeType
File mode not yet selected.
Definition: File.hpp:32
No more available resources.
Definition: File.hpp:52
Status preallocate(FwSizeType offset, FwSizeType length) override
pre-allocate file storage
Definition: File.cpp:123
Status position(FwSizeType &position_result) override
get file pointer position of the currently open file
Definition: File.cpp:113
Open file for writing; writes don&#39;t return until data is on disk.
Definition: File.hpp:36
Status seek_absolute(FwSizeType offset_unsigned)
seek the file pointer to the given offset absolutely with the full range
Definition: File.cpp:148
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:104
No permission to read/write file.
Definition: File.hpp:45
Overwrite file when it exists and creation was requested.
Definition: File.hpp:60
Open file for writing.
Definition: File.hpp:35
PlatformSignedSizeType FwSignedSizeType
Maximum value of status.
Definition: File.hpp:55
virtual FileHandle * getHandle()=0
returns the raw file handle
Declares F Prime read-only string base class.
virtual ~FileInterface()=default
virtual Status size(FwSizeType &size_result)=0
get size of currently open file
Os::FileInterface::Status open(const char *path, Mode mode)
open file with supplied path and mode
Definition: File.cpp:43
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:37
No space left.
Definition: File.hpp:44
Do not wait for read/write operation to finish.
Definition: File.hpp:71
Do wait for read/write operation to finish.
Definition: File.hpp:72
Invalid argument passed in.
Definition: File.hpp:51
Maximum value of mode.
Definition: File.hpp:38
Absolute seek from beginning of file.
Definition: File.hpp:66
Status seek(FwSignedSizeType offset, SeekType seekType) override
seek the file pointer to the given offset
Definition: File.cpp:135
File doesn&#39;t exist (for read)
Definition: File.hpp:43
FileHandle * getHandle() override
returns the raw file handle
Definition: File.cpp:225
Mode for file access is invalid for current operation.
Definition: File.hpp:50
Kernel or file system does not support operation.
Definition: File.hpp:49
void close() override
close the file, if not opened then do nothing
Definition: File.cpp:90
Status write(const U8 *buffer, FwSizeType &size)
write data to this file from the supplied buffer bounded by size
Definition: File.cpp:206
Relative seek from current file offset.
Definition: File.hpp:65
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:54
Status read(U8 *buffer, FwSizeType &size)
read data from this file into supplied buffer bounded by size
Definition: File.cpp:187
Status flush() override
flush file contents to storage
Definition: File.cpp:175
U8 FileHandleStorage[FW_FILE_HANDLE_MAX_SIZE]
Definition: Os.hpp:13
Do NOT overwrite existing files.
Definition: File.hpp:59
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:276
file hasn&#39;t been opened yet
Definition: File.hpp:47
Operation was successful.
Definition: File.hpp:42
A catch-all for other errors. Have to look in implementation-specific code.
Definition: File.hpp:53
Status finalizeCrc(U32 &crc)
finalize and retrieve the CRC value
Definition: File.cpp:266
Open file for reading.
Definition: File.hpp:33
~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:46
file already exist (for CREATE with O_EXCL enabled)
Definition: File.hpp:48
Implementation of malloc based allocator.
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:98
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:34