-
Notifications
You must be signed in to change notification settings - Fork 25
/
fm_child.c
1585 lines (1320 loc) · 59.6 KB
/
fm_child.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/************************************************************************
* NASA Docket No. GSC-18,918-1, and identified as “Core Flight
* Software System (cFS) File Manager Application Version 2.6.1”
*
* Copyright (c) 2021 United States Government as represented by the
* Administrator of the National Aeronautics and Space Administration.
* All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
************************************************************************/
/**
* @file
* File Manager (FM) Child task (low priority command handler)
*/
#include "cfe.h"
#include "fm_msg.h"
#include "fm_msgdefs.h"
#include "fm_msgids.h"
#include "fm_events.h"
#include "fm_app.h"
#include "fm_child.h"
#include "fm_cmds.h"
#include "fm_cmd_utils.h"
#include "fm_perfids.h"
#include "fm_platform_cfg.h"
#include "fm_verify.h"
#include <string.h>
#ifdef FM_INCLUDE_DECOMPRESS
#include "cfs_fs_lib.h"
#endif
/************************************************************************
** OSAL Compatibility for directory name access
** New OSAL version have an access macro to get the string. If that
** macro is defined, use it, otherwise assume "d_name" structure member.
*************************************************************************/
#ifndef OS_DIRENTRY_NAME
#define OS_DIRENTRY_NAME(x) ((x).d_name)
#endif
#define FM_QUEUE_SEM_NAME "FM_QUEUE_SEM"
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* */
/* FM child task -- startup initialization */
/* */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
int32 FM_ChildInit(void)
{
int32 TaskTextLen = OS_MAX_PATH_LEN;
char TaskText[OS_MAX_PATH_LEN] = "\0";
int32 Result = CFE_SUCCESS;
uint32 TaskEID = 0;
/* Create counting semaphore (given by parent to wake-up child) */
Result = OS_CountSemCreate(&FM_GlobalData.ChildSemaphore, FM_CHILD_SEM_NAME, 0, 0);
if (Result != CFE_SUCCESS)
{
TaskEID = FM_CHILD_INIT_SEM_ERR_EID;
strncpy(TaskText, "create semaphore failed", TaskTextLen - 1);
TaskText[TaskTextLen - 1] = '\0';
}
else
{
/* Create mutex semaphore (protect access to ChildQueueCount) */
Result = OS_MutSemCreate(&FM_GlobalData.ChildQueueCountSem, FM_QUEUE_SEM_NAME, 0);
if (Result != CFE_SUCCESS)
{
TaskEID = FM_CHILD_INIT_QSEM_ERR_EID;
strncpy(TaskText, "create queue count semaphore failed", TaskTextLen - 1);
TaskText[TaskTextLen - 1] = '\0';
}
else
{
/* Create child task (low priority command handler) */
Result = CFE_ES_CreateChildTask(&FM_GlobalData.ChildTaskID, FM_CHILD_TASK_NAME, FM_ChildTask, 0,
FM_CHILD_TASK_STACK_SIZE, FM_CHILD_TASK_PRIORITY, 0);
if (Result != CFE_SUCCESS)
{
TaskEID = FM_CHILD_INIT_CREATE_ERR_EID;
strncpy(TaskText, "create task failed", TaskTextLen - 1);
TaskText[TaskTextLen - 1] = '\0';
}
}
}
if (Result != CFE_SUCCESS)
{
CFE_EVS_SendEvent(TaskEID, CFE_EVS_EventType_ERROR, "Child Task initialization error: %s: result = %d",
TaskText, (int)Result);
}
return Result;
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* */
/* FM child task -- task entry point */
/* */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void FM_ChildTask(void)
{
const char *TaskText = "Child Task";
/*
** The child task runs until the parent dies (normal end) or
** until it encounters a fatal error (semaphore error, etc.)...
*/
CFE_EVS_SendEvent(FM_CHILD_INIT_EID, CFE_EVS_EventType_INFORMATION, "%s initialization complete", TaskText);
/* Child task process loop */
FM_ChildLoop();
/* Clear the semaphore ID */
FM_GlobalData.ChildSemaphore = OS_OBJECT_ID_UNDEFINED;
/* This call allows cFE to clean-up system resources */
CFE_ES_ExitChildTask();
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* */
/* FM child task -- main process loop */
/* */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void FM_ChildLoop(void)
{
const char *TaskText = "Child Task termination error: ";
int32 Result = CFE_SUCCESS;
while (Result == CFE_SUCCESS)
{
/* Pend on the "handshake" semaphore */
Result = OS_CountSemTake(FM_GlobalData.ChildSemaphore);
/* Mark the period when this task is active */
CFE_ES_PerfLogEntry(FM_CHILD_TASK_PERF_ID);
if (Result == CFE_SUCCESS)
{
/* Make sure the parent/child handshake is not broken */
if (FM_GlobalData.ChildQueueCount == 0)
{
FM_GlobalData.ChildCmdErrCounter++;
CFE_EVS_SendEvent(FM_CHILD_TERM_EMPTYQ_ERR_EID, CFE_EVS_EventType_ERROR, "%s empty queue", TaskText);
/* Set result that will terminate child task run loop */
Result = OS_ERROR;
}
else if (FM_GlobalData.ChildReadIndex >= FM_CHILD_QUEUE_DEPTH)
{
FM_GlobalData.ChildCmdErrCounter++;
CFE_EVS_SendEvent(FM_CHILD_TERM_QIDX_ERR_EID, CFE_EVS_EventType_ERROR,
"%s invalid queue index: index = %d", TaskText, (int)FM_GlobalData.ChildReadIndex);
/* Set result that will terminate child task run loop */
Result = OS_ERROR;
}
else
{
/* Invoke the child task command handler */
FM_ChildProcess();
}
}
else
{
CFE_EVS_SendEvent(FM_CHILD_TERM_SEM_ERR_EID, CFE_EVS_EventType_ERROR,
"%s semaphore take failed: result = %d", TaskText, (int)Result);
}
CFE_ES_PerfLogExit(FM_CHILD_TASK_PERF_ID);
}
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* */
/* FM child task -- interface handshake processor */
/* */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void FM_ChildProcess(void)
{
const char * TaskText = "Child Task";
FM_ChildQueueEntry_t *CmdArgs = &FM_GlobalData.ChildQueue[FM_GlobalData.ChildReadIndex];
/* Invoke the command specific handler */
switch (CmdArgs->CommandCode)
{
case FM_COPY_CC:
FM_ChildCopyCmd(CmdArgs);
break;
case FM_MOVE_CC:
FM_ChildMoveCmd(CmdArgs);
break;
case FM_RENAME_CC:
FM_ChildRenameCmd(CmdArgs);
break;
case FM_DELETE_CC:
FM_ChildDeleteCmd(CmdArgs);
break;
case FM_DELETE_ALL_CC:
FM_ChildDeleteAllCmd(CmdArgs);
break;
#ifdef FM_INCLUDE_DECOMPRESS
case FM_DECOMPRESS_CC:
FM_ChildDecompressCmd(CmdArgs);
break;
#endif
case FM_CONCAT_CC:
FM_ChildConcatCmd(CmdArgs);
break;
case FM_CREATE_DIR_CC:
FM_ChildCreateDirCmd(CmdArgs);
break;
case FM_DELETE_DIR_CC:
FM_ChildDeleteDirCmd(CmdArgs);
break;
case FM_GET_FILE_INFO_CC:
FM_ChildFileInfoCmd(CmdArgs);
break;
case FM_GET_DIR_FILE_CC:
FM_ChildDirListFileCmd(CmdArgs);
break;
case FM_GET_DIR_PKT_CC:
FM_ChildDirListPktCmd(CmdArgs);
break;
case FM_SET_FILE_PERM_CC:
FM_ChildSetPermissionsCmd(CmdArgs);
break;
default:
FM_GlobalData.ChildCmdErrCounter++;
CFE_EVS_SendEvent(FM_CHILD_EXE_ERR_EID, CFE_EVS_EventType_ERROR,
"%s execution error: invalid command code: cc = %d", TaskText, (int)CmdArgs->CommandCode);
break;
}
/* Update the handshake queue read index */
FM_GlobalData.ChildReadIndex++;
if (FM_GlobalData.ChildReadIndex >= FM_CHILD_QUEUE_DEPTH)
{
FM_GlobalData.ChildReadIndex = 0;
}
/* Prevent parent/child updating queue counter at same time */
OS_MutSemTake(FM_GlobalData.ChildQueueCountSem);
FM_GlobalData.ChildQueueCount--;
OS_MutSemGive(FM_GlobalData.ChildQueueCountSem);
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* */
/* FM child task command handler -- Copy File */
/* */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void FM_ChildCopyCmd(const FM_ChildQueueEntry_t *CmdArgs)
{
const char *CmdText = "Copy File";
int32 OS_Status = OS_SUCCESS;
/* Report current child task activity */
FM_GlobalData.ChildCurrentCC = CmdArgs->CommandCode;
/* Note the order of the arguments to OS_cp (src,tgt) */
OS_Status = OS_cp(CmdArgs->Source1, CmdArgs->Target);
if (OS_Status != OS_SUCCESS)
{
FM_GlobalData.ChildCmdErrCounter++;
/* Send command failure event (error) */
CFE_EVS_SendEvent(FM_COPY_OS_ERR_EID, CFE_EVS_EventType_ERROR,
"%s error: OS_cp failed: result = %d, src = %s, tgt = %s", CmdText, (int)OS_Status,
CmdArgs->Source1, CmdArgs->Target);
}
else
{
FM_GlobalData.ChildCmdCounter++;
/* Send command completion event (info) */
CFE_EVS_SendEvent(FM_COPY_CMD_EID, CFE_EVS_EventType_DEBUG, "%s command: src = %s, tgt = %s", CmdText,
CmdArgs->Source1, CmdArgs->Target);
}
/* Report previous child task activity */
FM_GlobalData.ChildPreviousCC = CmdArgs->CommandCode;
FM_GlobalData.ChildCurrentCC = 0;
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* */
/* FM child task command handler -- Move File */
/* */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void FM_ChildMoveCmd(const FM_ChildQueueEntry_t *CmdArgs)
{
const char *CmdText = "Move File";
int32 OS_Status = OS_SUCCESS;
/* Report current child task activity */
FM_GlobalData.ChildCurrentCC = CmdArgs->CommandCode;
OS_Status = OS_mv(CmdArgs->Source1, CmdArgs->Target);
if (OS_Status != OS_SUCCESS)
{
FM_GlobalData.ChildCmdErrCounter++;
/* Send command failure event (error) */
CFE_EVS_SendEvent(FM_MOVE_OS_ERR_EID, CFE_EVS_EventType_ERROR,
"%s error: OS_mv failed: result = %d, src = %s, tgt = %s", CmdText, (int)OS_Status,
CmdArgs->Source1, CmdArgs->Target);
}
else
{
FM_GlobalData.ChildCmdCounter++;
/* Send command completion event (info) */
CFE_EVS_SendEvent(FM_MOVE_CMD_EID, CFE_EVS_EventType_DEBUG, "%s command: src = %s, tgt = %s", CmdText,
CmdArgs->Source1, CmdArgs->Target);
}
/* Report previous child task activity */
FM_GlobalData.ChildPreviousCC = CmdArgs->CommandCode;
FM_GlobalData.ChildCurrentCC = 0;
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* */
/* FM child task command handler -- Rename File */
/* */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void FM_ChildRenameCmd(const FM_ChildQueueEntry_t *CmdArgs)
{
const char *CmdText = "Rename File";
int32 OS_Status = OS_SUCCESS;
/* Report current child task activity */
FM_GlobalData.ChildCurrentCC = CmdArgs->CommandCode;
OS_Status = OS_rename(CmdArgs->Source1, CmdArgs->Target);
if (OS_Status != OS_SUCCESS)
{
FM_GlobalData.ChildCmdErrCounter++;
/* Send command failure event (error) */
CFE_EVS_SendEvent(FM_RENAME_OS_ERR_EID, CFE_EVS_EventType_ERROR,
"%s error: OS_rename failed: result = %d, src = %s, tgt = %s", CmdText, (int)OS_Status,
CmdArgs->Source1, CmdArgs->Target);
}
else
{
FM_GlobalData.ChildCmdCounter++;
/* Send command completion event (info) */
CFE_EVS_SendEvent(FM_RENAME_CMD_EID, CFE_EVS_EventType_DEBUG, "%s command: src = %s, tgt = %s", CmdText,
CmdArgs->Source1, CmdArgs->Target);
}
/* Report previous child task activity */
FM_GlobalData.ChildPreviousCC = CmdArgs->CommandCode;
FM_GlobalData.ChildCurrentCC = 0;
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* */
/* FM child task command handler -- Delete File */
/* */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void FM_ChildDeleteCmd(const FM_ChildQueueEntry_t *CmdArgs)
{
const char *CmdText = "Delete File";
int32 OS_Status = OS_SUCCESS;
/* Report current child task activity */
FM_GlobalData.ChildCurrentCC = CmdArgs->CommandCode;
OS_Status = OS_remove(CmdArgs->Source1);
if (OS_Status != OS_SUCCESS)
{
FM_GlobalData.ChildCmdErrCounter++;
/* Send command failure event (error) */
CFE_EVS_SendEvent(FM_DELETE_OS_ERR_EID, CFE_EVS_EventType_ERROR,
"%s error: OS_remove failed: result = %d, file = %s", CmdText, (int)OS_Status,
CmdArgs->Source1);
}
else
{
FM_GlobalData.ChildCmdCounter++;
/* Send command completion event (info) */
CFE_EVS_SendEvent(FM_DELETE_CMD_EID, CFE_EVS_EventType_DEBUG, "%s command: file = %s", CmdText,
CmdArgs->Source1);
}
/* Report previous child task activity */
FM_GlobalData.ChildPreviousCC = CmdArgs->CommandCode;
FM_GlobalData.ChildCurrentCC = 0;
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* */
/* FM child task command handler -- Delete All Files */
/* */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void FM_ChildDeleteAllCmd(FM_ChildQueueEntry_t *CmdArgs)
{
const char *CmdText = "Delete All Files";
osal_id_t DirId = OS_OBJECT_ID_UNDEFINED;
os_dirent_t DirEntry;
int32 OS_Status = OS_SUCCESS;
uint32 FilenameState = FM_NAME_IS_INVALID;
uint32 NameLength = 0;
uint32 DeleteCount = 0;
uint32 FilesNotDeletedCount = 0;
uint32 DirectoriesSkippedCount = 0;
char Filename[2 * OS_MAX_PATH_LEN] = "";
/*
** Command argument useage for this command:
**
** CmdArgs->CommandCode = FM_DELETE_ALL_CC
** CmdArgs->Source1 = directory name
** CmdArgs->Source2 = directory name plus separator
*/
char *Directory = CmdArgs->Source1;
char *DirWithSep = CmdArgs->Source2;
memset(&DirEntry, 0, sizeof(DirEntry));
/* Report current child task activity */
FM_GlobalData.ChildCurrentCC = CmdArgs->CommandCode;
/* Open directory so that we can read from it */
OS_Status = OS_DirectoryOpen(&DirId, Directory);
if (OS_Status != OS_SUCCESS)
{
FM_GlobalData.ChildCmdErrCounter++;
/* Send command failure event (error) */
CFE_EVS_SendEvent(FM_DELETE_ALL_OS_ERR_EID, CFE_EVS_EventType_ERROR,
"%s error: OS_DirectoryOpen failed: dir = %s", CmdText, Directory);
}
else
{
/* Read each directory entry and delete the files */
while (OS_DirectoryRead(DirId, &DirEntry) == OS_SUCCESS)
{
/*
** Ignore the "." and ".." directory entries
*/
if ((strcmp(OS_DIRENTRY_NAME(DirEntry), FM_THIS_DIRECTORY) != 0) &&
(strcmp(OS_DIRENTRY_NAME(DirEntry), FM_PARENT_DIRECTORY) != 0))
{
/* Construct full path filename */
NameLength = snprintf(Filename, sizeof(Filename), "%s%s", DirWithSep, OS_DIRENTRY_NAME(DirEntry));
if (NameLength >= OS_MAX_PATH_LEN)
{
FilesNotDeletedCount++;
}
else
{
/* What kind of directory entry is this? */
FilenameState = FM_GetFilenameState(Filename, OS_MAX_PATH_LEN, false);
/* FilenameState cannot have a value beyond five macros in cases below */
switch (FilenameState)
{
case FM_NAME_IS_INVALID:
FilesNotDeletedCount++;
break;
case FM_NAME_IS_NOT_IN_USE:
/* This result is very unlikely - the */
/* name existed a moment ago when */
/* the directory entry was read but */
/* now the call to OS_stat() failed */
/* implying that the entry is gone */
FilesNotDeletedCount++;
break;
case FM_NAME_IS_DIRECTORY:
DirectoriesSkippedCount++;
break;
case FM_NAME_IS_FILE_OPEN:
FilesNotDeletedCount++;
break;
case FM_NAME_IS_FILE_CLOSED:
if ((OS_Status = OS_remove(Filename)) == OS_SUCCESS)
{
/*
** After deleting the file, rewind the directory
** to keep the file system from getting confused
*/
OS_DirectoryRewind(DirId);
/* Increment delete count */
DeleteCount++;
}
else
{
FilesNotDeletedCount++;
}
break;
default:
FilesNotDeletedCount++;
break;
} /* end switch statement */
}
} /* end if "." or ".." directory entries */
} /* End while OS_ReadDirectory */
OS_DirectoryClose(DirId);
/* Send command completion event (info) */
CFE_EVS_SendEvent(FM_DELETE_ALL_CMD_EID, CFE_EVS_EventType_DEBUG, "%s command: deleted %d files: dir = %s",
CmdText, (int)DeleteCount, Directory);
FM_GlobalData.ChildCmdCounter++;
if (FilesNotDeletedCount > 0)
{
/* If errors occured, report generic event(s) */
CFE_EVS_SendEvent(FM_DELETE_ALL_FILES_ND_WARNING_EID, CFE_EVS_EventType_INFORMATION,
"%s command: one or more files could not be deleted. Files may be open : dir = %s",
CmdText, Directory);
FM_GlobalData.ChildCmdWarnCounter++;
}
if (DirectoriesSkippedCount > 0)
{
/* If errors occured, report generic event(s) */
CFE_EVS_SendEvent(FM_DELETE_ALL_SKIP_WARNING_EID, CFE_EVS_EventType_INFORMATION,
"%s command: one or more directories skipped : dir = %s", CmdText, Directory);
FM_GlobalData.ChildCmdWarnCounter++;
}
} /* end if OS_Status != OS_SUCCESS */
/* Report previous child task activity */
FM_GlobalData.ChildPreviousCC = CmdArgs->CommandCode;
FM_GlobalData.ChildCurrentCC = 0;
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* */
/* FM child task command handler -- Decompress File */
/* */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#ifdef FM_INCLUDE_DECOMPRESS
void FM_ChildDecompressCmd(const FM_ChildQueueEntry_t *CmdArgs)
{
const char *CmdText = "Decompress File";
int32 CFE_Status = CFE_SUCCESS;
/* Report current child task activity */
FM_GlobalData.ChildCurrentCC = CmdArgs->CommandCode;
/* Decompress source file into target file */
CFE_Status = FS_LIB_Decompress(&FM_GlobalData.DecompressState, CmdArgs->Source1, CmdArgs->Target);
if (CFE_Status != CFE_SUCCESS)
{
FM_GlobalData.ChildCmdErrCounter++;
/* Send command failure event (error) */
CFE_EVS_SendEvent(FM_DECOM_CFE_ERR_EID, CFE_EVS_EventType_ERROR,
"%s error: CFE_FS_Decompress failed: result = %d, src = %s, tgt = %s", CmdText,
(int)CFE_Status, CmdArgs->Source1, CmdArgs->Target);
}
else
{
FM_GlobalData.ChildCmdCounter++;
/* Send command completion event (info) */
CFE_EVS_SendEvent(FM_DECOM_CMD_EID, CFE_EVS_EventType_DEBUG, "%s command: src = %s, tgt = %s", CmdText,
CmdArgs->Source1, CmdArgs->Target);
}
/* Report previous child task activity */
FM_GlobalData.ChildPreviousCC = CmdArgs->CommandCode;
FM_GlobalData.ChildCurrentCC = 0;
}
#endif
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* */
/* FM child task command handler -- Concatenate Files */
/* */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void FM_ChildConcatCmd(const FM_ChildQueueEntry_t *CmdArgs)
{
const char *CmdText = "Concat Files";
bool ConcatResult = false;
bool CopyInProgress = false;
bool CreatedTgtFile = false;
bool OpenedSource2 = false;
bool OpenedTgtFile = false;
int32 LoopCount = 0;
int32 OS_Status = OS_SUCCESS;
osal_id_t FileHandleSrc = OS_OBJECT_ID_UNDEFINED;
osal_id_t FileHandleTgt = OS_OBJECT_ID_UNDEFINED;
int32 BytesRead = 0;
int32 BytesWritten = 0;
/* Report current child task activity */
FM_GlobalData.ChildCurrentCC = CmdArgs->CommandCode;
/* Copy source file #1 to the target file */
OS_Status = OS_cp(CmdArgs->Source1, CmdArgs->Target);
if (OS_Status != OS_SUCCESS)
{
FM_GlobalData.ChildCmdErrCounter++;
/* Send command failure event (error) */
CFE_EVS_SendEvent(FM_CONCAT_OSCPY_ERR_EID, CFE_EVS_EventType_ERROR,
"%s error: OS_cp failed: result = %d, src = %s, tgt = %s", CmdText, (int)OS_Status,
CmdArgs->Source1, CmdArgs->Target);
}
else
{
CreatedTgtFile = true;
}
/* Open source file #2 */
if (CreatedTgtFile)
{
OS_Status = OS_OpenCreate(&FileHandleSrc, CmdArgs->Source2, OS_FILE_FLAG_NONE, OS_READ_ONLY);
if (OS_Status != OS_SUCCESS)
{
FM_GlobalData.ChildCmdErrCounter++;
/* Send command failure event (error) */
CFE_EVS_SendEvent(FM_CONCAT_OPEN_SRC2_ERR_EID, CFE_EVS_EventType_ERROR,
"%s error: OS_OpenCreate failed: result = %d, src2 = %s", CmdText, (int)OS_Status,
CmdArgs->Source2);
}
else
{
OpenedSource2 = true;
}
}
/* Open target file */
if (OpenedSource2)
{
OS_Status = OS_OpenCreate(&FileHandleTgt, CmdArgs->Target, OS_FILE_FLAG_NONE, OS_READ_WRITE);
if (OS_Status != OS_SUCCESS)
{
FM_GlobalData.ChildCmdErrCounter++;
/* Send command failure event (error) */
CFE_EVS_SendEvent(FM_CONCAT_OPEN_TGT_ERR_EID, CFE_EVS_EventType_ERROR,
"%s error: OS_OpenCreate failed: result = %d, tgt = %s", CmdText, (int)OS_Status,
CmdArgs->Target);
}
else
{
OpenedTgtFile = true;
}
}
/* Append source file #2 to target file */
if (OpenedTgtFile)
{
/* Seek to end of target file */
OS_lseek(FileHandleTgt, 0, OS_SEEK_END);
CopyInProgress = true;
LoopCount = 0;
while (CopyInProgress)
{
BytesRead = OS_read(FileHandleSrc, FM_GlobalData.ChildBuffer, FM_CHILD_FILE_BLOCK_SIZE);
if (BytesRead == 0)
{
/* Success - finished reading source file #2 */
CopyInProgress = false;
ConcatResult = true;
}
else if (BytesRead < 0)
{
CopyInProgress = false;
FM_GlobalData.ChildCmdErrCounter++;
/* Send command failure event (error) */
CFE_EVS_SendEvent(FM_CONCAT_OSRD_ERR_EID, CFE_EVS_EventType_ERROR,
"%s error: OS_read failed: result = %d, file = %s", CmdText, (int)BytesRead,
CmdArgs->Source2);
}
else
{
/* Write source file #2 to target file */
BytesWritten = OS_write(FileHandleTgt, FM_GlobalData.ChildBuffer, BytesRead);
if (BytesWritten != BytesRead)
{
CopyInProgress = false;
FM_GlobalData.ChildCmdErrCounter++;
/* Send command failure event (error) */
CFE_EVS_SendEvent(FM_CONCAT_OSWR_ERR_EID, CFE_EVS_EventType_ERROR,
"%s error: OS_write failed: result = %d, expected = %d", CmdText,
(int)BytesWritten, (int)BytesRead);
}
}
/* Avoid CPU hogging */
if (CopyInProgress)
{
LoopCount++;
if (LoopCount == FM_CHILD_FILE_LOOP_COUNT)
{
/* Give up the CPU */
CFE_ES_PerfLogExit(FM_CHILD_TASK_PERF_ID);
OS_TaskDelay(FM_CHILD_FILE_SLEEP_MS);
CFE_ES_PerfLogEntry(FM_CHILD_TASK_PERF_ID);
LoopCount = 0;
}
}
}
}
if (OpenedTgtFile)
{
/* Close target file */
OS_close(FileHandleTgt);
}
if (OpenedSource2)
{
/* Close source file #2 */
OS_close(FileHandleSrc);
}
if ((CreatedTgtFile == true) && (ConcatResult == false))
{
/* Remove partial target file after concat error */
OS_remove(CmdArgs->Target);
}
if (ConcatResult == true)
{
FM_GlobalData.ChildCmdCounter++;
/* Send command completion event (info) */
CFE_EVS_SendEvent(FM_CONCAT_CMD_EID, CFE_EVS_EventType_DEBUG, "%s command: src1 = %s, src2 = %s, tgt = %s",
CmdText, CmdArgs->Source1, CmdArgs->Source2, CmdArgs->Target);
}
/* Report previous child task activity */
FM_GlobalData.ChildPreviousCC = CmdArgs->CommandCode;
FM_GlobalData.ChildCurrentCC = 0;
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* */
/* FM child task command handler -- Get File Info */
/* */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void FM_ChildFileInfoCmd(FM_ChildQueueEntry_t *CmdArgs)
{
const char *CmdText = "Get File Info";
bool GettingCRC = false;
uint32 CurrentCRC = 0;
int32 LoopCount = 0;
int32 BytesRead = 0;
osal_id_t FileHandle = OS_OBJECT_ID_UNDEFINED;
int32 Status = 0;
/* Report current child task activity */
FM_GlobalData.ChildCurrentCC = CmdArgs->CommandCode;
/*
** Command argument useage for this command:
**
** CmdArgs->CommandCode = FM_GET_DIR_FILE_CC
** CmdArgs->Source1 = name of directory or file
** CmdArgs->FileInfoState = state of directory or file
** CmdArgs->FileInfoSize = file size, else zero
** CmdArgs->FileInfoTime = last modify time
*/
/* Initialize file info packet (set all data to zero) */
CFE_MSG_Init(&FM_GlobalData.FileInfoPkt.TlmHeader.Msg, CFE_SB_ValueToMsgId(FM_FILE_INFO_TLM_MID),
sizeof(FM_FileInfoPkt_t));
/* Report directory or filename state, name, size and time */
FM_GlobalData.FileInfoPkt.FileStatus = (uint8)CmdArgs->FileInfoState;
strncpy(FM_GlobalData.FileInfoPkt.Filename, CmdArgs->Source1, OS_MAX_PATH_LEN - 1);
FM_GlobalData.FileInfoPkt.Filename[OS_MAX_PATH_LEN - 1] = '\0';
FM_GlobalData.FileInfoPkt.FileSize = CmdArgs->FileInfoSize;
FM_GlobalData.FileInfoPkt.LastModifiedTime = CmdArgs->FileInfoTime;
FM_GlobalData.FileInfoPkt.Mode = CmdArgs->Mode;
/* Validate CRC algorithm */
if (CmdArgs->FileInfoCRC != FM_IGNORE_CRC)
{
if (CmdArgs->FileInfoState != FM_NAME_IS_FILE_CLOSED)
{
/* Can only calculate CRC for closed files */
FM_GlobalData.ChildCmdWarnCounter++;
CFE_EVS_SendEvent(FM_GET_FILE_INFO_STATE_WARNING_EID, CFE_EVS_EventType_INFORMATION,
"%s warning: unable to compute CRC: invalid file state = %d, file = %s", CmdText,
(int)CmdArgs->FileInfoState, CmdArgs->Source1);
CmdArgs->FileInfoCRC = FM_IGNORE_CRC;
}
else if ((CmdArgs->FileInfoCRC != CFE_MISSION_ES_CRC_8) && (CmdArgs->FileInfoCRC != CFE_MISSION_ES_CRC_16) &&
(CmdArgs->FileInfoCRC != CFE_MISSION_ES_CRC_32))
{
/* Can only calculate CRC using known algorithms */
FM_GlobalData.ChildCmdWarnCounter++;
CFE_EVS_SendEvent(FM_GET_FILE_INFO_TYPE_WARNING_EID, CFE_EVS_EventType_INFORMATION,
"%s warning: unable to compute CRC: invalid CRC type = %d, file = %s", CmdText,
(int)CmdArgs->FileInfoCRC, CmdArgs->Source1);
CmdArgs->FileInfoCRC = FM_IGNORE_CRC;
}
}
/* Compute CRC */
if (CmdArgs->FileInfoCRC != FM_IGNORE_CRC)
{
Status = OS_OpenCreate(&FileHandle, CmdArgs->Source1, OS_FILE_FLAG_NONE, OS_READ_ONLY);
if (Status != OS_SUCCESS)
{
FM_GlobalData.ChildCmdWarnCounter++;
/* Send CRC failure event (warning) */
CFE_EVS_SendEvent(FM_GET_FILE_INFO_OPEN_ERR_EID, CFE_EVS_EventType_ERROR,
"%s warning: unable to compute CRC: OS_OpenCreate result = %d, file = %s", CmdText,
(int)Status, CmdArgs->Source1);
GettingCRC = false;
}
else
{
GettingCRC = true;
}
while (GettingCRC)
{
BytesRead = OS_read(FileHandle, FM_GlobalData.ChildBuffer, FM_CHILD_FILE_BLOCK_SIZE);
if (BytesRead == 0)
{
/* Finished reading file */
GettingCRC = false;
OS_close(FileHandle);
/* Add CRC to telemetry packet */
FM_GlobalData.FileInfoPkt.CRC_Computed = true;
FM_GlobalData.FileInfoPkt.CRC = CurrentCRC;
}
else if (BytesRead < 0)
{
/* Error reading file */
CurrentCRC = 0;
GettingCRC = false;
OS_close(FileHandle);
/* Send CRC failure event (warning) */
FM_GlobalData.ChildCmdWarnCounter++;
CFE_EVS_SendEvent(FM_GET_FILE_INFO_READ_WARNING_EID, CFE_EVS_EventType_INFORMATION,
"%s warning: unable to compute CRC: OS_read result = %d, file = %s", CmdText,
(int)BytesRead, CmdArgs->Source1);
}
else
{
/* Continue CRC calculation */
CurrentCRC =
CFE_ES_CalculateCRC(FM_GlobalData.ChildBuffer, BytesRead, CurrentCRC, CmdArgs->FileInfoCRC);
}
/* Avoid CPU hogging */
if (GettingCRC)
{
LoopCount++;
if (LoopCount == FM_CHILD_FILE_LOOP_COUNT)
{
/* Give up the CPU */
CFE_ES_PerfLogExit(FM_CHILD_TASK_PERF_ID);
OS_TaskDelay(FM_CHILD_FILE_SLEEP_MS);
CFE_ES_PerfLogEntry(FM_CHILD_TASK_PERF_ID);
LoopCount = 0;
}
}
}
FM_GlobalData.FileInfoPkt.CRC = CurrentCRC;
}
/* Timestamp and send file info telemetry packet */
CFE_SB_TimeStampMsg(&FM_GlobalData.FileInfoPkt.TlmHeader.Msg);
CFE_SB_TransmitMsg(&FM_GlobalData.FileInfoPkt.TlmHeader.Msg, true);
FM_GlobalData.ChildCmdCounter++;
/* Send command completion event (debug) */
CFE_EVS_SendEvent(FM_GET_FILE_INFO_CMD_EID, CFE_EVS_EventType_DEBUG, "%s command: file = %s", CmdText,
CmdArgs->Source1);
/* Report previous child task activity */
FM_GlobalData.ChildPreviousCC = CmdArgs->CommandCode;
FM_GlobalData.ChildCurrentCC = 0;
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* */
/* FM child task command handler -- Create Directory */
/* */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void FM_ChildCreateDirCmd(const FM_ChildQueueEntry_t *CmdArgs)
{
const char *CmdText = "Create Directory";
int32 OS_Status = OS_SUCCESS;
/* Report current child task activity */
FM_GlobalData.ChildCurrentCC = CmdArgs->CommandCode;
OS_Status = OS_mkdir(CmdArgs->Source1, 0);
if (OS_Status != OS_SUCCESS)
{
FM_GlobalData.ChildCmdErrCounter++;
/* Send command failure event (error) */
CFE_EVS_SendEvent(FM_CREATE_DIR_OS_ERR_EID, CFE_EVS_EventType_ERROR,
"%s error: OS_mkdir failed: result = %d, dir = %s", CmdText, (int)OS_Status,
CmdArgs->Source1);
}
else
{
FM_GlobalData.ChildCmdCounter++;
/* Send command completion event (info) */
CFE_EVS_SendEvent(FM_CREATE_DIR_CMD_EID, CFE_EVS_EventType_DEBUG, "%s command: src = %s", CmdText,
CmdArgs->Source1);
}
/* Report previous child task activity */
FM_GlobalData.ChildPreviousCC = CmdArgs->CommandCode;
FM_GlobalData.ChildCurrentCC = 0;
}