From 81ac5a3b2cf6a396b9443994bc4a2f2821b6bb49 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Mon, 19 Jul 2021 19:21:34 +0200 Subject: [PATCH] tested dir remove functionality --- bsp_q7s/boardtest/Q7STestTask.cpp | 92 +++++++++++++++++++++++++--- bsp_q7s/boardtest/Q7STestTask.h | 7 ++- bsp_q7s/memory/FileSystemHandler.cpp | 24 ++++++-- bsp_q7s/memory/FileSystemHandler.h | 2 +- fsfw | 2 +- 5 files changed, 109 insertions(+), 18 deletions(-) diff --git a/bsp_q7s/boardtest/Q7STestTask.cpp b/bsp_q7s/boardtest/Q7STestTask.cpp index 9e21cc91..4e763641 100644 --- a/bsp_q7s/boardtest/Q7STestTask.cpp +++ b/bsp_q7s/boardtest/Q7STestTask.cpp @@ -24,7 +24,7 @@ ReturnValue_t Q7STestTask::performOneShotAction() { //testScratchApi(); //testJsonLibDirect(); //testDummyParams(); - FsOpCodes opCode = FsOpCodes::REMOVE_TMP_FILE; + FsOpCodes opCode = FsOpCodes::ATTEMPT_DIR_REMOVAL_NON_EMPTY; testFileSystemHandlerDirect(opCode); return TestTask::performOneShotAction(); } @@ -138,7 +138,31 @@ void Q7STestTask::testFileSystemHandlerDirect(FsOpCodes opCode) { sif::warning << "Q7STestTask::testFileSystemHandlerDirect: No FS handler running.." << std::endl; } - FileSystemHandler::FsCommandCfg cfg; + FileSystemHandler::FsCommandCfg cfg = {}; + ReturnValue_t result = HasReturnvaluesIF::RETURN_OK; + + // Lambda for common code + auto createNonEmptyTmpDir = [&]() { + if(not std::filesystem::exists("/tmp/test")) { + result = fsHandler->createDirectory("/tmp/test", &cfg); + if(result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + } + // Creating sample files + sif::info << "Creating sample files in directory" << std::endl; + result = fsHandler->createFile("/tmp/test", "test1.txt", nullptr, 0, &cfg); + if(result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + result = fsHandler->createFile("/tmp/test", "test2.txt", nullptr, 0, &cfg); + if(result != HasReturnvaluesIF::RETURN_OK) { + return result; + } + return result; + }; + + switch(opCode) { case(FsOpCodes::CREATE_EMPTY_FILE_IN_TMP): { // No mount prefix, cause file is created in tmp @@ -157,20 +181,74 @@ void Q7STestTask::testFileSystemHandlerDirect(FsOpCodes opCode) { sif::info << "Creating sample file /tmp/test.txt to delete" << std::endl; fsHandler->createFile("/tmp", "test.txt", nullptr, 0, &cfg); } - ReturnValue_t result = fsHandler->deleteFile("/tmp", "test.txt", &cfg); + result = fsHandler->removeFile("/tmp", "test.txt", &cfg); if(result == HasReturnvaluesIF::RETURN_OK) { - sif::info << "File deleted successfully" << std::endl; + sif::info << "File removed successfully" << std::endl; } else { - sif::info << "File deletion failed!" << std::endl; + sif::warning << "File removal failed!" << std::endl; } break; } - case(FsOpCodes::CREATE_DIR): { + case(FsOpCodes::CREATE_DIR_IN_TMP): { + // No mount prefix, cause file is created in tmp + cfg.useMountPrefix = false; + sif::info << "Creating empty file in /tmp folder" << std::endl; + // Do not delete file, user can check existence in shell + ReturnValue_t result = fsHandler->createDirectory("/tmp/test", &cfg); + if(result == HasReturnvaluesIF::RETURN_OK) { + sif::info << "Directory created successfully" << std::endl; + } + else { + sif::warning << "Directory creation failed!" << std::endl; + } break; } - case(FsOpCodes::REMOVE_DIR): { + case(FsOpCodes::REMOVE_EMPTY_DIR_IN_TMP): { + // No mount prefix, cause file is created in tmp + cfg.useMountPrefix = false; + if(not std::filesystem::exists("/tmp/test")) { + result = fsHandler->createDirectory("/tmp/test", &cfg); + } + else { + // Delete any leftover files to regular dir removal works + std::remove("/tmp/test/*"); + } + result = fsHandler->removeDirectory("/tmp/test", false, &cfg); + if(result == HasReturnvaluesIF::RETURN_OK) { + sif::info << "Directory removed successfully" << std::endl; + } + else { + sif::warning << "Directory removal failed!" << std::endl; + } break; } + case(FsOpCodes::REMOVE_FILLED_DIR_IN_TMP): { + result = createNonEmptyTmpDir(); + if(result != HasReturnvaluesIF::RETURN_OK) { + return; + } + result = fsHandler->removeDirectory("/tmp/test", true, &cfg); + if(result == HasReturnvaluesIF::RETURN_OK) { + sif::info << "Directory removed recursively successfully" << std::endl; + } + else { + sif::warning << "Recursive directory removal failed!" << std::endl; + } + break; + } + case(FsOpCodes::ATTEMPT_DIR_REMOVAL_NON_EMPTY): { + result = createNonEmptyTmpDir(); + if(result != HasReturnvaluesIF::RETURN_OK) { + return; + } + result = fsHandler->removeDirectory("/tmp/test", false, &cfg); + if(result != HasReturnvaluesIF::RETURN_OK) { + sif::info << "Directory removal attempt failed as expected" << std::endl; + } + else { + sif::warning << "Directory removal worked when it should not have!" << std::endl; + } + } } } diff --git a/bsp_q7s/boardtest/Q7STestTask.h b/bsp_q7s/boardtest/Q7STestTask.h index 6f6b04f2..a14d8428 100644 --- a/bsp_q7s/boardtest/Q7STestTask.h +++ b/bsp_q7s/boardtest/Q7STestTask.h @@ -19,11 +19,12 @@ private: enum FsOpCodes { CREATE_EMPTY_FILE_IN_TMP, REMOVE_TMP_FILE, - CREATE_DIR, - REMOVE_DIR + CREATE_DIR_IN_TMP, + REMOVE_EMPTY_DIR_IN_TMP, + ATTEMPT_DIR_REMOVAL_NON_EMPTY, + REMOVE_FILLED_DIR_IN_TMP, }; void testFileSystemHandlerDirect(FsOpCodes opCode); - }; diff --git a/bsp_q7s/memory/FileSystemHandler.cpp b/bsp_q7s/memory/FileSystemHandler.cpp index 171c470a..159512a0 100644 --- a/bsp_q7s/memory/FileSystemHandler.cpp +++ b/bsp_q7s/memory/FileSystemHandler.cpp @@ -6,6 +6,7 @@ #include "fsfw/memory/GenericFileSystemMessage.h" #include "fsfw/ipc/QueueFactory.h" +#include #include #include @@ -163,7 +164,7 @@ ReturnValue_t FileSystemHandler::createFile(const char *repositoryPath, const ch return HasReturnvaluesIF::RETURN_OK; } -ReturnValue_t FileSystemHandler::deleteFile(const char *repositoryPath, const char *filename, +ReturnValue_t FileSystemHandler::removeFile(const char *repositoryPath, const char *filename, void *args) { std::string fullPath; bool useMountPrefix = true; @@ -225,19 +226,30 @@ ReturnValue_t FileSystemHandler::removeDirectory(const char *repositoryPath, else { // Check error code. Most probably denied permissions because folder is not empty sif::warning << "FileSystemHandler::removeDirectory: Deleting directory failed with " - "err" << err << std::endl; - return GENERIC_FILE_ERROR; + "code" << err.value() << ": " << strerror(err.value()) << std::endl; + if(err.value() == ENOTEMPTY) { + return DIRECTORY_NOT_EMPTY; + } + else { + return GENERIC_FILE_ERROR; + } + } } else { if(std::filesystem::remove_all(fullPath, err)) { - sif::warning << "FileSystemHandler::removeDirectory: Deleting directory recursively " - "failed with err" << err << std::endl; return HasReturnvaluesIF::RETURN_OK; } else { + sif::warning << "FileSystemHandler::removeDirectory: Deleting directory failed with " + "code" << err.value() << ": " << strerror(err.value()) << std::endl; // Check error code - return GENERIC_FILE_ERROR; + if(err.value() == ENOTEMPTY) { + return DIRECTORY_NOT_EMPTY; + } + else { + return GENERIC_FILE_ERROR; + } } } diff --git a/bsp_q7s/memory/FileSystemHandler.h b/bsp_q7s/memory/FileSystemHandler.h index 2d22f24b..22a7e7ae 100644 --- a/bsp_q7s/memory/FileSystemHandler.h +++ b/bsp_q7s/memory/FileSystemHandler.h @@ -40,7 +40,7 @@ public: ReturnValue_t createFile(const char* repositoryPath, const char* filename, const uint8_t* data = nullptr, size_t size = 0, void* args = nullptr) override; - ReturnValue_t deleteFile(const char* repositoryPath, + ReturnValue_t removeFile(const char* repositoryPath, const char* filename, void* args = nullptr) override; ReturnValue_t createDirectory(const char* repositoryPath, void* args = nullptr) override; ReturnValue_t removeDirectory(const char* repositoryPath, bool deleteRecurively = false, diff --git a/fsfw b/fsfw index bdc5f593..a7a4e0f2 160000 --- a/fsfw +++ b/fsfw @@ -1 +1 @@ -Subproject commit bdc5f593e2631a7fc24c06513d8925a4765e45b6 +Subproject commit a7a4e0f219eb3f23e644f519605a79772a3c951a