1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922
|
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include "uv.h"
#include "task.h"
#include <errno.h>
#include <string.h> /* memset */
#include <fcntl.h>
#include <sys/stat.h>
#include <limits.h> /* INT_MAX, PATH_MAX, IOV_MAX */
#ifndef _WIN32
# include <unistd.h> /* unlink, rmdir, etc. */
#else
# include <winioctl.h>
# include <direct.h>
# include <io.h>
# ifndef ERROR_SYMLINK_NOT_SUPPORTED
# define ERROR_SYMLINK_NOT_SUPPORTED 1464
# endif
# ifndef S_IFIFO
# define S_IFIFO _S_IFIFO
# endif
# define unlink _unlink
# define rmdir _rmdir
# define open _open
# define write _write
# define close _close
# ifndef stat
# define stat _stati64
# endif
# ifndef lseek
# define lseek _lseek
# endif
# define S_IFDIR _S_IFDIR
# define S_IFCHR _S_IFCHR
# define S_IFREG _S_IFREG
#endif
#define TOO_LONG_NAME_LENGTH 65536
#define PATHMAX 4096
#ifdef _WIN32
static const int is_win32 = 1;
#else
static const int is_win32 = 0;
#endif
#if defined(__APPLE__) || defined(__SUNPRO_C)
static const int is_apple_or_sunpro_c = 1;
#else
static const int is_apple_or_sunpro_c = 0;
#endif
typedef struct {
const char* path;
double atime;
double mtime;
} utime_check_t;
static int dummy_cb_count;
static int close_cb_count;
static int create_cb_count;
static int open_cb_count;
static int read_cb_count;
static int write_cb_count;
static int unlink_cb_count;
static int mkdir_cb_count;
static int mkdtemp_cb_count;
static int mkstemp_cb_count;
static int rmdir_cb_count;
static int scandir_cb_count;
static int stat_cb_count;
static int rename_cb_count;
static int fsync_cb_count;
static int fdatasync_cb_count;
static int ftruncate_cb_count;
static int sendfile_cb_count;
static int fstat_cb_count;
static int access_cb_count;
static int chmod_cb_count;
static int fchmod_cb_count;
static int chown_cb_count;
static int fchown_cb_count;
static int lchown_cb_count;
static int link_cb_count;
static int symlink_cb_count;
static int readlink_cb_count;
static int realpath_cb_count;
static int utime_cb_count;
static int futime_cb_count;
static int lutime_cb_count;
static int statfs_cb_count;
static uv_loop_t* loop;
static uv_fs_t open_req1;
static uv_fs_t open_req2;
static uv_fs_t open_req_noclose;
static uv_fs_t read_req;
static uv_fs_t write_req;
static uv_fs_t unlink_req;
static uv_fs_t close_req;
static uv_fs_t mkdir_req;
static uv_fs_t mkdtemp_req1;
static uv_fs_t mkdtemp_req2;
static uv_fs_t mkstemp_req1;
static uv_fs_t mkstemp_req2;
static uv_fs_t mkstemp_req3;
static uv_fs_t rmdir_req;
static uv_fs_t scandir_req;
static uv_fs_t stat_req;
static uv_fs_t rename_req;
static uv_fs_t fsync_req;
static uv_fs_t fdatasync_req;
static uv_fs_t ftruncate_req;
static uv_fs_t sendfile_req;
static uv_fs_t utime_req;
static uv_fs_t futime_req;
static char buf[32];
static char buf2[32];
static char test_buf[] = "test-buffer\n";
static char test_buf2[] = "second-buffer\n";
static uv_buf_t iov;
#ifdef _WIN32
int uv_test_getiovmax(void) {
return INT32_MAX; /* Emulated by libuv, so no real limit. */
}
#else
int uv_test_getiovmax(void) {
#if defined(IOV_MAX)
return IOV_MAX;
#elif defined(_SC_IOV_MAX)
static int iovmax = -1;
if (iovmax == -1) {
iovmax = sysconf(_SC_IOV_MAX);
/* On some embedded devices (arm-linux-uclibc based ip camera),
* sysconf(_SC_IOV_MAX) can not get the correct value. The return
* value is -1 and the errno is EINPROGRESS. Degrade the value to 1.
*/
if (iovmax == -1) iovmax = 1;
}
return iovmax;
#else
return 1024;
#endif
}
#endif
#ifdef _WIN32
/*
* This tag and guid have no special meaning, and don't conflict with
* reserved ids.
*/
static unsigned REPARSE_TAG = 0x9913;
static GUID REPARSE_GUID = {
0x1bf6205f, 0x46ae, 0x4527,
{ 0xb1, 0x0c, 0xc5, 0x09, 0xb7, 0x55, 0x22, 0x80 }};
#endif
static void check_permission(const char* filename, unsigned int mode) {
int r;
uv_fs_t req;
uv_stat_t* s;
r = uv_fs_stat(NULL, &req, filename, NULL);
ASSERT_OK(r);
ASSERT_OK(req.result);
s = &req.statbuf;
#if defined(_WIN32) || defined(__CYGWIN__) || defined(__MSYS__)
/*
* On Windows, chmod can only modify S_IWUSR (_S_IWRITE) bit,
* so only testing for the specified flags.
*/
ASSERT((s->st_mode & 0777) & mode);
#else
ASSERT((s->st_mode & 0777) == mode);
#endif
uv_fs_req_cleanup(&req);
}
static void dummy_cb(uv_fs_t* req) {
(void) req;
dummy_cb_count++;
}
static void link_cb(uv_fs_t* req) {
ASSERT_EQ(req->fs_type, UV_FS_LINK);
ASSERT_OK(req->result);
link_cb_count++;
uv_fs_req_cleanup(req);
}
static void symlink_cb(uv_fs_t* req) {
ASSERT_EQ(req->fs_type, UV_FS_SYMLINK);
ASSERT_OK(req->result);
symlink_cb_count++;
uv_fs_req_cleanup(req);
}
static void readlink_cb(uv_fs_t* req) {
ASSERT_EQ(req->fs_type, UV_FS_READLINK);
ASSERT_OK(req->result);
ASSERT_OK(strcmp(req->ptr, "test_file_symlink2"));
readlink_cb_count++;
uv_fs_req_cleanup(req);
}
static void realpath_cb(uv_fs_t* req) {
char test_file_abs_buf[PATHMAX];
size_t test_file_abs_size = sizeof(test_file_abs_buf);
ASSERT_EQ(req->fs_type, UV_FS_REALPATH);
ASSERT_OK(req->result);
uv_cwd(test_file_abs_buf, &test_file_abs_size);
#ifdef _WIN32
strcat(test_file_abs_buf, "\\test_file");
ASSERT_OK(_stricmp(req->ptr, test_file_abs_buf));
#else
strcat(test_file_abs_buf, "/test_file");
ASSERT_OK(strcmp(req->ptr, test_file_abs_buf));
#endif
realpath_cb_count++;
uv_fs_req_cleanup(req);
}
static void access_cb(uv_fs_t* req) {
ASSERT_EQ(req->fs_type, UV_FS_ACCESS);
access_cb_count++;
uv_fs_req_cleanup(req);
}
static void fchmod_cb(uv_fs_t* req) {
ASSERT_EQ(req->fs_type, UV_FS_FCHMOD);
ASSERT_OK(req->result);
fchmod_cb_count++;
uv_fs_req_cleanup(req);
check_permission("test_file", *(int*)req->data);
}
static void chmod_cb(uv_fs_t* req) {
ASSERT_EQ(req->fs_type, UV_FS_CHMOD);
ASSERT_OK(req->result);
chmod_cb_count++;
uv_fs_req_cleanup(req);
check_permission("test_file", *(int*)req->data);
}
static void fchown_cb(uv_fs_t* req) {
ASSERT_EQ(req->fs_type, UV_FS_FCHOWN);
ASSERT_OK(req->result);
fchown_cb_count++;
uv_fs_req_cleanup(req);
}
static void chown_cb(uv_fs_t* req) {
ASSERT_EQ(req->fs_type, UV_FS_CHOWN);
ASSERT_OK(req->result);
chown_cb_count++;
uv_fs_req_cleanup(req);
}
static void lchown_cb(uv_fs_t* req) {
ASSERT_EQ(req->fs_type, UV_FS_LCHOWN);
ASSERT_OK(req->result);
lchown_cb_count++;
uv_fs_req_cleanup(req);
}
static void chown_root_cb(uv_fs_t* req) {
ASSERT_EQ(req->fs_type, UV_FS_CHOWN);
#if defined(_WIN32) || defined(__MSYS__)
/* On windows, chown is a no-op and always succeeds. */
ASSERT_OK(req->result);
#else
/* On unix, chown'ing the root directory is not allowed -
* unless you're root, of course.
*/
if (geteuid() == 0)
ASSERT_OK(req->result);
else
# if defined(__CYGWIN__)
/* On Cygwin, uid 0 is invalid (no root). */
ASSERT_EQ(req->result, UV_EINVAL);
# elif defined(__PASE__)
/* On IBMi PASE, there is no root user. uid 0 is user qsecofr.
* User may grant qsecofr's privileges, including changing
* the file's ownership to uid 0.
*/
ASSERT(req->result == 0 || req->result == UV_EPERM);
# else
ASSERT_EQ(req->result, UV_EPERM);
# endif
#endif
chown_cb_count++;
uv_fs_req_cleanup(req);
}
static void unlink_cb(uv_fs_t* req) {
ASSERT_PTR_EQ(req, &unlink_req);
ASSERT_EQ(req->fs_type, UV_FS_UNLINK);
ASSERT_OK(req->result);
unlink_cb_count++;
uv_fs_req_cleanup(req);
}
static void fstat_cb(uv_fs_t* req) {
uv_stat_t* s = req->ptr;
ASSERT_EQ(req->fs_type, UV_FS_FSTAT);
ASSERT_OK(req->result);
ASSERT_EQ(s->st_size, sizeof(test_buf));
uv_fs_req_cleanup(req);
fstat_cb_count++;
}
static void statfs_cb(uv_fs_t* req) {
uv_statfs_t* stats;
ASSERT_EQ(req->fs_type, UV_FS_STATFS);
ASSERT_OK(req->result);
ASSERT_NOT_NULL(req->ptr);
stats = req->ptr;
#if defined(_WIN32) || defined(__sun) || defined(_AIX) || defined(__MVS__) || \
defined(__OpenBSD__) || defined(__NetBSD__)
ASSERT_OK(stats->f_type);
#else
ASSERT_UINT64_GT(stats->f_type, 0);
#endif
ASSERT_GT(stats->f_bsize, 0);
ASSERT_GT(stats->f_blocks, 0);
ASSERT_LE(stats->f_bfree, stats->f_blocks);
ASSERT_LE(stats->f_bavail, stats->f_bfree);
#ifdef _WIN32
ASSERT_OK(stats->f_files);
ASSERT_OK(stats->f_ffree);
#else
/* There is no assertion for stats->f_files that makes sense, so ignore it. */
ASSERT_LE(stats->f_ffree, stats->f_files);
#endif
uv_fs_req_cleanup(req);
ASSERT_NULL(req->ptr);
statfs_cb_count++;
}
static void close_cb(uv_fs_t* req) {
int r;
ASSERT_PTR_EQ(req, &close_req);
ASSERT_EQ(req->fs_type, UV_FS_CLOSE);
ASSERT_OK(req->result);
close_cb_count++;
uv_fs_req_cleanup(req);
if (close_cb_count == 3) {
r = uv_fs_unlink(loop, &unlink_req, "test_file2", unlink_cb);
ASSERT_OK(r);
}
}
static void ftruncate_cb(uv_fs_t* req) {
int r;
ASSERT_PTR_EQ(req, &ftruncate_req);
ASSERT_EQ(req->fs_type, UV_FS_FTRUNCATE);
ASSERT_OK(req->result);
ftruncate_cb_count++;
uv_fs_req_cleanup(req);
r = uv_fs_close(loop, &close_req, open_req1.result, close_cb);
ASSERT_OK(r);
}
static void fail_cb(uv_fs_t* req) {
FATAL("fail_cb should not have been called");
}
static void read_cb(uv_fs_t* req) {
int r;
ASSERT_PTR_EQ(req, &read_req);
ASSERT_EQ(req->fs_type, UV_FS_READ);
ASSERT_GE(req->result, 0); /* FIXME(bnoordhuis) Check if requested size? */
read_cb_count++;
uv_fs_req_cleanup(req);
if (read_cb_count == 1) {
ASSERT_OK(strcmp(buf, test_buf));
r = uv_fs_ftruncate(loop, &ftruncate_req, open_req1.result, 7,
ftruncate_cb);
} else {
ASSERT_OK(strcmp(buf, "test-bu"));
r = uv_fs_close(loop, &close_req, open_req1.result, close_cb);
}
ASSERT_OK(r);
}
static void open_cb(uv_fs_t* req) {
int r;
ASSERT_PTR_EQ(req, &open_req1);
ASSERT_EQ(req->fs_type, UV_FS_OPEN);
if (req->result < 0) {
fprintf(stderr, "async open error: %d\n", (int) req->result);
ASSERT(0);
}
open_cb_count++;
ASSERT(req->path);
ASSERT_OK(memcmp(req->path, "test_file2\0", 11));
uv_fs_req_cleanup(req);
memset(buf, 0, sizeof(buf));
iov = uv_buf_init(buf, sizeof(buf));
r = uv_fs_read(loop, &read_req, open_req1.result, &iov, 1, -1,
read_cb);
ASSERT_OK(r);
}
static void open_cb_simple(uv_fs_t* req) {
ASSERT_EQ(req->fs_type, UV_FS_OPEN);
if (req->result < 0) {
fprintf(stderr, "async open error: %d\n", (int) req->result);
ASSERT(0);
}
open_cb_count++;
ASSERT(req->path);
uv_fs_req_cleanup(req);
}
static void fsync_cb(uv_fs_t* req) {
int r;
ASSERT_PTR_EQ(req, &fsync_req);
ASSERT_EQ(req->fs_type, UV_FS_FSYNC);
ASSERT_OK(req->result);
fsync_cb_count++;
uv_fs_req_cleanup(req);
r = uv_fs_close(loop, &close_req, open_req1.result, close_cb);
ASSERT_OK(r);
}
static void fdatasync_cb(uv_fs_t* req) {
int r;
ASSERT_PTR_EQ(req, &fdatasync_req);
ASSERT_EQ(req->fs_type, UV_FS_FDATASYNC);
ASSERT_OK(req->result);
fdatasync_cb_count++;
uv_fs_req_cleanup(req);
r = uv_fs_fsync(loop, &fsync_req, open_req1.result, fsync_cb);
ASSERT_OK(r);
}
static void write_cb(uv_fs_t* req) {
int r;
ASSERT_PTR_EQ(req, &write_req);
ASSERT_EQ(req->fs_type, UV_FS_WRITE);
ASSERT_GE(req->result, 0); /* FIXME(bnoordhuis) Check if requested size? */
write_cb_count++;
uv_fs_req_cleanup(req);
r = uv_fs_fdatasync(loop, &fdatasync_req, open_req1.result, fdatasync_cb);
ASSERT_OK(r);
}
static void create_cb(uv_fs_t* req) {
int r;
ASSERT_PTR_EQ(req, &open_req1);
ASSERT_EQ(req->fs_type, UV_FS_OPEN);
ASSERT_GE(req->result, 0);
create_cb_count++;
uv_fs_req_cleanup(req);
iov = uv_buf_init(test_buf, sizeof(test_buf));
r = uv_fs_write(loop, &write_req, req->result, &iov, 1, -1, write_cb);
ASSERT_OK(r);
}
static void rename_cb(uv_fs_t* req) {
ASSERT_PTR_EQ(req, &rename_req);
ASSERT_EQ(req->fs_type, UV_FS_RENAME);
ASSERT_OK(req->result);
rename_cb_count++;
uv_fs_req_cleanup(req);
}
static void mkdir_cb(uv_fs_t* req) {
ASSERT_PTR_EQ(req, &mkdir_req);
ASSERT_EQ(req->fs_type, UV_FS_MKDIR);
ASSERT_OK(req->result);
mkdir_cb_count++;
ASSERT(req->path);
ASSERT_OK(memcmp(req->path, "test_dir\0", 9));
uv_fs_req_cleanup(req);
}
static void check_mkdtemp_result(uv_fs_t* req) {
int r;
ASSERT_EQ(req->fs_type, UV_FS_MKDTEMP);
ASSERT_OK(req->result);
ASSERT(req->path);
ASSERT_EQ(15, strlen(req->path));
ASSERT_OK(memcmp(req->path, "test_dir_", 9));
ASSERT_NE(0, memcmp(req->path + 9, "XXXXXX", 6));
check_permission(req->path, 0700);
/* Check if req->path is actually a directory */
r = uv_fs_stat(NULL, &stat_req, req->path, NULL);
ASSERT_OK(r);
ASSERT(((uv_stat_t*)stat_req.ptr)->st_mode & S_IFDIR);
uv_fs_req_cleanup(&stat_req);
}
static void mkdtemp_cb(uv_fs_t* req) {
ASSERT_PTR_EQ(req, &mkdtemp_req1);
check_mkdtemp_result(req);
mkdtemp_cb_count++;
}
static void check_mkstemp_result(uv_fs_t* req) {
int r;
ASSERT_EQ(req->fs_type, UV_FS_MKSTEMP);
ASSERT_GE(req->result, 0);
ASSERT(req->path);
ASSERT_EQ(16, strlen(req->path));
ASSERT_OK(memcmp(req->path, "test_file_", 10));
ASSERT_NE(0, memcmp(req->path + 10, "XXXXXX", 6));
check_permission(req->path, 0600);
/* Check if req->path is actually a file */
r = uv_fs_stat(NULL, &stat_req, req->path, NULL);
ASSERT_OK(r);
ASSERT(stat_req.statbuf.st_mode & S_IFREG);
uv_fs_req_cleanup(&stat_req);
}
static void mkstemp_cb(uv_fs_t* req) {
ASSERT_PTR_EQ(req, &mkstemp_req1);
check_mkstemp_result(req);
mkstemp_cb_count++;
}
static void rmdir_cb(uv_fs_t* req) {
ASSERT_PTR_EQ(req, &rmdir_req);
ASSERT_EQ(req->fs_type, UV_FS_RMDIR);
ASSERT_OK(req->result);
rmdir_cb_count++;
ASSERT(req->path);
ASSERT_OK(memcmp(req->path, "test_dir\0", 9));
uv_fs_req_cleanup(req);
}
static void assert_is_file_type(uv_dirent_t dent) {
#ifdef HAVE_DIRENT_TYPES
/*
* For Apple and Windows, we know getdents is expected to work but for other
* environments, the filesystem dictates whether or not getdents supports
* returning the file type.
*
* See:
* http://man7.org/linux/man-pages/man2/getdents.2.html
* https://github.com/libuv/libuv/issues/501
*/
#if defined(__APPLE__) || defined(_WIN32)
ASSERT_EQ(dent.type, UV_DIRENT_FILE);
#else
ASSERT(dent.type == UV_DIRENT_FILE || dent.type == UV_DIRENT_UNKNOWN);
#endif
#else
ASSERT_EQ(dent.type, UV_DIRENT_UNKNOWN);
#endif
}
static void scandir_cb(uv_fs_t* req) {
uv_dirent_t dent;
ASSERT_PTR_EQ(req, &scandir_req);
ASSERT_EQ(req->fs_type, UV_FS_SCANDIR);
ASSERT_EQ(2, req->result);
ASSERT(req->ptr);
while (UV_EOF != uv_fs_scandir_next(req, &dent)) {
ASSERT(strcmp(dent.name, "file1") == 0 || strcmp(dent.name, "file2") == 0);
assert_is_file_type(dent);
}
scandir_cb_count++;
ASSERT(req->path);
ASSERT_OK(memcmp(req->path, "test_dir\0", 9));
uv_fs_req_cleanup(req);
ASSERT(!req->ptr);
}
static void empty_scandir_cb(uv_fs_t* req) {
uv_dirent_t dent;
ASSERT_PTR_EQ(req, &scandir_req);
ASSERT_EQ(req->fs_type, UV_FS_SCANDIR);
ASSERT_OK(req->result);
ASSERT_NULL(req->ptr);
ASSERT_EQ(UV_EOF, uv_fs_scandir_next(req, &dent));
uv_fs_req_cleanup(req);
scandir_cb_count++;
}
static void non_existent_scandir_cb(uv_fs_t* req) {
uv_dirent_t dent;
ASSERT_PTR_EQ(req, &scandir_req);
ASSERT_EQ(req->fs_type, UV_FS_SCANDIR);
ASSERT_EQ(req->result, UV_ENOENT);
ASSERT_NULL(req->ptr);
ASSERT_EQ(UV_ENOENT, uv_fs_scandir_next(req, &dent));
uv_fs_req_cleanup(req);
scandir_cb_count++;
}
static void file_scandir_cb(uv_fs_t* req) {
ASSERT_PTR_EQ(req, &scandir_req);
ASSERT_EQ(req->fs_type, UV_FS_SCANDIR);
ASSERT_EQ(req->result, UV_ENOTDIR);
ASSERT_NULL(req->ptr);
uv_fs_req_cleanup(req);
scandir_cb_count++;
}
static void stat_cb(uv_fs_t* req) {
ASSERT_PTR_EQ(req, &stat_req);
ASSERT(req->fs_type == UV_FS_STAT || req->fs_type == UV_FS_LSTAT);
ASSERT_OK(req->result);
ASSERT(req->ptr);
stat_cb_count++;
uv_fs_req_cleanup(req);
ASSERT(!req->ptr);
}
static void stat_batch_cb(uv_fs_t* req) {
ASSERT(req->fs_type == UV_FS_STAT || req->fs_type == UV_FS_LSTAT);
ASSERT_OK(req->result);
ASSERT(req->ptr);
stat_cb_count++;
uv_fs_req_cleanup(req);
ASSERT(!req->ptr);
}
static void sendfile_cb(uv_fs_t* req) {
ASSERT_PTR_EQ(req, &sendfile_req);
ASSERT_EQ(req->fs_type, UV_FS_SENDFILE);
ASSERT_EQ(65545, req->result);
sendfile_cb_count++;
uv_fs_req_cleanup(req);
}
static void sendfile_nodata_cb(uv_fs_t* req) {
ASSERT_PTR_EQ(req, &sendfile_req);
ASSERT_EQ(req->fs_type, UV_FS_SENDFILE);
ASSERT_OK(req->result);
sendfile_cb_count++;
uv_fs_req_cleanup(req);
}
static void open_noent_cb(uv_fs_t* req) {
ASSERT_EQ(req->fs_type, UV_FS_OPEN);
ASSERT_EQ(req->result, UV_ENOENT);
open_cb_count++;
uv_fs_req_cleanup(req);
}
static void open_nametoolong_cb(uv_fs_t* req) {
ASSERT_EQ(req->fs_type, UV_FS_OPEN);
ASSERT_EQ(req->result, UV_ENAMETOOLONG);
open_cb_count++;
uv_fs_req_cleanup(req);
}
static void open_loop_cb(uv_fs_t* req) {
ASSERT_EQ(req->fs_type, UV_FS_OPEN);
ASSERT_EQ(req->result, UV_ELOOP);
open_cb_count++;
uv_fs_req_cleanup(req);
}
TEST_IMPL(fs_file_noent) {
uv_fs_t req;
int r;
loop = uv_default_loop();
r = uv_fs_open(NULL, &req, "does_not_exist", UV_FS_O_RDONLY, 0, NULL);
ASSERT_EQ(r, UV_ENOENT);
ASSERT_EQ(req.result, UV_ENOENT);
uv_fs_req_cleanup(&req);
r = uv_fs_open(loop, &req, "does_not_exist", UV_FS_O_RDONLY, 0,
open_noent_cb);
ASSERT_OK(r);
ASSERT_OK(open_cb_count);
uv_run(loop, UV_RUN_DEFAULT);
ASSERT_EQ(1, open_cb_count);
/* TODO add EACCES test */
MAKE_VALGRIND_HAPPY(loop);
return 0;
}
TEST_IMPL(fs_file_nametoolong) {
uv_fs_t req;
int r;
char name[TOO_LONG_NAME_LENGTH + 1];
loop = uv_default_loop();
memset(name, 'a', TOO_LONG_NAME_LENGTH);
name[TOO_LONG_NAME_LENGTH] = 0;
r = uv_fs_open(NULL, &req, name, UV_FS_O_RDONLY, 0, NULL);
ASSERT_EQ(r, UV_ENAMETOOLONG);
ASSERT_EQ(req.result, UV_ENAMETOOLONG);
uv_fs_req_cleanup(&req);
r = uv_fs_open(loop, &req, name, UV_FS_O_RDONLY, 0, open_nametoolong_cb);
ASSERT_OK(r);
ASSERT_OK(open_cb_count);
uv_run(loop, UV_RUN_DEFAULT);
ASSERT_EQ(1, open_cb_count);
MAKE_VALGRIND_HAPPY(loop);
return 0;
}
TEST_IMPL(fs_file_loop) {
uv_fs_t req;
int r;
loop = uv_default_loop();
unlink("test_symlink");
r = uv_fs_symlink(NULL, &req, "test_symlink", "test_symlink", 0, NULL);
#ifdef _WIN32
/*
* Symlinks are only suported but only when elevated, otherwise
* we'll see UV_EPERM.
*/
if (r == UV_EPERM)
return 0;
#elif defined(__MSYS__)
/* MSYS2's approximation of symlinks with copies does not work for broken
links. */
if (r == UV_ENOENT)
return 0;
#endif
ASSERT_OK(r);
uv_fs_req_cleanup(&req);
r = uv_fs_open(NULL, &req, "test_symlink", UV_FS_O_RDONLY, 0, NULL);
ASSERT_EQ(r, UV_ELOOP);
ASSERT_EQ(req.result, UV_ELOOP);
uv_fs_req_cleanup(&req);
r = uv_fs_open(loop, &req, "test_symlink", UV_FS_O_RDONLY, 0, open_loop_cb);
ASSERT_OK(r);
ASSERT_OK(open_cb_count);
uv_run(loop, UV_RUN_DEFAULT);
ASSERT_EQ(1, open_cb_count);
unlink("test_symlink");
MAKE_VALGRIND_HAPPY(loop);
return 0;
}
static void check_utime(const char* path,
double atime,
double mtime,
int test_lutime) {
uv_stat_t* s;
uv_fs_t req;
int r;
if (test_lutime)
r = uv_fs_lstat(loop, &req, path, NULL);
else
r = uv_fs_stat(loop, &req, path, NULL);
ASSERT_OK(r);
ASSERT_OK(req.result);
s = &req.statbuf;
if (isfinite(atime)) {
/* Test sub-second timestamps only when supported (such as Windows with
* NTFS). Some other platforms support sub-second timestamps, but that
* support is filesystem-dependent. Notably OS X (HFS Plus) does NOT
* support sub-second timestamps. But kernels may round or truncate in
* either direction, so we may accept either possible answer.
*/
if (s->st_atim.tv_nsec == 0) {
if (is_win32)
ASSERT_DOUBLE_EQ(atime, (long) atime);
if (atime > 0 || (long) atime == atime)
ASSERT_EQ(s->st_atim.tv_sec, (long) atime);
ASSERT_GE(s->st_atim.tv_sec, (long) atime - 1);
ASSERT_LE(s->st_atim.tv_sec, (long) atime);
} else {
double st_atim;
/* TODO(vtjnash): would it be better to normalize this? */
if (!is_apple_or_sunpro_c)
ASSERT_DOUBLE_GE(s->st_atim.tv_nsec, 0);
st_atim = s->st_atim.tv_sec + s->st_atim.tv_nsec / 1e9;
/* Linux does not allow reading reliably the atime of a symlink
* since readlink() can update it
*/
if (!test_lutime)
ASSERT_DOUBLE_EQ(st_atim, atime);
}
} else if (isinf(atime)) {
/* We test with timestamps that are in the distant past
* (if you're a Gen Z-er) so check it's more recent than that.
*/
ASSERT_GT(s->st_atim.tv_sec, 1739710000);
} else {
ASSERT_OK(0);
}
if (isfinite(mtime)) {
/* Test sub-second timestamps only when supported (such as Windows with
* NTFS). Some other platforms support sub-second timestamps, but that
* support is filesystem-dependent. Notably OS X (HFS Plus) does NOT
* support sub-second timestamps. But kernels may round or truncate in
* either direction, so we may accept either possible answer.
*/
if (s->st_mtim.tv_nsec == 0) {
if (is_win32)
ASSERT_DOUBLE_EQ(mtime, (long) atime);
if (mtime > 0 || (long) mtime == mtime)
ASSERT_EQ(s->st_mtim.tv_sec, (long) mtime);
ASSERT_GE(s->st_mtim.tv_sec, (long) mtime - 1);
ASSERT_LE(s->st_mtim.tv_sec, (long) mtime);
} else {
double st_mtim;
/* TODO(vtjnash): would it be better to normalize this? */
if (!is_apple_or_sunpro_c)
ASSERT_DOUBLE_GE(s->st_mtim.tv_nsec, 0);
st_mtim = s->st_mtim.tv_sec + s->st_mtim.tv_nsec / 1e9;
ASSERT_DOUBLE_EQ(st_mtim, mtime);
}
} else if (isinf(mtime)) {
/* We test with timestamps that are in the distant past
* (if you're a Gen Z-er) so check it's more recent than that.
*/
ASSERT_GT(s->st_mtim.tv_sec, 1739710000);
} else {
ASSERT_OK(0);
}
uv_fs_req_cleanup(&req);
}
static void utime_cb(uv_fs_t* req) {
utime_check_t* c;
ASSERT_PTR_EQ(req, &utime_req);
ASSERT_OK(req->result);
ASSERT_EQ(req->fs_type, UV_FS_UTIME);
c = req->data;
check_utime(c->path, c->atime, c->mtime, /* test_lutime */ 0);
uv_fs_req_cleanup(req);
utime_cb_count++;
}
static void futime_cb(uv_fs_t* req) {
utime_check_t* c;
ASSERT_PTR_EQ(req, &futime_req);
ASSERT_OK(req->result);
ASSERT_EQ(req->fs_type, UV_FS_FUTIME);
c = req->data;
check_utime(c->path, c->atime, c->mtime, /* test_lutime */ 0);
uv_fs_req_cleanup(req);
futime_cb_count++;
}
static void lutime_cb(uv_fs_t* req) {
utime_check_t* c;
ASSERT_OK(req->result);
ASSERT_EQ(req->fs_type, UV_FS_LUTIME);
c = req->data;
check_utime(c->path, c->atime, c->mtime, /* test_lutime */ 1);
uv_fs_req_cleanup(req);
lutime_cb_count++;
}
TEST_IMPL(fs_file_async) {
int r;
/* Setup. */
unlink("test_file");
unlink("test_file2");
loop = uv_default_loop();
r = uv_fs_open(loop, &open_req1, "test_file", UV_FS_O_WRONLY | UV_FS_O_CREAT,
S_IRUSR | S_IWUSR, create_cb);
ASSERT_OK(r);
uv_run(loop, UV_RUN_DEFAULT);
ASSERT_EQ(1, create_cb_count);
ASSERT_EQ(1, write_cb_count);
ASSERT_EQ(1, fsync_cb_count);
ASSERT_EQ(1, fdatasync_cb_count);
ASSERT_EQ(1, close_cb_count);
r = uv_fs_rename(loop, &rename_req, "test_file", "test_file2", rename_cb);
ASSERT_OK(r);
uv_run(loop, UV_RUN_DEFAULT);
ASSERT_EQ(1, create_cb_count);
ASSERT_EQ(1, write_cb_count);
ASSERT_EQ(1, close_cb_count);
ASSERT_EQ(1, rename_cb_count);
r = uv_fs_open(loop, &open_req1, "test_file2", UV_FS_O_RDWR, 0, open_cb);
ASSERT_OK(r);
uv_run(loop, UV_RUN_DEFAULT);
ASSERT_EQ(1, open_cb_count);
ASSERT_EQ(1, read_cb_count);
ASSERT_EQ(2, close_cb_count);
ASSERT_EQ(1, rename_cb_count);
ASSERT_EQ(1, create_cb_count);
ASSERT_EQ(1, write_cb_count);
ASSERT_EQ(1, ftruncate_cb_count);
r = uv_fs_open(loop, &open_req1, "test_file2", UV_FS_O_RDONLY, 0, open_cb);
ASSERT_OK(r);
uv_run(loop, UV_RUN_DEFAULT);
ASSERT_EQ(2, open_cb_count);
ASSERT_EQ(2, read_cb_count);
ASSERT_EQ(3, close_cb_count);
ASSERT_EQ(1, rename_cb_count);
ASSERT_EQ(1, unlink_cb_count);
ASSERT_EQ(1, create_cb_count);
ASSERT_EQ(1, write_cb_count);
ASSERT_EQ(1, ftruncate_cb_count);
/* Cleanup. */
unlink("test_file");
unlink("test_file2");
MAKE_VALGRIND_HAPPY(loop);
return 0;
}
static void fs_file_sync(int add_flags) {
int r;
/* Setup. */
unlink("test_file");
unlink("test_file2");
loop = uv_default_loop();
r = uv_fs_open(loop, &open_req1, "test_file",
UV_FS_O_WRONLY | UV_FS_O_CREAT | add_flags, S_IWUSR | S_IRUSR,
NULL);
ASSERT_GE(r, 0);
ASSERT_GE(open_req1.result, 0);
uv_fs_req_cleanup(&open_req1);
iov = uv_buf_init(test_buf, sizeof(test_buf));
r = uv_fs_write(NULL, &write_req, open_req1.result, &iov, 1, -1, NULL);
ASSERT_GE(r, 0);
ASSERT_GE(write_req.result, 0);
uv_fs_req_cleanup(&write_req);
r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
ASSERT_OK(r);
ASSERT_OK(close_req.result);
uv_fs_req_cleanup(&close_req);
r = uv_fs_open(NULL, &open_req1, "test_file", UV_FS_O_RDWR | add_flags, 0,
NULL);
ASSERT_GE(r, 0);
ASSERT_GE(open_req1.result, 0);
uv_fs_req_cleanup(&open_req1);
iov = uv_buf_init(buf, sizeof(buf));
r = uv_fs_read(NULL, &read_req, open_req1.result, &iov, 1, -1, NULL);
ASSERT_GE(r, 0);
ASSERT_GE(read_req.result, 0);
ASSERT_OK(strcmp(buf, test_buf));
uv_fs_req_cleanup(&read_req);
r = uv_fs_ftruncate(NULL, &ftruncate_req, open_req1.result, 7, NULL);
ASSERT_OK(r);
ASSERT_OK(ftruncate_req.result);
uv_fs_req_cleanup(&ftruncate_req);
r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
ASSERT_OK(r);
ASSERT_OK(close_req.result);
uv_fs_req_cleanup(&close_req);
r = uv_fs_rename(NULL, &rename_req, "test_file", "test_file2", NULL);
ASSERT_OK(r);
ASSERT_OK(rename_req.result);
uv_fs_req_cleanup(&rename_req);
r = uv_fs_open(NULL, &open_req1, "test_file2", UV_FS_O_RDONLY | add_flags, 0,
NULL);
ASSERT_GE(r, 0);
ASSERT_GE(open_req1.result, 0);
uv_fs_req_cleanup(&open_req1);
memset(buf, 0, sizeof(buf));
iov = uv_buf_init(buf, sizeof(buf));
r = uv_fs_read(NULL, &read_req, open_req1.result, &iov, 1, -1, NULL);
ASSERT_GE(r, 0);
ASSERT_GE(read_req.result, 0);
ASSERT_OK(strcmp(buf, "test-bu"));
uv_fs_req_cleanup(&read_req);
r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
ASSERT_OK(r);
ASSERT_OK(close_req.result);
uv_fs_req_cleanup(&close_req);
r = uv_fs_unlink(NULL, &unlink_req, "test_file2", NULL);
ASSERT_OK(r);
ASSERT_OK(unlink_req.result);
uv_fs_req_cleanup(&unlink_req);
/* Cleanup */
unlink("test_file");
unlink("test_file2");
}
TEST_IMPL(fs_file_sync) {
fs_file_sync(0);
fs_file_sync(UV_FS_O_FILEMAP);
MAKE_VALGRIND_HAPPY(uv_default_loop());
return 0;
}
TEST_IMPL(fs_posix_delete) {
int r;
/* Setup. */
unlink("test_dir/file");
rmdir("test_dir");
r = uv_fs_mkdir(NULL, &mkdir_req, "test_dir", 0755, NULL);
ASSERT_OK(r);
r = uv_fs_open(NULL, &open_req_noclose, "test_dir/file", UV_FS_O_WRONLY | UV_FS_O_CREAT, S_IWUSR | S_IRUSR, NULL);
ASSERT_GE(r, 0);
uv_fs_req_cleanup(&open_req_noclose);
/* should not be possible to delete the non-empty dir */
r = uv_fs_rmdir(NULL, &rmdir_req, "test_dir", NULL);
ASSERT((r == UV_ENOTEMPTY) || (r == UV_EEXIST));
ASSERT_EQ(r, rmdir_req.result);
uv_fs_req_cleanup(&rmdir_req);
r = uv_fs_rmdir(NULL, &rmdir_req, "test_dir/file", NULL);
ASSERT((r == UV_ENOTDIR) || (r == UV_ENOENT));
ASSERT_EQ(r, rmdir_req.result);
uv_fs_req_cleanup(&rmdir_req);
r = uv_fs_unlink(NULL, &unlink_req, "test_dir/file", NULL);
ASSERT_OK(r);
ASSERT_OK(unlink_req.result);
uv_fs_req_cleanup(&unlink_req);
/* delete the dir while the file is still open, which should succeed on posix */
r = uv_fs_rmdir(NULL, &rmdir_req, "test_dir", NULL);
ASSERT_OK(r);
ASSERT_OK(rmdir_req.result);
uv_fs_req_cleanup(&rmdir_req);
/* Cleanup */
r = uv_fs_close(NULL, &close_req, open_req_noclose.result, NULL);
ASSERT_OK(r);
uv_fs_req_cleanup(&close_req);
MAKE_VALGRIND_HAPPY(uv_default_loop());
return 0;
}
static void fs_file_write_null_buffer(int add_flags) {
int r;
/* Setup. */
unlink("test_file");
loop = uv_default_loop();
r = uv_fs_open(NULL, &open_req1, "test_file",
UV_FS_O_WRONLY | UV_FS_O_CREAT | add_flags, S_IWUSR | S_IRUSR,
NULL);
ASSERT_GE(r, 0);
ASSERT_GE(open_req1.result, 0);
uv_fs_req_cleanup(&open_req1);
iov = uv_buf_init(NULL, 0);
r = uv_fs_write(NULL, &write_req, open_req1.result, &iov, 1, -1, NULL);
ASSERT_OK(r);
ASSERT_OK(write_req.result);
uv_fs_req_cleanup(&write_req);
r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
ASSERT_OK(r);
ASSERT_OK(close_req.result);
uv_fs_req_cleanup(&close_req);
unlink("test_file");
}
TEST_IMPL(fs_file_write_null_buffer) {
fs_file_write_null_buffer(0);
fs_file_write_null_buffer(UV_FS_O_FILEMAP);
MAKE_VALGRIND_HAPPY(loop);
return 0;
}
TEST_IMPL(fs_async_dir) {
int r;
uv_dirent_t dent;
/* Setup */
unlink("test_dir/file1");
unlink("test_dir/file2");
rmdir("test_dir");
loop = uv_default_loop();
r = uv_fs_mkdir(loop, &mkdir_req, "test_dir", 0755, mkdir_cb);
ASSERT_OK(r);
uv_run(loop, UV_RUN_DEFAULT);
ASSERT_EQ(1, mkdir_cb_count);
/* Create 2 files synchronously. */
r = uv_fs_open(NULL, &open_req1, "test_dir/file1",
UV_FS_O_WRONLY | UV_FS_O_CREAT,
S_IWUSR | S_IRUSR, NULL);
ASSERT_GE(r, 0);
uv_fs_req_cleanup(&open_req1);
r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
ASSERT_OK(r);
uv_fs_req_cleanup(&close_req);
r = uv_fs_open(NULL, &open_req1, "test_dir/file2",
UV_FS_O_WRONLY | UV_FS_O_CREAT,
S_IWUSR | S_IRUSR, NULL);
ASSERT_GE(r, 0);
uv_fs_req_cleanup(&open_req1);
r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
ASSERT_OK(r);
uv_fs_req_cleanup(&close_req);
r = uv_fs_scandir(loop, &scandir_req, "test_dir", 0, scandir_cb);
ASSERT_OK(r);
uv_run(loop, UV_RUN_DEFAULT);
ASSERT_EQ(1, scandir_cb_count);
/* sync uv_fs_scandir */
r = uv_fs_scandir(NULL, &scandir_req, "test_dir", 0, NULL);
ASSERT_EQ(2, r);
ASSERT_EQ(2, scandir_req.result);
ASSERT(scandir_req.ptr);
while (UV_EOF != uv_fs_scandir_next(&scandir_req, &dent)) {
ASSERT(strcmp(dent.name, "file1") == 0 || strcmp(dent.name, "file2") == 0);
assert_is_file_type(dent);
}
uv_fs_req_cleanup(&scandir_req);
ASSERT(!scandir_req.ptr);
r = uv_fs_stat(loop, &stat_req, "test_dir", stat_cb);
ASSERT_OK(r);
uv_run(loop, UV_RUN_DEFAULT);
r = uv_fs_stat(loop, &stat_req, "test_dir/", stat_cb);
ASSERT_OK(r);
uv_run(loop, UV_RUN_DEFAULT);
r = uv_fs_lstat(loop, &stat_req, "test_dir", stat_cb);
ASSERT_OK(r);
uv_run(loop, UV_RUN_DEFAULT);
r = uv_fs_lstat(loop, &stat_req, "test_dir/", stat_cb);
ASSERT_OK(r);
uv_run(loop, UV_RUN_DEFAULT);
ASSERT_EQ(4, stat_cb_count);
r = uv_fs_unlink(loop, &unlink_req, "test_dir/file1", unlink_cb);
ASSERT_OK(r);
uv_run(loop, UV_RUN_DEFAULT);
ASSERT_EQ(1, unlink_cb_count);
r = uv_fs_unlink(loop, &unlink_req, "test_dir/file2", unlink_cb);
ASSERT_OK(r);
uv_run(loop, UV_RUN_DEFAULT);
ASSERT_EQ(2, unlink_cb_count);
r = uv_fs_rmdir(loop, &rmdir_req, "test_dir", rmdir_cb);
ASSERT_OK(r);
uv_run(loop, UV_RUN_DEFAULT);
ASSERT_EQ(1, rmdir_cb_count);
/* Cleanup */
unlink("test_dir/file1");
unlink("test_dir/file2");
rmdir("test_dir");
MAKE_VALGRIND_HAPPY(loop);
return 0;
}
static int test_sendfile(void (*setup)(int), uv_fs_cb cb, size_t expected_size) {
int f, r;
struct stat s1, s2;
uv_fs_t req;
char buf1[1];
loop = uv_default_loop();
/* Setup. */
unlink("test_file");
unlink("test_file2");
f = open("test_file", UV_FS_O_WRONLY | UV_FS_O_CREAT, S_IWUSR | S_IRUSR);
ASSERT_NE(f, -1);
if (setup != NULL)
setup(f);
r = close(f);
ASSERT_OK(r);
/* Test starts here. */
r = uv_fs_open(NULL, &open_req1, "test_file", UV_FS_O_RDWR, 0, NULL);
ASSERT_GE(r, 0);
ASSERT_GE(open_req1.result, 0);
uv_fs_req_cleanup(&open_req1);
r = uv_fs_open(NULL, &open_req2, "test_file2", UV_FS_O_WRONLY | UV_FS_O_CREAT,
S_IWUSR | S_IRUSR, NULL);
ASSERT_GE(r, 0);
ASSERT_GE(open_req2.result, 0);
uv_fs_req_cleanup(&open_req2);
r = uv_fs_sendfile(loop, &sendfile_req, open_req2.result, open_req1.result,
1, 131072, cb);
ASSERT_OK(r);
uv_run(loop, UV_RUN_DEFAULT);
ASSERT_EQ(1, sendfile_cb_count);
r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
ASSERT_OK(r);
uv_fs_req_cleanup(&close_req);
r = uv_fs_close(NULL, &close_req, open_req2.result, NULL);
ASSERT_OK(r);
uv_fs_req_cleanup(&close_req);
memset(&s1, 0, sizeof(s1));
memset(&s2, 0, sizeof(s2));
ASSERT_OK(stat("test_file", &s1));
ASSERT_OK(stat("test_file2", &s2));
ASSERT_EQ(s2.st_size, expected_size);
if (expected_size > 0) {
ASSERT_UINT64_EQ(s1.st_size, s2.st_size + 1);
r = uv_fs_open(NULL, &open_req1, "test_file2", UV_FS_O_RDWR, 0, NULL);
ASSERT_GE(r, 0);
ASSERT_GE(open_req1.result, 0);
uv_fs_req_cleanup(&open_req1);
memset(buf1, 0, sizeof(buf1));
iov = uv_buf_init(buf1, sizeof(buf1));
r = uv_fs_read(NULL, &req, open_req1.result, &iov, 1, -1, NULL);
ASSERT_GE(r, 0);
ASSERT_GE(req.result, 0);
ASSERT_EQ(buf1[0], 'e'); /* 'e' from begin */
uv_fs_req_cleanup(&req);
} else {
ASSERT_UINT64_EQ(s1.st_size, s2.st_size);
}
/* Cleanup. */
unlink("test_file");
unlink("test_file2");
MAKE_VALGRIND_HAPPY(loop);
return 0;
}
static void sendfile_setup(int f) {
ASSERT_EQ(6, write(f, "begin\n", 6));
ASSERT_EQ(65542, lseek(f, 65536, SEEK_CUR));
ASSERT_EQ(4, write(f, "end\n", 4));
}
TEST_IMPL(fs_async_sendfile) {
return test_sendfile(sendfile_setup, sendfile_cb, 65545);
}
TEST_IMPL(fs_async_sendfile_nodata) {
return test_sendfile(NULL, sendfile_nodata_cb, 0);
}
TEST_IMPL(fs_mkdtemp) {
int r;
const char* path_template = "test_dir_XXXXXX";
loop = uv_default_loop();
r = uv_fs_mkdtemp(loop, &mkdtemp_req1, path_template, mkdtemp_cb);
ASSERT_OK(r);
uv_run(loop, UV_RUN_DEFAULT);
ASSERT_EQ(1, mkdtemp_cb_count);
/* sync mkdtemp */
r = uv_fs_mkdtemp(NULL, &mkdtemp_req2, path_template, NULL);
ASSERT_OK(r);
check_mkdtemp_result(&mkdtemp_req2);
/* mkdtemp return different values on subsequent calls */
ASSERT_NE(0, strcmp(mkdtemp_req1.path, mkdtemp_req2.path));
/* Cleanup */
rmdir(mkdtemp_req1.path);
rmdir(mkdtemp_req2.path);
uv_fs_req_cleanup(&mkdtemp_req1);
uv_fs_req_cleanup(&mkdtemp_req2);
MAKE_VALGRIND_HAPPY(loop);
return 0;
}
TEST_IMPL(fs_mkstemp) {
int r;
int fd;
const char path_template[] = "test_file_XXXXXX";
uv_fs_t req;
loop = uv_default_loop();
r = uv_fs_mkstemp(loop, &mkstemp_req1, path_template, mkstemp_cb);
ASSERT_OK(r);
uv_run(loop, UV_RUN_DEFAULT);
ASSERT_EQ(1, mkstemp_cb_count);
/* sync mkstemp */
r = uv_fs_mkstemp(NULL, &mkstemp_req2, path_template, NULL);
ASSERT_GE(r, 0);
check_mkstemp_result(&mkstemp_req2);
/* mkstemp return different values on subsequent calls */
ASSERT_NE(0, strcmp(mkstemp_req1.path, mkstemp_req2.path));
/* invalid template returns EINVAL */
ASSERT_EQ(UV_EINVAL, uv_fs_mkstemp(NULL, &mkstemp_req3, "test_file", NULL));
/* Make sure that path is empty string */
ASSERT_OK(strlen(mkstemp_req3.path));
uv_fs_req_cleanup(&mkstemp_req3);
/* We can write to the opened file */
iov = uv_buf_init(test_buf, sizeof(test_buf));
r = uv_fs_write(NULL, &req, mkstemp_req1.result, &iov, 1, -1, NULL);
ASSERT_EQ(r, sizeof(test_buf));
ASSERT_EQ(req.result, sizeof(test_buf));
uv_fs_req_cleanup(&req);
/* Cleanup */
uv_fs_close(NULL, &req, mkstemp_req1.result, NULL);
uv_fs_req_cleanup(&req);
uv_fs_close(NULL, &req, mkstemp_req2.result, NULL);
uv_fs_req_cleanup(&req);
fd = uv_fs_open(NULL, &req, mkstemp_req1.path, UV_FS_O_RDONLY, 0, NULL);
ASSERT_GE(fd, 0);
uv_fs_req_cleanup(&req);
memset(buf, 0, sizeof(buf));
iov = uv_buf_init(buf, sizeof(buf));
r = uv_fs_read(NULL, &req, fd, &iov, 1, -1, NULL);
ASSERT_GE(r, 0);
ASSERT_GE(req.result, 0);
ASSERT_OK(strcmp(buf, test_buf));
uv_fs_req_cleanup(&req);
uv_fs_close(NULL, &req, fd, NULL);
uv_fs_req_cleanup(&req);
unlink(mkstemp_req1.path);
unlink(mkstemp_req2.path);
uv_fs_req_cleanup(&mkstemp_req1);
uv_fs_req_cleanup(&mkstemp_req2);
MAKE_VALGRIND_HAPPY(loop);
return 0;
}
TEST_IMPL(fs_fstat) {
int r;
uv_fs_t req;
uv_file file;
uv_stat_t* s;
#ifndef _WIN32
struct stat t;
#endif
#if defined(__s390__) && defined(__QEMU__)
/* qemu-user-s390x has this weird bug where statx() reports nanoseconds
* but plain fstat() does not.
*/
RETURN_SKIP("Test does not currently work in QEMU");
#endif
/* Setup. */
unlink("test_file");
loop = uv_default_loop();
r = uv_fs_open(NULL, &req, "test_file", UV_FS_O_RDWR | UV_FS_O_CREAT,
S_IWUSR | S_IRUSR, NULL);
ASSERT_GE(r, 0);
ASSERT_GE(req.result, 0);
file = req.result;
uv_fs_req_cleanup(&req);
#ifndef _WIN32
memset(&t, 0, sizeof(t));
ASSERT_OK(fstat(file, &t));
ASSERT_OK(uv_fs_fstat(NULL, &req, file, NULL));
ASSERT_OK(req.result);
s = req.ptr;
# if defined(__APPLE__)
ASSERT_EQ(s->st_birthtim.tv_sec, t.st_birthtimespec.tv_sec);
ASSERT_EQ(s->st_birthtim.tv_nsec, t.st_birthtimespec.tv_nsec);
# elif defined(__linux__)
/* If statx() is supported, the birth time should be equal to the change time
* because we just created the file. On older kernels, it's set to zero.
*/
ASSERT(s->st_birthtim.tv_sec == 0 ||
s->st_birthtim.tv_sec == t.st_ctim.tv_sec);
ASSERT(s->st_birthtim.tv_nsec == 0 ||
s->st_birthtim.tv_nsec == t.st_ctim.tv_nsec);
# endif
#endif
iov = uv_buf_init(test_buf, sizeof(test_buf));
r = uv_fs_write(NULL, &req, file, &iov, 1, -1, NULL);
ASSERT_EQ(r, sizeof(test_buf));
ASSERT_EQ(req.result, sizeof(test_buf));
uv_fs_req_cleanup(&req);
memset(&req.statbuf, 0xaa, sizeof(req.statbuf));
r = uv_fs_fstat(NULL, &req, file, NULL);
ASSERT_OK(r);
ASSERT_OK(req.result);
s = req.ptr;
ASSERT_EQ(s->st_size, sizeof(test_buf));
#ifndef _WIN32
r = fstat(file, &t);
ASSERT_OK(r);
ASSERT_EQ(s->st_dev, (uint64_t) t.st_dev);
ASSERT_EQ(s->st_mode, (uint64_t) t.st_mode);
ASSERT_EQ(s->st_nlink, (uint64_t) t.st_nlink);
ASSERT_EQ(s->st_uid, (uint64_t) t.st_uid);
ASSERT_EQ(s->st_gid, (uint64_t) t.st_gid);
ASSERT_EQ(s->st_rdev, (uint64_t) t.st_rdev);
ASSERT_EQ(s->st_ino, (uint64_t) t.st_ino);
ASSERT_EQ(s->st_size, (uint64_t) t.st_size);
ASSERT_EQ(s->st_blksize, (uint64_t) t.st_blksize);
ASSERT_EQ(s->st_blocks, (uint64_t) t.st_blocks);
#if defined(__APPLE__)
ASSERT_EQ(s->st_atim.tv_sec, t.st_atimespec.tv_sec);
ASSERT_EQ(s->st_atim.tv_nsec, t.st_atimespec.tv_nsec);
ASSERT_EQ(s->st_mtim.tv_sec, t.st_mtimespec.tv_sec);
ASSERT_EQ(s->st_mtim.tv_nsec, t.st_mtimespec.tv_nsec);
ASSERT_EQ(s->st_ctim.tv_sec, t.st_ctimespec.tv_sec);
ASSERT_EQ(s->st_ctim.tv_nsec, t.st_ctimespec.tv_nsec);
#elif defined(_AIX) || \
defined(__MVS__)
ASSERT_EQ(s->st_atim.tv_sec, t.st_atime);
ASSERT_OK(s->st_atim.tv_nsec);
ASSERT_EQ(s->st_mtim.tv_sec, t.st_mtime);
ASSERT_OK(s->st_mtim.tv_nsec);
ASSERT_EQ(s->st_ctim.tv_sec, t.st_ctime);
ASSERT_OK(s->st_ctim.tv_nsec);
#elif defined(__ANDROID__)
ASSERT_EQ(s->st_atim.tv_sec, t.st_atime);
ASSERT_EQ(s->st_atim.tv_nsec, t.st_atimensec);
ASSERT_EQ(s->st_mtim.tv_sec, t.st_mtime);
ASSERT_EQ(s->st_mtim.tv_nsec, t.st_mtimensec);
ASSERT_EQ(s->st_ctim.tv_sec, t.st_ctime);
ASSERT_EQ(s->st_ctim.tv_nsec, t.st_ctimensec);
#elif defined(__sun) || \
defined(__DragonFly__) || \
defined(__FreeBSD__) || \
defined(__OpenBSD__) || \
defined(__NetBSD__) || \
defined(_GNU_SOURCE) || \
defined(_BSD_SOURCE) || \
defined(_SVID_SOURCE) || \
defined(_XOPEN_SOURCE) || \
defined(_DEFAULT_SOURCE)
ASSERT_EQ(s->st_atim.tv_sec, t.st_atim.tv_sec);
ASSERT_EQ(s->st_atim.tv_nsec, t.st_atim.tv_nsec);
ASSERT_EQ(s->st_mtim.tv_sec, t.st_mtim.tv_sec);
ASSERT_EQ(s->st_mtim.tv_nsec, t.st_mtim.tv_nsec);
ASSERT_EQ(s->st_ctim.tv_sec, t.st_ctim.tv_sec);
ASSERT_EQ(s->st_ctim.tv_nsec, t.st_ctim.tv_nsec);
# if defined(__FreeBSD__) || \
defined(__NetBSD__)
ASSERT_EQ(s->st_birthtim.tv_sec, t.st_birthtim.tv_sec);
ASSERT_EQ(s->st_birthtim.tv_nsec, t.st_birthtim.tv_nsec);
# endif
#else
ASSERT_EQ(s->st_atim.tv_sec, t.st_atime);
ASSERT_OK(s->st_atim.tv_nsec);
ASSERT_EQ(s->st_mtim.tv_sec, t.st_mtime);
ASSERT_OK(s->st_mtim.tv_nsec);
ASSERT_EQ(s->st_ctim.tv_sec, t.st_ctime);
ASSERT_OK(s->st_ctim.tv_nsec);
#endif
#endif
#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
ASSERT_EQ(s->st_flags, t.st_flags);
ASSERT_EQ(s->st_gen, t.st_gen);
#else
ASSERT_OK(s->st_flags);
ASSERT_OK(s->st_gen);
#endif
uv_fs_req_cleanup(&req);
/* Now do the uv_fs_fstat call asynchronously */
r = uv_fs_fstat(loop, &req, file, fstat_cb);
ASSERT_OK(r);
uv_run(loop, UV_RUN_DEFAULT);
ASSERT_EQ(1, fstat_cb_count);
r = uv_fs_close(NULL, &req, file, NULL);
ASSERT_OK(r);
ASSERT_OK(req.result);
uv_fs_req_cleanup(&req);
/*
* Run the loop just to check we don't have make any extraneous uv_ref()
* calls. This should drop out immediately.
*/
uv_run(loop, UV_RUN_DEFAULT);
/* Cleanup. */
unlink("test_file");
MAKE_VALGRIND_HAPPY(loop);
return 0;
}
TEST_IMPL(fs_fstat_st_dev) {
uv_fs_t req;
uv_fs_t req_link;
uv_loop_t* loop = uv_default_loop();
char* test_file = "tmp_st_dev";
char* symlink_file = "tmp_st_dev_link";
unlink(test_file);
unlink(symlink_file);
// Create file
int r = uv_fs_open(NULL, &req, test_file, UV_FS_O_RDWR | UV_FS_O_CREAT,
S_IWUSR | S_IRUSR, NULL);
ASSERT_GE(r, 0);
ASSERT_GE(req.result, 0);
uv_fs_req_cleanup(&req);
// Create a symlink
r = uv_fs_symlink(loop, &req, test_file, symlink_file, 0, NULL);
ASSERT_EQ(r, 0);
uv_fs_req_cleanup(&req);
// Call uv_fs_fstat for file
r = uv_fs_stat(loop, &req, test_file, NULL);
ASSERT_EQ(r, 0);
// Call uv_fs_fstat for symlink
r = uv_fs_stat(loop, &req_link, symlink_file, NULL);
ASSERT_EQ(r, 0);
// Compare st_dev
ASSERT_EQ(((uv_stat_t*)req.ptr)->st_dev, ((uv_stat_t*)req_link.ptr)->st_dev);
// Cleanup
uv_fs_req_cleanup(&req);
uv_fs_req_cleanup(&req_link);
unlink(test_file);
unlink(symlink_file);
MAKE_VALGRIND_HAPPY(loop);
return 0;
}
TEST_IMPL(fs_fstat_stdio) {
int fd;
int res;
uv_fs_t req;
#ifdef _WIN32
uv_stat_t* st;
DWORD ft;
#endif
for (fd = 0; fd <= 2; ++fd) {
res = uv_fs_fstat(NULL, &req, fd, NULL);
ASSERT_OK(res);
ASSERT_OK(req.result);
#ifdef _WIN32
st = req.ptr;
ft = uv_guess_handle(fd);
switch (ft) {
case UV_TTY:
case UV_NAMED_PIPE:
ASSERT_EQ(st->st_mode, (ft == UV_TTY ? S_IFCHR : S_IFIFO));
ASSERT_EQ(1, st->st_nlink);
ASSERT_EQ(st->st_rdev,
(ft == UV_TTY ? FILE_DEVICE_CONSOLE : FILE_DEVICE_NAMED_PIPE)
<< 16);
break;
default:
break;
}
#endif
uv_fs_req_cleanup(&req);
}
MAKE_VALGRIND_HAPPY(uv_default_loop());
return 0;
}
TEST_IMPL(fs_access) {
int r;
uv_fs_t req;
uv_file file;
/* Setup. */
unlink("test_file");
rmdir("test_dir");
loop = uv_default_loop();
/* File should not exist */
r = uv_fs_access(NULL, &req, "test_file", F_OK, NULL);
ASSERT_LT(r, 0);
ASSERT_LT(req.result, 0);
uv_fs_req_cleanup(&req);
/* File should not exist */
r = uv_fs_access(loop, &req, "test_file", F_OK, access_cb);
ASSERT_OK(r);
uv_run(loop, UV_RUN_DEFAULT);
ASSERT_EQ(1, access_cb_count);
access_cb_count = 0; /* reset for the next test */
/* Create file */
r = uv_fs_open(NULL, &req, "test_file", UV_FS_O_RDWR | UV_FS_O_CREAT,
S_IWUSR | S_IRUSR, NULL);
ASSERT_GE(r, 0);
ASSERT_GE(req.result, 0);
file = req.result;
uv_fs_req_cleanup(&req);
/* File should exist */
r = uv_fs_access(NULL, &req, "test_file", F_OK, NULL);
ASSERT_OK(r);
ASSERT_OK(req.result);
uv_fs_req_cleanup(&req);
/* File should exist */
r = uv_fs_access(loop, &req, "test_file", F_OK, access_cb);
ASSERT_OK(r);
uv_run(loop, UV_RUN_DEFAULT);
ASSERT_EQ(1, access_cb_count);
access_cb_count = 0; /* reset for the next test */
/* Close file */
r = uv_fs_close(NULL, &req, file, NULL);
ASSERT_OK(r);
ASSERT_OK(req.result);
uv_fs_req_cleanup(&req);
/* Directory access */
r = uv_fs_mkdir(NULL, &req, "test_dir", 0777, NULL);
ASSERT_OK(r);
uv_fs_req_cleanup(&req);
r = uv_fs_access(NULL, &req, "test_dir", W_OK, NULL);
ASSERT_OK(r);
ASSERT_OK(req.result);
uv_fs_req_cleanup(&req);
/*
* Run the loop just to check we don't have make any extraneous uv_ref()
* calls. This should drop out immediately.
*/
uv_run(loop, UV_RUN_DEFAULT);
/* Cleanup. */
unlink("test_file");
rmdir("test_dir");
MAKE_VALGRIND_HAPPY(loop);
return 0;
}
TEST_IMPL(fs_chmod) {
int r;
uv_fs_t req;
uv_file file;
/* Setup. */
unlink("test_file");
loop = uv_default_loop();
r = uv_fs_open(NULL, &req, "test_file", UV_FS_O_RDWR | UV_FS_O_CREAT,
S_IWUSR | S_IRUSR, NULL);
ASSERT_GE(r, 0);
ASSERT_GE(req.result, 0);
file = req.result;
uv_fs_req_cleanup(&req);
iov = uv_buf_init(test_buf, sizeof(test_buf));
r = uv_fs_write(NULL, &req, file, &iov, 1, -1, NULL);
ASSERT_EQ(r, sizeof(test_buf));
ASSERT_EQ(req.result, sizeof(test_buf));
uv_fs_req_cleanup(&req);
#ifndef _WIN32
/* Make the file write-only */
r = uv_fs_chmod(NULL, &req, "test_file", 0200, NULL);
ASSERT_OK(r);
ASSERT_OK(req.result);
uv_fs_req_cleanup(&req);
check_permission("test_file", 0200);
#endif
/* Make the file read-only */
r = uv_fs_chmod(NULL, &req, "test_file", 0400, NULL);
ASSERT_OK(r);
ASSERT_OK(req.result);
uv_fs_req_cleanup(&req);
check_permission("test_file", 0400);
/* Make the file read+write with sync uv_fs_fchmod */
r = uv_fs_fchmod(NULL, &req, file, 0600, NULL);
ASSERT_OK(r);
ASSERT_OK(req.result);
uv_fs_req_cleanup(&req);
check_permission("test_file", 0600);
#ifndef _WIN32
/* async chmod */
{
static int mode = 0200;
req.data = &mode;
}
r = uv_fs_chmod(loop, &req, "test_file", 0200, chmod_cb);
ASSERT_OK(r);
uv_run(loop, UV_RUN_DEFAULT);
ASSERT_EQ(1, chmod_cb_count);
chmod_cb_count = 0; /* reset for the next test */
#endif
/* async chmod */
{
static int mode = 0400;
req.data = &mode;
}
r = uv_fs_chmod(loop, &req, "test_file", 0400, chmod_cb);
ASSERT_OK(r);
uv_run(loop, UV_RUN_DEFAULT);
ASSERT_EQ(1, chmod_cb_count);
/* async fchmod */
{
static int mode = 0600;
req.data = &mode;
}
r = uv_fs_fchmod(loop, &req, file, 0600, fchmod_cb);
ASSERT_OK(r);
uv_run(loop, UV_RUN_DEFAULT);
ASSERT_EQ(1, fchmod_cb_count);
uv_fs_close(loop, &req, file, NULL);
/*
* Run the loop just to check we don't have make any extraneous uv_ref()
* calls. This should drop out immediately.
*/
uv_run(loop, UV_RUN_DEFAULT);
/* Cleanup. */
unlink("test_file");
MAKE_VALGRIND_HAPPY(loop);
return 0;
}
TEST_IMPL(fs_unlink_readonly) {
int r;
uv_fs_t req;
uv_file file;
/* Setup. */
unlink("test_file");
loop = uv_default_loop();
r = uv_fs_open(NULL,
&req, "test_file", UV_FS_O_RDWR | UV_FS_O_CREAT,
S_IWUSR | S_IRUSR,
NULL);
ASSERT_GE(r, 0);
ASSERT_GE(req.result, 0);
file = req.result;
uv_fs_req_cleanup(&req);
iov = uv_buf_init(test_buf, sizeof(test_buf));
r = uv_fs_write(NULL, &req, file, &iov, 1, -1, NULL);
ASSERT_EQ(r, sizeof(test_buf));
ASSERT_EQ(req.result, sizeof(test_buf));
uv_fs_req_cleanup(&req);
uv_fs_close(loop, &req, file, NULL);
/* Make the file read-only */
r = uv_fs_chmod(NULL, &req, "test_file", 0400, NULL);
ASSERT_OK(r);
ASSERT_OK(req.result);
uv_fs_req_cleanup(&req);
check_permission("test_file", 0400);
/* Try to unlink the file */
r = uv_fs_unlink(NULL, &req, "test_file", NULL);
ASSERT_OK(r);
ASSERT_OK(req.result);
uv_fs_req_cleanup(&req);
/*
* Run the loop just to check we don't have make any extraneous uv_ref()
* calls. This should drop out immediately.
*/
uv_run(loop, UV_RUN_DEFAULT);
/* Cleanup. */
uv_fs_chmod(NULL, &req, "test_file", 0600, NULL);
uv_fs_req_cleanup(&req);
unlink("test_file");
MAKE_VALGRIND_HAPPY(loop);
return 0;
}
#ifdef _WIN32
TEST_IMPL(fs_unlink_archive_readonly) {
int r;
uv_fs_t req;
uv_file file;
/* Setup. */
unlink("test_file");
loop = uv_default_loop();
r = uv_fs_open(NULL,
&req, "test_file", UV_FS_O_RDWR | UV_FS_O_CREAT,
S_IWUSR | S_IRUSR,
NULL);
ASSERT_GE(r, 0);
ASSERT_GE(req.result, 0);
file = req.result;
uv_fs_req_cleanup(&req);
iov = uv_buf_init(test_buf, sizeof(test_buf));
r = uv_fs_write(NULL, &req, file, &iov, 1, -1, NULL);
ASSERT_EQ(r, sizeof(test_buf));
ASSERT_EQ(req.result, sizeof(test_buf));
uv_fs_req_cleanup(&req);
uv_fs_close(loop, &req, file, NULL);
/* Make the file read-only and clear archive flag */
r = SetFileAttributes("test_file", FILE_ATTRIBUTE_READONLY);
ASSERT(r);
uv_fs_req_cleanup(&req);
check_permission("test_file", 0400);
/* Try to unlink the file */
r = uv_fs_unlink(NULL, &req, "test_file", NULL);
ASSERT_OK(r);
ASSERT_OK(req.result);
uv_fs_req_cleanup(&req);
/*
* Run the loop just to check we don't have make any extraneous uv_ref()
* calls. This should drop out immediately.
*/
uv_run(loop, UV_RUN_DEFAULT);
/* Cleanup. */
uv_fs_chmod(NULL, &req, "test_file", 0600, NULL);
uv_fs_req_cleanup(&req);
unlink("test_file");
MAKE_VALGRIND_HAPPY(loop);
return 0;
}
#endif
TEST_IMPL(fs_chown) {
int r;
uv_fs_t req;
uv_file file;
/* Setup. */
unlink("test_file");
unlink("test_file_link");
loop = uv_default_loop();
r = uv_fs_open(NULL, &req, "test_file", UV_FS_O_RDWR | UV_FS_O_CREAT,
S_IWUSR | S_IRUSR, NULL);
ASSERT_GE(r, 0);
ASSERT_GE(req.result, 0);
file = req.result;
uv_fs_req_cleanup(&req);
/* sync chown */
r = uv_fs_chown(NULL, &req, "test_file", -1, -1, NULL);
ASSERT_OK(r);
ASSERT_OK(req.result);
uv_fs_req_cleanup(&req);
/* sync fchown */
r = uv_fs_fchown(NULL, &req, file, -1, -1, NULL);
ASSERT_OK(r);
ASSERT_OK(req.result);
uv_fs_req_cleanup(&req);
/* async chown */
r = uv_fs_chown(loop, &req, "test_file", -1, -1, chown_cb);
ASSERT_OK(r);
uv_run(loop, UV_RUN_DEFAULT);
ASSERT_EQ(1, chown_cb_count);
#ifndef __MVS__
/* chown to root (fail) */
chown_cb_count = 0;
r = uv_fs_chown(loop, &req, "test_file", 0, 0, chown_root_cb);
ASSERT_OK(r);
uv_run(loop, UV_RUN_DEFAULT);
ASSERT_EQ(1, chown_cb_count);
#endif
/* async fchown */
r = uv_fs_fchown(loop, &req, file, -1, -1, fchown_cb);
ASSERT_OK(r);
uv_run(loop, UV_RUN_DEFAULT);
ASSERT_EQ(1, fchown_cb_count);
#ifndef __HAIKU__
/* Haiku doesn't support hardlink */
/* sync link */
r = uv_fs_link(NULL, &req, "test_file", "test_file_link", NULL);
ASSERT_OK(r);
ASSERT_OK(req.result);
uv_fs_req_cleanup(&req);
/* sync lchown */
r = uv_fs_lchown(NULL, &req, "test_file_link", -1, -1, NULL);
ASSERT_OK(r);
ASSERT_OK(req.result);
uv_fs_req_cleanup(&req);
/* async lchown */
r = uv_fs_lchown(loop, &req, "test_file_link", -1, -1, lchown_cb);
ASSERT_OK(r);
uv_run(loop, UV_RUN_DEFAULT);
ASSERT_EQ(1, lchown_cb_count);
#endif
/* Close file */
r = uv_fs_close(NULL, &req, file, NULL);
ASSERT_OK(r);
ASSERT_OK(req.result);
uv_fs_req_cleanup(&req);
/*
* Run the loop just to check we don't have make any extraneous uv_ref()
* calls. This should drop out immediately.
*/
uv_run(loop, UV_RUN_DEFAULT);
/* Cleanup. */
unlink("test_file");
unlink("test_file_link");
MAKE_VALGRIND_HAPPY(loop);
return 0;
}
TEST_IMPL(fs_link) {
int r;
uv_fs_t req;
uv_file file;
uv_file link;
/* Setup. */
unlink("test_file");
unlink("test_file_link");
unlink("test_file_link2");
loop = uv_default_loop();
r = uv_fs_open(NULL, &req, "test_file", UV_FS_O_RDWR | UV_FS_O_CREAT,
S_IWUSR | S_IRUSR, NULL);
ASSERT_GE(r, 0);
ASSERT_GE(req.result, 0);
file = req.result;
uv_fs_req_cleanup(&req);
iov = uv_buf_init(test_buf, sizeof(test_buf));
r = uv_fs_write(NULL, &req, file, &iov, 1, -1, NULL);
ASSERT_EQ(r, sizeof(test_buf));
ASSERT_EQ(req.result, sizeof(test_buf));
uv_fs_req_cleanup(&req);
uv_fs_close(loop, &req, file, NULL);
/* sync link */
r = uv_fs_link(NULL, &req, "test_file", "test_file_link", NULL);
ASSERT_OK(r);
ASSERT_OK(req.result);
uv_fs_req_cleanup(&req);
r = uv_fs_open(NULL, &req, "test_file_link", UV_FS_O_RDWR, 0, NULL);
ASSERT_GE(r, 0);
ASSERT_GE(req.result, 0);
link = req.result;
uv_fs_req_cleanup(&req);
memset(buf, 0, sizeof(buf));
iov = uv_buf_init(buf, sizeof(buf));
r = uv_fs_read(NULL, &req, link, &iov, 1, 0, NULL);
ASSERT_GE(r, 0);
ASSERT_GE(req.result, 0);
ASSERT_OK(strcmp(buf, test_buf));
close(link);
/* async link */
r = uv_fs_link(loop, &req, "test_file", "test_file_link2", link_cb);
ASSERT_OK(r);
uv_run(loop, UV_RUN_DEFAULT);
ASSERT_EQ(1, link_cb_count);
r = uv_fs_open(NULL, &req, "test_file_link2", UV_FS_O_RDWR, 0, NULL);
ASSERT_GE(r, 0);
ASSERT_GE(req.result, 0);
link = req.result;
uv_fs_req_cleanup(&req);
memset(buf, 0, sizeof(buf));
iov = uv_buf_init(buf, sizeof(buf));
r = uv_fs_read(NULL, &req, link, &iov, 1, 0, NULL);
ASSERT_GE(r, 0);
ASSERT_GE(req.result, 0);
ASSERT_OK(strcmp(buf, test_buf));
uv_fs_close(loop, &req, link, NULL);
/*
* Run the loop just to check we don't have make any extraneous uv_ref()
* calls. This should drop out immediately.
*/
uv_run(loop, UV_RUN_DEFAULT);
/* Cleanup. */
unlink("test_file");
unlink("test_file_link");
unlink("test_file_link2");
MAKE_VALGRIND_HAPPY(loop);
return 0;
}
TEST_IMPL(fs_readlink) {
/* Must return UV_ENOENT on an inexistent file */
{
uv_fs_t req;
loop = uv_default_loop();
ASSERT_OK(uv_fs_readlink(loop, &req, "no_such_file", dummy_cb));
ASSERT_OK(uv_run(loop, UV_RUN_DEFAULT));
ASSERT_EQ(1, dummy_cb_count);
ASSERT_NULL(req.ptr);
ASSERT_EQ(req.result, UV_ENOENT);
uv_fs_req_cleanup(&req);
ASSERT_EQ(UV_ENOENT, uv_fs_readlink(NULL, &req, "no_such_file", NULL));
ASSERT_NULL(req.ptr);
ASSERT_EQ(req.result, UV_ENOENT);
uv_fs_req_cleanup(&req);
}
/* Must return UV_EINVAL on a non-symlink file */
{
int r;
uv_fs_t req;
uv_file file;
/* Setup */
/* Create a non-symlink file */
r = uv_fs_open(NULL, &req, "test_file", UV_FS_O_RDWR | UV_FS_O_CREAT,
S_IWUSR | S_IRUSR, NULL);
ASSERT_GE(r, 0);
ASSERT_GE(req.result, 0);
file = req.result;
uv_fs_req_cleanup(&req);
r = uv_fs_close(NULL, &req, file, NULL);
ASSERT_OK(r);
ASSERT_OK(req.result);
uv_fs_req_cleanup(&req);
/* Test */
r = uv_fs_readlink(NULL, &req, "test_file", NULL);
ASSERT_EQ(r, UV_EINVAL);
uv_fs_req_cleanup(&req);
/* Cleanup */
unlink("test_file");
}
MAKE_VALGRIND_HAPPY(loop);
return 0;
}
TEST_IMPL(fs_realpath) {
uv_fs_t req;
loop = uv_default_loop();
ASSERT_OK(uv_fs_realpath(loop, &req, "no_such_file", dummy_cb));
ASSERT_OK(uv_run(loop, UV_RUN_DEFAULT));
ASSERT_EQ(1, dummy_cb_count);
ASSERT_NULL(req.ptr);
ASSERT_EQ(req.result, UV_ENOENT);
uv_fs_req_cleanup(&req);
ASSERT_EQ(UV_ENOENT, uv_fs_realpath(NULL, &req, "no_such_file", NULL));
ASSERT_NULL(req.ptr);
ASSERT_EQ(req.result, UV_ENOENT);
uv_fs_req_cleanup(&req);
MAKE_VALGRIND_HAPPY(loop);
return 0;
}
TEST_IMPL(fs_symlink) {
int r;
uv_fs_t req;
uv_file file;
uv_file link;
char test_file_abs_buf[PATHMAX];
size_t test_file_abs_size;
/* Setup. */
unlink("test_file");
unlink("test_file_symlink");
unlink("test_file_symlink2");
unlink("test_file_symlink_symlink");
unlink("test_file_symlink2_symlink");
test_file_abs_size = sizeof(test_file_abs_buf);
#ifdef _WIN32
uv_cwd(test_file_abs_buf, &test_file_abs_size);
strcat(test_file_abs_buf, "\\test_file");
#else
uv_cwd(test_file_abs_buf, &test_file_abs_size);
strcat(test_file_abs_buf, "/test_file");
#endif
loop = uv_default_loop();
r = uv_fs_open(NULL, &req, "test_file", UV_FS_O_RDWR | UV_FS_O_CREAT,
S_IWUSR | S_IRUSR, NULL);
ASSERT_GE(r, 0);
ASSERT_GE(req.result, 0);
file = req.result;
uv_fs_req_cleanup(&req);
iov = uv_buf_init(test_buf, sizeof(test_buf));
r = uv_fs_write(NULL, &req, file, &iov, 1, -1, NULL);
ASSERT_EQ(r, sizeof(test_buf));
ASSERT_EQ(req.result, sizeof(test_buf));
uv_fs_req_cleanup(&req);
uv_fs_close(loop, &req, file, NULL);
/* sync symlink */
r = uv_fs_symlink(NULL, &req, "test_file", "test_file_symlink", 0, NULL);
#ifdef _WIN32
if (r < 0) {
if (r == UV_ENOTSUP) {
/*
* Windows doesn't support symlinks on older versions.
* We just pass the test and bail out early if we get ENOTSUP.
*/
return 0;
} else if (r == UV_EPERM) {
/*
* Creating a symlink is only allowed when running elevated.
* We pass the test and bail out early if we get UV_EPERM.
*/
return 0;
}
}
#endif
ASSERT_OK(r);
ASSERT_OK(req.result);
uv_fs_req_cleanup(&req);
r = uv_fs_open(NULL, &req, "test_file_symlink", UV_FS_O_RDWR, 0, NULL);
ASSERT_GE(r, 0);
ASSERT_GE(req.result, 0);
link = req.result;
uv_fs_req_cleanup(&req);
memset(buf, 0, sizeof(buf));
iov = uv_buf_init(buf, sizeof(buf));
r = uv_fs_read(NULL, &req, link, &iov, 1, 0, NULL);
ASSERT_GE(r, 0);
ASSERT_GE(req.result, 0);
ASSERT_OK(strcmp(buf, test_buf));
uv_fs_close(loop, &req, link, NULL);
r = uv_fs_symlink(NULL,
&req,
"test_file_symlink",
"test_file_symlink_symlink",
0,
NULL);
ASSERT_OK(r);
uv_fs_req_cleanup(&req);
#if defined(__MSYS__)
RETURN_SKIP("symlink reading is not supported on MSYS2");
#endif
r = uv_fs_readlink(NULL, &req, "test_file_symlink_symlink", NULL);
ASSERT_OK(r);
ASSERT_OK(strcmp(req.ptr, "test_file_symlink"));
uv_fs_req_cleanup(&req);
r = uv_fs_realpath(NULL, &req, "test_file_symlink_symlink", NULL);
ASSERT_OK(r);
#ifdef _WIN32
ASSERT_OK(_stricmp(req.ptr, test_file_abs_buf));
#else
ASSERT_OK(strcmp(req.ptr, test_file_abs_buf));
#endif
uv_fs_req_cleanup(&req);
/* async link */
r = uv_fs_symlink(loop,
&req,
"test_file",
"test_file_symlink2",
0,
symlink_cb);
ASSERT_OK(r);
uv_run(loop, UV_RUN_DEFAULT);
ASSERT_EQ(1, symlink_cb_count);
r = uv_fs_open(NULL, &req, "test_file_symlink2", UV_FS_O_RDWR, 0, NULL);
ASSERT_GE(r, 0);
ASSERT_GE(req.result, 0);
link = req.result;
uv_fs_req_cleanup(&req);
memset(buf, 0, sizeof(buf));
iov = uv_buf_init(buf, sizeof(buf));
r = uv_fs_read(NULL, &req, link, &iov, 1, 0, NULL);
ASSERT_GE(r, 0);
ASSERT_GE(req.result, 0);
ASSERT_OK(strcmp(buf, test_buf));
uv_fs_close(loop, &req, link, NULL);
r = uv_fs_symlink(NULL,
&req,
"test_file_symlink2",
"test_file_symlink2_symlink",
0,
NULL);
ASSERT_OK(r);
uv_fs_req_cleanup(&req);
r = uv_fs_readlink(loop, &req, "test_file_symlink2_symlink", readlink_cb);
ASSERT_OK(r);
uv_run(loop, UV_RUN_DEFAULT);
ASSERT_EQ(1, readlink_cb_count);
r = uv_fs_realpath(loop, &req, "test_file", realpath_cb);
ASSERT_OK(r);
uv_run(loop, UV_RUN_DEFAULT);
ASSERT_EQ(1, realpath_cb_count);
/*
* Run the loop just to check we don't have make any extraneous uv_ref()
* calls. This should drop out immediately.
*/
uv_run(loop, UV_RUN_DEFAULT);
/* Cleanup. */
unlink("test_file");
unlink("test_file_symlink");
unlink("test_file_symlink_symlink");
unlink("test_file_symlink2");
unlink("test_file_symlink2_symlink");
MAKE_VALGRIND_HAPPY(loop);
return 0;
}
int test_symlink_dir_impl(int type) {
uv_fs_t req;
int r;
char* test_dir;
uv_dirent_t dent;
static char test_dir_abs_buf[PATHMAX];
size_t test_dir_abs_size;
/* set-up */
unlink("test_dir/file1");
unlink("test_dir/file2");
rmdir("test_dir");
rmdir("test_dir_symlink");
test_dir_abs_size = sizeof(test_dir_abs_buf);
loop = uv_default_loop();
uv_fs_mkdir(NULL, &req, "test_dir", 0777, NULL);
uv_fs_req_cleanup(&req);
#ifdef _WIN32
strcpy(test_dir_abs_buf, "\\\\?\\");
uv_cwd(test_dir_abs_buf + 4, &test_dir_abs_size);
test_dir_abs_size += 4;
strcat(test_dir_abs_buf, "\\test_dir");
test_dir_abs_size += strlen("\\test_dir");
test_dir = test_dir_abs_buf;
#else
uv_cwd(test_dir_abs_buf, &test_dir_abs_size);
strcat(test_dir_abs_buf, "/test_dir");
test_dir_abs_size += strlen("/test_dir");
test_dir = "test_dir";
#endif
r = uv_fs_symlink(NULL, &req, test_dir, "test_dir_symlink", type, NULL);
if (type == UV_FS_SYMLINK_DIR && (r == UV_ENOTSUP || r == UV_EPERM)) {
uv_fs_req_cleanup(&req);
RETURN_SKIP("this version of Windows doesn't support unprivileged "
"creation of directory symlinks");
}
fprintf(stderr, "r == %i\n", r);
ASSERT_OK(r);
ASSERT_OK(req.result);
uv_fs_req_cleanup(&req);
r = uv_fs_stat(NULL, &req, "test_dir_symlink", NULL);
ASSERT_OK(r);
ASSERT(((uv_stat_t*)req.ptr)->st_mode & S_IFDIR);
uv_fs_req_cleanup(&req);
r = uv_fs_lstat(NULL, &req, "test_dir_symlink", NULL);
ASSERT_OK(r);
#if defined(__MSYS__)
RETURN_SKIP("symlink reading is not supported on MSYS2");
#endif
ASSERT(((uv_stat_t*)req.ptr)->st_mode & S_IFLNK);
#ifdef _WIN32
ASSERT_EQ(((uv_stat_t*)req.ptr)->st_size, strlen(test_dir + 4));
#else
# ifdef __PASE__
/* On IBMi PASE, st_size returns the length of the symlink itself. */
ASSERT_EQ(((uv_stat_t*)req.ptr)->st_size, strlen("test_dir_symlink"));
# else
ASSERT_EQ(((uv_stat_t*)req.ptr)->st_size, strlen(test_dir));
# endif
#endif
uv_fs_req_cleanup(&req);
r = uv_fs_readlink(NULL, &req, "test_dir_symlink", NULL);
ASSERT_OK(r);
#ifdef _WIN32
ASSERT_OK(strcmp(req.ptr, test_dir + 4));
#else
ASSERT_OK(strcmp(req.ptr, test_dir));
#endif
uv_fs_req_cleanup(&req);
r = uv_fs_realpath(NULL, &req, "test_dir_symlink", NULL);
ASSERT_OK(r);
#ifdef _WIN32
ASSERT_EQ(strlen(req.ptr), test_dir_abs_size - 4);
ASSERT_OK(_strnicmp(req.ptr, test_dir + 4, test_dir_abs_size - 4));
#else
ASSERT_OK(strcmp(req.ptr, test_dir_abs_buf));
#endif
uv_fs_req_cleanup(&req);
r = uv_fs_open(NULL, &open_req1, "test_dir/file1",
UV_FS_O_WRONLY | UV_FS_O_CREAT,
S_IWUSR | S_IRUSR, NULL);
ASSERT_GE(r, 0);
uv_fs_req_cleanup(&open_req1);
r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
ASSERT_OK(r);
uv_fs_req_cleanup(&close_req);
r = uv_fs_open(NULL, &open_req1, "test_dir/file2",
UV_FS_O_WRONLY | UV_FS_O_CREAT,
S_IWUSR | S_IRUSR, NULL);
ASSERT_GE(r, 0);
uv_fs_req_cleanup(&open_req1);
r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
ASSERT_OK(r);
uv_fs_req_cleanup(&close_req);
r = uv_fs_scandir(NULL, &scandir_req, "test_dir_symlink", 0, NULL);
ASSERT_EQ(2, r);
ASSERT_EQ(2, scandir_req.result);
ASSERT(scandir_req.ptr);
while (UV_EOF != uv_fs_scandir_next(&scandir_req, &dent)) {
ASSERT(strcmp(dent.name, "file1") == 0 || strcmp(dent.name, "file2") == 0);
assert_is_file_type(dent);
}
uv_fs_req_cleanup(&scandir_req);
ASSERT(!scandir_req.ptr);
/* unlink will remove the directory symlink */
r = uv_fs_unlink(NULL, &req, "test_dir_symlink", NULL);
ASSERT_OK(r);
uv_fs_req_cleanup(&req);
r = uv_fs_scandir(NULL, &scandir_req, "test_dir_symlink", 0, NULL);
ASSERT_EQ(r, UV_ENOENT);
uv_fs_req_cleanup(&scandir_req);
r = uv_fs_scandir(NULL, &scandir_req, "test_dir", 0, NULL);
ASSERT_EQ(2, r);
ASSERT_EQ(2, scandir_req.result);
ASSERT(scandir_req.ptr);
while (UV_EOF != uv_fs_scandir_next(&scandir_req, &dent)) {
ASSERT(strcmp(dent.name, "file1") == 0 || strcmp(dent.name, "file2") == 0);
assert_is_file_type(dent);
}
uv_fs_req_cleanup(&scandir_req);
ASSERT(!scandir_req.ptr);
/* clean-up */
unlink("test_dir/file1");
unlink("test_dir/file2");
rmdir("test_dir");
rmdir("test_dir_symlink");
MAKE_VALGRIND_HAPPY(loop);
return 0;
}
TEST_IMPL(fs_symlink_dir) {
return test_symlink_dir_impl(UV_FS_SYMLINK_DIR);
}
TEST_IMPL(fs_symlink_junction) {
return test_symlink_dir_impl(UV_FS_SYMLINK_JUNCTION);
}
#ifdef _WIN32
TEST_IMPL(fs_non_symlink_reparse_point) {
uv_fs_t req;
int r;
HANDLE file_handle;
REPARSE_GUID_DATA_BUFFER reparse_buffer;
DWORD bytes_returned;
uv_dirent_t dent;
/* set-up */
unlink("test_dir/test_file");
rmdir("test_dir");
loop = uv_default_loop();
uv_fs_mkdir(NULL, &req, "test_dir", 0777, NULL);
uv_fs_req_cleanup(&req);
file_handle = CreateFile("test_dir/test_file",
GENERIC_WRITE | FILE_WRITE_ATTRIBUTES,
0,
NULL,
CREATE_ALWAYS,
FILE_FLAG_OPEN_REPARSE_POINT |
FILE_FLAG_BACKUP_SEMANTICS,
NULL);
ASSERT_PTR_NE(file_handle, INVALID_HANDLE_VALUE);
memset(&reparse_buffer, 0, REPARSE_GUID_DATA_BUFFER_HEADER_SIZE);
reparse_buffer.ReparseTag = REPARSE_TAG;
reparse_buffer.ReparseDataLength = 0;
reparse_buffer.ReparseGuid = REPARSE_GUID;
r = DeviceIoControl(file_handle,
FSCTL_SET_REPARSE_POINT,
&reparse_buffer,
REPARSE_GUID_DATA_BUFFER_HEADER_SIZE,
NULL,
0,
&bytes_returned,
NULL);
ASSERT(r);
CloseHandle(file_handle);
r = uv_fs_readlink(NULL, &req, "test_dir/test_file", NULL);
ASSERT(r == UV_EINVAL && GetLastError() == ERROR_SYMLINK_NOT_SUPPORTED);
uv_fs_req_cleanup(&req);
/*
Placeholder tests for exercising the behavior fixed in issue #995.
To run, update the path with the IP address of a Mac with the hard drive
shared via SMB as "Macintosh HD".
r = uv_fs_stat(NULL, &req, "\\\\<mac_ip>\\Macintosh HD\\.DS_Store", NULL);
ASSERT_OK(r);
uv_fs_req_cleanup(&req);
r = uv_fs_lstat(NULL, &req, "\\\\<mac_ip>\\Macintosh HD\\.DS_Store", NULL);
ASSERT_OK(r);
uv_fs_req_cleanup(&req);
*/
/*
uv_fs_stat and uv_fs_lstat can only work on non-symlink reparse
points when a minifilter driver is registered which intercepts
associated filesystem requests. Installing a driver is beyond
the scope of this test.
r = uv_fs_stat(NULL, &req, "test_dir/test_file", NULL);
ASSERT_OK(r);
uv_fs_req_cleanup(&req);
r = uv_fs_lstat(NULL, &req, "test_dir/test_file", NULL);
ASSERT_OK(r);
uv_fs_req_cleanup(&req);
*/
r = uv_fs_scandir(NULL, &scandir_req, "test_dir", 0, NULL);
ASSERT_EQ(1, r);
ASSERT_EQ(1, scandir_req.result);
ASSERT(scandir_req.ptr);
while (UV_EOF != uv_fs_scandir_next(&scandir_req, &dent)) {
ASSERT_OK(strcmp(dent.name, "test_file"));
/* uv_fs_scandir incorrectly identifies non-symlink reparse points
as links because it doesn't open the file and verify the reparse
point tag. The PowerShell Get-ChildItem command shares this
behavior, so it's reasonable to leave it as is. */
ASSERT_EQ(dent.type, UV_DIRENT_LINK);
}
uv_fs_req_cleanup(&scandir_req);
ASSERT(!scandir_req.ptr);
/* clean-up */
unlink("test_dir/test_file");
rmdir("test_dir");
MAKE_VALGRIND_HAPPY(loop);
return 0;
}
TEST_IMPL(fs_lstat_windows_store_apps) {
uv_loop_t* loop;
char localappdata[MAX_PATH];
char windowsapps_path[MAX_PATH];
char file_path[MAX_PATH];
size_t len;
int r;
uv_fs_t req;
uv_fs_t stat_req;
uv_dirent_t dirent;
loop = uv_default_loop();
ASSERT_NOT_NULL(loop);
len = sizeof(localappdata);
r = uv_os_getenv("LOCALAPPDATA", localappdata, &len);
if (r == UV_ENOENT) {
MAKE_VALGRIND_HAPPY(loop);
return TEST_SKIP;
}
ASSERT_OK(r);
r = snprintf(windowsapps_path,
sizeof(localappdata),
"%s\\Microsoft\\WindowsApps",
localappdata);
ASSERT_GT(r, 0);
if (uv_fs_opendir(loop, &req, windowsapps_path, NULL) != 0) {
/* If we cannot read the directory, skip the test. */
MAKE_VALGRIND_HAPPY(loop);
return TEST_SKIP;
}
if (uv_fs_scandir(loop, &req, windowsapps_path, 0, NULL) <= 0) {
MAKE_VALGRIND_HAPPY(loop);
return TEST_SKIP;
}
while (uv_fs_scandir_next(&req, &dirent) != UV_EOF) {
if (dirent.type != UV_DIRENT_LINK) {
continue;
}
if (snprintf(file_path,
sizeof(file_path),
"%s\\%s",
windowsapps_path,
dirent.name) < 0) {
continue;
}
ASSERT_OK(uv_fs_lstat(loop, &stat_req, file_path, NULL));
}
MAKE_VALGRIND_HAPPY(loop);
return 0;
}
#endif
TEST_IMPL(fs_utime) {
utime_check_t checkme;
const char* path = "test_file";
double atime;
double mtime;
uv_fs_t req;
int r;
/* Setup. */
loop = uv_default_loop();
unlink(path);
r = uv_fs_open(NULL, &req, path, UV_FS_O_RDWR | UV_FS_O_CREAT,
S_IWUSR | S_IRUSR,
NULL);
ASSERT_GE(r, 0);
ASSERT_GE(req.result, 0);
uv_fs_req_cleanup(&req);
uv_fs_close(loop, &req, r, NULL);
atime = mtime = 400497753.25; /* 1982-09-10 11:22:33.25 */
ASSERT_OK(uv_fs_utime(NULL, &req, path, atime, mtime, NULL));
ASSERT_OK(req.result);
uv_fs_req_cleanup(&req);
check_utime(path, atime, mtime, /* test_lutime */ 0);
ASSERT_OK(uv_fs_utime(NULL,
&req,
path,
UV_FS_UTIME_OMIT,
UV_FS_UTIME_OMIT,
NULL));
ASSERT_OK(req.result);
uv_fs_req_cleanup(&req);
check_utime(path, atime, mtime, /* test_lutime */ 0);
ASSERT_OK(uv_fs_utime(NULL,
&req,
path,
UV_FS_UTIME_NOW,
UV_FS_UTIME_OMIT,
NULL));
ASSERT_OK(req.result);
uv_fs_req_cleanup(&req);
check_utime(path, UV_FS_UTIME_NOW, mtime, /* test_lutime */ 0);
ASSERT_OK(uv_fs_utime(NULL, &req, path, atime, mtime, NULL));
ASSERT_OK(req.result);
uv_fs_req_cleanup(&req);
check_utime(path, atime, mtime, /* test_lutime */ 0);
ASSERT_OK(uv_fs_utime(NULL,
&req,
path,
UV_FS_UTIME_OMIT,
UV_FS_UTIME_NOW,
NULL));
ASSERT_OK(req.result);
uv_fs_req_cleanup(&req);
check_utime(path, atime, UV_FS_UTIME_NOW, /* test_lutime */ 0);
atime = mtime = 1291404900.25; /* 2010-12-03 20:35:00.25 - mees <3 */
checkme.path = path;
checkme.atime = atime;
checkme.mtime = mtime;
/* async utime */
utime_req.data = &checkme;
r = uv_fs_utime(loop, &utime_req, path, atime, mtime, utime_cb);
ASSERT_OK(r);
uv_run(loop, UV_RUN_DEFAULT);
ASSERT_EQ(1, utime_cb_count);
/* Cleanup. */
unlink(path);
MAKE_VALGRIND_HAPPY(loop);
return 0;
}
TEST_IMPL(fs_utime_round) {
const char path[] = "test_file";
double atime;
double mtime;
uv_fs_t req;
int r;
loop = uv_default_loop();
unlink(path);
r = uv_fs_open(NULL, &req, path, UV_FS_O_RDWR | UV_FS_O_CREAT,
S_IWUSR | S_IRUSR,
NULL);
ASSERT_GE(r, 0);
ASSERT_GE(req.result, 0);
uv_fs_req_cleanup(&req);
ASSERT_OK(uv_fs_close(loop, &req, r, NULL));
atime = mtime = -14245440.25; /* 1969-07-20T02:56:00.25Z */
r = uv_fs_utime(NULL, &req, path, atime, mtime, NULL);
#if !defined(__linux__) && \
!defined(_WIN32) && \
!defined(__APPLE__) && \
!defined(__FreeBSD__) && \
!defined(__sun)
if (r != 0) {
ASSERT_EQ(r, UV_EINVAL);
RETURN_SKIP("utime on some OS (z/OS, IBM i PASE, AIX) or filesystems may reject pre-epoch timestamps");
}
#endif
ASSERT_OK(r);
ASSERT_OK(req.result);
uv_fs_req_cleanup(&req);
check_utime(path, atime, mtime, /* test_lutime */ 0);
unlink(path);
MAKE_VALGRIND_HAPPY(loop);
return 0;
}
#ifdef _WIN32
TEST_IMPL(fs_stat_root) {
int r;
r = uv_fs_stat(NULL, &stat_req, "\\", NULL);
ASSERT_OK(r);
r = uv_fs_stat(NULL, &stat_req, "..\\..\\..\\..\\..\\..\\..", NULL);
ASSERT_OK(r);
r = uv_fs_stat(NULL, &stat_req, "..", NULL);
ASSERT_OK(r);
r = uv_fs_stat(NULL, &stat_req, "..\\", NULL);
ASSERT_OK(r);
/* stats the current directory on c: */
r = uv_fs_stat(NULL, &stat_req, "c:", NULL);
ASSERT_OK(r);
r = uv_fs_stat(NULL, &stat_req, "c:\\", NULL);
ASSERT_OK(r);
r = uv_fs_stat(NULL, &stat_req, "\\\\?\\C:\\", NULL);
ASSERT_OK(r);
MAKE_VALGRIND_HAPPY(uv_default_loop());
return 0;
}
#endif
TEST_IMPL(fs_futime) {
utime_check_t checkme;
const char* path = "test_file";
double atime;
double mtime;
uv_file file;
uv_fs_t req;
int r;
#if defined(_AIX) && !defined(_AIX71)
RETURN_SKIP("futime is not implemented for AIX versions below 7.1");
#endif
/* Setup. */
loop = uv_default_loop();
unlink(path);
r = uv_fs_open(NULL, &req, path, UV_FS_O_RDWR | UV_FS_O_CREAT,
S_IWUSR | S_IRUSR,
NULL);
ASSERT_GE(r, 0);
ASSERT_GE(req.result, 0);
uv_fs_req_cleanup(&req);
uv_fs_close(loop, &req, r, NULL);
atime = mtime = 400497753.25; /* 1982-09-10 11:22:33.25 */
r = uv_fs_open(NULL, &req, path, UV_FS_O_RDWR, 0, NULL);
ASSERT_GE(r, 0);
ASSERT_GE(req.result, 0);
file = req.result; /* FIXME probably not how it's supposed to be used */
uv_fs_req_cleanup(&req);
r = uv_fs_futime(NULL, &req, file, atime, mtime, NULL);
#if defined(__CYGWIN__) || defined(__MSYS__)
ASSERT_EQ(r, UV_ENOSYS);
RETURN_SKIP("futime not supported on Cygwin");
#else
ASSERT_OK(r);
ASSERT_OK(req.result);
#endif
uv_fs_req_cleanup(&req);
check_utime(path, atime, mtime, /* test_lutime */ 0);
ASSERT_OK(uv_fs_futime(NULL,
&req,
file,
UV_FS_UTIME_OMIT,
UV_FS_UTIME_OMIT,
NULL));
ASSERT_OK(req.result);
uv_fs_req_cleanup(&req);
check_utime(path, atime, mtime, /* test_lutime */ 0);
ASSERT_OK(uv_fs_futime(NULL,
&req,
file,
UV_FS_UTIME_NOW,
UV_FS_UTIME_OMIT,
NULL));
ASSERT_OK(req.result);
uv_fs_req_cleanup(&req);
check_utime(path, UV_FS_UTIME_NOW, mtime, /* test_lutime */ 0);
ASSERT_OK(uv_fs_futime(NULL, &req, file, atime, mtime, NULL));
ASSERT_OK(req.result);
uv_fs_req_cleanup(&req);
check_utime(path, atime, mtime, /* test_lutime */ 0);
ASSERT_OK(uv_fs_futime(NULL,
&req,
file,
UV_FS_UTIME_OMIT,
UV_FS_UTIME_NOW,
NULL));
ASSERT_OK(req.result);
uv_fs_req_cleanup(&req);
check_utime(path, atime, UV_FS_UTIME_NOW, /* test_lutime */ 0);
atime = mtime = 1291404900; /* 2010-12-03 20:35:00 - mees <3 */
checkme.atime = atime;
checkme.mtime = mtime;
checkme.path = path;
/* async futime */
futime_req.data = &checkme;
r = uv_fs_futime(loop, &futime_req, file, atime, mtime, futime_cb);
ASSERT_OK(r);
uv_run(loop, UV_RUN_DEFAULT);
ASSERT_EQ(1, futime_cb_count);
/* Cleanup. */
unlink(path);
MAKE_VALGRIND_HAPPY(loop);
return 0;
}
TEST_IMPL(fs_lutime) {
utime_check_t checkme;
const char* path = "test_file";
const char* symlink_path = "test_file_symlink";
double atime;
double mtime;
uv_fs_t req;
int r, s;
/* Setup */
loop = uv_default_loop();
unlink(path);
r = uv_fs_open(NULL, &req, path, UV_FS_O_RDWR | UV_FS_O_CREAT,
S_IWUSR | S_IRUSR,
NULL);
ASSERT_GE(r, 0);
ASSERT_GE(req.result, 0);
uv_fs_req_cleanup(&req);
uv_fs_close(loop, &req, r, NULL);
unlink(symlink_path);
s = uv_fs_symlink(NULL, &req, path, symlink_path, 0, NULL);
#ifdef _WIN32
if (s == UV_EPERM) {
/*
* Creating a symlink before Windows 10 Creators Update was only allowed
* when running elevated console (with admin rights)
*/
RETURN_SKIP(
"Symlink creation requires elevated console (with admin rights)");
}
#endif
ASSERT_OK(s);
ASSERT_OK(req.result);
uv_fs_req_cleanup(&req);
/* Test the synchronous version. */
atime = mtime = 400497753.25; /* 1982-09-10 11:22:33.25 */
r = uv_fs_lutime(NULL, &req, symlink_path, atime, mtime, NULL);
#if (defined(_AIX) && !defined(_AIX71)) || defined(__MVS__)
ASSERT_EQ(r, UV_ENOSYS);
RETURN_SKIP("lutime is not implemented for z/OS and AIX versions below 7.1");
#endif
ASSERT_OK(r);
ASSERT_OK(req.result);
uv_fs_req_cleanup(&req);
check_utime(symlink_path, atime, mtime, /* test_lutime */ 1);
ASSERT_OK(uv_fs_lutime(NULL,
&req,
symlink_path,
UV_FS_UTIME_OMIT,
UV_FS_UTIME_OMIT,
NULL));
ASSERT_OK(req.result);
uv_fs_req_cleanup(&req);
check_utime(symlink_path, atime, mtime, /* test_lutime */ 1);
ASSERT_OK(uv_fs_lutime(NULL,
&req,
symlink_path,
UV_FS_UTIME_NOW,
UV_FS_UTIME_OMIT,
NULL));
ASSERT_OK(req.result);
uv_fs_req_cleanup(&req);
check_utime(symlink_path, UV_FS_UTIME_NOW, mtime, /* test_lutime */ 1);
ASSERT_OK(uv_fs_lutime(NULL, &req, symlink_path, atime, mtime, NULL));
ASSERT_OK(req.result);
uv_fs_req_cleanup(&req);
check_utime(symlink_path, atime, mtime, /* test_lutime */ 1);
ASSERT_OK(uv_fs_lutime(NULL,
&req,
symlink_path,
UV_FS_UTIME_OMIT,
UV_FS_UTIME_NOW,
NULL));
ASSERT_OK(req.result);
uv_fs_req_cleanup(&req);
check_utime(symlink_path, atime, UV_FS_UTIME_NOW, /* test_lutime */ 1);
/* Test the asynchronous version. */
atime = mtime = 1291404900; /* 2010-12-03 20:35:00 */
checkme.atime = atime;
checkme.mtime = mtime;
checkme.path = symlink_path;
req.data = &checkme;
r = uv_fs_lutime(loop, &req, symlink_path, atime, mtime, lutime_cb);
ASSERT_OK(r);
uv_run(loop, UV_RUN_DEFAULT);
ASSERT_EQ(1, lutime_cb_count);
/* Cleanup. */
unlink(path);
unlink(symlink_path);
MAKE_VALGRIND_HAPPY(loop);
return 0;
}
TEST_IMPL(fs_stat_missing_path) {
uv_fs_t req;
int r;
loop = uv_default_loop();
r = uv_fs_stat(NULL, &req, "non_existent_file", NULL);
ASSERT_EQ(r, UV_ENOENT);
ASSERT_EQ(req.result, UV_ENOENT);
uv_fs_req_cleanup(&req);
MAKE_VALGRIND_HAPPY(loop);
return 0;
}
TEST_IMPL(fs_scandir_empty_dir) {
const char* path;
uv_fs_t req;
uv_dirent_t dent;
int r;
path = "./empty_dir/";
loop = uv_default_loop();
uv_fs_mkdir(NULL, &req, path, 0777, NULL);
uv_fs_req_cleanup(&req);
/* Fill the req to ensure that required fields are cleaned up */
memset(&req, 0xdb, sizeof(req));
r = uv_fs_scandir(NULL, &req, path, 0, NULL);
ASSERT_OK(r);
ASSERT_OK(req.result);
ASSERT_NULL(req.ptr);
ASSERT_EQ(UV_EOF, uv_fs_scandir_next(&req, &dent));
uv_fs_req_cleanup(&req);
r = uv_fs_scandir(loop, &scandir_req, path, 0, empty_scandir_cb);
ASSERT_OK(r);
ASSERT_OK(scandir_cb_count);
uv_run(loop, UV_RUN_DEFAULT);
ASSERT_EQ(1, scandir_cb_count);
uv_fs_rmdir(NULL, &req, path, NULL);
uv_fs_req_cleanup(&req);
MAKE_VALGRIND_HAPPY(loop);
return 0;
}
TEST_IMPL(fs_scandir_non_existent_dir) {
const char* path;
uv_fs_t req;
uv_dirent_t dent;
int r;
path = "./non_existent_dir/";
loop = uv_default_loop();
uv_fs_rmdir(NULL, &req, path, NULL);
uv_fs_req_cleanup(&req);
/* Fill the req to ensure that required fields are cleaned up */
memset(&req, 0xdb, sizeof(req));
r = uv_fs_scandir(NULL, &req, path, 0, NULL);
ASSERT_EQ(r, UV_ENOENT);
ASSERT_EQ(req.result, UV_ENOENT);
ASSERT_NULL(req.ptr);
ASSERT_EQ(UV_ENOENT, uv_fs_scandir_next(&req, &dent));
uv_fs_req_cleanup(&req);
r = uv_fs_scandir(loop, &scandir_req, path, 0, non_existent_scandir_cb);
ASSERT_OK(r);
ASSERT_OK(scandir_cb_count);
uv_run(loop, UV_RUN_DEFAULT);
ASSERT_EQ(1, scandir_cb_count);
MAKE_VALGRIND_HAPPY(loop);
return 0;
}
TEST_IMPL(fs_scandir_file) {
const char* path;
int r;
path = "test/fixtures/empty_file";
loop = uv_default_loop();
r = uv_fs_scandir(NULL, &scandir_req, path, 0, NULL);
ASSERT_EQ(r, UV_ENOTDIR);
uv_fs_req_cleanup(&scandir_req);
r = uv_fs_scandir(loop, &scandir_req, path, 0, file_scandir_cb);
ASSERT_OK(r);
ASSERT_OK(scandir_cb_count);
uv_run(loop, UV_RUN_DEFAULT);
ASSERT_EQ(1, scandir_cb_count);
MAKE_VALGRIND_HAPPY(loop);
return 0;
}
/* Run in Valgrind. Should not leak when the iterator isn't exhausted. */
TEST_IMPL(fs_scandir_early_exit) {
uv_dirent_t d;
uv_fs_t req;
ASSERT_LT(0, uv_fs_scandir(NULL, &req, "test/fixtures/one_file", 0, NULL));
ASSERT_NE(UV_EOF, uv_fs_scandir_next(&req, &d));
uv_fs_req_cleanup(&req);
ASSERT_LT(0, uv_fs_scandir(NULL, &req, "test/fixtures", 0, NULL));
ASSERT_NE(UV_EOF, uv_fs_scandir_next(&req, &d));
uv_fs_req_cleanup(&req);
MAKE_VALGRIND_HAPPY(uv_default_loop());
return 0;
}
TEST_IMPL(fs_open_dir) {
const char* path;
uv_fs_t req;
int r, file;
path = ".";
loop = uv_default_loop();
r = uv_fs_open(NULL, &req, path, UV_FS_O_RDONLY, 0, NULL);
ASSERT_GE(r, 0);
ASSERT_GE(req.result, 0);
ASSERT_NULL(req.ptr);
file = r;
uv_fs_req_cleanup(&req);
r = uv_fs_close(NULL, &req, file, NULL);
ASSERT_OK(r);
r = uv_fs_open(loop, &req, path, UV_FS_O_RDONLY, 0, open_cb_simple);
ASSERT_OK(r);
ASSERT_OK(open_cb_count);
uv_run(loop, UV_RUN_DEFAULT);
ASSERT_EQ(1, open_cb_count);
MAKE_VALGRIND_HAPPY(loop);
return 0;
}
static void fs_file_open_append(int add_flags) {
int r;
/* Setup. */
unlink("test_file");
loop = uv_default_loop();
r = uv_fs_open(NULL, &open_req1, "test_file",
UV_FS_O_WRONLY | UV_FS_O_CREAT | add_flags, S_IWUSR | S_IRUSR,
NULL);
ASSERT_GE(r, 0);
ASSERT_GE(open_req1.result, 0);
uv_fs_req_cleanup(&open_req1);
iov = uv_buf_init(test_buf, sizeof(test_buf));
r = uv_fs_write(NULL, &write_req, open_req1.result, &iov, 1, -1, NULL);
ASSERT_GE(r, 0);
ASSERT_GE(write_req.result, 0);
uv_fs_req_cleanup(&write_req);
r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
ASSERT_OK(r);
ASSERT_OK(close_req.result);
uv_fs_req_cleanup(&close_req);
r = uv_fs_open(NULL, &open_req1, "test_file",
UV_FS_O_RDWR | UV_FS_O_APPEND | add_flags, 0, NULL);
ASSERT_GE(r, 0);
ASSERT_GE(open_req1.result, 0);
uv_fs_req_cleanup(&open_req1);
iov = uv_buf_init(test_buf, sizeof(test_buf));
r = uv_fs_write(NULL, &write_req, open_req1.result, &iov, 1, -1, NULL);
ASSERT_GE(r, 0);
ASSERT_GE(write_req.result, 0);
uv_fs_req_cleanup(&write_req);
r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
ASSERT_OK(r);
ASSERT_OK(close_req.result);
uv_fs_req_cleanup(&close_req);
r = uv_fs_open(NULL, &open_req1, "test_file", UV_FS_O_RDONLY | add_flags,
S_IRUSR, NULL);
ASSERT_GE(r, 0);
ASSERT_GE(open_req1.result, 0);
uv_fs_req_cleanup(&open_req1);
iov = uv_buf_init(buf, sizeof(buf));
r = uv_fs_read(NULL, &read_req, open_req1.result, &iov, 1, -1, NULL);
printf("read = %d\n", r);
ASSERT_EQ(26, r);
ASSERT_EQ(26, read_req.result);
ASSERT_OK(memcmp(buf,
"test-buffer\n\0test-buffer\n\0",
sizeof("test-buffer\n\0test-buffer\n\0") - 1));
uv_fs_req_cleanup(&read_req);
r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
ASSERT_OK(r);
ASSERT_OK(close_req.result);
uv_fs_req_cleanup(&close_req);
/* Cleanup */
unlink("test_file");
}
TEST_IMPL(fs_file_open_append) {
fs_file_open_append(0);
fs_file_open_append(UV_FS_O_FILEMAP);
MAKE_VALGRIND_HAPPY(uv_default_loop());
return 0;
}
TEST_IMPL(fs_rename_to_existing_file) {
int r;
/* Setup. */
unlink("test_file");
unlink("test_file2");
loop = uv_default_loop();
r = uv_fs_open(NULL, &open_req1, "test_file", UV_FS_O_WRONLY | UV_FS_O_CREAT,
S_IWUSR | S_IRUSR, NULL);
ASSERT_GE(r, 0);
ASSERT_GE(open_req1.result, 0);
uv_fs_req_cleanup(&open_req1);
iov = uv_buf_init(test_buf, sizeof(test_buf));
r = uv_fs_write(NULL, &write_req, open_req1.result, &iov, 1, -1, NULL);
ASSERT_GE(r, 0);
ASSERT_GE(write_req.result, 0);
uv_fs_req_cleanup(&write_req);
r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
ASSERT_OK(r);
ASSERT_OK(close_req.result);
uv_fs_req_cleanup(&close_req);
r = uv_fs_open(NULL, &open_req1, "test_file2", UV_FS_O_WRONLY | UV_FS_O_CREAT,
S_IWUSR | S_IRUSR, NULL);
ASSERT_GE(r, 0);
ASSERT_GE(open_req1.result, 0);
uv_fs_req_cleanup(&open_req1);
r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
ASSERT_OK(r);
ASSERT_OK(close_req.result);
uv_fs_req_cleanup(&close_req);
r = uv_fs_rename(NULL, &rename_req, "test_file", "test_file2", NULL);
ASSERT_OK(r);
ASSERT_OK(rename_req.result);
uv_fs_req_cleanup(&rename_req);
r = uv_fs_open(NULL, &open_req1, "test_file2", UV_FS_O_RDONLY, 0, NULL);
ASSERT_GE(r, 0);
ASSERT_GE(open_req1.result, 0);
uv_fs_req_cleanup(&open_req1);
memset(buf, 0, sizeof(buf));
iov = uv_buf_init(buf, sizeof(buf));
r = uv_fs_read(NULL, &read_req, open_req1.result, &iov, 1, -1, NULL);
ASSERT_GE(r, 0);
ASSERT_GE(read_req.result, 0);
ASSERT_OK(strcmp(buf, test_buf));
uv_fs_req_cleanup(&read_req);
r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
ASSERT_OK(r);
ASSERT_OK(close_req.result);
uv_fs_req_cleanup(&close_req);
/* Cleanup */
unlink("test_file");
unlink("test_file2");
MAKE_VALGRIND_HAPPY(loop);
return 0;
}
static void fs_read_bufs(int add_flags) {
char scratch[768];
uv_buf_t bufs[4];
ASSERT_LE(0, uv_fs_open(NULL, &open_req1,
"test/fixtures/lorem_ipsum.txt",
UV_FS_O_RDONLY | add_flags, 0, NULL));
ASSERT_GE(open_req1.result, 0);
uv_fs_req_cleanup(&open_req1);
ASSERT_EQ(UV_EINVAL, uv_fs_read(NULL, &read_req, open_req1.result,
NULL, 0, 0, NULL));
ASSERT_EQ(UV_EINVAL, uv_fs_read(NULL, &read_req, open_req1.result,
NULL, 1, 0, NULL));
ASSERT_EQ(UV_EINVAL, uv_fs_read(NULL, &read_req, open_req1.result,
bufs, 0, 0, NULL));
bufs[0] = uv_buf_init(scratch + 0, 256);
bufs[1] = uv_buf_init(scratch + 256, 256);
bufs[2] = uv_buf_init(scratch + 512, 128);
bufs[3] = uv_buf_init(scratch + 640, 128);
ASSERT_EQ(446, uv_fs_read(NULL,
&read_req,
open_req1.result,
bufs + 0,
2, /* 2x 256 bytes. */
0, /* Positional read. */
NULL));
ASSERT_EQ(446, read_req.result);
uv_fs_req_cleanup(&read_req);
ASSERT_EQ(190, uv_fs_read(NULL,
&read_req,
open_req1.result,
bufs + 2,
2, /* 2x 128 bytes. */
256, /* Positional read. */
NULL));
ASSERT_EQ(read_req.result, /* 446 - 256 */ 190);
uv_fs_req_cleanup(&read_req);
ASSERT_OK(memcmp(bufs[1].base + 0, bufs[2].base, 128));
ASSERT_OK(memcmp(bufs[1].base + 128, bufs[3].base, 190 - 128));
ASSERT_OK(uv_fs_close(NULL, &close_req, open_req1.result, NULL));
ASSERT_OK(close_req.result);
uv_fs_req_cleanup(&close_req);
}
TEST_IMPL(fs_read_bufs) {
fs_read_bufs(0);
fs_read_bufs(UV_FS_O_FILEMAP);
MAKE_VALGRIND_HAPPY(uv_default_loop());
return 0;
}
static void fs_read_file_eof(int add_flags) {
#if defined(__CYGWIN__) || defined(__MSYS__)
RETURN_SKIP("Cygwin pread at EOF may (incorrectly) return data!");
#endif
int r;
/* Setup. */
unlink("test_file");
loop = uv_default_loop();
r = uv_fs_open(NULL, &open_req1, "test_file",
UV_FS_O_WRONLY | UV_FS_O_CREAT | add_flags, S_IWUSR | S_IRUSR,
NULL);
ASSERT_GE(r, 0);
ASSERT_GE(open_req1.result, 0);
uv_fs_req_cleanup(&open_req1);
iov = uv_buf_init(test_buf, sizeof(test_buf));
r = uv_fs_write(NULL, &write_req, open_req1.result, &iov, 1, -1, NULL);
ASSERT_GE(r, 0);
ASSERT_GE(write_req.result, 0);
uv_fs_req_cleanup(&write_req);
r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
ASSERT_OK(r);
ASSERT_OK(close_req.result);
uv_fs_req_cleanup(&close_req);
r = uv_fs_open(NULL, &open_req1, "test_file", UV_FS_O_RDONLY | add_flags, 0,
NULL);
ASSERT_GE(r, 0);
ASSERT_GE(open_req1.result, 0);
uv_fs_req_cleanup(&open_req1);
memset(buf, 0, sizeof(buf));
iov = uv_buf_init(buf, sizeof(buf));
r = uv_fs_read(NULL, &read_req, open_req1.result, &iov, 1, -1, NULL);
ASSERT_GE(r, 0);
ASSERT_GE(read_req.result, 0);
ASSERT_OK(strcmp(buf, test_buf));
uv_fs_req_cleanup(&read_req);
iov = uv_buf_init(buf, sizeof(buf));
r = uv_fs_read(NULL, &read_req, open_req1.result, &iov, 1,
read_req.result, NULL);
ASSERT_OK(r);
ASSERT_OK(read_req.result);
uv_fs_req_cleanup(&read_req);
r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
ASSERT_OK(r);
ASSERT_OK(close_req.result);
uv_fs_req_cleanup(&close_req);
/* Cleanup */
unlink("test_file");
}
TEST_IMPL(fs_read_file_eof) {
fs_read_file_eof(0);
fs_read_file_eof(UV_FS_O_FILEMAP);
MAKE_VALGRIND_HAPPY(uv_default_loop());
return 0;
}
static void fs_write_multiple_bufs(int add_flags) {
uv_buf_t iovs[2];
int r;
/* Setup. */
unlink("test_file");
loop = uv_default_loop();
r = uv_fs_open(NULL, &open_req1, "test_file",
UV_FS_O_WRONLY | UV_FS_O_CREAT | add_flags, S_IWUSR | S_IRUSR,
NULL);
ASSERT_GE(r, 0);
ASSERT_GE(open_req1.result, 0);
uv_fs_req_cleanup(&open_req1);
iovs[0] = uv_buf_init(test_buf, sizeof(test_buf));
iovs[1] = uv_buf_init(test_buf2, sizeof(test_buf2));
r = uv_fs_write(NULL, &write_req, open_req1.result, iovs, 2, 0, NULL);
ASSERT_GE(r, 0);
ASSERT_GE(write_req.result, 0);
uv_fs_req_cleanup(&write_req);
r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
ASSERT_OK(r);
ASSERT_OK(close_req.result);
uv_fs_req_cleanup(&close_req);
r = uv_fs_open(NULL, &open_req1, "test_file", UV_FS_O_RDONLY | add_flags, 0,
NULL);
ASSERT_GE(r, 0);
ASSERT_GE(open_req1.result, 0);
uv_fs_req_cleanup(&open_req1);
memset(buf, 0, sizeof(buf));
memset(buf2, 0, sizeof(buf2));
/* Read the strings back to separate buffers. */
iovs[0] = uv_buf_init(buf, sizeof(test_buf));
iovs[1] = uv_buf_init(buf2, sizeof(test_buf2));
ASSERT_OK(lseek(open_req1.result, 0, SEEK_CUR));
r = uv_fs_read(NULL, &read_req, open_req1.result, iovs, 2, -1, NULL);
ASSERT_GE(r, 0);
ASSERT_EQ(read_req.result, sizeof(test_buf) + sizeof(test_buf2));
ASSERT_OK(strcmp(buf, test_buf));
ASSERT_OK(strcmp(buf2, test_buf2));
uv_fs_req_cleanup(&read_req);
iov = uv_buf_init(buf, sizeof(buf));
r = uv_fs_read(NULL, &read_req, open_req1.result, &iov, 1, -1, NULL);
ASSERT_OK(r);
ASSERT_OK(read_req.result);
uv_fs_req_cleanup(&read_req);
/* Read the strings back to separate buffers. */
iovs[0] = uv_buf_init(buf, sizeof(test_buf));
iovs[1] = uv_buf_init(buf2, sizeof(test_buf2));
r = uv_fs_read(NULL, &read_req, open_req1.result, iovs, 2, 0, NULL);
ASSERT_GE(r, 0);
if (read_req.result == sizeof(test_buf)) {
/* Infer that preadv is not available. */
uv_fs_req_cleanup(&read_req);
r = uv_fs_read(NULL, &read_req, open_req1.result, &iovs[1], 1, read_req.result, NULL);
ASSERT_GE(r, 0);
ASSERT_EQ(read_req.result, sizeof(test_buf2));
} else {
ASSERT_EQ(read_req.result, sizeof(test_buf) + sizeof(test_buf2));
}
ASSERT_OK(strcmp(buf, test_buf));
ASSERT_OK(strcmp(buf2, test_buf2));
uv_fs_req_cleanup(&read_req);
iov = uv_buf_init(buf, sizeof(buf));
r = uv_fs_read(NULL, &read_req, open_req1.result, &iov, 1,
sizeof(test_buf) + sizeof(test_buf2), NULL);
ASSERT_OK(r);
ASSERT_OK(read_req.result);
uv_fs_req_cleanup(&read_req);
r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
ASSERT_OK(r);
ASSERT_OK(close_req.result);
uv_fs_req_cleanup(&close_req);
/* Cleanup */
unlink("test_file");
}
TEST_IMPL(fs_write_multiple_bufs) {
fs_write_multiple_bufs(0);
fs_write_multiple_bufs(UV_FS_O_FILEMAP);
MAKE_VALGRIND_HAPPY(uv_default_loop());
return 0;
}
static void fs_write_alotof_bufs(int add_flags) {
size_t iovcount;
size_t iovmax;
uv_buf_t* iovs;
char* buffer;
size_t index;
int r;
iovcount = 54321;
/* Setup. */
unlink("test_file");
loop = uv_default_loop();
iovs = malloc(sizeof(*iovs) * iovcount);
ASSERT_NOT_NULL(iovs);
iovmax = uv_test_getiovmax();
r = uv_fs_open(NULL,
&open_req1,
"test_file",
UV_FS_O_RDWR | UV_FS_O_CREAT | add_flags,
S_IWUSR | S_IRUSR,
NULL);
ASSERT_GE(r, 0);
ASSERT_GE(open_req1.result, 0);
uv_fs_req_cleanup(&open_req1);
for (index = 0; index < iovcount; ++index)
iovs[index] = uv_buf_init(test_buf, sizeof(test_buf));
r = uv_fs_write(NULL,
&write_req,
open_req1.result,
iovs,
iovcount,
-1,
NULL);
ASSERT_GE(r, 0);
ASSERT_EQ((size_t)write_req.result, sizeof(test_buf) * iovcount);
uv_fs_req_cleanup(&write_req);
/* Read the strings back to separate buffers. */
buffer = malloc(sizeof(test_buf) * iovcount);
ASSERT_NOT_NULL(buffer);
for (index = 0; index < iovcount; ++index)
iovs[index] = uv_buf_init(buffer + index * sizeof(test_buf),
sizeof(test_buf));
r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
ASSERT_OK(r);
ASSERT_OK(close_req.result);
uv_fs_req_cleanup(&close_req);
r = uv_fs_open(NULL, &open_req1, "test_file", UV_FS_O_RDONLY | add_flags, 0,
NULL);
ASSERT_GE(r, 0);
ASSERT_GE(open_req1.result, 0);
uv_fs_req_cleanup(&open_req1);
r = uv_fs_read(NULL, &read_req, open_req1.result, iovs, iovcount, -1, NULL);
if (iovcount > iovmax)
iovcount = iovmax;
ASSERT_GE(r, 0);
ASSERT_EQ((size_t)read_req.result, sizeof(test_buf) * iovcount);
for (index = 0; index < iovcount; ++index)
ASSERT_OK(strncmp(buffer + index * sizeof(test_buf),
test_buf,
sizeof(test_buf)));
uv_fs_req_cleanup(&read_req);
free(buffer);
ASSERT_EQ(lseek(open_req1.result, write_req.result, SEEK_SET),
write_req.result);
iov = uv_buf_init(buf, sizeof(buf));
r = uv_fs_read(NULL,
&read_req,
open_req1.result,
&iov,
1,
-1,
NULL);
ASSERT_OK(r);
ASSERT_OK(read_req.result);
uv_fs_req_cleanup(&read_req);
r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
ASSERT_OK(r);
ASSERT_OK(close_req.result);
uv_fs_req_cleanup(&close_req);
/* Cleanup */
unlink("test_file");
free(iovs);
}
TEST_IMPL(fs_write_alotof_bufs) {
fs_write_alotof_bufs(0);
fs_write_alotof_bufs(UV_FS_O_FILEMAP);
MAKE_VALGRIND_HAPPY(uv_default_loop());
return 0;
}
static void fs_write_alotof_bufs_with_offset(int add_flags) {
size_t iovcount;
size_t iovmax;
uv_buf_t* iovs;
char* buffer;
size_t index;
int r;
int64_t offset;
char* filler;
int filler_len;
filler = "0123456789";
filler_len = strlen(filler);
iovcount = 54321;
/* Setup. */
unlink("test_file");
loop = uv_default_loop();
iovs = malloc(sizeof(*iovs) * iovcount);
ASSERT_NOT_NULL(iovs);
iovmax = uv_test_getiovmax();
r = uv_fs_open(NULL,
&open_req1,
"test_file",
UV_FS_O_RDWR | UV_FS_O_CREAT | add_flags,
S_IWUSR | S_IRUSR,
NULL);
ASSERT_GE(r, 0);
ASSERT_GE(open_req1.result, 0);
uv_fs_req_cleanup(&open_req1);
iov = uv_buf_init(filler, filler_len);
r = uv_fs_write(NULL, &write_req, open_req1.result, &iov, 1, -1, NULL);
ASSERT_EQ(r, filler_len);
ASSERT_EQ(write_req.result, filler_len);
uv_fs_req_cleanup(&write_req);
offset = (int64_t)r;
for (index = 0; index < iovcount; ++index)
iovs[index] = uv_buf_init(test_buf, sizeof(test_buf));
r = uv_fs_write(NULL,
&write_req,
open_req1.result,
iovs,
iovcount,
offset,
NULL);
ASSERT_GE(r, 0);
ASSERT_EQ((size_t)write_req.result, sizeof(test_buf) * iovcount);
uv_fs_req_cleanup(&write_req);
/* Read the strings back to separate buffers. */
buffer = malloc(sizeof(test_buf) * iovcount);
ASSERT_NOT_NULL(buffer);
for (index = 0; index < iovcount; ++index)
iovs[index] = uv_buf_init(buffer + index * sizeof(test_buf),
sizeof(test_buf));
r = uv_fs_read(NULL, &read_req, open_req1.result,
iovs, iovcount, offset, NULL);
ASSERT_GE(r, 0);
if (r == sizeof(test_buf))
iovcount = 1; /* Infer that preadv is not available. */
else if (iovcount > iovmax)
iovcount = iovmax;
ASSERT_EQ((size_t)read_req.result, sizeof(test_buf) * iovcount);
for (index = 0; index < iovcount; ++index)
ASSERT_OK(strncmp(buffer + index * sizeof(test_buf),
test_buf,
sizeof(test_buf)));
uv_fs_req_cleanup(&read_req);
free(buffer);
r = uv_fs_stat(NULL, &stat_req, "test_file", NULL);
ASSERT_OK(r);
ASSERT_EQ((int64_t)((uv_stat_t*)stat_req.ptr)->st_size,
offset + (int64_t)write_req.result);
uv_fs_req_cleanup(&stat_req);
iov = uv_buf_init(buf, sizeof(buf));
r = uv_fs_read(NULL,
&read_req,
open_req1.result,
&iov,
1,
offset + write_req.result,
NULL);
ASSERT_OK(r);
ASSERT_OK(read_req.result);
uv_fs_req_cleanup(&read_req);
r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
ASSERT_OK(r);
ASSERT_OK(close_req.result);
uv_fs_req_cleanup(&close_req);
/* Cleanup */
unlink("test_file");
free(iovs);
}
TEST_IMPL(fs_write_alotof_bufs_with_offset) {
fs_write_alotof_bufs_with_offset(0);
fs_write_alotof_bufs_with_offset(UV_FS_O_FILEMAP);
MAKE_VALGRIND_HAPPY(uv_default_loop());
return 0;
}
TEST_IMPL(fs_read_dir) {
int r;
char buf[2];
loop = uv_default_loop();
/* Setup */
rmdir("test_dir");
r = uv_fs_mkdir(loop, &mkdir_req, "test_dir", 0755, mkdir_cb);
ASSERT_OK(r);
uv_run(loop, UV_RUN_DEFAULT);
ASSERT_EQ(1, mkdir_cb_count);
/* Setup Done Here */
/* Get a file descriptor for the directory */
r = uv_fs_open(loop,
&open_req1,
"test_dir",
UV_FS_O_RDONLY | UV_FS_O_DIRECTORY,
S_IWUSR | S_IRUSR,
NULL);
ASSERT_GE(r, 0);
uv_fs_req_cleanup(&open_req1);
/* Try to read data from the directory */
iov = uv_buf_init(buf, sizeof(buf));
r = uv_fs_read(NULL, &read_req, open_req1.result, &iov, 1, 0, NULL);
#if defined(__FreeBSD__) || \
defined(__OpenBSD__) || \
defined(__NetBSD__) || \
defined(__DragonFly__) || \
defined(_AIX) || \
defined(__sun) || \
defined(__MVS__)
/*
* As of now, these operating systems support reading from a directory,
* that too depends on the filesystem this temporary test directory is
* created on. That is why this assertion is a bit lenient.
*/
ASSERT((r >= 0) || (r == UV_EISDIR));
#else
ASSERT_EQ(r, UV_EISDIR);
#endif
uv_fs_req_cleanup(&read_req);
r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
ASSERT_OK(r);
uv_fs_req_cleanup(&close_req);
/* Cleanup */
rmdir("test_dir");
MAKE_VALGRIND_HAPPY(loop);
return 0;
}
#ifdef _WIN32
TEST_IMPL(fs_partial_read) {
RETURN_SKIP("Test not implemented on Windows.");
}
TEST_IMPL(fs_partial_write) {
RETURN_SKIP("Test not implemented on Windows.");
}
#else /* !_WIN32 */
struct thread_ctx {
pthread_t pid;
int fd;
char* data;
int size;
int interval;
int doread;
};
static void thread_main(void* arg) {
const struct thread_ctx* ctx;
int size;
char* data;
ctx = (struct thread_ctx*)arg;
size = ctx->size;
data = ctx->data;
while (size > 0) {
ssize_t result;
int nbytes;
nbytes = size < ctx->interval ? size : ctx->interval;
if (ctx->doread) {
result = write(ctx->fd, data, nbytes);
/* Should not see EINTR (or other errors) */
ASSERT_EQ(result, nbytes);
} else {
result = read(ctx->fd, data, nbytes);
/* Should not see EINTR (or other errors),
* but might get a partial read if we are faster than the writer
*/
ASSERT(result > 0 && result <= nbytes);
}
pthread_kill(ctx->pid, SIGUSR1);
size -= result;
data += result;
}
}
static void sig_func(uv_signal_t* handle, int signum) {
uv_signal_stop(handle);
}
static size_t uv_test_fs_buf_offset(uv_buf_t* bufs, size_t size) {
size_t offset;
/* Figure out which bufs are done */
for (offset = 0; size > 0 && bufs[offset].len <= size; ++offset)
size -= bufs[offset].len;
/* Fix a partial read/write */
if (size > 0) {
bufs[offset].base += size;
bufs[offset].len -= size;
}
return offset;
}
static void test_fs_partial(int doread) {
struct thread_ctx ctx;
uv_thread_t thread;
uv_signal_t signal;
int pipe_fds[2];
size_t iovcount;
uv_buf_t* iovs;
char* buffer;
size_t index;
iovcount = 54321;
iovs = malloc(sizeof(*iovs) * iovcount);
ASSERT_NOT_NULL(iovs);
ctx.pid = pthread_self();
ctx.doread = doread;
ctx.interval = 1000;
ctx.size = sizeof(test_buf) * iovcount;
ctx.data = calloc(ctx.size, 1);
ASSERT_NOT_NULL(ctx.data);
buffer = calloc(ctx.size, 1);
ASSERT_NOT_NULL(buffer);
for (index = 0; index < iovcount; ++index)
iovs[index] = uv_buf_init(buffer + index * sizeof(test_buf), sizeof(test_buf));
loop = uv_default_loop();
ASSERT_OK(uv_signal_init(loop, &signal));
ASSERT_OK(uv_signal_start(&signal, sig_func, SIGUSR1));
ASSERT_OK(pipe(pipe_fds));
ctx.fd = pipe_fds[doread];
ASSERT_OK(uv_thread_create(&thread, thread_main, &ctx));
if (doread) {
uv_buf_t* read_iovs;
int nread;
read_iovs = iovs;
nread = 0;
while (nread < ctx.size) {
int result;
result = uv_fs_read(loop, &read_req, pipe_fds[0], read_iovs, iovcount, -1, NULL);
if (result > 0) {
size_t read_iovcount;
read_iovcount = uv_test_fs_buf_offset(read_iovs, result);
read_iovs += read_iovcount;
iovcount -= read_iovcount;
nread += result;
} else {
ASSERT_EQ(result, UV_EINTR);
}
uv_fs_req_cleanup(&read_req);
}
} else {
int result;
result = uv_fs_write(loop, &write_req, pipe_fds[1], iovs, iovcount, -1, NULL);
ASSERT_EQ(write_req.result, result);
ASSERT_EQ(result, ctx.size);
uv_fs_req_cleanup(&write_req);
}
ASSERT_OK(uv_thread_join(&thread));
ASSERT_MEM_EQ(buffer, ctx.data, ctx.size);
ASSERT_OK(uv_run(loop, UV_RUN_DEFAULT));
ASSERT_OK(close(pipe_fds[1]));
uv_close((uv_handle_t*) &signal, NULL);
{ /* Make sure we read everything that we wrote. */
int result;
result = uv_fs_read(loop, &read_req, pipe_fds[0], iovs, 1, -1, NULL);
ASSERT_OK(result);
uv_fs_req_cleanup(&read_req);
}
ASSERT_OK(close(pipe_fds[0]));
free(iovs);
free(buffer);
free(ctx.data);
MAKE_VALGRIND_HAPPY(loop);
}
TEST_IMPL(fs_partial_read) {
test_fs_partial(1);
return 0;
}
TEST_IMPL(fs_partial_write) {
test_fs_partial(0);
return 0;
}
#endif/* _WIN32 */
TEST_IMPL(fs_read_write_null_arguments) {
int r;
r = uv_fs_read(NULL, &read_req, 0, NULL, 0, -1, NULL);
ASSERT_EQ(r, UV_EINVAL);
uv_fs_req_cleanup(&read_req);
r = uv_fs_write(NULL, &write_req, 0, NULL, 0, -1, NULL);
/* Validate some memory management on failed input validation before sending
fs work to the thread pool. */
ASSERT_EQ(r, UV_EINVAL);
ASSERT_NULL(write_req.path);
ASSERT_NULL(write_req.ptr);
#ifdef _WIN32
ASSERT_NULL(write_req.file.pathw);
ASSERT_NULL(write_req.fs.info.new_pathw);
ASSERT_NULL(write_req.fs.info.bufs);
#else
ASSERT_NULL(write_req.new_path);
ASSERT_NULL(write_req.bufs);
#endif
uv_fs_req_cleanup(&write_req);
iov = uv_buf_init(NULL, 0);
r = uv_fs_read(NULL, &read_req, 0, &iov, 0, -1, NULL);
ASSERT_EQ(r, UV_EINVAL);
uv_fs_req_cleanup(&read_req);
iov = uv_buf_init(NULL, 0);
r = uv_fs_write(NULL, &write_req, 0, &iov, 0, -1, NULL);
ASSERT_EQ(r, UV_EINVAL);
uv_fs_req_cleanup(&write_req);
/* If the arguments are invalid, the loop should not be kept open */
loop = uv_default_loop();
r = uv_fs_read(loop, &read_req, 0, NULL, 0, -1, fail_cb);
ASSERT_EQ(r, UV_EINVAL);
uv_run(loop, UV_RUN_DEFAULT);
uv_fs_req_cleanup(&read_req);
r = uv_fs_write(loop, &write_req, 0, NULL, 0, -1, fail_cb);
ASSERT_EQ(r, UV_EINVAL);
uv_run(loop, UV_RUN_DEFAULT);
uv_fs_req_cleanup(&write_req);
iov = uv_buf_init(NULL, 0);
r = uv_fs_read(loop, &read_req, 0, &iov, 0, -1, fail_cb);
ASSERT_EQ(r, UV_EINVAL);
uv_run(loop, UV_RUN_DEFAULT);
uv_fs_req_cleanup(&read_req);
iov = uv_buf_init(NULL, 0);
r = uv_fs_write(loop, &write_req, 0, &iov, 0, -1, fail_cb);
ASSERT_EQ(r, UV_EINVAL);
uv_run(loop, UV_RUN_DEFAULT);
uv_fs_req_cleanup(&write_req);
MAKE_VALGRIND_HAPPY(loop);
return 0;
}
TEST_IMPL(get_osfhandle_valid_handle) {
int r;
uv_os_fd_t fd;
/* Setup. */
unlink("test_file");
loop = uv_default_loop();
r = uv_fs_open(NULL,
&open_req1, "test_file", UV_FS_O_RDWR | UV_FS_O_CREAT,
S_IWUSR | S_IRUSR,
NULL);
ASSERT_GE(r, 0);
ASSERT_GE(open_req1.result, 0);
uv_fs_req_cleanup(&open_req1);
fd = uv_get_osfhandle(open_req1.result);
#ifdef _WIN32
ASSERT_PTR_NE(fd, INVALID_HANDLE_VALUE);
#else
ASSERT_GE(fd, 0);
#endif
r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
ASSERT_OK(r);
ASSERT_OK(close_req.result);
uv_fs_req_cleanup(&close_req);
/* Cleanup. */
unlink("test_file");
MAKE_VALGRIND_HAPPY(loop);
return 0;
}
TEST_IMPL(open_osfhandle_valid_handle) {
int r;
uv_os_fd_t handle;
int fd;
/* Setup. */
unlink("test_file");
loop = uv_default_loop();
r = uv_fs_open(NULL,
&open_req1,
"test_file",
UV_FS_O_RDWR | UV_FS_O_CREAT,
S_IWUSR | S_IRUSR,
NULL);
ASSERT_GE(r, 0);
ASSERT_GE(open_req1.result, 0);
uv_fs_req_cleanup(&open_req1);
handle = uv_get_osfhandle(open_req1.result);
#ifdef _WIN32
ASSERT_PTR_NE(handle, INVALID_HANDLE_VALUE);
#else
ASSERT_GE(handle, 0);
#endif
fd = uv_open_osfhandle(handle);
#ifdef _WIN32
ASSERT_GT(fd, 0);
#else
ASSERT_EQ(fd, open_req1.result);
#endif
r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
ASSERT_OK(r);
ASSERT_OK(close_req.result);
uv_fs_req_cleanup(&close_req);
/* Cleanup. */
unlink("test_file");
MAKE_VALGRIND_HAPPY(loop);
return 0;
}
TEST_IMPL(fs_file_pos_after_op_with_offset) {
int r;
/* Setup. */
unlink("test_file");
loop = uv_default_loop();
r = uv_fs_open(loop,
&open_req1, "test_file", UV_FS_O_RDWR | UV_FS_O_CREAT,
S_IWUSR | S_IRUSR,
NULL);
ASSERT_GT(r, 0);
uv_fs_req_cleanup(&open_req1);
iov = uv_buf_init(test_buf, sizeof(test_buf));
r = uv_fs_write(NULL, &write_req, open_req1.result, &iov, 1, 0, NULL);
ASSERT_EQ(r, sizeof(test_buf));
ASSERT_OK(lseek(open_req1.result, 0, SEEK_CUR));
uv_fs_req_cleanup(&write_req);
iov = uv_buf_init(buf, sizeof(buf));
r = uv_fs_read(NULL, &read_req, open_req1.result, &iov, 1, 0, NULL);
ASSERT_EQ(r, sizeof(test_buf));
ASSERT_OK(strcmp(buf, test_buf));
ASSERT_OK(lseek(open_req1.result, 0, SEEK_CUR));
uv_fs_req_cleanup(&read_req);
r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
ASSERT_OK(r);
uv_fs_req_cleanup(&close_req);
/* Cleanup */
unlink("test_file");
MAKE_VALGRIND_HAPPY(loop);
return 0;
}
#ifdef _WIN32
static void fs_file_pos_common(void) {
int r;
iov = uv_buf_init("abc", 3);
r = uv_fs_write(NULL, &write_req, open_req1.result, &iov, 1, -1, NULL);
ASSERT_EQ(3, r);
uv_fs_req_cleanup(&write_req);
/* Read with offset should not change the position */
iov = uv_buf_init(buf, 1);
r = uv_fs_read(NULL, &read_req, open_req1.result, &iov, 1, 1, NULL);
ASSERT_EQ(1, r);
ASSERT_EQ(buf[0], 'b');
uv_fs_req_cleanup(&read_req);
iov = uv_buf_init(buf, sizeof(buf));
r = uv_fs_read(NULL, &read_req, open_req1.result, &iov, 1, -1, NULL);
ASSERT_OK(r);
uv_fs_req_cleanup(&read_req);
/* Write without offset should change the position */
iov = uv_buf_init("d", 1);
r = uv_fs_write(NULL, &write_req, open_req1.result, &iov, 1, -1, NULL);
ASSERT_EQ(1, r);
uv_fs_req_cleanup(&write_req);
iov = uv_buf_init(buf, sizeof(buf));
r = uv_fs_read(NULL, &read_req, open_req1.result, &iov, 1, -1, NULL);
ASSERT_OK(r);
uv_fs_req_cleanup(&read_req);
}
static void fs_file_pos_close_check(const char *contents, int size) {
int r;
/* Close */
r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
ASSERT_OK(r);
uv_fs_req_cleanup(&close_req);
/* Confirm file contents */
r = uv_fs_open(NULL, &open_req1, "test_file", UV_FS_O_RDONLY, 0, NULL);
ASSERT_GE(r, 0);
ASSERT_GE(open_req1.result, 0);
uv_fs_req_cleanup(&open_req1);
iov = uv_buf_init(buf, sizeof(buf));
r = uv_fs_read(NULL, &read_req, open_req1.result, &iov, 1, -1, NULL);
ASSERT_EQ(r, size);
ASSERT_OK(strncmp(buf, contents, size));
uv_fs_req_cleanup(&read_req);
r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
ASSERT_OK(r);
uv_fs_req_cleanup(&close_req);
/* Cleanup */
unlink("test_file");
}
static void fs_file_pos_write(int add_flags) {
int r;
/* Setup. */
unlink("test_file");
r = uv_fs_open(NULL,
&open_req1,
"test_file",
UV_FS_O_TRUNC | UV_FS_O_CREAT | UV_FS_O_RDWR | add_flags,
S_IWUSR | S_IRUSR,
NULL);
ASSERT_GT(r, 0);
uv_fs_req_cleanup(&open_req1);
fs_file_pos_common();
/* Write with offset should not change the position */
iov = uv_buf_init("e", 1);
r = uv_fs_write(NULL, &write_req, open_req1.result, &iov, 1, 1, NULL);
ASSERT_EQ(1, r);
uv_fs_req_cleanup(&write_req);
iov = uv_buf_init(buf, sizeof(buf));
r = uv_fs_read(NULL, &read_req, open_req1.result, &iov, 1, -1, NULL);
ASSERT_OK(r);
uv_fs_req_cleanup(&read_req);
fs_file_pos_close_check("aecd", 4);
}
TEST_IMPL(fs_file_pos_write) {
fs_file_pos_write(0);
fs_file_pos_write(UV_FS_O_FILEMAP);
MAKE_VALGRIND_HAPPY(uv_default_loop());
return 0;
}
static void fs_file_pos_append(int add_flags) {
int r;
/* Setup. */
unlink("test_file");
r = uv_fs_open(NULL,
&open_req1,
"test_file",
UV_FS_O_APPEND | UV_FS_O_CREAT | UV_FS_O_RDWR | add_flags,
S_IWUSR | S_IRUSR,
NULL);
ASSERT_GT(r, 0);
uv_fs_req_cleanup(&open_req1);
fs_file_pos_common();
/* Write with offset appends (ignoring offset)
* but does not change the position */
iov = uv_buf_init("e", 1);
r = uv_fs_write(NULL, &write_req, open_req1.result, &iov, 1, 1, NULL);
ASSERT_EQ(1, r);
uv_fs_req_cleanup(&write_req);
iov = uv_buf_init(buf, sizeof(buf));
r = uv_fs_read(NULL, &read_req, open_req1.result, &iov, 1, -1, NULL);
ASSERT_EQ(1, r);
ASSERT_EQ(buf[0], 'e');
uv_fs_req_cleanup(&read_req);
fs_file_pos_close_check("abcde", 5);
}
TEST_IMPL(fs_file_pos_append) {
fs_file_pos_append(0);
fs_file_pos_append(UV_FS_O_FILEMAP);
MAKE_VALGRIND_HAPPY(uv_default_loop());
return 0;
}
#endif
TEST_IMPL(fs_null_req) {
/* Verify that all fs functions return UV_EINVAL when the request is NULL. */
int r;
r = uv_fs_open(NULL, NULL, NULL, 0, 0, NULL);
ASSERT_EQ(r, UV_EINVAL);
r = uv_fs_close(NULL, NULL, 0, NULL);
ASSERT_EQ(r, UV_EINVAL);
r = uv_fs_read(NULL, NULL, 0, NULL, 0, -1, NULL);
ASSERT_EQ(r, UV_EINVAL);
r = uv_fs_write(NULL, NULL, 0, NULL, 0, -1, NULL);
ASSERT_EQ(r, UV_EINVAL);
r = uv_fs_unlink(NULL, NULL, NULL, NULL);
ASSERT_EQ(r, UV_EINVAL);
r = uv_fs_mkdir(NULL, NULL, NULL, 0, NULL);
ASSERT_EQ(r, UV_EINVAL);
r = uv_fs_mkdtemp(NULL, NULL, NULL, NULL);
ASSERT_EQ(r, UV_EINVAL);
r = uv_fs_mkstemp(NULL, NULL, NULL, NULL);
ASSERT_EQ(r, UV_EINVAL);
r = uv_fs_rmdir(NULL, NULL, NULL, NULL);
ASSERT_EQ(r, UV_EINVAL);
r = uv_fs_scandir(NULL, NULL, NULL, 0, NULL);
ASSERT_EQ(r, UV_EINVAL);
r = uv_fs_link(NULL, NULL, NULL, NULL, NULL);
ASSERT_EQ(r, UV_EINVAL);
r = uv_fs_symlink(NULL, NULL, NULL, NULL, 0, NULL);
ASSERT_EQ(r, UV_EINVAL);
r = uv_fs_readlink(NULL, NULL, NULL, NULL);
ASSERT_EQ(r, UV_EINVAL);
r = uv_fs_realpath(NULL, NULL, NULL, NULL);
ASSERT_EQ(r, UV_EINVAL);
r = uv_fs_chown(NULL, NULL, NULL, 0, 0, NULL);
ASSERT_EQ(r, UV_EINVAL);
r = uv_fs_fchown(NULL, NULL, 0, 0, 0, NULL);
ASSERT_EQ(r, UV_EINVAL);
r = uv_fs_stat(NULL, NULL, NULL, NULL);
ASSERT_EQ(r, UV_EINVAL);
r = uv_fs_lstat(NULL, NULL, NULL, NULL);
ASSERT_EQ(r, UV_EINVAL);
r = uv_fs_fstat(NULL, NULL, 0, NULL);
ASSERT_EQ(r, UV_EINVAL);
r = uv_fs_rename(NULL, NULL, NULL, NULL, NULL);
ASSERT_EQ(r, UV_EINVAL);
r = uv_fs_fsync(NULL, NULL, 0, NULL);
ASSERT_EQ(r, UV_EINVAL);
r = uv_fs_fdatasync(NULL, NULL, 0, NULL);
ASSERT_EQ(r, UV_EINVAL);
r = uv_fs_ftruncate(NULL, NULL, 0, 0, NULL);
ASSERT_EQ(r, UV_EINVAL);
r = uv_fs_copyfile(NULL, NULL, NULL, NULL, 0, NULL);
ASSERT_EQ(r, UV_EINVAL);
r = uv_fs_sendfile(NULL, NULL, 0, 0, 0, 0, NULL);
ASSERT_EQ(r, UV_EINVAL);
r = uv_fs_access(NULL, NULL, NULL, 0, NULL);
ASSERT_EQ(r, UV_EINVAL);
r = uv_fs_chmod(NULL, NULL, NULL, 0, NULL);
ASSERT_EQ(r, UV_EINVAL);
r = uv_fs_fchmod(NULL, NULL, 0, 0, NULL);
ASSERT_EQ(r, UV_EINVAL);
r = uv_fs_utime(NULL, NULL, NULL, 0.0, 0.0, NULL);
ASSERT_EQ(r, UV_EINVAL);
r = uv_fs_futime(NULL, NULL, 0, 0.0, 0.0, NULL);
ASSERT_EQ(r, UV_EINVAL);
r = uv_fs_statfs(NULL, NULL, NULL, NULL);
ASSERT_EQ(r, UV_EINVAL);
/* This should be a no-op. */
uv_fs_req_cleanup(NULL);
return 0;
}
#ifdef _WIN32
TEST_IMPL(fs_exclusive_sharing_mode) {
int r;
/* Setup. */
unlink("test_file");
ASSERT_GT(UV_FS_O_EXLOCK, 0);
r = uv_fs_open(NULL,
&open_req1,
"test_file",
UV_FS_O_RDWR | UV_FS_O_CREAT | UV_FS_O_EXLOCK,
S_IWUSR | S_IRUSR,
NULL);
ASSERT_GE(r, 0);
ASSERT_GE(open_req1.result, 0);
uv_fs_req_cleanup(&open_req1);
r = uv_fs_open(NULL,
&open_req2,
"test_file", UV_FS_O_RDONLY | UV_FS_O_EXLOCK,
S_IWUSR | S_IRUSR,
NULL);
ASSERT_LT(r, 0);
ASSERT_LT(open_req2.result, 0);
uv_fs_req_cleanup(&open_req2);
r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
ASSERT_OK(r);
ASSERT_OK(close_req.result);
uv_fs_req_cleanup(&close_req);
r = uv_fs_open(NULL,
&open_req2,
"test_file", UV_FS_O_RDONLY | UV_FS_O_EXLOCK,
S_IWUSR | S_IRUSR,
NULL);
ASSERT_GE(r, 0);
ASSERT_GE(open_req2.result, 0);
uv_fs_req_cleanup(&open_req2);
r = uv_fs_close(NULL, &close_req, open_req2.result, NULL);
ASSERT_OK(r);
ASSERT_OK(close_req.result);
uv_fs_req_cleanup(&close_req);
/* Cleanup */
unlink("test_file");
MAKE_VALGRIND_HAPPY(uv_default_loop());
return 0;
}
#endif
#ifdef _WIN32
TEST_IMPL(fs_file_flag_no_buffering) {
int r;
/* Setup. */
unlink("test_file");
ASSERT_GT(UV_FS_O_APPEND, 0);
ASSERT_GT(UV_FS_O_CREAT, 0);
ASSERT_GT(UV_FS_O_DIRECT, 0);
ASSERT_GT(UV_FS_O_RDWR, 0);
/* FILE_APPEND_DATA must be excluded from FILE_GENERIC_WRITE: */
r = uv_fs_open(NULL,
&open_req1,
"test_file",
UV_FS_O_RDWR | UV_FS_O_CREAT | UV_FS_O_DIRECT,
S_IWUSR | S_IRUSR,
NULL);
ASSERT_GE(r, 0);
ASSERT_GE(open_req1.result, 0);
uv_fs_req_cleanup(&open_req1);
r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
ASSERT_OK(r);
ASSERT_OK(close_req.result);
uv_fs_req_cleanup(&close_req);
/* FILE_APPEND_DATA and FILE_FLAG_NO_BUFFERING are mutually exclusive: */
r = uv_fs_open(NULL,
&open_req2,
"test_file",
UV_FS_O_APPEND | UV_FS_O_DIRECT,
S_IWUSR | S_IRUSR,
NULL);
ASSERT_EQ(r, UV_EINVAL);
ASSERT_EQ(open_req2.result, UV_EINVAL);
uv_fs_req_cleanup(&open_req2);
/* Cleanup */
unlink("test_file");
MAKE_VALGRIND_HAPPY(uv_default_loop());
return 0;
}
#endif
#ifdef _WIN32
int call_icacls(const char* command, ...) {
char icacls_command[1024];
va_list args;
va_start(args, command);
vsnprintf(icacls_command, ARRAYSIZE(icacls_command), command, args);
va_end(args);
return system(icacls_command);
}
TEST_IMPL(fs_open_readonly_acl) {
uv_passwd_t pwd;
uv_fs_t req;
int r;
/*
Based on Node.js test from
https://github.com/nodejs/node/commit/3ba81e34e86a5c32658e218cb6e65b13e8326bc5
If anything goes wrong, you can delte the test_fle_icacls with:
icacls test_file_icacls /remove "%USERNAME%" /inheritance:e
attrib -r test_file_icacls
del test_file_icacls
*/
/* Setup - clear the ACL and remove the file */
loop = uv_default_loop();
r = uv_os_get_passwd(&pwd);
ASSERT_OK(r);
call_icacls("icacls test_file_icacls /remove \"%s\" /inheritance:e",
pwd.username);
uv_fs_chmod(loop, &req, "test_file_icacls", S_IWUSR, NULL);
unlink("test_file_icacls");
/* Create the file */
r = uv_fs_open(loop,
&open_req1,
"test_file_icacls",
UV_FS_O_RDONLY | UV_FS_O_CREAT,
S_IRUSR,
NULL);
ASSERT_GE(r, 0);
ASSERT_GE(open_req1.result, 0);
uv_fs_req_cleanup(&open_req1);
r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
ASSERT_OK(r);
ASSERT_OK(close_req.result);
uv_fs_req_cleanup(&close_req);
/* Set up ACL */
r = call_icacls("icacls test_file_icacls /inheritance:r /remove \"%s\"",
pwd.username);
if (r != 0) {
goto acl_cleanup;
}
r = call_icacls("icacls test_file_icacls /grant \"%s\":RX", pwd.username);
if (r != 0) {
goto acl_cleanup;
}
/* Try opening the file */
r = uv_fs_open(NULL, &open_req1, "test_file_icacls", UV_FS_O_RDONLY, 0,
NULL);
if (r < 0) {
goto acl_cleanup;
}
uv_fs_req_cleanup(&open_req1);
r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
if (r != 0) {
goto acl_cleanup;
}
uv_fs_req_cleanup(&close_req);
acl_cleanup:
/* Cleanup */
call_icacls("icacls test_file_icacls /remove \"%s\" /inheritance:e",
pwd.username);
unlink("test_file_icacls");
uv_os_free_passwd(&pwd);
ASSERT_OK(r);
MAKE_VALGRIND_HAPPY(loop);
return 0;
}
TEST_IMPL(fs_stat_no_permission) {
uv_passwd_t pwd;
uv_fs_t req;
int r;
char* filename = "test_file_no_permission.txt";
/* Setup - clear the ACL and remove the file */
loop = uv_default_loop();
r = uv_os_get_passwd(&pwd);
ASSERT_OK(r);
call_icacls("icacls %s /remove *S-1-1-0:(F)", filename);
unlink(filename);
/* Create the file */
r = uv_fs_open(loop,
&open_req1,
filename,
UV_FS_O_RDONLY | UV_FS_O_CREAT,
S_IRUSR,
NULL);
ASSERT_GE(r, 0);
ASSERT_GE(open_req1.result, 0);
uv_fs_req_cleanup(&open_req1);
r = uv_fs_close(NULL, &close_req, open_req1.result, NULL);
ASSERT_OK(r);
ASSERT_OK(close_req.result);
uv_fs_req_cleanup(&close_req);
/* Set up ACL */
r = call_icacls("icacls %s /deny *S-1-1-0:(F)", filename);
if (r != 0) {
goto acl_cleanup;
}
/* Read file stats */
r = uv_fs_stat(NULL, &req, filename, NULL);
if (r != 0) {
goto acl_cleanup;
}
uv_fs_req_cleanup(&req);
acl_cleanup:
/* Cleanup */
call_icacls("icacls %s /reset", filename);
uv_fs_unlink(NULL, &unlink_req, filename, NULL);
uv_fs_req_cleanup(&unlink_req);
unlink(filename);
uv_os_free_passwd(&pwd);
ASSERT_OK(r);
MAKE_VALGRIND_HAPPY(loop);
return 0;
}
#endif
#ifdef _WIN32
TEST_IMPL(fs_fchmod_archive_readonly) {
uv_fs_t req;
uv_file file;
int r;
/* Test clearing read-only flag from files with Archive flag cleared */
/* Setup*/
unlink("test_file");
r = uv_fs_open(NULL,
&req, "test_file", UV_FS_O_WRONLY | UV_FS_O_CREAT,
S_IWUSR | S_IRUSR,
NULL);
ASSERT_GE(r, 0);
ASSERT_GE(req.result, 0);
file = req.result;
uv_fs_req_cleanup(&req);
r = uv_fs_close(NULL, &req, file, NULL);
ASSERT_OK(r);
uv_fs_req_cleanup(&req);
/* Make the file read-only and clear archive flag */
r = SetFileAttributes("test_file", FILE_ATTRIBUTE_READONLY);
ASSERT(r);
check_permission("test_file", 0400);
/* Try fchmod */
r = uv_fs_open(NULL, &req, "test_file", UV_FS_O_RDONLY, 0, NULL);
ASSERT_GE(r, 0);
ASSERT_GE(req.result, 0);
file = req.result;
uv_fs_req_cleanup(&req);
r = uv_fs_fchmod(NULL, &req, file, S_IWUSR, NULL);
ASSERT_OK(r);
ASSERT_OK(req.result);
uv_fs_req_cleanup(&req);
r = uv_fs_close(NULL, &req, file, NULL);
ASSERT_OK(r);
uv_fs_req_cleanup(&req);
check_permission("test_file", S_IWUSR);
/* Restore Archive flag for rest of the tests */
r = SetFileAttributes("test_file", FILE_ATTRIBUTE_ARCHIVE);
ASSERT(r);
return 0;
}
TEST_IMPL(fs_invalid_mkdir_name) {
uv_loop_t* loop;
uv_fs_t req;
int r;
loop = uv_default_loop();
r = uv_fs_mkdir(loop, &req, "invalid>", 0, NULL);
ASSERT_EQ(r, UV_EINVAL);
ASSERT_EQ(UV_EINVAL, uv_fs_mkdir(loop, &req, "test:lol", 0, NULL));
return 0;
}
#endif
TEST_IMPL(fs_statfs) {
uv_fs_t req;
int r;
loop = uv_default_loop();
/* Test the synchronous version. */
r = uv_fs_statfs(NULL, &req, ".", NULL);
ASSERT_OK(r);
statfs_cb(&req);
ASSERT_EQ(1, statfs_cb_count);
/* Test the asynchronous version. */
r = uv_fs_statfs(loop, &req, ".", statfs_cb);
ASSERT_OK(r);
uv_run(loop, UV_RUN_DEFAULT);
ASSERT_EQ(2, statfs_cb_count);
MAKE_VALGRIND_HAPPY(loop);
return 0;
}
TEST_IMPL(fs_get_system_error) {
uv_fs_t req;
int r;
int system_error;
r = uv_fs_statfs(NULL, &req, "non_existing_file", NULL);
ASSERT(r);
system_error = uv_fs_get_system_error(&req);
#ifdef _WIN32
ASSERT_EQ(system_error, ERROR_FILE_NOT_FOUND);
#else
ASSERT_EQ(system_error, ENOENT);
#endif
return 0;
}
TEST_IMPL(fs_stat_batch_multiple) {
uv_fs_t req[300];
int r;
int i;
rmdir("test_dir");
r = uv_fs_mkdir(NULL, &mkdir_req, "test_dir", 0755, NULL);
ASSERT_OK(r);
loop = uv_default_loop();
for (i = 0; i < (int) ARRAY_SIZE(req); ++i) {
r = uv_fs_stat(loop, &req[i], "test_dir", stat_batch_cb);
ASSERT_OK(r);
}
uv_run(loop, UV_RUN_DEFAULT);
ASSERT_EQ(stat_cb_count, ARRAY_SIZE(req));
MAKE_VALGRIND_HAPPY(loop);
return 0;
}
#ifdef _WIN32
TEST_IMPL(fs_wtf) {
int r;
HANDLE file_handle;
uv_dirent_t dent;
static char test_file_buf[PATHMAX];
/* set-up */
_wunlink(L"test_dir/hi\xD801\x0037");
rmdir("test_dir");
loop = uv_default_loop();
r = uv_fs_mkdir(NULL, &mkdir_req, "test_dir", 0777, NULL);
ASSERT_OK(r);
uv_fs_req_cleanup(&mkdir_req);
file_handle = CreateFileW(L"test_dir/hi\xD801\x0037",
GENERIC_WRITE | FILE_WRITE_ATTRIBUTES,
0,
NULL,
CREATE_ALWAYS,
FILE_FLAG_OPEN_REPARSE_POINT |
FILE_FLAG_BACKUP_SEMANTICS,
NULL);
ASSERT_PTR_NE(file_handle, INVALID_HANDLE_VALUE);
CloseHandle(file_handle);
r = uv_fs_scandir(NULL, &scandir_req, "test_dir", 0, NULL);
ASSERT_EQ(1, r);
ASSERT_EQ(1, scandir_req.result);
ASSERT_NOT_NULL(scandir_req.ptr);
while (UV_EOF != uv_fs_scandir_next(&scandir_req, &dent)) {
snprintf(test_file_buf, sizeof(test_file_buf), "test_dir\\%s", dent.name);
printf("stat %s\n", test_file_buf);
r = uv_fs_stat(NULL, &stat_req, test_file_buf, NULL);
ASSERT_OK(r);
}
uv_fs_req_cleanup(&scandir_req);
ASSERT_NULL(scandir_req.ptr);
/* clean-up */
_wunlink(L"test_dir/hi\xD801\x0037");
rmdir("test_dir");
MAKE_VALGRIND_HAPPY(loop);
return 0;
}
#endif
|