10FileSystem::FileSystem() : m_handle_storage(), m_delegate(*FileSystemInterface::getDelegate(m_handle_storage)) {
11 FW_ASSERT(&this->m_delegate ==
reinterpret_cast<FileSystemInterface*
>(&this->m_handle_storage[0]));
14FileSystem::~FileSystem() {
16 m_delegate.~FileSystemInterface();
21 return this->m_delegate.getHandle();
27 return this->m_delegate._removeDirectory(path);
33 return this->m_delegate._removeFile(path);
40 return this->m_delegate._rename(sourcePath, destPath);
47 return this->m_delegate._getWorkingDirectory(path, bufferSize);
53 return this->m_delegate._changeWorkingDirectory(path);
59 return this->m_delegate._getFreeSpace(path, totalBytes, freeBytes);
62void FileSystem::init() {
64 (void) FileSystem::getSingleton();
82 return FileSystem::getSingleton()._removeFile(path);
86 return FileSystem::getSingleton()._rename(sourcePath, destPath);
90 return FileSystem::getSingleton()._getWorkingDirectory(path, bufferSize);
94 return FileSystem::getSingleton()._changeWorkingDirectory(path);
98 return FileSystem::getSingleton()._getFreeSpace(path, totalBytes, freeBytes);
108 Status status = Status::OP_OK;
111 Directory::OpenMode mode = errorIfAlreadyExists ? Directory::OpenMode::CREATE_EXCLUSIVE : Directory::OpenMode::CREATE_IF_MISSING;
114 if (dirStatus != Directory::OP_OK) {
115 return FileSystem::handleDirectoryError(dirStatus);
122 Status status = Status::OP_OK;
126 if (file_status != File::OP_OK) {
127 status = FileSystem::handleFileError(file_status);
137 if (file_status == File::OP_OK) {
138 return PathType::FILE;
143 if (dir_status == Directory::Status::OP_OK) {
144 return PathType::DIRECTORY;
146 return PathType::NOT_EXIST;
149bool FileSystem::exists(
const char* path) {
150 return FileSystem::getPathType(path) != PathType::NOT_EXIST;
160 return FileSystem::handleFileError(fileStatus);
164 return FileSystem::handleFileError(fileStatus);
169 if (fs_status != FileSystem::Status::OP_OK) {
173 fs_status = FileSystem::copyFileData(source, destination, sourceFileSize);
178FileSystem::Status FileSystem::appendFile(
const char* sourcePath,
const char* destPath,
bool createMissingDest) {
183 if (not createMissingDest and not FileSystem::exists(destPath)) {
184 return Status::DOESNT_EXIST;
189 return FileSystem::handleFileError(fileStatus);
193 return FileSystem::handleFileError(fileStatus);
199 fs_status = FileSystem::getFileSize(sourcePath, sourceFileSize);
200 if (fs_status != FileSystem::Status::OP_OK) {
204 fs_status = FileSystem::copyFileData(source, destination, sourceFileSize);
210 Status status = Status::OP_OK;
213 status = FileSystem::rename(source, destination);
216 if (status == Status::EXDEV_ERROR) {
217 status = FileSystem::copyFile(source, destination);
218 if (status != Status::OP_OK) {
221 status = FileSystem::removeFile(source);
230 if (status != File::Status::OP_OK) {
231 return FileSystem::handleFileError(status);
233 status = file.
size(size);
234 if (status != File::Status::OP_OK) {
235 return FileSystem::handleFileError(status);
237 return FileSystem::OP_OK;
248 switch (fileStatus) {
250 status = FileSystem::NO_SPACE;
252 case File::NO_PERMISSION:
253 status = FileSystem::NO_PERMISSION;
255 case File::DOESNT_EXIST:
256 status = FileSystem::DOESNT_EXIST;
259 status = FileSystem::OTHER_ERROR;
264FileSystem::Status FileSystem::handleDirectoryError(Directory::Status dirStatus) {
265 FileSystem::Status status = FileSystem::OTHER_ERROR;
268 case Directory::DOESNT_EXIST:
269 status = FileSystem::DOESNT_EXIST;
271 case Directory::NO_PERMISSION:
272 status = FileSystem::NO_PERMISSION;
274 case Directory::ALREADY_EXISTS:
275 status = FileSystem::ALREADY_EXISTS;
277 case Directory::NOT_SUPPORTED:
278 status = FileSystem::NOT_SUPPORTED;
281 status = FileSystem::OTHER_ERROR;
286FileSystem::Status FileSystem::copyFileData(File& source, File& destination,
FwSignedSizeType size) {
287 static_assert(FILE_SYSTEM_FILE_CHUNK_SIZE != 0,
"FILE_SYSTEM_FILE_CHUNK_SIZE must be >0");
288 U8 fileBuffer[FILE_SYSTEM_FILE_CHUNK_SIZE];
289 File::Status file_status;
295 for (copiedSize = 0; copiedSize < size; copiedSize += chunkSize) {
298 chunkSize =
FW_MIN(FILE_SYSTEM_FILE_CHUNK_SIZE, size - copiedSize);
300 if (file_status != File::OP_OK) {
301 return FileSystem::handleFileError(file_status);
304 if (file_status != File::OP_OK) {
305 return FileSystem::handleFileError(file_status);
309 return FileSystem::OP_OK;
#define FW_MIN(a, b)
MIN macro.
uint8_t U8
8-bit unsigned integer
PlatformSignedSizeType FwSignedSizeType
PlatformSizeType FwSizeType
Status open(const char *path, OpenMode mode) override
Open or create a directory.
void close() override
Close directory.
@ READ
Error if directory doesn't exist.
Status size(FwSignedSizeType &size_result) override
get size of currently open file
void close() override
close the file, if not opened then do nothing
Os::FileInterface::Status open(const char *path, Mode mode)
open file with supplied path and mode
@ WAIT
Do wait for read/write operation to finish.
@ OP_OK
Operation was successful.
@ OPEN_WRITE
Open file for writing.
@ OPEN_READ
Open file for reading.
@ OPEN_APPEND
Open file for appending.
Status _removeDirectory(const char *path) override
Remove a directory at the specified path.