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
|
/****************** Start of $RCSfile: client.c,v $ ****************
*
* $Source: /home/alb/afbackup/afbackup-3.3.6/RCS/client.c,v $
* $Id: client.c,v 1.2 2002/02/27 10:17:10 alb Exp alb $
* $Date: 2002/02/27 10:17:10 $
* $Author: alb $
*
*
******* description ***********************************************
*
*
*
*******************************************************************/
#include <conf.h>
#include <version.h>
static char * fileversion = "$RCSfile: client.c,v $ $Source: /home/alb/afbackup/afbackup-3.3.6/RCS/client.c,v $ $Id: client.c,v 1.2 2002/02/27 10:17:10 alb Exp alb $ " PACKAGE " " VERSION_STRING;
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/utsname.h>
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#ifdef HAVE_SYS_RESOURCE_H
#include <sys/resource.h>
#endif
#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#ifdef HAVE_NETINET_IN_SYSTM_H
#include <netinet/in_systm.h>
#endif
#ifdef HAVE_NETINET_IP_H
#include <netinet/ip.h>
#endif
#include <x_types.h>
#include <x_errno.h>
#include <genutils.h>
#include <sysutils.h>
#include <netutils.h>
#include <fileutil.h>
#include <packer.h>
#include "server.h"
#include "cryptkey.h"
#include "backup.h"
#define GETOUT { goto getout; }
#define CLEANUP { goto cleanup; }
#define CLEANUPR(ret) { r = ret; goto cleanup; }
#define SERIAL_OPERATION 1
#define QUEUED_OPERATION 2
#define MAX_QUEUE_MEMORY 10485760 /* 10 MB */
typedef struct _queueentry_ {
UChar *inmem;
UChar *outmem;
Uns32 num_in;
Uns32 num_out;
Uns16 instruction;
UChar processed;
Uns32 req_queue_cbs;
} QueueEntry;
#define NOT_PROCESSED 0
#define PROCESSED_OK RESERVED_ERROR_CODE
Flag noqueued = NO;
UChar comm_mode = QUEUED_OPERATION;
Uns32 queuelen = 512;
QueueEntry *queue_entries = NULL;
Uns32 max_queuelen = 0;
UChar *outbufmem = NULL;
UChar *inbufmem = NULL;
Uns32 allocated_outmem = 0;
Uns32 allocated_inmem = 0;
Uns32 queueent_done_idx = 0;
Uns32 queueent_requested_idx = 0;
Uns32 queueent_processed_idx = 0;
Uns32 queue_insert_idx = 0;
Uns32 cur_num_entries_in_queue = 0;
Int32 queue_commbufsiz = 0;
Flag std_io = NO;
#define QUEUEDOP (queue_entries != NULL)
#define num_entries_in_queue (cur_num_entries_in_queue = \
(queue_insert_idx >= queueent_done_idx ? \
queue_insert_idx - queueent_done_idx : \
queuelen - queueent_done_idx + queue_insert_idx))
UChar *servername = NULL;
short unsigned portnum = DEFAULT_PORT;
UChar *cryptfile = NULL;
Int32 commbufsiz = DEFCOMMBUFSIZ;
Int32 max_outarglen;
Int32 max_inresultlen;
UChar scbuf[MAXCOMMBUFSIZ + 8];
UChar *cbuf = &(scbuf[1]);
Int32 cbufptr = 0; /* pointer into send/receive-buffer */
Int32 bytes_transferred = 0;
Int32 bytes_wr_unkn_pos = 0;
#define reset_data_buffer { cbufptr = 0; }
#define reset_tape_io { endofrec = 0; }
int commfd = -1;
int commfd_socktype = -1;
struct timeval null_timeval;
Int32 cart = 0;
Int32 filenum = 0;
Int32 wrcart = 0;
Int32 wrfilenum = 0;
Int32 rdcart = 0;
Int32 rdfilenum = 0;
Int32 cartset = 0;
Int32 numcarts = 0;
Int32 tapeblocksize = 0;
UChar *infoheader = NULL;
UChar *identitystr = NULL;
Flag filenum_valid = NO;
Flag wrfilenum_valid = NO;
Flag rdfilenum_valid = NO;
Int32 endofrec = 0;
AarParams params;
Flag bu_create = NO;
Flag bu_extract = NO;
Flag bu_contents = NO;
Flag bu_verify = NO;
Flag bu_index = NO;
Flag bu_ready = NO;
Flag bu_duptape = NO;
UChar *bu_duptapeargs = NULL;
Flag bu_junction = NO;
Flag allfiles = NO;
Uns8 verbose = 0;
Flag c_f_verbose = NO;
Flag l_verbose = NO;
Flag o_verbose = NO;
Flag u_verbose = NO;
Flag bu_request = NO;
Flag bu_svrmsg = NO;
UChar *servermsg = NULL;
Flag req_newfile = NO;
Flag req_newcart = NO;
UChar *clientprog = NULL;
UChar *serverversion = NULL;
Flag server_can_sendtbufsiz = NO;
Flag server_can_setcbufsiz = NO;
Flag server_can_sendid = NO;
Flag server_can_sendutapes = NO;
Flag server_can_userid = NO;
Flag server_can_auth = NO;
Flag server_can_sendrdpos = NO;
UChar **filelist = NULL;
char *toextractfilename = NULL;
char *errorlogfile = NULL;
char *savefile = NULL;
Flag save_stdio = NO;
char username[128] = "";
Int32 num_errors = 0;
UChar long_errmsgs = 0;
UChar **gargv;
void (*cleanup_proc)(void *) = NULL;
void *cleanup_arg = NULL;
#define MAX_NUM_ERRORS 20
#define ESTIM_PROT_OVERHEAD 50
#define MAX_NUM_WRITE_WITHOUT_POS_UPDATE 5000000
struct fault_msgs fault_messages[] = FAULT_MESSAGES;
Int32 delete_command_queue();
Int32 queued_read(int, UChar *, Int32);
Int32 queued_write(int, UChar *, Int32);
Int32 post_process_entry(Int32);
Int32 post_process_rest_of_queue(Int32);
Int32 append_to_queue(Uns16, UChar *, Uns32, UChar *, Uns32);
Int32 simple_command(UChar);
Int32 setnumbytesvalid(Uns32);
Int32 getnumbytesvalid(Uns32 *);
Int32 getcartandfile(AarParams *);
Int32 getwrcartandfile(AarParams *);
Int32 getrdcartandfile(AarParams *);
Int32 send_commbufsiz(Int32);
Int32 duplicate_tape(UChar *, AarParams *);
Int32 copytaperemote(UChar *, Int32, Int32, UChar *);
Int32 copytapelocally(Int32);
#define set_server_serial simple_command(SETSERIALOP)
#define set_server_buffering simple_command(SETBUFFEREDOP)
#define set_server_erroneot simple_command(SETERRORONEOT)
#define set_server_chcartoneot simple_command(SETCHCARTONEOT)
#define getserverstate(ptr, w) getbufferwithcmd(ptr, 512, QUERYRDYFORSERV, w)
#define getserverid(ptr, w) getbufferwithcmd(ptr, 256, SERVERIDENT, w)
#define ER__(cmd, lerrfl) { if( (lerrfl = cmd) ) return(lerrfl); }
UChar * fetch_tape_contents(AarParams * params);
UChar * put_tape_contents(AarParams * params);
static void
errmsg(UChar * msg, ...)
{
va_list args;
va_start(args, msg);
if(long_errmsgs)
fprintf(stderr, "%s, ", actimestr());
vfprintf(stderr, msg, args);
va_end(args);
if(msg[strlen(msg) - 1] != '\n')
fprintf(stderr, ".\n");
fflush(stderr);
}
static Int32
E__(Int32 e)
{
if(e)
errmsg(T_("%sError: %s"), (e > 0 ? T_("Server ") : ""),
(e > 0 ? fault_string(e) : (UChar *) strerror(-e)));
return(e);
}
#define OK__(func) (! E__(func))
void
signal_handler(int s)
{
if(long_errmsgs || s == SIGTERM || s == SIGINT)
fprintf(stderr, T_("%s got signal %d, exiting.\n"),
FN_BASENAME(gargv[0]), s);
if(cleanup_proc)
(*cleanup_proc)(cleanup_arg);
exit(128 | s);
}
Int32
send_cmd(UChar cmd) /* utility */
{
if(write_forced(commfd, &cmd, 1) != 1)
return(errno ? - errno : EOF);
bytes_transferred += ESTIM_PROT_OVERHEAD + 1;
return(0);
}
UChar
result()
{
UChar c;
Int32 i;
if(savefile)
return(0);
i = (read_forced(commfd, &c, 1) != 1);
bytes_transferred += ESTIM_PROT_OVERHEAD + 1;
if(i){
return(errno ? - errno : EOF);
}
if(! c)
return(0);
return(c);
}
Int32 /* This function assumes, that we can do it. It is sure, that no data */
change_queue_bufsiz(Int32 newsize) /* will be lost */
{
Int32 new_queuelen, new_max_outarglen, new_max_inresultlen;
Int32 i, n, prev_qcbs;
UChar *new_outbufmem, *new_inbufmem, *scptr, *dcptr;
QueueEntry *new_queue_entries;
if(! queue_entries) /* no queue currently present */
return(0);
if(newsize == queue_commbufsiz)
return(0);
new_max_outarglen = new_max_inresultlen = newsize + 4;
new_queuelen = allocated_outmem / new_max_outarglen;
if(new_queuelen > max_queuelen){
queue_entries = RENEWP(queue_entries, QueueEntry, new_queuelen);
if(!queue_entries)
return(FATAL_ERROR);
memset(queue_entries + max_queuelen, 0,
(new_queuelen - max_queuelen) * sizeof(QueueEntry));
max_queuelen = new_queuelen;
}
if(newsize > queue_commbufsiz){ /* if there's size-change-requests */
for(i = queueent_done_idx; i != queue_insert_idx; i = (i + 1) % queuelen){
n = queue_entries[i].req_queue_cbs; /* in queue to size smaller */
if(n > 0 && n < newsize){ /* then current request */
queue_entries[i].req_queue_cbs = newsize; /* change them */
}
}
}
if(new_queuelen < 2){ /* it's rediculuous, if we have that few */
new_queuelen = 2; /* memory, nonetheless we catch this case */
allocated_outmem = new_max_outarglen * new_queuelen;
allocated_inmem = new_max_inresultlen * new_queuelen;
new_outbufmem = RENEWP(outbufmem, UChar, allocated_outmem);
new_inbufmem = RENEWP(inbufmem, UChar, allocated_inmem);
new_queue_entries = RENEWP(queue_entries, QueueEntry, new_queuelen);
if(!new_outbufmem || !new_inbufmem || !new_queue_entries){
ZFREE(new_outbufmem);
ZFREE(new_inbufmem);
ZFREE(new_queue_entries);
return(delete_command_queue());
}
outbufmem = new_outbufmem;
inbufmem = new_inbufmem;
queue_entries = new_queue_entries;
}
do{
prev_qcbs = queue_commbufsiz;
if(newsize > queue_commbufsiz){ /* wait for space */
i = post_process_rest_of_queue(new_queuelen - 1);
if(i) /* to move stuff around and to pack */
return(i); /* the queue entry data wider */
}
}while(prev_qcbs != queue_commbufsiz);
if(newsize > queue_commbufsiz){
if(queue_insert_idx > queueent_done_idx){
n = queue_insert_idx - queueent_done_idx;
if(queueent_done_idx > 0){
memmove(outbufmem, outbufmem + max_outarglen * queueent_done_idx,
max_outarglen * sizeof(UChar) * n);
memmove(inbufmem, inbufmem + max_inresultlen * queueent_done_idx,
max_inresultlen * sizeof(UChar) * n);
memmove(queue_entries, queue_entries + queueent_done_idx,
sizeof(queue_entries[0]) * n);
}
n--;
scptr = outbufmem + max_outarglen * n;
dcptr = outbufmem + new_max_outarglen * n;
for(i = n; i > 0; i--){
memmove(dcptr, scptr, max_outarglen);
scptr -= max_outarglen;
dcptr -= new_max_outarglen;
}
scptr = inbufmem + max_inresultlen * n;
dcptr = inbufmem + new_max_inresultlen * n;
for(i = n; i > 0; i--){
memmove(dcptr, scptr, max_inresultlen);
scptr -= max_inresultlen;
dcptr -= new_max_inresultlen;
}
queueent_requested_idx -= queueent_done_idx;
queueent_processed_idx -= queueent_done_idx;
queue_insert_idx -= queueent_done_idx;
queueent_done_idx = 0;
}
if(queue_insert_idx < queueent_done_idx){
n = queuelen - new_queuelen;
i = queuelen - queueent_done_idx;
if(i > 0){
i *= max_outarglen;
scptr = outbufmem + allocated_outmem - i;
memmove(scptr, outbufmem + max_outarglen * queueent_done_idx, i);
dcptr = outbufmem + (queueent_done_idx - n) * new_max_outarglen;
for(i = queueent_done_idx; i < queuelen; i++){
memmove(dcptr, scptr, max_outarglen);
scptr += max_outarglen;
dcptr += new_max_outarglen;
}
i = (queuelen - queueent_done_idx) * max_inresultlen;
scptr = inbufmem + allocated_inmem - i;
memmove(scptr, inbufmem + max_inresultlen * queueent_done_idx, i);
dcptr = inbufmem + (queueent_done_idx - n) * new_max_inresultlen;
for(i = queueent_done_idx; i < queuelen; i++){
memmove(dcptr, scptr, max_inresultlen);
scptr += max_inresultlen;
dcptr += new_max_inresultlen;
}
}
if(queue_insert_idx > 0){
scptr = outbufmem + max_outarglen * (queue_insert_idx - 1);
dcptr = outbufmem + new_max_outarglen * (queue_insert_idx - 1);
for(i = queue_insert_idx - 1; i > 0; i--){
memmove(dcptr, scptr, max_outarglen);
scptr -= max_outarglen;
dcptr -= new_max_outarglen;
}
scptr = inbufmem + max_inresultlen * (queue_insert_idx - 1);
dcptr = inbufmem + new_max_inresultlen * (queue_insert_idx - 1);
for(i = queue_insert_idx - 1; i > 0; i--){
memmove(dcptr, scptr, max_inresultlen);
scptr -= max_inresultlen;
dcptr -= new_max_inresultlen;
}
}
memmove(queue_entries + queueent_done_idx - n,
queue_entries + queueent_done_idx,
sizeof(queue_entries[0]) * (queuelen - queueent_done_idx));
if(queueent_requested_idx >= queueent_done_idx)
queueent_requested_idx -= n;
if(queueent_processed_idx >= queueent_done_idx)
queueent_processed_idx -= n;
queueent_done_idx -= n;
}
}
if(newsize < queue_commbufsiz){
if(queue_insert_idx > queueent_done_idx){
n = queue_insert_idx - queueent_done_idx;
if(queueent_done_idx > 0){
memmove(outbufmem, outbufmem + max_outarglen * queueent_done_idx,
max_outarglen * sizeof(UChar) * n);
memmove(inbufmem, inbufmem + max_inresultlen * queueent_done_idx,
max_inresultlen * sizeof(UChar) * n);
memmove(queue_entries, queue_entries + queueent_done_idx,
sizeof(queue_entries[0]) * n);
}
scptr = outbufmem + max_outarglen;
dcptr = outbufmem + new_max_outarglen;
for(i = 1; i < n; i++){
memmove(dcptr, scptr, max_outarglen);
scptr += max_outarglen;
dcptr += new_max_outarglen;
}
scptr = inbufmem + max_inresultlen;
dcptr = inbufmem + new_max_inresultlen;
for(i = 1; i < n; i++){
memmove(dcptr, scptr, max_inresultlen);
scptr += max_inresultlen;
dcptr += new_max_inresultlen;
}
queueent_requested_idx -= queueent_done_idx;
queueent_processed_idx -= queueent_done_idx;
queue_insert_idx -= queueent_done_idx;
queueent_done_idx = 0;
}
if(queue_insert_idx < queueent_done_idx){
n = new_queuelen - queuelen;
i = queuelen - queueent_done_idx;
if(i > 0){
i *= max_outarglen;
scptr = outbufmem + (queue_insert_idx + 1) * max_outarglen;
memmove(scptr, outbufmem + max_outarglen * queueent_done_idx, i);
scptr += (i - max_outarglen);
dcptr = outbufmem + (new_queuelen - 1) * new_max_outarglen;
for(i = queuelen; i > queueent_done_idx; i--){
memmove(dcptr, scptr, new_max_outarglen);
scptr -= max_outarglen;
dcptr -= new_max_outarglen;
}
i = (queuelen - queueent_done_idx) * max_inresultlen;
scptr = inbufmem + (queue_insert_idx + 1) * max_inresultlen;
memmove(scptr, inbufmem + max_inresultlen * queueent_done_idx, i);
scptr += (i - max_inresultlen);
dcptr = inbufmem + (new_queuelen - 1) * new_max_inresultlen;
for(i = queuelen; i > queueent_done_idx; i--){
memmove(dcptr, scptr, new_max_inresultlen);
scptr -= max_inresultlen;
dcptr -= new_max_inresultlen;
}
}
if(queue_insert_idx > 0){
scptr = outbufmem + max_outarglen;
dcptr = outbufmem + new_max_outarglen;
for(i = 1; i < queue_insert_idx; i++){
memmove(dcptr, scptr, new_max_outarglen);
scptr += max_outarglen;
dcptr += new_max_outarglen;
}
scptr = inbufmem + max_inresultlen;
dcptr = inbufmem + new_max_inresultlen;
for(i = 1; i < queue_insert_idx; i++){
memmove(dcptr, scptr, new_max_inresultlen);
scptr += max_inresultlen;
dcptr += new_max_inresultlen;
}
}
memmove(queue_entries + queueent_done_idx + n,
queue_entries + queueent_done_idx,
sizeof(queue_entries[0]) * (queuelen - queueent_done_idx));
if(queueent_requested_idx >= queueent_done_idx)
queueent_requested_idx += n;
if(queueent_processed_idx >= queueent_done_idx)
queueent_processed_idx += n;
queueent_done_idx += n;
}
}
max_outarglen = new_max_outarglen;
max_inresultlen = new_max_inresultlen;
queuelen = new_queuelen;
queue_commbufsiz = newsize;
return(0);
}
Int32
switch_queue_bufsiz(Int32 newsize)
{
Int32 i;
if(! queue_entries) /* no queue currently present */
return(0);
if(newsize == queue_commbufsiz)
return(0);
if(newsize > queue_commbufsiz){
ER__(change_queue_bufsiz(newsize), i);
}
else{
queue_entries[queue_insert_idx].req_queue_cbs = newsize;
}
return(0);
}
Int32
alloc_commbuf(Int32 newsize)
{
if(newsize > MAXCOMMBUFSIZ || newsize < 0)
return(NO_VALID_COMMBUFSIZ);
commbufsiz = newsize;
return(scbuf ? 0 : -1);
}
Int32
set_commbufsiz(Int32 newsize)
{
Int32 i;
if(newsize > MAXCOMMBUFSIZ || newsize < 0)
return(NO_VALID_COMMBUFSIZ);
if(QUEUEDOP){
i = switch_queue_bufsiz(newsize);
if(i)
return(i);
}
ER__(send_commbufsiz(newsize), i);
return(alloc_commbuf(newsize));
}
#define OPTIMIZER_CHUNKSIZE 5000000
#define MIN_CBSDIFF (MINCOMMBUFSIZ >> 1)
Int32 /* Albi's 3-walking-points optimizer */
try_optimize_throughput() /* ("albimizer") */
{
static Flag meas_initialized = NO;
static Real64 ratios[3];
static Int32 cbs[3], actidx, chunksize, cbsdiff;
static struct timeval last_time;
struct timeval act_time;
Real64 timediff, ratio;
Int32 i, maxidx;
if(!server_can_setcbufsiz)
return(0);
if(!meas_initialized){
chunksize = queue_entries ? MAX(allocated_outmem, OPTIMIZER_CHUNKSIZE) : OPTIMIZER_CHUNKSIZE;
gettimeofday(&last_time, NULL);
cbs[0] = MINCOMMBUFSIZ;
cbs[2] = MAXCOMMBUFSIZ;
cbs[1] = (MINCOMMBUFSIZ + MAXCOMMBUFSIZ) / 2;
cbsdiff = (MAXCOMMBUFSIZ - MINCOMMBUFSIZ) / 2;
ratios[0] = ratios[1] = ratios[2] = -1.0;
actidx = 0;
bytes_transferred = 0;
meas_initialized = YES;
return(set_commbufsiz(cbs[actidx]));
}
if(bytes_transferred < chunksize)
return(0);
gettimeofday(&act_time, NULL);
timediff = (Real64) act_time.tv_sec + ((Real64) act_time.tv_usec / 1.0e6)
- ((Real64) last_time.tv_sec + ((Real64) last_time.tv_usec / 1.0e6));
if(timediff < 0.0)
timediff += 86400.0;
COPYVAL(last_time, act_time);
ratio = (Real64) bytes_transferred / timediff;
bytes_transferred = 0;
ratios[actidx] = ratio;
if(ratios[0] < 0.0)
actidx = 0;
else if(ratios[1] < 0.0)
actidx = 1;
else if(ratios[2] < 0.0)
actidx = 2;
else
actidx = -1;
if(actidx >= 0) /* still have unknown values */
return(set_commbufsiz(cbs[actidx])); /* go on measuring */
maxidx = 0;
if(ratios[1] > ratios[0])
maxidx = 1;
if(ratios[2] > ratios[maxidx])
maxidx = 2;
switch(maxidx){
case 1:
i = cbsdiff >> 1;
if(i < MIN_CBSDIFF){
ratio = drandom();
actidx = 1;
if(ratio < 0.07){ /* curiosity kills the cat */
actidx = 0;
}
if(ratio > 0.93){
actidx = 2;
}
}
else{
cbsdiff = i;
ratios[0] = ratios[1] = ratios[2] = -1.0;
cbs[0] += cbsdiff;
cbs[2] -= cbsdiff;
actidx = 0;
}
break;
case 0:
actidx = 0;
if(cbs[0] - cbsdiff < MINCOMMBUFSIZ){
i = cbsdiff >> 1;
if(i >= MIN_CBSDIFF){
cbsdiff = i;
if(cbs[0] - i < MINCOMMBUFSIZ){
cbs[0] = MINCOMMBUFSIZ;
cbs[1] = MINCOMMBUFSIZ + cbsdiff;
cbs[2] = MINCOMMBUFSIZ + (cbsdiff << 1);
ratios[0] = ratios[1] = ratios[2] = -1.0;
}
else{
cbs[2] = cbs[1];
cbs[1] = cbs[0];
cbs[0] -= i;
ratios[2] = ratios[1];
ratios[1] = ratios[0];
}
}
else{
cbs[0] = MINCOMMBUFSIZ;
cbs[1] = MINCOMMBUFSIZ + MIN_CBSDIFF;
cbs[2] = MINCOMMBUFSIZ + (MIN_CBSDIFF << 1);
cbsdiff = MIN_CBSDIFF;
ratio = drandom();
if(ratio < 0.07)
actidx = 1;
if(ratio < 0.005)
actidx = 2;
}
}
else{
cbs[2] = cbs[1];
cbs[1] = cbs[0];
cbs[0] -= cbsdiff;
ratios[2] = ratios[1];
ratios[1] = ratios[0];
}
break;
case 2:
actidx = 2;
if(cbs[2] + cbsdiff > MAXCOMMBUFSIZ){
i = cbsdiff >> 1;
if(i >= MIN_CBSDIFF){
cbsdiff = i;
if(cbs[2] + i > MAXCOMMBUFSIZ){
cbs[2] = MAXCOMMBUFSIZ;
cbs[1] = MAXCOMMBUFSIZ - cbsdiff;
cbs[0] = MAXCOMMBUFSIZ - (cbsdiff << 1);
ratios[0] = ratios[1] = ratios[2] = -1.0;
}
else{
cbs[0] = cbs[1];
cbs[1] = cbs[2];
cbs[2] += i;
ratios[0] = ratios[1];
ratios[1] = ratios[2];
}
}
else{
cbs[2] = MAXCOMMBUFSIZ;
cbs[1] = MAXCOMMBUFSIZ - MIN_CBSDIFF;
cbs[0] = MAXCOMMBUFSIZ - (MIN_CBSDIFF << 1);
cbsdiff = MIN_CBSDIFF;
ratio = drandom();
if(ratio < 0.07)
actidx = 1;
if(ratio < 0.005)
actidx = 0;
}
}
else{
cbs[0] = cbs[1];
cbs[1] = cbs[2];
cbs[2] += cbsdiff;
ratios[0] = ratios[1];
ratios[1] = ratios[2];
}
break;
}
return(set_commbufsiz(cbs[actidx]));
}
Int32
send_svrmsg(int fromfd, int tofd, UChar * msg, Int32 len)
{
Int32 i;
UChar lbuf[DEFCOMMBUFSIZ + 2];
if(len > DEFCOMMBUFSIZ - 4)
return(-1);
Uns32_to_xref(lbuf + 1, len);
memcpy(lbuf + 1 + 4, msg, len * sizeof(UChar));
if(QUEUEDOP)
return(append_to_queue(MESSAGETEXT, NULL, 0, lbuf + 1, 4 + len));
lbuf[0] = MESSAGETEXT;
i = 1 + 4 + len;
if(write_forced(tofd, lbuf, i) != i)
return(- errno);
bytes_transferred += ESTIM_PROT_OVERHEAD + i;
return(result());
}
UChar
send_buffer_to_server()
{
Int32 i, num, r;
UChar *cptr;
scbuf[0] = WRITETOTAPE;
cptr = scbuf;
num = commbufsiz + 1;
if(savefile){
cptr = cbuf;
num = commbufsiz;
i = write_forced(commfd, cptr, num);
return(i < num ? - errno : 0);
}
filenum_valid = wrfilenum_valid = rdfilenum_valid = NO;
if(QUEUEDOP){
r = append_to_queue(WRITETOTAPE, NULL, 0, cbuf, commbufsiz);
return(r);
}
if(write_forced(commfd, cptr, num) != num)
return(- errno);
bytes_transferred += ESTIM_PROT_OVERHEAD + num;
r = result();
return(r);
}
Int32
send_pending(Flag rawaccess)
{
Int32 i, num_pad_bytes = 0;
while(cbufptr > commbufsiz){
ER__(send_buffer_to_server(), i);
cbufptr -= commbufsiz;
memcpy(cbuf, cbuf + commbufsiz, cbufptr);
}
if(cbufptr > 0){
if(cbufptr < commbufsiz){
num_pad_bytes = commbufsiz - cbufptr;
memset(cbuf + cbufptr, 0, num_pad_bytes * sizeof(UChar));
if(rawaccess)
setnumbytesvalid(cbufptr);
}
ER__(send_buffer_to_server(), i);
}
cbufptr = 0;
if(!savefile && !rawaccess){
memset(cbuf, 0, commbufsiz * sizeof(UChar));
num_pad_bytes = MAXCOMMBUFSIZ - num_pad_bytes;
/* This was on the server side previously, what is inaccurate. */
/* The client should take care itself, that there is enough pad */
/* space after the data to find the end of packing mark */
for(; num_pad_bytes > 0; num_pad_bytes -= commbufsiz){
ER__(send_buffer_to_server(), i);
}
}
return(0);
}
Int32
send_to_server(UChar * buf, Int32 num)
{
UChar *cptr;
Int32 j, n;
if(num == 0)
return(0);
cptr = buf;
forever{
while(cbufptr > commbufsiz){
ER__(send_buffer_to_server(), j);
cbufptr -= commbufsiz;
memcpy(cbuf, cbuf + commbufsiz, cbufptr);
}
if(cbufptr + num <= commbufsiz){
memcpy(cbuf + cbufptr, cptr, num * sizeof(UChar));
cbufptr += num;
return(0);
}
n = commbufsiz - cbufptr;
if(n > 0){
memcpy(cbuf + cbufptr, cptr, n * sizeof(UChar));
}
ER__(send_buffer_to_server(), j);
cptr += n;
num -= n;
cbufptr = 0;
}
return(0);
}
Int32
bu_output(UChar * buf, Int32 num, AarParams * params)
{
if(savefile)
return(send_to_server(buf, num));
params->vars.bytes_saved += (Real64) num;
try_optimize_throughput();
bytes_wr_unkn_pos += num; /* assuming write will succeed */
if(bytes_wr_unkn_pos > MAX_NUM_WRITE_WITHOUT_POS_UPDATE){
filenum_valid = wrfilenum_valid = rdfilenum_valid = NO;
/* force a tape position update */
getwrcartandfile(params); /* from time to time to avoid */
filenum = wrfilenum; /* falling behind too far */
cart = wrcart;
}
return(send_to_server(buf, num));
}
Int32
receive_buffer_from_server()
{
Int32 i, j;
filenum_valid = wrfilenum_valid = rdfilenum_valid = NO;
if(QUEUEDOP){
i = append_to_queue(READFROMTAPE, cbuf, commbufsiz, NULL, 0);
j = post_process_rest_of_queue(0);
return(i ? i : j);
}
if(!savefile){
if( (i = send_cmd(READFROMTAPE)) )
return(i);
}
if(read_forced(commfd, cbuf, commbufsiz) != commbufsiz)
return(- errno);
bytes_transferred += ESTIM_PROT_OVERHEAD + commbufsiz;
return(result());
}
Int32
receive(UChar * buf, Int32 num, Int32 * num_read)
{
static Int32 nbytes_in_inbuf = 0;
static Flag no_more_to_receive = NO;
Int32 res, j;
UChar *cptr;
if(num == 0){
*num_read = 0;
return(0);
}
cptr = buf;
j = num;
forever{
if(cbufptr == 0){
if(!endofrec){
res = receive_buffer_from_server();
no_more_to_receive = NO;
}
else{
no_more_to_receive = YES;
res = 0;
}
if(!res){
nbytes_in_inbuf = (no_more_to_receive ? 0 : commbufsiz);
}
else{
if(res < 0){
*num_read = num - j;
return(res);
}
else{
if(res == ENDOFFILEREACHED || res == ENDOFTAPEREACHED){
getnumbytesvalid(&nbytes_in_inbuf);
endofrec = 1;
}
else{
*num_read = num - j;
return(res);
}
}
}
}
if(nbytes_in_inbuf - cbufptr <= j){
if(cbufptr < nbytes_in_inbuf)
memcpy(cptr, cbuf + cbufptr, nbytes_in_inbuf - cbufptr);
cptr += (nbytes_in_inbuf - cbufptr);
j -= (nbytes_in_inbuf - cbufptr);
cbufptr = 0;
if(endofrec){
*num_read = num - j;
return(ENDOFFILEREACHED);
}
if(j == 0){
*num_read = num;
return(0);
}
}
else{ /* in case of eof this is not really good code */
if(j > 0)
memcpy(cptr, cbuf + cbufptr, j);
cbufptr += j;
*num_read = num;
return(0);
}
}
return(0); /* NOTREACHED */
}
Int32
bu_input(UChar * buf, Int32 num, AarParams * params)
{
Int32 i, r;
i = receive(buf, num, &r);
if(i && i != ENDOFFILEREACHED && i != ENDOFTAPEREACHED){
fprintf(params->errfp,
T_("%sError: unexpected fault receiving from server: %s. Trying to continue ...\n"),
(i > 0 ? T_("Server ") : ""),
(i > 0 ? fault_string(i) : (UChar *) strerror(-i)));
}
try_optimize_throughput();
return(r);
}
Int32
getcartandfile(AarParams * params)
{
static Flag first_call = YES;
static Int32 prev_cart = -1;
UChar buf[10];
Int32 r = 0;
if(filenum_valid)
return(0);
if(QUEUEDOP){
r = append_to_queue(QUERYPOSITION, NULL, 7, NULL, 0);
if(first_call){
r = post_process_rest_of_queue(0);
}
}
else{
if( (r = send_cmd(QUERYPOSITION)) )
return(r);
if(read_forced(commfd, buf, 7) != 7)
return(- errno);
bytes_transferred += ESTIM_PROT_OVERHEAD + 7;
if( (r = result()) )
return(r);
xref_to_UnsN(&cart, buf, 24);
xref_to_Uns32(&filenum, buf + 3);
}
filenum_valid = YES;
bytes_wr_unkn_pos = 0;
if(first_call){
params->vars.firstcart = cart;
params->vars.firstfile = filenum;
first_call = NO;
}
if(cart != prev_cart){
params->vars.used_carts = add_to_Uns32Ranges(params->vars.used_carts,
cart, cart);
if(params->vars.used_carts)
pack_Uns32Ranges(params->vars.used_carts, NULL);
prev_cart = cart;
}
return(r);
}
Int32
getrdcartandfile(AarParams * params)
{
static Flag first_call = YES;
static Int32 prev_cart = -1;
UChar buf[10];
Int32 r = 0;
if(rdfilenum_valid)
return(0);
if(QUEUEDOP){
r = append_to_queue(QUERYRDPOSITION, NULL, 7, NULL, 0);
if(first_call){
r = post_process_rest_of_queue(0);
}
}
else{
if( (r = send_cmd(QUERYRDPOSITION)) )
return(r);
if(read_forced(commfd, buf, 7) != 7)
return(- errno);
bytes_transferred += ESTIM_PROT_OVERHEAD + 7;
if( (r = result()) )
return(r);
xref_to_UnsN(&rdcart, buf, 24);
xref_to_Uns32(&rdfilenum, buf + 3);
}
rdfilenum_valid = YES;
if(first_call){
params->vars.firstcart = rdcart;
params->vars.firstfile = rdfilenum;
first_call = NO;
}
if(cart != prev_cart){
params->vars.used_carts = add_to_Uns32Ranges(params->vars.used_carts,
rdcart, rdcart);
if(params->vars.used_carts)
pack_Uns32Ranges(params->vars.used_carts, NULL);
prev_cart = rdcart;
}
return(r);
}
Int32
getwrcartandfile(AarParams * params)
{
static Flag first_call = YES;
static Int32 prev_cart = -1;
UChar buf[10];
Int32 r = 0;
if(wrfilenum_valid)
return(0);
if(QUEUEDOP){
r = append_to_queue(QUERYWRPOSITION, NULL, 7, NULL, 0);
if(first_call)
r = post_process_rest_of_queue(0);
}
else{
if( (r = send_cmd(QUERYWRPOSITION)) )
return(r);
if(read_forced(commfd, buf, 7) != 7)
return(- errno);
bytes_transferred += ESTIM_PROT_OVERHEAD + 7;
if( (r = result()) )
return(r);
xref_to_UnsN(&wrcart, buf, 24);
xref_to_Uns32(&wrfilenum, buf + 3);
}
wrfilenum_valid = YES;
bytes_wr_unkn_pos = 0;
if(first_call){
params->vars.firstcart = wrcart;
params->vars.firstfile = wrfilenum;
first_call = NO;
}
if(cart != prev_cart){
params->vars.used_carts = add_to_Uns32Ranges(params->vars.used_carts,
wrcart, wrcart);
if(params->vars.used_carts)
pack_Uns32Ranges(params->vars.used_carts, NULL);
prev_cart = cart;
}
return(r);
}
Int32
getint(UChar cmd, Int32 * res, Int32 size, Flag wait_if_buffered)
{
UChar buf[10];
Int32 i;
if(QUEUEDOP){
i = append_to_queue(cmd, NULL, size, NULL, 0);
if(i)
return(i);
if(wait_if_buffered)
return(post_process_rest_of_queue(0));
return(i);
}
if( (i = send_cmd(cmd)) )
return(i);
if(read_forced(commfd, buf, size) != size)
return(- errno);
bytes_transferred += ESTIM_PROT_OVERHEAD + size;
if( (i = result()) )
return(i);
xref_to_UnsN(res, buf, (size << 3));
return(0);
}
Int32
getnumcarts(AarParams * params, Flag wait_if_buffered)
{
static UChar have_numcarts = 0;
Int32 i;
if(have_numcarts)
return(0);
i = getint(QUERYNUMCARTS, &numcarts, 3, wait_if_buffered);
if(i)
return(i);
have_numcarts = 1;
return(0);
}
Int32
gettapeblocksize(AarParams * params, Flag wait_if_buffered)
{
return(getint(QUERYTAPEBLOCKSIZE, &tapeblocksize, 4, wait_if_buffered));
}
Int32
getcartset(AarParams * params, Flag wait_if_buffered)
{
return(getint(QUERYCARTSET, &cartset, 3, wait_if_buffered));
}
Int32
getusedtapes(UChar ** retbuf, UChar * clientid)
{
UChar *cptr, buf[4];
Int32 l, n;
n = 1 + 1 + (l = strlen(clientid));
cptr = NEWP(UChar, n);
if(!cptr)
return(-errno);
cptr[0] = QUERYNEEDEDTAPES;
cptr[1] = (UChar) l;
memcpy(cptr + 2, clientid, l);
if(write_forced(commfd, cptr, n) != n)
return(- errno);
bytes_transferred += ESTIM_PROT_OVERHEAD + n;
if(read_forced(commfd, buf, 4) != 4)
return(- errno);
xref_to_Uns32(&l, buf);
l++;
cptr = RENEWP(cptr, UChar, l);
if(!cptr)
return(-errno);
if(read_forced(commfd, cptr, l) != l)
return(- errno);
bytes_transferred += ESTIM_PROT_OVERHEAD + 4 + l;
l--;
n = (Int32) cptr[l];
cptr[l] = '\0';
if(l && (n == COMMAND_OK)){
*retbuf = cptr;
}
else{
*retbuf = NULL;
free(cptr);
}
return(n);
}
Int32
getbufferwithcmd(UChar * buffer, Int32 len, Int32 cmd, Flag wait_if_buffered)
{
Int32 r = 0;
UChar *rbuf = NULL;
rbuf = NEWP(UChar, len);
if(!rbuf){
r = - errno;
GETOUT;
}
if(QUEUEDOP){
r = append_to_queue(cmd, rbuf, len, NULL, 0);
if(r)
GETOUT;
if(wait_if_buffered)
r = post_process_rest_of_queue(0);
GETOUT;
}
if( (r = send_cmd(cmd)) )
GETOUT;
if(read_forced(commfd, rbuf, len) != len){
r = - errno;
GETOUT;
}
bytes_transferred += ESTIM_PROT_OVERHEAD + len;
memcpy(buffer, rbuf, len * sizeof(UChar));
r = result();
getout:
ZFREE(rbuf);
return(r);
}
Int32
setcart(Int32 cartnum)
{
UChar buf[8], cmd;
Int32 i;
filenum_valid = rdfilenum_valid = NO;
cmd = SETCARTRIDGE;
if(cartnum < 0){
cartnum = - cartnum;
cmd = SETRAWCARTRIDGE;
}
buf[0] = cmd;
UnsN_to_xref(buf + 1, cartnum, 24);
reset_data_buffer;
if(QUEUEDOP){
i = append_to_queue(cmd, NULL, 0, buf + 1, 3);
return(i);
}
if(write_forced(commfd, buf, 4) != 4)
return(- errno);
bytes_transferred += ESTIM_PROT_OVERHEAD + 4;
if( (i = result()) )
return(i);
cart = cartnum;
return(0);
}
Int32
setcartset(Int32 cset)
{
UChar buf[8];
Int32 i;
filenum_valid = wrfilenum_valid = rdfilenum_valid = NO;
buf[0] = SETCARTSET;
UnsN_to_xref(buf + 1, cset, 24);
if(QUEUEDOP){
i = append_to_queue(buf[0], NULL, 0, buf + 1, 3);
return(i);
}
if(write_forced(commfd, buf, 4) != 4)
return(- errno);
bytes_transferred += ESTIM_PROT_OVERHEAD + 4;
if( (i = result()) )
return(i);
cartset = cset;
return(0);
}
Int32
setfilenum(Int32 filen)
{
UChar buf[8], cmd;
Int32 i;
cmd = SETFILE;
if(filen < 0){
filen = - filen;
cmd = SETRAWFILE;
}
filenum_valid = wrfilenum_valid = rdfilenum_valid = NO;
buf[0] = cmd;
Uns32_to_xref(buf + 1, filen & 0x7fffffff);
reset_data_buffer;
if(QUEUEDOP){
i = append_to_queue(cmd, NULL, 0, buf + 1, 4);
return(i);
}
if(write_forced(commfd, buf, 5) != 5)
return(- errno);
bytes_transferred += ESTIM_PROT_OVERHEAD + 5;
if( (i = result()) )
return(i);
filenum = filen;
return(0);
}
Int32
sendint(UChar cmd, Int32 n, Int32 size)
{
UChar buf[8];
Int32 i;
buf[0] = cmd;
UnsN_to_xref(buf + 1, n, size << 3);
if(QUEUEDOP){
i = append_to_queue(cmd, NULL, 0, buf + 1, size);
return(i);
}
size++;
if(write_forced(commfd, buf, size) != size)
return(- errno);
bytes_transferred += ESTIM_PROT_OVERHEAD + size;
return(result());
}
Int32
send_commbufsiz(Int32 cbsize)
{
return(sendint(SETCOMMBUFSIZ, cbsize, 4));
}
Int32
getnumbytesvalid(Uns32 * numbytes)
{
UChar buf[8];
Int32 i;
if(QUEUEDOP){
i = append_to_queue(GETNUMREADVALID, NULL, 0, buf + 1, 4);
return(i);
}
if( (i = send_cmd(GETNUMREADVALID)) )
return(i);
if(read_forced(commfd, buf, 4) != 4)
return(- errno);
bytes_transferred += ESTIM_PROT_OVERHEAD + 4;
xref_to_Uns32(numbytes, buf);
return(result());
}
Int32
send_identity(UChar * idstr)
{
UChar buf[129];
memset(buf, 0, 129 * sizeof(UChar));
buf[0] = CLIENTIDENT;
strcpy(buf + 1, idstr);
if(write_forced(commfd, buf, 129) != 129)
return(- errno);
bytes_transferred += ESTIM_PROT_OVERHEAD + 129;
return(result());
}
Int32
setnumbytesvalid(Uns32 numbytes)
{
UChar buf[8];
Int32 i;
Uns32_to_xref(buf + 1, numbytes);
if(QUEUEDOP){
i = append_to_queue(SETNUMWRITEVALID, NULL, 0, buf + 1, 4);
return(i);
}
buf[0] = SETNUMWRITEVALID;
if(write_forced(commfd, buf, 5) != 5)
return(- errno);
bytes_transferred += ESTIM_PROT_OVERHEAD + 5;
return(result());
}
Int32
skipfiles(Int32 numfiles)
{
UChar buf[8];
Int32 i;
filenum_valid = wrfilenum_valid = rdfilenum_valid = NO;
buf[0] = SKIPFILES;
Uns32_to_xref(buf + 1, numfiles & 0x7fffffff);
reset_data_buffer;
if(QUEUEDOP){
i = append_to_queue(SKIPFILES, NULL, 0, buf + 1, 4);
return(i);
}
if(write_forced(commfd, buf, 5) != 5)
return(- errno);
bytes_transferred += ESTIM_PROT_OVERHEAD + 5;
return(result());
}
Int32
simple_command(UChar cmd)
{
Int32 i;
if(QUEUEDOP){
i = append_to_queue(cmd, NULL, 0, NULL, 0);
return(i);
}
if(write_forced(commfd, &cmd, 1) != 1)
return(- errno);
bytes_transferred += ESTIM_PROT_OVERHEAD + 1;
return(result());
}
Int32
open_write(Int8 raw)
{
Int32 i;
UChar cmd;
filenum_valid = wrfilenum_valid = rdfilenum_valid = NO;
cmd = (raw ? OPENFORRAWWRITE : OPENFORWRITE);
endofrec = 0;
reset_data_buffer;
filenum = wrfilenum;
cart = wrcart;
if(QUEUEDOP)
return(append_to_queue(cmd, NULL, 0, NULL, 0));
reset_tape_io;
bytes_wr_unkn_pos = 0;
if( (i = send_cmd(cmd)) )
return(i);
return(result());
}
Int32
open_read(Int8 raw)
{
Int32 i;
UChar cmd;
filenum_valid = wrfilenum_valid = rdfilenum_valid = NO;
cmd = (raw ? OPENFORRAWREAD : OPENFORREAD);
endofrec = 0;
reset_data_buffer;
filenum = rdfilenum;
cart = rdcart;
if(QUEUEDOP)
return(append_to_queue(cmd, NULL, 0, NULL, 0));
reset_tape_io;
if( (i = send_cmd(cmd)) )
return(i);
return(result());
}
Int32
closetape(Int8 rewind)
{
Int32 i;
UChar cmd;
cmd = (rewind ? CLOSETAPE : CLOSETAPEN);
filenum_valid = wrfilenum_valid = rdfilenum_valid = NO;
reset_data_buffer;
if(QUEUEDOP){
i = append_to_queue(cmd, NULL, 0, NULL, 0);
#if 0 /* don't waste time waiting for the tape */
j = post_process_rest_of_queue(0);
#endif
return(i);
}
reset_tape_io;
if( (i = send_cmd(cmd)) )
return(i);
return(result());
}
Int32
request_newfile()
{
Int32 i;
filenum_valid = wrfilenum_valid = rdfilenum_valid = NO;
if(QUEUEDOP)
return(append_to_queue(REQUESTNEWFILE, NULL, 0, NULL, 0));
if( (i = send_cmd(REQUESTNEWFILE)) )
return(i);
return(result());
}
Int32
request_newcart()
{
Int32 i;
filenum_valid = wrfilenum_valid = rdfilenum_valid = NO;
if(QUEUEDOP)
return(append_to_queue(REQUESTNEWCART, NULL, 0, NULL, 0));
if( (i = send_cmd(REQUESTNEWCART)) )
return(i);
return(result());
}
Int32
erasetape()
{
Int32 i;
UChar cmd;
cmd = ERASETAPE;
if(QUEUEDOP){
i = append_to_queue(cmd, NULL, 0, NULL, 0);
return(i);
}
if( (i = send_cmd(cmd)) )
return(i);
return(result());
}
UChar *
fault_string(Int32 code)
{
Int32 i, sz;
if(!code)
return("");
sz = sizeof(fault_messages) / sizeof(fault_messages[0]);
for(i = 0; i < sz; i++){
if(code == (Int32) fault_messages[i].code)
return(fault_messages[i].msg);
}
return(T_("unknown fault"));
}
Int32
close_connection()
{
Int32 i, j;
filenum_valid = wrfilenum_valid = rdfilenum_valid = NO;
reset_data_buffer;
if(QUEUEDOP){
i = append_to_queue(GOODBYE, NULL, 0, NULL, 0);
j = post_process_rest_of_queue(0);
return(i ? i : j);
}
if( (i = send_cmd(GOODBYE)) )
return(i);
return(result());
}
Int32
request_client_backup(UChar * cmdname)
{
Int32 i;
Uns32 n;
UChar *buf, lenstr[5], exitst, *cptr, *cptr2;
n = strlen(cmdname);
if(n == 0){
errmsg(T_("Warning: Empty command supplied. Nothing happens"));
return(0);
}
buf = strapp("...", cmdname);
if(!buf)
return(-errno);
buf[0] = CLIENTBACKUP;
buf[1] = n % 251;
buf[2] = 1; /* suppress output if queued */
if(QUEUEDOP){
i = append_to_queue(CLIENTBACKUP, cbuf, 5, buf + 1, n + 2);
free(buf);
return(i);
}
buf[2] = 0;
i = n + 3;
i = (write_forced(commfd, buf, i) != i);
free(buf);
bytes_transferred += ESTIM_PROT_OVERHEAD + i;
if(i)
return(i);
if(read_forced(commfd, lenstr, 5) != 5)
return(- errno);
bytes_transferred += ESTIM_PROT_OVERHEAD + 5;
exitst = lenstr[0];
xref_to_Uns32(&n, lenstr + 1);
buf = NEWP(UChar, n + 1);
if(!buf){
errmsg("Error: No memory, where absolutely needed");
}
if(read_forced(commfd, buf, n) != n)
return(- errno);
buf[n] = '\0';
bytes_transferred += ESTIM_PROT_OVERHEAD + n;
if(n > 0){
cptr = buf;
forever{
if(!(*cptr))
break;
cptr2 = strchr(cptr, '\n');
if(cptr2)
*cptr2 = '\0';
if(c_f_verbose)
fprintf(stdout, "%s: ", servername);
fprintf(stdout, "%s\n", cptr);
if(cptr2)
cptr = cptr2 + 1;
else
break;
}
}
free(buf);
if(n < 1 && exitst){
fprintf(stderr,
T_(">>> Remote process exited with status %d, but produced no output.\n"),
exitst);
}
return(result() | (exitst << 16));
}
UChar **
get_file_list(FILE * fp)
{
UChar *line;
UChar **filelist = NULL;
Int32 num_files = 0;
while(!feof(fp)){
line = fget_alloc_str(fp);
if(!line)
continue;
chop(line);
filelist = ZRENEWP(filelist, UChar *, num_files + 2);
if(!filelist){
errmsg("Error: memory exhausted by the list of files");
exit(54);
}
filelist[num_files] = line;
num_files++;
filelist[num_files] = NULL;
}
return(filelist);
}
void
pre_extended_verbose(UChar * str, AarParams * params)
{
if(! savefile){
if(bu_create){
E__(getwrcartandfile(params));
filenum = wrfilenum;
cart = wrcart;
}
else{
if(server_can_sendrdpos){
E__(getrdcartandfile(params));
filenum = rdfilenum;
cart = rdcart;
}
else{
E__(getcartandfile(params));
rdfilenum = filenum; /* for backward compatibility. In open_read */
rdcart = cart; /* rdxxxx must be set correctly */
}
}
params->vars.actfile = filenum;
params->vars.actcart = cart;
}
}
void
extended_verbose(UChar * str, AarParams * params)
{
static UChar *lstr = NULL; /* not multithreading safe */
static Int32 nall = 0;
FILE *fp;
UChar mtimestr[30];
Int32 l;
fp = ((params->mode == MODE_CREATE && save_stdio) ?
params->errfp : params->outfp);
if(verbose){
if(verbose & VERBOSE_LOCATION)
fprintf(fp, "%s%s%d%s", servername, PORTSEP, (int) portnum, LOCSEP);
if(verbose & VERBOSE_CART_FILE)
fprintf(fp, "%d.%d", (int) params->vars.actcart,
(int) params->vars.actfile);
if(verbose & VERBOSE_UID)
fprintf(fp, UIDSEP "%d", (int) params->vars.uid);
if(verbose & VERBOSE_MTIME){
time_t_to_intstr(params->vars.mtime, mtimestr);
fprintf(fp, MTIMESEP "%s", mtimestr);
}
fprintf(fp, ": ");
}
l = strlen(str) + 1;
if(l > nall)
lstr = ZRENEWP(lstr, UChar, nall = l);
if(!lstr){
errmsg("Error: No memory, where absolutely needed");
return;
}
strcpy(lstr, str);
rm_backspace(lstr);
fprintf(fp, "%s", lstr);
}
void
normal_verbose(UChar * str, AarParams * params)
{
static UChar *lstr = NULL; /* not multithreading safe */
static Int32 nall = 0;
FILE *fp;
Int32 l;
l = strlen(str) + 1;
if(l > nall)
lstr = ZRENEWP(lstr, UChar, nall = l);
if(!lstr){
errmsg("Error: No memory, where absolutely needed");
return;
}
strcpy(lstr, str);
rm_backspace(lstr);
fp = ((params->mode == MODE_CREATE && save_stdio) ?
params->errfp : params->outfp);
fprintf(fp, "%s", lstr);
}
void
write_reportfile(UChar * filename, AarParams * params, Int32 rc)
{
FILE *fp;
UChar numbuf[50];
Int32 i;
i = unlink(filename);
if(i < 0 && errno && errno != ENOENT){
errmsg(T_("Error: Cannot remove report file \"%s\"\n"), filename);
return;
}
fp = fopen(filename, "w");
if(!fp){
errmsg(T_("Error: Cannot write report file \"%s\"\n"), filename);
return;
}
fprintf(fp, T_("Backup statistics:\n"));
fprintf(fp, "==================\n\n");
if(identitystr)
fprintf(fp, T_("Client Identifier: %s\n\n"), identitystr);
fprintf(fp, T_("Exit Status: %ld\n\n"), rc);
fprintf(fp, T_("Start-Time: %s\n"), params->vars.startdate);
fprintf(fp, T_("End-Time: %s\n\n"), params->vars.enddate);
fprintf(fp, T_("Backup Server: %s\n"), servername);
fprintf(fp, T_("Backup Port: %d\n"), (int) portnum);
fprintf(fp, T_("First Cartridge used: %ld\n"), params->vars.firstcart);
fprintf(fp, T_("First Tapefile accessed: %ld\n"), params->vars.firstfile);
fprintf(fp, T_("Last Cartridge used: %ld\n"), params->vars.lastcart);
fprintf(fp, T_("Last Tapefile accessed: %ld\n"), params->vars.lastfile);
fprintf(fp, "%-31s", T_("Used Cartridges:"));
fprint_Uns32Ranges(fp, params->vars.used_carts, 0); fprintf(fp, "\n\n");
fprintf(fp, T_("Stored filesystem entries: %ld\n"), params->vars.num_fsentries);
fprintf(fp, T_("Bytes written to media: %s\n"),
Real64_to_intstr(params->vars.bytes_saved, numbuf));
fprintf(fp, T_("Sum of filesizes: %s\n"),
Real64_to_intstr(params->vars.sum_filesizes, numbuf));
fprintf(fp, T_("Sum of compressed filesizes: %s\n"),
Real64_to_intstr(params->vars.sum_compr_filesizes, numbuf));
fprintf(fp, T_("Sum of uncompressed filesizes: %s\n"),
Real64_to_intstr(params->vars.sum_uncompr_filesizes, numbuf));
if(params->vars.sum_compr_filesizes > 0){
fprintf(fp, T_("Total compression factor: %.2f\n"),
(float) params->vars.sum_filesizes /
(float) (params->vars.sum_compr_filesizes
+ params->vars.sum_uncompr_filesizes));
fprintf(fp, T_("Real compression factor: %.2f\n"),
(float) (params->vars.sum_filesizes -
params->vars.sum_uncompr_filesizes) /
(float) (params->vars.sum_compr_filesizes));
}
fprintf(fp, "\n");
fclose(fp);
}
Int32 /* buffered mode not supported for this function */
add_written_tapes(Uns32Range ** alltapes)
{
Uns32Range *newtapes = NULL;
UChar *instr = NULL, buf[4];
Int32 i, r = 0;
if( (i = send_cmd(QUERYWRITTENTAPES)) )
return(i);
if(read_forced(commfd, buf, 4) != 4)
return(- errno);
bytes_transferred += ESTIM_PROT_OVERHEAD + 4;
xref_to_Uns32(&i, buf);
if(!i)
return(result());
instr = NEWP(UChar, i + 1);
if(!instr){
r = - errno;
GETOUT;
}
if( (r = (read_forced(commfd, instr, i) != i ? - errno : 0)) )
GETOUT;
instr[i] = '\0';
bytes_transferred += ESTIM_PROT_OVERHEAD + i;
if(!(newtapes = sscan_Uns32Ranges(instr, 0, 0, NULL, NULL))){
r = - errno;
GETOUT;
}
if(alltapes)
r = merge_Uns32Ranges(alltapes, newtapes);
else{
*alltapes = newtapes;
newtapes = NULL;
}
getout:
ZFREE(instr);
ZFREE(newtapes);
i = result();
return(r ? r : i);
}
Int32
create_command_queue()
{
Int32 required_entry_mem, i;
#ifndef _WIN32
struct rlimit rlim;
#endif
max_outarglen = max_inresultlen = commbufsiz + 4;
required_entry_mem = max_outarglen + max_inresultlen + sizeof(QueueEntry);
#ifndef _WIN32
i = getrlimit(RLIMIT_STACK, &rlim);
if(i){
errmsg("Error: cannot get maximum available memory");
}
queuelen = rlim.rlim_cur / required_entry_mem / 4;
#else
queuelen = 1000000 / required_entry_mem / 4;
#endif
if(queuelen < 1){
comm_mode = SERIAL_OPERATION;
}
else{
if(required_entry_mem * queuelen > MAX_QUEUE_MEMORY){
queuelen = MAX_QUEUE_MEMORY / required_entry_mem;
}
if(max_outarglen * queuelen < 100000){
comm_mode = SERIAL_OPERATION;
}
else{
allocated_outmem = max_outarglen * queuelen;
allocated_inmem = max_inresultlen * queuelen;
outbufmem = NEWP(UChar, allocated_outmem);
inbufmem = NEWP(UChar, allocated_inmem);
queue_entries = NEWP(QueueEntry, queuelen);
if(!outbufmem || !inbufmem || !queue_entries){
errmsg("Error: Strange. Can't get memory. Disabling queued operation");
comm_mode = SERIAL_OPERATION;
allocated_outmem = allocated_inmem = 0;
ZFREE(outbufmem);
ZFREE(inbufmem);
ZFREE(queue_entries);
}
else{
params.readfunc = queued_read;
params.writefunc = queued_write;
memset(queue_entries, 0, queuelen * sizeof(QueueEntry));
}
}
}
max_queuelen = queuelen;
queueent_done_idx = queueent_requested_idx = queueent_processed_idx
= queue_insert_idx = 0;
queue_commbufsiz = commbufsiz;
return(0);
}
Int32
delete_command_queue()
{
Int32 r = 0;
if(QUEUEDOP)
r = post_process_rest_of_queue(0);
comm_mode = SERIAL_OPERATION;
ZFREE(outbufmem);
ZFREE(inbufmem);
ZFREE(queue_entries);
queuelen = 0;
allocated_outmem = allocated_inmem = 0;
params.readfunc = read_forced;
params.writefunc = write_forced;
return(r);
}
void
give_help()
{ /* automatically generated: */
{
char *l[323];
int i;
l[0]=T_("Description");
l[1]=T_("===========");
l[2]="";
l[3]=T_("This program is used to maintain archives on a backup server");
l[4]=T_("host or in a file. Archives can be created, extracted or their");
l[5]=T_("contents be listed. At least one of the following flags has to");
l[6]=T_("be supplied:");
l[7]="";
l[8]=T_(" -c to create an archive");
l[9]="";
l[10]=T_(" -x to extract from an archive");
l[11]="";
l[12]=T_(" -t to list the contents of an archive");
l[13]="";
l[14]=T_(" -d to verify (compare) the contents of an archive");
l[15]="";
l[16]=T_(" -C to set a certain cartridge on the backup server");
l[17]=T_(" (makes only sense extracting or listing with -x or");
l[18]=T_(" -t, the writing position can't be changed by clients)");
l[19]="";
l[20]=T_(" -F to set a certain file on the backup server's tape");
l[21]=T_(" (the same applies as for -C)");
l[22]="";
l[23]=T_(" -q to printout the current cartridge and tape file number");
l[24]=T_(" on the backup server");
l[25]="";
l[26]=T_(" -Q to printout the cartridge and tape file number for the");
l[27]=T_(" the next write access on the backup server");
l[28]="";
l[29]=T_(" -X followed by the full path name of a program to be started on");
l[30]=T_(" the client. This can be used to trigger a backup remotely.");
l[31]=T_(" If the program needs arguments, the command together with");
l[32]=T_(" the arguments has to be enclosed by quotes");
l[33]="";
l[34]=T_(" -I to printout an index of the backups written to the current");
l[35]=T_(" cartridge");
l[36]="";
l[37]=T_(" -w to check the status of the streamer on the server side, e.g.");
l[38]=T_(" whether it is ready and waiting for requests to service");
l[39]="";
l[40]=T_(" -G to request a new cartridge for the next writing operation.");
l[41]=T_(" If the current writing position is already at the beginning");
l[42]=T_(" of a new or reused tape, nothing happens");
l[43]="";
l[44]=T_(" -D <destination> to make an exact copy of a tape to another one");
l[45]=T_(" (duplicate). See below how to specify the destination tape.");
l[46]=T_(" Duplication can be either from one cartridge to another on");
l[47]=T_(" the same server, or from one server to another one. When");
l[48]=T_(" copying to the same server chunks of data are stored in a");
l[49]=T_(" temporary directory on the client, where the command is");
l[50]=T_(" started, what should preferably be the source server");
l[51]="";
l[52]=T_(" -M <message> send a message to the server. Messages will in the");
l[53]=T_(" most cases contain whitespace, so they should be enclosed");
l[54]=T_(" in quotes. Server messages should be sent to the single");
l[55]=T_(" stream server (port), the multi stream server might hang");
l[56]=T_(" receiving a message due to systematical reasons. Several");
l[57]=T_(" messages can be put into the string. They must be separated");
l[58]=T_(" by a real newline character or the usual C-like \\n .");
l[59]=T_(" The following messages are currently supported:");
l[60]="";
l[61]=T_(" PreciousTapes: <list-of-tapes>");
l[62]=T_(" The list of tapes is inserted into the table");
l[63]=T_(" with the tapes, that are crucial for clients");
l[64]=T_(" to restore all files, that are listed in all");
l[65]=T_(" existing index files. These tapes will not be");
l[66]=T_(" overwritten until explicitly permitted. This");
l[67]=T_(" message is generated automatically and should");
l[68]=T_(" not be used in other user contexts");
l[69]="";
l[70]=T_(" ReuseTapes: <list-of-tapes>");
l[71]=T_(" The opposite of PreciousTapes. Sending this");
l[72]=T_(" message permits the server to overwrite the");
l[73]=T_(" listed tapes, though they are crucial for");
l[74]=T_(" some client");
l[75]="";
l[76]=T_(" TapesReadOnly: <list-of-tapes>");
l[77]=T_(" The list of tapes is inserted into the file");
l[78]=T_(" listing the files, that should not be written");
l[79]=T_(" any more for whatever reason");
l[80]="";
l[81]=T_(" TapesReadWrite: <list-of-tapes>");
l[82]=T_(" This reverts the status of tapes set read-only");
l[83]=T_(" to read-write, the opposite of TapesReadOnly");
l[84]="";
l[85]=T_(" CartridgeReady");
l[86]=T_(" When an operator is requested to do something");
l[87]=T_(" the server is waiting for, this message can be");
l[88]=T_(" sent to trigger the server to proceed. This");
l[89]=T_(" message has the same effect as the cartready");
l[90]=T_(" command");
l[91]="";
l[92]=T_(" DeleteClient: <client-identifier>");
l[93]=T_(" The tapes, that are marked as reserved for a");
l[94]=T_(" client to recover all the data in his indexes,");
l[95]=T_(" are freed. That is, the appropriate line is");
l[96]=T_(" removed from the server's precious_tapes file");
l[97]="";
l[98]="";
l[99]=T_("-c, -x, -t, -d, -X, -D and -I are mutual exclusive. The other");
l[100]=T_("options can be supplied as needed. To set the cartridge and/or");
l[101]=T_("the tape file on the backup server is only making sense when not");
l[102]=T_("creating an archive. The serial order of writing to tape is");
l[103]=T_("handled by the server machine independently of the client.");
l[104]="";
l[105]="";
l[106]=T_("Filenames");
l[107]="";
l[108]=T_("The names of the files and directories, that have to be put");
l[109]=T_("into or extracted from an archive are by default read from the");
l[110]=T_("standard input. If filenames are supplied in the command line");
l[111]=T_("the -a flag is given when extracting, standard input is not read.");
l[112]=T_("The same applies, when filenames are read from a file with the");
l[113]=T_("-T option. When reading the names from a file or from standard");
l[114]=T_("input, they must be given one per line. If a name contains");
l[115]=T_("special characters (like newline or nonprintable ones), they");
l[116]=T_("have to be specified using backslash-sequences like in C-code,");
l[117]=T_("e.g. \\n for newline.");
l[118]=T_("In save mode (-c) filenames can be prefixed with character");
l[119]=T_("sequences, that have special meanings (no space between prefix");
l[120]=T_("and filename):");
l[121]="";
l[122]=T_(" /../ The file is not saved with all attributes present in");
l[123]=T_(" the inode, but only the contents are saved. This might");
l[124]=T_(" be useful for saving raw-devices");
l[125]=T_(" //../ With /../ the configured processing is not applied to");
l[126]=T_(" the file contents for safety reasons. With this prefix");
l[127]=T_(" processing can be forced nonetheless");
l[128]=T_(" ||| and a mandatory space character indicates, that the");
l[129]=T_(" following characters up to (but not including) another");
l[130]=T_(" triple bar ||| should be interpreted as a shell command,");
l[131]=T_(" that is started and whose standard output is written to");
l[132]=T_(" the backup. At restore time the command following the");
l[133]=T_(" second triple bar is started and the data stream read");
l[134]=T_(" at backup time is written to it's standard input. This");
l[135]=T_(" might be useful for saving e.g. databases. The second");
l[136]=T_(" command may be terminated by a triple sharp ###, that");
l[137]=T_(" starts an optional comment. Example:");
l[138]=T_(" ||| pg_dumpall ||| psql db_tmpl ### Store Postgres DBs");
l[139]="";
l[140]="";
l[141]=T_("Destination");
l[142]="";
l[143]=T_("The destination tape for the duplicate operation can be given");
l[144]=T_("in two ways: either with the options -h, -p, -C and -k following");
l[145]=T_("the -D immediately without space and enclosed in quotes, so that");
l[146]=T_("they appear as an own argument list in one real argument, e.g.:");
l[147]=T_(" -D' -C 5 -h targethost -p targetport'");
l[148]=T_("(double quotes are of course also possible ...).");
l[149]=T_("The second format is as follows:");
l[150]=T_(" [<targetcart>][@<targethost>][%targetport>][:<targetcryptkeyfile>]");
l[151]=T_("At least one of the specifiers must be present. Examples:");
l[152]=T_(" 5@otherhost 5%2990:/keyfile/for/target/server @otherhost%2970");
l[153]=T_("If one of the specifiers is omitted, it is assumed identical with");
l[154]=T_("the copy source specified in the normal options -h, -p, -C and -k.");
l[155]=T_("Copying a tape to itself is prevented.");
l[156]="";
l[157]="";
l[158]=T_("More options in alphabetical order:");
l[159]="";
l[160]=T_(" - in combination with -c: read standard input and");
l[161]=T_(" write it to tape, in combination with -x: read");
l[162]=T_(" tape and write it to standard output");
l[163]="";
l[164]=T_(" -A <time> process files (save or extract) modified after");
l[165]=T_(" the given time in seconds since 1.1.1970 00:00");
l[166]="";
l[167]=T_(" -a in combination with -x: extract all files and");
l[168]=T_(" directories in the archive");
l[169]="";
l[170]=T_(" -B <time> process files (save or extract) modified before");
l[171]=T_(" the given time in seconds since 1.1.1970 00:00");
l[172]="";
l[173]=T_(" -b don't enter buffering mode");
l[174]="";
l[175]=T_(" -e <errlog> Use the file <errlog> to write error messages to");
l[176]=T_(" instead of the standard error output");
l[177]="";
l[178]=T_(" -f <file> write to or read from a file instead of querying");
l[179]=T_(" the backup server");
l[180]="";
l[181]=T_(" -g while extracting/reading: ignore leading garbage,");
l[182]=T_(" suppress error messages at the beginning. This");
l[183]=T_(" is useful when extracting from tape files, that");
l[184]=T_(" are not the first ones of a whole archive.");
l[185]="";
l[186]=T_(" -H <header> put the supplied informational header to the begin");
l[187]=T_(" of the backup. If a - is supplied (no space may");
l[188]=T_(" follow -H i.e. -H-) the information is read from");
l[189]=T_(" the first line of stdin. Backslash sequences of");
l[190]=T_(" C-like style are replaced");
l[191]="";
l[192]=T_(" -h <host> use the backup server with the name <host>");
l[193]=T_(" default host is the machine with the name");
l[194]=T_(" backuphost");
l[195]="";
l[196]=T_(" -i while extracting: ignore the stored ownership and");
l[197]=T_(" do not restore it");
l[198]="";
l[199]=T_(" -j when starting to write: request starting a new");
l[200]=T_(" tape file");
l[201]="";
l[202]=T_(" -K when packing, do not keep the access time of the");
l[203]=T_(" file. By default after packing a filesystem entry");
l[204]=T_(" it's previous atime is restored");
l[205]="";
l[206]=T_(" -k <file> use the contents of the given file as encryption");
l[207]=T_(" key for authenticating to the server");
l[208]="";
l[209]=T_(" -l for each packed or unpacked filename, if sending");
l[210]=T_(" to or receiving from a backup server in verbose");
l[211]=T_(" mode in combination with -n:");
l[212]=T_(" printout server name and port number at the");
l[213]=T_(" beginning of the line, e.g.: orion%2988!");
l[214]="";
l[215]=T_(" -N <file> while archiving: ignore files with a modification");
l[216]=T_(" time before the one of the given file, only save");
l[217]=T_(" newer files or such with the same age in seconds");
l[218]="";
l[219]=T_(" -n for each packed or unpacked filename, if sending");
l[220]=T_(" to or receiving from a backup server in verbose");
l[221]=T_(" mode:");
l[222]=T_(" printout cartridge and tape file number at the");
l[223]=T_(" beginning of the line, e. g.: 7.15: <filename>");
l[224]=T_(" In combination with -X: precede each line of");
l[225]=T_(" output received from the remotely started program");
l[226]=T_(" with the identifier of the remote host and a");
l[227]=T_(" colon, e. g.: darkstar: Full backup finished.");
l[228]="";
l[229]=T_(" -O for each packed file creating a backup in verbose");
l[230]=T_(" mode: printout the user-ID of the file owner at");
l[231]=T_(" the beginning of the line prefixed with a bar |");
l[232]=T_(" eventually behind cartridge and file number");
l[233]="";
l[234]=T_(" -o <uid> archive or extract only files owned by the user");
l[235]=T_(" with the given user-ID (an integer)");
l[236]="";
l[237]=T_(" -p <portno> use a different port number for communicating with");
l[238]=T_(" the backup server. Default is TCP-Port 2988");
l[239]="";
l[240]=T_(" -R pack or extract directories recursively with all");
l[241]=T_(" of their contents");
l[242]="";
l[243]=T_(" -r use filenames relative to the current directory,");
l[244]=T_(" whether they start with a slash or not. If -r");
l[245]=T_(" is given more then 1 time, also let symlinks");
l[246]=T_(" originally pointing to absolute paths now point");
l[247]=T_(" to paths relative to the directory, where the");
l[248]=T_(" symlink will be created");
l[249]="";
l[250]=T_(" -S <cartset> The cartridge set to use, where <cartset> is the");
l[251]=T_(" number of a valid cartridge set on the server");
l[252]=T_(" side. Default is 1. This option makes sense only");
l[253]=T_(" when creating backups with -c");
l[254]="";
l[255]=T_(" -s <filepat> do not attempt processing on files matching the");
l[256]=T_(" given filename pattern. This parameter may");
l[257]=T_(" appear several times");
l[258]="";
l[259]=T_(" -T <file/dir> read the filenames to process from the <file>.");
l[260]=T_(" The filenames must be separated by whitespace.");
l[261]=T_(" If whitespace is part of a filename, it has to");
l[262]=T_(" be enclosed by double quotes. Double quotes or");
l[263]=T_(" backslashes within the filename have to be");
l[264]=T_(" preceded by a backslash. In combination with");
l[265]=T_(" -D: the tape files to be copied are temporarily");
l[266]=T_(" stored in the given directory instead of the");
l[267]=T_(" default directory /tmp");
l[268]="";
l[269]=T_(" -U for each packed file creating a backup in verbose");
l[270]=T_(" mode: printout the modification time of the file");
l[271]=T_(" in seconds since 1970/1/1 0:00 at the beginning");
l[272]=T_(" of the line prefixed with a tilde ~ eventually");
l[273]=T_(" behind cartridge number, file number and owner");
l[274]="";
l[275]=T_(" -u while extracting: remove existing files with the");
l[276]=T_(" same name as found in the archive. Otherwise");
l[277]=T_(" no existing files are overwritten");
l[278]="";
l[279]=T_(" -V <file> write a report containing statistics at the end of");
l[280]=T_(" a backup to the <file>");
l[281]="";
l[282]=T_(" -v verbose mode: print the filenames while creating");
l[283]=T_(" or extracting, be a little more verbose while");
l[284]=T_(" listing contents. If -v is the only given flag:");
l[285]=T_(" print out software name and version");
l[286]="";
l[287]=T_(" -W <id> identify as <id> to the server. This is needed when");
l[288]=T_(" connecting a multi-stream server to distinguish");
l[289]=T_(" between the clients. Default is the string");
l[290]=T_(" \"<client-program>\"");
l[291]="";
l[292]=T_(" -z <z> <uz> use <z> as the command, that is used to process");
l[293]=T_(" files, <uz> for the corresponding unprocess.");
l[294]=T_(" The command has to read from stdin and to write");
l[295]=T_(" to stdout. If arguments have to be supplied to");
l[296]=T_(" <z> and/or <uz>, don't forget to use quotes. If");
l[297]=T_(" built-in compression is desired, the command for");
l[298]=T_(" processing has to start with a dot (.), followed");
l[299]=T_(" by a space and a number ranging from 1 to 9, that");
l[300]=T_(" specifies the compression level. If an additional");
l[301]=T_(" external command should process the data, it may");
l[302]=T_(" follow, separated from the compression level by");
l[303]=T_(" whitespace. The order of processing is: First the");
l[304]=T_(" external program processes the data, then built-in");
l[305]=T_(" compression is applied. An empty string has to be");
l[306]=T_(" supplied for <uz> (or any other dummy is ok), if");
l[307]=T_(" only built-in compression is desired.");
l[308]=T_(" Examples for <z>:");
l[309]=T_(" gzip (run external command gzip),");
l[310]=T_(" \"gzip -2\" (the same with an argument),");
l[311]=T_(" \". 8\" (only built-in compression level 8),");
l[312]=T_(" \". 3 __descrpt -k /my/key\" (run command __descrpt");
l[313]=T_(" and apply built-in compression level 3)");
l[314]="";
l[315]=T_(" -Z while printing out the contents: check those files");
l[316]=T_(" in the archive that are processed for integrity.");
l[317]=T_(" While creating an archive: write a CRC32 checksum");
l[318]=T_(" for each file, file contents or command output to");
l[319]=T_(" the backup stream");
l[320]="";
l[321]="";
l[322]=T_(" -? to printout this text");
for(i=0;i<323;i++)
fprintf(stdout,"%s\n",l[i]);
}
exit(0);
}
void
usage(UChar * prnam)
{
prnam = FN_BASENAME(prnam);
errmsg(T_("usage: %s -{c|x|t|d|I|D<dest>|M<msg>} [ -[RraunlOUvgiqQwZbGK] ] \\\n"), prnam);
errmsg(T_(" [ -h <backup-server> ] \\\n"));
errmsg(T_(" [ -z <proccmd> <unproccmd> ] \\\n"));
errmsg(T_(" [ -T <to-extract-filename> ] \\\n"));
errmsg(T_(" [ -C <cartridge-number> ] \\\n"));
errmsg(T_(" [ -F <filenumber-on-tape> ] \\\n"));
errmsg(T_(" [ -f <archive-filename> ] \\\n"));
errmsg(T_(" [ -e <errorlog-filename> ] \\\n"));
errmsg(T_(" [ -p <server-port-number> ] \\\n"));
errmsg(T_(" [ -N <newer-than-filename> ] \\\n"));
errmsg(T_(" [ -o <user-ID> ] \\\n"));
errmsg(T_(" [ -W <client-identifier> ] \\\n"));
errmsg(T_(" [ -k <encryption-key-file> ] \\\n"));
errmsg(T_(" [ -s <dont-process-filepattern> [ -s ... ] ] \\\n"));
errmsg(T_(" [ -V <statistics-report-file> ] \\\n"));
errmsg(T_(" [ -A <after-time-seconds> ] \\\n"));
errmsg(T_(" [ -B <before-time-seconds> ] \\\n"));
errmsg(T_(" [ <files> <directories> ... ]\n"));
errmsg(T_("\n %s -X <program> \\\n"), prnam);
errmsg(T_(" [ -h <backup-client> ]\n"));
errmsg(T_("\n %s -\\? (to get help)\n"), prnam);
exit(1);
}
static Int32
start_server_comm(Int32 timeout)
{
Int32 i;
time_t starttime;
UChar cmd;
starttime = time(NULL);
fcntl(commfd, F_SETFD, fcntl(commfd, F_GETFD) | 1);
forever{
i = logon_to_server(commfd, commfd, GREETING_MESSAGE,
VERSION_MESSAGE_KEY, &serverversion,
#ifdef HAVE_DES_ENCRYPTION
AUTH_USE_DES
#else
0
#endif
);
if(!i)
break;
if((i && time(NULL) - starttime > timeout) || i == AUTHENTICATION)
return(i);
ms_sleep(drandom() * 400 + 100);
}
if(!serverversion)
serverversion = LAST_SERVER_WITHOUT_VERSIONMSG;
if(compare_version_strings(serverversion, "3.2") >= 0)
server_can_sendtbufsiz = YES;
if(compare_version_strings(serverversion, "3.2.4") >= 0)
server_can_setcbufsiz = server_can_sendid = YES;
if(compare_version_strings(serverversion, "3.2.7") >= 0)
server_can_sendutapes = YES;
if(compare_version_strings(serverversion, "3.3") >= 0)
server_can_userid = YES;
if(compare_version_strings(serverversion, "3.3.1") >= 0)
server_can_auth = YES;
if(compare_version_strings(serverversion, "3.3.6") >= 0)
server_can_sendrdpos = YES;
if(server_can_auth){
cmd = AUTHENTICATE;
if(write_forced(commfd, &cmd, 1) != 1)
i = AUTHENTICATION;
else
i = authenticate_client(commfd, commfd, NO, "", NO, AUTHENTICATION,
#ifdef HAVE_DES_ENCRYPTION
AUTH_USE_DES
#else
0
#endif
| AUTH_NOCONFIRM, 0);
if(i){
errmsg(T_("Error: Server did not proof authentication correctly. "));
errmsg(T_("Possible authentication key spoofing.\n"));
return(i);
}
}
return(0);
}
main(int argc, char ** argv)
{
Int32 i, j, n;
Int32 gexitst = 0;
struct stat statb;
FILE *fp;
UChar c, buf[BUFFERSIZ], *line;
UChar givehelp = 0;
UChar querypos = 0;
UChar querywrpos = 0;
UChar *req_portstr = NULL;
UChar **filelist = NULL;
UChar **files = NULL;
UChar *found_files = NULL;
UChar *uidstr = NULL;
Int32 s_cart = -1;
Int32 s_file = -1;
Int32 s_cset = 1;
Int32 num_unused = 0;
UChar *newer_than_file = NULL;
UChar **unused_args = NULL;
UChar *cptr = NULL;
UChar *reportfile = NULL;
UChar *older_than_sec = NULL;
UChar *newer_than_sec = NULL;
int i1, i2, n1;
int eexitst = 15;
time_t timemem;
#ifdef ENABLE_NLS
setlocale (LC_ALL, "");
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);
#endif
gargv = (UChar **) argv;
SETZERO(null_timeval);
aarparams_init(¶ms);
params.program = find_program(argv[0]);
strcpy(params.vars.startdate, actimestr());
sigemptyset(¶ms.blocked_signals);
sigprocmask(SIG_BLOCK, ¶ms.blocked_signals, NULL);
if(sizeof(Uns32) > 4){
fprintf(stderr, T_("Error: long int is wider than 4 Bytes. Inform author about machine type.\n"));
exit(3);
}
i = goptions(-argc, (UChar **) argv,
"b:c;b:x;b:t;b:d;b:I;b:w;s:D;b:R;c:r;b:v;b:n;b:O;b:l;b:u;b:a;s:h;b:;"
"s2:z;s:M;s:T;s:p;s:e;s:f;i:C;i:F;i:S;b:g;b:i;b:?;b:q;b:Q;b:J;*;b:Z;"
"s:X;s:N;s:o;s:k;b:b;s:s;s:V;s:B;s:A;b:j;b:G;b:E;s:H;s:W;b:U;b:K",
&bu_create, &bu_extract, &bu_contents, &bu_verify,
&bu_index, &bu_ready, &bu_duptapeargs,
¶ms.recursive, ¶ms.relative,
&verbose, &c_f_verbose, &o_verbose, &l_verbose,
¶ms.unlink, &allfiles, &servername, &std_io,
¶ms.zipcmd, ¶ms.unzipcmd, &servermsg,
&toextractfilename, &req_portstr, &errorlogfile,
&savefile, &s_cart, &s_file, &s_cset,
¶ms.skip_garbage, ¶ms.ignoreown,
&givehelp, &querypos, &querywrpos, &bu_junction,
&num_unused, &unused_args, ¶ms.check,
&clientprog, &newer_than_file, &uidstr,
&cryptfile, &noqueued, &cptr, &reportfile,
&older_than_sec, &newer_than_sec, &req_newfile,
&req_newcart, &long_errmsgs, &infoheader,
&identitystr, &u_verbose, ¶ms.dont_keep_atime);
if(i)
usage(argv[0]);
if(cptr){
params.dont_compress = NEWP(UChar *, 1);
params.dont_compress[0] = NULL;
for(i = 1, j = 0; i < argc; i++){
if(!strcmp(argv[i], "-s")){
params.dont_compress = RENEWP(params.dont_compress, UChar *, j + 2);
params.dont_compress[j] = strdup(argv[i + 1]);
j++;
params.dont_compress[j] = NULL;
i++;
}
}
}
if(givehelp)
give_help();
if(noqueued || (!bu_create))
comm_mode = SERIAL_OPERATION;
if(clientprog)
bu_request = 1;
if(bu_duptapeargs)
bu_duptape = 1;
if(servermsg){
bu_svrmsg = 1;
if(strlen(servermsg) > (i = commbufsiz - 4)){
errmsg(T_("Error: The server message length must be smaller than %d"), i);
usage(argv[0]);
}
}
if(bu_create + bu_extract + bu_contents + bu_request + bu_verify
+ bu_index + bu_duptape + bu_junction > 1){
errmsg(T_("You may supply only one of the flags: cxtXID"));
usage(argv[0]);
}
if(bu_create + bu_extract + bu_contents + bu_request + bu_verify
+ bu_index + bu_ready + bu_duptape + bu_svrmsg + bu_junction < 1
&& s_cart < 0 && s_file < 0 && ! querypos && ! querywrpos
&& ! req_newcart){
if(verbose){
fprintf(stdout, "%s: %s %s\n", T_("Version"), PACKAGE, VERSION_STRING);
exit(0);
}
errmsg(T_("You have to supply one of the flags: cxtCFqQwXIDMG"));
usage(argv[0]);
}
if(std_io && !bu_extract && !bu_create){
errmsg(T_("The flag - may only be given in combination wit -x or -c"));
usage(argv[0]);
}
if(toextractfilename && (! bu_extract && ! bu_create && ! bu_duptape)){
errmsg(T_("The flag -T may only be given in combination wit -x, -c or -D"));
usage(argv[0]);
}
if(toextractfilename && allfiles){
errmsg(T_("The flags -T and -a don't make sense together. Ignoring -a"));
allfiles = 0;
}
if(unused_args && (toextractfilename || allfiles)){
errmsg(T_("If you name files and/or directories in the commandline,\n -a or -T don't make any sense. Using filenames from the command line"));
allfiles = 0;
toextractfilename = NULL;
}
if(allfiles && ! bu_extract){
errmsg(T_("Warning: the flag -a makes only sense in combination with -x"));
}
if(params.unlink && ! bu_extract){
errmsg(T_("Warning: the flag -u makes only sense in combination with -x"));
}
if(params.recursive && !(bu_create || bu_extract)){
errmsg(T_("Warning: the flag -R makes only sense in combination with -x or -c"));
}
if(params.relative && ! bu_extract && ! bu_verify){
errmsg(T_("Warning: the flag -r makes only sense in combination with -x or -d"));
}
if(params.ignoreown && ! bu_extract){
errmsg(T_("Warning: the flag -r makes only sense in combination with -x"));
}
if(newer_than_file && ! bu_create){
errmsg(T_("Warning: the flag -N only makes sense in combination with -c"));
}
if(newer_than_file && newer_than_sec){
errmsg(T_("Warning: -N and -A may not be specified together, -N ignored."));
ZFREE(newer_than_file);
}
if(params.zipcmd && params.unzipcmd && !bu_create){
errmsg(T_("Warning: to specify (un-)processing is only allowed with -c"));
}
if(params.check && ! bu_contents && ! bu_create && ! bu_extract){
errmsg(T_("Warning: the flag -Z makes only sense in combination with -t, -x or -c. Ignoring -Z"));
params.check = NO;
}
if(bu_index && savefile){
errmsg(T_("Error: You cannot get a tape index (-I) of a file (-f)"));
usage(argv[0]);
}
if(bu_duptape && savefile){
errmsg(T_("Error: You cannot duplicate a file (-f) to tape"));
usage(argv[0]);
}
if(servername && savefile){
errmsg(T_("Error: You may not specify -h and -f together"));
usage(argv[0]);
}
if((s_cart != -1 || s_file != -1 || querypos || querywrpos) && savefile){
errmsg(T_("Error: with a file (-f) you cannot set/get cartridge and/or tape position"));
usage(argv[0]);
}
if(savefile && c_f_verbose){
errmsg(T_("Warning: -n makes no sense in combination with -f. -n is ignored"));
c_f_verbose = 0;
}
if(savefile && l_verbose){
errmsg(T_("Warning: -l makes no sense in combination with -f. -l is ignored"));
l_verbose = 0;
}
if(!bu_create && !bu_contents && !bu_extract && o_verbose){
errmsg(T_("Warning: -O is only supported in combination with -c, -x or -t, -O ignored"));
o_verbose = 0;
}
if(savefile && cryptfile){
errmsg(T_("Warning: -k makes no sense in combination with -f"));
}
if(savefile && identitystr){
errmsg(T_("Warning: -W makes no sense in combination with -f, -W ignored."));
}
if(s_file > 0x7fffffff){
errmsg(T_("Error: requested file number is too high - must be <= %ld"),
0x7fffffffL);
usage(argv[0]);
}
if(bu_request){
if(strlen(clientprog) > 250){
errmsg(T_("Error: program line supplied with -X is too long (max. 250 chars)"));
usage(argv[0]);
}
}
#ifndef USE_ZLIB
if(params.check && bu_create){
fprintf(stderr, T_("Warning: CRC32 checksumming not available (requires zlib)\n"));
params.check = NO;
}
#endif
i1 = i2 = -1; /* evaluate uidstr */
if(uidstr)
sscanf(uidstr, "%d:%d", &i1, &i2);
if(i1 != -1)
params.uid = i1;
if(i2 != -1){
params.gid = i2;
cptr = strchr(uidstr, ':');
if(cptr){
cptr = strchr(cptr + 1, ':');
if(cptr){
while(sscanf(cptr + 1, "%d%n", &i1, &i2) > 0){
params.gids = ZRENEWP(params.gids, gid_t, params.ngids + 1);
if(!params.gids){
errmsg(T_("Error: cannot allocate memory"));
exit(51);
}
params.gids[params.ngids++] = i1;
cptr += (1 + i2);
if(*cptr != ',')
break;
}
}
}
}
if(params.check && bu_extract && params.uid != 0){
i = set_eff_ugids(params.uid, params.gid, params.ngids, params.gids);
if(!i) /* if we can setuid to the user, there is no need */
params.check = NO; /* to check later, cause we are the user */
}
if(E__(alloc_commbuf(DEFCOMMBUFSIZ) ? - ENOMEM : 0))
exit(50);
if(set_cryptkey(cryptfile, ACCESSKEYSTRING,
#ifdef HAVE_DES_ENCRYPTION
YES
#else
NO
#endif
) && cryptfile){
errmsg(T_("Warning: Cannot read enough characters from encryption key file \"%s\""),
cryptfile);
errmsg(T_(" Ignoring file, using compiled-in key"));
}
if(!servername)
servername = DEFAULT_SERVER;
if(!identitystr)
identitystr = strdup(DEFAULT_CLIENT_IDSTR);
if(identitystr){
repl_esc_seq(identitystr, '\\');
if(strlen(identitystr) > 128){
errmsg(T_("Warning: Identity string is too long, truncating to 128 characters"));
identitystr[128] = '\0';
}
}
if(params.zipcmd){
if(params.zipcmd[0] == '.'){
params.bi_compr_level = 5;
if(params.zipcmd[1] && isspace(params.zipcmd[1])){
i = sscanf(params.zipcmd + 2, "%d,%d%n", &i1, &i2, &n1);
if(i < 2)
i = sscanf(params.zipcmd + 2, "%d%n", &i1, &n1);
if(i >= 1){
memmove(params.zipcmd, params.zipcmd + 2 + n1,
strlen(params.zipcmd) + 2 + n1 + 1);
if(empty_string(params.zipcmd)){
ZFREE(params.zipcmd);
ZFREE(params.unzipcmd);
}
params.bi_compr_level = i1;
if(i >= 2)
params.bi_compr_maxblock = i2;
}
}
else{
ZFREE(params.zipcmd);
ZFREE(params.unzipcmd);
}
#ifndef USE_ZLIB
errmsg(T_("Warning: Built-in compression not available (not compiled in)."
" Ignoring built-in compression request."));
params.bi_compr_level = params.bi_compr_maxblock = 0;
#endif
}
}
signal(SIGTERM, signal_handler);
signal(SIGINT, signal_handler);
signal(SIGHUP, signal_handler);
/* signal(SIGSEGV, signal_handler);*/
signal(SIGBUS, signal_handler);
signal(SIGPIPE, signal_handler);
params.verbose = verbose;
if(verbose || bu_contents){
verbose = VERBOSE_NORMAL;
if(c_f_verbose)
verbose |= VERBOSE_CART_FILE;
if(l_verbose)
verbose |= VERBOSE_LOCATION;
if(o_verbose)
verbose |= VERBOSE_UID;
if(u_verbose)
verbose |= VERBOSE_MTIME;
}
params.verbosefunc = normal_verbose;
if(c_f_verbose || o_verbose || u_verbose){
params.verbosefunc = extended_verbose;
params.pre_verbosefunc = pre_extended_verbose;
}
if(newer_than_file){
if(stat(newer_than_file, &statb)){
errmsg(T_("Warning: Cannot stat file \"%s\". Option ignored"),
newer_than_file);
}
else{
params.time_newer = statb.st_mtime;
}
}
if(older_than_sec){
params.time_older = strint2time(older_than_sec);
if(params.time_older == (time_t) -1){
errmsg(T_("Warning: Could not read upper time limit from "
"\"%s\". Option ignored"), older_than_sec);
params.time_older = 0;
}
}
if(newer_than_sec){
params.time_newer = strint2time(newer_than_sec);
if(params.time_newer == (time_t) -1){
errmsg(T_("Warning: Could not read lower time limit from "
"\"%s\". Option ignored"), newer_than_sec);
params.time_newer = 0;
}
}
if(savefile)
if(!strcmp(savefile, "-"))
save_stdio = YES;
if(savefile && !bu_create){
if(!save_stdio && access(savefile, R_OK)){
errmsg(T_("Error: Cannot open file `%s' for reading"), savefile);
exit(9);
}
}
if(unused_args){
filelist = unused_args;
}
else if((toextractfilename || (bu_extract && !allfiles))
&& !bu_duptape && !std_io){
if(toextractfilename){
fp = fopen(toextractfilename, "r");
if(!fp){
errmsg(T_("Error: cannot open file \"%s\""), toextractfilename);
exit(4);
}
}
else
fp = stdin;
filelist = get_file_list(fp);
if(toextractfilename)
fclose(fp);
if(! filelist){
errmsg(T_("Exiting, because %s does not contain any filename"),
toextractfilename ? toextractfilename : T_("input"));
exit(0);
}
}
if(filelist){
for(files = filelist, i = 0; *files; i++, files++);
if(i > 0){
found_files = NEWP(UChar, i);
memset(found_files, 0, i * sizeof(UChar));
if(!found_files){
errmsg(T_("Error: cannot allocate memory"));
exit(51);
}
}
}
if(errorlogfile){
fp = fopen(errorlogfile, "w");
if(!fp){
errmsg(T_("Error: cannot open error log file \"%s\""),
errorlogfile);
errmsg(T_(" Writing errors to stdout"));
}
else
params.errfp = fp;
}
if(!savefile){
if(!req_portstr)
req_portstr = DEFAULT_SERVICE;
i = get_tcp_portnum(req_portstr);
if(i >= 0)
portnum = (short unsigned) i;
timemem = time(NULL);
forever{
commfd = connect_afbu_server(servername, NULL, portnum,
AFSERVER_CONNECT_TIMEOUT / 10 + 1);
if(commfd < 0){
if(time(NULL) - timemem > AFSERVER_CONNECT_TIMEOUT){
errmsg(T_("Error: Cannot open communication socket"));
exit(52);
}
else
continue;
}
i = start_server_comm(AFSERVER_CONNECT_TIMEOUT / 10 + 1);
if(!i) /* buffered mode is enabled later, cause */
break; /* it makes no sense for the first actions */
if(i == AUTHENTICATION){
errmsg(T_("Error: %s"), fault_string(i));
exit(55);
}
close(commfd);
if(time(NULL) - timemem > AFSERVER_CONNECT_TIMEOUT){
errmsg(T_("Error: %s. Server closed connection"), fault_string(i));
exit(53);
}
}
if(server_can_sendtbufsiz){
E__(gettapeblocksize(¶ms, YES));
if(server_can_setcbufsiz && !savefile && !bu_junction){
n = MIN(tapeblocksize, MAXCOMMBUFSIZ);
E__(set_commbufsiz(n));
}
}
if(server_can_userid && (cptr = getlogin())){
struct utsname unam;
strncpy(username, cptr, 128);
username[127] = '\0';
memset(buf, 0, 256);
buf[0] = USERIDENT;
strcpy(buf + 1, username);
if(!uname(&unam))
strncpy(buf + 1 + 128, unam.nodename, 128);
buf[1 + 128 + 127] = '\0';
E__(write_forced(commfd, buf, 1 + 256) != 1 + 256);
E__(result());
}
}
else{
comm_mode = SERIAL_OPERATION;
if(bu_create){
if(save_stdio){
commfd = 1;
}
else{
if( (fp = fopen(savefile, "w")) ){
fclose(fp);
commfd = open(savefile, O_WRONLY | O_CREAT | O_BINARY, 0644);
}
else
commfd = -1;
}
}
else{
if(save_stdio){
commfd = 0;
}
else{
commfd = open(savefile, O_RDONLY | O_BINARY);
}
}
if(commfd < 0){
errmsg(T_("Error: Cannot open file \"%s\""), savefile);
exit(6);
}
}
if(s_cset != 1){
if(E__(setcartset(s_cset)))
GETOUT;
}
if(s_cart != -1){
if(E__(setcart(bu_duptape ? - s_cart : s_cart)))
GETOUT;
}
if(s_file != -1){
if(E__(setfilenum(s_file)))
GETOUT;
}
if(identitystr && !savefile)
E__(send_identity(identitystr));
if(bu_request){
E__( (i = request_client_backup(clientprog)) & 0xffff);
gexitst = (i >> 16) & 0xff;
}
if(bu_ready){
if(OK__(i = getserverstate(buf, YES))){
c = buf[0];
j = c & STREAMER_FLAGS_MASK;
c &= STREAMER_STATE_MASK;
fprintf(stdout, T_("Streamer state: %s"),
c == STREAMER_READY ? "READY" : (
c == STREAMER_BUSY ? "BUSY" : (
c == STREAMER_UNLOADED ? "UNLOADED" : (
c == STREAMER_DEVINUSE ? "DEVINUSE" : (
c == STREAMER_UNAVAIL ? "UNAVAILABLE" : "UNKNOWN")))));
if(j & STREAMER_CHANGEABLE)
fprintf(stdout, "+CHANGEABLE");
xref_to_Uns32(&j, buf + 1);
if(j & OPENED_RAW_ACCESS){
switch(j & MODE_MASK){
case OPENED_FOR_READ:
case OPENED_READING:
fprintf(stdout, "+RAWREADING");
break;
case OPENED_FOR_WRITE:
case OPENED_WRITING:
fprintf(stdout, "+RAWWRITING");
break;
default:
break;
}
}
else{
xref_to_Uns32(&n, buf + 5);
switch(j){
case OPENED_FOR_READ:
case OPENED_READING:
fprintf(stdout, "+READING=%d", (int) n);
break;
case OPENED_FOR_WRITE:
case OPENED_WRITING:
fprintf(stdout, "+WRITING=%d", (int) n);
break;
}
}
xref_to_Uns32(&j, buf + 9);
if(j)
fprintf(stdout, "+PENDING=%d", (int) j);
fprintf(stdout, "\n");
}
if(i)
errmsg(T_("Error: Cannot determine streamer state on server"));
if(verbose){
if(server_can_sendid && OK__(i = getserverid(buf, YES))){
fprintf(stdout, "Server-ID: %s\n", buf);
}
if(server_can_sendutapes && OK__(i = getusedtapes(&cptr, identitystr))){
if(cptr){
fprintf(stdout, "Precious tapes: %s\n", cptr);
free(cptr);
}
}
}
}
if(req_newcart){ /* this must be before querywrpos */
if(request_newcart())
errmsg(T_("Warning: No new or reusable cartridge found, keeping old writing position."));
if(QUEUEDOP)
E__(i = post_process_rest_of_queue(0));
}
if(querypos){
if(OK__(i = getcartandfile(¶ms))){
if(QUEUEDOP)
E__(i = post_process_rest_of_queue(0));
if(!i){
fprintf(stdout, T_("Actual tape access position\n"));
fprintf(stdout, T_("Cartridge: %d\nFile: %d\n"),
(int) cart, (int) filenum);
}
}
if(i)
errmsg(T_("Error: Cannot determine current tape access position"));
}
if(querywrpos){ /* this must be behind req_newcart */
if(OK__(i = getwrcartandfile(¶ms))){
if(QUEUEDOP)
E__(i = post_process_rest_of_queue(0));
if(!i){
fprintf(stdout, T_("Next tape writing position\n"));
fprintf(stdout, T_("Cartridge: %d\nFile: %d\n"),
(int) wrcart, (int) wrfilenum);
}
}
if(i)
errmsg(T_("Error: Cannot determine next tape writing position"));
}
if((querypos || querywrpos) && verbose){
if(OK__(i = getnumcarts(¶ms, YES))){
if(!i){
fprintf(stdout, T_("Number of cartridges: %d\n"), (int) numcarts);
}
}
if(i)
errmsg(T_("Error: Cannot determine number of cartridges"));
if(OK__(i = getcartset(¶ms, YES))){
if(!i){
fprintf(stdout, T_("Actual cartridge set: %d\n"), (int) cartset);
}
}
if(i)
errmsg(T_("Error: Cannot determine current cartridge set"));
}
if((bu_create || bu_extract || bu_contents || bu_verify || bu_index || bu_duptape)
&& (querypos || querywrpos || bu_ready) && (verbose || bu_contents)){
fprintf(stdout, "--\n");
fflush(stdout);
fflush(stderr);
}
if(!savefile){ /* Now start the buffered operation mode */
if(comm_mode == QUEUED_OPERATION){
create_command_queue();
}
if(QUEUEDOP){
sigaddset(¶ms.blocked_signals, SIGALRM);
sigaddset(¶ms.blocked_signals, SIGPIPE);
}
}
if(bu_svrmsg){ /* this must be before bu_junction (minrestoreinfo) */
repl_esc_seq(servermsg, '\\');
if(send_svrmsg(commfd, commfd, servermsg, strlen(servermsg))){
errmsg(T_("Error: Evaluating server message failed"));
}
}
if(bu_duptape){
E__( (i = duplicate_tape(bu_duptapeargs, ¶ms)) );
gexitst = (i >> 16) & 0xff;
}
if(bu_create){
params.mode = MODE_CREATE;
params.outputfunc = bu_output;
if(!savefile){
if(req_newfile)
if(E__(request_newfile()))
GETOUT;
if(E__(open_write(0)))
GETOUT;
}
if(!std_io){
if(infoheader){
if(!strcmp(infoheader, "-")){
infoheader = fget_alloc_str(stdin);
if(!infoheader)
infoheader = "";
chop(infoheader);
}
repl_esc_seq(infoheader, '\\');
sprintf(buf, "%d;%d;", INFORMATION, strlen(infoheader));
if(E__(bu_output(buf, strlen(buf), ¶ms))
|| E__(bu_output(infoheader, strlen(infoheader), ¶ms))
|| E__(bu_output(".", 1, ¶ms)))
GETOUT;
}
if(filelist){
while(*filelist){
i = E__(pack_writeout(*filelist, ¶ms, 0));
if(i){
if(i > 0)
num_errors++;
if(num_errors > MAX_NUM_ERRORS){
errmsg(T_("Too many errors on server. Exiting"));
break;
}
}
else{
num_errors = 0;
}
filelist++;
}
}
else{
while(!feof(stdin)){
line = fget_alloc_str(stdin);
if(!line)
continue;
chop(line);
i = E__(pack_writeout(line, ¶ms, 0));
free(line);
if(i){
if(i > 0)
num_errors++;
if(num_errors > MAX_NUM_ERRORS){
errmsg(T_("Too many errors on server. Exiting"));
break;
}
}
else{
num_errors = 0;
}
}
}
pack_writeout(NULL, ¶ms, ENDOFARCHIVE);
}
else{
forever{
i = read(0, buf, BUFFERSIZ);
if(i < 1)
break;
bu_output(buf, i, ¶ms);
}
}
if(E__(send_pending(NO)))
GETOUT;
if(!savefile){
if(E__(getwrcartandfile(¶ms)))
GETOUT;
if(QUEUEDOP)
E__(post_process_rest_of_queue(0));
params.vars.used_carts = add_to_Uns32Ranges(params.vars.used_carts,
wrcart, wrcart);
if(params.vars.used_carts)
pack_Uns32Ranges(params.vars.used_carts, NULL);
params.vars.lastcart = wrcart;
params.vars.lastfile = wrfilenum;
if(E__(closetape(1)))
GETOUT;
}
}
else if(bu_extract || bu_verify){
params.mode = (bu_extract ? MODE_EXTRACT : MODE_VERIFY);
params.inputfunc = bu_input;
if(!savefile){
if(E__(open_read(0)))
GETOUT;
}
if(!std_io){
if(bu_extract)
pack_readin(filelist, ¶ms, found_files);
else
pack_verify(filelist, ¶ms, found_files);
}
else{
do{
i = bu_input(buf, BUFFERSIZ, ¶ms);
i = write_forced(1, buf, i);
} while(i > 0);
}
if(!savefile){
if(E__(closetape(1)))
GETOUT;
}
if(filelist){
for(files = filelist, i = 0; *files; files++, i++){
if(!found_files[i]){
errmsg(T_("%s not found in archive"), *files);
gexitst = 5;
}
}
}
}
else if(bu_contents){
params.mode = MODE_CONTENTS;
params.inputfunc = bu_input;
if(!savefile){
if(E__(open_read(0)))
GETOUT;
}
pack_contents(¶ms);
if(!savefile){
if(E__(closetape(1)))
GETOUT;
}
}
else if(bu_index){
while(bu_index){
Int8 eot = 0;
Int32 len;
UChar htypebuf[20];
params.mode = MODE_INDEX;
sprintf(htypebuf, "%d;", INFORMATION);
j = strlen(htypebuf);
if(E__(set_server_serial))
break;
if(E__(set_server_erroneot))
break;
params.inputfunc = bu_input;
while(!eot){
if(open_read(0)){
eot = 1;
break;
}
if(params.pre_verbosefunc)
params.pre_verbosefunc("", ¶ms);
i = params.inputfunc(buf, j, ¶ms);
if(i < j){
E__(closetape(0));
break;
}
buf[i] = '\0';
if(!strcmp(htypebuf, buf)){
len = 0;
cptr = NULL;
forever{
i = params.inputfunc(&c, 1, ¶ms);
if(i < 1)
break;
if(isdigit(c))
len = len * 10 + (c - '0');
else if(c == ';'){
cptr = NEWP(UChar, len + 1);
i = params.inputfunc(cptr, len + 1, ¶ms);
if(i < len + 1){
break;
}
if(cptr[len] != '.')
break;
cptr[len] = '\0';
filenum_valid = wrfilenum_valid = rdfilenum_valid = NO;
getcartandfile(¶ms);
params.verbosefunc(cptr, ¶ms);
fflush(params.infp);
fflush(params.outfp);
break;
}
else
break;
} /* reading header information */
ZFREE(cptr);
} /* if have header */
E__(closetape(0));
if(skipfiles(1)){
eot = 1;
break;
}
}
break;
}
}
else if(bu_junction){ /* this must be after bu_svrmsg (minrestoreinfo) */
fd_set fds;
forever{
FD_ZERO(&fds);
FD_SET(commfd, &fds);
FD_SET(0, &fds);
i = select(commfd + 1, &fds, NULL, NULL, NULL);
if(i < 0 && errno != EINTR){
errmsg(T_("Error: select failed in main: %s"), strerror(errno));
exit(10);
}
if(FD_ISSET(0, &fds)){
i = read(0, buf, BUFFERSIZ);
if(i < 1)
break;
write_forced(commfd, buf, i);
}
if(FD_ISSET(commfd, &fds)){
i = read(commfd, buf, BUFFERSIZ);
if(i < 1)
break;
write_forced(1, buf, i);
}
}
}
if(QUEUEDOP)
E__(delete_command_queue()); /* synchronous mode for disconnect */
if(reportfile)
add_written_tapes(¶ms.vars.used_carts);
if(!savefile)
close_connection();
else{
if(save_stdio)
close(commfd);
}
if(errorlogfile)
fclose(params.errfp);
strcpy(params.vars.enddate, actimestr());
if(reportfile)
write_reportfile(reportfile, ¶ms, 0);
if(!gexitst)
gexitst = (params.vars.errnum ? 14 : 0);
if(params.vars.errnum){
UChar *estrs[33], **cpptr;
cpptr = packer_strerrors(estrs, params.vars.errnum);
errmsg(T_("The following errors occurred"));
while(*cpptr)
errmsg(*(cpptr++));
}
exit(gexitst);
/* Note on exit status: 1-49 and 100- are fatal
* 50-99 are retryable
*/
getout:
if(!savefile)
close_connection();
else{
if(save_stdio)
close(commfd);
}
if(QUEUEDOP){
E__(post_process_rest_of_queue(0));
}
if(errorlogfile)
fclose(params.errfp);
strcpy(params.vars.enddate, actimestr());
if(reportfile)
write_reportfile(reportfile, ¶ms, 1);
exit(eexitst);
}
Int32
process_queue_entry_response()
{
QueueEntry *entry;
UChar *inbuf;
Int32 res, num_to_read;
entry = queue_entries + queueent_processed_idx;
inbuf = inbufmem + max_inresultlen * queueent_processed_idx;
num_to_read = entry->num_in;
entry->processed = PROCESSED_OK;
if(num_to_read > 0){
res = (read_forced(commfd, inbuf, num_to_read) != num_to_read);
if(res)
entry->processed = - errno;
bytes_transferred += ESTIM_PROT_OVERHEAD + num_to_read;
}
if( (res = result()) )
entry->processed = res;
queueent_processed_idx++;
if(queueent_processed_idx >= queuelen)
queueent_processed_idx = 0;
return(1);
}
Int32
process_queue_entry_request()
{
QueueEntry *entry;
Uns16 instruction;
UChar *outbuf;
UChar c;
Int32 res, num_to_write, i;
entry = queue_entries + queueent_requested_idx;
outbuf = outbufmem + max_outarglen * queueent_requested_idx;
num_to_write = entry->num_out;
instruction = entry->instruction;
if(num_to_write == 0){
c = (UChar) instruction;
res = (write_forced(commfd, &c, 1) != 1);
if(res){
entry->processed = - errno;
}
bytes_transferred += ESTIM_PROT_OVERHEAD + 1;
}
if(num_to_write > 0){
outbuf[1] = (UChar) instruction;
i = num_to_write + 1;
res = (write_forced(commfd, outbuf + 1, i) != i);
if(res)
entry->processed = - errno;
bytes_transferred += ESTIM_PROT_OVERHEAD + i;
}
queueent_requested_idx++;
if(queueent_requested_idx >= queuelen)
queueent_requested_idx = 0;
return(1);
}
Int32
queued_read(int fd, UChar * buf, Int32 num)
{
fd_set infdset, outfdset, *ofdsp;
int maxfd;
Int32 i;
Flag did_sth;
do{
FD_ZERO(&infdset);
FD_SET(fd, &infdset);
maxfd = fd;
ofdsp = NULL;
did_sth = NO;
if(queue_insert_idx != queueent_processed_idx){
if(queue_insert_idx != queueent_requested_idx){
ofdsp = &outfdset;
FD_ZERO(ofdsp);
FD_SET(commfd, ofdsp);
maxfd = MAX(maxfd, commfd);
}
if(queueent_requested_idx != queueent_processed_idx){
FD_SET(commfd, &infdset);
maxfd = MAX(maxfd, commfd);
}
i = select(++maxfd, &infdset, ofdsp, NULL, NULL);
if(i < 0)
return(i);
if(FD_ISSET(commfd, &infdset)){
i = process_queue_entry_response();
did_sth = YES;
}
if(ofdsp){
if(FD_ISSET(commfd, ofdsp)){
i = process_queue_entry_request();
did_sth = YES;
}
}
}
} while(did_sth);
return(read_forced(fd, buf, num));
}
Int32
queued_write(int fd, UChar * buf, Int32 num)
{
fd_set infdset, outfdset, *ifdsp;
int maxfd;
Int32 i;
Flag did_sth;
do{
FD_ZERO(&outfdset);
FD_SET(fd, &outfdset);
maxfd = fd;
ifdsp = NULL;
did_sth = NO;
if(queue_insert_idx != queueent_processed_idx){
if(queue_insert_idx != queueent_requested_idx){
FD_SET(commfd, &outfdset);
maxfd = MAX(maxfd, commfd);
}
if(queueent_requested_idx != queueent_processed_idx){
ifdsp = &infdset;
FD_ZERO(ifdsp);
FD_SET(commfd, ifdsp);
maxfd = MAX(maxfd, commfd);
}
i = select(++maxfd, ifdsp, &outfdset, NULL, NULL);
if(i < 0)
return(i);
if(ifdsp){
if(FD_ISSET(commfd, ifdsp)){
i = process_queue_entry_response();
did_sth = YES;
}
}
if(FD_ISSET(commfd, &outfdset)){
i = process_queue_entry_request();
did_sth = YES;
}
}
} while(did_sth);
return(write_forced(fd, buf, num));
}
Int32
process_queue(Flag do_wait, Flag one_step_suff)
{
fd_set infdset, outfdset;
Int32 i;
Flag did_sth;
do{
FD_ZERO(&outfdset);
FD_ZERO(&infdset);
did_sth = NO;
if(queue_insert_idx != queueent_processed_idx){
if(queue_insert_idx != queueent_requested_idx){
FD_SET(commfd, &outfdset);
}
if(queueent_requested_idx != queueent_processed_idx){
FD_SET(commfd, &infdset);
}
i = select(commfd + 1, &infdset, &outfdset, NULL,
do_wait ? NULL : &null_timeval);
if(!i)
return(0);
if(i < 0)
return(i);
if(FD_ISSET(commfd, &infdset)){
i = process_queue_entry_response();
did_sth = YES;
}
if(FD_ISSET(commfd, &outfdset)){
i = process_queue_entry_request();
did_sth = YES;
}
}
} while(did_sth && !one_step_suff);
return(0);
}
Int32
post_process_proc(Uns32 entryidx)
{
QueueEntry *entry;
UChar *inmem;
UChar *inbuf;
UChar *outbuf;
Uns32 num_in;
Int32 res, n;
Uns16 instruction;
entry = queue_entries + entryidx;
inmem = entry->inmem;
inbuf = inbufmem + entryidx * max_inresultlen;
outbuf = outbufmem + entryidx * max_outarglen + 2;
num_in = entry->num_in;
instruction = entry->instruction;
if(inmem && num_in)
memcpy(inmem, inbuf, num_in * sizeof(UChar));
switch(instruction){
case QUERYPOSITION:
xref_to_UnsN(&cart, inbuf, 24);
xref_to_Uns32(&filenum, inbuf + 3);
break;
case QUERYWRPOSITION:
xref_to_UnsN(&wrcart, inbuf, 24);
xref_to_Uns32(&wrfilenum, inbuf + 3);
break;
case QUERYRDPOSITION:
xref_to_UnsN(&rdcart, inbuf, 24);
xref_to_Uns32(&rdfilenum, inbuf + 3);
break;
case QUERYNUMCARTS:
xref_to_UnsN(&numcarts, inbuf, 24);
break;
case QUERYCARTSET:
xref_to_UnsN(&cartset, inbuf, 24);
break;
case QUERYTAPEBLOCKSIZE:
xref_to_Uns32(&tapeblocksize, inbuf);
break;
case SETFILE:
case SETRAWFILE:
xref_to_Uns32(&filenum, outbuf);
break;
case SETCARTRIDGE:
case SETRAWCARTRIDGE:
xref_to_UnsN(&cart, outbuf, 24);
break;
}
switch(instruction){
case OPENFORWRITE:
case OPENFORREAD:
case OPENFORRAWWRITE:
case OPENFORRAWREAD:
case SKIPFILES:
case SETFILE:
case SETRAWFILE:
case SETCARTRIDGE:
case SETRAWCARTRIDGE:
case CLOSETAPE:
case CLOSETAPEN:
reset_tape_io;
break;
}
res = (entry->processed == PROCESSED_OK ? NO_ERROR
: entry->processed);
if( (n = entry->req_queue_cbs) > 0){
entry->req_queue_cbs = 0;
if(n < queue_commbufsiz){
ER__(change_queue_bufsiz(n), res);
}
}
return(res);
}
Int32
post_process_rest_of_queue(Int32 num_remain)
{
Int32 res = NO_ERROR;
while(num_entries_in_queue > num_remain){
if(queue_entries[queueent_done_idx].processed == NOT_PROCESSED){
process_queue(YES, NO);
continue;
}
res = post_process_proc(queueent_done_idx);
if(res)
return(res);
queueent_done_idx++;
if(queueent_done_idx >= queuelen)
queueent_done_idx = 0;
}
return(res);
}
Int32
post_process_pending()
{
Int32 res = NO_ERROR;
while(queueent_done_idx != queueent_processed_idx){
res = post_process_proc(queueent_done_idx);
if(res)
return(res);
queueent_done_idx++;
if(queueent_done_idx >= queuelen)
queueent_done_idx = 0;
}
return(res);
}
Int32
append_to_queue(
Uns16 instruction,
UChar *inmem,
Uns32 num_in,
UChar *outmem,
Uns32 num_out)
{
Int32 newidx, res;
UChar *outbuf;
if( (res = post_process_pending()) )
return(res);
while((newidx = (queue_insert_idx + 1) % queuelen) == queueent_done_idx){
process_queue(YES, YES); /* queue full */
if( (res = post_process_pending()) )
return(res);
}
outbuf = outbufmem + max_outarglen * queue_insert_idx;
queue_entries[queue_insert_idx].instruction = instruction;
queue_entries[queue_insert_idx].inmem = inmem;
queue_entries[queue_insert_idx].num_in = num_in;
queue_entries[queue_insert_idx].outmem = outmem;
queue_entries[queue_insert_idx].num_out = num_out;
queue_entries[queue_insert_idx].processed = NOT_PROCESSED;
if(outmem && num_out)
memcpy(outbuf + 2, outmem, num_out * sizeof(UChar));
/* data is always put into byte 3..., so we */
/* can put the instruction into byte 2 (and */
/* probably 1) and send it all together in */
/* a single packet */
queue_insert_idx = newidx;
return(0);
}
Int32
duplicate_tape(UChar * args, AarParams * params)
{
UChar **argv;
Int32 argc;
UChar *targethost = NULL;
UChar *targetcryptkeyfile = NULL;
UChar *cptr, *cptr2;
UChar c; /* uninitialized OK */
UChar *targetportstr = NULL;
Int32 targetport = -1;
Int32 targetcart = -1;
Int32 i, r = 0;
int i1, n;
if(cart < 1){ /* if cartridge is not supplied, ask server for current */
i = getcartandfile(params);
if(i)
return(i);
}
argc = str2wordsq(&argv, args);
if(argc < 0){
errmsg("Memory exhausted");
return(- errno);
}
i = goptions(-(argc + 1), argv - 1, "s:h;s:p;i:C;s:k",
&targethost, &targetportstr, &targetcart,
&targetcryptkeyfile);
free_array(argv, argc); /* don't need that anymore */
if(!i){
if(targetportstr){
targetport = get_tcp_portnum(targetportstr);
if(targetport < 0){
fprintf(stderr, T_("Error: Cannot resolve TCP service `%s'.\n"),
targetportstr);
GETOUT;
}
}
}
else{
i = sscanf(args, "%d%n", &i1, &n);
cptr = first_nospace(i > 0 ? args + n : args);
if(i > 0)
targetcart = i1;
if(*cptr && (UChar *) strstr(cptr, SEPLOC) != cptr
&& (UChar *) strstr(cptr, PORTSEP) != cptr
&& *cptr != ':')
usage(gargv[0]);
if((UChar *) strstr(cptr, SEPLOC) == cptr){
cptr++;
cptr2 = strstr(cptr, PORTSEP);
if(!cptr2)
cptr2 = strchr(cptr, ':');
if(cptr2){
c = *cptr2;
*cptr2 = '\0';
}
i = strlen(cptr);
massage_string(cptr);
if(!isfqhn(cptr))
usage(gargv[0]);
targethost = strdup(cptr);
if(cptr2){
cptr = cptr2;
*cptr = c;
}
else
cptr += i;
}
if((UChar *) strstr(cptr, PORTSEP) == cptr){
cptr2 = strchr(++cptr, ':');
if(cptr2)
*cptr2 = '\0';
i = get_tcp_portnum(cptr);
if(i < 1){
fprintf(stderr, T_("Error: Cannot resolve TCP service `%s'.\n"), cptr);
GETOUT;
}
if(cptr2)
*(cptr = cptr2) = ':';
targetport = i;
}
if(*cptr == ':'){
targetcryptkeyfile = strdup(cptr + 1);
massage_string(targetcryptkeyfile);
}
}
if(!targethost && servername) /* set defaults, if not given */
targethost = strdup(servername);
if(targetport < 0)
targetport = portnum;
if(targetcart < 0)
targetcart = cart;
if(!targetcryptkeyfile && cryptfile)
targetcryptkeyfile = strdup(cryptfile);
if(targetport < 0){ /* if no port supplied: find defaults */
targetport = get_tcp_portnum(DEFAULT_SERVICE);
if(targetport < 0)
targetport = DEFAULT_PORT;
}
i = same_host(targethost, servername);
if(i < 0){
errmsg(T_("Error: Cannot resolve hostname"));
r = - errno;
GETOUT;
}
if(i && targetport == (Int32) portnum && targetcart == cart){
errmsg(T_("Error: Attempt to copy tape to itself"));
r = - EACCES;
GETOUT;
}
if(!i || targetport != (Int32) portnum)
r = copytaperemote(targethost, targetport, targetcart,
targetcryptkeyfile);
else
r = copytapelocally(targetcart);
cleanup:
ZFREE(targethost);
ZFREE(targetcryptkeyfile);
return(r);
getout:
CLEANUP;
}
#define DPIPEBUFLEN (8192 - 5)
Int32
copytaperemote(
UChar *targethost,
Int32 targetport,
Int32 targetcart,
UChar *targetcryptkeyfile)
{
Int32 i, j, n, r, curfilenum, numfilescopied;
int pid, pst, inp[2], fd;
UChar commbuf[DPIPEBUFLEN + 10];
Flag first_read; /* uninitialized OK */
Flag first_file, opened;
time_t timemem;
if(pipe(inp))
return(-2);
pid = fork_forced();
if(pid < 0){
errmsg(T_("Error: Cannot create second client process"));
return(- errno);
}
if(pid){ /* on master side receiving from source server */
close(inp[0]);
fd = inp[1];
i = setcart(- cart); /* set source cartridge */
if(i){
errmsg(T_("Error: %s. Can't set cartridge %d on source server"),
fault_string(i), cart);
goto getout_master;
}
curfilenum = numfilescopied = 0;
if(filenum > 0){
i = setfilenum(filenum); /* set source file */
if(i){
errmsg(T_("Error: %s. Can't set tape file %d on source server"),
fault_string(i), filenum);
goto getout_master;
}
curfilenum = filenum;
}
if(verbose)
fprintf(stdout, T_("Starting to copy from cartridge %d to %d\n"),
(int) cart, (int) targetcart);
opened = NO;
first_file = YES; /* flag for rewriting tape label */
forever{
forever{
if(!opened){
i = open_read(1); /* open source tape */
if(i)
goto cleanup_master; /* end of data */
first_read = opened = YES; /* flag for the case: nothing read */
if(filenum <= 0) /* increment before printout */
curfilenum++;
numfilescopied++;
if(verbose)
fprintf(stdout, T_("Copying tape file number %d\n"), (int) curfilenum);
if(filenum > 0)
curfilenum++;
}
j = i = receive(commbuf + 5, DPIPEBUFLEN, &r);
if(r > 0 && first_read && first_file && filenum <= 0)
rewrite_tape_label(commbuf + 5, targetcart, servername, portnum);
memcpy(commbuf + 1, &r, 4); /* set number of read bytes */
commbuf[0] = 0; /* flag for end of transmission */
i = DPIPEBUFLEN + 5;
i = (write_forced(fd, commbuf, i) != i); /* send to slave */
if(i){
errmsg(T_("Error: %s, cannot send buffer to slave"), fault_string(i));
goto getout_master;
}
if(j == ENDOFFILEREACHED)
break;
first_read = NO;
}
i = closetape(0);
if(i){
errmsg(T_("Error: %s"), fault_string(i));
goto getout_master;
}
opened = NO;
first_file = NO;
if(first_read) /* nothing could be read from tape: end of data */
break;
}
cleanup_master:
memset(commbuf, 0, DPIPEBUFLEN + 5); /* send dummy indicating */
commbuf[0] = 1; /* end of transmission */
i = DPIPEBUFLEN + 5;
i = (write_forced(fd, commbuf, i) != i);
close(fd); /* close pipe to slave client */
waitpid_forced(pid, &pst, 0);
if( (i = WEXITSTATUS(pst)) )
errmsg(T_("Error: %s, slave client failed"), fault_string(i));
if(verbose)
fprintf(stdout, T_("Copied %d tape files.\n"), (int) numfilescopied);
return(i << 16);
getout_master:
close(fd); /* close pipe to slave client */
kill(pid, SIGTERM);
waitpid_forced(pid, &pst, 0);
return(i << 16);
}
else{ /* on slave side sending to target server */
close(inp[1]);
fd = inp[0];
if(commfd >= 0)
close(commfd); /* close connection to source server, open to target */
if(targetcryptkeyfile){
if(set_cryptkey(targetcryptkeyfile, ACCESSKEYSTRING,
#ifdef HAVE_DES_ENCRYPTION
YES
#else
NO
#endif
)){
errmsg(T_("Warning: Cannot read enough characters from encryption key file \"%s\""),
targetcryptkeyfile);
errmsg(T_(" Ignoring file, using compiled-in key"));
}
}
ER__(delete_command_queue(), i);
ER__(alloc_commbuf(DEFCOMMBUFSIZ), i);
timemem = time(NULL);
forever{
commfd = connect_afbu_server(targethost, NULL, targetport,
AFSERVER_CONNECT_TIMEOUT / 10 + 1);
if(commfd < 0){
if(time(NULL) - timemem > AFSERVER_CONNECT_TIMEOUT){
errmsg(T_("Error: Cannot connect to target server"));
i = FATAL_ERROR;
goto getout_slave;
}
else
continue;
}
i = start_server_comm(AFSERVER_CONNECT_TIMEOUT / 10 + 1);
if(!i)
break;
close(commfd);
if(time(NULL) - timemem > AFSERVER_CONNECT_TIMEOUT){
errmsg(T_("Error: %s. Target server closed connection"), fault_string(i));
i = AUTHENTICATION;
goto getout_slave;
}
}
if(server_can_sendtbufsiz && server_can_setcbufsiz){
i = MIN(tapeblocksize, MAXCOMMBUFSIZ);
E__(set_commbufsiz(i));
}
if(!noqueued)
comm_mode = QUEUED_OPERATION;
if(comm_mode == QUEUED_OPERATION)
create_command_queue(); /* buffered mode for sending */
i = setcart(- targetcart);
if(i){
errmsg(T_("Error: %s. Can't set cartridge %d on target server"),
fault_string(i), targetcart);
goto getout_slave;
}
if(filenum <= 0){
if( (i = erasetape()) ){
errmsg(T_("Error: %s. Can't erase tape on target server"),
fault_string(i));
goto getout_slave;
}
}
if( (i = setfilenum(filenum > 0 ? filenum : -1)) ){
errmsg(T_("Error: %s. Can't set tape file on target server"),
fault_string(i));
goto getout_slave;
}
opened = NO;
forever{ /* receive from master side */
i = read_forced(fd, commbuf, DPIPEBUFLEN + 5);
if(i < DPIPEBUFLEN + 5){
errmsg(T_("Error: Not enough bytes passed, possible parent process error"));
goto getout_slave;
}
memcpy(&n, commbuf + 1, 4); /* number of valid bytes */
if(!opened && n > 0){ /* if not yet open */
i = open_write(1); /* open target tape */
if(i){
errmsg(T_("Error: Could not open target tape for writing"));
goto getout_slave;
}
opened = YES;
}
i = send_to_server(commbuf + 5, n);
if(i){
errmsg(T_("Error: Could not send data to target backup server"));
goto getout_slave;
}
if(n < DPIPEBUFLEN){ /* end of this tape file */
if(opened){
i = send_pending(YES);
if(!i)
i = closetape(0); /* close without rewind */
if(i){
errmsg(T_("Error: Target tape could not be closed properly"));
goto getout_slave;
}
}
opened = NO;
if(commbuf[0])
goto cleanup_slave;
}
}
cleanup_slave:
i = closetape(1); /* close target tape with rewind */
close(fd); /* close connection to master */
return(i << 16);
getout_slave:
closetape(1);
close(fd);
return(i << 16);
}
}
static Int32 /* remove all files in the given directory */
cleandir(UChar * path)
{
UChar **files, **f, *newpath;
newpath = strchain(path, FN_DIRSEPSTR, "*", NULL);
if(!newpath)
GETOUT;
files = fnglob(newpath);
if(!files)
GETOUT;
for(f = files; *f; f++)
unlink(*f);
free(newpath);
free_array(files, f - files);
return(0);
getout:
ZFREE(newpath);
return(-1);
}
static void
cleanup_tmpdir(UChar * path)
{
cleandir(path);
rmdir(path);
}
Int32
flush_files_to_target(
UChar *tmpdir,
Int32 targetcart,
Int32 first_file,
Int32 last_file)
{
Int32 i, r = 0;
UChar tmpf[100], dbuf[8192];
int fd = -1;
if(!noqueued)
comm_mode = QUEUED_OPERATION;
if(comm_mode == QUEUED_OPERATION)
create_command_queue(); /* buffered mode for sending */
if( (r = setcart(- targetcart)) )
GETOUT;
if( (r = setfilenum(filenum > 0 ? first_file : - first_file)) )
GETOUT;
for(; first_file <= last_file; first_file++){
sprintf(tmpf, "%s%s%d", tmpdir, FN_DIRSEPSTR, (int) first_file);
fd = open(tmpf, O_RDONLY);
if(fd < 0){
r = FATAL_ERROR;
GETOUT;
}
if( (r = open_write(1)) )
CLEANUP;
if(verbose)
fprintf(stdout, T_("Writing tape file %d to target tape\n"),
(int) first_file);
while( (i = read_forced(fd, dbuf, 8192)) > 0){
r = send_to_server(dbuf, i);
if(r)
GETOUT;
}
close(fd);
fd = -1;
unlink(tmpf);
if( (r = send_pending(YES)) )
GETOUT;
if( (r = closetape(0)) )
GETOUT;
}
cleanup:
if(fd >= 0)
close(fd);
if( (i = closetape(1)) )
if(!r)
r = i;
if( (i = delete_command_queue()) )
if(!r)
r = i;
return(r);
getout:
CLEANUP;
}
Int32
copytapelocally(Int32 targetcart)
{
Real64 fsspace = 0.0, bytes_in_files;
off_t largest_filesize = 0, bytes_in_file;
UChar *tmpfile = NULL, *tmpdir = NULL, *tmpf = NULL, *cptr;
UChar dbuf[8192];
Flag first_read, save_files, end_of_tape;
Int32 i, j, n, r = 0, num_files, first_file, sourcecart;
Int32 curfilenum, numfilescopied;
int fd;
if(toextractfilename){ /* -T option: non-default tmp-dir */
cptr = strchain(toextractfilename, FN_DIRSEPSTR, "afbcp", NULL);
if(!cptr)
CLEANUPR(FATAL_ERROR);
tmpfile = tmp_name(cptr);
free(cptr);
cleanpath(tmpfile);
}
else{
tmpfile = tmp_name(FN_TMPDIR FN_DIRSEPSTR "afbcp");
}
tmpdir = strdup(tmpfile);
tmpf = NEWP(UChar, strlen(tmpfile) + 20);
if(!tmpfile || !tmpdir || !tmpf)
CLEANUPR(FATAL_ERROR);
if( (cptr = FN_LASTDIRDELIM(tmpfile)) )
*cptr = '\0';
get_fs_space(tmpfile, &fsspace);
if(fsspace < 1000000.0){
errmsg(T_("Error: 1 MB of free space in %s is for sure too few"));
CLEANUPR(FATAL_ERROR);
}
cleanup_proc = (void (*)(void *)) cleanup_tmpdir;
cleanup_arg = tmpdir;
if(mkdir(tmpdir, 0700))
CLEANUPR(errno << 16);
sourcecart = cart;
i = setcart(- sourcecart);
if(i){
errmsg(T_("Error: %s. Can't set cartridge %d on source server"),
fault_string(i), sourcecart);
CLEANUPR(i);
}
curfilenum = numfilescopied = 0;
if(filenum > 0){
curfilenum = filenum;
if( (i = setfilenum(filenum)) )
CLEANUPR(i);
}
end_of_tape = NO;
first_file = -1;
bytes_in_files = 0.0;
if(verbose)
fprintf(stdout, T_("Going to copy cartridge %d to %d\n"),
(int) sourcecart, (int) targetcart);
for(num_files = filenum > 0 ? filenum : 1; !end_of_tape; num_files++){
if(first_file < 0)
first_file = num_files;
i = open_read(1);
if(i){
save_files = end_of_tape = YES;
num_files--;
}
else{
if(filenum <= 0)
curfilenum++;
if(verbose)
fprintf(stdout, T_("Copying tape file %d to disk\n"), (int) curfilenum);
if(filenum > 0)
curfilenum++;
numfilescopied++;
save_files = NO;
first_read = YES;
bytes_in_file = 0;
sprintf(tmpf, "%s%s%d", tmpdir, FN_DIRSEPSTR, (int) num_files);
fd = open(tmpf, O_WRONLY | O_CREAT | O_TRUNC, 0600);
if(fd < 0){
if(first_file == num_files){
errmsg(T_("Error: Cannot save entire tape file. Too few space in %s, aborting"),
tmpdir);
CLEANUPR(FATAL_ERROR);
}
save_files = YES;
num_files--;
}
else{
forever{
i = receive(dbuf, 8192, &n);
if(n > 0){
if(num_files == 1 && first_read && filenum <= 0)
rewrite_tape_label(dbuf, targetcart, servername, portnum);
j = write_forced(fd, dbuf, n);
bytes_in_file += (j > 0 ? j : 0);
if(j < n){
close(fd);
if(fd >= 0)
fd = -1;
unlink(tmpf);
if(first_file == num_files){
errmsg(T_("Error: Cannot save entire tape file. Too few space in %s, aborting"),
tmpdir);
CLEANUPR(FATAL_ERROR);
}
num_files--;
save_files = YES;
break;
}
}
else{
if(first_read){
save_files = end_of_tape = YES;
num_files--;
}
}
if(i == ENDOFFILEREACHED){
break;
}
first_read = NO;
}
}
if(fd >= 0)
close(fd);
fd = -1;
bytes_in_files += (Real64) bytes_in_file;
if( (r = closetape(0)) )
CLEANUP;
if(bytes_in_file > largest_filesize)
largest_filesize = bytes_in_file;
}
get_fs_space(tmpfile, &fsspace);
if(fsspace - bytes_in_files < (Real64) largest_filesize || end_of_tape)
save_files = YES; /* force saving of files */
if(save_files){
i = flush_files_to_target(tmpdir, targetcart,
first_file, num_files);
if(i)
CLEANUPR(i);
if(!end_of_tape){
if( (i = setcart(- sourcecart)) )
CLEANUPR(i);
i = - (num_files + 1);
if(filenum > 0)
i = - i; /* positive i.e. logical file number */
if( (i = setfilenum(i)) )
CLEANUPR(i);
}
first_file = -1;
cleandir(tmpdir);
bytes_in_files = 0.0;
}
}
if(verbose)
fprintf(stdout, T_("Copied %d tape files\n"), (int) numfilescopied);
cleanup:
cleandir(tmpdir);
rmdir(tmpdir);
ZFREE(tmpdir);
ZFREE(tmpfile);
ZFREE(tmpf);
cleanup_proc = NULL;
return(r);
}
|