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 
18 #include "Fw/Types/Assert.hpp"
19 #include <FpConfig.hpp>
20 
21 namespace Svc {
22 
23  // ----------------------------------------------------------------------
24  // Construction, initialization, and destruction
25  // ----------------------------------------------------------------------
26 
29  const char *const compName
30  ) :
31  FileManagerComponentBase(compName),
32  commandCount(0),
33  errorCount(0)
34  {
35 
36  }
37 
40  {
41 
42  }
43 
44  // ----------------------------------------------------------------------
45  // Command handler implementations
46  // ----------------------------------------------------------------------
47 
48  void FileManager ::
49  CreateDirectory_cmdHandler(
50  const FwOpcodeType opCode,
51  const U32 cmdSeq,
52  const Fw::CmdStringArg& dirName
53  )
54  {
55  Fw::LogStringArg logStringDirName(dirName.toChar());
56  this->log_ACTIVITY_HI_CreateDirectoryStarted(logStringDirName);
57  bool errorIfDirExists = true;
58  const Os::FileSystem::Status status =
59  Os::FileSystem::createDirectory(dirName.toChar(), errorIfDirExists);
60  if (status != Os::FileSystem::OP_OK) {
62  logStringDirName,
63  status
64  );
65  } else {
66  this->log_ACTIVITY_HI_CreateDirectorySucceeded(logStringDirName);
67  }
68  this->emitTelemetry(status);
69  this->sendCommandResponse(opCode, cmdSeq, status);
70  }
71 
72  void FileManager ::
73  RemoveFile_cmdHandler(
74  const FwOpcodeType opCode,
75  const U32 cmdSeq,
76  const Fw::CmdStringArg& fileName,
77  const bool ignoreErrors
78  )
79  {
80  Fw::LogStringArg logStringFileName(fileName.toChar());
81  this->log_ACTIVITY_HI_RemoveFileStarted(logStringFileName);
82  const Os::FileSystem::Status status =
84  if (status != Os::FileSystem::OP_OK) {
86  logStringFileName,
87  status
88  );
89  if (ignoreErrors == true) {
90  ++this->errorCount;
91  this->tlmWrite_Errors(this->errorCount);
92  this->cmdResponse_out(
93  opCode,
94  cmdSeq,
96  );
97  return;
98  }
99  } else {
100  this->log_ACTIVITY_HI_RemoveFileSucceeded(logStringFileName);
101  }
102  this->emitTelemetry(status);
103  this->sendCommandResponse(opCode, cmdSeq, status);
104  }
105 
106  void FileManager ::
107  MoveFile_cmdHandler(
108  const FwOpcodeType opCode,
109  const U32 cmdSeq,
110  const Fw::CmdStringArg& sourceFileName,
111  const Fw::CmdStringArg& destFileName
112  )
113  {
114  Fw::LogStringArg logStringSource(sourceFileName.toChar());
115  Fw::LogStringArg logStringDest(destFileName.toChar());
116  this->log_ACTIVITY_HI_MoveFileStarted(logStringSource, logStringDest);
117  const Os::FileSystem::Status status =
119  sourceFileName.toChar(),
120  destFileName.toChar()
121  );
122  if (status != Os::FileSystem::OP_OK) {
124  logStringSource, logStringDest, status
125  );
126  } else {
127  this->log_ACTIVITY_HI_MoveFileSucceeded(logStringSource, logStringDest);
128  }
129  this->emitTelemetry(status);
130  this->sendCommandResponse(opCode, cmdSeq, status);
131  }
132 
133  void FileManager ::
134  RemoveDirectory_cmdHandler(
135  const FwOpcodeType opCode,
136  const U32 cmdSeq,
137  const Fw::CmdStringArg& dirName
138  )
139  {
140  Fw::LogStringArg logStringDirName(dirName.toChar());
141  this->log_ACTIVITY_HI_RemoveDirectoryStarted(logStringDirName);
142  const Os::FileSystem::Status status =
144  if (status != Os::FileSystem::OP_OK) {
146  logStringDirName,
147  status
148  );
149  } else {
150  this->log_ACTIVITY_HI_RemoveDirectorySucceeded(logStringDirName);
151  }
152  this->emitTelemetry(status);
153  this->sendCommandResponse(opCode, cmdSeq, status);
154  }
155 
156  void FileManager ::
157  ShellCommand_cmdHandler(
158  const FwOpcodeType opCode,
159  const U32 cmdSeq,
160  const Fw::CmdStringArg& command,
161  const Fw::CmdStringArg& logFileName
162  )
163  {
164  Fw::LogStringArg logStringCommand(command.toChar());
166  logStringCommand
167  );
168  NATIVE_INT_TYPE status =
169  this->systemCall(command, logFileName);
170  if (status == 0) {
172  logStringCommand
173  );
174  } else {
176  logStringCommand, static_cast<U32>(status)
177  );
178  }
179  this->emitTelemetry(
181  );
182  this->sendCommandResponse(
183  opCode,
184  cmdSeq,
186  );
187  }
188 
189  void FileManager ::
190  AppendFile_cmdHandler(
191  const FwOpcodeType opCode,
192  const U32 cmdSeq,
193  const Fw::CmdStringArg& source,
194  const Fw::CmdStringArg& target
195  )
196  {
197  Fw::LogStringArg logStringSource(source.toChar());
198  Fw::LogStringArg logStringTarget(target.toChar());
199  this->log_ACTIVITY_HI_AppendFileStarted(logStringSource, logStringTarget);
200 
201  Os::FileSystem::Status status;
202  status = Os::FileSystem::appendFile(source.toChar(), target.toChar(), true);
203  if (status != Os::FileSystem::OP_OK) {
205  logStringSource,
206  logStringTarget,
207  status
208  );
209  } else {
211  logStringSource,
212  logStringTarget
213  );
214  }
215 
216  this->emitTelemetry(status);
217  this->sendCommandResponse(opCode, cmdSeq, status);
218  }
219 
220  void FileManager ::
221  FileSize_cmdHandler(
222  const FwOpcodeType opCode,
223  const U32 cmdSeq,
224  const Fw::CmdStringArg& fileName
225  )
226  {
227  Fw::LogStringArg logStringFileName(fileName.toChar());
228  this->log_ACTIVITY_HI_FileSizeStarted(logStringFileName);
229 
230  FwSignedSizeType size_arg;
231  const Os::FileSystem::Status status =
232  Os::FileSystem::getFileSize(fileName.toChar(), size_arg);
233  if (status != Os::FileSystem::OP_OK) {
235  logStringFileName,
236  status
237  );
238  } else {
239  U64 size = static_cast<U64>(size_arg);
240  this->log_ACTIVITY_HI_FileSizeSucceeded(logStringFileName, size);
241  }
242  this->emitTelemetry(status);
243  this->sendCommandResponse(opCode, cmdSeq, status);
244  }
245 
246  void FileManager ::
247  pingIn_handler(
248  const NATIVE_INT_TYPE portNum,
249  U32 key
250  )
251  {
252  // return key
253  this->pingOut_out(0,key);
254  }
255  // ----------------------------------------------------------------------
256  // Helper methods
257  // ----------------------------------------------------------------------
258 
259  NATIVE_INT_TYPE FileManager ::
260  systemCall(
261  const Fw::CmdStringArg& command,
262  const Fw::CmdStringArg& logFileName
263  ) const
264  {
265  // Create a buffer of at least enough size for storing the eval string less the 2 %s tokens, two command strings,
266  // and a null terminator at the end
267  const char evalStr[] = "eval '%s' 1>>%s 2>&1\n";
268  constexpr U32 bufferSize = (sizeof(evalStr) - 4) + (2 * FW_CMD_STRING_MAX_SIZE) + 1;
269  char buffer[bufferSize];
270 
271  // Wrap that buffer in an external string for formatting purposes
272  Fw::ExternalString stringBuffer(buffer, bufferSize);
273  Fw::FormatStatus formatStatus = stringBuffer.format(evalStr, command.toChar(), logFileName.toChar());
274  // Since the buffer is exactly sized, the only error can occur is a software error not caused by ground
275  FW_ASSERT(formatStatus == Fw::FormatStatus::SUCCESS);
276 
277  // Call the system
278  const int status = system(stringBuffer.toChar());
279  return status;
280  }
281 
282  void FileManager ::
283  emitTelemetry(const Os::FileSystem::Status status)
284  {
285  if (status == Os::FileSystem::OP_OK) {
286  ++this->commandCount;
287  this->tlmWrite_CommandsExecuted(this->commandCount);
288  }
289  else {
290  ++this->errorCount;
291  this->tlmWrite_Errors(this->errorCount);
292  }
293  }
294 
295  void FileManager ::
296  sendCommandResponse(
297  const FwOpcodeType opCode,
298  const U32 cmdSeq,
299  const Os::FileSystem::Status status
300  )
301  {
302  this->cmdResponse_out(
303  opCode,
304  cmdSeq,
305  (status == Os::FileSystem::OP_OK) ?
307  );
308  }
309 
310 }
void log_ACTIVITY_HI_FileSizeSucceeded(const Fw::StringBase &fileName, U64 size) const
void log_ACTIVITY_HI_CreateDirectoryStarted(const Fw::StringBase &dirName) const
void log_ACTIVITY_HI_RemoveDirectorySucceeded(const Fw::StringBase &dirName) const
static Status moveFile(const char *sourcePath, const char *destPath)
Move a file from sourcePath to destPath.
Definition: FileSystem.cpp:209
void log_WARNING_HI_FileSizeError(const Fw::StringBase &fileName, U32 status) const
FileManager(const char *const compName)
Definition: FileManager.cpp:28
PlatformIntType NATIVE_INT_TYPE
Definition: BasicTypes.h:55
static Status removeDirectory(const char *path)
Remove a directory at the specified path.
Definition: FileSystem.cpp:77
static Status appendFile(const char *sourcePath, const char *destPath, bool createMissingDest=false)
Append the source file to the destination file.
Definition: FileSystem.cpp:178
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
U32 FwOpcodeType
Definition: FpConfig.h:91
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.
C++-compatible configuration header for fprime configuration.
void log_ACTIVITY_HI_MoveFileSucceeded(const Fw::StringBase &sourceFileName, const Fw::StringBase &destFileName) const
static Status createDirectory(const char *path, bool errorIfAlreadyExists=false)
Create a new directory at the specified path.
Definition: FileSystem.cpp:106
void log_WARNING_HI_DirectoryRemoveError(const Fw::StringBase &dirName, U32 status) const
Command had execution error.
other OS-specific error
Definition: FileSystem.hpp:41
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
void log_WARNING_HI_AppendFileFailed(const Fw::StringBase &source, const Fw::StringBase &target, U32 status) const
PlatformSignedSizeType FwSignedSizeType
Definition: FpConfig.h:30
static Status getFileSize(const char *path, FwSignedSizeType &size)
Get the size of the file (in bytes) at the specified path.
Definition: FileSystem.cpp:227
Operation was successful.
Definition: FileSystem.hpp:25
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:81
void log_WARNING_HI_FileRemoveError(const Fw::StringBase &fileName, U32 status) const
#define FW_CMD_STRING_MAX_SIZE
Max character size of command string arguments.
Definition: FpConfig.h:298
FormatStatus
status of string format calls
Definition: format.hpp:18
#define U64(C)
Definition: sha.h:180