From 20eee2c469425d94537e6429ea2481760658bf58 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Thu, 11 Aug 2022 09:32:18 +0200 Subject: [PATCH] finished host FS impl --- src/fsfw/filesystem/HasFileSystemIF.h | 25 ++++++----- src/fsfw_hal/host/HostFilesystem.cpp | 62 ++++++++++++++++++++++++--- src/fsfw_hal/host/HostFilesystem.h | 2 +- unittests/mocks/FilesystemMock.cpp | 3 +- unittests/mocks/FilesystemMock.h | 2 +- 5 files changed, 73 insertions(+), 21 deletions(-) diff --git a/src/fsfw/filesystem/HasFileSystemIF.h b/src/fsfw/filesystem/HasFileSystemIF.h index 4fc8a941..a9f2b8ea 100644 --- a/src/fsfw/filesystem/HasFileSystemIF.h +++ b/src/fsfw/filesystem/HasFileSystemIF.h @@ -34,18 +34,23 @@ class HasFileSystemIF { //! [EXPORT] : P1: Can be file system specific error code static constexpr ReturnValue_t GENERIC_FILE_ERROR = MAKE_RETURN_CODE(0); + static constexpr ReturnValue_t GENERIC_DIR_ERROR = MAKE_RETURN_CODE(1); + static constexpr ReturnValue_t GENERIC_RENAME_ERROR = MAKE_RETURN_CODE(3); + //! [EXPORT] : File system is currently busy - static constexpr ReturnValue_t IS_BUSY = MAKE_RETURN_CODE(1); + static constexpr ReturnValue_t IS_BUSY = MAKE_RETURN_CODE(4); //! [EXPORT] : Invalid parameters like file name or repository path - static constexpr ReturnValue_t INVALID_PARAMETERS = MAKE_RETURN_CODE(2); + static constexpr ReturnValue_t INVALID_PARAMETERS = MAKE_RETURN_CODE(5); - static constexpr ReturnValue_t FILE_DOES_NOT_EXIST = MAKE_RETURN_CODE(5); - static constexpr ReturnValue_t FILE_ALREADY_EXISTS = MAKE_RETURN_CODE(6); - static constexpr ReturnValue_t FILE_LOCKED = MAKE_RETURN_CODE(7); + static constexpr ReturnValue_t FILE_DOES_NOT_EXIST = MAKE_RETURN_CODE(7); + static constexpr ReturnValue_t FILE_ALREADY_EXISTS = MAKE_RETURN_CODE(8); + static constexpr ReturnValue_t NOT_A_FILE = MAKE_RETURN_CODE(9); + static constexpr ReturnValue_t FILE_LOCKED = MAKE_RETURN_CODE(10); - static constexpr ReturnValue_t DIRECTORY_DOES_NOT_EXIST = MAKE_RETURN_CODE(10); - static constexpr ReturnValue_t DIRECTORY_ALREADY_EXISTS = MAKE_RETURN_CODE(11); - static constexpr ReturnValue_t DIRECTORY_NOT_EMPTY = MAKE_RETURN_CODE(12); + static constexpr ReturnValue_t DIRECTORY_DOES_NOT_EXIST = MAKE_RETURN_CODE(15); + static constexpr ReturnValue_t DIRECTORY_ALREADY_EXISTS = MAKE_RETURN_CODE(16); + static constexpr ReturnValue_t NOT_A_DIRECTORY = MAKE_RETURN_CODE(17); + static constexpr ReturnValue_t DIRECTORY_NOT_EMPTY = MAKE_RETURN_CODE(18); //! [EXPORT] : P1: Sequence number missing static constexpr ReturnValue_t SEQUENCE_PACKET_MISSING_WRITE = MAKE_RETURN_CODE(15); @@ -139,9 +144,9 @@ class HasFileSystemIF { } virtual ReturnValue_t renameFile(const char* oldPath, char* newPath) { - return renameFile(oldPath, newPath, nullptr); + return rename(oldPath, newPath, nullptr); } - virtual ReturnValue_t renameFile(const char* oldPath, char* newPath, FileSystemArgsIF* args) = 0; + virtual ReturnValue_t rename(const char* oldPath, char* newPath, FileSystemArgsIF* args) = 0; }; #endif /* FSFW_MEMORY_HASFILESYSTEMIF_H_ */ diff --git a/src/fsfw_hal/host/HostFilesystem.cpp b/src/fsfw_hal/host/HostFilesystem.cpp index 879b9377..c4a00baf 100644 --- a/src/fsfw_hal/host/HostFilesystem.cpp +++ b/src/fsfw_hal/host/HostFilesystem.cpp @@ -80,20 +80,68 @@ ReturnValue_t HostFilesystem::removeFile(const char *path_, FileSystemArgsIF *ar } return HasFileSystemIF::GENERIC_FILE_ERROR; } + ReturnValue_t HostFilesystem::createDirectory(FilesystemParams params, bool createParentDirs) { if (params.path == nullptr) { return HasReturnvaluesIF::RETURN_FAILED; } path dirPath(params.path); - if (createParentDirs) { - create_directories(dirPath, errorCode); + + if (exists(dirPath)) { + return HasFileSystemIF::DIRECTORY_ALREADY_EXISTS; } - return 0; + if (is_regular_file(dirPath)) { + return HasFileSystemIF::NOT_A_DIRECTORY; + } + if (createParentDirs) { + if (create_directories(dirPath, errorCode)) { + return HasReturnvaluesIF::RETURN_OK; + } + return HasFileSystemIF::GENERIC_DIR_ERROR; + } + if (create_directory(dirPath, errorCode)) { + return HasReturnvaluesIF::RETURN_OK; + } + return HasFileSystemIF::GENERIC_DIR_ERROR; } + ReturnValue_t HostFilesystem::removeDirectory(FilesystemParams params, bool deleteRecurively) { - return 0; + if (params.path == nullptr) { + return HasReturnvaluesIF::RETURN_FAILED; + } + path dirPath(params.path); + if (not exists(dirPath)) { + return HasFileSystemIF::DIRECTORY_DOES_NOT_EXIST; + } + if (is_regular_file(dirPath)) { + return HasFileSystemIF::NOT_A_DIRECTORY; + } + if (deleteRecurively) { + if (remove_all(dirPath, errorCode)) { + return HasReturnvaluesIF::RETURN_OK; + } + } else { + if (remove(dirPath, errorCode)) { + return HasReturnvaluesIF::RETURN_OK; + } + } + // Error handling + if (errorCode == std::errc::directory_not_empty) { + return HasFileSystemIF::DIRECTORY_NOT_EMPTY; + } + return HasFileSystemIF::GENERIC_DIR_ERROR; } -ReturnValue_t HostFilesystem::renameFile(const char *oldPath, char *newPath, - FileSystemArgsIF *args) { - return 0; + +ReturnValue_t HostFilesystem::rename(const char *oldPath_, char *newPath_, FileSystemArgsIF *args) { + if (oldPath_ == nullptr or newPath_ == nullptr) { + return HasReturnvaluesIF::RETURN_FAILED; + } + path oldPath(oldPath_); + path newPath(newPath_); + errorCode.clear(); + std::filesystem::rename(oldPath, newPath, errorCode); + if (errorCode) { + return HasFileSystemIF::GENERIC_RENAME_ERROR; + } + return HasReturnvaluesIF::RETURN_OK; } diff --git a/src/fsfw_hal/host/HostFilesystem.h b/src/fsfw_hal/host/HostFilesystem.h index 2dd8f2f5..01e37f08 100644 --- a/src/fsfw_hal/host/HostFilesystem.h +++ b/src/fsfw_hal/host/HostFilesystem.h @@ -15,7 +15,7 @@ class HostFilesystem : public HasFileSystemIF { ReturnValue_t removeFile(const char *path, FileSystemArgsIF *args) override; ReturnValue_t createDirectory(FilesystemParams params, bool createParentDirs) override; ReturnValue_t removeDirectory(FilesystemParams params, bool deleteRecurively) override; - ReturnValue_t renameFile(const char *oldPath, char *newPath, FileSystemArgsIF *args) override; + ReturnValue_t rename(const char *oldPath, char *newPath, FileSystemArgsIF *args) override; std::error_code errorCode; diff --git a/unittests/mocks/FilesystemMock.cpp b/unittests/mocks/FilesystemMock.cpp index e4efd086..b52c744f 100644 --- a/unittests/mocks/FilesystemMock.cpp +++ b/unittests/mocks/FilesystemMock.cpp @@ -64,8 +64,7 @@ ReturnValue_t FilesystemMock::removeDirectory(FilesystemParams params, bool dele return HasReturnvaluesIF::RETURN_OK; } -ReturnValue_t FilesystemMock::renameFile(const char *oldPath, char *newPath, - FileSystemArgsIF *args) { +ReturnValue_t FilesystemMock::rename(const char *oldPath, char *newPath, FileSystemArgsIF *args) { renameQueue.push(RenameInfo(oldPath, newPath)); return HasReturnvaluesIF::RETURN_OK; } diff --git a/unittests/mocks/FilesystemMock.h b/unittests/mocks/FilesystemMock.h index b02928c7..a1af45f0 100644 --- a/unittests/mocks/FilesystemMock.h +++ b/unittests/mocks/FilesystemMock.h @@ -60,7 +60,7 @@ class FilesystemMock : public HasFileSystemIF { ReturnValue_t removeFile(const char *path, FileSystemArgsIF *args) override; ReturnValue_t createDirectory(FilesystemParams params, bool createParentDirs) override; ReturnValue_t removeDirectory(FilesystemParams params, bool deleteRecurively) override; - ReturnValue_t renameFile(const char *oldPath, char *newPath, FileSystemArgsIF *args) override; + ReturnValue_t rename(const char *oldPath, char *newPath, FileSystemArgsIF *args) override; void reset();