cleaned up code a bit

This commit is contained in:
2021-07-19 12:44:43 +02:00
committed by Robin Mueller
parent d98313de6c
commit 20e8fc3400
21 changed files with 360 additions and 180 deletions

View File

@ -1,9 +1,12 @@
#include "FileSystemHandler.h"
#include "bsp_q7s/core/CoreController.h"
#include "fsfw/tasks/TaskFactory.h"
#include "fsfw/memory/GenericFileSystemMessage.h"
#include "fsfw/ipc/QueueFactory.h"
#include <fstream>
#include <filesystem>
FileSystemHandler::FileSystemHandler(object_id_t fileSystemHandler):
@ -24,9 +27,9 @@ ReturnValue_t FileSystemHandler::performOperation(uint8_t unsignedChar) {
// Restart OBSW, hints at a memory leak
sif::error << "Allocation error in FileSystemHandler::performOperation"
<< e.what() << std::endl;
// TODO: If we trigger an event, it might not get sent because were restarting
// Set up an error file or a special flag in the scratch buffer.
// TODO: CoreController: Implement function to restart OBC
// Set up an error file or a special flag in the scratch buffer for these cases
triggerEvent(CoreController::ALLOC_FAILURE, 0 , 0);
CoreController::incrementAllocationFailureCount();
}
}
}
@ -94,8 +97,8 @@ void FileSystemHandler::fileSystemCheckup() {
ReturnValue_t result = sdcMan->sanitizeState(&statusPair, preferredSdCard);
if(result != HasReturnvaluesIF::RETURN_OK) {
// Oh no.
// TODO: Trigger medium severity event
sif::error << "Fix failed" << std::endl;
triggerEvent(SdCardManager::SANITIZATION_FAILED, 0, 0);
sif::error << "FileSystemHandler::fileSystemCheckup: Sanitization failed" << std::endl;
}
}
}
@ -125,6 +128,14 @@ ReturnValue_t FileSystemHandler::appendToFile(const char *repositoryPath, const
// A double slash between repo and filename should not be an issue, so add it in any case
std::string fullPath = currentMountPrefix + std::string(repositoryPath) + "/" +
std::string(filename);
if(not std::filesystem::exists(fullPath)) {
return FILE_DOES_NOT_EXIST;
}
std::ofstream file(fullPath, std::ios_base::app|std::ios_base::out);
file.write(reinterpret_cast<const char*>(data), size);
if(not file.good()) {
return GENERIC_FILE_ERROR;
}
return HasReturnvaluesIF::RETURN_OK;
}
@ -133,6 +144,14 @@ ReturnValue_t FileSystemHandler::createFile(const char *repositoryPath, const ch
// A double slash between repo and filename should not be an issue, so add it in any case
std::string fullPath = currentMountPrefix + std::string(repositoryPath) + "/" +
std::string(filename);
if(std::filesystem::exists(fullPath)) {
return FILE_ALREADY_EXISTS;
}
std::ofstream file(fullPath);
file.write(reinterpret_cast<const char*>(data), size);
if(not file.good()) {
return GENERIC_FILE_ERROR;
}
return HasReturnvaluesIF::RETURN_OK;
}
@ -141,6 +160,14 @@ ReturnValue_t FileSystemHandler::deleteFile(const char *repositoryPath, const ch
// A double slash between repo and filename should not be an issue, so add it in any case
std::string fullPath = currentMountPrefix + std::string(repositoryPath) + "/" +
std::string(filename);
if(not std::filesystem::exists(fullPath)) {
return FILE_DOES_NOT_EXIST;
}
int result = std::remove(fullPath.c_str());
if(result != 0) {
sif::warning << "FileSystemHandler::deleteFile: Failed with code " << result << std::endl;
return GENERIC_FILE_ERROR;
}
return HasReturnvaluesIF::RETURN_OK;
}
@ -169,14 +196,20 @@ 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;
}
}
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 {
// Check error code
return GENERIC_FILE_ERROR;
}
}