F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
FileManager.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title FileManager.hpp
3 // \author bocchino
4 // \brief hpp file for FileManager component implementation class
5 //
6 // \copyright
7 // Copyright 2009-2015, by the California Institute of Technology.
8 // ALL RIGHTS RESERVED. United States Government Sponsorship
9 // acknowledged.
10 //
11 // ======================================================================
12 
13 #include <cstdio>
14 #include <cstdlib>
15 
16 #include <Fw/FPrimeBasicTypes.hpp>
17 #include "Fw/Types/Assert.hpp"
20 
21 namespace Svc {
22 
23 // ----------------------------------------------------------------------
24 // Construction, initialization, and destruction
25 // ----------------------------------------------------------------------
26 
27 FileManager ::FileManager(const char* const compName
28  )
29  : FileManagerComponentBase(compName), commandCount(0), errorCount(0) {}
30 
32 
33 // ----------------------------------------------------------------------
34 // Command handler implementations
35 // ----------------------------------------------------------------------
36 
37 void FileManager ::CreateDirectory_cmdHandler(const FwOpcodeType opCode,
38  const U32 cmdSeq,
39  const Fw::CmdStringArg& dirName) {
40  Fw::LogStringArg logStringDirName(dirName.toChar());
41  this->log_ACTIVITY_HI_CreateDirectoryStarted(logStringDirName);
42  bool errorIfDirExists = true;
43  const Os::FileSystem::Status status = Os::FileSystem::createDirectory(dirName.toChar(), errorIfDirExists);
44  if (status != Os::FileSystem::OP_OK) {
45  this->log_WARNING_HI_DirectoryCreateError(logStringDirName, status);
46  } else {
47  this->log_ACTIVITY_HI_CreateDirectorySucceeded(logStringDirName);
48  }
49  this->emitTelemetry(status);
50  this->sendCommandResponse(opCode, cmdSeq, status);
51 }
52 
53 void FileManager ::RemoveFile_cmdHandler(const FwOpcodeType opCode,
54  const U32 cmdSeq,
55  const Fw::CmdStringArg& fileName,
56  const bool ignoreErrors) {
57  Fw::LogStringArg logStringFileName(fileName.toChar());
58  this->log_ACTIVITY_HI_RemoveFileStarted(logStringFileName);
59  const Os::FileSystem::Status status = Os::FileSystem::removeFile(fileName.toChar());
60  if (status != Os::FileSystem::OP_OK) {
61  this->log_WARNING_HI_FileRemoveError(logStringFileName, status);
62  if (ignoreErrors == true) {
63  ++this->errorCount;
64  this->tlmWrite_Errors(this->errorCount);
65  this->cmdResponse_out(opCode, cmdSeq, Fw::CmdResponse::OK);
66  return;
67  }
68  } else {
69  this->log_ACTIVITY_HI_RemoveFileSucceeded(logStringFileName);
70  }
71  this->emitTelemetry(status);
72  this->sendCommandResponse(opCode, cmdSeq, status);
73 }
74 
75 void FileManager ::MoveFile_cmdHandler(const FwOpcodeType opCode,
76  const U32 cmdSeq,
77  const Fw::CmdStringArg& sourceFileName,
78  const Fw::CmdStringArg& destFileName) {
79  Fw::LogStringArg logStringSource(sourceFileName.toChar());
80  Fw::LogStringArg logStringDest(destFileName.toChar());
81  this->log_ACTIVITY_HI_MoveFileStarted(logStringSource, logStringDest);
82  const Os::FileSystem::Status status = Os::FileSystem::moveFile(sourceFileName.toChar(), destFileName.toChar());
83  if (status != Os::FileSystem::OP_OK) {
84  this->log_WARNING_HI_FileMoveError(logStringSource, logStringDest, status);
85  } else {
86  this->log_ACTIVITY_HI_MoveFileSucceeded(logStringSource, logStringDest);
87  }
88  this->emitTelemetry(status);
89  this->sendCommandResponse(opCode, cmdSeq, status);
90 }
91 
92 void FileManager ::RemoveDirectory_cmdHandler(const FwOpcodeType opCode,
93  const U32 cmdSeq,
94  const Fw::CmdStringArg& dirName) {
95  Fw::LogStringArg logStringDirName(dirName.toChar());
96  this->log_ACTIVITY_HI_RemoveDirectoryStarted(logStringDirName);
98  if (status != Os::FileSystem::OP_OK) {
99  this->log_WARNING_HI_DirectoryRemoveError(logStringDirName, status);
100  } else {
101  this->log_ACTIVITY_HI_RemoveDirectorySucceeded(logStringDirName);
102  }
103  this->emitTelemetry(status);
104  this->sendCommandResponse(opCode, cmdSeq, status);
105 }
106 
107 void FileManager ::ShellCommand_cmdHandler(const FwOpcodeType opCode,
108  const U32 cmdSeq,
109  const Fw::CmdStringArg& command,
110  const Fw::CmdStringArg& logFileName) {
111  Fw::LogStringArg logStringCommand(command.toChar());
112  this->log_ACTIVITY_HI_ShellCommandStarted(logStringCommand);
113  int status = this->systemCall(command, logFileName);
114  if (status == 0) {
115  this->log_ACTIVITY_HI_ShellCommandSucceeded(logStringCommand);
116  } else {
117  this->log_WARNING_HI_ShellCommandFailed(logStringCommand, static_cast<U32>(status));
118  }
119  this->emitTelemetry(status == 0 ? Os::FileSystem::OP_OK : Os::FileSystem::OTHER_ERROR);
120  this->sendCommandResponse(opCode, cmdSeq, status == 0 ? Os::FileSystem::OP_OK : Os::FileSystem::OTHER_ERROR);
121 }
122 
123 void FileManager ::AppendFile_cmdHandler(const FwOpcodeType opCode,
124  const U32 cmdSeq,
125  const Fw::CmdStringArg& source,
126  const Fw::CmdStringArg& target) {
127  Fw::LogStringArg logStringSource(source.toChar());
128  Fw::LogStringArg logStringTarget(target.toChar());
129  this->log_ACTIVITY_HI_AppendFileStarted(logStringSource, logStringTarget);
130 
131  Os::FileSystem::Status status;
132  status = Os::FileSystem::appendFile(source.toChar(), target.toChar(), true);
133  if (status != Os::FileSystem::OP_OK) {
134  this->log_WARNING_HI_AppendFileFailed(logStringSource, logStringTarget, status);
135  } else {
136  this->log_ACTIVITY_HI_AppendFileSucceeded(logStringSource, logStringTarget);
137  }
138 
139  this->emitTelemetry(status);
140  this->sendCommandResponse(opCode, cmdSeq, status);
141 }
142 
143 void FileManager ::FileSize_cmdHandler(const FwOpcodeType opCode, const U32 cmdSeq, const Fw::CmdStringArg& fileName) {
144  Fw::LogStringArg logStringFileName(fileName.toChar());
145  this->log_ACTIVITY_HI_FileSizeStarted(logStringFileName);
146 
147  FwSizeType size_arg;
148  const Os::FileSystem::Status status = Os::FileSystem::getFileSize(fileName.toChar(), size_arg);
149  if (status != Os::FileSystem::OP_OK) {
150  this->log_WARNING_HI_FileSizeError(logStringFileName, status);
151  } else {
152  U64 size = static_cast<U64>(size_arg);
153  this->log_ACTIVITY_HI_FileSizeSucceeded(logStringFileName, size);
154  }
155  this->emitTelemetry(status);
156  this->sendCommandResponse(opCode, cmdSeq, status);
157 }
158 
159 void FileManager ::pingIn_handler(const FwIndexType portNum, U32 key) {
160  // return key
161  this->pingOut_out(0, key);
162 }
163 // ----------------------------------------------------------------------
164 // Helper methods
165 // ----------------------------------------------------------------------
166 
167 int FileManager ::systemCall(const Fw::CmdStringArg& command, const Fw::CmdStringArg& logFileName) const {
168  // Create a buffer of at least enough size for storing the eval string less the 2 %s tokens, two command strings,
169  // and a null terminator at the end
170  const char evalStr[] = "eval '%s' 1>>%s 2>&1\n";
171  constexpr U32 bufferSize = (sizeof(evalStr) - 4) + (2 * FW_CMD_STRING_MAX_SIZE) + 1;
172  char buffer[bufferSize];
173 
174  // Wrap that buffer in an external string for formatting purposes
175  Fw::ExternalString stringBuffer(buffer, bufferSize);
176  Fw::FormatStatus formatStatus = stringBuffer.format(evalStr, command.toChar(), logFileName.toChar());
177  // Since the buffer is exactly sized, the only error can occur is a software error not caused by ground
178  FW_ASSERT(formatStatus == Fw::FormatStatus::SUCCESS);
179 
180  // Call the system
181  const int status = system(stringBuffer.toChar());
182  return status;
183 }
184 
185 void FileManager ::emitTelemetry(const Os::FileSystem::Status status) {
186  if (status == Os::FileSystem::OP_OK) {
187  ++this->commandCount;
188  this->tlmWrite_CommandsExecuted(this->commandCount);
189  } else {
190  ++this->errorCount;
191  this->tlmWrite_Errors(this->errorCount);
192  }
193 }
194 
195 void FileManager ::sendCommandResponse(const FwOpcodeType opCode,
196  const U32 cmdSeq,
197  const Os::FileSystem::Status status) {
198  this->cmdResponse_out(opCode, cmdSeq,
200 }
201 
202 } // namespace Svc
void log_ACTIVITY_HI_FileSizeSucceeded(const Fw::StringBase &fileName, U64 size) const
void log_ACTIVITY_HI_CreateDirectoryStarted(const Fw::StringBase &dirName) const
FwIdType FwOpcodeType
The type of a command opcode.
void log_ACTIVITY_HI_RemoveDirectorySucceeded(const Fw::StringBase &dirName) const
PlatformSizeType FwSizeType
static Status moveFile(const char *sourcePath, const char *destPath)
Move a file from sourcePath to destPath.
Definition: FileSystem.cpp:207
void log_WARNING_HI_FileSizeError(const Fw::StringBase &fileName, U32 status) const
FileManager(const char *const compName)
Definition: FileManager.cpp:27
static Status removeDirectory(const char *path)
Remove a directory at the specified path.
Definition: FileSystem.cpp:82
static Status appendFile(const char *sourcePath, const char *destPath, bool createMissingDest=false)
Append the source file to the destination file.
Definition: FileSystem.cpp:176
Auto-generated base for FileManager component.
void pingOut_out(FwIndexType portNum, U32 key)
Invoke output port pingOut.
void log_ACTIVITY_HI_MoveFileStarted(const Fw::StringBase &sourceFileName, const Fw::StringBase &destFileName) const
void log_ACTIVITY_HI_AppendFileSucceeded(const Fw::StringBase &source, const Fw::StringBase &target) const
void log_ACTIVITY_HI_CreateDirectorySucceeded(const Fw::StringBase &dirName) const
void log_ACTIVITY_HI_RemoveDirectoryStarted(const Fw::StringBase &dirName) const
void log_ACTIVITY_HI_ShellCommandSucceeded(const Fw::StringBase &command) const
A string backed by an external buffer.
void cmdResponse_out(FwOpcodeType opCode, U32 cmdSeq, Fw::CmdResponse response)
Emit command response.
void log_ACTIVITY_HI_FileSizeStarted(const Fw::StringBase &fileName) const
Command successfully executed.
void log_ACTIVITY_HI_MoveFileSucceeded(const Fw::StringBase &sourceFileName, const Fw::StringBase &destFileName) const
static Status getFileSize(const char *path, FwSizeType &size)
Get the size of the file (in bytes) at the specified path.
Definition: FileSystem.cpp:225
static Status createDirectory(const char *path, bool errorIfAlreadyExists=false)
Create a new directory at the specified path.
Definition: FileSystem.cpp:110
void log_WARNING_HI_DirectoryRemoveError(const Fw::StringBase &dirName, U32 status) const
Command had execution error.
other OS-specific error
Definition: FileSystem.hpp:40
const char * toChar() const
Definition: CmdString.hpp:50
void log_ACTIVITY_HI_AppendFileStarted(const Fw::StringBase &source, const Fw::StringBase &target) const
void tlmWrite_Errors(U32 arg, Fw::Time _tlmTime=Fw::Time()) const
void log_ACTIVITY_HI_RemoveFileStarted(const Fw::StringBase &fileName) const
void log_ACTIVITY_HI_ShellCommandStarted(const Fw::StringBase &command) const
PlatformIndexType FwIndexType
void log_WARNING_HI_AppendFileFailed(const Fw::StringBase &source, const Fw::StringBase &target, U32 status) const
RateGroupDivider component implementation.
Operation was successful.
Definition: FileSystem.hpp:24
#define FW_CMD_STRING_MAX_SIZE
Max character size of command string arguments.
Definition: FpConfig.h:186
void log_WARNING_HI_ShellCommandFailed(const Fw::StringBase &command, U32 status) const
void log_WARNING_HI_DirectoryCreateError(const Fw::StringBase &dirName, U32 status) const
void log_ACTIVITY_HI_RemoveFileSucceeded(const Fw::StringBase &fileName) const
void log_WARNING_HI_FileMoveError(const Fw::StringBase &sourceFileName, const Fw::StringBase &destFileName, U32 status) const
void tlmWrite_CommandsExecuted(U32 arg, Fw::Time _tlmTime=Fw::Time()) const
#define FW_ASSERT(...)
Definition: Assert.hpp:14
static Status removeFile(const char *path)
Remove a file at the specified path.
Definition: FileSystem.cpp:86
void log_WARNING_HI_FileRemoveError(const Fw::StringBase &fileName, U32 status) const
FormatStatus
status of string format calls
Definition: format.hpp:18
#define U64(C)
Definition: sha.h:180