F´ Flight Software - C/C++ Documentation  NASA-v1.5.0
A framework for building embedded system applications to NASA flight quality standards.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 <stdio.h>
14 #include <stdlib.h>
15 
17 #include "Fw/Types/Assert.hpp"
18 #include "Fw/Types/BasicTypes.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 
47  ~FileManager(void)
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);
68  this->log_WARNING_HI_DirectoryCreateError(
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 
81  const FwOpcodeType opCode,
82  const U32 cmdSeq,
83  const Fw::CmdStringArg& fileName
84  )
85  {
86  Fw::LogStringArg logStringFileName(fileName.toChar());
87  this->log_ACTIVITY_HI_RemoveFileStarted(logStringFileName);
92  logStringFileName,
93  status
94  );
95  } else {
96  this->log_ACTIVITY_HI_RemoveFileSucceeded(logStringFileName);
97  }
98  this->emitTelemetry(status);
99  this->sendCommandResponse(opCode, cmdSeq, status);
100  }
101 
104  const FwOpcodeType opCode,
105  const U32 cmdSeq,
106  const Fw::CmdStringArg& sourceFileName,
107  const Fw::CmdStringArg& destFileName
108  )
109  {
110  Fw::LogStringArg logStringSource(sourceFileName.toChar());
111  Fw::LogStringArg logStringDest(destFileName.toChar());
112  this->log_ACTIVITY_HI_MoveFileStarted(logStringSource, logStringDest);
115  sourceFileName.toChar(),
116  destFileName.toChar()
117  );
118  if (status != Os::FileSystem::OP_OK) {
120  logStringSource, logStringDest, status
121  );
122  } else {
123  this->log_ACTIVITY_HI_MoveFileSucceeded(logStringSource, logStringDest);
124  }
125  this->emitTelemetry(status);
126  this->sendCommandResponse(opCode, cmdSeq, status);
127  }
128 
131  const FwOpcodeType opCode,
132  const U32 cmdSeq,
133  const Fw::CmdStringArg& dirName
134  )
135  {
136  Fw::LogStringArg logStringDirName(dirName.toChar());
137  this->log_ACTIVITY_HI_RemoveDirectoryStarted(logStringDirName);
140  if (status != Os::FileSystem::OP_OK) {
142  logStringDirName,
143  status
144  );
145  } else {
146  this->log_ACTIVITY_HI_RemoveDirectorySucceeded(logStringDirName);
147  }
148  this->emitTelemetry(status);
149  this->sendCommandResponse(opCode, cmdSeq, status);
150  }
151 
154  const FwOpcodeType opCode,
155  const U32 cmdSeq,
156  const Fw::CmdStringArg& command,
157  const Fw::CmdStringArg& logFileName
158  )
159  {
160  Fw::LogStringArg logStringCommand(command.toChar());
162  logStringCommand
163  );
165  this->systemCall(command, logFileName);
166  if (status == 0) {
168  logStringCommand
169  );
170  } else {
172  logStringCommand, status
173  );
174  }
175  this->emitTelemetry(
177  );
178  this->sendCommandResponse(
179  opCode,
180  cmdSeq,
182  );
183  }
184 
187  const FwOpcodeType opCode,
188  const U32 cmdSeq,
189  const Fw::CmdStringArg& source,
190  const Fw::CmdStringArg& target
191  )
192  {
193  Fw::LogStringArg logStringSource(source.toChar());
194  Fw::LogStringArg logStringTarget(target.toChar());
195  this->log_ACTIVITY_HI_AppendFileStarted(logStringSource, logStringTarget);
196 
198  status = Os::FileSystem::appendFile(source.toChar(), target.toChar(), true);
199  if (status != Os::FileSystem::OP_OK) {
201  logStringSource,
202  logStringTarget,
203  status
204  );
205  } else {
207  logStringSource,
208  logStringTarget
209  );
210  }
211 
212  this->emitTelemetry(status);
213  this->sendCommandResponse(opCode, cmdSeq, status);
214  }
215 
218  const NATIVE_INT_TYPE portNum,
219  U32 key
220  )
221  {
222  // return key
223  this->pingOut_out(0,key);
224  }
225  // ----------------------------------------------------------------------
226  // Helper methods
227  // ----------------------------------------------------------------------
228 
229  NATIVE_INT_TYPE FileManager ::
230  systemCall(
231  const Fw::CmdStringArg& command,
232  const Fw::CmdStringArg& logFileName
233  ) const
234  {
235  const U32 bufferSize = 10 + 2 * FW_CMD_STRING_MAX_SIZE;
236  char buffer[bufferSize];
237  snprintf(
238  buffer, sizeof(buffer), "eval '%s' 1>>%s 2>&1\n",
239  command.toChar(),
240  logFileName.toChar()
241  );
242  const int status = system(buffer);
243  return status;
244  }
245 
248  {
249  if (status == Os::FileSystem::OP_OK) {
250  ++this->commandCount;
251  this->tlmWrite_CommandsExecuted(this->commandCount);
252  }
253  else {
254  ++this->errorCount;
255  this->tlmWrite_Errors(this->errorCount);
256  }
257  }
258 
261  const FwOpcodeType opCode,
262  const U32 cmdSeq,
263  const Os::FileSystem::Status status
264  )
265  {
266  this->cmdResponse_out(
267  opCode,
268  cmdSeq,
271  );
272  }
273 
274 }
Svc::FileManager::emitTelemetry
void emitTelemetry(const Os::FileSystem::Status status)
Definition: FileManager.cpp:247
Svc::FileManagerComponentBase::log_ACTIVITY_HI_AppendFileSucceeded
void log_ACTIVITY_HI_AppendFileSucceeded(Fw::LogStringArg &source, Fw::LogStringArg &target)
Definition: FileManagerComponentAc.cpp:1965
Svc::FileManager::AppendFile_cmdHandler
void AppendFile_cmdHandler(const FwOpcodeType opCode, const U32 cmdSeq, const Fw::CmdStringArg &source, const Fw::CmdStringArg &target)
Definition: FileManager.cpp:186
Svc::FileManagerComponentBase::status
PROTECTED U32 status
Definition: FileManagerComponentAc.hpp:681
Svc::FileManager::sendCommandResponse
void sendCommandResponse(const FwOpcodeType opCode, const U32 cmdSeq, const Os::FileSystem::Status status)
Definition: FileManager.cpp:260
Os::FileSystem::Status
Status
Definition: FileSystem.hpp:15
Svc::FileManagerComponentBase::log_ACTIVITY_HI_RemoveFileStarted
void log_ACTIVITY_HI_RemoveFileStarted(Fw::LogStringArg &fileName)
Definition: FileManagerComponentAc.cpp:2965
Fw::CmdStringArg::toChar
const char * toChar(void) const
Definition: CmdString.cpp:34
FW_CMD_STRING_MAX_SIZE
#define FW_CMD_STRING_MAX_SIZE
Max character size of command string arguments.
Definition: FpConfig.hpp:231
Fw::LogStringArg
Definition: LogString.hpp:11
Svc::FileManager::dirName
PRIVATE const U32 const Fw::CmdStringArg & dirName
< The directory to create
Definition: FileManager.hpp:60
Svc::FileManagerComponentBase::log_ACTIVITY_HI_RemoveFileSucceeded
void log_ACTIVITY_HI_RemoveFileSucceeded(Fw::LogStringArg &fileName)
Definition: FileManagerComponentAc.cpp:2421
Svc::FileManagerComponentBase::log_ACTIVITY_HI_CreateDirectoryStarted
void log_ACTIVITY_HI_CreateDirectoryStarted(Fw::LogStringArg &dirName)
Definition: FileManagerComponentAc.cpp:2693
Svc::FileManagerComponentBase::log_ACTIVITY_HI_MoveFileStarted
void log_ACTIVITY_HI_MoveFileStarted(Fw::LogStringArg &sourceFileName, Fw::LogStringArg &destFileName)
Definition: FileManagerComponentAc.cpp:2869
Os::FileSystem::removeDirectory
Status removeDirectory(const char *path)
remove a directory at location path
Definition: FileSystem.cpp:13
Fw::CmdStringArg
Definition: CmdString.hpp:11
Svc::FileManagerComponentBase::log_WARNING_HI_FileRemoveError
void log_WARNING_HI_FileRemoveError(Fw::LogStringArg &fileName, U32 status)
Definition: FileManagerComponentAc.cpp:1639
Assert.hpp
Os::FileSystem::OTHER_ERROR
@ OTHER_ERROR
other OS-specific error
Definition: FileSystem.hpp:26
Os::FileSystem::moveFile
Status moveFile(const char *originPath, const char *destPath)
Definition: FileSystem.cpp:27
Svc::FileManagerComponentBase::log_WARNING_HI_DirectoryRemoveError
void log_WARNING_HI_DirectoryRemoveError(Fw::LogStringArg &dirName, U32 status)
Definition: FileManagerComponentAc.cpp:1419
Svc::FileManager::ShellCommand_cmdHandler
void ShellCommand_cmdHandler(const FwOpcodeType opCode, const U32 cmdSeq, const Fw::CmdStringArg &command, const Fw::CmdStringArg &logFileName)
Definition: FileManager.cpp:153
Fw::ObjBase::init
void init(void)
Object initializer.
Definition: ObjBase.cpp:26
Svc::FileManagerComponentBase::tlmWrite_Errors
void tlmWrite_Errors(U32 arg)
Definition: FileManagerComponentAc.cpp:1264
Os::FileSystem::OP_OK
@ OP_OK
Operation was successful.
Definition: FileSystem.hpp:16
Fw::COMMAND_EXECUTION_ERROR
@ COMMAND_EXECUTION_ERROR
Definition: CmdResponsePortAc.hpp:29
Svc::FileManagerComponentBase::log_ACTIVITY_HI_RemoveDirectorySucceeded
void log_ACTIVITY_HI_RemoveDirectorySucceeded(Fw::LogStringArg &dirName)
Definition: FileManagerComponentAc.cpp:2237
Svc::FileManager::~FileManager
~FileManager(void)
Definition: FileManager.cpp:47
FwOpcodeType
#define FwOpcodeType
Type representation for a command opcode.
Definition: FpConfig.hpp:62
Svc::FileManagerComponentBase::log_WARNING_HI_ShellCommandFailed
void log_WARNING_HI_ShellCommandFailed(Fw::LogStringArg &command, U32 status)
Definition: FileManagerComponentAc.cpp:1745
Svc::FileManagerComponentBase::log_WARNING_HI_FileMoveError
void log_WARNING_HI_FileMoveError(Fw::LogStringArg &sourceFileName, Fw::LogStringArg &destFileName, U32 status)
Definition: FileManagerComponentAc.cpp:1525
Svc::FileManager::errorCount
U32 errorCount
Definition: FileManager.hpp:151
Svc::FileManagerComponentBase::log_WARNING_HI_AppendFileFailed
void log_WARNING_HI_AppendFileFailed(Fw::LogStringArg &source, Fw::LogStringArg &target, U32 status)
Definition: FileManagerComponentAc.cpp:1851
Svc::FileManagerComponentBase::log_ACTIVITY_HI_MoveFileSucceeded
void log_ACTIVITY_HI_MoveFileSucceeded(Fw::LogStringArg &sourceFileName, Fw::LogStringArg &destFileName)
Definition: FileManagerComponentAc.cpp:2325
Os::FileSystem::appendFile
Status appendFile(const char *originPath, const char *destPath, bool createMissingDest=false)
copies a file from origin to destination
Definition: FileSystem.cpp:404
Svc::FileManagerComponentBase::key
PROTECTED U32 key
Definition: FileManagerComponentAc.hpp:249
Svc::FileManager::RemoveDirectory_cmdHandler
void RemoveDirectory_cmdHandler(const FwOpcodeType opCode, const U32 cmdSeq, const Fw::CmdStringArg &dirName)
Definition: FileManager.cpp:130
Svc::FileManager::cmdSeq
PRIVATE const U32 cmdSeq
The command sequence number.
Definition: FileManager.hpp:58
Svc::FileManagerComponentBase::opCode
PRIVATE NATIVE_INT_TYPE FwOpcodeType opCode
Definition: FileManagerComponentAc.hpp:936
Svc
Definition: ActiveLoggerComponentAc.cpp:22
FileManager.hpp
Svc::FileManagerComponentBase::log_ACTIVITY_HI_ShellCommandStarted
void log_ACTIVITY_HI_ShellCommandStarted(Fw::LogStringArg &command)
Definition: FileManagerComponentAc.cpp:2605
Svc::FileManagerComponentBase::log_ACTIVITY_HI_ShellCommandSucceeded
void log_ACTIVITY_HI_ShellCommandSucceeded(Fw::LogStringArg &command)
Definition: FileManagerComponentAc.cpp:2061
Svc::FileManager::pingIn_handler
void pingIn_handler(const NATIVE_INT_TYPE portNum, U32 key)
Definition: FileManager.cpp:217
Svc::FileManager::RemoveFile_cmdHandler
void RemoveFile_cmdHandler(const FwOpcodeType opCode, const U32 cmdSeq, const Fw::CmdStringArg &fileName)
Definition: FileManager.cpp:80
Svc::FileManagerComponentBase::log_ACTIVITY_HI_AppendFileStarted
void log_ACTIVITY_HI_AppendFileStarted(Fw::LogStringArg &source, Fw::LogStringArg &target)
Definition: FileManagerComponentAc.cpp:2509
Svc::FileManagerComponentBase
Auto-generated base for FileManager component.
Definition: FileManagerComponentAc.hpp:47
Os::FileSystem::createDirectory
Status createDirectory(const char *path)
create a new directory at location path
Definition: FileSystem.cpp:9
Fw::COMMAND_OK
@ COMMAND_OK
Definition: CmdResponsePortAc.hpp:25
Svc::FileManagerComponentBase::log_ACTIVITY_HI_CreateDirectorySucceeded
void log_ACTIVITY_HI_CreateDirectorySucceeded(Fw::LogStringArg &dirName)
Definition: FileManagerComponentAc.cpp:2149
BasicTypes.hpp
Declares ISF basic types.
Svc::FileManager::MoveFile_cmdHandler
void MoveFile_cmdHandler(const FwOpcodeType opCode, const U32 cmdSeq, const Fw::CmdStringArg &sourceFileName, const Fw::CmdStringArg &destFileName)
Definition: FileManager.cpp:103
NATIVE_INT_TYPE
int NATIVE_INT_TYPE
native integer type declaration
Definition: BasicTypes.hpp:29
Svc::FileManagerComponentBase::log_ACTIVITY_HI_RemoveDirectoryStarted
void log_ACTIVITY_HI_RemoveDirectoryStarted(Fw::LogStringArg &dirName)
Definition: FileManagerComponentAc.cpp:2781
Os::FileSystem::removeFile
Status removeFile(const char *path)
removes a file at location path
Definition: FileSystem.cpp:22
Svc::FileManager::FileManager
FileManager(const char *const compName)
Definition: FileManager.cpp:27