23 #if defined(S_IREAD) && defined(S_IWRITE)
24 #define USER_FLAGS (S_IREAD | S_IWRITE)
25 #elif defined(S_IRUSR) && defined(S_IWUSR)
26 #define USER_FLAGS (S_IRUSR | S_IWUSR)
28 #define USER_FLAGS (0)
32 static_assert(
sizeof(
FwSignedSizeType) >=
sizeof(off_t),
"FwSizeType is not large enough to store values of type off_t");
33 static_assert(
sizeof(
FwSignedSizeType) >=
sizeof(ssize_t),
"FwSizeType is not large enough to store values of type ssize_t");
36 static_assert(std::numeric_limits<FwSignedSizeType>::max() >= std::numeric_limits<off_t>::max(),
37 "Maximum value of FwSizeType less than the maximum value of off_t. Configure a larger type.");
38 static_assert(std::numeric_limits<FwSignedSizeType>::max() >= std::numeric_limits<ssize_t>::max(),
39 "Maximum value of FwSizeType less than the maximum value of ssize_t. Configure a larger type.");
41 "Minimum value of FwSizeType larger than the minimum value of off_t. Configure a larger type.");
43 "Minimum value of FwSizeType larger than the minimum value of ssize_t. Configure a larger type.");
61 switch (requested_mode) {
63 mode_flags = O_RDONLY;
66 mode_flags = O_WRONLY | O_CREAT;
69 mode_flags = O_WRONLY | O_CREAT | O_SYNC;
72 mode_flags = O_WRONLY | O_CREAT | O_TRUNC | ((overwrite == PosixFile::OverwriteType::OVERWRITE) ? 0 : O_EXCL);
75 mode_flags = O_WRONLY | O_CREAT | O_APPEND;
110 off_t original = ::lseek(this->m_handle.
m_file_descriptor, current_position, SEEK_SET);
116 size_result = end_of_file;
139 #if _POSIX_C_SOURCE >= 200112L
144 if (Os::File::Status::NOT_SUPPORTED == status) {
147 status = this->
size(file_size);
151 status = this->
position(file_position);
154 if ((std::numeric_limits<FwSignedSizeType>::max() - offset - length) < 0) {
156 }
else if (file_size < (offset + length)) {
158 status = this->
seek(file_size, PosixFile::SeekType::ABSOLUTE);
163 status = this->
write(
reinterpret_cast<const U8*
>(
"\0"), write_size, PosixFile::WaitType::NO_WAIT);
170 status = this->
seek(file_position, PosixFile::SeekType::ABSOLUTE);
182 off_t actual = ::lseek(this->m_handle.
m_file_descriptor, offset, (seekType == SeekType::ABSOLUTE) ? SEEK_SET : SEEK_CUR);
186 }
else if ((seekType == SeekType::ABSOLUTE) && (actual != offset)) {
205 const FwSignedSizeType maximum = (
size > (std::numeric_limits<FwSignedSizeType>::max()/2)) ? std::numeric_limits<FwSignedSizeType>::max() :
size * 2;
209 ssize_t read_size =
::read(
211 reinterpret_cast<CHAR*
>(&buffer[accumulated]),
212 static_cast<size_t>(
size - accumulated));
217 if (EINTR != errno_store) {
224 else if (read_size == 0) {
227 accumulated += read_size;
241 const FwSignedSizeType maximum = (
size > (std::numeric_limits<FwSignedSizeType>::max()/2)) ? std::numeric_limits<FwSignedSizeType>::max() :
size * 2;
245 ssize_t write_size =
::write(this->m_handle.
m_file_descriptor,
reinterpret_cast<const CHAR*
>(&buffer[accumulated]),
static_cast<size_t>(
size - accumulated));
250 if (EINTR != errno_store) {
256 accumulated += write_size;
271 return &this->m_handle;
uint8_t U8
8-bit unsigned integer
static U32 min(const U32 a, const U32 b)
int PlatformIntType
DefaultTypes.hpp provides fallback defaults for the platform types.
PlatformSignedSizeType FwSignedSizeType
@ OP_OK
Operation was successful.
@ 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.
posix implementation of Os::File
Status position(FwSignedSizeType &position_result) override
get file pointer position of the currently open file
void close() override
close the file, if not opened then do nothing
Status seek(FwSignedSizeType offset, SeekType seekType) override
seek the file pointer to the given offset
Status preallocate(FwSignedSizeType offset, FwSignedSizeType length) override
pre-allocate file storage
Status read(U8 *buffer, FwSignedSizeType &size, WaitType wait) override
read data from this file into supplied buffer bounded by size
Status write(const U8 *buffer, FwSignedSizeType &size, WaitType wait) override
read data from this file into supplied buffer bounded by size
FileHandle * getHandle() override
returns the raw file handle
PosixFile()=default
constructor
Status size(FwSignedSizeType &size_result) override
get size of currently open file
Status flush() override
flush file contents to storage
PosixFile & operator=(const PosixFile &other)
assignment operator that copies the internal representation
Os::FileInterface::Status open(const char *path, Mode mode, OverwriteType overwrite) override
open file with supplied path and mode
@ OP_OK
Operation was successful.
@ OTHER_ERROR
other OS-specific error
File::Status errno_to_file_status(PlatformIntType errno_input)
base implementation of FileHandle
PlatformIntType m_file_descriptor
Posix file descriptor.
static constexpr PlatformIntType INVALID_FILE_DESCRIPTOR
static constexpr PlatformIntType ERROR_RETURN_VALUE