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
|
/**************************************************************************
dkopp copy files to / restore files from BRD/DVD media
Copyright 2007-2024 Michael Cornelison
source code URL: https://kornelix.net
contact: mkornelix@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. See https://www.gnu.org/licenses
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
***************************************************************************/
#include <fcntl.h>
#include <dirent.h>
#include <sys/ioctl.h>
#include <sys/statfs.h>
#include <poll.h>
#if defined __linux__
#include <linux/cdrom.h>
#endif
#include "zfuncs.h" // order important
// parameters and limits
#define release "dkopp-8.2" // release 8.2
#define debug 0
#define vrcc 512*1024 // verify read I/O size
#define maxnx 1000 // max no. include/exclude recs.
#define maxfs 200000 // max no. disk or DVD/BRD files
#define maxhist 200 // max no. history files
#define giga 1000000000.0 // gigabyte (not 1024**3)
#define modtimetolr 1.0 // tolerance, equal mod times
#define nano 0.000000001 // nanosecond
#define gforce "-use-the-force-luke=tty -use-the-force-luke=notray" // growisofs hidden options
// special control files on DVD/BRD
#define V_DKOPPDIRK "/dkopp-data/" // dkopp special files on DVD/BRD
#define V_FILEPOOP V_DKOPPDIRK "filepoop" // directory data file
#define V_JOBFILE V_DKOPPDIRK "jobfile" // backup job data file
#define V_DATETIME V_DKOPPDIRK "datetime" // date-time file
// GTK GUI widgets
GtkWidget *mWin, *mVbox, *mScroll, *mLog; // main window
GtkTextBuffer *logBuff;
GtkWidget *fc_dialogbox, *fc_widget; // file-chooser dialog
GtkWidget *editwidget; // edit box in file selection dialogs
namespace zfuncs { // externals from zfuncs module
extern GdkDisplay *display; // X11 workstation (KB, mouse, screen)
extern GdkDeviceManager *manager; // knows screen / mouse associations
extern GdkScreen *screen; // monitor (screen)
extern GdkDevice *mouse; // pointer device
extern GtkSettings *settings; // screen settings
extern GtkWidget *mainwin; // main window for zfuncs parent 7.5
}
#define MWIN GTK_WINDOW(mWin)
// file scope variables
int killFlag; // tell function to quit
int pauseFlag; // tell function to pause/resume
int menuLock; // menu lock flag
int commFail; // command failure flag
int Fdialog; // dialog in progress
int clrun; // flag, command line 'run' command
int Fgui; // flag, GUI or command line
ch subprocName[20]; // name of created subprocess
ch scriptParam[200]; // parameter from script file
ch mbmode[20], mvmode[20]; // actual backup, verify modes
double pctdone; // % done from growisofs
ch scrFile[XFCC]; // command line script file
ch backupDT[16]; // nominal backup date: yyyymmdd-hhmm
ch homedir[200]; // /home/user/.dkopp
ch TFdiskfiles[200], TFdvdfiles[200]; // scratch files in homedir
ch TFjobfile[200], TFfilepoop[200], TFdatetime[200];
ch TFrestorefiles[200], TFrestoredirks[200];
// available DVD/BRD devices
int ndvds, maxdvds = 8;
ch dvddevs[8][20]; // DVD/BRD devices, /dev/sr0 etc.
ch dvddesc[8][40]; // DVD/BRD device descriptions
ch dvddevdesc[8][60]; // combined device and description
// backup job data
ch BJfile[XFCC]; // backup job file
ch BJdvd[20]; // DVD/BRD device: /dev/hdb
ch BJbmode[20]; // backup: full/incremental/accumulate
ch BJvmode[20]; // verify: full/incremental/thorough
ch BJdatefrom[12]; // mod date selection, yyyy.mm.dd
time_t BJtdate; // binary mod date selection
int BJval; // backup job data validated
int BJmod; // backup job data modified
ch *BJinex[maxnx]; // backup include/exclude records
int BJfiles[maxnx]; // corresp. file count per rec
double BJbytes[maxnx]; // corresp. byte count per rec
int BJdvdno[maxnx]; // corresp. DVD/BRD seqnc. no. (1,2...)
int BJnx; // actual record count < maxnx
// DVD/BRD medium data
ch dvdmp[100]; // mount point, /media/xxxxx
int dvdmpcc; // mount point cc
int dvdmtd; // DVD/BRD mounted
ch mediumDT[16]; // DVD/BRD medium last use date-time
time_t dvdtime; // DVD/BRD device mod time
ch dvdlabel[32]; // DVD/BRD label
// current files for backup
struct dfrec { // disk file record
ch *file; // directory/filename
double size; // byte count
double mtime; // mod time
int stat; // fstat() status
int inclx; // include rec for this file
ch disp; // status: new modified unchanged
ch ivf; // flag for incr. verify
};
dfrec Drec[maxfs]; // disk file data records
int Dnf; // actual file count < maxfs
double Dbytes; // disk files, total bytes
double Dbytes2; // bytes for DVD/BRD medium
// DVD/BRD file data
struct vfrec { // DVD/BRD file record
ch *file; // directory/file (- /media/xxx)
double size; // byte count
double mtime; // mod time
int stat; // fstat() status
ch disp; // status: deleted modified unchanged
};
vfrec Vrec[maxfs]; // DVD/BRD file data records
int Vnf; // actual file count < maxfs
double Vbytes; // DVD/BRD files, total bytes
// disk:DVD/BRD comparison data
int nnew, ndel, nmod, nunc; // counts: new del mod unch
int Mfiles; // new + mod + del file count
double Mbytes; // new + mod files, total bytes
// restore job data
ch *RJinex[maxnx]; // file restore include/exclude recs.
int RJnx; // actual RJinex count < maxnx
int RJval; // restore job data validated
ch RJfrom[XFCC]; // restore copy-from: /home/.../
ch RJto[XFCC]; // restore copy-to: /home/.../
struct rfrec { // restore file record
ch *file; // restore filespec: /home/.../file.ext
double size; // byte count
};
rfrec Rrec[maxfs]; // restore file data records
int Rnf; // actual file count < maxfs
double Rbytes; // total bytes
// dkopp local functions
int initfunc(void *data); // GTK init function
void buttonfunc(GtkWidget *item, ch *menu); // process toolbar button event
void menufunc(GtkWidget *item, ch *menu); // process menu select event
void script_func(void *); // execute script file
int quit_dkopp(ch *); // exit application
int clearScreen(ch *); // clear logging window
int signalFunc(ch *); // kill/pause/resume curr. function
int checkKillPause(); // test flags: killFlag and pauseFlag
int fileOpen(ch *); // file open dialog
int fileSave(ch *); // file save dialog
int BJload(ch *fspec); // backup job data <<< file
int BJstore(ch *fspec); // backup job data >>> file
int BJvload(ch *); // load job file from DVD/BRD
int BJedit(ch *); // backup job edit dialog
int BJedit_event(zdialog *zd, ch *event); // dialog event function
int BJedit_stuff(zdialog * zd); // stuff dialog widgets with job data
int BJedit_fetch(zdialog * zd); // set job data from dialog widgets
int Backup(ch *); // backup menu function
int FullBackup(ch *vmode); // full backup + verify
int IncrBackup(ch *bmode, ch *vmode); // incremental / accumulate + verify
int Verify(ch *); // verify functions
int Report(ch *); // report functions
int get_current_files(ch *); // file stats. per include/exclude
int report_summary_diffs(ch *); // disk:DVD/BRD differences summary
int report_directory_diffs(ch *); // disk:DVD/BRD differences by directory
int report_file_diffs(ch *); // disk:DVD/BRD differences by file
int list_current_files(ch *); // list all disk files for backup
int list_DVD_files(ch *); // list all files on DVD/BRD
int find_files(ch *); // find files on disk, DVD/BRD, hist
int view_backup_hist(ch *); // view backup history files
int RJedit(ch *); // restore job edit dialog
int RJedit_event(zdialog*, ch *event); // RJedit response
int RJlist(ch *); // list DVD/BRD files to be restored
int Restore(ch *); // file restore function
int getDVDs(void *); // get avail. DVD/BRD's, mount points
int setDVDdevice(ch *); // set DVD/BRD device and mount point
int setDVDlabel(ch *); // set new DVD/BRD label
int mountDVD(ch *); // mount DVD/BRD, echo outputs, status
int unmountDVD(ch *); // unmount DVD/BRD + echo outputs
int ejectDVD(ch *); // eject DVD/BRD + echo outputs
int resetDVD(ch *); // hardware reset
int eraseDVD(ch *); // fill DVD/BRD with zeros (long time)
int formatDVD(ch *); // quick format DVD/BRD
int helpFunc(ch *); // help function
int fc_dialog(ch *dirk); // file chooser dialog
int fc_response(GtkDialog *, int, void *); // fc_dialog response
int writeDT(); // write date-time to temp file
int save_filepoop(); // save file owner & permissions data
int restore_filepoop(); // restore file owner & perm. data
int createBackupHist(); // create backup history file
int inexParse(ch *rec, ch *&rtype, ch *&fspec); // parse include/exclude record
int BJvalidate(ch *); // validate backup job data
int RJvalidate(); // validate restore job data
int nxValidate(ch **recs, int nr); // validate include/exclude recs
int dGetFiles(); // generate file list from job
int vGetFiles(); // find all DVD/BRD files
int rGetFiles(); // generate restore job file list
int setFileDisps(); // set file disps: new del mod unch
int SortFileList(ch *recs, int RL, int NR, ch sort); // sort file list in memory
int filecomp(ch *file1, ch *file2); // compare directories before files
int BJreset(); // reset backup job file data
int RJreset(); // reset restore job data
int dFilesReset(); // reset disk file data and free memory
int vFilesReset(); // reset DVD/BRD file data, free memory
int rFilesReset(); // reset restore file data, free memory
ch *checkFile(ch *dfile, int compf, double &tcc); // validate file on BRD/DVD medium
ch *copyfile(ch *vfile, ch *dfile); // copy file from DVD/BRD to disk
int track_filespec(ch *filespec); // track filespec on screen, no scroll
int track_filespec_err(ch *filespec, ch *errmess); // error logger for track_filespec()
ch *kleenex(ch *name); // clean exotic file names for output
int do_shell(ch *pname, ch *command); // shell command + output to window
int track_growisofs_files(ch *buff); // convert %done to filespec, output
// dkopp menu table
struct menuent {
ch menu1[20], menu2[40]; // top-menu, sub-menu
int lock; // lock funcs: no run parallel
int (*mfunc)(ch *); // processing function
};
#define nmenu 43
struct menuent menus[nmenu] = {
// top-menu sub-menu lock menu-function
{ "button", "edit job", 1, BJedit },
{ "button", "clear", 0, clearScreen },
{ "button", "run job", 1, Backup },
{ "button", "run DVD/BRD", 1, Backup },
{ "button", "pause", 0, signalFunc },
{ "button", "resume", 0, signalFunc },
{ "button", "kill job", 0, signalFunc },
{ "button", "quit", 0, quit_dkopp },
{ "File", "open job", 1, fileOpen },
{ "File", "open DVD/BRD", 1, BJvload },
{ "File", "edit job", 1, BJedit },
{ "File", "show job", 0, BJvalidate },
{ "File", "save job", 0, fileSave },
{ "File", "run job", 1, Backup },
{ "File", "run DVD/BRD", 1, Backup },
{ "File", "quit", 0, quit_dkopp },
{ "Backup", "full", 1, Backup },
{ "Backup", "incremental", 1, Backup },
{ "Backup", "accumulate", 1, Backup },
{ "Verify", "full", 1, Verify },
{ "Verify", "incremental", 1, Verify },
{ "Verify", "thorough", 1, Verify },
{ "Report", "get files for backup", 1, Report },
{ "Report", "diffs summary", 1, Report },
{ "Report", "diffs by directory", 1, Report },
{ "Report", "diffs by file", 1, Report },
{ "Report", "list files for backup", 1, Report },
{ "Report", "list DVD/BRD files", 1, Report },
{ "Report", "find files", 1, Report },
{ "Report", "view backup hist", 1, Report },
{ "Restore", "setup DVD/BRD restore", 1, RJedit },
{ "Restore", "list restore files", 1, RJlist },
{ "Restore", "restore files", 1, Restore },
{ "DVD/BRD", "set DVD/BRD device", 1, setDVDdevice },
{ "DVD/BRD", "set DVD/BRD label", 1, setDVDlabel },
{ "DVD/BRD", "erase DVD/BRD", 1, eraseDVD },
{ "DVD/BRD", "format DVD/BRD", 1, formatDVD },
{ "DVD/BRD", "mount DVD/BRD", 1, mountDVD },
{ "DVD/BRD", "unmount DVD/BRD", 1, unmountDVD },
{ "DVD/BRD", "eject DVD/BRD", 1, ejectDVD },
{ "DVD/BRD", "reset DVD/BRD", 0, resetDVD },
{ "Help", "about", 0, helpFunc },
{ "Help", "user guide", 0, helpFunc } };
// dkopp main program
int main(int argc, ch *argv[])
{
GtkWidget *mbar; // menubar
GtkWidget *mFile, *mBackup, *mVerify, *mReport, *mRestore;
GtkWidget *mDVD, *mHelp;
int ii;
zinitapp(release,argc,argv); // get install directories
Fgui = 1; // assume GUI
clrun = 0; // no command line run command
*scrFile = 0; // no script file
*BJfile = 0; // no backup job file
for (ii = 1; ii < argc; ii++) // get command line options
{
if (strmatch(argv[ii],"-nogui")) Fgui = 0; // command line operation
else if (strmatch(argv[ii],"-job") && argc > ii+1) // -job jobfile (load job)
strcpy(BJfile,argv[++ii]);
else if (strmatch(argv[ii],"-run") && argc > ii+1) // -run jobfile (load and run job)
{ strcpy(BJfile,argv[++ii]); clrun++; }
else if (strmatch(argv[ii],"-script") && argc > ii+1) // -script scriptfile (execute script)
strcpy(scrFile,argv[++ii]);
else strcpy(BJfile,argv[ii]); // assume a job file and load it
}
if (! Fgui) // no GUI
{
mLog = mWin = 0; // output goes to STDOUT
initfunc(0); // start job or script
unmountDVD(0); // unmount DVD/BRD
ejectDVD(0); // eject DVD/BRD (may not work)
return 0; // exit
}
mWin = gtk_window_new(GTK_WINDOW_TOPLEVEL); // create main window
zfuncs::mainwin = mWin;
gtk_window_set_title(GTK_WINDOW(mWin),release);
gtk_window_set_position(GTK_WINDOW(mWin),GTK_WIN_POS_CENTER);
gtk_window_set_default_size(GTK_WINDOW(mWin),800,500);
mVbox = gtk_box_new(VERTICAL,0); // vertical packing box
gtk_container_add(GTK_CONTAINER(mWin),mVbox); // add to main window
mScroll = gtk_scrolled_window_new(0,0); // scrolled window
gtk_box_pack_end(GTK_BOX(mVbox),mScroll,1,1,0); // add to main window mVbox
mLog = gtk_text_view_new(); // text edit window
gtk_text_view_set_left_margin(GTK_TEXT_VIEW(mLog),2);
gtk_container_add(GTK_CONTAINER(mScroll),mLog); // add to scrolled window
logBuff = gtk_text_view_get_buffer(GTK_TEXT_VIEW(mLog)); // get related text buffer
gtk_text_buffer_set_text(logBuff,"", -1);
mbar = create_menubar(mVbox); // create menu bar and menus
mFile = add_menubar_item(mbar,"File",menufunc);
add_submenu_item(mFile,"open job",menufunc);
add_submenu_item(mFile,"open DVD/BRD",menufunc);
add_submenu_item(mFile,"edit job",menufunc);
add_submenu_item(mFile,"show job",menufunc);
add_submenu_item(mFile,"save job",menufunc);
add_submenu_item(mFile,"run job",menufunc);
add_submenu_item(mFile,"run DVD/BRD",menufunc);
add_submenu_item(mFile,"quit",menufunc);
mBackup = add_menubar_item(mbar,"Backup",menufunc);
add_submenu_item(mBackup,"full",menufunc);
add_submenu_item(mBackup,"incremental",menufunc);
add_submenu_item(mBackup,"accumulate",menufunc);
mVerify = add_menubar_item(mbar,"Verify",menufunc);
add_submenu_item(mVerify,"full",menufunc);
add_submenu_item(mVerify,"incremental",menufunc);
add_submenu_item(mVerify,"thorough",menufunc);
mReport = add_menubar_item(mbar,"Report",menufunc);
add_submenu_item(mReport,"get files for backup",menufunc);
add_submenu_item(mReport,"diffs summary",menufunc);
add_submenu_item(mReport,"diffs by directory",menufunc);
add_submenu_item(mReport,"diffs by file",menufunc);
add_submenu_item(mReport,"list files for backup",menufunc);
add_submenu_item(mReport,"list DVD/BRD files",menufunc);
add_submenu_item(mReport,"find files",menufunc);
add_submenu_item(mReport,"view backup hist",menufunc);
add_submenu_item(mReport,"save screen",menufunc);
mRestore = add_menubar_item(mbar,"Restore",menufunc);
add_submenu_item(mRestore,"setup DVD/BRD restore",menufunc);
add_submenu_item(mRestore,"list restore files",menufunc);
add_submenu_item(mRestore,"restore files",menufunc);
mDVD = add_menubar_item(mbar,"DVD/BRD",menufunc);
add_submenu_item(mDVD,"set DVD/BRD device",menufunc);
add_submenu_item(mDVD,"set DVD/BRD label",menufunc);
add_submenu_item(mDVD,"mount DVD/BRD",menufunc);
add_submenu_item(mDVD,"unmount DVD/BRD",menufunc);
add_submenu_item(mDVD,"eject DVD/BRD",menufunc);
add_submenu_item(mDVD,"reset DVD/BRD",menufunc);
add_submenu_item(mDVD,"erase DVD/BRD",menufunc);
add_submenu_item(mDVD,"format DVD/BRD",menufunc);
mHelp = add_menubar_item(mbar,"Help",menufunc);
add_submenu_item(mHelp,"about",menufunc);
add_submenu_item(mHelp,"user guide",menufunc);
/*** buttons removed 8.2
GtkWidget *tbar = create_toolbar(mVbox,32); // create toolbar and buttons
add_toolbar_button(tbar,"edit job","edit backup job","editjob.png",buttonfunc);
add_toolbar_button(tbar,"run job","run backup job","run.png",buttonfunc);
add_toolbar_button(tbar,"run DVD/BRD","run job on DVD/BRD","run.png",buttonfunc);
add_toolbar_button(tbar,"pause","pause running job","media-pause.png",buttonfunc);
add_toolbar_button(tbar,"resume","resume running job","media-play.png",buttonfunc);
add_toolbar_button(tbar,"kill job","kill running job","kill.png",buttonfunc);
add_toolbar_button(tbar,"clear","clear screen","clear.png",buttonfunc);
add_toolbar_button(tbar,"quit","quit dkopp","quit.png",buttonfunc);
***/
gtk_widget_show_all(mWin); // show all widgets
G_SIGNAL(mWin,"destroy",quit_dkopp,0); // connect window destroy event
g_timeout_add(0,initfunc,0); // setup initial call from gtk_main()
gtk_main(); // process window events
return 0;
}
// initial function called from gtk_main() at startup
int initfunc(void *)
{
int ii;
ch *home;
time_t datetime;
strcpy(homedir,get_zhomedir()); // get temp file names
snprintf(TFdiskfiles,200,"%s/diskfiles",homedir);
snprintf(TFdvdfiles,200,"%s/dvdfiles",homedir);
snprintf(TFfilepoop,200,"%s/filepoop",homedir);
snprintf(TFjobfile,200,"%s/jobfile",homedir);
snprintf(TFdatetime,200,"%s/datetime",homedir);
snprintf(TFrestorefiles,200,"%s/restorefiles.sh",homedir);
snprintf(TFrestoredirks,200,"%s/restoredirks.sh",homedir);
datetime = time(0);
printf("dkopp errlog %s \n",ctime(&datetime));
menuLock = Fdialog = 0; // initialize controls
killFlag = pauseFlag = commFail = 0;
strcpy(subprocName,"");
strcpy(scriptParam,"");
strcpy(BJdvd,"/dev/sr0"); // default DVD/BRD device
strcpy(dvdmp,"/media/dkopp"); // default mount point
dvdmpcc = strlen(dvdmp); // mount point cc
strcpy(dvdlabel,"dkopp"); // default DVD/BRD label
strcpy(BJbmode,"full"); // backup mode
strcpy(BJvmode,"full"); // verify mode
BJval = 0; // not validated
BJmod = 0; // not modified
strcpy(BJdatefrom,"1970.01.01"); // file age exclusion default
BJtdate = 0;
BJnx = 4; // backup job include/exclude recs
for (ii = 0; ii < BJnx; ii++)
BJinex[ii] = (ch *) zmalloc(50,"BJinex");
home = getenv("HOME"); // get "/home/username"
if (! home) home = (ch *) "/home/xxx";
strcpy(BJinex[0],"# dkopp default backup job"); // initz. default backup specs 8.1
sprintf(BJinex[1],"include %s/*",home); // include /home/username/*
sprintf(BJinex[2],"exclude *thumbnails*"); // exclude thumbnails
sprintf(BJinex[2],"exclude *.cache*"); // exclude cache
Dnf = Vnf = Rnf = Mfiles = 0; // file counts = 0
Dbytes = Dbytes2 = Vbytes = Mbytes = 0.0; // byte counts = 0
strcpy(RJfrom,"/home/"); // file restore copy-from location
strcpy(RJto,"/home/"); // file restore copy-to location
RJnx = 0; // no. restore include/exclude recs
RJval = 0; // restore job not validated
strcpy(mediumDT,"unknown"); // DVD/BRD medium last backup date-time
dvdtime = -1; // DVD/BRD device mod time
dvdmtd = 0; // DVD/BRD not mounted
if (*BJfile) { // command line job file
BJload(BJfile);
if (commFail) return 0;
}
if (clrun) { // command line run command
menufunc(null,"File");
menufunc(null,"run job");
}
if (*scrFile) script_func(0); // command line script file
txwidget_append2(mLog,0,"\n Searching for DVD/BRD devices ... \n");
g_timeout_add(1000,getDVDs,0); // blocks GTK until done
return 0;
}
// process toolbar button events (simulate menu selection)
void buttonfunc(GtkWidget *item, ch *button)
{
ch button2[20], *pp;
strncpy0(button2,button,19);
pp = strchr(button2,'\n'); // replace \n with blank
if (pp) *pp = ' ';
menufunc(item,"button"); // use menu function for button
menufunc(item,button2);
return;
}
// process menu selection event
void menufunc(GtkWidget *, ch *menu)
{
static int ii;
static ch menu1[20] = "", menu2[40] = "";
int kk;
for (ii = 0; ii < nmenu; ii++)
if (strmatch(menu,menus[ii].menu1)) break; // mark top-menu selection
if (ii < nmenu) { strcpy(menu1,menu); return; }
for (ii = 0; ii < nmenu; ii++)
if (strmatch(menu1,menus[ii].menu1) &&
strmatch(menu,menus[ii].menu2)) break; // mark sub-menu selection
if (ii < nmenu) strcpy(menu2,menu);
else { // no match to menus
txwidget_append2(mLog,0," *** bad command: %s \n",menu);
commFail++;
return;
}
if (menuLock && menus[ii].lock) { // no lock funcs can run parallel
if (Fgui)
zmessageACK(mWin,"wait for current function to complete");
return;
}
if (! menuLock) {
killFlag = pauseFlag = 0; // reset controls
*subprocName = 0;
commFail = 0; // start with no errors
}
if (! *scrFile) // if not a script file,
txwidget_append2(mLog,1,"\n""command: %s > %s \n",menu1,menu2); // echo command to window
kk = ii; // move to non-static memory
if (menus[kk].lock) ++menuLock; // call menu function
menus[kk].mfunc(menu2);
if (menus[kk].lock) --menuLock;
return;
}
// function to execute menu commands from a script file
void script_func(void *)
{
FILE *fid;
int cc, Nth;
ch buff[200], menu1[20], menu2[40];
ch *pp;
ch *bb;
fid = fopen(scrFile,"r"); // open file
if (! fid) {
txwidget_append2(mLog,0," *** can't open script file: %s \n",scrFile);
commFail++;
*scrFile = 0;
return;
}
while (true)
{
if (checkKillPause()) break; // exit script
if (commFail) break;
pp = fgets_trim(buff,199,fid,1); // read next record
if (! pp) break; // EOF
txwidget_append2(mLog,0,"\n""Script: %s \n",buff); // write to log
bb = strchr(buff,'#'); // get rid of comments
if (bb) *bb = 0;
cc = strTrim(buff); // and trailing blanks
if (cc < 2) continue;
*menu1 = *menu2 = 0;
*scriptParam = 0;
Nth = 1; // parse menu1 > menu2 > parameter
pp = substring(buff,'>',Nth++);
if (pp) strncpy0(menu1,pp,20);
pp = substring(buff,'>',Nth++);
if (pp) strncpy0(menu2,pp,40);
pp = substring(buff,'>',Nth++);
if (pp) strncpy0(scriptParam,pp,200);
strTrim(menu1); // get rid of trailing blanks
strTrim(menu2);
if (strmatch(menu1,"exit")) break;
menufunc(null,menu1); // simulate menu entries
menufunc(null,menu2);
while (Fdialog) sleep(1); // if dialog, wait for compl.
}
txwidget_append2(mLog,0,"script exiting \n");
fclose(fid);
*scrFile = 0;
return;
}
// quit dkopp, with last chance to save edits to backup job data
int quit_dkopp(ch *menu)
{
int yn;
if (! Fgui) return 0;
signalFunc("kill job");
if (BJmod) { // job data was modified
yn = zmessageYN(mWin,"SAVE changes to dkopp job?"); // give user a chance to save mods
if (yn) fileSave(null);
}
if (dvdmtd) {
unmountDVD(0); // unmount DVD/BRD
ejectDVD(0); // eject DVD/BRD (may not work)
}
gtk_main_quit(); // tell gtk_main() to quit
return 0;
}
// clear logging window
int clearScreen(ch *menu)
{
txwidget_clear(mLog);
return 0;
}
// kill/pause/resume current function - called from menu function
int signalFunc(ch *menu)
{
if (strmatch(menu,"kill job"))
{
if (! menuLock) {
txwidget_append2(mLog,0,"\n""ready \n"); // already dead
return 0;
}
if (killFlag) { // redundant kill
if (*subprocName) {
txwidget_append2(mLog,0," *** kill again: %s \n",subprocName);
signalProc(subprocName,"kill"); // kill subprocess
}
else txwidget_append2(mLog,0," *** waiting for function exit \n"); // or wait for function to die
return 0;
}
txwidget_append2(mLog,0," *** KILL current function \n"); // initial kill
pauseFlag = 0;
killFlag = 1;
if (*subprocName) {
signalProc(subprocName,"resume");
signalProc(subprocName,"kill");
}
return 0;
}
if (strmatch(menu,"pause")) {
pauseFlag = 1;
if (*subprocName) signalProc(subprocName,"pause");
return 0;
}
if (strmatch(menu,"resume")) {
pauseFlag = 0;
if (*subprocName) signalProc(subprocName,"resume");
return 0;
}
else zappcrash("signalFunc: %s",menu);
return 0;
}
// check kill and pause flags
// called periodically from long-running functions
int checkKillPause()
{
while (pauseFlag) { // idle loop while paused
zsleep(0.1);
zmainloop(); // process menus
}
if (killFlag) return 1; // return true = stop now
return 0; // return false = continue
}
// file open dialog - get backup job data from a file
int fileOpen(ch *menu)
{
ch *file;
int err = 0;
if (*scriptParam) { // get file from script
strcpy(BJfile,scriptParam);
*scriptParam = 0;
err = BJload(BJfile);
return err;
}
++Fdialog;
file = zgetfile("open backup job",MWIN,"file",homedir,1); // get file from user
if (file) {
if (strlen(file) > XFCC-2) zappcrash("pathname too big");
strcpy(BJfile,file);
zfree(file);
err = BJload(BJfile); // get job data from file
}
else err = 1;
--Fdialog;
return err;
}
// file save dialog - save backup job data to a file
int fileSave(ch *menu)
{
ch *file;
int nstat, err = 0;
if (*scriptParam) { // get file from script
strcpy(BJfile,scriptParam);
*scriptParam = 0;
BJstore(BJfile);
return 0;
}
if (! BJval) {
nstat = zmessageYN(mWin,"Job data not valid, save anyway?");
if (! nstat) return 0;
}
++Fdialog;
if (! *BJfile) strcpy(BJfile,"dkopp.job"); // if no job file, use default
file = zgetfile("save backup job",MWIN,"save",BJfile,1);
if (file) {
if (strlen(file) > XFCC-2) zappcrash("pathname too big");
strcpy(BJfile,file);
zfree(file);
err = BJstore(BJfile);
if (! err) BJmod = 0; // job not modified
}
--Fdialog;
return 0;
}
// backup job data <<< file
// errors not checked here are checked in BJvalidate()
int BJload(ch *fspec)
{
FILE *fid;
ch buff[1000];
ch *fgs, *rtype, *rdata;
ch rtype2[20];
int cc, Nth, nerrs;
BJreset(); // clear old job from memory
nerrs = 0;
txwidget_append2(mLog,1,"\n""loading job file: %s \n",fspec);
fid = fopen(fspec,"r"); // open file
if (! fid) {
txwidget_append2(mLog,0," *** cannot open job file: %s \n",fspec);
commFail++;
return 1;
}
while (true) // read file
{
fgs = fgets_trim(buff,998,fid,1);
if (! fgs) break; // EOF
cc = strlen(buff);
if (cc > 996) {
txwidget_append2(mLog,0," *** input record too big \n");
nerrs++;
continue;
}
Nth = 1;
rtype = substring(buff,' ',Nth++); // parse 1st field, record type
if (! rtype) rtype = "#"; // blank record is comment
strncpy0(rtype2,rtype,19);
strToLower(rtype2);
if (strmatch(rtype2,"device")) {
rdata = substring(buff,' ',Nth++); // DVD/BRD device: /dev/dvd
if (rdata) strncpy0(BJdvd,rdata,19);
continue;
}
if (strmatch(rtype2,"backup")) {
rdata = substring(buff,' ',Nth++); // backup mode
if (rdata) {
strncpy0(BJbmode,rdata,19);
strToLower(BJbmode);
}
continue;
}
if (strmatch(rtype2,"verify")) {
rdata = substring(buff,' ',Nth++); // verify mode
if (rdata) {
strncpy0(BJvmode,rdata,19);
strToLower(BJvmode);
}
continue;
}
if (strmatch(rtype2,"datefrom")) {
rdata = substring(buff,' ',Nth++); // file mod date selection
if (rdata) strncpy0(BJdatefrom,rdata,11);
continue;
}
if (strmatchV(rtype2,"include","exclude","#",null)) {
BJinex[BJnx] = zstrdup(buff,"BJinex"); // include/exclude or comment rec.
if (++BJnx >= maxnx) {
txwidget_append2(mLog,0," *** exceed %d include/exclude recs \n",maxnx);
nerrs++;
break;
}
continue;
}
txwidget_append2(mLog,0," *** unrecognized record: %s \n",buff);
continue;
}
fclose(fid); // close file
BJmod = 0; // new job, not modified
BJvalidate(0); // validation checks, set BJval
if (! nerrs && BJval) return 0;
BJval = 0;
commFail++;
return 1;
}
// backup job data >>> file
int BJstore(ch *fspec)
{
FILE *fid;
int ii;
fid = fopen(fspec,"w"); // open file
if (! fid) {
txwidget_append2(mLog,0," *** cannot open file: %s \n",fspec);
commFail++;
return 1;
}
fprintf(fid,"device %s \n",BJdvd); // device /dev/dvd
fprintf(fid,"backup %s \n",BJbmode); // backup full/incremental/accumulate
fprintf(fid,"verify %s \n",BJvmode); // verify full/incremental/thorough
fprintf(fid,"datefrom %s \n",BJdatefrom); // file mod date selection
for (ii = 0; ii < BJnx; ii++) // output all include/exclude recs
fprintf(fid,"%s \n",BJinex[ii]);
fclose(fid);
return 0;
}
// backup job data <<< DVD/BRD job file
// get job file from prior backup to this same medium
int BJvload(ch *menu)
{
ch vjfile[100];
BJreset(); // reset job data
mountDVD(0); // (re) mount DVD/BRD
if (! dvdmtd) {
commFail++;
return 1;
}
strcpy(vjfile,dvdmp); // dvd mount point
strcat(vjfile,V_JOBFILE); // + dvd job file
BJload(vjfile); // load job file (BJval set)
if (BJval) return 0;
commFail++;
return 1;
}
// edit dialog for backup job data
int BJedit(ch *menu)
{
zdialog *zd;
++Fdialog;
zd = zdialog_new("edit backup job",mWin,"browse","done","clear","cancel",null);
zdialog_add_widget(zd,"hbox","hb1","dialog",0,"space=5");
zdialog_add_widget(zd,"label","labdev","hb1","DVD/BRD device","space=3"); // DVD/BRD device [______________][v]
zdialog_add_widget(zd,"combo","entdvd","hb1",BJdvd);
zdialog_add_widget(zd,"hbox","hb2","dialog",0,"space=8");
zdialog_add_widget(zd,"button","bopen","hb2","open job file"); // [open job] [DVD/BRD job] [save as]
zdialog_add_widget(zd,"button","bdvd","hb2","open DVD/BRD job");
zdialog_add_widget(zd,"button","bsave","hb2"," save as ");
zdialog_add_widget(zd,"hbox","hb3","dialog");
zdialog_add_widget(zd,"vbox","vb3","hb3",0,"homog");
zdialog_add_widget(zd,"label","space","hb3",0,"space=20");
zdialog_add_widget(zd,"vbox","vb4","hb3",0,"homog");
zdialog_add_widget(zd,"label","labbmode","vb3","Backup Mode");
zdialog_add_widget(zd,"label","labvmode","vb4","Verify Mode");
zdialog_add_widget(zd,"radio","bmrb1","vb3","full"); // Backup Mode Verify Mode
zdialog_add_widget(zd,"radio","bmrb2","vb3","incremental"); // (o) full (o) full
zdialog_add_widget(zd,"radio","bmrb3","vb3","accumulate"); // (o) incremental (o) incremental
zdialog_add_widget(zd,"radio","vmrb1","vb4","full"); // (o) accumulate (o) thorough
zdialog_add_widget(zd,"radio","vmrb2","vb4","incremental"); // file date from: [ yyyy.mm.dd ]
zdialog_add_widget(zd,"radio","vmrb3","vb4","thorough");
zdialog_add_widget(zd,"label","labdate","vb3","file date from:");
zdialog_add_widget(zd,"zentry","entdate","vb4","yyyy.mm.dd","size=10");
zdialog_add_widget(zd,"hsep","sep2","dialog",0,"space=8"); // edit box for include/exclude recs
zdialog_add_widget(zd,"label","labinex","dialog","Include / Exclude Files");
zdialog_add_widget(zd,"frame","frminex","dialog",0,"expand");
zdialog_add_widget(zd,"scrwin","scrwinex","frminex");
zdialog_add_widget(zd,"zedit","edinex","scrwinex");
BJedit_stuff(zd); // stuff dialog widgets with job data
zdialog_resize(zd,0,600);
zdialog_run(zd,BJedit_event,"parent"); // run dialog
return 0;
}
// edit dialog event function
int BJedit_event(zdialog *zd, ch *event)
{
int zstat, err = 0;
zstat = zd->zstat;
zd->zstat = 0; // dialog may continue
if (zstat)
{
if (zstat == 1) { // browse, do file-chooser dialog
fc_dialog("/home");
return 0;
}
if (zstat == 2) { // done
BJedit_fetch(zd); // get all job data from dialog widgets
if (! BJval) commFail++;
zdialog_free(zd); // destroy dialog
--Fdialog;
return 0;
}
if (zstat == 3) {
txwidget_clear(editwidget); // clear include/exclude recs
return 0;
}
zdialog_free(zd); // cancel
--Fdialog;
return 0;
}
if (strmatch(event,"bopen")) {
err = fileOpen(""); // get job file from user
if (! err) BJedit_stuff(zd); // stuff dialog widgets
}
if (strmatch(event,"bdvd")) {
err = BJvload(""); // get job file on DVD/BRD
if (! err) BJedit_stuff(zd); // stuff dialog widgets
}
if (strmatch(event,"bsave")) {
BJedit_fetch(zd); // get job data from dialog widgets
fileSave(""); // save to file
}
return 0;
}
// backup job data in memory >>> job edit dialog widgets
int BJedit_stuff(zdialog * zd)
{
int ii;
for (ii = 0; ii < ndvds; ii++) // DVD/BRD drives available
zdialog_stuff(zd,"entdvd",dvddevdesc[ii]);
// remove mount point get/stuff
if (strmatch(BJbmode,"full")) zdialog_stuff(zd,"bmrb1",1);
if (strmatch(BJbmode,"incremental")) zdialog_stuff(zd,"bmrb2",1);
if (strmatch(BJbmode,"accumulate")) zdialog_stuff(zd,"bmrb3",1);
if (strmatch(BJvmode,"full")) zdialog_stuff(zd,"vmrb1",1);
if (strmatch(BJvmode,"incremental")) zdialog_stuff(zd,"vmrb2",1);
if (strmatch(BJvmode,"thorough")) zdialog_stuff(zd,"vmrb3",1);
zdialog_stuff(zd,"entdate",BJdatefrom); // file mod date selection
editwidget = zdialog_gtkwidget(zd,"edinex");
txwidget_clear(editwidget);
for (int ii = 0; ii < BJnx; ii++)
txwidget_append2(editwidget,0,"%s""\n",BJinex[ii]);
return 0;
}
// job edit dialog widgets >>> backup job data in memory
int BJedit_fetch(zdialog * zd)
{
int ii, line;
ch text[40], *pp;
BJreset(); // reset job data
zdialog_fetch(zd,"entdvd",text,19); // get DVD/BRD device
strncpy0(BJdvd,text,19);
pp = strchr(BJdvd,' ');
if (pp) *pp = 0;
// remove mount point fetch/save
zdialog_fetch(zd,"bmrb1",ii); if (ii) strcpy(BJbmode,"full"); // backup mode
zdialog_fetch(zd,"bmrb2",ii); if (ii) strcpy(BJbmode,"incremental");
zdialog_fetch(zd,"bmrb3",ii); if (ii) strcpy(BJbmode,"accumulate");
zdialog_fetch(zd,"vmrb1",ii); if (ii) strcpy(BJvmode,"full"); // verify mode
zdialog_fetch(zd,"vmrb2",ii); if (ii) strcpy(BJvmode,"incremental");
zdialog_fetch(zd,"vmrb3",ii); if (ii) strcpy(BJvmode,"thorough");
zdialog_fetch(zd,"entdate",BJdatefrom,11); // file mod date selection
for (line = 0; ; line++)
{
pp = txwidget_line(editwidget,line,1); // include/exclude recs.
if (! pp || ! *pp) break;
strTrim(pp); // remove trailing blanks
BJinex[BJnx] = zstrdup(pp,"BJinex"); // copy new record
if (++BJnx >= maxnx) {
txwidget_append2(mLog,0," *** exceed %d include/exclude recs \n",maxnx);
break;
}
}
BJmod++; // job modified
BJvalidate(0); // check for errors, set BJval
return 0;
}
// perform DVD/BRD backup using growisofs utility
int Backup(ch *menu)
{
strcpy(mbmode,"");
strcpy(mvmode,"");
if (strmatchV(menu,"full","incremental","accumulate",null)) // backup only
strcpy(mbmode,menu);
if (strmatch(menu,"run DVD/BRD")) BJvload(null); // load job file from DVD/BRD if req.
if (strmatchV(menu,"run job","run DVD/BRD",null)) { // if run job or job on DVD/BRD,
if (BJval) { // and valid job file,
strcpy(mbmode,BJbmode); // use job file backup & verify modes
strcpy(mvmode,BJvmode);
}
}
if (! BJval) { // check for errors
txwidget_append2(mLog,0," *** no valid backup job \n");
goto backup_done;
}
if (strmatch(mbmode,"full")) FullBackup(mvmode); // full backup (+ verify)
else IncrBackup(mbmode,mvmode); // incremental / accumulate (+ verify)
backup_done:
if (Fgui) txwidget_append2(mLog,0,"ready \n");
return 0;
}
// full backup using multiple DVD/BRD media if required
int FullBackup(ch *BJvmode)
{
FILE *fid = 0;
int gerr, ii, zstat;
ch command[200], Nspeed[20] = "";
ch *dfile, vfile[XFCC];
double secs, bspeed, time0;
dGetFiles(); // get files for backup
if (Dnf == 0) {
txwidget_append2(mLog,0," *** nothing to back-up \n");
goto backup_fail;
}
vFilesReset(); // reset DVD/BRD files data
txwidget_append2(mLog,1,"\n""begin full backup \n");
txwidget_append2(mLog,0," files: %d bytes: %.0f \n",Dnf, // files and bytes to copy
formatKBMB(Dbytes,3));
if (! *dvdlabel) strcpy(dvdlabel,"dkopp"); // if no label, default "dkopp"
BJstore(TFjobfile); // copy job file (DVD/BRD) to temp file
save_filepoop(); // + owner and permissions to temp file
writeDT(); // create date-time & usage temp file
fid = fopen(TFdiskfiles,"w"); // temp file for growisofs path-list
if (! fid) {
txwidget_append2(mLog,0," *** cannot open /tmp scratch file \n");
goto backup_fail;
}
fprintf(fid,"%s=%s\n",V_JOBFILE +1,TFjobfile); // add job file to growisofs list
fprintf(fid,"%s=%s\n",V_FILEPOOP +1,TFfilepoop); // add directory poop file
fprintf(fid,"%s=%s\n",V_DATETIME +1,TFdatetime); // add date-time file
Dbytes2 = 0.0;
for (ii = 0; ii < Dnf; ii++) // process all files for backup
{
dfile = Drec[ii].file; // add to growisofs path-list
repl_1str(dfile,vfile,XFCC,"=","\\\\="); // replace "=" with "\\=" in file name
fprintf(fid,"%s=%s\n",vfile+1,dfile); // directories/file=/directories/file
Dbytes2 += Drec[ii].size;
}
fclose(fid);
txwidget_append2(mLog,0," writing DVD/BRD, %s \n",formatKBMB(Dbytes,3));
start_timer(time0); // start timer for growisofs
snprintf(command,200, // build growisofs command line
"growisofs -Z %s %s -r -graft-points "
"-iso-level 4 -gui -V \"%s\" %s -path-list %s 2>&1", // label in quotes
BJdvd,Nspeed,dvdlabel,gforce,TFdiskfiles);
backup_retry:
gerr = do_shell("growisofs", command); // do growisofs, echo outputs
if (checkKillPause()) goto backup_fail; // killed by user
if (gerr) {
if (! Fgui) goto backup_fail;
zstat = zdialog_choose(mWin,"parent","growisofs error", // manual compensation for growisofs
"abort","retry","ignore (continue)",null); // and/or gnome bugs
if (zstat == 1) goto backup_fail;
if (zstat == 2) goto backup_retry;
}
secs = get_timer(time0); // output statistics
txwidget_append2(mLog,0," backup time: %.0f secs \n",secs);
bspeed = Dbytes2/1000000.0/secs;
txwidget_append2(mLog,0," backup speed: %.2f MB/sec \n",bspeed);
txwidget_append2(mLog,0," backup complete \n");
ejectDVD(0); // DVD may be hung after growisofs
verify_retry:
if (*BJvmode) // do verify if requested
{
mountDVD(0); // test if DVD hung
if (! dvdmtd) {
zstat = zdialog_choose(mWin,"parent","DVD mount failure",
"abort","retry","ignore (continue)",null);
if (zstat == 1) goto backup_fail;
if (zstat == 2) goto verify_retry;
}
Verify(BJvmode); // verify this DVD
if (commFail) {
zstat = zdialog_choose(mWin,"parent","verify error",
"abort","retry","ignore (continue)",null);
if (zstat == 1) goto backup_fail;
if (zstat == 2) goto verify_retry;
txwidget_append2(mLog,0," backup is being repeated \n");
commFail = 0;
}
}
createBackupHist(); // create backup history file
txwidget_append2(mLog,0," backup job complete \n");
ejectDVD(0);
return 0;
backup_fail:
commFail++;
secs = get_timer(time0); // output stats even if failed
txwidget_append2(mLog,0," backup time: %.0f secs \n",secs);
bspeed = Dbytes2/1000000.0/secs;
txwidget_append2(mLog,0," backup speed: %.2f MB/sec \n",bspeed);
txwidget_append2(mLog,1," *** BACKUP FAILED \n");
txwidget_append2(mLog,0," media may be OK: check with Verify \n");
ejectDVD(0);
return 0;
}
// incremental / accumulate backup (one DVD/BRD only)
int IncrBackup(ch *BJbmode, ch *BJvmode)
{
FILE *fid = 0;
int gerr, ii, zstat;
ch command[200], Nspeed[20] = "";
ch *dfile, vfile[XFCC], disp;
double secs, bspeed;
double time0;
mountDVD(0); // requires successful mount
if (! dvdmtd) goto backup_fail;
dGetFiles(); // get files for backup
vGetFiles(); // get DVD/BRD files
setFileDisps(); // file disps: new mod del unch
if (! Dnf) {
txwidget_append2(mLog,0," *** no files for backup \n");
goto backup_fail;
}
if (! Vnf) {
txwidget_append2(mLog,0," *** no DVD/BRD files \n");
goto backup_fail;
}
txwidget_append2(mLog,1,"\n""begin %s backup \n",BJbmode);
txwidget_append2(mLog,0," files: %d bytes: %s \n",Mfiles, // files and bytes to copy
formatKBMB(Mbytes,3));
if (Mfiles == 0) { // nothing to back up
txwidget_append2(mLog,0," nothing to back-up \n");
return 0;
}
if (! *dvdlabel) strcpy(dvdlabel,"dkopp"); // if no label, default "dkopp"
fid = fopen(TFdiskfiles,"w"); // temp file for growisofs path-list
if (! fid) {
txwidget_append2(mLog,0," *** cannot open /tmp scratch file \n");
goto backup_fail;
}
BJstore(TFjobfile); // copy job file to temp file
save_filepoop(); // + file owner & permissions
writeDT(); // create date-time & usage temp file
fprintf(fid,"%s=%s\n",V_JOBFILE +1,TFjobfile); // add job file to growisofs list
fprintf(fid,"%s=%s\n",V_FILEPOOP +1,TFfilepoop); // add directory poop file
fprintf(fid,"%s=%s\n",V_DATETIME +1,TFdatetime); // add date-time file
for (ii = 0; ii < Dnf; ii++) { // process new and modified disk files
disp = Drec[ii].disp;
if ((disp == 'n') || (disp == 'm')) { // new or modified file
dfile = Drec[ii].file; // add to growisofs path-list
repl_1str(dfile,vfile,XFCC,"=","\\\\="); // replace "=" with "\\=" in file name
fprintf(fid,"%s=%s\n",vfile+1,dfile); // directories/file=/directories/file
Drec[ii].ivf = 1; // set flag for incr. verify
}
}
if (strmatch(BJbmode,"incremental")) { // incremental backup (not accumulate)
for (ii = 0; ii < Vnf; ii++) { // process deleted files still on DVD/BRD
if (Vrec[ii].disp == 'd') {
dfile = Vrec[ii].file; // add to growisofs path-list
repl_1str(dfile,vfile,XFCC,"=","\\\\="); // replace "=" with "\\=" in file name
fprintf(fid,"%s=%s\n",vfile+1,"/dev/null"); // directories/file=/dev/null
}
}
}
fclose(fid);
start_timer(time0); // start timer for growisofs
snprintf(command,200,"growisofs -M %s %s -r -graft-points " // build growisofs command line
"-iso-level 4 -gui -V %s %s -path-list %s 2>&1",
BJdvd,Nspeed,dvdlabel,gforce,TFdiskfiles);
backup_retry:
gerr = do_shell("growisofs", command); // do growisofs, echo outputs
if (checkKillPause()) goto backup_fail; // killed by user
if (gerr) {
zstat = zdialog_choose(mWin,"parent","growisofs error", // manual compensation for growisofs
"abort","retry","ignore (continue)",null); // and/or gnome bugs
if (zstat == 1) goto backup_fail;
if (zstat == 2) goto backup_retry;
}
secs = get_timer(time0); // output statistics
txwidget_append2(mLog,0," backup time: %.0f secs \n",secs);
bspeed = Mbytes/1000000.0/secs;
txwidget_append2(mLog,0," backup speed: %.2f MB/sec \n",bspeed);
txwidget_append2(mLog,0," backup complete \n");
vFilesReset(); // reset DVD/BRD files
ejectDVD(0); // DVD may be hung after growisofs
sleep(5);
verify_retry:
if (*BJvmode) // do verify if requested
{
mountDVD(0); // test if DVD/BRD hung
if (! dvdmtd) {
zstat = zdialog_choose(mWin,"parent","DVD mount failure",
"abort","retry","ignore (continue)",null);
if (zstat == 1) goto backup_fail;
if (zstat == 2) goto verify_retry;
}
Verify(BJvmode); // verify new files on DVD/BRD
if (commFail) {
zstat = zdialog_choose(mWin,"parent","verify error",
"abort","retry","ignore (continue)",null);
if (zstat == 1) goto backup_fail;
if (zstat == 2) goto verify_retry;
}
}
createBackupHist(); // create backup history file
ejectDVD(0);
return 0;
backup_fail:
commFail++;
txwidget_append2(mLog,1," *** BACKUP FAILED \n");
vFilesReset();
ejectDVD(0);
return 0;
}
// verify DVD/BRD disc data integrity
int Verify(ch *menu)
{
int ii, comp, vfiles;
int dfiles1 = 0, dfiles2 = 0;
int verrs = 0, cerrs = 0;
ch *filespec;
ch *errmess = 0;
double secs, dcc1, vbytes, vspeed;
double mtime, diff;
double time0;
STATB filestat;
vGetFiles(); // get DVD/BRD files
txwidget_append2(mLog,0," %d files on DVD/BRD \n",Vnf);
if (! Vnf) goto verify_exit;
vfiles = verrs = cerrs = 0;
vbytes = 0.0;
start_timer(time0);
if (strmatch(menu,"full")) // verify all files are readable
{
txwidget_append2(mLog,1,"\n""verify ALL files on DVD/BRD \n");
if (Fgui) txwidget_append2(mLog,0,"\n\n");
for (ii = 0; ii < Vnf; ii++)
{
if (checkKillPause()) goto verify_exit;
filespec = Vrec[ii].file; // /home/.../file.ext
track_filespec(filespec); // track progress on screen
errmess = checkFile(filespec,0,dcc1); // check file, get length
if (errmess) track_filespec_err(filespec,errmess); // log errors
if (errmess) verrs++;
vfiles++;
vbytes += dcc1;
if (verrs + cerrs > 100) {
txwidget_append2(mLog,1," *** OVER 100 ERRORS, GIVING UP *** \n");
goto verify_exit;
}
}
}
if (strmatch(menu,"incremental")) // verify files in prior incr. backup
{
txwidget_append2(mLog,1,"\n""verify files in prior incremental backup \n");
for (ii = 0; ii < Dnf; ii++)
{
if (checkKillPause()) goto verify_exit;
if (! Drec[ii].ivf) continue; // skip if not in prior incr. backup
filespec = Drec[ii].file;
txwidget_append2(mLog,0," %s \n",kleenex(filespec)); // output filespec
errmess = checkFile(filespec,0,dcc1); // check file on DVD/BRD, get length
if (errmess) txwidget_append2(mLog,0," *** %s \n",errmess);
if (errmess) verrs++;
vfiles++;
vbytes += dcc1;
if (verrs + cerrs > 100) {
txwidget_append2(mLog,1," *** OVER 100 ERRORS, GIVING UP *** \n");
goto verify_exit;
}
}
}
if (strmatch(menu,"thorough")) // compare DVD/BRD to disk files
{
txwidget_append2(mLog,1,"\n Read and verify ALL files on DVD/BRD. \n");
txwidget_append2(mLog,0," Compare to disk files with matching names and mod times.\n");
if (Fgui) txwidget_append2(mLog,0,"\n\n");
for (ii = 0; ii < Vnf; ii++) // process DVD/BRD files
{
if (checkKillPause()) goto verify_exit;
filespec = Vrec[ii].file; // corresp. file name on disk
track_filespec(filespec); // track progress on screen
comp = 0;
if (stat(filespec,&filestat) == 0) { // disk file exists?
mtime = filestat.st_mtime + filestat.st_mtim.tv_nsec * nano; // yes, get file mod time
diff = fabs(mtime - Vrec[ii].mtime); // compare to DVD/BRD file mod time
if (diff < modtimetolr) comp = 1; // equal
dfiles1++; // count matching disk names
dfiles2 += comp; // count matching names and mod times
}
errmess = checkFile(filespec,comp,dcc1); // check DVD/BRD file, compare to disk
if (errmess) track_filespec_err(filespec,errmess); // log errors
if (errmess) {
if (strstr(errmess,"compare")) cerrs++; // file compare error
else verrs++;
}
vfiles++;
vbytes += dcc1;
if (verrs + cerrs > 100) {
txwidget_append2(mLog,1," *** OVER 100 ERRORS, GIVING UP *** \n");
goto verify_exit;
}
}
}
txwidget_append2(mLog,0," DVD/BRD files: %d bytes: %s \n",vfiles,formatKBMB(vbytes,3));
txwidget_append2(mLog,0," DVD/BRD read errors: %d \n",verrs);
if (strmatch(menu,"thorough")) {
txwidget_append2(mLog,0," matching disk names: %d mod times: %d \n",dfiles1,dfiles2);
txwidget_append2(mLog,0," compare failures: %d \n",cerrs);
}
secs = get_timer(time0);
txwidget_append2(mLog,0," verify time: %.0f secs \n",secs);
vspeed = vbytes/1000000.0/secs;
txwidget_append2(mLog,0," verify speed: %.2f MB/sec \n",vspeed);
if (verrs + cerrs) txwidget_append2(mLog,1," *** THERE WERE ERRORS *** \n");
else txwidget_append2(mLog,0," NO ERRORS \n");
verify_exit:
if (! Vnf) txwidget_append2(mLog,0," *** no files on DVD/BRD \n");
if (! Vnf) commFail++;
if (verrs + cerrs) commFail++;
if (Fgui) txwidget_append2(mLog,0," ready \n");
return 0;
}
// Reports menu function
int Report(ch *menu)
{
if (strmatch(menu, "get files for backup")) get_current_files(0);
if (strmatch(menu, "diffs summary")) report_summary_diffs(0);
if (strmatch(menu, "diffs by directory")) report_directory_diffs(0);
if (strmatch(menu, "diffs by file")) report_file_diffs(0);
if (strmatch(menu, "list files for backup")) list_current_files(0);
if (strmatch(menu, "list DVD/BRD files")) list_DVD_files(0);
if (strmatch(menu, "find files")) find_files(0);
if (strmatch(menu, "view backup hist")) view_backup_hist(0);
return 0;
}
// refresh files for backup and report summary statistics per include/exclude statement
int get_current_files(ch *menu)
{
ch *bytes;
int ii;
dFilesReset(); // force refresh
dGetFiles(); // get disk files
if (! BJval) {
txwidget_append2(mLog,0," *** backup job is invalid \n");
goto report_exit;
}
txwidget_append2(mLog,1,"\n files bytes include/exclude filespec \n");
for (ii = 0; ii < BJnx; ii++)
{
bytes = formatKBMB(BJbytes[ii],3);
if (BJfiles[ii] > 0) txwidget_append2(mLog,0," %6d %9s", BJfiles[ii], bytes);
if (BJfiles[ii] < 0) txwidget_append2(mLog,0," %6d %9s", BJfiles[ii], bytes);
if (BJfiles[ii] == 0) txwidget_append2(mLog,0," ");
txwidget_append2(mLog,0," %s \n",BJinex[ii]);
}
bytes = formatKBMB(Dbytes,3);
txwidget_append2(mLog,0," %6d %9s TOTAL \n", Dnf, bytes);
report_exit:
if (Fgui) txwidget_append2(mLog,0," ready \n");
return 0;
}
// report disk:DVD/BRD differences summary
int report_summary_diffs(ch *menu)
{
ch *bytes;
if (! BJval) {
txwidget_append2(mLog,0," *** backup job is invalid \n");
goto report_exit;
}
dGetFiles();
vGetFiles();
setFileDisps();
txwidget_append2(mLog,0,"\n disk files: %d DVD/BRD files: %d \n",Dnf,Vnf);
txwidget_append2(mLog,0,"\n Differences between DVD/BRD and files on disk: \n");
txwidget_append2(mLog,0," %7d disk files not on DVD/BRD - new \n",nnew);
txwidget_append2(mLog,0," %7d files on disk and DVD/BRD - unchanged \n",nunc);
txwidget_append2(mLog,0," %7d files on disk and DVD/BRD - modified \n",nmod);
txwidget_append2(mLog,0," %7d DVD/BRD files not on disk - deleted \n",ndel);
bytes = formatKBMB(Mbytes,3);
txwidget_append2(mLog,0," Total differences: %d files %s \n",nnew+ndel+nmod,bytes);
report_exit:
if (Fgui) txwidget_append2(mLog,0," ready \n");
return 0;
}
// report disk:DVD/BRD differences by directory, summary statistics
int report_directory_diffs(ch *menu)
{
int kfiles, knew, kdel, kmod;
int dii, vii, comp;
ch *pp, *pdirk, *bytes, ppdirk[XFCC];
double nbytes;
if (! BJval) {
txwidget_append2(mLog,0," *** backup job is invalid \n");
goto report_exit;
}
dGetFiles();
vGetFiles();
setFileDisps();
SortFileList((ch *) Drec, sizeof(dfrec), Dnf, 'D'); // re-sort, directories first
SortFileList((ch *) Vrec, sizeof(vfrec), Vnf, 'D');
txwidget_append2(mLog,0,"\n Disk:DVD/BRD differences by directory \n");
txwidget_append2(mLog,0," new mod del bytes directory \n");
nbytes = kfiles = knew = kmod = kdel = 0;
dii = vii = 0;
while ((dii < Dnf) || (vii < Vnf)) // scan disk and DVD/BRD files parallel
{
if ((dii < Dnf) && (vii == Vnf)) comp = -1;
else if ((dii == Dnf) && (vii < Vnf)) comp = +1;
else comp = filecomp(Drec[dii].file, Vrec[vii].file);
if (comp > 0) pdirk = Vrec[vii].file; // get file on DVD/BRD or disk
else pdirk = Drec[dii].file;
pp = (ch *) strrchr(pdirk,'/'); // isolate directory
if (pp) *pp = 0;
if (! strmatch(pdirk,ppdirk)) { // if directory changed, output
bytes = formatKBMB(nbytes,3); // totals from prior directory
if (kfiles > 0) txwidget_append2(mLog,0," %5d %5d %5d %8s %s \n",
knew,kmod,kdel,bytes,ppdirk);
nbytes = kfiles = knew = kmod = kdel = 0; // reset totals
strcpy(ppdirk,pdirk); // start new directory
}
if (pp) *pp = '/';
if (comp < 0) { // unmatched disk file
knew++; // count new file
nbytes += Drec[dii].size;
kfiles++;
dii++;
}
else if (comp > 0) { // unmatched DVD/BRD file: deleted
kdel++; // count deleted file
kfiles++;
vii++;
}
else if (comp == 0) { // file present on disk and DVD/BRD
if (Drec[dii].disp == 'm') {
kmod++; // count modified file
nbytes += Drec[dii].size;
kfiles++;
}
dii++; // other: u = unchanged
vii++;
}
}
if (kfiles > 0) {
bytes = formatKBMB(nbytes,3); // totals from last directory
txwidget_append2(mLog,0," %5d %5d %5d %8s %s \n",knew,kmod,kdel,
bytes,ppdirk);
}
SortFileList((ch *) Drec, sizeof(dfrec), Dnf, 'A'); // restore ascii sort
SortFileList((ch *) Vrec, sizeof(vfrec), Vnf, 'A');
report_exit:
if (Fgui) txwidget_append2(mLog,0," ready \n");
return 0;
}
// report disk:DVD/BRD differences by file (new, modified, deleted)
int report_file_diffs(ch *menu)
{
int dii, vii;
if (! BJval) {
txwidget_append2(mLog,0," *** backup job is invalid \n");
goto report_exit;
}
report_summary_diffs(0); // report summary first
txwidget_append2(mLog,0,"\n Detailed list of disk:DVD/BRD differences: \n");
txwidget_append2(mLog,0,"\n %d new files (on disk, not on DVD/BRD) \n",nnew);
for (dii = 0; dii < Dnf; dii++)
{
if (Drec[dii].disp != 'n') continue;
txwidget_append2(mLog,0," %s \n",kleenex(Drec[dii].file));
if (checkKillPause()) goto report_exit;
}
txwidget_append2(mLog,0,"\n %d modified files (disk and DVD/BRD files are different) \n",nmod);
for (dii = 0; dii < Dnf; dii++)
{
if (Drec[dii].disp != 'm') continue;
txwidget_append2(mLog,0," %s \n",kleenex(Drec[dii].file));
if (checkKillPause()) goto report_exit;
}
txwidget_append2(mLog,0,"\n %d deleted files (on DVD/BRD, not on disk) \n",ndel);
for (vii = 0; vii < Vnf; vii++)
{
if (Vrec[vii].disp != 'd') continue;
txwidget_append2(mLog,0," %s \n",kleenex(Vrec[vii].file));
if (checkKillPause()) goto report_exit;
}
report_exit:
if (Fgui) txwidget_append2(mLog,0," ready \n");
return 0;
}
// list all files for backup
int list_current_files(ch *menu)
{
int dii;
if (! BJval) {
txwidget_append2(mLog,0," *** backup job is invalid \n");
goto report_exit;
}
txwidget_append2(mLog,0,"\n List all files for backup: \n");
dGetFiles();
txwidget_append2(mLog,0," %d files found \n",Dnf);
for (dii = 0; dii < Dnf; dii++)
{
if (checkKillPause()) break;
txwidget_append2(mLog,0," %s \n",kleenex(Drec[dii].file));
}
report_exit:
if (Fgui) txwidget_append2(mLog,0," ready \n");
return 0;
}
// list all files on mounted DVD/BRD
int list_DVD_files(ch *menu)
{
int vii;
txwidget_append2(mLog,0,"\n List all files on DVD/BRD: \n");
vGetFiles();
txwidget_append2(mLog,0," %d files found \n",Vnf);
for (vii = 0; vii < Vnf; vii++)
{
if (checkKillPause()) break;
txwidget_append2(mLog,0," %s \n",kleenex(Vrec[vii].file));
}
if (Fgui) txwidget_append2(mLog,0," ready \n");
return 0;
}
// find desired files on disk, on mounted DVD/BRD, and in history files
int find_files(ch *menu)
{
int dii, vii, hii, ftf, nn;
ch *fspec1, *hfile1;
static ch fspec2[200] = "/home/*/file*";
ch hfile[200], buff[1000], *pp;
FILE *fid;
zlist_t *zlist = 0;
dGetFiles(); // get disk and DVD/BRD files
if (dvdmtd) vGetFiles();
else txwidget_append2(mLog,0," DVD/BRD not mounted \n");
txwidget_append2(mLog,0,"\n find files matching wildcard pattern \n"); // get search pattern
fspec1 = zdialog_text(mWin,"enter (wildcard) filespec:",fspec2);
if (blank_null(fspec1)) goto report_exit;
strncpy0(fspec2,fspec1,199);
strTrim(fspec2);
txwidget_append2(mLog,0," search pattern: %s \n",fspec2);
txwidget_append2(mLog,1,"\n matching files on disk: \n");
for (dii = 0; dii < Dnf; dii++) // search disk files
{
if (checkKillPause()) goto report_exit;
if (MatchWild(fspec2,Drec[dii].file) == 0)
txwidget_append2(mLog,0," %s \n",kleenex(Drec[dii].file));
}
txwidget_append2(mLog,1,"\n matching files on DVD/BRD: \n");
for (vii = 0; vii < Vnf; vii++) // search DVD/BRD files
{
if (checkKillPause()) goto report_exit;
if (MatchWild(fspec2,Vrec[vii].file) == 0)
txwidget_append2(mLog,0," %s \n",kleenex(Vrec[vii].file));
}
txwidget_append2(mLog,1,"\n matching files in backup history: \n");
zlist = zlist_new(maxhist);
snprintf(hfile,199,"%s/dkopp-hist-*",homedir); // find all backup history files
ftf = 1; // /home/user/.dkopp/dkopp-hist-*
nn = 0;
while (true)
{
hfile1 = SearchWild(hfile,ftf);
if (! hfile1) break;
if (nn == maxhist) break;
zlist_append(zlist,hfile1,0); // add to list
nn++;
}
if (nn == 0) txwidget_append2(mLog,0," no history files found \n");
if (nn == maxhist) txwidget_append2(mLog,0," *** too many history files, please purge");
if (nn == 0 || nn == maxhist) goto report_exit;
zlist_purge(zlist); // purge null entries 7.4
zlist_sort(zlist); // sort list ascending
for (hii = 0; hii < nn; hii++) // loop all history files
{
hfile1 = zlist_get(zlist,hii);
txwidget_append2(mLog,0," %s \n",hfile1);
fid = fopen(hfile1,"r"); // next history file
if (! fid) {
txwidget_append2(mLog,0," *** file open error \n");
continue;
}
while (true) // read and search for match
{
if (checkKillPause()) break;
pp = fgets_trim(buff,999,fid,1);
if (! pp) break;
if (MatchWild(fspec2,buff) == 0)
txwidget_append2(mLog,0," %s \n",buff);
}
fclose(fid);
}
report_exit:
if (zlist) zlist_free(zlist);
if (Fgui) txwidget_append2(mLog,0," ready \n");
return 0;
}
// list available backup history files, select one to view
int view_backup_hist(ch *menu)
{
ch *fspec1;
ch fspec2[200], histfile[200];
ch *pp;
int ii, jj, nn;
int zstat, ftf;
zdialog *zd;
zlist_t *zlist = 0;
txwidget_append2(mLog,0," available history files in %s \n",homedir);
snprintf(fspec2,199,"%s/dkopp-hist-*",homedir);
zlist = zlist_new(maxhist);
ftf = 1;
nn = 0;
while (true)
{
fspec1 = SearchWild(fspec2,ftf); // file: dkopp-hist-yyyymmdd-hhmm-label
if (! fspec1) break;
pp = (ch *) strrchr(fspec1,'/') + 12; // get yyyymmdd-hhmm-label
if (nn == maxhist) break;
zlist_append(zlist,pp,0); // add to list
nn++;
}
if (nn == 0) txwidget_append2(mLog,0," no history files found \n");
if (nn == maxhist) txwidget_append2(mLog,0," *** too many history files, please purge");
if (nn == 0 || nn == maxhist) goto report_exit;
zlist_purge(zlist); // purge null entries 7.4
zlist_sort(zlist); // sort list ascending
for (ii = 0; ii < nn; ii++) // report sorted list
txwidget_append2(mLog,0," dkopp-hist-%s \n",zlist_get(zlist,ii));
zd = zdialog_new("choose history file",mWin,"OK","cancel",null);
zdialog_add_widget(zd,"label","lab1","dialog","history file date and label");
zdialog_add_widget(zd,"combo","hfile","dialog");
jj = nn - 20;
if (jj < 0) jj = 0;
for (ii = jj; ii < nn; ii++) // stuff combo box list with
zdialog_stuff(zd,"hfile",zlist_get(zlist,ii)); // 20 newest hist file IDs
zdialog_stuff(zd,"hfile",zlist_get(zlist,nn-1)); // default entry is newest file
zdialog_run(zd,0,"parent"); // run dialog
zstat = zdialog_wait(zd);
zdialog_fetch(zd,"hfile",histfile,199); // get user choice
zdialog_free(zd);
if (zstat != 1) goto report_exit; // cancelled
zshell("log ack","xdg-open %s/%s-%s",homedir,"dkopp-hist",histfile); // view the file
report_exit:
if (zlist) zlist_free(zlist);
if (Fgui) txwidget_append2(mLog,0," ready \n");
return 0;
}
// file restore dialog - specify DVD/BRD files to be restored
int RJedit(ch *menu)
{
zdialog *zd;
txwidget_append2(mLog,0,"\n Restore files from DVD/BRD \n");
vGetFiles(); // get files on DVD/BRD
txwidget_append2(mLog,0," %d files on DVD/BRD \n",Vnf);
if (! Vnf) return 0;
++Fdialog;
zd = zdialog_new("copy files from DVD/BRD",mWin,"browse","done","cancel",null);
zdialog_add_widget(zd,"hbox","hb1","dialog",0,"space=10");
zdialog_add_widget(zd,"vbox","vb1","hb1",0,"homog|space=5");
zdialog_add_widget(zd,"vbox","vb2","hb1",0,"homog|space=5");
zdialog_add_widget(zd,"label","labdev","vb1","DVD/BRD device"); // DVD/BRD device [___________][v]
zdialog_add_widget(zd,"combo","entdvd","vb2",BJdvd);
zdialog_add_widget(zd,"label","labfrom","vb1","copy-from DVD/BRD"); // copy-from DVD/BRD [______________]
zdialog_add_widget(zd,"label","labto","vb1","copy-to disk"); // copy-to disk [______________]
zdialog_add_widget(zd,"zentry","entfrom","vb2",RJfrom);
zdialog_add_widget(zd,"zentry","entto","vb2",RJto);
zdialog_add_widget(zd,"hsep","hsep1","dialog");
zdialog_add_widget(zd,"label","labfiles","dialog","files to restore");
zdialog_add_widget(zd,"frame","framefiles","dialog",0,"expand");
zdialog_add_widget(zd,"scrwin","scrfiles","framefiles");
zdialog_add_widget(zd,"zedit","editfiles","scrfiles");
for (int ii = 0; ii < ndvds; ii++) // load curr. data into widgets
zdialog_stuff(zd,"entdvd",dvddevdesc[ii]);
// remove get/stuff mount point
editwidget = zdialog_gtkwidget(zd,"editfiles");
for (int ii = 0; ii < RJnx; ii++) // get restore include/exclude recs,
txwidget_append2(editwidget,0,"%s""\n",RJinex[ii]); // pack into file selection edit box
zdialog_resize(zd,400,400);
zdialog_run(zd,RJedit_event,"parent"); // run dialog with response function
return 0;
}
// edit dialog event function
int RJedit_event(zdialog *zd, ch *event)
{
ch text[40], *pp, fcfrom[XFCC];
int zstat, line, cc;
zstat = zd->zstat;
if (! zstat) return 0;
if (zstat != 1 && zstat != 2) goto end_dialog; // cancel or destroy
RJreset(); // reset restore job data
zdialog_fetch(zd,"entdvd",text,19); // get DVD/BRD device
strncpy0(BJdvd,text,19);
pp = strchr(BJdvd,' ');
if (pp) *pp = 0;
// remove fetch/save mount point
zdialog_fetch(zd,"entfrom",RJfrom,XFCC); // copy-from location /home/xxx/.../
strTrim(RJfrom);
zdialog_fetch(zd,"entto",RJto,XFCC); // copy-to location /home/yyy/.../
strTrim(RJto);
for (line = 0; ; line++)
{
pp = txwidget_line(editwidget,line,1);
if (! pp || ! *pp) break;
cc = strTrim(pp); // remove trailing blanks
if (cc < 3) continue; // ignore absurdities
if (cc > XFCC-100) continue;
RJinex[RJnx] = zstrdup(pp,"RJinex"); // copy new record
if (++RJnx == maxnx) {
txwidget_append2(mLog,0," *** exceed %d include/exclude recs \n",maxnx);
break;
}
}
if (zstat == 1) { // do file-chooser dialog
strcpy(fcfrom,dvdmp); // start at /media/xxxx/home/xxxx/
strcat(fcfrom,RJfrom);
fc_dialog(fcfrom);
zd->zstat = 0; // dialog continues
return 0;
}
RJvalidate(); // validate restore job data
if (RJval) rGetFiles(); // get files to restore
else txwidget_append2(mLog,0," *** correct errors in restore job \n");
end_dialog:
zdialog_free(zd); // destroy dialog
--Fdialog;
return 0;
}
// List and validate DVD/BRD files to be restored
int RJlist(ch *menu)
{
int cc1, cc2;
ch *file1, file2[XFCC];
if (! RJval) {
txwidget_append2(mLog,0," *** restore job has errors \n");
goto list_exit;
}
txwidget_append2(mLog,0,"\n copy %d files from DVD/BRD: %s \n",Rnf, RJfrom);
txwidget_append2(mLog,0," to directory: %s \n",RJto);
txwidget_append2(mLog,0,"\n resulting files will be the following: \n");
if (! Rnf) goto list_exit;
cc1 = strlen(RJfrom); // from: /home/xxx/.../
cc2 = strlen(RJto); // to: /home/yyy/.../
for (int ii = 0; ii < Rnf; ii++)
{
if (checkKillPause()) goto list_exit;
file1 = Rrec[ii].file;
if (! strmatchN(file1,RJfrom,cc1)) {
txwidget_append2(mLog,0," *** not within copy-from: %s \n",kleenex(file1));
RJval = 0;
continue;
}
strcpy(file2,RJto);
strcpy(file2+cc2,file1+cc1);
txwidget_append2(mLog,0," %s \n",kleenex(file2));
}
list_exit:
if (Fgui) txwidget_append2(mLog,0," ready \n");
return 0;
}
// Restore files based on data from restore dialog
int Restore(ch *menu)
{
int ii, nn, ccf;
ch dfile[XFCC];
ch *errmess;
if (! RJval || ! Rnf) {
txwidget_append2(mLog,0," *** restore job has errors \n");
goto restore_exit;
}
nn = zmessageYN(mWin,"Restore %d files from: %s%s \n to: %s \n"
"Proceed with file restore ?",Rnf,dvdmp,RJfrom,RJto);
if (! nn) goto restore_exit;
txwidget_append2(mLog,1,"\n""begin restore of %d files to: %s \n",Rnf,RJto);
ccf = strlen(RJfrom); // from: /media/xxx/filespec
for (ii = 0; ii < Rnf; ii++)
{
if (checkKillPause()) goto restore_exit;
strcpy(dfile,RJto); // to: /destination/filespec
strcat(dfile,Rrec[ii].file+ccf);
txwidget_append2(mLog,0," %s \n",kleenex(dfile));
errmess = copyfile(Rrec[ii].file,dfile);
if (errmess) txwidget_append2(mLog,0," *** %s \n",errmess);
}
restore_filepoop(); // restore owner/permissions
dFilesReset(); // reset disk file data
restore_exit:
if (Fgui) txwidget_append2(mLog,0," ready \n");
return 0;
}
// get available DVD/BRD devices
// the lshw command blocks everything for several seconds
int getDVDs(void *) // overhauled
{
int ii, dvdrw;
ch buff[200], *pp;
ch command[50] = "lshw -class disk 2>/dev/null"; // better than udevadm
FILE *fid;
dvdrw = ndvds = 0;
fid = popen(command,"r");
if (! fid) {
txwidget_append2(mLog,0," No DVD/BRD devices found");
return 0;
}
while (true)
{
pp = fgets_trim(buff,200,fid); // read lshw command output
if (! pp) break;
if (strstr(buff,"*-")) { // start some device
if (strstr(buff,"*-cdrom")) dvdrw = 1; // start DVD/BRD device
else dvdrw = 0;
continue;
}
if (! dvdrw) continue; // ignore recs for other devices
if (strstr(buff,"description:")) {
pp = strstr(buff,"description:"); // save DVD/BRD description
pp += 12;
if (*pp == ' ') pp++; // (assume description comes first)
strncpy0(dvddesc[ndvds],pp,40);
continue;
}
if (strstr(buff,"/dev/")) {
pp = strstr(buff,"/dev/"); // have /dev/sr0 or similar format
if (pp[7] < '0' || pp[7] > '9') continue;
pp[8] = 0;
strcpy(dvddevs[ndvds],pp); // save DVD/BRD device
ndvds++;
continue;
}
}
pclose(fid);
for (ii = 0; ii < ndvds; ii++) // combine devices and descriptions
{ // for use in GUI chooser list
strcpy(dvddevdesc[ii],dvddevs[ii]);
strcat(dvddevdesc[ii]," ");
strcat(dvddevdesc[ii],dvddesc[ii]);
}
txwidget_append2(mLog,0," DVD/BRD devices found: %d \n",ndvds); // output list of DVDs
for (ii = 0; ii < ndvds; ii++)
txwidget_append2(mLog,0," %s %s \n",dvddevs[ii],dvddesc[ii]);
return 0;
}
// set DVD/BRD device and mount point
int setDVDdevice(ch *menu)
{
ch *pp1;
ch *pp2, text[60];
int ii, Nth, zstat;
zdialog *zd;
if (*scriptParam) { // script
Nth = 1; // parse: /dev/dvd /media/xxxx
pp1 = substring(scriptParam,' ',Nth++);
if (pp1) strncpy0(BJdvd,pp1,19);
pp1 = substring(scriptParam,' ',Nth++);
if (pp1) {
strncpy0(dvdmp,pp1,99);
dvdmpcc = strlen(dvdmp);
if (dvdmp[dvdmpcc-1] == '/')
dvdmp[dvdmpcc--] = 0; // remove trailing /
}
*scriptParam = 0;
return 0;
}
zd = zdialog_new("select DVD/BRD drive",mWin,"OK","cancel",null); // dialog to select DVD/BRD & mount point
zdialog_add_widget(zd,"hbox","hb1","dialog",0,"space=5");
zdialog_add_widget(zd,"vbox","vb1","hb1",0,"homog|space=5");
zdialog_add_widget(zd,"vbox","vb2","hb1",0,"homog|space=5");
zdialog_add_widget(zd,"label","labdvd","vb1","DVD/BRD device");
zdialog_add_widget(zd,"label","labmp","vb1","mount point");
zdialog_add_widget(zd,"combo","entdvd","vb2",BJdvd);
zdialog_add_widget(zd,"zentry","entmp","vb2",dvdmp);
for (ii = 0; ii < ndvds; ii++) // stuff avail. DVDs, mount points
zdialog_stuff(zd,"entdvd",dvddevdesc[ii]);
zdialog_stuff(zd,"entmp",dvdmp);
zdialog_run(zd,0,"parent");
zstat = zdialog_wait(zd);
if (zstat != 1) {
zdialog_free(zd);
return 0;
}
zstat = zdialog_fetch(zd,"entdvd",text,60); // get selected DVD/BRD
strncpy0(BJdvd,text,19);
pp2 = strchr(BJdvd,' ');
if (pp2) *pp2 = 0;
zdialog_fetch(zd,"entmp",text,39); // DVD/BRD mount point
strncpy0(dvdmp,text,99);
strTrim(dvdmp);
dvdmpcc = strlen(dvdmp);
if (dvdmpcc && (dvdmp[dvdmpcc-1] == '/')) // remove trailing /
dvdmp[dvdmpcc--] = 0;
txwidget_append2(mLog,0," DVD/BRD and mount point: %s %s \n",BJdvd,dvdmp);
if (Fgui) txwidget_append2(mLog,0," ready \n");
zdialog_free(zd);
return 0;
}
// set label for subsequent DVD/BRD backup via growisofs
int setDVDlabel(ch *menu)
{
ch *pp;
if (*dvdlabel) txwidget_append2(mLog,0," old DVD/BRD label: %s \n",dvdlabel);
else strcpy(dvdlabel,"dkopp");
pp = zdialog_text(mWin,"set new DVD/BRD label",dvdlabel);
if (blank_null(pp)) pp = "dkopp";
strncpy0(dvdlabel,pp,31);
txwidget_append2(mLog,0," new DVD/BRD label: %s \n",dvdlabel);
return 1;
}
// Mount DVD/BRD with message feedback to window.
int mountDVD(ch *menu) // menu mount function
{
int err, reset;
ch buff[400], mbuff[200];
ch *pp, *pp1;
FILE *fid;
STATB dstat;
if (dvdmtd) {
err = stat(dvdmp,&dstat);
if ((! err) && (dvdtime == dstat.st_ctime)) return 0; // medium unchanged, do nothing
}
dvdmtd = 0; // set DVD/BRD not mounted
dvdtime = -1;
strcpy(mediumDT,"unknown");
*mediumDT = 0;
err = reset = 0;
vFilesReset(); // reset DVD/BRD files
fid = popen("cat /etc/mtab","r"); // get mounted devices
if (! fid) goto trymount;
while (true)
{
pp = fgets_trim(buff,400,fid);
if (! pp) break;
pp1 = substring(pp,' ',1); // get /dev/xxx
if (! pp1 || ! strmatch(pp1,BJdvd)) continue; // not my DVD/BRD
pp1 = substring(pp,' ',2); // get mount point
if (! pp1) continue;
repl_1str(pp1,dvdmp,100,"\\040"," "); // replace "\040" with " "
dvdmpcc = strlen(dvdmp);
txwidget_append2(mLog,0," already mounted: %s %s \n",BJdvd,dvdmp);
dvdmtd = 1;
}
pclose(fid);
if (dvdmtd) goto showpoop;
trymount:
mkdir(dvdmp,0755); // create default mount point
snprintf(mbuff,200,"mount -t iso9660 %s %s 2>&1",BJdvd,dvdmp); // mount the DVD/BRD
err = do_shell("mount",mbuff);
if (! err) {
dvdmtd = 1;
goto showpoop;
}
zmessageACK(mWin,"mount DVD/BRD and wait for completion");
while (true)
{
fid = popen("cat /etc/mtab","r"); // get mounted disk info
if (! fid) goto mount;
while (true)
{
pp = fgets_trim(buff,400,fid);
if (! pp) break;
pp1 = substring(pp,' ',1); // get /dev/xxx
if (! pp1 || ! strmatch(pp1,BJdvd)) continue; // not my DVD/BRD
pp1 = substring(pp,' ',2); // get mount point
if (! pp1) continue;
repl_1str(pp1,dvdmp,100,"\\040"," "); // replace "\040" with " "
dvdmpcc = strlen(dvdmp);
txwidget_append2(mLog,0," %d %d mounted \n",BJdvd,dvdmp);
dvdmtd = 1;
}
pclose(fid);
if (dvdmtd) goto showpoop; // mounted OK
mount:
snprintf(mbuff,200,"mount -t iso9660 %s %s 2>&1",BJdvd,dvdmp); // mount the DVD/BRD
err = do_shell("mount",mbuff);
if (! err) {
dvdmtd = 1;
goto showpoop;
}
txwidget_append2(mLog,0," waiting for mount ... \n");
for (int ii = 0; ii < 5; ii++) // 5 secs between "wait" messages
{
if (checkKillPause()) { // killed by user
commFail++;
return 1;
}
zsleep(1);
}
}
showpoop:
dvdtime = dstat.st_ctime; // set DVD/BRD ID = mod time
snprintf(buff,99,"blkid -c /dev/null -s LABEL -o value %s",BJdvd); // get DVD/BRD label
fid = popen(buff,"r");
if (fid) {
pp = fgets_trim(mbuff,99,fid,1);
if (pp) strncpy0(dvdlabel,pp,31);
pclose(fid);
}
strcpy(mbuff,dvdmp);
strcat(mbuff,V_DATETIME); // get last usage date/time if poss.
fid = fopen(mbuff,"r");
if (fid) {
pp = fgets_trim(mbuff,99,fid,1);
if (pp) strncpy0(mediumDT,pp,15);
fclose(fid);
}
txwidget_append2(mLog,0," DVD/BRD label: %s last dkopp: %s \n",dvdlabel,mediumDT);
commFail = 0;
return 0;
}
// unmount DVD/BRD
int unmountDVD(ch *menu)
{
ch command[100];
vFilesReset();
dvdmtd = 0;
dvdtime = -1;
snprintf(command,100,"umount %s 2>&1",dvdmp); // use mount point
do_shell("umount",command);
if (Fgui) txwidget_append2(mLog,0," ready \n");
commFail = 0; // ignore unmount error
return 0;
}
// eject DVD/BRD with message feedback to window
// not all computers support programmatic eject
int ejectDVD(ch *menu)
{
ch command[60];
vFilesReset();
dvdmtd = 0;
dvdtime = -1;
sprintf(command,"eject %s 2>&1",BJdvd);
do_shell("eject",command);
if (Fgui) txwidget_append2(mLog,0," ready \n");
commFail = 0; // ignore eject error
return 0;
}
// wait for DVD/BRD and reset hardware (get over lockups after growisofs)
int resetDVD(ch *menu)
{
if (*subprocName) { // try to kill running job
signalProc(subprocName,"resume");
signalProc(subprocName,"kill");
sleep(1);
}
ejectDVD(0); // the only way I know to reset
sleep(1); // a hung-up DVD/BRD drive
if (Fgui) txwidget_append2(mLog,0," ready \n");
return 0;
}
// Erase DVD/BRD medium by filling it with zeros
int eraseDVD(ch *menu)
{
ch command[200];
int nstat;
nstat = zmessageYN(mWin,"Erase DVD/BRD. This will take some time. \n Continue?");
if (! nstat) goto erase_exit;
vFilesReset(); // reset DVD/BRD file data
sprintf(command,"growisofs -Z %s=/dev/zero %s 2>&1",BJdvd,gforce);
do_shell("growisofs", command); // do growisofs, echo outputs
erase_exit:
if (Fgui) txwidget_append2(mLog,0," ready \n");
return 0;
}
// Format DVD/BRD (2-4 minutes)
int formatDVD(ch *menu)
{
ch command[60];
int nstat;
nstat = zmessageYN(mWin,"Format DVD/BRD. This will take 2-4 minutes. \n Continue?");
if (! nstat) goto format_exit;
vFilesReset(); // reset DVD/BRD file data
sprintf(command,"dvd+rw-format -force %s 2>&1",BJdvd);
do_shell("dvd+rw-format", command);
format_exit:
if (Fgui) txwidget_append2(mLog,0," ready \n");
return 0;
}
// Display help/about or help/user guide
int helpFunc(ch *menu)
{
if (strmatch(menu,"about")) zabout(mWin); // 7.7
if (strmatch(menu,"user guide")) showz_docfile(mWin,"userguide",0);
return 0;
}
// construct file-chooser dialog box
// note: Fdialog unnecessary: this dialog called from other dialogs
int fc_dialog(ch *dirk)
{
GtkWidget *vbox;
fc_dialogbox = gtk_dialog_new_with_buttons("choose files",
GTK_WINDOW(mWin), GTK_DIALOG_MODAL, "hidden",100,
"include",101, "exclude",102, "done",103, null);
gtk_window_set_default_size(GTK_WINDOW(fc_dialogbox),600,500);
G_SIGNAL(fc_dialogbox,"response",fc_response,0);
fc_widget = gtk_file_chooser_widget_new(GTK_FILE_CHOOSER_ACTION_OPEN);
vbox = gtk_dialog_get_content_area(GTK_DIALOG(fc_dialogbox));
gtk_box_pack_end(GTK_BOX(vbox),fc_widget,1,1,0);
gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(fc_widget),dirk);
gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(fc_widget),1);
gtk_file_chooser_set_show_hidden(GTK_FILE_CHOOSER(fc_widget),0);
gtk_widget_show_all(fc_dialogbox);
return 0;
}
// file-chooser dialog handler (file selection, OK, Cancel, Kill)
int fc_response(GtkDialog *dwin, int arg, void *data)
{
GSList *flist = 0;
ch *file1, *file2, *ppf;
int ii, err, hide;
STATB filestat;
if (arg == 103 || arg == -4) // done, cancel
{
gtk_widget_destroy(GTK_WIDGET(dwin));
return 0;
}
if (arg == 100) // hidden
{
hide = gtk_file_chooser_get_show_hidden(GTK_FILE_CHOOSER(fc_widget));
hide = 1 - hide;
gtk_file_chooser_set_show_hidden(GTK_FILE_CHOOSER(fc_widget),hide);
}
if (arg == 101 || arg == 102) // include, exclude
{
flist = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(fc_widget));
for (ii = 0; ; ii++) // process selected files
{
file1 = (ch *) g_slist_nth_data(flist,ii);
if (! file1) break;
file2 = zstrdup(file1,"file2",2); // extra space for wildcard
g_free(file1);
err = stat(file2,&filestat);
if (err) txwidget_append2(mLog,0," *** error: %s file: %s \n",
strerror(errno),kleenex(file2));
if (S_ISDIR(filestat.st_mode)) strcat(file2,"/*"); // if directory, append wildcard
ppf = file2;
if (strmatchN(ppf,dvdmp,dvdmpcc)) ppf += dvdmpcc; // omit DVD/BRD mount point
if (arg == 101) txwidget_append2(editwidget,0,"include %s""\n",ppf);
if (arg == 102) txwidget_append2(editwidget,0,"exclude %s""\n",ppf);
zfree(file2);
}
}
gtk_file_chooser_unselect_all(GTK_FILE_CHOOSER(fc_widget));
g_slist_free(flist);
return 0;
}
// backup helper function
// set nominal backup date/time
// write date/time and updated medium use count to temp file
int writeDT()
{
time_t dt1;
struct tm dt2; // year/month/day/hour/min/sec
FILE *fid;
dt1 = time(0);
dt2 = *localtime(&dt1);
snprintf(backupDT,15,"%4d%02d%02d-%02d%02d",dt2.tm_year+1900, // yyyymmdd-hhmm
dt2.tm_mon+1, dt2.tm_mday, dt2.tm_hour, dt2.tm_min);
strcpy(mediumDT,backupDT);
fid = fopen(TFdatetime,"w");
if (! fid) {
txwidget_append2(mLog,0," *** cannot open /tmp scratch file \n");
commFail++;
return 0;
}
fprintf(fid,"%s \n",mediumDT); // write date/time and medium count
fclose(fid);
return 0;
}
// backup helper function
// save all file and directory owner and permission data to temp file
int save_filepoop() // all files, not just directories
{
int ii, cc, err;
FILE *fid;
ch file[XFCC], dirk[XFCC], pdirk[XFCC], *pp;
STATB dstat;
fid = fopen(TFfilepoop,"w");
if (! fid) {
txwidget_append2(mLog,0," *** cannot open /tmp scratch file \n");
commFail++;
return 0;
}
*pdirk = 0; // no prior
for (ii = 0; ii < Dnf; ii++)
{
strcpy(dirk,Drec[ii].file); // next file on disk
pp = dirk;
while (true)
{
pp = strchr(pp+1,'/'); // next (last) directory level
if (! pp) break;
cc = pp - dirk + 1; // cc incl. '/'
if (strncmp(dirk,pdirk,cc) == 0) continue; // matches prior, skip
*pp = 0; // terminate this directory level
err = stat(dirk,&dstat); // get owner and permissions
if (err) {
txwidget_append2(mLog,0," *** error: %s file: %s \n",
strerror(errno),kleenex(dirk));
break;
}
dstat.st_mode = dstat.st_mode & 0777;
fprintf(fid,"%4d:%4d %3o %s\n", // output uid:gid permissions directory
dstat.st_uid, dstat.st_gid, dstat.st_mode, dirk); // (octal)
*pp = '/'; // restore '/'
}
strcpy(pdirk,dirk); // prior = this directory
strcpy(file,Drec[ii].file); // disk file, again
err = stat(file,&dstat); // get owner and permissions
if (err) {
txwidget_append2(mLog,0," *** error: %s file: %s \n",
strerror(errno),kleenex(file));
continue;
}
dstat.st_mode = dstat.st_mode & 0777;
fprintf(fid,"%4d:%4d %3o %s\n", // output uid:gid permissions file
dstat.st_uid, dstat.st_gid, dstat.st_mode, file); // (octal)
}
fclose(fid);
return 0;
}
// restore helper function
// restore original owner and permissions for restored files and directories
int restore_filepoop()
{
FILE *fid;
int cc1, cc2, ccf, nn, ii, err;
int uid, gid, perms;
ch file1[XFCC], file2[XFCC];
ch poopfile[100];
txwidget_append2(mLog,0,"\n restore directory owner and permissions \n");
txwidget_append2(mLog,0," for directories anchored at: %s \n",RJto);
cc1 = strlen(RJfrom); // from: /home/xxx/.../
cc2 = strlen(RJto); // to: /home/yyy/.../
strcpy(poopfile,dvdmp); // DVD/BRD file with owner & permissions
strcat(poopfile,V_FILEPOOP);
fid = fopen(poopfile,"r");
if (! fid) {
txwidget_append2(mLog,0," *** cannot open DVD/BRD file: %s \n",poopfile);
return 0;
}
ii = 0;
while (true)
{
nn = fscanf(fid,"%d:%d %o %[^\n]",&uid,&gid,&perms,file1); // uid, gid, permissions, file
if (nn == EOF) break; // (nnn:nnn) (octal)
if (nn != 4) continue;
ccf = strlen(file1); // match directories too
if (ccf < cc1) continue;
while (ii < Rnf)
{
nn = strncmp(Rrec[ii].file,file1,ccf); // file in restored file list?
if (nn >= 0) break; // (logic depends on sorted lists)
ii++;
}
if (ii == Rnf) break;
if (nn > 0) continue; // no
strcpy(file2,RJto); // copy-to location
strcpy(file2 + cc2, file1 + cc1); // + org. file, less copy-from part
txwidget_append2(mLog,0," owner: %4d:%4d permissions: %3o file: %s \n",
uid, gid, perms, kleenex(file2));
err = chown(file2,uid,gid);
if (err) txwidget_append2(mLog,0," *** error: %s \n",strerror(errno));
err = chmod(file2,perms);
if (err) txwidget_append2(mLog,0," *** error: %s \n",strerror(errno));
}
fclose(fid);
return 0;
}
// create backup history file after successful backup
int createBackupHist()
{
int ii, err;
FILE *fid;
ch backupfile[200];
ch disp;
snprintf(backupfile,199,"%s/dkopp-hist-%s-%s", // create history file name:
homedir,backupDT,dvdlabel); // dkopp-hist-yyyymmdd-hhmm-dvdlabel
txwidget_append2(mLog,1,"\n""create history file: %s \n",backupfile);
fid = fopen(backupfile,"w");
if (! fid) {
txwidget_append2(mLog,0," *** cannot open dkopp-hist file \n");
return 0;
}
fprintf(fid,"%s (%s backup) \n\n",backupfile,mbmode);
for (ii = 0; ii < BJnx; ii++) // output include/exclude recs
fprintf(fid," %s \n",BJinex[ii]);
fprintf(fid,"\n");
if (strmatch(mbmode,"full"))
for (ii = 0; ii < Dnf; ii++) // output all files for backup
fprintf(fid,"%s\n",Drec[ii].file);
else {
for (ii = 0; ii < Dnf; ii++) { // output new and modified files
disp = Drec[ii].disp;
if ((disp == 'n') || (disp == 'm'))
fprintf(fid,"%s\n",Drec[ii].file);
}
}
err = fclose(fid);
if (err) txwidget_append2(mLog,0," *** dkopp-hist file error %s \n",strerror(errno));
return 0;
}
// parse an include/exclude filespec statement
// return: 0=comment 1=OK 2=parse-error 3=fspec-error
int inexParse(ch * rec, ch *& rtype, ch *& fspec)
{
ch *pp1, *pp2;
int ii;
rtype = fspec = 0;
if (rec[0] == '#') return 0; // comment recs.
if (strlen(rec) < 3) return 0;
strTrim(rec);
ii = 0;
while ((rec[ii] == ' ') && (ii < 30)) ii++; // find 1st non-blank
if (rec[ii] == 0) return 0;
if (ii == 30) return 0; // blank record
rtype = rec + ii; // include/exclude
while ((rec[ii] > ' ') && (ii < 30)) ii++; // look for next blank or null
if (ii == 30) return 2;
if (rec[ii] == ' ') { rec[ii] = 0; ii++; } // end of rtype
if (strlen(rtype) > 7) return 2;
while ((rec[ii] == ' ') && (ii < 30)) ii++; // find next non-blank
if (ii == 30) return 2;
fspec = rec + ii; // filespec (wildcards)
if (strlen(fspec) < 4) return 3;
if (strlen(fspec) > XFCC-100) return 3;
if (strmatch(rtype,"exclude")) return 1; // exclude, done
if (! strmatch(rtype,"include")) return 2; // must be include
if (fspec[0] != '/') return 3; // must have at least /topdirk/
pp1 = strchr(fspec+1,'/');
if (!pp1) return 3;
if (pp1-fspec < 2) return 3;
pp2 = strchr(fspec+1,'*'); // any wildcards must be later
if (pp2 && (pp2 < pp1)) return 3;
pp2 = strchr(fspec+1,'%');
if (pp2 && (pp2 < pp1)) return 3;
return 1; // include + legit. fspec
}
// list backup job data and validate as much as practical
int BJvalidate(ch *menu)
{
int ii, err, nerr = 0;
int year, mon, day;
struct tm tm_date, *tm_date2;
STATB dstat;
txwidget_append2(mLog,1,"\n""Validate backup job data \n");
BJval = 0;
if (! BJnx) {
txwidget_append2(mLog,0," *** no job data present \n");
commFail++;
return 0;
}
txwidget_append2(mLog,0," DVD/BRD device: %s \n",BJdvd);
err = stat(BJdvd,&dstat);
if (err || ! S_ISBLK(dstat.st_mode)) {
txwidget_append2(mLog,0," *** DVD/BRD device is apparently invalid \n");
nerr++;
}
txwidget_append2(mLog,0," backup %s \n",BJbmode);
if (! strmatchV(BJbmode,"full","incremental","accumulate",null)) {
txwidget_append2(mLog,0," *** backup mode not full/incremental/accumulate \n");
nerr++;
}
txwidget_append2(mLog,0," verify %s \n",BJvmode);
if (! strmatchV(BJvmode,"full","incremental","thorough",null)) {
txwidget_append2(mLog,0," *** verify mode not full/incremental/thorough \n");
nerr++;
}
txwidget_append2(mLog,0," file date from: %s \n",BJdatefrom); // file age limit
err = 0;
ii = sscanf(BJdatefrom,"%d.%d.%d",&year,&mon,&day);
if (ii != 3) err = 1;
tm_date.tm_year = year - 1900;
tm_date.tm_mon = mon - 1;
tm_date.tm_mday = day;
tm_date.tm_hour = tm_date.tm_min = tm_date.tm_sec = 0;
tm_date.tm_isdst = -1;
BJtdate = mktime(&tm_date);
tm_date2 = localtime(&BJtdate);
if (tm_date2->tm_year - year + 1900 != 0) err = 3;
if (tm_date2->tm_year + 1900 < 1970) err = 4; // < 1970 disallowed
if (tm_date2->tm_mon - mon + 1 != 0) err = 5;
if (tm_date2->tm_mday - day != 0) err = 6;
if (err) {
txwidget_append2(mLog,0," *** date must be > 1970.01.01 \n");
nerr++;
BJtdate = 0;
}
nerr += nxValidate(BJinex,BJnx); // validate include/exclude recs
txwidget_append2(mLog,0," *** %d errors \n",nerr);
if (nerr) commFail++;
else BJval = 1;
return 0;
}
// validate restore job data
int RJvalidate()
{
int cc, nerr = 0;
ch rdirk[XFCC];
DIR *pdirk;
if (RJval) return 1;
txwidget_append2(mLog,0,"\n Validate restore job data \n");
if (! RJnx) {
txwidget_append2(mLog,0," *** no job data present \n");
return 0;
}
txwidget_append2(mLog,0," copy-from: %s \n",RJfrom);
strcpy(rdirk,dvdmp); // validate copy-from location
strcat(rdirk,RJfrom); // /media/dvd/home/...
pdirk = opendir(rdirk);
if (! pdirk) {
txwidget_append2(mLog,0," *** invalid copy-from location \n");
nerr++;
}
else closedir(pdirk);
cc = strlen(RJfrom); // insure '/' at end
if (RJfrom[cc-1] != '/') strcat(RJfrom,"/");
txwidget_append2(mLog,0," copy-to: %s \n",RJto);
pdirk = opendir(RJto); // validate copy-to location
if (! pdirk) {
txwidget_append2(mLog,0," *** invalid copy-to location \n");
nerr++;
}
else closedir(pdirk);
cc = strlen(RJto); // insure '/' at end
if (RJto[cc-1] != '/') strcat(RJto,"/");
nerr += nxValidate(RJinex,RJnx); // validate include/exclude recs
txwidget_append2(mLog,0," %d errors \n",nerr);
if (! nerr) RJval = 1;
else RJval = 0;
return RJval;
}
// list and validate a set of include/exclude recs
int nxValidate(ch **inexrecs, int nrecs)
{
ch *rtype, *fspec, nxrec[XFCC];
int ii, nstat, errs = 0;
for (ii = 0; ii < nrecs; ii++) // process include/exclude recs
{
strcpy(nxrec,inexrecs[ii]);
txwidget_append2(mLog,0," %s \n",nxrec); // output
nstat = inexParse(nxrec,rtype,fspec); // parse
if (nstat == 0) continue; // comment
if (nstat == 1) continue; // OK
if (nstat == 2) {
txwidget_append2(mLog,0," *** cannot parse \n"); // cannot parse
errs++;
continue;
}
if (nstat == 3) { // bad filespec
txwidget_append2(mLog,0," *** invalid filespec \n");
errs++;
continue;
}
}
return errs;
}
// get all files for backup as specified by include/exclude records
// save in Drec[] array
int dGetFiles()
{
ch *fsp;
ch *rtype, *fspec, bjrec[XFCC], *mbytes;
int ftf, cc, nstat, wstat, err;
int ii, jj, nfiles, nexc;
double nbytes;
STATB filestat;
if (! BJval) { // validate job data if needed
dFilesReset();
BJvalidate(0);
if (! BJval) return 0; // job has errors
}
if (Dnf > 0) return 0; // avoid refresh
txwidget_append2(mLog,1,"\n""finding all files for backup \n");
for (ii = 0; ii < BJnx; ii++) // process include/exclude recs
{
BJfiles[ii] = 0; // initz. include/exclude rec stats
BJbytes[ii] = 0.0;
BJdvdno[ii] = 0;
strcpy(bjrec,BJinex[ii]); // next record
nstat = inexParse(bjrec,rtype,fspec); // parse
if (nstat == 0) continue; // comment
if (strmatch(rtype,"include")) // include filespec
{
ftf = 1;
while (1)
{
fsp = SearchWild(fspec,ftf); // find matching files
if (! fsp) break;
cc = strlen(fsp);
if (cc > XFCC-100) zappcrash("file cc: %d, %99s...",cc,fsp);
Drec[Dnf].file = zstrdup(fsp,"Drec");
err = lstat(fsp,&filestat); // check accessibility
if (err == 0) {
if (! S_ISREG(filestat.st_mode) && // include files and symlinks
! S_ISLNK(filestat.st_mode)) continue; // omit pipes, devices ...
}
Drec[Dnf].stat = err; // save file status
Drec[Dnf].inclx = ii; // save pointer to include rec
Drec[Dnf].size = filestat.st_size; // save file size
Drec[Dnf].mtime = filestat.st_mtime // save last mod time
+ filestat.st_mtim.tv_nsec * nano; // (nanosec resolution)
if (err) Drec[Dnf].size = Drec[Dnf].mtime = 0;
Drec[Dnf].disp = Drec[Dnf].ivf = 0; // initialize
BJfiles[ii]++; // count included files and bytes
BJbytes[ii] += Drec[Dnf].size;
if (++Dnf == maxfs) {
txwidget_append2(mLog,0," *** exceeded %d files \n",maxfs);
goto errret;
}
}
}
if (strmatch(rtype,"exclude")) // exclude filespec
{
for (jj = 0; jj < Dnf; jj++) // check included files (SO FAR)
{
if (! Drec[jj].file) continue;
wstat = MatchWild(fspec,Drec[jj].file);
if (wstat != 0) continue;
BJfiles[ii]--; // un-count excluded file and bytes
BJbytes[ii] -= Drec[jj].size;
zfree(Drec[jj].file); // clear file data in array
Drec[jj].file = 0;
Drec[jj].stat = 0; // bugfix
}
}
} // end of include/exclude recs
for (ii = 0; ii < Dnf; ii++) // list and remove error files
{ // (after excluded files removed)
if (! Drec[ii].file) continue;
if (Drec[ii].stat)
{
err = stat(Drec[ii].file,&filestat);
txwidget_append2(mLog,0," *** %s omit: %s \n",strerror(errno),kleenex(Drec[ii].file));
jj = Drec[ii].inclx;
BJfiles[jj]--; // un-count file and bytes
BJbytes[jj] -= Drec[ii].size;
zfree(Drec[ii].file);
Drec[ii].file = 0;
}
}
for (nexc = ii = 0; ii < Dnf; ii++)
{
if (! Drec[ii].file) continue;
if (Drec[ii].mtime < BJtdate) // omit files excluded by date
{ // or older than 1970
jj = Drec[ii].inclx;
BJfiles[jj]--; // un-count file and bytes
BJbytes[jj] -= Drec[ii].size;
zfree(Drec[ii].file);
Drec[ii].file = 0;
nexc++;
}
}
if (nexc) txwidget_append2(mLog,0," %d files excluded by selection date \n",nexc);
ii = jj = 0; // repack file arrays after deletions
while (ii < Dnf)
{
if (Drec[ii].file == 0) ii++;
else {
if (ii > jj) {
if (Drec[jj].file) zfree(Drec[jj].file);
Drec[jj] = Drec[ii];
Drec[ii].file = 0;
}
ii++;
jj++;
}
}
Dnf = jj; // final file count
Dbytes = 0.0;
for (ii = 0; ii < Dnf; ii++) Dbytes += Drec[ii].size; // compute total bytes from files
nfiles = 0;
nbytes = 0.0;
for (ii = 0; ii < BJnx; ii++) // compute total files and bytes
{ // from include/exclude recs
nfiles += BJfiles[ii];
nbytes += BJbytes[ii];
}
mbytes = formatKBMB(nbytes,3);
txwidget_append2(mLog,0," files for backup: %d %s \n",nfiles,mbytes);
if ((nfiles != Dnf) || (Dbytes != nbytes)) { // must match
txwidget_append2(mLog,0," *** bug: nfiles: %d Dnf: %d \n",nfiles,Dnf);
txwidget_append2(mLog,0," *** bug: nbytes: %.0f Dbytes: %.0f \n",nbytes,Dbytes);
goto errret;
}
SortFileList((ch *) Drec,sizeof(dfrec),Dnf,'A'); // sort Drec[Dnf] by Drec[].file
for (ii = 1; ii < Dnf; ii++) // look for duplicate files
if (strmatch(Drec[ii].file,Drec[ii-1].file)) {
txwidget_append2(mLog,0," *** duplicate file: %s \n",kleenex(Drec[ii].file));
BJval = 0; // invalidate backup job
}
if (! BJval) goto errret;
return 0;
errret:
dFilesReset();
BJval = 0;
return 0;
}
// get existing files on DVD/BRD medium, save in Vrec[] array
// (the shell command "find ... -type f" does not find the
// files "deleted" via copy from /dev/null in growisofs)
int vGetFiles()
{
int cc, gcc, err;
ch command[200], *pp;
ch fspec1[XFCC], fspec2[XFCC];
FILE *fid;
STATB filestat;
if (Vnf) return 0; // avoid refresh
mountDVD(0); // mount with retries
if (! dvdmtd) return 0; // cannot mount
txwidget_append2(mLog,1,"\n""find all DVD/BRD files \n");
snprintf(command,200,"find \"%s\" -type f -or -type l >%s", // get regular files and symlinks
dvdmp,TFdvdfiles); // add quotes in case of blanks
txwidget_append2(mLog,0," %s \n",command);
err = zshell(0,command); // list all DVD/BRD files to temp file
if (err) {
txwidget_append2(mLog,0," *** find command failed: %s \n",strerror(err));
commFail++;
return 0;
}
fid = fopen(TFdvdfiles,"r"); // read file list
if (! fid) {
txwidget_append2(mLog,0," *** cannot open /tmp scratch file \n");
commFail++;
return 0;
}
gcc = strlen(V_DKOPPDIRK);
while (1)
{
pp = fgets_trim(fspec1,XFCC-2,fid); // get next file
if (! pp) break; // eof
cc = strlen(pp); // absurdly long file name
if (cc > XFCC-100) {
txwidget_append2(mLog,0," *** absurd file skipped: %200s (etc.) \n",kleenex(pp));
continue;
}
if (strmatchN(fspec1+dvdmpcc,V_DKOPPDIRK,gcc)) continue; // ignore special dkopp files
repl_1str(fspec1,fspec2,XFCC,"\\=","="); // replace "\=" with "=" in file name
Vrec[Vnf].file = zstrdup(fspec2 + dvdmpcc,"Vrec"); // save without DVD/BRD mount point
err = lstat(fspec1,&filestat); // check accessibility
Vrec[Vnf].stat = err; // save file status
Vrec[Vnf].size = filestat.st_size; // save file size
Vrec[Vnf].mtime = filestat.st_mtime // save last mod time
+ filestat.st_mtim.tv_nsec * nano;
if (err) Vrec[Vnf].size = Vrec[Vnf].mtime = 0;
Vnf++;
if (Vnf == maxfs) zappcrash("exceed %d files",maxfs);
}
fclose (fid);
SortFileList((ch *) Vrec,sizeof(vfrec),Vnf,'A'); // sort Vrec[Vnf] by Vrec[].file
txwidget_append2(mLog,0," DVD/BRD files: %d \n",Vnf);
return 0;
}
// get all DVD/BRD restore files specified by include/exclude records
int rGetFiles()
{
ch *rtype, *fspec, fspecx[XFCC], rjrec[XFCC];
int ii, jj, cc, nstat, wstat, ninc, nexc;
if (! RJval) return 0;
rFilesReset(); // clear restore files
vGetFiles(); // get DVD/BRD files
if (! Vnf) return 0;
txwidget_append2(mLog,0,"\n""find all DVD/BRD files to restore \n");
for (ii = 0; ii < RJnx; ii++) // process include/exclude recs
{
strcpy(rjrec,RJinex[ii]); // next record
txwidget_append2(mLog,0," %s \n",rjrec); // output
nstat = inexParse(rjrec,rtype,fspec); // parse
if (nstat == 0) continue; // comment
repl_1str(fspec,fspecx,XFCC,"\\=","="); // replace "\=" with "=" in file name
if (strmatch(rtype,"include")) // include filespec
{
ninc = 0; // count of included files
for (jj = 0; jj < Vnf; jj++) // screen all DVD/BRD files
{
wstat = MatchWild(fspecx,Vrec[jj].file);
if (wstat != 0) continue;
Rrec[Rnf].file = zstrdup(Vrec[jj].file,"Rrec"); // add matching files
Rnf++; ninc++;
if (Rnf == maxfs) zappcrash("exceed %d files",maxfs);
}
txwidget_append2(mLog,0," %d files added \n",ninc);
}
if (strmatch(rtype,"exclude")) // exclude filespec
{
nexc = 0;
for (jj = 0; jj < Rnf; jj++) // check included files (SO FAR)
{
if (! Rrec[jj].file) continue;
wstat = MatchWild(fspecx,Rrec[jj].file);
if (wstat != 0) continue;
zfree(Rrec[jj].file); // remove matching files
Rrec[jj].file = 0;
nexc++;
}
txwidget_append2(mLog,0," %d files removed \n",nexc);
}
}
ii = jj = 0; // repack after deletions
while (ii < Rnf)
{
if (Rrec[ii].file == 0) ii++;
else
{
if (ii > jj)
{
if (Rrec[jj].file) zfree(Rrec[jj].file);
Rrec[jj].file = Rrec[ii].file;
Rrec[ii].file = 0;
}
ii++;
jj++;
}
}
Rnf = jj;
txwidget_append2(mLog,0," total file count: %d \n",Rnf);
cc = strlen(RJfrom); // copy from: /home/.../
for (ii = 0; ii < Rnf; ii++) // get selected DVD/BRD files to restore
{
if (! strmatchN(Rrec[ii].file,RJfrom,cc)) {
txwidget_append2(mLog,0," *** not under copy-from; %s \n",Rrec[ii].file);
RJval = 0; // mark restore job invalid
continue;
}
}
SortFileList((ch *) Rrec,sizeof(rfrec),Rnf,'A'); // sort Rrec[Rnf] by Rrec[].file
return 0;
}
// helper function for backups and reports
//
// compare disk and DVD/BRD files, set dispositions in Drec[] and Vrec[] arrays
// n new on disk, not on DVD/BRD
// d deleted on DVD/BRD, not on disk
// m modified on both, but not equal
// u unchanged on both, and equal
int setFileDisps()
{
int dii, vii, comp;
ch disp;
double diff;
dii = vii = 0;
nnew = nmod = nunc = ndel = 0;
Mbytes = 0.0; // total bytes, new and modified files
while ((dii < Dnf) || (vii < Vnf)) // scan disk and DVD/BRD files parallel
{
if ((dii < Dnf) && (vii == Vnf)) comp = -1; // disk file after last DVD/BRD file
else if ((dii == Dnf) && (vii < Vnf)) comp = +1; // DVD/BRD file after last disk file
else comp = strcmp(Drec[dii].file,Vrec[vii].file); // compare disk and DVD/BRD file names
if (comp < 0)
{ // unmatched disk file: new on disk
Drec[dii].disp = 'n';
Mbytes += Drec[dii].size; // accumulate Mbytes
nnew++; // count new files
dii++;
}
else if (comp > 0)
{ // unmatched DVD/BRD file: not on disk
Vrec[vii].disp = 'd';
ndel++; // count deleted files
vii++;
}
else if (comp == 0) // file present on disk and DVD/BRD
{
disp = 'u'; // set initially unchanged
if (Drec[dii].stat != Vrec[vii].stat) disp = 'm'; // fstat() statuses are different
diff = fabs(Drec[dii].size - Vrec[vii].size);
if (diff > 0) disp = 'm'; // sizes are different
diff = fabs(Drec[dii].mtime - Vrec[vii].mtime);
if (diff > modtimetolr) disp = 'm'; // mod times are different
Drec[dii].disp = Vrec[vii].disp = disp;
if (disp == 'u') nunc++; // count unchanged files
if (disp == 'm') nmod++; // count modified files
if (disp == 'm') Mbytes += Drec[dii].size; // and accumulate Mbytes
dii++;
vii++;
}
}
Mfiles = nnew + nmod + ndel;
return 0;
}
// Sort file list in memory (disk files, DVD/BRD files, restore files).
// Sort ascii sequence, or sort subdirectories in a directory before files.
int SortFileList(ch * recs, int RL, int NR, ch sort)
{
HeapSortUcomp fcompA, fcompD; // compare filespecs functions
if (sort == 'A') HeapSort(recs,RL,NR,fcompA); // normal ascii compare
if (sort == 'D') HeapSort(recs,RL,NR,fcompD); // compare directories first
return 0;
}
int fcompA(ch *rec1, ch *rec2) // ascii comparison
{
dfrec *r1 = (dfrec *) rec1;
dfrec *r2 = (dfrec *) rec2;
return strcmp(r1->file,r2->file);
}
int fcompD(ch *rec1, ch *rec2) // special compare filenames
{ // subdirectories in a directory are
dfrec *r1 = (dfrec *) rec1; // less than files in the directory
dfrec *r2 = (dfrec *) rec2;
return filecomp(r1->file,r2->file);
}
int filecomp(ch *file1, ch *file2) // special compare filenames
{ // subdirectories compare before files
ch *pp1, *pp10, *pp2, *pp20;
ch slash = '/';
int cc1, cc2, comp;
pp1 = file1; // first directory level or file
pp2 = file2;
while (true)
{
pp10 = strchr(pp1,slash); // find next slash
pp20 = strchr(pp2,slash);
if (pp10 && pp20) { // both are directories
cc1 = pp10 - pp1;
cc2 = pp20 - pp2;
if (cc1 < cc2) comp = strncmp(pp1,pp2,cc1); // compare the directories
else comp = strncmp(pp1,pp2,cc2);
if (comp) return comp;
else if (cc1 != cc2) return (cc1 - cc2);
pp1 = pp10 + 1; // equal, check next level
pp2 = pp20 + 1;
continue;
}
if (pp10 && ! pp20) return -1; // only one is a directory,
if (pp20 && ! pp10) return 1; // the directory is first
comp = strcmp(pp1,pp2); // both are files, compare
return comp;
}
}
// reset all backup job data and free allocated memory
int BJreset()
{
for (int ii = 0; ii < BJnx; ii++) zfree(BJinex[ii]);
BJnx = 0;
*BJbmode = *BJvmode = 0;
BJval = BJmod = 0;
dFilesReset(); // reset dependent disk file data
return 0;
}
// reset all restore job data and free allocated memory
int RJreset()
{
for (int ii = 0; ii < RJnx; ii++) zfree(RJinex[ii]);
RJnx = 0;
RJval = 0;
rFilesReset(); // reset dependent disk file data
return 0;
}
// reset all file data and free allocated memory
int dFilesReset()
{ // disk files data
for (int ii = 0; ii < Dnf; ii++)
{
zfree(Drec[ii].file);
Drec[ii].file = 0;
}
Dnf = 0;
Dbytes = Dbytes2 = Mbytes = 0.0;
return 0;
}
int vFilesReset()
{ // DVD/BRD files data
for (int ii = 0; ii < Vnf; ii++)
{
zfree(Vrec[ii].file);
Vrec[ii].file = 0;
}
Vnf = 0;
Vbytes = Mbytes = 0.0;
return 0;
}
int rFilesReset()
{ // DVD/BRD restore files data
for (int ii = 0; ii < Rnf; ii++)
{
zfree(Rrec[ii].file);
Rrec[ii].file = 0;
}
Rnf = 0;
return 0;
}
// helper function to copy a file from DVD/BRD to disk
ch *copyfile(ch *vfile, ch *dfile)
{
ch vfile1[XFCC], vfilex[XFCC];
int fid1, fid2, err, rcc;
ch *pp, buff[vrcc];
ch *errmess;
STATB fstat;
struct timeval ftimes[2];
strcpy(vfile1,dvdmp); // prepend DVD/BRD mount point
strcat(vfile1,vfile);
repl_1str(vfile1,vfilex,XFCC,"=","\\="); // replace "=" with "\=" in DVD/BRD file
fid1 = open(vfilex,O_RDONLY+O_NOATIME+O_LARGEFILE); // open input file
if (fid1 == -1) return strerror(errno);
fid2 = open(dfile,O_WRONLY+O_CREAT+O_TRUNC+O_LARGEFILE,0700); // open output file
if (fid2 == -1 && errno == ENOENT) {
pp = dfile;
while (true) { // create one or more directories,
pp = strchr(pp+1,'/'); // one level at a time
if (! pp) break;
*pp = 0;
err = mkdir(dfile,0700);
if (! err) chmod(dfile,0700);
*pp = '/';
if (err) {
if (errno == EEXIST) continue;
errmess = strerror(errno);
close(fid1);
return errmess;
}
}
fid2 = open(dfile,O_WRONLY+O_CREAT+O_TRUNC+O_LARGEFILE,0700); // open output file again
}
if (fid2 == -1) {
errmess = strerror(errno);
close(fid1);
return errmess;
}
while (true)
{
rcc = read(fid1,buff,vrcc); // read huge blocks
if (rcc == 0) break;
if (rcc == -1) {
errmess = strerror(errno);
close(fid1);
close(fid2);
return errmess;
}
rcc = write(fid2,buff,rcc); // write blocks
if (rcc == -1) {
errmess = strerror(errno);
close(fid1);
close(fid2);
return errmess;
}
}
close(fid1);
close(fid2);
stat(vfilex,&fstat); // get input file attributes
ftimes[0].tv_sec = fstat.st_atime; // conv. access times to microsecs
ftimes[0].tv_usec = fstat.st_atim.tv_nsec / 1000;
ftimes[1].tv_sec = fstat.st_mtime;
ftimes[1].tv_usec = fstat.st_mtim.tv_nsec / 1000;
chmod(dfile,fstat.st_mode); // set output file attributes
err = chown(dfile,fstat.st_uid,fstat.st_gid); // (if supported by file system)
if (err) printf("error: %s \n",strerror(err));
utimes(dfile,ftimes);
return 0;
}
// Verify helper function
// Verify that file on BRD/DVD medium is readable, return its length.
// Optionally compare backup file to current file, byte for byte.
// return: 0: OK 1: open error 2: read error 3: compare fail
ch *checkFile(ch *dfile, int compf, double &tcc)
{
int vfid = 0, dfid = 0;
int err, vcc, dcc, cmperr = 0;
int open_flags = O_RDONLY+O_NOATIME+O_LARGEFILE; // O_DIRECT not allowed for DVD/BRD
ch vfile[XFCC], *vbuff = 0, *dbuff = 0;
ch *errmess = 0;
double dtime, vtime;
STATB filestat;
tcc = 0.0;
strcpy(vfile,dvdmp); // prepend mount point
vcc = XFCC - dvdmpcc;
repl_1str(dfile,vfile+dvdmpcc,vcc,"=","\\="); // replace "=" with "\=" in DVD/BRD file
err = lstat(vfile,&filestat); // check symlinks but do not follow
if (err) return strerror(errno);
if (S_ISLNK(filestat.st_mode)) return 0;
if (compf) goto comparefiles;
vfid = open(vfile,open_flags); // open DVD/BRD file
if (vfid == -1) return strerror(errno);
err = posix_memalign((void**) &vbuff,512,vrcc); // get 512-aligned buffer
if (err) zappcrash("memory allocation failure");
while (1) // read DVD/BRD file
{
vcc = read(vfid,vbuff,vrcc);
if (vcc == 0) break;
if (vcc == -1) { errmess = strerror(errno); break; }
tcc += vcc; // accumulate length
if (checkKillPause()) break;
zmainloop(10); // keep gtk alive
}
goto cleanup;
comparefiles:
vfid = open(vfile,open_flags); // open DVD/BRD file
if (vfid == -1) return strerror(errno);
dfid = open(dfile,open_flags); // open corresp. disk file
if (dfid == -1) { errmess = strerror(errno); goto cleanup; }
err = posix_memalign((void**) &vbuff,512,vrcc); // get 512-aligned buffers
if (err) zappcrash("memory allocation failure");
err = posix_memalign((void**) &dbuff,512,vrcc);
if (err) zappcrash("memory allocation failure");
while (1)
{
vcc = read(vfid,vbuff,vrcc); // read two files
if (vcc == -1) { errmess = strerror(errno); goto cleanup; }
dcc = read(dfid,dbuff,vrcc);
if (dcc == -1) { errmess = strerror(errno); goto cleanup; }
if (vcc != dcc) cmperr++; // compare buffers
if (memcmp(vbuff,dbuff,vcc)) cmperr++;
tcc += vcc; // accumulate length
if (vcc == 0) break;
if (dcc == 0) break;
if (checkKillPause()) break;
zmainloop(5); // keep gtk alive
}
if (vcc != dcc) cmperr++;
if (cmperr) { // compare error
stat(dfile,&filestat);
dtime = filestat.st_mtime + filestat.st_mtim.tv_nsec * nano; // file modified since snapshot?
stat(vfile,&filestat);
vtime = filestat.st_mtime + filestat.st_mtim.tv_nsec * nano;
if (fabs(dtime-vtime) < modtimetolr) errmess = "compare error"; // no, a real compare error
}
cleanup:
if (vfid) close(vfid); // close files
if (dfid) close(dfid);
if (vbuff) free(vbuff); // free buffers
if (dbuff) free(dbuff);
return errmess;
}
// track current /directory/.../filename.ext on logging window
// display directory and file names in overlay mode (no scrolling)
int track_filespec(ch *filespec)
{
int cc;
ch pdirk[300], pfile[300], *pp;
if (! Fgui) {
printf(" %s \n",filespec);
return 0;
}
pp = (ch *) strrchr(filespec+1,'/'); // parse directory/filename
if (pp) {
cc = pp - filespec + 2;
strncpy0(pdirk,filespec,cc);
strncpy0(pfile,pp+1,299);
}
else {
strcpy(pdirk," ");
strncpy0(pfile,filespec,299);
}
txwidget_replace(mLog,0,-2," %s \n",kleenex(pdirk)); // output /directory
txwidget_replace(mLog,0,-1," %s \n",kleenex(pfile)); // filename
return 0;
}
// log error message and scroll down to prevent it from being overlaid
int track_filespec_err(ch *filespec, ch *errmess)
{
if (Fgui) {
txwidget_replace(mLog,0,-2," *** %s %s \n",errmess,kleenex(filespec));
txwidget_append2(mLog,0," \n");
}
else printf(" %s %s \n",errmess,filespec);
return 0;
}
// remove special characters in exotic file names causing havoc in output formatting
ch *kleenex(ch *name)
{
static ch name2[1000];
strncpy0(name2,name,999);
for (int ii = 0; name2[ii]; ii++)
if (name2[ii] >= 8 && name2[ii] <= 13) // screen out formatting chars.
name2[ii] = '?';
return name2;
}
// do shell command (subprocess) and echo outputs to log window
// returns command status: 0 = OK, +N = error
// compensate for growisofs failure not always indicated as bad status
// depends on growisofs output being in english
int do_shell(ch *pname, ch *command)
{
int scroll, pscroll;
int err = 0, gerr = 0;
ch *pp, buff[1000];
ch *errmess;
FILE *fid;
static pollfd *pfd = 0; // 8.1
if (! pfd)
pfd = (pollfd *) zmalloc(sizeof(pollfd),"pollfd");
txwidget_append2(mLog,1,"\n""shell: %s \n",command);
strncpy0(subprocName,pname,20); // set subprocess name, active
if (strmatch(pname,"growisofs")) track_growisofs_files(0); // initialize progress tracker
scroll = pscroll = 1;
txwidget_scroll(mLog,-1); // scroll to last line
fid = popen(command,"r");
if (! fid) {
err = errno;
goto checkstat;
}
while (true)
{
pfd->fd = fid->_fileno;
pfd->events = POLLIN + POLLHUP;
pfd->revents = 0;
while (! poll(pfd,1,10000)) { // wait for data or 10 seconds 8.1
zmainloop();
txwidget_append2(mLog,0," waiting ...\n");
}
pp = fgets_trim(buff,1000,fid);
if (! pp) break;
pscroll = scroll;
scroll = 1;
if (strmatch(pname,"growisofs")) { // growisofs output
if (track_growisofs_files(buff)) scroll = 0; // conv. % done into file position
if (strstr(buff,"genisoimage:")) gerr = 999; // trap errors not reported in
if (strstr(buff,"mkisofs:")) gerr = 998; // flakey growisofs status
if (strstr(buff,"failed")) gerr = 997;
if (strstr(buff,"media is not recognized")) gerr = 996;
}
if (strstr(buff,"formatting")) scroll = 0; // dvd+rw-format output
if (scroll) { // output to next line
txwidget_append2(mLog,0," %s: %s \n",pname,kleenex(buff));
zsleep(0.002); // throttle output a little
}
else if (Fgui) { // supress output in batch mode
if (pscroll) txwidget_append2(mLog,0,"\n"); // transition from scroll to overlay
txwidget_replace(mLog,0,-1," %s: %s \n",pname,kleenex(buff)); // output, overlay prior output
}
if (killFlag) {
txwidget_append2(mLog,0,"*** %s \n","pkill %s",subprocName);
err = zshell(0,buff);
}
zmainloop();
while (pauseFlag) zmainsleep(0.2);
}
pclose(fid);
checkstat:
errmess = 0;
if (err) errmess = strerror(err);
if (strmatch(pname,"growisofs")) {
err = gerr;
if (err == 997) err = 0; // growisofs likes 997
if (err) errmess = "growisofs failure";
}
if (err) txwidget_append2(mLog,0," %s status: %d %s \n", pname, err, errmess);
else txwidget_append2(mLog,0," %s status: OK \n",pname);
*subprocName = 0; // no longer active
if (err) commFail++;
return err;
}
// Convert "% done" from growisofs into corresponding position in list of files being copied.
// Incremental backups start with % done = (initial DVD/BRD space used) / (final DVD/BRD space used).
int track_growisofs_files(ch * buff)
{
static double bbytes, gpct0, gpct;
static int dii, dii2, err;
static ch *dfile;
if (! buff) { // initialize
dii = 0;
dii2 = -1;
bbytes = 0;
dfile = (ch *) "";
return 0;
}
if (! strstr(buff,"% done")) return 0; // not a % done record
err = convSD(buff,gpct,0.0,100.0); // get % done, 0-100
if (err > 1) return 0;
if (strmatch(mbmode,"full")) { // full backup, possibly > 1 DVD/BRD
while (dii < Dnf) {
if (bbytes/Dbytes2 > gpct/100) break; // exit if enough bytes
bbytes += Drec[dii].size; // sum files
dii2 = dii;
dii++;
}
}
else { // incremental backup
if (bbytes == 0) gpct0 = gpct; // establish base % done
while (dii < Dnf) {
if (bbytes/Mbytes > (gpct-gpct0)/(100-gpct0)) break; // exit if enough bytes
if (Drec[dii].disp == 'n' || Drec[dii].disp == 'm') {
bbytes += Drec[dii].size; // sum new and modified files
dii2 = dii;
}
dii++;
}
}
if (dii2 > -1) dfile = Drec[dii2].file; // file corresponding to byte count
snprintf(buff,999,"%6.1f %c %s",gpct,'%',dfile); // nn.n % /directory/.../filename
return 1;
}
// supply unused zdialog callback function
void KBevent(GdkEventKey *event)
{ return; }
|