Select one of the symbols to view example projects that use it.
 
Outline
...
...
...
...
#define FX_SOURCE_CODE
#include "fx_api.h"
#include "fx_system.h"
#include "fx_directory.h"
#include "fx_file.h"
#include "fx_utility.h"
#include "fx_fault_tolerant.h"
...
...
_fx_file_extended_truncate_release(FX_FILE *, ULONG64)
Files
loading...
CodeScopeSTM32 Libraries and Samplesfilexcommon/src/fx_file_extended_truncate_release.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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/**************************************************************************/ /* */ /* Copyright (c) Microsoft Corporation. All rights reserved. */ /* */ /* This software is licensed under the Microsoft Software License */ /* Terms for Microsoft Azure RTOS. Full text of the license can be */ /* found in the LICENSE file at https://aka.ms/AzureRTOS_EULA */ /* and in the root directory of this software. */ /* */... /**************************************************************************/ ... /**************************************************************************/ /**************************************************************************/ /** */ /** FileX Component */ /** */ /** File */ /** */... /**************************************************************************/ /**************************************************************************/ #define FX_SOURCE_CODE /* Include necessary system files. */ #include "fx_api.h" #include "fx_system.h" #include "fx_directory.h" #include "fx_file.h" #include "fx_utility.h" 5 includes#ifdef FX_ENABLE_FAULT_TOLERANT #include "fx_fault_tolerant.h" #endif /* FX_ENABLE_FAULT_TOLERANT */ ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _fx_file_extended_truncate_release PORTABLE C */ /* 6.1.3 */ /* AUTHOR */ /* */ /* William E. Lamie, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function sets the file to the specified size, if smaller than */ /* the current file size. If the new file size is less than the */ /* current file read/write position, the internal file pointers will */ /* also be modified. Any unused clusters are released back to the */ /* media. */ /* */ /* INPUT */ /* */ /* file_ptr File control block pointer */ /* size New size of the file in bytes */ /* */ /* OUTPUT */ /* */ /* return status */ /* */ /* CALLS */ /* */ /* _fx_directory_entry_write Write directory entry */ /* _fx_utility_exFAT_bitmap_flush Flush exFAT allocation bitmap */ /* _fx_utility_exFAT_cluster_state_set Set cluster state */ /* _fx_utility_FAT_entry_read Read a FAT entry */ /* _fx_utility_FAT_entry_write Write a FAT entry */ /* _fx_utility_FAT_flush Flush written FAT entries */ /* _fx_fault_tolerant_transaction_start Start fault tolerant */ /* transaction */ /* _fx_fault_tolerant_transaction_end End fault tolerant transaction*/ /* _fx_fault_tolerant_recover Recover FAT chain */ /* _fx_fault_tolerant_reset_log_file Reset the log file */ /* _fx_fault_tolerant_set_FAT_chain Set data of FAT chain */ /* */ /* CALLED BY */ /* */ /* Application Code */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 William E. Lamie Initial Version 6.0 */ /* 09-30-2020 William E. Lamie Modified comment(s), */ /* resulting in version 6.1 */ /* 12-31-2020 William E. Lamie Modified comment(s), fixed */ /* available cluster issue, */ /* resulting in version 6.1.3 */ /* */... /**************************************************************************/ UINT _fx_file_extended_truncate_release(FX_FILE *file_ptr, ULONG64 size) { UINT status; ULONG cluster; ULONG contents; ULONG bytes_per_cluster; ULONG last_cluster; ULONG cluster_count; ULONG64 bytes_remaining; FX_MEDIA *media_ptr; #ifdef FX_ENABLE_EXFAT ULONG original_last_physical_cluster; #endif /* FX_ENABLE_EXFAT */ #ifndef FX_DONT_UPDATE_OPEN_FILES ULONG open_count; FX_FILE *search_ptr;/* ... */ #endif #ifdef TX_ENABLE_EVENT_TRACE TX_TRACE_BUFFER_ENTRY *trace_event; ULONG trace_timestamp;/* ... */ #endif /* First, determine if the file is still open. */ if (file_ptr -> fx_file_id != FX_FILE_ID) { /* Return the file not open error status. */ return(FX_NOT_OPEN); }if (file_ptr -> fx_file_id != FX_FILE_ID) { ... } #ifndef FX_MEDIA_STATISTICS_DISABLE /* Setup pointer to media structure. */ media_ptr = file_ptr -> fx_file_media_ptr; /* Increment the number of times this service has been called. */ media_ptr -> fx_media_file_truncate_releases++;/* ... */ #endif /* Setup pointer to associated media control block. */ media_ptr = file_ptr -> fx_file_media_ptr; /* If trace is enabled, insert this event into the trace buffer. */ FX_TRACE_IN_LINE_INSERT(FX_TRACE_FILE_TRUNCATE_RELEASE, file_ptr, size, file_ptr -> fx_file_current_file_size, 0, FX_TRACE_FILE_EVENTS, &trace_event, &trace_timestamp) /* Protect against other threads accessing the media. */ FX_PROTECT #ifdef FX_ENABLE_FAULT_TOLERANT /* Start transaction. */ _fx_fault_tolerant_transaction_start(media_ptr);/* ... */ #endif /* FX_ENABLE_FAULT_TOLERANT */ /* Make sure this file is open for writing. */ if (file_ptr -> fx_file_open_mode != FX_OPEN_FOR_WRITE) { #ifdef FX_ENABLE_FAULT_TOLERANT FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr); #endif /* FX_ENABLE_FAULT_TOLERANT */ /* Release media protection. */ FX_UNPROTECT /* Return the access error exception - a write was attempted from a file opened for reading! *//* ... */ return(FX_ACCESS_ERROR); }if (file_ptr -> fx_file_open_mode != FX_OPEN_FOR_WRITE) { ... } /* Check for write protect at the media level (set by driver). */ if (media_ptr -> fx_media_driver_write_protect) { #ifdef FX_ENABLE_FAULT_TOLERANT FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr); #endif /* FX_ENABLE_FAULT_TOLERANT */ /* Release media protection. */ FX_UNPROTECT /* Return write protect error. */ return(FX_WRITE_PROTECT); }if (media_ptr -> fx_media_driver_write_protect) { ... } /* Calculate the number of bytes per cluster. */ bytes_per_cluster = ((ULONG)media_ptr -> fx_media_bytes_per_sector) * ((ULONG)media_ptr -> fx_media_sectors_per_cluster); /* Check for invalid value. */ if (bytes_per_cluster == 0) { #ifdef FX_ENABLE_FAULT_TOLERANT FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr); #endif /* FX_ENABLE_FAULT_TOLERANT */ /* Release media protection. */ FX_UNPROTECT /* Invalid media, return error. */ return(FX_MEDIA_INVALID); }if (bytes_per_cluster == 0) { ... } /* Setup the new file available size - if less than the current available size. */ if (size < file_ptr -> fx_file_current_available_size) { /* Yes, the file needs to be truncated. */ /* Update the available file size. */ file_ptr -> fx_file_current_available_size = ((size + bytes_per_cluster - 1) / bytes_per_cluster) * bytes_per_cluster; /* Is the new available size less than the actual file size? */ if (size < file_ptr -> fx_file_current_file_size) { /* Setup the new file size. */ file_ptr -> fx_file_current_file_size = size; /* Set the modified flag. */ file_ptr -> fx_file_modified = FX_TRUE; /* Copy the new file size into the directory entry. */ file_ptr -> fx_file_dir_entry.fx_dir_entry_file_size = size; /* Set the first cluster to NULL. */ if (size == 0) { /* Yes, the first cluster needs to be cleared since the entire file is going to be truncated. *//* ... */ file_ptr -> fx_file_dir_entry.fx_dir_entry_cluster = FX_NULL; }if (size == 0) { ... } /* Write the directory entry to the media. */ #ifdef FX_ENABLE_EXFAT if (media_ptr -> fx_media_FAT_type == FX_exFAT) { status = _fx_directory_exFAT_entry_write( media_ptr, &(file_ptr -> fx_file_dir_entry), UPDATE_STREAM); }if (media_ptr -> fx_media_FAT_type == FX_exFAT) { ... } else { #endif /* FX_ENABLE_EXFAT */ status = _fx_directory_entry_write(media_ptr, &(file_ptr -> fx_file_dir_entry)); #ifdef FX_ENABLE_EXFAT }else { ... } #endif /* FX_ENABLE_EXFAT */ /* Check for a good status. */ if (status != FX_SUCCESS) { #ifdef FX_ENABLE_FAULT_TOLERANT FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr); #endif /* FX_ENABLE_FAULT_TOLERANT */ /* Release media protection. */ FX_UNPROTECT /* Error writing the directory. */ return(status); }if (status != FX_SUCCESS) { ... } }if (size < file_ptr -> fx_file_current_file_size) { ... } /* Update the trace event with the truncated size. */ FX_TRACE_EVENT_UPDATE(trace_event, trace_timestamp, FX_TRACE_FILE_TRUNCATE_RELEASE, 0, 0, 0, file_ptr -> fx_file_current_file_size) }if (size < file_ptr -> fx_file_current_available_size) { ... } else { /* Update the trace event with the truncated size. */ FX_TRACE_EVENT_UPDATE(trace_event, trace_timestamp, FX_TRACE_FILE_TRUNCATE_RELEASE, 0, 0, 0, file_ptr -> fx_file_current_file_size) #ifdef FX_ENABLE_FAULT_TOLERANT FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr); #endif /* FX_ENABLE_FAULT_TOLERANT */ /* Release media protection. */ FX_UNPROTECT /* Just return, the truncate size is larger than the available size. */ return(FX_SUCCESS); }else { ... } /* Calculate the number of bytes per cluster. */ bytes_per_cluster = ((ULONG)media_ptr -> fx_media_bytes_per_sector) * ((ULONG)media_ptr -> fx_media_sectors_per_cluster); /* Now check to see if the read/write internal file pointers need to be adjusted. *//* ... */ if (file_ptr -> fx_file_current_file_offset > file_ptr -> fx_file_current_file_size) { /* At this point, we are ready to walk list of clusters to setup the seek position of this file. *//* ... */ cluster = file_ptr -> fx_file_first_physical_cluster; bytes_remaining = size; last_cluster = 0; cluster_count = 0; /* Follow the link of FAT entries. */ while ((cluster >= FX_FAT_ENTRY_START) && (cluster < media_ptr -> fx_media_fat_reserved)) { /* Increment the number of clusters. */ cluster_count++; #ifdef FX_ENABLE_EXFAT if (file_ptr -> fx_file_dir_entry.fx_dir_entry_dont_use_fat & 1) { if (cluster >= file_ptr -> fx_file_last_physical_cluster) { contents = FX_LAST_CLUSTER_exFAT; }if (cluster >= file_ptr -> fx_file_last_physical_cluster) { ... } else { contents = cluster + 1; }else { ... } }if (file_ptr -> fx_file_dir_entry.fx_dir_entry_dont_use_fat & 1) { ... } else { #endif /* FX_ENABLE_EXFAT */ /* Read the current cluster entry from the FAT. */ status = _fx_utility_FAT_entry_read(media_ptr, cluster, &contents); /* Check the return value. */ if (status != FX_SUCCESS) { #ifdef FX_ENABLE_FAULT_TOLERANT FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr); #endif /* FX_ENABLE_FAULT_TOLERANT */ /* Release media protection. */ FX_UNPROTECT /* Return the error status. */ return(status); }if (status != FX_SUCCESS) { ... } #ifdef FX_ENABLE_EXFAT }else { ... } #endif /* FX_ENABLE_EXFAT */ /* Save the last valid cluster. */ last_cluster = cluster; /* Setup for the next cluster. */ cluster = contents; /* Determine if this is the last written cluster. */ if (bytes_remaining >= bytes_per_cluster) { /* Still more seeking, just decrement the working byte offset. */ bytes_remaining = bytes_remaining - bytes_per_cluster; }if (bytes_remaining >= bytes_per_cluster) { ... } else { /* This is the cluster that contains the seek position. */ break; }else { ... } }while ((cluster >= FX_FAT_ENTRY_START) && (cluster < media_ptr -> fx_media_fat_reserved)) { ... } /* Check for errors in traversal of the FAT chain. */ if (size > (((ULONG64) bytes_per_cluster) * ((ULONG64) cluster_count))) { #ifdef FX_ENABLE_FAULT_TOLERANT FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr); #endif /* FX_ENABLE_FAULT_TOLERANT */ /* Release media protection. */ FX_UNPROTECT /* This is an error that suggests a corrupt file. */ return(FX_FILE_CORRUPT); }if (size > (((ULONG64) bytes_per_cluster) * ((ULONG64) cluster_count))) { ... } /* Position the pointers to the new offset. */ file_ptr -> fx_file_current_physical_cluster = last_cluster; file_ptr -> fx_file_current_relative_cluster = cluster_count - 1; file_ptr -> fx_file_current_logical_sector = ((ULONG)media_ptr -> fx_media_data_sector_start) + (((ULONG64)file_ptr -> fx_file_current_physical_cluster - FX_FAT_ENTRY_START) * ((ULONG)media_ptr -> fx_media_sectors_per_cluster)) + (bytes_remaining / (ULONG)media_ptr -> fx_media_bytes_per_sector); file_ptr -> fx_file_current_relative_sector = (UINT)((bytes_remaining / (ULONG)media_ptr -> fx_media_bytes_per_sector)); file_ptr -> fx_file_current_file_offset = size; file_ptr -> fx_file_current_logical_offset = (ULONG)bytes_remaining % ((ULONG)media_ptr -> fx_media_bytes_per_sector); }if (file_ptr -> fx_file_current_file_offset > file_ptr -> fx_file_current_file_size) { ... } /* Determine how many clusters are actually in-use now. */ cluster_count = (ULONG) (file_ptr -> fx_file_current_available_size + (((ULONG64) bytes_per_cluster) - ((ULONG64) 1)))/bytes_per_cluster; /* Save the number of clusters in-use. */ file_ptr -> fx_file_total_clusters = cluster_count; /* At this point, we are ready to walk list of clusters to find the clusters that can be released. *//* ... */ cluster = file_ptr -> fx_file_first_physical_cluster; #ifdef FX_ENABLE_EXFAT original_last_physical_cluster = file_ptr -> fx_file_last_physical_cluster; #endif /* FX_ENABLE_EXFAT */ #ifdef FX_ENABLE_FAULT_TOLERANT /* Is this the last used cluster? */ if ((cluster_count == 0) && (media_ptr -> fx_media_fault_tolerant_enabled)) { #ifdef FX_ENABLE_EXFAT if (!(file_ptr -> fx_file_dir_entry.fx_dir_entry_dont_use_fat & 1)) { #endif /* FX_ENABLE_EXFAT */ /* Set undo log. */ status = _fx_fault_tolerant_set_FAT_chain(media_ptr, FX_FALSE, FX_FREE_CLUSTER, media_ptr -> fx_media_fat_last, cluster, media_ptr -> fx_media_fat_last); /* Determine if the write was successful. */ if (status != FX_SUCCESS) { FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr); /* Release media protection. */ FX_UNPROTECT /* Return the error code. */ return(status); }if (status != FX_SUCCESS) { ... } #ifdef FX_ENABLE_EXFAT }if (!(file_ptr -> fx_file_dir_entry.fx_dir_entry_dont_use_fat & 1)) { ... } else { /* Set undo log. */ status = _fx_fault_tolerant_set_FAT_chain(media_ptr, FX_TRUE, FX_FREE_CLUSTER, media_ptr -> fx_media_fat_last, cluster, file_ptr -> fx_file_last_physical_cluster + 1); /* Determine if the write was successful. */ if (status != FX_SUCCESS) { FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr); /* Release media protection. */ FX_UNPROTECT /* Return the error code. */ return(status); }if (status != FX_SUCCESS) { ... } }else { ... } #endif /* FX_ENABLE_EXFAT && FX_ENABLE_FAULT_TOLERANT */ file_ptr -> fx_file_last_physical_cluster = cluster; }if ((cluster_count == 0) && (media_ptr -> fx_media_fault_tolerant_enabled)) { ... } /* ... */#endif /* FX_ENABLE_FAULT_TOLERANT */ /* Follow the link of FAT entries. */ while ((cluster >= FX_FAT_ENTRY_START) && (cluster < media_ptr -> fx_media_fat_reserved)) { #ifdef FX_ENABLE_EXFAT if (file_ptr -> fx_file_dir_entry.fx_dir_entry_dont_use_fat & 1) { if (cluster >= original_last_physical_cluster) { contents = FX_LAST_CLUSTER_exFAT; }if (cluster >= original_last_physical_cluster) { ... } else { contents = cluster + 1; }else { ... } }if (file_ptr -> fx_file_dir_entry.fx_dir_entry_dont_use_fat & 1) { ... } else { #endif /* FX_ENABLE_EXFAT */ /* Read the current cluster entry from the FAT. */ status = _fx_utility_FAT_entry_read(media_ptr, cluster, &contents); /* Check the return value. */ if (status != FX_SUCCESS) { #ifdef FX_ENABLE_FAULT_TOLERANT FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr); #endif /* FX_ENABLE_FAULT_TOLERANT */ /* Release media protection. */ FX_UNPROTECT /* Return the error status. */ return(status); }if (status != FX_SUCCESS) { ... } #ifdef FX_ENABLE_EXFAT }else { ... } #endif /* FX_ENABLE_EXFAT */ /* Determine if are more clusters to release. */ if (cluster_count > 0) { /* Decrement the number of clusters. */ cluster_count--; /* Is this the last used cluster? */ if (cluster_count == 0) { #ifdef FX_ENABLE_EXFAT if (!(file_ptr -> fx_file_dir_entry.fx_dir_entry_dont_use_fat & 1)) #endif /* FX_ENABLE_EXFAT */ { #ifdef FX_ENABLE_FAULT_TOLERANT if (media_ptr -> fx_media_fault_tolerant_enabled) { /* Set undo phase. */ media_ptr -> fx_media_fault_tolerant_state |= FX_FAULT_TOLERANT_STATE_SET_FAT_CHAIN; /* Read the current cluster entry from the FAT. */ status = _fx_utility_FAT_entry_read(media_ptr, cluster, &contents); /* Check the return value. */ if (status != FX_SUCCESS) { FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr); /* Release media protection. */ FX_UNPROTECT /* Return the error status. */ return(status); }if (status != FX_SUCCESS) { ... } /* Set undo log. */ status = _fx_fault_tolerant_set_FAT_chain(media_ptr, FX_FALSE, cluster, media_ptr -> fx_media_fat_last, contents, media_ptr -> fx_media_fat_last); /* Determine if the write was successful. */ if (status != FX_SUCCESS) { FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr); /* Release media protection. */ FX_UNPROTECT /* Return the error code. */ return(status); }if (status != FX_SUCCESS) { ... } }if (media_ptr -> fx_media_fault_tolerant_enabled) { ... } /* ... */#endif /* FX_ENABLE_FAULT_TOLERANT */ /* Yes, it should be designated as last cluster. */ status = _fx_utility_FAT_entry_write(media_ptr, cluster, media_ptr -> fx_media_fat_last); /* Check the return value. */ if (status) { #ifdef FX_ENABLE_FAULT_TOLERANT FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr); #endif /* FX_ENABLE_FAULT_TOLERANT */ /* Release media protection. */ FX_UNPROTECT /* Return the error status. */ return(status); }if (status) { ... } #ifdef FX_ENABLE_FAULT_TOLERANT if (media_ptr -> fx_media_fault_tolerant_enabled) { /* Clear undo phase. */ media_ptr -> fx_media_fault_tolerant_state &= (UCHAR)(~FX_FAULT_TOLERANT_STATE_SET_FAT_CHAIN & 0xff); }if (media_ptr -> fx_media_fault_tolerant_enabled) { ... } /* ... */#endif /* FX_ENABLE_FAULT_TOLERANT */ ...} #if defined(FX_ENABLE_EXFAT) && defined(FX_ENABLE_FAULT_TOLERANT) else if (media_ptr -> fx_media_fault_tolerant_enabled) { /* Set undo log. */ status = _fx_fault_tolerant_set_FAT_chain(media_ptr, FX_TRUE, cluster, media_ptr -> fx_media_fat_last, contents, file_ptr -> fx_file_last_physical_cluster + 1); /* Determine if the write was successful. */ if (status != FX_SUCCESS) { FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr); /* Release media protection. */ FX_UNPROTECT /* Return the error code. */ return(status); }if (status != FX_SUCCESS) { ... } }else if (media_ptr -> fx_media_fault_tolerant_enabled) { ... } /* ... */#endif /* FX_ENABLE_EXFAT && FX_ENABLE_FAULT_TOLERANT */ file_ptr -> fx_file_last_physical_cluster = cluster; #ifdef FX_FAULT_TOLERANT /* Flush the cached individual FAT entries. */ status = _fx_utility_FAT_flush(media_ptr); /* Check the return value. */ if (status) { #ifdef FX_ENABLE_FAULT_TOLERANT FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr); #endif /* FX_ENABLE_FAULT_TOLERANT */ /* Release media protection. */ FX_UNPROTECT /* Return the error status. */ return(status); }if (status) { ... } /* ... */#endif }if (cluster_count == 0) { ... } }if (cluster_count > 0) { ... } else { /* This is a cluster after the clusters used by the file, release it back to the media. *//* ... */ #ifdef FX_ENABLE_FAULT_TOLERANT if (media_ptr -> fx_media_fault_tolerant_enabled == FX_FALSE) { #endif /* FX_ENABLE_FAULT_TOLERANT */ #ifdef FX_ENABLE_EXFAT if (!(file_ptr -> fx_file_dir_entry.fx_dir_entry_dont_use_fat & 1)) { #endif /* FX_ENABLE_EXFAT */ status = _fx_utility_FAT_entry_write(media_ptr, cluster, FX_FREE_CLUSTER); /* Check the return value. */ if (status) { #ifdef FX_ENABLE_FAULT_TOLERANT FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr); #endif /* FX_ENABLE_FAULT_TOLERANT */ /* Release media protection. */ FX_UNPROTECT /* Return the error status. */ return(status); }if (status) { ... } #ifdef FX_ENABLE_EXFAT }if (!(file_ptr -> fx_file_dir_entry.fx_dir_entry_dont_use_fat & 1)) { ... } if (media_ptr -> fx_media_FAT_type == FX_exFAT) { /* Mark the cluster as free. */ _fx_utility_exFAT_cluster_state_set(media_ptr, cluster, FX_EXFAT_BITMAP_CLUSTER_FREE); }if (media_ptr -> fx_media_FAT_type == FX_exFAT) { ... } #endif /* FX_ENABLE_EXFAT */ /* Increment the number of available clusters. */ media_ptr -> fx_media_available_clusters++; #ifdef FX_ENABLE_FAULT_TOLERANT }if (media_ptr -> fx_media_fault_tolerant_enabled == FX_FALSE) { ... } #endif /* FX_ENABLE_FAULT_TOLERANT */ }else { ... } /* Setup for the next cluster. */ cluster = contents; }while ((cluster >= FX_FAT_ENTRY_START) && (cluster < media_ptr -> fx_media_fat_reserved)) { ... } /* Determine if we need to adjust the number of leading consecutive clusters. */ if (file_ptr -> fx_file_consecutive_cluster > file_ptr -> fx_file_total_clusters) { /* Adjust the leading consecutive cluster count. */ file_ptr -> fx_file_consecutive_cluster = file_ptr -> fx_file_total_clusters; }if (file_ptr -> fx_file_consecutive_cluster > file_ptr -> fx_file_total_clusters) { ... } /* Determine if the file available size has been truncated to zero. */ if (file_ptr -> fx_file_current_available_size == 0) { /* Yes, the first cluster has already been released. Update the file info to indicate the file has no clusters. *//* ... */ file_ptr -> fx_file_last_physical_cluster = 0; file_ptr -> fx_file_first_physical_cluster = 0; file_ptr -> fx_file_current_physical_cluster = 0; file_ptr -> fx_file_current_logical_sector = 0; file_ptr -> fx_file_current_relative_cluster = 0; file_ptr -> fx_file_current_relative_sector = 0; file_ptr -> fx_file_current_available_size = 0; file_ptr -> fx_file_consecutive_cluster = 1; }if (file_ptr -> fx_file_current_available_size == 0) { ... } #ifndef FX_DONT_UPDATE_OPEN_FILES /* Search the opened files list to see if the same file is opened for reading. */ open_count = media_ptr -> fx_media_opened_file_count; search_ptr = media_ptr -> fx_media_opened_file_list; while (open_count) { /* Is this file the same file opened for reading? */ if ((search_ptr != file_ptr) && (search_ptr -> fx_file_dir_entry.fx_dir_entry_log_sector == file_ptr -> fx_file_dir_entry.fx_dir_entry_log_sector) && (search_ptr -> fx_file_dir_entry.fx_dir_entry_byte_offset == file_ptr -> fx_file_dir_entry.fx_dir_entry_byte_offset)) { /* Yes, the same file is opened for reading. */ /* Setup the new file size. */ search_ptr -> fx_file_current_file_size = size; /* Setup the new total clusters. */ search_ptr -> fx_file_total_clusters = file_ptr -> fx_file_total_clusters; /* Copy the directory entry. */ search_ptr -> fx_file_dir_entry.fx_dir_entry_cluster = file_ptr -> fx_file_dir_entry.fx_dir_entry_cluster; search_ptr -> fx_file_dir_entry.fx_dir_entry_file_size = file_ptr -> fx_file_dir_entry.fx_dir_entry_file_size; search_ptr -> fx_file_dir_entry.fx_dir_entry_log_sector = file_ptr -> fx_file_dir_entry.fx_dir_entry_log_sector; search_ptr -> fx_file_dir_entry.fx_dir_entry_byte_offset = file_ptr -> fx_file_dir_entry.fx_dir_entry_byte_offset; /* Setup the other file parameters. */ search_ptr -> fx_file_last_physical_cluster = file_ptr -> fx_file_last_physical_cluster; search_ptr -> fx_file_first_physical_cluster = file_ptr -> fx_file_first_physical_cluster; search_ptr -> fx_file_current_available_size = file_ptr -> fx_file_current_available_size; search_ptr -> fx_file_consecutive_cluster = file_ptr -> fx_file_consecutive_cluster; /* Determine if the truncated file is smaller than the current file offset. */ if (search_ptr -> fx_file_current_file_offset > size) { /* Yes, the current file parameters need to be changed since the file was truncated to a position prior to the current file position. *//* ... */ /* Calculate the number of bytes per cluster. */ bytes_per_cluster = ((ULONG)media_ptr -> fx_media_bytes_per_sector) * ((ULONG)media_ptr -> fx_media_sectors_per_cluster); /* At this point, we are ready to walk list of clusters to setup the seek position of this file. *//* ... */ cluster = search_ptr -> fx_file_first_physical_cluster; bytes_remaining = size; last_cluster = 0; cluster_count = 0; /* Follow the link of FAT entries. */ while ((cluster >= FX_FAT_ENTRY_START) && (cluster < media_ptr -> fx_media_fat_reserved)) { /* Increment the number of clusters. */ cluster_count++; #ifdef FX_ENABLE_EXFAT if (file_ptr -> fx_file_dir_entry.fx_dir_entry_dont_use_fat & 1) { if (cluster >= file_ptr -> fx_file_last_physical_cluster) { contents = FX_LAST_CLUSTER_exFAT; }if (cluster >= file_ptr -> fx_file_last_physical_cluster) { ... } else { contents = cluster + 1; }else { ... } }if (file_ptr -> fx_file_dir_entry.fx_dir_entry_dont_use_fat & 1) { ... } else { #endif /* FX_ENABLE_EXFAT */ /* Read the current cluster entry from the FAT. */ status = _fx_utility_FAT_entry_read(media_ptr, cluster, &contents); /* Check the return value. */ if (status != FX_SUCCESS) { #ifdef FX_ENABLE_FAULT_TOLERANT FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr); #endif /* FX_ENABLE_FAULT_TOLERANT */ /* Release media protection. */ FX_UNPROTECT /* Return the error status. */ return(status); }if (status != FX_SUCCESS) { ... } #ifdef FX_ENABLE_EXFAT }else { ... } #endif /* FX_ENABLE_EXFAT */ /* Save the last valid cluster. */ last_cluster = cluster; /* Setup for the next cluster. */ cluster = contents; /* Determine if this is the last written cluster. */ if (bytes_remaining >= bytes_per_cluster) { /* Still more seeking, just decrement the working byte offset. */ bytes_remaining = bytes_remaining - bytes_per_cluster; }if (bytes_remaining >= bytes_per_cluster) { ... } else { /* This is the cluster that contains the seek position. */ break; }else { ... } }while ((cluster >= FX_FAT_ENTRY_START) && (cluster < media_ptr -> fx_media_fat_reserved)) { ... } /* Check for errors in traversal of the FAT chain. */ if (size > (((ULONG64) bytes_per_cluster) * ((ULONG64) cluster_count))) { #ifdef FX_ENABLE_FAULT_TOLERANT FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr); #endif /* FX_ENABLE_FAULT_TOLERANT */ /* Release media protection. */ FX_UNPROTECT /* This is an error that suggests a corrupt file. */ return(FX_FILE_CORRUPT); }if (size > (((ULONG64) bytes_per_cluster) * ((ULONG64) cluster_count))) { ... } /* Position the pointers to the new offset. */ /* Determine if there is at least one cluster. */ if (cluster_count) { /* Calculate real file parameters. */ search_ptr -> fx_file_current_physical_cluster = last_cluster; search_ptr -> fx_file_current_relative_cluster = cluster_count - 1; search_ptr -> fx_file_current_logical_sector = ((ULONG)media_ptr -> fx_media_data_sector_start) + (((ULONG64)search_ptr -> fx_file_current_physical_cluster - FX_FAT_ENTRY_START) * ((ULONG)media_ptr -> fx_media_sectors_per_cluster)) + (bytes_remaining / (ULONG)media_ptr -> fx_media_bytes_per_sector); search_ptr -> fx_file_current_relative_sector = (UINT)((bytes_remaining / (ULONG)media_ptr -> fx_media_bytes_per_sector)); search_ptr -> fx_file_current_file_offset = size; search_ptr -> fx_file_current_logical_offset = (ULONG)bytes_remaining % ((ULONG)media_ptr -> fx_media_bytes_per_sector); }if (cluster_count) { ... } else { /* Calculate zero-length file parameters. */ search_ptr -> fx_file_current_physical_cluster = 0; search_ptr -> fx_file_current_relative_cluster = 0; search_ptr -> fx_file_current_logical_sector = 0; search_ptr -> fx_file_current_relative_sector = 0; search_ptr -> fx_file_current_file_offset = 0; search_ptr -> fx_file_current_logical_offset = 0; }else { ... } }if (search_ptr -> fx_file_current_file_offset > size) { ... } }if ((search_ptr != file_ptr) && (search_ptr -> fx_file_dir_entry.fx_dir_entry_log_sector == file_ptr -> fx_file_dir_entry.fx_dir_entry_log_sector) && (search_ptr -> fx_file_dir_entry.fx_dir_entry_byte_offset == file_ptr -> fx_file_dir_entry.fx_dir_entry_byte_offset)) { ... } /* Adjust the pointer and decrement the search count. */ search_ptr = search_ptr -> fx_file_opened_next; open_count--; }while (open_count) { ... } /* ... */#endif #ifdef FX_FAULT_TOLERANT /* Flush the cached individual FAT entries */ status = _fx_utility_FAT_flush(media_ptr); /* Check the return value. */ if (status) { #ifdef FX_ENABLE_FAULT_TOLERANT FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr); #endif /* FX_ENABLE_FAULT_TOLERANT */ /* Release media protection. */ FX_UNPROTECT /* Return the error status. */ return(status); }if (status) { ... } #ifdef FX_ENABLE_EXFAT /* Flush Bit Map. */ if (media_ptr -> fx_media_FAT_type == FX_exFAT) { _fx_utility_exFAT_bitmap_flush(media_ptr); }if (media_ptr -> fx_media_FAT_type == FX_exFAT) { ... } /* ... */#endif /* FX_ENABLE_EXFAT */ /* ... */ #endif #ifdef FX_ENABLE_FAULT_TOLERANT if (media_ptr -> fx_media_fault_tolerant_enabled && (media_ptr -> fx_media_fault_tolerant_state & FX_FAULT_TOLERANT_STATE_STARTED)) { /* Copy the new file size into the directory entry. */ file_ptr -> fx_file_dir_entry.fx_dir_entry_file_size = file_ptr -> fx_file_current_file_size; }if (media_ptr -> fx_media_fault_tolerant_enabled && (media_ptr -> fx_media_fault_tolerant_state & FX_FAULT_TOLERANT_STATE_STARTED)) { ... } /* Write the directory entry to the media. */ #ifdef FX_ENABLE_EXFAT if (media_ptr -> fx_media_FAT_type == FX_exFAT) { status = _fx_directory_exFAT_entry_write(media_ptr, &(file_ptr -> fx_file_dir_entry), UPDATE_STREAM); }if (media_ptr -> fx_media_FAT_type == FX_exFAT) { ... } else { #endif /* FX_ENABLE_EXFAT */ status = _fx_directory_entry_write(media_ptr, &(file_ptr -> fx_file_dir_entry)); #ifdef FX_ENABLE_EXFAT }else { ... } #endif /* FX_ENABLE_EXFAT */ /* Check for a good status. */ if (status != FX_SUCCESS) { FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr); /* Release media protection. */ FX_UNPROTECT /* Error writing the directory. */ return(status); }if (status != FX_SUCCESS) { ... } /* End transaction. */ status = _fx_fault_tolerant_transaction_end(media_ptr); /* Check for a bad status. */ if (status != FX_SUCCESS) { /* Release media protection. */ FX_UNPROTECT /* Return the bad status. */ return(status); }if (status != FX_SUCCESS) { ... } /* Update maximum size used if necessary. */ if (size < file_ptr -> fx_file_maximum_size_used) { file_ptr -> fx_file_maximum_size_used = size; }if (size < file_ptr -> fx_file_maximum_size_used) { ... } /* ... */#endif /* FX_ENABLE_FAULT_TOLERANT */ /* Release media protection. */ FX_UNPROTECT /* Truncate is complete, return successful status. */ return(FX_SUCCESS); }{ ... }