1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456
|
/*
* Copyright 1999-2006 University of Chicago
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef GLOBUS_DONT_DOCUMENT_INTERNAL
/**
* @file globus_gass_cache.c
* @brief GASS Cache API
*/
#endif /* GLOBUS_DONT_DOCUMENT_INTERNAL */
/**
* @addtogroup globus_gass_cache
* The GASS cache management API is part of the Globus module called "GASS",
* (Global Access to Secondary Storage)
* The GASS cache management API defines calls for manipulating a local file
* cache.
*
* Each cache entry can be locked during addition and deletion to allow for
* atomic handling of the cache file contents.
*
* Each cache entry also has an associated timestamp. This timestamp is
* independent of the local cache file's modification time. Rather, the
* cache entry timestamp is maintained by the user. It can, for example, be
* used to keep track of the timestamp of a remote file that is associated
* with the cache entry.
*
* Note: all timestamps are as seconds since the epoch.
* (01 Jan 1970, 00:00 GMT)
*
* The following functions are part of the API:
*
* - globus_gass_cache_open()
* - globus_gass_cache_close()
* - globus_gass_cache_add()
* - globus_gass_cache_add_done()
* - globus_gass_cache_delete_start()
* - globus_gass_cache_delete()
* - globus_gass_cache_cleanup_tag()
* - globus_gass_cache_cleanup_tag_all()
* - globus_gass_cache_mangle_url()
* - globus_gass_cache_mangle_tag()
* - globus_gass_cache_get_dirs()
* - globus_gass_cache_get_cache_dir()
*/
#include "globus_common.h"
#include "globus_hashtable.h"
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <limits.h>
#include <string.h>
#include <utime.h>
#include <sys/types.h>
#include <ctype.h>
#include "openssl/md5.h"
#ifdef _WIN32
#define mkdir(path, mode) mkdir(path)
/* works when t is NULL */
#define utimes(path, t) utime(path, t)
#define srandom(seed) srand(seed)
#define random() rand()
#define link(oldname, newname) (CreateHardLink((newname), (oldname), NULL)==0)
#define sleep(t) Sleep(t*1000)
#endif
/*#define DEBUG 1 */
/*#define GLOBUS_L_GASS_CACHE_LOG 1 */
#include "globus_i_gass_cache.h"
#include "globus_i_gass_cache_config.h"
#include "globus_gass_cache.h"
#include "version.h"
/* Lock existing files when returning ADD_EXISTS? */
#define LOCK_ADD_EXISTS 1
/* RedHat 6.2 (2.2.19 kernel) appears to have a bug in rename */
/* See the code for more details */
#define GLOBUS_L_GASS_CACHE_RENAMEBUG 1
/*
* UNICOS has four quota errno values: EQUSR, EQGRP, EQACT, EOFQUOTA
* #define EQUSR 60 (User file/inode quota limit reached)
* #define EQGRP 61 (Group file/inode quota limit reached)
* #define EQACT 62 (Account file/inode quota limit reached)
* #define EOFQUOTA 363 (File offline, retrieval would
* exceed disk space quota)
*/
#ifdef EDQUOT
#define IS_QUOTA_ERROR(err) ((err) == EDQUOT)
#elif defined(EQUSR) && defined(EQGRP) && defined(EQACT) && defined(EOFQUOTA)
#define IS_QUOTA_ERROR(err) ((err) == EQUSR || \
(err) == EQGRP || \
(err) == EQACT || \
(err) == EOFQUOTA)
#else
#define IS_QUOTA_ERROR(err) (GLOBUS_FALSE)
#endif
/* Local error to return; never returned to the application */
#define GLOBUS_L_EOTHER -100 /* Unknown error */
#define GLOBUS_L_ENOENT -101 /* Does not exist */
#define GLOBUS_L_EEXISTS -102 /* Does exist */
#define GLOBUS_L_ENODATA -103 /* No data file */
#define GLOBUS_L_ENOTDIR -104 /* Not a directory */
#define GLOBUS_L_ENOTUNIQ -105 /* Not the first */
#define GLOBUS_L_UNLINK_LAST -106 /* Last file unlinked */
#define GLOBUS_L_READY_MYPROC -107 /* Data prev. ready for this PID */
#define GLOBUS_L_READY_OTHER -108 /* Data ready from another process */
#define GLOBUS_L_ETIMEOUT -109 /* Time out waiting for ready */
/* HACK */
#ifndef PATH_MAX
#define PATH_MAX 4096
#endif
/* Prototype the mangling functions. */
static
int
globus_l_gass_cache_mangle_html(const char *string,
const char *separator,
int levels,
char *mangled,
int *length );
static
int
globus_l_gass_cache_mangle_md5(const char *string,
const char *separator,
int levels,
char *mangled,
int *length );
/* Other prototypes */
static
int
globus_l_gass_cache_make_dirtree( const char *filepath,
int cache_type);
/* Structure for storing directory paths & other info */
typedef struct cache_names_s
{
/* Mangled URL & tag */
char *mangled_url;
char *mangled_tag;
/* The directories that we use */
char *global_dir;
char *local_dir;
char *local_base_dir;
/* The data file names are built if the URL (& tag for local) are spec. */
char *global_data_file;
char *global_url_file;
char *local_data_file;
char *local_tag_file;
char *local_tag_link;
/* The "uniq" name that we use */
char *uniq;
/* Other names that can be stored here.. */
/* There aren't allocated/filled automatically */
/* Items in the "local" directory */
char *local_uniq_file;
char *localdir_lock_file;
/* Items in the "global" directory */
char *global_uniq_file;
char *globaldir_lock_file; /* Only used in RH62 */
/* These are the deepest directories we will want to remove */
char *local_mangle_root;
char *global_mangle_root;
/* These *never* get freed up 'cause they're aliases to stuff in
the cache_handle */
const char *global_root;
const char *local_root;
const char *tmp_root;
const char *log_root;
/* These *never* get freed up 'cause they're aliases to the
original tag & URL */
const char *tag;
const char *url;
/* This never get freed up 'cause it's an alias to a static string */
int cache_type;
const char *separator;
} cache_names_t;
/* Size of an MD5ed URL / tag */
#define MD5_SIZE (32 + 3)
/* Free a pointer if it's not NULL */
#define FREE_PTR(_ptr_) \
if ( NULL != (_ptr_) ) { free(_ptr_); (_ptr_) = NULL; }
/* Array of mangling functions.. */
#define MANGLING_OPTION_HTML 0x0001
#define MANGLING_OPTION_MD5 0x0002
#define MANGLING_OPTION_ALL (MANGLING_OPTION_HTML | MANGLING_OPTION_MD5)
#define MANGLING_OPTION_DEFAULT (MANGLING_OPTION_MD5)
typedef struct cache_mangling_option_s
{
unsigned flagbits;
char *prefix;
int (* mangle_function) (const char *, const char*, int, char *, int * );
} cache_mangling_option_t;
static cache_mangling_option_t cache_mangling_list [] =
{
{ MANGLING_OPTION_HTML, "html", globus_l_gass_cache_mangle_html },
{ MANGLING_OPTION_MD5, "md5", globus_l_gass_cache_mangle_md5 },
{ 0x0000, NULL, NULL }
};
/* Structures for listing URLs */
typedef struct url_list_elem_s
{
struct url_list_elem_s *next; /* Next in the linked list */
char *mangled; /* Full path to the dir */
int data_count; /* How many data files */
} url_list_elem_t;
typedef struct
{
url_list_elem_t *head; /* Head of the list */
int count; /* Size of the list */
} url_list_head_t;
/* directory types that are supported.
* - a flat cache is performing worse than a normal due to slow listing
* of 1000s of files, but required on some file systems such as AFS due
* to the use of hard links
* - the nolink definition is for file systems with no link support at all,
* such as current releases of PVFS. Needed for a linktest() tool below,
* but we don't write any code for it at this time...
*/
#define DIRECTORY_TYPE_NORMAL 0x0000
#define DIRECTORY_TYPE_FLAT 0x0001
#define DIRECTORY_TYPE_NOLINK 0x0002
/*
* Config settings: the file name is appended to the gass cache directory
* name. Note that the order in the arrays below correspond to the values
* of the DIRECTORY_TYPE_XXX definitions above.
*/
#define GLOBUS_L_GASS_CACHE_CONFIG_FILE "/config"
#define GLOBUS_L_GASS_CACHE_CONFIG_KEY_TYPE "type"
static char* directory_type_values[] = { "normal", "flat", NULL };
static char* directory_separator[] = { "/", "_", NULL };
/* # of MD5 directory Levels */
#define GLOBUS_L_GASS_CACHE_CONFIG_KEY_LEVELS "levels"
#define GLOBUS_L_GASS_CACHE_MAX_LEVELS 4
#define GLOBUS_L_GASS_CACHE_DEFAULT_LEVELS 2
#define GLOBUS_L_GASS_CACHE_DEFAULT_LEVELS_OLD 4
/*
* OLLE: Hmm??? This variable is never used. Commenting it out, instead
* suggesting that work on this is to be continued as an implementation
* for the DIRECTORY_TYPE_NOLINK cache type (currently unsupported).
*/
/* static globus_bool_t globus_l_gass_cache_link_works = GLOBUS_TRUE; */
static int
globus_l_gass_cache_module_activate(void);
/* NRLs Debugging stuff */
static double TODOGetTime(void)
{
struct timeval tv;
gettimeofday( &tv, NULL );
return ( tv.tv_sec + ( tv.tv_usec * 0.000001 ) );
}
globus_module_descriptor_t globus_i_gass_cache_module =
{
"globus_gass_cache",
globus_l_gass_cache_module_activate,
NULL,
NULL,
NULL,
&local_version
};
/******************************************************************************
Module specific variables
******************************************************************************/
/* variables not used by themselves, but pointed by the field "init" of the
cache_handle structures, to indicate if the cache has been initialised
simple methode to help the user: it is his responsbility to open the cache
before using it. A list of handlers could be used...*/
static char globus_l_gass_cache_is_init;
static char globus_l_gass_cache_is_not_init;
static int globus_l_gass_cache_pid;
static int globus_l_gass_cache_fn_fudge;
/* it is fine to declare those macro here, together with the var. they uses */
#define CHECK_CACHE_IS_INIT(_handle_) if (_handle_->init != &globus_l_gass_cache_is_init) return(GLOBUS_GASS_CACHE_ERROR_CACHE_NOT_OPENED)
#define CHECK_CACHE_IS_NOT_INIT(_handle_) if (_handle_->init == &globus_l_gass_cache_is_init) return(GLOBUS_GASS_CACHE_ERROR_CACHE_ALREADY_OPENED)
/* this is not in the globus_gass_cache specs, but we can see if we want to
add it. It is a list of error message corresponding to each error codes
returned by globus_gass_cache functions. The user does not have direct
access to this variable, but thrue the function
globus_gass_cache_error_string()*/
static
char *
globus_gass_cache_error_strings[] =
{
"success",
"your home directory could not be determined",
"the GASS cache directory or status file could not be created",
"the pathname of the internal cache files is too long for the system to handle",
"the GASS cache lock file could not be created or read",
"the GASS cache lock file was broken due to a time out",
"the GASS cache state file could not be opened for reading",
"the GASS cache state file is corrupt",
"a memory allocation failed",
"the local file for an URL could not be created",
"the URL was not found in the GASS cache",
"an old lock file could not be removed",
"the cache entry was locked using a different tag",
"the cache entry was not locked",
"the cache state could not be written",
"the cache state file could not be read",
"the local file for an URL was not being used, but could not be removed",
"the cache handle was invalid",
"the cache handle was already opened",
"the cache_directory_path parameter is not supported",
"the cache library and the cache state file have an incompatible versions",
"the cache file system is full",
"your quota is exceeded in your home filesystem"
};
/******************************************************************************
Module specific prototypes
******************************************************************************/
#ifdef GLOBUS_L_GASS_CACHE_LOG
static
void
globus_l_gass_cache_log(
FILE* f,
const char * str,...);
#endif
#ifdef DEBUG
static
void
globus_l_gass_cache_trace(
const char* source_file,
int line,
const char * str, ...);
#endif
static int
globus_l_gass_cache_module_activate(void)
{
globus_l_gass_cache_pid = globus_libc_getpid();
globus_l_gass_cache_fn_fudge = 0;
return GLOBUS_SUCCESS;
}
void
globus_l_gass_cache_name_lock_file(char * lock_file,
const char * file_to_be_locked);
void
globus_l_gass_cache_name_uniq_lock_file(char * uniq_lock_file,
const char * file_to_be_locked);
#ifdef GLOBUS_L_GASS_CACHE_LOG
#define CACHE_ERROR_MSGSIZE 255
typedef struct
{
int SrcLine;
int ErrNo;
int rc;
char msgbuf[CACHE_ERROR_MSGSIZE + 1];
} CACHE_ERROR;
#define MAX_ERRORS 20
static struct
{
int NumErrors;
CACHE_ERROR Errors[MAX_ERRORS];
} cache_errors;
void error_return(int line, int ErrNo, int rc, const char *msg )
{
int num = cache_errors.NumErrors;
if ( num < ( MAX_ERRORS - 1 ) )
{
CACHE_ERROR *error = &cache_errors.Errors[num];
error->SrcLine = line;
error->ErrNo = ErrNo;
error->rc = rc;
if ( NULL != msg )
{
strncpy( error->msgbuf, msg, CACHE_ERROR_MSGSIZE );
error->msgbuf[CACHE_ERROR_MSGSIZE] = '\0';
}
else
{
error->msgbuf[0] = '\0';
}
cache_errors.NumErrors++;
}
}
void error_backtrace(FILE *fp)
{
int num;
if ( cache_errors.NumErrors )
{
globus_l_gass_cache_log( fp, "Backtrace:\n" );
for ( num = (cache_errors.NumErrors - 1) ; num >= 0; num-- )
{
CACHE_ERROR *error = &cache_errors.Errors[num];
if ( '\0' == error->msgbuf[0] )
{
globus_l_gass_cache_log(
fp,
" Line=%d, errno=%d, Rc=%d\n",
error->SrcLine, error->ErrNo, error->rc );
}
else
{
globus_l_gass_cache_log(
fp,
" Line=%d, errno=%d, Rc=%d, msg='%s'\n",
error->SrcLine, error->ErrNo, error->rc, error->msgbuf );
}
}
}
}
#define CLR_ERROR cache_errors.NumErrors = 0
#define MARK_ERROR(_rc_) \
error_return(__LINE__, errno, (_rc_), NULL);
#define MARK_ERRORMSG(_rc_,_msg_) \
error_return(__LINE__, errno, (_rc_), (_msg_) );
#define RET_ERROR(_rc_) \
error_return(__LINE__, errno, (_rc_), NULL); return (_rc_)
#define RET_ERRORMSG(_rc_,_msg_) \
error_return(__LINE__, errno, (_rc_), (_msg_) ); return (_rc_)
#define LOG_ERROR(_x_) \
globus_l_gass_cache_log(cache_handle->log_FILE,\
"ERROR: Line %d, errno %d; rc=%d\n",\
__LINE__, errno, _x_ );\
error_backtrace(cache_handle->log_FILE)
/*
* globus_l_gass_cache_log()
*
* Write in the file [first param] the message passed as a
* "printf-like" argument list, prepended by the date, the hostname and
* the PID of the caller process.
*
* Parameters:
* f - file to write in.
*
* Returns:
* none */
static
void
globus_l_gass_cache_log(
FILE* f,
const char* str,
...)
{
va_list args;
char hname[MAXHOSTNAMELEN];
char time_buff[26];
time_t ttime;
globus_thread_t mytid;
/* Note : I use the local time to log. This might not be the best */
time(&ttime);
globus_libc_ctime_r(&ttime,time_buff,sizeof(time_buff));
/* If the log file hasn't been opened yet (i.e. we're logging from open) */
/* log *somewhere* */
if ( NULL == f )
f = stderr;
/* remove the \n */
time_buff[24]='\0';
globus_libc_gethostname(hname,sizeof(hname));
mytid = globus_thread_self();
fprintf(f,"GASSCACHE: %s %s PID:%ld TID:%ld : ",
time_buff,
hname,
(long)getpid(),
(long) mytid.dummy);
va_start(args, str);
vfprintf(f, str, args);
va_end(args);
fprintf(f,"\n");
fflush(f);
}
/* globus_gass_cache_log() */
#else
#define CLR_ERROR
#define LOG_ERROR(_x_)
#define RET_ERROR(_rc_) return(_rc_)
#define RET_ERRORMSG(_rc_,_msg_) return(_rc_)
#define MARK_ERROR(_rc_)
#define MARK_ERRORMSG(_rc_,_msg_)
#endif
#ifdef DEBUG
/*
* globus_l_gass_cache_trace()
*
* Writes on the standard error the message passed as a
* "printf-like" argument list [3rd arg], prepended by the file name of
* the source code and the line number passed as 2 first arguments.
* This function should be called this way:
* globus_l_gass_cache_trace(__FILE__,__LINE," printf-like format string",
* args...);
*
* Parameters:
* source_file - File name of the source code
* line - line number in the source code
*
*Returns:
* none
*/
static
void
globus_l_gass_cache_trace(
const char* source_file,
int line,
const char *format,
...)
{
va_list args;
struct timeval tv;
static FILE *fp = NULL;
static globus_bool_t enabled = GLOBUS_FALSE;
if( fp == NULL )
{
char *env = getenv( GLOBUS_L_GASS_CACHE_DEBUG );
if ( NULL == env )
{
enabled = GLOBUS_FALSE;
}
else
{
enabled = GLOBUS_TRUE;
if ( ( '\0' == *env ) || ( ! strcmp ( env, "stderr" ) ) )
{
fp = stderr;
}
else if ( ! strcmp ( env, "stdout" ) )
{
fp = stdout;
}
else
{
fp = fopen( env, "a+" );
}
}
}
/* Are we enabled? */
if ( GLOBUS_FALSE == enabled )
{
return;
}
gettimeofday( & tv, NULL );
va_start(args,format);
fprintf(fp,"GC %ld.%06ld: %s %d : ",
tv.tv_sec, tv.tv_usec, source_file, line);
vfprintf(fp, format, args);
va_end(args);
fprintf(fp,"\n");
fflush(fp);
}
/* globus_l_gass_cache_trace() */
#endif
/*
* globus_l_gass_cache_linktest()
*
* Perform a test if hard links can be used in the current directory.
* Two tests are performed:
* (a) link inside a local directory
* (b) link across a directory boundary
*
* Parameters:
* cache - Cache handle, containing cache_directory_path which
* is known to exist
*
* Returns:
* DIRECTORY_TYPE_NOLINK - test (a) doesn't work
* DIRECTORY_TYPE_NORMAL - tests (a) and (b) both work
* DIRECTORY_TYPE_FLAT - test (a) work but not (b)
*/
static int
globus_l_gass_cache_linktest(globus_i_gass_cache_t *cache)
{
char dir[PATH_MAX];
char file[PATH_MAX];
char link1[PATH_MAX];
char link2[PATH_MAX];
int fd;
int rc;
struct stat stx;
globus_thread_t t = globus_thread_self();
rc = DIRECTORY_TYPE_NOLINK;
sprintf(dir, "%s/dir-%ld-%ld",
cache->cache_directory_path,
(long) globus_libc_getpid(),
(long) t.dummy );
sprintf(file, "%s/file", dir );
sprintf(link1, "%s/link", dir );
sprintf(link2, "%s/link-%ld-%ld",
cache->cache_directory_path,
(long) globus_libc_getpid(),
(long) t.dummy );
/* create test dir and file */
rmdir(dir);
if (mkdir( dir, GLOBUS_L_GASS_CACHE_DIR_MODE ))
goto cleanup;
fd = open(file, O_RDWR|O_CREAT|O_TRUNC, GLOBUS_L_GASS_CACHE_MODE_RW);
if (fd < 0)
goto cleanup;
close(fd);
/* verify n_link returns correct value */
stx.st_nlink = 0;
if (stat(file, &stx) || (stx.st_nlink != 1))
goto cleanup;
/* test (a): link in same directory */
stx.st_nlink = 0;
if (link(file,link1) || stat(file,&stx) || stx.st_nlink!=2)
goto cleanup;
rc = DIRECTORY_TYPE_FLAT;
/* test (b): link from another directory */
stx.st_nlink = 0;
if (link(file,link2) || stat(file,&stx) || stx.st_nlink!=3)
goto cleanup;
rc = DIRECTORY_TYPE_NORMAL;
cleanup:
remove(link1);
remove(link2);
remove(file);
rmdir(dir);
return rc;
}
/*
* globus_l_gass_cache_build_filename()
*
* Build a cache filename by concatenating a file name onto a
* directory name. If no file name is provided, the directory name
* alone is copied into the output path buffer.
*
* Parameters:
* dir - Incoming directory name
* separator - Incoming directory separator
* file - Optional file name
* uniq - Optional uniqueness string to append at the end
* pathsize - if non-null, points to size of already allocated memory
* for path. Will realloc if more is necessary.
* path - Outgoing full file path
*
* Returns: VOID
*
*/
static
int
globus_l_gass_cache_build_filename(const char *dir,
const char *separator,
const char *file,
const char *uniq,
int * pathsize,
char **path )
{
globus_size_t dirlen = strlen( dir );
globus_size_t filelen;
globus_size_t uniqlen;
globus_size_t len;
char * p;
/* Compute the lengths */
filelen = (file) ? strlen( file ) : 0;
uniqlen = (uniq) ? strlen( uniq ) : 0;
len = dirlen + filelen + uniqlen + 2; /* a separator and \0 */
/* Allocate the buffer */
if ( !*path || !pathsize || *pathsize < len )
{
if ( *path ) free( *path );
if ( pathsize ) *pathsize = len;
*path = malloc( len );
}
if ( NULL == *path )
{
RET_ERROR( GLOBUS_GASS_CACHE_ERROR_NO_MEMORY );
}
/* Fill it */
memcpy( *path, dir, dirlen );
p = *path + dirlen;
if ( file )
{
*p++ = *separator;
}
if ( file )
{
memcpy( p, file, filelen );
p += filelen;
}
if ( uniq )
{
memcpy( p, uniq, uniqlen );
p += uniqlen;
}
*p = '\0';
/* All done, good */
return GLOBUS_SUCCESS;
} /* globus_l_gass_cache_build_filename() */
/*
* globus_l_gass_cache_mangle_html()
*
* Parameters:
* string - The string to mangle
* mangled - Pointer to the buffer to mangle into (or NULL)
* length - Pointer to the length of the mangled output (or NULL)
*
* Returns:
*
*/
static
int
globus_l_gass_cache_mangle_html(const char *string,
const char *separator,
int levels,
char *mangled,
int *length )
{
/* These are "safe" characters */
const char *safe = "$-_.+";
const char *extra = "!*'(),";
int mangle_separator = 0; /* Bool: Mangle extra slashes? */
int len = 0;
int c;
/* We ignore the levels setting here... */
(void) levels;
/* Copy & clean.. */
while( ( c = *string++ ) != '\0' )
{
int mangle = 0;
/* Alpha-numerics get copied directly */
if ( isalnum( c ) )
{
/* Do nothing */
}
/* Safe chars get copied directly */
else if ( strchr( safe, c ) != NULL )
{
/* Do nothing */
}
/* Extra chars get copied directly */
else if ( strchr( extra, c ) != NULL )
{
/* Do nothing */
}
/* Copy slashes (/) directly... */
else if ( ( *separator == c ) && ( ! mangle_separator ) )
{
/* Do nothing */
}
/* Everything else gets mangled */
else
{
mangle = 1;
}
/* Copy directly.. */
if ( ! mangle )
{
mangle_separator = ( *separator == c );
len++;
if ( mangled )
{
*mangled++ = c;
}
}
/* Mangle the byte here */
else
{
len += 3;
if ( mangled )
{
sprintf( mangled, "%%%02x", c );
mangled += 3;
}
/* If this isn't a slash, force off / mangling for the next one */
if ( *separator != c )
{
mangle_separator = 0;
}
}
}
/* Terminate the string */
if ( mangled )
{
*mangled = '\0';
}
/* Store off the length (add one for \0) */
if ( length )
{
*length = ++len;
}
/* All ok! */
return GLOBUS_SUCCESS;
} /* globus_l_gass_cache_mangle_html() */
/*
* globus_l_gass_cache_mangle_md5()
*
* Parameters:
* string - The string to mangle
* mangled - Pointer to the buffer to mangle into (or NULL)
* length - Pointer to the length of the mangled output (or NULL)
*
* Returns:
*
*/
static
int
globus_l_gass_cache_mangle_md5(const char *string,
const char *separator,
int levels,
char *mangled,
int *length )
{
/* Length is fixed */
if ( NULL != length )
{
/* +1 for \0, +3 for added slashes, +3 for .00 extension */
*length = ( MD5_DIGEST_LENGTH * 2 ) + 1 + 3 + 3;
}
/* Do the mangling? */
if ( NULL != mangled )
{
unsigned char md5[MD5_DIGEST_LENGTH], *md5ptr = &md5[0];
int i;
/* Use the "levels" for the max # of separators.. Adjust it
* here for the "i" index of the loop. */
levels -= 2;
/* Do the real work. */
MD5( (const unsigned char *) string, strlen(string), md5 );
/* Convert it to string format */
for ( i=0; i<MD5_DIGEST_LENGTH; i++ )
{
sprintf( mangled, "%02x", *md5ptr );
mangled += 2;
md5ptr++;
if ( i <= levels )
{
*mangled++ = *separator;
}
}
*mangled = '\0';
}
/* All ok! */
return GLOBUS_SUCCESS;
} /* globus_l_gass_cache_mangle_md5() */
/*
* globus_l_gass_cache_mangle()
*
* Mangles the given string (typically URL or tag) into a chunk
* suitable for using as a file / path name.
*
* Parameters:
* cache - GASS cache handle
*
* string - The incoming string to mangle (\0 terminated)
*
* mangled - Pointer to the output string; a buffer for the real
* string is malloc()ed for the application. If mangled is NULL,
* then no such buffer is allocated, and no mangled string is
* created. This can be useful to just get the length of the
* mangled string.
*
* Length - The length of the resulting string. If NULL, this is
* not assigned to.
*
* Returns:
* GLOBUS_SUCCESS
* GLOBUS_GASS_CACHE_ERROR_NO_MEMORY
*
*/
int
globus_l_gass_cache_mangle(const globus_gass_cache_t cache,
const char *string,
const int max_mangled_len,
char **mangled,
int *length )
{
cache_mangling_option_t *option;
int rc;
int option_no;
int len;
char *separator;
separator = directory_separator[cache->cache_type];
/* Get the mangled length */
for ( option_no = 0, option = &cache_mangling_list[0];
option->flagbits;
option_no++, option++ )
{
if ( cache->mangling_options & option->flagbits )
{
rc = option->mangle_function(string, separator,
cache->directory_levels,
NULL, &len);
if ( GLOBUS_SUCCESS != rc )
{
RET_ERROR( rc );
}
len += strlen( option-> prefix ) + 1; /* for the separator */
if ( max_mangled_len > 0 && len <= max_mangled_len )
{
break;
}
}
}
/* Valid option found? */
if ( ! option->flagbits )
{
RET_ERROR( GLOBUS_GASS_CACHE_ERROR_NAME_TOO_LONG );
}
/* Store the length */
if ( length )
{
*length = len;
}
/* Allocate a buffer for it & mangle into it */
if ( mangled )
{
char *mptr;
mptr = *mangled = malloc( len );
if ( NULL == mptr )
{
return GLOBUS_GASS_CACHE_ERROR_NO_MEMORY;
}
strcpy( mptr, option->prefix );
mptr += strlen( mptr );
*mptr++ = *separator;
rc = option->mangle_function( string,
separator,
cache->directory_levels,
mptr,
NULL );
if ( GLOBUS_SUCCESS != rc )
{
RET_ERROR( rc );
}
}
return GLOBUS_SUCCESS;
} /* globus_l_gass_cache_mangle() */
/*
* globus_l_gass_cache_build_dirname()
*
* Build a cache filename; can be used to generate either the global
* or local file names. Pass tag=NULL for global.
*
* Parameters:
*
* Returns:
*
*/
static
int
globus_l_gass_cache_build_dirname( const char *root,
const char *separator,
const char *mangled_tag,
const char *mangled_url,
char **path,
int *path_len )
{
int len = strlen( root ) + 1; /* root + '\0' */
/* NULL out the passed in parameters in case of error */
*path = NULL;
/* Add in for the tag, if relevant. */
if ( mangled_tag )
{
len += ( strlen( mangled_tag ) + 1 ); /* + slash */
}
/* Add in the mangled */
if ( NULL != mangled_url )
{
len += ( strlen( mangled_url ) + 1 ); /* + slash */
}
/* Allocate the buffer.. */
*path = malloc( len );
if ( NULL == *path )
{
return GLOBUS_GASS_CACHE_ERROR_NO_MEMORY;
}
/* Store off the length.. */
if ( path_len )
{
*path_len = len;
}
/* Build it */
strcpy( *path, root );
if ( mangled_tag )
{
strcat( *path, separator );
strcat( *path, mangled_tag );
}
if ( mangled_url )
{
strcat( *path, separator );
strcat( *path, mangled_url );
}
/* Ok */
return GLOBUS_SUCCESS;
} /* globus_l_gass_cache_build_dirname() */
/*
* globus_l_gass_cache_build_uniqname()
*
* Parameters:
*
* Returns:
*
*/
static
int
globus_l_gass_cache_build_uniqname( char **uniq )
{
char hostname[MAXHOSTNAMELEN];
char uniq_string[UNIQ_NAME_MAX];
globus_thread_t t = globus_thread_self();
/* !!! need to handle multi threaded !!! */
globus_libc_gethostname( hostname, sizeof(hostname) );
sprintf(uniq_string,
UNIQ_NAME_FORMAT,
hostname,
(long) globus_libc_getpid(),
(long) t.dummy );
/* Assign to the uniq passed in.. */
*uniq = strdup( uniq_string );
if ( NULL == *uniq )
{
RET_ERROR( GLOBUS_GASS_CACHE_ERROR_NO_MEMORY );
}
/* Outa here */
return GLOBUS_SUCCESS;
} /* globus_l_gass_cache_build_uniqname() */
/*
* globus_l_gass_cache_names_free()
*
* Free all allocated names
*
* Parameters:
* names - Pointer to the "names" structure
*
* Returns:
* GLOBUS_SUCCESS
*/
static
int
globus_l_gass_cache_names_free( cache_names_t *names )
{
/* Free the mangled URL & tag */
FREE_PTR( names->mangled_url );
FREE_PTR( names->mangled_tag );
/* Free the global... */
FREE_PTR( names->global_dir );
/* Free the local dir.. */
FREE_PTR( names->local_dir );
FREE_PTR( names->local_base_dir );
/* The Uniq string */
FREE_PTR( names->uniq );
/* Optional names. */
/* The local names. */
FREE_PTR( names->local_data_file );
FREE_PTR( names->local_tag_file );
FREE_PTR( names->local_tag_link );
FREE_PTR( names->local_uniq_file );
FREE_PTR( names->localdir_lock_file );
/* The global names. */
FREE_PTR( names->global_data_file );
FREE_PTR( names->global_url_file );
FREE_PTR( names->global_uniq_file );
FREE_PTR( names->globaldir_lock_file );
/* Mangle roots */
FREE_PTR( names->local_mangle_root);
FREE_PTR( names->global_mangle_root);
/* Finally, zero out the PATHs */
memset( names, 0, sizeof( cache_names_t ) );
/* Here, all is good. Return ok. */
return GLOBUS_SUCCESS;
} /* globus_l_gass_cache_names_free() */
/*
* globus_l_gass_cache_names_fill_global()
*
* Fill the global portion of the names structure
*
* Parameters:
* names - The names structure to fill
*
* Returns:
* GLOBUS_SUCCESS
* GLOBUS_GASS_CACHE_ERROR_NO_MEMORY
*
*/
static
int
globus_l_gass_cache_names_fill_global( cache_names_t *names )
{
int rc = GLOBUS_SUCCESS;
/* Assemble the global... */
if ( NULL == names->mangled_url )
{
return GLOBUS_SUCCESS;
}
if ( GLOBUS_SUCCESS == rc )
{
rc = globus_l_gass_cache_build_dirname(
names->global_root,
names->separator,
NULL,
names->mangled_url,
&names->global_dir,
NULL );
}
/* Build the global data file name; *everything* uses it anyway */
if ( GLOBUS_SUCCESS == rc )
{
rc = globus_l_gass_cache_build_filename(
names->global_dir,
names->separator,
DATA_FILE,
NULL,
NULL,
&names->global_data_file );
}
if ( GLOBUS_SUCCESS == rc )
{
rc = globus_l_gass_cache_build_filename(
names->global_dir,
names->separator,
URL_FILE,
NULL,
NULL,
&names->global_url_file );
}
/* Done; */
if ( GLOBUS_SUCCESS == rc )
{
return GLOBUS_SUCCESS;
}
else
{
RET_ERROR( rc );
}
} /* globus_l_gass_cache_names_fill_global() */
/*
* globus_l_gass_cache_names_fill_local()
*
* Fill the local portion of the names structure
*
* Parameters:
* names - The names structure to fill
*
* Returns:
* GLOBUS_SUCCESS
* GLOBUS_GASS_CACHE_ERROR_NO_MEMORY
*
*/
static
int
globus_l_gass_cache_names_fill_local( cache_names_t *names )
{
int rc = GLOBUS_SUCCESS;
/* Valid tag? */
if ( NULL == names->mangled_tag )
{
return GLOBUS_SUCCESS;
}
/* Create the base local directory name (we don't need the URL for it) */
if ( GLOBUS_SUCCESS == rc )
{
rc = globus_l_gass_cache_build_dirname(
names->local_root,
names->separator,
names->mangled_tag,
NULL,
&names->local_base_dir,
NULL );
if ( GLOBUS_SUCCESS != rc )
{
RET_ERROR( rc );
}
}
/* And, the local tag file */
if ( GLOBUS_SUCCESS == rc )
{
rc = globus_l_gass_cache_build_filename(
names->local_base_dir,
names->separator,
TAG_FILE,
NULL,
NULL,
&names->local_tag_file );
}
/* For everything else, we need a valid URL, too */
if ( NULL == names->mangled_url )
{
return GLOBUS_SUCCESS;
}
/* Create the local directory name. */
if ( GLOBUS_SUCCESS == rc )
{
rc = globus_l_gass_cache_build_dirname(
names->local_root,
names->separator,
names->mangled_tag,
names->mangled_url,
&names->local_dir,
NULL );
}
/* Build the local data file name; *everything* uses it anyway */
if ( GLOBUS_SUCCESS == rc )
{
rc = globus_l_gass_cache_build_filename(
names->local_dir,
names->separator,
DATA_FILE,
NULL,
NULL,
&names->local_data_file );
}
/* The local tag "link" name */
if ( GLOBUS_SUCCESS == rc )
{
rc = globus_l_gass_cache_build_filename(
names->local_dir,
names->separator,
TAG_FILE,
NULL,
NULL,
&names->local_tag_link );
}
/* Done; */
if ( GLOBUS_SUCCESS == rc )
{
return GLOBUS_SUCCESS;
}
else
{
RET_ERROR( rc );
}
} /* globus_l_gass_cache_names_fill_local() */
/*
* globus_l_gass_cache_names_init()
*
* Initialize the common names structure
*
* Parameters:
*
* Returns:
*
*/
static
int
globus_l_gass_cache_names_init( const globus_gass_cache_t cache,
const char *url,
const char *tag,
cache_names_t *names )
{
int rc = GLOBUS_SUCCESS;
char * mangle_prefix;
char * mangle_prefix_end;
int mangle_prefix_len;
/* Zero out the PATHs first... */
memset( names, 0, sizeof( cache_names_t ) );
/* Create the links to the root dirs */
names->global_root = cache->global_directory_path;
names->local_root = cache->local_directory_path;
names->tmp_root = cache->tmp_directory_path;
names->log_root = cache->tmp_directory_path;
/* if no tag supplied, we map it to the tag
GLOBUS_L_GASS_CACHE_NULL_TAG ("null") */
if ( NULL == tag )
{
tag = GLOBUS_L_GASS_CACHE_NULL_TAG;
}
/* Create the URL & tag aliases */
names->url = url;
names->tag = tag;
/* Mangle the URL */
if ( ( GLOBUS_SUCCESS == rc ) && ( NULL != url ) )
{
rc = globus_l_gass_cache_mangle(cache,
url,
cache->max_mangled_url,
&names->mangled_url,
NULL );
}
/* And, the tag */
if ( ( GLOBUS_SUCCESS == rc ) && ( NULL != tag ) )
{
rc = globus_l_gass_cache_mangle(cache,
tag,
cache->max_mangled_tag,
&names->mangled_tag,
NULL );
}
/* directory separator character (for flat vs. hierarchial) */
if ( GLOBUS_SUCCESS == rc )
{
names->cache_type = cache->cache_type;
names->separator = directory_separator[cache->cache_type];
}
/* Assemble the global dir & related names */
if ( GLOBUS_SUCCESS == rc )
{
rc = globus_l_gass_cache_names_fill_global( names );
}
/* Assemble the local dir.. */
if ( GLOBUS_SUCCESS == rc )
{
rc = globus_l_gass_cache_names_fill_local( names );
}
/* Uniq string */
if ( GLOBUS_SUCCESS == rc )
{
rc = globus_l_gass_cache_build_uniqname( &names->uniq );
}
mangle_prefix = names->mangled_tag;
mangle_prefix_end = strstr(names->mangled_tag, names->separator);
mangle_prefix_len = mangle_prefix_end - mangle_prefix;
if ( GLOBUS_SUCCESS == rc )
{
assert(mangle_prefix_end != NULL);
names->local_mangle_root =
globus_common_create_string(
"%s%s%.*s",
names->local_root,
names->separator,
mangle_prefix_len,
mangle_prefix);
if (names->local_mangle_root == NULL)
{
rc = GLOBUS_GASS_CACHE_ERROR_NO_MEMORY;
MARK_ERROR( rc );
}
}
if ( GLOBUS_SUCCESS == rc )
{
assert(mangle_prefix_end != NULL);
names->global_mangle_root = globus_common_create_string(
"%s%s%.*s",
names->global_root,
names->separator,
mangle_prefix_len,
mangle_prefix);
if (names->global_mangle_root == NULL)
{
rc = GLOBUS_GASS_CACHE_ERROR_NO_MEMORY;
MARK_ERROR( rc );
}
}
/* If we've had failures, free up all allocated memory */
if ( GLOBUS_SUCCESS != rc )
{
globus_l_gass_cache_names_free( names );
RET_ERROR( rc );
}
/* Here, all is good. Return ok. */
return GLOBUS_SUCCESS;
} /* globus_l_gass_cache_names_init() */
/*
* globus_l_gass_cache_names_new_murl()
*
* Set a new mangled URL in the names structure. This is for use
* primarily for cleanup_tag_all().
*
* Parameters:
*
* Returns:
*
*/
static
int
globus_l_gass_cache_names_new_murl( const char *mangled_url,
cache_names_t *names )
{
int rc = GLOBUS_SUCCESS;
/* We don't know the unmangled URL here... */
names->url = NULL;
/* Is it really different? */
if ( NULL != names->mangled_url )
{
if ( ! strcmp( names->mangled_url, mangled_url ) )
{
return GLOBUS_SUCCESS;
}
else
{
free( names->mangled_url );
}
}
/* Copy it */
names->mangled_url = strdup( mangled_url );
if ( NULL == names->mangled_url )
{
RET_ERROR( GLOBUS_GASS_CACHE_ERROR_NO_MEMORY );
}
/* Free up existing local & global directories */
FREE_PTR( names->global_dir );
FREE_PTR( names->local_dir );
FREE_PTR( names->local_base_dir );
/* And, anything else that might be affected. */
FREE_PTR( names->local_data_file );
FREE_PTR( names->local_uniq_file );
FREE_PTR( names->local_tag_file );
FREE_PTR( names->local_tag_link );
FREE_PTR( names->localdir_lock_file );
/* The global names. */
FREE_PTR( names->global_data_file );
FREE_PTR( names->global_url_file );
FREE_PTR( names->global_uniq_file );
FREE_PTR( names->globaldir_lock_file );
/* Assemble the global dir & related names */
if ( GLOBUS_SUCCESS == rc )
{
rc = globus_l_gass_cache_names_fill_global( names );
}
/* Assemble the local dir.. */
if ( GLOBUS_SUCCESS == rc )
{
rc = globus_l_gass_cache_names_fill_local( names );
}
/* If we've had failures, free up all allocated memory */
if ( GLOBUS_SUCCESS != rc )
{
globus_l_gass_cache_names_free( names );
RET_ERROR( rc );
}
/* Here, all is good. Return ok. */
return GLOBUS_SUCCESS;
} /* globus_l_gass_cache_names_new_murl() */
/*
* globus_l_gass_cache_stat()
*
* Build a cache filename; can be used to generate either the global
* or local file names. Pass tag=NULL for global.
*
* Parameters:
*
* Returns:
*
*/
static
int
globus_l_gass_cache_stat ( const char *filepath,
struct stat *statptr )
{
struct stat statbuf;
int rc;
/* Check for null statptr.. */
if ( NULL == statptr )
{
statptr = &statbuf;
}
/* Run stat 'til we get an answer.. */
while( 1 )
{
rc = stat( filepath, statptr );
if ( 0 == rc )
{
return GLOBUS_SUCCESS;
}
if ( ENOENT == errno )
{
RET_ERRORMSG( GLOBUS_L_ENOENT, filepath );
}
if ( EINTR != errno )
{
RET_ERROR( GLOBUS_L_EOTHER );
}
}
} /* globus_l_gass_cache_stat() */
#if defined( DEBUGxxxx )
static void TODOgu( const char *g, const char *u, const char *msg )
{
static char uniq_name[1024], data_name[1024];
struct stat statbuf;
int uniq_inode = -1, data_inode = -1;
if ( g )
{
sprintf( data_name, "%s/%s", g, DATA_FILE );
CACHE_TRACE3( "%d: Data name is '%s'", getpid(), data_name );
if ( u )
{
sprintf( uniq_name, "%s/%s%s", g, UDATA_FILE, u );
CACHE_TRACE3( "%d: Uniq name is '%s'", getpid(), uniq_name );
}
}
if ( msg )
{
if ( globus_l_gass_cache_stat( data_name, &statbuf ) == 0 )
data_inode = statbuf.st_ino;
if ( globus_l_gass_cache_stat( uniq_name, &statbuf ) == 0 )
uniq_inode = statbuf.st_ino;
CACHE_TRACE5( "%d/%s: data/uniq %d/%d",
getpid(), msg, data_inode, uniq_inode );
}
}
#else
# define TODOgu(_g_,_u_,_m_)
#endif
/*
* globus_l_gass_cache_create()
*
* Parameters:
*
* Returns:
*
*/
static
int
globus_l_gass_cache_create( const char *filepath,
const char *dir,
int mode,
const char *buf,
int buflen )
{
int fd;
int mkdir_try = 0;
/* Try to create the file... */
while ( ( fd = creat( filepath, mode ) ) < 0 )
{
/* Interrupted? Go try again */
if ( EINTR == errno )
{
continue;
}
/* ENOENT? Directory went away.. */
if ( ENOENT == errno )
{
/* If the app gave us a directory, try again... */
if ( ( NULL != dir ) &&
( ++mkdir_try < CREATE_MAX_TRIES ) )
{
int rc = globus_l_gass_cache_make_dirtree(
dir,
DIRECTORY_TYPE_NORMAL );
if ( GLOBUS_SUCCESS != rc )
{
RET_ERROR( rc );
}
continue; /* Try again.. */
}
/* No dir specified or time to give up */
else
{
RET_ERROR( GLOBUS_L_ENOENT );
}
}
/* Handle other errors */
if ( ENOSPC == errno)
{
RET_ERROR( GLOBUS_GASS_CACHE_ERROR_NO_SPACE );
}
else if (IS_QUOTA_ERROR(errno))
{
RET_ERROR( GLOBUS_GASS_CACHE_ERROR_QUOTA_EXCEEDED );
}
else
{
CACHE_TRACE2("Error creat()ing '%s'", filepath );
RET_ERROR( GLOBUS_GASS_CACHE_ERROR_CAN_NOT_WRITE );
}
}
/* Write a buffer to it.. */
if ( ( NULL != buf ) && ( 0 != buflen ) )
{
int written;
while( buflen > 0)
{
written = write( fd, buf, buflen );
if ( written < 0 )
{
if ( errno != EINTR )
{
CACHE_TRACE2( "creat: Error writing to '%s'; unlinking\n",
filepath );
unlink( filepath );
RET_ERROR( GLOBUS_GASS_CACHE_ERROR_CAN_NOT_WRITE );
}
}
else if ( written > 0 )
{
buf += written;
buflen -= written;
}
}
}
/* Close it */
while ( close( fd ) < 0 )
{
if (errno != EINTR)
{
CACHE_TRACE2( "creat: Error closing '%s'; unlinking\n", filepath );
unlink( filepath );
RET_ERROR( GLOBUS_GASS_CACHE_ERROR_CAN_NOT_WRITE );
}
}
/* Done */
return GLOBUS_SUCCESS;
} /* globus_l_gass_cache_create() */
/*
* globus_l_gass_cache_link()
*
* Parameters:
*
* Returns:
*
*/
static
int
globus_l_gass_cache_link( const char *oldfile,
const char *newfile )
{
int link_retry = 0; /* How many link() retries? */
/* Loop til link fails w/o EINTR or 'til link succeeds */
while ( link( oldfile, newfile ) < 0 )
{
if ( EINTR == errno )
{
continue;
}
else if (errno == ENOSPC)
{
RET_ERROR( GLOBUS_GASS_CACHE_ERROR_NO_SPACE );
}
else if ( EEXIST == errno )
{
RET_ERROR( GLOBUS_L_EEXISTS );
}
/*else if ( ( ENOENT == errno ) || ( EINVAL == errno ) ) */
else if ( ENOENT == errno )
{
int rc;
/* RedHat 6.2 seems to return ENOENT sometimes even though
* it *does* exist & stat says so. So, we'll stat it, and, if
* it does exist, sleep a bit & try again.
*/
if ( ++link_retry < LINKBUG_MAX_RETRY )
{
rc = globus_l_gass_cache_stat( oldfile, NULL );
if ( GLOBUS_SUCCESS == rc )
{
CACHE_TRACE3(
"LINK: link bug encountered; try %d, errno %d",
link_retry, errno );
globus_libc_usleep( LINKBUG_SLEEP_USEC );
continue;
}
RET_ERROR( GLOBUS_L_ENOENT );
}
/* Give up! */
else
{
RET_ERROR( GLOBUS_L_ENOENT );
}
}
else if ( IS_QUOTA_ERROR(errno) )
{
RET_ERROR( GLOBUS_GASS_CACHE_ERROR_QUOTA_EXCEEDED );
}
else
{
MARK_ERRORMSG( GLOBUS_L_EOTHER, oldfile );
MARK_ERRORMSG( GLOBUS_L_EOTHER, newfile );
RET_ERROR( GLOBUS_GASS_CACHE_ERROR_CAN_NOT_WRITE );
}
}
/* Return ok */
return GLOBUS_SUCCESS;
} /* globus_l_gass_cache_link() */
/*
* globus_l_gass_cache_rename()
*
* Parameters:
*
* Returns:
*
*/
static
int
globus_l_gass_cache_rename( const char *oldfile,
const char *newfile )
{
int rename_retry = 0; /* How many rename() retries? */
/* Loop til link fails w/o EINTR or 'til link succeeds */
while ( rename( oldfile, newfile ) < 0 )
{
if ( EINTR == errno )
{
continue;
}
else if (errno == ENOSPC)
{
RET_ERROR( GLOBUS_GASS_CACHE_ERROR_NO_SPACE );
}
else if ( EEXIST == errno )
{
RET_ERROR( GLOBUS_L_EEXISTS );
}
else if ( ENOENT == errno )
{
int rc;
/* RedHat 6.2 seems to return ENOENT sometimes even though
* it *does* exist & stat says so. So, we'll stat it, and, if
* it does exist, sleep a bit & try again.
*/
if ( ++rename_retry < LINKBUG_MAX_RETRY )
{
rc = globus_l_gass_cache_stat( oldfile, NULL );
if ( GLOBUS_SUCCESS == rc )
{
CACHE_TRACE3( "RENAME/%d: rename bug encountered; try %d",
getpid(), rename_retry );
globus_libc_usleep( LINKBUG_SLEEP_USEC );
continue;
}
RET_ERROR( GLOBUS_L_ENOENT );
}
/* Give up! */
else
{
RET_ERROR( GLOBUS_L_ENOENT );
}
}
else if ( IS_QUOTA_ERROR(errno) )
{
RET_ERROR( GLOBUS_GASS_CACHE_ERROR_QUOTA_EXCEEDED );
}
else
{
RET_ERROR( GLOBUS_GASS_CACHE_ERROR_CAN_NOT_WRITE );
}
}
/* Return ok */
return GLOBUS_SUCCESS;
} /* globus_l_gass_cache_link() */
/*
* globus_l_gass_cache_unlink()
*
* Parameters:
*
* Returns:
*
*/
static
int
globus_l_gass_cache_unlink( const char *filepath )
{
int tries = 0;
/* Loop til unlink fails w/o EINTR or 'til link succeeds */
while ( unlink( filepath ) < 0 )
{
/* EINTR; just try again */
if ( EINTR == errno )
{
if ( ++tries > UNLINK_MAX_TRIES )
{
RET_ERROR( GLOBUS_GASS_CACHE_ERROR_CAN_NOT_WRITE );
}
continue;
}
/* If it's already gone, then we'll consider it a success */
if ( ENOENT == errno )
{
return GLOBUS_SUCCESS;
}
/* If it's "busy" wait a bit & try again.. */
else if ( EBUSY == errno )
{
if ( ++tries > UNLINK_MAX_TRIES )
{
RET_ERROR( GLOBUS_GASS_CACHE_ERROR_CAN_NOT_WRITE );
}
globus_libc_usleep( EBUSY_SLEEP_USEC );
continue;
}
/* Otherwise, something bad happened. */
RET_ERROR( GLOBUS_GASS_CACHE_ERROR_CAN_NOT_WRITE );
}
/* Return ok */
return GLOBUS_SUCCESS;
} /* globus_l_gass_cache_unlink() */
/*
* globus_l_gass_cache_scandir_free()
*
* Free the list from scandir()
*
* Parameters:
*
* Returns:
*
*/
static
void
globus_l_gass_cache_scandir_free( struct dirent **list,
int count )
{
int i;
/* IF the list is empty, do nothing! */
if ( NULL == list )
{
return;
}
/* Loop til unlink fails w/o EINTR or 'til link succeeds */
for( i = 0; i < count; i++ )
{
free( list[i] );
}
free( list );
} /* globus_l_gass_cache_scandir_free() */
/*
* globus_l_gass_cache_scandir()
*
* Parameters:
*
* Returns:
*
*/
static
int
globus_l_gass_cache_scandir( const char *directory,
const int cache_type,
struct dirent ***list,
int *list_count,
globus_bool_t (*selectfn)(const struct dirent *,
const char *,
const int) )
{
DIR *dirptr;
struct dirent *dir_entry = NULL;
struct dirent **newlist;
char *dirname;
char *prefix = NULL;
int prefix_len = -1;
int newlist_size = 2;
int count = 0;
int rc = GLOBUS_SUCCESS;
/* Initialize the count */
*list_count = 0;
dirname = strdup(directory);
if (cache_type == DIRECTORY_TYPE_FLAT)
{
prefix = strrchr(dirname, '/');
*prefix++ = '\0';
prefix_len = strlen(prefix);
}
do
{
dirptr = opendir( dirname );
/* Open the directory for reading.. */
if ( dirptr == NULL )
{
if ( ENOENT == errno )
{
free(dirname);
RET_ERROR( GLOBUS_L_ENOENT );
}
else if ( EINTR != errno )
{
free(dirname);
RET_ERROR( GLOBUS_L_EOTHER );
}
}
} while (dirptr == NULL && errno == EINTR);
assert(dirptr != NULL);
/* Allocate & fill our "head" node */
newlist = malloc( newlist_size * sizeof( struct dirent * ) );
if ( NULL == newlist )
{
free(dirname);
closedir( dirptr );
RET_ERROR( GLOBUS_GASS_CACHE_ERROR_NO_MEMORY );
}
/* Read the whole directory... */
for( globus_libc_readdir_r( dirptr, &dir_entry );
( dir_entry != NULL ) && ( GLOBUS_SUCCESS == rc );
globus_libc_readdir_r( dirptr, &dir_entry ) )
{
int keep = GLOBUS_FALSE;
/* Invoke the select function... */
if ( selectfn==NULL || selectfn(dir_entry, prefix, prefix_len) )
{
keep = GLOBUS_TRUE;
if (prefix)
{
/* remove prefix from d_name */
char *p = dir_entry->d_name;
char *q = p + prefix_len + 1; /* +1 for the separator */
while (*q)
{
*p++ = *q++;
}
*p = '\0';
}
}
/* If we should keep this entry.. */
if (keep)
{
/* Did we exceed the size of our list? */
if ( count >= newlist_size )
{
struct dirent **tmp;
/* Grow our list... */
newlist_size *= 2;
tmp = globus_realloc(
newlist,
newlist_size * sizeof( struct dirent *) );
if ( NULL == tmp )
{
globus_l_gass_cache_scandir_free( newlist, count );
free(dirname);
free(dir_entry);
closedir( dirptr );
RET_ERROR( GLOBUS_GASS_CACHE_ERROR_NO_MEMORY );
}
newlist = tmp;
}
/* The globus readdir_r() function malloc()s the dir_entry,
* so, I can just use it as is */
newlist[count++] = dir_entry;
}
/* Not keeping it; because readdir_r() malloc()ed dir_entry,
* I must free it */
else
{
free( dir_entry );
}
}
/* Done */
*list = newlist;
*list_count = count;
free(dirname);
closedir( dirptr );
return GLOBUS_SUCCESS;
} /* globus_l_gass_cache_scandir() */
/*
* globus_l_gass_cache_set_timestamp()
*
* Parameters:
*
* Returns:
*
*/
static
int
globus_l_gass_cache_set_timestamp( const char *filepath,
unsigned long timestamp )
{
struct utimbuf timebuf;
int rc;
/* Copy the timestamp into the utime structure */
timebuf.actime = (time_t) timestamp;
timebuf.modtime = (time_t) timestamp;
/* Loop til we succeed or fail */
do
{
rc = utime( filepath, &timebuf );
if (rc != 0)
{
if ( ENOENT == errno )
{
RET_ERROR( GLOBUS_L_ENOENT );
}
else if ( EINTR != errno )
{
RET_ERROR( GLOBUS_L_EOTHER );
}
}
}
while (rc != 0);
return GLOBUS_SUCCESS;
} /* globus_l_gass_cache_set_timestamp() */
/*
* globus_l_gass_cache_calc_timeskew()
*
* Parameters:
*
* Returns:
*
*/
static
int
globus_l_gass_cache_calc_timeskew( const char *tmp_file,
struct timeval *skew )
{
int rc;
struct timeval start, end;
double mean;
struct stat statbuf;
double mtime;
double diff;
/* Get start time */
gettimeofday( & start, NULL );
/* Create the file. */
rc = globus_l_gass_cache_create(
tmp_file, NULL, GLOBUS_L_GASS_CACHE_SKEWFILE_MODE,
NULL, 0 );
if ( rc != GLOBUS_SUCCESS )
{
RET_ERROR( rc );
}
/* Get the end time. */
gettimeofday( & end, NULL );
/* Go get it's time stamp. */
rc = globus_l_gass_cache_stat( tmp_file, &statbuf );
if ( rc != GLOBUS_SUCCESS )
{
RET_ERROR( rc );
}
/* Perform the calculations... */
mean = 0.5 * ( ( end.tv_sec + ( 1e-6 * end.tv_usec ) ) +
( start.tv_sec + ( 1e-6 * start.tv_usec ) ) );
mtime = statbuf.st_mtime;
diff = mtime - mean;
skew->tv_sec = ( long ) diff;
skew->tv_usec = ( long ) ( ( diff - skew->tv_sec ) * 1e6 );
/* Done with the file */
(void) globus_l_gass_cache_unlink( tmp_file );
/* Done */
return GLOBUS_SUCCESS;
} /* globus_l_gass_cache_calc_timeskew() */
/*
* globus_l_gass_cache_calc_timeskew()
*
* Parameters:
*
* Returns:
*
*/
static
int
globus_l_gass_cache_calc_file_age( const char *tmp_file,
time_t cur_time,
time_t mtime )
{
static time_t next_skewcalc = -1;
static int cur_skew = 0;
static const char *file = NULL;
struct timeval skew;
int tmp_skew;
int rc;
int age;
/* Do we need the tmp file? */
if ( ( NULL == file ) && ( NULL != tmp_file ) )
{
file = strdup( tmp_file );
if ( NULL == file )
{
file = tmp_file;
}
}
/* Do we need to recalculate? */
if ( ( NULL != file ) &&
( ( next_skewcalc < 0 ) || ( cur_time > next_skewcalc ) ) )
{
next_skewcalc = cur_time + SKEWCALC_SECONDS;
rc = globus_l_gass_cache_calc_timeskew( file, & skew );
if ( GLOBUS_SUCCESS == rc )
{
tmp_skew = skew.tv_sec;
if ( ( skew.tv_usec > 500000 ) && ( cur_skew >= 0 ) )
{
tmp_skew++;
}
else if ( ( skew.tv_usec >= 500000 ) && ( cur_skew < 0 ) )
{
tmp_skew--;
}
/* Update in one (should be atomic) operation. */
cur_skew = tmp_skew;
}
/* If we're using the tmp file, reset to NULL */
if ( file == tmp_file )
{
file = NULL;
}
}
/* Perform the calc.. */
age = ( cur_time - mtime ) + cur_skew;
return age;
} /* globus_l_gass_cache_calc_file_age() */
/*
* globus_l_gass_cache_make_dirtree()
*
* Parameters:
* - filepath -- the directory to create
* - cache_type -- one of DIRECTORY_TYPE_NORMAL or DIRECTORY_TYPE_FLAT
* if FLAT, will assume anything after the last / is a
* file name component: thus, no directory hiearchies
* will be created below the base directory.
*
* Returns:
*
*/
static
int
globus_l_gass_cache_make_dirtree( const char *filepath, int cache_type )
{
char *temppath = malloc( strlen( filepath ) + 1 );
const char *pos;
int offset = 0;
int rc;
unsigned length = strlen( filepath );
struct stat statbuf;
int tries = 0;
/* Check that malloc() didn't fail */
if ( NULL == temppath )
{
RET_ERROR( GLOBUS_GASS_CACHE_ERROR_NO_MEMORY );
}
if (cache_type==DIRECTORY_TYPE_FLAT)
{
/* anything after the last / is a file name */
pos = strrchr(filepath, '/');
if (pos)
{
int n = pos - filepath;
if (n==0)
{
/* root file system always exist... */
free(temppath);
return GLOBUS_SUCCESS;
}
/* put parent directory in temppath */
strncpy(temppath,filepath,n);
*(temppath+n) = '\0';
/* now create the file */
rc = globus_l_gass_cache_create(filepath,
temppath,
GLOBUS_L_GASS_CACHE_MODE_RW,
NULL,
0 );
free(temppath);
return rc;
}
}
/* Loop through each component of the path... */
while( *(filepath + offset) != '\0' )
{
pos = strchr( filepath + offset, '/' );
if ( NULL == pos )
{
pos = filepath + strlen( filepath );
}
/* Calculate the offset of the current "/" in the path */
offset = ( pos - filepath );
/* Copy the up to this "/" into the temp buffer */
if ( 0 == offset )
{
strcpy( temppath, "/" );
}
else
{
strncpy( temppath, filepath, offset );
*(temppath + offset) = '\0';
}
/* Does it exist? Is it a directory? */
rc = globus_l_gass_cache_stat( temppath, &statbuf );
/* If it exists, make sure its a directory! */
if ( GLOBUS_SUCCESS == rc )
{
if ( ! S_ISDIR( statbuf.st_mode ) )
{
free( temppath );
RET_ERROR( GLOBUS_L_ENOTDIR );
}
}
/* If it doesn't exist, create it */
else if ( GLOBUS_L_ENOENT == rc )
{
CLR_ERROR;
if ( mkdir( temppath, GLOBUS_L_GASS_CACHE_DIR_MODE ) < 0 )
{
/* Check if somebody else beat me to it */
if ( EEXIST == errno )
{
/* If we keep failing something else must be wrong */
if ( ++tries > MAKE_DIRTREE_MAX_TRIES )
{
free( temppath );
RET_ERROR( GLOBUS_GASS_CACHE_ERROR_CAN_NOT_CREATE );
}
continue;
}
/* Somebody else deleted the base out from under us... */
else if ( ENOENT == errno )
{
/* If we keep failing something else must be wrong */
if ( ++tries > MAKE_DIRTREE_MAX_TRIES )
{
free( temppath );
RET_ERROR( GLOBUS_GASS_CACHE_ERROR_CAN_NOT_CREATE );
}
/* Start the whole process over */
offset = 0;
continue;
}
/* Something else went wrong; give up. */
else
{
free( temppath );
RET_ERROR( GLOBUS_GASS_CACHE_ERROR_CAN_NOT_CREATE );
}
}
}
/* Otherwise, something else is wrong.. */
else
{
free( temppath );
RET_ERROR( rc );
}
/* Are we done? */
if ( length <= strlen( temppath ) )
{
break;
}
/* Move the offset past the "/" for next time */
offset++;
}
/* Done */
free( temppath );
return GLOBUS_SUCCESS;
} /* globus_l_gass_cache_make_dirtree() */
/*
* globus_l_gass_cache_scandir_select_uniq()
*
* Select only files which match any file name. Passed as the third
* parameter to scandir()
*
* Parameters:
*
* Returns:
* - 0: Don't include in the dirlist
* - <>0: Include in dirlist
*/
static
globus_bool_t
globus_l_gass_cache_scandir_select_uniq(const struct dirent *entry,
const char *prefix,
const int prefix_len)
{
const char *p = entry->d_name;
char * pattern = UDATA_FILE_PAT;
int len = UDATA_FILE_PAT_LEN;
if (prefix)
{
if (strncmp(p, prefix, prefix_len) || *(p+prefix_len)=='\0')
{
return GLOBUS_FALSE;
}
p += prefix_len+1; /* +1 for the separator */
}
/* All uniq files should start with our "data" pattern */
return ( ! strncmp( p, pattern, len ) );
} /* globus_l_gass_cache_scandir_select_uniq() */
/*
* globus_l_gass_cache_scandir_select_data()
*
* Select only files which match any *data* file name. Passed as the
* third paramter to scandir()
*
* Parameters:
*
* Returns:
* - 0: Don't include in the dirlist
* - <>0: Include in dirlist
*/
static
globus_bool_t
globus_l_gass_cache_scandir_select_data(const struct dirent *entry,
const char *prefix,
const int prefix_len)
{
const char *p = entry->d_name;
char * pattern = DATA_FILE_PAT;
int len = DATA_FILE_PAT_LEN;
if (prefix)
{
if (strncmp(p, prefix, prefix_len) || *(p+prefix_len)=='\0')
{
return GLOBUS_FALSE;
}
p += prefix_len+1; /* +1 for the separator */
}
/* All uniq files should start with our "data" pattern */
return ( ! strncmp( p, pattern, len ) );
} /* globus_l_gass_cache_scandir_select_data() */
/*
* globus_l_gass_cache_scandir_select_all()
*
* Select only files which match all patterns. Passed as the third
* paramter to scandir()
*
* Parameters:
*
* Returns:
* - 0: Don't include in the dirlist
* - <>0: Include in dirlist
*/
static
globus_bool_t
globus_l_gass_cache_scandir_select_all(const struct dirent *entry,
const char *prefix,
const int prefix_len)
{
const char * p = entry->d_name;
return (prefix)
? ( ! strncmp(p, prefix, prefix_len) ) && ( *(p+prefix_len)!='\0' )
: GLOBUS_TRUE;
} /* globus_l_gass_cache_scandir_select_all() */
/*
* globus_l_gass_cache_remove_dirtree_flat()
*
* This function is a replacement for remove_dirtree() that works for
* both cache directory types. Instead of discovering afterwards that
* an operation failed (by checking errno), we instead perform a listing
* first...
*
* Currently only used for the flat cache type though, thereby the name.
*
* Parameters:
*
* Returns:
*
*/
static
int
globus_l_gass_cache_remove_dirtree_flat( const cache_names_t *names,
const char *base,
const char *tree )
{
struct dirent **list = NULL;
struct stat statbuf;
int list_count;
int rc = GLOBUS_SUCCESS;
/* check if subdir is empty */
rc = globus_l_gass_cache_scandir(
tree,
names->cache_type,
&list,
&list_count,
globus_l_gass_cache_scandir_select_all );
if (rc != GLOBUS_SUCCESS)
{
/* nothing to do */
return rc;
}
globus_l_gass_cache_scandir_free(list, list_count);
if (list_count>0)
{
return 1; /* "opportunistic error code" */
}
if ((rc = globus_l_gass_cache_stat(tree, &statbuf)) != GLOBUS_SUCCESS)
{
/* 'tree' is no more... someone already removed it for us */
return GLOBUS_SUCCESS;
}
/* Check if it's a file or a directory */
if ( ! S_ISDIR( statbuf.st_mode ) )
{
rc = globus_l_gass_cache_unlink(tree);
}
else
{
while ( rmdir( tree ) < 0 )
{
/* Not empty (Solaris returns EEXIST - go figure) */
if ( ( ENOTEMPTY == errno ) || ( EEXIST == errno ) )
{
return 1;
}
else if ( EINTR == errno )
{
continue;
}
else if ( ENOENT == errno )
{
break; /* Somebody else beat me to it! */
}
RET_ERROR( -1 );
}
}
if (rc==GLOBUS_SUCCESS)
{
char *q, *p = strdup(tree);
if (!p)
{
RET_ERROR( -1 );
}
if ((q = strrchr(p,*names->separator)))
{
*q = '\0';
if (strlen(p) > strlen(base))
{
rc = globus_l_gass_cache_remove_dirtree_flat(names,
base,
p );
}
}
free(p);
}
return rc;
}
/* globus_l_gass_cache_remove_dirtree_flat() */
/*
* globus_l_gass_cache_remove_dirtree()
*
* Parameters:
*
* Returns:
*
*/
static
int
globus_l_gass_cache_remove_dirtree( const cache_names_t *names,
const char *base,
const char *mangle_base,
const char *tree )
{
char *fullpath = NULL;
char *pos;
struct stat statbuf;
int rc;
unsigned length = strlen( mangle_base );
if (names->cache_type==DIRECTORY_TYPE_FLAT)
{
return globus_l_gass_cache_remove_dirtree_flat(names, base, tree);
}
/* Make a local copy of hte full path.. */
fullpath = strdup( tree );
if ( NULL == fullpath )
{
RET_ERROR( -1 );
}
/* Loop through each component of the path... */
while( strlen( fullpath ) > length )
{
/* Does it exist? Is it a directory? */
rc = globus_l_gass_cache_stat( fullpath, &statbuf );
if ( GLOBUS_SUCCESS == rc )
{
/* Bomb out if it's not a directory! */
if ( ! S_ISDIR( statbuf.st_mode ) )
{
free( fullpath );
RET_ERROR( -1 );
}
/* If rmdir fails for any reason, we're hosed */
while ( rmdir( fullpath ) < 0 )
{
/* Not empty (Solaris returns EEXIST - go figure) */
if ( ( ENOTEMPTY == errno ) || ( EEXIST == errno ) )
{
free( fullpath );
return 1;
}
else if ( EINTR == errno )
{
continue;
}
else if ( ENOENT == errno )
{
break; /* Somebody else beat me to it! */
}
free( fullpath );
RET_ERROR( -1 );
}
}
else if ( GLOBUS_L_ENOENT == rc )
{
/* Nothing to do */
}
else
{
free( fullpath );
return rc;
}
/* Back up one level... */
pos = strrchr( fullpath, '/' );
*pos = '\0';
}
/* Done */
free( fullpath );
return GLOBUS_SUCCESS;
} /* globus_l_gass_cache_remove_dirtree() */
/*
* globus_l_gass_cache_check_global_file()
*
* Check for the existence of a global file
*
* Parameters:
*
* Returns:
* - GLOBUS_SUCCESS
* - GLOBUS_L_ENODATA - No data files
*/
static
int
globus_l_gass_cache_check_global_file( const cache_names_t *names,
int *num_data,
int *num_uniq )
{
int rc;
struct dirent **data_list = NULL;
int data_list_count;
int data_num;
int data_count = 0;
int uniq_count = 0;
/* Init the ptrs */
if ( NULL != num_data )
{
*num_data = 0;
}
if ( NULL != num_uniq )
{
*num_uniq = 0;
}
/* Scan the directory; we're looking at *all* of the "data" files */
rc = globus_l_gass_cache_scandir(
names->global_dir,
names->cache_type,
&data_list,
&data_list_count,
globus_l_gass_cache_scandir_select_data );
/* ENOENT means the directory somehow got smoked on us... */
if ( ( GLOBUS_L_ENOENT == rc ) || ( 0 == data_list_count ) )
{
globus_l_gass_cache_scandir_free( data_list, data_list_count );
return GLOBUS_L_ENODATA;
}
/* Something else bad happenned */
else if ( GLOBUS_SUCCESS != rc )
{
RET_ERROR( GLOBUS_L_EOTHER );
}
/* Here, there's one or more entries ( count in rc ).. */
for ( data_num = 0; data_num < data_list_count; data_num++ )
{
int len = strlen(data_list[data_num]->d_name) - DATA_FILE_PAT_LEN;
/* Is it *the* data file? */
if ( ! strcmp( DATA_FILE_PAT, data_list[data_num]->d_name + len) )
{
data_count++;
}
else
{
uniq_count++;
}
}
globus_l_gass_cache_scandir_free( data_list, data_list_count );
/* Copy out the counts... */
if ( NULL != num_data )
{
*num_data = data_count;
}
if ( NULL != num_uniq )
{
*num_uniq = uniq_count;
}
/* Done */
RET_ERROR( GLOBUS_SUCCESS );
} /* globus_l_gass_cache_check_global_file() */
/*
* globus_l_gass_cache_create_uniq_global_file()
*
* Build a cache filename; can be used to generate either the global
* or local file names. Pass tag=NULL for global.
*
* Parameters:
*
* Returns:
* - GLOBUS_SUCCESS
* - GLOBUS_L_ENOTUNIQ - We didn't win the race
* - Returned failures from globus_l_gass_cache_creat()
* - Returned failures from globus_l_gass_cache_link()
* - Returned failures from globus_l_gass_cache_unlink()
*/
static
int
globus_l_gass_cache_create_uniq_global_file( const cache_names_t *names )
{
int rc;
int uniq_count;
struct dirent **uniq_list = NULL;
struct stat statbuf;
int uniq_num;
ino_t uniq_inode;
int lower_inode_found = GLOBUS_FALSE;
const char *TODOname = NULL;
# ifdef DEBUG
ino_t TODOinode;
# endif
/* Create my uniq file */
rc = globus_l_gass_cache_create(
names->global_uniq_file, names->global_dir,
GLOBUS_L_GASS_CACHE_UNIQFILE_MODE,
NULL, 0 );
/* Other error */
if ( rc < 0 )
{
RET_ERROR( rc );
}
/* Stat *my* file */
rc = globus_l_gass_cache_stat( names->global_uniq_file, &statbuf );
/* There are no errors that I can handle here... */
if ( rc < 0 )
{
(void) globus_l_gass_cache_unlink( names->global_uniq_file );
RET_ERROR( rc );
}
uniq_inode = statbuf.st_ino;
/* Scan the directory; we're looking at *all* of the "uniq" files */
rc = globus_l_gass_cache_scandir(
names->global_dir,
names->cache_type,
&uniq_list,
&uniq_count,
globus_l_gass_cache_scandir_select_uniq );
/* ENOENT means the directory somehow got smoked on us... */
if ( GLOBUS_L_ENOENT == rc )
{
(void) globus_l_gass_cache_scandir_free( uniq_list, uniq_count );
RET_ERROR( rc );
}
/* Something else bad happenned */
else if ( GLOBUS_SUCCESS != rc )
{
(void) globus_l_gass_cache_unlink( names->global_uniq_file );
RET_ERROR( GLOBUS_GASS_CACHE_ERROR_CAN_NOT_CREATE );
}
/* No matches... Something's wrong.. */
else if ( 0 == uniq_count )
{
(void) globus_l_gass_cache_scandir_free( uniq_list, uniq_count );
(void) globus_l_gass_cache_unlink( names->global_uniq_file );
RET_ERROR( GLOBUS_L_ENOENT );
}
/* 1 should mean just *my* file (hopefully) */
else if ( 1 == uniq_count )
{
globus_l_gass_cache_scandir_free( uniq_list, 1 );
return GLOBUS_SUCCESS;
}
/* More than one "uniq" file. Look at them all, find the lowest inode */
/* If we're the lowest, return SUCCESS, else ENOTUNIQ */
for ( uniq_num = 0; uniq_num < uniq_count; uniq_num++ )
{
/* Don't need to stat my uniq; I already did that above. */
if ( ! strcmp( names->uniq, uniq_list[uniq_num]->d_name ) )
{
/* If it's indoe # is lower than mine, it wins, we lose */
if ( uniq_list[uniq_num]->d_ino < uniq_inode )
{
lower_inode_found = GLOBUS_TRUE;
TODOname = strdup( uniq_list[uniq_num]->d_name );
# ifdef DEBUG
TODOinode = uniq_list[uniq_num]->d_ino;
# endif
break; /* Don't need to look any further */
}
}
}
/* Free up the list that scandir() built so we don't leak memory */
globus_l_gass_cache_scandir_free( uniq_list, uniq_count );
/* Found a lower inode; it wins, we lose */
if ( GLOBUS_TRUE == lower_inode_found )
{
CACHE_TRACE5( "INODE %d '%s' < %d '%s'; we lose",
TODOinode, TODOname, uniq_inode,
names->global_uniq_file );
free( (void *) TODOname );
rc = globus_l_gass_cache_unlink( names->global_uniq_file );
if ( ( GLOBUS_L_ENOENT != rc ) && ( rc < 0 ) )
{
RET_ERROR( rc );
}
RET_ERROR( GLOBUS_L_ENOTUNIQ );
}
else
{
return GLOBUS_SUCCESS;
}
} /* globus_l_gass_cache_create_uniq_global_file() */
/*
* globus_l_gass_cache_create_global_url_file()
*
* Create the global "URL" file
* Note: Currently we're not checking the URL for match
*
* Parameters:
*
* Returns:
* GLOBUS_SUCCESS
* Errors returned by _build_filename(), _create(), _link()
*/
static
int
globus_l_gass_cache_create_global_url_file( cache_names_t *names )
{
int rc = GLOBUS_SUCCESS;
char *uniq_filename = NULL;
/* Build "uniq" file name */
rc = globus_l_gass_cache_build_filename(
names->global_dir,
names->separator,
URL_FILE,
names->uniq,
NULL,
&uniq_filename );
if ( GLOBUS_SUCCESS != rc )
{
RET_ERROR( rc );
}
/* Write the file */
rc = globus_l_gass_cache_create(
uniq_filename, names->global_dir, GLOBUS_L_GASS_CACHE_URLFILE_MODE,
names->url, strlen( names->url ) );
if ( GLOBUS_SUCCESS != rc )
{
free( uniq_filename );
RET_ERROR( rc );
}
/* Link it to the real URL file */
rc = globus_l_gass_cache_link( uniq_filename, names->global_url_file );
(void) globus_l_gass_cache_unlink( uniq_filename );
free( uniq_filename );
if ( GLOBUS_L_EEXISTS == rc )
{
/* Probably, here, we should verify that the URLs match... */
return GLOBUS_SUCCESS;
}
else if ( GLOBUS_SUCCESS != rc )
{
RET_ERROR( rc );
}
/* All ok! */
return GLOBUS_SUCCESS;
} /* globus_l_gass_cache_create_global_url_file() */
/*
* globus_l_gass_cache_create_global_file()
*
* Build a cache filename; can be used to generate either the global
* or local file names. Pass tag=NULL for global.
*
* Parameters:
*
* Returns:
* - GLOBUS_SUCCESS
* - Returned failures from globus_l_gass_cache_creat()
* - Returned failures from globus_l_gass_cache_link()
* - Returned failures from globus_l_gass_cache_unlink()
*/
static
int
globus_l_gass_cache_create_global_file( cache_names_t *names,
globus_bool_t force )
{
int rc = GLOBUS_SUCCESS;
int uniq_count;
struct dirent **uniq_list;
/* Build file name */
if ( GLOBUS_SUCCESS == rc )
{
rc = globus_l_gass_cache_build_filename(
names->global_dir,
names->separator,
UDATA_FILE,
names->uniq,
NULL,
&names->global_uniq_file );
if ( GLOBUS_SUCCESS != rc )
{
RET_ERROR( rc );
}
}
/* Scan the directory */
rc = globus_l_gass_cache_scandir(
names->global_dir,
names->cache_type,
&uniq_list,
&uniq_count,
globus_l_gass_cache_scandir_select_uniq );
/* Check for errors */
if ( GLOBUS_L_ENOENT == rc )
{
return GLOBUS_L_ENOENT;
}
else if ( GLOBUS_SUCCESS != rc )
{
return GLOBUS_GASS_CACHE_ERROR_CAN_NOT_CREATE;
}
else if ( ( uniq_count > 0 ) && ( GLOBUS_FALSE == force ) )
{
globus_l_gass_cache_scandir_free( uniq_list, uniq_count );
return GLOBUS_L_ENOTUNIQ;
}
/* Here, either the count is zero, or we're in force mode.. */
globus_l_gass_cache_scandir_free( uniq_list, uniq_count );
/* Create my uniq file */
rc = globus_l_gass_cache_create_uniq_global_file( names );
if ( rc < 0 )
{
RET_ERROR( rc );
}
/* Create my "URL" file */
rc = globus_l_gass_cache_create_global_url_file( names );
if ( rc < 0 )
{
RET_ERROR( rc );
}
/* Done; all ok */
return GLOBUS_SUCCESS;
} /* globus_l_gass_cache_create_global_file() */
/*
* globus_l_gass_cache_make_ready()
*
* Make the global file "ready".
*
* Parameters:
*
* Returns:
*
*/
static
int
globus_l_gass_cache_make_ready( cache_names_t *names,
unsigned long timestamp )
{
struct stat statbuf;
int rc = GLOBUS_SUCCESS;
/* Build the file names */
/* Note that these will be freed by the top level call to names_free() */
/* Build the uniq file name */
if ( GLOBUS_SUCCESS == rc )
{
rc = globus_l_gass_cache_build_filename(
names->global_dir,
names->separator,
UDATA_FILE,
names->uniq,
NULL,
&names->global_uniq_file );
}
/* Handle errors */
if ( GLOBUS_SUCCESS != rc )
{
RET_ERROR( rc );
}
/* Does the uniq file exist? If so, something's wrong! */
while ( 1 )
{
rc = globus_l_gass_cache_stat( names->global_uniq_file, &statbuf );
if ( GLOBUS_L_ENOENT == rc )
{
/* If uniq doesn't exist & data does, it's already ready! */
rc = globus_l_gass_cache_stat( names->global_data_file,
NULL );
if ( GLOBUS_SUCCESS == rc )
{
RET_ERROR( GLOBUS_L_READY_MYPROC );
}
else
{
RET_ERROR( GLOBUS_L_ENODATA );
}
}
else if ( GLOBUS_SUCCESS != rc )
{
RET_ERROR( rc );
}
if ( S_ISDIR( statbuf.st_mode ) )
{
RET_ERROR( GLOBUS_L_EOTHER );
}
/* Set the uniq file's timestamp */
rc = globus_l_gass_cache_set_timestamp(
names->global_uniq_file, timestamp );
if ( GLOBUS_L_ENOENT == rc )
{
continue; /* Do nothing, loop back to top, should fail stat() */
}
else if ( GLOBUS_SUCCESS != rc )
{
RET_ERROR( GLOBUS_GASS_CACHE_ERROR_CAN_NOT_CREATE );
}
/* Create a link between uniq and data */
rc = globus_l_gass_cache_link( names->global_uniq_file,
names->global_data_file );
/* If successful, exit the loop */
if ( GLOBUS_SUCCESS == rc )
{
break;
}
/* Handle errors from link() */
if ( GLOBUS_L_EEXISTS == rc )
{
/* Other process beat us to it; no harm done, though... */
CACHE_TRACE2( "MAKE_READY: Lost, unlinking '%s'",
names->global_uniq_file );
globus_l_gass_cache_unlink( names->global_uniq_file );
RET_ERROR( GLOBUS_L_READY_OTHER );
}
else if ( GLOBUS_L_ENOENT == rc )
{
/* Do nothing, loop back to top, should fail stat() */
}
else
{
RET_ERROR( GLOBUS_GASS_CACHE_ERROR_CAN_NOT_CREATE );
}
}
/* And, unlink the "uniq" version, leaving behind only the global */
rc = globus_l_gass_cache_unlink( names->global_uniq_file );
if ( GLOBUS_SUCCESS != rc )
{
RET_ERROR( rc );
}
/* Done. */
return GLOBUS_SUCCESS;
} /* globus_l_gass_cache_make_ready() */
/*
* globus_l_gass_cache_make_unready()
*
* Build a cache filename; can be used to generate either the global
* or local file names. Pass tag=NULL for global.
*
* Parameters:
*
* Returns:
* GLOBUS_L_CACHE_UNLINK_LAST
*
*/
static
int
globus_l_gass_cache_make_unready( cache_names_t *names )
{
# if GLOBUS_L_GASS_CACHE_RENAMEBUG
struct stat statbuf;
int lock_age;
time_t cur_time;
# endif
int rc = GLOBUS_SUCCESS;
/* Build the name of the uniq file... */
if ( GLOBUS_SUCCESS == rc )
{
rc = globus_l_gass_cache_build_filename(
names->global_dir,
names->separator,
UDATA_FILE,
names->uniq,
NULL,
&names->global_uniq_file );
}
if ( GLOBUS_SUCCESS != rc )
{
RET_ERROR( rc );
}
/* The RedHat 6.2 kernel (2.2.19) seems to have a race bug in which
* rename() allows two processes to "win". i.e.:
* process a: a=rename("x", "a");
* process b: b=rename("x", "b");
* Results: a==0, b==0, file "x" gone, but "a" and "b" are hard
* links to each other. This is a work-around for this bug
*/
# if GLOBUS_L_GASS_CACHE_RENAMEBUG
/*
* 1. Link data -> lock
* 2a. Fail: Check lock age, break lock if old, start over
* 2b. Fail, lock not old: return ENODATA
* 2c. Ok: Rename 'data' -> data.uniq
*/
/* Create lock file name */
rc = globus_l_gass_cache_build_filename(
names->global_dir,
names->separator,
LOCK_FILE,
NULL,
NULL,
&names->globaldir_lock_file );
if ( GLOBUS_SUCCESS != rc )
{
RET_ERROR( rc );
}
/* Loop 'til we get the lock ok, lock taken, or failure */
while( 1 )
{
/* Link it to the lock file */
rc = globus_l_gass_cache_link( names->global_data_file,
names->globaldir_lock_file );
/* If ok, we win! */
if ( GLOBUS_SUCCESS == rc )
{
break;
}
/* Some other process killed our temp file! */
else if ( GLOBUS_L_ENOENT == rc )
{
CACHE_TRACE( "Unready; data file (@link) gone!" );
RET_ERROR( GLOBUS_L_ENODATA );
}
/* Some other error from link(). Bomb out */
else if ( GLOBUS_L_EEXISTS != rc )
{
MARK_ERRORMSG( rc, names->global_data_file );
MARK_ERRORMSG( rc, names->globaldir_lock_file );
RET_ERRORMSG( rc, "Unready: unknown link error" );
}
/* Lock file already exists */
/* ( GLOBUS_L_EEXISTS == rc ) */
CACHE_TRACE( "Unready; didn't get lock (EEXISTS)" );
/* Check the lock file's status. */
rc = globus_l_gass_cache_stat( names->globaldir_lock_file, &statbuf );
if ( GLOBUS_L_ENOENT == rc )
{
/* It went away; go try again */
CACHE_TRACE( "Unready: Link failed, but ENOENT" );
continue;
}
else if ( GLOBUS_SUCCESS != rc )
{
RET_ERROR( rc );
}
/* Make sure it's still valid */
cur_time = time( NULL );
lock_age = globus_l_gass_cache_calc_file_age( NULL,
cur_time,
statbuf.st_mtime );
/* If it's too old, invalidate it */
if ( lock_age > LOCK_MAX_SECONDS )
{
CACHE_TRACE3( "Killing global lock file %s; age = %d sec",
names->globaldir_lock_file, lock_age );
(void) globus_l_gass_cache_unlink( names->globaldir_lock_file );
/* Go back to the top and try again! */
continue;
}
/* Here, we lost the race. Return ENODATA to the caller. */
globus_libc_usleep( LOCK_SLEEP_USEC );
rc = GLOBUS_L_ENODATA;
break;
}
# endif
/* Do the rename if all is ok */
if ( GLOBUS_SUCCESS == rc )
{
int tmp_rc = GLOBUS_SUCCESS;
rc = globus_l_gass_cache_rename( names->global_data_file,
names->global_uniq_file );
/* Unlock the file (if required) */
# if GLOBUS_L_GASS_CACHE_RENAMEBUG
tmp_rc = globus_l_gass_cache_unlink( names->globaldir_lock_file );
# endif
/* Just call rename.. */
if ( ( GLOBUS_SUCCESS == rc ) && ( GLOBUS_SUCCESS == tmp_rc ) )
{
return GLOBUS_SUCCESS;
}
else if ( GLOBUS_L_ENOENT == rc )
{
RET_ERROR( GLOBUS_L_ENODATA );
}
else
{
RET_ERROR( GLOBUS_L_EOTHER );
}
}
/* Failed to get the lock */
/* Return the error */
RET_ERROR( rc );
} /* globus_l_gass_cache_make_unready() */
/*
* globus_l_gass_cache_wait_ready()
*
* Wait for the global file to become ready.
*
* Parameters:
*
* Returns:
* GLOBUS_SUCCESS
* GLOBUS_L_ENODATA - *Data* file does not exist
* Retuned failures from globus_l_gass_cache_stat()
* Retuned failures from globus_l_gass_cache_unlink()
*
*/
static
int
globus_l_gass_cache_wait_ready( const cache_names_t *names,
unsigned long *timestamp )
{
int rc;
struct stat statbuf;
time_t checktime; /* When should we do a lock check? */
int uniq_count;
struct dirent **uniq_list = NULL;
int uniq_num;
int uniq_num_recent;
time_t cur_time;
time_t dir_age = 0;
time_t TODOmin_age = 9999999;
int cur_statpath_size = 0;
char * cur_statpath;
/* Default to check right away, then we'll wait before further checks */
cur_time = checktime = ( time(NULL) + 0 );
/* Run til we're done */
while( 1 )
{
/* Stat the global directory.. */
rc = globus_l_gass_cache_stat( names->global_dir, &statbuf );
if ( GLOBUS_L_ENOENT == rc )
{
RET_ERROR( GLOBUS_L_ENODATA );
}
else if ( GLOBUS_SUCCESS == rc )
{
dir_age = globus_l_gass_cache_calc_file_age(
NULL, cur_time, statbuf.st_mtime );
}
else
{
RET_ERROR( rc );
}
CLR_ERROR;
/* Stat the global data file. If it's there, we're done. */
rc = globus_l_gass_cache_stat( names->global_data_file, &statbuf );
if ( GLOBUS_SUCCESS == rc )
{
if ( NULL != timestamp )
{
*timestamp = (unsigned long) statbuf.st_mtime;
}
return GLOBUS_SUCCESS;
}
/* If it's not ENOENT, bad... */
else if ( GLOBUS_L_ENOENT != rc )
{
RET_ERROR( rc );
}
CLR_ERROR;
/* Time to check the transfer stats? */
if ( time(NULL) < checktime )
{
sleep( 1 );
continue;
}
/* Update for next time.. */
checktime = ( time(NULL) + NOTREADY_CHECK_SECONDS );
/* Scan the directory; we're looking at *all* of the "uniq" files */
rc = globus_l_gass_cache_scandir(
names->global_dir,
names->cache_type,
&uniq_list,
&uniq_count,
globus_l_gass_cache_scandir_select_uniq );
/* ENOENT means the directory somehow got smoked on us... */
if ( GLOBUS_L_ENOENT == rc )
{
RET_ERROR( GLOBUS_L_ENODATA );
}
/* Something else bad happenned */
else if ( GLOBUS_SUCCESS != rc )
{
RET_ERROR( GLOBUS_GASS_CACHE_ERROR_CAN_NOT_CREATE );
}
/* rc == 0 means no matches... Something's wrong.. */
else if ( 0 == uniq_count )
{
globus_l_gass_cache_scandir_free( uniq_list, uniq_count );
RET_ERRORMSG( GLOBUS_L_ENODATA, names->global_dir );
}
/* Stat them all, check their mtime's */
uniq_num_recent = 0;
cur_time = time( NULL );
cur_statpath = NULL;
for ( uniq_num = 0; uniq_num < uniq_count; uniq_num++ )
{
time_t age; /* Age (seconds) of the file */
/* Fill in the file name buffer */
rc = globus_l_gass_cache_build_filename(
names->global_dir,
names->separator,
uniq_list[uniq_num]->d_name,
NULL,
&cur_statpath_size,
&cur_statpath );
if (rc != GLOBUS_SUCCESS)
{
free( cur_statpath );
globus_l_gass_cache_scandir_free( uniq_list, uniq_count );
RET_ERROR( rc );
}
/* Now, go stat() the file and learn all about it */
rc = globus_l_gass_cache_stat( cur_statpath, &statbuf);
/* If it's gone away, ignore it.. */
if ( GLOBUS_L_ENOENT == rc )
{
CLR_ERROR;
continue;
}
else if ( GLOBUS_SUCCESS != rc )
{
/* BAD */
free( cur_statpath );
globus_l_gass_cache_scandir_free( uniq_list, uniq_count );
RET_ERROR( rc );
}
/* Now, if the file's been modified recently, update our oount */
age = globus_l_gass_cache_calc_file_age( NULL,
cur_time,
statbuf.st_mtime );
if ( age < NOTREADY_MAX_SECONDS )
{
uniq_num_recent++;
}
if ( age < TODOmin_age )
{
TODOmin_age = age;
}
}
/* Done with the list! */
globus_l_gass_cache_scandir_free( uniq_list, uniq_count );
if ( cur_statpath )
{
free( cur_statpath );
}
/*
* If there are none, it means we should give up waiting, and
* take over the download process oursleves.
*/
if ( ( 0 == uniq_num_recent ) && ( dir_age > NOTREADY_MAX_SECONDS ) )
{
CACHE_TRACE4( "WREADY: %d files, none recent, min age = %d"
", dir age = %d", uniq_count, TODOmin_age, dir_age );
RET_ERROR( GLOBUS_L_ETIMEOUT );
}
/* Otherwise, just sleep a bit, then go try the whole thing over.. */
sleep( 1 );
continue;
}
} /* globus_l_gass_cache_wait_ready() */
/*
* globus_l_gass_cache_find_uniq()
*
* Find a uniq file
*
* Parameters:
*
* Returns:
* GLOBUS_SUCCESS
* GLOBUS_L_ENODATA - *Data* file does not exist
* Retuned failures from globus_l_gass_cache_stat()
* Retuned failures from globus_l_gass_cache_unlink()
*
*/
static
int
globus_l_gass_cache_find_uniq( const char *dir,
int cache_type,
char **uniq_file,
int *uniq_count )
{
int rc;
struct dirent **uniq_list = NULL;
/* Scan the directory; we're looking at *all* of the "uniq" files */
*uniq_count = 0;
rc = globus_l_gass_cache_scandir(
dir,
cache_type,
&uniq_list,
uniq_count,
globus_l_gass_cache_scandir_select_uniq );
/* Something else bad happenned */
if ( GLOBUS_SUCCESS != rc )
{
RET_ERROR( rc );
}
/* Count == 0 means no matches... Something's wrong.. */
else if ( 0 == *uniq_count )
{
globus_l_gass_cache_scandir_free( uniq_list, *uniq_count );
RET_ERROR( GLOBUS_L_ENOENT );
}
/* Copy out the name of the first one.. */
*uniq_file = strdup( uniq_list[0]->d_name );
/* Free the list */
globus_l_gass_cache_scandir_free( uniq_list, *uniq_count );
/* Done */
return ( ( NULL == uniq_file ) ?
GLOBUS_GASS_CACHE_ERROR_NO_MEMORY : GLOBUS_SUCCESS );
} /* globus_l_gass_cache_find_uniq() */
/*
* globus_l_gass_cache_lock_delay()
*
* Sleep for LOOP_LOCK_TIME uSeconds, but backs off based on the value of
* tryno, and also has a random factor built in.
*
* Parameters:
*
* tryno : Attempt number
*
* Returns:
*
* # Of uSec acutal slept
*/
static
long
globus_l_gass_cache_lock_delay( int tryno )
{
long usec;
usec = LOCK_SLEEP_USEC + ( random() % LOCK_SLEEP_USEC );
if ( tryno > 5 )
usec >>= 1;
if ( tryno > 10 )
usec >>= 1;
if ( tryno > 100 )
usec >>= 1;
if ( tryno > 1000 )
usec >>= 1;
if ( 0 != usec )
{
globus_libc_usleep( usec );
}
return usec;
}
/* globus_l_gass_cache_lock_delay() */
/*
* globus_l_gass_cache_lock_local_dir()
*
* Lock the local directory
*
* Parameters:
*
* Returns:
*
*/
static
int
globus_l_gass_cache_lock_local_dir( cache_names_t *names,
const char *existing_file )
{
int rc;
struct stat statbuf;
time_t cur_time;
int tryno = 0;
int lock_age;
/* Build the lock file name */
rc = globus_l_gass_cache_build_filename(
names->local_dir,
names->separator,
LOCK_FILE,
NULL,
NULL,
&names->localdir_lock_file );
if ( GLOBUS_SUCCESS != rc )
{
RET_ERROR( rc );
}
/* Run til we're done */
while( 1 )
{
/* Link to the lock file. If it works, we're done */
rc = globus_l_gass_cache_link( existing_file,
names->localdir_lock_file );
if ( GLOBUS_SUCCESS == rc )
{
return GLOBUS_SUCCESS;
}
/* If it's not EEXISTS, bad... */
else if ( GLOBUS_L_EEXISTS != rc )
{
RET_ERROR( rc );
}
CLR_ERROR;
/* EEXISTS; check the lock file time stamp */
rc = globus_l_gass_cache_stat( names->localdir_lock_file, &statbuf );
if ( GLOBUS_L_ENOENT == rc )
{
CLR_ERROR;
continue;
}
else if ( GLOBUS_SUCCESS != rc )
{
RET_ERROR( rc );
}
/* Has the file been modified recently? If not, then probably
the process which is downloading it died */
cur_time = time( NULL );
lock_age = globus_l_gass_cache_calc_file_age( NULL,
cur_time,
statbuf.st_mtime );
if ( lock_age > LOCK_MAX_SECONDS )
{
/* Kill the file and start over */
CACHE_TRACE3( "Breaking lock on tag '%s'; local dir = %s",
names->tag, names->local_dir );
rc = globus_l_gass_cache_unlink( names->localdir_lock_file );
if ( GLOBUS_SUCCESS != rc )
{
RET_ERROR( rc );
}
continue;
}
/* Sleep for a bit, then go back and try again.. */
globus_l_gass_cache_lock_delay( tryno++ );
}
} /* globus_l_gass_cache_lock_local_dir() */
/*
* globus_l_gass_cache_unlock_local_dir()
*
* Unlock the local directory
*
* Parameters:
* names - The standard "names" structure of various file/dir names
*
* target_name - Unlock can rename the existing lock file to a
* target name. Specify NULL to not do this.
*
* Returns:
* GLOBUS_SUCESS
* Values returned from _build_filename(), _unlink(), and _rename()
*
* Notes:
*/
static
int
globus_l_gass_cache_unlock_local_dir( cache_names_t *names,
const char *target_name )
{
int rc = GLOBUS_SUCCESS;
/* Build the lock file name (should be valid already, but doesn't hurt */
rc = globus_l_gass_cache_build_filename(
names->local_dir,
names->separator,
LOCK_FILE,
NULL,
NULL,
&names->localdir_lock_file );
if ( GLOBUS_SUCCESS != rc )
{
RET_ERROR( rc );
}
/* If the target name is valid, rename to it.. */
if ( NULL != target_name )
{
int tmp_rc;
/* rename(a,b) doesn't always work as you would expect. If a
* & b are _hard links_ to each other, it is valid for
* rename(a,b) to return 0 (success), and do _nothing_
* (i.e. leave a and b). For this reason, you see the
* _unlink() followed by _rename() * logic below. */
/* Kill the target file, if it exists */
tmp_rc = globus_l_gass_cache_stat( target_name, NULL );
if ( GLOBUS_SUCCESS == tmp_rc )
{
(void) globus_l_gass_cache_unlink( target_name );
}
CLR_ERROR; /* This is ok! */
/* Now, we should be safe to rename to it (we still have the
* "dir lock". This will also _release_ this lock. */
rc = globus_l_gass_cache_rename( names->localdir_lock_file,
target_name );
if ( GLOBUS_SUCCESS != rc )
{
RET_ERROR( rc );
}
}
/* Otherwise, just kill the lock file */
else
{
rc = globus_l_gass_cache_unlink( names->localdir_lock_file );
if ( GLOBUS_SUCCESS != rc )
{
RET_ERROR( rc );
}
}
/* All ok */
return GLOBUS_SUCCESS;
} /* globus_l_gass_cache_unlock_local_dir() */
/*
* globus_l_gass_cache_create_local_tag_file()
*
* Create the local "Tag" file
* Note: Currently we're not checking the tag for match
*
* Parameters:
*
* Returns:
* GLOBUS_SUCCESS
* Errors returned by _build_filename(), _create(), _link()
*/
static
int
globus_l_gass_cache_create_local_tag_file( cache_names_t *names )
{
int rc = GLOBUS_SUCCESS;
char *uniq_filename = NULL;
/* Build "uniq" file name */
rc = globus_l_gass_cache_build_filename(
names->tmp_root,
names->separator,
TAG_FILE,
names->uniq,
NULL,
&uniq_filename );
if ( GLOBUS_SUCCESS != rc )
{
RET_ERROR( rc );
}
/* Write the file */
rc = globus_l_gass_cache_create(
uniq_filename, names->local_dir, GLOBUS_L_GASS_CACHE_TAGFILE_MODE,
names->tag, strlen( names->tag ) );
if ( GLOBUS_SUCCESS != rc )
{
free( uniq_filename );
RET_ERROR( rc );
}
/* Create the real tag file by linking to this temp one.. */
rc = globus_l_gass_cache_link( uniq_filename, names->local_tag_file );
(void) globus_l_gass_cache_unlink( uniq_filename );
free( uniq_filename );
if ( ( GLOBUS_SUCCESS != rc ) && ( GLOBUS_L_EEXISTS != rc ) )
{
RET_ERROR( rc );
}
/* Link it to the "tag link" file, which is down in the URL subdir */
rc = globus_l_gass_cache_link( names->local_tag_file,
names->local_tag_link );
if ( ( GLOBUS_SUCCESS != rc ) && ( GLOBUS_L_EEXISTS != rc ) )
{
RET_ERROR( rc );
}
/* All ok! */
return GLOBUS_SUCCESS;
} /* globus_l_gass_cache_create_local_tag_file() */
/*
* globus_l_gass_cache_make_local_file()
*
* Build a cache filename; can be used to generate either the global
* or local file names. Pass tag=NULL for global.
*
* Parameters:
*
* Returns:
*
*/
static
int
globus_l_gass_cache_make_local_file( cache_names_t *names,
const char *global_name,
const char *global_uniq )
{
int rc = GLOBUS_SUCCESS;
char *global_file = NULL;
/* Build the uniq file name */
if ( GLOBUS_SUCCESS == rc )
{
rc = globus_l_gass_cache_build_filename(
names->local_dir,
names->separator,
UDATA_FILE,
names->uniq,
NULL,
&names->local_uniq_file );
if ( GLOBUS_SUCCESS != rc )
{
RET_ERROR( rc );
}
}
/* Make sure the local directory exists. */
rc = globus_l_gass_cache_make_dirtree(names->local_dir, names->cache_type);
if ( GLOBUS_SUCCESS != rc )
{
RET_ERROR( rc );
}
/* Create the "tag" file... */
rc = globus_l_gass_cache_create_local_tag_file( names );
if ( GLOBUS_SUCCESS != rc )
{
RET_ERROR( rc );
}
/* Build the name of the global file..
* Note: We're using our own "global_file" here, and it must
* be freed up before returning */
rc = globus_l_gass_cache_build_filename(
names->global_dir,
names->separator,
global_name,
global_uniq,
NULL,
&global_file );
if ( GLOBUS_SUCCESS != rc )
{
RET_ERROR( rc );
}
/* Link the global file to the local file. */
rc = globus_l_gass_cache_link( global_file, names->local_data_file );
/* We're done with the gloal file buffer, so free it right away. */
free( global_file );
/* Now, check the return status of the _link() call above */
if ( GLOBUS_L_ENOENT == rc )
{
RET_ERROR( GLOBUS_L_ENODATA );
}
else if ( GLOBUS_SUCCESS == rc )
{
/* Create the tag file */
if ( GLOBUS_SUCCESS != rc )
{
RET_ERROR( rc );
}
}
else if ( GLOBUS_L_EEXISTS == rc )
{
CLR_ERROR; /* This is ok! */
}
else
{
/* Badness 10000 */
RET_ERROR( rc );
}
/* Get a lock on the local directory (use data to link from; its easy) */
rc = globus_l_gass_cache_lock_local_dir( names, names->local_data_file );
if ( GLOBUS_SUCCESS != rc )
{
/* Failed to get lock for some reason. Bail out. */
RET_ERROR( rc );
}
/* Release the lock & create a uniq "link count" file in one op */
rc = globus_l_gass_cache_unlock_local_dir( names,
names->local_uniq_file );
if ( GLOBUS_SUCCESS != rc )
{
RET_ERROR( rc );
}
return rc;
} /* globus_l_gass_cache_make_local_file() */
/*
* globus_l_gass_cache_unlink_local()
*
* Build a cache filename; can be used to generate either the global
* or local file names. Pass tag=NULL for global.
*
* Parameters:
*
* Returns:
* GLOBUS_L_CACHE_UNLINK_LAST
*
*/
static
int
globus_l_gass_cache_unlink_local( cache_names_t *names )
{
char *uniq_file = NULL;
int uniq_count;
int rc = GLOBUS_SUCCESS;
/* Lock the target tag directory */
rc = globus_l_gass_cache_lock_local_dir( names, names->local_data_file );
if ( GLOBUS_SUCCESS != rc )
{
RET_ERROR( rc );
}
/* Find a uniq file to kill... */
rc = globus_l_gass_cache_find_uniq( names->local_dir,
names->cache_type,
&uniq_file,
&uniq_count );
if ( GLOBUS_SUCCESS != rc )
{
(void) globus_l_gass_cache_unlock_local_dir( names, NULL );
RET_ERROR( rc );
}
/* Kill the uniq file */
if ( uniq_count > 0 )
{
char *uniq_filepath = NULL;
rc = globus_l_gass_cache_build_filename(
names->local_dir,
names->separator,
uniq_file,
NULL,
NULL,
&uniq_filepath );
free( uniq_file ); /* We're done with this */
uniq_file = NULL;
if ( GLOBUS_SUCCESS != rc )
{
(void) globus_l_gass_cache_unlock_local_dir( names, NULL );
RET_ERROR( rc );
}
rc = globus_l_gass_cache_unlink( uniq_filepath );
free( uniq_filepath ); /* We're done with this now */
if ( GLOBUS_SUCCESS != rc )
{
(void) globus_l_gass_cache_unlock_local_dir( names, NULL );
RET_ERROR( rc );
}
}
/* Free up the buffer for the uniq file. */
if ( NULL != uniq_file )
{
free( uniq_file );
}
/* If this is the last one, we can do some bigger cleanups... */
if ( 1 == uniq_count )
{
rc = globus_l_gass_cache_unlink( names->local_data_file );
if ( ( GLOBUS_SUCCESS != rc ) &&
( GLOBUS_L_ENOENT != rc ) )
{
(void) globus_l_gass_cache_unlock_local_dir( names, NULL );
RET_ERROR( rc );
}
CLR_ERROR;
}
/* Release the lock */
rc = globus_l_gass_cache_unlock_local_dir( names, NULL );
if ( ( GLOBUS_SUCCESS != rc ) && ( GLOBUS_L_ENOENT != rc ) )
{
RET_ERROR( rc );
}
CLR_ERROR;
/* Finally, cleanup the directory tree... */
if ( 1 == uniq_count )
{
struct stat statbuf;
/* Kill the local data file */
rc = globus_l_gass_cache_unlink( names->local_data_file );
if ( ( GLOBUS_SUCCESS != rc ) &&
( GLOBUS_L_ENOENT != rc ) )
{
RET_ERROR( rc );
}
/* Kill the URL under the local tag dir (if empty) */
rc = globus_l_gass_cache_remove_dirtree( names,
names->local_root,
names->local_mangle_root,
names->local_dir );
/* >0 is ok: remove_dirtree() is oportunistic... */
if ( rc < 0 )
{
RET_ERROR( GLOBUS_GASS_CACHE_ERROR_CAN_NOT_DELETE_DATA_F );
}
/* Kill the tag *link* file */
/* It's ok if somebody else beat us to it */
(void) globus_l_gass_cache_unlink( names->local_tag_link );
/* Force the NFS server to update its cache info */
utimes( names->local_tag_file, NULL );
/* Now, stat the *main* tag file, see how many links it has */
/* It's ok if somebody else killed it before us! */
rc = globus_l_gass_cache_stat( names->local_tag_file, &statbuf );
if ( GLOBUS_SUCCESS == rc )
{
if ( 1 == statbuf.st_nlink )
{
(void) globus_l_gass_cache_unlink( names->local_tag_file );
}
}
/* Finally, try to kill the rest of the tree... */
rc = globus_l_gass_cache_remove_dirtree( names,
names->local_root,
names->local_mangle_root,
names->local_dir );
/* >0 is ok: remove_dirtree() is oportunistic... */
if ( rc < 0 )
{
RET_ERROR( GLOBUS_GASS_CACHE_ERROR_CAN_NOT_DELETE_DATA_F );
}
/* Tell the caller that we've removed the last one.. */
return GLOBUS_L_UNLINK_LAST;
}
return GLOBUS_SUCCESS;
} /* globus_l_gass_cache_unlink_local() */
/*
* globus_l_gass_cache_unlink_global()
*
* Unlink the global data & URL file, remove the global directory if empty.
*
* Parameters:
* names - File & dir name
*
* Returns:
* GLOBUS_SUCCESS
* GLOBUS_L_CACHE_UNLINK_LAST
* Errors returned by _stat(), _unlink(), _remove_dirtree()
*
*/
static
int
globus_l_gass_cache_unlink_global( cache_names_t *names,
globus_bool_t is_locked )
{
struct stat statbuf;
const char *global_file;
int rc = GLOBUS_SUCCESS;
/* Stat the global file (or uniq file if _we_ have it locked) */
if ( is_locked )
{
/* Make sure that the uniq file name is valid */
rc = globus_l_gass_cache_build_filename(
names->global_dir,
names->separator,
UDATA_FILE,
names->uniq,
NULL,
&names->global_uniq_file );
if ( GLOBUS_SUCCESS != rc )
{
RET_ERROR( rc );
}
global_file = names->global_uniq_file;
}
else
{
global_file = names->global_data_file;
}
utimes(global_file, NULL);
rc = globus_l_gass_cache_stat( global_file, &statbuf );
/* ENOENT here means that it was killed by another process, ok */
if ( GLOBUS_L_ENOENT == rc )
{
/* Other proc's shoulded kill _our_ uniq file!! */
if ( is_locked )
{
RET_ERROR( GLOBUS_L_ENOENT );
}
/* But, it's ok to kill the _global_ (shared) data file */
else
{
return GLOBUS_SUCCESS;
}
}
/* Something else went wrong! */
else if ( GLOBUS_SUCCESS != rc )
{
RET_ERROR( rc );
}
/* If it's still in use, nothing to do! */
if ( statbuf.st_nlink > 1 )
{
return GLOBUS_SUCCESS;
}
/* Kill it */
(void) globus_l_gass_cache_unlink( names->global_url_file );
rc = globus_l_gass_cache_unlink( global_file );
if ( ( GLOBUS_SUCCESS != rc ) && ( GLOBUS_L_ENOENT != rc ) )
{
RET_ERROR( rc );
}
/* And, clean up the dirtree.. */
rc = globus_l_gass_cache_remove_dirtree( names,
names->global_root,
names->global_mangle_root,
names->global_dir );
if ( rc < 0 )
{
RET_ERROR( rc );
}
/* Tell the caller that we've removed the last one.. */
return GLOBUS_L_UNLINK_LAST;
} /* globus_l_gass_cache_unlink_global() */
static
int
globus_l_gass_cache_list_all_urls_flat( globus_gass_cache_t cache_handle,
const char * search_dir,
url_list_head_t * url_list )
{
int rc;
int dirent_count;
int dirent_num;
struct dirent **dirent_list = NULL;
globus_hashtable_t table;
int table_size = 16;
/* Scan the directory; we're looking at *all* of the "uniq" files */
rc = globus_l_gass_cache_scandir(
search_dir,
cache_handle->cache_type,
&dirent_list,
&dirent_count,
globus_l_gass_cache_scandir_select_all );
/* Something bad happened */
if ( rc < 0 )
{
CACHE_TRACE2( "SCAN: Error scanning '%s'", search_dir );
RET_ERROR( rc );
}
/* No matches? Hmmmm.. Skip it for now */
else if ( 0 == dirent_count )
{
globus_l_gass_cache_scandir_free( dirent_list, dirent_count );
RET_ERROR( GLOBUS_L_ENOENT );
}
while (dirent_count > table_size)
{
table_size *= 2;
}
rc = globus_hashtable_init( &table, table_size,
globus_hashtable_string_hash,
globus_hashtable_string_keyeq );
if ( rc != GLOBUS_SUCCESS )
{
CACHE_TRACE2( "SCAN: hashtable error, dir = '%s'", search_dir );
globus_l_gass_cache_scandir_free( dirent_list, dirent_count );
RET_ERROR( rc );
}
/* Walk through the matches... */
for ( dirent_num = 0; dirent_num < dirent_count; dirent_num++ )
{
const char *name = dirent_list[dirent_num]->d_name;
char *p;
url_list_elem_t *elem;
/* is it a "data." file? */
if ( ( p = strstr( name, UDATA_FILE_PAT ) ) == NULL )
{
continue;
}
/* remove the {separator}data.* part of the file.
'name' now holds the mangled url!!! */
if ( p > name)
{
*(p-1) = '\0';
}
/* any hits on this mangled url before? */
elem = (url_list_elem_t*) globus_hashtable_lookup(&table, (void*)name);
if (elem)
{
elem->data_count++;
}
else
{
/* add new entry to the list and the hashtable */
elem = (url_list_elem_t*) malloc(sizeof(url_list_elem_t));
if (!elem)
{
CACHE_TRACE( "list_all_files: malloc failed" );
globus_hashtable_destroy(&table);
globus_l_gass_cache_scandir_free( dirent_list, dirent_count );
RET_ERROR( GLOBUS_GASS_CACHE_ERROR_NO_MEMORY );
}
elem->mangled = strdup(name);
elem->data_count = 1;
elem->next = url_list->head;
globus_hashtable_insert(&table, elem->mangled, elem);
url_list->head = elem;
url_list->count++;
}
}
/* Free up stuff */
globus_hashtable_destroy(&table);
globus_l_gass_cache_scandir_free( dirent_list, dirent_count );
return GLOBUS_SUCCESS;
} /* globus_l_gass_cache_list_all_urls_flat() */
/*
* globus_l_gass_cache_list_all_urls()
*
* Build a cache filename; can be used to generate either the global
* or local file names. Pass tag=NULL for global.
*
* Parameters:
*
* Returns:
* - GLOBUS_SUCCESS
* - GLOBUS_L_ENOTUNIQ - We didn't win the race
* - Returned failures from globus_l_gass_cache_creat()
* - Returned failures from globus_l_gass_cache_link()
* - Returned failures from globus_l_gass_cache_unlink()
*/
static
int
globus_l_gass_cache_list_all_urls( globus_gass_cache_t cache_handle,
const char *search_dir,
const char *base_mangled,
url_list_head_t *url_list )
{
int rc;
int dirent_count;
int dirent_num;
struct dirent **dirent_list = NULL;
struct stat statbuf;
int data_count = 0;
char *separator;
if (cache_handle->cache_type==DIRECTORY_TYPE_FLAT)
{
return globus_l_gass_cache_list_all_urls_flat(cache_handle,
search_dir,
url_list);
}
separator = directory_separator[cache_handle->cache_type];
/* Scan the directory; we're looking at *all* of the "uniq" files */
rc = globus_l_gass_cache_scandir(
search_dir,
cache_handle->cache_type,
&dirent_list,
&dirent_count,
globus_l_gass_cache_scandir_select_all );
/* Something bad happenned */
if ( rc < 0 )
{
CACHE_TRACE2( "SCAN: Error scanning '%s'", search_dir );
RET_ERROR( rc );
}
/* rc == 0 means no matches. Hmmmm.. Skip it for now */
else if ( 0 == dirent_count )
{
globus_l_gass_cache_scandir_free( dirent_list, dirent_count );
RET_ERROR( GLOBUS_L_ENOENT );
}
/* Just . & .. ? */
else if ( dirent_count <= 2 )
{
globus_l_gass_cache_scandir_free( dirent_list, dirent_count );
return GLOBUS_SUCCESS;
}
/* Walk through the matches... */
for ( dirent_num = 0; dirent_num < dirent_count; dirent_num++ )
{
const char *name = dirent_list[dirent_num]->d_name;
char name_path[PATH_MAX+1];
/* Build it's full path */
strcpy( name_path, search_dir );
strcat( name_path, separator );
strcat( name_path, name );
/* Stat it to find out what it is... */
rc = globus_l_gass_cache_stat( name_path, &statbuf );
/* Errors?! */
if ( GLOBUS_SUCCESS != rc )
{
globus_l_gass_cache_scandir_free( dirent_list, dirent_count );
CACHE_TRACE2( "SCAN: Can't stat '%s'", name_path );
RET_ERROR( rc );
}
/* If it's a file, this must be a URL; mark it & go on */
if ( ! S_ISDIR( statbuf.st_mode ) )
{
/* If it's a "uniq" data file ("data.*"), count it */
if ( ! strncmp( name, UDATA_FILE_PAT, UDATA_FILE_PAT_LEN ) )
{
data_count++;
}
/* Otherwise, ignore it (probably a URL or tag file) */
}
/* Ignore "." and "..", but process all other dirs */
else if ( ( strcmp ( name, "." ) ) && ( strcmp( name, ".." ) ) )
{
char new_mangled[ PATH_MAX + 1 ];
strcpy( new_mangled, base_mangled );
if ( new_mangled[0] != '\0' )
{
strcat( new_mangled, "/" );
}
strcat( new_mangled, name );
rc = globus_l_gass_cache_list_all_urls( cache_handle,
name_path,
new_mangled,
url_list );
if ( GLOBUS_SUCCESS != rc )
{
RET_ERROR( rc );
}
}
}
/* Free up the dirent list */
globus_l_gass_cache_scandir_free( dirent_list, dirent_count );
/* Add myself to the list */
if ( data_count )
{
url_list_elem_t *new_url_elem =
malloc( sizeof( url_list_elem_t ) );
if ( NULL == new_url_elem )
{
CACHE_TRACE( "list_all_files: malloc failed" );
RET_ERROR( GLOBUS_GASS_CACHE_ERROR_NO_MEMORY );
}
else
{
new_url_elem->mangled = strdup( base_mangled );
new_url_elem->data_count = data_count;
new_url_elem->next = url_list->head;
url_list->head = new_url_elem;
url_list->count++;
}
}
return GLOBUS_SUCCESS;
} /* globus_l_gass_cache_list_all_urls() */
/*
* globus_l_gass_cache_delete()
*
* Build a cache filename; can be used to generate either the global
* or local file names. Pass tag=NULL for global.
*
* Parameters:
*
* Returns:
* - GLOBUS_SUCCESS
* - GLOBUS_L_ENOTUNIQ - We didn't win the race
* - Returned failures from globus_l_gass_cache_creat()
* - Returned failures from globus_l_gass_cache_link()
* - Returned failures from globus_l_gass_cache_unlink()
*/
static
int
globus_l_gass_cache_delete( cache_names_t *names,
const unsigned long *timestamp,
globus_bool_t is_locked )
{
int rc = GLOBUS_SUCCESS; /* Temp return code */
/* Wait for the file to become "ready" */
if ( ! is_locked )
{
rc = globus_l_gass_cache_wait_ready( names, NULL );
/* It got blown away by somebody else */
if ( ( GLOBUS_L_ENODATA == rc ) || ( GLOBUS_L_ENOENT == rc ) )
{
return GLOBUS_SUCCESS; /* Nothing to do */
}
else if ( GLOBUS_SUCCESS != rc )
{
RET_ERROR( rc );
}
}
/* Cleanup the local */
rc = globus_l_gass_cache_unlink_local( names );
if ( GLOBUS_L_UNLINK_LAST == rc )
{
rc = globus_l_gass_cache_unlink_global( names, is_locked );
if ( GLOBUS_L_UNLINK_LAST == rc )
{
/* Do nothing */
}
else if ( GLOBUS_SUCCESS != rc )
{
RET_ERROR( rc );
}
}
/* Set the file's timestamp (if the we're passed one) */
else if ( ( GLOBUS_SUCCESS == rc ) && ( NULL != timestamp ) )
{
rc = globus_l_gass_cache_set_timestamp(
names->global_data_file, *timestamp );
if ( GLOBUS_SUCCESS != rc )
{
RET_ERROR( rc );
}
}
/* Log errors from unlink_local */
else if ( GLOBUS_SUCCESS != rc )
{
RET_ERROR( rc );
}
/* Done */
return GLOBUS_SUCCESS;
} /* globus_l_gass_cache_delete() */
/******************************************************************************
PUBLIC FUNCTIONS
******************************************************************************/
/**
* @brief Open a GASS Cache
* @ingroup globus_gass_cache
* @details
* Open the cache specified by the cache_directory_path argument, and return
* a cache handle that can be used in subsequent cache calls.
*
* If cache_directory_path is NULL, then use the value contained in the
* GLOBUS_GASS_CACHE_DEFAULT environment variable if it is defined,
* otherwise use ~/.globus_gass_cache.
*
* The cache_directory_path must be a directory. If it is a file, this call
* will fail with a non-0 return value.
*
* If the specified directory does not exist, then this call will create the
* directory.
*
* @param cache_directory_path
* Path to the cache directory to open. Can be NULL (see above)
* @param cache_handlep
* Structure containning all the necessary
* information to access the cache (file names, descriptor,...)
* (see globus_gass_gache.h) Some files are also opened:
* globus_gass_cache_close() must be called subsequently to close those
* files. This parameter is modified by the globus_gass_cache_open()
*
* @retval GLOBUS_SUCCESS
* Success
* @retval GLOBUS_GASS_CACHE_ERROR_CACHE_ALREADY_OPENED
* Handle already opened
* @retval GLOBUS_GASS_CACHE_ERROR_NAME_TOO_LONG
* Path name too long
* @retval GLOBUS_GASS_CACHE_ERROR_NO_HOME
* Can't find home directory for default cache path
*/
int
globus_gass_cache_open(const char *cache_directory_path,
globus_gass_cache_t *cache_handlep)
{
int rc; /* general purpose returned code */
char * pt; /* general purpose returned pointer */
int f_name_length = 0; /* to verify len of the file names */
char f_name[PATH_MAX+1]; /* path name of the 3 files to open */
# if defined GLOBUS_L_GASS_CACHE_LOG
char log_f_name[PATH_MAX+1]; /* log file file name */
# endif
char *uniq;
char homedir[PATH_MAX];
char *separator;
globus_l_gass_cache_config_t config;
unsigned write_config = 0x0;
globus_i_gass_cache_t * cache_handle;
# define WRITE_CONFIG_TYPE 0x01
# define WRITE_CONFIG_LEVELS 0x02
if (cache_handlep == NULL)
{
return GLOBUS_GASS_CACHE_ERROR_NO_MEMORY;
}
(*cache_handlep) = cache_handle = calloc(1, sizeof(globus_i_gass_cache_t));
if (cache_handle == NULL)
{
return GLOBUS_GASS_CACHE_ERROR_NO_MEMORY;
}
CHECK_CACHE_IS_NOT_INIT(cache_handle);
CLR_ERROR;
/* Random initialize */
{
struct timeval tv;
gettimeofday( &tv, NULL );
srandom( tv.tv_usec );
}
/* look for the correct directory path */
/* if cache_directory_path empty (""), behave as if NULL */
if ( cache_directory_path != NULL)
{
f_name_length=strlen(cache_directory_path);
if ( f_name_length == 0 )
{
cache_directory_path = NULL;
CACHE_TRACE("Error: cache_directory_path empty");
}
}
/* if cache_directory_path empty, read it from GLOBUS_GASS_CACHE_DEFAULT */
if ( cache_directory_path == NULL )
{
pt = getenv(GLOBUS_L_GASS_CACHE_DEFAULT_DIR_ENV_VAR);
/* if GLOBUS_GASS_CACHE_DEFAULT empty (""),
behave as if not defined */
if ( pt && (0 == (f_name_length = strlen(pt))) )
{
pt = NULL;
}
if ( NULL == pt )
{
GLOBUS_L_GASS_CACHE_LG2( "'%s' is empty",
GLOBUS_L_GASS_CACHE_DEFAULT_DIR_ENV_VAR );
/* cache directory still not defined; use the defaults */
/* "$HOME/.globus_gass_cache" */
if ( globus_libc_gethomedir(homedir,PATH_MAX) == GLOBUS_SUCCESS)
{
f_name_length=strlen(homedir);
if ( f_name_length > 0)
pt = homedir;
}
if ( NULL == pt )
{
/* $HOME not defined or null ! this should not happen */
LOG_ERROR(0);
return ( GLOBUS_GASS_CACHE_ERROR_NO_HOME );
}
if ((f_name_length +
strlen(GLOBUS_L_GASS_CACHE_DEFAULT_DIR_NAME))>=PATH_MAX)
{
CACHE_TRACE("ENAMETOOLONG");
printf("NAMETOOLONG: f_name_length: %d, "
"default_name_length: %d, filename_max: %d\n",
f_name_length,
(int) strlen(GLOBUS_L_GASS_CACHE_DEFAULT_DIR_NAME),
PATH_MAX);
LOG_ERROR(0);
return ( GLOBUS_GASS_CACHE_ERROR_NAME_TOO_LONG);
}
/* Allocate a buffer for & build the base directory */
cache_handle->cache_directory_path = (char * ) malloc(
1 +
strlen( pt ) +
strlen( GLOBUS_L_DOT_GLOBUS_DIR_NAME ) +
strlen( GLOBUS_L_GASS_CACHE_DEFAULT_DIR_NAME) );
if ( NULL == cache_handle->cache_directory_path )
{
LOG_ERROR( GLOBUS_GASS_CACHE_ERROR_NO_MEMORY );
return GLOBUS_GASS_CACHE_ERROR_NO_MEMORY;
}
strcpy( cache_handle->cache_directory_path, pt );
strcat( cache_handle->cache_directory_path,
GLOBUS_L_DOT_GLOBUS_DIR_NAME );
strcat(cache_handle->cache_directory_path,
GLOBUS_L_GASS_CACHE_DEFAULT_DIR_NAME);
cache_handle->global_dir_len =
strlen( cache_handle->cache_directory_path );
/* Build & verify the whole tree */
rc = globus_l_gass_cache_make_dirtree(
cache_handle->cache_directory_path,
DIRECTORY_TYPE_NORMAL);
if ( GLOBUS_L_ENOTDIR == rc )
{
CACHE_TRACE( " .globus is not a directory" );
LOG_ERROR(rc);
return GLOBUS_GASS_CACHE_ERROR_CAN_NOT_CREATE;
}
else if ( GLOBUS_SUCCESS != rc )
{
CACHE_TRACE( "Could not create the .globus directory" );
LOG_ERROR(rc);
return GLOBUS_GASS_CACHE_ERROR_CAN_NOT_CREATE;
}
}
else /* *pt is not null or empty */
{
if (f_name_length >= PATH_MAX)
{
CACHE_TRACE("ENAMETOOLONG");
LOG_ERROR(0);
return ( GLOBUS_GASS_CACHE_ERROR_NAME_TOO_LONG);
}
cache_handle->cache_directory_path = strdup( pt );
if ( NULL == cache_handle->cache_directory_path )
{
LOG_ERROR( GLOBUS_GASS_CACHE_ERROR_NO_MEMORY );
return GLOBUS_GASS_CACHE_ERROR_NO_MEMORY;
}
}
}
else /* cache_directory_path is valid */
{
/* For the first version, we do not accept a cache_directory_path */
{
/* for the version which will accept a cache directory not null */
if (f_name_length >= PATH_MAX)
{
CACHE_TRACE("ENAMETOOLONG");
LOG_ERROR(0);
return ( GLOBUS_GASS_CACHE_ERROR_NAME_TOO_LONG);
}
cache_handle->cache_directory_path =
strdup( cache_directory_path );
if ( NULL == cache_handle->cache_directory_path )
{
LOG_ERROR( GLOBUS_GASS_CACHE_ERROR_NO_MEMORY );
return GLOBUS_GASS_CACHE_ERROR_NO_MEMORY;
}
}
}
GLOBUS_L_GASS_CACHE_LG2(" cache directory :%s ",
cache_handle->cache_directory_path );
/* here, *cache_handle.cache_directory_path should be defined */
/* let see if it exists, and create it if it does not */
rc = globus_l_gass_cache_make_dirtree(
cache_handle->cache_directory_path,
DIRECTORY_TYPE_NORMAL);
if ( GLOBUS_L_ENOTDIR == rc )
{
CACHE_TRACE( " The cache directory is not a directory" );
LOG_ERROR(rc);
return GLOBUS_GASS_CACHE_ERROR_CAN_NOT_CREATE;
}
else if ( GLOBUS_SUCCESS != rc )
{
CACHE_TRACE( "Could not create the cache directory" );
LOG_ERROR(rc);
return GLOBUS_GASS_CACHE_ERROR_CAN_NOT_CREATE;
}
/* here I suppose cache_directory_path existe and is a directory */
if ( ( f_name_length + MAX_FILENAME_LEN ) >= PATH_MAX )
{
CACHE_TRACE("ENAMETOOLONG");
LOG_ERROR(0);
return ( GLOBUS_GASS_CACHE_ERROR_NAME_TOO_LONG);
}
/* read config file... */
strcpy(f_name,cache_handle->cache_directory_path);
strcat(f_name,GLOBUS_L_GASS_CACHE_CONFIG_FILE);
cache_handle->cache_type = -1;
cache_handle->directory_levels = -1;
if (globus_l_gass_cache_config_init(f_name, &config) == GLOBUS_SUCCESS)
{
int i;
char* value = globus_l_gass_cache_config_get(
&config,
GLOBUS_L_GASS_CACHE_CONFIG_KEY_TYPE);
for (i=0; value && directory_type_values[i]; i++)
{
if (strcmp(directory_type_values[i], value)==0)
{
cache_handle->cache_type = i;
break;
}
}
/* Get the # of levels */
value = globus_l_gass_cache_config_get(
&config,
GLOBUS_L_GASS_CACHE_CONFIG_KEY_LEVELS);
if ( ( NULL != value ) && ( isdigit ( *value ) ) )
{
int levels;
levels = atoi( value );
if ( levels <= GLOBUS_L_GASS_CACHE_MAX_LEVELS )
{
cache_handle->directory_levels = levels;
}
}
globus_l_gass_cache_config_destroy(&config);
}
/* Create the global directory name & path. We're doing this
* before the below test because we're useing it to detect an
* existing cache. */
rc = globus_l_gass_cache_build_filename(
cache_handle->cache_directory_path,
"/", /* always */
GLOBUS_L_GASS_CACHE_GLOBAL_DIR,
NULL,
NULL,
&cache_handle->global_directory_path );
if ( GLOBUS_SUCCESS != rc )
{
LOG_ERROR( rc );
return rc;
}
/* Set the default # of levels */
/* If the global directory exists, assume old settings */
if ( cache_handle->directory_levels < 0 )
{
rc = globus_l_gass_cache_stat( cache_handle->global_directory_path,
NULL );
/* If stat ok, use the "old" value, otherwise the default */
cache_handle->directory_levels =
( ( GLOBUS_SUCCESS == rc ) ?
GLOBUS_L_GASS_CACHE_DEFAULT_LEVELS_OLD :
GLOBUS_L_GASS_CACHE_DEFAULT_LEVELS );
/* Force a config write... */
write_config |= WRITE_CONFIG_LEVELS;
}
/* if no config, perform link test */
if ( cache_handle->cache_type < 0 )
{
write_config |= WRITE_CONFIG_TYPE;
/* find out what type by performing a link test */
cache_handle->cache_type = globus_l_gass_cache_linktest( cache_handle );
if (cache_handle->cache_type == DIRECTORY_TYPE_NOLINK)
{
rc = GLOBUS_GASS_CACHE_ERROR_CAN_NOT_CREATE;
return rc;
}
}
/* Save the config settings */
if ( write_config )
{
FILE* fp;
/* save these settings */
fp = fopen(f_name, "a");
if (fp)
{
if ( write_config & WRITE_CONFIG_TYPE )
{
fprintf(
fp,
"%s=%s\n",
GLOBUS_L_GASS_CACHE_CONFIG_KEY_TYPE,
directory_type_values[cache_handle->cache_type]);
}
if ( write_config & WRITE_CONFIG_LEVELS )
{
fprintf(
fp,
"%s=%d\n",
GLOBUS_L_GASS_CACHE_CONFIG_KEY_LEVELS,
cache_handle->directory_levels );
}
}
if (fp)
{
fclose(fp);
}
}
separator = directory_separator[cache_handle->cache_type];
/* open the log file and log some information */
# if defined GLOBUS_L_GASS_CACHE_LOG
{
char tmp[20];
strcpy( log_f_name, cache_handle->cache_directory_path );
strcat( log_f_name, separator );
strcat( log_f_name, GLOBUS_L_GASS_CACHE_LOG_DIR );
rc = globus_l_gass_cache_make_dirtree( log_f_name,
DIRECTORY_TYPE_NORMAL);
if ( GLOBUS_SUCCESS != rc )
{
CACHE_TRACE("Could NOT open or create the log directory");
}
else
{
sprintf( tmp, "%s%05d", separator, getpid() );
strcat( log_f_name, tmp );
cache_handle->log_FILE = fopen( log_f_name, "a" );
if ( cache_handle->log_FILE == NULL )
{
CACHE_TRACE("Could NOT open or create the log file");
}
cache_handle->log_file_name = strdup( log_f_name );
if ( cache_handle->log_file_name == NULL )
{
CACHE_TRACE("Could NOT copy log file name");
}
}
}
# endif
/* Now, create the global directory (after the above test). Note
* that the directory path is built above, before the config
* tests */
rc = globus_l_gass_cache_make_dirtree(
cache_handle->global_directory_path,
cache_handle->cache_type);
if ( GLOBUS_SUCCESS != rc )
{
CACHE_TRACE( "Can't create the global cache directory" );
LOG_ERROR(0);
return ( GLOBUS_GASS_CACHE_ERROR_NAME_TOO_LONG);
}
/* Create the local directory name & path */
rc = globus_l_gass_cache_build_filename(
cache_handle->cache_directory_path,
"/", /* always */
GLOBUS_L_GASS_CACHE_LOCAL_DIR,
NULL,
NULL,
&cache_handle->local_directory_path );
if ( GLOBUS_SUCCESS != rc )
{
LOG_ERROR( rc );
return rc;
}
rc = globus_l_gass_cache_make_dirtree(
cache_handle->local_directory_path,
cache_handle->cache_type );
if ( GLOBUS_SUCCESS != rc )
{
CACHE_TRACE( "Can't create the local cache directory" );
LOG_ERROR(0);
return ( GLOBUS_GASS_CACHE_ERROR_NAME_TOO_LONG);
}
/* Create the tmp directory name & path */
rc = globus_l_gass_cache_build_filename(
cache_handle->cache_directory_path,
"/", /* always */
GLOBUS_L_GASS_CACHE_TMP_DIR,
NULL,
NULL,
&cache_handle->tmp_directory_path );
if ( GLOBUS_SUCCESS != rc )
{
LOG_ERROR( rc );
return rc;
}
rc = globus_l_gass_cache_make_dirtree(
cache_handle->tmp_directory_path,
cache_handle->cache_type );
if ( GLOBUS_SUCCESS != rc )
{
CACHE_TRACE( "Can't create the tmp cache directory" );
LOG_ERROR(0);
return ( GLOBUS_GASS_CACHE_ERROR_NAME_TOO_LONG);
}
/* Finally, initialize the skew calc stuff */
rc = globus_l_gass_cache_build_uniqname( &uniq );
if ( GLOBUS_SUCCESS == rc )
{
char *skew_file = NULL;
rc = globus_l_gass_cache_build_filename(
cache_handle->tmp_directory_path,
separator,
NULL,
uniq,
NULL,
&skew_file );
free( uniq );
if ( GLOBUS_SUCCESS != rc )
{
LOG_ERROR( GLOBUS_GASS_CACHE_ERROR_NO_MEMORY );
return GLOBUS_GASS_CACHE_ERROR_NO_MEMORY;
}
(void) globus_l_gass_cache_calc_file_age( skew_file,
time(NULL),
time(NULL) );
free( skew_file );
}
else
{
LOG_ERROR( rc );
return rc;
}
/* Max mangled lengths */
{
/* Just take the longest & tack on some "buffer"... */
int max_dirlen = strlen( cache_handle->global_directory_path ) + 8;
#ifdef _WIN32
int max_len = PATH_MAX - max_dirlen;
#else
int max_len = _POSIX_PATH_MAX - max_dirlen;
#endif
cache_handle->max_mangled_url = max_len / 2;
cache_handle->max_mangled_tag = max_len / 2;
/* Default to enable all mangling options. */
cache_handle->mangling_options = MANGLING_OPTION_DEFAULT;
}
/* Lastly, note that we are initialized. */
cache_handle->init = &globus_l_gass_cache_is_init;
/* Done */
return GLOBUS_SUCCESS;
/* Cleanup the namespace... */
# undef WRITE_CONFIG_TYPE
# undef WRITE_CONFIG_LEVELS
}
/* globus_gass_cache_open() */
/**
* @brief Close a cache handle
* @ingroup globus_gass_cache
* @details
* Close (NOT delete) a previously opened cache:
* - close the opened files and
* - free the memory allocated for the cache_handle.
* - mark the handle as "not initialized".
*
* Parameters:
* cache_handle: Handle to the opened cache directory to use.
*
* cache_handle->is_init set to "not initialized" and all the
* files opened bu globus_gass_cache_open are closed .
*
* Returns:
* GLOBUS_SUCCESS or error code:
* GLOBUS_GASS_CACHE_ERROR_CACHE_NOT_OPENED
*
*/
int
globus_gass_cache_close(
globus_gass_cache_t * cache_handlep)
{
globus_i_gass_cache_t * cache_handle;
cache_handle = *cache_handlep;
/* simply check if the cache has been opened */
CHECK_CACHE_IS_INIT(cache_handle);
CLR_ERROR;
/* marque this handle as not opened */
cache_handle->init=&globus_l_gass_cache_is_not_init;
# if defined GLOBUS_L_GASS_CACHE_LOG
{
if ( NULL != cache_handle->log_FILE )
{
fclose(cache_handle->log_FILE);
cache_handle->log_FILE = NULL;
}
/* Blow away zero sized log files */
if ( NULL != cache_handle->log_file_name )
{
struct stat statbuf;
int rc;
rc = globus_l_gass_cache_stat(
cache_handle->log_file_name, &statbuf );
if ( GLOBUS_SUCCESS == rc )
{
if ( 0 == statbuf.st_size )
{
globus_l_gass_cache_unlink( cache_handle->log_file_name );
}
}
free( cache_handle->log_file_name );
}
}
# endif
/* Free up memory */
free( cache_handle->cache_directory_path );
free( cache_handle->global_directory_path );
free( cache_handle->local_directory_path );
free( cache_handle->tmp_directory_path );
free( *cache_handlep );
GLOBUS_L_GASS_CACHE_LG("Cache Closed");
return(GLOBUS_SUCCESS);
}
/* globus_gass_cache_close() */
/**
* @brief Add a tag to an URL in the cache
* @ingroup globus_gass_cache
* @details
* Create a new cache file or add a tag on it.
*
* If the URL is already in the cache but is locked, then this call will block
* until the cache entry is unlocked, then will proceed with the subsequent
* operations.
*
* If the URL is already in the cache and unlocked, then add the tag to the
* cache entry's tag list, return the local cache filename in *local_filename,
* return the entry's current timestamp in *timestamp, lock the cache entry,
* and return GLOBUS_GASS_CACHE_ADD_EXISTS.
*
* If the URL is not in the cache, and create==GLOBUS_TRUE, then create a new
* unique empty local cache file, add it to the cache with the specified tag,
* return the filename in *local_filename, return *timestamp set to
* GLOBUS_GASS_CACHE_TIMESTAMP_UNKNOWN, lock the cache entry, and
* return GLOBUS_GASS_CACHE_ADD_NEW.
*
* If the URL is not in the cache, and create==GLOBUS_FALSE, then do not
* add it to the cache, and return GLOBUS_GASS_CACHE_URL_NOT_FOUND.
*
* If this function returns GLOBUS_GASS_CACHE_ADD_EXISTS or
* GLOBUS_GASS_CACHE_ADD_NEW, then globus_gass_cache_add_done() or
* globus_gass_cache_delete() must be subsequently
* called to unlock the cache entry.
*
* Subsequent calls to globus_gass_cache_add() and
* globus_gass_cache_delete_start() on the same cache and url, made either
* from this process or another, will block until the cache entry is unlocked.
*
* If tag==NULL, then a tag with the value "null" will be added to the cache
* entry's tag list.
*
* The same tag can be used multiple times, in which case this tag will be
* added to the entry's tag list multiple times.
*
* Note: It is recommended that proglobus_grams started via GLOBUS_GRAM
* pass a tag value
* of getenv("GLOBUS_GRAM_JOB_CONTACT"), since upon completion of a
* job GLOBUS_GRAM will automatically cleanup entries with this tag.
*
* @note the local_filename MUST be free by the user in a
* subsequent operation, using free()
*
* @param cache_handle
* Handle to the opened cache directory to use.
* @param url
* URL of the file to be cached. It is used as the main
* key to the cache entries.
* @param tag
* Tag specifying which job is/are using the cache. This
* is usually the GLOBUS_GRAM_JOB_CONTACT. Can be NULL or empty; the
* tag "null" is then used.
* create - Tells if the cache entry should be created if it is
* not already existing.
* @param create
* Flag to create if it does not already exist.
* @param timestamp
* Timestamp of the cached file, set by globus_gass_cache_done(), (or
* globus_gass_cache_delete() ).
* @param local_filename
* Path the the local file caching the file
* specified by "url". NULL if "url" not yet cached and
* creation not requested (create false).
*
* @return
* - GLOBUS_GASS_CACHE_URL_NOT_FOUND
* - GLOBUS_GASS_CACHE_ADD_NEW
* - GLOBUS_GASS_CACHE_ADD_EXISTS
* - or any of the defined gass error code.
*/
extern
int
globus_gass_cache_add(
globus_gass_cache_t cache_handle,
const char *url,
const char *tag,
globus_bool_t create,
unsigned long * timestamp,
char ** local_filename )
{
int rc; /* general purpose ret code */
int retval = GLOBUS_SUCCESS; /* Our return value. */
cache_names_t names;
int TODOattempts = 0;
int data_count, uniq_count;
double TODOstime = TODOGetTime();
const char *global_name;
const char *global_uniq;
globus_bool_t wait_timeout = GLOBUS_FALSE;
/* simply check if the cache has been opened */
CHECK_CACHE_IS_INIT(cache_handle);
CLR_ERROR;
/* Timestamp is unknown for now, no filename, ... */
*timestamp = GLOBUS_GASS_CACHE_TIMESTAMP_UNKNOWN;
*local_filename = NULL;
/* Generate the local and global filenames */
rc = globus_l_gass_cache_names_init( cache_handle, url, tag, &names );
/* Check if it all succeeded. */
if ( GLOBUS_SUCCESS != rc )
{
LOG_ERROR( rc );
return rc;
}
/* I want to do every thing again and again until the data file
is ready */
while ( 1 )
{
TODOattempts++;
/* Create the global directory tree if we need to */
if ( create )
{
rc = globus_l_gass_cache_make_dirtree( names.global_dir,
names.cache_type );
if ( rc != GLOBUS_SUCCESS )
{
globus_l_gass_cache_names_free( &names );
LOG_ERROR( rc );
return rc;
}
}
/* Check the file *data* file */
rc = globus_l_gass_cache_check_global_file( &names,
&data_count,
&uniq_count );
/* Special case: handle data lock timeout */
if ( ( GLOBUS_SUCCESS == rc ) &&
( GLOBUS_TRUE == wait_timeout ) &&
( 0 == data_count ) )
{
/* Handle this like the "no data" condition. */
rc = GLOBUS_L_ENODATA;
}
/* Doesn't exist? */
if ( GLOBUS_L_ENODATA == rc )
{
CLR_ERROR;
/* If we're not supposed to create it, give up.. */
if ( ! create )
{
globus_l_gass_cache_names_free( &names );
return GLOBUS_GASS_CACHE_URL_NOT_FOUND;
}
/* Now, go create the global file. */
CACHE_TRACE4( "Global file DNE; creating %s/%s%s",
names.global_dir, UDATA_FILE, names.uniq );
rc = globus_l_gass_cache_create_global_file( &names,
wait_timeout );
if ( GLOBUS_L_ENOENT == rc )
{
/* Directory has disappeared. */
continue;
}
else if ( GLOBUS_L_ENOTUNIQ == rc )
{
/* We're not uniq; go back to the top */
continue;
}
else if ( GLOBUS_SUCCESS != rc )
{
globus_l_gass_cache_names_free( &names );
LOG_ERROR( rc );
return rc;
}
/* Ok. We've created the global "uniq" file. Now, make
* the local. Don't wait for it to become ready - we
* created it!! */
rc = globus_l_gass_cache_make_local_file(
&names,
UDATA_FILE,
names.uniq );
if ( GLOBUS_L_ENODATA == rc )
{
continue;
}
else if ( GLOBUS_SUCCESS != rc )
{
globus_l_gass_cache_names_free( &names );
LOG_ERROR( rc );
return rc;
}
/* Ok. We're done! */
retval = GLOBUS_GASS_CACHE_ADD_NEW;
break;
}
/* Something bad happenned */
else if ( GLOBUS_SUCCESS != rc )
{
globus_l_gass_cache_names_free( &names );
LOG_ERROR( rc );
return rc;
}
/* Ok, the global data existed before we got here. */
/* Wait for it to become ready */
CACHE_TRACE3( "Global file exists; using %s/%s",
names.global_dir, DATA_FILE );
rc = globus_l_gass_cache_wait_ready( &names, timestamp );
if ( GLOBUS_L_ENODATA == rc )
{
CACHE_TRACE( "Data file went away; start over" );
continue; /* Data file went away; start over. */
}
else if ( GLOBUS_L_ETIMEOUT == rc )
{
CACHE_TRACE( "Wait timed out" );
wait_timeout = GLOBUS_TRUE;
continue; /* Wait timed out; go back to the top... */
}
else if ( GLOBUS_SUCCESS != rc )
{
globus_l_gass_cache_names_free( &names );
LOG_ERROR( rc );
return rc;
}
/* Lock the file */
# if ( LOCK_ADD_EXISTS )
TODOgu( global_dir, uniq, NULL );
TODOgu( NULL, NULL, "Ready; locking" );
CACHE_TRACE2( "Global file ready; locking -> %s", names.uniq );
rc = globus_l_gass_cache_make_unready( &names );
if ( GLOBUS_L_ENODATA == rc )
{
CACHE_TRACE( "Global file went away!" );
continue; /* Data file went away; start over. */
}
else if ( GLOBUS_SUCCESS != rc )
{
globus_l_gass_cache_names_free( &names );
LOG_ERROR( rc );
return rc;
}
/* Lock used; use the uniq file name in make_local_file() */
global_name = UDATA_FILE;
global_uniq = names.uniq;
# else
/* No lock; use the "raw" data file name in make_local_file() */
global_name = DATA_FILE;
global_uniq = NULL;
# endif
/* Ok; it's ready. Make the local file, then we're done */
TODOgu( NULL, NULL, "Before MLF" );
rc = globus_l_gass_cache_make_local_file(
&names,
global_name,
global_uniq );
TODOgu( NULL, NULL, "After MLF" );
if ( GLOBUS_L_ENODATA == rc )
{
CACHE_TRACE( "Global file went away!" );
continue; /* Data file went away; start over. */
}
else if ( GLOBUS_SUCCESS != rc )
{
LOG_ERROR( rc );
return rc;
}
else
{
retval = GLOBUS_GASS_CACHE_ADD_EXISTS;
break;
}
}
{
char buf[256];
sprintf( buf, "%s: %d attempts; %.5fs",
( ( GLOBUS_GASS_CACHE_ADD_NEW == retval ) ?
"AddNew" : "AddExists" ),
TODOattempts,
TODOGetTime() - TODOstime );
CACHE_TRACE( buf );
TODOgu( NULL, NULL, "AddExists" );
}
if ( retval == GLOBUS_GASS_CACHE_ADD_NEW ||
retval == GLOBUS_GASS_CACHE_ADD_EXISTS )
{
*local_filename = strdup( names.local_data_file );
}
/* Free up the allocated memory */
globus_l_gass_cache_names_free( &names );
return retval;
}
/* globus_gass_cache_add() */
/**
* @brief Complete adding a cache entry and unlock it
* @ingroup globus_gass_cache
* @details
* globus_gass_cache_add_done() MUST be called after globus_gass_cache_add(),
* to set the timestamp in the cache entry for the URL, and then unlock the
* cache entry. (The only case it does not need to be called is if
* globus_gass_cache_add() has returned GLOBUS_GASS_CACHE_URL_NOT_FOUND, of
* course.
*
* @param cache_handle
* Handle to the opened cache directory to use.
* @param url
* URL of the cached file to set as "done" (unlock)
* @param tag
* Tag specifying which job has locked the cache and must
* therefore be unlocked. It is an error to call this function
* with a tag which does not currently own the cache lock.
* @param timestamp
* Timestamp of the cached file.
*
* @return globus_gass_cache_add_done() returns GLOBUS_SUCCESS or any of the
* defined GASS Cache error codes.
*/
extern
int
globus_gass_cache_add_done(
globus_gass_cache_t cache_handle,
const char *url,
const char *tag,
unsigned long timestamp)
{
int rc; /* general purpose return code */
cache_names_t names;
/* simply check if the cache has been opened */
CHECK_CACHE_IS_INIT(cache_handle);
CLR_ERROR;
/* Generate the local and global filenames */
rc = globus_l_gass_cache_names_init( cache_handle, url, tag, &names );
if ( GLOBUS_SUCCESS != rc )
{
LOG_ERROR( rc );
return rc;
}
/* Create the ready file */
rc = globus_l_gass_cache_make_ready( &names, timestamp );
/* Another process beat us to the punch */
if ( GLOBUS_L_READY_OTHER == rc )
{
/* No harm done, make_ready() killed our uniq file for us. */
globus_l_gass_cache_names_free( &names );
return GLOBUS_SUCCESS;
}
/* OUR process already has it in the ready state */
else if ( GLOBUS_L_READY_MYPROC == rc )
{
# if ( ! LOCK_ADD_EXISTS )
globus_l_gass_cache_names_free( &names );
CLR_ERROR;
return GLOBUS_SUCCESS;
# else
CACHE_TRACE3( "Lost my uniq file '%s' in '%s', data exists",
names.uniq, names.global_dir );
globus_l_gass_cache_names_free( &names );
LOG_ERROR( GLOBUS_GASS_CACHE_ERROR_ALREADY_DONE );
return GLOBUS_GASS_CACHE_ERROR_ALREADY_DONE;
# endif
}
else if ( GLOBUS_SUCCESS != rc )
{
globus_l_gass_cache_names_free( &names );
LOG_ERROR( rc );
return rc;
}
globus_l_gass_cache_names_free( &names );
return GLOBUS_SUCCESS;
}
/* globus_gass_cache_add_done() */
/**
* @brief Query the GASS Cache
* @ingroup globus_gass_cache
* @details
* Query if an item is in the cache
*
* This call will block only if wait_for_lock is GLOBUS_TRUE
*
* @param cache_handle
* Handle to the opened cache directory to use.
* @param url
* URL of the file to query. It is used as the main
* key to the cache entries.
* @param tag
* Tag specifying which job is/are using the cache. This
* is usually the GLOBUS_GRAM_JOB_CONTACT. Can be NULL or empty; the
* tag "null" is then used.
* @param wait_for_lock
* If GLOBUS_TRUE, wait for any lock existing lock
* to be released. If GLOBUS_FALSE, doesn't wait for a lock to be
* released.
* @param timestamp
* Timestamp of the cached file, set by
* globus_gass_cache_done(), (or globus_gass_cache_delete() ).
* @param local_filename
* Path the the local file caching the file
* specified by "url". NULL if "url" not yet cached and
* creation not requested (create false).
* @param is_locked
* GLOBUS_TRUE if the file is currently (at return
* time) locked.
*
* @return The globus_gass_cache_query() function returns GLOBUS_SUCCESS,
* GLOBUS_GASS_CACHE_URL_NOT_FOUND, or any of the defined gass error code.
*/
extern
int
globus_gass_cache_query(
globus_gass_cache_t cache_handle,
const char *url,
const char *tag,
globus_bool_t wait_for_lock,
unsigned long *timestamp,
char **local_filename,
globus_bool_t *is_locked )
{
int rc; /* general purpose ret code */
cache_names_t names;
globus_bool_t locked;
int data_count, uniq_count;
/* simply check if the cache has been opened */
CHECK_CACHE_IS_INIT(cache_handle);
CLR_ERROR;
/* Generate the local and global filenames */
rc = globus_l_gass_cache_names_init( cache_handle, url, tag, &names );
/* Check if it all succeeded. */
if ( GLOBUS_SUCCESS != rc )
{
LOG_ERROR( rc );
return rc;
}
/* Check the file *data* file */
rc = globus_l_gass_cache_check_global_file( &names,
&data_count,
&uniq_count );
/* Wait for it to become ready? */
if ( ( GLOBUS_SUCCESS == rc ) && ( wait_for_lock ) )
{
rc = globus_l_gass_cache_wait_ready( &names, timestamp );
locked = GLOBUS_FALSE;
}
else
{
locked = ( 0 == data_count ) ? GLOBUS_FALSE : GLOBUS_TRUE;
}
/* Does a local file exist? */
if ( GLOBUS_SUCCESS == rc )
{
rc = globus_l_gass_cache_stat( names.local_data_file, NULL );
}
/* Copy output to passed in ptrs. */
if ( GLOBUS_SUCCESS == rc )
{
if ( local_filename )
{
*local_filename = strdup( names.local_data_file );
}
if ( is_locked )
{
*is_locked = locked;
}
}
/* Clean up & finish. */
globus_l_gass_cache_names_free( &names );
if ( GLOBUS_L_ENODATA == rc )
{
rc = GLOBUS_GASS_CACHE_URL_NOT_FOUND;
}
return rc;
}
/* globus_gass_cache_query() */
/**
* @brief Remove a cache tag
* @ingroup globus_gass_cache
* @details
* Lock the cache entry for the URL, and return the cache entry's current
* timestamp in *timestamp.
* This function will block if the data file is already locked, until it is
* unlocked.
*
* @param cache_handle
* Handle to the opened cache directory to use.
*
* @param url
* URL of the cached file to set as "done" (unlock)
* @param tag
* Tag specifying which job has locked the cache and must
* therefore be unlocked. It is an error to call this function
* with a tag which does not currently own the cache lock.
* @param timestamp
* Timestamp of the cached file, set by
* globus_gass_cache_done(), (or globus_gass_cache_delete() ).
*
* @return The globus_gass_cache_delete_start() function returns
* GLOBUS_SUCCESS or any of the defined GASS Cache error code.
*/
extern
int
globus_gass_cache_delete_start(globus_gass_cache_t cache_handle,
const char *url,
const char *tag,
unsigned long *timestamp)
{
int rc; /* general purpose return code */
cache_names_t names;
/* simply check if the cache has been opened */
CHECK_CACHE_IS_INIT(cache_handle);
CLR_ERROR;
/* Generate the local and global filenames */
rc = globus_l_gass_cache_names_init( cache_handle, url, tag, &names );
if ( GLOBUS_SUCCESS != rc )
{
LOG_ERROR( rc );
return rc;
}
/* Wait for the file to become "ready" */
rc = globus_l_gass_cache_wait_ready( &names, timestamp );
if ( ( GLOBUS_L_ENODATA == rc ) || ( GLOBUS_L_ETIMEOUT == rc ) )
{
globus_l_gass_cache_names_free( &names );
LOG_ERROR( rc );
return GLOBUS_GASS_CACHE_ERROR_URL_NOT_FOUND;
}
else if ( GLOBUS_SUCCESS != rc )
{
globus_l_gass_cache_names_free( &names );
LOG_ERROR( rc );
return GLOBUS_GASS_CACHE_ERROR_STATE_F_CORRUPT;
}
/* Lock the file */
CACHE_TRACE2( "Global file ready; locking -> %s", names.uniq );
rc = globus_l_gass_cache_make_unready( &names );
/* Free up the name buffers before checking for simplicity */
globus_l_gass_cache_names_free( &names );
/* Now, check the status */
if ( GLOBUS_L_ENODATA == rc )
{
CACHE_TRACE( "Global file went away!" );
LOG_ERROR( rc );
return GLOBUS_GASS_CACHE_ERROR_URL_NOT_FOUND;
}
else if ( GLOBUS_SUCCESS != rc )
{
LOG_ERROR( rc );
return GLOBUS_GASS_CACHE_ERROR_STATE_F_CORRUPT;
}
/* Done */
return GLOBUS_SUCCESS;
}
/* globus_gass_cache_delete_start() */
/**
* @brief Remove one instance of the tag from the cache entry's tag list.
* @ingroup globus_gass_cache
* @details
* If there are no more tags in the tag list, then remove this cache
* entry and delete the associated local cache file.
*
* Otherwise, update the timestamp to the passed value.
*
* This call will leave the cache entry unlocked.
*
* If is_locked==GLOBUS_TRUE, then this cache entry was locked during a
* previous call to globus_gass_cache_add() or
* globus_gass_cache_delete_start(). The cache
* file should be locked by the corresponding url/tag, or an error is
* returned. If it is locked by the corresponding url/tag, then the normal
* operation occur, without blocking (remove one instance from the tag
* update the timestamp and unlock the cache).
*
* If is_locked==GLOBUS_FALSE, eventually wait the cache is not locked any
* more, and then proceed with the normal operations.(remove one instance
* from the tag list and update the timestamp).
*
* @param cache_handle
* Handle to the opened cache directory to use.
* @param url
* URL of the file to be cached. It is used as the main
* key to the cache entries.
* @param tag
* Tag specifying which job is/are using the cache. This
* is usually the GLOBUS_GRAM_JOB_CONTACT. Can be NULL or empty; the
* tag "null" is then used.
* @param timestamp
* Timestamp of the cached file.
* @param is_locked
* Indicate if this cache entry was locked during a
* previous call to globus_gass_cache_add() or
* globus_gass_cache_delete_start().
*
* @return The globus_gass_cache_delete() function returns
* GLOBUS_SUCCESS or any of the defined GASS Cache error codes.
*/
extern
int
globus_gass_cache_delete(
globus_gass_cache_t cache_handle,
const char *url,
const char *tag,
unsigned long timestamp,
globus_bool_t is_locked )
{
int rc; /* general purpose return code */
int retval = GLOBUS_SUCCESS; /* Our return value. */
cache_names_t names;
double TODOstime = TODOGetTime();
/* simply check if the cache has been opened */
CHECK_CACHE_IS_INIT(cache_handle);
/* Generate the local and global filenames */
rc = globus_l_gass_cache_names_init( cache_handle, url, tag, &names );
/* Check if they all succeeded. */
if ( GLOBUS_SUCCESS != rc )
{
LOG_ERROR( rc );
return rc;
}
/* Do the actual delete... */
rc = globus_l_gass_cache_delete(
&names, ×tamp, is_locked );
/* Free up name buffers */
globus_l_gass_cache_names_free( &names );
/* Clean up! */
if ( GLOBUS_SUCCESS != rc )
{
LOG_ERROR( rc );
return rc;
}
/* Build some file names that we'll use. */
{
char buf[256];
sprintf( buf, "DELETE; %.3fs", TODOGetTime() - TODOstime );
CACHE_TRACE( buf );
}
return retval;
}
/* globus_gass_cache_delete() */
/**
* @brief Remove a tag from a cache entry
* @ingroup globus_gass_cache
* @details
* Remove all instances of the tag from the cache entry's tag list.
* If there are no more tags in the tag list, then remove this cache entry
* and delete the associated local cache file.
* If the cache entry is locked with the same tag as is passed to this
* function, then the entry is unlocked after removing the tags.
* Otherwise, the cache entry's lock is left untouched.
*
* This function does not block on a locked reference.
*
* @note The GLOBUS_GRAM job manager will automatically call this function
* with a tag of getenv("GLOBUS_GRAM_JOB_CONTACT") upon completion of a job.
*
* @param cache_handle
* Handle to the opened cache directory to use.
* @param url
* URL of the file to be cached. It is used as the main
* key to the cache entries.
* @param tag
* Tag specifying which job is/are using the cache. This
* is usually the GLOBUS_GRAM_JOB_CONTACT. Can be NULL or empty; the
* tag "null" is then used.
* @return The globus_gass_cache_cleanup_tag() function returns
* GLOBUS_SUCCESS or any of the defined GASS Cache error codes.
*/
extern
int
globus_gass_cache_cleanup_tag(
globus_gass_cache_t cache_handle,
const char *url,
const char *tag)
{
int rc; /* general purpose return code */
cache_names_t names;
double TODOstime = TODOGetTime();
int TODOnum = 0;
/* simply check if the cache has been opened */
CHECK_CACHE_IS_INIT(cache_handle);
/* Generate the local and global filenames */
rc = globus_l_gass_cache_names_init( cache_handle, url, tag, &names );
/* Check if they all succeeded. */
if ( GLOBUS_SUCCESS != rc )
{
LOG_ERROR( rc );
return rc;
}
/* It's got a mangled name; kill it! */
rc = globus_l_gass_cache_delete( &names, NULL, GLOBUS_FALSE );
/* Free up the name strings */
globus_l_gass_cache_names_free( &names );
/* Check our status */
if ( GLOBUS_SUCCESS != rc )
{
LOG_ERROR( rc );
return rc;
}
/* Build some file names that we'll use. */
{
char buf[256];
sprintf( buf, "Cleanup %d; %.3fs",
TODOnum, TODOGetTime()-TODOstime );
CACHE_TRACE( buf );
}
return rc;
}
/* globus_gass_cache_add_cleanup_tag() */
/**
* @brief Remove a tag from all cache entries
* @ingroup globus_gass_cache
* Remove all instances of the tag from the cache entry's tag list.
* If there are no more tags in the tag list, then remove this cache entry
* and delete the associated local cache file.
* If the cache entry is locked with the same tag as is passed to this
* function, then the entry is unlocked after removing the tags.
* Otherwise, the cache entry's lock is left untouched.
*
* This function does not block on a locked reference. This function
* differs from globus_gass_cache_cleanup_tag() in that this cleans up
* <b>all</b> URLs related with this tag.
*
* @note The GLOBUS_GRAM job manager will automatically call this function
* with a tag of getenv("GLOBUS_GRAM_JOB_CONTACT") upon completion of a job.
*
* @param cache_handle
* Handle to the opened cache directory to use.
* @param tag
* Tag specifying which job is/are using the cache. This
* is usually the GLOBUS_GRAM_JOB_CONTACT. Can be NULL or empty; the
* tag "null" is then used.
*
* @return The globus_gass_cache_cleanup_tag_all() function returns
* GLOBUS_SUCCESS or any of the defined GASS Cache error codes.
*/
extern
int
globus_gass_cache_cleanup_tag_all(
globus_gass_cache_t cache_handle,
char *tag )
{
int rc; /* Temp return code */
int retval = GLOBUS_SUCCESS; /* Our return value. */
cache_names_t names;
char *base_local_dir;
double TODOstime = TODOGetTime();
url_list_head_t url_list;
url_list_elem_t *url_elem, *url_next;
int data_num;
/* simply check if the cache has been opened */
CHECK_CACHE_IS_INIT(cache_handle);
/* Build the base local directory to use. */
rc = globus_l_gass_cache_names_init(
cache_handle, NULL, tag, &names );
if ( GLOBUS_SUCCESS == rc )
{
rc = globus_l_gass_cache_build_dirname(
names.local_root,
names.separator,
names.mangled_tag,
NULL,
&base_local_dir,
NULL );
if (rc)
{
RET_ERROR( rc );
}
}
else
{
RET_ERROR( rc );
}
/* Scan for URLs */
url_list.head = NULL;
url_list.count = 0;
rc = globus_l_gass_cache_list_all_urls( cache_handle,
base_local_dir,
"",
&url_list );
if (rc)
{
RET_ERROR( rc );
}
CACHE_TRACE4( "cleanup: scanned '%s' (tag '%s'), found %d",
base_local_dir, tag, url_list.count );
/* Walk through 'em all */
url_elem = url_list.head;
while ( NULL != url_elem )
{
/* It's got a mangled name; kill it! */
if ( ( NULL != url_elem->mangled ) &&
( strlen( url_elem->mangled ) > 0 ) )
{
/* Put the new mangled URL into the name structure */
rc = globus_l_gass_cache_names_new_murl(
url_elem->mangled, &names );
if ( GLOBUS_SUCCESS != rc )
{
MARK_ERRORMSG( rc, url_elem->mangled );
LOG_ERROR( rc );
retval = rc;
continue;
}
/* Loop through all of the "data" links found */
for( data_num = 0; data_num < url_elem->data_count; data_num++ )
{
rc = globus_l_gass_cache_delete( &names, NULL,
GLOBUS_FALSE);
if ( GLOBUS_SUCCESS != rc )
{
char buff[1024];
sprintf(buff,"MURL=\"%s\"", url_elem->mangled);
MARK_ERRORMSG( rc, buff );
LOG_ERROR( rc );
retval = rc;
break;
}
}
}
/* Free up the mangled URL buffer */
if ( NULL != url_elem->mangled )
{
free( url_elem->mangled );
}
/* Free up the URL list structure & name buffer */
url_next = url_elem->next;
free( url_elem );
url_elem = url_next;
}
/* Done */
globus_l_gass_cache_names_free( &names );
free(base_local_dir);
/* Build some file names that we'll use. */
{
char buf[256];
sprintf( buf,
"CleanupAll %.3fs",
TODOGetTime()-TODOstime );
CACHE_TRACE( buf );
}
return retval;
}
/* globus_gass_cache_add_cleanup_tag_all() */
/**
* @brief Convert a URL to a string suitable as a file path
* @ingroup globus_gass_cache
* @details
* Mangles the given URL into a chunk suitable for using as a file /
* path name. This is exported for use in the globus_gass_cache
* program.
*
* @param cache_handle
* Handle to the opened cache directory to use.
* @param url
* The incoming URL to mangle (NULL-terminated)
* @param mangled_url
* Pointer to the output string; a buffer for the
* real string is malloc()ed for the application. If mangled is
* NULL, then no such buffer is allocated, and no mangled string
* is created. This can be useful to just get the length of the
* mangled string.
* @param length
* A pointer to the length of the resulting string. If NULL, this is not
* assigned to.
*
* @retval GLOBUS_SUCCESS
* Success
* @retval GLOBUS_GASS_CACHE_ERROR_NO_MEMORY
* No memory
*/
extern
int
globus_gass_cache_mangle_url( const globus_gass_cache_t cache_handle,
const char *url,
char **mangled_url,
int *length )
{
/* Let _mangle() do all the work! */
return globus_l_gass_cache_mangle(cache_handle,
url,
cache_handle->max_mangled_url,
mangled_url,
length );
} /* globus_gass_cache_mangle_url() */
/**
* @brief Convert a tag to a string suitable as a file path
* @ingroup globus_gass_cache
* @details
* Mangles the given tag into a chunk suitable for using as a file /
* path name. This is exported for use in the globus_gass_cache
* program.
*
* @param cache_handle
* Handle to the opened cache directory to use.
* @param tag
* The incoming tag to mangle (NULL-terminated)
* @param mangled_tag
* Pointer to the output string; a buffer for the
* real string is malloc()ed for the application. If mangled is
* NULL, then no such buffer is allocated, and no mangled string
* is created. This can be useful to just get the length of the
* mangled string.
* @param length
* A pointer to the length of the resulting string. If NULL, this is
* not assigned to.
*
* @retval GLOBUS_SUCCESS
* Success
* @retval GLOBUS_GASS_CACHE_ERROR_NO_MEMORY
* No memory
*/
extern
int
globus_gass_cache_mangle_tag( const globus_gass_cache_t cache_handle,
const char *tag,
char **mangled_tag,
int *length )
{
/* Let _mangle() do all the work! */
return globus_l_gass_cache_mangle(cache_handle,
tag,
cache_handle->max_mangled_tag,
mangled_tag,
length );
} /* globus_gass_cache_mangle_tag() */
/**
* @brief Get the GASS Cache's root directory
* @ingroup globus_gass_cache
* @details
* Get a the root cache of directory. This is exported for use in the
* globus_gass_cache program.
*
* @param cache_handle
* Handle to the opened cache directory to use.
* @param cache_dir
* Pointer to the cache directory
*
* @retval GLOBUS_SUCCESS
* Success
* @retval GLOBUS_GASS_CACHE_ERROR_NO_MEMORY
* No memory
*/
extern
int
globus_gass_cache_get_cache_dir( const globus_gass_cache_t cache_handle,
char **cache_dir )
{
int rc = GLOBUS_SUCCESS; /* general purpose return code */
if ( cache_dir )
{
*cache_dir = NULL;
}
/* Copy the cache root directory out */
if ( ( cache_dir ) && ( cache_handle->cache_directory_path ) )
{
*cache_dir = strdup( cache_handle->cache_directory_path );
if ( NULL == *cache_dir )
{
rc = GLOBUS_GASS_CACHE_ERROR_NO_MEMORY;
}
}
return rc;
} /* globus_gass_cache_get_cache_dir() */
/**
* @brief Get the set of directories used by a GASS Cache
* @ingroup globus_gass_cache
* @details
* Gets a bunch of directories. This is exported for use in the
* globus_gass_cache program.
*
* @param cache_handle
* Handle to the opened cache directory to use.
* @param url
* The incoming URL
* @param tag
* The incoming tag
* @param local_root
* Pointer to the "local root" directory
* @param global_root
* Pointer to the "global root" directory
* @param tmp_root
* Pointer to the "tmp root" directory
* @param log_root
* Pointer to the root log directory
* @param local_dir
* Pointer to the related "local" directory
* @param global_dir
* Pointer to the related "global" directory
*
* @retval GLOBUS_SUCCESS
* Success
* @retval GLOBUS_GASS_CACHE_ERROR_NO_MEMORY
* No memory
*/
extern
int
globus_gass_cache_get_dirs( const globus_gass_cache_t cache_handle,
const char *url,
const char *tag,
char **global_root,
char **local_root,
char **tmp_root,
char **log_root,
char **global_dir,
char **local_dir )
{
int rc = GLOBUS_SUCCESS; /* general purpose return code */
cache_names_t names;
/* Initialize 'em all to NULL */
if ( global_root )
{
*global_root = NULL;
}
if ( local_root )
{
*local_root = NULL;
}
if ( tmp_root )
{
*tmp_root = NULL;
}
if ( log_root )
{
*log_root = NULL;
}
if ( global_dir )
{
*global_dir = NULL;
}
if ( local_dir )
{
*local_dir = NULL;
}
/* simply check if the cache has been opened */
CHECK_CACHE_IS_INIT(cache_handle);
/* Generate the local and global filenames */
rc = globus_l_gass_cache_names_init( cache_handle, url, tag, &names );
/* Check if they all succeeded. */
if ( GLOBUS_SUCCESS != rc )
{
LOG_ERROR( rc );
return rc;
}
/* Copy out the path pieces.... */
#define copy(var) \
if ( ( var ) && ( names.var ) ) \
{ \
if ( ( *var = strdup( names.var ) ) == NULL ) \
{ \
rc = GLOBUS_GASS_CACHE_ERROR_NO_MEMORY; \
} \
}
copy(global_root);
copy(local_root);
copy(tmp_root);
copy(log_root);
copy(global_dir);
copy(local_dir);
#undef copy
/* Free up name buffers */
globus_l_gass_cache_names_free( &names );
/* Done if all ok. */
if ( GLOBUS_SUCCESS == rc )
{
return GLOBUS_SUCCESS;
}
/* Handle errors w/o leaking memory! */
#define clean(var) \
if ( var && *var ) \
{ \
free( *var ); \
*var = NULL; \
}
clean(global_root);
clean(local_root);
clean(tmp_root);
clean(log_root);
clean(global_dir);
clean(local_dir);
#undef clean
return rc;
} /* globus_gass_cache_get_dirs() */
/**
* @brief Get the type of GASS Cache directory layout
* @ingroup globus_gass_cache
* @details
* Get a string which describes the cache type ("normal" or "flat")
*
* @param cache_handle
* Handle to the opened cache directory to use.
* @param cache_type
* Pointer to the strdup()ed string
*
* @retval GLOBUS_SUCCESS
* Success
* @retval GLOBUS_GASS_CACHE_ERROR_NO_MEMORY
* No memory
*/
extern
int
globus_gass_cache_get_cache_type_string( const globus_gass_cache_t cache_handle,
char **cache_type )
{
int rc = GLOBUS_SUCCESS; /* general purpose return code */
if ( cache_type )
{
*cache_type = NULL;
}
/* Copy the cache root directory out */
if ( cache_type )
{
char *temp = ( 0 == cache_handle->cache_type ) ? "normal" : "flat";
*cache_type = strdup( temp );
if ( NULL == *cache_type )
{
rc = GLOBUS_GASS_CACHE_ERROR_NO_MEMORY;
}
}
return rc;
} /* globus_gass_cache_get_cache_type_string() */
/**
* @brief Look up the error string corresponding to a GASS Cache error
* @ingroup globus_gass_cache
* @details
* Return a pointer on an error description string.
*
* @param error_code
* error code returned by a previously called globus_gass_cache function.
*
* @return The globus_gass_cache_error_string() function returns a
* pointer to an error message, or NULL if error_code is invalid.
*/
extern
const
char *
globus_gass_cache_error_string(
int error_code)
{
unsigned pos_errcode = ( unsigned ) ( 0 - error_code );
unsigned max_errcode = ( sizeof( globus_gass_cache_error_strings ) /
sizeof( globus_gass_cache_error_strings[0] ) );
/* Check; is this a valid error code? */
if ( ( error_code > 0 ) || ( pos_errcode >= max_errcode ) )
{
return("Invalid error code");
}
return ( globus_gass_cache_error_strings[ pos_errcode ] );
}
/* globus_gass_cache_error_string() */
|