24 #if defined(S_IREAD) && defined(S_IWRITE) 25 #define USER_FLAGS (S_IREAD | S_IWRITE) 26 #elif defined(S_IRUSR) && defined(S_IWUSR) 27 #define USER_FLAGS (S_IRUSR | S_IWUSR) 29 #define USER_FLAGS (0) 35 #define SYNC_FLAGS O_SYNC 37 #define SYNC_FLAGS (0) 49 "FwSignedSizeType is not large enough to store values of type off_t");
51 "FwSignedSizeType is not large enough to store values of type ssize_t");
52 static_assert(
sizeof(
FwSizeType) >=
sizeof(
size_t),
"FwSizeType is not large enough to store values of type size_t");
55 static_assert(std::numeric_limits<FwSignedSizeType>::max() >= std::numeric_limits<off_t>::max(),
56 "Maximum value of FwSignedSizeType less than the maximum value of off_t. Configure a larger type.");
57 static_assert(std::numeric_limits<FwSizeType>::max() >=
OFF_T_MAX_LIMIT,
58 "Maximum value of FwSizeType less than the maximum value of off_t. Configure a larger type.");
59 static_assert(std::numeric_limits<FwSignedSizeType>::max() >= std::numeric_limits<ssize_t>::max(),
60 "Maximum value of FwSignedSizeType less than the maximum value of ssize_t. Configure a larger type.");
62 "Minimum value of FwSignedSizeType larger than the minimum value of off_t. Configure a larger type.");
64 "Minimum value of FwSizeType larger than the minimum value of ssize_t. Configure a larger type.");
65 static_assert(std::numeric_limits<FwSizeType>::max() >= std::numeric_limits<size_t>::max(),
66 "Maximum value of FwSizeType less than the maximum value of size_t. Configure a larger type.");
86 switch (requested_mode) {
88 mode_flags = O_RDONLY;
91 mode_flags = O_WRONLY | O_CREAT;
98 O_WRONLY | O_CREAT | O_TRUNC | ((overwrite == PosixFile::OverwriteType::OVERWRITE) ? 0 : O_EXCL);
101 mode_flags = O_WRONLY | O_CREAT | O_APPEND;
109 int errno_store = errno;
134 int errno_store = errno;
138 (void)::lseek(this->m_handle.
m_file_descriptor, static_cast<off_t>(current_position), SEEK_SET);
139 size_result =
static_cast<FwSizeType>(end_of_file);
149 int errno_store = errno;
153 position_result =
static_cast<FwSizeType>(actual);
161 (std::numeric_limits<off_t>::max() - length) < offset) {
162 status = Os::File::Status::BAD_SIZE;
168 #if _POSIX_C_SOURCE >= 200112L && !(defined(FPRIME_SYNTHETIC_FALLOCATE) && FPRIME_SYNTHETIC_FALLOCATE) 171 ::posix_fallocate(this->m_handle.
m_file_descriptor, static_cast<off_t>(offset), static_cast<off_t>(length));
176 if (Os::File::Status::NOT_SUPPORTED == status) {
179 status = this->
size(file_size);
183 status = this->
position(file_position);
185 if (file_position > static_cast<FwSizeType>(std::numeric_limits<FwSignedSizeType>::max()) ||
186 file_size > static_cast<FwSizeType>(std::numeric_limits<FwSignedSizeType>::max())) {
187 status = Os::File::Status::BAD_SIZE;
191 const FwSizeType write_length = (offset + length) - file_size;
192 status = this->
seek(static_cast<FwSignedSizeType>(file_size), PosixFile::SeekType::ABSOLUTE);
195 for (
FwSizeType i = 0; i < write_length; i++) {
198 this->
write(reinterpret_cast<const U8*>(
"\0"), write_size, PosixFile::WaitType::NO_WAIT);
206 this->
seek(static_cast<FwSignedSizeType>(file_position), PosixFile::SeekType::ABSOLUTE);
217 if (offset > std::numeric_limits<off_t>::max()) {
220 off_t actual = ::lseek(this->m_handle.
m_file_descriptor, static_cast<off_t>(offset),
221 (seekType == SeekType::ABSOLUTE) ? SEEK_SET : SEEK_CUR);
222 int errno_store = errno;
225 }
else if ((seekType == SeekType::ABSOLUTE) && (actual != offset)) {
235 int errno_store = errno;
246 (
size > (std::numeric_limits<FwSizeType>::max() / 2)) ? std::numeric_limits<FwSizeType>::max() :
size * 2;
256 static_cast<size_t>(
size - accumulated));
259 int errno_store = errno;
261 if (EINTR != errno_store) {
268 else if (read_size == 0) {
271 accumulated +=
static_cast<FwSizeType>(read_size);
286 (
size > (std::numeric_limits<FwSizeType>::max() / 2)) ? std::numeric_limits<FwSizeType>::max() :
size * 2;
297 static_cast<size_t>(
size - accumulated));
300 int errno_store = errno;
302 if (EINTR != errno_store) {
308 accumulated +=
static_cast<FwSizeType>(write_size);
315 int errno_store = errno;
323 return &this->m_handle;
Status size(FwSizeType &size_result) override
get size of currently open file
base implementation of FileHandle
Status preallocate(FwSizeType offset, FwSizeType length) override
pre-allocate file storage
A catch-all for other errors. Have to look in implementation-specific code.
PlatformSizeType FwSizeType
PosixFile()=default
constructor
static const UnsignedSSizeT SSIZE_T_MAX_LIMIT
Open file for writing; writes don't return until data is on disk.
int m_file_descriptor
Posix file descriptor.
PlatformSignedSizeType FwSignedSizeType
posix implementation of Os::File
Status write(const U8 *buffer, FwSizeType &size, WaitType wait) override
read data from this file into supplied buffer bounded by size
static constexpr int ERROR_RETURN_VALUE
static constexpr int INVALID_FILE_DESCRIPTOR
Status read(U8 *buffer, FwSizeType &size, WaitType wait) override
read data from this file into supplied buffer bounded by size
void close() override
close the file, if not opened then do nothing
uint8_t U8
8-bit unsigned integer
PosixFile & operator=(const PosixFile &other)
assignment operator that copies the internal representation
static U32 min(const U32 a, const U32 b)
Operation was successful.
Status seek(FwSignedSizeType offset, SeekType seekType) override
seek the file pointer to the given offset
File::Status errno_to_file_status(int errno_input)
std::make_unsigned< ssize_t >::type UnsignedSSizeT
static const UnsignedOffT OFF_T_MAX_LIMIT
Status flush() override
flush file contents to storage
FileHandle * getHandle() override
returns the raw file handle
Status position(FwSizeType &position_result) override
get file pointer position of the currently open file
std::make_unsigned< off_t >::type UnsignedOffT
Os::FileInterface::Status open(const char *path, Mode mode, OverwriteType overwrite) override
open file with supplied path and mode
Open file for writing and truncates file if it exists, ie same flags as creat()