F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
File.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title Os/File.cpp
3 // \brief common function implementation for Os::File
4 // ======================================================================
5 #include <Os/File.hpp>
6 #include <Fw/Types/Assert.hpp>
7 
8 extern "C" {
9 #include <Utils/Hash/libcrc/lib_crc.h> // borrow CRC
10 }
11 namespace Os {
12 
13 File::File() : m_crc_buffer(), m_handle_storage(), m_delegate(*FileInterface::getDelegate(m_handle_storage)) {
14  FW_ASSERT(&this->m_delegate == reinterpret_cast<FileInterface*>(&this->m_handle_storage[0]));
15 }
16 
18  FW_ASSERT(&this->m_delegate == reinterpret_cast<FileInterface*>(&this->m_handle_storage[0]));
19  if (this->m_mode != OPEN_NO_MODE) {
20  this->close();
21  }
22  m_delegate.~FileInterface();
23 }
24 
25 File::File(const File& other) :
26  m_mode(other.m_mode),
27  m_path(other.m_path),
28  m_crc(other.m_crc),
29  m_crc_buffer(),
30  m_handle_storage(),
31  m_delegate(*FileInterface::getDelegate(m_handle_storage, &other.m_delegate)) {
32  FW_ASSERT(&this->m_delegate == reinterpret_cast<FileInterface*>(&this->m_handle_storage[0]));
33 }
34 
35 File& File::operator=(const File& other) {
36  if (this != &other) {
37  this->m_mode = other.m_mode;
38  this->m_path = other.m_path;
39  this->m_crc = other.m_crc;
40  this->m_delegate = *FileInterface::getDelegate(m_handle_storage, &other.m_delegate);
41  }
42  return *this;
43 }
44 
45 File::Status File::open(const CHAR* filepath, File::Mode requested_mode) {
46  return this->open(filepath, requested_mode, OverwriteType::NO_OVERWRITE);
47 }
48 
49 File::Status File::open(const CHAR* filepath, File::Mode requested_mode, File::OverwriteType overwrite) {
50  FW_ASSERT(&this->m_delegate == reinterpret_cast<FileInterface*>(&this->m_handle_storage[0]));
51  FW_ASSERT(nullptr != filepath);
52  FW_ASSERT(File::Mode::OPEN_NO_MODE < requested_mode && File::Mode::MAX_OPEN_MODE > requested_mode);
53  FW_ASSERT((0 <= this->m_mode) && (this->m_mode < Mode::MAX_OPEN_MODE));
54  FW_ASSERT((0 <= overwrite) && (overwrite < OverwriteType::MAX_OVERWRITE_TYPE));
55  // Check for already opened file
56  if (this->isOpen()) {
57  return File::Status::INVALID_MODE;
58  }
59  File::Status status = this->m_delegate.open(filepath, requested_mode, overwrite);
60  if (status == File::Status::OP_OK) {
61  this->m_mode = requested_mode;
62  this->m_path = filepath;
63  // Reset any open CRC calculations
64  this->m_crc = File::INITIAL_CRC;
65  }
66 
67  return status;
68 }
69 
70 void File::close() {
71  FW_ASSERT(&this->m_delegate == reinterpret_cast<FileInterface*>(&this->m_handle_storage[0]));
72  FW_ASSERT(this->m_mode < Mode::MAX_OPEN_MODE);
73  FW_ASSERT((0 <= this->m_mode) && (this->m_mode < Mode::MAX_OPEN_MODE));
74  this->m_delegate.close();
75  this->m_mode = Mode::OPEN_NO_MODE;
76  this->m_path = nullptr;
77 }
78 
79 bool File::isOpen() const {
80  FW_ASSERT(&this->m_delegate == reinterpret_cast<const FileInterface*>(&this->m_handle_storage[0]));
81  FW_ASSERT((0 <= this->m_mode) && (this->m_mode < Mode::MAX_OPEN_MODE));
82  return this->m_mode != Mode::OPEN_NO_MODE;
83 }
84 
86  FW_ASSERT(&this->m_delegate == reinterpret_cast<FileInterface*>(&this->m_handle_storage[0]));
87  FW_ASSERT((0 <= this->m_mode) && (this->m_mode < Mode::MAX_OPEN_MODE));
88  if (OPEN_NO_MODE == this->m_mode) {
89  return File::Status::NOT_OPENED;
90  }
91  return this->m_delegate.size(size_result);
92 }
93 
95  FW_ASSERT(&this->m_delegate == reinterpret_cast<FileInterface*>(&this->m_handle_storage[0]));
96  FW_ASSERT((0 <= this->m_mode) && (this->m_mode < Mode::MAX_OPEN_MODE));
97  // Check that the file is open before attempting operation
98  if (OPEN_NO_MODE == this->m_mode) {
99  return File::Status::NOT_OPENED;
100  }
101  return this->m_delegate.position(position_result);
102 }
103 
105  FW_ASSERT(&this->m_delegate == reinterpret_cast<FileInterface*>(&this->m_handle_storage[0]));
106  FW_ASSERT((0 <= this->m_mode) && (this->m_mode < Mode::MAX_OPEN_MODE));
107  // Check that the file is open before attempting operation
108  if (OPEN_NO_MODE == this->m_mode) {
109  return File::Status::NOT_OPENED;
110  } else if (OPEN_READ == this->m_mode) {
111  return File::Status::INVALID_MODE;
112  }
113  return this->m_delegate.preallocate(offset, length);
114 }
115 
117  FW_ASSERT(&this->m_delegate == reinterpret_cast<FileInterface*>(&this->m_handle_storage[0]));
118  FW_ASSERT((0 <= seekType) && (seekType < SeekType::MAX_SEEK_TYPE));
119  // Cannot do a seek with a negative offset in absolute mode
120  FW_ASSERT((seekType == File::SeekType::RELATIVE) || (offset >= 0));
121  FW_ASSERT((0 <= this->m_mode) && (this->m_mode < Mode::MAX_OPEN_MODE));
122  // Check that the file is open before attempting operation
123  if (OPEN_NO_MODE == this->m_mode) {
124  return File::Status::NOT_OPENED;
125  }
126  return this->m_delegate.seek(offset, seekType);
127 }
128 
131  // If the offset can be represented by a signed value, then we can perform a single seek
132  if (static_cast<FwSizeType>(std::numeric_limits<FwSignedSizeType>::max()) >= offset) {
133  // Check that the bounding above is correct
134  FW_ASSERT(static_cast<FwSignedSizeType>(offset) >= 0);
135  status = this->seek(static_cast<FwSignedSizeType>(offset), File::SeekType::ABSOLUTE);
136  }
137  // Otherwise, a full seek to any value represented by FwSizeType can be performed
138  // by at most 3 seeks of a FwSignedSizeType. Two half seeks (rounded down) that are
139  // strictly bounded by std::numeric_limits<FwSignedSizeType>::max() and one seek of
140  // a possibile "odd" byte to ensure odds offsets do not introduce an off-by-one-error.
141  // Thus we perform 3 seeks to guarantee that we can reach any position.
142  else {
143  FwSignedSizeType half_offset = static_cast<FwSignedSizeType>(offset >> 1);
144  bool is_odd = (offset % 2) == 1;
145  status = this->seek(half_offset, File::SeekType::ABSOLUTE);
146  if (status == File::Status::OP_OK) {
147  status = this->seek(half_offset, File::SeekType::RELATIVE);
148  }
149  if (status == File::Status::OP_OK) {
150  status = this->seek((is_odd) ? 1 : 0, File::SeekType::RELATIVE);
151  }
152  }
153  return status;
154 }
155 
157  FW_ASSERT(&this->m_delegate == reinterpret_cast<FileInterface*>(&this->m_handle_storage[0]));
158  FW_ASSERT(this->m_mode < Mode::MAX_OPEN_MODE);
159  // Check that the file is open before attempting operation
160  if (OPEN_NO_MODE == this->m_mode) {
161  return File::Status::NOT_OPENED;
162  } else if (OPEN_READ == this->m_mode) {
163  return File::Status::INVALID_MODE;
164  }
165  return this->m_delegate.flush();
166 }
167 
169  return this->read(buffer, size, WaitType::WAIT);
170 }
171 
173  FW_ASSERT(&this->m_delegate == reinterpret_cast<FileInterface*>(&this->m_handle_storage[0]));
174  FW_ASSERT(buffer != nullptr);
175  FW_ASSERT(this->m_mode < Mode::MAX_OPEN_MODE);
176  // Check that the file is open before attempting operation
177  if (OPEN_NO_MODE == this->m_mode) {
178  size = 0;
179  return File::Status::NOT_OPENED;
180  } else if (OPEN_READ != this->m_mode) {
181  size = 0;
182  return File::Status::INVALID_MODE;
183  }
184  return this->m_delegate.read(buffer, size, wait);
185 }
186 
187 File::Status File::write(const U8* buffer, FwSizeType &size) {
188  return this->write(buffer, size, WaitType::WAIT);
189 }
190 
191 
192 File::Status File::write(const U8* buffer, FwSizeType &size, File::WaitType wait) {
193  FW_ASSERT(&this->m_delegate == reinterpret_cast<FileInterface*>(&this->m_handle_storage[0]));
194  FW_ASSERT(buffer != nullptr);
195  FW_ASSERT(this->m_mode < Mode::MAX_OPEN_MODE);
196  // Check that the file is open before attempting operation
197  if (OPEN_NO_MODE == this->m_mode) {
198  size = 0;
199  return File::Status::NOT_OPENED;
200  } else if (OPEN_READ == this->m_mode) {
201  size = 0;
202  return File::Status::INVALID_MODE;
203  }
204  return this->m_delegate.write(buffer, size, wait);
205 }
206 
208  FW_ASSERT(&this->m_delegate == reinterpret_cast<FileInterface*>(&this->m_handle_storage[0]));
209  return this->m_delegate.getHandle();
210 }
211 
215  crc = 0;
216  for (FwSizeType i = 0; i < std::numeric_limits<FwSizeType>::max(); i++) {
217  status = this->incrementalCrc(size);
218  // Break on eof or error
219  if ((size != FW_FILE_CHUNK_SIZE) || (status != File::OP_OK)) {
220  break;
221  }
222  }
223  // When successful, finalize the CRC
224  if (status == File::OP_OK) {
225  status = this->finalizeCrc(crc);
226  }
227  return status;
228 }
229 
233  if (OPEN_NO_MODE == this->m_mode) {
234  status = File::Status::NOT_OPENED;
235  } else if (OPEN_READ != this->m_mode) {
236  status = File::Status::INVALID_MODE;
237  } else {
238  // Read data without waiting for additional data to be available
239  status = this->read(this->m_crc_buffer, size, File::WaitType::NO_WAIT);
240  if (OP_OK == status) {
241  for (FwSizeType i = 0; i < size && i < FW_FILE_CHUNK_SIZE; i++) {
242  this->m_crc =
243  static_cast<U32>(
244  update_crc_32(this->m_crc, static_cast<CHAR>(this->m_crc_buffer[i]))
245  );
246  }
247  }
248  }
249  return status;
250 }
251 
254  crc = this->m_crc;
255  this->m_crc = File::INITIAL_CRC;
256  return status;
257 }
258 
260  const FwSizeType requested_size = size;
261  FW_ASSERT(&this->m_delegate == reinterpret_cast<FileInterface*>(&this->m_handle_storage[0]));
262  FW_ASSERT(buffer != nullptr);
263  FW_ASSERT(this->m_mode < Mode::MAX_OPEN_MODE);
264  // Check that the file is open before attempting operation
265  if (OPEN_NO_MODE == this->m_mode) {
266  size = 0;
267  return File::Status::NOT_OPENED;
268  } else if (OPEN_READ != this->m_mode) {
269  size = 0;
270  return File::Status::INVALID_MODE;
271  }
272  FwSizeType original_location;
273  File::Status status = this->position(original_location);
274  if (status != Os::File::Status::OP_OK) {
275  size = 0;
276  (void) this->seek_absolute(original_location);
277  return status;
278  }
279  FwSizeType read = 0;
280  // Loop reading chunk by chunk
281  for (FwSizeType i = 0; i < size; i += read) {
282  FwSizeType current_chunk_size = FW_MIN(size - i, FW_FILE_CHUNK_SIZE);
283  read = current_chunk_size;
284  status = this->read(buffer + i, read, wait);
285  if (status != File::Status::OP_OK) {
286  return status;
287  }
288  // EOF break out now
289  if (read == 0) {
290  size = i;
292  }
293  // Loop from i to i + current_chunk_size looking for `\n`
294  for (FwSizeType j = i; j < (i + read); j++) {
295  // Newline seek back to after it, return the size read
296  if (buffer[j] == '\n') {
297  size = j + 1;
298  // Ensure that the computation worked and there is not overflow
299  FW_ASSERT(size <= requested_size);
300  FW_ASSERT(std::numeric_limits<FwSizeType>::max() - size >= original_location);
301  (void) this->seek_absolute(original_location + j + 1);
303  }
304  }
305  }
306  // Failed to find newline within data available
308 }
309 } // Os
310 
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:230
base implementation of FileHandle
Definition: File.hpp:14
Operation succeeded.
Definition: Os.hpp:26
Status calculateCrc(U32 &crc)
calculate the CRC32 of the entire file
Definition: File.cpp:212
A catch-all for other errors. Have to look in implementation-specific code.
PlatformSizeType FwSizeType
File mode not yet selected.
Definition: File.hpp:20
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
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
PlatformSignedSizeType FwSignedSizeType
Os::FileInterface::Status open(const char *path, Mode mode)
open file with supplied path and mode
char CHAR
Definition: BasicTypes.h:62
virtual FileHandle * getHandle()=0
returns the raw file handle
virtual ~FileInterface()=default
unsigned long update_crc_32(unsigned long crc, char c)
Definition: lib_crc.c:271
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
#define FW_MIN(a, b)
MIN macro.
Definition: BasicTypes.h:95
Status seek(FwSignedSizeType offset, SeekType seekType) override
seek the file pointer to the given offset
Definition: File.cpp:116
FileHandle * getHandle() override
returns the raw file handle
Definition: File.cpp:207
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
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:56
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
#define FW_FILE_CHUNK_SIZE
Chunk size for working with files in the OSAL layer.
Definition: FpConfig.h:356
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:259
Operation was successful.
Definition: File.hpp:30
Status finalizeCrc(U32 &crc)
finalize and retrieve the CRC value
Definition: File.cpp:252
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
virtual Status preallocate(FwSizeType offset, FwSizeType length)=0
pre-allocate file storage
File & operator=(const File &other)
assignment operator that copies the internal representation
Definition: File.cpp:35
#define FW_ASSERT(...)
Definition: Assert.hpp:14
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