fs_internal.c
185 KB
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
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
/*****************************************************************************
* Copyright Statement:
* --------------------
* This software is protected by Copyright and the information contained
* herein is confidential. The software may not be copied and the information
* contained herein may not be used or disclosed except with the written
* permission of MediaTek Inc. (C) 2005
*
* BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
* THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
* RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
* AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
* NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
* SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
* SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
* THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
* NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
* SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
*
* BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
* LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
* AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
* OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
* MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
*
* THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
* WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
* LAWS PRINCIPLES. ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
* RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
* THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
*
*****************************************************************************/
/*****************************************************************************
*
* Filename:
* ---------
* fs_internal.h
*
* Project:
* --------
* Maui
*
* Description:
* ------------
* This file defines the internals of file system abstraction layer
*
* Author:
* -------
* -------
*
*============================================================================
* HISTORY
* Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
*------------------------------------------------------------------------------
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
* removed!
* removed!
* removed!
* removed!
* removed!
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
*
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
*
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*------------------------------------------------------------------------------
* Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
*============================================================================
****************************************************************************/
#include "kal_general_types.h"
#include "kal_public_api.h"
#include "fs_internal_def.h"
#include "fs_gprot.h"
#include "rtfiles.h"
#include "rtfex.h"
#include "rtfbuf.h"
#include "fs_kal.h"
#include "fs_internal.h"
#include "fs_internal_api.h"
#include "kal_trace.h"
#include "fs_utility.h"
#include "fs_trc.h" // "stack_config.h" is included by "kal_release.h"
#include "setjmp.h"
#include "string.h"
/************************* Internal Use *************************/
// FS trace
#define fs_util_trace_err_noinfo(error_code) fs_util_trace_err_slim(error_code, fs_internal_c, __LINE__)
// Assert
#define fs_assert_local(expr) fs_assert(expr, fs_internal_c)
#define fs_ext_assert_local(expr, e1, e2, e3) fs_ext_assert(expr, fs_internal_c, e1, e2, e3)
//Global Data
#ifdef __FS_CHECKDRIVE_SUPPORT__
kal_bool g_CheckDrive = KAL_FALSE;
#endif
// External & Dynamic Device
kal_bool g_ExternalDevice = KAL_FALSE; // init as not configured
// Abort Mechanism
kal_bool g_Xdelete = KAL_TRUE; //must be init with true
//Outside Function
extern void nvram_space_reserve(kal_uint32 *size_from_code);
extern void nvram_get_folder_name(WCHAR *nvram_folder_name);
extern int Check_NORFlash_Formatted(void);
#ifdef __FS_QM_SUPPORT__
static int FolderInQuotaSet(WCHAR * Folder);
#endif
#ifdef __FS_CHECKDRIVE_SUPPORT__
static int ScanFindFirst(WCHAR * NamePattern, RTFDirEntry * DirPos);
static int ScanFindNext(RTFHANDLE Handle, WCHAR * FileName, RTFDirEntry * DirPos);
static void ScanFATDelete(InternScanDataStruct *SDCD, RTFCluster Cluster);
#endif
#ifdef __P_PROPRIETARY_COPYRIGHT__
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#endif
/* ---------------------------------------------------------------------------------- */
/* Conventions For Source Code Maintain in fs_internal.c */
/* > *Must* put FS Trace Log with XRAISE error in rtfiles.c and fs_func.c */
/* however, sub-routines here usually are middleware between rtfiles and fs_func */
/* you may skip FS Trace if the XRAISE doesn't need log */
/* ---------------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------------- */
/* Retrieve the system time
*/
static kal_uint32 GetTime(void)
{
kal_uint32 time;
kal_get_time(&time);
return time;
}
/* ------------------------------------------------------------------------------- */
/* Close all files on this device and then discard all related buffer */
void CloseDevice(RTFDevice * Dev)
{
int i;
RTFile * f;
if(Dev == NULL) return;
//Lock RTF and device before calling this function
FlushAllBuffers(Dev);
DiscardAllBuffers(Dev);
for (i=0, f = gFS_Data.FileTable; i<FS_MAX_FILES; i++, f++)
{
if ((f->Lock != 0) && (f->Dev == Dev))
{
//don't call RTFClose here cuz it will unmount device
FreeFTSlot(f);
}
}
//should not unlock here
}
/* ------------------------------------------------------------------------------- */
/* Lookup device number by DriveLetter , 0 if not found */
int FindDeviceNumberByDriveIdx(int DriveLetter)
{
RTFDrive *Drive = NULL;
Drive = (RTFDrive*)fs_conf_get_drv_struct_by_drv_letter(DriveLetter);
if (NULL != Drive)
{
if (Drive->Dev != NULL)
{
return Drive->Dev->DeviceNumber;
}
}
return 0;
}
/* ------------------------------------------------------------------------------- */
/* Lookup device type by translate to flag query */
RTFDevice * FindFirstDeviceByType(FS_DEVICE_TYPE_ENUM dtype)
{
UINT match;
RTFDevice * Dev = gFS_DeviceList;
match = fs_conf_get_devflag_by_devtype(dtype);
for (; Dev->DeviceType; Dev++)
{
if (Dev->DeviceFlags & match)
{
return Dev;
}
}
return NULL;
}
/* ------------------------------------------------------------------------------- */
/* Mount Drive and Issue BatchCountFreeClusters on the Fixed Device Again
* NOTE: This sub-routine DO NOT check argument, use it careful!!
*/
int ReMountDriveAndCountFreeClusters(RTFDrive *Drive)
{
int Result = RTF_NO_ERROR;
fs_assert_local(Drive != NULL);
XTRY
case XCODE:
SafeLock(MT_LOCK_RTF | MT_LOCK_DEV, Drive->Dev, RTF_INFINITE);
DiscardAllBuffers(Drive->Dev);
MountLogicalDrive(Drive, HasFileSystem);
BatchCountFreeClusters(Drive);
break;
default:
Result = XVALUE; //API dose not need XHANDLED
break;
case XFINALLY:
SafeUnlock(MT_LOCK_RTF | MT_LOCK_DEV);
break;
XEND_API
return Result;
}
/* ------------------------------------------------------------------------------- */
int ReleaseFH(void * TaskId)
{
RTFile * f;
UINT i;
RTFSYSLockMutex(RTFLock, RTF_INFINITE);
for (i=0, f = gFS_Data.FileTable; i<FS_MAX_FILES; i++, f++)
{
if (TaskId)
{
if (f->Task == TaskId)
FreeFTSlot(f);
}
else if ((f->Task == kal_get_current_thread_ID()) && (f->Task != NULL))
{
FreeFTSlot(f);
}
}
RTFSYSFreeMutex(RTFLock);
return RTF_NO_ERROR;
}
/* ------------------------------------------------------------------------------- */
int CountUsedFH(void * TaskId)
{
RTFile * f;
UINT i;
int Result=0;
RTFSYSLockMutex(RTFLock, RTF_INFINITE);
for (i=0, f = gFS_Data.FileTable; i<FS_MAX_FILES; i++, f++)
{
if (TaskId)
{
if (f->Task == TaskId) Result++;
}
else if (f->Unique)
{
Result++;
}
}
RTFSYSFreeMutex(RTFLock);
return Result;
}
/* ------------------------------------------------------------------------------- */
/* Copy file to file, this function get at least 512B mem */
static int CopyFileSyncLastDateTime(RTFHANDLE FileSrc, RTFHANDLE FileDst)
{
RTFile * volatile f1 = NULL;
RTFile * volatile f2 = NULL;
XTRY
case XCODE:
f1 = ParseFileHandle(FileSrc);
/* do-not sync date time with virtual file file handle */
if (f1->SpecialKind == FileMapFile) break;
RTFSYSFreeMutex(RTFLock);
f2 = ParseFileHandle(FileDst);
f2->DirEntry.Dir.DateTime = f1->DirEntry.Dir.DateTime;
f2->DirEntry.Dir.FileSize = f1->DirEntry.Dir.FileSize;
// inherit the attributes in copying process
f2->DirEntry.Dir.Attributes = f1->DirEntry.Dir.Attributes;
UpdateDirEntry(f2);
break;
default:
break;
case XFINALLY:
if (f2 != NULL) /* imply f1 != NULL, see above */
{
UnlockDevice(f2->Dev);
RTFSYSLockMutex(RTFLock, RTF_INFINITE);
UnlockDevice(f1->Dev);
}
else if (f1 != NULL)
{
SafeLock(MT_LOCK_RTF | MT_LOCK_DEV, f1->Dev, RTF_INFINITE); /* it may raise an exception after release the system lock */
UnlockDevice(f1->Dev);
}
break;
XEND_API
return RTF_NO_ERROR;
}
int CopyFileLightWeight(const WCHAR * SrcFullPath, const WCHAR * DstFullPath, FS_ProgressCallback Progress, BYTE * Buffer, int BufferLen, kal_uint32 caller_address)
{
RTFHANDLE FHandle1 = 0, FHandle2 = 0;
int volatile Result = RTF_NO_ERROR;
kal_char *Content = NULL;
UINT wLength1, wLength2 = 0, wLength3 = 0;
UINT allLength = 0, okLength = 0;
kal_uint32 LastPgsTime;
char need_delete = 0;
kal_bool flush_chain_head = KAL_TRUE;
#ifdef __FS_DEDICATED_BUFFER__
kal_uint32 dedicated_buffer_allocated_len = 0;
#endif
#ifdef __FS_DEDICATED_BUFFER__
// If BufferLen < size of FS Dedicated Buffer, try to get FS Dedicated Buffer
if (Buffer)
{
BufferLen &= 0xFFFFFE00; // let buffer length be 512 x N
if (BufferLen != 0)
{
Content = (kal_char*)Buffer;
}
else // buffer < 512 bytes, NOT allowed!
{
Result = MT_FAIL_GET_MEM;
goto CopyReturn;
}
}
else // no external buffer
{
BufferLen = MTBufAlloc(FS_BUFF_SIZE_MOVE, (unsigned char**)&Content, FS_INT_DBUF_ALLOC_GREEDY);
if (BufferLen > 0) // allocate buffer successfully
{
if (BufferLen < FS_DEFAULT_BUFF_SIZE_COPY_FILE) // dedicated buffer size < control buffer size
{
MTBufFree(BufferLen, (unsigned char**)&Content); // Content will be set as NULL
// go through to allocate control buffer
}
else // dedicated buffer size >= control buffer size
{
dedicated_buffer_allocated_len = BufferLen;
BufferLen &= 0xFFFFFE00;
}
}
// if get dedicated buffer failed, go through to get control buffer
}
if (NULL == Content) // working buffer is still NULL
#endif /* __FS_DEDICATED_BUFFER__ */
{
if (!Buffer)
{
Content = get_ctrl_buffer(FS_DEFAULT_BUFF_SIZE_COPY_FILE);
BufferLen = FS_DEFAULT_BUFF_SIZE_COPY_FILE;
}
else // this should not happen!
{
Result = MT_FAIL_GET_MEM;
goto CopyReturn;
}
}
fs_util_trace_info1(TRACE_FUNC, FS_INFO_COPY_FILE_BUFFER_SIZE, BufferLen, NULL);
FHandle1 = RTFOpen(SrcFullPath, RTF_READ_ONLY | RTF_OPEN_NO_DIR | RTF_OPEN_SHARED, caller_address);
if (FHandle1 < RTF_NO_ERROR)
{
Result = FHandle1;
goto CopyReturn;
}
RTFGetFileSize(FHandle1, &wLength1);
FHandle2 = RTFOpen(DstFullPath, RTF_READ_WRITE | RTF_OPEN_NO_DIR | RTF_CREATE, caller_address);
if (FHandle2 < RTF_NO_ERROR)
{
Result = FHandle2;
goto CopyReturn;
}
allLength = wLength1;
LastPgsTime = GetTime() & MT_PGS_PERIOD_MASK;
while(wLength1 > 0)
{
if (wLength1 >= BufferLen)
{
wLength2 = BufferLen;
wLength1 -= BufferLen;
}
else
{
kal_mem_set(Content, 0, BufferLen); //reduce time, do it here only
wLength2 = wLength1;
wLength1 = 0;
}
Result = RTFRead(FHandle1, Content, wLength2, &wLength3);
if (Result < RTF_NO_ERROR)
goto CopyReturn;
Result = RTFWrite(FHandle2, Content, wLength2, &wLength3);
if (Result < RTF_NO_ERROR)
goto CopyReturn;
okLength += wLength3;
if (Progress != NULL)
{
if (LastPgsTime != (GetTime() & MT_PGS_PERIOD_MASK))
{
LastPgsTime = GetTime() & MT_PGS_PERIOD_MASK;
Progress(FS_MOVE_PGS_ING, allLength, okLength, FHandle1);
}
}
if (flush_chain_head)
{
/*
* For sudden power lose.
* Force to commit dir and first fat entry, so that users is able to see and delete the gargabe
*/
RTFCommit(FHandle2, FS_COMMIT_DEFAULT);
flush_chain_head = KAL_FALSE;
}
}
CopyFileSyncLastDateTime(FHandle1, FHandle2);
CopyReturn:
#ifdef __FS_DEDICATED_BUFFER__
if (dedicated_buffer_allocated_len > 0)
{
MTBufFree(dedicated_buffer_allocated_len, (unsigned char**)&Content);
}
else
#endif /* __FS_DEDICATED_BUFFER__ */
{
if (Content && !Buffer)
free_ctrl_buffer(Content);
}
if(FHandle1 > 0) RTFClose(FHandle1);
if(FHandle2 > 0)
{
need_delete = 1; // destination file was created, need to be deleted
/*
* RTFCommit will commit all dirty buffers by the order of logical sector number.
* To reach the maximum performance, call RTFCommit() to flush buffers in order first. (W10.32)
*/
RTFCommit(FHandle2, FS_COMMIT_DEFAULT);
RTFClose(FHandle2);
}
if(Result < RTF_NO_ERROR)
{
if(Progress) Progress(FS_MOVE_PGS_FAIL, 0, 0, Result);
if (need_delete == 1)
{
#ifndef __FS_TRACE_SUPPORT__
RTFDelete(DstFullPath);
#else // __FS_TRACE_SUPPORT__
{
int Tmp_Result;
Tmp_Result = RTFDelete(DstFullPath);
if (Tmp_Result < RTF_NO_ERROR)
{
fs_util_trace_err_noinfo(Tmp_Result);
fs_util_trace_info0(TRACE_ERROR, FS_ERR_FS_MOVE_DELETE_FILE_ERROR, NULL);
fs_util_trace_str(TRACE_ERROR | MT_TRACE_INFO_WSTR, (void*)DstFullPath);
}
}
#endif // !__FS_TRACE_SUPPORT__
}
}
return Result;
}
/* This subroutine fetch continguous cluster chain and fill into ClusterArray */
static int CopyFileListClusterChain(RTFHANDLE FileSrc, RTFCluster *ClusterArray, int ArraySize, UINT *CurrFilePointer)
{
RTFile * volatile f1 = NULL;
int volatile idx=0;
kal_int32 fat_prefetch_size;
XTRY
case XCODE:
f1 = ParseFileHandle(FileSrc);
if (f1->Cluster != 0)
{
fat_prefetch_size = GetFATPrefetchSectors(f1);
while (f1->Cluster < RTF_CLUSTER_CHAIN_END && idx < ArraySize)
{
RTFileCheck_Aborted(f1);
ClusterArray[idx++] = f1->Cluster;
f1->LastCluster = f1->Cluster;
f1->Cluster = GetClusterValue(f1->Drive, f1->LastCluster, fat_prefetch_size);
f1->FilePointer += f1->Drive->ClusterSize;
if (f1->Cluster != f1->LastCluster + 1) break;
}
}
*CurrFilePointer = f1->FilePointer;
break;
default:
break;
case XFINALLY:
if (f1 != NULL) UnlockDevice(f1->Dev);
break;
XEND_API
return idx;
}
int CopyFileOnSameDrive(const WCHAR * SrcFullPath, const WCHAR * DstFullPath, FS_ProgressCallback Progress, BYTE * Buffer, int BufferLen, kal_uint32 caller_address)
{
RTFHANDLE FHandle1 = 0, FHandle2 = 0;
RTFCluster *ClustersArray = NULL;
int ReadCluster, WrittenCluster;
int volatile Result = RTF_NO_ERROR;
UINT allLength = 0, okLength = 0;
kal_uint32 LastPgsTime;
int need_canceling = 0;
FHandle1 = RTFOpen(SrcFullPath, RTF_READ_ONLY | RTF_OPEN_NO_DIR | RTF_OPEN_SHARED, caller_address);
if (FHandle1 < RTF_NO_ERROR)
{
Result = FHandle1;
goto CopyReturn;
}
FHandle2 = RTFOpen(DstFullPath, RTF_READ_WRITE | RTF_OPEN_NO_DIR | RTF_CREATE, caller_address);
if (FHandle2 < RTF_NO_ERROR)
{
Result = FHandle2;
goto CopyReturn;
}
if(Buffer==NULL)
{
ClustersArray = get_ctrl_buffer(512);
}
else
{
if(BufferLen<512)
{
Result = MT_FAIL_GET_MEM;
goto CopyReturn;
}
ClustersArray = (RTFCluster*)Buffer;
}
kal_mem_set(ClustersArray, 0xFF, 512); //reduce time, do it here only
RTFGetFileSize(FHandle1, &allLength);
LastPgsTime = GetTime() & MT_PGS_PERIOD_MASK;
need_canceling = 1;
/* Fill Contingous Clusters Chain into ClustersArray */
ReadCluster = CopyFileListClusterChain(FHandle1, ClustersArray, FS_MAX_COPY_CLUSTER, &okLength);
while (ReadCluster > 0)
{
/* Copy to Destination file */
WrittenCluster = MTCopyFileByClusterChain(FHandle2, ClustersArray, ReadCluster);
if (WrittenCluster < 0)
{
Result = WrittenCluster;
goto CopyReturn;
}
/* Check Progress */
if (Progress != NULL)
{
if (LastPgsTime != (GetTime() & MT_PGS_PERIOD_MASK))
{
LastPgsTime = GetTime() & MT_PGS_PERIOD_MASK;
Progress(FS_MOVE_PGS_ING, allLength, okLength, FHandle1);
}
}
/* Fill Contingous Clusters Chain into ClustersArray, Iteration */
ReadCluster = CopyFileListClusterChain(FHandle1, ClustersArray, FS_MAX_COPY_CLUSTER, &okLength);
}
if (ReadCluster == 0)
{
if(allLength>okLength)
{
Result = RTF_FAT_ALLOC_ERROR;
}
else
{
CopyFileSyncLastDateTime(FHandle1, FHandle2);
}
}
else
{
Result = ReadCluster;
}
CopyReturn:
if (ClustersArray && !Buffer) free_ctrl_buffer(ClustersArray);
if (FHandle1 > 0) RTFClose(FHandle1);
if (FHandle2 > 0) RTFClose(FHandle2);
if (Result < RTF_NO_ERROR)
{
int Tmp_Result;
if(Progress&&need_canceling) Progress(FS_MOVE_PGS_FAIL, 0, 0, Result);
Tmp_Result = RTFDelete(DstFullPath);
if (Tmp_Result < RTF_NO_ERROR)
{
fs_util_trace_err_noinfo(Tmp_Result);
fs_util_trace_info0(TRACE_ERROR, FS_ERR_FS_MOVE_DELETE_FILE_ERROR, NULL);
fs_util_trace_str(TRACE_ERROR | MT_TRACE_INFO_WSTR, (void*)DstFullPath);
}
}
return Result;
}
int CreateCopyDestPath(const WCHAR * SrcFullPath, const WCHAR * DstFullPath)
{
RTFHANDLE FHandle = 0;
UINT U;
UINT i;
RTFile * f;
char *ptr, *ptr2;
int Result = RTF_NO_ERROR;
FHandle = RTFOpen(SrcFullPath, RTF_READ_ONLY | RTF_OPEN_NO_DIR | RTF_OPEN_SHARED, 0);
if (FHandle < RTF_NO_ERROR)
{
Result = FHandle;
return Result;
}
U = FHandle >> (4*sizeof(int));
i = FHandle & ((1 << (4*sizeof(int))) - 1);
f = gFS_Data.FileTable + i;
ptr = kal_dchar_strrchr((char*)DstFullPath, '\\') + 2;
if(f->DirEntry.LongPos.Cluster)
{
Result = fs_srv_get_name_by_pos(f, NULL, NULL, (WCHAR *)ptr, MT_MAX_WIDE_PATH-((UINT)ptr-(UINT)DstFullPath), &f->DirEntry.LongPos, FS_FIND_DEFAULT);
}
else // only SFN is existed
{
ptr2 = kal_dchar_strrchr((char*)SrcFullPath, '\\') + 2;
U = (MT_MAX_WIDE_PATH-((UINT)ptr-(UINT)DstFullPath))/2;
for(i=0; *(((WCHAR*)ptr2)+i) && i < U; i++) *(((WCHAR*)ptr)+i)=*(((WCHAR*)ptr2)+i);
if(i>=U)
{
Result = MT_PATH_OVER_LEN_ERROR;
}
else
{
*(((WCHAR*)ptr)+i) = 0;
}
}
if (FHandle > 0) RTFClose(FHandle);
return Result;
}
#ifdef __FS_CHECKDRIVE_SUPPORT__
/* ------------------------------------------------------------------------------- */
/* This function do "cd..". This function will keep the last back slash */
static int CdUp(WCHAR * Path)
{
UINT idx;
if(Path[kal_wstrlen((WCHAR *)Path)-1] == 0x5C)
{
Path[kal_wstrlen((WCHAR *)Path)-1] = 0;
Path[kal_wstrlen((WCHAR *)Path)] = 0;
}
idx = kal_wstrlen((WCHAR *)Path);
while((Path[idx] != 0x5C) && (idx > 0))
{
Path[idx] = 0;
idx--;
}
return idx;
}
#endif
int fs_srv_get_name_by_pos(RTFile * f, const WCHAR * Pattern, RTFDOSDirEntry * FileInfo, WCHAR * FileName, UINT MaxLength, RTFDirLocation * Pos, UINT Flag)
{
int volatile Result = RTF_NO_ERROR;
RTFDOSDirEntry *D;
LFNDirEntry *LD;
int Kind, LFNIndex = -1;
RTFDrive *Drive = NULL;
RTFDirLocation Pos_Copy;
kal_bool volatile Release_Mem = KAL_FALSE;
WCHAR * volatile FindName = NULL;
kal_uint32 FindLength = 0;
// get drive
if (f)
{
Drive = f->Drive;
}
else if (Pattern)
{
Drive = (RTFDrive*)fs_conf_get_drv_struct_by_drv_letter(Pattern[0]);
}
if (NULL == Drive)
{
return RTF_PATH_NOT_FOUND;
}
Pos_Copy.Cluster = Pos->Cluster;
Pos_Copy.Index = Pos->Index;
XTRY //exception handler for GetDir
case XCODE:
SafeLock(MT_LOCK_RTF | MT_LOCK_DEV, Drive->Dev, RTF_INFINITE);
D = GetDirPrefetch(Drive, &Pos_Copy, 1); //check for current dir entry
if (D == NULL)
{
XRAISE(RTF_NO_MORE_FILES);
}
Kind = EntryType(D);
if(Kind != InUse) //try to make LFN
{
if(Kind == Lfn)
{
if(Flag == FS_FIND_LFN_TRUNC)
{
FindName = get_ctrl_buffer(MT_MAX_WIDE_NAME);
// if get_ctrl_buffer failed, it will cause fatal inside. No error handling should be made here.
Release_Mem = KAL_TRUE;
kal_mem_set(FindName, 0, MT_MAX_WIDE_NAME);
FindLength = MT_MAX_WIDE_NAME;
}
else
{
FindName = FileName;
FindLength = MaxLength;
}
}
while(Kind == Lfn)
{
LD = (void*) D;
if (LD->Ord & 0x40) // it's the start of an LFN
{
char * Limit;
LFNIndex = LD->Ord & 0x3F;
Limit = ((char *)FindName) + ((LFNIndex-1)*13*2 + 2*LFNCharCount(LD));
if (Limit > (((char *)FindName) + FindLength - 1))
{
/*
* FS_Move(): If destination is too long with LFN, bypass SFN and return
* FS_PATH_OVER_LEN_ERROR directly.
*
* Another policy is use SFN as destination file name, however SFN
* should be transform to Unicode first before create it, otherwise
* the result file name will be a mess because native encoding should
* not be used to create file.
*/
fs_util_trace_err_noinfo(MT_PATH_OVER_LEN_ERROR);
XRAISE(MT_PATH_OVER_LEN_ERROR);
//LFNIndex = -1; // old policy, SFN will be used below.
}
else
{
Limit[0] = '\0';
Limit[1] = '\0';
}
}
if(LFNIndex != (LD->Ord & 0x3F) || (LFNIndex <= 0))
LFNIndex = -1;
else
CopyLFN((char *)FindName + --LFNIndex * 26, LD);
D = NextDirPrefetch(Drive, &Pos_Copy, 1);
/*
* Disk Corruption: LFN entries are followed by a NULL entry, set Kind as
* NeverUsed and leave while-loop, then FS_FILE_NOT_FOUND will be returned below.
*
* Avoid NULL D causing data abort in EntryType(). (W10.19)
*/
if (NULL == D)
{
XRAISE(RTF_FILE_NOT_FOUND);
}
Kind = EntryType(D);
}
if (LFNIndex == 0) //forget checksum to speedup
{
if (Release_Mem)
{
WORD i;
for (i = 0 ; i < (MaxLength / 2 - 1); i++)
FileName[i] = FindName[i];
FileName[i] = 0x0000;
}
if (FileInfo)
{
*FileInfo = *D;
FileInfo->NTReserved = MT_ENUM_FIND_LFN;
}
break; // break to XFINALLY
}
}
if(Kind == InUse)
{
Result = MakeShortFileName(D, (char *)FileName, MaxLength);
if (RTF_NO_ERROR == Result)
{
FileNameExtendToWCHAR((char *)FileName, FS_MAX_SFN_NATIVE_FILE_NAME_LENGTH_B);
MTCheckFileNameCase(FileName, MaxLength, D->NTReserved);
if(FileInfo)
{
*FileInfo = *D;
if (FileInfo->FileName[0] == 0x05)
FileInfo->FileName[0] = 0xE5;
FileInfo->NTReserved = MT_ENUM_FIND_SFN;
}
break;
}
else if (RTF_STRING_BUFFER_TOO_SMALL == Result)
{
fs_util_trace_err_noinfo(MT_PATH_OVER_LEN_ERROR);
XRAISE(MT_PATH_OVER_LEN_ERROR);
}
else
{
XRAISE(RTF_NO_MORE_FILES);
}
}
else
{
XRAISE(RTF_FILE_NOT_FOUND);
}
break;
default:
Result = XVALUE; //API dose not need XHANDLED
break;
case XFINALLY:
if (Release_Mem)
{
free_ctrl_buffer(FindName);
}
SafeUnlock(MT_LOCK_RTF | MT_LOCK_DEV);
break;
XEND_API
return Result;
}
/* ------------------------------------------------------------------------------- */
/* Special FinFirst for FindReset */
int FindFirst(const WCHAR * NamePattern, FS_Pattern_Struct * PatternArray, UINT PatternNum,
BYTE ArrayMask, BYTE Attr, BYTE AttrMask, RTFDOSDirEntry * FileInfo,
WCHAR * FileName, UINT MaxLength, RTFDirLocation * Pos_Hint, UINT *DirCluster)
{
int volatile Result;
RTFile * volatile f = NULL;
//const RTFDOSDirEntry * D;
RTFDirLocation * Pos;
BYTE * volatile A;
XTRY
case XCODE:
f = ParseFileName((char *)NamePattern);
RTFileCheck_NormalFile_InvalidFilename(f);
f->Flags = RTF_OPEN_DIR | RTF_READ_ONLY | RTF_CACHE_DATA;
if(SearchFile(f, SEARCH_PARENT, (char *)NamePattern, NULL))
{
if(f->DirEntry.DirCluster == 0)
{
MakePseudoRootDirEntry(f->Drive, &f->DirEntry);
if (FileInfo)
*FileInfo = f->DirEntry.Dir;
setASCII(FileName, 0, '\\');
setASCII(FileName, 1, 0);
AttrMask = Attr = 0xFF;
}
else
SET_FIRST_FILE_CLUSTER(f->DirEntry.Dir, f->DirEntry.DirCluster);
}
#ifdef __FS_OPEN_HINT__
else // else we found the parent (return 0)
{
/*
* f->DirEntry.Dir now is the parent of target folders/files.
* Copy its start cluster to f->DirEntry.DirCluster to let
* FS keep DirCluster with new location.
*
* Note. FindNext will not touch f->DirEntry.DirCluster
*/
f->DirEntry.DirCluster = FIRST_FILE_CLUSTER(f->DirEntry.Dir);
}
// keep DirCluster
*DirCluster = f->DirEntry.DirCluster;
#endif /* __FS_OPEN_HINT__ */
InitFilePointer(f);
A = (void*) &f->LastCluster;
Pos = (void*) &f->Cluster;
Pos->Index--;
A[0] = Attr;
A[1] = AttrMask | Attr;
Result = MakeNewFileHandle(f);
break;
default:
Result = XVALUE;
break;
case XFINALLY:
if (f){
if (Result < RTF_NO_ERROR)
FreeFTSlotAndDevice(f);
else
UnlockDevice(f->Dev);
}
break;
XEND_API
if ((Result >= RTF_NO_ERROR) && (A[0] != 0xFF) && (A[1] != 0xFF))
{
int R = fs_srv_findnext(Result, PatternArray, PatternNum, ArrayMask, (FS_DOSDirEntry*)FileInfo, FileName, MaxLength, FS_FIND_DEFAULT, (FS_FileLocationHint *)Pos_Hint);
if (R == RTF_NO_ERROR)
return Result;
else
{
RTFFindClose(Result);
return R;
}
}
else
return Result;
}
/* ------------------------------------------------------------------------------- */
int GetFirstClusterByFileName(const WCHAR *FileName, UINT * Cluster)
{
int volatile Result = RTF_NO_ERROR;
RTFile * volatile f = NULL;
XTRY
case XCODE:
f = ParseFileName((char *)FileName);
RTFileCheck_NormalFile_InvalidFilename(f);
if (!SearchFile(f, SEARCH_FILES, (char *)FileName, NULL))
XRAISE(RTF_PATH_NOT_FOUND);
*Cluster = FIRST_FILE_CLUSTER(f->DirEntry.Dir);
break;
default:
Result = XVALUE; //API dose not need XHANDLED
break;
case XFINALLY:
if (f != NULL)
FreeFTSlotAndDevice(f);
break;
XEND_API
return Result;
}
/* ------------------------------------------------------------------------------- */
int GetFirstClusterByFileHandle(RTFHANDLE FileHandle, UINT * Cluster)
{
int volatile Result = RTF_NO_ERROR;
RTFile * volatile f = NULL;
XTRY
case XCODE:
f = ParseFileHandle(FileHandle);
if (f->SpecialKind != NormalFile)
XRAISE(RTF_DATA_ERROR);
*Cluster = FIRST_FILE_CLUSTER(f->DirEntry.Dir);
break;
default:
Result = XVALUE; //API dose not need XHANDLED
break;
case XFINALLY:
if (f != NULL)
UnlockDevice(f->Dev);
break;
XEND_API
return Result;
}
/* ------------------------------------------------------------------------------- */
#ifdef __FS_QM_SUPPORT__
static int FolderInQuotaSet(WCHAR * Folder)
{
int i, j, k, m;
WCHAR *WidePath = NULL;
WidePath = get_ctrl_buffer(MT_MAX_WIDE_PATH);
if(WidePath == NULL)
return 0;
for (i = FS_MAX_QSET - 1 ; i >= 0; i--)
{
kal_mem_set(WidePath, 0, RTF_MAX_PATH);
k = strlen((char *)gFS_IntQuotaSet[i].Path) - 1; //remove the last '\'
m = 0;
for(j = 3; j < k; j++, m++)
WidePath[m] = gFS_IntQuotaSet[i].Path[j];
if(kal_dchar_strcmp((char *)Folder, (char *)WidePath) == 0)
{
if(WidePath) free_ctrl_buffer(WidePath);
return 1;
}
}
if(WidePath) free_ctrl_buffer(WidePath);
return 0;
}
#endif
/* ------------------------------------------------------------------------------- */
#ifdef __FS_QM_SUPPORT__
void SweepDrive(BYTE DriveLetter)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
int Result = RTF_NO_ERROR;
int i;
UINT j = 0;
UINT RFS = 0;
int FolderSize;
RTFDrive * Drive;
WCHAR *WidePath = NULL;
WCHAR *WideName = NULL;
WCHAR RootName[4];
RTFHANDLE FHandle;
RTFDOSDirEntry FileInfo;
/*----------------------------------------------------------------*/
/* Code Body , SECTION A : */
/*----------------------------------------------------------------*/
Drive = (RTFDrive*)fs_conf_get_drv_struct_by_drv_letter(DriveLetter);
if (Drive == NULL)
{
return;
}
/* do nothing if alreay swept before */
if (Drive->QuotaMgt) return;
/* This Drive must successful mounted */
fs_assert_local(Drive->MountState == HasFileSystem);
/* Get disk free space */
if(Drive->FreeClusterCount == RTF_INVALID_CLUSTER)
{
ReMountDriveAndCountFreeClusters(Drive);
}
SafeLock(MT_LOCK_RTF | MT_LOCK_DEV, Drive->Dev, RTF_INFINITE);
/*----------------------------------------------------------------*/
/* Code Body , SECTION B : Check gFS_IntQuotaSet Table Setting */
/*----------------------------------------------------------------*/
for (i = FS_MAX_QSET - 1 ; i >= 0; i--)
{
if (gFS_IntQuotaSet[i].Priority > MT_QP_DEL_ENUM) fs_assert_local(0);
if (gFS_IntQuotaSet[i].Qmax == 0) fs_assert_local(0);
if (gFS_IntQuotaSet[i].Qmin > gFS_IntQuotaSet[i].Qmax) fs_assert_local(0);
if ((gFS_IntQuotaSet[i].Uint != FS_COUNT_IN_BYTE) && (gFS_IntQuotaSet[i].Uint != FS_COUNT_IN_CLUSTER)) fs_assert_local(0);
if (DriveLetter == gFS_IntQuotaSet[i].Path[0])
{
if (gFS_IntQuotaSet[i].Qmax == FS_QMAX_NO_LIMIT)
{
gFS_IntQuotaSet[i].Qmax = 0;
}
else
{
if (gFS_IntQuotaSet[i].Uint == FS_COUNT_IN_BYTE)
{
if (gFS_IntQuotaSet[i].Qmax % Drive->ClusterSize)
gFS_IntQuotaSet[i].Qmax = (gFS_IntQuotaSet[i].Qmax / Drive->ClusterSize) + 1;
else
gFS_IntQuotaSet[i].Qmax = (gFS_IntQuotaSet[i].Qmax / Drive->ClusterSize);
}
else
{
gFS_IntQuotaSet[i].Qmax = gFS_IntQuotaSet[i].Qmax;
}
}
if (gFS_IntQuotaSet[i].Uint == FS_COUNT_IN_BYTE)
{
if (gFS_IntQuotaSet[i].Qmin % Drive->ClusterSize)
gFS_IntQuotaSet[i].Qmin = (gFS_IntQuotaSet[i].Qmin / Drive->ClusterSize) + 1;
else
gFS_IntQuotaSet[i].Qmin = (gFS_IntQuotaSet[i].Qmin / Drive->ClusterSize);
}
else
{
gFS_IntQuotaSet[i].Qmin = gFS_IntQuotaSet[i].Qmin;
}
}
}
/*----------------------------------------------------------------*/
/* Code Body , SECTION C : Disk Free Space And Quota Entry Usage */
/*----------------------------------------------------------------*/
/* Raise Drive Quota Flag */
Drive->QuotaMgt = 1;
/* Start to sweep disk */
WidePath = get_ctrl_buffer(MT_MAX_WIDE_PATH);
WideName = get_ctrl_buffer(MT_MAX_WIDE_NAME);
for (i = FS_MAX_QSET - 1 ; i >= 0; i--)
{
if (DriveLetter == gFS_IntQuotaSet[i].Path[0])
{
j = i;
kal_mem_set(WidePath, 0, RTF_MAX_PATH);
strcpy((char *)WidePath, (char *)gFS_IntQuotaSet[i].Path);
FileNameExtendToWCHAR((char *)WidePath, (RTF_MAX_PATH / 2) - 1);
// C - 1. Is Folder Exist ?
Result = RecAUX_IsFolder(WidePath, KAL_FALSE);
if (Result < RTF_NO_ERROR)
{
gFS_IntQuotaSet[i].Uint= 0;
goto SweepCalc;
}
//FolderSize = RecAUX_CountSumOfSizeUnderFolderTree(WidePath, FS_COUNT_IN_CLUSTER, NULL, 0, NULL);
FolderSize = RecAUX(WidePath, FS_COUNT_IN_CLUSTER, NULL, 0, NULL, RecAUX_CountSize);
if (FolderSize > RTF_NO_ERROR)
gFS_IntQuotaSet[i].Uint = FolderSize;
else
gFS_IntQuotaSet[i].Uint = 0;
// C - 2. Remove DEL
if (gFS_IntQuotaSet[i].Priority == MT_QP_DEL_ENUM)
{
// Result = RecAUX_XDeleteFolder(WidePath, FS_FILE_TYPE | FS_DIR_TYPE | FS_RECURSIVE_TYPE, NULL, NULL, 0);
Result = RecAUX(WidePath, FS_FILE_TYPE | FS_DIR_TYPE | FS_RECURSIVE_TYPE, NULL, NULL, 0, RecAUX_Delete);
if(Result >= RTF_NO_ERROR)
{
SafeLock(MT_LOCK_RTF | MT_LOCK_DEV, Drive->Dev, RTF_INFINITE);
gFS_IntQuotaSet[i].Uint = 0;
}
else //error handling
{
//FolderSize = RecAUX_CountSumOfSizeUnderFolderTree(WidePath, FS_COUNT_IN_CLUSTER, NULL, 0, NULL);
FolderSize = RecAUX(WidePath, FS_COUNT_IN_CLUSTER, NULL, 0, NULL, RecAUX_CountSize);
if (FolderSize > RTF_NO_ERROR)
gFS_IntQuotaSet[i].Uint = FolderSize;
else
gFS_IntQuotaSet[i].Uint = 0;
}
}
// C - 3. Remove if over quota
if (gFS_IntQuotaSet[i].Qmax) //quota with limit
{
if (gFS_IntQuotaSet[i].Uint > gFS_IntQuotaSet[i].Qmax)
{
// Result = RecAUX_XDeleteFolder(WidePath, FS_FILE_TYPE | FS_DIR_TYPE | FS_RECURSIVE_TYPE, NULL, NULL, 0);
Result = RecAUX(WidePath, FS_FILE_TYPE | FS_DIR_TYPE | FS_RECURSIVE_TYPE, NULL, NULL, 0, RecAUX_Delete);
if (Result >= RTF_NO_ERROR)
{
SafeLock(MT_LOCK_RTF | MT_LOCK_DEV, Drive->Dev, RTF_INFINITE);
gFS_IntQuotaSet[i].Uint = 0;
}
}
else //error handling
{
// FolderSize = RecAUX_CountSumOfSizeUnderFolderTree(WidePath, FS_COUNT_IN_CLUSTER, NULL, 0, NULL);
FolderSize = RecAUX(WidePath, FS_COUNT_IN_CLUSTER, NULL, 0, NULL, RecAUX_CountSize);
if (FolderSize > RTF_NO_ERROR)
gFS_IntQuotaSet[i].Uint = FolderSize;
else
gFS_IntQuotaSet[i].Uint = 0;
}
}
// C - 4. Count RFS
SweepCalc:
if (gFS_IntQuotaSet[i].Qmin > gFS_IntQuotaSet[i].Uint)
RFS += (gFS_IntQuotaSet[i].Qmin - gFS_IntQuotaSet[i].Uint);
}
}
/* Special for NVRAM
* Note !!! nvram interface split on 2006/07/17,
* here we get the NVRAM name only
*/
nvram_get_folder_name(NvramName);
/*----------------------------------------------------------------*/
/* Code Body , SECTION D : Start to Sweep to Get More Free Space */
/*----------------------------------------------------------------*/
if ((Drive->FreeClusterCount < RFS) || (Drive->FreeClusterCount == 0))
{
kal_mem_set(WidePath, 0, MT_MAX_WIDE_PATH);
strncpy((char *)WidePath, (char *)gFS_IntQuotaSet[j].Path, 3);
FileNameExtendToWCHAR((char *)WidePath, (RTF_MAX_PATH / 2) - 1);
kal_wstrcpy(RootName, WidePath);
// D - 1. Remove files under "X:\"
//RecAUX_XDeleteFolder(WidePath, FS_FILE_TYPE, NULL, NULL, 0);
RecAUX(WidePath, FS_FILE_TYPE, NULL, NULL, 0, RecAUX_Delete);
}
if ((Drive->FreeClusterCount < RFS) || (Drive->FreeClusterCount == 0))
{
// D - 2. Remove folders not in gFS_IntQuotaSet
kal_int32 nextfile=FS_NO_ERROR;
kal_wstrcat(WidePath, L"*");
for ( FHandle = RTFFindFirstEx(WidePath, 0, 0, &FileInfo, WideName, RTF_MAX_PATH, NULL, NULL);
FHandle >= 0 && nextfile==FS_NO_ERROR;
nextfile = RTFFindNextEx(FHandle, &FileInfo, (WCHAR *)WideName, RTF_MAX_PATH, NULL)) {
// Skip "." ".." and NVRAM folder
if (WFNamesMatch((char *)WideName, (char *)dchar_dot_dot) ||
WFNamesMatch((char *)WideName, (char *)dchar_dot) ||
WFNamesMatch((char *)WideName, (char *)NvramName)) {
continue;
}
if (FileInfo.Attributes & RTF_ATTR_DIR) {
// Skip registered quota folders
if (WideName[0] == 0x40) {
if (FolderInQuotaSet(WideName)) continue;
}
kal_wstrcpy(WidePath, RootName);
kal_wstrcat(WidePath, WideName);
// RecAUX_XDeleteFolder(WidePath, FS_FILE_TYPE | FS_DIR_TYPE | FS_RECURSIVE_TYPE, NULL, NULL, 0);
RecAUX(WidePath, FS_FILE_TYPE | FS_DIR_TYPE | FS_RECURSIVE_TYPE, NULL, NULL, 0, RecAUX_Delete);
if (Drive->FreeClusterCount >= RFS)
goto SweepReturn;
}
}
}
{
int current_priority = -1; // default value -1: Before start to sweep MID & LOW
SweepDriveLowMid:
if (current_priority == -1)
{
current_priority = MT_QP_LOW_ENUM; // delete LOW first
}
else if (current_priority == MT_QP_LOW_ENUM)
{
current_priority = MT_QP_MID_ENUM; // delete MID after LOW is deleted if required
}
else
{
goto SweepReturn;
}
if ((Drive->FreeClusterCount < RFS) || (Drive->FreeClusterCount == 0))
{
for (i = FS_MAX_QSET - 1 ; i >= 0; i--)
{
if (DriveLetter == gFS_IntQuotaSet[i].Path[0])
{
// D - 3. Remove LOW
if (gFS_IntQuotaSet[i].Priority == current_priority)
{
kal_mem_set(WidePath, 0, RTF_MAX_PATH);
strcpy((char *)WidePath, (char *)gFS_IntQuotaSet[i].Path);
FileNameExtendToWCHAR((char *)WidePath, (RTF_MAX_PATH / 2) - 1);
/* check if WidePath is an existed DIR */
Result = RecAUX_IsFolder(WidePath, KAL_FALSE);
if (Result < RTF_NO_ERROR) continue;
/* delete LOW folder */
// Result = RecAUX_XDeleteFolder(WidePath, FS_FILE_TYPE | FS_DIR_TYPE | FS_RECURSIVE_TYPE, NULL, NULL, 0);
Result = RecAUX(WidePath, FS_FILE_TYPE | FS_DIR_TYPE | FS_RECURSIVE_TYPE, NULL, NULL, 0, RecAUX_Delete);
if (Result >= RTF_NO_ERROR)
{
SafeLock(MT_LOCK_RTF | MT_LOCK_DEV, Drive->Dev, RTF_INFINITE);
/* update RFS and Uint */
if(gFS_IntQuotaSet[i].Uint > gFS_IntQuotaSet[i].Qmin)
RFS += gFS_IntQuotaSet[i].Qmin;
else
RFS += gFS_IntQuotaSet[i].Uint;
gFS_IntQuotaSet[i].Uint = 0;
if (Drive->FreeClusterCount >= RFS)
goto SweepReturn;
}
}
}
} /* for (i = FS_MAX_QSET - 1 ; i >= 0; i--) , Priority LOW */
}
goto SweepDriveLowMid;
}
/*----------------------------------------------------------------*/
/* Code Body , SECTION E : ENDING */
/*----------------------------------------------------------------*/
SweepReturn:
if(WidePath) free_ctrl_buffer(WidePath);
if(WideName) free_ctrl_buffer(WideName);
SafeUnlock(MT_LOCK_RTF | MT_LOCK_DEV);
}
#endif
/* ------------------------------------------------------------------------------- */
#ifdef __FS_QM_SUPPORT__
int ChkQuotaConfig(BYTE DriveLetter)
{
int Result = RTF_NO_ERROR, i;
UINT MRS = 0, RFS = 0;
RTFDrive * Drive;
//--- for gFS_IntQuotaSet table checking
int k, j;
kal_bool g_FirstDownload = KAL_TRUE;
BYTE MaxDrvIdx;
Drive = (RTFDrive*)fs_conf_get_drv_struct_by_drv_letter((WCHAR)DriveLetter);
/* This Drive must successful mounted, and swept called before */
if (Drive == NULL ||
Drive->MountState != HasFileSystem ||
Drive->QuotaMgt != 1)
{
fs_assert_local(0);
}
SafeLock(MT_LOCK_RTF | MT_LOCK_DEV, Drive->Dev, RTF_INFINITE);
#if !defined(__FS_SLIM_QUOTA_CONFIG_CHECK__)
/*
* Check gFS_IntQuotaSet table setting
*
* (Only check at first download, fs_assert_local only when in-house testing)
*/
if (Check_NORFlash_Formatted() == 0)
g_FirstDownload = KAL_FALSE;
if (g_FirstDownload == KAL_TRUE)
{
if ((FS_MAX_QSET + 1) > MT_MAX_QUOTA_ENTRY) fs_assert_local(0);
for (i = MT_BASE_DRIVE_INDEX; i < FS_MAX_DRIVES; i++)
{
if (gFS_Data.DriveTable[i].Dev == NULL)
break;
}
MaxDrvIdx = (MT_BASE_DRIVE_LETTER + i - 1);
for (i = FS_MAX_QSET - 1 ; i >= 0; i--)
{
// Check table setting
// Here is configure error
if (DriveLetter != (BYTE)DrvMappingNew)
{
if ((gFS_IntQuotaSet[i].Path[0] < MT_BASE_DRIVE_LETTER) || (gFS_IntQuotaSet[i].Path[0]) > MaxDrvIdx) fs_assert_local(0);
}
if (gFS_IntQuotaSet[i].Path[1] != 0x3A) fs_assert_local(0); // ':'
if (gFS_IntQuotaSet[i].Path[2] != 0x5C) fs_assert_local(0); // '\'
if (gFS_IntQuotaSet[i].Path[3] != 0x40) fs_assert_local(0); // '@'
if (strlen((char *)gFS_IntQuotaSet[i].Path) >= MT_MAXPATH_IN_WCHAR_UNIT) fs_assert_local(0);
k = strlen((char *)gFS_IntQuotaSet[i].Path) - 1;
if (gFS_IntQuotaSet[i].Path[k] != 0x5C) fs_assert_local(0); // last char should be '\'
for(j = 4; j < k; j++)
{
if (gFS_IntQuotaSet[i].Path[j] == 0x5C) fs_assert_local(0); // not support multi-level folder
}
}
}
#endif
/* Get NVRAM's MRS */
nvram_space_reserve(&MRS);
/* Calculate MRS, RFS */
for (i = FS_MAX_QSET - 1 ; i >= 0; i--)
{
if (DriveLetter == gFS_IntQuotaSet[i].Path[0])
{
if (Drive->Clusters < gFS_IntQuotaSet[i].Qmax)
{
/* App. Quota Max over disk space
* This should be configuration error
*/
fs_util_trace_err_noinfo(MT_QUOTA_OVER_DISK_SPACE);
fs_util_trace_info2(TRACE_ERROR, FS_ERR_QMAX_OVER_DISK, Drive->Clusters, gFS_IntQuotaSet[i].Qmax, NULL);
Result = MT_QUOTA_OVER_DISK_SPACE;
break;
}
MRS += gFS_IntQuotaSet[i].Qmin;
if (gFS_IntQuotaSet[i].Qmin > gFS_IntQuotaSet[i].Uint)
{
RFS += (gFS_IntQuotaSet[i].Qmin - gFS_IntQuotaSet[i].Uint);
}
}
}
if (Drive->Clusters < MRS)
{
/* Total Minal Reserved Space for Applications (NVRAM included) over disk space
* This should be configuration error
*/
fs_util_trace_err_noinfo(MT_QUOTA_OVER_DISK_SPACE);
fs_util_trace_info2(TRACE_ERROR, FS_ERR_QMIN_OVER_DISK, Drive->Clusters, MRS, NULL);
Result = MT_QUOTA_OVER_DISK_SPACE;
}
else if (Drive->FreeClusterCount < RFS)
{
/* Minal Reserved Space Not Enought for Application required,
* This should be run-time disk full error, raise warnning
*/
fs_util_trace_err_noinfo(MT_QUOTA_USAGE_WARNING);
fs_util_trace_info2(TRACE_ERROR, FS_ERR_QRFS_OVER_DFS, Drive->FreeClusterCount, RFS, NULL);
Result = MT_QUOTA_USAGE_WARNING;
}
SafeUnlock(MT_LOCK_RTF | MT_LOCK_DEV);
return Result;
}
#endif
/* ------------------------------------------------------------------------------- */
#ifdef __P_PROPRIETARY_COPYRIGHT__
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#endif //__P_PROPRIETARY_COPYRIGHT__
/* ------------------------------------------------------------------------------- */
#ifdef __P_PROPRIETARY_COPYRIGHT__
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#endif //__P_PROPRIETARY_COPYRIGHT__
/* ------------------------------------------------------------------------------- */
// This function is replaced by fs_srv_get_name_by_pos(), and shall not be used
int MTMakeFileName(RTFDrive * Drive, RTFDirLocation * Pos, WCHAR * FileName, UINT MaxLength)
{
BYTE CheckSum = 0;
BYTE NTReserved = 0;
int LFNIndex = -1;
RTFDOSDirEntry *D;
int Kind;
XTRY
case XCODE:
SafeLock(MT_LOCK_DEV | MT_LOCK_RTF, Drive->Dev, RTF_INFINITE);
D = GetDirPrefetch(Drive, Pos, 1);
do
{
if (D == NULL)
XRAISE(RTF_PARAM_ERROR);
Kind = EntryType(D);
if (Kind == NeverUsed)
{
XRAISE(RTF_PARAM_ERROR);
}
else if (Kind == Lfn)
{
const LFNDirEntry * LD = (void*) D;
if (LD->Ord & 0x40) // it's the start of an LFN
{
// see if it fits
char * Limit;
LFNIndex = LD->Ord & 0x3F;
CheckSum = LD->CheckSum;
/* characters stored in the LFN entry are in 2-byte unicode*/
Limit = ( (char *)FileName) + ((LFNIndex-1)*13*2 + 2*LFNCharCount(LD) );
if (Limit > ( ((char *)FileName) + MaxLength - 1)) /* not able to hold */
LFNIndex = -1;
else
{
Limit[0] = '\0';
Limit[1] = '\0';
}
}
if ((LD->CheckSum != CheckSum) || (LFNIndex != (LD->Ord & 0x3F)) || (LFNIndex <= 0))
LFNIndex = -1;
else
CopyLFN((char *)FileName + --LFNIndex * 26, LD);
}
else if (Kind == InUse)
{
// check against LFN first
if ((LFNIndex == 0) && (CheckSum== ShortNameCheckSum((BYTE*) D->FileName)))
{
break;
}
// try short name
if ((MakeShortFileName(D, (char *)FileName, MaxLength) == RTF_NO_ERROR) )
{
FileNameExtendToWCHAR((char *)FileName, FS_MAX_SFN_NATIVE_FILE_NAME_LENGTH_B);
NTReserved = D->NTReserved;
break;
}
}
if (Kind != Lfn)
LFNIndex = -1;
D = NextDirPrefetch(Drive, Pos, 1);
} while (1);
MTCheckFileNameCase(FileName, MaxLength, NTReserved);
break;
default:
break;
case XFINALLY:
SafeUnlock(MT_LOCK_DEV | MT_LOCK_RTF);
break;
XEND_API
return RTF_NO_ERROR;
}
/* ------------------------------------------------------------------------------- */
#ifdef __FS_CHECKDRIVE_SUPPORT__
/**************************************************
* __FS_CHECKDRIVE_SUPPORT__ feature
* Utilities For Directory Entries Processing
**************************************************/
static int ScanFindFirst(WCHAR * NamePattern, RTFDirEntry * DirPos)
{
int volatile Result;
RTFile * volatile f = NULL;
RTFDirLocation * Pos;
BYTE * volatile A;
BYTE Attr = 0, AttrMask = 0;
XTRY
case XCODE:
f = ParseFileName((char *)NamePattern);
RTFileCheck_NormalFile_InvalidFilename(f);
f->Flags = RTF_OPEN_DIR | RTF_READ_ONLY | RTF_CACHE_DATA;
if(SearchFile(f, SEARCH_PARENT, (char *)NamePattern, NULL))
{
if(f->DirEntry.DirCluster == 0)
{
MakePseudoRootDirEntry(f->Drive, &f->DirEntry);
if (DirPos)
DirPos->Dir = f->DirEntry.Dir;
AttrMask = Attr = 0xFF;
}
else
SET_FIRST_FILE_CLUSTER(f->DirEntry.Dir, f->DirEntry.DirCluster);
}
InitFilePointer(f);
A = (void*) &f->LastCluster;
Pos = (void*) &f->Cluster;
Pos->Index--;
A[0] = Attr;
A[1] = AttrMask | Attr;
Result = MakeNewFileHandle(f);
break;
default:
Result = XVALUE;
break;
case XFINALLY:
if (f != NULL)
{
if (Result < RTF_NO_ERROR)
FreeFTSlotAndDevice(f);
else
UnlockDevice(f->Dev);
}
break;
XEND_API
return Result;
}
static int ScanFindNext(RTFHANDLE Handle, WCHAR * FileName, RTFDirEntry * DirPos)
{
BYTE CheckSum = 0;
int LFNIndex = -1;
RTFile * volatile f = NULL;
const char * NamePattern;
RTFDirLocation * Pos;
BYTE * A;
kal_bool HintDone = KAL_FALSE;
const WCHAR *p;
kal_mem_set(DirPos, 0, sizeof(RTFDirEntry));
XTRY
case XCODE:
f = ParseFileHandle(Handle);
RTFileCheck_NormalFile_InvalidHandle(f);
RTFileCheck_AttrDir_InvalidHandle(f);
NamePattern = kal_dchar_strrchr(f->FullName, '\\')+2;
A = (void*) &f->LastCluster;
Pos = (void*) &f->Cluster;
if ((A[0] == 0xFF) && (A[1] == 0xFF))
XRAISE(RTF_NO_MORE_FILES);
if(kal_dchar_strcmp(NamePattern, (char *)dchar_start_dot_star) == 0)
NamePattern += 4;
while (1)
{
int Kind;
const RTFDOSDirEntry * D = NextDir(f->Drive, Pos);
if(D == NULL)
XRAISE(RTF_NO_MORE_FILES);
Kind = EntryType(D);
if(Kind == NeverUsed)
XRAISE(RTF_NO_MORE_FILES);
else if(Kind == Lfn)
{
const LFNDirEntry * LD = (void*) D;
if(HintDone == KAL_FALSE)
{
HintDone = KAL_TRUE;
DirPos->LongPos.Cluster = Pos->Cluster;
DirPos->LongPos.Index = Pos->Index;
}
if(LD->Ord & 0x40)
{
char * Limit;
LFNIndex = LD->Ord & 0x3F;
CheckSum = LD->CheckSum;
Limit = ( (char *)FileName) + ((LFNIndex-1)*13*2 + 2*LFNCharCount(LD) );
if(Limit > ( ((char *)FileName) + RTF_MAX_PATH - 1))
LFNIndex = -2;
else
{
Limit[0] = '\0';
Limit[1] = '\0';
}
}
if((LD->CheckSum != CheckSum) || (LFNIndex != (LD->Ord & 0x3F)) || (LFNIndex <= 0))
LFNIndex = -2;
else
CopyLFN((char *)FileName + --LFNIndex * 26, LD);
}
else if((Kind == InUse) && ((D->Attributes & A[1]) == A[0]))
{
if(DirPos)
DirPos->Dir = *D;
if(LFNIndex == -2)
DelDirEntry(f->Drive, &DirPos->LongPos, Pos);
else if((LFNIndex == 0) && (CheckSum== ShortNameCheckSum((BYTE*) D->FileName)))
{
p = (WCHAR *)FileName;
while (p[0])
{
if (!fs_util_validate_lfn_char(*p++))
{
DelDirEntry(f->Drive, &DirPos->LongPos, Pos);
HintDone = KAL_FALSE;
goto ScanFindNextContinue;
}
}
DirPos->ShortPos.Cluster = Pos->Cluster;
DirPos->ShortPos.Index = Pos->Index;
break;
}
else if(MakeShortFileName(D, (char *)FileName, RTF_MAX_PATH) != RTF_NO_ERROR)
DelDirEntry(f->Drive, &DirPos->LongPos, Pos);
else
{
FileNameExtendToWCHAR((char *)FileName, FS_MAX_SFN_NATIVE_FILE_NAME_LENGTH_B);
DirPos->ShortPos.Cluster = Pos->Cluster;
DirPos->ShortPos.Index = Pos->Index;
break;
}
HintDone = KAL_FALSE;
}
else //Kind = Available
HintDone = KAL_FALSE;
ScanFindNextContinue:
if(Kind != Lfn)
LFNIndex = -1;
}
break;
default:
break;
case XFINALLY:
if (f) UnlockDevice(f->Dev);
break;
XEND_API
return RTF_NO_ERROR;
}
#endif
/* ------------------------------------------------------------------------------- */
#ifdef __FS_CHECKDRIVE_SUPPORT__
/**************************************************
* __FS_CHECKDRIVE_SUPPORT__ feature
* Macros & Utilities For BIT MAP Operation
**************************************************/
#define INLINE_CMPARE_CLUSTER_BIT(Cdata, Cluster) \
(Cluster >= Cdata->ClsOffset && Cluster <= Cdata->ClsRange)
#define INLINE_GET_CLUSTER_BIT(Cdata, Cluster) \
((Cdata->ClusterMap[ (Cluster - Cdata->ClsOffset) / 8] & 1 << ((Cluster - Cdata->ClsOffset) % 8)) != 0)
#define INLINE_SET_CLUSTER_BIT(Cdata, Cluster) \
(Cdata->ClusterMap[ (Cluster - Cdata->ClsOffset) / 8] |= 1 << ((Cluster - Cdata->ClsOffset) % 8))
#define INLINE_CLEAR_CLUSTER_BIT(Cdata, Cluster) \
(Cdata->ClusterMap[ (Cluster - Cdata->ClsOffset) / 8] &= ~(1 << ((Cluster - Cdata->ClsOffset) % 8)))
static void Clear_ClusterMap_Chain(InternScanDataStruct *SDCD, RTFCluster Cluster, UINT Count)
{
while (Count--)
{
if (INLINE_CMPARE_CLUSTER_BIT(SDCD, Cluster))
INLINE_CLEAR_CLUSTER_BIT(SDCD, Cluster);
Cluster = (SDCD->RAWCluster)(SDCD->Drive, Cluster);
if ((Cluster < 2L) || (Cluster >= SDCD->Drive->Clusters))
break;
}
}
static void ScanRootDirFATChain(InternScanDataStruct *SDCD)
{
// FAT32 Root Directory must marked
if(SDCD->Drive->FATType == 32)
{
RTFCluster tmpCluster, nextCluster;
tmpCluster = SDCD->Drive->FirstDirSector; /* Note that it's not sector number in FAT32 */
do
{
nextCluster = (SDCD->RAWCluster)(SDCD->Drive, tmpCluster);
if (INLINE_CMPARE_CLUSTER_BIT(SDCD, tmpCluster))
INLINE_SET_CLUSTER_BIT(SDCD, tmpCluster);
if (nextCluster >= 2L && nextCluster < SDCD->Drive->Clusters)
{ /* Valid Cluster */
tmpCluster = nextCluster;
continue;
}
if (nextCluster < RTF_CLUSTER_CHAIN_END)
{
/* InValid Cluster , Truncate the FAT Chain */
SetClusterValue(SDCD->Drive, tmpCluster, RTF_CLUSTER_CHAIN_END);
return;
}
} while (nextCluster < RTF_CLUSTER_CHAIN_END);
} /* if(SDCD->Drive->FATType == 32) */
}
#endif /* __FS_CHECKDRIVE_SUPPORT__ */
/* ------------------------------------------------------------------------------- */
#ifdef __FS_CHECKDRIVE_SUPPORT__
/**************************************************
* __FS_CHECKDRIVE_SUPPORT__ feature
* Second Level Procedures
**************************************************/
static int IfFHEnough(void) //for CHECKDRIVE_SUPPORT ScanDirTree only
{
int i, j = 0;
RTFile * f = gFS_Data.FileTable;
for (i=0; i<FS_MAX_FILES; i++, f++)
{
if ((f->Dev == NULL) && (f->Task == NULL))
j++;
}
if (j)
return j;
return RTF_TOO_MANY_FILES;
}
static int ScanDirTree(InternScanDataStruct *SDCD)
{
RTFHANDLE FHandle = 0;
RTFHANDLE *HistoryFH = NULL;
UINT i = 0;
WCHAR *Filename1 = NULL, *Filename2 = NULL, *Filename3 = NULL;
int idx = 0, Result = RTF_NO_ERROR;
RTFDirEntry DirPos;
RTFCluster C;
RTFCluster FATClusters, DirClusters;
HistoryFH = get_ctrl_buffer((FS_MAX_FILES)*4);
if(HistoryFH == NULL)
{
Result = MT_FAIL_GET_MEM;
return Result;
}
kal_mem_set(HistoryFH, 0, (FS_MAX_FILES)*4);
Filename1 = get_ctrl_buffer(MT_MAX_WIDE_PATH);
if(Filename1 == NULL)
{
Result = MT_FAIL_GET_MEM;
goto ScanDirTreeReturn;
}
kal_mem_set(Filename1, 0, MT_MAX_WIDE_PATH);
Filename2 = get_ctrl_buffer(MT_MAX_WIDE_PATH);
if(Filename2 == NULL)
{
Result = MT_FAIL_GET_MEM;
goto ScanDirTreeReturn;
}
kal_mem_set(Filename2, 0, MT_MAX_WIDE_PATH);
Filename3 = get_ctrl_buffer(MT_MAX_WIDE_PATH);
if(Filename3 == NULL)
{
Result = MT_FAIL_GET_MEM;
goto ScanDirTreeReturn;
}
kal_mem_set(Filename3, 0, MT_MAX_WIDE_PATH);
kal_wstrncpy((WCHAR *)Filename3, (WCHAR *)SDCD->Drive->CurrDir, 3); /* Scan From Root */
ScanRootDirFATChain(SDCD); /* FAT32 Only */
while(idx >= 0)
{
ScanDirTreeFirst:
Result = IfFHEnough() - 2 /* Reserve 2 file handle for concurrency */;
if(Result < RTF_NO_ERROR)
{
Result = RTF_TOO_MANY_FILES;
goto ScanDirTreeReturn;
}
kal_mem_set(Filename1, 0, MT_MAX_WIDE_PATH);
if(kal_dchar_strlen((char *)Filename3) > RTF_MAX_PATH)
{
Result = MT_PATH_OVER_LEN_ERROR;
goto ScanDirTreeReturn;
}
kal_wstrcpy((WCHAR *)Filename1, (WCHAR *)Filename3);
kal_wstrcat((WCHAR *)Filename1, (WCHAR *)L"*");
FHandle = ScanFindFirst((WCHAR *)Filename1, &DirPos);
if(FHandle > 0)
{
ScanDirTreeNext:
while((Result = ScanFindNext(FHandle, (WCHAR *)Filename2, &DirPos)) == RTF_NO_ERROR)
{
if(g_CheckDrive == KAL_FALSE)
goto ScanDirTreeReturn;
if((!WFNamesMatch((char *)Filename2, (char *)dchar_dot)) &&
(!WFNamesMatch((char *)Filename2, (char *)dchar_dot_dot)))
{
FATClusters = 0;
SDCD->D = DirPos.Dir;
C = FIRST_FILE_CLUSTER(SDCD->D);
//Invalid cluster
if(FileNameInvalid((const char *)SDCD->D.FileName) || (C == 1L) ||
(C >= SDCD->Drive->Clusters) ||
((SDCD->D.Attributes & RTF_ATTR_DIR) && (C == 0)) )
{
DelDirEntry(SDCD->Drive, &DirPos.LongPos, &DirPos.ShortPos);
goto ScanDirTreeNext;
}
if ((C != RTF_ROOT_DIR) && (C != 0))
{
do
{
//Invalid cluster
if ((C < 2L) || (C >= SDCD->Drive->Clusters))
{
DelDirEntry(SDCD->Drive, &DirPos.LongPos, &DirPos.ShortPos);
goto ScanDirTreeNext;
}
//Cross link
if (INLINE_CMPARE_CLUSTER_BIT(SDCD, C) && INLINE_GET_CLUSTER_BIT(SDCD, C))
{
Clear_ClusterMap_Chain(SDCD, FIRST_FILE_CLUSTER(SDCD->D), FATClusters);
DelDirEntry(SDCD->Drive, &DirPos.LongPos, &DirPos.ShortPos);
ScanFATDelete(SDCD, FIRST_FILE_CLUSTER(SDCD->D));
Result = RTF_CHECKDISK_RETRY;
goto ScanDirTreeReturn;
}
else
//Set Bit
{
if (INLINE_CMPARE_CLUSTER_BIT(SDCD, C))
INLINE_SET_CLUSTER_BIT(SDCD, C);
}
FATClusters++;
C = (SDCD->RAWCluster)(SDCD->Drive, C);
} while (g_CheckDrive == KAL_TRUE && C < RTF_CLUSTER_CHAIN_END);
}
if((SDCD->D.Attributes & RTF_ATTR_DIR) == 0) //File
{
DirClusters = (SDCD->D.FileSize > 0) ? ((SDCD->D.FileSize-1) / SDCD->Drive->ClusterSize) + 1 : 0;
//Size too small
if (FATClusters > DirClusters)
{
DelDirEntry(SDCD->Drive, &DirPos.LongPos, &DirPos.ShortPos);
Clear_ClusterMap_Chain(SDCD, FIRST_FILE_CLUSTER(SDCD->D), FATClusters);
ScanFATDelete(SDCD, FIRST_FILE_CLUSTER(SDCD->D)); /* Clear the chain */
goto ScanDirTreeNext;
}
//Size too large
else if (FATClusters < DirClusters)
{
DelDirEntry(SDCD->Drive, &DirPos.LongPos, &DirPos.ShortPos);
Clear_ClusterMap_Chain(SDCD, FIRST_FILE_CLUSTER(SDCD->D), FATClusters);
ScanFATDelete(SDCD, FIRST_FILE_CLUSTER(SDCD->D)); /* Clear the chain */
goto ScanDirTreeNext;
}
}
else //Folder
{
#if defined(FS_CHECKDRIVE_SUPPORT_REMOVAL_DEEP_FOLDER)
/* Enable this option if you want to remove the folder that located in too deep level */
if(idx == (FS_MAX_FILES-2))
{
DelDirEntry(SDCD->Drive, &DirPos.LongPos, &DirPos.ShortPos);
Clear_ClusterMap_Chain(SDCD, FIRST_FILE_CLUSTER(SDCD->D), FATClusters);
ScanFATDelete(SDCD, FIRST_FILE_CLUSTER(SDCD->D)); /* Clear the chain */
goto ScanDirTreeNext;
}
#endif /* FS_CHECKDRIVE_SUPPORT_REMOVAL_DEEP_FOLDER */
kal_wstrcat((WCHAR *)Filename3, (WCHAR *)Filename2);
kal_wstrcat((WCHAR *)Filename3, (WCHAR *)L"\\");
HistoryFH[idx] = FHandle;
idx++;
goto ScanDirTreeFirst;
}
} /* If Not dot , dotdot */
}
if((Result != RTF_NO_MORE_FILES) && (Result < 0))
goto ScanDirTreeReturn;
}
else /* (FHandle < 0) */
{
Result = FHandle;
goto ScanDirTreeReturn;
} /* End of ScanFindFrist */
idx--;
if(idx >= 0)
{
if(FHandle > 0)
{
RTFFindClose(FHandle);
}
FHandle = HistoryFH[idx];
HistoryFH[idx] = 0;
CdUp((WCHAR *)Filename3);
goto ScanDirTreeNext;
}
else
break;
} /* while( idx >= 0) */
ScanDirTreeReturn:
if(idx >= 0)
{
for(i = 0; i <= idx; i++)
{
if(HistoryFH[i] == 0) break;
RTFFindClose(HistoryFH[i]);
}
}
if(FHandle > 0)
{
RTFFindClose(FHandle);
}
if(Filename1) free_ctrl_buffer(Filename1);
if(Filename2) free_ctrl_buffer(Filename2);
if(Filename3) free_ctrl_buffer(Filename3);
if(HistoryFH) free_ctrl_buffer(HistoryFH);
if((Result != RTF_NO_MORE_FILES) && (Result < RTF_NO_ERROR))
return Result;
return RTF_NO_ERROR;
}
static void ChkLostClusters(InternScanDataStruct *SDCD)
{
RTFCluster i, C;
UINT State = 1;
// mark up all free and bad clusters
i = (SDCD->ClsOffset) ? SDCD->ClsOffset : 2;
for (;g_CheckDrive == KAL_TRUE && i <= SDCD->ClsRange; i++)
{
if (!INLINE_GET_CLUSTER_BIT(SDCD, i))
{
C = (SDCD->RAWCluster)(SDCD->Drive, i);
/* Free or Broken Cluster */
if (C == 0 || C == RTF_BAD_CLUSTER)
{
INLINE_SET_CLUSTER_BIT(SDCD, i);
}
/**********************************************************************************
* The strange cluster value should not happen during power lose.
* There's many trade-off opinions on dealing with this.
* Conclusion is, 3 policies applied on different cases,
* 1) Leave it as-is , applied on system drive , RTFCheckDisk().
* Because Critial Application such as NVRAM will stop system when hit.
* 2) EXT_fs_assert_local() , applied on public drive when in-house testing.
* To early detect error.
* 3) Logging, warnning and clear , applied on public drive when production released.
* To achieve integrity of file system.
************************************************************ 2006/03/31 ***********/
else if (C == 1 || C >= SDCD->Drive->Clusters)
{
// Policy 1: Leave it as-is
// INLINE_SET_CLUSTER_BIT(SDCD, i);
// Policy 2: fs_assert_local
// Use fs_assert_local() to assert when in-house test only
if ((SDCD->Drive->Dev->DeviceFlags & RTF_DEVICE_REMOVABLE) == 0 && (C < RTF_CLUSTER_CHAIN_END))
fs_assert_local(0);
// Policy 3: Clear it (Do nothing here)
// There should be warning or trace here
}
}
} /* for (;g_CheckDrive == KAL_TRUE && i <= SDCD->ClsRange; i++) */
// look for unmarked areas
C = 0;
i = (SDCD->ClsOffset) ? SDCD->ClsOffset : 2;
for (;g_CheckDrive == KAL_TRUE && i <= SDCD->ClsRange; i++)
{
if (State != INLINE_GET_CLUSTER_BIT(SDCD, i))
{
if (State == 0) // end of a lost chain
{
RTFCluster j;
for (j=0; j < C; j++)
SetClusterValue(SDCD->Drive, j+i-C, 0);
}
State = !State;
C = 0;
}
C++;
}
}
#endif /* __FS_CHECKDRIVE_SUPPORT__ */
/* ------------------------------------------------------------------------------- */
#ifdef __FS_CHECKDRIVE_SUPPORT__
/**************************************************
* __FS_CHECKDRIVE_SUPPORT__ feature
* Utilities For Dirve Specific Operation
**************************************************/
/*
* ChkMonopolizeDrive()
* To get exclusion control on specific drive.
* check if any opening file handle on this drive,
*/
static void ChkMonopolizeDrive(RTFDrive * Drive)
{
int i;
RTFile * f;
//Lock RTF and device before calling this function
for (i=0, f = gFS_Data.FileTable; i<FS_MAX_FILES; i++, f++)
{
if ((f->Lock != 0) && (f->Drive == Drive))
{
fs_util_trace_err_noinfo(RTF_ACCESS_DENIED);
fs_util_trace_info2(TRACE_ERROR | MT_TRACE_INFO_TASK, FS_ERR_ACCESS_DENIED, f->OwnerLR, GetFileHandle(f), f);
XRAISE(RTF_ACCESS_DENIED);
}
}
//should not unlock here
}
static void ScanFATDelete(InternScanDataStruct *SDCD, RTFCluster Cluster)
{
RTFCluster NextCluster;
while (g_CheckDrive == KAL_TRUE && Cluster < RTF_CLUSTER_CHAIN_END)
{
NextCluster = (SDCD->RAWCluster)(SDCD->Drive, Cluster);
SetClusterValue(SDCD->Drive, Cluster, 0);
Cluster = NextCluster;
}
}
#endif /* __FS_CHECKDRIVE_SUPPORT__ */
// Define fs_srv_get_cluster_value() parameter in ScanFAT (FS Cache/RTF Buffer switching)
#if defined(__FS_CACHE_SUPPORT__) && !defined(__FS_CARD_DOWNLOAD__)
#define GET_CLUSTER_PARAMETER 0
#else
#define GET_CLUSTER_PARAMETER GetBuffer
#endif
static RTFCluster GetRAWClusterValue(RTFDrive *Drive, RTFCluster Cluster)
{
return fs_srv_get_cluster_value(Drive, Cluster, FS_GET_CLUSTER_RAW, GET_CLUSTER_PARAMETER);
}
/* ------------------------------------------------------------------------------- */
#ifdef __FS_CHECKDRIVE_SUPPORT__
/**************************************************
* __FS_CHECKDRIVE_SUPPORT__ feature
* Main Procedure
**************************************************/
int ScanDrive(RTFDrive * Drive, void * Buffer, unsigned int BufferLen)
{
InternScanDataStruct SDCD;
int volatile Result = RTF_NO_ERROR;
RTFDirEntry Root;
/* ScanDrive - 1. Setup Scan-Disk-Control-Data */
SDCD.Drive = Drive;
SDCD.ClusterMap = (BYTE*) Buffer;
SDCD.ClsSpan = (BufferLen - 1) * 8;
XTRY
case XCODE:
/* ScanDrive - 2. Lock Device, and exclusion access check */
SafeLock(MT_LOCK_RTF | MT_LOCK_DEV, Drive->Dev, RTF_INFINITE);
ChkMonopolizeDrive(Drive);
/* ScanDrive - 3. Hook the Function Pointer to do faster GetClusterValue() */
SDCD.RAWCluster = GetRAWClusterValue;
/* ScanDrive - 4. Build Recursive initial Data */
ScanDriveAgain:
SDCD.ClsOffset = 0;
ScanDriveNextIterative:
SDCD.ClsRange = SDCD.ClsOffset + SDCD.ClsSpan;
if (SDCD.ClsRange > Drive->Clusters)
{
SDCD.ClsRange = Drive->Clusters;
}
MakePseudoRootDirEntry(Drive, &Root);
SDCD.D = Root.Dir;
kal_mem_set(SDCD.ClusterMap, 0, SDCD.ClsSpan / 8 + 1);
/* ScanDrive - 5. Detail Scan by Root Directory */
Result = ScanDirTree(&SDCD);
if (Result == RTF_CHECKDISK_RETRY)
{
goto ScanDriveAgain;
}
else if (Result < RTF_NO_ERROR)
{
fs_util_trace_err_noinfo(Result);
XRAISE(Result);
}
ChkLostClusters(&SDCD);
FlushAllBuffers(SDCD.Drive->Dev);
if (SDCD.ClsRange < Drive->Clusters)
{
SDCD.ClsOffset = SDCD.ClsRange;
goto ScanDriveNextIterative;
}
break;
default:
Result = XVALUE;
break;
case XFINALLY:
SafeUnlock(MT_LOCK_RTF | MT_LOCK_DEV);
break;
XEND_API
return Result;
}
#endif /* __FS_CHECKDRIVE_SUPPORT__ */
/* ------------------------------------------------------------------------------- */
/**************************************************
* Recursive Folder Traverse Engine
* Members:
* RecTravStart
* RecTravClose
* RecTravNextFolder
* RecTravBackward
* RecTravDownLevel
* RecTravUpLevel
**************************************************/
/* Note: Usage Sample C:\ABC\* , must be end with star */
const static WCHAR dchar_backslash_star_postfix[] = /* \* */ { 0x005c, 0x002a, 0x0000};
RTFHANDLE RecTravStart(WCHAR * PathNamePattern)
{
int volatile Result;
RTFile * volatile f = NULL;
RTFDirLocation * Pos;
BYTE * volatile A;
BYTE Attr = 0, AttrMask = 0;
XTRY
case XCODE:
f = ParseFileName((char *)PathNamePattern);
RTFileCheck_NormalFile_InvalidFilename(f);
f->Flags = RTF_OPEN_DIR | RTF_READ_ONLY | RTF_CACHE_DATA;
if(SearchFile(f, SEARCH_PARENT, (char *)PathNamePattern, NULL)) // search a file or a direcotry
{
// PathNamePattern is a file, and found !
XRAISE(RTF_PARAM_ERROR);
}
// else we found the parent
InitFilePointer(f);
A = (void*) &f->LastCluster;
Pos = (void*) &f->Cluster;
Pos->Index--;
Attr = RTF_ATTR_DIR; /* Folder Only */
A[0] = Attr;
A[1] = AttrMask | Attr;
Result = MakeNewFileHandle(f);
break;
default:
Result = XVALUE;
break;
case XFINALLY:
if (f != NULL)
{
if (Result < RTF_NO_ERROR)
FreeFTSlotAndDevice(f);
else
UnlockDevice(f->Dev);
}
break;
XEND_API
return Result;
}
int RecTravClose(RTFHANDLE Handle)
{
return RTFClose(Handle);
}
static int RecTravNextFolder(RTFHANDLE Handle, WCHAR * FileName, RTFDirEntry * DirPos)
{
BYTE CheckSum = 0;
int LFNIndex = -1;
RTFile * volatile f = NULL;
const WCHAR * NamePattern;
RTFDirLocation * Pos;
kal_bool HintDone = KAL_FALSE;
BYTE * A;
#ifdef __EFS_DEBUG__
fs_assert_local(DirPos != NULL && FileName != NULL);
#endif /* __EFS_DEBUG__ */
kal_mem_set(DirPos, 0, sizeof(RTFDirEntry));
XTRY
case XCODE:
f = ParseFileHandle(Handle);
RTFileCheck_NormalFile_InvalidHandle(f);
RTFileCheck_AttrDir_InvalidHandle(f);
RTFileCheck_Aborted(f);
NamePattern = kal_wstrrchr((WCHAR*)f->FullName, 0x005c);
A = (void*) &f->LastCluster;
Pos = (void*) &f->Cluster;
/*
* normally FirstCluster should not be 0. Raise FS_BAD_DIR_ENTRY if it is. (W11.11)
*/
if (Pos->Cluster == 0)
{
fs_util_trace_err_noinfo(FS_BAD_DIR_ENTRY);
fs_util_trace_info0(TRACE_ERROR | MT_TRACE_INFO_WSTR, FS_INFO_DISK_ERROR, FileName);
XRAISE(FS_BAD_DIR_ENTRY); // [debug]
}
/*
* Before W10.22, NamePattern must be end with "\\*"
* if (kal_wstrcmp(NamePattern, dchar_backslash_star_postfix) != 0) XRAISE(RTF_PARAM_ERROR)
*
* Remove this limitation because if f->FullName will exceeds RTF_MAX_PATH after appending "\\*",
* SearchFile() in RecTravStart() will not append "\\*" in f->FullName. For extreme case, FS_PARAM_ERROR
* will be returned add TravCB->TravStatus will be assigned as FS_PARAM_ERROR. Then recursive operation
* may be successful but return a error code. (W10.22 / Stanley Chu)
*/
if (kal_wstrcmp(NamePattern, dchar_backslash_star_postfix) != 0 || A[0] != RTF_ATTR_DIR)
{
fs_util_trace_err_noinfo(RTF_PARAM_ERROR);
XRAISE(RTF_PARAM_ERROR);
}
while (1)
{
int Kind;
const RTFDOSDirEntry * D = NextDir(f->Drive, Pos);
if(D == NULL)
{
XRAISE(RTF_NO_MORE_FILES);
}
Kind = EntryType(D);
if(Kind == NeverUsed)
{
XRAISE(RTF_NO_MORE_FILES);
}
else if(Kind == Lfn)
{
const LFNDirEntry * LD = (void*) D;
if(HintDone == KAL_FALSE)
{
HintDone = KAL_TRUE;
DirPos->LongPos.Cluster = Pos->Cluster;
DirPos->LongPos.Index = Pos->Index;
}
if(LD->Ord & 0x40)
{
char * Limit;
LFNIndex = LD->Ord & 0x3F;
CheckSum = LD->CheckSum;
Limit = ( (char *)FileName) + ((LFNIndex-1)*13*2 + 2*LFNCharCount(LD) );
if(Limit > ( ((char *)FileName) + RTF_MAX_PATH - 1))
LFNIndex = -2;
else
{
Limit[0] = '\0';
Limit[1] = '\0';
}
}
if((LD->CheckSum != CheckSum) || (LFNIndex != (LD->Ord & 0x3F)) || (LFNIndex <= 0))
LFNIndex = -2;
else
CopyLFN((char *)FileName + --LFNIndex * 26, LD);
}
else if((Kind == InUse) && ((D->Attributes & A[1]) == A[0]))
{
DirPos->Dir = *D;
DirPos->DirCluster = FIRST_FILE_CLUSTER(f->DirEntry.Dir);
// try LFN first
if((LFNIndex == 0) &&
(CheckSum== ShortNameCheckSum((BYTE*) D->FileName)) ) // Note: Name Match not requeired
{
/* Now found one that with LFN & SFN */
DirPos->ShortPos.Cluster = Pos->Cluster;
DirPos->ShortPos.Index = Pos->Index;
break;
}
// try short name
if ((D->FileName[0] != 0x2e) /* skip dot or dot-dot */ &&
(MakeShortFileName(D, (char *)FileName, RTF_MAX_PATH) == RTF_NO_ERROR))
{
/* Now found one that with SFN only */
if (D->FileName[0] == 0x05)
{
FileName[0] = 0xE5;
}
FileNameExtendToWCHAR((char *)FileName, FS_MAX_SFN_NATIVE_FILE_NAME_LENGTH_B);
MTCheckFileNameCase(FileName, RTF_MAX_PATH, D->NTReserved);
DirPos->ShortPos.Cluster = Pos->Cluster;
DirPos->ShortPos.Index = Pos->Index;
break;
}
HintDone = KAL_FALSE;
}
else
HintDone = KAL_FALSE;
if(Kind != Lfn)
LFNIndex = -1;
}
break;
default:
break;
case XFINALLY:
if (f) UnlockDevice(f->Dev);
break;
XEND_API
return RTF_NO_ERROR;
}
/**********************************************************
* RecTravDownLevel ---- implement for fast
* f: The File Table Pointer
* SaveDirInfo: The Current DirPos to be Saved
* NewDirInfo : The Down Level DirPos to be Setup
* FolderName : The Folder Name to be append on
*
**********************************************************/
static int RecTravDownLevel(RTFile *f, RTFDirEntry *SaveDirInfo, RTFDirEntry *NewDirInfo, WCHAR *FolderName)
{
WCHAR * NamePtr;
RTFDirLocation * Pos;
unsigned int NameLen;
int result = RTF_NO_ERROR;
#ifdef __EFS_DEBUG__
fs_assert_local(SaveDirInfo != NULL && NewDirInfo != NULL);
fs_assert_local(FolderName != NULL);
#endif /* __EFS_DEBUG__ */
RTFSYSLockMutex(RTFLock, RTF_INFINITE);
// Now save and swap the directory Entry information
memcpy(SaveDirInfo, &(f->DirEntry), sizeof(RTFDirEntry));
memcpy(&(f->DirEntry), NewDirInfo, sizeof(RTFDirEntry));
// Reset position information
Pos = (void*) &f->Cluster;
Pos->Cluster = FIRST_FILE_CLUSTER(f->DirEntry.Dir);
Pos->Index = (UINT)-1;
// Update f->FullName , string process
NamePtr = kal_wstrrchr((WCHAR*) f->FullName, 0x005c);
NameLen = kal_wstrlen(FolderName);
if ((NamePtr - (WCHAR*)f->FullName) + 1 /* Length of f->FullName including the last '\' */ + NameLen >= MT_MAXPATH_IN_WCHAR_UNIT)
{
result = MT_PATH_OVER_LEN_ERROR;
}
else
{
kal_wstrncpy(++NamePtr, FolderName, NameLen); // append folder name
kal_wstrcpy (NamePtr+NameLen, dchar_backslash_star_postfix); // append "\*"
}
RTFSYSFreeMutex(RTFLock);
return result;
}
/**********************************************************
* RecTravUpLevel ---- implement for fast
* f: The File Table Pointer
* UpDirInfo: The Current DirPos to be Saved
*
**********************************************************/
static void RecTravUpLevel(RTFile *f, RTFDirEntry *UpDirInfo)
{
WCHAR * NamePtr;
RTFDirLocation * Pos;
#ifdef __EFS_DEBUG__
fs_assert_local(UpDirInfo != NULL);
#endif /* __EFS_DEBUG__ */
RTFSYSLockMutex(RTFLock, RTF_INFINITE);
// Now clear position information, shift directory information as position information
Pos = (void*) &f->Cluster;
Pos->Cluster = f->DirEntry.ShortPos.Cluster;
Pos->Index = f->DirEntry.ShortPos.Index;
// Reload the Directory info
memcpy(&(f->DirEntry), UpDirInfo, sizeof(RTFDirEntry));
// Update f->FullName , string process
NamePtr = kal_wstrrchr((WCHAR*) f->FullName, 0x005c);
NamePtr[0] = 0x0;
NamePtr = kal_wstrrchr((WCHAR*) f->FullName, 0x005c);
kal_wstrcpy(NamePtr, dchar_backslash_star_postfix);
RTFSYSFreeMutex(RTFLock);
}
/**********************************************************
* RecTravUpLevelAndBackward ---- implement for fast
* f: The File Table Pointer
* UpDirInfo: The Current DirPos to be Saved, and set back to reterive name again
*
**********************************************************/
static void RecTravUpLevelAndBackward(RTFile *f, RTFDirEntry *UpDirInfo)
{
WCHAR * NamePtr;
RTFDirLocation * Pos;
#ifdef __EFS_DEBUG__
fs_assert_local(UpDirInfo != NULL);
#endif /* __EFS_DEBUG__ */
RTFSYSLockMutex(RTFLock, RTF_INFINITE);
// Now clear position information, shift directory information as position information
// But, set back to the parent entry
Pos = (void*) &f->Cluster;
if (f->DirEntry.LongPos.Cluster != 0)
{
Pos->Cluster= f->DirEntry.LongPos.Cluster;
Pos->Index = f->DirEntry.LongPos.Index - 1;
}
else
{
Pos->Cluster= f->DirEntry.ShortPos.Cluster;
Pos->Index = f->DirEntry.ShortPos.Index - 1;
}
// Reload the Directory info
memcpy(&(f->DirEntry), UpDirInfo, sizeof(RTFDirEntry));
// Update f->FullName , string process
NamePtr = kal_wstrrchr((WCHAR*) f->FullName, 0x005c);
NamePtr[0] = 0x0;
NamePtr = kal_wstrrchr((WCHAR*) f->FullName, 0x005c);
kal_wstrcpy(NamePtr, dchar_backslash_star_postfix);
RTFSYSFreeMutex(RTFLock);
}
/* ------------------------------------------------------------------------------- */
/**************************************************
* Recursive Directroy Tree Traverse Core
* Members:
* RecTravCore_DFS
* RecTravCore_BFS
* RecTravCore_Flat
* RecTravCore_CloseAndRootNodeAct
* RecTravCore_DFS_CR
**************************************************/
/**************************************************
* Structure Member Required
* TravFH --- The FileHandle To Get Next Folder
* MyCallBack --- The CallBack To Process Found Item
* NameBuf
* LevelStack
* ListFH --- The FileHandle To Get File In Current Level
**************************************************/
void RecTravCore_DFS(InternRecursiveEngineStruct *TravCB, RTFHANDLE TravFH, MTGenericCallBack *Act)
{
RTFHANDLE ListFH;
int LevelIdx=0;
RTFDirEntry TmpDirPos;
#ifdef __EFS_DEBUG__
UINT U = TravFH >> (4*sizeof(int));
#endif /* __EFS_DEBUG__ */
UINT i = TravFH & ((1 << (4*sizeof(int))) - 1);
RTFile* fp;
int TravResult, ActResult;
WCHAR* NamePtr;
int MaxLength; // for FS_Count() enhancement
int attr_mask;
if (Act == RecAct_CountNum)
{
MaxLength = 8 + 3 + 1 + 1; // We only need sufficient length for SFN to skipped LFN handling in RTFFindNextEx() to save time
#ifdef __FS_OPEN_HINT__
MaxLength |= MT_HINT_DISABLE;
#endif
}
else
{
MaxLength = RTF_MAX_PATH;
}
fp = gFS_Data.FileTable + i;
#ifdef __EFS_DEBUG__
fs_assert_local(fp->Unique == U && i < FS_MAX_FILES && fp->Lock != 0 && fp->Dev != NULL);
fs_assert_local(Act != NULL && TravCB->NameBuf != NULL);
#endif /* __EFS_DEBUG__ */
do
{
/*-- DFS 0 -------------------------------------------
* Setup current folder.
*----------------------------------------------------*/
/*
* Prepare TravCB->CurrPath and TravCB->CurrLeftLen.
* Here we want TravCB->CurrPath to be like "X:\\A\\TARGET_FOLDER\\" for Act()
*
* The input fp->FullName must be like X:\\A\\TARGET_FOLDER\\*
*/
kal_wstrcpy(TravCB->CurrPath, (WCHAR*) fp->FullName);
RecConf_MemoryChecker(&TravCB); // for debug (check name buffer guard pattern)
TravResult = kal_wstrlen(TravCB->CurrPath); // borrow TravResult as length of CurrPath
NamePtr = TravCB->CurrPath + TravResult - 1; // NamePtr is the last WCHAR just before tailed NULL
// calculate CurrLeftLen
if ('*' == *NamePtr && '\\' == *(NamePtr - 1))
{
TravCB->CurrLeftLen = 1 /* preserve space for '\0' */ + (TravResult - 1) /* length of CurrPath - '*' */;
if (TravCB->CurrLeftLen < MT_MAXPATH_IN_WCHAR_UNIT)
TravCB->CurrLeftLen = MT_MAXPATH_IN_WCHAR_UNIT - TravCB->CurrLeftLen;
else
TravCB->CurrLeftLen = 0;
*NamePtr = '\0'; // remove '*'
}
else
{
TravCB->TravStatus = RTF_PARAM_ERROR;
return;
}
if (TravCB->Parameters.Flag & FS_RECURSIVE_TYPE) // DFS
{
attr_mask = RTF_ATTR_DIR;
}
else // flat
{
attr_mask = 0;
goto RecTravCoreFlat;
}
/*-- DFS 1 --------------------------------------------------------------------------------
* Find a folder in current folder.
* If found, enter it (go to next level), then back to DFS 0 to start a new traverse in next level.
* If not found, do ACT for all files, go back to upper layer,
*-----------------------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------
* TmpDirPos reserves the position of (1st LFN entry if existed and) SFN entry found
* fp->Cluster and fp->Offset reserves the position of SFN entry found
* TravCB->NameBuf stores the file (folder) name only.
*----------------------------------------------------------------------------*/
TravResult = RecTravNextFolder(TravFH, TravCB->NameBuf, &TmpDirPos);
RecConf_MemoryChecker(&TravCB); // for debug (check name buffer guard pattern)
if (TravResult == RTF_NO_ERROR) // 1 folder found
{
if (LevelIdx == TravCB->LevelLimit) // The directory tree is too deep to reach, fail
{
TravCB->TravStatus = RTF_TOO_MANY_FILES;
break;
}
if (kal_wstrlen(TravCB->NameBuf) > TravCB->CurrLeftLen)
{
// The Path Length over Spec. definiation , reserve \\ , * , and \0
TravCB->TravStatus = MT_PATH_OVER_LEN_ERROR;
break;
}
// abort watching in XDelete case (check abort flag whenever a folder is found)
if ((TravCB->Parameters.Flag & FS_XDEL_ABORT_WATCH) && (g_Xdelete != KAL_TRUE))
{
TravCB->Parameters.ErrorCode = MT_ABORTED_ERROR;
break;
}
/* store f->DirEntry to LevelStack, goto DFS 0 to start a new traverse in next level. */
ActResult = RecTravDownLevel(fp, TravCB->LevelStack + LevelIdx, &TmpDirPos, TravCB->NameBuf);
if (ActResult < RTF_NO_ERROR) // path over len
{
TravCB->TravStatus = MT_PATH_OVER_LEN_ERROR;
break;
}
LevelIdx++;
continue;
}
else if (TravResult == FS_NO_MORE_FILES || TravResult == FS_BAD_DIR_ENTRY) // No more folder, List all Files [debug]
{
int ListResult;
/*
* RecTravNextFolder may return TravResult = FS_BAD_DIR_ENTRY if the FirstCluster of current folder is 0.
* For this case, ignore file listing and go delete current folder directly. (W11.11)
*/
if (TravResult == FS_NO_MORE_FILES)
{
/* reset start position for listing all files */
kal_mem_set(&TmpDirPos, 0, sizeof(RTFDirEntry));
RecTravCoreFlat:
ListFH = RTFFindFirstEx((WCHAR *)fp->FullName, 0, attr_mask, &(TmpDirPos.Dir), TravCB->NameBuf, MaxLength, NULL, TravCB->CallerAddress); // search once inside
if (ListFH < 0) ListResult = ListFH;
else ListResult = RTF_NO_ERROR;
while (ListResult == RTF_NO_ERROR)
{
/* do ACT for each file */
ActResult = Act(TravCB, TravCB->NameBuf , &(TmpDirPos.Dir) /* File */); // search once inside
if (ActResult < RTF_NO_ERROR)
{
// path over len or operation abort indication
ListResult = ActResult;
if(TravCB->Parameters.ErrorCode == MT_ABORTED_ERROR)
{
ListResult = MT_ABORTED_ERROR;
break;
}
continue;
}
ListResult = RTFFindNextEx(ListFH, &(TmpDirPos.Dir), TravCB->NameBuf, MaxLength, NULL);
}
RTFFindClose(ListFH);
if ((TravCB->Parameters.Flag & FS_RECURSIVE_TYPE) == 0) // flat
{
if (ListResult == RTF_NO_MORE_FILES) // No more folder, No more files
{
TravCB->TravStatus = RTF_NO_ERROR;
}
else // Error Handle of Flat
{
TravCB->TravStatus = ListResult;
}
break; // leave this routine (do-while-loop)
}
}
else // FS_BAD_DIR_ENTRY
{
// treat TravResult == FS_BAD_DIR_ENTRY as ListResult = FS_NO_MORE_FILES to delete the current folder below (W11.11)
ListResult = FS_NO_MORE_FILES;
}
if (ListResult == FS_NO_MORE_FILES) // No more folder, No more files
{
if (LevelIdx-- > 0)
{
RecTravUpLevelAndBackward(fp, TravCB->LevelStack + LevelIdx);
// update current folder
kal_wstrcpy(TravCB->CurrPath, (WCHAR*) fp->FullName);
RecConf_MemoryChecker(&TravCB); // for debug (check name buffer guard pattern)
NamePtr = kal_wstrrchr(TravCB->CurrPath, 0x005c);
*(++NamePtr) = 0x0;
TravCB->CurrLeftLen = MT_MAXPATH_IN_WCHAR_UNIT - 1 /* preserve space for '\0' */ - (NamePtr - TravCB->CurrPath) /* kal_wstrlen(TravCB->CurrPath) */;
// Reterieve the folder name
TravResult = RecTravNextFolder(TravFH, TravCB->NameBuf, &TmpDirPos);
#ifdef __EFS_DEBUG__
// Double Check
fs_assert_local(TravResult == RTF_NO_ERROR);
#endif /* __EFS_DEBUG__ */
// Act
ActResult = Act(TravCB, TravCB->NameBuf , &(TmpDirPos.Dir) /* Folder */);
// path over len or operation abort indication
if (ActResult < RTF_NO_ERROR)
{
ListResult = ActResult;
goto DFS_LIST_ERROR;
}
}
continue;
}
DFS_LIST_ERROR:
TravCB->TravStatus = ListResult;
break;
}
else /* DFS_TRAV_ERROR: */
{
TravCB->TravStatus = TravResult;
break;
}
} while (LevelIdx >= 0);
}
void RecTravCore_BFS(InternRecursiveEngineStruct *TravCB, RTFHANDLE TravFH, MTGenericCallBack *Act)
{
RTFHANDLE ListFH;
int LevelIdx=0;
RTFDirEntry TmpDirPos;
#ifdef __EFS_DEBUG__
UINT U = TravFH >> (4*sizeof(int));
#endif /* __EFS_DEBUG__ */
UINT i = TravFH & ((1 << (4*sizeof(int))) - 1);
RTFile* fp;
int ListResult;
int ActResult;
WCHAR* NamePtr;
int MaxLength; // for FS_Count() enhancement
if (Act == RecAct_CountNum)
{
MaxLength = 8 + 3 + 1 + 1; // We only need sufficient length for SFN to skipped LFN handling in RTFFindNextEx() to save time
#ifdef __FS_OPEN_HINT__
MaxLength |= MT_HINT_DISABLE;
#endif
}
else
{
MaxLength = RTF_MAX_PATH;
}
fp = gFS_Data.FileTable + i;
#ifdef __EFS_DEBUG__
fs_assert_local(fp->Unique == U && i < FS_MAX_FILES && fp->Lock != 0 && fp->Dev != NULL);
fs_assert_local(Act != NULL && TravCB->NameBuf != NULL);
#endif /* __EFS_DEBUG__ */
do
{
/*-- BFS 0 -------------------------------------------
* Setup current folder.
*----------------------------------------------------*/
kal_wstrcpy(TravCB->CurrPath, (WCHAR*) fp->FullName);
NamePtr = kal_wstrrchr(TravCB->CurrPath, 0x005c);
*(++NamePtr) = 0x0; // remove tailed '*'
// now TravCB->CurrPath is like "X:\\ABC\\"
TravCB->CurrLeftLen = 1 /* preserve space for '\0' */ + kal_wstrlen(TravCB->CurrPath) /* length of CurrPath ('*' was removed above) */;
if (TravCB->CurrLeftLen < MT_MAXPATH_IN_WCHAR_UNIT)
TravCB->CurrLeftLen = MT_MAXPATH_IN_WCHAR_UNIT - TravCB->CurrLeftLen;
else
TravCB->CurrLeftLen = 0;
/*-- BFS 1 ------------------------------------------
* Traverse all files in current folder.
*----------------------------------------------------*/
/* Use another handle allocated by RTFFindFirstEx to list all files */
ListFH = RTFFindFirstEx((WCHAR *)fp->FullName, 0, RTF_ATTR_DIR, &(TmpDirPos.Dir), TravCB->NameBuf, MaxLength, NULL, TravCB->CallerAddress);
if (ListFH < 0) ListResult = ListFH;
else ListResult = RTF_NO_ERROR;
while (ListResult == RTF_NO_ERROR)
{
ActResult = Act(TravCB, TravCB->NameBuf , &(TmpDirPos.Dir) /* File */);
if (ActResult < RTF_NO_ERROR)
{ // path over len or operation abort indication
ListResult = ActResult;
if(TravCB->Parameters.ErrorCode==MT_ABORTED_ERROR)
{
ListResult = MT_ABORTED_ERROR;
break;
}
continue;
}
ListResult = RTFFindNextEx(ListFH, &(TmpDirPos.Dir), TravCB->NameBuf, MaxLength, NULL);
}
RTFFindClose(ListFH);
/*-- BFS 2 ----------------------------------------------------------------------
* Find a folder in current folder, then enter it and goto BFS 0.
* If no more folder, back to upper level and find next folder in upper level.
*-------------------------------------------------------------------------------*/
if (ListResult == RTF_NO_MORE_FILES) // No more files, Trav Next Folder
{
int TravResult;
BFS_PREV_LEVEL:
/* find next folder (attribute DIR and mask were properly set by RecTravStart) */
TravResult = RecTravNextFolder(TravFH, TravCB->NameBuf, &TmpDirPos);
if (TravResult == RTF_NO_ERROR) // 1 folder found
{
ActResult = Act(TravCB, TravCB->NameBuf , &(TmpDirPos.Dir) /* Folder */);
if (ActResult < RTF_NO_ERROR) // path over len or operation abort indication
{
ListResult = ActResult;
goto BFS_LIST_ERROR;
}
/*---------------------------------------------------------------------
* We have no memory to store current layer's information for going to next level, break!
* Note that all files in current level have been processed!
*---------------------------------------------------------------------*/
if (LevelIdx == TravCB->LevelLimit)
{
TravCB->TravStatus = RTF_TOO_MANY_FILES;
break;
}
if (kal_wstrlen(TravCB->NameBuf) > TravCB->CurrLeftLen)
{
// The Path Length over Spec. definiation
TravCB->TravStatus = MT_PATH_OVER_LEN_ERROR;
break;
}
/* save current layer's information and go to next level */
ActResult = RecTravDownLevel(fp, TravCB->LevelStack + LevelIdx, &TmpDirPos, TravCB->NameBuf);
if (ActResult < RTF_NO_ERROR) // path over len
{
TravCB->TravStatus = MT_PATH_OVER_LEN_ERROR;
break;
}
LevelIdx++;
/* back to BFS 0 to start a traverse in next level */
continue;
}
else if (TravResult == RTF_NO_MORE_FILES) // No more files, No more folders
{
if (LevelIdx-- > 0)
{
RecTravUpLevel(fp, TravCB->LevelStack + LevelIdx);
kal_wstrcpy(TravCB->CurrPath, (WCHAR*) fp->FullName);
NamePtr = kal_wstrrchr(TravCB->CurrPath, 0x005c);
*(++NamePtr) = 0x0;
TravCB->CurrLeftLen = MT_MAXPATH_IN_WCHAR_UNIT - kal_wstrlen(TravCB->CurrPath) - 1;
goto BFS_PREV_LEVEL;
}
continue;
}
/* BFS_TRAV_ERROR: */
TravCB->TravStatus = TravResult;
break;
}
BFS_LIST_ERROR:
TravCB->TravStatus = ListResult;
break;
} while (LevelIdx >= 0);
}
void RecTravCore_Flat(InternRecursiveEngineStruct *TravCB, RTFHANDLE TravFH, MTGenericCallBack *Act)
{
RTFHANDLE ListFH;
RTFDirEntry TmpDirPos;
#ifdef __EFS_DEBUG__
UINT U = TravFH >> (4*sizeof(int));
#endif /* __EFS_DEBUG__ */
UINT i = TravFH & ((1 << (4*sizeof(int))) - 1);
RTFile* fp;
int ListResult = RTF_NO_ERROR;
int ActResult;
WCHAR* NamePtr;
int MaxLength;
fp = gFS_Data.FileTable + i;
#ifdef __EFS_DEBUG__
fs_assert_local(fp->Unique == U && i < FS_MAX_FILES && fp->Lock != 0 && fp->Dev != NULL);
fs_assert_local(Act != NULL && TravCB->NameBuf != NULL);
#endif /* __EFS_DEBUG__ */
// setup current folder
kal_wstrcpy(TravCB->CurrPath, (WCHAR*) fp->FullName);
NamePtr = kal_wstrrchr(TravCB->CurrPath, 0x005c);
*(++NamePtr) = 0x0; // remove tailed '*'
// now TravCB->CurrPath is like "X:\\ABC\\"
TravCB->CurrLeftLen = 1 /* preserve space for '\0' */ + kal_wstrlen(TravCB->CurrPath) /* length of CurrPath ('*' was removed above) */;
if (TravCB->CurrLeftLen < MT_MAXPATH_IN_WCHAR_UNIT)
TravCB->CurrLeftLen = MT_MAXPATH_IN_WCHAR_UNIT - TravCB->CurrLeftLen;
else
TravCB->CurrLeftLen = 0;
if (Act == RecAct_CountNum)
{
MaxLength = 8 + 3 + 1 + 1; // We only need sufficient length for SFN to skipped LFN handling in RTFFindNextEx() to save time
#ifdef __FS_OPEN_HINT__
MaxLength |= MT_HINT_DISABLE;
#endif
}
else
MaxLength = RTF_MAX_PATH;
// Flat , list all
ListFH = RTFFindFirstEx((WCHAR *)fp->FullName, 0, 0, &(TmpDirPos.Dir), TravCB->NameBuf, MaxLength, NULL, TravCB->CallerAddress);
if (ListFH < 0) ListResult = ListFH;
while(ListResult == RTF_NO_ERROR)
{
if (TmpDirPos.Dir.FileName[0] != 0x2e) // skip dot and dot-dot
{
ActResult = Act(TravCB, TravCB->NameBuf , &(TmpDirPos.Dir) /* File */);
if (ActResult < RTF_NO_ERROR)
{ // path over len or operation abort indication
ListResult = ActResult;
if(TravCB->Parameters.ErrorCode==MT_ABORTED_ERROR)
{
ListResult = MT_ABORTED_ERROR;
break;
}
continue;
}
}
ListResult = RTFFindNextEx(ListFH, &(TmpDirPos.Dir), TravCB->NameBuf, MaxLength, NULL);
}
RTFFindClose(ListFH);
if (ListResult == RTF_NO_MORE_FILES) // No more folder, No more files
{
TravCB->TravStatus = RTF_NO_ERROR;
}
else // Error Handle of Flat
{
TravCB->TravStatus = ListResult;
}
}
void RecTravCore_CloseAndRootNodeAct(InternRecursiveEngineStruct *TravCB, RTFHANDLE TravFH, MTGenericCallBack *Act)
{
#ifdef __EFS_DEBUG__
UINT U = TravFH >> (4*sizeof(int));
#endif /* __EFS_DEBUG__ */
UINT i = TravFH & ((1 << (4*sizeof(int))) - 1);
RTFile* fp;
WCHAR* NamePtr;
RTFDirEntry TmpDirPos;
fp = gFS_Data.FileTable + i;
#ifdef __EFS_DEBUG__
fs_assert_local(fp->Unique == U && i < FS_MAX_FILES && fp->Lock != 0 && fp->Dev != NULL);
fs_assert_local(Act != NULL && TravCB->NameBuf != NULL);
#endif /* __EFS_DEBUG__ */
// setup Name Buf & current folder
kal_wstrcpy(TravCB->CurrPath, (WCHAR*) fp->FullName); /* C:\Level1\* */
NamePtr = kal_wstrrchr(TravCB->CurrPath, 0x005c);
if (NamePtr == NULL)
{
fs_util_trace_info2(TRACE_ERROR, FS_ERR_NULL_PTR_1, fs_internal_c, __LINE__, NULL);
return;
}
*NamePtr = 0x0;
NamePtr = kal_wstrrchr(TravCB->CurrPath, 0x005c);
if (NamePtr == NULL) /* ROOT Directory, do nothing */
{
return;
}
NamePtr++;
kal_wstrcpy(TravCB->NameBuf, NamePtr);
*NamePtr = 0x0;
TravCB->CurrLeftLen = MT_MAXPATH_IN_WCHAR_UNIT - kal_wstrlen(TravCB->CurrPath) - 1;
// prepare Dos Dir Entry
memcpy(&TmpDirPos, &(fp->DirEntry), sizeof(RTFDirEntry));
// Close Before Act
RecTravClose(TravFH);
// Act
Act(TravCB, TravCB->NameBuf , &(TmpDirPos.Dir) /* File or Dir */);
}
#ifdef __P_PROPRIETARY_COPYRIGHT__
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#ifdef __EFS_DEBUG__
/* under construction !*/
#endif /* __EFS_DEBUG__ */
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#ifdef __EFS_DEBUG__
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#endif /* __EFS_DEBUG__ */
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#endif /* __P_PROPRIETARY_COPYRIGHT__ */
/* ------------------------------------------------------------------------------- */
/**************************************************
* Generic CallBack Class
* Members:
* RecAct_List
* RecAct_CountNum
* RecAct_CountSize
* RecAct_Deletion
* RecAct_Copy
* RecAct_CopyrightDeletion
* RecAct_CopyrightList
**************************************************/
/**************************************************
* Structure Member Required
**************************************************/
int RecAct_CountNum(InternRecursiveEngineStruct *RES, WCHAR *ObjName, RTFDOSDirEntry *ObjInfo)
{
#ifdef __EFS_DEBUG__
fs_assert_local(RES != NULL && ObjName != NULL && ObjInfo != NULL);
#endif /* __EFS_DEBUG__ */
if ((RES->Parameters.Flag & FS_FILTER_SYSTEM_ATTR) && (ObjInfo->Attributes & RTF_ATTR_SYSTEM))
{
return 0;
}
if ((RES->Parameters.Flag & FS_FILTER_HIDDEN_ATTR) && (ObjInfo->Attributes & RTF_ATTR_HIDDEN))
{
return 0;
}
if ((RES->Parameters.Flag & FS_DIR_TYPE) && (ObjInfo->Attributes & RTF_ATTR_DIR))
{
RES->Parameters.Result++;
}
else if ((RES->Parameters.Flag & FS_FILE_TYPE) && (ObjInfo->Attributes & (RTF_ATTR_DIR|RTF_ATTR_VOLUME)) == 0)
{
RES->Parameters.Result++;
}
if (RES->Parameters.Progress) // abort checking
{
if (RES->Parameters.Progress(FS_PGS_ING, 0, 0, 0) < 0)
{
RES->Parameters.ErrorCode = MT_ABORTED_ERROR;
return MT_ABORTED_ERROR;
}
}
return 0;
}
int RecAct_CountSize(InternRecursiveEngineStruct *RES, WCHAR *ObjName, RTFDOSDirEntry *ObjInfo)
{
UINT volatile Count = 0;
RTFile * volatile f = NULL;
UINT Cluster, NextCluster = 0;
#ifdef __EFS_DEBUG__
fs_assert_local(RES != NULL && ObjName != NULL && ObjInfo != NULL);
fs_assert_local(RES->Parameters.Drive != NULL && RES->Parameters.RAWCluster != NULL);
#endif /* __EFS_DEBUG__ */
XTRY
case XCODE:
f = ParseFileHandle(RES->Parameters.ProgInfo);
Cluster = FIRST_FILE_CLUSTER((*ObjInfo));
if (Cluster == 0) // this file has no cluster allocated for it
{
break;
}
while (NextCluster < RTF_CLUSTER_CHAIN_END)
{
NextCluster = RES->Parameters.RAWCluster(RES->Parameters.Drive, Cluster);
if (NextCluster < 2)
break;
Count++;
if (Count > f->Drive->Clusters) // to avoid endless loop due to cross-link
{
XRAISE(FS_FAT_ALLOC_ERROR);
}
Cluster = NextCluster;
}
break;
default:
break;
case XFINALLY:
if (f != NULL)
{
UnlockDevice(f->Dev);
}
break;
XEND_API
if (RES->Parameters.Flag & FS_COUNT_IN_CLUSTER)
{
RES->Parameters.Result += Count;
}
else if (RES->Parameters.Flag & FS_COUNT_IN_BYTE)
{
RES->Parameters.Result += Count << RES->Parameters.Drive->ClusterShift;
}
if (RES->Parameters.Progress) // abort checking
{
if (RES->Parameters.Progress(FS_PGS_ING, 0, RES->Parameters.Result, 0) < 0)
{
RES->Parameters.ErrorCode = MT_ABORTED_ERROR;
return MT_ABORTED_ERROR;
}
}
return 0;
}
int RecAct_Deletion(InternRecursiveEngineStruct *RES, WCHAR *ObjName, RTFDOSDirEntry *ObjInfo)
{
int result = RTF_NO_ERROR;
#ifdef __EFS_DEBUG__
fs_assert_local(RES != NULL && ObjName != NULL && ObjInfo != NULL);
#endif /* __EFS_DEBUG__ */
/*------------------------------------------------------------------------
* Check if deletion is aborted before.
*
* There is another abort mechanism by calling progress call-back function
* and check the result. However it might not be assigned, like FMGR.
*
* Remember to set error code in parameter, otherwise
* RecAUX_XDeleteFolder() may return FS_NO_ERROR even if g_Xdelete was
* set to KAL_FALSE, and "Deleted" will pop-up incorrectly.
*-------------------------------------------------------------- W10.03 --*/
if ((RES->Parameters.Flag & FS_XDEL_ABORT_WATCH) && (g_Xdelete != KAL_TRUE))
{
RES->Parameters.ErrorCode = MT_ABORTED_ERROR;
return MT_ABORTED_ERROR;
}
// check if the Path Length of that file over Spec. definiation
if (kal_wstrlen(ObjName) > RES->CurrLeftLen)
{
RES->Parameters.ErrorCode = MT_PATH_OVER_LEN_ERROR;
return MT_PATH_OVER_LEN_ERROR;
}
kal_wstrcpy(RES->DestPath, RES->CurrPath);
RecConf_MemoryChecker(&RES); // for debug (check name buffer guard pattern)
kal_wstrcat(RES->DestPath, ObjName);
RecConf_MemoryChecker(&RES); // for debug (check name buffer guard pattern)
if ((RES->Parameters.Flag & FS_DIR_TYPE) && (ObjInfo->Attributes & RTF_ATTR_DIR))
{
result = RTFRemoveDir(RES->DestPath);
/* Ignore Fail on Folder Deletion here (if any INUSE directory entry found in this DIR, RTF_ACCESS_DENIED is raised). */
if (result < 0)
{
RES->Parameters.ErrorCode = result;
}
RES->Parameters.Result++;
}
else if ((RES->Parameters.Flag & FS_FILE_TYPE) && (ObjInfo->Attributes & (RTF_ATTR_DIR|RTF_ATTR_VOLUME)) == 0)
{
result = RTFDelete(RES->DestPath);
if (result < 0)
{
RES->Parameters.ErrorCode = result;
return 1;
}
RES->Parameters.Result++;
}
if(result == RTF_NO_ERROR && RES->Parameters.Progress)
{
if(RES->Parameters.Progress(FS_XDELETE_PGS_ING, 0, RES->Parameters.Result, 0) < 0)
{
RES->Parameters.ErrorCode = MT_ABORTED_ERROR;
return MT_ABORTED_ERROR;
}
}
return 0;
}
int RecAct_Copy(InternRecursiveEngineStruct *RES, WCHAR *ObjName, RTFDOSDirEntry *ObjInfo)
{
int result = RTF_NO_ERROR;
int Len_Curr, Len_Obj;
#ifdef __EFS_DEBUG__
fs_assert_local(RES != NULL && ObjName != NULL && ObjInfo != NULL);
#endif /* __EFS_DEBUG__ */
Len_Curr = kal_wstrlen(RES->CurrPath);
Len_Obj = kal_wstrlen(ObjName);
if (Len_Obj > RES->CurrLeftLen)
{ // The Src Path Length of that file over Spec. definiation
return MT_PATH_OVER_LEN_ERROR;
}
if (Len_Curr - RES->PrefixPathLen + RES->DestPrefixPathLen + Len_Obj > MT_MAXPATH_IN_WCHAR_UNIT)
{ // The Dest Path Length of that file over Spec. definiation
return MT_PATH_OVER_LEN_ERROR;
}
kal_wstrcpy(RES->DestPath + RES->DestPrefixPathLen, RES->CurrPath + RES->PrefixPathLen);
kal_wstrcat(RES->DestPath, ObjName);
if (ObjInfo->Attributes & RTF_ATTR_DIR)
{
result = RTFCreateDir(RES->DestPath, ObjInfo->Attributes);
if (result < 0) return result; // do not ignore fail on folder creation!
RES->Parameters.Result++;
}
else
{
/* Note : Here we will borrow RES->CurrPath to store full source path */
kal_wstrcat(RES->CurrPath, ObjName);
result = CopyFileLightWeight(RES->CurrPath, RES->DestPath, RES->Parameters.Progress, RES->Parameters.PrivateData, RES->Parameters.PrivateData?512:0, RES->CallerAddress);
/* Note : Here we will return RES->CurrPath to store full source path */
RES->CurrPath[Len_Curr] = 0x0;
if (result < 0)
{
RES->Parameters.ErrorCode = result;
return result;
}
RES->Parameters.Result++;
}
if (RES->Parameters.Progress) // add abort mechansim for folder copy
{
if(RES->Parameters.Progress(FS_MOVE_PGS_ING, RES->Parameters.Total, RES->Parameters.Result, RES->Parameters.ProgInfo) < 0)
{
RES->Parameters.ErrorCode = MT_ABORTED_ERROR;
return MT_ABORTED_ERROR;
}
}
return 0;
}
#ifdef __P_PROPRIETARY_COPYRIGHT__
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#ifdef __EFS_DEBUG__
/* under construction !*/
#endif /* __EFS_DEBUG__ */
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#endif /* __P_PROPRIETARY_COPYRIGHT__ */
#ifdef __P_PROPRIETARY_COPYRIGHT__
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#ifdef __EFS_DEBUG__
/* under construction !*/
#endif /* __EFS_DEBUG__ */
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#endif /* __P_PROPRIETARY_COPYRIGHT__ */
/* ------------------------------------------------------------------------------- */
/**************************************************
* Recursive Engine Configure Utilities
* Members:
* RecConf_Alloc
* RecConf_Free
**************************************************/
int RecConf_Alloc(InternRecursiveEngineStruct **RES, BYTE *RecursiveStack, const UINT StackSize)
{
kal_char *bufptr;
InternRecursiveEngineStruct *RESptr;
int ModifiedStackSize;
if (RecursiveStack == NULL)
{
#ifdef __FS_DEDICATED_BUFFER__
MTBufAlloc(FS_XDELETE_BUFFER_SIZE_FOR_FOLDER_LEVEL(FS_MAX_FOLDER_LEVEL), (unsigned char**)&bufptr, FS_INT_DBUF_ALLOC_PRECISE);
if (bufptr != NULL)
{
RESptr = (InternRecursiveEngineStruct *) bufptr;
kal_mem_set(bufptr, 0, sizeof(InternRecursiveEngineStruct) + (3 * RECCONF_NAMEBUF_PACKAGE_SIZE));
RESptr->LevelStack = (RTFDirEntry*)(bufptr + (sizeof(InternRecursiveEngineStruct) + (3 * RECCONF_NAMEBUF_PACKAGE_SIZE)));
RESptr->LevelLimit = FS_MAX_FOLDER_LEVEL;
}
else
#endif /* __FS_DEDICATED_BUFFER__ */
{
/* Memory Usage : 64 + 3 * ((520+4) + 4) = 1648 */
bufptr = get_ctrl_buffer(sizeof(InternRecursiveEngineStruct) + 3 * RECCONF_NAMEBUF_PACKAGE_SIZE);
RESptr = (InternRecursiveEngineStruct *) bufptr;
kal_mem_set(bufptr, 0, sizeof(InternRecursiveEngineStruct) + (3 * RECCONF_NAMEBUF_PACKAGE_SIZE));
/* default max level count: 39 */
RESptr->LevelStack = get_ctrl_buffer(RECCONF_DIRSTACK_SIZE);
RESptr->LevelLimit = RECCONF_DIRSTACK_SIZE / sizeof(RTFDirEntry);
}
}
else
{
/* Must 4-Byte align buffer pointer, stop dangerous usage early */
fs_ext_assert_local( ((UINT)RecursiveStack & 0x03) == 0x00, (UINT)RecursiveStack, StackSize, 0);
if(StackSize<(sizeof(InternRecursiveEngineStruct) + 3 * RECCONF_NAMEBUF_PACKAGE_SIZE))
{
return MT_FAIL_GET_MEM;
}
bufptr = (kal_char*) RecursiveStack;
kal_mem_set(bufptr, 0, sizeof(InternRecursiveEngineStruct) + 3 * RECCONF_NAMEBUF_PACKAGE_SIZE);
ModifiedStackSize = StackSize - (sizeof(InternRecursiveEngineStruct) + 3 * RECCONF_NAMEBUF_PACKAGE_SIZE);
RESptr = (InternRecursiveEngineStruct *) bufptr;
/* if user specify a RecursiveStack, max level count can be customized */
RESptr->LevelStack = (RTFDirEntry*)((kal_char *)RecursiveStack + (sizeof(InternRecursiveEngineStruct) + 3 * RECCONF_NAMEBUF_PACKAGE_SIZE));
RESptr->LevelLimit = ModifiedStackSize / sizeof(RTFDirEntry);
}
bufptr += sizeof(InternRecursiveEngineStruct);
*((int *)bufptr) = RECCONF_NAMEBUF_GUARD_PRINT;
RESptr->CurrPath = (WCHAR*)(bufptr + sizeof(int));
bufptr += RECCONF_NAMEBUF_PACKAGE_SIZE;
*((int *)bufptr) = RECCONF_NAMEBUF_GUARD_PRINT;
RESptr->NameBuf = (WCHAR*)(bufptr + sizeof(int));
bufptr += RECCONF_NAMEBUF_PACKAGE_SIZE;
*((int *)bufptr) = RECCONF_NAMEBUF_GUARD_PRINT;
RESptr->DestPath = (WCHAR*)(bufptr + sizeof(int));
*RES = RESptr;
return RTF_NO_ERROR;
}
void RecConf_MemoryChecker(InternRecursiveEngineStruct **RES)
{
kal_char *bufptr;
bufptr = (void *)(*RES);
// Check RECCONF_NAMEBUF_GUARD_PRINT (in-house only)
bufptr += sizeof(InternRecursiveEngineStruct);
fs_assert_local(*((int *)bufptr) == RECCONF_NAMEBUF_GUARD_PRINT);
bufptr += RECCONF_NAMEBUF_PACKAGE_SIZE;
fs_assert_local(*((int *)bufptr) == RECCONF_NAMEBUF_GUARD_PRINT);
bufptr += RECCONF_NAMEBUF_PACKAGE_SIZE;
fs_assert_local(*((int *)bufptr) == RECCONF_NAMEBUF_GUARD_PRINT);
}
void RecConf_Free(InternRecursiveEngineStruct **RES, BYTE *RecursiveStack, const UINT StackSize)
{
RecConf_MemoryChecker(RES);
/* Free Memory */
if (RecursiveStack == NULL)
{
#ifdef __FS_DEDICATED_BUFFER__
if (MTBufCheckRange((unsigned int)*RES))
MTBufFree(FS_XDELETE_BUFFER_SIZE_FOR_FOLDER_LEVEL(FS_MAX_FOLDER_LEVEL), (unsigned char**)RES);
else
#endif /* __FS_DEDICATED_BUFFER__ */
{
free_ctrl_buffer((*RES)->LevelStack);
free_ctrl_buffer((*RES));
}
}
*RES = NULL;
}
/* ------------------------------------------------------------------------------- */
/**************************************************
* Recursive Type API Auxiliary Sub-Routines
* Members:
* RecAUX_IsFolder
* RecAUX_IsFolderRW
* RecAUX_TestSrcAndDestPath
* RecAUX_CountNumOfObjUnderFolderTree
* RecAUX_CountSumOfSizeUnderFolderTree
* RecAUX_XDeleteFolder
* RecAUX_XCopyFolder
**************************************************/
int RecAUX_IsFolder(const WCHAR * PathName, kal_bool rw_check)
{
int volatile Result = RTF_NO_ERROR;
RTFile * volatile f = NULL;
XTRY
case XCODE:
f = ParseFileName((char *)PathName);
if (rw_check) RTFileCheck_WriteProtect(f);
RTFileCheck_NormalFile_InvalidFilename(f);
if (!SearchFile(f, SEARCH_FILES, (char *)PathName, NULL))
XRAISE(RTF_PATH_NOT_FOUND);
if((f->DirEntry.Dir.Attributes & RTF_ATTR_DIR) == 0)
XRAISE(RTF_PARAM_ERROR);
break;
default:
Result = XVALUE; //API dose not need XHANDLED
break;
case XFINALLY:
if (f != NULL)
FreeFTSlotAndDevice(f);
break;
XEND_API
return Result;
}
/**********************************************************
* RecAUX_TestSrcAndDestPath ---- The sub-routine to check source full path and destination full path
* RETURNS
* < RTF_NO_ERROR , serious error
* >= 0 , Value that combination of MT_XMOVE_XXX bit flag
*
**********************************************************/
int RecAUX_TestSrcAndDestPath(const WCHAR * SrcPathName, const WCHAR * DestPathName, UINT Flag)
{
int volatile Result = RTF_NO_ERROR;
RTFile * volatile f1 = NULL; // src
RTFile * volatile f2 = NULL; // dst
XTRY
case XCODE:
/* TestSrcAndDestPath - 1 : Process SrcPathName basic test */
f1 = ParseFileName((char *)SrcPathName);
if (f1->SpecialKind == FileMapFile)
{
Result |= MT_XMOVE_SRC_IS_VIRTUAL;
}
else
{
RTFileCheck_NormalFile_InvalidFilename(f1);
CheckValidFileName2(SrcPathName, KAL_FALSE);
if (!SearchFile(f1, SEARCH_FILES, (char *)SrcPathName, NULL))
{
fs_util_trace_err_noinfo(RTF_PATH_NOT_FOUND);
XRAISE(RTF_PATH_NOT_FOUND);
}
if(Flag&FS_MOVE_KILL)
{
CheckNotOpen(f1);
}
}
/* TestSrcAndDestPath - 2 : Try to access DestPathName, f2 */
RTFSYSFreeMutex(RTFLock);
f2 = ParseFileName((char *)DestPathName);
RTFileCheck_NormalFile_InvalidFilename(f2);
RTFileCheck_WriteProtect(f2);
/* TestSrcAndDestPath - 3 : set result accroding to f1 */
Result |= MT_XMOVE_SRC_EXIST;
if (f1->DirEntry.Dir.Attributes & RTF_ATTR_DIR)
Result |= MT_XMOVE_SRC_IS_FOLDER;
if (f1->DirEntry.Dir.Attributes & RTF_ATTR_READ_ONLY)
Result |= MT_XMOVE_SRC_ATTR_RO;
if (f1->Dev->DeviceFlags & MT_DEVICE_WRITE_PROTECT)
Result |= MT_XMOVE_SRC_DEVICE_RO;
/* TestSrcAndDestPath - 4 : set result accroding to f2 */
if (SearchFile(f2, SEARCH_FILES, (char *)DestPathName, NULL))
{
/* destination file or folder is already existed */
Result |= MT_XMOVE_DEST_EXIST;
if (f2->DirEntry.Dir.Attributes & RTF_ATTR_DIR)
Result |= MT_XMOVE_DEST_IS_FOLDER;
if (f2->DirEntry.Dir.Attributes & RTF_ATTR_READ_ONLY)
Result |= MT_XMOVE_DEST_ATTR_RO;
/* Check if SrcPathName == DestPathName */
if ((f1->Drive == f2->Drive) &&
(f1->SpecialKind == f2->SpecialKind) &&
((f1->DirEntry.ShortPos.Cluster == f2->DirEntry.ShortPos.Cluster) &&
(f1->DirEntry.ShortPos.Index == f2->DirEntry.ShortPos.Index)))
{
fs_util_trace_err_noinfo(RTF_FILE_EXISTS);
XRAISE(RTF_FILE_EXISTS);
}
// if we do not want to overwrite the existed "file", raise RTF_FILE_EXISTS
else if (!(Flag & FS_MOVE_OVERWRITE) && !(Result & MT_XMOVE_DEST_IS_FOLDER))
{
fs_util_trace_err_noinfo(RTF_FILE_EXISTS);
XRAISE(RTF_FILE_EXISTS);
}
CheckNotOpen(f2);
}
if (f2->Dev->DeviceFlags & MT_DEVICE_WRITE_PROTECT)
Result |= MT_XMOVE_DEST_DEVICE_RO;
/* TestSrcAndDestPath - 5 : set result accroding to f1,f2 */
if (f1->Dev == f2->Dev)
Result |= MT_XMOVE_SAME_DEVICE;
if (f1->Drive == f2->Drive)
Result |= MT_XMOVE_SAME_DRIVE;
break;
default:
Result = XVALUE; //API dose not need XHANDLED
break;
case XFINALLY:
if (f2 != NULL) /* imply f1 != NULL, see above */
{
FreeFTSlotAndDevice(f2);
RTFSYSLockMutex(RTFLock, RTF_INFINITE);
FreeFTSlotAndDevice(f1);
}
else if (f1 != NULL)
{
SafeLock(MT_LOCK_RTF | MT_LOCK_DEV, f1->Dev, RTF_INFINITE); /* it may raise an exception after release the system lock */
FreeFTSlotAndDevice(f1);
}
break;
XEND_API
return Result;
}
int RecAUX_Delete(
UINT Flag,
FS_HANDLE TravFH,
InternRecursiveEngineStruct *ReCB)
{
int volatile Result = RTF_NO_ERROR;
/* XDeleteFolder - 3 : Recusrive Traverse Call */
// last function call may spend lots of time (search parent), check if XDelete is aborted
if ((Flag & FS_XDEL_ABORT_WATCH) && (g_Xdelete != KAL_TRUE))
{
Result = MT_ABORTED_ERROR;
fs_util_trace_err_noinfo(Result);
fs_util_trace_info0(TRACE_ERROR, FS_ERR_REC_TRAV_CORE, NULL);
RecTravClose(TravFH); // close file handle, otherwise this folder will always not be deleted in the future.
return Result;
}
if (Flag & FS_RECURSIVE_TYPE)
{
RecTravCore_DFS(ReCB, TravFH, RecAct_Deletion);
RecTravCore_CloseAndRootNodeAct(ReCB, TravFH, RecAct_Deletion);
}
else
{
RecTravCore_DFS(ReCB, TravFH, RecAct_Deletion); // Merge Flat engine to DFS engine
//RecTravCore_Flat(ReCB, TravFH, RecAct_Deletion);
RecTravClose(TravFH);
}
/* XDeleteFolder - 4 : Check Result and Error Status after Recusrive Call */
if (ReCB->TravStatus < 0) /* Recursive Traverse Failure */
{
Result = ReCB->TravStatus;
fs_util_trace_err_noinfo(Result);
fs_util_trace_info0(TRACE_ERROR, FS_ERR_REC_TRAV_CORE, NULL);
}
else if (ReCB->Parameters.ErrorCode < 0) /* RecAction Failure */
{
Result = ReCB->Parameters.ErrorCode;
fs_util_trace_err_noinfo(Result);
fs_util_trace_info0(TRACE_ERROR, FS_ERR_REC_TRAV_ACTION, NULL);
}
else /* Success */
{
Result = ReCB->Parameters.Result;
}
return Result;
}
int RecAUX_CountObject(
UINT Flag,
FS_HANDLE TravFH,
InternRecursiveEngineStruct *ReCB)
{
int volatile Result = RTF_NO_ERROR;
if (Flag & FS_RECURSIVE_TYPE)
{
#if !defined(__FS_SLIM_BFS__)
RecTravCore_BFS(ReCB, TravFH, RecAct_CountNum);
#else // use DFS instead of BFS in Slim projects
RecTravCore_DFS(ReCB, TravFH, RecAct_CountNum);
#endif
RecTravClose(TravFH);
}
else
{
RecTravCore_DFS(ReCB, TravFH, RecAct_CountNum); // Merge Flat engine to DFS engine
//RecTravCore_Flat(ReCB, TravFH, RecAct_CountNum);
RecTravClose(TravFH);
}
/* CountNumOfObjUnderFolderTree - 4 : Check Result and Error Status after Recusrive Call */
if (ReCB->TravStatus < 0) /* Recursive Traverse Failure */
{
Result = ReCB->TravStatus;
fs_util_trace_err_noinfo(Result);
fs_util_trace_info0(TRACE_ERROR, FS_ERR_REC_TRAV_CORE, NULL);
}
else if (ReCB->Parameters.ErrorCode < 0) /* RecAction Failure */
{
Result = ReCB->Parameters.ErrorCode;
fs_util_trace_err_noinfo(Result);
fs_util_trace_info0(TRACE_ERROR, FS_ERR_REC_TRAV_ACTION, NULL);
}
else /* Success */
{
Result = ReCB->Parameters.Result;
}
return Result;
}
int RecAUX_CountSize(
UINT Flag,
FS_HANDLE TravFH,
InternRecursiveEngineStruct *ReCB)
{
int volatile Result = RTF_NO_ERROR;
/* CountSumOfSizeUnderFolderTree - 3 : Recusrive Traverse Call */
ReCB->Parameters.Drive = (RTFDrive*)fs_conf_get_drv_struct_by_drv_letter(ReCB->NameBuf[0]);
if (NULL == ReCB->Parameters.Drive)
{
fs_assert_local(0);
}
ReCB->Parameters.RAWCluster = GetRAWClusterValue;
ReCB->Parameters.ProgInfo = TravFH;
#if !defined(__FS_SLIM_BFS__)
RecTravCore_BFS(ReCB, TravFH, RecAct_CountSize);
#else
RecTravCore_DFS(ReCB, TravFH, RecAct_CountSize);
#endif
RecTravCore_CloseAndRootNodeAct(ReCB, TravFH, RecAct_CountSize);
/* CountSumOfSizeUnderFolderTree - 4 : Check Result and Error Status after Recusrive Call */
if (ReCB->TravStatus < 0) /* Recursive Traverse Failure */
{
Result = ReCB->TravStatus;
fs_util_trace_err_noinfo(Result);
fs_util_trace_info0(TRACE_ERROR, FS_ERR_REC_TRAV_CORE, NULL);
}
else if (ReCB->Parameters.ErrorCode < 0) /* RecAction Failure */
{
Result = ReCB->Parameters.ErrorCode;
fs_util_trace_err_noinfo(Result);
fs_util_trace_info0(TRACE_ERROR, FS_ERR_REC_TRAV_ACTION, NULL);
}
else /* Success */
{
Result = ReCB->Parameters.Result;
}
return Result;
}
int RecAUX(const WCHAR * FullPath, UINT Flag, FS_ProgressCallback Progress, BYTE *RecursiveStack, const UINT StackSize, RecAUX_FuncType Aux)
{
int volatile Result = RTF_NO_ERROR;
FS_HANDLE TravFH;
InternRecursiveEngineStruct *ReCB;
/* 1 : Setup InternRecursiveEngineStruct & Resources */
Result = RecConf_Alloc(&ReCB, RecursiveStack, StackSize);
if(Result<0)
{
return Result;
}
ReCB->Parameters.Flag = Flag;
ReCB->Parameters.Progress = Progress;
/* 2 : Start a Traverse Handle */
kal_wstrcpy(ReCB->NameBuf, FullPath);
kal_wstrcat(ReCB->NameBuf, (WCHAR*)L"\\*");
TravFH = RecTravStart(ReCB->NameBuf);
if (TravFH < 0)
{
Result = TravFH;
fs_util_trace_err_noinfo(Result);
fs_util_trace_info0(TRACE_ERROR, FS_ERR_REC_TRAV_START, NULL);
}
else
{
// Function Body
Result = Aux(Flag, TravFH, ReCB);
}
/* Release InternRecursiveEngineStruct & Resources */
RecConf_Free(&ReCB, RecursiveStack, StackSize);
return Result;
}
/**********************************************************
* RecAUX_CountNumOfObjUnderFolderTree ---- The sub-procedure to do FS_Count
*
**********************************************************/
/**********************************************************
* RecAUX_XCopyFolder ---- The sub-procedure to do FS_Move
*
**********************************************************/
int RecAUX_XCopyFolder(const WCHAR * FullPath, const WCHAR * DstPath, int Status,
FS_ProgressCallback Progress, UINT Total, BYTE *RecursiveStack, const UINT StackSize, kal_uint32 caller_address)
{
int volatile Result = RTF_NO_ERROR;
FS_HANDLE TravFH;
InternRecursiveEngineStruct *ReCB;
BYTE *CopyBuffer = NULL;
int CopyBufferLen = 0;
int ModifiedStackSize = StackSize;
/* Reserve Copy Folder */
if(RecursiveStack)
{
if(StackSize<CopyBufferSize)
{
return MT_FAIL_GET_MEM;
}
if(StackSize<FS_MOVE_BUFFER_SIZE_FOR_FOLDER_LEVEL(128))
{
ModifiedStackSize = StackSize - CopyBufferSize;
CopyBufferLen = CopyBufferSize;
}
else
{
ModifiedStackSize = FS_MOVE_BUFFER_SIZE_FOR_FOLDER_LEVEL(128) - CopyBufferSize;
CopyBufferLen = StackSize - ModifiedStackSize;
}
CopyBuffer = RecursiveStack;
RecursiveStack += CopyBufferLen;
}
/* XCopyFolder - 1 : Setup InternRecursiveEngineStruct & Resources */
Result = RecConf_Alloc(&ReCB, RecursiveStack, ModifiedStackSize);
if(Result<0)
{
return Result;
}
ReCB->CallerAddress = caller_address;
ReCB->Parameters.Flag = (Status & (MT_XMOVE_SAME_DRIVE | MT_XMOVE_SAME_DEVICE));
ReCB->Parameters.Progress = Progress;
ReCB->Parameters.Total = Total;
ReCB->Parameters.PrivateData = CopyBuffer;
/* XCopyFolder - 2 : Start a Traverse Handle */
ReCB->DestPrefixPathLen = kal_wstrlen(DstPath);
ReCB->PrefixPathLen = kal_wstrlen(FullPath);
kal_wstrcpy(ReCB->DestPath, DstPath);
kal_wstrcpy(ReCB->NameBuf, FullPath);
kal_wstrcat(ReCB->NameBuf, (WCHAR*)L"\\*");
TravFH = RecTravStart(ReCB->NameBuf);
if (TravFH < 0)
{
Result = TravFH;
fs_util_trace_err_noinfo(Result);
fs_util_trace_info0(TRACE_ERROR, FS_ERR_REC_TRAV_START, NULL);
}
else
{
/* XCopyFolder - 3 : Recusrive Traverse Call */
ReCB->Parameters.ProgInfo = TravFH;
#if !defined(__FS_SLIM_BFS__)
RecTravCore_BFS(ReCB, TravFH, RecAct_Copy);
#else
RecTravCore_DFS(ReCB, TravFH, RecAct_Copy);
#endif
RecTravClose(TravFH);
/* XCopyFolder - 4 : Check Result and Error Status after Recusrive Call */
if (ReCB->TravStatus < 0) /* Recursive Traverse Failure */
{
Result = ReCB->TravStatus;
fs_util_trace_err_noinfo(Result);
fs_util_trace_info0(TRACE_ERROR, FS_ERR_REC_TRAV_CORE, NULL);
}
else if (ReCB->Parameters.ErrorCode < 0) /* RecAction Failure */
{
Result = ReCB->Parameters.ErrorCode;
fs_util_trace_err_noinfo(Result);
fs_util_trace_info0(TRACE_ERROR, FS_ERR_REC_TRAV_ACTION, NULL);
}
else /* Success */
{
Result = ReCB->Parameters.Result;
}
} /* if RecTracStart(...) */
/* XCopyFolder - 5 : Release InternRecursiveEngineStruct & Resources */
RecConf_Free(&ReCB, RecursiveStack, ModifiedStackSize);
return Result;
}
#ifdef __FS_SORT_SUPPORT__
unsigned int FileUintHint(WCHAR *pUCS2)
{
unsigned char UTF8[8];
unsigned char *pUTF8 = UTF8;
unsigned int UCS2;
int i, j;
unsigned int Result = 0;
for(i=0, j=0; i<4 && j<4; i++)
{
UCS2 = fs_util_wchar_toupper(pUCS2[i]);
if(UCS2==0)
{
pUTF8[j++] = 0;
break;
}
if(UCS2<0x80)
{
pUTF8[j++] = UCS2;
}
else if(UCS2<0x800)
{
pUTF8[j++] = (0xC0|(UCS2>>6));
pUTF8[j++] = (0x80|(UCS2&0x3F));
}
else if(UCS2<0x10000)
{
pUTF8[j++] = (0xE0|(UCS2>>12));
pUTF8[j++] = (0x80|((UCS2>>6)&0x3F));
pUTF8[j++] = (0x80|(UCS2&0x3F));
}
else if(UCS2<0x200000)
{
pUTF8[j++] = (0xF0|(UCS2>>18));
pUTF8[j++] = (0x80|((UCS2>>12)&0x3F));
pUTF8[j++] = (0x80|((UCS2>>6)&0x3F));
pUTF8[j++] = (0x80|(UCS2&0x3F));
}
else if(UCS2<0x4000000)
{
pUTF8[j++] = (0xF8|(UCS2>>24));
pUTF8[j++] = (0x80|((UCS2>>18)&0x3F));
pUTF8[j++] = (0x80|((UCS2>>12)&0x3F));
pUTF8[j++] = (0x80|((UCS2>>6)&0x3F));
break;
}
else
{
pUTF8[j++] = (0xFC|(UCS2>>30));
pUTF8[j++] = (0x80|((UCS2>>24)&0x3F));
pUTF8[j++] = (0x80|((UCS2>>18)&0x3F));
pUTF8[j++] = (0x80|((UCS2>>12)&0x3F));
break;
}
}
for(i=0; i<4 && pUTF8[i]; i++)
{
Result<<=8;
Result|=pUTF8[i];
}
for(; i<4; i++)
{
Result<<=8;
}
return Result;
}
int CompareFileName(WCHAR *FileName1, UINT*Hint1, WCHAR *FileName2, UINT *Hint2)
{
int j;
UINT u, v;
if(Hint1)
{
*Hint1 = FileUintHint(FileName1);
}
if(Hint2)
{
*Hint2 = FileUintHint(FileName2);
}
for(j=2; j<=RTF_MAX_PATH; j++)
{
u = fs_util_wchar_toupper(FileName1[j]);
v = fs_util_wchar_toupper(FileName2[j]);
if(u < v)
{
return -1;
}
else if(u != v)
{
return 1;
}
else if(u == 0)
{
return 0;
}
}
return 0;
}
static int CompareFileType(WCHAR *FileName1, WCHAR *FileName2, UINT Hint)
{
UINT TypeIdx = 0;
char * sPtr;
char * TmpsPtr;
UINT TmpDotIdx;
int j;
UINT CheckPoint;
int OffSet;
kal_bool ExtNameTieFlag = KAL_FALSE;
sPtr = kal_dchar_strrchr((char *)FileName1, 0x002e);
if(sPtr)
{
sPtr += 2;
TypeIdx = (sPtr - (char *)FileName1)/2;
}
if(TypeIdx == 0 || (Hint & 0x000000FF) == 0x00000000)
{ /* There is no extension name on both filename,
* or just singular WCHAR extension name on both filename,
* or the extension name are compared and tie to tie.
*/
SortTypeTie: ExtNameTieFlag = KAL_TRUE;
CheckPoint = 0;
OffSet = 0;
goto SortTypeContinue;
}
else
{
TmpsPtr = kal_dchar_strrchr((char *)FileName2, 0x002e);
TmpDotIdx = (TmpsPtr - (char *)FileName2)/2 + 1;
OffSet = TmpDotIdx - TypeIdx;
CheckPoint = TypeIdx + 2;
}
SortTypeContinue:
for(j=CheckPoint; j<=RTF_MAX_PATH; j++)
{
if((FileName1[j] == 0) && (FileName2[j+OffSet] == 0))
{
if(ExtNameTieFlag == KAL_FALSE)
{ goto SortTypeTie;}
return 0;
}
if((FileName2[j+OffSet] == 0x2E) || (fs_util_wchar_toupper(FileName1[j]) > fs_util_wchar_toupper(FileName2[j+OffSet])))
return 1;
else if((FileName1[j] == 0x2E) || fs_util_wchar_toupper(FileName1[j]) < fs_util_wchar_toupper(FileName2[j+OffSet]))
return -1;
}
return 0;
}
/* ------------------------------------------------------------------------------- */
/* This function is to create heap */
int CreateHeap(FS_SortingParam *Param, FSSortingInternalS *SortingData)
{
RTFHANDLE FHandle = 0;
RTFDirLocation *PosList;
UINT *HintList;
//UINT *HintList2;
RTFDirLocation *PosList_File;
UINT *HintList_File;
UINT *HintList2_File;
UINT MaxNameLength = 4;
UINT NameLength;
int index = 1;
int Count = 0;
int index_File = -1;
int Count_File = 0;
int Total_Count = 0;
int Result;
int Flag = Param->Flag;
int MaxCount = SortingData->MaxCount;
int s, p;
WCHAR * TmpName;
WCHAR * FileName = (WCHAR *)SortingData->FileName;
RTFDOSDirEntry *FileInfo = (RTFDOSDirEntry *)SortingData->FileInfo;
kal_uint32 LastPgsTime = 0;
#ifdef __FS_OPEN_HINT__
/*
* If Param->Pattern is end with '\\', e.g., X:\\A\\, then we will find out "A", not files
* inside folder "A". This will let FS_XFindStart->MTHintNewManual add wrong hint path.
*
* Thus we disable hint operation in this case.
*/
for (TmpName = Param->Pattern; *TmpName != '\0'; TmpName++);
if (*(TmpName - 1) == '\\')
SortingData->Flag |= FS_SORTING_IS_FLAG_HINT_DISABLED;
#endif
TmpName = (WCHAR *)SortingData->TmpName1;
PosList = SortingData->PosList;
HintList = SortingData->HintList;
//HintList2 = SortingData->HintList2;
PosList_File = SortingData->PosList_File;
HintList_File = SortingData->HintList_File;
HintList2_File = SortingData->HintList2_File;
if(Param->ProgressCallback)
{
SortingData->Status = FS_SORT_PGS_PREPARE;
if(Param->ProgressCallback(FS_SORT_PGS_PREPARE, 0, 0, Param->ProgressCallbackParam)<0)
{
return MT_ABORTED_ERROR;
}
}
/* 1. Load all the directory entry to create heap */
if(Flag & FS_DIR_TYPE)
FHandle = FindFirst((WCHAR *)Param->Pattern, Param->PatternArray, Param->PatternNum, Param->ArrayMask, 0, Param->AttrMask, FileInfo, (WCHAR *)FileName, MT_MAX_WIDE_NAME, PosList_File + index_File, &(SortingData->DirCluster));
else
FHandle = FindFirst((WCHAR *)Param->Pattern, Param->PatternArray, Param->PatternNum, Param->ArrayMask, 0, Param->AttrMask|RTF_ATTR_DIR, FileInfo, (WCHAR *)FileName, MT_MAX_WIDE_NAME, PosList_File + index_File, &(SortingData->DirCluster));
if(FHandle >= RTF_NO_ERROR)
{
if(!WFNamesMatch((char *)FileName, (char *)dchar_dot))
{//Don't worry about dchar_dot_dot here
NameLength = kal_dchar_strlen((char*)FileName);
MaxNameLength = MaxNameLength>NameLength? MaxNameLength: NameLength;
if((FileInfo->Attributes & RTF_ATTR_DIR) && (Flag & FS_DIR_TYPE))
{//----------------------------- Sort Folder ------------------------------------
// Setup default hint information to the new file.
PosList[index].Cluster = PosList_File[index_File].Cluster;
PosList[index].Index = PosList_File[index_File].Index;
if(Flag & (FS_SORT_USER|FS_SORT_NAME|FS_SORT_TYPE|FS_SORT_SIZE))
{
Param->CompareFunc(FileName, (UINT*)(HintList+index), (WCHAR*)L"", NULL);
}
else if(Flag & FS_SORT_ATTR)
{
HintList[index] = FileInfo->Attributes;
}
else if(Flag & FS_SORT_TIME)
{
HintList[index] = 0xFFFFFFFF - ((FileInfo->DateTime.Year1980*12*31*24*60*60) +
(FileInfo->DateTime.Month*31*24*60*60) +
(FileInfo->DateTime.Day*24*60*60) +
(FileInfo->DateTime.Hour*60*60) +
(FileInfo->DateTime.Minute*60) +
(FileInfo->DateTime.Second2));
}
else if(Flag & FS_NO_SORT)
{
}
else
{
Result = RTF_PARAM_ERROR;
goto SortReturn;
}
Total_Count ++;
Count++;
index++;
}
else if(!(FileInfo->Attributes & RTF_ATTR_DIR) && (Flag & FS_FILE_TYPE))
{//----------------------------- Sort File ------------------------------------
if(Flag & (FS_SORT_USER|FS_SORT_NAME))
{
Param->CompareFunc(FileName, (UINT*)(HintList_File+index_File), (WCHAR*)L"", NULL);
}
else if(Flag & FS_SORT_TYPE)
{
char * sPtr;
sPtr = kal_dchar_strrchr((char *)FileName, 0x002e);
if(sPtr)
{
HintList_File[index_File] = FileUintHint((WCHAR*)(sPtr+2));
}
HintList2_File[index_File] = (fs_util_wchar_toupper(FileName[0])<<16) + fs_util_wchar_toupper(FileName[1]);
}
else if(Flag & FS_SORT_ATTR)
{
HintList_File[index_File] = FileInfo->Attributes;
}
else if(Flag & FS_SORT_SIZE)
{
HintList_File[index_File] = FileInfo->FileSize;
}
else if(Flag & FS_SORT_TIME)
{
HintList_File[index_File] = 0xFFFFFFFF - ((FileInfo->DateTime.Year1980*12*31*24*60*60) +
(FileInfo->DateTime.Month*31*24*60*60) +
(FileInfo->DateTime.Day*24*60*60) +
(FileInfo->DateTime.Hour*60*60) +
(FileInfo->DateTime.Minute*60) +
(FileInfo->DateTime.Second2));
}
else if(Flag & FS_NO_SORT)
{
}
else
{
Result = RTF_PARAM_ERROR;
goto SortReturn;
}
Total_Count ++;
Count_File--;
index_File--;
}
}
while((Result = fs_srv_findnext(FHandle, Param->PatternArray, Param->PatternNum, Param->ArrayMask, (FS_DOSDirEntry*)FileInfo, (WCHAR *)FileName, MT_MAX_WIDE_NAME, FS_FIND_DEFAULT, (FS_FileLocationHint*)(PosList+index) )) == RTF_NO_ERROR)
{
/* Check Progress */
if (LastPgsTime != (GetTime() & MT_PGS_PERIOD_MASK))
{
LastPgsTime = GetTime() & MT_PGS_PERIOD_MASK;
if(Param->ProgressCallback && Param->ProgressCallback(FS_SORT_PGS_PREPARE, 0, 0, Param->ProgressCallbackParam)<0)
{
Result = MT_ABORTED_ERROR;
goto SortErrorReturn;
}
}
if(Result >= RTF_NO_ERROR)
{
if(!WFNamesMatch((char *)FileName, (char *)dchar_dot_dot))
{//Don't worry about dchar_dot here
NameLength = kal_dchar_strlen((char*)FileName);
MaxNameLength = MaxNameLength>NameLength? MaxNameLength: NameLength;
if((FileInfo->Attributes & RTF_ATTR_DIR) && (Flag & FS_DIR_TYPE))
{
// Setup default hint information to the new file.
if(Flag & (FS_SORT_USER|FS_SORT_NAME|FS_SORT_TYPE|FS_SORT_SIZE))
{
Param->CompareFunc(FileName, (UINT*)(HintList+index), (WCHAR*)L"", NULL);
}
else if(Flag & FS_SORT_ATTR)
{
HintList[index] = FileInfo->Attributes;
}
else if(Flag & FS_SORT_TIME)
{
HintList[index] = 0xFFFFFFFF - ((FileInfo->DateTime.Year1980*12*31*24*60*60) +
(FileInfo->DateTime.Month*31*24*60*60) +
(FileInfo->DateTime.Day*24*60*60) +
(FileInfo->DateTime.Hour*60*60) +
(FileInfo->DateTime.Minute*60) +
(FileInfo->DateTime.Second2));
}
else if(Flag & FS_NO_SORT)
{
}
else
{
Result = RTF_PARAM_ERROR;
goto SortReturn;
}
Total_Count ++;
Count++;
index++;
s = Count;
p = Count>>1;
while(ABS(s)>=2)
{
// Check hint first
if(HintList[p]<HintList[s])
{
break;
}
else if(HintList[p]==HintList[s])
{
if(Flag & (FS_SORT_NAME|FS_SORT_USER|FS_SORT_TYPE|FS_SORT_SIZE))
{
//Result = GetFindByPos(Param->Pattern, FileInfo, TmpName, MT_MAX_FILE_NUM, &PosList[p], FS_FIND_DEFAULT);
Result = fs_srv_get_name_by_pos(NULL, Param->Pattern, FileInfo, TmpName, MT_MAX_WIDE_PATH, &PosList[p], FS_FIND_DEFAULT);
if(Result < RTF_NO_ERROR)
goto SortErrorReturn;
if(Param->CompareFunc(FileName, NULL, TmpName, NULL)>=0)
{
goto NextFile;
}
}
else
{
goto NextFile;
}
}
// Swap the hint information
SWAP_SORTING_ENTRY(p, s);
s = p;
p = s/2;
}
}
else if(!(FileInfo->Attributes & RTF_ATTR_DIR) && (Flag & FS_FILE_TYPE))
{
PosList_File[index_File].Cluster = PosList[index].Cluster;
PosList_File[index_File].Index = PosList[index].Index;
if(Flag & (FS_SORT_USER|FS_SORT_NAME))
{
Param->CompareFunc(FileName, (UINT*)(HintList_File+index_File), (WCHAR*)L"", NULL);
}
else if(Flag & FS_SORT_TYPE)
{
char * sPtr;
sPtr = kal_dchar_strrchr((char *)FileName, 0x002e);
if(sPtr)
{
HintList_File[index_File] = FileUintHint((WCHAR*)(sPtr+2));
}
else
{
HintList_File[index_File] = 0;
}
HintList2_File[index_File] = (fs_util_wchar_toupper(FileName[0])<<16) + fs_util_wchar_toupper(FileName[1]);
}
else if(Flag & FS_SORT_ATTR)
{
HintList_File[index_File] = FileInfo->Attributes;
}
else if(Flag & FS_SORT_SIZE)
{
HintList_File[index_File] = FileInfo->FileSize;
}
else if(Flag & FS_SORT_TIME)
{
HintList_File[index_File] = 0xFFFFFFFF - ((FileInfo->DateTime.Year1980*12*31*24*60*60) +
(FileInfo->DateTime.Month*31*24*60*60) +
(FileInfo->DateTime.Day*24*60*60) +
(FileInfo->DateTime.Hour*60*60) +
(FileInfo->DateTime.Minute*60) +
(FileInfo->DateTime.Second2));
}
else if(Flag & FS_NO_SORT)
{
}
else
{
Result = RTF_PARAM_ERROR;
goto SortErrorReturn;
}
Total_Count ++;
index_File--;
Count_File--;
s = Count_File;
p = Count_File/2;
while(ABS(s)>=2)
{
// Check hint first
if(HintList_File[p]<HintList_File[s])
{
break;
}
else if(HintList_File[p]==HintList_File[s])
{
if(Flag & (FS_SORT_NAME|FS_SORT_USER|FS_SORT_TIME))
{
//Result = GetFindByPos(Param->Pattern, FileInfo, TmpName, MT_MAX_FILE_NUM, &PosList_File[p], FS_FIND_DEFAULT);
Result = fs_srv_get_name_by_pos(NULL, Param->Pattern, FileInfo, TmpName, MT_MAX_WIDE_PATH, &PosList_File[p], FS_FIND_DEFAULT);
if(Result < RTF_NO_ERROR)
goto SortErrorReturn;
if(Param->CompareFunc(FileName, NULL, TmpName, NULL)>=0)
{
goto NextFile;
}
}
else if(Flag & FS_SORT_TYPE)
{
if((HintList_File[p]&0x000000FF)!=0 || HintList2_File[p]==HintList2_File[s])
{
//Result = GetFindByPos(Param->Pattern, FileInfo, TmpName, MT_MAX_FILE_NUM, &PosList_File[p], FS_FIND_DEFAULT);
Result = fs_srv_get_name_by_pos(NULL, Param->Pattern, FileInfo, TmpName, MT_MAX_WIDE_PATH, &PosList_File[p], FS_FIND_DEFAULT);
if(Result < RTF_NO_ERROR)
goto SortReturn;
if(CompareFileType(FileName, TmpName, HintList_File[s])>=0)
{
goto NextFile;
}
}
else if(HintList2_File[p]<HintList2_File[s])
{
break;
}
}
else
{
goto NextFile;
}
}
// Swap the hint information
SWAP_SORTING_ENTRY_FILE(p, s);
if(Flag & FS_SORT_TYPE)
{
SWAP(HintList2_File[p], HintList2_File[s]);
}
s = p;
p = s/2;
}
}
}
}
else
{
break;
}
NextFile:
if(Total_Count>MaxCount)
{
Result = RTF_TOO_MANY_FILES;
goto SortErrorReturn;
}
}
}
else
{
Result = FHandle;
}
SortReturn:
if((Result!=RTF_NO_MORE_FILES) && (Result<RTF_NO_ERROR))
{
goto SortErrorReturn;
}
if(Flag & FS_NO_SORT)
{
// SWAP folder
for(s=1, p=Count; s<p; s++, p--)
{
SWAP(PosList[s].Index, PosList[p].Index);
SWAP(PosList[s].Cluster, PosList[p].Cluster);
}
// SWAP file
for(s=-1, p=Count_File; p<s; s--, p++)
{
SWAP(PosList_File[s].Index, PosList_File[p].Index);
SWAP(PosList_File[s].Cluster, PosList_File[p].Cluster);
}
}
SortingData->FileCount = Count_File;
SortingData->FolderCount = Count;
SortingData->TotalCount = Total_Count;
MaxNameLength = (MaxNameLength+7)&0xFFFFFFFC;
MaxNameLength = MaxNameLength>MT_MAX_WIDE_PATH?MT_MAX_WIDE_PATH:MaxNameLength;
SortingData->MaxFileNameLength = MaxNameLength;
if(FHandle > 0) RTFClose(FHandle);
return Total_Count;
SortErrorReturn:
SortingData->FileCount = 0;
SortingData->FolderCount = 0;
SortingData->TotalCount = 0;
SortingData->ReadyCount = 0;
if(FHandle > 0) RTFClose(FHandle);
return Result;
}
int CacheGetFindByPos(const WCHAR * Pattern, RTFDOSDirEntry * FileInfo, WCHAR * FileName,
UINT MaxLength, RTFDirLocation * Pos, UINT Flag, WCHAR ** CachedName, char **FreeCacheList)
{
int Result;
if(Pos->Index&0xFFFF0000)
{ // Cache Hit
*CachedName = ((WCHAR*)Pos->Cluster)+2;
return RTF_NO_ERROR;
}
// Check if free cache space is available
if(*FreeCacheList)
{
*CachedName = (WCHAR*)*FreeCacheList;
*FreeCacheList = *((char**)(*FreeCacheList));
// Save the original Cluster value
*(int*)*CachedName = Pos->Cluster;
*CachedName += 2;
//Result = GetFindByPos(Pattern, FileInfo, *CachedName, MaxLength, Pos, Flag);
Result = fs_srv_get_name_by_pos(NULL, Pattern, FileInfo, *CachedName, MaxLength, Pos, Flag);
Pos->Cluster = (int)(*CachedName - 2);
Pos->Index |= 0x00010000;
return Result;
}
*CachedName = FileName;
//Result = GetFindByPos(Pattern, FileInfo, *CachedName, MaxLength, Pos, Flag);
Result = fs_srv_get_name_by_pos(NULL, Pattern, FileInfo, *CachedName, MaxLength, Pos, Flag);
return Result;
}
int HeapSort(FS_SortingParam *Param, FSSortingInternalS *SortingData)
{
RTFDirLocation *PosList;
UINT *HintList;
UINT *HintList2;
RTFDirLocation *PosList_File;
UINT *HintList_File;
UINT *HintList2_File;
int m, p, s;
int Result = RTF_NO_ERROR;
int Flag = Param->Flag;
int Progress = 0;
int CacheCount = 0;
int MaxFileNameLength = SortingData->MaxFileNameLength;
kal_uint32 LastPgsTime = 0;
WCHAR * TmpName1 = (WCHAR *)SortingData->TmpName1;
WCHAR * TmpName2 = (WCHAR *)SortingData->TmpName2;
WCHAR * FileName = (WCHAR *)SortingData->FileName;
WCHAR * CachedTmpName1;
WCHAR * CachedTmpName2;
WCHAR * CachedFileName;
/* Create Free Cache List */
MaxFileNameLength += 4;
/* Gather the PosList space */
PosList = (SortingData->PosList + SortingData->FolderCount + 1);
PosList_File = (SortingData->PosList_File + SortingData->FileCount - (MaxFileNameLength/4) + 1);
for(; PosList<PosList_File; PosList = (RTFDirLocation*)((char*)PosList+MaxFileNameLength))
{
*((char**)PosList) = SortingData->FreeCacheList;
SortingData->FreeCacheList = (char*)PosList;
}
/* Gather the HintList space */
HintList = (SortingData->HintList + SortingData->FolderCount + 1);
HintList_File = (SortingData->HintList_File + SortingData->FileCount - (MaxFileNameLength/4) + 1);
for(; HintList<HintList_File; HintList = (UINT*)((char*)HintList+MaxFileNameLength))
{
*((char**)HintList) = SortingData->FreeCacheList;
SortingData->FreeCacheList = (char*)HintList;
}
/* Gather the HintList2 space */
if(Flag&FS_SORT_TYPE)
{
HintList2 = (SortingData->HintList2 + 1);
HintList2_File = (SortingData->HintList2_File + SortingData->FileCount - (MaxFileNameLength/4) + 1);
}
else
{
HintList2 = SortingData->HintList2 + 1;
HintList2_File = SortingData->HintList2_File - (MaxFileNameLength/4) + 1;
}
for(; HintList2<HintList2_File; HintList2 = (UINT*)((char*)HintList2+MaxFileNameLength))
{
*((char**)HintList2) = SortingData->FreeCacheList;
SortingData->FreeCacheList = (char*)HintList2;
}
MaxFileNameLength -= 4;
/* Setup Internal Data */
PosList = SortingData->PosList;
HintList = SortingData->HintList;
HintList2 = SortingData->HintList2;
PosList_File = SortingData->PosList_File;
HintList_File = SortingData->HintList_File;
HintList2_File = SortingData->HintList2_File;
if(Param->ProgressCallback)
{
if(Param->ProgressCallback(FS_SORT_PGS_START, SortingData->TotalCount, 0, Param->ProgressCallbackParam)<0)
{
return MT_ABORTED_ERROR;
}
}
SortingData->Status = FS_SORT_PGS_ING;
m = SortingData->FolderCount;
NextFolder:
while(m > 1)
{
SWAP(PosList[1].Cluster, PosList[m].Cluster);
SWAP(PosList[1].Index, PosList[m].Index);
HintList[1] = HintList[m];
if(PosList[m].Index&0xFFFF0000)
{
PosList[m].Index &= 0x0000FFFF;
p = *((int*)PosList[m].Cluster);
*((char**)PosList[m].Cluster) = SortingData->FreeCacheList;
SortingData->FreeCacheList = (char*)PosList[m].Cluster;
PosList[m].Cluster = p;
}
CacheCount+=4;
if(CacheCount>MaxFileNameLength)
{
CacheCount = m<<2;
*((char**)(((char*)HintList)+CacheCount)) = SortingData->FreeCacheList;
SortingData->FreeCacheList = ((char*)HintList)+CacheCount;
CacheCount = 0;
}
m--;
p = 1;
s = p<<1;
/* Check Progress */
if (LastPgsTime != (GetTime() & MT_PGS_PERIOD_MASK))
{
LastPgsTime = GetTime() & MT_PGS_PERIOD_MASK;
SortingData->ReadyCount = Progress;
if(Param->ProgressCallback && Param->ProgressCallback(FS_SORT_PGS_ING, SortingData->TotalCount, Progress, Param->ProgressCallbackParam)<0)
{
return MT_ABORTED_ERROR;
}
}
FileName[0] = 0;
while(s <= m)
{
TmpName1[0] = TmpName2[0] = 0;
if(s < m)
{
if(HintList[s+1] < HintList[s])
{
s++;
}
else if(HintList[s+1] == HintList[s])
{
if(Flag & (FS_SORT_NAME|FS_SORT_USER|FS_SORT_TYPE|FS_SORT_SIZE))
{
Result = CacheGetFindByPos(Param->Pattern, NULL, TmpName1, MaxFileNameLength, &PosList[s], FS_FIND_DEFAULT, &CachedTmpName1, &SortingData->FreeCacheList);
if(Result < RTF_NO_ERROR)
goto SortReturn;
Result = CacheGetFindByPos(Param->Pattern, NULL, TmpName2, MaxFileNameLength, &PosList[s+1], FS_FIND_DEFAULT, &CachedTmpName2, &SortingData->FreeCacheList);
if(Result < RTF_NO_ERROR)
goto SortReturn;
if(Param->CompareFunc(CachedTmpName1, NULL, CachedTmpName2, NULL)>0)
{
s++;
}
}
}
}
if(HintList[p] < HintList[s])
{
break;
}
else if(HintList[p] == HintList[s]) // parent/child primary keys are the same
{
if(FileName[0]==0)
{
Result = CacheGetFindByPos(Param->Pattern, NULL, FileName, MaxFileNameLength, &PosList[p], FS_FIND_DEFAULT, &CachedFileName, &SortingData->FreeCacheList);
if(Result < RTF_NO_ERROR)
goto SortReturn;
}
if(s&0x00000001)
{
if(TmpName2[0]==0)
{
Result = CacheGetFindByPos(Param->Pattern, NULL, TmpName2, MaxFileNameLength, &PosList[s], FS_FIND_DEFAULT, &CachedTmpName2, &SortingData->FreeCacheList);
if(Result < RTF_NO_ERROR)
goto SortReturn;
}
if(Flag & (FS_SORT_NAME|FS_SORT_USER|FS_SORT_SIZE|FS_SORT_TYPE))
{
if(Param->CompareFunc(CachedFileName, NULL, CachedTmpName2, NULL)<=0)
{
goto NextFolder;
}
}
else
{
goto NextFolder;
}
}
else
{
if(TmpName1[0]==0)
{
Result = CacheGetFindByPos(Param->Pattern, NULL, TmpName1, MaxFileNameLength, &PosList[s], FS_FIND_DEFAULT, &CachedTmpName1, &SortingData->FreeCacheList);
if(Result < RTF_NO_ERROR)
goto SortReturn;
}
if(Flag & (FS_SORT_NAME|FS_SORT_USER|FS_SORT_SIZE|FS_SORT_TYPE))
{
if(Param->CompareFunc(CachedFileName, NULL, CachedTmpName1, NULL)<=0)
{
goto NextFolder;
}
}
else
{
goto NextFolder;
}
}
}
SWAP_SORTING_ENTRY(p, s);
p = s;
s = p<<1;
}
Progress ++;
}
if(m==1 && PosList[1].Index&0xFFFF0000)
{
PosList[1].Index &= 0x0000FFFF;
p = *((int*)PosList[1].Cluster);
*((char**)PosList[1].Cluster) = SortingData->FreeCacheList;
SortingData->FreeCacheList = (char*)PosList[1].Cluster;
PosList[1].Cluster = p;
}
CacheCount = 0;
m = SortingData->FileCount;
NextFile:
while(m < -1)
{
SWAP(PosList_File[-1].Cluster ,PosList_File[m].Cluster);
SWAP(PosList_File[-1].Index ,PosList_File[m].Index);
HintList_File[-1] = HintList_File[m];
HintList2_File[-1] = HintList2_File[m];
if(PosList_File[m].Index&0xFFFF0000)
{
PosList_File[m].Index &= 0x0000FFFF;
p = *(int*)PosList_File[m].Cluster;
*((char**)PosList_File[m].Cluster) = SortingData->FreeCacheList;
SortingData->FreeCacheList = (char*)PosList_File[m].Cluster;
PosList_File[m].Cluster = p;
}
CacheCount+=4;
if(CacheCount>MaxFileNameLength)
{
CacheCount = (m<<2) - CacheCount + 4;
*((char**)(((char*)HintList_File)+CacheCount)) = SortingData->FreeCacheList;
if(Flag & FS_SORT_TYPE)
{
*((char**)(((char*)HintList2_File)+CacheCount)) = ((char*)HintList_File)+CacheCount;
SortingData->FreeCacheList = ((char*)HintList2_File)+CacheCount;
}
else
{
SortingData->FreeCacheList = ((char*)HintList_File)+CacheCount;
}
CacheCount = 0;
}
m++;
p = -1;
s = p<<1;
/* Check Progress */
if (LastPgsTime != (GetTime() & MT_PGS_PERIOD_MASK))
{
LastPgsTime = GetTime() & MT_PGS_PERIOD_MASK;
SortingData->ReadyCount = Progress;
if(Param->ProgressCallback && Param->ProgressCallback(FS_SORT_PGS_ING, SortingData->TotalCount, Progress, Param->ProgressCallbackParam)<0)
{
return MT_ABORTED_ERROR;
}
}
FileName[0] = 0;
while(s >= m)
{
TmpName1[0] = TmpName2[0] = 0;
if(s > m)
{
if(HintList_File[s-1] < HintList_File[s])
{
s--;
}
else if(HintList_File[s-1] == HintList_File[s])
{
// primary key are the same, compare file name
if(Flag & (FS_SORT_NAME|FS_SORT_USER|FS_SORT_TIME))
{
Result = CacheGetFindByPos(Param->Pattern, NULL, TmpName1, MaxFileNameLength, &PosList_File[s], FS_FIND_DEFAULT, &CachedTmpName1, &SortingData->FreeCacheList);
if(Result < RTF_NO_ERROR)
goto SortReturn;
Result = CacheGetFindByPos(Param->Pattern, NULL, TmpName2, MaxFileNameLength, &PosList_File[s-1], FS_FIND_DEFAULT, &CachedTmpName2, &SortingData->FreeCacheList);
if(Result < RTF_NO_ERROR)
goto SortReturn;
if(Param->CompareFunc(CachedTmpName1, NULL, CachedTmpName2, NULL)>0)
{
s--;
}
}
else if(Flag & FS_SORT_TYPE)
{
if((HintList_File[s]&0x000000FF)!=0 || HintList2_File[s-1] == HintList2_File[s])
{
Result = CacheGetFindByPos(Param->Pattern, NULL, TmpName1, MaxFileNameLength, &PosList_File[s], FS_FIND_DEFAULT, &CachedTmpName1, &SortingData->FreeCacheList);
if(Result < RTF_NO_ERROR)
goto SortReturn;
Result = CacheGetFindByPos(Param->Pattern, NULL, TmpName2, MaxFileNameLength, &PosList_File[s-1], FS_FIND_DEFAULT, &CachedTmpName2, &SortingData->FreeCacheList);
if(Result < RTF_NO_ERROR)
goto SortReturn;
if(CompareFileType(CachedTmpName1, CachedTmpName2, HintList_File[s])>0)
{
s--;
}
}
else if(HintList2_File[s-1] < HintList2_File[s])
{
s--;
}
}
}
}
if(HintList_File[p] < HintList_File[s])
{
break;
}
// primary key are the same, compare file name
else if(HintList_File[p] == HintList_File[s])
{
if ((Flag & FS_SORT_TYPE)&&((HintList_File[p] & 0x000000FF)==0))
{
if(HintList2_File[p] < HintList2_File[s])
{
break;
}
else if(HintList2_File[p] > HintList2_File[p])
{
SWAP(HintList2_File[p], HintList2_File[s]);
goto Do_Swap_File;
}
}
// sort by size and attribute will not use file name as secondary key
else if(Flag & (FS_SORT_SIZE|FS_SORT_ATTR))
{
break;
}
if(FileName[0]==0)
{
Result = CacheGetFindByPos(Param->Pattern, NULL, FileName, MaxFileNameLength, &PosList_File[p], FS_FIND_DEFAULT, &CachedFileName, &SortingData->FreeCacheList);
if(Result < RTF_NO_ERROR)
goto SortReturn;
}
if(s&0x00000001) // right child node
{
if(Flag & (FS_SORT_NAME|FS_SORT_USER|FS_SORT_TIME))
{
if(TmpName2[0]==0)
{
Result = CacheGetFindByPos(Param->Pattern, NULL, TmpName2, MaxFileNameLength, &PosList_File[s], FS_FIND_DEFAULT, &CachedTmpName2, &SortingData->FreeCacheList);
if(Result < RTF_NO_ERROR)
goto SortReturn;
}
if(Param->CompareFunc(CachedFileName, NULL, CachedTmpName2, NULL)<=0)
{
goto NextFile;
}
}
else if(Flag & FS_SORT_TYPE)
{
if(TmpName2[0]==0)
{
Result = CacheGetFindByPos(Param->Pattern, NULL, TmpName2, MaxFileNameLength, &PosList_File[s], FS_FIND_DEFAULT, &CachedTmpName2, &SortingData->FreeCacheList);
if(Result < RTF_NO_ERROR)
goto SortReturn;
}
if(CompareFileType(CachedFileName, CachedTmpName2, HintList_File[s])<=0)
{
goto NextFile;
}
SWAP(HintList2_File[p], HintList2_File[s]);
}
else
{
goto NextFile;
}
}
else // left child node
{
if(Flag & (FS_SORT_NAME|FS_SORT_USER|FS_SORT_TIME))
{
if(TmpName1[0]==0)
{
Result = CacheGetFindByPos(Param->Pattern, NULL, TmpName1, MaxFileNameLength, &PosList_File[s], FS_FIND_DEFAULT, &CachedTmpName1, &SortingData->FreeCacheList);
if(Result < RTF_NO_ERROR)
goto SortReturn;
}
if(Param->CompareFunc(CachedFileName, NULL, CachedTmpName1, NULL)<=0)
{
goto NextFile;
}
}
else if(Flag & FS_SORT_TYPE)
{
if(TmpName1[0]==0)
{
Result = CacheGetFindByPos(Param->Pattern, NULL, TmpName1, MaxFileNameLength, &PosList_File[s], FS_FIND_DEFAULT, &CachedTmpName1, &SortingData->FreeCacheList);
if(Result < RTF_NO_ERROR)
goto SortReturn;
}
if(CompareFileType(CachedFileName, CachedTmpName1, HintList_File[s])<=0)
{
goto NextFile;
}
SWAP(HintList2_File[p], HintList2_File[s]);
}
else
{
goto NextFile;
}
}
}
else
{
if(Flag & FS_SORT_TYPE)
{
SWAP(HintList2_File[p], HintList2_File[s]);
}
}
Do_Swap_File:
SWAP_SORTING_ENTRY_FILE(p, s);
p = s;
s = p<<1;
}
Progress ++;
}
if(m==-1 && PosList_File[-1].Index&0xFFFF0000)
{
PosList_File[-1].Index &= 0x0000FFFF;
PosList_File[-1].Cluster = *(int*)PosList_File[-1].Cluster;
}
SortReturn:
SortingData->ReadyCount = SortingData->TotalCount;
return Result;
}
#endif
#ifdef __AUDIO_DSP_LOWPOWER__
#define FS_PMAPQUERY_MAX_CLUSTER_COUNT (16)
int MTQueryPhysicalMap(FS_PMapInfo* PMapInfo)
{
kal_uint32 dev_type;
kal_uint32 fpos_ori = 0xFFFFFFFF;
kal_uint32 sec_cur, sec_cnt;
kal_uint32 cluster_cur, cluster_next;
kal_uint16 volatile valid_entry_cnt;
kal_uint16 total_entry_cnt;
kal_int32 volatile result = RTF_NO_ERROR;
RTFile * volatile f = NULL;
FS_NANDPMapQuery NANDQueryData;
FS_CardPMapEntry *pCardEntry;
XTRY
case XCODE:
f = ParseFileHandle(PMapInfo->FH);
// check parameters
if (NormalFile != f->SpecialKind)
{
XRAISE(RTF_PARAM_ERROR);
}
// check start file offset
if (PMapInfo->Offset & 0x1FF)
{
XRAISE(RTF_PARAM_ERROR);
}
// check supported device type
dev_type = fs_conf_get_devtype_by_devflag(f->Dev->DeviceFlags);
if ((dev_type != FS_DEVICE_TYPE_NAND) && (dev_type != FS_DEVICE_TYPE_CARD))
{
XRAISE(RTF_PARAM_ERROR);
}
else
{
PMapInfo->DevType = dev_type;
}
// buffer should be 4-byte aligned
if ((PMapInfo->pBuf == NULL) || ((UINT)(PMapInfo->pBuf) & 0x3 != 0))
{
XRAISE(MT_FAIL_GET_MEM);
}
// keep original file pointer
fpos_ori = f->FilePointer;
// go to start point of map transformation
result = RTFSeek(PMapInfo->FH, PMapInfo->Offset, RTF_FILE_BEGIN);
if (result < RTF_NO_ERROR) XRAISE(result);
// get free start address of map entries and entry count
if (FS_DEVICE_TYPE_NAND == dev_type)
{
total_entry_cnt = (PMapInfo->BufSize - sizeof(FS_NANDPMapHeader)) / sizeof(FS_NANDPMapEntry);
// initialize query data for NAND
NANDQueryData.pBuf = (FS_NANDPMapEntry*)(PMapInfo->pBuf + sizeof(FS_NANDPMapHeader));
NANDQueryData.BufSize = total_entry_cnt * sizeof(FS_NANDPMapEntry);
}
else // FS_DEVICE_TYPE_CARD
{
pCardEntry = (FS_CardPMapEntry*)PMapInfo->pBuf;
total_entry_cnt = PMapInfo->BufSize / sizeof(FS_CardPMapEntry);
}
if (0 == f->Cluster)
{
XRAISE(RTF_INVALID_FILE_POS);
}
sec_cur = CLUSTER_TO_SECTOR_OFS(f->Drive, f->Cluster, f->Offset);
// handle f->Cluster
sec_cnt = f->Drive->ClusterSize - f->Offset;
// sec_cnt = ((sec_cnt - 1) / f->Dev->DevData.SectorSize) + 1; // DIV SLIM
sec_cnt = ((sec_cnt - 1) >> f->Dev->DevData.SectorShift) + 1;
// now cluster_cur is handled, enter while-loop to handle next cluster
cluster_cur = f->Cluster;
valid_entry_cnt = 0;
// do until buffer is full (handle "cluster_next")
while (valid_entry_cnt < total_entry_cnt)
{
cluster_next = GetClusterValue(f->Drive, cluster_cur, 0);
// chain is fragmented or terminated
if ((cluster_next != cluster_cur + 1) || (sec_cnt >= FS_PMAPQUERY_MAX_CLUSTER_COUNT))
{
if (FS_DEVICE_TYPE_NAND == dev_type)
{
// prepare query data for NAND driver
NANDQueryData.LSN = sec_cur;
NANDQueryData.LSCnt = sec_cnt;
NANDQueryData.ValidEntryCnt = 0;
NANDQueryData.XferCnt = 0;
NANDQueryData.ChipSel = 0;
result = f->Dev->Driver->IOCtrl(f->Dev->DriverData, FS_IOCTRL_QUERY_PHYSICAL_MAP, (void*)&NANDQueryData);
if (result < RTF_NO_ERROR) XRAISE(result);
// update buffer information
NANDQueryData.BufSize -= (NANDQueryData.ValidEntryCnt * sizeof(FS_NANDPMapEntry));
NANDQueryData.pBuf += NANDQueryData.ValidEntryCnt;
valid_entry_cnt += NANDQueryData.ValidEntryCnt;
}
else // FS_DEVICE_TYPE_CARD
{
pCardEntry->SN = sec_cur;
pCardEntry->SecCnt = sec_cnt;
valid_entry_cnt++;
pCardEntry++;
}
if (cluster_next >= RTF_CHAIN_END_MARK) // this is the last cluster, we are done
break;
else
{
// get start sector no. of next continuous chain
sec_cur = CLUSTER_TO_SECTOR_OFS(f->Drive, cluster_next, 0);
// sec_cnt = (f->Drive->ClusterSize / f->Dev->DevData.SectorSize); // DIV SLIM
sec_cnt = (f->Drive->ClusterSize >> f->Dev->DevData.SectorShfit);
}
}
else // cluster_next == cluster_cur + 1
{
// sec_cnt += (f->Drive->ClusterSize / f->Dev->DevData.SectorSize); // DIV SLIM
sec_cnt += (f->Drive->ClusterSize >> f->Dev->DevData.SectorShfit);
}
cluster_cur = cluster_next;
}
break;
default:
result = XVALUE;
XHANDLED;
break;
case XFINALLY:
if (result >= RTF_NO_ERROR)
{
PMapInfo->ValidEntryCnt = valid_entry_cnt;
// update ChipSel for NAND device
if (FS_DEVICE_TYPE_NAND == dev_type)
{
((FS_NANDPMap*)(PMapInfo->pBuf))->Header.ChipSel = NANDQueryData.ChipSel;
}
result = RTF_NO_ERROR;
}
// restore file pointer
if (fpos_ori != 0xFFFFFFFF)
{
RTFSeek(PMapInfo->FH, fpos_ori, RTF_FILE_BEGIN);
}
if (f != NULL) UnlockDevice(f->Dev);
XENDX
return result;
}
#endif // __AUDIO_DSP_LOWPOWER__