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_directory.h"
#include "fx_utility.h"
#include "fx_fault_tolerant.h"
...
...
_fx_file_write(FX_FILE *, void *, ULONG)
Files
loading (2/3)...
SourceVuSTM32 Libraries and Samplesfilexcommon/src/fx_file_write.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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/**************************************************************************/ /* */ /* 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_directory.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_write PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* William E. Lamie, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function writes the specified number of bytes into the file's */ /* data area. The status of the write operation is returned to the */ /* caller. In addition, various internal file pointers in the file */ /* control block are also updated. */ /* */ /* INPUT */ /* */ /* file_ptr File control block pointer */ /* buffer_ptr Buffer pointer */ /* size Number of bytes to write */ /* */ /* OUTPUT */ /* */ /* return status */ /* */ /* CALLS */ /* */ /* _fx_directory_entry_write Update the file's size */ /* _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 written logical sectors */ /* _fx_utility_logical_sector_read Read a logical sector */ /* _fx_utility_logical_sector_write Write a logical sector */ /* _fx_utility_memory_copy Fast memory copy routine */ /* _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), verified */ /* memcpy usage, */ /* resulting in version 6.1 */ /* */... /**************************************************************************/ UINT _fx_file_write(FX_FILE *file_ptr, VOID *buffer_ptr, ULONG size) { UINT status; ULONG64 bytes_remaining; ULONG i; ULONG copy_bytes; ULONG bytes_per_cluster; UCHAR *source_ptr; ULONG first_new_cluster; ULONG last_cluster; ULONG cluster, next_cluster; ULONG FAT_index; ULONG FAT_value; ULONG clusters; ULONG total_clusters; UINT sectors; FX_MEDIA *media_ptr; #ifdef FX_ENABLE_EXFAT UCHAR cluster_state; #endif /* FX_ENABLE_EXFAT */ #ifdef FX_FAULT_TOLERANT_DATA FX_INT_SAVE_AREA #endif #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 #ifdef FX_ENABLE_FAULT_TOLERANT UCHAR data_append = FX_FALSE; /* Whether or not new data would extend beyond the size of the original file. Operations such as append or overwrite could cause the new file to exceed its original size, resulting in data-appending operation. *//* ... */ ULONG copy_head_cluster = 0; /* The first cluster of the original chain that needs to be replaced. */ ULONG copy_tail_cluster = 0; /* The last cluster of the original chain that needs to be replaced. */ ULONG insertion_back; /* The insertion point (back) */ ULONG insertion_front = 0; /* The insertion point (front) */ ULONG replace_clusters = 0; /* The number of clusters to be replaced. */ UCHAR dont_use_fat_old = FX_FALSE; /* Used by exFAT logic to indicate whether or not the FAT table should be used. *//* ... */ #endif /* FX_ENABLE_FAULT_TOLERANT */ /* 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_writes++;/* ... */ #endif #ifdef FX_ENABLE_EXFAT if ((media_ptr -> fx_media_FAT_type != FX_exFAT) && (file_ptr -> fx_file_current_file_offset + size > 0xFFFFFFFFULL))/* ... */ #else if (file_ptr -> fx_file_current_file_offset + size > 0xFFFFFFFFULL) #endif /* FX_ENABLE_EXFAT */ { /* 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); ...} #ifdef FX_ENABLE_FAULT_TOLERANT /* Initialize next cluster of tail. */ insertion_back = media_ptr -> fx_media_fat_last;/* ... */ #endif /* FX_ENABLE_FAULT_TOLERANT */ /* If trace is enabled, insert this event into the trace buffer. */ FX_TRACE_IN_LINE_INSERT(FX_TRACE_FILE_WRITE, file_ptr, buffer_ptr, size, 0, FX_TRACE_FILE_EVENTS, &trace_event, &trace_timestamp) /* Protect against other threads accessing the media. */ FX_PROTECT /* Check for write protect at the media level (set by driver). */ if (media_ptr -> fx_media_driver_write_protect) { /* Release media protection. */ FX_UNPROTECT /* Return write protect error. */ return(FX_WRITE_PROTECT); }if (media_ptr -> fx_media_driver_write_protect) { ... } /* Make sure this file is open for writing. */ if (file_ptr -> fx_file_open_mode != FX_OPEN_FOR_WRITE) { /* 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) { ... } #ifdef FX_ENABLE_FAULT_TOLERANT /* Start transaction. */ _fx_fault_tolerant_transaction_start(media_ptr);/* ... */ #endif /* FX_ENABLE_FAULT_TOLERANT */ /* 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) { ... } /* Initialized first new cluster. */ first_new_cluster = 0; #ifdef FX_ENABLE_FAULT_TOLERANT /* Calculate clusters need to be replaced when fault tolerant is enabled. */ if (media_ptr -> fx_media_fault_tolerant_enabled) { if (file_ptr -> fx_file_current_file_offset == file_ptr -> fx_file_current_file_size) { /* Appending data. No need to replace existing clusters. */ replace_clusters = 0; }if (file_ptr -> fx_file_current_file_offset == file_ptr -> fx_file_current_file_size) { ... } else if (file_ptr -> fx_file_current_file_offset == file_ptr -> fx_file_maximum_size_used) { /* Similar to append data. No actual data after fx_file_maximum_size_used. * No need to replace existing clusters. *//* ... */ replace_clusters = 0; }else if (file_ptr -> fx_file_current_file_offset == file_ptr -> fx_file_maximum_size_used) { ... } else if (((file_ptr -> fx_file_current_file_offset / media_ptr -> fx_media_bytes_per_sector) == ((file_ptr -> fx_file_current_file_offset + size - 1) / media_ptr -> fx_media_bytes_per_sector)) && ((file_ptr -> fx_file_current_file_offset + size) <= file_ptr -> fx_file_current_file_size)) { /* Overwriting data in one sector. No need to replace existing clusters. */ replace_clusters = 0; }else if (((file_ptr -> fx_file_current_file_offset / media_ptr -> fx_media_bytes_per_sector) == ((file_ptr -> fx_file_current_file_offset + size - 1) / media_ptr -> fx_media_bytes_per_sector)) && ((file_ptr -> fx_file_current_file_offset + size) <= file_ptr -> fx_file_current_file_size)) { ... } else if ((file_ptr -> fx_file_current_available_size - file_ptr -> fx_file_current_file_offset) < size) { /* Replace all clusters from current_cluster. */ replace_clusters = (UINT)(file_ptr -> fx_file_total_clusters - file_ptr -> fx_file_current_relative_cluster); copy_head_cluster = file_ptr -> fx_file_current_physical_cluster; data_append = FX_TRUE; }else if ((file_ptr -> fx_file_current_available_size - file_ptr -> fx_file_current_file_offset) < size) { ... } else { /* Replace clusters from current_cluster to the end of written cluster. */ replace_clusters = (UINT)((file_ptr -> fx_file_current_file_offset + size + bytes_per_cluster - 1) / bytes_per_cluster - file_ptr -> fx_file_current_relative_cluster); copy_head_cluster = file_ptr -> fx_file_current_physical_cluster; data_append = FX_FALSE; }else { ... } }if (media_ptr -> fx_media_fault_tolerant_enabled) { ... } /* ... */#endif /* FX_ENABLE_FAULT_TOLERANT */ /* Next, determine if there is enough room to write the specified number of bytes to the clusters already allocated to this file. *//* ... */ if (((file_ptr -> fx_file_current_available_size - file_ptr -> fx_file_current_file_offset) < size) #ifdef FX_ENABLE_FAULT_TOLERANT || (replace_clusters) #endif /* FX_ENABLE_FAULT_TOLERANT */ ) { /* The new request will not fit within the currently allocated clusters. */ /* Calculate the number of additional clusters that must be allocated for this write request. *//* ... */ #ifdef FX_ENABLE_FAULT_TOLERANT /* Find the number of clusters that need to be replaced. */ if (media_ptr -> fx_media_fault_tolerant_enabled) { /* Mark the recovery phase. */ media_ptr -> fx_media_fault_tolerant_state |= FX_FAULT_TOLERANT_STATE_SET_FAT_CHAIN; bytes_remaining = file_ptr -> fx_file_current_file_offset + size; if (replace_clusters > 0) { #ifdef FX_ENABLE_EXFAT if ((media_ptr -> fx_media_FAT_type == FX_exFAT) && (file_ptr -> fx_file_dir_entry.fx_dir_entry_dont_use_fat & 1)) { /* Find the insertion points in the exFAT with bitmap case.. this is a simple case.. */ /* The previous cluster is not initialized. Find it. */ if (copy_head_cluster != file_ptr -> fx_file_first_physical_cluster) { insertion_front = copy_head_cluster - 1; }if (copy_head_cluster != file_ptr -> fx_file_first_physical_cluster) { ... } /* Find copy tail cluster. */ if (data_append == FX_FALSE) { copy_tail_cluster = file_ptr -> fx_file_first_physical_cluster + (ULONG)((bytes_remaining - 1) / bytes_per_cluster); if (copy_tail_cluster != file_ptr -> fx_file_last_physical_cluster) { insertion_back = copy_tail_cluster + 1; }if (copy_tail_cluster != file_ptr -> fx_file_last_physical_cluster) { ... } }if (data_append == FX_FALSE) { ... } }if ((media_ptr -> fx_media_FAT_type == FX_exFAT) && (file_ptr -> fx_file_dir_entry.fx_dir_entry_dont_use_fat & 1)) { ... } else { #endif /* FX_ENABLE_EXFAT */ /* Find previous cluster of copy head cluster. */ /* The previous cluster is not initialized. Find it. */ if (copy_head_cluster != file_ptr -> fx_file_first_physical_cluster) { /* The copy head cluster is not the first cluster of file. */ /* Copy head is not the first cluster of file. */ if (file_ptr -> fx_file_current_relative_cluster < file_ptr -> fx_file_consecutive_cluster) { /* Clusters before current cluster are consecutive. */ insertion_front = copy_head_cluster - 1; bytes_remaining -= file_ptr -> fx_file_current_relative_cluster * bytes_per_cluster; }if (file_ptr -> fx_file_current_relative_cluster < file_ptr -> fx_file_consecutive_cluster) { ... } else { /* Skip consecutive clusters first. */ cluster = file_ptr -> fx_file_first_physical_cluster; /* Loop the link of FAT to find the previous cluster. */ while ((cluster >= FX_FAT_ENTRY_START) && (cluster < media_ptr -> fx_media_fat_reserved)) { /* Reduce remaining bytes. */ bytes_remaining -= bytes_per_cluster; /* Read the current cluster entry from the FAT. */ status = _fx_utility_FAT_entry_read(media_ptr, cluster, &FAT_value); /* 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) { ... } if (FAT_value == copy_head_cluster) { break; }if (FAT_value == copy_head_cluster) { ... } /* Move to next cluster. */ cluster = FAT_value; }while ((cluster >= FX_FAT_ENTRY_START) && (cluster < media_ptr -> fx_media_fat_reserved)) { ... } if ((cluster >= FX_FAT_ENTRY_START) && (cluster < media_ptr -> fx_media_fat_reserved)) { /* Find the previous cluster. */ insertion_front = cluster; }if ((cluster >= FX_FAT_ENTRY_START) && (cluster < media_ptr -> fx_media_fat_reserved)) { ... } else { FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr); /* Release media protection. */ FX_UNPROTECT /* Return the error status. */ return(FX_NOT_FOUND); }else { ... } }else { ... } }if (copy_head_cluster != file_ptr -> fx_file_first_physical_cluster) { ... } /* Find copy tail cluster. */ if (bytes_remaining <= bytes_per_cluster) { /* Only one cluster is modified. */ copy_tail_cluster = copy_head_cluster; }if (bytes_remaining <= bytes_per_cluster) { ... } else if (data_append == FX_FALSE) { /* Search from copy head cluster. */ cluster = copy_head_cluster; FAT_value = FX_FAT_ENTRY_START; /* Loop the link of FAT to find the previous cluster. */ while ((cluster >= FX_FAT_ENTRY_START) && (cluster < media_ptr -> fx_media_fat_reserved)) { if (bytes_remaining <= bytes_per_cluster) { break; }if (bytes_remaining <= bytes_per_cluster) { ... } /* Reduce remaining bytes. */ bytes_remaining -= bytes_per_cluster; /* Read the current cluster entry from the FAT. */ status = _fx_utility_FAT_entry_read(media_ptr, cluster, &FAT_value); /* 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) { ... } /* Move to next cluster. */ cluster = FAT_value; }while ((cluster >= FX_FAT_ENTRY_START) && (cluster < media_ptr -> fx_media_fat_reserved)) { ... } /* Find the previous cluster. */ copy_tail_cluster = FAT_value; }else if (data_append == FX_FALSE) { ... } /* Get the cluster next to copy tail. */ if (data_append == FX_FALSE) { /* Read FAT entry. */ status = _fx_utility_FAT_entry_read(media_ptr, copy_tail_cluster, &insertion_back); /* 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 (data_append == FX_FALSE) { ... } else { insertion_back = media_ptr -> fx_media_fat_last; }else { ... } #ifdef FX_ENABLE_EXFAT }else { ... } #endif /* FX_ENABLE_EXFAT */ }if (replace_clusters > 0) { ... } else { insertion_back = media_ptr -> fx_media_fat_last; }else { ... } }if (media_ptr -> fx_media_fault_tolerant_enabled) { ... } if (file_ptr -> fx_file_current_available_size - file_ptr -> fx_file_current_file_offset < size) { #endif /* FX_ENABLE_FAULT_TOLERANT */ /* Calculate clusters that are needed for data append except ones overwritten. */ clusters = (UINT)((size + (bytes_per_cluster - 1) - (file_ptr -> fx_file_current_available_size - file_ptr -> fx_file_current_file_offset)) / bytes_per_cluster); #ifdef FX_ENABLE_FAULT_TOLERANT }if (file_ptr -> fx_file_current_available_size - file_ptr -> fx_file_current_file_offset < size) { ... } else { clusters = 0; }else { ... } #endif /* FX_ENABLE_FAULT_TOLERANT */ /* Determine if we have enough space left. */ #ifdef FX_ENABLE_FAULT_TOLERANT if (clusters + replace_clusters > media_ptr -> fx_media_available_clusters) #else if (clusters > media_ptr -> fx_media_available_clusters) #endif /* FX_ENABLE_FAULT_TOLERANT */ { #ifdef FX_ENABLE_FAULT_TOLERANT FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr); #endif /* FX_ENABLE_FAULT_TOLERANT */ /* Release media protection. */ FX_UNPROTECT /* Out of disk space. */ return(FX_NO_MORE_SPACE); ...} /* Update the file total cluster count. */ file_ptr -> fx_file_total_clusters = file_ptr -> fx_file_total_clusters + clusters; /* 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 + (ULONG64)bytes_per_cluster * (ULONG64)clusters > 0xFFFFFFFFULL))/* ... */ #else if (file_ptr -> fx_file_current_available_size + (ULONG64)bytes_per_cluster * (ULONG64)clusters > 0xFFFFFFFFULL) #endif /* FX_ENABLE_EXFAT */ { /* 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 = 0xFFFFFFFFULL; ...} else { /* Normal condition, update the available size. */ file_ptr -> fx_file_current_available_size = file_ptr -> fx_file_current_available_size + (ULONG64)bytes_per_cluster * (ULONG64)clusters; }else { ... } #ifdef FX_ENABLE_FAULT_TOLERANT /* Account for newly allocated clusters. */ clusters += replace_clusters;/* ... */ #endif /* FX_ENABLE_FAULT_TOLERANT */ /* Decrease the available clusters in the media control block. */ media_ptr -> fx_media_available_clusters = media_ptr -> fx_media_available_clusters - clusters; #if defined(FX_ENABLE_EXFAT) && defined(FX_ENABLE_FAULT_TOLERANT) /* Get dont_use_fat value. */ if (media_ptr -> fx_media_FAT_type == FX_exFAT) { dont_use_fat_old = (UCHAR)file_ptr -> fx_file_dir_entry.fx_dir_entry_dont_use_fat; }if (media_ptr -> fx_media_FAT_type == FX_exFAT) { ... } /* ... */#endif /* FX_ENABLE_EXFAT && FX_ENABLE_FAULT_TOLERANT */ /* Search for the additional clusters we need. */ total_clusters = media_ptr -> fx_media_total_clusters; #ifdef FX_ENABLE_FAULT_TOLERANT if (replace_clusters > 0) { /* Reset consecutive cluster. */ file_ptr -> fx_file_consecutive_cluster = 1; last_cluster = insertion_front; #ifdef FX_ENABLE_EXFAT if ((file_ptr -> fx_file_dir_entry.fx_dir_entry_dont_use_fat & 1) && (file_ptr -> fx_file_total_clusters > replace_clusters)) { /* Now we should use FAT. */ file_ptr -> fx_file_dir_entry.fx_dir_entry_dont_use_fat &= (CHAR)0xfe; /* Clear bit 0. */ /* Build FAT chain. */ for (i = file_ptr -> fx_file_dir_entry.fx_dir_entry_cluster; i < file_ptr -> fx_file_last_physical_cluster; ++i) { status = _fx_utility_FAT_entry_write(media_ptr, i, i + 1); 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) { ... } }for (i = file_ptr -> fx_file_dir_entry.fx_dir_entry_cluster; i < file_ptr -> fx_file_last_physical_cluster; ++i) { ... } /* Write the last cluster FAT entry. */ status = _fx_utility_FAT_entry_write(media_ptr, i, FX_LAST_CLUSTER_exFAT); 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 ((file_ptr -> fx_file_dir_entry.fx_dir_entry_dont_use_fat & 1) && (file_ptr -> fx_file_total_clusters > replace_clusters)) { ... } /* ... */#endif /* FX_ENABLE_EXFAT */ }if (replace_clusters > 0) { ... } else #endif /* FX_ENABLE_FAULT_TOLERANT */ { last_cluster = file_ptr -> fx_file_last_physical_cluster; }else { ... } FAT_index = media_ptr -> fx_media_cluster_search_start; /* Loop to find the needed clusters. */ while (clusters) { /* Decrease the cluster count. */ clusters--; #ifdef FX_ENABLE_EXFAT if (media_ptr -> fx_media_FAT_type == FX_exFAT) { /* Find a free cluster. */ if (file_ptr -> fx_file_dir_entry.fx_dir_entry_dont_use_fat & 1) { if (last_cluster) { cluster_state = FX_EXFAT_BITMAP_CLUSTER_OCCUPIED; FAT_index = last_cluster + 1; if (FAT_index < media_ptr -> fx_media_total_clusters + FX_FAT_ENTRY_START) { /* Get the state of the cluster. */ status = _fx_utility_exFAT_cluster_state_get(media_ptr, FAT_index, &cluster_state); 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) { ... } }if (FAT_index < media_ptr -> fx_media_total_clusters + FX_FAT_ENTRY_START) { ... } /* Check if we still can do not use FAT. */ if (cluster_state == FX_EXFAT_BITMAP_CLUSTER_FREE) { /* Clusters are still consecutive. */ file_ptr -> fx_file_consecutive_cluster++; }if (cluster_state == FX_EXFAT_BITMAP_CLUSTER_FREE) { ... } else { /* Now we should use FAT. */ file_ptr -> fx_file_dir_entry.fx_dir_entry_dont_use_fat &= (CHAR)0xfe; /* Clear bit 0. */ /* Build FAT chain. */ for (i = file_ptr -> fx_file_dir_entry.fx_dir_entry_cluster; i < last_cluster; ++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 < last_cluster; ++i) { ... } /* Write the last cluster FAT entry. */ status = _fx_utility_FAT_entry_write(media_ptr, last_cluster, FX_LAST_CLUSTER_exFAT); 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) { ... } /* Find free cluster from exFAT media. */ status = _fx_utility_exFAT_bitmap_free_cluster_find(media_ptr, media_ptr -> fx_media_cluster_search_start, &FAT_index); }else { ... } }if (last_cluster) { ... } else { /* Find the first cluster for file. */ status = _fx_utility_exFAT_bitmap_free_cluster_find(media_ptr, media_ptr -> fx_media_cluster_search_start, &FAT_index); }else { ... } }if (file_ptr -> fx_file_dir_entry.fx_dir_entry_dont_use_fat & 1) { ... } else { /* Find free cluster from exFAT media. */ status = _fx_utility_exFAT_bitmap_free_cluster_find(media_ptr, media_ptr -> fx_media_cluster_search_start, &FAT_index); }else { ... } 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) { ... } }if (media_ptr -> fx_media_FAT_type == FX_exFAT) { ... } else { #endif /* FX_ENABLE_EXFAT */ /* Loop to find the first available cluster. */ do { /* Make sure we stop looking after one pass through the FAT table. */ if (!total_clusters) { #ifdef FX_ENABLE_FAULT_TOLERANT FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr); #endif /* FX_ENABLE_FAULT_TOLERANT */ /* Release media protection. */ FX_UNPROTECT /* Something is wrong with the media - the desired clusters were not found in the FAT table. *//* ... */ return(FX_NO_MORE_SPACE); }if (!total_clusters) { ... } /* Read FAT entry. */ status = _fx_utility_FAT_entry_read(media_ptr, FAT_index, &FAT_value); /* 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 bad status. */ return(status); }if (status != FX_SUCCESS) { ... } /* Decrement the total cluster count. */ total_clusters--; /* Determine if the FAT entry is free. */ if (FAT_value == FX_FREE_CLUSTER) { /* Move cluster search pointer forward. */ media_ptr -> fx_media_cluster_search_start = FAT_index + 1; /* Determine if this needs to be wrapped. */ if (media_ptr -> fx_media_cluster_search_start >= (media_ptr -> fx_media_total_clusters + FX_FAT_ENTRY_START)) { /* Wrap the search to the beginning FAT entry. */ media_ptr -> fx_media_cluster_search_start = FX_FAT_ENTRY_START; }if (media_ptr -> fx_media_cluster_search_start >= (media_ptr -> fx_media_total_clusters + FX_FAT_ENTRY_START)) { ... } /* Break this loop. */ break; }if (FAT_value == FX_FREE_CLUSTER) { ... } else { /* FAT entry is not free... Advance the FAT index. */ FAT_index++; /* Determine if we need to wrap the FAT index around. */ if (FAT_index >= (media_ptr -> fx_media_total_clusters + FX_FAT_ENTRY_START)) { /* Wrap the search to the beginning FAT entry. */ FAT_index = FX_FAT_ENTRY_START; }if (FAT_index >= (media_ptr -> fx_media_total_clusters + FX_FAT_ENTRY_START)) { ... } }else { ... } ...} while (FX_TRUE); #ifdef FX_ENABLE_EXFAT }else { ... } #endif /* FX_ENABLE_EXFAT */ /* Determine if we have found the first new cluster yet. */ if (first_new_cluster == 0) { /* Remember the first new cluster. */ first_new_cluster = FAT_index; #ifdef FX_ENABLE_FAULT_TOLERANT if (media_ptr -> fx_media_fault_tolerant_enabled) { /* Set the undo log now. */ if (copy_head_cluster == 0) { status = _fx_fault_tolerant_set_FAT_chain(media_ptr, dont_use_fat_old, file_ptr -> fx_file_current_physical_cluster, first_new_cluster, media_ptr -> fx_media_fat_last, media_ptr -> fx_media_fat_last); }if (copy_head_cluster == 0) { ... } #ifdef FX_ENABLE_EXFAT else if (dont_use_fat_old && (insertion_back == media_ptr -> fx_media_fat_last)) { status = _fx_fault_tolerant_set_FAT_chain(media_ptr, dont_use_fat_old, insertion_front, first_new_cluster, copy_head_cluster, file_ptr -> fx_file_last_physical_cluster + 1); }else if (dont_use_fat_old && (insertion_back == media_ptr -> fx_media_fat_last)) { ... } /* ... */#endif /* FX_ENABLE_EXFAT */ else { status = _fx_fault_tolerant_set_FAT_chain(media_ptr, dont_use_fat_old, insertion_front, first_new_cluster, copy_head_cluster, insertion_back); }else { ... } /* Check for good completion status. */ 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) { ... } }if (media_ptr -> fx_media_fault_tolerant_enabled) { ... } /* ... */#endif /* FX_ENABLE_FAULT_TOLERANT */ }if (first_new_cluster == 0) { ... } /* Make a quick check to see if an empty, cluster-less file is being written to for the first time. *//* ... */ if (last_cluster) { /* Check for the file's cluster. We won't perform this link until the entire FAT chain is built. *//* ... */ if (last_cluster != file_ptr -> fx_file_last_physical_cluster) { #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 */ /* Normal condition - link the last cluster with the new found cluster. *//* ... */ status = _fx_utility_FAT_entry_write(media_ptr, last_cluster, FAT_index); #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 */ }if (last_cluster != file_ptr -> fx_file_last_physical_cluster) { ... } /* Check for a bad FAT write 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 bad status. */ return(status); }if (status != FX_SUCCESS) { ... } /* 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 writing 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 (last_cluster) { ... } else { /* This is the first cluster allocated for the file. Just remember it as being the first and setup the other file pointers accordingly. *//* ... */ file_ptr -> fx_file_first_physical_cluster = FAT_index; file_ptr -> fx_file_current_physical_cluster = FAT_index; file_ptr -> fx_file_current_relative_cluster = 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)); #ifdef FX_ENABLE_FAULT_TOLERANT if (file_ptr -> fx_file_last_physical_cluster == 0) #endif /* FX_ENABLE_FAULT_TOLERANT */ { file_ptr -> fx_file_current_logical_offset = 0; file_ptr -> fx_file_current_file_offset = 0; ...} /* Also remember this as the first cluster in the directory entry. *//* ... */ file_ptr -> fx_file_dir_entry.fx_dir_entry_cluster = FAT_index; }else { ... } #ifdef FX_ENABLE_EXFAT if (media_ptr -> fx_media_FAT_type == FX_exFAT) { /* Update Bitmap */ status = _fx_utility_exFAT_cluster_state_set(media_ptr, FAT_index, FX_EXFAT_BITMAP_CLUSTER_OCCUPIED); 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) { ... } /* Move cluster search pointer forward. */ media_ptr -> fx_media_cluster_search_start = FAT_index + 1; /* Determine if this needs to be wrapped. */ if (media_ptr -> fx_media_cluster_search_start >= media_ptr -> fx_media_total_clusters + FX_FAT_ENTRY_START) { /* Wrap the search to the beginning FAT entry. */ media_ptr -> fx_media_cluster_search_start = FX_FAT_ENTRY_START; }if (media_ptr -> fx_media_cluster_search_start >= media_ptr -> fx_media_total_clusters + FX_FAT_ENTRY_START) { ... } }if (media_ptr -> fx_media_FAT_type == FX_exFAT) { ... } /* ... */#endif /* FX_ENABLE_EXFAT */ /* Otherwise, remember the new FAT index as the last. */ last_cluster = FAT_index; /* Move to the next FAT entry. */ FAT_index = media_ptr -> fx_media_cluster_search_start; }while (clusters) { ... } #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 */ #ifdef FX_ENABLE_FAULT_TOLERANT if (media_ptr -> fx_media_fault_tolerant_enabled) { /* Link the last cluster back to original FAT. */ status = _fx_utility_FAT_entry_write(media_ptr, last_cluster, insertion_back); }if (media_ptr -> fx_media_fault_tolerant_enabled) { ... } else #endif /* FX_ENABLE_FAULT_TOLERANT */ { /* Place an end-of-file marker on the last cluster. */ status = _fx_utility_FAT_entry_write(media_ptr, last_cluster, media_ptr -> fx_media_fat_last); }else { ... } /* Check for a bad FAT write 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 bad 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 */) { ... } #endif /* FX_ENABLE_EXFAT */ /* Determine if the file already had clusters. */ if (file_ptr -> fx_file_last_physical_cluster) { #ifdef FX_ENABLE_EXFAT if (!(file_ptr -> fx_file_dir_entry.fx_dir_entry_dont_use_fat & 1)) { #endif /* FX_ENABLE_EXFAT */ /* Now, link the file's old last cluster to the first cluster of the new chain. */ #ifdef FX_ENABLE_FAULT_TOLERANT if (insertion_front) { status = _fx_utility_FAT_entry_write(media_ptr, insertion_front, first_new_cluster); }if (insertion_front) { ... } else if ((media_ptr -> fx_media_fault_tolerant_enabled == FX_FALSE) || ((replace_clusters == 0) && (first_new_cluster))) { status = _fx_utility_FAT_entry_write(media_ptr, file_ptr -> fx_file_last_physical_cluster, first_new_cluster); }else if ((media_ptr -> fx_media_fault_tolerant_enabled == FX_FALSE) || ((replace_clusters == 0) && (first_new_cluster))) { ... } /* ... */#else status = _fx_utility_FAT_entry_write(media_ptr, file_ptr -> fx_file_last_physical_cluster, first_new_cluster); #endif /* FX_ENABLE_FAULT_TOLERANT */ /* Check for a bad FAT write 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 bad 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 */ }if (file_ptr -> fx_file_last_physical_cluster) { ... } #ifdef FX_FAULT_TOLERANT #ifdef FX_ENABLE_EXFAT if (media_ptr -> fx_media_FAT_type == FX_exFAT) { /* Flush exFAT bitmap. */ _fx_utility_exFAT_bitmap_flush(media_ptr); }if (media_ptr -> fx_media_FAT_type == FX_exFAT) { ... } /* ... */#endif /* FX_ENABLE_EXFAT */ /* Ensure the new FAT chain is properly written to the media. */ /* Flush the cached individual FAT entries */ _fx_utility_FAT_flush(media_ptr);/* ... */ #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); /* Update the last cluster? */ if (insertion_back == media_ptr -> fx_media_fat_last) { /* Yes. Update the file control block with the last physical cluster. */ file_ptr -> fx_file_last_physical_cluster = last_cluster; }if (insertion_back == media_ptr -> fx_media_fat_last) { ... } }if (media_ptr -> fx_media_fault_tolerant_enabled) { ... } else #endif /* FX_ENABLE_FAULT_TOLERANT */ { /* Update the file control block with the last physical cluster. */ file_ptr -> fx_file_last_physical_cluster = last_cluster; }else { ... } }if (((file_ptr -> fx_file_current_available_size - file_ptr -> fx_file_current_file_offset) < size) #ifdef FX_ENABLE_FAULT_TOLERANT || (replace_clusters) #endif /* FX_ENABLE_FAULT_TOLERANT */) { ... } /* Check for a need to increment to the next sector within a previously allocated cluster. *//* ... */ if (file_ptr -> fx_file_current_logical_offset >= media_ptr -> fx_media_bytes_per_sector) { /* Update the sector specific file parameters to start at the next logical sector. *//* ... */ file_ptr -> fx_file_current_logical_sector++; file_ptr -> fx_file_current_relative_sector++; file_ptr -> fx_file_current_logical_offset = 0; }if (file_ptr -> fx_file_current_logical_offset >= media_ptr -> fx_media_bytes_per_sector) { ... } /* At this point there is enough room to perform the file write operation. */ /* Setup local buffer pointer. */ source_ptr = (UCHAR *)buffer_ptr; /* Setup the remaining number of bytes to write. */ bytes_remaining = size; #ifdef FX_ENABLE_FAULT_TOLERANT if (replace_clusters > 0) { /* Force update current cluster and sector. */ file_ptr -> fx_file_current_physical_cluster = first_new_cluster; file_ptr -> fx_file_current_logical_sector = ((ULONG)media_ptr -> fx_media_data_sector_start) + (((ULONG64)(first_new_cluster - FX_FAT_ENTRY_START)) * ((ULONG)media_ptr -> fx_media_sectors_per_cluster)) + file_ptr -> fx_file_current_relative_sector; /* Copy sectors in replaced cluster but not overwritten at the front. */ for (i = 0; i < file_ptr -> fx_file_current_relative_sector; i++) { status = _fx_utility_logical_sector_read(media_ptr, ((ULONG)media_ptr -> fx_media_data_sector_start) + (((ULONG)(copy_head_cluster - FX_FAT_ENTRY_START)) * ((ULONG)media_ptr -> fx_media_sectors_per_cluster)) + i, media_ptr -> fx_media_memory_buffer, ((ULONG) 1), FX_DATA_SECTOR); /* Check for good completion status. */ 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) { ... } /* Write back the current logical sector. */ status = _fx_utility_logical_sector_write(media_ptr, ((ULONG)media_ptr -> fx_media_data_sector_start) + (((ULONG)(file_ptr -> fx_file_current_physical_cluster - FX_FAT_ENTRY_START)) * ((ULONG)media_ptr -> fx_media_sectors_per_cluster)) + i, media_ptr -> fx_media_memory_buffer, ((ULONG) 1), FX_DATA_SECTOR); /* Check for good completion status. */ 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) { ... } }for (i = 0; i < file_ptr -> fx_file_current_relative_sector; i++) { ... } }if (replace_clusters > 0) { ... } /* ... */#endif /* FX_ENABLE_FAULT_TOLERANT */ /* Loop to write all of the bytes. */ while (bytes_remaining) { /* Determine if a beginning or ending partial write is required. */ if ((file_ptr -> fx_file_current_logical_offset) || (bytes_remaining < media_ptr -> fx_media_bytes_per_sector)) { /* A partial sector write is required. */ /* Read the current logical sector. */ #ifdef FX_ENABLE_FAULT_TOLERANT if (replace_clusters > 0) { if (file_ptr -> fx_file_current_physical_cluster == first_new_cluster) { /* It's at beginning. */ status = _fx_utility_logical_sector_read(media_ptr, ((ULONG)media_ptr -> fx_media_data_sector_start) + (((ULONG)(copy_head_cluster - FX_FAT_ENTRY_START)) * ((ULONG)media_ptr -> fx_media_sectors_per_cluster)) + file_ptr -> fx_file_current_relative_sector, media_ptr -> fx_media_memory_buffer, (ULONG)1, FX_DATA_SECTOR); }if (file_ptr -> fx_file_current_physical_cluster == first_new_cluster) { ... } else if (data_append == FX_FALSE) { /* It's at ending. */ status = _fx_utility_logical_sector_read(media_ptr, ((ULONG)media_ptr -> fx_media_data_sector_start) + (((ULONG)(copy_tail_cluster - FX_FAT_ENTRY_START)) * ((ULONG)media_ptr -> fx_media_sectors_per_cluster)) + file_ptr -> fx_file_current_relative_sector, media_ptr -> fx_media_memory_buffer, (ULONG)1, FX_DATA_SECTOR); }else if (data_append == FX_FALSE) { ... } else { /* It's at ending. */ status = _fx_utility_logical_sector_read(media_ptr, file_ptr -> fx_file_current_logical_sector, media_ptr -> fx_media_memory_buffer, ((ULONG) 1), FX_DATA_SECTOR); }else { ... } }if (replace_clusters > 0) { ... } else #endif /* FX_ENABLE_FAULT_TOLERANT */ { status = _fx_utility_logical_sector_read(media_ptr, file_ptr -> fx_file_current_logical_sector, media_ptr -> fx_media_memory_buffer, ((ULONG) 1), FX_DATA_SECTOR); }else { ... } /* Check for good completion 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) { ... } /* Copy the appropriate number of bytes into the destination buffer. */ copy_bytes = media_ptr -> fx_media_bytes_per_sector - file_ptr -> fx_file_current_logical_offset; /* Check to see if only a portion of the sector needs to be copied. *//* ... */ if (copy_bytes > bytes_remaining) { /* Adjust the number of bytes to copy. */ copy_bytes = (ULONG)bytes_remaining; }if (copy_bytes > bytes_remaining) { ... } /* Actually perform the memory copy. */ _fx_utility_memory_copy(source_ptr, ((UCHAR *)media_ptr -> fx_media_memory_buffer) + /* Use case of memcpy is verified. */ file_ptr -> fx_file_current_logical_offset, copy_bytes); /* Write back the current logical sector. */ status = _fx_utility_logical_sector_write(media_ptr, file_ptr -> fx_file_current_logical_sector, media_ptr -> fx_media_memory_buffer, ((ULONG) 1), FX_DATA_SECTOR); /* Check for good completion 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) { ... } /* Increment the logical sector byte offset. */ file_ptr -> fx_file_current_logical_offset = file_ptr -> fx_file_current_logical_offset + copy_bytes; /* Adjust the remaining bytes to read. */ bytes_remaining = bytes_remaining - copy_bytes; /* Adjust the pointer to the source buffer. */ source_ptr = source_ptr + copy_bytes; }if ((file_ptr -> fx_file_current_logical_offset) || (bytes_remaining < media_ptr -> fx_media_bytes_per_sector)) { ... } else { /* Attempt to write multiple sectors directly to the media. */ /* Calculate the number of whole sectors to write. */ sectors = (UINT)(bytes_remaining / media_ptr -> fx_media_bytes_per_sector); next_cluster = cluster = file_ptr -> fx_file_current_physical_cluster; for (i = (media_ptr -> fx_media_sectors_per_cluster - file_ptr -> fx_file_current_relative_sector); i < sectors; i += media_ptr -> fx_media_sectors_per_cluster) { #ifdef FX_ENABLE_EXFAT if (file_ptr -> fx_file_dir_entry.fx_dir_entry_dont_use_fat & 1) { cluster++; }if (file_ptr -> fx_file_dir_entry.fx_dir_entry_dont_use_fat & 1) { ... } else { #endif /* FX_ENABLE_EXFAT */ status = _fx_utility_FAT_entry_read(media_ptr, cluster, &next_cluster); /* Determine if an error is present. */ if ((status != FX_SUCCESS) || (next_cluster < FX_FAT_ENTRY_START) || (next_cluster > media_ptr -> fx_media_fat_reserved)) { #ifdef FX_ENABLE_FAULT_TOLERANT FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr); #endif /* FX_ENABLE_FAULT_TOLERANT */ /* Release media protection. */ FX_UNPROTECT /* Send error message back to caller. */ if (status != FX_SUCCESS) { return(status); }if (status != FX_SUCCESS) { ... } else { return(FX_FILE_CORRUPT); }else { ... } }if ((status != FX_SUCCESS) || (next_cluster < FX_FAT_ENTRY_START) || (next_cluster > media_ptr -> fx_media_fat_reserved)) { ... } if (next_cluster != cluster + 1) { break; }if (next_cluster != cluster + 1) { ... } else { cluster = next_cluster; }else { ... } #ifdef FX_ENABLE_EXFAT }else { ... } #endif /* FX_ENABLE_EXFAT */ }for (i = (media_ptr -> fx_media_sectors_per_cluster - file_ptr -> fx_file_current_relative_sector); i < sectors; i += media_ptr -> fx_media_sectors_per_cluster) { ... } if (i < sectors) { sectors = i; }if (i < sectors) { ... } /* Perform the data write directly from the user's buffer of the appropriate number of sectors. *//* ... */ status = _fx_utility_logical_sector_write(media_ptr, file_ptr -> fx_file_current_logical_sector, source_ptr, (ULONG) sectors, FX_DATA_SECTOR); /* Check for good completion 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) { ... } /* Now adjust the various file pointers. */ /* Increment the current logical sector. Subtract one from the sector count because we are going to use the logical offset to do additional sector/cluster arithmetic below. *//* ... */ file_ptr -> fx_file_current_logical_sector = file_ptr -> fx_file_current_logical_sector + (sectors - 1); /* Move the relative cluster and sector as well. */ file_ptr -> fx_file_current_relative_cluster = file_ptr -> fx_file_current_relative_cluster + (file_ptr -> fx_file_current_relative_sector + (sectors - 1)) / media_ptr -> fx_media_sectors_per_cluster; file_ptr -> fx_file_current_relative_sector = (file_ptr -> fx_file_current_relative_sector + (sectors - 1)) % media_ptr -> fx_media_sectors_per_cluster; /* Increment the logical sector byte offset. */ file_ptr -> fx_file_current_logical_offset = media_ptr -> fx_media_bytes_per_sector; file_ptr -> fx_file_current_physical_cluster = cluster; /* Adjust the remaining bytes. */ bytes_remaining = bytes_remaining - (((ULONG)media_ptr -> fx_media_bytes_per_sector) * sectors); /* Adjust the pointer to the source buffer. */ source_ptr = source_ptr + (((ULONG)media_ptr -> fx_media_bytes_per_sector) * sectors); }else { ... } /* At this point, we have either written a partial sector or have successfully written one or more whole sectors. Determine if we are at the end of the current logical sector. *//* ... */ if (file_ptr -> fx_file_current_logical_offset >= media_ptr -> fx_media_bytes_per_sector) { /* Determine if we are at the exact physical end of the file. */ if ((bytes_remaining == 0) && ((file_ptr -> fx_file_current_file_offset + size) >= file_ptr -> fx_file_current_available_size)) { /* Skip the following file parameter adjustments. The next write will detect the logical offset out of the range of the sector and reset all of the pertinent information. *//* ... */ break; }if ((bytes_remaining == 0) && ((file_ptr -> fx_file_current_file_offset + size) >= file_ptr -> fx_file_current_available_size)) { ... } /* We need to move to the next logical sector, but first determine if the next logical sector is within the same cluster. *//* ... */ /* Increment the current relative sector in the cluster. */ file_ptr -> fx_file_current_relative_sector++; /* Determine if this is in a new cluster. */ if (file_ptr -> fx_file_current_relative_sector >= media_ptr -> fx_media_sectors_per_cluster) { /* Yes, we need to move to the next cluster. */ #ifdef FX_ENABLE_EXFAT if (file_ptr -> fx_file_dir_entry.fx_dir_entry_dont_use_fat & 1) { next_cluster = file_ptr -> fx_file_current_physical_cluster + 1; }if (file_ptr -> fx_file_dir_entry.fx_dir_entry_dont_use_fat & 1) { ... } else { #endif /* FX_ENABLE_EXFAT */ /* Read the FAT entry of the current cluster to find the next cluster. *//* ... */ status = _fx_utility_FAT_entry_read(media_ptr, file_ptr -> fx_file_current_physical_cluster, &next_cluster); /* Determine if an error is present. */ if ((status != FX_SUCCESS) || (next_cluster < FX_FAT_ENTRY_START) || (next_cluster > media_ptr -> fx_media_fat_reserved)) { #ifdef FX_ENABLE_FAULT_TOLERANT FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr); #endif /* FX_ENABLE_FAULT_TOLERANT */ /* Release media protection. */ FX_UNPROTECT /* Send error message back to caller. */ if (status != FX_SUCCESS) { return(status); }if (status != FX_SUCCESS) { ... } else { return(FX_FILE_CORRUPT); }else { ... } }if ((status != FX_SUCCESS) || (next_cluster < FX_FAT_ENTRY_START) || (next_cluster > media_ptr -> fx_media_fat_reserved)) { ... } #ifdef FX_ENABLE_EXFAT }else { ... } #endif /* FX_ENABLE_EXFAT */ /* Otherwise, we have a new cluster. Save it in the file control block and calculate a new logical sector value. *//* ... */ file_ptr -> fx_file_current_physical_cluster = next_cluster; file_ptr -> fx_file_current_relative_cluster++; file_ptr -> fx_file_current_logical_sector = ((ULONG)media_ptr -> fx_media_data_sector_start) + ((((ULONG64)next_cluster) - FX_FAT_ENTRY_START) * ((ULONG)media_ptr -> fx_media_sectors_per_cluster)); file_ptr -> fx_file_current_relative_sector = 0; }if (file_ptr -> fx_file_current_relative_sector >= media_ptr -> fx_media_sectors_per_cluster) { ... } else { /* Still within the same cluster so just increment the logical sector. *//* ... */ file_ptr -> fx_file_current_logical_sector++; }else { ... } /* In either case, we are now positioned at a new sector so clear the logical sector offset. *//* ... */ file_ptr -> fx_file_current_logical_offset = 0; }if (file_ptr -> fx_file_current_logical_offset >= media_ptr -> fx_media_bytes_per_sector) { ... } }while (bytes_remaining) { ... } #ifdef FX_ENABLE_FAULT_TOLERANT if (((replace_clusters > 0) && (data_append == FX_FALSE)) && (file_ptr -> fx_file_current_logical_offset || file_ptr -> fx_file_current_relative_sector)) { /* Copy sectors in replaced cluster but not overwritten at the end. */ if (file_ptr -> fx_file_current_logical_offset == 0) { i = file_ptr -> fx_file_current_relative_sector; }if (file_ptr -> fx_file_current_logical_offset == 0) { ... } else { i = file_ptr -> fx_file_current_relative_sector + 1; }else { ... } for (; i < media_ptr -> fx_media_sectors_per_cluster; i++) { status = _fx_utility_logical_sector_read(media_ptr, ((ULONG)media_ptr -> fx_media_data_sector_start) + (((ULONG)(copy_tail_cluster - FX_FAT_ENTRY_START)) * ((ULONG)media_ptr -> fx_media_sectors_per_cluster)) + i, media_ptr -> fx_media_memory_buffer, ((ULONG) 1), FX_DATA_SECTOR); /* Check for good completion status. */ 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) { ... } /* Write back the current logical sector. */ status = _fx_utility_logical_sector_write(media_ptr, ((ULONG)media_ptr -> fx_media_data_sector_start) + (((ULONG)(file_ptr -> fx_file_current_physical_cluster - FX_FAT_ENTRY_START)) * ((ULONG)media_ptr -> fx_media_sectors_per_cluster)) + i, media_ptr -> fx_media_memory_buffer, ((ULONG) 1), FX_DATA_SECTOR); /* Check for good completion status. */ 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) { ... } }for (; i < media_ptr -> fx_media_sectors_per_cluster; i++) { ... } }if (((replace_clusters > 0) && (data_append == FX_FALSE)) && (file_ptr -> fx_file_current_logical_offset || file_ptr -> fx_file_current_relative_sector)) { ... } /* ... */#endif /* FX_ENABLE_FAULT_TOLERANT */ /* Adjust the current file offset accordingly. */ file_ptr -> fx_file_current_file_offset = file_ptr -> fx_file_current_file_offset + 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; /* Determine if this write was done past the previous file size. */ if (file_ptr -> fx_file_current_file_offset > file_ptr -> fx_file_current_file_size) { /* Yes, we have written past the previous end of the file. Update the file size. *//* ... */ file_ptr -> fx_file_current_file_size = file_ptr -> fx_file_current_file_offset; #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 size. */ search_ptr -> fx_file_current_file_size = file_ptr -> fx_file_current_file_offset; /* Setup the new 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 last cluster. This really isn't used during reading, but it is nice to keep things consistent. *//* ... */ search_ptr -> fx_file_last_physical_cluster = file_ptr -> fx_file_last_physical_cluster; /* Update the available clusters as well. */ search_ptr -> fx_file_current_available_size = file_ptr -> fx_file_current_available_size; /* Determine if an empty file was previously opened. */ if (search_ptr -> fx_file_total_clusters == 0) { /* Setup initial parameters. */ search_ptr -> fx_file_total_clusters = file_ptr -> fx_file_total_clusters; search_ptr -> fx_file_current_physical_cluster = file_ptr -> fx_file_first_physical_cluster; search_ptr -> fx_file_current_relative_cluster = 0; search_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)); search_ptr -> fx_file_current_relative_sector = 0; search_ptr -> fx_file_current_logical_offset = 0; search_ptr -> fx_file_current_file_offset = 0; }if (search_ptr -> fx_file_total_clusters == 0) { ... } }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 }if (file_ptr -> fx_file_current_file_offset > file_ptr -> fx_file_current_file_size) { ... } /* Finally, mark this file as modified. */ file_ptr -> fx_file_modified = FX_TRUE; #ifdef FX_FAULT_TOLERANT_DATA /* Ensure that all file data is flushed out. */ /* Flush the internal logical sector cache. */ _fx_utility_logical_sector_flush(media_ptr, 1, media_ptr -> fx_media_total_sectors, FX_FALSE); /* Lockout interrupts for time/date access. */ FX_DISABLE_INTS /* Set the new time and date. */ file_ptr -> fx_file_dir_entry.fx_dir_entry_time = _fx_system_time; file_ptr -> fx_file_dir_entry.fx_dir_entry_date = _fx_system_date; /* Restore interrupts. */ FX_RESTORE_INTS #ifdef FX_ENABLE_FAULT_TOLERANT 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) { ... } /* ... */#endif /* FX_ENABLE_FAULT_TOLERANT */ /* 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) { ... } /* ... */#endif /* Update the trace event with the bytes written. */ FX_TRACE_EVENT_UPDATE(trace_event, trace_timestamp, FX_TRACE_FILE_WRITE, 0, 0, 0, size) #ifdef FX_ENABLE_FAULT_TOLERANT /* 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 (file_ptr -> fx_file_current_file_offset > file_ptr -> fx_file_maximum_size_used) { file_ptr -> fx_file_maximum_size_used = file_ptr -> fx_file_current_file_offset; }if (file_ptr -> fx_file_current_file_offset > file_ptr -> fx_file_maximum_size_used) { ... } /* ... */#endif /* FX_ENABLE_FAULT_TOLERANT */ /* Invoke file write callback. */ if (file_ptr -> fx_file_write_notify) { file_ptr -> fx_file_write_notify(file_ptr); }if (file_ptr -> fx_file_write_notify) { ... } /* Release media protection. */ FX_UNPROTECT /* Return a successful status to the caller. */ return(FX_SUCCESS); }{ ... }
Details
Show:
from
Types: Columns:
This file uses the notable symbols shown below. Click anywhere in the file to view more details.