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 <Fw/Types/Assert.hpp>
7 #include <Os/File.hpp>
8 #include <algorithm>
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_hash(other.m_hash),
28  m_crc_buffer(),
29  m_handle_storage(),
30  m_delegate(*FileInterface::getDelegate(m_handle_storage, &other.m_delegate)) {
31  FW_ASSERT(&this->m_delegate == reinterpret_cast<FileInterface*>(&this->m_handle_storage[0]));
32 }
33 
34 File& File::operator=(const File& other) {
35  if (this != &other) {
36  this->m_mode = other.m_mode;
37  this->m_hash = other.m_hash;
38  this->m_delegate = *FileInterface::getDelegate(m_handle_storage, &other.m_delegate);
39  }
40  return *this;
41 }
42 
43 File::Status File::open(const CHAR* filepath, File::Mode requested_mode) {
44  return this->open(filepath, requested_mode, OverwriteType::NO_OVERWRITE);
45 }
46 
47 File::Status File::open(const CHAR* filepath, File::Mode requested_mode, File::OverwriteType overwrite) {
48  FW_ASSERT(nullptr != filepath);
49  return this->open(filepath, static_cast<FwSizeType>(FileNameStringSize + 1), requested_mode, overwrite);
50 }
51 
52 File::Status File::open(const CHAR* filepath, FwSizeType length, File::Mode requested_mode) {
53  return this->open(filepath, length, requested_mode, OverwriteType::NO_OVERWRITE);
54 }
55 
56 File::Status File::open(const CHAR* filepath,
57  FwSizeType length,
58  File::Mode requested_mode,
59  File::OverwriteType overwrite) {
60  FW_ASSERT(&this->m_delegate == reinterpret_cast<FileInterface*>(&this->m_handle_storage[0]));
61  FW_ASSERT(nullptr != filepath);
62  const FwSizeType string_len = static_cast<FwSizeType>(Fw::StringUtils::string_length(filepath, length));
63  FW_ASSERT(string_len < length, static_cast<FwAssertArgType>(string_len), static_cast<FwAssertArgType>(length));
64  FW_ASSERT(File::Mode::OPEN_NO_MODE < requested_mode && File::Mode::MAX_OPEN_MODE > requested_mode);
65  FW_ASSERT((0 <= this->m_mode) && (this->m_mode < Mode::MAX_OPEN_MODE));
66  FW_ASSERT((0 <= overwrite) && (overwrite < OverwriteType::MAX_OVERWRITE_TYPE));
67  // Check for already opened file
68  if (this->isOpen()) {
69  return File::Status::INVALID_MODE;
70  }
71  File::Status status = this->m_delegate.open(filepath, requested_mode, overwrite);
72  if (status == File::Status::OP_OK) {
73  this->m_mode = requested_mode;
74  // Reset any open CRC calculations
75  this->m_hash.init();
76  }
77 
78  return status;
79 }
80 
81 File::Status File::open(const Fw::ConstStringBase& path, File::Mode requested_mode) {
82  return this->open(path.toChar(), static_cast<FwSizeType>(path.getCapacity()), requested_mode,
83  OverwriteType::NO_OVERWRITE);
84 }
85 
87  return this->open(path.toChar(), static_cast<FwSizeType>(path.getCapacity()), requested_mode, overwrite);
88 }
89 
90 void File::close() {
91  FW_ASSERT(&this->m_delegate == reinterpret_cast<FileInterface*>(&this->m_handle_storage[0]));
92  FW_ASSERT(this->m_mode < Mode::MAX_OPEN_MODE);
93  FW_ASSERT((0 <= this->m_mode) && (this->m_mode < Mode::MAX_OPEN_MODE));
94  this->m_delegate.close();
95  this->m_mode = Mode::OPEN_NO_MODE;
96 }
97 
98 bool File::isOpen() const {
99  FW_ASSERT(&this->m_delegate == reinterpret_cast<const FileInterface*>(&this->m_handle_storage[0]));
100  FW_ASSERT((0 <= this->m_mode) && (this->m_mode < Mode::MAX_OPEN_MODE));
101  return this->m_mode != Mode::OPEN_NO_MODE;
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  if (OPEN_NO_MODE == this->m_mode) {
108  return File::Status::NOT_OPENED;
109  }
110  return this->m_delegate.size(size_result);
111 }
112 
114  FW_ASSERT(&this->m_delegate == reinterpret_cast<FileInterface*>(&this->m_handle_storage[0]));
115  FW_ASSERT((0 <= this->m_mode) && (this->m_mode < Mode::MAX_OPEN_MODE));
116  // Check that the file is open before attempting operation
117  if (OPEN_NO_MODE == this->m_mode) {
118  return File::Status::NOT_OPENED;
119  }
120  return this->m_delegate.position(position_result);
121 }
122 
124  FW_ASSERT(&this->m_delegate == reinterpret_cast<FileInterface*>(&this->m_handle_storage[0]));
125  FW_ASSERT((0 <= this->m_mode) && (this->m_mode < Mode::MAX_OPEN_MODE));
126  // Check that the file is open before attempting operation
127  if (OPEN_NO_MODE == this->m_mode) {
128  return File::Status::NOT_OPENED;
129  } else if (OPEN_READ == this->m_mode) {
130  return File::Status::INVALID_MODE;
131  }
132  return this->m_delegate.preallocate(offset, length);
133 }
134 
136  FW_ASSERT(&this->m_delegate == reinterpret_cast<FileInterface*>(&this->m_handle_storage[0]));
137  FW_ASSERT((0 <= seekType) && (seekType < SeekType::MAX_SEEK_TYPE));
138  // Cannot do a seek with a negative offset in absolute mode
139  FW_ASSERT((seekType == File::SeekType::RELATIVE) || (offset >= 0));
140  FW_ASSERT((0 <= this->m_mode) && (this->m_mode < Mode::MAX_OPEN_MODE));
141  // Check that the file is open before attempting operation
142  if (OPEN_NO_MODE == this->m_mode) {
143  return File::Status::NOT_OPENED;
144  }
145  return this->m_delegate.seek(offset, seekType);
146 }
147 
150  // If the offset can be represented by a signed value, then we can perform a single seek
151  if (static_cast<FwSizeType>(std::numeric_limits<FwSignedSizeType>::max()) >= offset) {
152  // Check that the bounding above is correct
153  FW_ASSERT(static_cast<FwSignedSizeType>(offset) >= 0);
154  status = this->seek(static_cast<FwSignedSizeType>(offset), File::SeekType::ABSOLUTE);
155  }
156  // Otherwise, a full seek to any value represented by FwSizeType can be performed
157  // by at most 3 seeks of a FwSignedSizeType. Two half seeks (rounded down) that are
158  // strictly bounded by std::numeric_limits<FwSignedSizeType>::max() and one seek of
159  // a possibile "odd" byte to ensure odds offsets do not introduce an off-by-one-error.
160  // Thus we perform 3 seeks to guarantee that we can reach any position.
161  else {
162  FwSignedSizeType half_offset = static_cast<FwSignedSizeType>(offset >> 1);
163  bool is_odd = (offset % 2) == 1;
164  status = this->seek(half_offset, File::SeekType::ABSOLUTE);
165  if (status == File::Status::OP_OK) {
166  status = this->seek(half_offset, File::SeekType::RELATIVE);
167  }
168  if (status == File::Status::OP_OK) {
169  status = this->seek((is_odd) ? 1 : 0, File::SeekType::RELATIVE);
170  }
171  }
172  return status;
173 }
174 
176  FW_ASSERT(&this->m_delegate == reinterpret_cast<FileInterface*>(&this->m_handle_storage[0]));
177  FW_ASSERT(this->m_mode < Mode::MAX_OPEN_MODE);
178  // Check that the file is open before attempting operation
179  if (OPEN_NO_MODE == this->m_mode) {
180  return File::Status::NOT_OPENED;
181  } else if (OPEN_READ == this->m_mode) {
182  return File::Status::INVALID_MODE;
183  }
184  return this->m_delegate.flush();
185 }
186 
188  return this->read(buffer, size, WaitType::WAIT);
189 }
190 
192  FW_ASSERT(&this->m_delegate == reinterpret_cast<FileInterface*>(&this->m_handle_storage[0]));
193  FW_ASSERT(buffer != nullptr);
194  FW_ASSERT(this->m_mode < Mode::MAX_OPEN_MODE);
195  // Check that the file is open before attempting operation
196  if (OPEN_NO_MODE == this->m_mode) {
197  size = 0;
198  return File::Status::NOT_OPENED;
199  } else if (OPEN_READ != this->m_mode) {
200  size = 0;
201  return File::Status::INVALID_MODE;
202  }
203  return this->m_delegate.read(buffer, size, wait);
204 }
205 
206 File::Status File::write(const U8* buffer, FwSizeType& size) {
207  return this->write(buffer, size, WaitType::WAIT);
208 }
209 
210 File::Status File::write(const U8* buffer, FwSizeType& size, File::WaitType wait) {
211  FW_ASSERT(&this->m_delegate == reinterpret_cast<FileInterface*>(&this->m_handle_storage[0]));
212  FW_ASSERT(buffer != nullptr);
213  FW_ASSERT(this->m_mode < Mode::MAX_OPEN_MODE);
214  // Check that the file is open before attempting operation
215  if (OPEN_NO_MODE == this->m_mode) {
216  size = 0;
217  return File::Status::NOT_OPENED;
218  } else if (OPEN_READ == this->m_mode) {
219  size = 0;
220  return File::Status::INVALID_MODE;
221  }
222  return this->m_delegate.write(buffer, size, wait);
223 }
224 
226  FW_ASSERT(&this->m_delegate == reinterpret_cast<FileInterface*>(&this->m_handle_storage[0]));
227  return this->m_delegate.getHandle();
228 }
229 
233  crc = 0;
234  for (FwSizeType i = 0; i < std::numeric_limits<FwSizeType>::max(); i++) {
235  status = this->incrementalCrc(size);
236  // Break on eof or error
237  if ((size != FW_FILE_CHUNK_SIZE) || (status != File::OP_OK)) {
238  break;
239  }
240  }
241  // When successful, finalize the CRC
242  if (status == File::OP_OK) {
243  status = this->finalizeCrc(crc);
244  }
245  return status;
246 }
247 
251  if (OPEN_NO_MODE == this->m_mode) {
252  status = File::Status::NOT_OPENED;
253  } else if (OPEN_READ != this->m_mode) {
254  status = File::Status::INVALID_MODE;
255  } else {
256  // Read data without waiting for additional data to be available
257  status = this->read(this->m_crc_buffer, size, File::WaitType::NO_WAIT);
258  if (OP_OK == status) {
260  this->m_hash.update(this->m_crc_buffer, size);
261  }
262  }
263  return status;
264 }
265 
268  this->m_hash.finalize(crc);
269  // Historically, the CRC calculation in File omitted the final 1's complement step. Utils::Hash performs that step
270  // and as such, we must undo it before returning the value to ensure backwards compatibility.
271  crc = ~crc;
272  this->m_hash.init();
273  return status;
274 }
275 
277  const FwSizeType requested_size = size;
278  FW_ASSERT(&this->m_delegate == reinterpret_cast<FileInterface*>(&this->m_handle_storage[0]));
279  FW_ASSERT(buffer != nullptr);
280  FW_ASSERT(this->m_mode < Mode::MAX_OPEN_MODE);
281  // Check that the file is open before attempting operation
282  if (OPEN_NO_MODE == this->m_mode) {
283  size = 0;
284  return File::Status::NOT_OPENED;
285  } else if (OPEN_READ != this->m_mode) {
286  size = 0;
287  return File::Status::INVALID_MODE;
288  }
289  FwSizeType original_location = 0;
290  File::Status status = this->position(original_location);
291  if (status != Os::File::Status::OP_OK) {
292  size = 0;
293  (void)this->seek_absolute(original_location);
294  return status;
295  }
296  FwSizeType read = 0;
297  // Loop reading chunk by chunk
298  for (FwSizeType i = 0; i < size; i += read) {
299  // read in chunks to avoid large buffer allocations
300  FwSizeType current_chunk_size = std::min(size - i, static_cast<FwSizeType>(FW_FILE_CHUNK_SIZE));
301  read = current_chunk_size;
302  status = this->read(buffer + i, read, wait);
303  if (status != File::Status::OP_OK) {
304  return status;
305  }
306  // EOF break out now
307  if (read == 0) {
308  size = i;
310  }
311  // Loop from i to i + current_chunk_size looking for `\n`
312  const FwSizeType chunk_end = i + read;
313  for (FwSizeType j = i; j < chunk_end; j++) {
314  // Newline seek back to after it, return the size read
315  if (buffer[j] == '\n') {
316  size = j + 1;
317  // Ensure that the computation worked and there is not overflow
318  FW_ASSERT(size <= requested_size);
319  FW_ASSERT(std::numeric_limits<FwSizeType>::max() - size >= original_location);
320  (void)this->seek_absolute(original_location + j + 1);
322  }
323  }
324  }
325  // Failed to find newline within data available
327 }
328 } // namespace Os
void update(const void *const data, const FwSizeType len)
Definition: HashImpl.cpp:34
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
Operation succeeded.
Definition: Os.hpp:27
Status calculateCrc(U32 &crc)
calculate the CRC32 of the entire file
Definition: File.cpp:230
A catch-all for other errors. Have to look in implementation-specific code.
PlatformSizeType FwSizeType
File mode not yet selected.
Definition: File.hpp:32
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
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
virtual const CHAR * toChar() const =0
Convert to a C-style char*.
PlatformSignedSizeType FwSignedSizeType
char CHAR
Definition: BasicTypes.h:60
virtual FileHandle * getHandle()=0
returns the raw file handle
virtual ~FileInterface()=default
void init()
Definition: HashImpl.cpp:30
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
Status seek(FwSignedSizeType offset, SeekType seekType) override
seek the file pointer to the given offset
Definition: File.cpp:135
FileHandle * getHandle() override
returns the raw file handle
Definition: File.cpp:225
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
virtual SizeType getCapacity() const =0
Return the size of the buffer.
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
static U32 min(const U32 a, const U32 b)
Definition: Checksum.cpp:16
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
Operation was successful.
Definition: File.hpp:42
A read-only abstract superclass for StringBase.
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
File & operator=(const File &other)
assignment operator that copies the internal representation
Definition: File.cpp:34
void finalize(HashBuffer &buffer) const
Definition: HashImpl.cpp:40
#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:98
PlatformAssertArgType FwAssertArgType
The type of arguments to assert functions.
virtual Status read(U8 *buffer, FwSizeType &size, WaitType wait)=0
read data from this file into supplied buffer bounded by size
FwSizeType string_length(const CHAR *source, FwSizeType buffer_size)
get the length of the source string
Definition: StringUtils.cpp:32