forked from FreeRTOS/Lab-Project-FreeRTOS-FAT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ff_file.c
3346 lines (2866 loc) · 111 KB
/
ff_file.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
/*
* FreeRTOS+FAT V2.3.3
* Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* https://www.FreeRTOS.org
* https://github.com/FreeRTOS
*
*/
/**
* @file ff_file.c
* @ingroup FILEIO
*
* @defgroup FILEIO FILE I/O Access
* @brief Provides an interface to allow File I/O on a mounted IOMAN.
*
* Provides file-system interfaces for the FAT file-system.
**/
#include "ff_headers.h"
#if ( ffconfigUNICODE_UTF16_SUPPORT != 0 )
#include <wchar.h>
#endif
static FF_Error_t FF_Truncate( FF_FILE * pxFile,
BaseType_t bClosing );
static uint32_t FF_ReadPartial( FF_FILE * pxFile,
uint32_t ulItemLBA,
uint32_t ulRelBlockPos,
uint32_t ulCount,
uint8_t * pucBuffer,
FF_Error_t * pxError );
static uint32_t FF_WritePartial( FF_FILE * pxFile,
uint32_t ulItemLBA,
uint32_t ulRelBlockPos,
uint32_t ulCount,
const uint8_t * pucBuffer,
FF_Error_t * pxError );
static uint32_t FF_SetCluster( FF_FILE * pxFile,
FF_Error_t * pxError );
static uint32_t FF_FileLBA( FF_FILE * pxFile );
static FF_Error_t FF_ExtendFile( FF_FILE * pxFile,
uint32_t ulSize );
/*-----------------------------------------------------------*/
/**
* @brief Converts STDIO mode strings into the equivalent FreeRTOS+FAT mode.
*
* @param pcMode The mode string e.g. "rb" "rb+" "w" "a" "r" "w+" "a+" etc
*
* @return Returns the mode bits that should be passed to the FF_Open function.
**/
uint8_t FF_GetModeBits( const char * pcMode )
{
uint8_t ucModeBits = 0x00;
while( *pcMode != '\0' )
{
switch( *pcMode )
{
case 'r': /* Allow Read. */
case 'R':
ucModeBits |= FF_MODE_READ;
break;
case 'w': /* Allow Write. */
case 'W':
ucModeBits |= FF_MODE_WRITE;
ucModeBits |= FF_MODE_CREATE; /* Create if not exist. */
ucModeBits |= FF_MODE_TRUNCATE;
break;
case 'a': /* Append new writes to the end of the file. */
case 'A':
ucModeBits |= FF_MODE_WRITE;
ucModeBits |= FF_MODE_APPEND;
ucModeBits |= FF_MODE_CREATE; /* Create if not exist. */
break;
case '+': /* Update the file, don't Append! */
ucModeBits |= FF_MODE_READ;
ucModeBits |= FF_MODE_WRITE; /* RW Mode. */
break;
case 'D':
/* Internal use only! */
ucModeBits |= FF_MODE_DIR;
break;
case 'b':
case 'B':
/* b|B flags not supported (Binary mode is native anyway). */
break;
default:
break;
}
pcMode++;
}
return ucModeBits;
} /* FF_GetModeBits() */
/*-----------------------------------------------------------*/
static FF_FILE * prvAllocFileHandle( FF_IOManager_t * pxIOManager,
FF_Error_t * pxError )
{
FF_FILE * pxFile;
pxFile = ffconfigMALLOC( sizeof( FF_FILE ) );
if( pxFile == NULL )
{
*pxError = FF_createERR( FF_ERR_NOT_ENOUGH_MEMORY, FF_OPEN );
}
else
{
memset( pxFile, 0, sizeof( *pxFile ) );
#if ( ffconfigOPTIMISE_UNALIGNED_ACCESS != 0 )
{
pxFile->pucBuffer = ( uint8_t * ) ffconfigMALLOC( pxIOManager->usSectorSize );
if( pxFile->pucBuffer != NULL )
{
memset( pxFile->pucBuffer, 0, pxIOManager->usSectorSize );
}
else
{
*pxError = FF_createERR( FF_ERR_NOT_ENOUGH_MEMORY, FF_OPEN );
ffconfigFREE( pxFile );
/* Make sure that NULL will be returned. */
pxFile = NULL;
}
}
#else /* if ( ffconfigOPTIMISE_UNALIGNED_ACCESS != 0 ) */
{
/* Remove compiler warnings. */
( void ) pxIOManager;
}
#endif /* if ( ffconfigOPTIMISE_UNALIGNED_ACCESS != 0 ) */
}
return pxFile;
} /* prvAllocFileHandle() */
/*-----------------------------------------------------------*/
/**
* FF_Open() Mode Information
* - FF_MODE_WRITE
* - Allows WRITE access to the file.
* .
* - FF_MODE_READ
* - Allows READ access to the file.
* .
* - FF_MODE_CREATE
* - Create file if it doesn't exist.
* .
* - FF_MODE_TRUNCATE
* - Erase the file if it already exists and overwrite.
* *
* - FF_MODE_APPEND
* - Causes all writes to occur at the end of the file. (Its impossible to overwrite other data in a file with this flag set).
* .
* .
*
* Some sample modes:
* - (FF_MODE_WRITE | FF_MODE_CREATE | FF_MODE_TRUNCATE)
* - Write access to the file. (Equivalent to "w").
* .
* - (FF_MODE_WRITE | FF_MODE_READ)
* - Read and Write access to the file. (Equivalent to "rb+").
* .
* - (FF_MODE_WRITE | FF_MODE_READ | FF_MODE_APPEND | FF_MODE_CREATE)
* - Read and Write append mode access to the file. (Equivalent to "a+").
* .
* .
* Be careful when choosing modes. For those using FF_Open() at the application layer
* its best to use the provided FF_GetModeBits() function, as this complies to the same
* behaviour as the stdio.h fopen() function.
*
**/
/**
* @brief Opens a File for Access
*
* @param pxIOManager FF_IOManager_t object that was created by FF_CreateIOManager().
* @param pcPath Path to the File or object.
* @param ucMode Access Mode required. Modes are a little complicated, the function FF_GetModeBits()
* @param ucMode will convert a stdio Mode string into the equivalent Mode bits for this parameter.
* @param pxError Pointer to a signed byte for error checking. Can be NULL if not required.
* @param pxError To be checked when a NULL pointer is returned.
*
* @return NULL pointer on error, in which case pxError should be checked for more information.
**/
/* *INDENT-OFF* */
#if ( ffconfigUNICODE_UTF16_SUPPORT != 0 )
FF_FILE * FF_Open( FF_IOManager_t * pxIOManager,
const FF_T_WCHAR * pcPath,
uint8_t ucMode,
FF_Error_t * pxError )
#else
FF_FILE * FF_Open( FF_IOManager_t * pxIOManager,
const char * pcPath,
uint8_t ucMode,
FF_Error_t * pxError )
#endif
/* *INDENT-ON* */
{
FF_FILE * pxFile = NULL;
FF_FILE * pxFileChain;
FF_DirEnt_t xDirEntry;
uint32_t ulFileCluster;
FF_Error_t xError;
BaseType_t xIndex;
FF_FindParams_t xFindParams;
#if ( ffconfigUNICODE_UTF16_SUPPORT != 0 )
FF_T_WCHAR pcFileName[ ffconfigMAX_FILENAME ];
#else
char pcFileName[ ffconfigMAX_FILENAME ];
#endif
#if ( ffconfigPROTECT_FF_FOPEN_WITH_SEMAPHORE == 1 )
{
if( ( ucMode & FF_MODE_CREATE ) != 0U )
{
FF_PendSemaphore( pxIOManager->pvSemaphoreOpen );
}
}
#endif
memset( &xFindParams, '\0', sizeof( xFindParams ) );
/* Inform the functions that the entry will be created if not found. */
if( ( ucMode & FF_MODE_CREATE ) != 0 )
{
xFindParams.ulFlags |= FIND_FLAG_CREATE_FLAG;
}
if( pxIOManager == NULL )
{
/* Use the error function code 'FF_OPEN' as this static
* function is only called from that function. */
xError = FF_createERR( FF_ERR_NULL_POINTER, FF_OPEN );
}
#if ( ffconfigREMOVABLE_MEDIA != 0 )
else if( ( pxIOManager->ucFlags & FF_IOMAN_DEVICE_IS_EXTRACTED ) != 0 )
{
xError = FF_createERR( FF_ERR_IOMAN_DRIVER_NOMEDIUM, FF_OPEN );
}
#endif /* ffconfigREMOVABLE_MEDIA */
else
{
xError = FF_ERR_NONE;
/* Let xIndex point to the last occurrence of '/' or '\',
* to separate the path from the file name. */
xIndex = ( BaseType_t ) STRLEN( pcPath );
while( xIndex != 0 )
{
if( ( pcPath[ xIndex ] == '\\' ) || ( pcPath[ xIndex ] == '/' ) )
{
break;
}
xIndex--;
}
/* Copy the file name, i.e. the string that comes after the last separator. */
STRNCPY( pcFileName, pcPath + xIndex + 1, ffconfigMAX_FILENAME - 1 );
pcFileName[ ffconfigMAX_FILENAME - 1 ] = 0;
if( xIndex == 0 )
{
/* Only for the root, the slash is part of the directory name.
* 'xIndex' now equals to the length of the path name. */
xIndex = 1;
}
/* FF_CreateShortName() might set flags FIND_FLAG_FITS_SHORT and FIND_FLAG_SIZE_OK. */
FF_CreateShortName( &xFindParams, pcFileName );
/* Lookup the path and find the cluster pointing to the directory: */
xFindParams.ulDirCluster = FF_FindDir( pxIOManager, pcPath, ( uint16_t ) xIndex, &xError );
if( xFindParams.ulDirCluster == 0ul )
{
if( ( ucMode & FF_MODE_WRITE ) != 0 )
{
FF_PRINTF( "FF_Open[%s]: Path not found\n", pcPath );
}
/* The user tries to open a file but the path leading to the file does not exist. */
}
else if( FF_isERR( xError ) == pdFALSE )
{
/* Allocate an empty file handle and buffer space for 'unaligned access'. */
pxFile = prvAllocFileHandle( pxIOManager, &xError );
}
}
if( FF_isERR( xError ) == pdFALSE )
{
/* Copy the Mode Bits. */
pxFile->ucMode = ucMode;
/* See if the file does exist within the given directory. */
ulFileCluster = FF_FindEntryInDir( pxIOManager, &xFindParams, pcFileName, 0x00, &xDirEntry, &xError );
if( ulFileCluster == 0ul )
{
/* If cluster 0 was returned, it might be because the file has no allocated cluster,
* i.e. only a directory entry and no stored data. */
if( STRLEN( pcFileName ) == STRLEN( xDirEntry.pcFileName ) )
{
if( ( xDirEntry.ulFileSize == 0 ) && ( FF_strmatch( pcFileName, xDirEntry.pcFileName, ( BaseType_t ) STRLEN( pcFileName ) ) == pdTRUE ) )
{
/* It is the file, give it a pseudo cluster number '1'. */
ulFileCluster = 1;
/* And reset any error. */
xError = FF_ERR_NONE;
}
}
}
/* Test 'ulFileCluster' again, it might have been changed. */
if( ulFileCluster == 0ul )
{
/* The path is found, but it does not contain the file name yet.
* Maybe the user wants to create it? */
if( ( ucMode & FF_MODE_CREATE ) == 0 )
{
xError = FF_createERR( FF_ERR_FILE_NOT_FOUND, FF_OPEN );
}
else
{
ulFileCluster = FF_CreateFile( pxIOManager, &xFindParams, pcFileName, &xDirEntry, &xError );
if( FF_isERR( xError ) == pdFALSE )
{
xDirEntry.usCurrentItem += 1;
}
}
}
}
if( FF_isERR( xError ) == pdFALSE )
{
/* Now the file exists, or it has been created.
* Check if the Mode flags are allowed: */
if( ( xDirEntry.ucAttrib == FF_FAT_ATTR_DIR ) && ( ( ucMode & FF_MODE_DIR ) == 0 ) )
{
/* Not the object, File Not Found! */
xError = FF_createERR( FF_ERR_FILE_OBJECT_IS_A_DIR, FF_OPEN );
}
/*---------- Ensure Read-Only files don't get opened for Writing. */
else if( ( ( ucMode & ( FF_MODE_WRITE | FF_MODE_APPEND ) ) != 0 ) && ( ( xDirEntry.ucAttrib & FF_FAT_ATTR_READONLY ) != 0 ) )
{
xError = FF_createERR( FF_ERR_FILE_IS_READ_ONLY, FF_OPEN );
}
}
if( FF_isERR( xError ) == pdFALSE )
{
pxFile->pxIOManager = pxIOManager;
pxFile->ulFilePointer = 0;
/* Despite the warning output by MSVC - it is not possible to get here
* if xDirEntry has not been initialised. */
pxFile->ulObjectCluster = xDirEntry.ulObjectCluster;
pxFile->ulFileSize = xDirEntry.ulFileSize;
pxFile->ulCurrentCluster = 0;
pxFile->ulAddrCurrentCluster = pxFile->ulObjectCluster;
pxFile->pxNext = NULL;
pxFile->ulDirCluster = xFindParams.ulDirCluster;
pxFile->usDirEntry = xDirEntry.usCurrentItem - 1;
pxFile->ulChainLength = 0;
pxFile->ulEndOfChain = 0;
pxFile->ulValidFlags &= ~( FF_VALID_FLAG_DELETED );
/* Add pxFile onto the end of our linked list of FF_FILE objects.
* But first make sure that there are not 2 handles with write access
* to the same object. */
FF_PendSemaphore( pxIOManager->pvSemaphore );
{
pxFileChain = ( FF_FILE * ) pxIOManager->FirstFile;
if( pxFileChain == NULL )
{
pxIOManager->FirstFile = pxFile;
}
else
{
for( ; ; )
{
/* See if two file handles point to the same object. */
if( ( pxFileChain->ulObjectCluster == pxFile->ulObjectCluster ) &&
( pxFileChain->ulDirCluster == pxFile->ulDirCluster ) &&
( pxFileChain->usDirEntry == pxFile->usDirEntry ) )
{
/* Fail if any of the two has write access to the object. */
if( ( ( pxFileChain->ucMode | pxFile->ucMode ) & ( FF_MODE_WRITE | FF_MODE_APPEND ) ) != 0 )
{
/* File is already open! DON'T ALLOW IT! */
xError = FF_createERR( FF_ERR_FILE_ALREADY_OPEN, FF_OPEN );
break;
}
}
if( pxFileChain->pxNext == NULL )
{
pxFileChain->pxNext = pxFile;
break;
}
pxFileChain = ( FF_FILE * ) pxFileChain->pxNext;
}
}
}
FF_ReleaseSemaphore( pxIOManager->pvSemaphore );
}
if( FF_isERR( xError ) == pdFALSE )
{
/* If the file is opened with the truncate flag, truncate its contents. */
if( ( ucMode & FF_MODE_TRUNCATE ) != 0 )
{
/* Set the current size and position to zero. */
pxFile->ulFileSize = 0;
pxFile->ulFilePointer = 0;
}
}
if( FF_isERR( xError ) != pdFALSE )
{
if( pxFile != NULL )
{
#if ( ffconfigOPTIMISE_UNALIGNED_ACCESS != 0 )
{
ffconfigFREE( pxFile->pucBuffer );
}
#endif
ffconfigFREE( pxFile );
}
pxFile = NULL;
}
#if ( ffconfigPROTECT_FF_FOPEN_WITH_SEMAPHORE == 1 )
{
if( ( ucMode & FF_MODE_CREATE ) != 0U )
{
FF_ReleaseSemaphore( pxIOManager->pvSemaphoreOpen );
}
}
#endif
if( pxError != NULL )
{
*pxError = xError;
}
return pxFile;
} /* FF_Open() */
/*-----------------------------------------------------------*/
/**
* @brief Tests if a Directory contains any other files or folders.
*
* @param pxIOManager FF_IOManager_t object returned from the FF_CreateIOManager() function.
*
**/
/* *INDENT-OFF* */
#if ( ffconfigUNICODE_UTF16_SUPPORT != 0 )
BaseType_t FF_isDirEmpty( FF_IOManager_t * pxIOManager,
const FF_T_WCHAR * pcPath )
#else
BaseType_t FF_isDirEmpty( FF_IOManager_t * pxIOManager,
const char * pcPath )
#endif
/* *INDENT-ON* */
{
FF_DirEnt_t xDirEntry;
FF_Error_t xError = FF_ERR_NONE;
BaseType_t xReturn;
if( pxIOManager == NULL )
{
xReturn = pdFALSE;
}
else
{
xError = FF_FindFirst( pxIOManager, &xDirEntry, pcPath );
/* Assume the directory is empty until a file is
* encountered with a name other than ".." or "." */
xReturn = pdTRUE;
while( xError == 0 )
{
/* As we can't be sure the first 2 entries contain
* "." and "..", check it, not just count them
*/
#if ( ffconfigUNICODE_UTF16_SUPPORT != 0 )
if( ( wcscmp( xDirEntry.pcFileName, L".." ) != 0 ) && ( wcscmp( xDirEntry.pcFileName, L"." ) != 0 ) )
#else
if( ( strcmp( xDirEntry.pcFileName, ".." ) != 0 ) && ( strcmp( xDirEntry.pcFileName, "." ) != 0 ) )
#endif
{
xReturn = pdFALSE;
break;
}
xError = FF_FindNext( pxIOManager, &xDirEntry );
}
}
return xReturn;
} /* FF_isDirEmpty() */
/*-----------------------------------------------------------*/
#if ( ffconfigPATH_CACHE != 0 )
/* _HT_ After a directory has been renamed, the path cache becomes out-of-date */
#if ( ffconfigUNICODE_UTF16_SUPPORT != 0 )
static void FF_RmPathCache( FF_IOManager_t * pxIOManager,
const FF_T_WCHAR * pcPath )
#else
static void FF_RmPathCache( FF_IOManager_t * pxIOManager,
const char * pcPath )
#endif
{
/*
* The directory 'path' will be removed or renamed
* now clear all entries starting with 'path' in the path cache
*/
UBaseType_t pathLen = STRLEN( pcPath );
FF_PendSemaphore( pxIOManager->pvSemaphore );
{
for( UBaseType_t xIndex = 0; xIndex < ffconfigPATH_CACHE_DEPTH; xIndex++ )
{
UBaseType_t len2 = STRLEN( pxIOManager->xPartition.pxPathCache[ xIndex ].pcPath );
if( ( len2 >= pathLen ) && FF_strmatch( pxIOManager->xPartition.pxPathCache[ xIndex ].pcPath, pcPath, ( BaseType_t ) pathLen ) )
{
pxIOManager->xPartition.pxPathCache[ xIndex ].pcPath[ 0 ] = '\0';
pxIOManager->xPartition.pxPathCache[ xIndex ].ulDirCluster = 0;
}
}
}
FF_ReleaseSemaphore( pxIOManager->pvSemaphore );
}
#endif /* ffconfigPATH_CACHE */
/*-----------------------------------------------------------*/
/* *INDENT-OFF* */
#if ( ffconfigUNICODE_UTF16_SUPPORT != 0 )
FF_Error_t FF_RmDir( FF_IOManager_t * pxIOManager,
const FF_T_WCHAR * pcPath )
#else
FF_Error_t FF_RmDir( FF_IOManager_t * pxIOManager,
const char * pcPath )
#endif
/* *INDENT-ON* */
{
FF_FILE * pxFile;
uint8_t ucEntryBuffer[ 32 ];
FF_FetchContext_t xFetchContext;
FF_Error_t xError = FF_ERR_NONE;
if( pxIOManager == NULL )
{
xError = FF_createERR( FF_ERR_NULL_POINTER, FF_RMDIR );
}
#if ( ffconfigREMOVABLE_MEDIA != 0 )
else if( ( pxIOManager->ucFlags & FF_IOMAN_DEVICE_IS_EXTRACTED ) != 0 )
{
xError = FF_createERR( FF_ERR_IOMAN_DRIVER_NOMEDIUM, FF_RMDIR );
}
#endif /* ffconfigREMOVABLE_MEDIA */
else
{
pxFile = FF_Open( pxIOManager, pcPath, FF_MODE_DIR, &xError );
if( pxFile != NULL )
{
pxFile->ulValidFlags |= FF_VALID_FLAG_DELETED;
/* Clear the struct to allow a call to FF_CleanupEntryFetch() in any
* state. */
memset( &xFetchContext, '\0', sizeof( xFetchContext ) );
/* This task will get the unique right to change directories. */
FF_LockDirectory( pxIOManager );
do /* while( pdFALSE ) */
{
/* This while loop is only introduced to be able to use break
* statements. */
if( FF_isDirEmpty( pxIOManager, pcPath ) == pdFALSE )
{
xError = FF_createERR( FF_ERR_DIR_NOT_EMPTY, FF_RMDIR );
break;
}
FF_LockFAT( pxIOManager );
#if ( ffconfigHASH_CACHE != 0 )
{
/* A directory is removed so invalidate any hash table
* referring to this directory. */
FF_UnHashDir( pxIOManager, pxFile->ulObjectCluster );
}
#endif /* ffconfigHASH_CACHE */
{
/* Add parameter 0 to delete the entire chain!
* The actual directory entries on disk will be freed. */
xError = FF_UnlinkClusterChain( pxIOManager, pxFile->ulObjectCluster, 0 );
}
FF_UnlockFAT( pxIOManager );
if( FF_isERR( xError ) )
{
break;
}
/* Now remove this directory from its parent directory.
* Initialise the dirent Fetch Context object for faster removal of
* dirents. */
xError = FF_InitEntryFetch( pxIOManager, pxFile->ulDirCluster, &xFetchContext );
if( FF_isERR( xError ) )
{
break;
}
#if ( ffconfigHASH_CACHE != 0 )
{
/* Invalidate any hash table of the parent directory
* as well. */
FF_UnHashDir( pxIOManager, pxFile->ulDirCluster );
}
#endif /* ffconfigHASH_CACHE */
/* Edit the Directory Entry, so it will show as deleted.
* First remove the LFN entries: */
xError = FF_RmLFNs( pxIOManager, pxFile->usDirEntry, &xFetchContext );
if( FF_isERR( xError ) )
{
break;
}
/* And remove the Short file name entry: */
xError = FF_FetchEntryWithContext( pxIOManager, pxFile->usDirEntry, &xFetchContext, ucEntryBuffer );
if( FF_isERR( xError ) == pdFALSE )
{
ucEntryBuffer[ 0 ] = FF_FAT_DELETED;
FF_putShort( ucEntryBuffer, FF_FAT_DIRENT_CLUS_HIGH, ( uint32_t ) 0ul );
FF_putShort( ucEntryBuffer, FF_FAT_DIRENT_CLUS_LOW, ( uint32_t ) 0ul );
xError = FF_PushEntryWithContext( pxIOManager, pxFile->usDirEntry, &xFetchContext, ucEntryBuffer );
}
if( FF_isERR( xError ) )
{
break;
}
#if ( ffconfigPATH_CACHE != 0 )
{
/* We're removing a directory which might contain
* subdirectories. Instead of iterating through all
* subdirectories, just clear the path cache. */
FF_RmPathCache( pxIOManager, pcPath );
}
#endif
} while( pdFALSE );
{
FF_Error_t xTempError;
xTempError = FF_CleanupEntryFetch( pxIOManager, &xFetchContext );
if( FF_isERR( xError ) == pdFALSE )
{
xError = xTempError;
}
FF_UnlockDirectory( pxIOManager );
/* Free the file pointer resources. */
xTempError = FF_Close( pxFile );
if( FF_isERR( xError ) == pdFALSE )
{
xError = xTempError;
}
xTempError = FF_FlushCache( pxIOManager );
if( FF_isERR( xError ) == pdFALSE )
{
xError = xTempError;
}
}
} /* if( pxFile != NULL ) */
} /* else if( pxIOManager != NULL ) */
return xError;
} /* FF_RmDir() */
/*-----------------------------------------------------------*/
/* *INDENT-OFF* */
#if ( ffconfigUNICODE_UTF16_SUPPORT != 0 )
FF_Error_t FF_RmFile( FF_IOManager_t * pxIOManager,
const FF_T_WCHAR * pcPath )
#else
FF_Error_t FF_RmFile( FF_IOManager_t * pxIOManager,
const char * pcPath )
#endif
/* *INDENT-ON* */
{
FF_FILE * pxFile;
FF_Error_t xError = FF_ERR_NONE;
uint8_t ucEntryBuffer[ 32 ];
FF_FetchContext_t xFetchContext;
/* Opening the file-to-be-deleted in WR mode has two advantages:
* 1. The file handle gives all necessary information to delete it such
* as the data clusters and directory entries.
* 2. The file is now locked, it can not be opened by another task. */
pxFile = FF_Open( pxIOManager, pcPath, FF_MODE_WRITE, &xError );
if( pxFile != NULL )
{
/* FF_Close() will see this flag and won't do any disc access. */
pxFile->ulValidFlags |= FF_VALID_FLAG_DELETED;
/* Ensure there is actually a cluster chain to delete! */
if( pxFile->ulObjectCluster != 0 )
{
/* Lock the FAT so its thread-safe. */
FF_LockFAT( pxIOManager );
{
/* 0 to delete the entire chain! */
xError = FF_UnlinkClusterChain( pxIOManager, pxFile->ulObjectCluster, 0 );
}
FF_UnlockFAT( pxIOManager );
}
if( FF_isERR( xError ) == pdFALSE )
{
/* Clear the struct to allow a call to FF_CleanupEntryFetch() in any
* state. */
memset( &xFetchContext, '\0', sizeof( xFetchContext ) );
/* Get sole access to "directory changes" */
FF_LockDirectory( pxIOManager );
/* Edit the Directory Entry! (So it appears as deleted); */
do
{
xError = FF_InitEntryFetch( pxIOManager, pxFile->ulDirCluster, &xFetchContext );
if( FF_isERR( xError ) )
{
break;
}
#if ( ffconfigHASH_CACHE != 0 )
{
FF_UnHashDir( pxIOManager, pxFile->ulDirCluster );
}
#endif /* ffconfigHASH_CACHE */
/* Remove LFN entries, if any. */
xError = FF_RmLFNs( pxIOManager, ( uint16_t ) pxFile->usDirEntry, &xFetchContext );
if( FF_isERR( xError ) )
{
break;
}
/* Remove the Short file name entry. */
xError = FF_FetchEntryWithContext( pxIOManager, pxFile->usDirEntry, &xFetchContext, ucEntryBuffer );
if( FF_isERR( xError ) == pdFALSE )
{
ucEntryBuffer[ 0 ] = FF_FAT_DELETED;
FF_putShort( ucEntryBuffer, FF_FAT_DIRENT_CLUS_HIGH, ( uint32_t ) 0ul );
FF_putShort( ucEntryBuffer, FF_FAT_DIRENT_CLUS_LOW, ( uint32_t ) 0ul );
xError = FF_PushEntryWithContext( pxIOManager, pxFile->usDirEntry, &xFetchContext, ucEntryBuffer );
}
} while( pdFALSE );
{
FF_Error_t xTempError;
xTempError = FF_CleanupEntryFetch( pxIOManager, &xFetchContext );
if( FF_isERR( xError ) == pdFALSE )
{
xError = xTempError;
}
FF_UnlockDirectory( pxIOManager );
/* Free the file pointer resources. */
xTempError = FF_Close( pxFile );
if( FF_isERR( xError ) == pdFALSE )
{
xError = xTempError;
}
xTempError = FF_FlushCache( pxIOManager );
if( FF_isERR( xError ) == pdFALSE )
{
xError = xTempError;
}
}
}
} /* if( pxFile != NULL ) */
return xError;
} /* FF_RmFile() */
/*-----------------------------------------------------------*/
/**
* @brief Moves a file or directory from source to destination.
*
* @param pxIOManager The FF_IOManager_t object pointer.
* @param szSourceFile String of the source file to be moved or renamed.
* @param szDestinationFile String of the destination file to where the source should be moved or renamed.
*
* @retval FF_ERR_NONE on success.
* @retval FF_ERR_FILE_DESTINATION_EXISTS if the destination file exists.
* @retval FF_ERR_FILE_COULD_NOT_CREATE_DIRENT if dirent creation failed (fatal error!).
* @retval FF_ERR_FILE_DIR_NOT_FOUND if destination directory was not found.
* @retval FF_ERR_FILE_INVALID_PATH if destination path is invalid.
* @retval FF_ERR_FILE_SOURCE_NOT_FOUND if the source file was not found.
*
**/
/* *INDENT-OFF* */
#if ( ffconfigUNICODE_UTF16_SUPPORT != 0 )
FF_Error_t FF_Move( FF_IOManager_t * pxIOManager,
const FF_T_WCHAR * szSourceFile,
const FF_T_WCHAR * szDestinationFile,
BaseType_t xDeleteIfExists )
#else
FF_Error_t FF_Move( FF_IOManager_t * pxIOManager,
const char * szSourceFile,
const char * szDestinationFile,
BaseType_t xDeleteIfExists )
#endif
/* *INDENT-ON* */
{
FF_Error_t xError = FF_ERR_NONE;
FF_FILE * pSrcFile, * pxDestFile;
FF_DirEnt_t xMyFile;
uint8_t ucEntryBuffer[ 32 ];
size_t uxIndex = 0U;
uint32_t ulDirCluster = 0ul;
FF_FetchContext_t xFetchContext;
#if ( ffconfigPATH_CACHE != 0 )
BaseType_t xIsDirectory = pdFALSE;
#endif
memset( &xFetchContext, '\0', sizeof( xFetchContext ) );
if( pxIOManager == NULL )
{
xError = FF_createERR( FF_ERR_NULL_POINTER, FF_MOVE );
}
#if ( ffconfigREMOVABLE_MEDIA != 0 )
else if( ( pxIOManager->ucFlags & FF_IOMAN_DEVICE_IS_EXTRACTED ) != 0U )
{
xError = FF_createERR( FF_ERR_IOMAN_DRIVER_NOMEDIUM, FF_MOVE );
}
#endif /* ffconfigREMOVABLE_MEDIA */
if( FF_isERR( xError ) == pdFALSE )
{
uxIndex = ( size_t ) STRLEN( szDestinationFile );
/* Find the base name. */
while( uxIndex != 0U )
{
if( ( szDestinationFile[ uxIndex ] == '\\' ) || ( szDestinationFile[ uxIndex ] == '/' ) )
{
break;
}
uxIndex--;
}
/* Copy the base name of the destination file. */
STRNCPY( xMyFile.pcFileName, ( szDestinationFile + uxIndex + 1 ), ffconfigMAX_FILENAME - 1 );
xMyFile.pcFileName[ ffconfigMAX_FILENAME - 1 ] = 0;
/* Now check if the target base name is compliant. */
if( FF_IsNameCompliant( xMyFile.pcFileName ) == pdFALSE )
{
xError = FF_createERR( FF_ERR_FILE_INVALID_PATH, FF_MOVE );
}
if( uxIndex == 0U )
{
/* Give the directory part a minimum
* length of 1, to get '/' at least. */
uxIndex = 1U;
}
}
if( FF_isERR( xError ) == pdFALSE )
{
/* Check destination file doesn't exist! */
pxDestFile = FF_Open( pxIOManager, szDestinationFile, FF_MODE_READ, &xError );
if( ( pxDestFile != NULL ) || ( FF_GETERROR( xError ) == FF_ERR_FILE_OBJECT_IS_A_DIR ) )
{
xError = FF_createERR( FF_ERR_FILE_DESTINATION_EXISTS, FF_MOVE );
if( pxDestFile != NULL )
{
FF_Close( pxDestFile );
if( xDeleteIfExists != pdFALSE )
{
xError = FF_RmFile( pxIOManager, szDestinationFile );
}
}
}
else
{
/* Discard the error set by FF_Open().
* The target file (or directory) is not found: continue renaming. */
xError = FF_ERR_NONE;
}
}
if( FF_isERR( xError ) == pdFALSE )
{
/* About to move/rename 'szSourceFile'. When opening it with 'FF_MODE_WRITE'
* only succeeds if it has no other open handle to it. */
pSrcFile = FF_Open( pxIOManager, szSourceFile, FF_MODE_WRITE, &xError );
if( FF_GETERROR( xError ) == FF_ERR_FILE_OBJECT_IS_A_DIR )
{
/* Open a directory for moving! */
pSrcFile = FF_Open( pxIOManager, szSourceFile, FF_MODE_DIR, &xError );
#if ( ffconfigPATH_CACHE != 0 )
xIsDirectory = pdTRUE;
#endif
}
if( pSrcFile != NULL )
{
/* Collect information about the current directory entry. */
xError = FF_InitEntryFetch( pxIOManager, pSrcFile->ulDirCluster, &xFetchContext );
if( FF_isERR( xError ) == pdFALSE )
{
xError = FF_FetchEntryWithContext( pxIOManager, pSrcFile->usDirEntry, &xFetchContext, ucEntryBuffer );
if( FF_isERR( xError ) == pdFALSE )