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
|
/*
* Copyright (c) 2006-2025 RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2023-05-05 Bernard Implement file APIs in dfs v2.0
*/
#include "errno.h"
#include "fcntl.h"
#include <dfs.h>
#include "dfs_file.h"
#include "dfs_dentry.h"
#include "dfs_fs.h"
#include "dfs_mnt.h"
#include "dfs_private.h"
#ifdef RT_USING_PAGECACHE
#include "dfs_pcache.h"
#endif
#define DBG_TAG "DFS.file"
#define DBG_LVL DBG_WARNING
#include <rtdbg.h>
#define MAX_RW_COUNT 0xfffc0000
/**
* @brief Get the length of the first path component
*
* This function calculates the length of the first path component in a given path string.
* For absolute paths (starting with '/'), it returns the length from the first '/' to
* the next '/' or end of string. For relative paths, it returns 0.
*
* @param[in] path The input path string to analyze
*
* @return int The length of the first path component, or 0 if not found
*/
rt_inline int _first_path_len(const char *path)
{
int i = 0;
if (path[i] == '/')
{
i++;
while (path[i] != '\0' && path[i] != '/')
{
i++;
}
}
return i;
}
/**
* @brief Get the parent directory path from a given full path
*
* This function extracts the parent directory path from a given full path string.
* It handles paths ending with '/' correctly by skipping the trailing slash.
*
* @param[in] fullpath The input full path string to analyze
* @param[out] path Buffer to store the extracted parent directory path
*
* @return int Length of the parent directory path, or 0 if no parent found
*/
static int _get_parent_path(const char *fullpath, char *path)
{
int len = 0;
char *str = 0;
char *full_path = rt_strdup(fullpath);
if (full_path == NULL)
{
rt_set_errno(ENOMEM);
return -1;
}
str = strrchr(full_path, '/');
/* skip last '/' */
if (str && *(str + 1) == '\0')
{
*str = '\0';
str = strrchr(full_path, '/');
}
if (str)
{
len = str - full_path;
if (len > 0)
{
rt_memcpy(path, full_path, len);
path[len] = '\0';
}
else if (len == 0) /* parent path is root path. */
{
path[0] = '/';
path[1] = '\0';
len = 1;
}
}
rt_free(full_path);
return len;
}
/**
* @brief Attempt to read the target of a symbolic link
*
* This function tries to read the contents of a symbolic link file. It first looks up
* the dentry for the given path, checks if it's a symlink, and then calls the filesystem's
* readlink operation if available.
*
* @param[in] path The path of the symbolic link to read
* @param[in] mnt The mount point containing the symbolic link
* @param[out] link Buffer to store the link target (contents of the symlink)
*
* @return int Length of the link target on success, -1 on failure
*/
static int _try_readlink(const char *path, struct dfs_mnt *mnt, char *link)
{
int ret = -1;
struct dfs_dentry *dentry = dfs_dentry_lookup(mnt, path, 0);
if (dentry && dentry->vnode->type == FT_SYMLINK)
{
if (mnt->fs_ops->readlink)
{
if (dfs_is_mounted(mnt) == 0)
{
ret = mnt->fs_ops->readlink(dentry, link, DFS_PATH_MAX);
}
}
}
dfs_dentry_unref(dentry);
return ret;
}
/**
* @brief Normalize a path by combining base path and link path
*
* This function creates a temporary path by combining the base path and link path,
* then normalizes it using dfs_normalize_path(). It handles memory allocation and
* cleanup internally.
*
* @param[in] path The base path to combine with the link
* @param[in] path_len Length of the base path
* @param[in] link_fn The link path to combine with the base path
* @param[in] link_len Length of the link path
*
* @return char* Normalized path string on success, RT_NULL on failure
*
* @note The caller is responsible for freeing the returned path
*/
static char *_dfs_normalize_path(const char *path, int path_len, const char *link_fn, int link_len)
{
char *tmp_path, *fp;
tmp_path = (char *)rt_malloc(path_len + link_len + 2);
if (!tmp_path)
{
return RT_NULL;
}
memcpy(tmp_path, path, path_len);
tmp_path[path_len] = '/';
memcpy(tmp_path + path_len + 1, link_fn, link_len);
tmp_path[path_len + 1 + link_len] = '\0';
fp = dfs_normalize_path(NULL, tmp_path);
rt_free(tmp_path);
return fp;
}
/**
* @brief Insert a link path into temporary path buffer
*
* This function inserts a symbolic link path into a temporary path buffer before
* the specified index position. It handles both relative and absolute paths.
*
* @param[in] link_fn The link path to insert
* @param[in] link_len Length of the link path
* @param[in,out] tmp_path The temporary path buffer to insert into
* @param[in,out] index Pointer to the insertion position index (updated after insertion)
*
* @return int 0 for relative path, 1 for absolute path, -1 on failure
* @note The index is modified to reflect the new insertion position
*/
static int _insert_link_path(const char *link_fn, int link_len, char *tmp_path, int *index)
{
int ret = -1;
if (link_fn[0] != '/')
{
if (link_len + 1 <= *index)
{
*index -= link_len;
rt_memcpy(tmp_path + *index, link_fn, link_len);
*index -= 1;
tmp_path[*index] = '/';
ret = 0;
}
}
else if (link_len <= *index)
{
*index -= link_len;
rt_memcpy(tmp_path + *index, link_fn, link_len);
ret = 1;
}
return ret;
}
/**
* @brief Verify read/write area parameters and limit count size
*
* This function checks the validity of read/write parameters and limits the count
* to a maximum value (MAX_RW_COUNT) to prevent overflow. It ensures the position
* and count values are within valid ranges.
*
* @param[in] file Pointer to the file structure (unused in current implementation)
* @param[in] ppos Pointer to the position offset (input/output)
* @param[in] count Requested read/write count (input)
*
* @return ssize_t The verified count value (limited to MAX_RW_COUNT) or negative error code:
* -EINVAL for invalid parameters
* -EOVERFLOW if position + count would overflow
*
* @note rw_verify_area doesn't like huge counts. We limit them to something that fits in "int"
* so that others won't have to do range checks all the time.
*/
ssize_t rw_verify_area(struct dfs_file *file, off_t *ppos, size_t count)
{
off_t pos;
ssize_t retval = -EINVAL;
if ((size_t)count < 0)
return retval;
pos = *ppos;
if (pos < 0)
{
if (count >= -pos) /* both values are in 0..LLONG_MAX */
return -EOVERFLOW;
}
return count > MAX_RW_COUNT ? MAX_RW_COUNT : count;
}
/**
* @brief Get the current file position
*
* This function retrieves the current file position (offset) from the file structure.
* For regular files, it acquires a mutex lock before accessing the position to ensure
* thread safety. For other file types, it directly returns the position without locking.
*
* @param[in] file Pointer to the file structure containing position information
*
* @return off_t Current file position, or 0 if file pointer is NULL
*/
off_t dfs_file_get_fpos(struct dfs_file *file)
{
if (file)
{
if (file->vnode->type == FT_REGULAR)
{
rt_mutex_take(&file->pos_lock, RT_WAITING_FOREVER);
}
return file->fpos;
}
return 0;
}
/**
* @brief Set the current file position
*
* This function sets the file position (offset) in the file structure.
* It must be used as a pair of dfs_file_get_fpos(). For regular files, pos lock is acquared
* in dfs_file_get_fpos(), so it can be released directly after setting the position.
* Otherwise, pos lock should be acquired first to avoid releasing it without being acquired.
*
* @param[in] file Pointer to the file structure to modify
* @param[in] fpos The new file position to set
*/
void dfs_file_set_fpos(struct dfs_file *file, off_t fpos)
{
if (file)
{
if (file->vnode->type != FT_REGULAR)
{
rt_mutex_take(&file->pos_lock, RT_WAITING_FOREVER);
}
file->fpos = fpos;
rt_mutex_release(&file->pos_lock);
}
}
/**
* @brief Initialize a file structure
*
* @param[in,out] file Pointer to the file structure to be initialized
*
* @note This function must be called before using any file operations
* on a newly allocated file structure
*/
void dfs_file_init(struct dfs_file *file)
{
if (file)
{
rt_memset(file, 0x00, sizeof(struct dfs_file));
file->magic = DFS_FD_MAGIC;
rt_mutex_init(&file->pos_lock, "fpos", RT_IPC_FLAG_PRIO);
rt_atomic_store(&(file->ref_count), 1);
}
}
/**
* @brief Deinitialize a file structure
*
* @param[in,out] file Pointer to the file structure to be deinitialized
*/
void dfs_file_deinit(struct dfs_file *file)
{
if (file)
{
rt_mutex_detach(&file->pos_lock);
}
}
/**
* @brief Decrement reference count and release file resources when count reaches zero
*
* This function safely decrements the reference count of a file structure and releases
* associated resources (dentry or vnode) when the reference count drops to zero.
*
* @param[in,out] file Pointer to the file structure to be unreferenced
*/
static void dfs_file_unref(struct dfs_file *file)
{
rt_err_t ret = RT_EOK;
ret = dfs_file_lock();
if (ret == RT_EOK)
{
if (rt_atomic_load(&(file->ref_count)) == 1)
{
/* should release this file */
if (file->dentry)
{
DLOG(msg, "dfs_file", "dentry", DLOG_MSG, "dfs_dentry_unref(dentry(%s))", file->dentry->pathname);
dfs_dentry_unref(file->dentry);
file->dentry = RT_NULL;
}
else if (file->vnode)
{
if (file->vnode->ref_count > 1)
{
rt_atomic_sub(&(file->vnode->ref_count), 1);
}
else if (file->vnode->ref_count == 1)
{
rt_free(file->vnode);
file->vnode = RT_NULL;
}
}
LOG_I("release a file: %p", file);
}
dfs_file_unlock();
}
}
/**
* @brief Resolve the real path by resolving symbolic links and normalizing the path
*
* This function resolves the real path of a given file path by handling symbolic links
* and normalizing the path components. It supports two modes of operation:
* - DFS_REALPATH_EXCEPT_LAST: Resolve all path components except the last one
* - DFS_REALPATH_ONLY_LAST: Resolve only the last path component
* - DFS_REALPATH_EXCEPT_NONE: Resolve all path components
*
* @param[in,out] mnt Pointer to the mount point structure (updated if path changes)
* @param[in] fullpath The input path to resolve
* @param[in] mode Resolution mode (DFS_REALPATH_EXCEPT_LAST or DFS_REALPATH_ONLY_LAST)
*
* @return char* The resolved real path on success, RT_NULL on failure
*/
char *dfs_file_realpath(struct dfs_mnt **mnt, const char *fullpath, int mode)
{
int path_len = 0, index = 0;
char *path = RT_NULL, *link_fn, *tmp_path;
struct dfs_mnt *tmp_mnt;
if (*mnt && fullpath)
{
int len, link_len;
path = (char *)rt_malloc((DFS_PATH_MAX * 3) + 3); /* path + \0 + link_fn + \0 + tmp_path + \0 */
if (!path)
{
return RT_NULL;
}
link_fn = path + DFS_PATH_MAX + 1;
tmp_path = link_fn + (DFS_PATH_MAX + 1);
len = rt_strlen(fullpath);
if (len > DFS_PATH_MAX)
{
goto _ERR_RET;
}
index = (DFS_PATH_MAX - len);
rt_strcpy(tmp_path + index, fullpath);
if (mode == DFS_REALPATH_ONLY_LAST)
{
path_len = _get_parent_path(fullpath, path);
index += path_len;
}
while ((len = _first_path_len(tmp_path + index)) > 0)
{
if (len + path_len > DFS_PATH_MAX)
{
goto _ERR_RET;
}
rt_memcpy(path + path_len, tmp_path + index, len);
path[path_len + len] = '\0';
index += len;
tmp_mnt = dfs_mnt_lookup(path);
if (tmp_mnt == RT_NULL)
{
goto _ERR_RET;
}
*mnt = tmp_mnt;
/* the last should by mode process. */
if ((tmp_path[index] == '\0') && (mode == DFS_REALPATH_EXCEPT_LAST))
{
break;
}
/* Process symbolic links if found */
link_len = _try_readlink(path, *mnt, link_fn);
if (link_len > 0)
{
if (link_fn[0] == '/') /* Handle absolute path symlinks */
{
int ret = _insert_link_path(link_fn, link_len, tmp_path, &index);
if (ret < 0)
{
goto _ERR_RET;
}
path_len = 0;
}
else /* Handle relative path symlinks */
{
char *fp = _dfs_normalize_path(path, path_len, link_fn, link_len);
if (fp)
{
int pos = rt_strncmp(path, fp, path_len);
if (pos == 0)
{
int ret = _insert_link_path(fp + path_len, rt_strlen(fp + path_len), tmp_path, &index);
if (ret < 0)
{
rt_free(fp);
goto _ERR_RET;
}
}
else
{
int pos;
while(1)
{
while(path_len > 0 && path[path_len] != '/')
{
path_len--;
}
if (path_len > 0)
{
pos = rt_strncmp(path, fp, path_len);
}
else
{
pos = -1;
}
if (pos == 0 || path_len == 0)
{
int ret;
ret = _insert_link_path(fp + path_len, rt_strlen(fp + path_len), tmp_path, &index);
if (ret < 0)
{
rt_free(fp);
goto _ERR_RET;
}
else
{
break;
}
}
else
{
path_len--;
}
}
}
rt_free(fp);
}
}
}
else
{
path_len += len; /* Not a symlink, just advance path length */
}
}
return path;
_ERR_RET:
rt_free(path);
path = RT_NULL;
}
return path;
}
/**
* @brief Open a file which specified by path with specified oflags.
*
* This function opens or creates a file with given path and flags. It handles:
* - Path normalization and resolution (including symbolic links)
* - File creation when O_CREAT is specified
* - Permission checking
* - Directory vs regular file validation
* - Symbolic link following (unless O_NOFOLLOW is set)
*
* @param[in,out] file Pointer to file structure to be initialized
* @param[in] path the specified file path.
* @param[in] oflags the oflags for open operator. (O_RDONLY, O_WRONLY, O_CREAT, etc)
* @param[in] mode File permission mode (used when O_CREAT is specified)
*
* @return 0 on successful, -1 on failure:
* -ENOENT if file doesn't exist and O_CREAT not set
* -EEXIST if file exists and O_EXCL|O_CREAT set
* -EACCES if permission denied
* -ENOTDIR if path is not a directory when O_DIRECTORY set
* -EISDIR if path is directory when opening as regular file
*/
int dfs_file_open(struct dfs_file *file, const char *path, int oflags, mode_t mode)
{
int ret = -RT_ERROR;
char *fullpath = RT_NULL;
struct dfs_dentry *dentry = RT_NULL;
int fflags = dfs_fflags(oflags);
if (mode == 0)
{
mode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); /* 0666 */
}
if (file && path)
{
fullpath = dfs_normalize_path(NULL, path);
if (fullpath)
{
struct dfs_mnt *mnt = RT_NULL;
DLOG(msg, "dfs_file", "mnt", DLOG_MSG, "dfs_mnt_lookup(%s)", fullpath);
mnt = dfs_mnt_lookup(fullpath);
if (mnt)
{
char *tmp = dfs_file_realpath(&mnt, fullpath, DFS_REALPATH_EXCEPT_LAST);
if (tmp)
{
rt_free(fullpath);
fullpath = tmp;
}
DLOG(msg, "dfs_file", "dentry", DLOG_MSG, "dfs_dentry_lookup(mnt, %s)", fullpath);
dentry = dfs_dentry_lookup(mnt, fullpath, oflags);
if (dentry && dentry->vnode->type == FT_SYMLINK)
{
/* it's a symbol link but not follow */
if (oflags & O_NOFOLLOW)
{
/* no follow symbol link */
dfs_dentry_unref(dentry);
dentry = RT_NULL;
}
else
{
struct dfs_dentry *target_dentry = RT_NULL;
char *path = dfs_file_realpath(&mnt, fullpath, DFS_REALPATH_ONLY_LAST);
if (path)
{
target_dentry = dfs_dentry_lookup(mnt, path, oflags);
rt_free(path);
}
dfs_dentry_unref(dentry);
dentry = target_dentry;
}
}
if (dentry)
{
if (oflags & O_DIRECTORY)
{
if (dentry->vnode->type != FT_DIRECTORY)
{
dfs_dentry_unref(dentry);
dentry = RT_NULL;
}
}
else if (dentry->vnode->type == FT_DIRECTORY)
{
if (fflags & (DFS_F_FWRITE))
{
dfs_dentry_unref(dentry);
dentry = RT_NULL;
}
else
{
oflags |= O_DIRECTORY;
}
}
}
if (oflags & O_CREAT)
{
if (dentry)
{
oflags &= ~O_CREAT;
if (oflags & O_EXCL)
{
oflags &= ~O_EXCL;
/* the dentry already exists */
dfs_dentry_unref(dentry);
ret = -EEXIST;
goto _ERR_RET;
}
}
else
{
/* create file/directory */
if (mnt->fs_ops->create_vnode)
{
struct dfs_vnode *vnode = RT_NULL;
DLOG(msg, "dfs_file", "dentry", DLOG_MSG, "dfs_dentry_create(%s)", fullpath);
dfs_file_lock();
dentry = dfs_dentry_create(mnt, fullpath);
if (dentry)
{
DLOG(msg, "dfs_file", mnt->fs_ops->name, DLOG_MSG, "fs_ops->create_vnode");
if (dfs_is_mounted(mnt) == 0)
{
vnode = mnt->fs_ops->create_vnode(dentry, oflags & O_DIRECTORY ? FT_DIRECTORY:FT_REGULAR, mode);
}
if (vnode)
{
/* set vnode */
dentry->vnode = vnode; /* the refcount of created vnode is 1. no need to reference */
dfs_dentry_insert(dentry);
}
else
{
DLOG(msg, mnt->fs_ops->name, "dfs_file", DLOG_MSG_RET, "create failed.");
dfs_dentry_unref(dentry);
dentry = RT_NULL;
}
}
dfs_file_unlock();
}
}
}
if (dentry)
{
rt_bool_t permission = RT_TRUE;
file->dentry = dentry;
file->vnode = dentry->vnode;
file->fops = dentry->mnt->fs_ops->default_fops;
file->flags = oflags;
/* check permission */
if (!(oflags & O_CREAT))
{
if (fflags & DFS_F_FWRITE)
{
if (!(file->vnode->mode & S_IWUSR))
{
permission = RT_FALSE;
}
}
if (fflags & DFS_F_FREAD)
{
if (!(file->vnode->mode & S_IRUSR))
{
permission = RT_FALSE;
}
}
if (oflags & O_EXEC)
{
if (!(file->vnode->mode & S_IXUSR))
{
permission = RT_FALSE;
}
}
}
if (permission && file->fops->open)
{
DLOG(msg, "dfs_file", mnt->fs_ops->name, DLOG_MSG, "fops->open(file)");
if (dfs_is_mounted(file->vnode->mnt) == 0)
{
dfs_file_lock();
ret = file->fops->open(file);
dfs_file_unlock();
}
else
{
ret = -EINVAL;
}
if (ret < 0)
{
LOG_I("open %s failed in file system: %s", path, dentry->mnt->fs_ops->name);
DLOG(msg, mnt->fs_ops->name, "dfs_file", DLOG_MSG_RET, "open failed.");
dfs_file_unref(file);
}
else
{
/* for char/block device */
if ((S_ISCHR(file->vnode->mode)) || (S_ISBLK(file->vnode->mode)))
{
file->fops = file->vnode->fops;
}
}
}
else
{
DLOG(msg, "dfs_file", mnt->fs_ops->name, DLOG_MSG, "no permission or fops->open");
dfs_file_unref(file);
ret = -EACCES;
}
}
else
{
LOG_I("lookup file:%s failed in file system", path);
ret = -ENOENT;
}
}
}
if (ret >= 0 && (oflags & O_TRUNC))
{
/* trunc file */
if (!(fflags & DFS_F_FWRITE) || file->vnode->type == FT_DIRECTORY)
{
/* truncate on read a only file or a directory */
DLOG(msg, "dfs_file", "dfs_file", DLOG_MSG, "dfs_file_unref(file), trunc on RDOnly or directory");
ret = -RT_ERROR;
}
else
{
if (file->fops->truncate)
{
DLOG(msg, "dfs_file", dentry->mnt->fs_ops->name, DLOG_MSG, "fops->truncate(file, 0)");
if (dfs_is_mounted(file->vnode->mnt) == 0)
{
#ifdef RT_USING_PAGECACHE
if (file->vnode->aspace)
{
dfs_aspace_clean(file->vnode->aspace);
}
#endif
ret = file->fops->truncate(file, 0);
}
else
{
ret = -EINVAL;
}
}
}
if (ret < 0)
{
dfs_file_unref(file);
}
file->flags &= ~O_TRUNC;
}
}
_ERR_RET:
if (fullpath != NULL)
{
rt_free(fullpath);
}
return ret;
}
int dfs_file_mknod(const char *path, int type, mode_t mode)
{
int ret = -EINVAL;
int create_type = type;
mode_t create_mode = mode;
char *fullpath;
struct dfs_mnt *mnt;
struct dfs_dentry *dentry;
if (path == RT_NULL || type < FT_REGULAR || type > FT_NONLOCK)
{
return -EINVAL;
}
fullpath = dfs_normalize_path(RT_NULL, path);
if (fullpath == RT_NULL)
{
return -ENOMEM;
}
mnt = dfs_mnt_lookup(fullpath);
if (mnt == RT_NULL)
{
ret = -ENOENT;
goto __exit;
}
{
char *realpath;
realpath = dfs_file_realpath(&mnt, fullpath, DFS_REALPATH_EXCEPT_LAST);
if (realpath != RT_NULL)
{
rt_free(fullpath);
fullpath = realpath;
}
}
if (strcmp(mnt->fullpath, fullpath) == 0)
{
ret = -EEXIST;
goto __exit;
}
dentry = dfs_dentry_lookup(mnt, fullpath, 0);
if (dentry != RT_NULL)
{
dfs_dentry_unref(dentry);
ret = -EEXIST;
goto __exit;
}
if (mnt->fs_ops->create_vnode == RT_NULL)
{
ret = -ENOSYS;
goto __exit;
}
if (type == FT_SOCKET)
{
create_type = FT_REGULAR;
create_mode = (mode & ~S_IFMT) | S_IFSOCK;
}
ret = dfs_file_lock();
if (ret != RT_EOK)
{
goto __exit;
}
dentry = dfs_dentry_create(mnt, fullpath);
if (dentry != RT_NULL)
{
struct dfs_vnode *vnode = RT_NULL;
if (dfs_is_mounted(mnt) == 0)
{
vnode = mnt->fs_ops->create_vnode(dentry, create_type,
create_mode);
}
if (vnode != RT_NULL)
{
if (type == FT_SOCKET && !S_ISSOCK(vnode->mode))
{
if (mnt->fs_ops->unlink != RT_NULL)
{
dentry->vnode = vnode;
(void)mnt->fs_ops->unlink(dentry);
dentry->vnode = RT_NULL;
}
dfs_vnode_unref(vnode);
ret = -EOPNOTSUPP;
}
else
{
vnode->type = type;
dentry->vnode = vnode;
dfs_dentry_insert(dentry);
ret = RT_EOK;
}
}
else
{
ret = -ENOENT;
}
}
else
{
ret = -ENOMEM;
}
dfs_file_unlock();
if (dentry != RT_NULL)
{
dfs_dentry_unref(dentry);
}
__exit:
rt_free(fullpath);
return ret;
}
/**
* @brief Close a file and release associated resources
*
* This function closes a file and performs necessary cleanup operations:
* - Flushes page cache if enabled (RT_USING_PAGECACHE)
* - Calls filesystem-specific close operation if available
* - Decrements reference count and releases resources when count reaches zero
*
* @param[in,out] file Pointer to the file structure to close
*
* @return int Operation result:
* - 0 on success
* - Negative error code on failure
*/
int dfs_file_close(struct dfs_file *file)
{
int ret = -RT_ERROR;
if (file)
{
if (dfs_file_lock() == RT_EOK)
{
rt_atomic_t ref_count = rt_atomic_load(&(file->ref_count));
if (ref_count == 1 && file->fops && file->fops->close)
{
DLOG(msg, "dfs_file", file->dentry->mnt->fs_ops->name, DLOG_MSG, "fops->close(file)");
#ifdef RT_USING_PAGECACHE
if (file->vnode->aspace)
{
dfs_aspace_flush(file->vnode->aspace);
}
#endif
ret = file->fops->close(file);
if (ret == 0) /* close file sucessfully */
{
DLOG(msg, "dfs_file", "dfs_file", DLOG_MSG, "dfs_file_unref(file)");
dfs_file_unref(file);
}
else
{
LOG_W("close file:%s failed on low level file system", file->dentry->pathname);
}
}
else
{
DLOG(msg, "dfs_file", "dfs_file", DLOG_MSG, "dfs_file_unref(file)");
dfs_file_unref(file);
ret = 0;
}
dfs_file_unlock();
}
}
return ret;
}
/**
* @brief Read data from a file at specified offset
*
* @param[in] file Pointer to the file structure to read from
* @param[out] buf Buffer to store the read data
* @param[in] len Number of bytes to read
* @param[in] offset Offset in the file to start reading from
*
* @return ssize_t Number of bytes read on success, or negative error code:
* -EBADF if invalid file descriptor
* -EPERM if read permission denied
* -ENOSYS if read operation not supported
* -EINVAL if invalid parameters or not mounted
*/
ssize_t dfs_file_pread(struct dfs_file *file, void *buf, size_t len, off_t offset)
{
ssize_t ret = -EBADF;
if (file)
{
/* check whether read */
if (!(dfs_fflags(file->flags) & DFS_F_FREAD))
{
ret = -EPERM;
}
else if (!file->fops || !file->fops->read)
{
ret = -ENOSYS;
}
else if (file->vnode && file->vnode->type != FT_DIRECTORY)
{
off_t pos = offset;
ret = rw_verify_area(file, &pos, len);
if (ret > 0)
{
len = ret;
if (dfs_is_mounted(file->vnode->mnt) == 0)
{
#ifdef RT_USING_PAGECACHE
if (file->vnode->aspace && !(file->flags & O_DIRECT))
{
ret = dfs_aspace_read(file, buf, len, &pos);
}
else
#endif
{
ret = file->fops->read(file, buf, len, &pos);
}
}
else
{
ret = -EINVAL;
}
}
}
}
return ret;
}
/**
* @brief Read data from a file at current position
*
* @param[in] file Pointer to the file structure to read from
* @param[out] buf Buffer to store the read data
* @param[in] len Number of bytes to read
*
* @return ssize_t Number of bytes read on success, or negative error code:
* -EBADF if invalid file descriptor
* -EPERM if read permission denied
* -ENOSYS if read operation not supported
* -EINVAL if invalid parameters or not mounted
*/
ssize_t dfs_file_read(struct dfs_file *file, void *buf, size_t len)
{
ssize_t ret = -EBADF;
if (file)
{
/* check whether read */
if (!(dfs_fflags(file->flags) & DFS_F_FREAD))
{
ret = -EPERM;
}
else if (!file->fops || !file->fops->read)
{
ret = -ENOSYS;
}
else if (file->vnode && file->vnode->type != FT_DIRECTORY)
{
/* fpos lock */
off_t pos = dfs_file_get_fpos(file);
ret = rw_verify_area(file, &pos, len);
if (ret > 0)
{
len = ret;
if (dfs_is_mounted(file->vnode->mnt) == 0)
{
#ifdef RT_USING_PAGECACHE
if (file->vnode->aspace && !(file->flags & O_DIRECT))
{
ret = dfs_aspace_read(file, buf, len, &pos);
}
else
#endif
{
ret = file->fops->read(file, buf, len, &pos);
}
}
else
{
ret = -EINVAL;
}
}
/* fpos unlock */
dfs_file_set_fpos(file, pos);
}
}
return ret;
}
/**
* @brief Write data to a file at specified offset
*
* @param[in] file Pointer to the file structure to write to
* @param[in] buf Buffer containing data to write
* @param[in] len Number of bytes to write
* @param[in] offset Offset in the file to start writing from
*
* @return ssize_t Number of bytes written on success, or negative error code:
* -EBADF if invalid file descriptor or bad write flags
* -ENOSYS if write operation not supported
* -EINVAL if invalid parameters or not mounted
*
* @note If O_SYNC flag is set, the data will be immediately flushed to storage device
* after write operation.
*/
ssize_t dfs_file_pwrite(struct dfs_file *file, const void *buf, size_t len, off_t offset)
{
ssize_t ret = -EBADF;
if (file)
{
if (!(dfs_fflags(file->flags) & DFS_F_FWRITE))
{
LOG_W("bad write flags.");
ret = -EBADF;
}
else if (!file->fops || !file->fops->write)
{
LOG_W("no fops write.");
ret = -ENOSYS;
}
else if (file->vnode && file->vnode->type != FT_DIRECTORY)
{
off_t pos = offset;
ret = rw_verify_area(file, &pos, len);
if (ret > 0)
{
len = ret;
DLOG(msg, "dfs_file", file->dentry->mnt->fs_ops->name, DLOG_MSG,
"dfs_file_write(fd, buf, %d)", len);
if (dfs_is_mounted(file->vnode->mnt) == 0)
{
#ifdef RT_USING_PAGECACHE
if (file->vnode->aspace && !(file->flags & O_DIRECT))
{
ret = dfs_aspace_write(file, buf, len, &pos);
}
else
#endif
{
ret = file->fops->write(file, buf, len, &pos);
}
if (file->flags & O_SYNC)
{
file->fops->flush(file);
}
}
else
{
ret = -EINVAL;
}
}
}
}
return ret;
}
/**
* @brief Write data to a file at current position
*
* This function writes data to a file at the current position or at the end if O_APPEND flag is set.
*
* @param[in,out] file Pointer to the file structure to write to
* @param[in] buf Buffer containing data to write
* @param[in] len Number of bytes to write
*
* @return ssize_t Number of bytes written on success, or negative error code:
* -EBADF if invalid file descriptor or bad write flags
* -ENOSYS if write operation not supported
* -EINVAL if invalid parameters or not mounted
*
* @note If O_SYNC flag is set, the data will be immediately flushed to storage device
* @note In append mode (O_APPEND), data is always written at the end of file
*/
ssize_t dfs_file_write(struct dfs_file *file, const void *buf, size_t len)
{
ssize_t ret = -EBADF;
if (file)
{
if (!(dfs_fflags(file->flags) & DFS_F_FWRITE))
{
LOG_W("bad write flags.");
ret = -EBADF;
}
else if (!file->fops || !file->fops->write)
{
LOG_W("no fops write.");
ret = -ENOSYS;
}
else if (file->vnode && file->vnode->type != FT_DIRECTORY)
{
off_t pos;
if (!(file->flags & O_APPEND))
{
/* fpos lock */
pos = dfs_file_get_fpos(file);
}
else
{
pos = file->vnode->size;
}
ret = rw_verify_area(file, &pos, len);
if (ret > 0)
{
len = ret;
DLOG(msg, "dfs_file", file->dentry->mnt->fs_ops->name, DLOG_MSG,
"dfs_file_write(fd, buf, %d)", len);
if (dfs_is_mounted(file->vnode->mnt) == 0)
{
#ifdef RT_USING_PAGECACHE
if (file->vnode->aspace && !(file->flags & O_DIRECT))
{
ret = dfs_aspace_write(file, buf, len, &pos);
}
else
#endif
{
ret = file->fops->write(file, buf, len, &pos);
}
if (file->flags & O_SYNC)
{
file->fops->flush(file);
}
}
else
{
ret = -EINVAL;
}
}
if (!(file->flags & O_APPEND))
{
/* fpos unlock */
dfs_file_set_fpos(file, pos);
}
}
}
return ret;
}
/**
* @brief Generic file seek implementation
*
* This function calculates the new file position based on the specified offset and whence parameter.
* It supports three seek modes:
* - SEEK_SET: Set position relative to start of file
* - SEEK_CUR: Set position relative to current position
* - SEEK_END: Set position relative to end of file
*
* @param[in] file Pointer to the file structure containing current position
* @param[in] offset Offset value to seek
* @param[in] whence Seek mode (SEEK_SET/SEEK_CUR/SEEK_END)
*
* @return off_t The calculated new file position, or -EINVAL for invalid whence
*/
off_t generic_dfs_lseek(struct dfs_file *file, off_t offset, int whence)
{
off_t foffset;
if (whence == SEEK_SET)
foffset = offset;
else if (whence == SEEK_CUR)
foffset = file->fpos + offset;
else if (whence == SEEK_END)
foffset = file->vnode->size + offset;
else
return -EINVAL;
return foffset;
}
/**
* @brief Change the file position indicator
*
* This function sets the file position indicator for the file referenced by the file descriptor
* based on the offset and whence parameters.
*
* @param[in,out] file Pointer to the file structure (position will be modified)
* @param[in] offset Number of bytes to offset from position
* @param[in] whence Reference position (SEEK_SET/SEEK_CUR/SEEK_END)
*
* @return off_t New file position on success, or negative error code:
* -EINVAL if invalid parameters or not mounted
*/
off_t dfs_file_lseek(struct dfs_file *file, off_t offset, int wherece)
{
off_t retval = -EINVAL;
if (file && file->fops->lseek)
{
if (dfs_is_mounted(file->vnode->mnt) == 0)
{
/* fpos lock */
off_t pos = dfs_file_get_fpos(file);
retval = file->fops->lseek(file, offset, wherece);
if (retval >= 0)
{
pos = retval;
}
/* fpos unlock */
dfs_file_set_fpos(file, pos);
}
}
return retval;
}
/**
* @brief Get file status information
*
* @param[in] path The file path to get status for
* @param[out] buf Pointer to stat structure to store the status information
*
* @return int Operation result:
* - 0 on success
* -ENOENT if file not found
* -ENOMEM if memory allocation failed
* Other negative error codes from filesystem operations
*/
int dfs_file_stat(const char *path, struct stat *buf)
{
int ret = -ENOENT;
char *fullpath = RT_NULL;
struct dfs_mnt *mnt = RT_NULL;
struct dfs_dentry *dentry = RT_NULL;
fullpath = dfs_normalize_path(NULL, path);
if (fullpath)
{
DLOG(msg, "dfs_file", "mnt", DLOG_MSG, "dfs_mnt_lookup(%s)", fullpath);
mnt = dfs_mnt_lookup(fullpath);
if (mnt)
{
char *tmp = dfs_file_realpath(&mnt, fullpath, DFS_REALPATH_EXCEPT_NONE);
if (tmp)
{
rt_free(fullpath);
fullpath = tmp;
}
DLOG(msg, "dfs_file", "dentry", DLOG_MSG, "dentry = dfs_dentry_lookup(mnt, %s)", fullpath);
dentry = dfs_dentry_lookup(mnt, fullpath, 0);
if (dentry)
{
DLOG(msg, "dentry", "dfs_file", DLOG_MSG_RET, "return dentry");
if (mnt->fs_ops->stat)
{
DLOG(msg, "dfs_file", mnt->fs_ops->name, DLOG_MSG, "fs_ops->stat(dentry, buf)");
if (dfs_is_mounted(mnt) == 0)
{
ret = mnt->fs_ops->stat(dentry, buf);
if (ret == RT_EOK && buf->st_nlink == 0 && dentry->vnode != RT_NULL)
{
buf->st_nlink = dentry->vnode->nlink;
}
}
}
/* unref dentry */
DLOG(msg, "dfs_file", "dentry", DLOG_MSG, "dfs_dentry_unref(dentry)");
dfs_dentry_unref(dentry);
dentry = RT_NULL;
}
}
rt_free(fullpath);
fullpath = RT_NULL;
}
else
{
ret = -ENOMEM;
}
return ret;
}
/**
* @brief Get file status information without following symbolic links
*
* @param[in] path The file path to get status for (does not follow symlinks)
* @param[out] buf Pointer to stat structure to store the status information
*
* @return int Operation result:
* - 0 on success
* -ENOENT if file not found
* -ENOMEM if memory allocation failed
* Other negative error codes from filesystem operations
*
* @note Unlike dfs_file_stat(), this function does not follow symbolic links
* @see dfs_file_stat()
*/
int dfs_file_lstat(const char *path, struct stat *buf)
{
int ret = -ENOENT;
char *fullpath = RT_NULL;
struct dfs_mnt *mnt = RT_NULL;
struct dfs_dentry *dentry = RT_NULL;
fullpath = dfs_normalize_path(NULL, path);
if (fullpath)
{
DLOG(msg, "dfs_file", "mnt", DLOG_MSG, "dfs_mnt_lookup(%s)", fullpath);
mnt = dfs_mnt_lookup(fullpath);
if (mnt)
{
char *tmp = dfs_file_realpath(&mnt, fullpath, DFS_REALPATH_EXCEPT_LAST);
if (tmp)
{
rt_free(fullpath);
fullpath = tmp;
}
DLOG(msg, "dfs_file", "dentry", DLOG_MSG, "dentry = dfs_dentry_lookup(mnt, %s)", fullpath);
dentry = dfs_dentry_lookup(mnt, fullpath, 0);
if (dentry)
{
DLOG(msg, "dentry", "dfs_file", DLOG_MSG_RET, "return dentry");
if (mnt->fs_ops->stat)
{
DLOG(msg, "dfs_file", mnt->fs_ops->name, DLOG_MSG, "fs_ops->stat(dentry, buf)");
if (dfs_is_mounted(mnt) == 0)
{
ret = mnt->fs_ops->stat(dentry, buf);
if (ret == RT_EOK && buf->st_nlink == 0 && dentry->vnode != RT_NULL)
{
buf->st_nlink = dentry->vnode->nlink;
}
}
}
/* unref dentry */
DLOG(msg, "dfs_file", "dentry", DLOG_MSG, "dfs_dentry_unref(dentry)");
dfs_dentry_unref(dentry);
dentry = RT_NULL;
}
}
rt_free(fullpath);
fullpath = RT_NULL;
}
else
{
ret = -ENOMEM;
}
rt_set_errno(-ret);
return ret;
}
/**
* @brief Get file status information using file descriptor
*
* @param[in] file Pointer to the open file structure
* @param[out] buf Pointer to stat structure to store status information
*
* @return int Operation result:
* - 0 on success
* -EBADF if invalid file descriptor
* -ENOSYS if operation not supported
*
* @note Currently unimplemented (returns -ENOSYS)
*/
int dfs_file_fstat(struct dfs_file *file, struct stat *buf)
{
size_t ret = -EBADF;
if (file)
{
if (file->fops && file->fops->ioctl)
{
/* ret = file->fops->fstat(file, buf); */
}
else
{
ret = -ENOSYS;
}
}
else
{
ret = -EBADF;
}
return ret;
}
/**
* @brief Set file attributes for the specified path
*
* This function sets file attributes (permissions, ownership, timestamps, etc.)
* for the file specified by path.
*
* @param[in] path The file path to set attributes for
* @param[in] attr Pointer to attribute structure containing new attributes
*
* @return int Operation result:
* - 0 on success
* -RT_ERROR if general error occurred
* -ENOENT if file not found
* Other negative error codes from filesystem operations
*
* @note The actual supported attributes depend on the underlying filesystem
*/
int dfs_file_setattr(const char *path, struct dfs_attr *attr)
{
int ret = -RT_ERROR;
char *fullpath = RT_NULL;
struct dfs_mnt *mnt = RT_NULL;
struct dfs_dentry *dentry = RT_NULL;
fullpath = dfs_normalize_path(NULL, path);
if (fullpath)
{
DLOG(msg, "dfs_file", "mnt", DLOG_MSG, "dfs_mnt_lookup(%s)", fullpath);
mnt = dfs_mnt_lookup(fullpath);
if (mnt)
{
char *tmp = dfs_file_realpath(&mnt, fullpath, DFS_REALPATH_EXCEPT_LAST);
if (tmp)
{
rt_free(fullpath);
fullpath = tmp;
}
DLOG(msg, "dfs_file", "dentry", DLOG_MSG, "dentry = dfs_dentry_lookup(mnt, %s)", fullpath);
dentry = dfs_dentry_lookup(mnt, fullpath, 0);
if (dentry)
{
DLOG(msg, "dentry", "dfs_file", DLOG_MSG_RET, "return dentry");
if (mnt->fs_ops->setattr)
{
DLOG(msg, "dfs_file", mnt->fs_ops->name, DLOG_MSG, "fs_ops->setattr(dentry, attr)");
if (dfs_is_mounted(mnt) == 0)
{
ret = mnt->fs_ops->setattr(dentry, attr);
}
}
/* unref dentry */
DLOG(msg, "dfs_file", "dentry", DLOG_MSG, "dfs_dentry_unref(dentry)");
dfs_dentry_unref(dentry);
dentry = RT_NULL;
}
}
rt_free(fullpath);
fullpath = RT_NULL;
}
return ret;
}
int dfs_file_fsetattr(struct dfs_file *file, struct dfs_attr *attr)
{
struct dfs_mnt *mnt;
if (file == RT_NULL || file->dentry == RT_NULL || attr == RT_NULL)
{
return -EBADF;
}
mnt = file->dentry->mnt;
if (mnt == RT_NULL || mnt->fs_ops == RT_NULL ||
mnt->fs_ops->setattr == RT_NULL)
{
return -ENOSYS;
}
if (dfs_is_mounted(mnt) != 0)
{
return -EINVAL;
}
return mnt->fs_ops->setattr(file->dentry, attr);
}
/**
* @brief Perform device-specific control operations
*
* This function performs device-specific control operations on an open file descriptor.
* It is typically used for operations that cannot be expressed by regular file operations.
*
* @param[in] file Pointer to the file structure to perform ioctl on
* @param[in] cmd Device-dependent request code
* @param[in,out] args Pointer to optional argument buffer (input/output depends on cmd)
*
* @return int Operation result:
* - 0 or positive value on success (meaning depends on cmd)
* -EBADF if invalid file descriptor
* -ENOSYS if ioctl operation not supported
* -EINVAL if invalid parameters or not mounted
*
* @note The actual supported commands and their semantics depend on the underlying device driver
*/
int dfs_file_ioctl(struct dfs_file *file, int cmd, void *args)
{
size_t ret = 0;
if (file)
{
if (file->fops && file->fops->ioctl)
{
if (dfs_is_mounted(file->vnode->mnt) == 0)
{
ret = file->fops->ioctl(file, cmd, args);
}
else
{
ret = -EINVAL;
}
}
else
{
ret = -ENOSYS;
}
}
else
{
ret = -EBADF;
}
return ret;
}
/**
* @brief Perform file control operations
*
* This function performs various control operations on an open file descriptor.
* It supports the following operations:
* - F_DUPFD: Duplicate file descriptor
* - F_GETFD: Get file descriptor flags
* - F_SETFD: Set file descriptor flags
* - F_GETFL: Get file status flags
* - F_SETFL: Set file status flags
* - F_GETLK/F_SETLK/F_SETLKW: POSIX advisory record locking
* - F_DUPFD_CLOEXEC: Duplicate file descriptor with close-on-exec flag (if supported)
*
* @param[in] fd File descriptor to operate on
* @param[in] cmd Control command (F_DUPFD/F_GETFD/F_SETFD/F_GETFL/F_SETFL/etc)
* @param[in,out] arg Command-specific argument (input/output depends on cmd)
*
* @return int Operation result:
* - For F_DUPFD: new file descriptor on success
* - For F_GETFD/F_GETFL: current flags on success
* - 0 on success for other commands
* -EBADF if invalid file descriptor
* -EINVAL if invalid command (F_DUPFD_CLOEXEC when not supported)
* -EPERM for unsupported commands
*
* @note Not all commands may be supported by all filesystems
*/
int dfs_file_fcntl(int fd, int cmd, unsigned long arg)
{
int ret = 0;
struct dfs_file *file;
file = fd_get(fd);
if (file)
{
switch (cmd)
{
case F_DUPFD:
ret = dfs_dup(fd, arg);
break;
case F_GETFD:
ret = file->mode;
break;
case F_SETFD:
file->mode = arg;
break;
case F_GETFL:
ret = file->flags;
break;
case F_SETFL:
{
int flags = (int)(rt_base_t)arg;
int mask =
#ifdef O_ASYNC
O_ASYNC |
#endif
#ifdef O_DIRECT
O_DIRECT |
#endif
#ifdef O_NOATIME
O_NOATIME |
#endif
O_APPEND | O_NONBLOCK;
flags &= mask;
if (file->vnode->type == FT_SOCKET && file->fops != RT_NULL &&
file->fops->ioctl != RT_NULL)
{
ret = file->fops->ioctl(file, F_SETFL, (void *)(rt_base_t)flags);
if (ret < 0)
{
break;
}
}
file->flags &= ~mask;
file->flags |= flags;
break;
}
case F_GETLK:
ret = dfs_record_lock_fcntl(file, cmd, (struct flock *)arg);
break;
case F_SETLK:
case F_SETLKW:
ret = dfs_record_lock_fcntl(file, cmd, (struct flock *)arg);
break;
#ifdef RT_USING_MUSLLIBC
case F_DUPFD_CLOEXEC:
ret = -EINVAL;
break;
#endif
default:
ret = -EPERM;
break;
}
}
else
{
ret = -EBADF;
}
return ret;
}
/**
* @brief Synchronize file data to storage device
*
* This function flushes all modified file data and metadata to the underlying storage device.
* It ensures data integrity by:
* - Flushing page cache if enabled (RT_USING_PAGECACHE)
* - Calling filesystem-specific flush operation
*
* @param[in] file Pointer to the file structure to synchronize
*
* @return int Operation result:
* - 0 on success
* -EBADF if invalid file descriptor
* -EINVAL if not mounted or invalid parameters
*
* @note This function provides stronger guarantees than regular writes
* about data persistence on storage media
*/
int dfs_file_fsync(struct dfs_file *file)
{
int ret = -EBADF;
if (file)
{
if (file->fops->flush)
{
if (dfs_is_mounted(file->vnode->mnt) == 0)
{
#ifdef RT_USING_PAGECACHE
if (file->vnode->aspace)
{
dfs_aspace_flush(file->vnode->aspace);
}
#endif
ret = file->fops->flush(file);
}
else
{
ret = -EINVAL;
}
}
}
return ret;
}
/**
* @brief Delete a file or directory entry from the filesystem
*
* This function removes a filesystem entry (file or empty directory) specified by path.
*
* @param[in] path The filesystem path to be deleted
*
* @return int Operation result:
* - 0 on success
* -RT_ERROR if general error occurred
* -ENOENT if file not found
* -ENOMEM if memory allocation failed
* -EBUSY if file is in use (mount point or has child mounts)
* Other negative error codes from filesystem operations
*
* @note This function cannot remove non-empty directories
* @note Mount points cannot be removed while mounted
*/
int dfs_file_unlink(const char *path)
{
int ret = -RT_ERROR;
char *fullpath = RT_NULL;
struct dfs_mnt *mnt = RT_NULL;
struct dfs_dentry *dentry = RT_NULL;
fullpath = dfs_normalize_path(NULL, path);
if (fullpath)
{
DLOG(msg, "dfs_file", "mnt", DLOG_MSG, "dfs_mnt_lookup(%s)", fullpath);
mnt = dfs_mnt_lookup(fullpath);
if (mnt)
{
char *tmp = dfs_file_realpath(&mnt, fullpath, DFS_REALPATH_EXCEPT_LAST);
if (tmp)
{
rt_free(fullpath);
fullpath = tmp;
}
if (strcmp(mnt->fullpath, fullpath) != 0)
{
DLOG(msg, "dfs_file", "dentry", DLOG_MSG, "dfs_dentry_lookup(mnt, %s)", fullpath);
dentry = dfs_dentry_lookup(mnt, fullpath, 0);
if (dentry)
{
rt_bool_t has_child = RT_FALSE;
has_child = dfs_mnt_has_child_mnt(mnt, fullpath);
#ifdef RT_USING_PAGECACHE
if (dentry->vnode->aspace)
{
dfs_aspace_clean(dentry->vnode->aspace);
}
#endif
dfs_file_lock();
if (has_child == RT_FALSE)
{
/* no child mnt point, unlink it */
ret = -RT_ERROR;
if (mnt->fs_ops->unlink)
{
if (dfs_is_mounted(mnt) == 0)
{
ret = mnt->fs_ops->unlink(dentry);
}
}
}
else
{
ret = -EBUSY;
}
dfs_file_unlock();
/* release this dentry */
dfs_dentry_unref(dentry);
}
else
{
/* no this entry */
ret = -ENOENT;
}
}
else
{
/* it's a mount point, failed for busy */
ret = -EBUSY;
}
}
else
{
ret = -ENOENT;
}
/* release fullpath */
rt_free(fullpath);
}
else
{
ret = -ENOMEM;
}
return ret;
}
/**
* @brief Create a hard link between files
*
* This function creates a hard link named 'newname' which refers to the same file as 'oldname'.
*
* @param[in] oldname Path to the existing file to link from
* @param[in] newname Path to the new link to be created
*
* @return int Operation result:
* - 0 on success
* -1 on general error
* -EPERM if oldname is a directory
* Other negative error codes from filesystem operations
*
* @note Both files must reside on the same filesystem
* @note The function will fail if newname already exists
*/
int dfs_file_link(const char *oldname, const char *newname)
{
int ret = -1;
struct stat stat;
struct dfs_mnt *mnt = RT_NULL;
char *old_fullpath, *new_fullpath;
if (dfs_file_isdir(oldname) == 0)
{
rt_set_errno(-EPERM);
return ret;
}
if (dfs_file_lstat(newname, &stat) >= 0)
{
return ret;
}
old_fullpath = dfs_normalize_path(NULL, oldname);
if (old_fullpath)
{
DLOG(msg, "dfs_file", "mnt", DLOG_MSG, "dfs_mnt_lookup(%s)", old_fullpath);
mnt = dfs_mnt_lookup(old_fullpath);
if (mnt == RT_NULL)
{
rt_free(old_fullpath);
return -1;
}
char *tmp = dfs_file_realpath(&mnt, old_fullpath, DFS_REALPATH_EXCEPT_LAST);
if (tmp)
{
rt_free(old_fullpath);
old_fullpath = tmp;
}
}
new_fullpath = dfs_normalize_path(NULL, newname);
if (new_fullpath)
{
char *tmp = dfs_file_realpath(&mnt, new_fullpath, DFS_REALPATH_EXCEPT_LAST);
if (tmp)
{
rt_free(new_fullpath);
new_fullpath = tmp;
}
}
if (old_fullpath && new_fullpath)
{
struct dfs_dentry *old_dentry, *new_dentry;
DLOG(msg, "dfs_file", "dentry", DLOG_MSG, "dfs_dentry_lookup(mnt, %s)", old_fullpath);
old_dentry = dfs_dentry_lookup(mnt, old_fullpath, 0);
DLOG(msg, "dfs_file", "dentry", DLOG_MSG, "dfs_dentry_create(%s)", new_fullpath);
new_dentry = dfs_dentry_create(mnt, new_fullpath);
if (old_dentry && new_dentry)
{
if (mnt->fs_ops->link)
{
if (dfs_is_mounted(mnt) == 0)
{
ret = mnt->fs_ops->link(old_dentry, new_dentry);
}
}
}
dfs_dentry_unref(old_dentry);
dfs_dentry_unref(new_dentry);
}
if (old_fullpath)
{
rt_free(old_fullpath);
}
if (new_fullpath)
{
rt_free(new_fullpath);
}
return ret;
}
/**
* @brief Create a symbolic link named 'linkpath' containing the string 'target'
*
* This function creates a symbolic link which refers to the specified target path.
* The linkpath should not exist before calling this function.
*
* @param[in] target The path string that the symbolic link will point to
* @param[in] linkpath The path where the symbolic link will be created
*
* @return int Operation status:
* - 0 on success
* -ENOSYS if symlink operation not supported by filesystem
* -ENOENT if parent directory doesn't exist
* -EPERM if linkpath already exists
* -EINVAL if invalid parameters
* -RT_ERROR for general errors
*/
int dfs_file_symlink(const char *target, const char *linkpath)
{
int ret = -RT_ERROR;
char *fullpath = RT_NULL, *parent = RT_NULL;
struct dfs_mnt *mnt = RT_NULL;
struct dfs_dentry *dentry = RT_NULL;
if (target && linkpath)
{
if (linkpath[0] != '/')
{
fullpath = dfs_normalize_path(NULL, linkpath);
}
else
{
fullpath = (char*)linkpath;
}
/* linkpath should be not exist */
if (dfs_file_access(fullpath, O_RDONLY) != 0)
{
char *index;
/* get parent path */
index = strrchr(fullpath, '/');
if (index)
{
int length = index - fullpath;
if (length > 0)
{
parent = (char*) rt_malloc (length + 1);
if (parent)
{
memcpy(parent, fullpath, length);
parent[length] = '\0';
}
}
else
{
parent = (char*) rt_malloc (1 + 1);
if (parent)
{
parent[0] = '/';
parent[1] = '\0';
}
}
}
if (parent)
{
DLOG(msg, "dfs_file", "mnt", DLOG_MSG, "dfs_mnt_lookup(%s)", fullpath);
mnt = dfs_mnt_lookup(parent);
if (mnt)
{
char *tmp = dfs_file_realpath(&mnt, parent, DFS_REALPATH_EXCEPT_LAST);
if (tmp)
{
rt_free(parent);
parent = tmp;
}
DLOG(msg, "dfs_file", "dentry", DLOG_MSG, "dfs_dentry_lookup(mnt, %s)", fullpath);
dentry = dfs_dentry_lookup(mnt, parent, DFS_REALPATH_EXCEPT_LAST);
if (dentry)
{
if (dentry->mnt->fs_ops->symlink)
{
char *path = dfs_normalize_path(parent, target);
if (path)
{
ret = rt_strncmp(parent, path, strlen(parent));
if (ret == 0)
{
tmp = path + strlen(parent);
if (*tmp == '/')
{
tmp ++;
}
}
else
{
tmp = path;
}
if (dfs_is_mounted(mnt) == 0)
{
ret = mnt->fs_ops->symlink(dentry, tmp, index + 1);
}
rt_free(path);
}
}
else
{
ret = -ENOSYS;
}
dfs_dentry_unref(dentry);
}
else
{
ret = -ENOENT;
}
}
else
{
ret = -ENOENT;
}
rt_free(parent);
}
}
else
{
rt_set_errno(-EPERM);
}
if (fullpath != linkpath)
rt_free(fullpath);
}
else
{
ret = -EINVAL;
}
return ret;
}
/**
* @brief Read the contents of a symbolic link
*
* This function reads the contents of the symbolic link specified by path into
* the buffer provided.
*
* @param[in] path The path to the symbolic link to be read
* @param[out] buf Buffer to store the link contents
* @param[in] bufsize Size of the buffer in bytes
*
* @return int Number of bytes placed in buffer on success, or negative error code:
* -ENOSYS if readlink operation not supported by filesystem
* -ENOENT if symbolic link does not exist
* -ENOMEM if memory allocation failed
* -EINVAL if invalid parameters
* -RT_ERROR for general errors
*/
int dfs_file_readlink(const char *path, char *buf, int bufsize)
{
int ret = -RT_ERROR;
char *fullpath = RT_NULL;
struct dfs_mnt *mnt = RT_NULL;
struct dfs_dentry *dentry = RT_NULL;
fullpath = dfs_normalize_path(NULL, path);
if (fullpath)
{
DLOG(msg, "dfs_file", "mnt", DLOG_MSG, "dfs_mnt_lookup(%s)", fullpath);
mnt = dfs_mnt_lookup(fullpath);
if (mnt)
{
char *tmp = dfs_file_realpath(&mnt, fullpath, DFS_REALPATH_EXCEPT_LAST);
if (tmp)
{
rt_free(fullpath);
fullpath = tmp;
}
DLOG(msg, "dfs_file", "dentry", DLOG_MSG, "dfs_dentry_lookup(mnt, %s)", fullpath);
dentry = dfs_dentry_lookup(mnt, fullpath, 0);
if (dentry)
{
if (mnt->fs_ops->readlink)
{
if (dfs_is_mounted(mnt) == 0)
{
ret = mnt->fs_ops->readlink(dentry, buf, bufsize);
}
}
else
{
ret = -ENOSYS;
}
/* release this dentry */
dfs_dentry_unref(dentry);
}
else
{
/* no this entry */
ret = -ENOENT;
}
}
else
{
ret = -ENOENT;
}
/* release fullpath */
rt_free(fullpath);
}
else
{
ret = -ENOMEM;
}
return ret;
}
/**
* @brief Rename a file/directory
*
* This function renames a filesystem entry from old_file to new_file.
*
* @param[in] old_file Path to the existing file/directory to be renamed
* @param[in] new_file New path for the file/directory
*
* @return int Operation result:
* - 0 on success
* -1 on general error
* -ENOMEM if memory allocation failed
* -ENOSYS if rename operation not supported
* -EINVAL if invalid parameters or not mounted
*
* @note Page cache will be cleaned if RT_USING_PAGECACHE is enabled
*/
int dfs_file_rename(const char *old_file, const char *new_file)
{
int ret = -1;
struct dfs_mnt *mnt = RT_NULL;
char *old_fullpath, *new_fullpath;
old_fullpath = dfs_normalize_path(NULL, old_file);
if (old_fullpath)
{
DLOG(msg, "dfs_file", "mnt", DLOG_MSG, "dfs_mnt_lookup(%s)", old_fullpath);
mnt = dfs_mnt_lookup(old_fullpath);
if (mnt == RT_NULL)
{
rt_free(old_fullpath);
return -1;
}
char *tmp = dfs_file_realpath(&mnt, old_fullpath, DFS_REALPATH_EXCEPT_LAST);
if (tmp)
{
rt_free(old_fullpath);
old_fullpath = tmp;
}
}
new_fullpath = dfs_normalize_path(NULL, new_file);
if (new_fullpath)
{
char *tmp = dfs_file_realpath(&mnt, new_fullpath, DFS_REALPATH_EXCEPT_LAST);
if (tmp)
{
rt_free(new_fullpath);
new_fullpath = tmp;
}
}
if (old_fullpath && new_fullpath)
{
struct dfs_dentry *old_dentry, *new_dentry;
DLOG(msg, "dfs_file", "dentry", DLOG_MSG, "dfs_dentry_lookup(mnt, %s)", old_fullpath);
old_dentry = dfs_dentry_lookup(mnt, old_fullpath, 0);
DLOG(msg, "dfs_file", "dentry", DLOG_MSG, "dfs_dentry_create(%s)", new_fullpath);
new_dentry = dfs_dentry_create(mnt, new_fullpath);
if (old_dentry && new_dentry)
{
if (mnt->fs_ops->rename)
{
if (dfs_is_mounted(mnt) == 0)
{
#ifdef RT_USING_PAGECACHE
if (old_dentry->vnode->aspace)
{
dfs_aspace_clean(old_dentry->vnode->aspace);
}
#endif
ret = mnt->fs_ops->rename(old_dentry, new_dentry);
}
}
}
dfs_dentry_unref(old_dentry);
dfs_dentry_unref(new_dentry);
}
if (old_fullpath)
{
rt_free(old_fullpath);
}
if (new_fullpath)
{
rt_free(new_fullpath);
}
return ret;
}
/**
* @brief Truncate or extend a file to the specified length
*
* This function changes the size of the file referenced by the file descriptor.
* If the new size is smaller than current size, the file is truncated.
* If larger, the file is extended and the extended area is filled with zeros.
*
* @param[in] file Pointer to the file structure to truncate
* @param[in] length New length of the file in bytes
*
* @return int Operation result:
* - 0 on success
* -EBADF if invalid file descriptor
* -EISDIR if the file is a directory (directories cannot be truncated)
* -ENOSYS if truncate operation not supported
* -EINVAL if invalid parameters or not mounted
*
* @note If RT_USING_PAGECACHE is enabled, the page cache will be cleaned
* before truncation to ensure data consistency
*/
int dfs_file_ftruncate(struct dfs_file *file, off_t length)
{
int ret = 0;
if (file)
{
if (file->vnode->type == FT_DIRECTORY)
{
return -EISDIR;
}
if (file->fops->truncate)
{
if (dfs_is_mounted(file->vnode->mnt) == 0)
{
#ifdef RT_USING_PAGECACHE
if (file->vnode->aspace)
{
dfs_aspace_clean(file->vnode->aspace);
}
#endif
ret = file->fops->truncate(file, length);
}
else
{
ret = -EINVAL;
}
}
else
{
ret = -ENOSYS;
}
}
else
{
ret = -EBADF;
}
return ret;
}
/**
* @brief Flush file buffers to storage device
*
* This function forces any buffered data to be written to the underlying storage device.
*
* @param[in,out] file Pointer to the file structure to flush (both input and output)
*
* @return int Operation result:
* - 0 on success
* -EBADF if invalid file descriptor
* -ENOSYS if flush operation not supported
* -EINVAL if invalid parameters or not mounted
*
* @note This function provides stronger guarantees than regular writes
* about data persistence on storage media
* @note If RT_USING_PAGECACHE is enabled, the page cache will be flushed first
*/
int dfs_file_flush(struct dfs_file *file)
{
int ret = 0;
if (file)
{
if (file->fops->flush)
{
if (dfs_is_mounted(file->vnode->mnt) == 0)
{
#ifdef RT_USING_PAGECACHE
if (file->vnode->aspace)
{
dfs_aspace_flush(file->vnode->aspace);
}
#endif
ret = file->fops->flush(file);
}
else
{
ret = -EINVAL;
}
}
else
{
ret = -ENOSYS;
}
}
else
{
ret = -EBADF;
}
return ret;
}
/**
* @brief Read directory entries
*
* This function reads directory entries from the directory file descriptor into
* the buffer provided. Each entry is stored as a struct dirent.
*
* @param[in] file Pointer to the directory file structure
* @param[out] dirp Buffer to store directory entries
* @param[in] nbytes Size of the buffer in bytes
*
* @return int Number of bytes read on success, or negative error code:
* -EBADF if invalid file descriptor
* -ENOTDIR if not a directory
* -EINVAL if not mounted or invalid parameters
* -RT_ERROR for general errors
*/
int dfs_file_getdents(struct dfs_file *file, struct dirent *dirp, size_t nbytes)
{
int ret = -RT_ERROR;
if (file)
{
if (file->vnode && S_ISDIR(file->vnode->mode))
{
if (file->fops && file->fops->getdents)
{
DLOG(msg, "dfs_file", file->dentry->mnt->fs_ops->name, DLOG_MSG, "fops->getdents()");
if (dfs_is_mounted(file->vnode->mnt) == 0)
{
ret = file->fops->getdents(file, dirp, nbytes);
}
else
{
ret = -EINVAL;
}
}
}
}
else
{
ret = -EBADF;
}
return ret;
}
/**
* @brief Check if a path refers to a directory
*
* This function checks whether the specified path exists and is a directory.
*
* @param[in] path The filesystem path to check
*
* @return int Operation result:
* - 0 if path exists and is a directory
* -RT_ERROR if path doesn't exist or isn't a directory
*/
int dfs_file_isdir(const char *path)
{
int ret = -RT_ERROR;
char *fullpath = RT_NULL;
struct dfs_mnt *mnt = RT_NULL;
struct dfs_dentry *dentry = RT_NULL;
fullpath = dfs_normalize_path(NULL, path);
if (fullpath)
{
DLOG(msg, "dfs_file", "mnt", DLOG_MSG, "dfs_mnt_lookup(%s)", fullpath);
mnt = dfs_mnt_lookup(fullpath);
if (mnt)
{
char *tmp = dfs_file_realpath(&mnt, fullpath, DFS_REALPATH_EXCEPT_NONE);
if (tmp)
{
rt_free(fullpath);
fullpath = tmp;
}
DLOG(msg, "dfs_file", "dentry", DLOG_MSG, "dentry = dfs_dentry_lookup(mnt, %s)", fullpath);
dentry = dfs_dentry_lookup(mnt, fullpath, 0);
if (dentry)
{
DLOG(msg, "dentry", "dfs_file", DLOG_MSG_RET, "return dentry");
if (mnt->fs_ops->stat)
{
struct stat stat = {0};
DLOG(msg, "dfs_file", mnt->fs_ops->name, DLOG_MSG, "fs_ops->stat(dentry, buf)");
if (dfs_is_mounted(mnt) == 0)
{
ret = mnt->fs_ops->stat(dentry, &stat);
}
if (ret == RT_EOK && S_ISDIR(stat.st_mode))
{
ret = RT_EOK;
}
else
{
ret = -RT_ERROR;
}
}
/* unref dentry */
DLOG(msg, "dfs_file", "dentry", DLOG_MSG, "dfs_dentry_unref(dentry)");
dfs_dentry_unref(dentry);
dentry = RT_NULL;
}
}
rt_free(fullpath);
fullpath = RT_NULL;
}
return ret;
}
/**
* @brief Check file accessibility with specified mode
*
* This function checks whether the file specified by path can be accessed
* with the given mode.
*
* @param[in] path The file path to check accessibility
* @param[in] mode The access mode to check (read/write/execute permissions)
*
* @return int Access status:
* - 0 if file is accessible with specified mode
* - -1 if file is not accessible
*/
int dfs_file_access(const char *path, mode_t mode)
{
int ret;
struct dfs_file file;
dfs_file_init(&file);
if (dfs_file_open(&file, path, O_RDONLY, mode) >= 0)
{
ret = 0;
dfs_file_close(&file);
}
else
{
ret = -1;
}
dfs_file_deinit(&file);
return ret;
}
#ifdef RT_USING_SMART
/**
* @brief Memory map a file or device into process address space
*
* This function maps a file or device into the calling process's address space.
* It handles both regular files and device files differently:
* - For regular files: uses standard mmap operation
* - For device files: uses device-specific ioctl with RT_FIOMMAP2 command
*
* @param[in] file Pointer to the file structure to be mapped
* @param[in,out] mmap2 Pointer to mmap arguments structure (both input and output)
*
* @return int Operation result:
* - RT_EOK on success
* - EINVAL if invalid parameters or operation not supported
* - Other error codes from underlying mmap/ioctl operations
*
* @note For device files, the actual mapping behavior depends on the device driver
*/
int dfs_file_mmap2(struct dfs_file *file, struct dfs_mmap2_args *mmap2)
{
int ret = RT_EOK;
if (file && mmap2)
{
if (file->vnode->type == FT_REGULAR)
{
ret = dfs_file_mmap(file, mmap2);
if (ret != 0)
{
ret = ret > 0 ? ret : -ret;
rt_set_errno(ret);
}
}
else if (file->vnode->type != FT_DEVICE || !file->vnode->fops->ioctl)
{
rt_set_errno(EINVAL);
}
else if (file->vnode->type == FT_DEVICE && file->vnode->fops->ioctl)
{
if (dfs_is_mounted(file->vnode->mnt) == 0)
{
ret = file->vnode->fops->ioctl(file, RT_FIOMMAP2, mmap2);
}
else
{
ret = EINVAL;
}
if (ret != 0)
{
ret = ret > 0 ? ret : -ret;
rt_set_errno(ret);
}
}
}
return ret;
}
#endif
#ifdef RT_USING_FINSH
#define _COLOR_RED "\033[31m"
#define _COLOR_GREEN "\033[32m"
#define _COLOR_YELLOW "\033[33m"
#define _COLOR_BLUE "\033[34m"
#define _COLOR_CYAN "\033[36m"
#define _COLOR_WHITE "\033[37m"
#define _COLOR_NORMAL "\033[0m"
/**
* @brief List directory contents with colored output
*
* This function lists all entries in the specified directory with colored output
* that distinguishes different file types. It handles:
* - Directories (blue)
* - Symbolic links (cyan with target path)
* - Executable files (green)
* - Character devices (yellow)
* - Regular files (default color)
*
* @param[in] pathname The directory path to list (NULL for current directory)
*/
void ls(const char *pathname)
{
struct dirent dirent;
struct stat stat;
int length;
char *fullpath, *path;
struct dfs_file file;
if (pathname == NULL)
{
#ifdef DFS_USING_WORKDIR
/* open current working directory */
path = rt_strdup(working_directory);
#else
path = rt_strdup("/");
#endif
if (path == NULL)
{
return; /* out of memory */
}
}
else
{
path = dfs_normalize_path(NULL, (char *)pathname);
if (path == NULL)
{
return; /* out of memory */
}
}
dfs_file_init(&file);
/* list directory */
DLOG(msg, "dfs", "dfs_file", DLOG_MSG, "dfs_file_open(%s, O_DIRECTORY, 0)", path);
if (dfs_file_open(&file, path, O_DIRECTORY, 0) >= 0)
{
char *link_fn = (char *)rt_malloc(DFS_PATH_MAX);
if (link_fn)
{
rt_kprintf("Directory %s:\n", path);
do
{
memset(&dirent, 0, sizeof(struct dirent));
DLOG(group, "foreach_item");
DLOG(msg, "dfs", "dfs_file", DLOG_MSG, "dfs_file_getdents(&dirent)");
length = dfs_file_getdents(&file, &dirent, sizeof(struct dirent));
if (length > 0)
{
DLOG(msg, "dfs_file", "dfs", DLOG_MSG_RET, "dirent.d_name=%s", dirent.d_name);
memset(&stat, 0, sizeof(struct stat));
/* build full path for each file */
fullpath = dfs_normalize_path(path, dirent.d_name);
if (fullpath == NULL)
break;
DLOG(msg, "dfs", "dfs_file", DLOG_MSG, "dfs_file_lstat(%s, &stat)", fullpath);
if (dfs_file_lstat(fullpath, &stat) == 0)
{
if (S_ISDIR(stat.st_mode))
{
rt_kprintf(_COLOR_BLUE "%-20s" _COLOR_NORMAL, dirent.d_name);
rt_kprintf(" %-25s\n", "<DIR>");
}
else if (S_ISLNK(stat.st_mode))
{
int ret = 0;
rt_kprintf(_COLOR_CYAN "%-20s" _COLOR_NORMAL, dirent.d_name);
ret = dfs_file_readlink(fullpath, link_fn, DFS_PATH_MAX);
if (ret > 0)
{
char *link_path = link_fn;
struct dfs_mnt *mnt = RT_NULL;
mnt = dfs_mnt_lookup(fullpath);
if (mnt)
{
char *tmp = dfs_file_realpath(&mnt, fullpath, DFS_REALPATH_EXCEPT_LAST);
if (tmp)
{
char *index;
index = strrchr(fullpath, '/');
if (index)
{
int length = index - fullpath;
char *parent = (char*) rt_malloc (length + 1);
if (parent)
{
rt_memcpy(parent, fullpath, length);
parent[length] = '\0';
ret = rt_strncmp(parent, link_fn, length);
if (ret == 0)
{
link_path = link_fn + length;
if (*link_path == '/')
{
link_path ++;
}
}
rt_free(parent);
}
}
rt_free(tmp);
}
}
rt_kprintf("-> %s\n", link_path);
}
else
{
rt_kprintf(_COLOR_RED "-> link_error\n" _COLOR_NORMAL);
}
}
else if (stat.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))
{
rt_kprintf(_COLOR_GREEN "%-20s" _COLOR_NORMAL, dirent.d_name);
rt_kprintf(" %-25lu\n", (unsigned long)stat.st_size);
}
else if (S_ISCHR(stat.st_mode))
{
rt_kprintf(_COLOR_YELLOW "%-20s" _COLOR_NORMAL, dirent.d_name);
rt_kprintf(" %-25s\n", "<CHR>");
}
else
{
rt_kprintf("%-20s", dirent.d_name);
rt_kprintf(" %-25lu\n", (unsigned long)stat.st_size);
}
}
else
{
rt_kprintf(_COLOR_RED "%-20s\n" _COLOR_NORMAL, dirent.d_name);
}
rt_free(fullpath);
}
else
{
DLOG(msg, "dfs_file", "dfs", DLOG_MSG_RET, "return NULL");
}
DLOG(group_end);
} while (length > 0);
rt_free(link_fn);
}
DLOG(msg, "dfs", "dfs_file", DLOG_MSG, "dfs_file_close()");
dfs_file_close(&file);
}
else
{
rt_kprintf("No such directory\n");
}
dfs_file_deinit(&file);
DLOG(msg, "dfs_file", "dfs", DLOG_MSG_RET, "return");
rt_free(path);
}
/**
* @brief Display file contents to standard output
*
* This function reads and prints the contents of the specified file to the console.
* It handles both text and binary files by reading in chunks and printing the output.
*
* @param[in] filename Path to the file to be displayed
*/
void cat(const char *filename)
{
int length = 0;
char buffer[81];
struct dfs_file file;
if (filename && dfs_file_isdir(filename) == 0)
{
rt_kprintf("cat: %s Is a directory\n", filename);
return;
}
dfs_file_init(&file);
DLOG(msg, "dfs", "dfs_file", DLOG_MSG, "dfs_file_open(%s, O_RDONLY, 0)", filename);
if (dfs_file_open(&file, filename, O_RDONLY, 0) < 0)
{
rt_kprintf("Open %s failed\n", filename);
dfs_file_deinit(&file);
return;
}
do
{
rt_memset(buffer, 0x0, sizeof(buffer));
DLOG(msg, "dfs", "dfs_file", DLOG_MSG, "dfs_file_read(fd, buffer, %d)", sizeof(buffer) - 1);
length = dfs_file_read(&file, (void *)buffer, sizeof(buffer) - 1);
if (length > 0)
{
buffer[length] = '\0';
rt_kprintf("%s", buffer);
}
} while (length > 0);
rt_kprintf("\n");
DLOG(msg, "dfs", "dfs_file", DLOG_MSG, "dfs_file_close()");
dfs_file_close(&file);
dfs_file_deinit(&file);
}
#define BUF_SZ 4096
/**
* @brief Copy file contents from source to destination
*
* This function copies the contents of a source file to a destination file.
* It handles memory allocation, file operations, and error checking.
*
* @param[in] src Path to the source file to be copied
* @param[in] dst Path to the destination file to be created/overwritten
*/
static void copyfile(const char *src, const char *dst)
{
int ret;
struct dfs_file src_file, dst_file;
rt_uint8_t *block_ptr;
rt_int32_t read_bytes;
block_ptr = (rt_uint8_t *)rt_malloc(BUF_SZ);
if (block_ptr == NULL)
{
rt_kprintf("out of memory\n");
return;
}
dfs_file_init(&src_file);
ret = dfs_file_open(&src_file, src, O_RDONLY, 0);
if (ret < 0)
{
dfs_file_deinit(&src_file);
rt_free(block_ptr);
rt_kprintf("Read %s failed\n", src);
return;
}
dfs_file_init(&dst_file);
ret = dfs_file_open(&dst_file, dst, O_WRONLY | O_CREAT | O_TRUNC, 0);
if (ret < 0)
{
dfs_file_deinit(&dst_file);
dfs_file_close(&src_file);
dfs_file_deinit(&src_file);
rt_free(block_ptr);
rt_kprintf("Write %s failed\n", dst);
return;
}
do
{
read_bytes = dfs_file_read(&src_file, block_ptr, BUF_SZ);
if (read_bytes > 0)
{
int length;
length = dfs_file_write(&dst_file, block_ptr, read_bytes);
if (length != read_bytes)
{
/* write failed. */
rt_kprintf("Write file data failed, errno=%d\n", length);
break;
}
}
} while (read_bytes > 0);
dfs_file_close(&dst_file);
dfs_file_deinit(&dst_file);
dfs_file_close(&src_file);
dfs_file_deinit(&src_file);
rt_free(block_ptr);
}
extern int mkdir(const char *path, mode_t mode);
/**
* @brief Recursively copy directory contents from source to destination
*
* This function recursively copies all files and subdirectories from the source
* directory to the destination directory. It handles both files and directories
* appropriately.
*
* @param[in] src Path to the source directory to be copied
* @param[in] dst Path to the destination directory to be created
*/
static void copydir(const char *src, const char *dst)
{
struct dirent dirent;
struct stat stat;
int length;
struct dfs_file file;
dfs_file_init(&file);
if (dfs_file_open(&file, src, O_DIRECTORY, 0) < 0)
{
rt_kprintf("open %s failed\n", src);
dfs_file_deinit(&file);
return ;
}
do
{
rt_memset(&dirent, 0, sizeof(struct dirent));
length = dfs_file_getdents(&file, &dirent, sizeof(struct dirent));
if (length > 0)
{
char *src_entry_full = NULL;
char *dst_entry_full = NULL;
if (strcmp(dirent.d_name, "..") == 0 || strcmp(dirent.d_name, ".") == 0)
continue;
/* build full path for each file */
if ((src_entry_full = dfs_normalize_path(src, dirent.d_name)) == NULL)
{
rt_kprintf("out of memory!\n");
break;
}
if ((dst_entry_full = dfs_normalize_path(dst, dirent.d_name)) == NULL)
{
rt_kprintf("out of memory!\n");
rt_free(src_entry_full);
break;
}
rt_memset(&stat, 0, sizeof(struct stat));
if (dfs_file_lstat(src_entry_full, &stat) != 0)
{
rt_kprintf("open file: %s failed\n", dirent.d_name);
continue;
}
if (S_ISDIR(stat.st_mode))
{
mkdir(dst_entry_full, 0);
copydir(src_entry_full, dst_entry_full);
}
else
{
copyfile(src_entry_full, dst_entry_full);
}
rt_free(src_entry_full);
rt_free(dst_entry_full);
}
}
while (length > 0);
dfs_file_close(&file);
dfs_file_deinit(&file);
}
/**
* @brief Extract the last component from a path string
*
* This function extracts the filename or last directory name from a given path.
* It searches for the last '/' character and returns the substring after it.
*
* @param[in] path The input path string to process
*
* @return const char* Pointer to:
* - The last path component if '/' is found
* - The original path if no '/' is found
* - NULL if input path is NULL
*/
static const char *_get_path_lastname(const char *path)
{
char *ptr;
if ((ptr = (char *)strrchr(path, '/')) == NULL)
return path;
/* skip the '/' then return */
return ++ptr;
}
/**
* @brief Copy files or directories from source to destination
*
* This function handles copying operations between files and directories with
* various combinations of source and destination types. It supports:
* - File to file copy
* - File to directory copy (copies into directory with original filename)
* - Directory to directory copy (recursive)
* - Directory to new directory creation and copy
*
* @param[in] src Path to the source file/directory to copy
* @param[in] dst Path to the destination file/directory
*/
void copy(const char *src, const char *dst)
{
#define FLAG_SRC_TYPE 0x03
#define FLAG_SRC_IS_DIR 0x01
#define FLAG_SRC_IS_FILE 0x02
#define FLAG_SRC_NON_EXSIT 0x00
#define FLAG_DST_TYPE 0x0C
#define FLAG_DST_IS_DIR 0x04
#define FLAG_DST_IS_FILE 0x08
#define FLAG_DST_NON_EXSIT 0x00
struct stat stat;
uint32_t flag = 0;
/* check the staus of src and dst */
if (dfs_file_lstat(src, &stat) < 0)
{
rt_kprintf("copy failed, bad %s\n", src);
return;
}
if (S_ISDIR(stat.st_mode))
flag |= FLAG_SRC_IS_DIR;
else
flag |= FLAG_SRC_IS_FILE;
if (dfs_file_stat(dst, &stat) < 0)
{
flag |= FLAG_DST_NON_EXSIT;
}
else
{
if (S_ISDIR(stat.st_mode))
flag |= FLAG_DST_IS_DIR;
else
flag |= FLAG_DST_IS_FILE;
}
/* 2. check status */
if ((flag & FLAG_SRC_IS_DIR) && (flag & FLAG_DST_IS_FILE))
{
rt_kprintf("cp faild, cp dir to file is not permitted!\n");
return ;
}
/* 3. do copy */
if (flag & FLAG_SRC_IS_FILE)
{
if (flag & FLAG_DST_IS_DIR)
{
char *fdst;
fdst = dfs_normalize_path(dst, _get_path_lastname(src));
if (fdst == NULL)
{
rt_kprintf("out of memory\n");
return;
}
copyfile(src, fdst);
rt_free(fdst);
}
else
{
copyfile(src, dst);
}
}
else /* flag & FLAG_SRC_IS_DIR */
{
if (flag & FLAG_DST_IS_DIR)
{
char *fdst;
fdst = dfs_normalize_path(dst, _get_path_lastname(src));
if (fdst == NULL)
{
rt_kprintf("out of memory\n");
return;
}
mkdir(fdst, 0);
copydir(src, fdst);
rt_free(fdst);
}
else if ((flag & FLAG_DST_TYPE) == FLAG_DST_NON_EXSIT)
{
mkdir(dst, 0);
copydir(src, dst);
}
else
{
copydir(src, dst);
}
}
}
FINSH_FUNCTION_EXPORT(copy, copy file or dir)
#endif
|