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
|
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 1996-2015. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% %CopyrightEnd%
%%
%% This is a developement feature when developing a new file module,
%% ugly but practical.
-ifndef(FILE_MODULE).
-define(FILE_MODULE, file).
-endif.
-ifndef(FILE_SUITE).
-define(FILE_SUITE, file_SUITE).
-endif.
-ifndef(FILE_INIT).
-define(FILE_INIT(Config), Config).
-endif.
-ifndef(FILE_FINI).
-define(FILE_FINI(Config), Config).
-endif.
-ifndef(FILE_INIT_PER_TESTCASE).
-define(FILE_INIT_PER_TESTCASE(Config), Config).
-endif.
-ifndef(FILE_FIN_PER_TESTCASE).
-define(FILE_FIN_PER_TESTCASE(Config), Config).
-endif.
-module(?FILE_SUITE).
-export([all/0, suite/0,groups/0,init_per_suite/1, end_per_suite/1,
init_per_group/2,end_per_group/2,
init_per_testcase/2, end_per_testcase/2,
read_write_file/1, names/1]).
-export([cur_dir_0/1, cur_dir_1/1, make_del_dir/1,
list_dir/1,list_dir_error/1,
untranslatable_names/1, untranslatable_names_error/1,
pos1/1, pos2/1, pos3/1]).
-export([close/1, consult1/1, path_consult/1, delete/1]).
-export([ eval1/1, path_eval/1, script1/1, path_script/1,
open1/1,
old_modes/1, new_modes/1, path_open/1, open_errors/1]).
-export([ file_info_basic_file/1, file_info_basic_directory/1,
file_info_bad/1, file_info_times/1, file_write_file_info/1]).
-export([rename/1, access/1, truncate/1, datasync/1, sync/1,
read_write/1, pread_write/1, append/1, exclusive/1]).
-export([ e_delete/1, e_rename/1, e_make_dir/1, e_del_dir/1]).
-export([otp_5814/1, otp_10852/1]).
-export([ read_not_really_compressed/1,
read_compressed_cooked/1, read_compressed_cooked_binary/1,
read_cooked_tar_problem/1,
write_compressed/1, compress_errors/1, catenated_gzips/1,
compress_async_crash/1]).
-export([ make_link/1, read_link_info_for_non_link/1, symlinks/1]).
-export([copy/1]).
-export([new_slave/2, old_slave/2, run_test/2]).
-export([delayed_write/1, read_ahead/1, segment_read/1, segment_write/1]).
-export([ipread/1]).
-export([pid2name/1]).
-export([interleaved_read_write/1]).
-export([unicode/1]).
-export([altname/1]).
-export([large_file/0, large_file/1, large_write/1]).
-export([read_line_1/1, read_line_2/1, read_line_3/1,read_line_4/1]).
-export([advise/1]).
-export([allocate/1]).
-export([standard_io/1,mini_server/1]).
-export([old_io_protocol/1]).
-export([unicode_mode/1]).
%% Debug exports
-export([create_file_slow/2, create_file/2, create_bin/2]).
-export([verify_file/2, verify_bin/3]).
-export([bytes/2, iterate/3]).
%% System probe functions that might be handy to check from the shell
-export([disc_free/1, memsize/0]).
-include_lib("common_test/include/ct.hrl").
-include_lib("kernel/include/file.hrl").
-define(THROW_ERROR(RES), throw({fail, ?LINE, RES})).
suite() ->
[{ct_hooks,[ts_install_cth]},
{timetrap,{minutes,1}}].
all() ->
[unicode, altname, read_write_file, {group, dirs},
{group, files}, delete, rename, names, {group, errors},
{group, compression}, {group, links}, copy,
delayed_write, read_ahead, segment_read, segment_write,
ipread, pid2name, interleaved_read_write, otp_5814, otp_10852,
large_file, large_write, read_line_1, read_line_2, read_line_3,
read_line_4, standard_io, old_io_protocol,
unicode_mode
].
groups() ->
[{dirs, [], [make_del_dir, cur_dir_0, cur_dir_1,
list_dir, list_dir_error, untranslatable_names,
untranslatable_names_error]},
{files, [],
[{group, open}, {group, pos}, {group, file_info},
{group, consult}, {group, eval}, {group, script},
truncate, sync, datasync, advise, allocate]},
{open, [],
[open1, old_modes, new_modes, path_open, close, access,
read_write, pread_write, append, open_errors,
exclusive]},
{pos, [], [pos1, pos2, pos3]},
{file_info, [],
[file_info_basic_file, file_info_basic_directory,
file_info_bad, file_info_times, file_write_file_info]},
{consult, [], [consult1, path_consult]},
{eval, [], [eval1, path_eval]},
{script, [], [script1, path_script]},
{errors, [],
[e_delete, e_rename, e_make_dir, e_del_dir]},
{compression, [],
[read_compressed_cooked, read_compressed_cooked_binary,
read_cooked_tar_problem, read_not_really_compressed,
write_compressed, compress_errors, catenated_gzips,
compress_async_crash]},
{links, [],
[make_link, read_link_info_for_non_link, symlinks]}].
init_per_group(_GroupName, Config) ->
Config.
end_per_group(_GroupName, Config) ->
Config.
init_per_suite(Config) when is_list(Config) ->
SaslConfig = case application:start(sasl) of
{error, {already_started, sasl}} ->
[];
ok ->
[{sasl,started}]
end,
application:start(os_mon),
case os:type() of
{win32, _} ->
Priv = ?config(priv_dir, Config),
HasAccessTime =
case ?FILE_MODULE:read_file_info(Priv) of
{ok, #file_info{atime={_, {0, 0, 0}}}} ->
%% This is a unfortunately a FAT file system.
[no_access_time];
{ok, _} ->
[]
end,
?FILE_INIT(HasAccessTime++Config++SaslConfig);
_ ->
?FILE_INIT(Config++SaslConfig)
end.
end_per_suite(Config) when is_list(Config) ->
case os:type() of
{win32, _} ->
os:cmd("subst z: /d");
_ ->
ok
end,
application:stop(os_mon),
case proplists:get_value(sasl, Config) of
started ->
application:stop(sasl);
_Else ->
ok
end,
?FILE_FINI(Config).
init_per_testcase(_Func, Config) ->
%%error_logger:info_msg("~p:~p *****~n", [?MODULE, _Func]),
?FILE_INIT_PER_TESTCASE(Config).
end_per_testcase(_Func, Config) ->
%% error_logger:info_msg("~p:~p END *****~n", [?MODULE, _Func]),
?FILE_FIN_PER_TESTCASE(Config).
%% Matches a term (the last) against alternatives
expect(X, _, X) ->
X;
expect(_, X, X) ->
X.
expect(X, _, _, X) ->
X;
expect(_, X, _, X) ->
X;
expect(_, _, X, X) ->
X.
expect(X, _, _, _, X) ->
X;
expect(_, X, _, _, X) ->
X;
expect(_, _, X, _, X) ->
X;
expect(_, _, _, X, X) ->
X.
%% Calculate the time difference
time_dist({YY, MM, DD, H, M, S}, DT) ->
time_dist({{YY, MM, DD}, {H, M, S}}, DT);
time_dist(DT, {YY, MM, DD, H, M, S}) ->
time_dist(DT, {{YY, MM, DD}, {H, M, S}});
time_dist({_D1, _T1} = DT1, {_D2, _T2} = DT2) ->
calendar:datetime_to_gregorian_seconds(DT2)
- calendar:datetime_to_gregorian_seconds(DT1).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
mini_server(Parent) ->
receive
die ->
ok;
{io_request,From,To,{put_chars,_Encoding,Data}} ->
Parent ! {io_request,From,To,{put_chars,Data}},
From ! {io_reply, To, ok},
mini_server(Parent);
{io_request,From,To,{get_chars,'',N}} ->
Parent ! {io_request,From,To,{get_chars,'',N}},
From ! {io_reply, To, {ok, lists:duplicate(N,$a)}},
mini_server(Parent);
{io_request,From,To,{get_line,''}} ->
Parent ! {io_request,From,To,{get_line,''}},
From ! {io_reply, To, {ok, "hej\n"}},
mini_server(Parent)
end.
standard_io(suite) ->
[];
standard_io(doc) ->
["Test that standard i/o-servers work with file module"];
standard_io(Config) when is_list(Config) ->
%% Really just a smoke test
?line Pid = spawn(?MODULE,mini_server,[self()]),
?line register(mini_server,Pid),
?line ok = file:write(mini_server,<<"hej\n">>),
?line receive
{io_request,_,_,{put_chars,<<"hej\n">>}} ->
ok
after 1000 ->
exit(noreply)
end,
?line {ok,"aaaaa"} = file:read(mini_server,5),
?line receive
{io_request,_,_,{get_chars,'',5}} ->
ok
after 1000 ->
exit(noreply)
end,
?line {ok,"hej\n"} = file:read_line(mini_server),
?line receive
{io_request,_,_,{get_line,''}} ->
ok
after 1000 ->
exit(noreply)
end,
?line OldGL = group_leader(),
?line group_leader(Pid,self()),
?line ok = file:write(standard_io,<<"hej\n">>),
?line group_leader(OldGL,self()),
?line receive
{io_request,_,_,{put_chars,<<"hej\n">>}} ->
ok
after 1000 ->
exit(noreply)
end,
?line group_leader(Pid,self()),
?line {ok,"aaaaa"} = file:read(standard_io,5),
?line group_leader(OldGL,self()),
?line receive
{io_request,_,_,{get_chars,'',5}} ->
ok
after 1000 ->
exit(noreply)
end,
?line group_leader(Pid,self()),
?line {ok,"hej\n"} = file:read_line(standard_io),
?line group_leader(OldGL,self()),
?line receive
{io_request,_,_,{get_line,''}} ->
ok
after 1000 ->
exit(noreply)
end,
Pid ! die,
receive after 1000 -> ok end.
old_io_protocol(suite) ->
[];
old_io_protocol(doc) ->
["Test that the old file IO protocol =< R16B still works"];
old_io_protocol(Config) when is_list(Config) ->
RootDir = ?config(priv_dir,Config),
Name = filename:join(RootDir,
atom_to_list(?MODULE)
++"old_io_protocol.fil"),
MyData = "0123456789abcdefghijklmnopqrstuvxyz",
ok = ?FILE_MODULE:write_file(Name, MyData),
{ok, Fd} = ?FILE_MODULE:open(Name, write),
Fd ! {file_request,self(),Fd,truncate},
receive
{file_reply,Fd,ok} -> ok
end,
ok = ?FILE_MODULE:close(Fd),
{ok, <<>>} = ?FILE_MODULE:read_file(Name),
[] = flush(),
ok.
unicode_mode(suite) -> [];
unicode_mode(doc) -> [""];
unicode_mode(Config) ->
Dir = {dir, ?config(priv_dir,Config)},
OptVariants = [[Dir],
[Dir, {encoding, utf8}],
[Dir, binary],
[Dir, binary, {encoding, utf8}]
],
ReadVariants = [{read, fun(Fd) -> um_read(Fd, fun(Fd1) -> file:read(Fd1, 1024) end) end},
{read_line, fun(Fd) -> um_read(Fd, fun(Fd1) -> file:read_line(Fd1) end) end}
%%{pread, fun(Fd) -> file:pread(Fd, 0, 1024) end},
%%{preadl, fun(Fd) -> file:pread(Fd, [{0, 1024}]) end},
],
_ = [read_write_0("ASCII: list: Hello World", Read, Opt) ||
Opt <- OptVariants, Read <- ReadVariants],
_ = [read_write_0("LATIN1: list: åäöÅÄÖ", Read, Opt) ||
Opt <- OptVariants, Read <- ReadVariants],
_ = [read_write_0(<<"ASCII: bin: Hello World">>, Read, Opt) ||
Opt <- OptVariants, Read <- ReadVariants],
_ = [read_write_0(<<"LATIN1: bin: åäöÅÄÖ">>, Read, Opt) ||
Opt <- OptVariants, Read <- ReadVariants],
%% These will be double encoded if option is encoding utf-8
_ = [read_write_0(<<"UTF8: bin: Ωß"/utf8>>, Read, Opt) ||
Opt <- OptVariants, Read <- ReadVariants],
%% These should not work (with encoding set to utf-8)
%% according to file's documentation
_ = [read_write_0("UTF8: list: Ωß", Read, Opt) ||
Opt <- OptVariants, Read <- ReadVariants],
ok.
read_write_0(Str, {Func, ReadFun}, Options) ->
try
Res = read_write_1(Str, ReadFun, Options),
io:format("~p: ~ts ~p '~p'~n", [Func, Str, tl(Options), Res]),
ok
catch {fail, Line, ReadBytes = [_|_]} ->
io:format("~p:~p: ~p ERROR: ~w vs~n ~w~n - ~p~n",
[?MODULE, Line, Func, Str, ReadBytes, Options]),
exit({error, ?LINE});
{fail, Line, ReadBytes} ->
io:format("~p:~p: ~p ERROR: ~ts vs~n ~w~n - ~p~n",
[?MODULE, Line, Func, Str, ReadBytes, Options]),
exit({error, ?LINE});
error:What ->
io:format("~p:??: ~p ERROR: ~p from~n ~w~n ~p~n",
[?MODULE, Func, What, Str, Options]),
io:format("\t~p~n", [erlang:get_stacktrace()]),
exit({error, ?LINE})
end.
read_write_1(Str0, ReadFun, [{dir,Dir}|Options]) ->
File = um_filename(Str0, Dir, Options),
Pre = "line 1\n", Post = "\nlast line\n",
Str = case is_list(Str0) andalso lists:max(Str0) > 255 of
false -> %% Normal case Use options
{ok, FdW} = file:open(File, [write|Options]),
IO = [Pre, Str0, Post],
ok = file:write(FdW, IO),
case is_binary(Str0) of
true -> iolist_to_binary(IO);
false -> lists:append(IO)
end;
true -> %% Test unicode lists
{ok, FdW} = file:open(File, [write]),
Utf8 = unicode:characters_to_binary([Pre, Str0, Post]),
file:write(FdW, Utf8),
{unicode, Utf8}
end,
file:close(FdW),
{ok, FdR} = file:open(File, [read|Options]),
ReadRes = ReadFun(FdR),
file:close(FdR),
Res = um_check(Str, ReadRes, Options),
file:delete(File),
Res.
um_read(Fd, Fun) ->
um_read(Fd, Fun, []).
um_read(Fd, Fun, Acc) ->
case Fun(Fd) of
eof ->
case is_binary(hd(Acc)) of
true -> {ok, iolist_to_binary(lists:reverse(Acc))};
false -> {ok, lists:append(lists:reverse(Acc))}
end;
{ok, Data} ->
um_read(Fd, Fun, [Data|Acc]);
Error ->
Error
end.
um_check(Str, {ok, Str}, _) -> ok;
um_check(Bin, {ok, Res}, _Options) when is_binary(Bin), is_list(Res) ->
case list_to_binary(Res) of
Bin -> ok;
_ -> ?THROW_ERROR(Res)
end;
um_check(Str, {ok, Res}, _Options) when is_list(Str), is_binary(Res) ->
case iolist_to_binary(Str) of
Res -> ok;
_ -> ?THROW_ERROR(Res)
end;
um_check({unicode, Utf8Bin}, Res, Options) ->
um_check_unicode(Utf8Bin, Res,
proplists:get_value(binary, Options, false),
proplists:get_value(encoding, Options, none));
um_check(_Str, Res, _Options) ->
?THROW_ERROR(Res).
um_check_unicode(Utf8Bin, {ok, Utf8Bin}, true, none) ->
ok;
um_check_unicode(Utf8Bin, {ok, List = [_|_]}, false, none) ->
case binary_to_list(Utf8Bin) == List of
true -> ok;
false -> ?THROW_ERROR(List)
end;
um_check_unicode(_Utf8Bin, {error, {no_translation, unicode, latin1}}, _, _) ->
no_translation;
um_check_unicode(_Utf8Bin, Error = {error, _}, _, _Unicode) ->
?THROW_ERROR(Error);
um_check_unicode(_Utf8Bin, {ok, _ListOrBin}, _, _UTF8_) ->
%% List = if is_binary(ListOrBin) -> unicode:characters_to_list(ListOrBin);
%% true -> ListOrBin
%% end,
%% io:format("In: ~w~n", [binary_to_list(Utf8Bin)]),
%% io:format("Ut: ~w~n", [List]),
?THROW_ERROR({shoud_be, no_translation}).
um_filename(Bin, Dir, Options) when is_binary(Bin) ->
um_filename(binary_to_list(Bin), Dir, Options);
um_filename(Str = [_|_], Dir, Options) ->
Name = hd(string:tokens(Str, ":")),
Enc = atom_to_list(proplists:get_value(encoding, Options, latin1)),
File = case lists:member(binary, Options) of
true ->
"test_" ++ Name ++ "_bin_enc_" ++ Enc;
false ->
"test_" ++ Name ++ "_list_enc_" ++ Enc
end,
filename:join(Dir, File).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
read_write_file(suite) -> [];
read_write_file(doc) -> [];
read_write_file(Config) when is_list(Config) ->
?line RootDir = ?config(priv_dir,Config),
?line Name = filename:join(RootDir,
atom_to_list(?MODULE)
++"_read_write_file"),
%% Try writing and reading back some term
?line SomeTerm = {"This term",{will,be},[written,$t,$o],1,file,[]},
?line ok = ?FILE_MODULE:write_file(Name,term_to_binary(SomeTerm)),
?line {ok,Bin1} = ?FILE_MODULE:read_file(Name),
?line SomeTerm = binary_to_term(Bin1),
%% Try a "null" term
?line NullTerm = [],
?line ok = ?FILE_MODULE:write_file(Name,term_to_binary(NullTerm)),
?line {ok,Bin2} = ?FILE_MODULE:read_file(Name),
?line NullTerm = binary_to_term(Bin2),
%% Try some "complicated" types
?line BigNum = 123456789012345678901234567890,
?line ComplTerm = {self(),make_ref(),BigNum,3.14159},
?line ok = ?FILE_MODULE:write_file(Name,term_to_binary(ComplTerm)),
?line {ok,Bin3} = ?FILE_MODULE:read_file(Name),
?line ComplTerm = binary_to_term(Bin3),
%% Try reading a nonexistent file
?line Name2 = filename:join(RootDir,
atom_to_list(?MODULE)
++"_nonexistent_file"),
?line {error, enoent} = ?FILE_MODULE:read_file(Name2),
?line {error, enoent} = ?FILE_MODULE:read_file(""),
?line {error, enoent} = ?FILE_MODULE:read_file(''),
% Try writing to a bad filename
?line {error, enoent} =
?FILE_MODULE:write_file("",term_to_binary(NullTerm)),
% Try writing something else than a binary
?line {error, badarg} = ?FILE_MODULE:write_file(Name,{1,2,3}),
?line {error, badarg} = ?FILE_MODULE:write_file(Name,self()),
%% Some non-term binaries
?line ok = ?FILE_MODULE:write_file(Name,[]),
?line {ok,Bin4} = ?FILE_MODULE:read_file(Name),
?line 0 = byte_size(Bin4),
?line ok = ?FILE_MODULE:write_file(Name,[Bin1,[],[[Bin2]]]),
?line {ok,Bin5} = ?FILE_MODULE:read_file(Name),
?line {Bin1,Bin2} = split_binary(Bin5,byte_size(Bin1)),
?line [] = flush(),
ok.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
make_del_dir(suite) -> [];
make_del_dir(doc) -> [];
make_del_dir(Config) when is_list(Config) ->
?line RootDir = ?config(priv_dir,Config),
?line NewDir = filename:join(RootDir,
atom_to_list(?MODULE)
++"_mk-dir"),
?line ok = ?FILE_MODULE:make_dir(NewDir),
?line {error, eexist} = ?FILE_MODULE:make_dir(NewDir),
?line ok = ?FILE_MODULE:del_dir(NewDir),
?line {error, enoent} = ?FILE_MODULE:del_dir(NewDir),
% Make sure we are not in a directory directly under test_server
% as that would result in eacces errors when trying to delete '..',
% because there are processes having that directory as current.
?line ok = ?FILE_MODULE:make_dir(NewDir),
?line {ok,CurrentDir} = file:get_cwd(),
case {os:type(), length(NewDir) >= 260 } of
{{win32,_}, true} ->
io:format("Skip set_cwd for windows path longer than 260 (MAX_PATH)\n", []),
io:format("\nNewDir = ~p\n", [NewDir]);
_ ->
?line ok = ?FILE_MODULE:set_cwd(NewDir)
end,
try
%% Check that we get an error when trying to create...
%% a deep directory
?line NewDir2 = filename:join(RootDir,
atom_to_list(?MODULE)
++"_mk-dir-noexist/foo"),
?line {error, enoent} = ?FILE_MODULE:make_dir(NewDir2),
%% a nameless directory
?line {error, enoent} = ?FILE_MODULE:make_dir(""),
%% a directory with illegal name
?line {error, badarg} = ?FILE_MODULE:make_dir({1,2,3}),
%% a directory with illegal name, even if it's a (bad) list
?line {error, badarg} = ?FILE_MODULE:make_dir([1,2,3,{}]),
%% Maybe this isn't an error, exactly, but worth mentioning anyway:
%% ok = ?FILE_MODULE:make_dir([$f,$o,$o,0,$b,$a,$r])),
%% The above line works, and created a directory "./foo"
%% More elegant would maybe have been to fail, or to really create
%% a directory, but with a name that incorporates the "bar" part of
%% the list, so that [$f,$o,$o,0,$f,$o,$o] wouldn't refer to the same
%% dir. But this would slow it down.
%% Try deleting some bad directories
%% Deleting the parent directory to the current, sounds dangerous, huh?
%% Don't worry ;-) the parent directory should never be empty, right?
?line case ?FILE_MODULE:del_dir('..') of
{error, eexist} -> ok;
{error, eacces} -> ok; %OpenBSD
{error, einval} -> ok %FreeBSD
end,
?line {error, enoent} = ?FILE_MODULE:del_dir(""),
?line {error, badarg} = ?FILE_MODULE:del_dir([3,2,1,{}]),
?line [] = flush()
after
?FILE_MODULE:set_cwd(CurrentDir)
end,
ok.
cur_dir_0(suite) -> [];
cur_dir_0(doc) -> [];
cur_dir_0(Config) when is_list(Config) ->
%% Find out the current dir, and cd to it ;-)
?line {ok,BaseDir} = ?FILE_MODULE:get_cwd(),
?line Dir1 = BaseDir ++ "", %% Check that it's a string
?line ok = ?FILE_MODULE:set_cwd(Dir1),
%% Make a new dir, and cd to that
?line RootDir = ?config(priv_dir,Config),
?line NewDir = filename:join(RootDir,
atom_to_list(?MODULE)
++"_curdir"),
?line ok = ?FILE_MODULE:make_dir(NewDir),
case {os:type(), length(NewDir) >= 260} of
{{win32,_}, true} ->
io:format("Skip set_cwd for windows path longer than 260 (MAX_PATH):\n"),
io:format("\nNewDir = ~p\n", [NewDir]);
_ ->
io:format("cd to ~s",[NewDir]),
ok = ?FILE_MODULE:set_cwd(NewDir),
%% Create a file in the new current directory, and check that it
%% really is created there
UncommonName = "uncommon.fil",
{ok,Fd} = ?FILE_MODULE:open(UncommonName,read_write),
ok = ?FILE_MODULE:close(Fd),
{ok,NewDirFiles} = ?FILE_MODULE:list_dir("."),
true = lists:member(UncommonName,NewDirFiles),
%% Delete the directory and return to the old current directory
%% and check that the created file isn't there (too!)
expect({error, einval}, {error, eacces},
?FILE_MODULE:del_dir(NewDir)),
?FILE_MODULE:delete(UncommonName),
{ok,[]} = ?FILE_MODULE:list_dir("."),
ok = ?FILE_MODULE:set_cwd(Dir1),
io:format("cd back to ~s",[Dir1]),
ok = ?FILE_MODULE:del_dir(NewDir),
{error, enoent} = ?FILE_MODULE:set_cwd(NewDir),
ok = ?FILE_MODULE:set_cwd(Dir1),
io:format("cd back to ~s",[Dir1]),
{ok,OldDirFiles} = ?FILE_MODULE:list_dir("."),
false = lists:member(UncommonName,OldDirFiles)
end,
%% Try doing some bad things
?line {error, badarg} = ?FILE_MODULE:set_cwd({foo,bar}),
?line {error, enoent} = ?FILE_MODULE:set_cwd(""),
?line {error, enoent} = ?FILE_MODULE:set_cwd(".......a......"),
?line {ok,BaseDir} = ?FILE_MODULE:get_cwd(), %% Still there?
%% On Windows, there should only be slashes, no backslashes,
%% in the return value of get_cwd().
%% (The test is harmless on Unix, because filenames usually
%% don't contain backslashes.)
?line {ok, BaseDir} = ?FILE_MODULE:get_cwd(),
?line false = lists:member($\\, BaseDir),
?line [] = flush(),
ok.
%% Tests ?FILE_MODULE:get_cwd/1.
cur_dir_1(suite) -> [];
cur_dir_1(doc) -> [];
cur_dir_1(Config) when is_list(Config) ->
?line case os:type() of
{win32, _} ->
win_cur_dir_1(Config);
_ ->
?line {error, enotsup} = ?FILE_MODULE:get_cwd("d:")
end,
?line [] = flush(),
ok.
win_cur_dir_1(_Config) ->
?line {ok,BaseDir} = ?FILE_MODULE:get_cwd(),
%% Get the drive letter from the current directory,
%% and try to get current directory for that drive.
?line [Drive,$:|_] = BaseDir,
?line {ok,BaseDir} = ?FILE_MODULE:get_cwd([Drive,$:]),
io:format("BaseDir = ~s\n", [BaseDir]),
%% Unfortunately, there is no way to move away from the
%% current drive as we can't use the "subst" command from
%% a SSH connection. We can't test any more.
ok.
%%%
%%% Test list_dir() on a non-existing pathname.
%%%
list_dir_error(Config) ->
Priv = ?config(priv_dir, Config),
NonExisting = filename:join(Priv, "non-existing-dir"),
{error,enoent} = ?FILE_MODULE:list_dir(NonExisting),
ok.
%%%
%%% Test list_dir() and list_dir_all().
%%%
list_dir(Config) ->
RootDir = ?config(priv_dir, Config),
TestDir = filename:join(RootDir, ?MODULE_STRING++"_list_dir"),
?FILE_MODULE:make_dir(TestDir),
list_dir_1(TestDir, 42, []).
list_dir_1(TestDir, 0, Sorted) ->
[ok = ?FILE_MODULE:delete(filename:join(TestDir, F)) ||
F <- Sorted],
ok = ?FILE_MODULE:del_dir(TestDir);
list_dir_1(TestDir, Cnt, Sorted0) ->
Base = "file" ++ integer_to_list(Cnt),
Name = filename:join(TestDir, Base),
ok = ?FILE_MODULE:write_file(Name, Base),
Sorted = lists:merge([Base], Sorted0),
{ok,DirList0} = ?FILE_MODULE:list_dir(TestDir),
{ok,DirList1} = ?FILE_MODULE:list_dir_all(TestDir),
Sorted = lists:sort(DirList0),
Sorted = lists:sort(DirList1),
list_dir_1(TestDir, Cnt-1, Sorted).
untranslatable_names(Config) ->
case no_untranslatable_names() of
true ->
{skip,"Not a problem on this OS"};
false ->
untranslatable_names_1(Config)
end.
untranslatable_names_1(Config) ->
{ok,OldCwd} = file:get_cwd(),
PrivDir = ?config(priv_dir, Config),
Dir = filename:join(PrivDir, "untranslatable_names"),
ok = file:make_dir(Dir),
Node = start_node(untranslatable_names, "+fnu"),
try
ok = file:set_cwd(Dir),
[ok = file:write_file(F, F) || {_,F} <- untranslatable_names()],
ExpectedListDir0 = [unicode:characters_to_list(N, utf8) ||
{utf8,N} <- untranslatable_names()],
ExpectedListDir = lists:sort(ExpectedListDir0),
io:format("ExpectedListDir: ~p\n", [ExpectedListDir]),
ExpectedListDir = call_and_sort(Node, file, list_dir, [Dir]),
ExpectedListDirAll0 = [case Enc of
utf8 ->
unicode:characters_to_list(N, utf8);
latin1 ->
N
end || {Enc,N} <- untranslatable_names()],
ExpectedListDirAll = lists:sort(ExpectedListDirAll0),
io:format("ExpectedListDirAll: ~p\n", [ExpectedListDirAll]),
ExpectedListDirAll = call_and_sort(Node, file, list_dir_all, [Dir])
after
catch test_server:stop_node(Node),
file:set_cwd(OldCwd),
[file:delete(F) || {_,F} <- untranslatable_names()],
file:del_dir(Dir)
end,
ok.
untranslatable_names_error(Config) ->
case no_untranslatable_names() of
true ->
{skip,"Not a problem on this OS"};
false ->
untranslatable_names_error_1(Config)
end.
untranslatable_names_error_1(Config) ->
{ok,OldCwd} = file:get_cwd(),
PrivDir = ?config(priv_dir, Config),
Dir = filename:join(PrivDir, "untranslatable_names_error"),
ok = file:make_dir(Dir),
Node = start_node(untranslatable_names, "+fnue"),
try
ok = file:set_cwd(Dir),
[ok = file:write_file(F, F) || {_,F} <- untranslatable_names()],
ExpectedListDir0 = [unicode:characters_to_list(N, utf8) ||
{utf8,N} <- untranslatable_names()],
ExpectedListDir = lists:sort(ExpectedListDir0),
io:format("ExpectedListDir: ~p\n", [ExpectedListDir]),
{error,{no_translation,BadFile}} =
rpc:call(Node, file, list_dir, [Dir]),
true = lists:keymember(BadFile, 2, untranslatable_names())
after
catch test_server:stop_node(Node),
file:set_cwd(OldCwd),
[file:delete(F) || {_,F} <- untranslatable_names()],
file:del_dir(Dir)
end,
ok.
untranslatable_names() ->
[{utf8,<<"abc">>},
{utf8,<<"def">>},
{utf8,<<"Lagerl",195,182,"f">>},
{utf8,<<195,150,"stra Emterwik">>},
{latin1,<<"M",229,"rbacka">>},
{latin1,<<"V",228,"rmland">>}].
call_and_sort(Node, M, F, A) ->
{ok,Res} = rpc:call(Node, M, F, A),
lists:sort(Res).
no_untranslatable_names() ->
case os:type() of
{unix,darwin} -> true;
{win32,_} -> true;
_ -> false
end.
start_node(Name, Args) ->
[_,Host] = string:tokens(atom_to_list(node()), "@"),
ct:log("Trying to start ~w@~s~n", [Name,Host]),
case test_server:start_node(Name, peer, [{args,Args}]) of
{error,Reason} ->
test_server:fail(Reason);
{ok,Node} ->
ct:log("Node ~p started~n", [Node]),
Node
end.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
open1(suite) -> [];
open1(doc) -> [];
open1(Config) when is_list(Config) ->
?line RootDir = ?config(priv_dir,Config),
?line NewDir = filename:join(RootDir,
atom_to_list(?MODULE)
++"_files"),
?line ok = ?FILE_MODULE:make_dir(NewDir),
?line Name = filename:join(NewDir, "foo1.fil"),
?line {ok,Fd1} = ?FILE_MODULE:open(Name,read_write),
?line {ok,Fd2} = ?FILE_MODULE:open(Name,read),
?line Str = "{a,tuple}.\n",
?line io:format(Fd1,Str,[]),
?line {ok,0} = ?FILE_MODULE:position(Fd1,bof),
?line Str = io:get_line(Fd1,''),
?line Str = io:get_line(Fd2,''),
?line ok = ?FILE_MODULE:close(Fd2),
?line {ok,0} = ?FILE_MODULE:position(Fd1,bof),
?line ok = ?FILE_MODULE:truncate(Fd1),
?line eof = io:get_line(Fd1,''),
?line ok = ?FILE_MODULE:close(Fd1),
?line {ok,Fd3} = ?FILE_MODULE:open(Name,read),
?line eof = io:get_line(Fd3,''),
?line ok = ?FILE_MODULE:close(Fd3),
?line [] = flush(),
ok.
%% Tests all open modes.
old_modes(suite) -> [];
old_modes(doc) -> [];
old_modes(Config) when is_list(Config) ->
?line RootDir = ?config(priv_dir, Config),
?line NewDir = filename:join(RootDir,
atom_to_list(?MODULE)
++"_old_open_modes"),
?line ok = ?FILE_MODULE:make_dir(NewDir),
?line Name1 = filename:join(NewDir, "foo1.fil"),
?line Marker = "hello, world",
%% write
?line {ok, Fd1} = ?FILE_MODULE:open(Name1, write),
?line ok = io:write(Fd1, Marker),
?line ok = io:put_chars(Fd1, ".\n"),
?line ok = ?FILE_MODULE:close(Fd1),
%% read
?line {ok, Fd2} = ?FILE_MODULE:open(Name1, read),
?line {ok, Marker} = io:read(Fd2, prompt),
?line ok = ?FILE_MODULE:close(Fd2),
%% read_write
?line {ok, Fd3} = ?FILE_MODULE:open(Name1, read_write),
?line {ok, Marker} = io:read(Fd3, prompt),
?line ok = io:write(Fd3, Marker),
?line ok = ?FILE_MODULE:close(Fd3),
?line [] = flush(),
ok.
new_modes(suite) -> [];
new_modes(doc) -> [];
new_modes(Config) when is_list(Config) ->
?line RootDir = ?config(priv_dir, Config),
?line NewDir = filename:join(RootDir,
atom_to_list(?MODULE)
++"_new_open_modes"),
?line ok = ?FILE_MODULE:make_dir(NewDir),
?line Name1 = filename:join(NewDir, "foo1.fil"),
?line Marker = "hello, world",
%% write
?line {ok, Fd1} = ?FILE_MODULE:open(Name1, [write]),
?line ok = io:write(Fd1, Marker),
?line ok = io:put_chars(Fd1, ".\n"),
?line ok = ?FILE_MODULE:close(Fd1),
%% read
?line {ok, Fd2} = ?FILE_MODULE:open(Name1, [read]),
?line {ok, Marker} = io:read(Fd2, prompt),
?line ok = ?FILE_MODULE:close(Fd2),
%% read and write
?line {ok, Fd3} = ?FILE_MODULE:open(Name1, [read, write]),
?line {ok, Marker} = io:read(Fd3, prompt),
?line ok = io:write(Fd3, Marker),
?line ok = ?FILE_MODULE:close(Fd3),
%% read by default
?line {ok, Fd4} = ?FILE_MODULE:open(Name1, []),
?line {ok, Marker} = io:read(Fd4, prompt),
?line ok = ?FILE_MODULE:close(Fd4),
%% read and binary
?line {ok, Fd5} = ?FILE_MODULE:open(Name1, [read, binary]),
?line {ok, Marker} = io:read(Fd5, prompt),
?line ok = ?FILE_MODULE:close(Fd5),
%% read, raw
?line {ok, Fd6} = ?FILE_MODULE:open(Name1, [read, raw]),
?line {ok, [$\[]} = ?FILE_MODULE:read(Fd6, 1),
?line ok = ?FILE_MODULE:close(Fd6),
%% write and sync
case ?FILE_MODULE:open(Name1, [write, sync]) of
{ok, Fd7} ->
ok = io:write(Fd7, Marker),
ok = io:put_chars(Fd7, ".\n"),
ok = ?FILE_MODULE:close(Fd7),
{ok, Fd8} = ?FILE_MODULE:open(Name1, [read]),
{ok, Marker} = io:read(Fd8, prompt),
ok = ?FILE_MODULE:close(Fd8);
{error, enotsup} ->
%% for platforms that don't support the sync option
ok
end,
?line [] = flush(),
ok.
path_open(suite) -> [];
path_open(doc) -> [];
path_open(Config) when is_list(Config) ->
?line RootDir = ?config(priv_dir,Config),
?line NewDir = filename:join(RootDir,
atom_to_list(?MODULE)
++"_path_open"),
?line ok = ?FILE_MODULE:make_dir(NewDir),
?line FileName = "path_open.fil",
?line Name = filename:join(RootDir, FileName),
?line {ok,Fd1,_FullName1} =
?FILE_MODULE:path_open(
[RootDir,
"nosuch1",
NewDir],FileName,write),
?line io:format(Fd1,"ABCDEFGH",[]),
?line ok = ?FILE_MODULE:close(Fd1),
%% locate it in the last dir
?line {ok,Fd2,_FullName2} =
?FILE_MODULE:path_open(
["nosuch1",
NewDir,
RootDir],FileName,read),
?line {ok,2} =
?FILE_MODULE:position(Fd2,2), "C" = io:get_chars(Fd2,'',1),
?line ok = ?FILE_MODULE:close(Fd2),
%% Try a failing path
?line {error, enoent} = ?FILE_MODULE:path_open(
["nosuch1",
NewDir],FileName,read),
%% Check that it's found regardless of path, if an absolute name given
?line {ok,Fd3,_FullPath3} =
?FILE_MODULE:path_open(
["nosuch1",
NewDir],Name,read),
?line {ok,2} =
?FILE_MODULE:position(Fd3,2), "C" = io:get_chars(Fd3,'',1),
?line ok = ?FILE_MODULE:close(Fd3),
?line [] = flush(),
ok.
close(suite) -> [];
close(doc) -> [];
close(Config) when is_list(Config) ->
?line RootDir = ?config(priv_dir,Config),
?line Name = filename:join(RootDir,
atom_to_list(?MODULE)
++"_close.fil"),
?line {ok,Fd1} = ?FILE_MODULE:open(Name,read_write),
%% Just closing it is no fun, we did that a million times already
%% This is a common error, for code written before Erlang 4.3
%% bacause then ?FILE_MODULE:open just returned a Pid, and not everyone
%% really checked what they got.
?line {'EXIT',_Msg} = (catch ok = ?FILE_MODULE:close({ok,Fd1})),
?line ok = ?FILE_MODULE:close(Fd1),
%% Try closing one more time
?line Val = ?FILE_MODULE:close(Fd1),
?line io:format("Second close gave: ~p",[Val]),
?line [] = flush(),
ok.
access(suite) -> [];
access(doc) -> [];
access(Config) when is_list(Config) ->
?line RootDir = ?config(priv_dir,Config),
?line Name = filename:join(RootDir,
atom_to_list(?MODULE)
++"_access.fil"),
?line Str = "ABCDEFGH",
?line {ok,Fd1} = ?FILE_MODULE:open(Name,write),
?line io:format(Fd1,Str,[]),
?line ok = ?FILE_MODULE:close(Fd1),
%% Check that we can't write when in read only mode
?line {ok,Fd2} = ?FILE_MODULE:open(Name,read),
?line case catch io:format(Fd2,"XXXX",[]) of
ok ->
test_server:fail({format,write});
_ ->
ok
end,
?line ok = ?FILE_MODULE:close(Fd2),
?line {ok,Fd3} = ?FILE_MODULE:open(Name,read),
?line Str = io:get_line(Fd3,''),
?line ok = ?FILE_MODULE:close(Fd3),
?line [] = flush(),
ok.
%% Tests ?FILE_MODULE:read/2 and ?FILE_MODULE:write/2.
read_write(suite) -> [];
read_write(doc) -> [];
read_write(Config) when is_list(Config) ->
?line RootDir = ?config(priv_dir, Config),
?line NewDir = filename:join(RootDir,
atom_to_list(?MODULE)
++"_read_write"),
?line ok = ?FILE_MODULE:make_dir(NewDir),
?line Marker = "hello, world",
?line MarkerB = list_to_binary(Marker),
%% Plain file.
?line Name1 = filename:join(NewDir, "plain.fil"),
?line {ok, Fd1} = ?FILE_MODULE:open(Name1, [read, write]),
?line read_write_test(Fd1, Marker, []),
%% Raw file.
?line Name2 = filename:join(NewDir, "raw.fil"),
?line {ok, Fd2} = ?FILE_MODULE:open(Name2, [read, write, raw]),
?line read_write_test(Fd2, Marker, []),
%% Plain binary file.
?line Name3 = filename:join(NewDir, "plain-b.fil"),
?line {ok, Fd3} = ?FILE_MODULE:open(Name3, [read, write, binary]),
?line read_write_test(Fd3, MarkerB, <<>>),
%% Raw binary file.
?line Name4 = filename:join(NewDir, "raw-b.fil"),
?line {ok, Fd4} = ?FILE_MODULE:open(Name4, [read, write, raw, binary]),
?line read_write_test(Fd4, MarkerB, <<>>),
ok.
read_write_test(File, Marker, Empty) ->
?line ok = ?FILE_MODULE:write(File, Marker),
?line {ok, 0} = ?FILE_MODULE:position(File, 0),
?line {ok, Empty} = ?FILE_MODULE:read(File, 0),
?line {ok, Marker} = ?FILE_MODULE:read(File, 100),
?line eof = ?FILE_MODULE:read(File, 100),
?line {ok, Empty} = ?FILE_MODULE:read(File, 0),
?line ok = ?FILE_MODULE:close(File),
?line [] = flush(),
ok.
%% Tests ?FILE_MODULE:pread/2 and ?FILE_MODULE:pwrite/2.
pread_write(suite) -> [];
pread_write(doc) -> [];
pread_write(Config) when is_list(Config) ->
?line RootDir = ?config(priv_dir, Config),
?line NewDir = filename:join(RootDir,
atom_to_list(?MODULE)
++"_pread_write"),
?line ok = ?FILE_MODULE:make_dir(NewDir),
?line List = "hello, world",
?line Bin = list_to_binary(List),
%% Plain file.
?line Name1 = filename:join(NewDir, "plain.fil"),
?line {ok, Fd1} = ?FILE_MODULE:open(Name1, [read, write]),
?line pread_write_test(Fd1, List),
%% Raw file.
?line Name2 = filename:join(NewDir, "raw.fil"),
?line {ok, Fd2} = ?FILE_MODULE:open(Name2, [read, write, raw]),
?line pread_write_test(Fd2, List),
%% Plain file. Binary mode.
?line Name3 = filename:join(NewDir, "plain-binary.fil"),
?line {ok, Fd3} = ?FILE_MODULE:open(Name3, [binary, read, write]),
?line pread_write_test(Fd3, Bin),
%% Raw file. Binary mode.
?line Name4 = filename:join(NewDir, "raw-binary.fil"),
?line {ok, Fd4} = ?FILE_MODULE:open(Name4, [binary, read, write, raw]),
?line pread_write_test(Fd4, Bin),
ok.
pread_write_test(File, Data) ->
?line io:format("~p:pread_write_test(~p,~p)~n", [?MODULE, File, Data]),
?line Size = if is_binary(Data) -> byte_size(Data);
is_list(Data) -> length(Data)
end,
?line I = Size + 17,
?line ok = ?FILE_MODULE:pwrite(File, 0, Data),
Res = ?FILE_MODULE:pread(File, 0, I),
?line {ok, Data} = Res,
?line eof = ?FILE_MODULE:pread(File, I, 1),
?line ok = ?FILE_MODULE:pwrite(File, [{0, Data}, {I, Data}]),
?line {ok, [Data, eof, Data]} =
?FILE_MODULE:pread(File, [{0, Size}, {2*I, 1}, {I, Size}]),
?line Plist = lists:seq(21*I, 0, -I),
?line Pwrite = lists:map(fun(P)->{P,Data}end, Plist),
?line Pread = [{22*I,Size} | lists:map(fun(P)->{P,Size}end, Plist)],
?line Presult = [eof | lists:map(fun(_)->Data end, Plist)],
?line ok = ?FILE_MODULE:pwrite(File, Pwrite),
?line {ok, Presult} = ?FILE_MODULE:pread(File, Pread),
?line ok = ?FILE_MODULE:close(File),
?line [] = flush(),
ok.
append(doc) -> "Test appending to a file.";
append(suite) -> [];
append(Config) when is_list(Config) ->
?line RootDir = ?config(priv_dir, Config),
?line NewDir = filename:join(RootDir,
atom_to_list(?MODULE)
++"_append"),
?line ok = ?FILE_MODULE:make_dir(NewDir),
?line First = "First line\n",
?line Second = "Seond lines comes here\n",
?line Third = "And here is the third line\n",
%% Write a small text file.
?line Name1 = filename:join(NewDir, "a_file.txt"),
?line {ok, Fd1} = ?FILE_MODULE:open(Name1, [write]),
?line ok = io:format(Fd1, First, []),
?line ok = io:format(Fd1, Second, []),
?line ok = ?FILE_MODULE:close(Fd1),
%% Open it a again and a append a line to it.
?line {ok, Fd2} = ?FILE_MODULE:open(Name1, [append]),
?line ok = io:format(Fd2, Third, []),
?line ok = ?FILE_MODULE:close(Fd2),
%% Read it back and verify.
?line Expected = list_to_binary([First, Second, Third]),
?line {ok, Expected} = ?FILE_MODULE:read_file(Name1),
?line [] = flush(),
ok.
open_errors(suite) -> [];
open_errors(doc) -> [];
open_errors(Config) when is_list(Config) ->
?line DataDir =
filename:dirname(
filename:join(?config(data_dir, Config), "x")),
?line DataDirSlash = DataDir++"/",
?line {error, E1} = ?FILE_MODULE:open(DataDir, [read]),
?line {error, E2} = ?FILE_MODULE:open(DataDirSlash, [read]),
?line {error, E3} = ?FILE_MODULE:open(DataDir, [write]),
?line {error, E4} = ?FILE_MODULE:open(DataDirSlash, [write]),
?line {eisdir,eisdir,eisdir,eisdir} = {E1,E2,E3,E4},
?line [] = flush(),
ok.
exclusive(suite) -> [];
exclusive(doc) -> "Test exclusive access to a file.";
exclusive(Config) when is_list(Config) ->
?line RootDir = ?config(priv_dir,Config),
?line NewDir = filename:join(RootDir,
atom_to_list(?MODULE)
++"_exclusive"),
?line ok = ?FILE_MODULE:make_dir(NewDir),
?line Name = filename:join(NewDir, "ex_file.txt"),
?line {ok, Fd} = ?FILE_MODULE:open(Name, [write, exclusive]),
?line {error, eexist} = ?FILE_MODULE:open(Name, [write, exclusive]),
?line ok = ?FILE_MODULE:close(Fd),
ok.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
pos1(suite) -> [];
pos1(doc) -> [];
pos1(Config) when is_list(Config) ->
?line RootDir = ?config(priv_dir,Config),
?line Name = filename:join(RootDir,
atom_to_list(?MODULE)
++"_pos1.fil"),
?line {ok,Fd1} = ?FILE_MODULE:open(Name,write),
?line io:format(Fd1,"ABCDEFGH",[]),
?line ok = ?FILE_MODULE:close(Fd1),
?line {ok,Fd2} = ?FILE_MODULE:open(Name,read),
%% Start pos is first char
?line io:format("Relative positions"),
?line "A" = io:get_chars(Fd2,'',1),
?line {ok,2} = ?FILE_MODULE:position(Fd2,{cur,1}),
?line "C" = io:get_chars(Fd2,'',1),
?line {ok,0} = ?FILE_MODULE:position(Fd2,{cur,-3}),
?line "A" = io:get_chars(Fd2,'',1),
%% Backwards from first char should be an error
?line {ok,0} = ?FILE_MODULE:position(Fd2,{cur,-1}),
?line {error, einval} = ?FILE_MODULE:position(Fd2,{cur,-1}),
%% Reset position and move again
?line {ok,0} = ?FILE_MODULE:position(Fd2,0),
?line {ok,2} = ?FILE_MODULE:position(Fd2,{cur,2}),
?line "C" = io:get_chars(Fd2,'',1),
%% Go a lot forwards
?line {ok,13} = ?FILE_MODULE:position(Fd2,{cur,10}),
?line eof = io:get_chars(Fd2,'',1),
%% Try some fixed positions
?line io:format("Fixed positions"),
?line {ok,8} =
?FILE_MODULE:position(Fd2,8), eof = io:get_chars(Fd2,'',1),
?line {ok,8} =
?FILE_MODULE:position(Fd2,cur), eof = io:get_chars(Fd2,'',1),
?line {ok,7} =
?FILE_MODULE:position(Fd2,7), "H" = io:get_chars(Fd2,'',1),
?line {ok,0} =
?FILE_MODULE:position(Fd2,0), "A" = io:get_chars(Fd2,'',1),
?line {ok,3} =
?FILE_MODULE:position(Fd2,3), "D" = io:get_chars(Fd2,'',1),
?line {ok,12} =
?FILE_MODULE:position(Fd2,12), eof = io:get_chars(Fd2,'',1),
?line {ok,3} =
?FILE_MODULE:position(Fd2,3), "D" = io:get_chars(Fd2,'',1),
%% Try the {bof,X} notation
?line {ok,3} = ?FILE_MODULE:position(Fd2,{bof,3}),
?line "D" = io:get_chars(Fd2,'',1),
%% Try eof positions
?line io:format("EOF positions"),
?line {ok,8} =
?FILE_MODULE:position(Fd2,{eof,0}), eof=io:get_chars(Fd2,'',1),
?line {ok,7} =
?FILE_MODULE:position(Fd2,{eof,-1}),
?line "H" = io:get_chars(Fd2,'',1),
?line {ok,0} =
?FILE_MODULE:position(Fd2,{eof,-8}), "A"=io:get_chars(Fd2,'',1),
?line {error, einval} = ?FILE_MODULE:position(Fd2,{eof,-9}),
?line [] = flush(),
ok.
pos2(suite) -> [];
pos2(doc) -> [];
pos2(Config) when is_list(Config) ->
?line RootDir = ?config(priv_dir,Config),
?line Name = filename:join(RootDir,
atom_to_list(?MODULE)
++"_pos2.fil"),
?line {ok,Fd1} = ?FILE_MODULE:open(Name,write),
?line io:format(Fd1,"ABCDEFGH",[]),
?line ok = ?FILE_MODULE:close(Fd1),
?line {ok,Fd2} = ?FILE_MODULE:open(Name,read),
?line {error, einval} = ?FILE_MODULE:position(Fd2,-1),
%% Make sure that we still can search after an error.
?line {ok,0} = ?FILE_MODULE:position(Fd2, 0),
?line {ok,3} = ?FILE_MODULE:position(Fd2, {bof,3}),
?line "D" = io:get_chars(Fd2,'',1),
?line [] = flush(),
?line io:format("DONE"),
ok.
pos3(suite) -> [];
pos3(doc) -> ["When it does not use raw mode, file:position had a bug."];
pos3(Config) when is_list(Config) ->
?line RootDir = ?config(data_dir, Config),
?line Name = filename:join(RootDir, "realmen.html.gz"),
?line {ok, Fd} = ?FILE_MODULE:open(Name, [read, binary]),
?line {ok, _} = ?FILE_MODULE:read(Fd, 5),
?line {error, einval} = ?FILE_MODULE:position(Fd, {bof, -1}),
%% Here ok had returned =(
?line {error, einval} = ?FILE_MODULE:position(Fd, {cur, -10}),
%% That test is actually questionable since file:position/2
%% is documented to leave the file position undefined after
%% it has returned an error. But on Posix systems the position
%% is guaranteed to be unchanged after an error return. On e.g
%% Windows there is nothing stated about this in the documentation.
ok.
file_info_basic_file(suite) -> [];
file_info_basic_file(doc) -> [];
file_info_basic_file(Config) when is_list(Config) ->
?line RootDir = ?config(priv_dir, Config),
%% Create a short file.
?line Name = filename:join(RootDir,
atom_to_list(?MODULE)
++"_basic_test.fil"),
?line {ok,Fd1} = ?FILE_MODULE:open(Name, write),
?line io:put_chars(Fd1, "foo bar"),
?line ok = ?FILE_MODULE:close(Fd1),
%% Test that the file has the expected attributes.
%% The times are tricky, so we will save them to a separate test case.
{ok,FileInfo} = ?FILE_MODULE:read_file_info(Name),
{ok,FileInfo} = ?FILE_MODULE:read_file_info(Name, [raw]),
#file_info{size=Size,type=Type,access=Access,
atime=AccessTime,mtime=ModifyTime} = FileInfo,
?line io:format("Access ~p, Modify ~p", [AccessTime, ModifyTime]),
?line Size = 7,
?line Type = regular,
?line read_write = Access,
?line true = abs(time_dist(filter_atime(AccessTime, Config),
filter_atime(ModifyTime,
Config))) < 2,
?line all_integers(tuple_to_list(AccessTime) ++ tuple_to_list(ModifyTime)),
?line [] = flush(),
ok.
file_info_basic_directory(suite) -> [];
file_info_basic_directory(doc) -> [];
file_info_basic_directory(Config) when is_list(Config) ->
%% Note: filename:join/1 removes any trailing slash,
%% which is essential for ?FILE_MODULE:file_info/1 to work on
%% platforms such as Windows95.
RootDir = filename:join([?config(priv_dir, Config)]),
%% Test that the RootDir directory has the expected attributes.
test_directory(RootDir, read_write),
%% Note that on Windows file systems,
%% "/" or "c:/" are *NOT* directories.
%% Therefore, test that ?FILE_MODULE:file_info/1 behaves as if they were
%% directories.
case os:type() of
{win32, _} ->
?line test_directory("/", read_write),
?line test_directory("c:/", read_write),
?line test_directory("c:\\", read_write);
_ ->
?line test_directory("/", read)
end,
ok.
test_directory(Name, ExpectedAccess) ->
{ok,FileInfo} = ?FILE_MODULE:read_file_info(Name),
{ok,FileInfo} = ?FILE_MODULE:read_file_info(Name, [raw]),
#file_info{size=Size,type=Type,access=Access,
atime=AccessTime,mtime=ModifyTime} = FileInfo,
?line io:format("Testing directory ~s", [Name]),
?line io:format("Directory size is ~p", [Size]),
?line io:format("Access ~p", [Access]),
?line io:format("Access time ~p; Modify time~p",
[AccessTime, ModifyTime]),
?line Type = directory,
?line Access = ExpectedAccess,
?line all_integers(tuple_to_list(AccessTime) ++ tuple_to_list(ModifyTime)),
?line [] = flush(),
ok.
all_integers([{A,B,C}|T]) ->
all_integers([A,B,C|T]);
all_integers([Int|Rest]) when is_integer(Int) ->
?line all_integers(Rest);
all_integers([]) -> ok.
%% Try something nonexistent.
file_info_bad(suite) -> [];
file_info_bad(doc) -> [];
file_info_bad(Config) when is_list(Config) ->
?line RootDir = filename:join([?config(priv_dir, Config)]),
FileName = filename:join(RootDir, atom_to_list(?MODULE) ++ "_nonexistent"),
{error,enoent} = ?FILE_MODULE:read_file_info(FileName),
{error,enoent} = ?FILE_MODULE:read_file_info(FileName, [raw]),
?line {error, enoent} = ?FILE_MODULE:read_file_info(""),
{error, enoent} = ?FILE_MODULE:read_file_info("", [raw]),
?line [] = flush(),
ok.
%% Test that the file times behave as they should.
file_info_times(suite) -> [];
file_info_times(doc) -> [];
file_info_times(Config) when is_list(Config) ->
%% We have to try this twice, since if the test runs across the change
%% of a month the time diff calculations will fail. But it won't happen
%% if you run it twice in succession.
?line test_server:m_out_of_n(
1,2,
fun() -> ?line file_info_int(Config) end),
ok.
file_info_int(Config) ->
%% Note: filename:join/1 removes any trailing slash,
%% which is essential for ?FILE_MODULE:file_info/1 to work on
%% platforms such as Windows95.
?line RootDir = filename:join([?config(priv_dir, Config)]),
?line test_server:format("RootDir = ~p", [RootDir]),
?line Name = filename:join(RootDir,
atom_to_list(?MODULE)
++"_file_info.fil"),
?line {ok,Fd1} = ?FILE_MODULE:open(Name,write),
?line io:put_chars(Fd1,"foo"),
%% check that the file got a modify date max a few seconds away from now
{ok,FileInfo1} = ?FILE_MODULE:read_file_info(Name),
{ok,FileInfo1Raw} = ?FILE_MODULE:read_file_info(Name, [raw]),
%% We assert that everything but the size is the same, on some OSs the
%% size may not have been flushed to disc and we do not want to do a
%% sync to force it.
FileInfo1Raw = FileInfo1#file_info{ size = FileInfo1Raw#file_info.size },
#file_info{type=regular,atime=AccTime1,mtime=ModTime1} = FileInfo1,
?line Now = erlang:localtime(), %???
?line io:format("Now ~p",[Now]),
?line io:format("Open file Acc ~p Mod ~p",[AccTime1,ModTime1]),
?line true = abs(time_dist(filter_atime(Now, Config),
filter_atime(AccTime1,
Config))) < 8,
?line true = abs(time_dist(Now,ModTime1)) < 8,
%% Sleep until we can be sure the seconds value has changed.
%% Note: FAT-based filesystem (like on Windows 95) have
%% a resolution of 2 seconds.
?line test_server:sleep(test_server:seconds(2.2)),
%% close the file, and watch the modify date change
?line ok = ?FILE_MODULE:close(Fd1),
{ok,FileInfo2} = ?FILE_MODULE:read_file_info(Name),
{ok,FileInfo2} = ?FILE_MODULE:read_file_info(Name, [raw]),
#file_info{size=Size,type=regular,access=Access,
atime=AccTime2,mtime=ModTime2} = FileInfo2,
?line io:format("Closed file Acc ~p Mod ~p",[AccTime2,ModTime2]),
?line true = time_dist(ModTime1,ModTime2) >= 0,
%% this file is supposed to be binary, so it'd better keep it's size
?line Size = 3,
?line Access = read_write,
%% Do some directory checking
{ok,FileInfo3} = ?FILE_MODULE:read_file_info(RootDir),
{ok,FileInfo3} = ?FILE_MODULE:read_file_info(RootDir, [raw]),
#file_info{size=DSize,type=directory,access=DAccess,
atime=AccTime3,mtime=ModTime3} = FileInfo3,
%% this dir was modified only a few secs ago
?line io:format("Dir Acc ~p; Mod ~p; Now ~p", [AccTime3, ModTime3, Now]),
?line true = abs(time_dist(Now,ModTime3)) < 5,
?line DAccess = read_write,
?line io:format("Dir size is ~p",[DSize]),
?line [] = flush(),
ok.
%% Filter access times, to copy with a deficiency of FAT file systems
%% (on Windows): The access time is actually only a date.
filter_atime(Atime, Config) ->
case lists:member(no_access_time, Config) of
true ->
case Atime of
{Date, _} ->
{Date, {0, 0, 0}};
{Y, M, D, _, _, _} ->
{Y, M, D, 0, 0, 0}
end;
false ->
Atime
end.
%% Test the write_file_info/2 function.
file_write_file_info(suite) -> [];
file_write_file_info(doc) -> [];
file_write_file_info(Config) when is_list(Config) ->
?line RootDir = get_good_directory(Config),
?line test_server:format("RootDir = ~p", [RootDir]),
%% Set the file to read only AND update the file times at the same time.
%% (This used to fail on Windows NT/95 for a local filesystem.)
%% Note: Seconds must be even; see note in file_info_times/1.
?line Name1 = filename:join(RootDir,
atom_to_list(?MODULE)
++"_write_file_info_ro"),
?line ok = ?FILE_MODULE:write_file(Name1, "hello"),
?line Time = {{1997, 01, 02}, {12, 35, 42}},
?line Info = #file_info{mode=8#400, atime=Time, mtime=Time, ctime=Time},
?line ok = ?FILE_MODULE:write_file_info(Name1, Info),
%% Read back the times.
?line {ok, ActualInfo} = ?FILE_MODULE:read_file_info(Name1),
?line #file_info{mode=_Mode, atime=ActAtime, mtime=Time,
ctime=ActCtime} = ActualInfo,
?line FilteredAtime = filter_atime(Time, Config),
?line FilteredAtime = filter_atime(ActAtime, Config),
?line case os:type() of
{win32, _} ->
%% On Windows, "ctime" means creation time and it can
%% be set.
ActCtime = Time;
_ ->
ok
end,
?line {error, eacces} = ?FILE_MODULE:write_file(Name1, "hello again"),
%% Make the file writable again.
?line ?FILE_MODULE:write_file_info(Name1, #file_info{mode=8#600}),
?line ok = ?FILE_MODULE:write_file(Name1, "hello again"),
%% And unwritable.
?line ?FILE_MODULE:write_file_info(Name1, #file_info{mode=8#400}),
?line {error, eacces} = ?FILE_MODULE:write_file(Name1, "hello again"),
%% Same with raw.
?FILE_MODULE:write_file_info(Name1, #file_info{mode=8#600}, [raw]),
ok = ?FILE_MODULE:write_file(Name1, "hello again"),
?FILE_MODULE:write_file_info(Name1, #file_info{mode=8#400}, [raw]),
{error,eacces} = ?FILE_MODULE:write_file(Name1, "hello again"),
%% Write the times again.
%% Note: Seconds must be even; see note in file_info_times/1.
?line NewTime = {{1997, 02, 15}, {13, 18, 20}},
?line NewInfo = #file_info{atime=NewTime, mtime=NewTime, ctime=NewTime},
?line ok = ?FILE_MODULE:write_file_info(Name1, NewInfo),
?line {ok, ActualInfo2} = ?FILE_MODULE:read_file_info(Name1),
?line #file_info{atime=NewActAtime, mtime=NewTime,
ctime=NewActCtime} = ActualInfo2,
?line NewFilteredAtime = filter_atime(NewTime, Config),
?line NewFilteredAtime = filter_atime(NewActAtime, Config),
?line case os:type() of
{win32, _} -> NewActCtime = NewTime;
_ -> ok
end,
%% The file should still be unwritable.
?line {error, eacces} = ?FILE_MODULE:write_file(Name1, "hello again"),
%% Make the file writeable again, so that we can remove the
%% test suites ... :-)
?line ?FILE_MODULE:write_file_info(Name1, #file_info{mode=8#600}),
?line [] = flush(),
ok.
%% Returns a directory on a file system that has correct file times.
get_good_directory(Config) ->
?line ?config(priv_dir, Config).
consult1(suite) -> [];
consult1(doc) -> [];
consult1(Config) when is_list(Config) ->
?line RootDir = ?config(priv_dir,Config),
?line Name = filename:join(RootDir,
atom_to_list(?MODULE)
++"_consult.fil"),
?line {ok,Fd1} = ?FILE_MODULE:open(Name,write),
%% note that there is no final \n (only a space)
?line io:format(Fd1,
"{this,[is,1.0],'journey'}.\n\"into\". (sound). ",
[]),
?line ok = ?FILE_MODULE:close(Fd1),
?line {ok,[{this,[is,1.0],journey},"into",sound]} =
?FILE_MODULE:consult(Name),
?line {ok,Fd2} = ?FILE_MODULE:open(Name,write),
%% note the missing double quote
?line io:format(
Fd2,"{this,[is,1.0],'journey'}.\n \"into. (sound). ",[]),
?line ok = ?FILE_MODULE:close(Fd2),
?line {error, {_, _, _} = Msg} = ?FILE_MODULE:consult(Name),
?line io:format("Errmsg: ~p",[Msg]),
?line {error, enoent} = ?FILE_MODULE:consult(Name ++ ".nonexistent"),
?line [] = flush(),
ok.
path_consult(suite) -> [];
path_consult(doc) -> [];
path_consult(Config) when is_list(Config) ->
?line RootDir = ?config(priv_dir,Config),
?line FileName = atom_to_list(?MODULE)++"_path_consult.fil",
?line Name = filename:join(RootDir, FileName),
?line {ok,Fd1} = ?FILE_MODULE:open(Name,write),
?line io:format(Fd1,"{this,is,a,journey,into,sound}.\n",[]),
?line ok = ?FILE_MODULE:close(Fd1),
%% File last in path
?line {ok,[{this,is,a,journey,into,sound}],Dir} =
?FILE_MODULE:path_consult(
[filename:join(RootDir, "dir1"),
filename:join(RootDir, ".."),
filename:join(RootDir, "dir2"),
RootDir], FileName),
?line true = lists:prefix(RootDir,Dir),
%% While maybe not an error, it may be worth noting that
%% when the full path to a file is given, it's always found
%% regardless of the contents of the path
?line {ok,_,_} = ?FILE_MODULE:path_consult(["nosuch1","nosuch2"],Name),
?line [] = flush(),
ok.
eval1(suite) -> [];
eval1(doc) -> [];
eval1(Config) when is_list(Config) ->
?line RootDir = ?config(priv_dir,Config),
?line Name = filename:join(RootDir,
atom_to_list(?MODULE)++"_eval.fil"),
?line {ok,Fd1} = ?FILE_MODULE:open(Name,write),
%% note that there is no final \n (only a space)
?line io:format(Fd1,"put(evaluated_ok,\ntrue). ",[]),
?line ok = ?FILE_MODULE:close(Fd1),
?line ok = ?FILE_MODULE:eval(Name),
?line true = get(evaluated_ok),
?line {ok,Fd2} = ?FILE_MODULE:open(Name,write),
%% note that there is no final \n (only a space)
?line io:format(Fd2,"put(evaluated_ok,\nR). ",[]),
?line ok = ?FILE_MODULE:close(Fd2),
?line ok = ?FILE_MODULE:eval(
Name,
erl_eval:add_binding('R', true, erl_eval:new_bindings())),
?line true = get(evaluated_ok),
?line {ok,Fd3} = ?FILE_MODULE:open(Name,write),
%% garbled
?line io:format(Fd3,"puGARBLED-GARBLED\ntrue). ",[]),
?line ok = ?FILE_MODULE:close(Fd3),
?line {error, {_, _, _} = Msg} = ?FILE_MODULE:eval(Name),
?line io:format("Errmsg1: ~p",[Msg]),
?line {error, enoent} = ?FILE_MODULE:eval(Name ++ ".nonexistent"),
?line [] = flush(),
ok.
path_eval(suite) -> [];
path_eval(doc) -> [];
path_eval(Config) when is_list(Config) ->
?line RootDir = ?config(priv_dir,Config),
?line FileName = atom_to_list(?MODULE)++"_path_eval.fil",
?line Name = filename:join(RootDir, FileName),
?line {ok,Fd1} = ?FILE_MODULE:open(Name,write),
?line io:format(Fd1,"put(evaluated_ok,true).\n",[]),
?line ok = ?FILE_MODULE:close(Fd1),
%% File last in path
?line {ok,Dir} =
?FILE_MODULE:path_eval(
[filename:join(RootDir, "dir1"),
filename:join(RootDir, ".."),
filename:join(RootDir, "dir2"),
RootDir],FileName),
?line true = get(evaluated_ok),
?line true = lists:prefix(RootDir,Dir),
%% While maybe not an error, it may be worth noting that
%% when the full path to a file is given, it's always found
%% regardless of the contents of the path
?line {ok,Fd2} = ?FILE_MODULE:open(Name,write),
?line io:format(Fd2,"put(evaluated_ok,R).\n",[]),
?line ok = ?FILE_MODULE:close(Fd2),
?line {ok,_} = ?FILE_MODULE:path_eval(
["nosuch1","nosuch2"],
Name,
erl_eval:add_binding('R', true, erl_eval:new_bindings())),
?line [] = flush(),
ok.
script1(suite) -> [];
script1(doc) -> "";
script1(Config) when is_list(Config) ->
?line RootDir = ?config(priv_dir,Config),
?line Name = filename:join(RootDir,
atom_to_list(?MODULE)++"_script.fil"),
?line {ok,Fd1} = ?FILE_MODULE:open(Name,write),
%% note that there is no final \n (only a space)
?line io:format(Fd1,"A = 11,\nB = 6,\nA+B. ",[]),
?line ok = ?FILE_MODULE:close(Fd1),
?line {ok,17} = ?FILE_MODULE:script(Name),
?line {ok,Fd2} = ?FILE_MODULE:open(Name,write),
%% note that there is no final \n (only a space)
?line io:format(Fd2,"A = 11,\nA+B. ",[]),
?line ok = ?FILE_MODULE:close(Fd2),
?line {ok,17} = ?FILE_MODULE:script(
Name,
erl_eval:add_binding('B', 6, erl_eval:new_bindings())),
?line {ok,Fd3} = ?FILE_MODULE:open(Name,write),
?line io:format(Fd3,"A = 11,\nB = six,\nA+B. ",[]),
?line ok = ?FILE_MODULE:close(Fd3),
?line {error, {_, _, _} = Msg} = ?FILE_MODULE:script(Name),
?line io:format("Errmsg1: ~p",[Msg]),
?line {error, enoent} = ?FILE_MODULE:script(Name ++ ".nonexistent"),
?line [] = flush(),
ok.
path_script(suite) -> [];
path_script(doc) -> [];
path_script(Config) when is_list(Config) ->
?line RootDir = ?config(priv_dir,Config),
?line FileName = atom_to_list(?MODULE)++"_path_script.fil",
?line Name = filename:join(RootDir, FileName),
?line {ok,Fd1} = ?FILE_MODULE:open(Name,write),
?line io:format(Fd1,"A = 11,\nB = 6,\nA+B.\n",[]),
?line ok = ?FILE_MODULE:close(Fd1),
%% File last in path
?line {ok, 17, Dir} =
?FILE_MODULE:path_script(
[filename:join(RootDir, "dir1"),
filename:join(RootDir, ".."),
filename:join(RootDir, "dir2"),
RootDir],FileName),
?line true = lists:prefix(RootDir,Dir),
%% While maybe not an error, it may be worth noting that
%% when the full path to a file is given, it's always found
%% regardless of the contents of the path
?line {ok,Fd2} = ?FILE_MODULE:open(Name,write),
?line io:format(Fd2,"A = 11,\nA+B.",[]),
?line ok = ?FILE_MODULE:close(Fd2),
?line {ok, 17, Dir} =
?FILE_MODULE:path_script(
["nosuch1","nosuch2"],
Name,
erl_eval:add_binding('B', 6, erl_eval:new_bindings())),
?line [] = flush(),
ok.
truncate(suite) -> [];
truncate(doc) -> [];
truncate(Config) when is_list(Config) ->
?line RootDir = ?config(priv_dir,Config),
?line Name = filename:join(RootDir,
atom_to_list(?MODULE)
++"_truncate.fil"),
%% Create a file with some data.
?line MyData = "0123456789abcdefghijklmnopqrstuvxyz",
?line ok = ?FILE_MODULE:write_file(Name, MyData),
%% Truncate the file to 10 characters.
?line {ok, Fd} = ?FILE_MODULE:open(Name, read_write),
?line {ok, 10} = ?FILE_MODULE:position(Fd, 10),
?line ok = ?FILE_MODULE:truncate(Fd),
?line ok = ?FILE_MODULE:close(Fd),
%% Read back the file and check that it has been truncated.
?line Expected = list_to_binary("0123456789"),
?line {ok, Expected} = ?FILE_MODULE:read_file(Name),
%% Open the file read only and verify that it is not possible to
%% truncate it, OTP-1960
?line {ok, Fd2} = ?FILE_MODULE:open(Name, read),
?line {ok, 5} = ?FILE_MODULE:position(Fd2, 5),
?line {error, _} = ?FILE_MODULE:truncate(Fd2),
?line [] = flush(),
ok.
datasync(suite) -> [];
datasync(doc) -> "Tests that ?FILE_MODULE:datasync/1 at least doesn't crash.";
datasync(Config) when is_list(Config) ->
?line PrivDir = ?config(priv_dir, Config),
?line Sync = filename:join(PrivDir,
atom_to_list(?MODULE)
++"_sync.fil"),
%% Raw open.
?line {ok, Fd} = ?FILE_MODULE:open(Sync, [write, raw]),
?line ok = ?FILE_MODULE:datasync(Fd),
?line ok = ?FILE_MODULE:close(Fd),
%% Ordinary open.
?line {ok, Fd2} = ?FILE_MODULE:open(Sync, [write]),
?line ok = ?FILE_MODULE:datasync(Fd2),
?line ok = ?FILE_MODULE:close(Fd2),
?line [] = flush(),
ok.
sync(suite) -> [];
sync(doc) -> "Tests that ?FILE_MODULE:sync/1 at least doesn't crash.";
sync(Config) when is_list(Config) ->
?line PrivDir = ?config(priv_dir, Config),
?line Sync = filename:join(PrivDir,
atom_to_list(?MODULE)
++"_sync.fil"),
%% Raw open.
?line {ok, Fd} = ?FILE_MODULE:open(Sync, [write, raw]),
?line ok = ?FILE_MODULE:sync(Fd),
?line ok = ?FILE_MODULE:close(Fd),
%% Ordinary open.
?line {ok, Fd2} = ?FILE_MODULE:open(Sync, [write]),
?line ok = ?FILE_MODULE:sync(Fd2),
?line ok = ?FILE_MODULE:close(Fd2),
?line [] = flush(),
ok.
advise(suite) -> [];
advise(doc) -> "Tests that ?FILE_MODULE:advise/4 at least doesn't crash.";
advise(Config) when is_list(Config) ->
?line PrivDir = ?config(priv_dir, Config),
?line Advise = filename:join(PrivDir,
atom_to_list(?MODULE)
++"_advise.fil"),
Line1 = "Hello\n",
Line2 = "World!\n",
?line {ok, Fd} = ?FILE_MODULE:open(Advise, [write]),
?line ok = ?FILE_MODULE:advise(Fd, 0, 0, normal),
?line ok = io:format(Fd, "~s", [Line1]),
?line ok = io:format(Fd, "~s", [Line2]),
?line ok = ?FILE_MODULE:close(Fd),
?line {ok, Fd2} = ?FILE_MODULE:open(Advise, [write]),
?line ok = ?FILE_MODULE:advise(Fd2, 0, 0, random),
?line ok = io:format(Fd2, "~s", [Line1]),
?line ok = io:format(Fd2, "~s", [Line2]),
?line ok = ?FILE_MODULE:close(Fd2),
?line {ok, Fd3} = ?FILE_MODULE:open(Advise, [write]),
?line ok = ?FILE_MODULE:advise(Fd3, 0, 0, sequential),
?line ok = io:format(Fd3, "~s", [Line1]),
?line ok = io:format(Fd3, "~s", [Line2]),
?line ok = ?FILE_MODULE:close(Fd3),
?line {ok, Fd4} = ?FILE_MODULE:open(Advise, [write]),
?line ok = ?FILE_MODULE:advise(Fd4, 0, 0, will_need),
?line ok = io:format(Fd4, "~s", [Line1]),
?line ok = io:format(Fd4, "~s", [Line2]),
?line ok = ?FILE_MODULE:close(Fd4),
?line {ok, Fd5} = ?FILE_MODULE:open(Advise, [write]),
?line ok = ?FILE_MODULE:advise(Fd5, 0, 0, dont_need),
?line ok = io:format(Fd5, "~s", [Line1]),
?line ok = io:format(Fd5, "~s", [Line2]),
?line ok = ?FILE_MODULE:close(Fd5),
?line {ok, Fd6} = ?FILE_MODULE:open(Advise, [write]),
?line ok = ?FILE_MODULE:advise(Fd6, 0, 0, no_reuse),
?line ok = io:format(Fd6, "~s", [Line1]),
?line ok = io:format(Fd6, "~s", [Line2]),
?line ok = ?FILE_MODULE:close(Fd6),
?line {ok, Fd7} = ?FILE_MODULE:open(Advise, [write]),
?line {error, einval} = ?FILE_MODULE:advise(Fd7, 0, 0, bad_advise),
?line ok = ?FILE_MODULE:close(Fd7),
%% test write without advise, then a read after an advise
?line {ok, Fd8} = ?FILE_MODULE:open(Advise, [write]),
?line ok = io:format(Fd8, "~s", [Line1]),
?line ok = io:format(Fd8, "~s", [Line2]),
?line ok = ?FILE_MODULE:close(Fd8),
?line {ok, Fd9} = ?FILE_MODULE:open(Advise, [read]),
Offset = 0,
%% same as a 0 length in some implementations
Length = length(Line1) + length(Line2),
?line ok = ?FILE_MODULE:advise(Fd9, Offset, Length, sequential),
?line {ok, Line1} = ?FILE_MODULE:read_line(Fd9),
?line {ok, Line2} = ?FILE_MODULE:read_line(Fd9),
?line eof = ?FILE_MODULE:read_line(Fd9),
?line ok = ?FILE_MODULE:close(Fd9),
?line [] = flush(),
ok.
allocate(suite) -> [];
allocate(doc) -> "Tests that ?FILE_MODULE:allocate/3 at least doesn't crash.";
allocate(Config) when is_list(Config) ->
?line PrivDir = ?config(priv_dir, Config),
?line Allocate = filename:join(PrivDir,
atom_to_list(?MODULE)
++"_allocate.fil"),
Line1 = "Hello\n",
Line2 = "World!\n",
?line {ok, Fd} = ?FILE_MODULE:open(Allocate, [write, binary]),
allocate_and_assert(Fd, 1, iolist_size([Line1, Line2])),
?line ok = io:format(Fd, "~s", [Line1]),
?line ok = io:format(Fd, "~s", [Line2]),
?line ok = ?FILE_MODULE:close(Fd),
?line {ok, Fd2} = ?FILE_MODULE:open(Allocate, [write, binary]),
allocate_and_assert(Fd2, 1, iolist_size(Line1)),
?line ok = io:format(Fd2, "~s", [Line1]),
?line ok = io:format(Fd2, "~s", [Line2]),
?line ok = ?FILE_MODULE:close(Fd2),
?line {ok, Fd3} = ?FILE_MODULE:open(Allocate, [write, binary]),
allocate_and_assert(Fd3, 1, iolist_size(Line1) + 1),
?line ok = io:format(Fd3, "~s", [Line1]),
?line ok = io:format(Fd3, "~s", [Line2]),
?line ok = ?FILE_MODULE:close(Fd3),
?line {ok, Fd4} = ?FILE_MODULE:open(Allocate, [write, binary]),
allocate_and_assert(Fd4, 1, 4 * iolist_size([Line1, Line2])),
?line ok = io:format(Fd4, "~s", [Line1]),
?line ok = io:format(Fd4, "~s", [Line2]),
?line ok = ?FILE_MODULE:close(Fd4),
?line [] = flush(),
ok.
allocate_and_assert(Fd, Offset, Length) ->
% Just verify that calls to ?PRIM_FILE:allocate/3 don't crash or have
% any other negative side effect. We can't really asssert against a
% specific return value, because support for file space pre-allocation
% depends on the OS, OS version and underlying filesystem.
%
% The Linux kernel added support for fallocate() in version 2.6.23,
% which currently works only for the ext4, ocfs2, xfs and btrfs file
% systems. posix_fallocate() is available in glibc as of version
% 2.1.94, but it was buggy until glibc version 2.7.
%
% Mac OS X, as of version 10.3, supports the fcntl operation F_PREALLOCATE.
%
% Solaris supports posix_fallocate() but only for the UFS file system
% apparently (not supported for ZFS).
%
% FreeBSD 9.0 is the first FreeBSD release supporting posix_fallocate().
%
% For Windows there's apparently no way to pre-allocate file space, at
% least with same semantics as posix_fallocate(), fallocate() and
% fcntl F_PREALLOCATE.
Result = ?FILE_MODULE:allocate(Fd, Offset, Length),
case os:type() of
{win32, _} ->
?line {error, enotsup} = Result;
_ ->
?line _ = Result
end.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
delete(suite) -> [];
delete(doc) -> [];
delete(Config) when is_list(Config) ->
?line RootDir = ?config(priv_dir,Config),
?line Name = filename:join(RootDir,
atom_to_list(?MODULE)
++"_delete.fil"),
?line {ok, Fd1} = ?FILE_MODULE:open(Name, write),
?line io:format(Fd1,"ok.\n",[]),
?line ok = ?FILE_MODULE:close(Fd1),
%% Check that the file is readable
?line {ok, Fd2} = ?FILE_MODULE:open(Name, read),
?line ok = ?FILE_MODULE:close(Fd2),
?line ok = ?FILE_MODULE:delete(Name),
%% Check that the file is not readable anymore
?line {error, _} = ?FILE_MODULE:open(Name, read),
%% Try deleting a nonexistent file
?line {error, enoent} = ?FILE_MODULE:delete(Name),
?line [] = flush(),
ok.
rename(suite) ->[];
rename(doc) ->[];
rename(Config) when is_list(Config) ->
?line RootDir = ?config(priv_dir,Config),
?line FileName1 = atom_to_list(?MODULE)++"_rename.fil",
?line FileName2 = atom_to_list(?MODULE)++"_rename.ful",
?line Name1 = filename:join(RootDir, FileName1),
?line Name2 = filename:join(RootDir, FileName2),
?line {ok,Fd1} = ?FILE_MODULE:open(Name1,write),
?line ok = ?FILE_MODULE:close(Fd1),
%% Rename, and check that id really changed name
?line ok = ?FILE_MODULE:rename(Name1,Name2),
?line {error, _} = ?FILE_MODULE:open(Name1,read),
?line {ok,Fd2} = ?FILE_MODULE:open(Name2,read),
?line ok = ?FILE_MODULE:close(Fd2),
%% Try renaming something to itself
?line ok = ?FILE_MODULE:rename(Name2,Name2),
%% Try renaming something that doesn't exist
?line {error, enoent} = ?FILE_MODULE:rename(Name1,Name2),
%% Try renaming to something else than a string
?line {error, badarg} = ?FILE_MODULE:rename(Name1,{foo,bar}),
%% Move between directories
?line DirName1 = filename:join(RootDir,
atom_to_list(?MODULE)
++"_rename_dir"),
?line DirName2 = filename:join(RootDir,
atom_to_list(?MODULE)
++"_second_rename_dir"),
?line Name1foo = filename:join(DirName1, "foo.fil"),
?line Name2foo = filename:join(DirName2, "foo.fil"),
?line Name2bar = filename:join(DirName2, "bar.dir"),
?line ok = ?FILE_MODULE:make_dir(DirName1),
%% The name has to include the full file name, path in not enough
?line expect({error, eisdir}, {error, eexist},
?FILE_MODULE:rename(Name2,DirName1)),
?line ok = ?FILE_MODULE:rename(Name2, Name1foo),
%% Now rename the directory
?line ok = ?FILE_MODULE:rename(DirName1,DirName2),
%% And check that the file is there now
?line {ok,Fd3} = ?FILE_MODULE:open(Name2foo, read),
?line ok = ?FILE_MODULE:close(Fd3),
%% Try some dirty things now: move the directory into itself
?line {error, Msg1} = ?FILE_MODULE:rename(DirName2, Name2bar),
?line io:format("Errmsg1: ~p",[Msg1]),
%% move dir into a file in itself
?line {error, Msg2} = ?FILE_MODULE:rename(DirName2, Name2foo),
?line io:format("Errmsg2: ~p",[Msg2]),
?line [] = flush(),
ok.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
names(suite) -> [];
names(doc) -> [];
names(Config) when is_list(Config) ->
?line RootDir = ?config(priv_dir,Config),
?line FileName = "foo1.fil",
?line Name1 = filename:join(RootDir, FileName),
?line Name2 = [RootDir,"/","foo1",".","fil"],
?line Name3 = [RootDir,"/",foo,$1,[[[],[],'.']],"f",il],
?line {ok,Fd0} = ?FILE_MODULE:open(Name1,write),
?line ok = ?FILE_MODULE:close(Fd0),
%% Try some file names
?line {ok,Fd1} = ?FILE_MODULE:open(Name1,read),
?line ok = ?FILE_MODULE:close(Fd1),
?line {ok,Fd2f} = ?FILE_MODULE:open(lists:flatten(Name2),read),
?line ok = ?FILE_MODULE:close(Fd2f),
?line {ok,Fd2} = ?FILE_MODULE:open(Name2,read),
?line ok = ?FILE_MODULE:close(Fd2),
?line {ok,Fd3} = ?FILE_MODULE:open(Name3,read),
?line ok = ?FILE_MODULE:close(Fd3),
case length(Name1) > 255 of
true ->
io:format("Path too long for an atom:\n\n~p\n", [Name1]);
false ->
Name4 = list_to_atom(Name1),
{ok,Fd4} = ?FILE_MODULE:open(Name4,read),
ok = ?FILE_MODULE:close(Fd4)
end,
%% Try some path names
?line Path1 = RootDir,
?line Path2 = [RootDir],
?line Path3 = ['',[],[RootDir,[[]]]],
?line {ok,Fd11,_} = ?FILE_MODULE:path_open([Path1],FileName,read),
?line ok = ?FILE_MODULE:close(Fd11),
?line {ok,Fd12,_} = ?FILE_MODULE:path_open([Path2],FileName,read),
?line ok = ?FILE_MODULE:close(Fd12),
?line {ok,Fd13,_} = ?FILE_MODULE:path_open([Path3],FileName,read),
?line ok = ?FILE_MODULE:close(Fd13),
case length(Path1) > 255 of
true->
io:format("Path too long for an atom:\n\n~p\n", [Path1]);
false ->
Path4 = list_to_atom(Path1),
{ok,Fd14,_} = ?FILE_MODULE:path_open([Path4],FileName,read),
ok = ?FILE_MODULE:close(Fd14)
end,
?line [] = flush(),
ok.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
e_delete(suite) -> [];
e_delete(doc) -> [];
e_delete(Config) when is_list(Config) ->
?line RootDir = ?config(priv_dir, Config),
?line Base = filename:join(RootDir,
atom_to_list(?MODULE)++"_e_delete"),
?line ok = ?FILE_MODULE:make_dir(Base),
%% Delete a non-existing file.
?line {error, enoent} =
?FILE_MODULE:delete(filename:join(Base, "non_existing")),
%% Delete a directory.
?line {error, eperm} = ?FILE_MODULE:delete(Base),
%% Use a path-name with a non-directory component.
?line Afile = filename:join(Base, "a_file"),
?line ok = ?FILE_MODULE:write_file(Afile, "hello\n"),
?line {error, E} =
expect({error, enotdir}, {error, enoent},
?FILE_MODULE:delete(filename:join(Afile, "another_file"))),
?line io:format("Result: ~p~n", [E]),
%% No permission.
?line case os:type() of
{win32, _} ->
%% Remove a character device.
?line {error, eacces} = ?FILE_MODULE:delete("nul");
_ ->
?line ?FILE_MODULE:write_file_info(
Base, #file_info {mode=0}),
?line {error, eacces} = ?FILE_MODULE:delete(Afile),
?line ?FILE_MODULE:write_file_info(
Base, #file_info {mode=8#600})
end,
?line [] = flush(),
ok.
%%% FreeBSD gives EEXIST when renaming a file to an empty dir, although the
%%% manual page can be interpreted as saying that EISDIR should be given.
%%% (What about FreeBSD? We store our nightly build results on a FreeBSD
%%% file system, that's what.)
e_rename(suite) -> [];
e_rename(doc) -> [];
e_rename(Config) when is_list(Config) ->
RootDir = ?config(priv_dir, Config),
Base = filename:join(RootDir,
atom_to_list(?MODULE)++"_e_rename"),
ok = ?FILE_MODULE:make_dir(Base),
%% Create an empty directory.
EmptyDir = filename:join(Base, "empty_dir"),
ok = ?FILE_MODULE:make_dir(EmptyDir),
%% Create a non-empty directory.
NonEmptyDir = filename:join(Base, "non_empty_dir"),
ok = ?FILE_MODULE:make_dir(NonEmptyDir),
ok = ?FILE_MODULE:write_file(
filename:join(NonEmptyDir, "a_file"),
"hello\n"),
%% Create another non-empty directory.
ADirectory = filename:join(Base, "a_directory"),
ok = ?FILE_MODULE:make_dir(ADirectory),
ok = ?FILE_MODULE:write_file(
filename:join(ADirectory, "a_file"),
"howdy\n\n"),
%% Create a data file.
File = filename:join(Base, "just_a_file"),
ok = ?FILE_MODULE:write_file(File, "anything goes\n\n"),
%% Move an existing directory to a non-empty directory.
{error, eexist} = ?FILE_MODULE:rename(ADirectory, NonEmptyDir),
%% Move a root directory.
{error, einval} = ?FILE_MODULE:rename("/", "arne"),
%% Move Base into Base/new_name.
{error, einval} =
?FILE_MODULE:rename(Base, filename:join(Base, "new_name")),
%% Overwrite a directory with a file.
expect({error, eexist}, %FreeBSD (?)
{error, eisdir},
?FILE_MODULE:rename(File, EmptyDir)),
expect({error, eexist}, %FreeBSD (?)
{error, eisdir},
?FILE_MODULE:rename(File, NonEmptyDir)),
%% Move a non-existing file.
NonExistingFile = filename:join(Base, "non_existing_file"),
{error, enoent} = ?FILE_MODULE:rename(NonExistingFile, NonEmptyDir),
%% Overwrite a file with a directory.
expect({error, eexist}, %FreeBSD (?)
{error, enotdir},
?FILE_MODULE:rename(ADirectory, File)),
%% Move a file to another filesystem.
%% XXX - This test case is bogus. We cannot be guaranteed that
%% the source and destination are on
%% different filesystems.
%%
%% XXX - Gross hack!
Comment = case os:type() of
{unix, _} ->
OtherFs = "/tmp",
NameOnOtherFs = filename:join(OtherFs, filename:basename(File)),
{ok, Com} = case ?FILE_MODULE:rename(File, NameOnOtherFs) of
{error, exdev} ->
%% The file could be in
%% the same filesystem!
{ok, ok};
ok ->
{ok, {comment,
"Moving between filesystems "
"suceeded, files are probably "
"in the same filesystem!"}};
{error, eperm} ->
{ok, {comment, "SBS! You don't "
"have the permission to do "
"this test!"}};
Else ->
Else
end,
Com;
{win32, _} ->
%% At least Windows NT can
%% successfully move a file to
%% another drive.
ok
end,
[] = flush(),
Comment.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
e_make_dir(suite) -> [];
e_make_dir(doc) -> [];
e_make_dir(Config) when is_list(Config) ->
RootDir = ?config(priv_dir, Config),
Base = filename:join(RootDir,
atom_to_list(?MODULE)++"_e_make_dir"),
ok = ?FILE_MODULE:make_dir(Base),
%% A component of the path does not exist.
{error, enoent} = ?FILE_MODULE:make_dir(filename:join([Base, "a", "b"])),
%% Use a path-name with a non-directory component.
Afile = filename:join(Base, "a_directory"),
ok = ?FILE_MODULE:write_file(Afile, "hello\n"),
case ?FILE_MODULE:make_dir(
filename:join(Afile, "another_directory")) of
{error, enotdir} -> io:format("Result: enotdir");
{error, enoent} -> io:format("Result: enoent")
end,
%% No permission (on Unix only).
case os:type() of
{win32, _} ->
ok;
_ ->
?FILE_MODULE:write_file_info(Base, #file_info {mode=0}),
{error, eacces} = ?FILE_MODULE:make_dir(filename:join(Base, "xxxx")),
?FILE_MODULE:write_file_info(
Base, #file_info {mode=8#600})
end,
ok.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
e_del_dir(suite) -> [];
e_del_dir(doc) -> [];
e_del_dir(Config) when is_list(Config) ->
RootDir = ?config(priv_dir, Config),
Base = test_server:temp_name(filename:join(RootDir, "e_del_dir")),
io:format("Base: ~p", [Base]),
ok = ?FILE_MODULE:make_dir(Base),
%% Delete a non-existent directory.
{error, enoent} =
?FILE_MODULE:del_dir(filename:join(Base, "non_existing")),
%% Use a path-name with a non-directory component.
Afile = filename:join(Base, "a_directory"),
ok = ?FILE_MODULE:write_file(Afile, "hello\n"),
{error, E1} = expect({error, enotdir}, {error, enoent},
?FILE_MODULE:del_dir(
filename:join(Afile, "another_directory"))),
io:format("Result: ~p", [E1]),
%% Delete a non-empty directory.
{error, E2} = expect({error, enotempty}, {error, eexist}, {error, eacces},
?FILE_MODULE:del_dir(Base)),
io:format("Result: ~p", [E2]),
%% Remove the current directory.
{error, E3} = expect({error, einval},
{error, eperm}, % Linux and DUX
{error, eacces},
{error, ebusy},
?FILE_MODULE:del_dir(".")),
io:format("Result: ~p", [E3]),
%% No permission.
case os:type() of
{win32, _} ->
ok;
_ ->
ADirectory = filename:join(Base, "no_perm"),
ok = ?FILE_MODULE:make_dir(ADirectory),
?FILE_MODULE:write_file_info( Base, #file_info {mode=0}),
{error, eacces} = ?FILE_MODULE:del_dir(ADirectory),
?FILE_MODULE:write_file_info( Base, #file_info {mode=8#600})
end,
[] = flush(),
ok.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Trying reading and positioning from a compressed file.
read_compressed_cooked(Config) when is_list(Config) ->
?line Data = ?config(data_dir, Config),
?line Real = filename:join(Data, "realmen.html.gz"),
?line {ok, Fd} = ?FILE_MODULE:open(Real, [read,compressed]),
?line try_read_file_list(Fd).
read_compressed_cooked_binary(Config) when is_list(Config) ->
?line Data = ?config(data_dir, Config),
?line Real = filename:join(Data, "realmen.html.gz"),
?line {ok, Fd} = ?FILE_MODULE:open(Real, [read,compressed,binary]),
?line try_read_file_binary(Fd).
%% Trying reading and positioning from an uncompressed file,
%% but with the compressed flag given.
read_not_really_compressed(Config) when is_list(Config) ->
?line Data = ?config(data_dir, Config),
?line Priv = ?config(priv_dir, Config),
%% The file realmen.html might have got CRs added (by WinZip).
%% Remove them, or the file positions will not be correct.
?line Real = filename:join(Data, "realmen.html"),
?line RealPriv = filename:join(Priv,
atom_to_list(?MODULE)++"_realmen.html"),
?line {ok, RealDataBin} = ?FILE_MODULE:read_file(Real),
?line RealData = remove_crs(binary_to_list(RealDataBin), []),
?line ok = ?FILE_MODULE:write_file(RealPriv, RealData),
?line {ok, Fd} = ?FILE_MODULE:open(RealPriv, [read, compressed]),
?line try_read_file_list(Fd).
remove_crs([$\r|Rest], Result) ->
remove_crs(Rest, Result);
remove_crs([C|Rest], Result) ->
remove_crs(Rest, [C|Result]);
remove_crs([], Result) ->
lists:reverse(Result).
try_read_file_list(Fd) ->
%% Seek to the current position (nothing should happen).
?line {ok, 0} = ?FILE_MODULE:position(Fd, 0),
?line {ok, 0} = ?FILE_MODULE:position(Fd, {cur, 0}),
%% Read a few lines from a compressed file.
?line ShouldBe = "<TITLE>Real Programmers Don't Use PASCAL</TITLE>\n",
?line ShouldBe = io:get_line(Fd, ''),
%% Now seek forward.
?line {ok, 381} = ?FILE_MODULE:position(Fd, 381),
?line Back = "Back in the good old days -- the \"Golden Era\" " ++
"of computers, it was\n",
?line Back = io:get_line(Fd, ''),
%% Try to search forward relative to the current position.
?line {ok, CurPos} = ?FILE_MODULE:position(Fd, {cur, 0}),
?line RealPos = 4273,
?line {ok, RealPos} = ?FILE_MODULE:position(Fd, {cur, RealPos-CurPos}),
?line RealProg = "<LI> Real Programmers aren't afraid to use GOTOs.\n",
?line RealProg = io:get_line(Fd, ''),
%% Seek backward.
?line AfterTitle = length("<TITLE>"),
?line {ok, AfterTitle} = ?FILE_MODULE:position(Fd, AfterTitle),
?line Title = "Real Programmers Don't Use PASCAL</TITLE>\n",
?line Title = io:get_line(Fd, ''),
%% Seek past the end of the file.
?line {ok, _} = ?FILE_MODULE:position(Fd, 25000),
%% Done.
?line ?FILE_MODULE:close(Fd),
?line [] = flush(),
ok.
try_read_file_binary(Fd) ->
%% Seek to the current position (nothing should happen).
?line {ok, 0} = ?FILE_MODULE:position(Fd, 0),
?line {ok, 0} = ?FILE_MODULE:position(Fd, {cur, 0}),
%% Read a few lines from a compressed file.
?line ShouldBe = <<"<TITLE>Real Programmers Don't Use PASCAL</TITLE>\n">>,
?line ShouldBe = io:get_line(Fd, ''),
%% Now seek forward.
?line {ok, 381} = ?FILE_MODULE:position(Fd, 381),
?line Back = <<"Back in the good old days -- the \"Golden Era\" "
"of computers, it was\n">>,
?line Back = io:get_line(Fd, ''),
%% Try to search forward relative to the current position.
?line {ok, CurPos} = ?FILE_MODULE:position(Fd, {cur, 0}),
?line RealPos = 4273,
?line {ok, RealPos} = ?FILE_MODULE:position(Fd, {cur, RealPos-CurPos}),
?line RealProg = <<"<LI> Real Programmers aren't afraid to use GOTOs.\n">>,
?line RealProg = io:get_line(Fd, ''),
%% Seek backward.
?line AfterTitle = length("<TITLE>"),
?line {ok, AfterTitle} = ?FILE_MODULE:position(Fd, AfterTitle),
?line Title = <<"Real Programmers Don't Use PASCAL</TITLE>\n">>,
?line Title = io:get_line(Fd, ''),
%% Done.
?line ?FILE_MODULE:close(Fd),
?line [] = flush(),
ok.
read_cooked_tar_problem(Config) when is_list(Config) ->
?line Data = ?config(data_dir, Config),
?line ProblemFile = filename:join(Data, "cooked_tar_problem.tar.gz"),
?line {ok,Fd} = ?FILE_MODULE:open(ProblemFile, [read,compressed,binary]),
?line {ok,34304} = file:position(Fd, 34304),
?line {ok,Bin} = file:read(Fd, 512),
?line 512 = byte_size(Bin),
?line {ok,34304+512+1024} = file:position(Fd, {cur,1024}),
?line ok = file:close(Fd),
ok.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
write_compressed(suite) -> [];
write_compressed(doc) -> [];
write_compressed(Config) when is_list(Config) ->
?line Priv = ?config(priv_dir, Config),
?line MyFile = filename:join(Priv,
atom_to_list(?MODULE)++"_test.gz"),
%% Write a file.
?line {ok, Fd} = ?FILE_MODULE:open(MyFile, [write, compressed]),
?line {ok, 0} = ?FILE_MODULE:position(Fd, 0),
?line Prefix = "hello\n",
?line End = "end\n",
?line ok = io:put_chars(Fd, Prefix),
?line {ok, 143} = ?FILE_MODULE:position(Fd, 143),
?line ok = io:put_chars(Fd, End),
?line ok = ?FILE_MODULE:close(Fd),
%% Read the file and verify the contents.
?line {ok, Fd1} = ?FILE_MODULE:open(MyFile, [read, compressed]),
?line Prefix = io:get_line(Fd1, ''),
?line Second = lists:duplicate(143-length(Prefix), 0) ++ End,
?line Second = io:get_line(Fd1, ''),
?line ok = ?FILE_MODULE:close(Fd1),
%% Verify successful compression by uncompressing the file
%% using zlib:gunzip/1.
?line {ok,Contents} = file:read_file(MyFile),
?line <<"hello\n",0:137/unit:8,"end\n">> = zlib:gunzip(Contents),
%% Ensure that the file is compressed.
TotalSize = 143 + length(End),
case ?FILE_MODULE:read_file_info(MyFile) of
{ok, #file_info{size=Size}} when Size < TotalSize ->
ok;
{ok, #file_info{size=Size}} when Size == TotalSize ->
test_server:fail(file_not_compressed)
end,
%% Write again to ensure that the file is truncated.
?line {ok, Fd2} = ?FILE_MODULE:open(MyFile, [write, compressed]),
?line NewString = "aaaaaaaaaaa",
?line ok = io:put_chars(Fd2, NewString),
?line ok = ?FILE_MODULE:close(Fd2),
?line {ok, Fd3} = ?FILE_MODULE:open(MyFile, [read, compressed]),
?line {ok, NewString} = ?FILE_MODULE:read(Fd3, 1024),
?line ok = ?FILE_MODULE:close(Fd3),
%% Done.
?line [] = flush(),
ok.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
catenated_gzips(Config) when is_list(Config) ->
?line Priv = ?config(priv_dir, Config),
?line MyFile = filename:join(Priv, ?MODULE_STRING++"_test.gz"),
First = "Hello, all good men going to search parties. ",
Second = "Now I really need your help.",
All = iolist_to_binary([First|Second]),
?line Cat = [zlib:gzip(First),zlib:gzip(Second)],
?line ok = file:write_file(MyFile, Cat),
?line {ok,Fd} = file:open(MyFile, [read,compressed,binary]),
?line {ok,All} = file:read(Fd, 100000),
?line ok = file:close(Fd),
ok.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
compress_errors(suite) -> [];
compress_errors(doc) -> [];
compress_errors(Config) when is_list(Config) ->
?line DataDir =
filename:dirname(
filename:join(?config(data_dir, Config), "x")),
?line DataDirSlash = DataDir++"/",
?line {error, enoent} = ?FILE_MODULE:open("non_existing__",
[compressed, read]),
?line {error, einval} = ?FILE_MODULE:open("non_existing__",
[compressed, read, write]),
?line {error, einval} = ?FILE_MODULE:open("non_existing__",
[compressed, read, append]),
?line {error, einval} = ?FILE_MODULE:open("non_existing__",
[compressed, write, append]),
?line {error, E1} = ?FILE_MODULE:open(DataDir, [compressed, read]),
?line {error, E2} = ?FILE_MODULE:open(DataDirSlash, [compressed, read]),
?line {error, E3} = ?FILE_MODULE:open(DataDir, [compressed, write]),
?line {error, E4} = ?FILE_MODULE:open(DataDirSlash, [compressed, write]),
?line {eisdir,eisdir,eisdir,eisdir} = {E1,E2,E3,E4},
%% Read a corrupted .gz file.
?line Corrupted = filename:join(DataDir, "corrupted.gz"),
?line {ok, Fd} = ?FILE_MODULE:open(Corrupted, [read, compressed]),
?line {error, eio} = ?FILE_MODULE:read(Fd, 100),
?line ?FILE_MODULE:close(Fd),
?line [] = flush(),
ok.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
compress_async_crash(suite) -> [];
compress_async_crash(doc) -> [];
compress_async_crash(Config) when is_list(Config) ->
?line DataDir = ?config(data_dir, Config),
?line Path = filename:join(DataDir, "test.gz"),
ExpectedData = <<"qwerty">>,
?line _ = ?FILE_MODULE:delete(Path),
?line {ok, Fd} = ?FILE_MODULE:open(Path, [write, binary, compressed]),
?line ok = ?FILE_MODULE:write(Fd, ExpectedData),
?line ok = ?FILE_MODULE:close(Fd),
% Test that when using async thread pool, the emulator doesn't crash
% when the efile port driver is stopped while a compressed file operation
% is in progress (being carried by an async thread).
?line ok = compress_async_crash_loop(10000, Path, ExpectedData),
?line ok = ?FILE_MODULE:delete(Path),
ok.
compress_async_crash_loop(0, _Path, _ExpectedData) ->
ok;
compress_async_crash_loop(N, Path, ExpectedData) ->
Parent = self(),
{Pid, Ref} = spawn_monitor(
fun() ->
?line {ok, Fd} = ?FILE_MODULE:open(
Path, [read, compressed, raw, binary]),
Len = byte_size(ExpectedData),
Parent ! {self(), continue},
?line {ok, ExpectedData} = ?FILE_MODULE:read(Fd, Len),
?line ok = ?FILE_MODULE:close(Fd),
receive foobar -> ok end
end),
receive
{Pid, continue} ->
exit(Pid, shutdown),
receive
{'DOWN', Ref, _, _, Reason} ->
?line shutdown = Reason
end;
{'DOWN', Ref, _, _, Reason2} ->
test_server:fail({worker_exited, Reason2})
after 60000 ->
exit(Pid, shutdown),
erlang:demonitor(Ref, [flush]),
test_server:fail(worker_timeout)
end,
compress_async_crash_loop(N - 1, Path, ExpectedData).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
unicode(Config) when is_list(Config) ->
Dir = ?config(priv_dir, Config),
Name = filename:join(Dir, "data-utf8.txt"),
Txt = lists:seq(128, 255),
D = unicode:characters_to_binary(Txt, latin1, latin1),
{ok,Fd1} =
?FILE_MODULE:open(Name, [write,read,binary,{encoding,unicode}]),
ok = ?FILE_MODULE:truncate(Fd1),
ok = ?FILE_MODULE:write(Fd1, Txt),
{ok,0} = ?FILE_MODULE:position(Fd1, bof),
{ok,D} = ?FILE_MODULE:read(Fd1, 129),
{ok,0} = ?FILE_MODULE:position(Fd1, bof),
{ok,D1} = ?FILE_MODULE:read(Fd1, 64),
{ok,Pos} = ?FILE_MODULE:position(Fd1, cur),
{ok,D2} = ?FILE_MODULE:pread(Fd1, {cur,0}, 65),
D = <<D1/binary, D2/binary>>,
{ok,D1} = ?FILE_MODULE:pread(Fd1, bof, 64),
{ok,Pos} = ?FILE_MODULE:position(Fd1, Pos),
{ok,D2} = ?FILE_MODULE:read(Fd1, 64),
ok = ?FILE_MODULE:close(Fd1),
%%
RawD = unicode:characters_to_binary(Txt, latin1, unicode),
{ok,RawD} = ?FILE_MODULE:read_file(Name),
%%
{ok,Fd2} = ?FILE_MODULE:open(Name, [read,{encoding,unicode}]),
{ok,Txt} = ?FILE_MODULE:read(Fd2, 129),
{Txt1,Txt2} = lists:split(64, Txt),
{ok,Txt2} = ?FILE_MODULE:pread(Fd2, Pos, 65),
{ok,0} = ?FILE_MODULE:position(Fd2, bof),
{ok,Txt1} = ?FILE_MODULE:read(Fd2, 64),
ok = ?FILE_MODULE:close(Fd2).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
altname(doc) ->
"Test the file:altname/1 function";
altname(suite) ->
[];
altname(Config) when is_list(Config) ->
?line RootDir = ?config(priv_dir, Config),
?line NewDir = filename:join(RootDir,
"long alternative path name with spaces"),
?line ok = ?FILE_MODULE:make_dir(NewDir),
?line Name = filename:join(NewDir, "a_file_with_long_name"),
?line ShortName = filename:join(NewDir, "short"),
?line NonexName = filename:join(NewDir, "nonexistent"),
?line ok = ?FILE_MODULE:write_file(Name, "some contents\n"),
?line ok = ?FILE_MODULE:write_file(ShortName, "some contents\n"),
?line Result =
case ?FILE_MODULE:altname(NewDir) of
{error, enotsup} ->
{skipped, "Altname not supported on this platform"};
{ok, "LONGAL~1"} ->
?line {ok, "A_FILE~1"} = ?FILE_MODULE:altname(Name),
?line {ok, "C:/"} = ?FILE_MODULE:altname("C:/"),
?line {ok, "C:\\"} = ?FILE_MODULE:altname("C:\\"),
?line {error,enoent} = ?FILE_MODULE:altname(NonexName),
?line {ok, "short"} = ?FILE_MODULE:altname(ShortName),
ok
end,
?line [] = flush(),
Result.
make_link(doc) -> "Test creating a hard link.";
make_link(suite) -> [];
make_link(Config) when is_list(Config) ->
?line RootDir = ?config(priv_dir, Config),
?line NewDir = filename:join(RootDir,
atom_to_list(?MODULE)
++"_make_link"),
?line ok = ?FILE_MODULE:make_dir(NewDir),
?line Name = filename:join(NewDir, "a_file"),
?line ok = ?FILE_MODULE:write_file(Name, "some contents\n"),
?line Alias = filename:join(NewDir, "an_alias"),
?line Result =
case ?FILE_MODULE:make_link(Name, Alias) of
{error, enotsup} ->
{skipped, "Links not supported on this platform"};
ok ->
%% Note: We take the opportunity to test
%% ?FILE_MODULE:read_link_info/1,
%% which should in behave exactly as
%% ?FILE_MODULE:read_file_info/1
%% since they are not used on symbolic links.
?line {ok, Info} = ?FILE_MODULE:read_link_info(Name),
{ok,Info} = ?FILE_MODULE:read_link_info(Name, [raw]),
?line {ok, Info} = ?FILE_MODULE:read_link_info(Alias),
{ok,Info} = ?FILE_MODULE:read_link_info(Alias, [raw]),
?line #file_info{links = 2, type = regular} = Info,
?line {error, eexist} =
?FILE_MODULE:make_link(Name, Alias),
ok
end,
?line [] = flush(),
Result.
read_link_info_for_non_link(doc) ->
"Test that reading link info for an ordinary file or directory works "
"(on all platforms).";
read_link_info_for_non_link(suite) -> [];
read_link_info_for_non_link(Config) when is_list(Config) ->
?line {ok, #file_info{type=directory}} =
?FILE_MODULE:read_link_info("."),
{ok, #file_info{type=directory}} = ?FILE_MODULE:read_link_info(".", [raw]),
?line [] = flush(),
ok.
symlinks(doc) -> "Test operations on symbolic links (for Unix).";
symlinks(suite) -> [];
symlinks(Config) when is_list(Config) ->
?line {error, _} = ?FILE_MODULE:read_link(lists:duplicate(10000,$a)),
{error, _} = ?FILE_MODULE:read_link_all(lists:duplicate(10000,$a)),
?line RootDir = ?config(priv_dir, Config),
?line NewDir = filename:join(RootDir,
atom_to_list(?MODULE)
++"_symlinks"),
?line ok = ?FILE_MODULE:make_dir(NewDir),
?line Name = filename:join(NewDir, "a_plain_file"),
?line ok = ?FILE_MODULE:write_file(Name, "some stupid content\n"),
?line Alias = filename:join(NewDir, "a_symlink_alias"),
?line Result =
case ?FILE_MODULE:make_symlink(Name, Alias) of
{error, enotsup} ->
{skipped, "Links not supported on this platform"};
{error, eperm} ->
{win32,_} = os:type(),
{skipped, "Windows user not privileged to create symlinks"};
ok ->
?line {ok, Info1} = ?FILE_MODULE:read_file_info(Name),
{ok,Info1} = ?FILE_MODULE:read_file_info(Name, [raw]),
?line {ok, Info1} = ?FILE_MODULE:read_file_info(Alias),
{ok,Info1} = ?FILE_MODULE:read_file_info(Alias, [raw]),
?line {ok, Info1} = ?FILE_MODULE:read_link_info(Name),
{ok,Info1} = ?FILE_MODULE:read_link_info(Name, [raw]),
?line #file_info{links = 1, type = regular} = Info1,
?line {ok, Info2} = ?FILE_MODULE:read_link_info(Alias),
{ok,Info2} = ?FILE_MODULE:read_link_info(Alias, [raw]),
?line #file_info{links=1, type=symlink} = Info2,
?line {ok, Name} = ?FILE_MODULE:read_link(Alias),
{ok, Name} = ?FILE_MODULE:read_link_all(Alias),
%% If all is good, delete dir again (avoid hanging dir on windows)
rm_rf(?FILE_MODULE,NewDir),
ok
end,
?line [] = flush(),
Result.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
copy(doc) -> [];
copy(suite) -> [];
copy(Config) when is_list(Config) ->
?line RootDir = ?config(priv_dir, Config),
%% Create a text file.
?line Name1 = filename:join(RootDir, atom_to_list(?MODULE)++"_copy_1.txt"),
?line Line = "The quick brown fox jumps over a lazy dog. 0123456789\n",
?line Len = length(Line),
?line {ok, Handle1} = ?FILE_MODULE:open(Name1, [write]),
?line {_, Size1} =
iterate({0, 0},
done,
fun({_, S}) when S >= 128*1024 ->
done;
({N, S}) ->
H = integer_to_list(N),
ok = ?FILE_MODULE:write(Handle1, [H, " ", Line]),
{N + 1, S + length(H) + 1 + Len}
end),
?line ?FILE_MODULE:close(Handle1),
%% Make a copy
?line Name2 = filename:join(RootDir, atom_to_list(?MODULE)++"_copy_2.txt"),
?line {ok, Size1} = ?FILE_MODULE:copy(Name1, Name2),
%% Concatenate 1
?line Name3 = filename:join(RootDir, atom_to_list(?MODULE)++"_copy_3.txt"),
?line {ok, Handle3} = ?FILE_MODULE:open(Name3, [raw, write, binary]),
?line {ok, Size1} = ?FILE_MODULE:copy(Name1, Handle3),
?line {ok, Handle2} = ?FILE_MODULE:open(Name2, [read, binary]),
?line {ok, Size1} = ?FILE_MODULE:copy(Handle2, Handle3),
?line ok = ?FILE_MODULE:close(Handle3),
?line ok = ?FILE_MODULE:close(Handle2),
%% Concatenate 2
?line Name4 = filename:join(RootDir, atom_to_list(?MODULE)++"_copy_4.txt"),
?line {ok, Handle4} = ?FILE_MODULE:open(Name4, [write, binary]),
?line {ok, Size1} = ?FILE_MODULE:copy(Name1, Handle4),
?line {ok, Handle5} = ?FILE_MODULE:open(Name2, [raw, read, binary]),
?line {ok, Size1} = ?FILE_MODULE:copy(Handle5, Handle4),
?line ok = ?FILE_MODULE:close(Handle5),
?line ok = ?FILE_MODULE:close(Handle4),
%% %% Just for test of the test
%% ?line {ok, Handle2q} = ?FILE_MODULE:open(Name2, [write, append]),
%% ?line ok = ?FILE_MODULE:write(Handle2q, "q"),
%% ?line ok = ?FILE_MODULE:close(Handle2q),
%% Compare the files
?line {ok, Handle1a} = ?FILE_MODULE:open(Name1, [raw, read]),
?line {ok, Handle2a} = ?FILE_MODULE:open(Name2, [raw, read]),
?line true = stream_cmp(fd_stream_factory([Handle1a]),
fd_stream_factory([Handle2a])),
?line {ok, 0} = ?FILE_MODULE:position(Handle1a, 0),
?line {ok, 0} = ?FILE_MODULE:position(Handle2a, 0),
?line {ok, Handle3a} = ?FILE_MODULE:open(Name3, [raw, read]),
?line true = stream_cmp(fd_stream_factory([Handle1a, Handle2a]),
fd_stream_factory([Handle2a])),
?line ok = ?FILE_MODULE:close(Handle1a),
?line ok = ?FILE_MODULE:close(Handle2a),
?line ok = ?FILE_MODULE:close(Handle3a),
?line [] = flush(),
ok.
fd_stream_factory([]) ->
[];
fd_stream_factory([Fd | T] = L) ->
fun() ->
case ?FILE_MODULE:read(Fd, 8192) of
{ok, Data} when is_binary(Data) ->
binary_to_list(Data) ++ fd_stream_factory(L);
{ok, Data} when is_list(Data) ->
Data ++ fd_stream_factory(L);
eof ->
fd_stream_factory(T);
{error, _} = Error ->
Error
end
end.
stream_cmp(F1, F2) when is_function(F1), is_function(F2) ->
stream_cmp(F1(), F2());
stream_cmp(F, X) when is_function(F) ->
stream_cmp(F(), X);
stream_cmp(X, F) when is_function(F) ->
stream_cmp(X, F());
stream_cmp({error, _} = Error, _) ->
Error;
stream_cmp(_, {error, _} = Error) ->
Error;
stream_cmp([], []) ->
true;
stream_cmp([], [_|_]) ->
false;
stream_cmp([_|_], []) ->
false;
stream_cmp([H | T1], [H | T2]) ->
stream_cmp(T1, T2).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Test the get_cwd(), open(), and copy() file server calls.
new_slave(_RootDir, Cwd) ->
?line L = "qwertyuiopasdfghjklzxcvbnm",
?line N = length(L),
?line {ok, Cwd} = ?FILE_MODULE:get_cwd(),
?line {error, enotsup} = ?FILE_MODULE:get_cwd("C:"), % Unix only testcase
?line {ok, FD1} = ?FILE_MODULE:open("file1.txt", write),
?line ok = ?FILE_MODULE:close(FD1),
?line {ok, FD2} = ?FILE_MODULE:open("file1.txt",
[write, append,
binary, compressed,
delayed_write,
{delayed_write, 0, 0},
read_ahead,
{read_ahead, 0}]),
?line ok = ?FILE_MODULE:write(FD2, L),
?line ok = ?FILE_MODULE:close(FD2),
?line {ok, N2} = ?FILE_MODULE:copy("file1.txt", "file2.txt"),
?line io:format("Size ~p, compressed ~p.~n", [N, N2]),
?line {ok, FD3} = ?FILE_MODULE:open("file2.txt",
[binary, compressed]),
%% The file_io_server will translate the binary into a list
?line {ok, L} = ?FILE_MODULE:read(FD3, N+1),
?line ok = ?FILE_MODULE:close(FD3),
%%
?line ok = ?FILE_MODULE:delete("file1.txt"),
?line ok = ?FILE_MODULE:delete("file2.txt"),
?line [] = flush(),
ok.
%% Test the get_cwd() and open() file server calls.
old_slave(_RootDir, Cwd) ->
?line L = "qwertyuiopasdfghjklzxcvbnm",
?line N = length(L),
?line {ok, Cwd} = ?FILE_MODULE:get_cwd(),
?line {error, enotsup} = ?FILE_MODULE:get_cwd("C:"), % Unix only testcase
?line {ok, FD1} = ?FILE_MODULE:open("file1.txt", write),
?line ok = ?FILE_MODULE:close(FD1),
?line {ok, FD2} = ?FILE_MODULE:open("file1.txt",
[write, binary, compressed]),
?line ok = ?FILE_MODULE:write(FD2, L),
?line ok = ?FILE_MODULE:close(FD2),
?line {ok, FD3} = ?FILE_MODULE:open("file1.txt", [write, append]),
?line ok = ?FILE_MODULE:close(FD3),
?line {ok, FD4} = ?FILE_MODULE:open("file1.txt",
[binary, compressed]),
%% The file_io_server will translate the binary into a list
?line {ok, L} = ?FILE_MODULE:read(FD4, N+1),
?line ok = ?FILE_MODULE:close(FD4),
%%
?line ok = ?FILE_MODULE:delete("file1.txt"),
?line [] = flush(),
ok.
run_test(Test, Args) ->
?line case (catch apply(?MODULE, Test, Args)) of
{'EXIT', _} = Exit ->
{done, Exit, get(test_server_loc)};
Result ->
{done, Result}
end.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
delayed_write(suite) ->
[];
delayed_write(doc) ->
["Tests the file open option {delayed_write, Size, Delay}"];
delayed_write(Config) when is_list(Config) ->
RootDir = ?config(priv_dir, Config),
File = filename:join(RootDir,
atom_to_list(?MODULE)++"_delayed_write.txt"),
Data1 = "asdfghjkl",
Data2 = "qwertyuio",
Data3 = "zxcvbnm,.",
Size = length(Data1),
Size = length(Data2),
Size = length(Data3),
Data1Data1 = Data1++Data1,
Data1Data1Data1 = Data1Data1++Data1,
Data1Data1Data1Data1 = Data1Data1++Data1Data1,
%%
%% Test caching and normal close of non-raw file
{ok, Fd1} =
?FILE_MODULE:open(File, [write, {delayed_write, Size+1, 2000}]),
ok = ?FILE_MODULE:write(Fd1, Data1),
?t:sleep(1000), % Just in case the file system is slow
{ok, Fd2} = ?FILE_MODULE:open(File, [read]),
eof = ?FILE_MODULE:read(Fd2, 1),
ok = ?FILE_MODULE:write(Fd1, Data1), % Data flush on size
?t:sleep(1000), % Just in case the file system is slow
{ok, Data1Data1} = ?FILE_MODULE:pread(Fd2, bof, 2*Size+1),
ok = ?FILE_MODULE:write(Fd1, Data1),
?t:sleep(3000), % Wait until data flush on timeout
{ok, Data1Data1Data1} = ?FILE_MODULE:pread(Fd2, bof, 3*Size+1),
ok = ?FILE_MODULE:write(Fd1, Data1),
ok = ?FILE_MODULE:close(Fd1), % Data flush on close
?t:sleep(1000), % Just in case the file system is slow
{ok, Data1Data1Data1Data1} = ?FILE_MODULE:pread(Fd2, bof, 4*Size+1),
ok = ?FILE_MODULE:close(Fd2),
%%
%% Test implicit close through exit by file owning process,
%% raw file, default parameters.
Parent = self(),
Fun = fun() ->
Child = self(),
Test =
fun () ->
{ok, Fd} = ?FILE_MODULE:open(File,
[raw, write, delayed_write]),
ok = ?FILE_MODULE:write(Fd, Data1),
Parent ! {Child, wrote},
receive
{Parent, continue, Reason} ->
{ok, Reason}
end
end,
case (catch Test()) of
{ok, Reason} -> exit(Reason);
Unknown ->
exit({Unknown, get(test_server_loc)})
end
end,
Child1 = spawn(Fun),
Mref1 = erlang:monitor(process, Child1),
receive
{Child1, wrote} ->
ok;
{'DOWN', Mref1, _, _, _} = Down1a ->
?t:fail(Down1a)
end,
?t:sleep(1000), % Just in case the file system is slow
{ok, Fd3} = ?FILE_MODULE:open(File, [read]),
eof = ?FILE_MODULE:read(Fd3, 1),
Child1 ! {Parent, continue, normal},
receive
{'DOWN', Mref1, process, Child1, normal} ->
ok;
{'DOWN', Mref1, _, _, _} = Down1b ->
?t:fail(Down1b)
end,
?t:sleep(1000), % Just in case the file system is slow
{ok, Data1} = ?FILE_MODULE:pread(Fd3, bof, Size+1),
ok = ?FILE_MODULE:close(Fd3),
%%
%% The same again, but this time with reason 'kill'.
Child2 = spawn(Fun),
Mref2 = erlang:monitor(process, Child2),
receive
{Child2, wrote} ->
ok;
{'DOWN', Mref2, _, _, _} = Down2a ->
?t:fail(Down2a)
end,
?t:sleep(1000), % Just in case the file system is slow
{ok, Fd4} = ?FILE_MODULE:open(File, [read]),
eof = ?FILE_MODULE:read(Fd4, 1),
Child2 ! {Parent, continue, kill},
receive
{'DOWN', Mref2, process, Child2, kill} ->
ok;
{'DOWN', Mref2, _, _, _} = Down2b ->
?t:fail(Down2b)
end,
?t:sleep(1000), % Just in case the file system is slow
eof = ?FILE_MODULE:pread(Fd4, bof, 1),
ok = ?FILE_MODULE:close(Fd4),
%%
%% Test if file position works with delayed_write
{ok, Fd5} = ?FILE_MODULE:open(File, [raw, read, write,
delayed_write]),
ok = ?FILE_MODULE:truncate(Fd5),
ok = ?FILE_MODULE:write(Fd5, [Data1|Data2]),
{ok, 0} = ?FILE_MODULE:position(Fd5, bof),
ok = ?FILE_MODULE:write(Fd5, [Data3]),
{ok, Data2} = ?FILE_MODULE:read(Fd5, Size+1),
{ok, 0} = ?FILE_MODULE:position(Fd5, bof),
Data3Data2 = Data3++Data2,
{ok, Data3Data2} = ?FILE_MODULE:read(Fd5, 2*Size+1),
ok = ?FILE_MODULE:close(Fd5),
%%
[] = flush(),
ok.
pid2name(doc) -> "Tests file:pid2name/1.";
pid2name(suite) -> [];
pid2name(Config) when is_list(Config) ->
?line RootDir = ?config(priv_dir, Config),
?line Base = test_server:temp_name(
filename:join(RootDir, "pid2name_")),
?line Name1 = [Base, '.txt'],
?line Name2 = Base ++ ".txt",
%%
?line {ok, Pid} = file:open(Name1, [write]),
?line {ok, Name2} = file:pid2name(Pid),
?line undefined = file:pid2name(self()),
?line ok = file:close(Pid),
?line test_server:sleep(1000),
?line false = is_process_alive(Pid),
?line undefined = file:pid2name(Pid),
ok.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
read_ahead(suite) ->
[];
read_ahead(doc) ->
["Tests the file open option {read_ahead, Size}"];
read_ahead(Config) when is_list(Config) ->
?line RootDir = ?config(priv_dir, Config),
?line File = filename:join(RootDir,
atom_to_list(?MODULE)++"_read_ahead.txt"),
?line Data1 = "asdfghjkl", % Must be
?line Data2 = "qwertyuio", % same
?line Data3 = "zxcvbnm,.", % length
?line Size = length(Data1),
?line Size = length(Data2),
?line Size = length(Data3),
%%
%% Test caching of normal non-raw file
?line {ok, Fd1} = ?FILE_MODULE:open(File, [write]),
?line ok = ?FILE_MODULE:write(Fd1, [Data1|Data1]),
?line ?t:sleep(1000), % Just in case the file system is slow
?line {ok, Fd2} = ?FILE_MODULE:open(File, [read, {read_ahead, 2*Size}]),
?line {ok, Data1} = ?FILE_MODULE:read(Fd2, Size),
?line ok = ?FILE_MODULE:pwrite(Fd1, Size, Data2),
?line ?t:sleep(1000), % Just in case the file system is slow
?line {ok, Data1} = ?FILE_MODULE:read(Fd2, Size), % Will read cached data
?line Data2Data2Data2 = Data2++Data2++Data2,
?line ok = ?FILE_MODULE:pwrite(Fd1, eof, Data2Data2Data2),
?line ?t:sleep(1000), % Just in case the file system is slow
?line {ok, Data2Data2Data2} =
?FILE_MODULE:read(Fd2, 3*Size), % Read more than cache buffer
?line ok = ?FILE_MODULE:close(Fd1),
?line ok = ?FILE_MODULE:close(Fd2),
%% Test caching of raw file and default parameters
?line {ok, Fd3} = ?FILE_MODULE:open(File, [raw, write]),
?line ok = ?FILE_MODULE:write(Fd3, [Data1|Data1]),
?line ?t:sleep(1000), % Just in case the file system is slow
?line {ok, Fd4} = ?FILE_MODULE:open(File, [raw, read, read_ahead]),
?line {ok, Data1} = ?FILE_MODULE:read(Fd4, Size),
?line ok = ?FILE_MODULE:pwrite(Fd3, Size, Data2),
?line ?t:sleep(1000), % Just in case the file system is slow
?line {ok, Data1} = ?FILE_MODULE:read(Fd4, Size), % Will read cached data
?line ok = ?FILE_MODULE:close(Fd3),
?line ok = ?FILE_MODULE:close(Fd4),
%% Test if the file position works in combination with read_ahead
?line {ok, Fd5} = ?FILE_MODULE:open(File, [raw, read, write, read_ahead]),
?line ok = ?FILE_MODULE:truncate(Fd5),
?line ok = ?FILE_MODULE:write(Fd5, [Data1,Data1|Data3]),
?line {ok, 0} = ?FILE_MODULE:position(Fd5, bof),
?line {ok, Data1} = ?FILE_MODULE:read(Fd5, Size),
?line ok = ?FILE_MODULE:write(Fd5, Data2),
?line {ok, 0} = ?FILE_MODULE:position(Fd5, bof),
?line Data1Data2Data3 = Data1++Data2++Data3,
?line {ok, Data1Data2Data3} = ?FILE_MODULE:read(Fd5, 3*Size+1),
?line ok = ?FILE_MODULE:close(Fd5),
%%
?line [] = flush(),
ok.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
segment_read(suite) ->
[];
segment_read(doc) ->
["Tests the segmenting of large reads"];
segment_read(Config) when is_list(Config) ->
?line Name = filename:join(?config(priv_dir, Config),
?MODULE_STRING ++ "_segment_read"),
?line SegSize = 256*1024,
?line SegCnt = SegSize div 4,
?line Cnt = 4 * SegCnt,
?line ok = create_file(Name, Cnt),
%%
%% read_file/1
%%
?line {ok, Bin} = ?FILE_MODULE:read_file(Name),
?line true = verify_bin(Bin, 0, Cnt),
%%
%% read/2
%%
%% Not segmented
?line {ok, FD1} = ?FILE_MODULE:open(Name, [read, raw, binary]),
?line {ok, B1a} = ?FILE_MODULE:read(FD1, SegSize),
?line {ok, B1b} = ?FILE_MODULE:read(FD1, SegSize),
?line {ok, B1c} = ?FILE_MODULE:read(FD1, SegSize),
?line {ok, B1d} = ?FILE_MODULE:read(FD1, SegSize),
?line ok = ?FILE_MODULE:close(FD1),
?line true = verify_bin(B1a, 0*SegCnt, SegCnt),
?line true = verify_bin(B1b, 1*SegCnt, SegCnt),
?line true = verify_bin(B1c, 2*SegCnt, SegCnt),
?line true = verify_bin(B1d, 3*SegCnt, SegCnt),
%%
%% Segmented
?line {ok, FD2} = ?FILE_MODULE:open(Name, [read, raw, binary]),
?line {ok, B2a} = ?FILE_MODULE:read(FD2, 1*SegSize),
?line {ok, B2b} = ?FILE_MODULE:read(FD2, 2*SegSize),
?line {ok, B2c} = ?FILE_MODULE:read(FD2, 2*SegSize),
?line ok = ?FILE_MODULE:close(FD2),
?line true = verify_bin(B2a, 0*SegCnt, 1*SegCnt),
?line true = verify_bin(B2b, 1*SegCnt, 2*SegCnt),
?line true = verify_bin(B2c, 3*SegCnt, 1*SegCnt),
%%
%% pread/3
%%
?line {ok, FD3} = ?FILE_MODULE:open(Name, [read, raw, binary]),
%%
%% Not segmented
?line {ok, B3d} = ?FILE_MODULE:pread(FD3, 3*SegSize, SegSize),
?line {ok, B3c} = ?FILE_MODULE:pread(FD3, 2*SegSize, SegSize),
?line {ok, B3b} = ?FILE_MODULE:pread(FD3, 1*SegSize, SegSize),
?line {ok, B3a} = ?FILE_MODULE:pread(FD3, 0*SegSize, SegSize),
?line true = verify_bin(B3a, 0*SegCnt, SegCnt),
?line true = verify_bin(B3b, 1*SegCnt, SegCnt),
?line true = verify_bin(B3c, 2*SegCnt, SegCnt),
?line true = verify_bin(B3d, 3*SegCnt, SegCnt),
%%
%% Segmented
?line {ok, B3g} = ?FILE_MODULE:pread(FD3, 3*SegSize, 2*SegSize),
?line {ok, B3f} = ?FILE_MODULE:pread(FD3, 1*SegSize, 2*SegSize),
?line {ok, B3e} = ?FILE_MODULE:pread(FD3, 0*SegSize, 1*SegSize),
?line true = verify_bin(B3e, 0*SegCnt, 1*SegCnt),
?line true = verify_bin(B3f, 1*SegCnt, 2*SegCnt),
?line true = verify_bin(B3g, 3*SegCnt, 1*SegCnt),
%%
?line ok = ?FILE_MODULE:close(FD3),
%%
%% pread/2
%%
?line {ok, FD5} = ?FILE_MODULE:open(Name, [read, raw, binary]),
%%
%% +---+---+---+---+
%% | 4 | 3 | 2 | 1 |
%% +---+---+---+---+
%% < ^ >
?line {ok, [B5d, B5c, B5b, B5a]} =
?FILE_MODULE:pread(FD5, [{3*SegSize, SegSize},
{2*SegSize, SegSize},
{1*SegSize, SegSize},
{0*SegSize, SegSize}]),
?line true = verify_bin(B5a, 0*SegCnt, SegCnt),
?line true = verify_bin(B5b, 1*SegCnt, SegCnt),
?line true = verify_bin(B5c, 2*SegCnt, SegCnt),
?line true = verify_bin(B5d, 3*SegCnt, SegCnt),
%%
%% +---+-------+-------+
%% | 3 | 2 | 1 |
%% +---+-------+-------+
%% < ^ ^ >
?line {ok, [B5g, B5f, B5e]} =
?FILE_MODULE:pread(FD5, [{3*SegSize, 2*SegSize},
{1*SegSize, 2*SegSize},
{0*SegSize, 1*SegSize}]),
?line true = verify_bin(B5e, 0*SegCnt, 1*SegCnt),
?line true = verify_bin(B5f, 1*SegCnt, 2*SegCnt),
?line true = verify_bin(B5g, 3*SegCnt, 1*SegCnt),
%%
%%
%% +-------+-----------+
%% | 2 | 1 |
%% +-------+-----------+
%% < ^ ^ >
?line {ok, [B5i, B5h]} =
?FILE_MODULE:pread(FD5, [{2*SegSize, 3*SegSize},
{0*SegSize, 2*SegSize}]),
?line true = verify_bin(B5h, 0*SegCnt, 2*SegCnt),
?line true = verify_bin(B5i, 2*SegCnt, 2*SegCnt),
%%
%% +-------+---+---+
%% | 3 | 2 | 1 |
%% +-------+---+---+
%% < ^ ^ >
?line {ok, [B5l, B5k, B5j]} =
?FILE_MODULE:pread(FD5, [{3*SegSize, 1*SegSize},
{2*SegSize, 1*SegSize},
{0*SegSize, 2*SegSize}]),
?line true = verify_bin(B5j, 0*SegCnt, 2*SegCnt),
?line true = verify_bin(B5k, 2*SegCnt, 1*SegCnt),
?line true = verify_bin(B5l, 3*SegCnt, 1*SegCnt),
%%
%% Real time response time test.
%%
Req = lists:flatten(lists:duplicate(17,
[{2*SegSize, 2*SegSize},
{0*SegSize, 2*SegSize}])),
?line {{ok, _}, Comment} =
response_analysis(?FILE_MODULE, pread, [FD5, Req]),
?line ok = ?FILE_MODULE:close(FD5),
%%
?line [] = flush(),
{comment, Comment}.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
segment_write(suite) ->
[];
segment_write(doc) ->
["Tests the segmenting of large writes"];
segment_write(Config) when is_list(Config) ->
?line Name = filename:join(?config(priv_dir, Config),
?MODULE_STRING ++ "_segment_write"),
?line SegSize = 256*1024,
?line SegCnt = SegSize div 4,
?line Cnt = 4 * SegCnt,
?line Bin = create_bin(0, Cnt),
%%
%% write/2
%%
%% Not segmented
?line {ok, FD1} = ?FILE_MODULE:open(Name, [write, raw, binary]),
?line ok = ?FILE_MODULE:write(FD1, subbin(Bin, 0*SegSize, 1*SegSize)),
?line ok = ?FILE_MODULE:write(FD1, subbin(Bin, 1*SegSize, 1*SegSize)),
?line ok = ?FILE_MODULE:write(FD1, subbin(Bin, 2*SegSize, 1*SegSize)),
?line ok = ?FILE_MODULE:write(FD1, subbin(Bin, 3*SegSize, 1*SegSize)),
?line ok = ?FILE_MODULE:close(FD1),
?line true = verify_file(Name, Cnt),
%%
%% Segmented
?line {ok, FD2} = ?FILE_MODULE:open(Name, [write, raw, binary]),
?line ok = ?FILE_MODULE:write(FD2, subbin(Bin, 0*SegSize, 1*SegSize)),
?line ok = ?FILE_MODULE:write(FD2, subbin(Bin, 1*SegSize, 2*SegSize)),
?line ok = ?FILE_MODULE:write(FD2, subbin(Bin, 3*SegSize, 1*SegSize)),
?line ok = ?FILE_MODULE:close(FD2),
?line true = verify_file(Name, Cnt),
%%
%% +---+---+---+---+
%% | | | | |
%% +---+---+---+---+
%% < ^ >
?line ok = write_file(Name, [subbin(Bin, 0*SegSize, 1*SegSize),
subbin(Bin, 1*SegSize, 1*SegSize),
subbin(Bin, 2*SegSize, 1*SegSize),
subbin(Bin, 3*SegSize, 1*SegSize)]),
?line true = verify_file(Name, Cnt),
%%
%% +---+-------+---+
%% | | | |
%% +---+-------+---+
%% < ^ ^ >
?line ok = write_file(Name, [subbin(Bin, 0*SegSize, 1*SegSize),
subbin(Bin, 1*SegSize, 2*SegSize),
subbin(Bin, 3*SegSize, 1*SegSize)]),
?line true = verify_file(Name, Cnt),
%%
%% +-------+-------+
%% | | |
%% +-------+-------+
%% < ^ ^ >
?line ok = write_file(Name, [subbin(Bin, 0*SegSize, 2*SegSize),
subbin(Bin, 2*SegSize, 2*SegSize)]),
?line true = verify_file(Name, Cnt),
%%
%% +-------+---+---+
%% | | | |
%% +-------+---+---+
%% < ^ ^ >
?line ok = write_file(Name, [subbin(Bin, 0*SegSize, 2*SegSize),
subbin(Bin, 2*SegSize, 1*SegSize),
subbin(Bin, 3*SegSize, 1*SegSize)]),
?line true = verify_file(Name, Cnt),
%%
%% pwrite/3
%%
%% Not segmented
?line {ok, FD3} = ?FILE_MODULE:open(Name, [write, raw, binary]),
?line ok = ?FILE_MODULE:pwrite(FD3, 3*SegSize,
subbin(Bin, 3*SegSize, 1*SegSize)),
?line ok = ?FILE_MODULE:pwrite(FD3, 2*SegSize,
subbin(Bin, 2*SegSize, 1*SegSize)),
?line ok = ?FILE_MODULE:pwrite(FD3, 1*SegSize,
subbin(Bin, 1*SegSize, 1*SegSize)),
?line ok = ?FILE_MODULE:pwrite(FD3, 0*SegSize,
subbin(Bin, 0*SegSize, 1*SegSize)),
?line ok = ?FILE_MODULE:close(FD3),
?line true = verify_file(Name, Cnt),
%%
%% Segmented
?line {ok, FD4} = ?FILE_MODULE:open(Name, [write, raw, binary]),
?line ok = ?FILE_MODULE:pwrite(FD4, 3*SegSize,
subbin(Bin, 3*SegSize, 1*SegSize)),
?line ok = ?FILE_MODULE:pwrite(FD4, 1*SegSize,
subbin(Bin, 1*SegSize, 2*SegSize)),
?line ok = ?FILE_MODULE:pwrite(FD4, 0*SegSize,
subbin(Bin, 0*SegSize, 1*SegSize)),
?line ok = ?FILE_MODULE:close(FD4),
?line true = verify_file(Name, Cnt),
%%
%% pwrite/2
%%
%% Not segmented
?line {ok, FD5} = ?FILE_MODULE:open(Name, [write, raw, binary]),
?line ok = ?FILE_MODULE:pwrite(FD5, [{3*SegSize,
subbin(Bin, 3*SegSize, 1*SegSize)}]),
?line ok = ?FILE_MODULE:pwrite(FD5, [{2*SegSize,
subbin(Bin, 2*SegSize, 1*SegSize)}]),
?line ok = ?FILE_MODULE:pwrite(FD5, [{1*SegSize,
subbin(Bin, 1*SegSize, 1*SegSize)}]),
?line ok = ?FILE_MODULE:pwrite(FD5, [{0*SegSize,
subbin(Bin, 0*SegSize, 1*SegSize)}]),
?line ok = ?FILE_MODULE:close(FD5),
?line true = verify_file(Name, Cnt),
%%
%% Segmented
?line {ok, FD6} = ?FILE_MODULE:open(Name, [write, raw, binary]),
?line ok = ?FILE_MODULE:pwrite(FD6, [{3*SegSize,
subbin(Bin, 3*SegSize, 1*SegSize)}]),
?line ok = ?FILE_MODULE:pwrite(FD6, [{1*SegSize,
subbin(Bin, 1*SegSize, 2*SegSize)}]),
?line ok = ?FILE_MODULE:pwrite(FD6, [{0*SegSize,
subbin(Bin, 0*SegSize, 1*SegSize)}]),
?line ok = ?FILE_MODULE:close(FD6),
?line true = verify_file(Name, Cnt),
%%
%% +---+---+---+---+
%% | 4 | 3 | 2 | 1 |
%% +---+---+---+---+
%% < ^ >
?line ok = pwrite_file(Name, [{3*SegSize,
subbin(Bin, 3*SegSize, 1*SegSize)},
{2*SegSize,
subbin(Bin, 2*SegSize, 1*SegSize)},
{1*SegSize,
subbin(Bin, 1*SegSize, 1*SegSize)},
{0*SegSize,
subbin(Bin, 0*SegSize, 1*SegSize)}]),
?line true = verify_file(Name, Cnt),
%%
%% +---+-------+---+
%% | 3 | 2 | 1 |
%% +---+-------+---+
%% < ^ ^ >
?line ok = pwrite_file(Name, [{3*SegSize,
subbin(Bin, 3*SegSize, 1*SegSize)},
{1*SegSize,
subbin(Bin, 1*SegSize, 2*SegSize)},
{0*SegSize,
subbin(Bin, 0*SegSize, 1*SegSize)}]),
?line true = verify_file(Name, Cnt),
%%
%% +-------+-------+
%% | 2 | 1 |
%% +-------+-------+
%% < ^ ^ >
?line ok = pwrite_file(Name, [{2*SegSize,
subbin(Bin, 2*SegSize, 2*SegSize)},
{0*SegSize,
subbin(Bin, 0*SegSize, 2*SegSize)}]),
?line true = verify_file(Name, Cnt),
%%
%% +-------+---+---+
%% | 3 | 2 | 1 |
%% +-------+---+---+
%% < ^ ^ >
?line ok = pwrite_file(Name, [{3*SegSize,
subbin(Bin, 3*SegSize, 1*SegSize)},
{2*SegSize,
subbin(Bin, 2*SegSize, 1*SegSize)},
{0*SegSize,
subbin(Bin, 0*SegSize, 2*SegSize)}]),
?line true = verify_file(Name, Cnt),
%%
%% Real time response time test.
%%
?line {ok, FD7} = ?FILE_MODULE:open(Name, [write, raw, binary]),
Req = lists:flatten(lists:duplicate(17,
[{2*SegSize,
subbin(Bin, 2*SegSize, 2*SegSize)},
{0*SegSize,
subbin(Bin, 0*SegSize, 2*SegSize)}])),
?line {ok, Comment} =
response_analysis(?FILE_MODULE, pwrite, [FD7, Req]),
?line ok = ?FILE_MODULE:close(FD7),
%%
?line [] = flush(),
{comment, Comment}.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ipread(suite) ->
[];
ipread(doc) ->
["Test Dets special indirect pread"];
ipread(Config) when is_list(Config) ->
?line Dir = ?config(priv_dir, Config),
?line ok = ipread_int(Dir, [raw, binary]),
?line ok = ipread_int(Dir, [raw]),
?line ok = ipread_int(Dir, [binary]),
?line ok = ipread_int(Dir, []),
?line ok = ipread_int(Dir, [ram, binary]),
?line ok = ipread_int(Dir, [ram]),
%%
?line [] = flush(),
ok.
ipread_int(Dir, ModeList) ->
?line Name =
filename:join(Dir,
lists:flatten([?MODULE_STRING, "_ipread",
lists:map(fun (X) ->
["_", atom_to_list(X)]
end,
ModeList)])),
?line io:format("ipread_int<~p, ~p>~n", [Name, ModeList]),
?line {Conv, Sizeof} =
case lists:member(binary, ModeList) of
true ->
{fun (Bin) when is_binary(Bin) -> Bin;
(List) when is_list(List) -> list_to_binary(List)
end,
fun erlang:byte_size/1};
false ->
{fun (Bin) when is_binary(Bin) -> binary_to_list(Bin);
(List) when is_list(List) -> List
end,
fun erlang:length/1}
end,
?line Pos = 4711,
?line Data = Conv("THE QUICK BROWN FOX JUMPS OVER A LAZY DOG"),
?line Size = Sizeof(Data),
?line Init = Conv(" "),
?line SizeInit = Sizeof(Init),
?line Head = Conv(<<Size:32/big-unsigned, Pos:32/big-unsigned>>),
?line Filler = Conv(bytes($ , Pos-SizeInit-Sizeof(Head))),
?line Size1 = Size+1,
?line SizePos = Size+Pos,
%%
?line {ok, FD} = ?FILE_MODULE:open(Name, [write, read | ModeList]),
?line ok = ?FILE_MODULE:truncate(FD),
?line ok = ?FILE_MODULE:write(FD, Init),
?line ok = ?FILE_MODULE:write(FD, Head),
?line ok = ?FILE_MODULE:write(FD, Filler),
?line ok = ?FILE_MODULE:write(FD, Data),
%% Correct read
?line {ok, {Size, Pos, Data}} =
?FILE_MODULE:ipread_s32bu_p32bu(FD, SizeInit, infinity),
%% Invalid header - size > max
?line eof =
?FILE_MODULE:ipread_s32bu_p32bu(FD, SizeInit, Size-1),
%% Data block protudes over eof
?line ok =
?FILE_MODULE:pwrite(FD, SizeInit,
<<Size1:32/big-unsigned,
Pos:32/big-unsigned>>),
?line {ok, {Size1, Pos, Data}} =
?FILE_MODULE:ipread_s32bu_p32bu(FD, SizeInit, Size1),
%% Data block outside file
?line ok =
?FILE_MODULE:pwrite(FD, SizeInit,
<<Size:32/big-unsigned,
SizePos:32/big-unsigned>>),
?line {ok, {Size, SizePos, eof}} =
?FILE_MODULE:ipread_s32bu_p32bu(FD, SizeInit, Size),
%% Zero size
?line ok =
?FILE_MODULE:pwrite(FD, SizeInit,
<<0:32/big-unsigned,
Pos:32/big-unsigned>>),
?line {ok, {0, Pos, eof}} =
?FILE_MODULE:ipread_s32bu_p32bu(FD, SizeInit, Size),
%% Invalid header - protudes over eof
?line eof =
?FILE_MODULE:ipread_s32bu_p32bu(FD,
Pos+Size-(Sizeof(Head)-1),
infinity),
%% Header not even in file
?line eof =
?FILE_MODULE:ipread_s32bu_p32bu(FD, Pos+Size, infinity),
%%
?line ok = ?FILE_MODULE:close(FD),
ok.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
interleaved_read_write(suite) ->
[];
interleaved_read_write(doc) ->
["Tests interleaved read and writes"];
interleaved_read_write(Config) when is_list(Config) ->
?line Dir = ?config(priv_dir, Config),
?line File =
filename:join(Dir, ?MODULE_STRING++"interleaved_read_write.txt"),
?line {ok,F1} = ?FILE_MODULE:open(File, [write]),
?line ok = ?FILE_MODULE:write(F1, "data---r1."), % 10 chars each
?line ok = ?FILE_MODULE:write(F1, "data---r2."),
?line ok = ?FILE_MODULE:write(F1, "data---r3."),
?line ok = ?FILE_MODULE:close(F1),
?line {ok,F2} = ?FILE_MODULE:open(File, [read, write]),
?line {ok, "data---r1."} = ?FILE_MODULE:read(F2, 10),
?line ok = ?FILE_MODULE:write(F2, "data---w2."),
?line ok = ?FILE_MODULE:close(F2),
?line {ok,F3} = ?FILE_MODULE:open(File, [read]),
?line {ok, "data---r1."} = ?FILE_MODULE:read(F3, 10),
?line {ok, "data---w2."} = ?FILE_MODULE:read(F3, 10),
?line {ok, "data---r3."} = ?FILE_MODULE:read(F3, 10),
?line eof = ?FILE_MODULE:read(F3, 1),
?line ok = ?FILE_MODULE:close(F2),
%%
?line [] = flush(),
ok.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
otp_5814(suite) ->
[];
otp_5814(doc) ->
["OTP-5814. eval/consult/script return correct line numbers"];
otp_5814(Config) when is_list(Config) ->
PrivDir = ?config(priv_dir, Config),
File = filename:join(PrivDir, "otp_5814"),
Path = [PrivDir],
?line ok = file:write_file(File, <<"{a,b,c}.
a.
b.
c.
{d,e,
[}.">>),
?line {error, {6,erl_parse,_}} = file:eval(File),
?line {error, {6,erl_parse,_}} = file:consult(File),
?line {error, {6,erl_parse,_}} = file:path_consult(Path, File),
?line {error, {6,erl_parse,_}} = file:path_eval(Path, File),
?line {error, {6,erl_parse,_}} = file:script(File),
?line {error, {6,erl_parse,_}} = file:path_script(Path, File),
?line ok = file:write_file(File, <<>>),
?line {error, {1,file,undefined_script}} = file:path_script(Path, File),
%% The error is not propagated...
?line ok = file:write_file(File, <<"a.
b.
1/0.">>),
?line {error, {3, file, {error, badarith, _}}} = file:eval(File),
?line ok = file:write_file(File, <<"erlang:raise(throw, apa, []).">>),
?line {error, {1, file, {throw, apa, _}}} = file:eval(File),
file:delete(File),
ok.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
otp_10852(suite) ->
[];
otp_10852(doc) ->
["OTP-10852. +fnu and latin1 filenames"];
otp_10852(Config) when is_list(Config) ->
Node = start_node(erl_pp_helper, "+fnu"),
Dir = ?config(priv_dir, Config),
B = filename:join(Dir, <<"\xE4">>),
ok = rpc_call(Node, get_cwd, [B]),
{error, no_translation} = rpc_call(Node, set_cwd, [B]),
ok = rpc_call(Node, delete, [B]),
ok = rpc_call(Node, rename, [B, B]),
ok = rpc_call(Node, read_file_info, [B]),
ok = rpc_call(Node, read_link_info, [B]),
ok = rpc_call(Node, read_link, [B]),
ok = rpc_call(Node, write_file_info, [B,#file_info{}]),
ok = rpc_call(Node, list_dir, [B]),
ok = rpc_call(Node, list_dir_all, [B]),
ok = rpc_call(Node, read_file, [B]),
ok = rpc_call(Node, make_link, [B,B]),
case rpc_call(Node, make_symlink, [B,B]) of
ok -> ok;
{error, E} when (E =:= enotsup) or (E =:= eperm) ->
{win32,_} = os:type()
end,
ok = rpc_call(Node, delete, [B]),
ok = rpc_call(Node, make_dir, [B]),
ok = rpc_call(Node, del_dir, [B]),
ok = rpc_call(Node, write_file, [B,B]),
{ok, Fd} = rpc_call(Node, open, [B,[read]]),
ok = rpc_call(Node, close, [Fd]),
{ok,0} = rpc_call(Node, copy, [B,B]),
{ok, Fd2, B} = rpc_call(Node, path_open, [["."], B, [read]]),
ok = rpc_call(Node, close, [Fd2]),
true = test_server:stop_node(Node),
ok.
rpc_call(N, F, As) ->
case rpc:call(N, ?FILE_MODULE, F, As) of
{error, enotsup} -> ok;
{error, enoent} -> ok;
{error, badarg} -> ok;
Else -> Else
end.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
large_file() ->
[{timetrap,{minutes,20}}].
large_file(suite) ->
[];
large_file(doc) ->
["Tests positioning in large files (> 4G)"];
large_file(Config) when is_list(Config) ->
run_large_file_test(Config,
fun(Name) -> do_large_file(Name) end,
"_large_file").
do_large_file(Name) ->
?line S = "1234567890",
L = length(S),
R = lists:reverse(S),
P = 1 bsl 32,
Ss = lists:sort(S),
Rs = lists:reverse(Ss),
?line {ok,F} = ?FILE_MODULE:open(Name, [raw,read,write]),
?line ok = ?FILE_MODULE:write(F, S),
?line {ok,P} = ?FILE_MODULE:position(F, P),
?line ok = ?FILE_MODULE:write(F, R),
?line {ok,0} = ?FILE_MODULE:position(F, bof),
?line {ok,S} = ?FILE_MODULE:read(F, L),
?line {ok,P} = ?FILE_MODULE:position(F, {eof,-L}),
?line {ok,R} = ?FILE_MODULE:read(F, L+1),
?line {ok,S} = ?FILE_MODULE:pread(F, 0, L),
?line {ok,R} = ?FILE_MODULE:pread(F, P, L+1),
?line ok = ?FILE_MODULE:pwrite(F, 0, Ss),
?line ok = ?FILE_MODULE:pwrite(F, P, Rs),
?line {ok,0} = ?FILE_MODULE:position(F, bof),
?line {ok,Ss} = ?FILE_MODULE:read(F, L),
?line {ok,P} = ?FILE_MODULE:position(F, {eof,-L}),
?line {ok,Rs} = ?FILE_MODULE:read(F, L+1),
?line ok = ?FILE_MODULE:close(F),
%% Reopen the file with 'append'; used to fail on Windows causing
%% writes to go to the beginning of the file for files > 4GB.
?line PL = P + L,
?line PLL = PL + L,
?line {ok,F1} = ?FILE_MODULE:open(Name, [raw,read,write,append]),
?line ok = ?FILE_MODULE:write(F1, R),
?line {ok,PLL} = ?FILE_MODULE:position(F1, {cur,0}),
?line {ok,Rs} = ?FILE_MODULE:pread(F1, P, L),
?line {ok,PL} = ?FILE_MODULE:position(F1, {eof,-L}),
?line {ok,R} = ?FILE_MODULE:read(F1, L+1),
?line ok = ?FILE_MODULE:close(F1),
ok.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
large_write(Config) when is_list(Config) ->
run_large_file_test(Config,
fun(Name) -> do_large_write(Name) end,
"_large_write").
do_large_write(Name) ->
Memsize = memsize(),
io:format("Memsize = ~w Bytes~n", [Memsize]),
case {erlang:system_info(wordsize),Memsize} of
{4,_} ->
{skip,"Needs a 64-bit emulator"};
{8,N} when N < 6 bsl 30 ->
{skip,
"This machine has < 6 GB memory: "
++integer_to_list(N)};
{8,_} ->
Size = 4*1024*1024*1024+1,
Bin = <<0:Size/unit:8>>,
ok = file:write_file(Name, Bin),
{ok,#file_info{size=Size}} = file:read_file_info(Name),
ok
end.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
response_analysis(Module, Function, Arguments) ->
Parent = self(),
?line erlang:yield(), % Schedule out before test
?line Child =
spawn_link(
fun () ->
receive {Parent, start, Ts} -> ok end,
Stat =
iterate(response_stat(response_stat(init, Ts),
micro_ts()),
done,
fun (S) ->
erlang:yield(),
receive
{Parent, stop} ->
done
after 0 ->
response_stat(S, micro_ts())
end
end),
Parent ! {self(), stopped, response_stat(Stat, micro_ts())}
end),
Child ! {Parent, start, micro_ts()},
?line Result = apply(Module, Function, Arguments),
?line Child ! {Parent, stop},
?line {N, Sum, _, M, Max} = receive {Child, stopped, X} -> X end,
?line Mean_ms = (0.001*Sum) / (N-1),
?line Max_ms = 0.001 * Max,
?line Comment =
lists:flatten(
io_lib:format(
"Scheduling interval: Mean = ~.3f ms, "
++"Max = ~.3f ms for no ~p of ~p.~n",
[Mean_ms, Max_ms, M, (N-1)])),
?line {Result, Comment}.
micro_ts() ->
erlang:monotonic_time(micro_seconds).
response_stat(init, Ts) ->
{0, 0, Ts, 0, 0};
response_stat({N, Sum, Ts0, M, Max}, Ts) ->
D = Ts - Ts0,
if D > Max ->
{N+1, Sum+D, Ts, N, D};
true ->
{N+1, Sum+D, Ts, M, Max}
end.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% This function is kept just for benchmarking reasons.
%% create_file/2 below is some 44 times faster.
create_file_slow(Name, N) when is_integer(N), N >= 0 ->
?line {ok, FD} =
?FILE_MODULE:open(Name, [raw, write, delayed_write, binary]),
?line ok = create_file_slow(FD, 0, N),
?line ok = ?FILE_MODULE:close(FD),
ok.
create_file_slow(_FD, M, M) ->
ok;
create_file_slow(FD, M, N) ->
ok = ?FILE_MODULE:write(FD, <<M:32/unsigned>>),
create_file_slow(FD, M+1, N).
%% Creates a file 'Name' containing 'N' unsigned 32 bit integers
%% from 0 to N-1.
create_file(Name, N) when is_integer(N), N >= 0 ->
?line {ok, FD} =
?FILE_MODULE:open(Name, [raw, write, delayed_write, binary]),
?line ok = create_file(FD, 0, N),
?line ok = ?FILE_MODULE:close(FD),
ok.
create_file(_FD, M, M) ->
ok;
create_file(FD, M, N) when M + 1024 =< N ->
create_file(FD, M, M + 1024, []),
create_file(FD, M + 1024, N);
create_file(FD, M, N) ->
create_file(FD, M, N, []).
create_file(FD, M, M, R) ->
ok = ?FILE_MODULE:write(FD, R);
create_file(FD, M, N0, R) when M + 8 =< N0 ->
N1 = N0-1, N2 = N0-2, N3 = N0-3, N4 = N0-4,
N5 = N0-5, N6 = N0-6, N7 = N0-7, N8 = N0-8,
create_file(FD, M, N8,
[<<N8:32/unsigned, N7:32/unsigned,
N6:32/unsigned, N5:32/unsigned,
N4:32/unsigned, N3:32/unsigned,
N2:32/unsigned, N1:32/unsigned>> | R]);
create_file(FD, M, N0, R) ->
N1 = N0-1,
create_file(FD, M, N1, [<<N1:32/unsigned>> | R]).
create_bin(M, N) when is_integer(M), is_integer(N), N >= 0, M >= 0 ->
create_bin(M, M+N, []).
create_bin(N, N, R) ->
list_to_binary(R);
create_bin(M, N0, R) when M+8 =< N0 ->
N1 = N0-1, N2 = N0-2, N3 = N0-3, N4 = N0-4,
N5 = N0-5, N6 = N0-6, N7 = N0-7, N8 = N0-8,
create_bin(M, N8,
[<<N8:32/unsigned, N7:32/unsigned,
N6:32/unsigned, N5:32/unsigned,
N4:32/unsigned, N3:32/unsigned,
N2:32/unsigned, N1:32/unsigned>> | R]);
create_bin(M, N0, R) ->
N1 = N0-1,
create_bin(M, N1, [<<N1:32/unsigned>> | R]).
verify_bin(<<>>, _, 0) ->
true;
verify_bin(<<>>, _, _) ->
false;
verify_bin(Bin, N, Cnt) ->
N0 = N + 0, N1 = N + 1, N2 = N + 2, N3 = N + 3,
N4 = N + 4, N5 = N + 5, N6 = N + 6, N7 = N + 7,
case Bin of
<<N0:32/unsigned, N1:32/unsigned, N2:32/unsigned, N3:32/unsigned,
N4:32/unsigned, N5:32/unsigned, N6:32/unsigned, N7:32/unsigned,
B/binary>> ->
verify_bin(B, N+8, Cnt-8);
<<N:32/unsigned, B/binary>> ->
verify_bin(B, N+1, Cnt-1);
_ ->
false
end.
verify_file(Name, N) when is_integer(N), N >= 0 ->
case ?FILE_MODULE:open(Name, [raw, read, binary]) of
{ok, FD} ->
Result = verify_file(FD, 0, 64*1024, N),
ok = ?FILE_MODULE:close(FD),
Result;
Error ->
Error
end.
verify_file(FD, N, _, N) ->
case ?FILE_MODULE:read(FD, 1) of
eof ->
true;
{ok, _} ->
false
end;
verify_file(FD, M, Cnt, N) when M+Cnt =< N ->
case ?FILE_MODULE:read(FD, 4*Cnt) of
{ok, Bin} ->
case verify_bin(Bin, M, Cnt) of
true ->
verify_file(FD, M+Cnt, Cnt, N);
false ->
false
end;
_ ->
false
end;
verify_file(FD, M, _Cnt, N) ->
verify_file(FD, M, N-M, N).
subbin(Bin, M, N) ->
<<_:M/binary, B:N/binary, _/binary>> = Bin,
B.
write_file(Name, Data) ->
case ?FILE_MODULE:open(Name, [raw, write, binary]) of
{ok, FD} ->
Result = ?FILE_MODULE:write(FD, Data),
case {Result, ?FILE_MODULE:close(FD)} of
{ok, R} -> R;
_ -> Result
end;
Error ->
Error
end.
pwrite_file(Name, Data) ->
case ?FILE_MODULE:open(Name, [raw, write, binary]) of
{ok, FD} ->
Result = ?FILE_MODULE:pwrite(FD, Data),
case {Result, ?FILE_MODULE:close(FD)} of
{ok, R} -> R;
_ -> Result
end;
Error ->
Error
end.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Read_line tests
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
read_line_testdata(PrivDir) ->
All0 = [{fun read_line_create0/1,"Testdata1.txt",5,10},
{fun read_line_create1/1,"Testdata2.txt",401,802},
{fun read_line_create2/1,"Testdata3.txt",1,2},
{fun read_line_create3/1,"Testdata4.txt",601,fail},
{fun read_line_create4/1,"Testdata5.txt",601,1002},
{fun read_line_create5/1,"Testdata6.txt",601,1202},
{fun read_line_create6/1,"Testdata7.txt",601,1202},
{fun read_line_create7/1,"Testdata8.txt",4001,8002}],
[ {A,filename:join([PrivDir,B]),C,D} || {A,B,C,D} <- All0 ].
read_line_create_files(TestData) ->
[ Function(File) || {Function,File,_,_} <- TestData ].
read_line_remove_files(TestData) ->
[ file:delete(File) || {_Function,File,_,_} <- TestData ].
read_line_1(suite) ->
[];
read_line_1(doc) ->
["read_line with prim_file"];
read_line_1(Config) when is_list(Config) ->
?line PrivDir = ?config(priv_dir, Config),
?line All = read_line_testdata(PrivDir),
?line read_line_create_files(All),
?line [ begin
io:format("read_line_all: ~s~n",[File]),
{X,_} = read_line_all(File),
true
end || {_,File,X,_} <- All ],
?line [ begin
io:format("read_line_all_alternating: ~s~n",[File]),
{Y,_} = read_line_all_alternating(File),
true
end || {_,File,_,Y} <- All , Y =/= fail],
?line [ begin
io:format("read_line_all_alternating (failing as should): ~s~n",[File]),
{'EXIT',_} = (catch read_line_all_alternating(File)),
true
end || {_,File,_,Y} <- All , Y =:= fail],
?line read_line_remove_files(All),
ok.
read_line_2(suite) ->
[];
read_line_2(doc) ->
["read_line with file"];
read_line_2(Config) when is_list(Config) ->
?line PrivDir = ?config(priv_dir, Config),
?line All = read_line_testdata(PrivDir),
?line read_line_create_files(All),
?line [ begin
io:format("read_line_all: ~s~n",[File]),
{X,_} = read_line_all2(File),
true
end || {_,File,X,_} <- All ],
?line [ begin
io:format("read_line_all_alternating: ~s~n",[File]),
{Y,_} = read_line_all_alternating2(File),
true
end || {_,File,_,Y} <- All , Y =/= fail],
?line [ begin
io:format("read_line_all_alternating (failing as should): ~s~n",[File]),
{'EXIT',_} = (catch read_line_all_alternating2(File)),
true
end || {_,File,_,Y} <- All , Y =:= fail],
?line read_line_remove_files(All),
ok.
read_line_3(suite) ->
[];
read_line_3(doc) ->
["read_line with raw file"];
read_line_3(Config) when is_list(Config) ->
?line PrivDir = ?config(priv_dir, Config),
?line All = read_line_testdata(PrivDir),
?line read_line_create_files(All),
?line [ begin
io:format("read_line_all: ~s~n",[File]),
{X,_} = read_line_all3(File),
true
end || {_,File,X,_} <- All ],
?line [ begin
io:format("read_line_all_alternating: ~s~n",[File]),
{Y,_} = read_line_all_alternating3(File),
true
end || {_,File,_,Y} <- All , Y =/= fail],
?line [ begin
io:format("read_line_all_alternating (failing as should): ~s~n",[File]),
{'EXIT',_} = (catch read_line_all_alternating3(File)),
true
end || {_,File,_,Y} <- All , Y =:= fail],
?line read_line_remove_files(All),
ok.
read_line_4(suite) ->
[];
read_line_4(doc) ->
["read_line with raw buffered file"];
read_line_4(Config) when is_list(Config) ->
?line PrivDir = ?config(priv_dir, Config),
?line All = read_line_testdata(PrivDir),
?line read_line_create_files(All),
?line [ begin
io:format("read_line_all: ~s~n",[File]),
{X,_} = read_line_all4(File),
true
end || {_,File,X,_} <- All ],
?line [ begin
io:format("read_line_all_alternating: ~s~n",[File]),
{Y,_} = read_line_all_alternating4(File),
true
end || {_,File,_,Y} <- All , Y =/= fail],
?line [ begin
io:format("read_line_all_alternating (failing as should): ~s~n",[File]),
{'EXIT',_} = (catch read_line_all_alternating4(File)),
true
end || {_,File,_,Y} <- All , Y =:= fail],
?line read_line_remove_files(All),
ok.
rl_lines() ->
[ <<"hej">>,<<"hopp">>,<<"i">>,<<"lingon\rskogen">>].
read_line_create0(Filename) ->
{ok,F} = file:open(Filename,[write]),
L = rl_lines(),
[ file:write(F,[R,<<"\r\n">>]) || R <- L ],
file:write(F,<<"Inget radslut\r">>),
file:close(F).
read_line_create1(Filename) ->
{ok,F} = file:open(Filename,[write]),
L = rl_lines(),
[ begin
[ file:write(F,[R,<<"\r\n">>]) || R <- L ],
file:write(F,<<"Inget radslut\r">>)
end || _ <- lists:seq(1,100)],
file:close(F).
read_line_create2(Filename) ->
{ok,F} = file:open(Filename,[write]),
L = rl_lines(),
[ begin
[ file:write(F,[R]) || R <- L ],
file:write(F,<<"Inget radslut\r">>)
end || _ <- lists:seq(1,200)],
file:write(F,<<"\r\n">>),
file:close(F).
read_line_create3(Filename) ->
{ok,F} = file:open(Filename,[write]),
L = rl_lines(),
[ begin
file:write(F,<<"\r\n">>),
file:write(F,<<"\r\n">>),
[ file:write(F,[R,<<"\r\n">>]) || R <- L ],
file:write(F,<<"Inget radslut\r">>)
end || _ <- lists:seq(1,100)],
file:close(F).
read_line_create4(Filename) ->
{ok,F} = file:open(Filename,[write]),
L = rl_lines(),
[ begin
file:write(F,<<"\n">>),
file:write(F,<<"\n">>),
[ file:write(F,[R,<<"\r\n">>]) || R <- L ],
file:write(F,<<"Inget radslut\r">>)
end || _ <- lists:seq(1,100)],
file:close(F).
read_line_create5(Filename) ->
{ok,F} = file:open(Filename,[write]),
L = rl_lines(),
[ begin
file:write(F,<<"i\n">>),
file:write(F,<<"i\n">>),
[ file:write(F,[R,<<"\r\n">>]) || R <- L ],
file:write(F,<<"Inget radslut\r">>)
end || _ <- lists:seq(1,100)],
file:close(F).
read_line_create6(Filename) ->
{ok,F} = file:open(Filename,[write]),
L = rl_lines(),
[ begin
file:write(F,<<"i\r\n">>),
file:write(F,<<"i\r\n">>),
[ file:write(F,[R,<<"\r\n">>]) || R <- L ],
file:write(F,<<"Inget radslut\r">>)
end || _ <- lists:seq(1,100)],
file:close(F).
read_line_create7(Filename) ->
{ok,F} = file:open(Filename,[write]),
L = rl_lines(),
[ begin
[ file:write(F,[R,<<"\r\n">>]) || R <- L ],
file:write(F,<<"Inget radslut\r">>)
end || _ <- lists:seq(1,1000)],
file:close(F).
read_line_all(Filename) ->
{ok,F} = prim_file:open(Filename,[read,binary]),
X=read_rl_lines(F),
prim_file:close(F),
Bin = list_to_binary([B || {ok,B} <- X]),
Bin = re:replace(list_to_binary([element(2,file:read_file(Filename))]),
"\r\n","\n",[global,{return,binary}]),
{length(X),Bin}.
read_line_all2(Filename) ->
{ok,F} = file:open(Filename,[read,binary]),
X=read_rl_lines2(F),
file:close(F),
Bin = list_to_binary([B || {ok,B} <- X]),
Bin = re:replace(list_to_binary([element(2,file:read_file(Filename))]),
"\r\n","\n",[global,{return,binary}]),
{length(X),Bin}.
read_line_all3(Filename) ->
{ok,F} = file:open(Filename,[read,binary,raw]),
X=read_rl_lines2(F),
file:close(F),
Bin = list_to_binary([B || {ok,B} <- X]),
Bin = re:replace(list_to_binary([element(2,file:read_file(Filename))]),
"\r\n","\n",[global,{return,binary}]),
{length(X),Bin}.
read_line_all4(Filename) ->
{ok,F} = file:open(Filename,[read,binary,raw,{read_ahead,8192}]),
X=read_rl_lines2(F),
file:close(F),
Bin = list_to_binary([B || {ok,B} <- X]),
Bin = re:replace(list_to_binary([element(2,file:read_file(Filename))]),
"\r\n","\n",[global,{return,binary}]),
{length(X),Bin}.
read_rl_lines(F) ->
case prim_file:read_line(F) of
eof ->
[];
{error,X} ->
{error,X};
List ->
[List | read_rl_lines(F)]
end.
read_rl_lines2(F) ->
case file:read_line(F) of
eof ->
[];
{error,X} ->
{error,X};
List ->
[List | read_rl_lines2(F)]
end.
read_line_all_alternating(Filename) ->
{ok,F} = prim_file:open(Filename,[read,binary]),
X=read_rl_lines(F,true),
prim_file:close(F),
Bin = list_to_binary([B || {ok,B} <- X]),
Bin = re:replace(list_to_binary([element(2,file:read_file(Filename))]),
"\r\n","\n",[global,{return,binary}]),
{length(X),Bin}.
read_line_all_alternating2(Filename) ->
{ok,F} = file:open(Filename,[read,binary]),
X=read_rl_lines2(F,true),
file:close(F),
Bin = list_to_binary([B || {ok,B} <- X]),
Bin = re:replace(list_to_binary([element(2,file:read_file(Filename))]),
"\r\n","\n",[global,{return,binary}]),
{length(X),Bin}.
read_line_all_alternating3(Filename) ->
{ok,F} = file:open(Filename,[read,binary,raw]),
X=read_rl_lines2(F,true),
file:close(F),
Bin = list_to_binary([B || {ok,B} <- X]),
Bin = re:replace(list_to_binary([element(2,file:read_file(Filename))]),
"\r\n","\n",[global,{return,binary}]),
{length(X),Bin}.
read_line_all_alternating4(Filename) ->
{ok,F} = file:open(Filename,[read,binary,raw,{read_ahead,8192}]),
X=read_rl_lines2(F,true),
file:close(F),
Bin = list_to_binary([B || {ok,B} <- X]),
Bin = re:replace(list_to_binary([element(2,file:read_file(Filename))]),
"\r\n","\n",[global,{return,binary}]),
{length(X),Bin}.
read_rl_lines(F,Alternate) ->
case begin
case Alternate of
true -> prim_file:read(F,1);
false -> prim_file:read_line(F)
end
end of
eof ->
[];
{error,X} ->
{error,X};
List ->
[List | read_rl_lines(F,not Alternate)]
end.
read_rl_lines2(F,Alternate) ->
case begin
case Alternate of
true -> file:read(F,1);
false -> file:read_line(F)
end
end of
eof ->
[];
{error,X} ->
{error,X};
List ->
[List | read_rl_lines2(F,not Alternate)]
end.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
bytes(B, N)
when is_integer(B), 0 =< B, B =< 255, is_integer(N), N > 2, N band 1 == 0 ->
[bytes(B, N bsr 1), bytes(B, N bsr 1)];
bytes(B, 0)
when is_integer(B), 0 =< B, B =< 255 ->
[];
bytes(B, 2)
when is_integer(B), 0 =< B, B =< 255 ->
[B, B];
bytes(B, N)
when is_integer(B), 0 =< B, B =< 255, is_integer(N), N > 0 ->
[B, bytes(B, N-1)].
%% A simple loop construct.
%%
%% Calls 'Fun' with argument 'Start' first and then repeatedly with
%% its returned value (state) until 'Fun' returns 'Stop'. Then
%% the last state value that was not 'Stop' is returned.
iterate(Start, Done, Fun) when is_function(Fun) ->
iterate(Start, Done, Fun, Start).
iterate(Done, Done, _Fun, I) ->
I;
iterate(I, Done, Fun, _) ->
iterate(Fun(I), Done, Fun, I).
flush() ->
flush([]).
flush(Msgs) ->
receive
Msg ->
flush([Msg | Msgs])
after 0 ->
lists:reverse(Msgs)
end.
%%%
%%% Support for testing large files.
%%%
run_large_file_test(Config, Run, Name) ->
case {os:type(),os:version()} of
{{win32,nt},_} ->
do_run_large_file_test(Config, Run, Name);
{{unix,sunos},OsVersion} when OsVersion < {5,5,1} ->
{skip,"Only supported on Win32, Unix or SunOS >= 5.5.1"};
{{unix,_},_} ->
N = disc_free(?config(priv_dir, Config)),
io:format("Free disk: ~w KByte~n", [N]),
if N < 5 * (1 bsl 20) ->
%% Less than 5 GByte free
{skip,"Less than 5 GByte free"};
true ->
do_run_large_file_test(Config, Run, Name)
end;
_ ->
{skip,"Only supported on Win32, Unix or SunOS >= 5.5.1"}
end.
do_run_large_file_test(Config, Run, Name0) ->
Name = filename:join(?config(priv_dir, Config),
?MODULE_STRING ++ Name0),
%% Set up a process that will delete this file.
Tester = self(),
Deleter =
spawn(
fun() ->
Mref = erlang:monitor(process, Tester),
receive
{'DOWN',Mref,_,_,_} -> ok;
{Tester,done} -> ok
end,
?FILE_MODULE:delete(Name)
end),
%% Run the test case.
Res = Run(Name),
%% Delete file and finish deleter process.
Mref = erlang:monitor(process, Deleter),
Deleter ! {Tester,done},
receive {'DOWN',Mref,_,_,_} -> ok end,
Res.
disc_free(Path) ->
Data = disksup:get_disk_data(),
{_,Tot,Perc} = hd(lists:filter(
fun({P,_Size,_Full}) ->
lists:prefix(filename:nativename(P),
filename:nativename(Path))
end, lists:reverse(lists:sort(Data)))),
round(Tot * (1-(Perc/100))).
memsize() ->
{Tot,_Used,_} = memsup:get_memory_data(),
Tot.
%%%-----------------------------------------------------------------
%%% Utilities
rm_rf(Mod,Dir) ->
case Mod:read_link_info(Dir) of
{ok, #file_info{type = directory}} ->
{ok, Content} = Mod:list_dir_all(Dir),
[ rm_rf(Mod,filename:join(Dir,C)) || C <- Content ],
Mod:del_dir(Dir),
ok;
{ok, #file_info{}} ->
Mod:delete(Dir);
_ ->
ok
end.
|