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) 41 "FwSignedSizeType is not large enough to store values of type off_t");
43 "FwSignedSizeType is not large enough to store values of type ssize_t");
44 static_assert(
sizeof(
FwSizeType) >=
sizeof(
size_t),
"FwSizeType is not large enough to store values of type size_t");
47 static_assert(std::numeric_limits<FwSignedSizeType>::max() >= std::numeric_limits<off_t>::max(),
48 "Maximum value of FwSignedSizeType less than the maximum value of off_t. Configure a larger type.");
49 static_assert(std::numeric_limits<FwSizeType>::max() >=
OFF_T_MAX_LIMIT,
50 "Maximum value of FwSizeType less than the maximum value of off_t. Configure a larger type.");
51 static_assert(std::numeric_limits<FwSignedSizeType>::max() >= std::numeric_limits<ssize_t>::max(),
52 "Maximum value of FwSignedSizeType less than the maximum value of ssize_t. Configure a larger type.");
54 "Minimum value of FwSignedSizeType larger than the minimum value of off_t. Configure a larger type.");
56 "Minimum value of FwSizeType larger than the minimum value of ssize_t. Configure a larger type.");
57 static_assert(std::numeric_limits<FwSizeType>::max() >= std::numeric_limits<size_t>::max(),
58 "Maximum value of FwSizeType less than the maximum value of size_t. Configure a larger type.");
78 switch (requested_mode) {
80 mode_flags = O_RDONLY;
83 mode_flags = O_WRONLY | O_CREAT;
86 mode_flags = O_WRONLY | O_CREAT | O_SYNC;
90 O_WRONLY | O_CREAT | O_TRUNC | ((overwrite == PosixFile::OverwriteType::OVERWRITE) ? 0 : O_EXCL);
93 mode_flags = O_WRONLY | O_CREAT | O_APPEND;
101 int errno_store = errno;
126 int errno_store = errno;
130 (void)::lseek(this->m_handle.
m_file_descriptor, static_cast<off_t>(current_position), SEEK_SET);
131 size_result =
static_cast<FwSizeType>(end_of_file);
141 int errno_store = errno;
145 position_result =
static_cast<FwSizeType>(actual);
153 (std::numeric_limits<off_t>::max() - length) < offset) {
154 status = Os::File::Status::BAD_SIZE;
160 #if _POSIX_C_SOURCE >= 200112L 163 ::posix_fallocate(this->m_handle.
m_file_descriptor, static_cast<off_t>(offset), static_cast<off_t>(length));
168 if (Os::File::Status::NOT_SUPPORTED == status) {
171 status = this->
size(file_size);
175 status = this->
position(file_position);
177 if (file_position > static_cast<FwSizeType>(std::numeric_limits<FwSignedSizeType>::max()) ||
178 file_size > static_cast<FwSizeType>(std::numeric_limits<FwSignedSizeType>::max())) {
179 status = Os::File::Status::BAD_SIZE;
183 const FwSizeType write_length = (offset + length) - file_size;
184 status = this->
seek(static_cast<FwSignedSizeType>(file_size), PosixFile::SeekType::ABSOLUTE);
187 for (
FwSizeType i = 0; i < write_length; i++) {
190 this->
write(reinterpret_cast<const U8*>(
"\0"), write_size, PosixFile::WaitType::NO_WAIT);
198 this->
seek(static_cast<FwSignedSizeType>(file_position), PosixFile::SeekType::ABSOLUTE);
209 if (offset > std::numeric_limits<off_t>::max()) {
212 off_t actual = ::lseek(this->m_handle.
m_file_descriptor, static_cast<off_t>(offset),
213 (seekType == SeekType::ABSOLUTE) ? SEEK_SET : SEEK_CUR);
214 int errno_store = errno;
217 }
else if ((seekType == SeekType::ABSOLUTE) && (actual != offset)) {
227 int errno_store = errno;
238 (
size > (std::numeric_limits<FwSizeType>::max() / 2)) ? std::numeric_limits<FwSizeType>::max() :
size * 2;
248 static_cast<size_t>(
size - accumulated));
251 int errno_store = errno;
253 if (EINTR != errno_store) {
260 else if (read_size == 0) {
263 accumulated +=
static_cast<FwSizeType>(read_size);
278 (
size > (std::numeric_limits<FwSizeType>::max() / 2)) ? std::numeric_limits<FwSizeType>::max() :
size * 2;
289 static_cast<size_t>(
size - accumulated));
292 int errno_store = errno;
294 if (EINTR != errno_store) {
300 accumulated +=
static_cast<FwSizeType>(write_size);
307 int errno_store = errno;
315 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()