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
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",
288 errno, buffer,
static_cast<POINTER_CAST>(buffer) % 512, size, size % 512);
292 this->m_lastError = errno;
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;
347 FW_ASSERT(idx + size <= totalSize, idx + size);
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;
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.
bool isOpen()
check if file descriptor is open or not.
@ DOESNT_EXIST
File doesn't exist (for read)
@ NOT_OPENED
file hasn't been opened yet
@ BAD_SIZE
Invalid size parameter.
@ OTHER_ERROR
A catch-all for other errors. Have to look in implementation-specific code.
@ FILE_EXISTS
file already exist (for CREATE with O_EXCL enabled)
@ NO_PERMISSION
No permission to read/write file.
@ OP_OK
Operation was successful.
Status write(const void *buffer, NATIVE_INT_TYPE &size, bool waitForDone=true)
write size; will return amount written or errno
const char * getLastErrorString()
get a string of the last error (typically from strerror)
Status seek(NATIVE_INT_TYPE offset, bool absolute=true)
seek to location. If absolute = true, absolute from beginning of file
Status prealloc(NATIVE_INT_TYPE offset, NATIVE_INT_TYPE len)
Status flush()
flush data to disk. No-op on systems that do not support.
@ OPEN_SYNC_DIRECT_WRITE
Open file for writing, bypassing all caching. Requires data alignment.
@ OPEN_APPEND
Open file for appending.
@ OPEN_WRITE
Open file for writing.
@ OPEN_NO_MODE
File mode not yet selected.
@ OPEN_SYNC_WRITE
Open file for writing; writes don't return until data is on disk.
@ OPEN_CREATE
Open file for writing and truncates file if it exists, ie same flags as creat()
@ OPEN_READ
Open file for reading.
NATIVE_INT_TYPE getLastError()
read back last error code (typically errno)
Status open(const char *fileName, Mode mode)
open file. Writing creates file if it doesn't exist
virtual ~File()
Destructor. Will close file if still open.
Status bulkWrite(const void *buffer, NATIVE_UINT_TYPE &totalSize, NATIVE_INT_TYPE chunkSize)
write size; will return amount written or errno
Status read(void *buffer, NATIVE_INT_TYPE &size, bool waitForFull=true)
waitForFull = true to wait for all bytes to be read
Status calculateCRC32(U32 &crc)
calculates the CRC32 of the file
unsigned long update_crc_32(unsigned long crc, char c)