F´ Flight Software - C/C++ Documentation devel
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 <cstdio>
14#include <cstdlib>
15
17#include "Fw/Types/Assert.hpp"
18#include <FpConfig.hpp>
19
20namespace Svc {
21
22 // ----------------------------------------------------------------------
23 // Construction, initialization, and destruction
24 // ----------------------------------------------------------------------
25
26 FileManager ::
27 FileManager(
28 const char *const compName
29 ) :
31 commandCount(0),
32 errorCount(0)
33 {
34
35 }
36
37 void FileManager ::
38 init(
39 const NATIVE_INT_TYPE queueDepth,
40 const NATIVE_INT_TYPE instance
41 )
42 {
43 FileManagerComponentBase::init(queueDepth, instance);
44 }
45
46 FileManager ::
47 ~FileManager()
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) {
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
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) {
92 this->log_WARNING_HI_FileRemoveError(
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) {
130 this->log_WARNING_HI_FileMoveError(
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) {
152 this->log_WARNING_HI_DirectoryRemoveError(
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());
172 this->log_ACTIVITY_HI_ShellCommandStarted(
173 logStringCommand
174 );
175 NATIVE_INT_TYPE status =
176 this->systemCall(command, logFileName);
177 if (status == 0) {
178 this->log_ACTIVITY_HI_ShellCommandSucceeded(
179 logStringCommand
180 );
181 } else {
182 this->log_WARNING_HI_ShellCommandFailed(
183 logStringCommand, status
184 );
185 }
186 this->emitTelemetry(
187 status == 0 ? Os::FileSystem::OP_OK : Os::FileSystem::OTHER_ERROR
188 );
189 this->sendCommandResponse(
190 opCode,
191 cmdSeq,
192 status == 0 ? Os::FileSystem::OP_OK : Os::FileSystem::OTHER_ERROR
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
209 status = Os::FileSystem::appendFile(source.toChar(), target.toChar(), true);
210 if (status != Os::FileSystem::OP_OK) {
211 this->log_WARNING_HI_AppendFileFailed(
212 logStringSource,
213 logStringTarget,
214 status
215 );
216 } else {
217 this->log_ACTIVITY_HI_AppendFileSucceeded(
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 FwSizeType size_arg;
238 const Os::FileSystem::Status status =
239 Os::FileSystem::getFileSize(fileName.toChar(), size_arg);
240 if (status != Os::FileSystem::OP_OK) {
241 this->log_WARNING_HI_FileSizeError(
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) ?
311 Fw::CmdResponse::OK : Fw::CmdResponse::EXECUTION_ERROR
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
#define FW_CMD_STRING_MAX_SIZE
Max character size of command string arguments.
Definition FpConfig.h:239
U32 FwOpcodeType
Definition FpConfig.h:56
PlatformSizeType FwSizeType
Definition FpConfig.h:18
C++-compatible configuration header for fprime configuration.
@ OK
Command successfully executed.
const char * toChar() const
Definition CmdString.cpp:48
void init()
Object initializer.
Definition ObjBase.cpp:27
Auto-generated base for FileManager component.
Status createDirectory(const char *path)
create a new directory at location path
Definition FileSystem.cpp:8
Status moveFile(const char *originPath, const char *destPath)
Status getFileSize(const char *path, FwSizeType &size)
append file origin to destination file. If boolean true, creates a brand new file if the destination ...
Status appendFile(const char *originPath, const char *destPath, bool createMissingDest)
copies a file from origin to destination
@ OP_OK
Operation was successful.
@ OTHER_ERROR
other OS-specific error
Status removeDirectory(const char *path)
remove a directory at location path
Status removeFile(const char *path)
removes a file at location path
Definition File.cpp:6
#define U64(C)
Definition sha.h:176