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),
45 "FwSizeType is not large enough to store values of type size_t");
48 static_assert(std::numeric_limits<FwSignedSizeType>::max() >= std::numeric_limits<off_t>::max(),
49 "Maximum value of FwSignedSizeType less than the maximum value of off_t. Configure a larger type.");
50 static_assert(std::numeric_limits<FwSizeType>::max() >=
OFF_T_MAX_LIMIT,
51 "Maximum value of FwSizeType less than the maximum value of off_t. Configure a larger type.");
52 static_assert(std::numeric_limits<FwSignedSizeType>::max() >= std::numeric_limits<ssize_t>::max(),
53 "Maximum value of FwSignedSizeType less than the maximum value of ssize_t. Configure a larger type.");
55 "Minimum value of FwSignedSizeType larger than the minimum value of off_t. Configure a larger type.");
57 "Minimum value of FwSizeType larger than the minimum value of ssize_t. Configure a larger type.");
58 static_assert(std::numeric_limits<FwSizeType>::max() >= std::numeric_limits<size_t>::max(),
59 "Maximum value of FwSizeType less than the maximum value of size_t. Configure a larger type.");
77 PlatformIntType mode_flags = 0;
79 switch (requested_mode) {
81 mode_flags = O_RDONLY;
84 mode_flags = O_WRONLY | O_CREAT;
87 mode_flags = O_WRONLY | O_CREAT | O_SYNC;
91 O_WRONLY | O_CREAT | O_TRUNC | ((overwrite == PosixFile::OverwriteType::OVERWRITE) ? 0 : O_EXCL);
94 mode_flags = O_WRONLY | O_CREAT | O_APPEND;
102 PlatformIntType errno_store = errno;
127 PlatformIntType errno_store = errno;
131 (void) ::lseek(this->m_handle.
m_file_descriptor, static_cast<off_t>(current_position), SEEK_SET);
132 size_result =
static_cast<FwSizeType>(end_of_file);
142 PlatformIntType errno_store = errno;
146 position_result =
static_cast<FwSizeType>(actual);
155 (std::numeric_limits<off_t>::max() - length) < offset) {
156 status = Os::File::Status::BAD_SIZE;
162 #if _POSIX_C_SOURCE >= 200112L 164 PlatformIntType errno_status = ::posix_fallocate(this->m_handle.
m_file_descriptor, static_cast<off_t>(offset), static_cast<off_t>(length));
169 if (Os::File::Status::NOT_SUPPORTED == status) {
172 status = this->
size(file_size);
176 status = this->
position(file_position);
178 if (file_position > static_cast<FwSizeType>(std::numeric_limits<FwSignedSizeType>::max()) ||
179 file_size > static_cast<FwSizeType>(std::numeric_limits<FwSignedSizeType>::max())) {
180 status = Os::File::Status::BAD_SIZE;
184 const FwSizeType write_length = (offset + length) - file_size;
185 status = this->
seek(static_cast<FwSignedSizeType>(file_size), PosixFile::SeekType::ABSOLUTE);
188 for (
FwSizeType i = 0; i < write_length; i++) {
190 status = this->
write(reinterpret_cast<const U8*>(
"\0"), write_size,
191 PosixFile::WaitType::NO_WAIT);
198 status = this->
seek(static_cast<FwSignedSizeType>(file_position), PosixFile::SeekType::ABSOLUTE);
209 if (offset > std::numeric_limits<off_t>::max()) {
213 ::lseek(this->m_handle.
m_file_descriptor, static_cast<off_t>(offset), (seekType == SeekType::ABSOLUTE) ? SEEK_SET : SEEK_CUR);
214 PlatformIntType errno_store = errno;
217 }
else if ((seekType == SeekType::ABSOLUTE) && (actual != offset)) {
227 PlatformIntType errno_store = errno;
237 const FwSizeType maximum = (
size > (std::numeric_limits<FwSizeType>::max() / 2))
238 ? std::numeric_limits<FwSizeType>::max()
249 static_cast<size_t>(
size - accumulated));
252 PlatformIntType errno_store = errno;
254 if (EINTR != errno_store) {
261 else if (read_size == 0) {
264 accumulated +=
static_cast<FwSizeType>(read_size);
278 const FwSizeType maximum = (
size > (std::numeric_limits<FwSizeType>::max() / 2))
279 ? std::numeric_limits<FwSizeType>::max()
291 static_cast<size_t>(
size - accumulated));
294 PlatformIntType errno_store = errno;
296 if (EINTR != errno_store) {
302 accumulated +=
static_cast<FwSizeType>(write_size);
309 PlatformIntType errno_store = errno;
317 return &this->m_handle;
static constexpr PlatformIntType ERROR_RETURN_VALUE
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.
PlatformSignedSizeType FwSignedSizeType
static constexpr PlatformIntType INVALID_FILE_DESCRIPTOR
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
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)
File::Status errno_to_file_status(PlatformIntType errno_input)
Operation was successful.
Status seek(FwSignedSizeType offset, SeekType seekType) override
seek the file pointer to the given offset
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
PlatformIntType m_file_descriptor
Posix file descriptor.
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()