eive-obsw/bsp_q7s/memory/FileSystemHandler.cpp

258 lines
8.9 KiB
C++
Raw Normal View History

2021-07-08 12:22:41 +02:00
#include "FileSystemHandler.h"
2021-07-19 12:44:43 +02:00
#include "bsp_q7s/core/CoreController.h"
2021-07-08 12:22:41 +02:00
#include "fsfw/tasks/TaskFactory.h"
2021-07-08 12:32:24 +02:00
#include "fsfw/memory/GenericFileSystemMessage.h"
#include "fsfw/ipc/QueueFactory.h"
2021-07-08 12:22:41 +02:00
2021-07-19 19:21:34 +02:00
#include <cstring>
2021-07-19 12:44:43 +02:00
#include <fstream>
#include <filesystem>
2021-07-08 12:22:41 +02:00
FileSystemHandler::FileSystemHandler(object_id_t fileSystemHandler):
SystemObject(fileSystemHandler) {
2021-07-12 16:32:14 +02:00
mq = QueueFactory::instance()->createMessageQueue(FS_MAX_QUEUE_SIZE);
2021-07-08 12:32:24 +02:00
}
FileSystemHandler::~FileSystemHandler() {
QueueFactory::instance()->deleteMessageQueue(mq);
2021-07-08 12:22:41 +02:00
}
ReturnValue_t FileSystemHandler::performOperation(uint8_t unsignedChar) {
while(true) {
2021-07-12 17:23:32 +02:00
try {
fileSystemHandlerLoop();
2021-07-08 12:32:24 +02:00
}
2021-07-12 17:23:32 +02:00
catch(std::bad_alloc& e) {
// Restart OBSW, hints at a memory leak
sif::error << "Allocation error in FileSystemHandler::performOperation"
<< e.what() << std::endl;
2021-07-19 12:44:43 +02:00
// Set up an error file or a special flag in the scratch buffer for these cases
triggerEvent(CoreController::ALLOC_FAILURE, 0 , 0);
CoreController::incrementAllocationFailureCount();
2021-07-12 17:23:32 +02:00
}
}
}
void FileSystemHandler::fileSystemHandlerLoop() {
CommandMessage filemsg;
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
while(true) {
if(opCounter % 5 == 0) {
2021-08-06 01:25:36 +02:00
if(coreCtrl->sdInitFinished()) {
fileSystemCheckup();
}
2021-07-12 17:23:32 +02:00
}
result = mq->receiveMessage(&filemsg);
if(result == MessageQueueIF::EMPTY) {
break;
}
else if(result != HasReturnvaluesIF::RETURN_FAILED) {
sif::warning << "FileSystemHandler::performOperation: Message reception failed!"
<< std::endl;
break;
}
Command_t command = filemsg.getCommand();
switch(command) {
case(GenericFileSystemMessage::CMD_CREATE_DIRECTORY): {
break;
}
case(GenericFileSystemMessage::CMD_CREATE_FILE): {
break;
}
}
opCounter++;
}
// This task will have a low priority and will run permanently in the background
// so we will just run in a permanent loop here and check file system
// messages permanently
opCounter++;
2021-07-12 17:23:32 +02:00
TaskFactory::instance()->delayTask(1000);
}
2021-07-08 12:32:24 +02:00
2021-07-12 17:23:32 +02:00
void FileSystemHandler::fileSystemCheckup() {
2021-08-06 01:25:36 +02:00
SdCardManager::SdStatePair statusPair;
2021-07-12 17:23:32 +02:00
sdcMan->getSdCardActiveStatus(statusPair);
sd::SdCard preferredSdCard;
sdcMan->getPreferredSdCard(preferredSdCard);
if((preferredSdCard == sd::SdCard::SLOT_0) and
(statusPair.first == sd::SdState::MOUNTED)) {
2021-07-12 17:23:32 +02:00
currentMountPrefix = SdCardManager::SD_0_MOUNT_POINT;
}
else if((preferredSdCard == sd::SdCard::SLOT_1) and
(statusPair.second == sd::SdState::MOUNTED)) {
2021-07-12 17:23:32 +02:00
currentMountPrefix = SdCardManager::SD_1_MOUNT_POINT;
}
else {
std::string sdString;
if(preferredSdCard == sd::SdCard::SLOT_0) {
sdString = "0";
}
else {
sdString = "1";
}
sif::warning << "FileSystemHandler::performOperation: "
"Inconsistent state detected" << std::endl;
sif::warning << "Preferred SD card is " << sdString <<
2021-07-12 17:23:32 +02:00
" but does not appear to be mounted. Attempting fix.." << std::endl;
// This function will appear to fix the inconsistent state
ReturnValue_t result = sdcMan->sanitizeState(&statusPair, preferredSdCard);
2021-07-12 17:23:32 +02:00
if(result != HasReturnvaluesIF::RETURN_OK) {
2021-07-12 17:50:01 +02:00
// Oh no.
2021-07-19 12:44:43 +02:00
triggerEvent(SdCardManager::SANITIZATION_FAILED, 0, 0);
sif::error << "FileSystemHandler::fileSystemCheckup: Sanitization failed" << std::endl;
2021-07-12 17:23:32 +02:00
}
2021-07-08 12:22:41 +02:00
}
}
2021-07-08 12:32:24 +02:00
MessageQueueId_t FileSystemHandler::getCommandQueue() const {
return mq->getId();
}
2021-07-12 17:23:32 +02:00
ReturnValue_t FileSystemHandler::initialize() {
2021-08-06 01:25:36 +02:00
coreCtrl = ObjectManager::instance()->get<CoreController>(objects::CORE_CONTROLLER);
if(coreCtrl == nullptr) {
sif::error << "FileSystemHandler::initialize: Could not retrieve core controller handle" <<
std::endl;
}
2021-07-12 17:23:32 +02:00
sdcMan = SdCardManager::instance();
sd::SdCard preferredSdCard;
ReturnValue_t result = sdcMan->getPreferredSdCard(preferredSdCard);
if(result != HasReturnvaluesIF::RETURN_OK) {
return result;
}
if(preferredSdCard == sd::SdCard::SLOT_0) {
currentMountPrefix = SdCardManager::SD_0_MOUNT_POINT;
}
else if(preferredSdCard == sd::SdCard::SLOT_1) {
currentMountPrefix = SdCardManager::SD_1_MOUNT_POINT;
}
return HasReturnvaluesIF::RETURN_OK;
}
2021-11-19 13:22:20 +01:00
ReturnValue_t FileSystemHandler::appendToFile(const char* repositoryPath,
const char* filename, const uint8_t* data, size_t size,
uint16_t packetNumber, FileSystemArgsIF* args) {
2021-11-25 21:38:13 +01:00
auto path = getInitPath(args) / repositoryPath / filename;
if(not std::filesystem::exists(path)) {
2021-07-19 12:44:43 +02:00
return FILE_DOES_NOT_EXIST;
}
2021-11-25 21:38:13 +01:00
std::ofstream file(path, std::ios_base::app|std::ios_base::out);
2021-07-19 12:44:43 +02:00
file.write(reinterpret_cast<const char*>(data), size);
if(not file.good()) {
return GENERIC_FILE_ERROR;
}
2021-07-08 12:32:24 +02:00
return HasReturnvaluesIF::RETURN_OK;
}
2021-11-19 13:22:20 +01:00
ReturnValue_t FileSystemHandler::createFile(const char* repositoryPath,
const char* filename, const uint8_t* data, size_t size, FileSystemArgsIF* args) {
2021-11-25 21:38:13 +01:00
auto path = getInitPath(args) / filename;
if(std::filesystem::exists(path)) {
2021-07-19 12:44:43 +02:00
return FILE_ALREADY_EXISTS;
}
2021-11-25 21:38:13 +01:00
std::ofstream file(path);
2021-07-19 12:44:43 +02:00
file.write(reinterpret_cast<const char*>(data), size);
if(not file.good()) {
return GENERIC_FILE_ERROR;
}
2021-07-08 12:32:24 +02:00
return HasReturnvaluesIF::RETURN_OK;
}
2021-11-19 13:22:20 +01:00
ReturnValue_t FileSystemHandler::removeFile(const char* repositoryPath,
const char* filename, FileSystemArgsIF* args) {
2021-11-25 21:38:13 +01:00
auto path = getInitPath(args) / repositoryPath / filename;
if(not std::filesystem::exists(path)) {
2021-07-19 12:44:43 +02:00
return FILE_DOES_NOT_EXIST;
}
2021-11-25 21:38:13 +01:00
int result = std::remove(path.c_str());
2021-07-19 12:44:43 +02:00
if(result != 0) {
sif::warning << "FileSystemHandler::deleteFile: Failed with code " << result << std::endl;
return GENERIC_FILE_ERROR;
}
2021-07-08 12:32:24 +02:00
return HasReturnvaluesIF::RETURN_OK;
}
2021-07-12 17:23:32 +02:00
2021-11-19 13:22:20 +01:00
ReturnValue_t FileSystemHandler:: createDirectory(const char* repositoryPath, const char* dirname,
bool createParentDirs, FileSystemArgsIF* args) {
2021-11-25 21:38:13 +01:00
auto path = getInitPath(args) / repositoryPath / dirname;
if(std::filesystem::exists(path)) {
return DIRECTORY_ALREADY_EXISTS;
}
2021-11-25 21:38:13 +01:00
if(std::filesystem::create_directory(path)) {
return HasReturnvaluesIF::RETURN_OK;
}
2021-11-25 21:38:13 +01:00
sif::warning << "Creating directory " << path << " failed" << std::endl;
return GENERIC_FILE_ERROR;
2021-07-12 17:23:32 +02:00
}
2021-11-19 13:22:20 +01:00
ReturnValue_t FileSystemHandler::removeDirectory(const char* repositoryPath, const char* dirname,
bool deleteRecurively, FileSystemArgsIF* args) {
2021-11-25 21:38:13 +01:00
auto path = getInitPath(args) / repositoryPath / dirname;
if(not std::filesystem::exists(path)) {
return DIRECTORY_DOES_NOT_EXIST;
}
std::error_code err;
if(not deleteRecurively) {
2021-11-25 21:38:13 +01:00
if(std::filesystem::remove(path, err)) {
return HasReturnvaluesIF::RETURN_OK;
}
else {
// Check error code. Most probably denied permissions because folder is not empty
2021-07-19 12:44:43 +02:00
sif::warning << "FileSystemHandler::removeDirectory: Deleting directory failed with "
"code " << err.value() << ": " << strerror(err.value()) << std::endl;
2021-07-19 19:21:34 +02:00
if(err.value() == ENOTEMPTY) {
return DIRECTORY_NOT_EMPTY;
}
else {
return GENERIC_FILE_ERROR;
}
}
}
else {
2021-11-25 21:38:13 +01:00
if(std::filesystem::remove_all(path, err)) {
return HasReturnvaluesIF::RETURN_OK;
}
else {
2021-07-19 19:21:34 +02:00
sif::warning << "FileSystemHandler::removeDirectory: Deleting directory failed with "
"code " << err.value() << ": " << strerror(err.value()) << std::endl;
// Check error code
2021-07-19 19:21:34 +02:00
if(err.value() == ENOTEMPTY) {
return DIRECTORY_NOT_EMPTY;
}
else {
return GENERIC_FILE_ERROR;
}
}
}
2021-07-12 17:23:32 +02:00
return HasReturnvaluesIF::RETURN_OK;
}
2021-07-19 14:34:03 +02:00
2021-11-25 21:38:13 +01:00
ReturnValue_t FileSystemHandler::renameFile(const char *repositoryPath, const char *oldFilename,
const char *newFilename, FileSystemArgsIF *args) {
auto basepath = getInitPath(args) / repositoryPath;
std::filesystem::rename(basepath / oldFilename, basepath / newFilename);
return HasReturnvaluesIF::RETURN_OK;
}
2021-07-19 14:34:03 +02:00
void FileSystemHandler::parseCfg(FsCommandCfg *cfg, bool& useMountPrefix) {
if(cfg != nullptr) {
useMountPrefix = cfg->useMountPrefix;
}
}
2021-11-19 13:22:20 +01:00
2021-11-25 21:38:13 +01:00
std::filesystem::path FileSystemHandler::getInitPath(FileSystemArgsIF* args) {
bool useMountPrefix = true;
parseCfg(reinterpret_cast<FsCommandCfg*>(args), useMountPrefix);
std::string path;
if(useMountPrefix) {
path = currentMountPrefix;
}
return std::filesystem::path(path);
2021-11-19 13:22:20 +01:00
}