FileManager updates (#1522)

* add ignoreErrors argument to RemoveFile command

* adding FileStat command

* add FileSize tests
This commit is contained in:
codeflight1 2023-02-03 10:16:00 -08:00 committed by GitHub
parent 7184ca9971
commit a349f81ea0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 148 additions and 7 deletions

View File

@ -20,6 +20,7 @@ async command RemoveDirectory(
@ Remove a file
async command RemoveFile(
fileName: string size 256 @< The file to remove
ignoreErrors: bool @< Ignore non-existent files
) \
opcode 0x03
@ -36,3 +37,8 @@ async command AppendFile(
target: string size 256 @< The name of the file to append to
) \
opcode 0x05
async command FileSize(
fileName: string size 256 @< The file to get the size of
) \
opcode 0x06

View File

@ -153,3 +153,29 @@ event RemoveFileStarted(
severity activity high \
id 0x11 \
format "Removing file {}..."
@ File size response
event FileSizeSucceeded(
fileName: string size 256 @< The name of the file
$size: U64 @< The size of the file in bytes
) \
severity activity high \
id 0x12 \
format "The size of file {} is {} B"
@ Failed to get file size
event FileSizeError(
fileName: string size 256 @< The name of the file
status: U32 @< The error status
) \
severity warning high \
id 0x13 \
format "Failed to get the size of file {}, returned status {}"
@ Checking file size
event FileSizeStarted(
fileName: string size 256 @< The name of the file
) \
severity activity high \
id 0x14 \
format "Checking size of file {}..."

View File

@ -80,7 +80,8 @@ namespace Svc {
RemoveFile_cmdHandler(
const FwOpcodeType opCode,
const U32 cmdSeq,
const Fw::CmdStringArg& fileName
const Fw::CmdStringArg& fileName,
const bool ignoreErrors
)
{
Fw::LogStringArg logStringFileName(fileName.toChar());
@ -92,6 +93,16 @@ namespace Svc {
logStringFileName,
status
);
if (ignoreErrors == true) {
++this->errorCount;
this->tlmWrite_Errors(this->errorCount);
this->cmdResponse_out(
opCode,
cmdSeq,
Fw::CmdResponse::OK
);
return;
}
} else {
this->log_ACTIVITY_HI_RemoveFileSucceeded(logStringFileName);
}
@ -213,6 +224,31 @@ namespace Svc {
this->sendCommandResponse(opCode, cmdSeq, status);
}
void FileManager ::
FileSize_cmdHandler(
const FwOpcodeType opCode,
const U32 cmdSeq,
const Fw::CmdStringArg& fileName
)
{
Fw::LogStringArg logStringFileName(fileName.toChar());
this->log_ACTIVITY_HI_FileSizeStarted(logStringFileName);
U64 size;
const Os::FileSystem::Status status =
Os::FileSystem::getFileSize(fileName.toChar(), size);
if (status != Os::FileSystem::OP_OK) {
this->log_WARNING_HI_FileSizeError(
logStringFileName,
status
);
} else {
this->log_ACTIVITY_HI_FileSizeSucceeded(logStringFileName, size);
}
this->emitTelemetry(status);
this->sendCommandResponse(opCode, cmdSeq, status);
}
void FileManager ::
pingIn_handler(
const NATIVE_INT_TYPE portNum,

View File

@ -64,7 +64,8 @@ namespace Svc {
void RemoveFile_cmdHandler(
const FwOpcodeType opCode, //!< The opcode
const U32 cmdSeq, //!< The command sequence number
const Fw::CmdStringArg& fileName //!< The file to remove
const Fw::CmdStringArg& fileName, //!< The file to remove
const bool ignoreErrors //!< Ignore non-existent files
);
//! Implementation for MoveFile command handler
@ -102,6 +103,14 @@ namespace Svc {
const Fw::CmdStringArg& target //! The name of the file to append to
);
//! Implementation for FileSize command handler
//!
void FileSize_cmdHandler(
const FwOpcodeType opCode, //!< The opcode
const U32 cmdSeq, //!< The command sequence number
const Fw::CmdStringArg& fileName //!< The file to get the size of
);
//! Handler implementation for pingIn
//!
void pingIn_handler(

View File

@ -69,6 +69,16 @@ TEST(Test, appendFileFail) {
tester.appendFileFail();
}
TEST(Test, fileSizeSucceed) {
Svc::Tester tester;
tester.fileSizeSucceed();
}
TEST(Test, fileSizeFail) {
Svc::Tester tester;
tester.fileSizeFail();
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();

View File

@ -233,7 +233,7 @@ namespace Svc {
#endif
// Remove test_file
this->removeFile("test_file");
this->removeFile("test_file", false);
// Assert success
this->assertSuccess(
@ -259,7 +259,7 @@ namespace Svc {
#endif
// Attempt to remove test_file (should fail)
this->removeFile("test_file");
this->removeFile("test_file", false);
// Assert failure
this->assertFailure(
@ -419,6 +419,50 @@ namespace Svc {
);
}
void Tester ::
fileSizeSucceed() {
#if defined TGT_OS_TYPE_LINUX || TGT_OS_TYPE_DARWIN
// Remove testing files, if they exist
this->system("rm -rf file1");
this->system("echo 'file1 text' > file1");
#else
FAIL(); // Commands not implemented for this OS
#endif
Fw::CmdStringArg cmdStringFile("file1");
this->sendCmd_FileSize(
INSTANCE,
CMD_SEQ,
cmdStringFile
);
this->component.doDispatch();
this->assertSuccess(FileManager::OPCODE_FILESIZE, 2);
ASSERT_EVENTS_FileSizeSucceeded(0, "file1", 11);
}
void Tester ::
fileSizeFail() {
#if defined TGT_OS_TYPE_LINUX || TGT_OS_TYPE_DARWIN
// Remove testing files, if they exist
this->system("rm -rf file1");
#else
FAIL(); // Commands not implemented for this OS
#endif
Fw::CmdStringArg cmdStringFile("file1");
this->sendCmd_FileSize(
INSTANCE,
CMD_SEQ,
cmdStringFile
);
this->component.doDispatch();
this->assertFailure(
FileManager::OPCODE_FILESIZE
);
}
// ----------------------------------------------------------------------
// Helper methods
// ----------------------------------------------------------------------
@ -530,13 +574,14 @@ namespace Svc {
}
void Tester ::
removeFile(const char *const fileName)
removeFile(const char *const fileName, bool ignoreErrors)
{
Fw::CmdStringArg cmdStringFile(fileName);
this->sendCmd_RemoveFile(
INSTANCE,
CMD_SEQ,
cmdStringFile
cmdStringFile,
ignoreErrors
);
this->component.doDispatch();
}

View File

@ -94,6 +94,14 @@ namespace Svc {
//!
void appendFileFail();
//! File size (succeed)
//!
void fileSizeSucceed();
//! File size (fail)
//!
void fileSizeFail();
private:
// ----------------------------------------------------------------------
@ -130,7 +138,8 @@ namespace Svc {
//! Remove a file
void removeFile(
const char *const fileName
const char *const fileName,
bool ignoreErrors
);
//! Perform a shell command