F´ Flight Software - C/C++ Documentation  devel
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 
17 #include "Fw/Types/Assert.hpp"
18 #include <FpConfig.hpp>
19 
20 namespace Svc {
21 
22  // ----------------------------------------------------------------------
23  // Construction, initialization, and destruction
24  // ----------------------------------------------------------------------
25 
28  const char *const compName
29  ) :
30  FileManagerComponentBase(compName),
31  commandCount(0),
32  errorCount(0)
33  {
34 
35  }
36 
39  {
40 
41  }
42 
43  // ----------------------------------------------------------------------
44  // Command handler implementations
45  // ----------------------------------------------------------------------
46 
47  void FileManager ::
48  CreateDirectory_cmdHandler(
49  const FwOpcodeType opCode,
50  const U32 cmdSeq,
51  const Fw::CmdStringArg& dirName
52  )
53  {
54  Fw::LogStringArg logStringDirName(dirName.toChar());
55  this->log_ACTIVITY_HI_CreateDirectoryStarted(logStringDirName);
56  bool errorIfDirExists = true;
57  const Os::FileSystem::Status status =
58  Os::FileSystem::createDirectory(dirName.toChar(), errorIfDirExists);
59  if (status != Os::FileSystem::OP_OK) {
61  logStringDirName,
62  status
63  );
64  } else {
65  this->log_ACTIVITY_HI_CreateDirectorySucceeded(logStringDirName);
66  }
67  this->emitTelemetry(status);
68  this->sendCommandResponse(opCode, cmdSeq, status);
69  }
70 
71  void FileManager ::
72  RemoveFile_cmdHandler(
73  const FwOpcodeType opCode,
74  const U32 cmdSeq,
75  const Fw::CmdStringArg& fileName,
76  const bool ignoreErrors
77  )
78  {
79  Fw::LogStringArg logStringFileName(fileName.toChar());
80  this->log_ACTIVITY_HI_RemoveFileStarted(logStringFileName);
81  const Os::FileSystem::Status status =
83  if (status != Os::FileSystem::OP_OK) {
85  logStringFileName,
86  status
87  );
88  if (ignoreErrors == true) {
89  ++this->errorCount;
90  this->tlmWrite_Errors(this->errorCount);
91  this->cmdResponse_out(
92  opCode,
93  cmdSeq,
95  );
96  return;
97  }
98  } else {
99  this->log_ACTIVITY_HI_RemoveFileSucceeded(logStringFileName);
100  }
101  this->emitTelemetry(status);
102  this->sendCommandResponse(opCode, cmdSeq, status);
103  }
104 
105  void FileManager ::
106  MoveFile_cmdHandler(
107  const FwOpcodeType opCode,
108  const U32 cmdSeq,
109  const Fw::CmdStringArg& sourceFileName,
110  const Fw::CmdStringArg& destFileName
111  )
112  {
113  Fw::LogStringArg logStringSource(sourceFileName.toChar());
114  Fw::LogStringArg logStringDest(destFileName.toChar());
115  this->log_ACTIVITY_HI_MoveFileStarted(logStringSource, logStringDest);
116  const Os::FileSystem::Status status =
118  sourceFileName.toChar(),
119  destFileName.toChar()
120  );
121  if (status != Os::FileSystem::OP_OK) {
123  logStringSource, logStringDest, status
124  );
125  } else {
126  this->log_ACTIVITY_HI_MoveFileSucceeded(logStringSource, logStringDest);
127  }
128  this->emitTelemetry(status);
129  this->sendCommandResponse(opCode, cmdSeq, status);
130  }
131 
132  void FileManager ::
133  RemoveDirectory_cmdHandler(
134  const FwOpcodeType opCode,
135  const U32 cmdSeq,
136  const Fw::CmdStringArg& dirName
137  )
138  {
139  Fw::LogStringArg logStringDirName(dirName.toChar());
140  this->log_ACTIVITY_HI_RemoveDirectoryStarted(logStringDirName);
141  const Os::FileSystem::Status status =
143  if (status != Os::FileSystem::OP_OK) {
145  logStringDirName,
146  status
147  );
148  } else {
149  this->log_ACTIVITY_HI_RemoveDirectorySucceeded(logStringDirName);
150  }
151  this->emitTelemetry(status);
152  this->sendCommandResponse(opCode, cmdSeq, status);
153  }
154 
155  void FileManager ::
156  ShellCommand_cmdHandler(
157  const FwOpcodeType opCode,
158  const U32 cmdSeq,
159  const Fw::CmdStringArg& command,
160  const Fw::CmdStringArg& logFileName
161  )
162  {
163  Fw::LogStringArg logStringCommand(command.toChar());
165  logStringCommand
166  );
167  NATIVE_INT_TYPE status =
168  this->systemCall(command, logFileName);
169  if (status == 0) {
171  logStringCommand
172  );
173  } else {
175  logStringCommand, static_cast<U32>(status)
176  );
177  }
178  this->emitTelemetry(
180  );
181  this->sendCommandResponse(
182  opCode,
183  cmdSeq,
185  );
186  }
187 
188  void FileManager ::
189  AppendFile_cmdHandler(
190  const FwOpcodeType opCode,
191  const U32 cmdSeq,
192  const Fw::CmdStringArg& source,
193  const Fw::CmdStringArg& target
194  )
195  {
196  Fw::LogStringArg logStringSource(source.toChar());
197  Fw::LogStringArg logStringTarget(target.toChar());
198  this->log_ACTIVITY_HI_AppendFileStarted(logStringSource, logStringTarget);
199 
200  Os::FileSystem::Status status;
201  status = Os::FileSystem::appendFile(source.toChar(), target.toChar(), true);
202  if (status != Os::FileSystem::OP_OK) {
204  logStringSource,
205  logStringTarget,
206  status
207  );
208  } else {
210  logStringSource,
211  logStringTarget
212  );
213  }
214 
215  this->emitTelemetry(status);
216  this->sendCommandResponse(opCode, cmdSeq, status);
217  }
218 
219  void FileManager ::
220  FileSize_cmdHandler(
221  const FwOpcodeType opCode,
222  const U32 cmdSeq,
223  const Fw::CmdStringArg& fileName
224  )
225  {
226  Fw::LogStringArg logStringFileName(fileName.toChar());
227  this->log_ACTIVITY_HI_FileSizeStarted(logStringFileName);
228 
229  FwSignedSizeType size_arg;
230  const Os::FileSystem::Status status =
231  Os::FileSystem::getFileSize(fileName.toChar(), size_arg);
232  if (status != Os::FileSystem::OP_OK) {
234  logStringFileName,
235  status
236  );
237  } else {
238  U64 size = static_cast<U64>(size_arg);
239  this->log_ACTIVITY_HI_FileSizeSucceeded(logStringFileName, size);
240  }
241  this->emitTelemetry(status);
242  this->sendCommandResponse(opCode, cmdSeq, status);
243  }
244 
245  void FileManager ::
246  pingIn_handler(
247  const NATIVE_INT_TYPE portNum,
248  U32 key
249  )
250  {
251  // return key
252  this->pingOut_out(0,key);
253  }
254  // ----------------------------------------------------------------------
255  // Helper methods
256  // ----------------------------------------------------------------------
257 
258  NATIVE_INT_TYPE FileManager ::
259  systemCall(
260  const Fw::CmdStringArg& command,
261  const Fw::CmdStringArg& logFileName
262  ) const
263  {
264  const char evalStr[] = "eval '%s' 1>>%s 2>&1\n";
265  const U32 bufferSize = sizeof(evalStr) - 4 + 2 * FW_CMD_STRING_MAX_SIZE;
266  char buffer[bufferSize];
267 
268  NATIVE_INT_TYPE bytesCopied = snprintf(
269  buffer, sizeof(buffer), evalStr,
270  command.toChar(),
271  logFileName.toChar()
272  );
273  FW_ASSERT(static_cast<NATIVE_UINT_TYPE>(bytesCopied) < sizeof(buffer));
274 
275  const int status = system(buffer);
276  return status;
277  }
278 
279  void FileManager ::
280  emitTelemetry(const Os::FileSystem::Status status)
281  {
282  if (status == Os::FileSystem::OP_OK) {
283  ++this->commandCount;
284  this->tlmWrite_CommandsExecuted(this->commandCount);
285  }
286  else {
287  ++this->errorCount;
288  this->tlmWrite_Errors(this->errorCount);
289  }
290  }
291 
292  void FileManager ::
293  sendCommandResponse(
294  const FwOpcodeType opCode,
295  const U32 cmdSeq,
296  const Os::FileSystem::Status status
297  )
298  {
299  this->cmdResponse_out(
300  opCode,
301  cmdSeq,
302  (status == Os::FileSystem::OP_OK) ?
304  );
305  }
306 
307 }
#define FW_ASSERT(...)
Definition: Assert.hpp:14
PlatformIntType NATIVE_INT_TYPE
Definition: BasicTypes.h:55
PlatformUIntType NATIVE_UINT_TYPE
Definition: BasicTypes.h:56
PlatformSignedSizeType FwSignedSizeType
Definition: FpConfig.h:30
#define FW_CMD_STRING_MAX_SIZE
Max character size of command string arguments.
Definition: FpConfig.h:298
U32 FwOpcodeType
Definition: FpConfig.h:91
C++-compatible configuration header for fprime configuration.
@ EXECUTION_ERROR
Command had execution error.
@ OK
Command successfully executed.
const char * toChar() const
Definition: CmdString.hpp:50
static Status moveFile(const char *sourcePath, const char *destPath)
Move a file from sourcePath to destPath.
Definition: FileSystem.cpp:209
static Status appendFile(const char *sourcePath, const char *destPath, bool createMissingDest=false)
Append the source file to the destination file.
Definition: FileSystem.cpp:178
static Status removeFile(const char *path)
Remove a file at the specified path.
Definition: FileSystem.cpp:81
static Status removeDirectory(const char *path)
Remove a directory at the specified path.
Definition: FileSystem.cpp:77
static Status getFileSize(const char *path, FwSignedSizeType &size)
Get the size of the file (in bytes) at the specified path.
Definition: FileSystem.cpp:227
static Status createDirectory(const char *path, bool errorIfAlreadyExists=false)
Create a new directory at the specified path.
Definition: FileSystem.cpp:106
@ OP_OK
Operation was successful.
Definition: FileSystem.hpp:25
@ OTHER_ERROR
other OS-specific error
Definition: FileSystem.hpp:41
Auto-generated base for FileManager component.
void log_ACTIVITY_HI_MoveFileSucceeded(const Fw::StringBase &sourceFileName, const Fw::StringBase &destFileName)
void log_ACTIVITY_HI_RemoveFileStarted(const Fw::StringBase &fileName)
void pingOut_out(FwIndexType portNum, U32 key)
Invoke output port pingOut.
void log_ACTIVITY_HI_RemoveDirectorySucceeded(const Fw::StringBase &dirName)
void log_ACTIVITY_HI_MoveFileStarted(const Fw::StringBase &sourceFileName, const Fw::StringBase &destFileName)
void log_ACTIVITY_HI_RemoveFileSucceeded(const Fw::StringBase &fileName)
void log_WARNING_HI_ShellCommandFailed(const Fw::StringBase &command, U32 status)
void log_ACTIVITY_HI_ShellCommandSucceeded(const Fw::StringBase &command)
void cmdResponse_out(FwOpcodeType opCode, U32 cmdSeq, Fw::CmdResponse response)
Emit command response.
void tlmWrite_CommandsExecuted(U32 arg, Fw::Time _tlmTime=Fw::Time())
void tlmWrite_Errors(U32 arg, Fw::Time _tlmTime=Fw::Time())
void log_ACTIVITY_HI_AppendFileSucceeded(const Fw::StringBase &source, const Fw::StringBase &target)
void log_WARNING_HI_DirectoryRemoveError(const Fw::StringBase &dirName, U32 status)
void log_ACTIVITY_HI_FileSizeStarted(const Fw::StringBase &fileName)
void log_WARNING_HI_FileMoveError(const Fw::StringBase &sourceFileName, const Fw::StringBase &destFileName, U32 status)
void log_ACTIVITY_HI_RemoveDirectoryStarted(const Fw::StringBase &dirName)
void log_WARNING_HI_FileSizeError(const Fw::StringBase &fileName, U32 status)
void log_WARNING_HI_DirectoryCreateError(const Fw::StringBase &dirName, U32 status)
void log_WARNING_HI_AppendFileFailed(const Fw::StringBase &source, const Fw::StringBase &target, U32 status)
void log_ACTIVITY_HI_CreateDirectorySucceeded(const Fw::StringBase &dirName)
void log_ACTIVITY_HI_ShellCommandStarted(const Fw::StringBase &command)
void log_ACTIVITY_HI_CreateDirectoryStarted(const Fw::StringBase &dirName)
void log_ACTIVITY_HI_AppendFileStarted(const Fw::StringBase &source, const Fw::StringBase &target)
void log_WARNING_HI_FileRemoveError(const Fw::StringBase &fileName, U32 status)
void log_ACTIVITY_HI_FileSizeSucceeded(const Fw::StringBase &fileName, U64 size)
FileManager(const char *const compName)
Definition: FileManager.cpp:27
#define U64(C)
Definition: sha.h:176