1 #include <FpConfig.hpp>
2 #include <Fw/Types/BasicTypes.hpp>
4 #include <Fw/Types/Assert.hpp>
17 #include <sys/types.h>
26 #define DEBUG_PRINT(x,...)
30 File::File() :m_fd(-1),m_mode(OPEN_NO_MODE),m_lastError(0) {
40 return this->
open(fileName, mode,
true);
52 flags = O_WRONLY | O_CREAT;
55 flags = O_WRONLY | O_CREAT | O_SYNC;
57 #ifndef TGT_OS_TYPE_RTEMS
59 flags = O_WRONLY | O_CREAT | O_DSYNC
68 flags = O_WRONLY | O_CREAT | O_TRUNC;
74 flags = O_WRONLY | O_CREAT | O_APPEND;
90 this->m_lastError = errno;
116 return this->m_fd > 0;
159 off_t act_off = ::lseek(this->m_fd,offset,whence);
163 if (act_off != offset) {
165 this->m_lastError = errno;
197 while (maxIters > 0) {
199 ssize_t readSize =
::read(this->m_fd,
201 static_cast<char*
>(buffer)
207 if (readSize != size-accSize) {
209 if (-1 == readSize) {
218 this->m_lastError = errno;
221 }
else if (0 == readSize) {
225 if (not waitForFull) {
229 U8* charPtr =
static_cast<U8*
>(buffer);
230 charPtr = &charPtr[readSize];
231 buffer =
static_cast<void*
>(charPtr);
270 while (maxIters > 0) {
273 static_cast<char*
>(
const_cast<void*
>(buffer))
279 if (-1 == writeSize) {
288 DEBUG_PRINT(
"Error %d during write of 0x%p, addrMod %d, size %d, sizeMod %d\n",
289 errno, buffer,
static_cast<POINTER_CAST
>(buffer) % 512, size, size % 512);
293 this->m_lastError = errno;
301 sync_file_range(this->m_fd, position - writeSize, writeSize,
302 SYNC_FILE_RANGE_WAIT_BEFORE
303 | SYNC_FILE_RANGE_WRITE
304 | SYNC_FILE_RANGE_WAIT_AFTER);
327 if (totalSize == 0) {
331 else if (chunkSize <= 0) {
344 if (idx + chunkSize > totalSize) {
345 size = totalSize - idx;
348 FW_ASSERT(idx + size <= totalSize, idx + size);
352 totalSize = newBytesWritten;
357 sync_file_range(this->m_fd,
358 startPosition + newBytesWritten,
360 SYNC_FILE_RANGE_WRITE);
362 if (newBytesWritten) {
363 sync_file_range(this->m_fd,
364 startPosition + newBytesWritten - chunkSize,
366 SYNC_FILE_RANGE_WAIT_BEFORE
367 | SYNC_FILE_RANGE_WRITE
368 | SYNC_FILE_RANGE_WAIT_AFTER);
370 posix_fadvise(this->m_fd,
371 startPosition + newBytesWritten - chunkSize,
372 chunkSize, POSIX_FADV_DONTNEED);
376 newBytesWritten += toWrite;
379 totalSize = newBytesWritten;
391 if (-1 == fsync(this->m_fd)) {
406 if ((this->m_fd != -1) and (this->m_mode !=
OPEN_NO_MODE)) {
407 (void)::
close(this->m_fd);
414 return this->m_lastError;
418 return strerror(this->m_lastError);
430 const U32 maxChunkSize = 32;
431 const U32 initialSeed = 0xFFFFFFFF;
435 if (status !=
OP_OK) {
440 U8 file_buffer[maxChunkSize];
442 bool endOfFile =
false;
444 U32 seed = initialSeed;
445 const U32 maxIters = std::numeric_limits<U32>::max();
448 while (!endOfFile && numIters < maxIters) {
451 int chunkSize = maxChunkSize;
453 status =
read(file_buffer, chunkSize,
false);
454 if (status ==
OP_OK) {
457 if (chunkSize == 0) {
464 while (chunkIdx < chunkSize) {