F´ Flight Software - C/C++ Documentation  devel
A framework for building embedded system applications to NASA flight quality standards.
File.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title Os/Posix/File.cpp
3 // \brief posix implementation for Os::File
4 // ======================================================================
5 #include <cerrno>
6 #include <fcntl.h>
7 #include <unistd.h>
8 #include <limits>
9 
10 #include <Os/File.hpp>
11 #include <Os/Posix/File.hpp>
12 #include <Fw/Types/Assert.hpp>
13 #include <Os/Posix/error.hpp>
14 
15 namespace Os {
16 namespace Posix {
17 namespace File {
18 
19 // Sets up the default file permission as user read + user write
20 // Some posix systems (e.g. Darwin) use the older S_IREAD and S_IWRITE flags while other systems (e.g. Linux) use the
21 // newer S_IRUSR and S_IWUSR flags, and some don't support these flags at all. Hence, we look if flags are defined then
22 // set USER_FLAGS to be the set of flags supported or 0 in the case neither is defined.
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)
27 #else
28 #define USER_FLAGS (0)
29 #endif
30 
31 // Ensure size of FwSizeType is large enough to fit eh necessary range
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");
34 
35 // Now check ranges of FwSizeType
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.");
44 
47  // Must properly duplicate the file handle
48  this->m_handle.m_file_descriptor = ::dup(other.m_handle.m_file_descriptor);
49 }
50 
52  if (this != &other) {
53  this->m_handle.m_file_descriptor = ::dup(other.m_handle.m_file_descriptor);
54  }
55  return *this;
56 }
57 
58 PosixFile::Status PosixFile::open(const char* filepath, PosixFile::Mode requested_mode, PosixFile::OverwriteType overwrite) {
59  PlatformIntType mode_flags = 0;
60  Status status = OP_OK;
61  switch (requested_mode) {
62  case OPEN_READ:
63  mode_flags = O_RDONLY;
64  break;
65  case OPEN_WRITE:
66  mode_flags = O_WRONLY | O_CREAT;
67  break;
68  case OPEN_SYNC_WRITE:
69  mode_flags = O_WRONLY | O_CREAT | O_SYNC;
70  break;
71  case OPEN_CREATE:
72  mode_flags = O_WRONLY | O_CREAT | O_TRUNC | ((overwrite == PosixFile::OverwriteType::OVERWRITE) ? 0 : O_EXCL);
73  break;
74  case OPEN_APPEND:
75  mode_flags = O_WRONLY | O_CREAT | O_APPEND;
76  break;
77  default:
78  FW_ASSERT(0, requested_mode);
79  break;
80  }
81  PlatformIntType descriptor = ::open(filepath, mode_flags, USER_FLAGS);
82  if (PosixFileHandle::INVALID_FILE_DESCRIPTOR == descriptor) {
83  PlatformIntType errno_store = errno;
84  status = Os::Posix::errno_to_file_status(errno_store);
85  }
86  this->m_handle.m_file_descriptor = descriptor;
87  return status;
88 }
89 
91  // Only close file handles that are not open
93  (void)::close(this->m_handle.m_file_descriptor);
95  }
96 }
97 
99  FwSignedSizeType current_position = 0;
100  Status status = this->position(current_position);
101  size_result = 0;
102  if (Os::File::Status::OP_OK == status) {
103  // Seek to the end of the file to determine size
104  off_t end_of_file = ::lseek(this->m_handle.m_file_descriptor, 0, SEEK_END);
105  if (PosixFileHandle::ERROR_RETURN_VALUE == end_of_file) {
106  PlatformIntType errno_store = errno;
107  status = Os::Posix::errno_to_file_status(errno_store);
108  } else {
109  // Return the file pointer back to the original position
110  off_t original = ::lseek(this->m_handle.m_file_descriptor, current_position, SEEK_SET);
111  if ((PosixFileHandle::ERROR_RETURN_VALUE == original) || (current_position != original)) {
112  PlatformIntType errno_store = errno;
113  status = Os::Posix::errno_to_file_status(errno_store);
114  }
115  }
116  size_result = end_of_file;
117  }
118  return status;
119 }
120 
122  Status status = OP_OK;
123  position_result = 0;
124  off_t actual = ::lseek(this->m_handle.m_file_descriptor, 0, SEEK_CUR);
125  if (PosixFileHandle::ERROR_RETURN_VALUE == actual) {
126  PlatformIntType errno_store = errno;
127  status = Os::Posix::errno_to_file_status(errno_store);
128  }
129  position_result = static_cast<FwSignedSizeType>(actual);
130  return status;
131 }
132 
134  PosixFile::Status status = Os::File::Status::NOT_SUPPORTED;
135  // posix_fallocate is only available with the posix C-API post version 200112L, however; it is not guaranteed that
136  // this call is properly implemented. This code starts with a status of "NOT_SUPPORTED". When the standard is met
137  // an attempt will be made to called posix_fallocate, and should that still return NOT_SUPPORTED then fallback
138  // code is engaged to synthesize this behavior.
139 #if _POSIX_C_SOURCE >= 200112L
140  PlatformIntType errno_status = ::posix_fallocate(this->m_handle.m_file_descriptor, offset, length);
141  status = Os::Posix::errno_to_file_status(errno_status);
142 #endif
143  // When the operation is not supported or posix-API is not sufficient, fallback to a slower algorithm
144  if (Os::File::Status::NOT_SUPPORTED == status) {
145  // Calculate size
146  FwSignedSizeType file_size = 0;
147  status = this->size(file_size);
148  if (Os::File::Status::OP_OK == status) {
149  // Calculate current position
150  FwSignedSizeType file_position = 0;
151  status = this->position(file_position);
152  if (Os::File::Status::OP_OK == status) {
153  // Check for integer overflow
154  if ((std::numeric_limits<FwSignedSizeType>::max() - offset - length) < 0) {
155  status = PosixFile::NO_SPACE;
156  } else if (file_size < (offset + length)) {
157  const FwSignedSizeType write_length = (offset + length) - file_size;
158  status = this->seek(file_size, PosixFile::SeekType::ABSOLUTE);
159  if (Os::File::Status::OP_OK == status) {
160  // Fill in zeros past size of file to ensure compatibility with fallocate
161  for (FwSignedSizeType i = 0; i < write_length; i++) {
162  FwSignedSizeType write_size = 1;
163  status = this->write(reinterpret_cast<const U8*>("\0"), write_size, PosixFile::WaitType::NO_WAIT);
164  if (Status::OP_OK != status || write_size != 1) {
165  break;
166  }
167  }
168  // Return to original position
169  if (Os::File::Status::OP_OK == status) {
170  status = this->seek(file_position, PosixFile::SeekType::ABSOLUTE);
171  }
172  }
173  }
174  }
175  }
176  }
177  return status;
178 }
179 
181  Status status = OP_OK;
182  off_t actual = ::lseek(this->m_handle.m_file_descriptor, offset, (seekType == SeekType::ABSOLUTE) ? SEEK_SET : SEEK_CUR);
183  PlatformIntType errno_store = errno;
184  if (actual == PosixFileHandle::ERROR_RETURN_VALUE) {
185  status = Os::Posix::errno_to_file_status(errno_store);
186  } else if ((seekType == SeekType::ABSOLUTE) && (actual != offset)) {
188  }
189  return status;
190 }
191 
193  PosixFile::Status status = OP_OK;
194  if (PosixFileHandle::ERROR_RETURN_VALUE == ::fsync(this->m_handle.m_file_descriptor)) {
195  PlatformIntType errno_store = errno;
196  status = Os::Posix::errno_to_file_status(errno_store);
197  }
198  return status;
199 }
200 
202  Status status = OP_OK;
203  FwSignedSizeType accumulated = 0;
204  // Loop up to 2 times for each by, bounded to prevent overflow
205  const FwSignedSizeType maximum = (size > (std::numeric_limits<FwSignedSizeType>::max()/2)) ? std::numeric_limits<FwSignedSizeType>::max() : size * 2;
206 
207  for (FwSignedSizeType i = 0; i < maximum && accumulated < size; i++) {
208  // char* for some posix implementations
209  ssize_t read_size = ::read(
210  this->m_handle.m_file_descriptor,
211  reinterpret_cast<CHAR*>(&buffer[accumulated]),
212  static_cast<size_t>(size - accumulated));
213  // Non-interrupt error
214  if (PosixFileHandle::ERROR_RETURN_VALUE == read_size) {
215  PlatformIntType errno_store = errno;
216  // Interrupted w/o read, try again
217  if (EINTR != errno_store) {
218  continue;
219  }
220  status = Os::Posix::errno_to_file_status(errno_store);
221  break;
222  }
223  // End-of-file
224  else if (read_size == 0) {
225  break;
226  }
227  accumulated += read_size;
228  // Stop looping when we had a good read and are not waiting
229  if (not wait) {
230  break;
231  }
232  }
233  size = accumulated;
234  return status;
235 }
236 
238  Status status = OP_OK;
239  FwSignedSizeType accumulated = 0;
240  // Loop up to 2 times for each by, bounded to prevent overflow
241  const FwSignedSizeType maximum = (size > (std::numeric_limits<FwSignedSizeType>::max()/2)) ? std::numeric_limits<FwSignedSizeType>::max() : size * 2;
242 
243  for (FwSignedSizeType i = 0; i < maximum && accumulated < size; i++) {
244  // char* for some posix implementations
245  ssize_t write_size = ::write(this->m_handle.m_file_descriptor, reinterpret_cast<const CHAR*>(&buffer[accumulated]), static_cast<size_t>(size - accumulated));
246  // Non-interrupt error
247  if (PosixFileHandle::ERROR_RETURN_VALUE == write_size) {
248  PlatformIntType errno_store = errno;
249  // Interrupted w/o read, try again
250  if (EINTR != errno_store) {
251  continue;
252  }
253  status = Os::Posix::errno_to_file_status(errno_store);
254  break;
255  }
256  accumulated += write_size;
257  }
258  size = accumulated;
259  // When waiting, sync to disk
260  if (wait) {
261  PlatformIntType fsync_return = ::fsync(this->m_handle.m_file_descriptor);
262  if (PosixFileHandle::ERROR_RETURN_VALUE == fsync_return) {
263  PlatformIntType errno_store = errno;
264  status = Os::Posix::errno_to_file_status(errno_store);
265  }
266  }
267  return status;
268 }
269 
271  return &this->m_handle;
272 }
273 
274 } // namespace File
275 } // namespace Posix
276 } // namespace Os
277 
#define FW_ASSERT(...)
Definition: Assert.hpp:14
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:26
char CHAR
Definition: BasicTypes.h:28
static U32 min(const U32 a, const U32 b)
Definition: Checksum.cpp:16
int PlatformIntType
DefaultTypes.hpp provides fallback defaults for the platform types.
PlatformSignedSizeType FwSignedSizeType
Definition: FpConfig.h:25
#define USER_FLAGS
Definition: File.cpp:28
@ NO_SPACE
No space left.
Definition: File.hpp:32
@ OP_OK
Operation was successful.
Definition: File.hpp:30
@ OPEN_WRITE
Open file for writing.
Definition: File.hpp:23
@ OPEN_CREATE
Open file for writing and truncates file if it exists, ie same flags as creat()
Definition: File.hpp:22
@ OPEN_READ
Open file for reading.
Definition: File.hpp:21
@ OPEN_APPEND
Open file for appending.
Definition: File.hpp:25
@ OPEN_SYNC_WRITE
Open file for writing; writes don't return until data is on disk.
Definition: File.hpp:24
posix implementation of Os::File
Definition: File.hpp:29
Status position(FwSignedSizeType &position_result) override
get file pointer position of the currently open file
Definition: File.cpp:121
void close() override
close the file, if not opened then do nothing
Definition: File.cpp:90
Status seek(FwSignedSizeType offset, SeekType seekType) override
seek the file pointer to the given offset
Definition: File.cpp:180
Status preallocate(FwSignedSizeType offset, FwSignedSizeType length) override
pre-allocate file storage
Definition: File.cpp:133
Status read(U8 *buffer, FwSignedSizeType &size, WaitType wait) override
read data from this file into supplied buffer bounded by size
Definition: File.cpp:201
Status write(const U8 *buffer, FwSignedSizeType &size, WaitType wait) override
read data from this file into supplied buffer bounded by size
Definition: File.cpp:237
FileHandle * getHandle() override
returns the raw file handle
Definition: File.cpp:270
PosixFile()=default
constructor
Status size(FwSignedSizeType &size_result) override
get size of currently open file
Definition: File.cpp:98
Status flush() override
flush file contents to storage
Definition: File.cpp:192
PosixFile & operator=(const PosixFile &other)
assignment operator that copies the internal representation
Definition: File.cpp:51
Os::FileInterface::Status open(const char *path, Mode mode, OverwriteType overwrite) override
open file with supplied path and mode
Definition: File.cpp:58
@ OP_OK
Operation was successful.
Definition: FileSystem.hpp:15
@ OTHER_ERROR
other OS-specific error
Definition: FileSystem.hpp:25
File::Status errno_to_file_status(PlatformIntType errno_input)
Definition: error.cpp:11
base implementation of FileHandle
Definition: File.hpp:14
PlatformIntType m_file_descriptor
Posix file descriptor.
Definition: File.hpp:20
static constexpr PlatformIntType INVALID_FILE_DESCRIPTOR
Definition: File.hpp:16
static constexpr PlatformIntType ERROR_RETURN_VALUE
Definition: File.hpp:17