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 
38  init(
39  const NATIVE_INT_TYPE queueDepth,
40  const NATIVE_INT_TYPE instance
41  )
42  {
43  FileManagerComponentBase::init(queueDepth, instance);
44  }
45 
48  {
49 
50  }
51 
52  // ----------------------------------------------------------------------
53  // Command handler implementations
54  // ----------------------------------------------------------------------
55 
56  void FileManager ::
57  CreateDirectory_cmdHandler(
58  const FwOpcodeType opCode,
59  const U32 cmdSeq,
60  const Fw::CmdStringArg& dirName
61  )
62  {
63  Fw::LogStringArg logStringDirName(dirName.toChar());
64  this->log_ACTIVITY_HI_CreateDirectoryStarted(logStringDirName);
65  const Os::FileSystem::Status status =
67  if (status != Os::FileSystem::OP_OK) {
69  logStringDirName,
70  status
71  );
72  } else {
73  this->log_ACTIVITY_HI_CreateDirectorySucceeded(logStringDirName);
74  }
75  this->emitTelemetry(status);
76  this->sendCommandResponse(opCode, cmdSeq, status);
77  }
78 
79  void FileManager ::
80  RemoveFile_cmdHandler(
81  const FwOpcodeType opCode,
82  const U32 cmdSeq,
83  const Fw::CmdStringArg& fileName,
84  const bool ignoreErrors
85  )
86  {
87  Fw::LogStringArg logStringFileName(fileName.toChar());
88  this->log_ACTIVITY_HI_RemoveFileStarted(logStringFileName);
89  const Os::FileSystem::Status status =
91  if (status != Os::FileSystem::OP_OK) {
93  logStringFileName,
94  status
95  );
96  if (ignoreErrors == true) {
97  ++this->errorCount;
98  this->tlmWrite_Errors(this->errorCount);
99  this->cmdResponse_out(
100  opCode,
101  cmdSeq,
103  );
104  return;
105  }
106  } else {
107  this->log_ACTIVITY_HI_RemoveFileSucceeded(logStringFileName);
108  }
109  this->emitTelemetry(status);
110  this->sendCommandResponse(opCode, cmdSeq, status);
111  }
112 
113  void FileManager ::
114  MoveFile_cmdHandler(
115  const FwOpcodeType opCode,
116  const U32 cmdSeq,
117  const Fw::CmdStringArg& sourceFileName,
118  const Fw::CmdStringArg& destFileName
119  )
120  {
121  Fw::LogStringArg logStringSource(sourceFileName.toChar());
122  Fw::LogStringArg logStringDest(destFileName.toChar());
123  this->log_ACTIVITY_HI_MoveFileStarted(logStringSource, logStringDest);
124  const Os::FileSystem::Status status =
126  sourceFileName.toChar(),
127  destFileName.toChar()
128  );
129  if (status != Os::FileSystem::OP_OK) {
131  logStringSource, logStringDest, status
132  );
133  } else {
134  this->log_ACTIVITY_HI_MoveFileSucceeded(logStringSource, logStringDest);
135  }
136  this->emitTelemetry(status);
137  this->sendCommandResponse(opCode, cmdSeq, status);
138  }
139 
140  void FileManager ::
141  RemoveDirectory_cmdHandler(
142  const FwOpcodeType opCode,
143  const U32 cmdSeq,
144  const Fw::CmdStringArg& dirName
145  )
146  {
147  Fw::LogStringArg logStringDirName(dirName.toChar());
148  this->log_ACTIVITY_HI_RemoveDirectoryStarted(logStringDirName);
149  const Os::FileSystem::Status status =
151  if (status != Os::FileSystem::OP_OK) {
153  logStringDirName,
154  status
155  );
156  } else {
157  this->log_ACTIVITY_HI_RemoveDirectorySucceeded(logStringDirName);
158  }
159  this->emitTelemetry(status);
160  this->sendCommandResponse(opCode, cmdSeq, status);
161  }
162 
163  void FileManager ::
164  ShellCommand_cmdHandler(
165  const FwOpcodeType opCode,
166  const U32 cmdSeq,
167  const Fw::CmdStringArg& command,
168  const Fw::CmdStringArg& logFileName
169  )
170  {
171  Fw::LogStringArg logStringCommand(command.toChar());
173  logStringCommand
174  );
175  NATIVE_INT_TYPE status =
176  this->systemCall(command, logFileName);
177  if (status == 0) {
179  logStringCommand
180  );
181  } else {
183  logStringCommand, static_cast<U32>(status)
184  );
185  }
186  this->emitTelemetry(
188  );
189  this->sendCommandResponse(
190  opCode,
191  cmdSeq,
193  );
194  }
195 
196  void FileManager ::
197  AppendFile_cmdHandler(
198  const FwOpcodeType opCode,
199  const U32 cmdSeq,
200  const Fw::CmdStringArg& source,
201  const Fw::CmdStringArg& target
202  )
203  {
204  Fw::LogStringArg logStringSource(source.toChar());
205  Fw::LogStringArg logStringTarget(target.toChar());
206  this->log_ACTIVITY_HI_AppendFileStarted(logStringSource, logStringTarget);
207 
208  Os::FileSystem::Status status;
209  status = Os::FileSystem::appendFile(source.toChar(), target.toChar(), true);
210  if (status != Os::FileSystem::OP_OK) {
212  logStringSource,
213  logStringTarget,
214  status
215  );
216  } else {
218  logStringSource,
219  logStringTarget
220  );
221  }
222 
223  this->emitTelemetry(status);
224  this->sendCommandResponse(opCode, cmdSeq, status);
225  }
226 
227  void FileManager ::
228  FileSize_cmdHandler(
229  const FwOpcodeType opCode,
230  const U32 cmdSeq,
231  const Fw::CmdStringArg& fileName
232  )
233  {
234  Fw::LogStringArg logStringFileName(fileName.toChar());
235  this->log_ACTIVITY_HI_FileSizeStarted(logStringFileName);
236 
237  FwSignedSizeType size_arg;
238  const Os::FileSystem::Status status =
239  Os::FileSystem::getFileSize(fileName.toChar(), size_arg);
240  if (status != Os::FileSystem::OP_OK) {
242  logStringFileName,
243  status
244  );
245  } else {
246  U64 size = static_cast<U64>(size_arg);
247  this->log_ACTIVITY_HI_FileSizeSucceeded(logStringFileName, size);
248  }
249  this->emitTelemetry(status);
250  this->sendCommandResponse(opCode, cmdSeq, status);
251  }
252 
253  void FileManager ::
254  pingIn_handler(
255  const NATIVE_INT_TYPE portNum,
256  U32 key
257  )
258  {
259  // return key
260  this->pingOut_out(0,key);
261  }
262  // ----------------------------------------------------------------------
263  // Helper methods
264  // ----------------------------------------------------------------------
265 
266  NATIVE_INT_TYPE FileManager ::
267  systemCall(
268  const Fw::CmdStringArg& command,
269  const Fw::CmdStringArg& logFileName
270  ) const
271  {
272  const char evalStr[] = "eval '%s' 1>>%s 2>&1\n";
273  const U32 bufferSize = sizeof(evalStr) - 4 + 2 * FW_CMD_STRING_MAX_SIZE;
274  char buffer[bufferSize];
275 
276  NATIVE_INT_TYPE bytesCopied = snprintf(
277  buffer, sizeof(buffer), evalStr,
278  command.toChar(),
279  logFileName.toChar()
280  );
281  FW_ASSERT(static_cast<NATIVE_UINT_TYPE>(bytesCopied) < sizeof(buffer));
282 
283  const int status = system(buffer);
284  return status;
285  }
286 
287  void FileManager ::
288  emitTelemetry(const Os::FileSystem::Status status)
289  {
290  if (status == Os::FileSystem::OP_OK) {
291  ++this->commandCount;
292  this->tlmWrite_CommandsExecuted(this->commandCount);
293  }
294  else {
295  ++this->errorCount;
296  this->tlmWrite_Errors(this->errorCount);
297  }
298  }
299 
300  void FileManager ::
301  sendCommandResponse(
302  const FwOpcodeType opCode,
303  const U32 cmdSeq,
304  const Os::FileSystem::Status status
305  )
306  {
307  this->cmdResponse_out(
308  opCode,
309  cmdSeq,
310  (status == Os::FileSystem::OP_OK) ?
312  );
313  }
314 
315 }
#define FW_ASSERT(...)
Definition: Assert.hpp:14
PlatformIntType NATIVE_INT_TYPE
Definition: BasicTypes.h:51
PlatformUIntType NATIVE_UINT_TYPE
Definition: BasicTypes.h:52
PlatformSignedSizeType FwSignedSizeType
Definition: FpConfig.h:25
#define FW_CMD_STRING_MAX_SIZE
Max character size of command string arguments.
Definition: FpConfig.h:279
U32 FwOpcodeType
Definition: FpConfig.h:78
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
void init()
Object initializer.
Definition: ObjBase.cpp:27
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
Status createDirectory(const char *path)
create a new directory at location path
Definition: FileSystem.cpp:8
Status moveFile(const char *originPath, const char *destPath)
Definition: FileSystem.cpp:25
Status getFileSize(const char *path, FwSizeType &size)
Definition: FileSystem.cpp:38
Status appendFile(const char *originPath, const char *destPath, bool createMissingDest)
copies a file from origin to destination
Definition: FileSystem.cpp:49
@ OP_OK
Operation was successful.
Definition: FileSystem.hpp:15
@ OTHER_ERROR
other OS-specific error
Definition: FileSystem.hpp:25
Status removeDirectory(const char *path)
remove a directory at location path
Definition: FileSystem.cpp:12
Status removeFile(const char *path)
removes a file at location path
Definition: FileSystem.cpp:21
#define U64(C)
Definition: sha.h:176