tested dir remove functionality

This commit is contained in:
Robin Müller 2021-07-19 19:21:34 +02:00 committed by Robin Mueller
parent 4e05b396c4
commit 81ac5a3b2c
5 changed files with 109 additions and 18 deletions

View File

@ -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;
}
}
}
}

View File

@ -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);
};

View File

@ -6,6 +6,7 @@
#include "fsfw/memory/GenericFileSystemMessage.h"
#include "fsfw/ipc/QueueFactory.h"
#include <cstring>
#include <fstream>
#include <filesystem>
@ -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;
}
}
}

View File

@ -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,

2
fsfw

@ -1 +1 @@
Subproject commit bdc5f593e2631a7fc24c06513d8925a4765e45b6
Subproject commit a7a4e0f219eb3f23e644f519605a79772a3c951a