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_file.h"
#include "fx_utility.h"
#include "fx_directory.h"
#include "fx_fault_tolerant.h"
...
...
_fx_file_extended_allocate(FX_FILE *, ULONG64)
Files
loading...
CodeScopeSTM32 Libraries and Samplesfilexcommon/src/fx_file_extended_allocate.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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/**************************************************************************/ /* */ /* 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_file.h" #include "fx_utility.h" #include "fx_directory.h" 5 includes#ifdef FX_ENABLE_FAULT_TOLERANT #include "fx_fault_tolerant.h" #endif /* FX_ENABLE_FAULT_TOLERANT */ ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _fx_file_extended_allocate PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* William E. Lamie, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function attempts to allocate the number of consecutive */ /* clusters required to satisfy the user's request. If there are */ /* enough clusters, the clusters are allocated and linked to the file. */ /* Otherwise, if there are not enough consecutive clusters, an error */ /* code is returned to the caller. */ /* */ /* INPUT */ /* */ /* file_ptr File control block pointer */ /* size Number of bytes to allocate */ /* */ /* OUTPUT */ /* */ /* return status */ /* */ /* CALLS */ /* */ /* _fx_directory_entry_write Update directory entry */ /* _fx_utility_exFAT_bitmap_flush Flush exFAT allocation bitmap */ /* _fx_utility_exFAT_bitmap_free_cluster_find */ /* Find exFAT free cluster */ /* _fx_utility_exFAT_cluster_state_get Get cluster state */ /* _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_utility_logical_sector_flush Flush the written log sector */ /* _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 */ /* */... /**************************************************************************/ UINT _fx_file_extended_allocate(FX_FILE *file_ptr, ULONG64 size) { UINT status; ULONG i; UINT found; ULONG bytes_per_cluster; ULONG FAT_index; ULONG FAT_value; ULONG clusters; FX_MEDIA *media_ptr; #ifdef FX_ENABLE_EXFAT UCHAR cluster_state; #endif /* FX_ENABLE_EXFAT */ #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) { ... } /* Setup pointer to media structure. */ media_ptr = file_ptr -> fx_file_media_ptr; #ifndef FX_MEDIA_STATISTICS_DISABLE /* Increment the number of times this service has been called. */ media_ptr -> fx_media_file_allocates++;/* ... */ #endif /* Make sure this file is open for writing. */ if (file_ptr -> fx_file_open_mode != FX_OPEN_FOR_WRITE) { /* 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) { ... } /* Determine if the requested allocation is for zero bytes. */ if (size == 0) { /* Return a successful completion - nothing needs to be done. */ return(FX_SUCCESS); }if (size == 0) { ... } /* 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_ALLOCATE, file_ptr, size, file_ptr -> fx_file_current_available_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 */ /* 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) { ... } /* Calculate the number of consecutive clusters needed to satisfy this request. *//* ... */ clusters = (ULONG)(((size + bytes_per_cluster - 1) / bytes_per_cluster)); /* Determine if cluster count is 0. */ if (clusters == 0) { #ifdef FX_ENABLE_FAULT_TOLERANT FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr); #endif /* FX_ENABLE_FAULT_TOLERANT */ /* Release media protection. */ FX_UNPROTECT /* Size overflow when rounding to the next cluster, return an error status. */ return(FX_NO_MORE_SPACE); }if (clusters == 0) { ... } /* Determine if there are enough available clusters on the media. */ if (clusters > media_ptr -> fx_media_available_clusters) { #ifdef FX_ENABLE_FAULT_TOLERANT FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr); #endif /* FX_ENABLE_FAULT_TOLERANT */ /* Release media protection. */ FX_UNPROTECT /* Not enough clusters, return an error status. */ return(FX_NO_MORE_SPACE); }if (clusters > media_ptr -> fx_media_available_clusters) { ... } /* Determine if the requested file allocation would exceed the physical limit of the file. */ if (((file_ptr -> fx_file_current_available_size + (((ULONG64) clusters) * ((ULONG64) bytes_per_cluster))) < file_ptr -> fx_file_current_available_size) || ((file_ptr -> fx_file_current_available_size + (((ULONG64) clusters) * ((ULONG64) bytes_per_cluster))) > 0xFFFFFFFFULL)) { #ifdef FX_ENABLE_FAULT_TOLERANT FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr); #endif /* FX_ENABLE_FAULT_TOLERANT */ /* Release media protection. */ FX_UNPROTECT /* Return the no more space error, since the new file size would be larger than the 32-bit field to represent it in the file's directory entry. *//* ... */ return(FX_NO_MORE_SPACE); }if (((file_ptr -> fx_file_current_available_size + (((ULONG64) clusters) * ((ULONG64) bytes_per_cluster))) < file_ptr -> fx_file_current_available_size) || ((file_ptr -> fx_file_current_available_size + (((ULONG64) clusters) * ((ULONG64) bytes_per_cluster))) > 0xFFFFFFFFULL)) { ... } /* Now we need to find the consecutive clusters. */ FAT_index = FX_FAT_ENTRY_START; found = FX_FALSE; #ifdef FX_ENABLE_EXFAT if ((file_ptr -> fx_file_dir_entry.fx_dir_entry_dont_use_fat & 1) && (file_ptr -> fx_file_last_physical_cluster > FX_FAT_ENTRY_START) && (file_ptr -> fx_file_last_physical_cluster < media_ptr -> fx_media_total_clusters - clusters + FX_FAT_ENTRY_START)) { found = FX_TRUE; /* Try to keep clusters consecutive. */ for (FAT_index = file_ptr -> fx_file_last_physical_cluster + 1; FAT_index < clusters + file_ptr -> fx_file_last_physical_cluster + 1; FAT_index++) { /* Get cluster state. */ status = _fx_utility_exFAT_cluster_state_get(media_ptr, FAT_index, &cluster_state); /* Check for a successful 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 /* Return the error status. */ return(status); }if (status != FX_SUCCESS) { ... } /* Determine if the entry is free. */ if (cluster_state == FX_EXFAT_BITMAP_CLUSTER_OCCUPIED) { found = FX_FALSE; break; }if (cluster_state == FX_EXFAT_BITMAP_CLUSTER_OCCUPIED) { ... } }for (FAT_index = file_ptr -> fx_file_last_physical_cluster + 1; FAT_index < clusters + file_ptr -> fx_file_last_physical_cluster + 1; FAT_index++) { ... } FAT_index = file_ptr -> fx_file_last_physical_cluster + 1; }if ((file_ptr -> fx_file_dir_entry.fx_dir_entry_dont_use_fat & 1) && (file_ptr -> fx_file_last_physical_cluster > FX_FAT_ENTRY_START) && (file_ptr -> fx_file_last_physical_cluster < media_ptr -> fx_media_total_clusters - clusters + FX_FAT_ENTRY_START)) { ... } if (!found) { #endif /* FX_ENABLE_EXFAT */ while (FAT_index <= (media_ptr -> fx_media_total_clusters - clusters + FX_FAT_ENTRY_START)) { /* Determine if enough consecutive FAT entries are available. */ i = 0; #ifdef FX_ENABLE_EXFAT if (media_ptr -> fx_media_FAT_type == FX_exFAT) { do { /* Get cluster state. */ status = _fx_utility_exFAT_cluster_state_get(media_ptr, (FAT_index + i), &cluster_state); /* Check for a successful 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 /* Return the error status. */ return(status); }if (status != FX_SUCCESS) { ... } /* Determine if the entry is free. */ if (cluster_state == FX_EXFAT_BITMAP_CLUSTER_OCCUPIED) { break; }if (cluster_state == FX_EXFAT_BITMAP_CLUSTER_OCCUPIED) { ... } /* Otherwise, increment the consecutive FAT indices. */ i++; ...} while (i < clusters); }if (media_ptr -> fx_media_FAT_type == FX_exFAT) { ... } else { #endif /* FX_ENABLE_EXFAT */ do { /* Read a FAT entry. */ status = _fx_utility_FAT_entry_read(media_ptr, (FAT_index + i), &FAT_value); /* Check for a successful 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 /* Return the error status. */ return(status); }if (status != FX_SUCCESS) { ... } /* Determine if the entry is free. */ if (FAT_value != FX_FREE_CLUSTER) { break; }if (FAT_value != FX_FREE_CLUSTER) { ... } /* Otherwise, increment the consecutive FAT indices. */ i++; ...} while (i < clusters); #ifdef FX_ENABLE_EXFAT }else { ... } #endif /* FX_ENABLE_EXFAT */ /* Determine if we found enough FAT entries. */ if (i >= clusters) { /* Yes, we have found enough FAT entries - set the found flag and get out of this loop. *//* ... */ found = FX_TRUE; break; }if (i >= clusters) { ... } else { #ifdef FX_ENABLE_EXFAT if (media_ptr -> fx_media_FAT_type == FX_exFAT) { /* Find free cluster from exFAT media. */ status = _fx_utility_exFAT_bitmap_free_cluster_find(media_ptr, FAT_index + i + 1, &FAT_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) { ... } if (FAT_value < FAT_index + i + 1) { /* If we wrapped. */ FAT_index = media_ptr -> fx_media_total_clusters + FX_FAT_ENTRY_START; }if (FAT_value < FAT_index + i + 1) { ... } else { FAT_index = FAT_value; }else { ... } }if (media_ptr -> fx_media_FAT_type == FX_exFAT) { ... } else { #endif /* FX_ENABLE_EXFAT */ /* Position to the next possibly free FAT entry. */ FAT_index = FAT_index + i + 1; #ifdef FX_ENABLE_EXFAT }else { ... } #endif /* FX_ENABLE_EXFAT */ }else { ... } }while (FAT_index <= (media_ptr -> fx_media_total_clusters - clusters + FX_FAT_ENTRY_START)) { ... } #ifdef FX_ENABLE_EXFAT }if (!found) { ... } #endif /* Determine if we found enough consecutive clusters to satisfy the request. *//* ... */ if (found) { #ifdef FX_ENABLE_FAULT_TOLERANT if (media_ptr -> fx_media_fault_tolerant_enabled) { /* Record the action that is about to take place. This information would aid the undo process should fault condition happens. *//* ... */ media_ptr -> fx_media_fault_tolerant_state |= FX_FAULT_TOLERANT_STATE_SET_FAT_CHAIN; _fx_fault_tolerant_set_FAT_chain(media_ptr, FX_FALSE, file_ptr -> fx_file_last_physical_cluster, FAT_index, media_ptr -> fx_media_fat_last, media_ptr -> fx_media_fat_last); }if (media_ptr -> fx_media_fault_tolerant_enabled) { ... } /* ... */#endif /* FX_ENABLE_FAULT_TOLERANT */ #ifdef FX_ENABLE_EXFAT if (file_ptr -> fx_file_dir_entry.fx_dir_entry_dont_use_fat & 1) { if ((file_ptr -> fx_file_total_clusters) && (FAT_index != file_ptr -> fx_file_last_physical_cluster + 1)) { /* Clusters are not consecutive. */ file_ptr -> fx_file_dir_entry.fx_dir_entry_dont_use_fat &= (CHAR)0xfe; /* Set 0bit to 0 */ /* Rebuild FAT. */ FAT_value = file_ptr -> fx_file_dir_entry.fx_dir_entry_cluster + file_ptr -> fx_file_total_clusters - 1; /* Last cluster */ for (i = file_ptr -> fx_file_dir_entry.fx_dir_entry_cluster; i < FAT_value; ++i) { status = _fx_utility_FAT_entry_write(media_ptr, i, i + 1); 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 bad status. */ return(status); }if (status != FX_SUCCESS) { ... } }for (i = file_ptr -> fx_file_dir_entry.fx_dir_entry_cluster; i < FAT_value; ++i) { ... } /* Close chain. */ status = _fx_utility_FAT_entry_write(media_ptr, FAT_value, media_ptr -> fx_media_fat_last); 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 bad status. */ return(status); }if (status != FX_SUCCESS) { ... } #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 */ /* Update stream. */ status = _fx_directory_exFAT_entry_write( media_ptr, &(file_ptr -> fx_file_dir_entry), UPDATE_STREAM); 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 bad status. */ return(status); }if (status != FX_SUCCESS) { ... } #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; }if (media_ptr -> fx_media_fault_tolerant_enabled) { ... } /* ... */#endif /* FX_ENABLE_FAULT_TOLERANT */ }if ((file_ptr -> fx_file_total_clusters) && (FAT_index != file_ptr -> fx_file_last_physical_cluster + 1)) { ... } }if (file_ptr -> fx_file_dir_entry.fx_dir_entry_dont_use_fat & 1) { ... } /* ... */#endif /* FX_ENABLE_EXFAT */ /* Update the link pointers in the new clusters. */ for (i = 0; i < (clusters - 1); i++) { #ifdef FX_ENABLE_EXFAT if (!(file_ptr -> fx_file_dir_entry.fx_dir_entry_dont_use_fat & 1) #ifdef FX_ENABLE_FAULT_TOLERANT || (media_ptr -> fx_media_fault_tolerant_enabled == FX_TRUE) #endif /* FX_ENABLE_FAULT_TOLERANT */ ) { #endif /* FX_ENABLE_EXFAT */ /* Update the cluster links. Since the allocation is sequential, we just have to link each FAT entry to the next one. *//* ... */ status = _fx_utility_FAT_entry_write(media_ptr, FAT_index + i, FAT_index + i + 1); /* Check for a bad 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 /* Return the error status. */ return(status); }if (status != FX_SUCCESS) { ... } #ifdef FX_ENABLE_EXFAT }if (!(file_ptr -> fx_file_dir_entry.fx_dir_entry_dont_use_fat & 1) #ifdef FX_ENABLE_FAULT_TOLERANT || (media_ptr -> fx_media_fault_tolerant_enabled == FX_TRUE) #endif /* FX_ENABLE_FAULT_TOLERANT */) { ... } if (media_ptr -> fx_media_FAT_type == FX_exFAT) { /* Mark the cluster as used. */ status = _fx_utility_exFAT_cluster_state_set(media_ptr, FAT_index + i, FX_EXFAT_BITMAP_CLUSTER_OCCUPIED); /* Check for a bad 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 /* Return the error status. */ return(status); }if (status != FX_SUCCESS) { ... } }if (media_ptr -> fx_media_FAT_type == FX_exFAT) { ... } #endif /* FX_ENABLE_EXFAT */ }for (i = 0; i < (clusters - 1); i++) { ... } #ifdef FX_ENABLE_EXFAT if (!(file_ptr -> fx_file_dir_entry.fx_dir_entry_dont_use_fat & 1)) { #endif /* FX_ENABLE_EXFAT */ /* Now place an EOF in the last cluster entry. */ status = _fx_utility_FAT_entry_write(media_ptr, FAT_index + clusters - 1, media_ptr -> fx_media_fat_last); /* Check for a bad 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 /* Return the error status. */ return(status); }if (status != FX_SUCCESS) { ... } #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 used. */ status = _fx_utility_exFAT_cluster_state_set(media_ptr, FAT_index + clusters - 1, FX_EXFAT_BITMAP_CLUSTER_OCCUPIED); /* Check for a bad 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 /* Return the error status. */ return(status); }if (status != FX_SUCCESS) { ... } }if (media_ptr -> fx_media_FAT_type == FX_exFAT) { ... } #endif /* FX_ENABLE_EXFAT */ #ifdef FX_FAULT_TOLERANT /* Flush the cached individual FAT entries */ _fx_utility_FAT_flush(media_ptr); #ifdef FX_ENABLE_EXFAT 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 /* Actually link up the new clusters to the file. */ /* Determine if there are already clusters allocated for this file. */ if (file_ptr -> fx_file_total_clusters) { #ifdef FX_ENABLE_EXFAT if (!(file_ptr -> fx_file_dir_entry.fx_dir_entry_dont_use_fat & 1)) { #endif /* FX_ENABLE_EXFAT */ /* Linkup the last cluster. */ status = _fx_utility_FAT_entry_write(media_ptr, file_ptr -> fx_file_last_physical_cluster, FAT_index); /* Check for a bad 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 /* Return the error status. */ return(status); }if (status != FX_SUCCESS) { ... } #ifdef FX_ENABLE_EXFAT }if (!(file_ptr -> fx_file_dir_entry.fx_dir_entry_dont_use_fat & 1)) { ... } #endif /* FX_ENABLE_EXFAT */ /* Determine if we are adding a sector after a write filled the previously allocated cluster exactly. *//* ... */ if ((file_ptr -> fx_file_current_relative_sector >= (media_ptr -> fx_media_sectors_per_cluster - 1)) && (file_ptr -> fx_file_current_logical_offset >= media_ptr -> fx_media_bytes_per_sector)) { /* Yes, we need to adjust all of the pertinent file parameters for access into this newly allocated cluster. *//* ... */ file_ptr -> fx_file_current_physical_cluster = FAT_index; file_ptr -> fx_file_current_relative_cluster++; file_ptr -> fx_file_current_relative_sector = 0; file_ptr -> fx_file_current_logical_sector = ((ULONG)media_ptr -> fx_media_data_sector_start) + (((ULONG64)(FAT_index - FX_FAT_ENTRY_START)) * ((ULONG)media_ptr -> fx_media_sectors_per_cluster)); file_ptr -> fx_file_current_logical_offset = 0; }if ((file_ptr -> fx_file_current_relative_sector >= (media_ptr -> fx_media_sectors_per_cluster - 1)) && (file_ptr -> fx_file_current_logical_offset >= media_ptr -> fx_media_bytes_per_sector)) { ... } }if (file_ptr -> fx_file_total_clusters) { ... } else { /* These new clusters are also the first! Setup the initial file parameters. *//* ... */ file_ptr -> fx_file_first_physical_cluster = FAT_index; file_ptr -> fx_file_current_physical_cluster = file_ptr -> fx_file_first_physical_cluster; file_ptr -> fx_file_current_relative_cluster = 0; file_ptr -> fx_file_current_logical_sector = ((ULONG)media_ptr -> fx_media_data_sector_start) + (((ULONG64)(file_ptr -> fx_file_first_physical_cluster - FX_FAT_ENTRY_START)) * ((ULONG)media_ptr -> fx_media_sectors_per_cluster)); file_ptr -> fx_file_current_logical_offset = 0; file_ptr -> fx_file_current_file_offset = 0; /* Update the first cluster in the directory entry. */ file_ptr -> fx_file_dir_entry.fx_dir_entry_cluster = FAT_index; }else { ... } /* Remember the last physical cluster. */ file_ptr -> fx_file_last_physical_cluster = FAT_index + clusters - 1; #ifdef FX_ENABLE_EXFAT if (file_ptr -> fx_file_current_available_size > (file_ptr -> fx_file_current_available_size + (bytes_per_cluster * clusters))) { /* 64-bit wrap around condition is present. Just set the available file size to all ones, which is the maximum file size. *//* ... */ file_ptr -> fx_file_current_available_size = 0xFFFFFFFFFFFFFFFFULL; }if (file_ptr -> fx_file_current_available_size > (file_ptr -> fx_file_current_available_size + (bytes_per_cluster * clusters))) { ... } /* ... */#endif /* FX_ENABLE_EXFAT */ /* Check for wrap-around when updating the available size. */ #ifdef FX_ENABLE_EXFAT if ((media_ptr -> fx_media_FAT_type != FX_exFAT) && (file_ptr -> fx_file_current_available_size + (bytes_per_cluster * clusters) > 0xFFFFFFFFUL)) { /* 32-bit wrap around condition is present. Just set the available file size to all ones, which is the maximum file size. *//* ... */ file_ptr -> fx_file_current_available_size = ((ULONG)0xFFFFFFFF); }if ((media_ptr -> fx_media_FAT_type != FX_exFAT) && (file_ptr -> fx_file_current_available_size + (bytes_per_cluster * clusters) > 0xFFFFFFFFUL)) { ... } else { /* Normal condition, update the available size. */ file_ptr -> fx_file_current_available_size = file_ptr -> fx_file_current_available_size + (bytes_per_cluster * clusters); }else { ... } /* ... */#else /* Update the available size. */ file_ptr -> fx_file_current_available_size = file_ptr -> fx_file_current_available_size + (bytes_per_cluster * clusters); /* ... */ #endif /* FX_ENABLE_EXFAT */ /* Increment the total clusters for this file. */ file_ptr -> fx_file_total_clusters = file_ptr -> fx_file_total_clusters + clusters; /* Decrease the available clusters on the media. */ media_ptr -> fx_media_available_clusters = media_ptr -> fx_media_available_clusters - clusters; #if defined(FX_UPDATE_FILE_SIZE_ON_ALLOCATE) || defined(FX_ENABLE_FAULT_TOLERANT) /* Set the file size the current size plus what what was added. */ file_ptr -> fx_file_current_file_size += size; /* 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;/* ... */ #endif #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 */ /* Update the trace event with the new size. */ FX_TRACE_EVENT_UPDATE(trace_event, trace_timestamp, FX_TRACE_FILE_ALLOCATE, 0, 0, 0, file_ptr -> fx_file_current_file_size); /* 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 /* Return the error status. */ return(status); }if (status != FX_SUCCESS) { ... } }if (found) { ... } else { #ifdef FX_ENABLE_FAULT_TOLERANT FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr); #endif /* FX_ENABLE_FAULT_TOLERANT */ /* Release media protection. */ FX_UNPROTECT /* Not enough contiguous space on the media. Return error status. */ return(FX_NO_MORE_SPACE); }else { ... } #ifdef FX_FAULT_TOLERANT /* Flush the cached individual FAT entries */ _fx_utility_FAT_flush(media_ptr);/* ... */ #endif /* Flush the internal logical sector cache. */ status = _fx_utility_logical_sector_flush(media_ptr, ((ULONG64) 1), (ULONG64)(media_ptr -> fx_media_sectors_per_FAT), FX_FALSE); #ifdef FX_ENABLE_FAULT_TOLERANT /* Check for a bad status. */ if (status != FX_SUCCESS) { FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr); /* Release media protection. */ FX_UNPROTECT /* Return the bad status. */ return(status); }if (status != FX_SUCCESS) { ... } if (media_ptr -> fx_media_fault_tolerant_enabled) { /* 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) { ... } /* End transaction. */ status = _fx_fault_tolerant_transaction_end(media_ptr);/* ... */ #endif /* FX_ENABLE_FAULT_TOLERANT */ /* Release media protection. */ FX_UNPROTECT /* Return status to the caller. */ return(status); }{ ... }