72 File::File() :m_fd(0),m_mode(OPEN_NO_MODE),m_lastError(0) {
76 if (this->m_mode != OPEN_NO_MODE) {
81 File::Status File::open(
const char* fileName, File::Mode mode) {
82 return this->open(fileName, mode,
true);
85 File::Status File::open(
const char* fileName, File::Mode mode,
bool include_excl) {
87 if (openInterceptor) {
102 flags = O_WRONLY | O_CREAT | O_TRUNC;
104 case OPEN_SYNC_WRITE:
105 flags = O_WRONLY | O_CREAT | O_SYNC;
107 case OPEN_SYNC_DIRECT_WRITE:
108 flags = O_WRONLY | O_CREAT | O_DSYNC
116 flags = O_WRONLY | O_CREAT | O_TRUNC;
135 this->m_lastError = errno;
159 if (OPEN_NO_MODE == this->m_mode) {
163 File::Status fileStatus =
OP_OK;
175 fileStatus = DOESNT_EXIST;
189 if (seekInterceptor) {
198 if (OPEN_NO_MODE == this->m_mode) {
205 off_t act_off = ::lseek(this->m_fd,offset,whence);
209 if (act_off != offset) {
211 this->m_lastError = errno;
221 File::Status File::read(
void * buffer,
NATIVE_INT_TYPE &size,
bool waitForFull) {
225 if (readInterceptor) {
227 if (not
readInterceptor(stat,buffer,size,waitForFull,readInterceptorPtr)) {
233 if (OPEN_NO_MODE == this->m_mode) {
243 while (maxIters > 0) {
245 ssize_t readSize = ::read(this->m_fd,
247 static_cast<char*
>(buffer)
253 if (readSize != size-accSize) {
255 if (-1 == readSize) {
264 this->m_lastError = errno;
267 }
else if (0 == readSize) {
272 if (not waitForFull) {
276 U8* charPtr =
static_cast<U8*
>(buffer);
277 charPtr = &charPtr[readSize];
278 buffer =
static_cast<void*
>(charPtr);
300 File::Status File::write(
const void * buffer,
NATIVE_INT_TYPE &size,
bool waitForDone) {
302 if (writeInterceptor) {
304 if (not
writeInterceptor(stat,buffer,size,waitForDone,writeInterceptorPtr)) {
310 if (OPEN_NO_MODE == this->m_mode) {
317 while (maxIters > 0) {
320 static_cast<char*
>(
const_cast<void*
>(buffer))
325 if (-1 == writeSize) {
337 this->m_lastError = errno;
350 File::Status File::bulkWrite(
const void * buffer,
NATIVE_UINT_TYPE &totalSize,
360 if (idx + chunkSize > totalSize) {
361 size = totalSize - idx;
367 totalSize = newBytesWritten;
372 sync_file_range(this->m_fd,
373 startPosition + newBytesWritten,
375 SYNC_FILE_RANGE_WRITE);
377 if (newBytesWritten) {
378 sync_file_range(this->m_fd,
379 startPosition + newBytesWritten - chunkSize,
381 SYNC_FILE_RANGE_WAIT_BEFORE
382 | SYNC_FILE_RANGE_WRITE
383 | SYNC_FILE_RANGE_WAIT_AFTER);
387 newBytesWritten += toWrite;
390 totalSize = newBytesWritten;
394 File::Status File::flush() {
396 if (OPEN_NO_MODE == this->m_mode) {
400 File::Status stat =
OP_OK;
402 if (-1 == fsync(this->m_fd)) {
417 (void)::close(this->m_fd);
418 this->m_mode = OPEN_NO_MODE;
425 const char* File::getLastErrorString() {
426 return strerror(this->m_lastError);
PlatformIntType NATIVE_INT_TYPE
uint8_t U8
8-bit unsigned integer
PlatformUIntType NATIVE_UINT_TYPE
C++-compatible configuration header for fprime configuration.
@ OP_OK
Operation was successful.
@ OP_OK
Operation was successful.
@ NO_PERMISSION
No permission to write.
@ OTHER_ERROR
other OS-specific error
void registerReadInterceptor(ReadInterceptor funcPtr, void *ptr)
bool(* OpenInterceptor)(Os::File::Status &status, const char *fileName, Os::File::Mode mode, void *ptr)
static WriteInterceptor writeInterceptor
static void * readInterceptorPtr
static void * seekInterceptorPtr
static OpenInterceptor openInterceptor
void clearWriteInterceptor()
void clearSeekInterceptor()
static ReadInterceptor readInterceptor
void registerSeekInterceptor(SeekInterceptor funcPtr, void *ptr)
void setLastError(NATIVE_INT_TYPE error)
static NATIVE_INT_TYPE lastError
void registerWriteInterceptor(WriteInterceptor funcPtr, void *ptr)
static SeekInterceptor seekInterceptor
static void * openInterceptorPtr
static void * writeInterceptorPtr
bool(* WriteInterceptor)(Os::File::Status &status, const void *buffer, NATIVE_INT_TYPE &size, bool waitForDone, void *ptr)
void registerOpenInterceptor(OpenInterceptor funcPtr, void *ptr)
bool(* SeekInterceptor)(Os::File::Status &status, NATIVE_INT_TYPE offset, bool absolute, void *ptr)
void clearReadInterceptor()
void clearOpenInterceptor()
bool(* ReadInterceptor)(Os::File::Status &status, void *buffer, NATIVE_INT_TYPE &size, bool waitForFull, void *ptr)