16 #include <sys/types.h>
25 #define DEBUG_PRINT(...)
29 File::File() :m_fd(-1),m_mode(OPEN_NO_MODE),m_lastError(0) {
39 return this->
open(fileName, mode,
true);
51 flags = O_WRONLY | O_CREAT;
54 flags = O_WRONLY | O_CREAT | O_SYNC;
56 #ifndef TGT_OS_TYPE_RTEMS
57 case OPEN_SYNC_DIRECT_WRITE:
58 flags = O_WRONLY | O_CREAT | O_DSYNC
67 flags = O_WRONLY | O_CREAT | O_TRUNC;
73 flags = O_WRONLY | O_CREAT | O_APPEND;
89 this->m_lastError = errno;
115 return this->m_fd > 0;
158 off_t act_off = ::lseek(this->m_fd,offset,whence);
162 if (act_off != offset) {
164 this->m_lastError = errno;
196 while (maxIters > 0) {
198 ssize_t readSize =
::read(this->m_fd,
200 static_cast<char*
>(buffer)
206 if (readSize !=
size-accSize) {
208 if (-1 == readSize) {
217 this->m_lastError = errno;
220 }
else if (0 == readSize) {
224 if (not waitForFull) {
228 U8* charPtr =
static_cast<U8*
>(buffer);
229 charPtr = &charPtr[readSize];
230 buffer =
static_cast<void*
>(charPtr);
269 while (maxIters > 0) {
272 static_cast<char*
>(
const_cast<void*
>(buffer))
278 if (-1 == writeSize) {
287 DEBUG_PRINT(
"Error %d during write of 0x%p, addrMod %d, size %d, sizeMod %d\n",
292 this->m_lastError = errno;
298 if ((OPEN_SYNC_DIRECT_WRITE != this->m_mode) && (waitForDone)) {
300 sync_file_range(this->m_fd,
position - writeSize, writeSize,
301 SYNC_FILE_RANGE_WAIT_BEFORE
302 | SYNC_FILE_RANGE_WRITE
303 | SYNC_FILE_RANGE_WAIT_AFTER);
326 if (totalSize == 0) {
330 else if (chunkSize <= 0) {
343 if (idx + chunkSize > totalSize) {
344 size = totalSize - idx;
351 totalSize = newBytesWritten;
356 sync_file_range(this->m_fd,
357 startPosition + newBytesWritten,
359 SYNC_FILE_RANGE_WRITE);
361 if (newBytesWritten) {
362 sync_file_range(this->m_fd,
363 startPosition + newBytesWritten - chunkSize,
365 SYNC_FILE_RANGE_WAIT_BEFORE
366 | SYNC_FILE_RANGE_WRITE
367 | SYNC_FILE_RANGE_WAIT_AFTER);
369 posix_fadvise(this->m_fd,
370 startPosition + newBytesWritten - chunkSize,
371 chunkSize, POSIX_FADV_DONTNEED);
375 newBytesWritten += toWrite;
378 totalSize = newBytesWritten;
390 if (-1 == fsync(this->m_fd)) {
405 if ((this->m_fd != -1) and (this->m_mode !=
OPEN_NO_MODE)) {
406 (void)::
close(this->m_fd);
413 return this->m_lastError;
416 const char* File::getLastErrorString() {
417 return strerror(this->m_lastError);
429 const U32 maxChunkSize = 32;
430 const U32 initialSeed = 0xFFFFFFFF;
434 if (status !=
OP_OK) {
439 U8 file_buffer[maxChunkSize];
441 bool endOfFile =
false;
443 U32 seed = initialSeed;
444 const U32 maxIters = std::numeric_limits<U32>::max();
447 while (!endOfFile && numIters < maxIters) {
450 int chunkSize = maxChunkSize;
452 status =
read(file_buffer, chunkSize,
false);
453 if (status ==
OP_OK) {
456 if (chunkSize == 0) {
463 while (chunkIdx < chunkSize) {
PlatformPointerCastType POINTER_CAST
PlatformIntType NATIVE_INT_TYPE
uint8_t U8
8-bit unsigned integer
PlatformUIntType NATIVE_UINT_TYPE
C++-compatible configuration header for fprime configuration.
Status seek(FwSignedSizeType offset, SeekType seekType) override
seek the file pointer to the given offset
Status read(U8 *buffer, FwSignedSizeType &size)
read data from this file into supplied buffer bounded by size
Status flush() override
flush file contents to storage
Status size(FwSignedSizeType &size_result) override
get size of currently open file
void close() override
close the file, if not opened then do nothing
Os::FileInterface::Status open(const char *path, Mode mode)
open file with supplied path and mode
Status position(FwSignedSizeType &position_result) override
get file pointer position of the currently open file
bool isOpen() const
determine if the file is open
Status write(const U8 *buffer, FwSignedSizeType &size)
write data to this file from the supplied buffer bounded by size
@ NO_PERMISSION
No permission to read/write file.
@ NOT_OPENED
file hasn't been opened yet
@ OTHER_ERROR
A catch-all for other errors. Have to look in implementation-specific code.
@ BAD_SIZE
Invalid size parameter.
@ OP_OK
Operation was successful.
@ DOESNT_EXIST
File doesn't exist (for read)
@ FILE_EXISTS
file already exist (for CREATE with O_EXCL enabled)
@ OPEN_NO_MODE
File mode not yet selected.
@ OPEN_WRITE
Open file for writing.
@ OPEN_CREATE
Open file for writing and truncates file if it exists, ie same flags as creat()
@ OPEN_READ
Open file for reading.
@ OPEN_APPEND
Open file for appending.
@ OPEN_SYNC_WRITE
Open file for writing; writes don't return until data is on disk.
unsigned long update_crc_32(unsigned long crc, char c)