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
|
/*
* ratDbase.c --
*
* TkRat software and its included text is Copyright 1996,1997,1998
* by Martin Forssn
*
* Postilion software and its included text and images
* Copyright (C) 1998 Nic Bernstein
*
* The full text of the legal notices is contained in the files called
* COPYING and COPYRIGHT.TkRat, included with this distribution.
*
* 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 2
* of the License, or (at your option) any later version.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/*
* This file contains support for a database of messages. This file
* uses version 3 of the database. The format of the database is as
* follows:
*
* The database directory contains the following entries:
* index The main database index. This file consists
* of a number of entries separated by newline and
* each entry has the following format:
* To
* From
* Cc
* Subject
* Date (UNIX time_t as a string)
* Keywords (SPACE separated list)
* Size
* Status
* Expiration time (UNIX time_t as a string)
* Expiration event (*)
* Filename
* Expiration event is one of none, remove, incoming,
* backup and custom. Custom is followed by the custom
* command.
* index.info This file contains information about the database.
* It contains two integers. The first is the version
* (in this case 3) and the second is the number of
* entries in the indexfile.
* index.changes This file contains a log of changes made to the
* index file. This log is only kept if the multiple
* agents has opened the database. In this file each
* entry is one line. There are addition entries;
* 'a OFFSET' where OFFSET is the position in the
* index file where this entry starts. There are also
* deletion entries; 'd INDEX'. Finally there are
* the status changes; they are of the form:
* 's INDEX STATUS' where STATUS is the new status of
* the specified message.
* lock If this file exists the database is locked and
* no other agent may do anything with it. It should
* contain a string identifying the agent owning the
* lock.
* rlock.* Each agent that opens the database should construct
* a file with the following name: rlock.HOST:PID .
* This file should be touched at least once every
* hour.
* dbase/ This is a directory which holds a number of
* directories which in turn holds the actual messages.
*
* In the dbase directory messages are stored as recipient-name/number.
* Where the last number taken in a recipient-name directory is
* contained in a .seq file found in said directory.
*/
#include "ratFolder.h"
#define DBASE_VERSION 3 /* Version of the database format */
#define EXTRA_ENTRIES 100 /* How many extra entries we should allocate
* room for when allocating the entryPtr
* array */
#define RLOCK_TIMEOUT 2*60*60 /* Actual timeout time for rlock files */
#define UPDATE_INTERVAL 40*60 /* Time between updates to the rlock file */
static int isRead = 0; /* 0 means that the database hasn't been
* read yet */
static int numRead; /* The number of entries in the entryPtr list */
static int numAlloc; /* The number of entries that will fit
* into the entryPtr list */
static RatDbEntry *entryPtr; /* The list of entries in the database */
static char *dbDir = 0; /* Full path to the database directory */
static char *ident = 0; /* String which is used to identify us.
* It is of the form hostname:pid */
static int changeSize = 0; /* The number of bytes in the index.changes
* file that we have read and incorporated
* into our memory resident database */
static int numChanges = 0; /* Number of changes in the changes file */
/*
* This structure is used while checking the dbase
*/
typedef struct {
int fileSize; /* The actual size of the file */
int index; /* Index in the list that handles this entry */
RatDbEntry entry; /* The actual entry in the index file */
} RatDbItem;
/*
* Forward declarations for procedures defined in this file:
*/
static int Read(Tcl_Interp *interp);
static void Lock(Tcl_Interp *interp);
static void Unlock(Tcl_Interp *interp);
static int Sync(Tcl_Interp *interp);
static void Update(ClientData clientData);
static int IsRlocked(char *ignore);
static void RatDbBuildList(Tcl_Interp *interp, Tcl_DString *dsPtr,
char *prefix, char *dir, Tcl_HashTable *tablePtr, int fix);
static int NoLFPrint(FILE *fp, char *s);
/*
*----------------------------------------------------------------------
*
* Read --
*
* Reads the database from disk into memory.
*
* Results:
* The return value is normally TCL_OK; if something goes wrong
* TCL_ERROR is returned and an error message will be left in
* interp->result.
*
* Side effects:
* The internal list of entries is allocated and initalized. An
* rlock-file is created to indicate that we have the database
* open. To keep this lock up to date the Update() procedure must
* be called at least once every hour. When the agent won't access
* the database anymore it must be closed with a call to RatDbClose().
*
*
*----------------------------------------------------------------------
*/
static int
Read(Tcl_Interp *interp)
{
char buf[1024]; /* Scratch area */
int size; /* Size of indexfile */
struct stat sbuf; /* Buffer for stat() calls */
int fhIndex; /* File handle for index-file */
int fhReadlock; /* File handle for read lock file */
FILE *fpIndexinfo; /* File pointer for index.info file */
int i, j; /* Loop variables */
int version; /* Version read */
char *cPtr; /* Running pointer */
/*
* First make sure we know where the database should reside and which
* identifier we should use.
*/
if (0 == dbDir) {
Tcl_DString ds; /* Scratch area */
char *value; /* Value of "dbase_dir" option */
value = Tcl_GetVar2(interp, "option", "dbase_dir", TCL_GLOBAL_ONLY);
if (NULL == value) {
Tcl_SetResult(interp, "option(dbase_dir) has no value", TCL_STATIC);
return TCL_ERROR;
}
dbDir = cpystr(Tcl_TildeSubst(interp, value, &ds));
Tcl_DStringFree(&ds);
}
if (0 == ident) {
gethostname(buf, sizeof(buf));
ident = (char*)ckalloc(strlen(buf)+16);
sprintf(ident, "%s:%d", buf, (int)getpid());
}
/*
* Check if the database actually exists. If it doesn't then we
* must create the needed directories and files.
*/
sprintf(buf, "%s/index", dbDir);
if ( (0 != stat(dbDir, &sbuf) && ENOENT == errno)
|| (0 != stat(buf, &sbuf) && ENOENT == errno)) {
if (0 != mkdir(dbDir, DIRMODE) && EEXIST != errno) {
Tcl_AppendResult(interp, "error creating directory \"", dbDir,
"\": ", Tcl_PosixError(interp), (char *) NULL);
return TCL_ERROR;
}
sprintf(buf, "%s/dbase", dbDir);
if (0 != mkdir(buf, DIRMODE) && EEXIST != errno) {
Tcl_AppendResult(interp, "error creating directory \"", buf,
"\": ", Tcl_PosixError(interp), (char *) NULL);
return TCL_ERROR;
}
Lock(interp);
sprintf(buf, "%s/index", dbDir);
if (0 > (fhIndex = open(buf, O_CREAT|O_WRONLY, FILEMODE))
|| 0 != close(fhIndex)) {
Unlock(interp);
Tcl_AppendResult(interp, "error creating file \"", buf,
"\": ", Tcl_PosixError(interp), (char *) NULL);
return TCL_ERROR;
}
sprintf(buf, "%s/index.info", dbDir);
if (0 == (fpIndexinfo = fopen(buf, "w"))) {
Unlock(interp);
Tcl_AppendResult(interp, "error creating file \"", buf,
"\": ", Tcl_PosixError(interp), (char *) NULL);
return TCL_ERROR;
}
if (0 > fprintf(fpIndexinfo, "%d 0\n", DBASE_VERSION)) {
Unlock(interp);
Tcl_AppendResult(interp, "error writing to file \"", buf, "\"",
(char *) NULL);
return TCL_ERROR;
}
if (0 != fclose(fpIndexinfo)) {
Unlock(interp);
Tcl_AppendResult(interp, "error closing file \"", buf,
"\": ", Tcl_PosixError(interp), (char *) NULL);
return TCL_ERROR;
}
} else {
Lock(interp);
}
/*
* Create rlock file.
*/
sprintf(buf, "%s/rlock.%s", dbDir, ident);
if (0 > (fhReadlock = open(buf, O_CREAT|O_WRONLY, FILEMODE))
|| 0 != close(fhReadlock)) {
Unlock(interp);
Tcl_AppendResult(interp, "error creating file \"", buf,
"\": ", Tcl_PosixError(interp), (char *) NULL);
return TCL_ERROR;
}
(void)Tk_CreateTimerHandler(UPDATE_INTERVAL*1000, Update, (ClientData)NULL);
/*
* Read the index.info file
*/
sprintf(buf, "%s/index.info", dbDir);
if (0 == (fpIndexinfo = fopen(buf, "r"))) {
Tcl_AppendResult(interp, "error opening file (for reading)\"", buf,
"\": ", Tcl_PosixError(interp), (char *) NULL);
goto error;
}
if ( 2 != fscanf(fpIndexinfo, "%d %d", &version, &numRead)) {
Tcl_SetResult(interp, "index.info file corrupt", TCL_STATIC);
fclose(fpIndexinfo);
goto error;
}
fclose(fpIndexinfo);
/*
* Check if this is the current version of the database. If not
* complain!
*/
if (version != DBASE_VERSION) {
sprintf(buf, "wrong version of database got %d expected %d", version,
DBASE_VERSION);
Tcl_SetResult(interp, buf, TCL_VOLATILE);
goto error;
}
/*
* Read the indexfile and build internal data structures
*/
sprintf(buf, "%s/index", dbDir);
if (0 != stat(buf, &sbuf)) {
Tcl_AppendResult(interp, "error stating file \"", buf,
"\": ", Tcl_PosixError(interp), (char *) NULL);
goto error;
}
size = sbuf.st_size;
if (size > 0) {
char *indexPtr; /* Pointer to read version of the indexfile */
/*
* We should not free this value since we never close the database.
* Purify will note it as a leak, but we will have lots of
* pointers into this data.
*/
if ( 0 == (indexPtr = (char*)ckalloc(size))) {
Tcl_SetResult(interp, "failed to allocate memory for index",
TCL_STATIC);
goto error;
}
fhIndex = open(buf, O_RDONLY);
if (size != read(fhIndex, indexPtr, size)) {
Tcl_SetResult(interp, "error reading index", TCL_STATIC);
close(fhIndex);
goto error;
}
close(fhIndex);
entryPtr = (RatDbEntry*)ckalloc((numRead+EXTRA_ENTRIES) *
sizeof(RatDbEntry));
numAlloc = numRead+EXTRA_ENTRIES;
cPtr = indexPtr;
for (i=0; i<numRead; i++) {
for (j=0; j<RATDBETYPE_END; j++) {
entryPtr[i].content[j] = cPtr;
while (cPtr <= &indexPtr[size] && *cPtr != '\n') {
cPtr++;
}
if (cPtr > &indexPtr[size]) {
Tcl_SetResult(interp, "error in index-file", TCL_STATIC);
ckfree(indexPtr);
goto error;
}
*cPtr++ = '\0';
}
/*
* This is a KLUDGE to work around a bug which existed for a
* short time /MaF 960218
*/
if ('+' == entryPtr[i].content[EX_TYPE][0]) {
char buf[64];
sprintf(buf, "%d", atoi(entryPtr[i].content[EX_TYPE])*24*60*60+
atoi(entryPtr[i].content[DATE]));
entryPtr[i].content[EX_TIME] = cpystr(buf);
entryPtr[i].content[EX_TYPE] = "backup";
}
}
} else {
entryPtr = (RatDbEntry*)ckalloc(EXTRA_ENTRIES * sizeof(RatDbEntry));
numAlloc = EXTRA_ENTRIES;
}
isRead = 1;
/*
* Let's get up to date with any changes made.
*/
Sync(interp);
Unlock(interp);
return TCL_OK;
error:
sprintf(buf, "%s/rlock.%s", dbDir, ident);
unlink(buf);
Unlock(interp);
return TCL_ERROR;
}
/*
*----------------------------------------------------------------------
*
* Lock --
*
* Exculsively lock the database. A lock like this must be obtained
* before you do anything at all with the database. And while a
* lock like this is active nobody but the owner of the lock may
* do anything to the database. The lock obtained must be released
* with a call to the Unlock() procedure.
*
* Results:
* None.
*
* Side effects:
* Creates a lockfile in the database directory.
*
*----------------------------------------------------------------------
*/
static void
Lock(Tcl_Interp *interp)
{
char buf[1024]; /* Scratch area */
int fhLock; /* Lockfile filehandle */
int msgPost = 0; /* True of message has been posted */
do {
sprintf(buf, "%s/lock", dbDir);
if (-1 == (fhLock = open(buf, O_CREAT|O_EXCL|O_WRONLY, FILEMODE))) {
if (EEXIST == errno) {
if (!msgPost) {
sprintf(buf, "RatLog 2 $t(waiting_dbase_lock) explicit");
Tcl_Eval(interp, buf);
msgPost = 1;
}
sleep(2);
} else {
sprintf(buf, "Failed to create lockfile \"%s\": %s",
buf, Tcl_PosixError(interp));
RatLog(interp, RAT_FATAL, buf, 0);
exit(1);
}
}
} while (-1 == fhLock);
write(fhLock, ident, strlen(ident));
close(fhLock);
if (msgPost) {
sprintf(buf, "RatClearLog %s; update idletasks", interp->result);
Tcl_Eval(interp, buf);
}
}
/*
*----------------------------------------------------------------------
*
* Unlock --
*
* Releases a lock previously obtained with the Lock() procedure.
*
* Results:
* None.
*
* Side effects:
* The lockfile is removed.
*
*----------------------------------------------------------------------
*/
static void
Unlock(Tcl_Interp *interp)
{
char buf[1024]; /* Scratch area */
sprintf(buf, "%s/lock", dbDir);
if (0 != unlink(buf)) {
char errMsg[1024];
sprintf(errMsg, "Failed to unlink lockfile \"%s\": %s", buf,
Tcl_PosixError(interp));
RatLog(interp, RAT_FATAL, errMsg, 0);
exit(1);
}
}
/*
*----------------------------------------------------------------------
*
* Sync --
*
* Make sure that the database in memory is consistent with the
* master on disk. This is accomplished by reading the index.changes
* file. This call assumes that we have an exclusive lock on the
* database.
*
* Results:
* The return value is normally TCL_OK; if something goes wrong
* TCL_ERROR is returned and an error message will be left in
* interp->result.
*
* Side effects:
* The internal list of entries may be affected as well as the
* index-file on disk.
*
*----------------------------------------------------------------------
*/
static int
Sync(Tcl_Interp *interp)
{
char buf[1024]; /* Scratch area */
struct stat sbuf; /* Buffer for stat() calls */
FILE *fpChanges; /* index.changes file pointer */
FILE *fpIndex; /* Index file pointer */
char command; /* Command in changes file */
int cmdArg; /* Argument to command */
int i; /* Loop counter */
int doWrite = 0; /* 1 if we should write the changes to the disk */
int needRewrite = 0; /* 1 if we need to rewrite the indexfile */
int numEntries = 0; /* How many entries there actually are */
char *indexBuf = NULL; /* New part of index file */
int indexOffset = 0; /* Offset of new part of index file */
char *cPtr;
sprintf(buf, "%s/index.changes", dbDir);
if (0 > stat(buf, &sbuf)) {
if (ENOENT != errno) {
Tcl_AppendResult(interp, "error stating file \"", buf,
"\": ", Tcl_PosixError(interp), (char *) NULL);
return TCL_ERROR;
}
return TCL_OK;
}
if (changeSize >= sbuf.st_size) {
return TCL_OK;
}
/*
* Read and perform changes mentioned in index.changes file
*/
if (0 == (fpChanges = fopen(buf, "r"))) {
Tcl_AppendResult(interp, "error opening file (for reading) \"",
buf, "\": ", Tcl_PosixError(interp), (char *) NULL);
return TCL_ERROR;
}
if (0 != fseek(fpChanges, changeSize, SEEK_SET)) {
Tcl_AppendResult(interp, "error seeking in file \"", buf,
"\": ", Tcl_PosixError(interp), (char *) NULL);
fclose(fpChanges);
return TCL_ERROR;
}
changeSize = sbuf.st_size;
while(1) {
if (2 != fscanf(fpChanges, "%c %d ", &command, &cmdArg) ||
('d'!=command && 'a'!=command && 's'!=command)
|| ('s' == command &&
buf != fgets(buf, sizeof(buf), fpChanges))) {
if (feof(fpChanges)) {
break;
}
Tcl_SetResult(interp, "syntax error in changes file",
TCL_STATIC);
fclose(fpChanges);
return TCL_ERROR;
}
numChanges++;
if ('d' == command) {
if (cmdArg < 0 || cmdArg >= numRead) {
continue;
}
needRewrite = 1;
entryPtr[cmdArg].content[FROM] = NULL;
} else if ('s' == command) {
if (cmdArg < 0 || cmdArg >= numRead) {
continue;
}
needRewrite = 1;
buf[strlen(buf)-1] = '\0';
if ( (int) strlen(buf) <=
(int) strlen(entryPtr[cmdArg].content[STATUS])){
strcpy(entryPtr[cmdArg].content[STATUS], buf);
} else {
/*
* This code may leak the memory occupied by the
* previous status string. I believe this loss can
* be lived with (it should be quite rare).
*/
entryPtr[cmdArg].content[STATUS] =
(char *) ckalloc(strlen(buf)+1);
strcpy(entryPtr[cmdArg].content[STATUS], buf);
}
} else {
if (numRead == numAlloc) {
numAlloc += EXTRA_ENTRIES;
entryPtr = (RatDbEntry*)REALLOC(entryPtr,
numAlloc*sizeof(RatDbEntry));
}
if (!indexBuf) {
sprintf(buf, "%s/index", dbDir);
if (NULL == (fpIndex = fopen(buf, "r"))) {
Tcl_AppendResult(interp,
"error opening file (for reading) \"", buf,
"\": ", Tcl_PosixError(interp), (char *) NULL);
fclose(fpChanges);
return TCL_ERROR;
}
if (0 != fseek(fpIndex, cmdArg, SEEK_SET)) {
Tcl_AppendResult(interp, "error seeking in file \"",buf,
"\": ", Tcl_PosixError(interp), (char *) NULL);
fclose(fpIndex);
fclose(fpChanges);
return TCL_ERROR;
}
(void)fstat(fileno(fpIndex), &sbuf);
/*
* Purify will probably report this as a leak, but that
* is not true.
*/
indexBuf = (char*)malloc(sbuf.st_size - cmdArg + 1);
fread(indexBuf, sbuf.st_size - cmdArg, 1, fpIndex);
fclose(fpIndex);
indexBuf[sbuf.st_size - cmdArg] = '\0';
indexOffset = cmdArg;
}
cPtr = indexBuf + (cmdArg - indexOffset);
for (i=0; i<RATDBETYPE_END; i++) {
entryPtr[numRead].content[i] = cPtr;
for (; *cPtr != '\n' && *cPtr; cPtr++);
if (!*cPtr) {
Tcl_AppendResult(interp, "error reading \"",buf,
"\": ", Tcl_PosixError(interp),(char*)NULL);
fclose(fpChanges);
return TCL_ERROR;
}
*cPtr++ = '\0';
}
numRead++;
}
}
fclose(fpChanges);
/*
* If the number of changes is at least 20 and we are the only agent
* which have the database open we write the changes into the main
* index.
*/
/*
* Check that we are the only agent who has the database open.
* But only if have enough changes to write.
*/
if (20 <= numChanges) {
char myLock[1024]; /* Name of my rlock file */
sprintf(myLock, "rlock.%s", ident);
if (IsRlocked(myLock)) {
doWrite = 0;
} else {
doWrite = 1;
}
}
if (doWrite) {
FILE *fpIndexinfo; /* Filepointer to index info */
if (needRewrite) {
char oldIndex[1024]; /* Name of old index file */
char newIndex[1024]; /* Name of new index file */
FILE *fpNewIndex; /* Filepointer to new index */
int j; /* Loop variable */
sprintf(oldIndex, "%s/index", dbDir);
sprintf(newIndex, "%s/index.new", dbDir);
if (0 == (fpNewIndex = fopen(newIndex, "w"))) {
Tcl_AppendResult(interp, "error creating file \"", newIndex,
"\": ", Tcl_PosixError(interp), (char *) NULL);
return TCL_ERROR;
}
if (0 == (fpIndex = fopen(oldIndex, "r"))) {
Tcl_AppendResult(interp, "error opening file (for reading)\"",
oldIndex,"\": ", Tcl_PosixError(interp), (char *) NULL);
(void)fclose(fpNewIndex);
return TCL_ERROR;
}
for (i=0 ; i < numRead; i++) {
if (0 != entryPtr[i].content[FROM]) {
numEntries++;
for (j=0; j<RATDBETYPE_END; j++) {
if (0 > fprintf(fpNewIndex, "%s\n",
entryPtr[i].content[j])) {
Tcl_AppendResult(interp,"error writing to file \"",
newIndex, "\"", (char *) NULL);
(void)fclose(fpNewIndex);
(void)unlink(newIndex);
(void)fclose(fpIndex);
return TCL_ERROR;
}
}
} else {
sprintf(buf, "%s/dbase/%s", dbDir,
entryPtr[i].content[FILENAME]);
(void)unlink(buf);
}
}
(void)fclose(fpIndex);
if (0 != fclose(fpNewIndex)) {
Tcl_AppendResult(interp,"error closing file \"", newIndex,
"\": ", Tcl_PosixError(interp), (char *) NULL);
(void)unlink(newIndex);
return TCL_ERROR;
}
if (0 != rename(newIndex, oldIndex)) {
Tcl_AppendResult(interp,"error moving file \"", newIndex,
"\" -> \"", oldIndex, "\": ", Tcl_PosixError(interp),
(char *) NULL);
return TCL_ERROR;
}
} else {
numEntries = numRead;
}
sprintf(buf, "%s/index.info", dbDir);
if (0 == (fpIndexinfo = fopen(buf, "w"))) {
Tcl_AppendResult(interp, "error opening file (for writing)\"",
buf, "\": ", Tcl_PosixError(interp), (char *) NULL);
return TCL_ERROR;
}
if (0 > fprintf(fpIndexinfo, "%d %d\n", DBASE_VERSION, numEntries)) {
Tcl_AppendResult(interp, "error writing to file \"", buf, "\"",
(char *) NULL);
return TCL_ERROR;
}
if (0 > fclose(fpIndexinfo)) {
Tcl_AppendResult(interp, "error closing file \"", buf,
"\": ", Tcl_PosixError(interp), (char *) NULL);
return TCL_ERROR;
}
sprintf(buf, "%s/index.changes", dbDir);
if (0 != unlink(buf)) {
Tcl_AppendResult(interp, "error unlinking file \"", buf,
"\": ", Tcl_PosixError(interp), (char *) NULL);
return TCL_ERROR;
}
changeSize = 0;
numChanges = 0;
}
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* Update --
*
* This routine updates the read-lock we have on the database. This
* must be done at least as often as is specified by the Read
* rotine. Preferably more often. If the database hasn't been read
* yet (or has been closed) this routine does nothing.
*
* Results:
* None.
*
* Side effects:
* The rlock-file will be touched (if the database is open).
*
*----------------------------------------------------------------------
*/
static void
Update(ClientData clientData)
{
char rlockName[1024]; /* Name of rlock file */
if (0 != isRead) {
return;
}
sprintf(rlockName, "%s/rlock.%s", dbDir, ident);
(void)utime(rlockName, (struct utimbuf*) NULL);
(void)Tk_CreateTimerHandler(UPDATE_INTERVAL*1000, Update, (ClientData)NULL);
}
/*
*----------------------------------------------------------------------
*
* IsRlocked --
*
* Checks if any othe rprocess has an read lock on the database.
*
* Results:
* True if any othe rprocess has.
*
* Side effects:
* Stale rlock-files will be removed
*
*----------------------------------------------------------------------
*/
static int
IsRlocked(char *ignore)
{
time_t deadline; /* Lockfiles older than this can be ignored */
struct dirent *direntPtr;
struct stat sbuf;
int result = 0;
DIR *dirPtr;
char buf[1024];
deadline = time((time_t*) NULL) - RLOCK_TIMEOUT;
dirPtr = opendir(dbDir);
while (0 != (direntPtr = readdir(dirPtr))) {
if (!strncmp("rlock.", (char*)direntPtr->d_name, 6)
&& (ignore && strcmp((char*)direntPtr->d_name, ignore))) {
sprintf(buf, "%s/%s", dbDir, (char*)direntPtr->d_name);
(void)stat(buf, &sbuf);
if (deadline < sbuf.st_mtime) {
result = 1;
break;
} else {
unlink(buf);
}
}
}
closedir(dirPtr);
return result;
}
/*
*----------------------------------------------------------------------
*
* RatDbInsert --
*
* This procedure inserts a copy of the message, whose id is passed
* in the mail parameter, into the database. An entry is made in the
* index file. One of the arguments is the expiration date as a string.
* This string is the number of days the message should stay in the
* database until it expires.
*
* The algorithm is to update the index on disk and then let Sync()
* insert the new value into the internal database.
*
* Results:
* The return value is normally TCL_OK; if something goes wrong
* TCL_ERROR is returned and an error message will be left in
* interp->result.
*
* Side effects:
* The internal and external databases are updated.
*
*----------------------------------------------------------------------
*/
int
RatDbInsert(Tcl_Interp *interp, char *to, char *from, char *cc, char *subject,
int date, char *flags, char *keywords, int exDate, char *exType,
char *fromline, char *mail, int length)
{
char fname[1024]; /* filename of new entry */
char buf[1024]; /* Scratch area */
char *dir; /* Message directory */
FILE *indexFP; /* File pointer to index file */
long indexPos; /* Start position in index file */
char *cPtr; /* Misc character pointer */
FILE *seqFP; /* Filepointer to seq file */
int seq; /* sequence number */
FILE *indchaFP; /* File pointer to the index.changes file */
Tcl_Channel dataChannel; /* Data channel */
ADDRESS *adrPtr; /* Address list */
int mode; /* Mode to use when creating files */
int i;
/*
* Get file creation mode
*/
Tcl_GetInt(interp, Tcl_GetVar2(interp, "option", "permissions",
TCL_GLOBAL_ONLY), &mode);
if (0 == isRead) {
if (TCL_OK != Read(interp)) {
return TCL_ERROR;
}
}
Lock(interp);
/*
* Generate the filename we are to use for this entry in the database.
* Create the directory it will be stored in too (if needed).
*/
adrPtr = NULL;
if (to && *to) {
strcpy(buf, to);
rfc822_parse_adrlist(&adrPtr, buf, currentHost);
dir = cpystr(adrPtr->mailbox);
} else {
dir = cpystr(currentMailboxName);
}
mail_free_address(&adrPtr);
sprintf(fname, "%s/", dir);
sprintf(buf, "%s/dbase/%s/.seq", dbDir, dir);
if (NULL == (seqFP = fopen(buf, "r+"))) {
sprintf(buf, "%s/dbase/%s", dbDir, dir);
if (0 != mkdir(buf, DIRMODE) && EEXIST != errno) {
Unlock(interp);
Tcl_AppendResult(interp, "error creating directory \"", buf,
"\": ", Tcl_PosixError(interp), (char *) NULL);
ckfree(dir);
return TCL_ERROR;
}
seq = 0;
sprintf(buf, "%s/dbase/%s/.seq", dbDir, dir);
if (NULL == (seqFP = fopen(buf, "w"))) {
Unlock(interp);
Tcl_AppendResult(interp, "error opening (for writing)\"", buf,
"\": ", Tcl_PosixError(interp), (char *) NULL);
ckfree(dir);
return TCL_ERROR;
}
} else {
if (1 != fscanf(seqFP, "%d", &seq)) {
(void)fclose(seqFP);
Unlock(interp);
Tcl_AppendResult(interp, "error parsing: \"", buf, "\"",
(char*) NULL);
ckfree(dir);
return TCL_ERROR;
}
seq++;
}
ckfree(dir);
rewind(seqFP);
if (0 > fprintf(seqFP, "%d", seq)) {
(void)fclose(seqFP);
Unlock(interp);
Tcl_AppendResult(interp, "error writing to \"", buf,
"\": ", Tcl_PosixError(interp), (char *) NULL);
return TCL_ERROR;
}
if (0 != fclose(seqFP)) {
Unlock(interp);
Tcl_AppendResult(interp, "error closing file \"", buf,
"\": ", Tcl_PosixError(interp), (char *) NULL);
return TCL_ERROR;
}
sprintf(buf, "%d", seq);
cPtr = fname + strlen(fname);
for (i=strlen(buf)-1; i>=0; i--) {
*cPtr++ = buf[i];
}
*cPtr = '\0';
/*
* Open the indexfile and remember where we are
*/
sprintf(buf, "%s/index", dbDir);
if (NULL == (indexFP = fopen(buf, "a"))) {
Unlock(interp);
Tcl_AppendResult(interp, "error opening (for append)\"", buf,
"\": ", Tcl_PosixError(interp), (char *) NULL);
return TCL_ERROR;
}
indexPos = ftell(indexFP);
/*
* Construct the entries... some are simple some others are more
* complicated :-) The order and format of the entries is documented
* at the top of this file.
*/
NoLFPrint(indexFP, to);
NoLFPrint(indexFP, from);
NoLFPrint(indexFP, cc);
NoLFPrint(indexFP, subject);
fprintf(indexFP, "%d\n", date);
NoLFPrint(indexFP, keywords);
fprintf(indexFP, "%d\n", length);
NoLFPrint(indexFP, flags);
fprintf(indexFP, "%ld\n", exDate*24*60*60 + time((time_t*) NULL));
NoLFPrint(indexFP, exType);
if (0 > NoLFPrint(indexFP, fname)) {
goto losing;
}
if (0 != fclose(indexFP)) {
Tcl_AppendResult(interp, "error closing index file :",
Tcl_PosixError(interp), (char *) NULL);
goto losing;
}
/*
* Create the actual entry in the database.
*/
sprintf(buf, "%s/dbase/%s", dbDir, fname);
if (NULL == (dataChannel = Tcl_OpenFileChannel(interp, buf, "w", mode))) {
Tcl_AppendResult(interp, "error creating file \"", buf,
"\": ", Tcl_PosixError(interp), (char *) NULL);
goto losing;
}
Tcl_Write(dataChannel, fromline, strlen(fromline));
Tcl_Write(dataChannel, mail, length);
if (TCL_OK != Tcl_Close(interp, dataChannel)) {
Tcl_AppendResult(interp, "error closing file \"", buf,
"\": ", Tcl_PosixError(interp), (char *) NULL);
goto losing_but_nearly_got_it;
}
/*
* Write an entry to the index.changes file and then update
*/
sprintf(buf, "%s/index.changes", dbDir);
if (NULL == (indchaFP = fopen(buf, "a"))) {
Tcl_AppendResult(interp, "error opening file (for append)\"", buf,
"\": ", Tcl_PosixError(interp), (char *) NULL);
goto losing_but_nearly_got_it;
}
if (0 > fprintf(indchaFP, "a %ld\n", indexPos)) {
Tcl_AppendResult(interp, "error writing to file \"", buf, "\"",
(char *) NULL);
goto losing_but_nearly_got_it;
}
if (0 != fclose(indchaFP)) {
Tcl_AppendResult(interp, "error closing file \"", buf,
"\": ", Tcl_PosixError(interp), (char *) NULL);
goto losing_but_nearly_got_it;
}
Sync(interp);
Unlock(interp);
return TCL_OK;
losing_but_nearly_got_it:
sprintf(buf, "%s/dbase/%s", dbDir, fname);
(void)unlink(buf);
losing:
(void)sprintf(buf, "%s/index", dbDir);
(void)truncate(buf, indexPos);
Unlock(interp);
return TCL_ERROR;
}
/*
*----------------------------------------------------------------------
*
* RatDbSetStatus --
*
* This procedure modifies the status of the given message.
*
* Results:
* The return value is normally TCL_OK; if something goes wrong
* TCL_ERROR is returned and an error message will be left in
* interp->result.
*
* Side effects:
* The internal and external databases are updated.
*
*----------------------------------------------------------------------
*/
int
RatDbSetStatus(Tcl_Interp *interp, int index, char *status)
{
char buf[1024]; /* Name of index.changes file */
FILE *indexFP; /* FIle pointer to index.changes file */
/*
* Check the index for validity.
*/
if (index >= numRead || index < 0) {
Tcl_SetResult(interp, "error: the given index is invalid", TCL_STATIC);
return TCL_ERROR;
}
/*
* Check if we really need to do this
*/
if (!strcmp(status, entryPtr[index].content[STATUS])) {
return TCL_OK;
}
Lock(interp);
sprintf(buf, "%s/index.changes", dbDir);
if (NULL == (indexFP = fopen(buf, "a"))) {
Tcl_AppendResult(interp, "error opening (for append)\"", buf,
"\": ", Tcl_PosixError(interp), (char *) NULL);
Unlock(interp);
return TCL_ERROR;
}
if (0 > fprintf(indexFP, "s %d %s\n", index, status)){
Tcl_AppendResult(interp, "Failed to write to file \"", buf, "\"",
(char*) NULL);
(void)fclose(indexFP);
Unlock(interp);
return TCL_ERROR;
}
if (0 != fclose(indexFP)) {
Tcl_AppendResult(interp, "error closing file \"", buf,
"\": ", Tcl_PosixError(interp), (char *) NULL);
Unlock(interp);
return TCL_ERROR;
}
Sync(interp);
Unlock(interp);
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* RatDbSearch --
*
* Searches the database for entries matching the given expression.
* The search expression is in the following form:
* op exp [exp ...]
* Where op is either "and" or "or". and exp is as follows:
* [not] field value
* Where field is one of "to", "from", "cc", "subject", "keywords"
* and "all". if op is "and" the all following expressions must be
* true but if it is "or" then only one has to be true.
*
* Results:
* The number of items found is returned in numFoundPtr if it is
* non zero a pointer to a list of found ones is put into *foundPtrPtr.
* It is the callers resopnsibility to free this list with a
* call to free(). The rotine normally returns TCL_OK except when
* there is an error. In that case TCL_ERROR is returned and the cause
* may be found in interp->error.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
int
RatDbSearch(Tcl_Interp *interp, char *exp, int *numFoundPtr, int **foundPtrPtr)
{
int or; /* Indicates the operation; 0 = and 1 = or */
int i, j, k; /* Loop counters */
int match;
int numAlloc = 0; /* Number of entries allocated room for */
int expNumWords, argc;
char **expWords, **argv;
int numExp; /* The number of subexpressions in the exp */
char fname[1024]; /* Filename of actual message */
int bodyfd; /* File descriptor to actual message */
char *message = NULL; /* Actual message */
int messageSize = 0; /* Size of message area */
struct stat sbuf; /* Buffer for stat calls */
/*
* The following three lists describes the search expression. Every
* expression has one entry in each list.
*/
int *notPtr;
RatDbEType *fieldPtr;
char **valuePtr;
*numFoundPtr = 0;
*foundPtrPtr = NULL;
/*
* Parse the expression and build the lists.
*/
if (TCL_OK != Tcl_SplitList(interp, exp, &expNumWords, &expWords)) {
return TCL_ERROR;
}
if (!strcmp(*expWords, "and") && !strcmp(*expWords, "or")) {
Tcl_SetResult(interp, "exp must start with \"and\" or \"or\".",
TCL_STATIC);
return TCL_ERROR;
}
notPtr = (int*) ckalloc(sizeof(int) * (expNumWords/2));
fieldPtr = (RatDbEType*) ckalloc(sizeof(RatDbEType) * (expNumWords/2));
valuePtr = (char**) ckalloc(sizeof(char*) * (expNumWords/2));
expNumWords--;
if (!strcmp(*expWords, "or")) {
or = 1;
} else {
or = 0;
}
numExp = 0;
i = 1;
while (i < expNumWords) {
if (!strcmp(expWords[i], "not")) {
notPtr[numExp] = 1;
i++;
} else {
notPtr[numExp] = 0;
}
if (i > expNumWords-1) {
Tcl_SetResult(interp, "Parse error in exp (to few words)",
TCL_STATIC);
goto losing;
}
if (!strcmp(expWords[i], "to")) {
fieldPtr[numExp] = TO;
} else if (!strcmp(expWords[i], "from")) {
fieldPtr[numExp] = FROM;
} else if (!strcmp(expWords[i], "cc")) {
fieldPtr[numExp] = CC;
} else if (!strcmp(expWords[i], "subject")) {
fieldPtr[numExp] = SUBJECT;
} else if (!strcmp(expWords[i], "keywords")) {
fieldPtr[numExp] = KEYWORDS;
} else if (!strcmp(expWords[i], "all")) {
fieldPtr[numExp] = -1;
} else {
Tcl_SetResult(interp, "Parse error in exp (illegal field value)",
TCL_STATIC);
goto losing;
}
i++;
valuePtr[numExp++] = expWords[i++];
}
/*
* Now we are ready to do the searching. First make sure that the
* database is read and synced, then run through it.
*/
if (0 == isRead) {
if (TCL_OK != Read(interp)) {
goto losing;
}
} else {
if (TCL_OK != Sync(interp)) {
goto losing;
}
}
for (i=0; i < numRead; i++) {
if (!entryPtr[i].content[FROM]) { /* Entry deleted */
continue;
}
match = 0;
for(j=0; j < numExp && !(j != 0 && or == match); j++) {
Tcl_SplitList(interp, valuePtr[j], &argc, &argv);
for (k=0; k<argc && !(k != 0 && or == match); k++) {
if (fieldPtr[j] == -1) {
sprintf(fname, "%s/dbase/%s", dbDir,
entryPtr[i].content[FILENAME]);
if (0 > (bodyfd = open(fname, O_RDONLY))) {
Tcl_AppendResult(interp,
"error opening file (for read)\"", fname,
"\": ", Tcl_PosixError(interp), (char*)NULL);
goto losing;
}
if (0 != fstat(bodyfd, &sbuf)) {
Tcl_AppendResult(interp, "error stating file \"", fname,
"\": ", Tcl_PosixError(interp), (char *) NULL);
close(bodyfd);
goto losing;
}
if (messageSize < sbuf.st_size+1) {
ckfree(message);
message = (char*)ckalloc(messageSize = sbuf.st_size+1);
}
read(bodyfd, message, sbuf.st_size);
message[sbuf.st_size] = '\0';
(void)close(bodyfd);
match = RatSearch(argv[k], message);
} else {
match = RatSearch(argv[k],entryPtr[i].content[fieldPtr[j]]);
}
if (1 == notPtr[j]) {
match = match ? 0 : 1;
}
}
free(argv);
}
if (match) {
if (*numFoundPtr >= numAlloc) {
numAlloc += EXTRA_ENTRIES;
*foundPtrPtr = (int*)REALLOC(*foundPtrPtr,numAlloc*sizeof(int));
}
(*foundPtrPtr)[(*numFoundPtr)++] = i;
}
}
ckfree((char*) expWords);
ckfree((char*) notPtr);
ckfree((char*) fieldPtr);
ckfree((char*) valuePtr);
if (messageSize > 0) {
ckfree(message);
}
return TCL_OK;
losing:
ckfree((char*) expWords);
ckfree((char*) notPtr);
ckfree((char*) fieldPtr);
ckfree((char*) valuePtr);
if (messageSize > 0) {
ckfree(message);
}
return TCL_ERROR;
}
/*
*----------------------------------------------------------------------
*
* RatDbGetEntry --
*
* This routine retrieves an entry from the database. The pointer
* returned is ONLY good until the next call to RatDbInsert(),
* RatDbSetStatus(), RatDbSearch(), RatDbDelete().
*
* Results:
* The routine returns a pointer to a RatDbEntry structure which
* should be treated as read only. If the index is invalid or
* points to a deleted entry a null pointer is returned.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
RatDbEntry*
RatDbGetEntry(int index)
{
if (index<0 || index>=numRead || NULL == entryPtr[index].content[FROM]) {
return NULL;
}
return &entryPtr[index];
}
/*
*----------------------------------------------------------------------
*
* RatDbGetMessage --
*
* This routine extracts a copy of a message in the database and
* returns a MESSAGE structure.
*
* Results:
* A pointer to a MESSAGE* structure. It alse fills in the pointer
* to a buffer among the arguments with the address that needs to be
* freed when the message is deleted.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
MESSAGE*
RatDbGetMessage(Tcl_Interp *interp, int index, char **buffer)
{
char fname[1024]; /* Filename of message */
int messfd; /* Message file descriptor */
struct stat sbuf; /* Buffer for stat call (to find out size of message)*/
char *message; /* Pointer to actual message */
/*
* Check the index for validity.
*/
if (index >= numRead || index < 0) {
Tcl_SetResult(interp, "error: the given index is invalid", TCL_STATIC);
return NULL;
}
if (NULL == entryPtr[index].content[FROM]) {
Tcl_SetResult(interp, "error: the message is deleted", TCL_STATIC);
return NULL;
}
Lock(interp);
/*
* Read the message into an array pointed to by 'message'.
*/
sprintf(fname, "%s/dbase/%s", dbDir, entryPtr[index].content[FILENAME]);
if (0 > (messfd = open(fname, O_RDONLY))) {
Unlock(interp);
Tcl_AppendResult(interp, "error opening file (for read)\"",
fname, "\": ", Tcl_PosixError(interp), (char*)NULL);
return NULL;
}
if (0 != fstat(messfd, &sbuf)) {
Unlock(interp);
Tcl_AppendResult(interp, "error stating file \"", fname,
"\": ", Tcl_PosixError(interp), (char *) NULL);
close(messfd);
return NULL;
}
*buffer = message = (char*)ckalloc(sbuf.st_size+1);
read(messfd, message, sbuf.st_size);
message[sbuf.st_size] = '\0';
(void)close(messfd);
Unlock(interp);
return RatParseMsg(interp, message);
}
/*
*----------------------------------------------------------------------
*
* RatDbGetHeaders --
*
* This routine extracts a copy of the headers of a message in
* the database.
*
* Results:
* A pointer to a static area containing the message headers
* is returned.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
char*
RatDbGetHeaders(Tcl_Interp *interp, int index)
{
static char *header; /* Static storage area */
static int headerSize = 0; /* Size of static storage area */
char fname[1024]; /* Filename of message */
char buf[2048]; /* Temporary holding area */
char *hPtr; /* The header to return */
FILE *messFp; /* Message file pointer */
int length = 0; /* Length of header */
/*
* Check the index for validity.
*/
if (index >= numRead || index < 0) {
Tcl_SetResult(interp, "error: the given index is invalid", TCL_STATIC);
return NULL;
}
if (NULL == entryPtr[index].content[FROM]) {
Tcl_SetResult(interp, "error: the message is deleted", TCL_STATIC);
return NULL;
}
Lock(interp);
/*
* Read the message into an array pointed to by 'message'.
*/
sprintf(fname, "%s/dbase/%s", dbDir, entryPtr[index].content[FILENAME]);
if (NULL == (messFp = fopen(fname, "r"))) {
Unlock(interp);
Tcl_AppendResult(interp, "error opening file (for read)\"",
fname, "\": ", Tcl_PosixError(interp), (char*)NULL);
return NULL;
}
while (fgets(buf, sizeof(buf), messFp), !feof(messFp)) {
if ('\n' == buf[0]) {
length = ftell(messFp)-1;
break;
}
if ('\r' == buf[0]) {
length = ftell(messFp)-2;
break;
}
}
if (length == 0) {
fseek(messFp, 0, SEEK_END);
length = ftell(messFp);
}
if (length+1 > headerSize) {
ckfree(header);
headerSize = length+1;
header = (char*)ckalloc(headerSize);
}
rewind(messFp);
fread(header, length, 1, messFp);
fclose(messFp);
header[length] = '\0';
Unlock(interp);
if (strncmp("From ", header, 5)) {
hPtr = header;
} else {
hPtr = strchr(header, '\n')+1;
if ('\r' == *hPtr) {
hPtr++;
}
}
return hPtr;
}
/*
*----------------------------------------------------------------------
*
* RatDbGetText --
*
* This routine extracts a copy of the body of a message in
* the database.
*
* Results:
* A pointer to a static area containing the message body
* is returned.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
char*
RatDbGetText(Tcl_Interp *interp, int index)
{
static char *body; /* Static storage area */
static int bodySize = 0; /* Size of static storage area */
char fname[1024]; /* Filename of message */
FILE *messFp; /* Message file pointer */
int length = 0; /* Length of header */
int bodyLength; /* Length of body */
char buf[2048]; /* Temporary holding area */
/*
* Check the index for validity.
*/
if (index >= numRead || index < 0) {
Tcl_SetResult(interp, "error: the given index is invalid", TCL_STATIC);
return NULL;
}
if (NULL == entryPtr[index].content[FROM]) {
Tcl_SetResult(interp, "error: the message is deleted", TCL_STATIC);
return NULL;
}
Lock(interp);
/*
* Read the message into an array pointed to by 'message'.
*/
sprintf(fname, "%s/dbase/%s", dbDir, entryPtr[index].content[FILENAME]);
if (NULL == (messFp = fopen(fname, "r"))) {
Unlock(interp);
Tcl_AppendResult(interp, "error opening file (for read)\"",
fname, "\": ", Tcl_PosixError(interp), (char*)NULL);
return NULL;
}
while (fgets(buf, sizeof(buf), messFp), !feof(messFp)) {
if ('\n' == buf[0] || '\r' == buf[0]) {
length = ftell(messFp);
break;
}
}
if (length == 0) {
fseek(messFp, 0, SEEK_END);
length = ftell(messFp);
}
fseek(messFp, 0, SEEK_END);
bodyLength = ftell(messFp) - length;
if (bodyLength+1 > bodySize) {
ckfree(body);
bodySize = bodyLength+1;
body = (char*)ckalloc(bodySize);
}
fseek(messFp, length, SEEK_SET);
fread(body, bodyLength, 1, messFp);
fclose(messFp);
body[bodyLength] = '\0';
Unlock(interp);
return body;
}
/*
*----------------------------------------------------------------------
*
* RatDbDelete --
*
* Deletes the specified entry from the database.
*
* Results:
* The return value is normally TCL_OK; if something goes wrong
* TCL_ERROR is returned and an error message will be left in
* interp->result.
*
* Side effects:
* Both the internal and the disk copy of the database are affected.
* Observer that if some caller has previously retrieved this entry
* from the database with a call to RatDbGet() the RatDbEntry
* obtained will be destroyed (filled with nulls).
*
*----------------------------------------------------------------------
*/
int
RatDbDelete(Tcl_Interp *interp, int index)
{
char buf[1024]; /* Name of index.changes file */
FILE *indexFP; /* File pointer to index.changes file */
/*
* Check the index for validity.
*/
if (index >= numRead || index < 0) {
Tcl_SetResult(interp, "error: the given index is invalid", TCL_STATIC);
return TCL_ERROR;
}
Lock(interp);
sprintf(buf, "%s/index.changes", dbDir);
if (NULL == (indexFP = fopen(buf, "a"))) {
Tcl_AppendResult(interp, "error opening (for append)\"", buf,
"\": ", Tcl_PosixError(interp), (char *) NULL);
Unlock(interp);
return TCL_ERROR;
}
if (0 > fprintf(indexFP, "d %d\n", index)) {
Tcl_AppendResult(interp, "Failed to write to file \"", buf, "\"",
(char*) NULL);
(void)fclose(indexFP);
Unlock(interp);
return TCL_ERROR;
}
if (0 != fclose(indexFP)) {
Tcl_AppendResult(interp, "error closing file \"", buf,
"\": ", Tcl_PosixError(interp), (char *) NULL);
Unlock(interp);
return TCL_ERROR;
}
Sync(interp);
Unlock(interp);
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* RatDbExpunge --
*
* Deletes all entries marked for deletion (status contains D).
*
* Results:
* The return value is normally TCL_OK; if something goes wrong
* TCL_ERROR is returned and an error message will be left in
* interp->result.
*
* Side effects:
* Both the internal and the disk copy of the database are affected.
* Observer that if some caller has previously retrieved this entry
* from the database with a call to RatDbGet() the RatDbEntry
* obtained will be destroyed (filled with nulls).
*
*----------------------------------------------------------------------
*/
int
RatDbExpunge(Tcl_Interp *interp)
{
char buf[1024]; /* Name of index.changes file */
FILE *indexFP; /* File pointer to index.changes file */
int index, i;
Lock(interp);
sprintf(buf, "%s/index.changes", dbDir);
if (NULL == (indexFP = fopen(buf, "a"))) {
Tcl_AppendResult(interp, "error opening (for append)\"", buf,
"\": ", Tcl_PosixError(interp), (char *) NULL);
Unlock(interp);
return TCL_ERROR;
}
for (index=0; index < numRead; index++) {
for (i=0; entryPtr[index].content[STATUS][i]; i++) {
if ('D' == entryPtr[index].content[STATUS][i]) {
fprintf(indexFP, "d %d\n", index);
break;
}
}
}
if (0 != fclose(indexFP)) {
Tcl_AppendResult(interp, "error closing file \"", buf,
"\": ", Tcl_PosixError(interp), (char *) NULL);
Unlock(interp);
return TCL_ERROR;
}
Sync(interp);
Unlock(interp);
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* RatDbDaysSinceExpire --
*
* Finds ut how long it was since the database was expired last.
*
* Results:
* An integer which is the number of days since the database was expired.
* If the user has no database we return 0.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
int
RatDbDaysSinceExpire(Tcl_Interp *interp)
{
struct stat sbuf;
char buf[1024];
/*
* First make sure we know where the database should reside.
*/
if (0 == dbDir) {
Tcl_DString ds; /* Scratch area */
char *value; /* Value of "dbase_dir" option */
value = Tcl_GetVar2(interp, "option", "dbase_dir", TCL_GLOBAL_ONLY);
if (NULL == value) {
Tcl_SetResult(interp, "option(dbase_dir) has no value", TCL_STATIC);
return TCL_ERROR;
}
dbDir = cpystr(Tcl_TildeSubst(interp, value, &ds));
Tcl_DStringFree(&ds);
}
sprintf(buf, "%s/expired", dbDir);
if (stat(buf, &sbuf)) {
sprintf(buf, "%s/dbase", dbDir);
if (stat(buf, &sbuf)) {
return 0;
}
}
if (sbuf.st_mtime > time(NULL)) {
return 0;
} else {
return (time(NULL)-sbuf.st_mtime)/(24*60*60);
}
}
/*
*----------------------------------------------------------------------
*
* RatDbExpire --
*
* Runs through the database and carries out any expiration that
* should be done. This routine should be called periodically.
*
* Results:
* If nothing went wrong TCL_OK is returned and in interp->result is
* a list containing 5 numbers {num_scanned num_delete, num_backup,
* num_inbox num_custom}. Otherwise TCL_ERROR is returned.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
int
RatDbExpire(Tcl_Interp *interp, char *infolder, char *backupDirectory)
{
char *compressProg, *compressSuffix, *backupDir, *statusId;
int numScan = 0, numDelete = 0, numBackup = 0, numInbox = 0, numCustom = 0;
int i, t, len, delete, mode, fd, doBackup = 0, changed = 0, error = 0;
int move;
char buf[1024], buf2[1024];
FILE *indexFP = NULL;
time_t now = time(NULL);
struct tm *tmPtr;
struct stat sbuf;
struct dirent *direntPtr;
DIR *dirPtr;
if (0 == isRead) {
if (TCL_OK != Read(interp)) {
return TCL_ERROR;
}
}
/*
* Get file creation mode
*/
Tcl_GetInt(interp, Tcl_GetVar2(interp, "option", "permissions",
TCL_GLOBAL_ONLY), &mode);
/*
* Make sure the inbox directory exists.
*/
sprintf(buf, "%s/inbox", dbDir);
if (-1 == stat(buf, &sbuf)) {
if (mkdir(buf, DIRMODE)) {
Tcl_AppendResult(interp, "error creating\"", buf,
"\": ", Tcl_PosixError(interp), (char *) NULL);
return TCL_ERROR;
}
}
/*
* Prepare backup
*/
if (-1 == stat(backupDirectory, &sbuf)) {
if (mkdir(backupDirectory, DIRMODE)) {
Tcl_AppendResult(interp, "error creating\"", backupDirectory,
"\": ", Tcl_PosixError(interp), (char *) NULL);
return TCL_ERROR;
}
}
compressProg = getenv("COMPRESS");
compressSuffix = getenv("CSUFFIX");
backupDir = Tcl_GetVar2(interp, "option", "dbase_backup", TCL_GLOBAL_ONLY);
if (!compressProg || !compressSuffix || !backupDir) {
Tcl_AppendResult(interp, "Internal error: compressProg, ",
"compressSuffix or option(dbase_backup) not defined",
(char*) NULL);
return TCL_ERROR;
}
strcpy(buf, "RatLog 2 $t(db_expire) explicit");
Tcl_Eval(interp, buf);
statusId = cpystr(interp->result);
Lock(interp);
for (i=0; !error && i < numRead; i++) {
if (!entryPtr[i].content[FROM]) { /* Entry deleted */
continue;
}
numScan++;
t = atoi(entryPtr[i].content[EX_TIME]);
if (!t || t >now) {
continue;
}
/*
* If we get here the entry should be expired.
*
* Currently we only handle the backup, delete and inbox actions.
* The rest are quitely ignored.
*/
delete = move = 0;
if (!strcmp("delete", entryPtr[i].content[EX_TYPE])) {
delete = 1;
numDelete++;
} else if (!strcmp("backup", entryPtr[i].content[EX_TYPE])) {
move = 1;
numBackup++;
sprintf(buf, "%s/dbase/%s", dbDir, entryPtr[i].content[FILENAME]);
RatGenId(NULL, interp, 0, NULL);
sprintf(buf2, "%s/message.%s", backupDirectory, interp->result);
} else if (!strcmp("incoming", entryPtr[i].content[EX_TYPE])) {
move = 1;
numInbox++;
sprintf(buf, "%s/dbase/%s", dbDir, entryPtr[i].content[FILENAME]);
RatGenId(NULL, interp, 0, NULL);
sprintf(buf2, "%s/inbox/%s", dbDir, interp->result);
} else if (!strncmp("custom", entryPtr[i].content[EX_TYPE], 6)) {
numCustom++;
} else if (!strcmp("none", entryPtr[i].content[EX_TYPE])) {
continue;
} else {
/*
* If we get here it is an unkown type and we just silently
* deletes it (old versions of tkrat may have generated it.
*/
delete = 1;
numDelete++;
}
if (move) {
if (link(buf, buf2)) {
int fdSrc, fdDst;
/*
* Sigh. the files are on different filesystems. We have to
* copy them.
*/
fdSrc = open(buf, O_RDONLY);
fdDst = open(buf, O_WRONLY|O_TRUNC|O_CREAT, mode);
do {
len = read(fdSrc, buf, sizeof(buf));
if (0 > write(fdDst, buf, len)) {
error = errno;
len = 0;
}
} while (len);
close(fdSrc);
if (close(fdDst) || error) {
sprintf(buf, "Failed to move to file '%s': %s", buf2,
Tcl_PosixError(interp));
RatLog(interp, RAT_ERROR, buf, 0);
delete = 0;
}
}
}
if (delete || move) {
if (!indexFP) {
changed = 1;
sprintf(buf, "%s/index.changes", dbDir);
if (NULL == (indexFP = fopen(buf, "a"))) {
sprintf(interp->result, "error opening (for append)\"%s\": %s\n",
buf, Tcl_PosixError(interp));
Unlock(interp);
return TCL_ERROR;
}
}
fprintf(indexFP, "d %d\n", i);
}
}
if (changed) {
fclose(indexFP);
Sync(interp);
}
Unlock(interp);
/*
* Compress the messages in the backup directory if we have enough
* messages to make it meaningful.
*/
if (numBackup) {
int chunkSize;
char *chunk;
chunk = Tcl_GetVar2(interp, "option", "chunksize", TCL_GLOBAL_ONLY);
if (chunk) {
Tcl_GetInt(interp, chunk, &chunkSize);
} else {
chunkSize = 100;
}
if (numBackup < chunkSize) {
i = 0;
dirPtr = opendir(backupDirectory);
while (0 != (direntPtr = readdir(dirPtr))) {
if (!strncmp(direntPtr->d_name, "message", 7)) {
i++;
}
}
closedir(dirPtr);
doBackup = (i>=chunkSize);
} else {
doBackup = 1;
}
}
if (doBackup) {
Tcl_Channel backupChannel, inChannel;
char *argv[2], *error = NULL;
free(statusId);
strcpy(buf, "RatLog 2 $t(packing_backup) explicit");
Tcl_Eval(interp, buf);
statusId = cpystr(interp->result);
tmPtr = localtime(&now);
sprintf(buf2, ">%s/backup_%02d%02d%02d%s", backupDirectory,
tmPtr->tm_year+1900, tmPtr->tm_mon+1, tmPtr->tm_mday+1,
compressSuffix);
argv[0] = compressProg;
argv[1] = buf2;
if (!(backupChannel = Tcl_OpenCommandChannel(interp, 2, argv,
TCL_STDIN))) {
Tcl_BackgroundError(interp);
}
if (backupChannel) {
dirPtr = opendir(backupDirectory);
while (!error && 0 != (direntPtr = readdir(dirPtr))) {
if (strncmp(direntPtr->d_name, "message", 7)) {
continue;
}
sprintf(buf, "%s/%s", backupDirectory, direntPtr->d_name);
inChannel = Tcl_OpenFileChannel(interp, buf, "r", 0);
do {
len = Tcl_Read(inChannel, buf, sizeof(buf));
if (-1 == Tcl_Write(backupChannel, buf, len)) {
error = Tcl_PosixError(interp);
}
} while (!error && !Tcl_Eof(inChannel));
Tcl_Close(interp, inChannel);
}
if (TCL_OK != Tcl_Close(interp, backupChannel)) {
error = Tcl_PosixError(interp);
}
if (error) {
unlink(buf2);
Tcl_SetResult(interp, error, TCL_STATIC);
Tcl_BackgroundError(interp);
} else {
rewinddir(dirPtr);
while (0 != (direntPtr = readdir(dirPtr))) {
if (!strncmp(direntPtr->d_name, "message", 7)) {
sprintf(buf,"%s/%s",backupDirectory,direntPtr->d_name);
unlink(buf);
}
}
}
closedir(dirPtr);
}
}
/*
* Move messages to inbox
*/
if (numInbox) {
char *data = NULL, *msg, *cPtr;
int fd, allocated = 0;
sprintf(buf, "%s/inbox", dbDir);
dirPtr = opendir(buf);
while (0 != (direntPtr = readdir(dirPtr))) {
sprintf(buf2, "%s/%s", buf, direntPtr->d_name);
if (stat(buf2, &sbuf) || !S_ISREG(sbuf.st_mode)) {
continue;
}
if (allocated < sbuf.st_size+1) {
allocated = sbuf.st_size+1;
data = (char*)REALLOC(data, allocated);
}
fd = open(buf2, O_RDONLY);
read(fd, data, sbuf.st_size);
close(fd);
data[sbuf.st_size] = '\0';
unlink(buf2);
for (cPtr = data; *cPtr != '\n'; cPtr++);
if (*(++cPtr) == '\r') {
cPtr++;
}
msg = RatFrMessageCreate(interp, cPtr, sbuf.st_size, NULL);
if (TCL_OK == Tcl_VarEval(interp, infolder, " insert ", msg,NULL)){
unlink(buf2);
}
}
closedir(dirPtr);
}
/*
* Mark that we have done expire
*/
sprintf(buf, "%s/expired", dbDir);
(void)unlink(buf);
if (0 <= (fd = open(buf, O_WRONLY|O_CREAT, mode))) {
close(fd);
}
Tcl_VarEval(interp, "RatClearLog ", statusId, "; update idletasks",
(char*) NULL);
free(statusId);
/*
* Create result list and return
*/
Tcl_ResetResult(interp);
sprintf(interp->result, "%d %d %d %d %d", numScan, numDelete, numBackup,
numInbox, numCustom);
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* RatDbClose --
*
* Closes the database on disk.
*
* Results:
* None.
*
* Side effects:
* The rlock file is removed and some of the internal data structures
* are freed (but not all :-().
*
*----------------------------------------------------------------------
*/
void
RatDbClose()
{
char buf[1024]; /* Scratch area */
if (1 == isRead) {
ckfree(entryPtr);
isRead = 0;
sprintf(buf, "%s/rlock.%s", dbDir, ident);
unlink(buf);
}
}
/*
*----------------------------------------------------------------------
*
* RatDbBuildList --
*
* Builds a list of the files in the database
*
* The algorithm is to open the directory and for all files do:
* - If the file is ".seq" then we read it and remember the number
* - If the name starts with a dot ('.') then we continue with the
* next file.
* - If it is a directory the we recursively call ourselves to check
* that directory.
* - If it is an ordinary file then we add it to the hastable (the
* hash is computed from the prefix/filename). We also decode the
* number of the file and remembers the highest number found.
* When all files are checked we check if the highest number found
* was bigger than the content of seq. If it is so then we
* - Append a warning to interp->result
* - write a new .seq file IF fix is true.
*
* Results:
* May append diagnostic messages to interp->result.
*
* Side effects:
* Will probably add items to the given hashtable.
*
*----------------------------------------------------------------------
*/
static void
RatDbBuildList(Tcl_Interp *interp, Tcl_DString *dsPtr, char *prefix, char *dir,
Tcl_HashTable *tablePtr, int fix)
{
char buf[1024], path[1024];
int seq = -1, i;
Tcl_HashEntry *entryPtr;
RatDbItem *itemPtr;
struct dirent *entPtr;
struct stat sbuf;
DIR *dirPtr;
FILE *fp;
if (NULL == (dirPtr = opendir(dir))) {
sprintf(buf, "Failed to open directory \"%s\": %s", dir,
Tcl_PosixError(interp));
Tcl_DStringAppendElement(dsPtr, buf);
return;
}
while (NULL != (entPtr = readdir(dirPtr))) {
if (!strcmp(entPtr->d_name, ".seq")) {
sprintf(path, "%s/.seq", dir);
if (NULL == (fp = fopen(path, "r"))) {
sprintf(buf, "Failed to open file \"%s\": %s", path,
Tcl_PosixError(interp));
Tcl_DStringAppendElement(dsPtr, buf);
if (fix) {
if (unlink(path)) {
sprintf(buf, "Failed to unlink file \"%s\": %s", path,
Tcl_PosixError(interp));
Tcl_DStringAppendElement(dsPtr, buf);
}
}
} else {
fscanf(fp, "%d", &seq);
fclose(fp);
}
}
if ('.' == entPtr->d_name[0]) {
continue;
}
sprintf(path, "%s/%s", dir, entPtr->d_name);
if (stat(path, &sbuf)) {
sprintf(buf, "Failed to stat file %s: %s\n", path,
Tcl_PosixError(interp));
Tcl_DStringAppendElement(dsPtr, buf);
continue;
}
if (S_IFREG == (sbuf.st_mode&S_IFMT)) {
if (!(S_IRUSR & sbuf.st_mode)) {
sprintf(buf, "\"%s\" is not readable by the owner", path);
Tcl_DStringAppendElement(dsPtr, buf);
if (fix) {
if (chmod(path, sbuf.st_mode|S_IRUSR)) {
sprintf(buf, "Failed to chmod \"%s\": %s", path,
Tcl_PosixError(interp));
Tcl_DStringAppendElement(dsPtr, buf);
continue;
}
}
}
if (0 == sbuf.st_size) {
sprintf(buf, "Empty file \"%s\" found", path);
if (fix) {
if (unlink(path)) {
sprintf(buf, "Failed to unlink \"%s\": %s", path,
Tcl_PosixError(interp));
Tcl_DStringAppendElement(dsPtr, buf);
}
}
continue;
}
if (*prefix) {
sprintf(buf, "%s/%s", prefix, entPtr->d_name);
} else {
strcpy(buf, entPtr->d_name);
}
itemPtr = (RatDbItem*)malloc(sizeof(RatDbItem));
itemPtr->fileSize = sbuf.st_size;
itemPtr->index = -1;
for (i=0; i<RATDBETYPE_END; i++) {
itemPtr->entry.content[i] = NULL;
}
entryPtr = Tcl_CreateHashEntry(tablePtr, buf, &i);
Tcl_SetHashValue(entryPtr, (ClientData)itemPtr);
} else if (S_IFDIR == (sbuf.st_mode&S_IFMT)) {
if (prefix && *prefix) {
sprintf(path, "%s/%s", prefix, entPtr->d_name);
} else {
strcpy(path, entPtr->d_name);
}
sprintf(buf, "%s/%s", dir, entPtr->d_name);
RatDbBuildList(interp, dsPtr, path, buf, tablePtr, fix);
} else {
sprintf(buf, "\"%s\" is not a file", path);
Tcl_DStringAppendElement(dsPtr, buf);
}
}
closedir(dirPtr);
}
/*
*----------------------------------------------------------------------
*
* RatDbCheck --
*
* Checks the database.
*
* Results:
* A diagnostic string.
*
* Side effects:
* The database on disk may be rewritten (depends on the fix argument).
*
*----------------------------------------------------------------------
*/
#define EXP_NUM "[0-9]*"
#define EXP_TYPE "^((none)|(remove)|(incoming)|(backup)|(custom.*))?$"
#define EXP_FILE "[^/]+/[0-9]*"
int
RatDbCheck(Tcl_Interp *interp, int fix)
{
int numFound = 0, numMal = 0, numAlone = 0, numUnlinked = 0, totSize = 0,
fd, lines, start, index, i, j, extraNum = 0, extraAlloc = 0, msgLen = 0,
date = 0, listArgc, elemArgc, indexInfo = 0;
char buf[8092], *indexPtr = NULL, **linePtrPtr = NULL, *cPtr,
*to, *from, *cc, *subject, *flags, **listArgv, **elemArgv,
*msgBuf = NULL;
Tcl_HashTable items, status;
char **extraPtrPtr = NULL;
Tcl_HashEntry *entryPtr;
Tcl_HashSearch search;
Tcl_DString reportDS;
RatDbItem *itemPtr;
struct stat sbuf;
MESSAGECACHE elt;
struct tm tm;
FILE *fp;
/*
* Initialize variables
*/
if (0 == dbDir) {
Tcl_DString ds; /* Scratch area */
char *value; /* Value of "dbase_dir" option */
value = Tcl_GetVar2(interp, "option", "dbase_dir", TCL_GLOBAL_ONLY);
if (NULL == value) {
return TCL_OK;
}
dbDir = cpystr(Tcl_TildeSubst(interp, value, &ds));
Tcl_DStringFree(&ds);
}
if (0 == ident) {
gethostname(buf, sizeof(buf));
ident = (char*)ckalloc(strlen(buf)+16);
sprintf(ident, "%s:%d", buf, (int)getpid());
}
/*
* Check that the database directory exists. If not we return zeros
*/
if (0 > stat(dbDir, &sbuf) ||
!S_ISDIR(sbuf.st_mode)) {
Tcl_SetResult(interp, "0 0 0 0 0 {}", TCL_STATIC);
return TCL_OK;
}
/*
* Lock the database. We should also check that nobody else has
* a read lock as well.
*/
Lock(interp);
if (IsRlocked(NULL)) {
Unlock(interp);
Tcl_SetResult(interp, "Some other process has locked the database.",
TCL_STATIC);
return TCL_ERROR;
}
/*
* Check index.info file
*/
sprintf(buf, "%s/index.info", dbDir);
if (NULL == (fp = fopen(buf, "r"))) {
Tcl_DStringAppendElement(&reportDS, "Failed to open index.info file");
} else {
fscanf(fp, "%d %d", &i, &indexInfo);
fclose(fp);
if (i != 3) {
Tcl_DStringAppendElement(&reportDS, "Wrong version of dbase");
Unlock(interp);
return TCL_ERROR;
}
}
/*
* Initialize variables
*/
Tcl_DStringInit(&reportDS);
Tcl_InitHashTable(&items, TCL_STRING_KEYS);
Tcl_InitHashTable(&status, TCL_ONE_WORD_KEYS);
/*
* Get a list of messages actually stored
*/
sprintf(buf, "%s/dbase", dbDir);
RatDbBuildList(interp, &reportDS, "", buf, &items, fix);
/*
* Check the changes file for flag changes and store them in the
* status hash table.
*/
sprintf(buf, "%s/index.changes", dbDir);
if (NULL != (fp = fopen(buf, "r"))) {
while (fgets(buf, sizeof(buf), fp), !feof(fp)) {
switch (buf[0]) {
case 'a':
indexInfo++;
break;
case 'd':
indexInfo--;
break;
case 's':
if (extraNum == extraAlloc) {
extraAlloc += 32;
extraPtrPtr = (char**)REALLOC(extraPtrPtr,
extraAlloc*sizeof(char*));
}
extraPtrPtr[extraNum] = (char*)malloc(strlen(buf));
sscanf(buf, "%*s %d %s", &index, extraPtrPtr[extraNum]);
entryPtr = Tcl_CreateHashEntry(&status, (char*)index, &i);
Tcl_SetHashValue(entryPtr, (ClientData)extraPtrPtr[extraNum]);
extraNum++;
break;
}
}
fclose(fp);
}
/*
* Check the index file
*/
sprintf(buf, "%s/index", dbDir);
if (-1 != (fd = open(buf, O_RDONLY))) {
/*
* Read file and build pointers to the lines
*/
fstat(fd, &sbuf);
indexPtr = (char*)malloc(sbuf.st_size+1);
read(fd, indexPtr, sbuf.st_size);
close(fd);
indexPtr[sbuf.st_size] = '\0';
for (lines = 0, cPtr = indexPtr; *cPtr; cPtr++) {
if ('\n' == *cPtr) {
lines++;
}
}
linePtrPtr = (char**)malloc(sizeof(char*)*lines);
for (cPtr = indexPtr, i = 0; *cPtr && cPtr; i++) {
linePtrPtr[i] = cPtr;
if ((cPtr = strchr(cPtr, '\n'))) {
*cPtr++ = '\0';
}
}
/*
* Now we are ready to reconstruct the index. We do this one entry
* a time. For each entry we read one line at a time and check the
* content against what we expected. We expect the following lines
* and contents:
* to - any string
* from - any string
* cc - any string
* subject - any string
* date - a number
* keywords - any string
* size - a number
* status - any string
* extime - a number
* exevent - one of none, remove, incoming, backup and custom
* filename - (.*)/[0-9]+
* When we have found an index we check for the corresponding entry
* in the list of files and fill it in.
*/
for (start = index = 0; start < lines; index++) {
if (start > lines-11) {
break;
}
if (!Tcl_RegExpMatch(interp, linePtrPtr[start+4], EXP_NUM)
|| !Tcl_RegExpMatch(interp, linePtrPtr[start+6], EXP_NUM)
|| !Tcl_RegExpMatch(interp, linePtrPtr[start+8], EXP_NUM)
|| !Tcl_RegExpMatch(interp, linePtrPtr[start+9], EXP_TYPE)
|| !Tcl_RegExpMatch(interp, linePtrPtr[start+10],EXP_FILE)){
sprintf(buf, "Entry %d is malformed", index);
Tcl_DStringAppendElement(&reportDS, buf);
numMal++;
/*
* We have found an malformed entry, first we search for the
* filename which should be the last item. From that we go
* backwards and try to collapse lines that somehow was
* splitted.
*/
i=0;
while (!Tcl_RegExpMatch(interp,linePtrPtr[start+i],EXP_FILE)) {
if (start + (++i) == lines) {
break;
};
}
i++;
if (start+i >= lines) {
/*
* We have reached the end of the file
*/
break;
}
/*
* Here we should collapse the lines but for now we
* just continue with the next item. /MaF
*/
start += i;
continue;
}
if (!(entryPtr = Tcl_FindHashEntry(&items, linePtrPtr[start+10]))) {
numAlone++;
sprintf(buf, "Entry %d has no associated file '%s'",
index, linePtrPtr[start+10]);
Tcl_DStringAppendElement(&reportDS, buf);
start += 11;
continue;
}
itemPtr = (RatDbItem*)Tcl_GetHashValue(entryPtr);
for (i=0; i<RATDBETYPE_END; i++, start++) {
if (i == STATUS && (entryPtr = Tcl_FindHashEntry(&status,
(char*)numFound))) {
itemPtr->entry.content[i]=(char*)Tcl_GetHashValue(entryPtr);
} else {
itemPtr->entry.content[i] = linePtrPtr[start];
}
}
numFound++;
}
}
/*
* Check for unlinked messages
* And calculate total size while we are at it.
*/
for (entryPtr = Tcl_FirstHashEntry(&items, &search);
entryPtr;
entryPtr = Tcl_NextHashEntry(&search)) {
itemPtr = (RatDbItem*)Tcl_GetHashValue(entryPtr);
totSize += itemPtr->fileSize;
if (itemPtr->entry.content[0]) {
continue;
}
numUnlinked++;
if (fix) {
/*
* Generate index entries for this message
*/
to = from = cc = subject = flags = NULL;
date = 0;
if (extraNum+8 >= extraAlloc) {
extraAlloc += 32;
extraPtrPtr = (char**)REALLOC(extraPtrPtr,
extraAlloc*sizeof(char*));
}
if (msgLen < itemPtr->fileSize+1) {
msgLen = itemPtr->fileSize+4096;
msgBuf = (char*)REALLOC(msgBuf, msgLen);
}
sprintf(buf, "%s/dbase/%s", dbDir, Tcl_GetHashKey(&items,entryPtr));
if (-1 == (fd = open(buf, O_RDONLY))) {
continue;
}
read(fd, msgBuf, itemPtr->fileSize);
msgBuf[itemPtr->fileSize] = '\0';
close(fd);
if (NULL == (cPtr = strstr(msgBuf, "\n\n"))) {
cPtr = strstr(msgBuf, "\r\n\r");
}
*(++cPtr) = '\0';
RatMessageGetHeader(interp, msgBuf, "full");
Tcl_SplitList(interp, interp->result, &listArgc, &listArgv);
for (i=0; i<listArgc; i++) {
Tcl_SplitList(interp, listArgv[i], &elemArgc, &elemArgv);
if (!to && !strcasecmp(elemArgv[0], "to")) {
to = extraPtrPtr[extraNum++] = cpystr(elemArgv[1]);
} else if (!from && !strcasecmp(elemArgv[0], "from")) {
from = extraPtrPtr[extraNum++] = cpystr(elemArgv[1]);
} else if (!cc && !strcasecmp(elemArgv[0], "cc")) {
cc = extraPtrPtr[extraNum++] = cpystr(elemArgv[1]);
} else if (!subject && !strcasecmp(elemArgv[0], "subject")) {
subject = extraPtrPtr[extraNum++] = cpystr(elemArgv[1]);
} else if (!strcasecmp(elemArgv[0], "status") ||
!strcasecmp(elemArgv[0], "x-status")) {
if (flags) {
flags=(char*)realloc(flags,strlen(flags)+
strlen(elemArgv[1])+1);
strcpy(&flags[strlen(flags)], elemArgv[1]);
} else {
flags = cpystr(elemArgv[1]);
}
} else if (!strcasecmp(elemArgv[0], "date")) {
if (T == mail_parse_date(&elt, elemArgv[1])) {
tm.tm_sec = elt.seconds;
tm.tm_min = elt.minutes;
tm.tm_hour = elt.hours;
tm.tm_mday = elt.day;
tm.tm_mon = elt.month - 1;
tm.tm_year = elt.year+70;
tm.tm_wday = 0;
tm.tm_yday = 0;
tm.tm_isdst = -1;
date = (int)mktime(&tm);
} else {
date = 0;
}
}
ckfree(elemArgv);
}
ckfree(listArgv);
if (flags) {
extraPtrPtr[extraNum++] = flags;
}
itemPtr->entry.content[TO] = to ? to : "";
itemPtr->entry.content[FROM] = from ? from : "";
itemPtr->entry.content[CC] = cc ? cc : "";
itemPtr->entry.content[SUBJECT] = subject ? subject : "";
sprintf(buf, "%d", date);
itemPtr->entry.content[DATE] = extraPtrPtr[extraNum++] =cpystr(buf);
itemPtr->entry.content[KEYWORDS] = "LostMessage";
sprintf(buf, "%d", itemPtr->fileSize);
itemPtr->entry.content[RSIZE] = extraPtrPtr[extraNum++]=cpystr(buf);
itemPtr->entry.content[STATUS] = flags ? flags : "";
sprintf(buf, "%ld", time(NULL)+60*60*24*100);
itemPtr->entry.content[EX_TIME]=extraPtrPtr[extraNum++]=cpystr(buf);
itemPtr->entry.content[EX_TYPE] = "backup";
itemPtr->entry.content[FILENAME] = Tcl_GetHashKey(&items, entryPtr);
}
}
if (numUnlinked && fix) {
Tcl_DStringAppendElement(&reportDS,
"The unlinked messages has been inserted with the keyword 'LostMessage'");
}
if (indexInfo != numFound+numUnlinked) {
if (fix) {
sprintf(buf, "Number of messages in index.info was wrong (was: %d is now: %d)",
indexInfo, numFound+numUnlinked);
} else {
sprintf(buf, "Number of messages in index.info is wrong (was: %d should be: %d)",
indexInfo, numFound+numUnlinked);
}
Tcl_DStringAppendElement(&reportDS, buf);
}
/*
* Write new index if fixing and needing
*/
if (fix && (numMal || numAlone || numUnlinked ||
indexInfo != numFound+numUnlinked)) {
sprintf(buf, "%s/index", dbDir);
fp = fopen(buf, "w");
for (entryPtr = Tcl_FirstHashEntry(&items, &search), j=0;
entryPtr;
entryPtr = Tcl_NextHashEntry(&search)) {
itemPtr = (RatDbItem*)Tcl_GetHashValue(entryPtr);
for (i=0; i<RATDBETYPE_END; i++) {
fputs(itemPtr->entry.content[i], fp);
fputc('\n', fp);
}
j++;
}
fclose(fp);
sprintf(buf, "%s/index.info", dbDir);
fp = fopen(buf, "w");
fprintf(fp, "3 %d\n", j);
fclose(fp);
sprintf(buf, "%s/index.changes", dbDir);
(void)unlink(buf);
if (isRead) {
isRead = 0;
sprintf(buf, "Popup $t(need_restart)");
Tcl_Eval(interp, buf);
}
}
/*
* Cleaning up
*/
Unlock(interp);
for (entryPtr = Tcl_FirstHashEntry(&items, &search);
entryPtr;
entryPtr = Tcl_NextHashEntry(&search)) {
free(Tcl_GetHashValue(entryPtr));
}
Tcl_DeleteHashTable(&items);
Tcl_DeleteHashTable(&status);
if (indexPtr) {
free(indexPtr);
}
if (linePtrPtr) {
free(linePtrPtr);
}
Tcl_ResetResult(interp);
sprintf(buf, "%d", numFound);
Tcl_AppendElement(interp, buf);
sprintf(buf, "%d", numMal);
Tcl_AppendElement(interp, buf);
sprintf(buf, "%d", numAlone);
Tcl_AppendElement(interp, buf);
sprintf(buf, "%d", numUnlinked);
Tcl_AppendElement(interp, buf);
sprintf(buf, "%d", totSize);
Tcl_AppendElement(interp, buf);
Tcl_AppendElement(interp, Tcl_DStringValue(&reportDS));
Tcl_DStringFree(&reportDS);
for (i=0; i<extraNum; i++) {
free(extraPtrPtr[i]);
}
free(extraPtrPtr);
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* NoLFPrint --
*
* Prints the given string, but replaces any newlines with space.
* Also handles null strings.
*
* Results:
* -1 if any error ocurred
*
* Side effects:
* The mentioned file is modified
*
*----------------------------------------------------------------------
*/
static int
NoLFPrint(FILE *fp, char *s)
{
unsigned char *cPtr;
for (cPtr = (unsigned char*)s; cPtr && *cPtr; cPtr++) {
if ('\n' == *cPtr) {
if (isspace(cPtr[1])) {
cPtr++;
} else {
fputc(' ', fp);
}
} else {
fputc(*cPtr, fp);
}
}
return fputc('\n', fp);
}
|