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
|
/*
* Copyright (C) Volition, Inc. 1999. All rights reserved.
*
* All source code herein is the property of Volition, Inc. You may not sell
* or otherwise commercially exploit the source or things you created based on the
* source.
*
*/
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cerrno>
#include <sstream>
#include <algorithm>
#ifdef _WIN32
#include <io.h>
#include <direct.h>
#include <windows.h>
#include <winbase.h> /* needed for memory mapping of file functions */
#include <shlwapi.h>
#endif
#ifdef SCP_UNIX
#include <sys/types.h>
#include <dirent.h>
#include <fnmatch.h>
#include <sys/stat.h>
#include <unistd.h>
#include <libgen.h>
#endif
#include "cfile/cfile.h"
#include "cfile/cfilesystem.h"
#include "cmdline/cmdline.h"
#include "globalincs/pstypes.h"
#include "def_files/def_files.h"
#include "osapi/osapi.h"
#include "parse/parselo.h"
enum CfileRootType {
CF_ROOTTYPE_PATH = 0,
CF_ROOTTYPE_PACK = 1,
CF_ROOTTYPE_MEMORY = 2,
};
// Created by:
// specifying hard drive tree
// searching for pack files on hard drive // Found by searching all known paths
// specifying cd-rom tree
// searching for pack files on CD-rom tree
typedef struct cf_root {
SCP_string path; // Contains something like c:\projects\freespace or c:\projects\freespace\freespace.vp
int roottype; // CF_ROOTTYPE_PATH = Path, CF_ROOTTYPE_PACK =Pack file, CF_ROOTTYPE_MEMORY=In memory
uint32_t location_flags;
#ifdef SCP_UNIX
// map of existing case sensitive paths
SCP_unordered_map<int, SCP_string> pathTypeToRealPath;
#endif
cf_root() : roottype(-1), location_flags(0) {}
} cf_root;
// convenient type for sorting (see cf_build_pack_list())
typedef struct cf_root_sort {
SCP_string path;
int roottype;
int cf_type;
} cf_root_sort;
#define CF_NUM_ROOTS_PER_BLOCK 32
#define CF_MIN_ROOT_BLOCKS 2 // minimum number of root blocks to create on resize
// (set to value which fits retail data without resizing)
static_assert(CF_MIN_ROOT_BLOCKS >= 2, "CF_MIN_ROOT_BLOCKS is set below retail threshold!");
typedef struct cf_root_block {
cf_root roots[CF_NUM_ROOTS_PER_BLOCK];
} cf_root_block;
static int Num_roots = 0;
static SCP_vector<std::unique_ptr<cf_root_block>> Root_blocks;
static int Num_path_roots = 0;
// Created by searching all roots in order. This means Files is then sorted by precedence.
typedef struct cf_file {
SCP_string name_ext; // Filename and extension
int root_index; // Where in Roots this is located
int pathtype_index; // Where in Paths this is located
time_t write_time; // When it was last written
int size; // How big it is in bytes
int pack_offset; // For pack files, where it is at. 0 if not in a pack file. This can be used to tell if in a pack file.
SCP_string real_name; // For real files, the full path
SCP_string sub_path; // subfolder path off of pathtype root (should include trailing directory seperator)
const void* data; // For in-memory files, the data pointer
cf_file() : root_index(0), pathtype_index(0), write_time(0), size(0), pack_offset(0), data(nullptr) {}
} cf_file;
#define CF_NUM_FILES_PER_BLOCK 512
#define CF_MIN_FILE_BLOCKS 16 // minimum number of file blocks to create on resize
// (set to value which fits retail data without resizing)
static_assert(CF_MIN_FILE_BLOCKS >= 16, "CF_MIN_FILE_BLOCKS is set below retail threshold!");
typedef struct cf_file_block {
cf_file files[CF_NUM_FILES_PER_BLOCK];
} cf_file_block;
static uint Num_files = 0;
static SCP_vector<std::unique_ptr<cf_file_block>> File_blocks;
// Return a pointer to to file 'index'.
cf_file *cf_get_file(int index)
{
size_t block = index / CF_NUM_FILES_PER_BLOCK;
size_t offset = index % CF_NUM_FILES_PER_BLOCK;
Assertion(block < File_blocks.size(), "File index is outside of block range!");
return &File_blocks[block]->files[offset];
}
// Create a new file and return a pointer to it.
cf_file *cf_create_file()
{
size_t block = Num_files / CF_NUM_FILES_PER_BLOCK;
size_t offset = Num_files % CF_NUM_FILES_PER_BLOCK;
if (block >= File_blocks.size()) {
// we really don't want to have this use automatic resizing so make sure
// to allocate using a set size to avoid too much memory being reserved
// with larger file sets
if (File_blocks.size() == File_blocks.capacity()) {
File_blocks.reserve(File_blocks.size() + CF_MIN_FILE_BLOCKS);
}
File_blocks.push_back(std::unique_ptr<cf_file_block>(new cf_file_block));
}
Num_files++;
return &File_blocks[block]->files[offset];
}
extern int cfile_inited;
// Create a new root and return a pointer to it. The structure is assumed unitialized.
cf_root *cf_get_root(int n)
{
size_t block = n / CF_NUM_ROOTS_PER_BLOCK;
size_t offset = n % CF_NUM_ROOTS_PER_BLOCK;
if (!cfile_inited)
return NULL;
Assertion(block < Root_blocks.size(), "Root index is outside of block range!");
return &Root_blocks[block]->roots[offset];
}
// Create a new root and return a pointer to it. The structure is assumed unitialized.
cf_root *cf_create_root()
{
size_t block = Num_roots / CF_NUM_ROOTS_PER_BLOCK;
size_t offset = Num_roots % CF_NUM_ROOTS_PER_BLOCK;
if (block >= Root_blocks.size()) {
// we really don't want to have this use automatic resizing so make sure
// to allocate using a set size to avoid too much memory being reserved
// with larger file sets
if (Root_blocks.size() == Root_blocks.capacity()) {
Root_blocks.reserve(Root_blocks.size() + CF_MIN_ROOT_BLOCKS);
}
Root_blocks.push_back(std::unique_ptr<cf_root_block>(new cf_root_block));
}
Num_roots++;
return &Root_blocks[block]->roots[offset];
}
struct _file_list_t {
SCP_string name;
SCP_string sub_path;
time_t m_time;
size_t size;
int pathtype;
int offset;
_file_list_t() : m_time(0), size(0), pathtype(CF_TYPE_INVALID), offset(0) {}
};
static bool sort_file_list(const _file_list_t &a, const _file_list_t &b)
{
if ( a.sub_path.empty() && !b.sub_path.empty() ) {
return stricmp(a.name.c_str(), b.sub_path.c_str()) < 0;
}
if ( !a.sub_path.empty() && b.sub_path.empty() ) {
return stricmp(a.sub_path.c_str(), b.name.c_str()) < 0;
}
if ( !a.sub_path.empty() && !b.sub_path.empty() ) {
int rc = stricmp(a.sub_path.c_str(), b.sub_path.c_str());
if (rc) {
return rc < 0;
}
}
return stricmp(a.name.c_str(), b.name.c_str()) < 0;
}
static size_t cf_get_list_of_files(const SCP_string &in_path, SCP_vector<_file_list_t> &files, const char *filter = nullptr, bool recursive = false, const char *subpath = nullptr)
{
_file_list_t nfile;
SCP_string path = in_path;
if (path.back() != DIR_SEPARATOR_CHAR) {
path += DIR_SEPARATOR_CHAR;
}
if (subpath) {
path += subpath;
path += DIR_SEPARATOR_CHAR;
}
#if defined _WIN32
intptr_t find_handle;
_finddata_t find;
// make sure we return all entries by default
path += "*";
find_handle = _findfirst(path.c_str(), &find);
if (find_handle == -1) {
return 0;
}
do {
if (find.attrib & _A_SUBDIR) {
if ( recursive && strcmp(find.name, ".") && strcmp(find.name, "..") ) {
SCP_string sub;
if (subpath) {
sub = subpath;
sub += DIR_SEPARATOR_CHAR;
}
sub += find.name;
cf_get_list_of_files(in_path, files, filter, recursive, sub.c_str());
}
continue;
}
if (filter && !PathMatchSpec(find.name, filter)) {
continue;
}
nfile.name = find.name;
nfile.m_time = find.time_write;
nfile.size = find.size;
if (subpath) {
nfile.sub_path = subpath;
nfile.sub_path += DIR_SEPARATOR_CHAR;
}
files.push_back(nfile);
} while ( !_findnext(find_handle, &find) );
_findclose(find_handle);
#elif defined SCP_UNIX
DIR *dirp;
struct dirent *dir;
SCP_string filepath;
dirp = opendir(path.c_str());
if ( !dirp ) {
return 0;
}
while ((dir = readdir(dirp)) != nullptr) {
filepath = path;
filepath += dir->d_name;
struct stat buf;
if (stat(filepath.c_str(), &buf) == -1) {
continue;
}
if ( recursive && S_ISDIR(buf.st_mode) && strcmp(dir->d_name, ".") && strcmp(dir->d_name, "..") ) {
SCP_string sub;
if (subpath) {
sub = subpath;
sub += DIR_SEPARATOR_CHAR;
}
sub += dir->d_name;
cf_get_list_of_files(in_path, files, filter, recursive, sub.c_str());
continue;
}
if ( !S_ISREG(buf.st_mode) ) {
continue;
}
// zero byte files shouldn't be indexed, but that breaks unit tests
// if ( !buf.st_size ) {
// continue;
// }
if (filter && fnmatch(filter, dir->d_name, 0)) {
continue;
}
nfile.name = dir->d_name;
nfile.m_time = buf.st_mtime;
nfile.size = buf.st_size;
if (subpath) {
nfile.sub_path = subpath;
nfile.sub_path += DIR_SEPARATOR_CHAR;
}
files.push_back(nfile);
}
closedir(dirp);
#endif
if ( !subpath ) {
std::sort(files.begin(), files.end(), sort_file_list);
}
return files.size();
}
static bool cf_should_scan_subdirs(int pathtype)
{
if ((pathtype <= CF_TYPE_DATA) || (pathtype >= CF_MAX_PATH_TYPES)) {
return false;
}
switch (pathtype) {
// all pilot file related directories, except for images, should be ignored
case CF_TYPE_PLAYERS:
case CF_TYPE_SINGLE_PLAYERS:
case CF_TYPE_MULTI_PLAYERS:
case CF_TYPE_PLAYER_BINDS:
// voice, missing extensions, has no automatic subfolder support
case CF_TYPE_VOICE:
return false;
default:
break;
}
return true;
}
static void cf_init_root_pathtypes(cf_root *root)
{
Assertion(root != nullptr, "Root must be specified!");
#ifdef SCP_UNIX
DIR *dirp;
struct dirent *dir;
struct stat buf;
SCP_string base_path = root->path;
root->pathTypeToRealPath.clear();
if (base_path.back() != DIR_SEPARATOR_CHAR) {
base_path += DIR_SEPARATOR_CHAR;
}
for (int i = CF_TYPE_DATA; i < CF_MAX_PATH_TYPES; ++i) {
SCP_string full_path;
SCP_string path;
SCP_string search_name;
SCP_string fn;
auto parentPathIter = root->pathTypeToRealPath.find(Pathtypes[i].parent_index);
if (parentPathIter == root->pathTypeToRealPath.end()) {
path = Pathtypes[Pathtypes[i].parent_index].path;
} else {
path = parentPathIter->second;
}
if ( !path.empty() && path.back() != DIR_SEPARATOR_CHAR) {
path += DIR_SEPARATOR_CHAR;
}
fn = Pathtypes[i].path;
SCP_string::size_type pos = fn.find_last_of(DIR_SEPARATOR_CHAR);
if (pos != SCP_string::npos) {
search_name = fn.substr(pos+1);
} else {
search_name = fn;
}
full_path = base_path + path;
dirp = opendir(full_path.c_str());
if ( !dirp ) {
continue;
}
while ((dir = readdir(dirp)) != nullptr) {
if (stricmp(search_name.c_str(), dir->d_name)) {
continue;
}
fn = full_path + dir->d_name;
if (stat(fn.c_str(), &buf) == -1) {
continue;
}
if (S_ISDIR(buf.st_mode)) {
root->pathTypeToRealPath.insert(std::make_pair(i, path + dir->d_name));
}
}
closedir(dirp);
}
#endif
}
static SCP_string cf_get_root_pathtype(__UNUSED const cf_root *root, const int type)
{
#ifdef SCP_UNIX
auto parentPathIter = root->pathTypeToRealPath.find(type);
if (parentPathIter != root->pathTypeToRealPath.end()) {
return parentPathIter->second;
}
#endif
return Pathtypes[type].path;
}
// packfile sort function
bool cf_packfile_sort_func(const cf_root_sort &r1, const cf_root_sort &r2)
{
// if the 2 directory types are the same, do a string compare
if (r1.cf_type == r2.cf_type) {
return (stricmp(r1.path.c_str(), r2.path.c_str()) < 0);
}
// otherwise return them in order of CF_TYPE_* precedence
return (r1.cf_type < r2.cf_type);
}
// Go through a root and look for pack files
void cf_build_pack_list( cf_root *root )
{
SCP_vector<cf_root_sort> temp_roots_sort;
SCP_string filespec;
SCP_string fullpath;
int i;
#ifdef _WIN32
const SCP_vector<SCP_string> filters = { "*.vpc", "*.vp" };
#else
const SCP_vector<SCP_string> filters = { "*.[vV][pP][cC]", "*.[vV][pP]" };
#endif
// now just setup all the root info
for (i = CF_TYPE_ROOT; i < CF_MAX_PATH_TYPES; i++) {
filespec = root->path;
if ( strlen(Pathtypes[i].path) ) {
if (filespec.back() != DIR_SEPARATOR_CHAR) {
filespec += DIR_SEPARATOR_CHAR;
}
filespec += cf_get_root_pathtype(root, i);
}
if (filespec.back() != DIR_SEPARATOR_CHAR) {
filespec += DIR_SEPARATOR_CHAR;
}
SCP_vector<_file_list_t> files;
for (auto &filter : filters) {
cf_get_list_of_files(filespec, files, filter.c_str());
}
if (files.empty()) {
continue;
}
for (auto &file : files) {
cf_root_sort s_root;
fullpath = filespec + file.name;
// fill in the proper info
s_root.path = fullpath;
s_root.roottype = CF_ROOTTYPE_PACK;
s_root.cf_type = i;
temp_roots_sort.push_back(s_root);
}
}
// sort the roots
std::sort(temp_roots_sort.begin(), temp_roots_sort.end(), cf_packfile_sort_func);
// now insert them all into the real root list properly
for (auto &s_root : temp_roots_sort) {
auto new_root = cf_create_root();
new_root->location_flags = root->location_flags;
// mwa -- 4/2/98 put in the next 2 lines because the path name needs to be there
// to find the files.
new_root->path = s_root.path;
new_root->roottype = CF_ROOTTYPE_PACK;
#ifndef NDEBUG
uint chksum = 0;
cf_chksum_pack(s_root.path.c_str(), &chksum);
mprintf(("Found root pack '%s' with a checksum of 0x%08x\n", s_root.path.c_str(), chksum));
#endif
}
}
void normalize_directory_separators(SCP_string &str)
{
char bad_sep = '/';
if (bad_sep == DIR_SEPARATOR_CHAR) {
bad_sep = '\\';
}
std::replace(str.begin(), str.end(), bad_sep, DIR_SEPARATOR_CHAR);
}
static void cf_add_mod_roots(const char* rootDirectory, uint32_t basic_location)
{
if (Cmdline_mod)
{
bool primary = true;
for (const char* cur_pos=Cmdline_mod; strlen(cur_pos) != 0; cur_pos+= (strlen(cur_pos)+1))
{
SCP_stringstream ss;
ss << rootDirectory;
if (rootDirectory[strlen(rootDirectory) - 1] != DIR_SEPARATOR_CHAR)
{
ss << DIR_SEPARATOR_CHAR;
}
ss << cur_pos << DIR_SEPARATOR_STR;
SCP_string rootPath = ss.str();
if (rootPath.size() + 1 >= CF_MAX_PATHNAME_LENGTH) {
Error(LOCATION, "The length of mod directory path '%s' exceeds the maximum of %d!\n", rootPath.c_str(), CF_MAX_PATHNAME_LENGTH);
}
// normalize the path to the native path format
normalize_directory_separators(rootPath);
cf_root* root = cf_create_root();
root->path = std::move(rootPath);
if (primary) {
root->location_flags = basic_location | CF_LOCATION_TYPE_PRIMARY_MOD;
} else {
root->location_flags = basic_location | CF_LOCATION_TYPE_SECONDARY_MODS;
}
root->roottype = CF_ROOTTYPE_PATH;
cf_init_root_pathtypes(root);
cf_build_pack_list(root);
primary = false;
}
}
}
void cf_build_root_list(const char *cdrom_dir)
{
Num_roots = 0;
Num_path_roots = 0;
cf_root *root = nullptr;
if (os_is_legacy_mode())
{
// =========================================================================
#ifdef WIN32
// Nothing to do here, Windows uses the current directory as the base
#else
cf_add_mod_roots(os_get_legacy_user_dir(), CF_LOCATION_ROOT_USER);
root = cf_create_root();
root->path = os_get_legacy_user_dir();
root->location_flags |= CF_LOCATION_ROOT_USER | CF_LOCATION_TYPE_ROOT;
if (Cmdline_mod == nullptr || strlen(Cmdline_mod) <= 0) {
// If there are no mods then the root is the primary mod
root->location_flags |= CF_LOCATION_TYPE_PRIMARY_MOD;
}
// do we already have a slash? as in the case of a root directory install
if (root->path.back() != DIR_SEPARATOR_CHAR) {
root->path += DIR_SEPARATOR_CHAR;
}
root->roottype = CF_ROOTTYPE_PATH;
cf_init_root_pathtypes(root);
// Next, check any VP files under the current directory.
cf_build_pack_list(root);
#endif
// =========================================================================
}
else if (!Cmdline_portable_mode)
{
// =========================================================================
// now look for mods under the users HOME directory to use before system ones
cf_add_mod_roots(Cfile_user_dir, CF_LOCATION_ROOT_USER);
// =========================================================================
// =========================================================================
// set users HOME directory as default for loading and saving files
root = cf_create_root();
root->path = Cfile_user_dir;
root->location_flags |= CF_LOCATION_ROOT_USER | CF_LOCATION_TYPE_ROOT;
if (Cmdline_mod == nullptr || strlen(Cmdline_mod) <= 0) {
// If there are no mods then the root is the primary mod
root->location_flags |= CF_LOCATION_TYPE_PRIMARY_MOD;
}
// do we already have a slash? as in the case of a root directory install
if (root->path.back() != DIR_SEPARATOR_CHAR) {
root->path += DIR_SEPARATOR_CHAR;
}
root->roottype = CF_ROOTTYPE_PATH;
cf_init_root_pathtypes(root);
// Next, check any VP files under the current directory.
cf_build_pack_list(root);
// =========================================================================
}
char working_directory[CF_MAX_PATHNAME_LENGTH];
if ( !_getcwd(working_directory, CF_MAX_PATHNAME_LENGTH ) ) {
Error(LOCATION, "Can't get current working directory -- %d", errno );
}
if (Cmdline_override_data) {
//To allow user-override of mod files, this new root takes presecdence over all mod roots.
cf_root *last = nullptr;
last = cf_create_root();
last->location_flags |= CF_LOCATION_ROOT_GAME | CF_LOCATION_TYPE_ROOT | CF_LOCATION_TYPE_SECONDARY_MODS;
last->path = working_directory;
if (last->path.back() != DIR_SEPARATOR_CHAR) {
last->path += DIR_SEPARATOR_CHAR;
}
last->path += "_override";
last->path += DIR_SEPARATOR_CHAR;
last->roottype = CF_ROOTTYPE_PATH;
cf_init_root_pathtypes(last);
cf_build_pack_list(last);
}
//now that the _override root is finished(or not), handle all mods
cf_add_mod_roots(working_directory, CF_LOCATION_ROOT_GAME);
//finally, handle the default root.
root = cf_create_root();
root->location_flags |= CF_LOCATION_ROOT_GAME | CF_LOCATION_TYPE_ROOT;
if (Cmdline_mod == nullptr || strlen(Cmdline_mod) <= 0) {
// If there are no mods then the root is the primary mod
root->location_flags |= CF_LOCATION_TYPE_PRIMARY_MOD;
}
root->path = working_directory;
// do we already have a slash? as in the case of a root directory install
if (root->path.back() != DIR_SEPARATOR_CHAR) {
root->path += DIR_SEPARATOR_CHAR;
}
root->roottype = CF_ROOTTYPE_PATH;
cf_init_root_pathtypes(root);
//======================================================
// Next, check any VP files under the current directory.
cf_build_pack_list(root);
//======================================================
// Check the real CD if one...
if ( cdrom_dir && (strlen(cdrom_dir) < CF_MAX_PATHNAME_LENGTH) ) {
root = cf_create_root();
root->path = cdrom_dir;
root->roottype = CF_ROOTTYPE_PATH;
cf_init_root_pathtypes(root);
//======================================================
// Next, check any VP files in the CD-ROM directory.
cf_build_pack_list(root);
}
// The final root is the in-memory root
root = cf_create_root();
root->location_flags = CF_LOCATION_ROOT_MEMORY | CF_LOCATION_TYPE_ROOT;
root->roottype = CF_ROOTTYPE_MEMORY;
}
// Given a lower case list of file extensions
// separated by spaces, return zero if ext is
// not in the list.
int is_ext_in_list( const char *ext_list, const char *ext )
{
char tmp_ext[128];
strcpy_s( tmp_ext, ext);
strlwr(tmp_ext);
if ( strstr(ext_list, tmp_ext )) {
return 1;
}
return 0;
}
// Run a basic test for indexed files that may be shadowed
#ifndef NDEBUG
#define ENABLE_SHADOW_CHECK 1
SCP_vector<SCP_string> critical_shadowed;
#endif
static void check_file_shadows(const int root_index __UNUSED, const int pathtype __UNUSED, const SCP_string &name __UNUSED, const SCP_string &sub_path __UNUSED, const char *real_name = nullptr)
{
#if ENABLE_SHADOW_CHECK
if ( !cf_should_scan_subdirs(pathtype) ) {
return;
}
SCP_string curfile, newfile;
const bool critical = ((pathtype == CF_TYPE_TABLES) || (pathtype == CF_TYPE_MISSIONS));
const auto root = cf_get_root(root_index);
newfile = root->path + ((root->roottype == CF_ROOTTYPE_PACK) ? "::" : "");
newfile += cf_get_root_pathtype(root, pathtype) + DIR_SEPARATOR_CHAR;
newfile += sub_path + (real_name ? real_name : name);
for (uint i = 0; i < Num_files; ++i) {
const auto f = cf_get_file(i);
const auto r = cf_get_root(f->root_index);
// skip memory roots, no subdirs there
if (r->roottype == CF_ROOTTYPE_MEMORY) {
continue;
}
// if basic path type doesn't match then skip
if (pathtype != f->pathtype_index) {
continue;
}
if (name != f->name_ext) {
continue;
}
// if the subpath matches then consider it an override rather than shadow
if (sub_path == f->sub_path) {
continue;
}
if (r->roottype == CF_ROOTTYPE_PATH) {
curfile = f->real_name;
} else {
curfile = r->path + ((r->roottype == CF_ROOTTYPE_PACK) ? "::" : "");
curfile += cf_get_root_pathtype(r, pathtype) + DIR_SEPARATOR_CHAR;
curfile += f->sub_path + f->name_ext;
}
// this log message occurs in the middle of an existing line, hence the extra new lines
// critical files and files in same root are always logged, files in different root are behind a filter
nprintf(((critical || !root->path.compare(r->path)) ? "General" : "CFileSystem", "\nWARNING! A %sfile being indexed may be shadowed by an existing file!\n New:\n %s\n Existing:\n %s\n",
critical ? "critical " : "",
newfile.c_str(), curfile.c_str()));
if (critical) {
critical_shadowed.push_back(name);
}
break;
}
#endif
}
void cf_search_root_path(int root_index)
{
int i;
int num_files = 0;
cf_root* root = cf_get_root(root_index);
mprintf(("Searching root '%s' ... ", root->path.c_str()));
#ifndef WIN32
try {
auto current = root->path.begin();
const auto prefPathEnd = root->path.end();
while (current != prefPathEnd) {
const auto cp = utf8::next(current, prefPathEnd);
if (cp > 127) {
// On Windows, we currently do not support Unicode paths so catch this early and let the user
// know
const auto invalid_end = current;
utf8::prior(current, root->path.begin());
Error(LOCATION,
"Trying to use path \"%s\" as a data root. That path is not supported since it "
"contains a Unicode character (%s). If possible, change this path to something that only uses "
"ASCII characters.",
root->path.c_str(), std::string(current, invalid_end).c_str());
}
}
} catch (const std::exception& e) {
Error(LOCATION, "UTF-8 error while checking the root path \"%s\": %s", root->path.c_str(), e.what());
}
#endif
SCP_string search_path;
for (i = CF_TYPE_ROOT; i < CF_MAX_PATH_TYPES; i++) {
// we don't want to add player files to the cache - taylor
if ( (i == CF_TYPE_SINGLE_PLAYERS) || (i == CF_TYPE_MULTI_PLAYERS) ) {
continue;
}
search_path = root->path;
if (strlen(Pathtypes[i].path)) {
if (search_path.back() != DIR_SEPARATOR_CHAR) {
search_path += DIR_SEPARATOR_CHAR;
}
search_path += cf_get_root_pathtype(root, i);
}
SCP_vector<_file_list_t> files;
cf_get_list_of_files(search_path, files, "*.*", cf_should_scan_subdirs(i));
for (auto &file : files) {
auto ext_idx = file.name.rfind('.');
auto orig_name = file.name;
if ( ext_idx != SCP_string::npos && SCP_string_lcase_equal_to()(file.name.substr(ext_idx), SCP_string(".lz41")) ) {
file.name = file.name.erase(ext_idx, file.name.length());
ext_idx = file.name.rfind('.');
}
if ( ext_idx == SCP_string::npos || !is_ext_in_list(Pathtypes[i].extensions, file.name.substr(ext_idx+1).c_str()) ) {
continue;
}
check_file_shadows(root_index, i, file.name, file.sub_path, orig_name.c_str());
cf_file *cfile = cf_create_file();
cfile->name_ext = file.name;
cfile->root_index = root_index;
cfile->pathtype_index = i;
cfile->write_time = file.m_time;
cfile->size = static_cast<int>(file.size);
cfile->pack_offset = 0;
cfile->real_name = search_path + DIR_SEPARATOR_STR + file.sub_path + orig_name;
cfile->sub_path = file.sub_path;
++num_files;
}
}
mprintf(( "%i files\n", num_files ));
}
typedef struct VP_FILE_HEADER {
char id[4];
int version;
int index_offset;
int num_files;
} VP_FILE_HEADER;
typedef struct VP_FILE {
int offset;
int size;
char filename[32];
_fs_time_t write_time;
} VP_FILE;
static int cf_add_pack_files(const int root_index, SCP_vector<_file_list_t> &files)
{
if (files.empty()) {
return 0;
}
std::sort(files.begin(), files.end(), sort_file_list);
for (auto &file : files) {
check_file_shadows(root_index, file.pathtype, file.name, file.sub_path);
cf_file *pf = cf_create_file();
pf->name_ext = file.name;
pf->root_index = root_index;
pf->pathtype_index = file.pathtype;
pf->write_time = file.m_time;
pf->size = static_cast<int>(file.size);
pf->pack_offset = file.offset; // Mark as a packed file
pf->sub_path = file.sub_path;
}
return static_cast<int>(files.size());
}
void cf_search_root_pack(int root_index)
{
int num_files = 0;
cf_root *root = cf_get_root(root_index);
Assert( root != NULL );
// Open data
FILE *fp = fopen( root->path.c_str(), "rb" );
// Read the file header
if (!fp) {
return;
}
if ( filelength(fileno(fp)) < (int)(sizeof(VP_FILE_HEADER) + (sizeof(int) * 3)) ) {
mprintf(( "Skipping VP file ('%s') of invalid size...\n", root->path.c_str() ));
fclose(fp);
return;
}
VP_FILE_HEADER VP_header;
Assert( sizeof(VP_header) == 16 );
if (fread(&VP_header, sizeof(VP_header), 1, fp) != 1) {
mprintf(("Skipping VP file ('%s') because the header could not be read...\n", root->path.c_str()));
fclose(fp);
return;
}
VP_header.version = INTEL_INT( VP_header.version ); //-V570
VP_header.index_offset = INTEL_INT( VP_header.index_offset ); //-V570
VP_header.num_files = INTEL_INT( VP_header.num_files ); //-V570
mprintf(( "Searching root pack '%s' ... ", root->path.c_str() ));
// Read index info
fseek(fp, VP_header.index_offset, SEEK_SET);
SCP_string search_path;
SCP_string sub_path;
int path_type = CF_TYPE_INVALID;
SCP_vector<_file_list_t> files;
files.reserve(256); // should be set to a good baseline of files per path
// Go through all the files
int i;
for (i=0; i<VP_header.num_files; i++ ) {
VP_FILE find;
if (fread( &find, sizeof(VP_FILE), 1, fp ) != 1) {
mprintf(("Failed to read file entry (currently in directory %s)!\n", search_path.c_str()));
break;
}
find.offset = INTEL_INT( find.offset ); //-V570
find.size = INTEL_INT( find.size ); //-V570
find.write_time = INTEL_INT( find.write_time ); //-V570
find.filename[sizeof(find.filename)-1] = '\0';
if ( find.size == 0 ) {
if ( !stricmp(find.filename, "..")) {
auto end = search_path.rfind(DIR_SEPARATOR_CHAR);
if (end != SCP_string::npos) {
search_path.erase(end);
} else {
search_path.clear();
}
} else {
if ( !search_path.empty() && (search_path.back() != DIR_SEPARATOR_CHAR) ) {
search_path += DIR_SEPARATOR_CHAR;
}
search_path += find.filename;
}
auto rval = cfile_get_path_type(search_path);
// if the pathtype root changed then add all of the files
if (rval != path_type) {
num_files += cf_add_pack_files(root_index, files);
files.clear();
}
path_type = rval;
if ( (path_type > CF_TYPE_DATA) && Pathtypes[path_type].path &&
(search_path.length() > strlen(Pathtypes[path_type].path)) )
{
sub_path = search_path.substr(strlen(Pathtypes[path_type].path)+1) + DIR_SEPARATOR_STR;
} else {
sub_path.clear();
}
//mprintf(( "Current dir = '%s'\n", search_path.c_str() ));
} else {
if (path_type != CF_TYPE_INVALID) {
char *ext = strrchr( find.filename, '.' );
if ( ext ) {
if ( is_ext_in_list( Pathtypes[path_type].extensions, ext ) ) {
// Found a file!!!!
_file_list_t file;
file.name = find.filename;
file.m_time = find.write_time;
file.size = find.size;
file.sub_path = sub_path;
file.pathtype = path_type;
file.offset = find.offset;
files.push_back(file);
//mprintf(( "Found pack file '%s'\n", find.filename ));
}
}
}
}
}
// add final set of files
num_files += cf_add_pack_files(root_index, files);
files.clear();
fclose(fp);
mprintf(( "%i files\n", num_files ));
}
void cf_search_memory_root(int root_index) {
int num_files = 0;
mprintf(( "Searching memory root ... " ));
auto default_files = defaults_get_all();
for (auto& default_file : default_files) {
// Pure built in files have an empty path_type string
if (strlen(default_file.path_type) <= 0) {
// Ignore pure builtin files. They should only be accessible with defaults_get_file
continue;
}
int pathtype = -1;
for (int i = 0; i < CF_MAX_PATH_TYPES; ++i) {
if (Pathtypes[i].path == nullptr) {
continue;
}
if (!strcmp(Pathtypes[i].path, default_file.path_type)) {
pathtype = i;
break;
}
}
Assertion(pathtype != -1, "Default file '%s%s%s' does not use a valid path type!",
default_file.path_type,
DIR_SEPARATOR_STR,
default_file.filename);
cf_file *file = cf_create_file();
file->name_ext = default_file.filename;
file->root_index = root_index;
file->pathtype_index = pathtype;
file->write_time = time(nullptr); // Just assume that memory files were last written to just now
file->size = (int)default_file.size;
file->data = default_file.data;
num_files++;
}
mprintf(( "%i files\n", num_files ));
}
void cf_build_file_list()
{
int i;
Num_files = 0;
// For each root, find all files...
for (i=0; i<Num_roots; i++ ) {
cf_root *root = cf_get_root(i);
if ( root->roottype == CF_ROOTTYPE_PATH ) {
cf_search_root_path(i);
} else if ( root->roottype == CF_ROOTTYPE_PACK ) {
cf_search_root_pack(i);
} else if (root->roottype == CF_ROOTTYPE_MEMORY) {
cf_search_memory_root(i);
}
}
#ifndef NDEBUG
// if some special/critical files might be shadowed then make sure the user knows about it
if ( !critical_shadowed.empty() && !running_unittests ) {
SCP_string shadowed;
const auto count = critical_shadowed.size();
// only report a few of the files so that the warning dialog doesn't get freakishly large
for (size_t j = 0; (j < 5) && (j < count); ++j) {
shadowed += critical_shadowed[j] + '\n';
}
Warning(LOCATION, "Some critical files might be shadowed! Please check the debug log for details.\n\n"
SIZE_T_ARG " file(s) detected, including:\n%s", count, shadowed.c_str());
}
critical_shadowed.clear();
critical_shadowed.shrink_to_fit();
#endif
}
void cf_build_secondary_filelist(const char *cdrom_dir)
{
// Assume no files
cf_free_secondary_filelist();
mprintf(( "Building file index...\n" ));
// build the list of searchable roots
cf_build_root_list(cdrom_dir);
// build the list of files themselves
cf_build_file_list();
mprintf(( "Found %d roots and %d files.\n", Num_roots, Num_files ));
}
void cf_free_secondary_filelist()
{
// Free the root blocks
Root_blocks.clear();
Num_roots = 0;
// Free the file blocks
File_blocks.clear();
Num_files = 0;
}
static bool is_absolute_path(const char *path)
{
if ( !path || !strlen(path) ) {
return false;
}
#ifdef WIN32
return (PathIsRelative(path) == FALSE);
#else
return (*path == '/');
#endif
}
static bool sub_path_match(const SCP_string &search, const SCP_string &index)
{
// if search path is empty then we only care about the file name
if (search.empty()) {
return true;
}
// if we have search but no index then fail
if (index.empty()) {
return false;
}
return !stricmp(search.c_str(), index.c_str());
}
static time_t get_mtime(int fd)
{
#ifdef _WIN32
struct _stat buf;
#define fstat _fstat
#else
struct stat buf;
#endif
if (fstat(fd, &buf) != 0) {
return 0;
}
return buf.st_mtime;
}
/**
* Searches for a file.
*
* @note Follows all rules and precedence and searches CD's and pack files.
*
* @param filespec Filename & extension
* @param pathtype See CF_TYPE_ defines in CFILE.H
* @param location_flags Specifies where to search for the specified flag
*
* @return A structure which describes the found file
*/
CFileLocation cf_find_file_location(const char* filespec, int pathtype, uint32_t location_flags)
{
int i;
uint ui;
int cfs_slow_search = 0;
SCP_string longname;
Assert( (filespec != NULL) && (strlen(filespec) > 0) ); //-V805
// see if we have something other than just a filename
// our current rules say that any file that specifies a direct
// path will try to be opened on that path. If that open
// fails, then we will open the file based on the extension
// of the file
// NOTE: full path should also include localization, if so desired
// do we have a full path already?
if (is_absolute_path(filespec)) {
FILE *fp = fopen(filespec, "rb" );
if (fp) {
CFileLocation res(true);
res.size = static_cast<size_t>(filelength(fileno(fp)));
res.m_time = get_mtime(fileno(fp));
fclose(fp);
auto last_separator = strrchr(filespec, DIR_SEPARATOR_CHAR);
Assertion(last_separator != nullptr, "We have full path, but no separator!?");
res.offset = 0;
res.full_name = filespec;
res.name_ext = last_separator + 1;
return res;
}
return CFileLocation(); // If they give a full path, fail if not found.
}
// Search the hard drive for files first.
uint num_search_dirs = 0;
int search_order[CF_MAX_PATH_TYPES];
if ( CF_TYPE_SPECIFIED(pathtype) ) {
search_order[num_search_dirs++] = pathtype;
} else {
for (i = CF_TYPE_ROOT; i < CF_MAX_PATH_TYPES; i++) {
if (i != pathtype)
search_order[num_search_dirs++] = i;
}
}
for (ui=0; ui<num_search_dirs; ui++ ) {
cfs_slow_search = 0;
switch (search_order[ui])
{
case CF_TYPE_ROOT:
case CF_TYPE_DATA:
case CF_TYPE_PLAYERS:
case CF_TYPE_SINGLE_PLAYERS:
case CF_TYPE_MULTI_PLAYERS:
case CF_TYPE_MULTI_CACHE:
case CF_TYPE_MISSIONS:
case CF_TYPE_CACHE:
cfs_slow_search = 1;
break;
default:
// always hit the disk if we are looking in only one path
cfs_slow_search = (num_search_dirs == 1) ? 1 : 0;
break;
}
if (cfs_slow_search) {
if ( !cf_create_default_path_string(longname, search_order[ui], filespec, location_flags) ) {
continue;
}
FILE *fp = fopen(longname.c_str(), "rb" );
if (fp) {
CFileLocation res(true);
res.size = static_cast<size_t>(filelength( fileno(fp) ));
res.m_time = get_mtime(fileno(fp));
fclose(fp);
res.offset = 0;
res.full_name = longname;
res.name_ext = filespec;
return res;
}
}
}
// fixup filename and sub directory path, if needed
SCP_string filename = filespec;
SCP_string sub_path;
normalize_directory_separators(filename);
auto seperator = filename.rfind(DIR_SEPARATOR_CHAR);
if (seperator != SCP_string::npos) {
sub_path = filename.substr(0, seperator);
sub_path += DIR_SEPARATOR_CHAR;
filename.erase(0, seperator+1);
}
// Search the pak files and CD-ROM.
for (ui = 0; ui < Num_files; ui++ ) {
cf_file *f = cf_get_file(ui);
// only search paths we're supposed to...
if ( (pathtype != CF_TYPE_ANY) && (pathtype != f->pathtype_index) )
continue;
if (location_flags != CF_LOCATION_ALL) {
// If a location flag was specified we need to check if the root of this file satisfies the request
auto root = cf_get_root(f->root_index);
if (!cf_check_location_flags(root->location_flags, location_flags)) {
// Root does not satisfy location flags
continue;
}
}
if ( !sub_path_match(sub_path, f->sub_path) ) {
continue;
}
// file either not localized or localized version not found
if ( !stricmp(filename.c_str(), f->name_ext.c_str()) ) {
CFileLocation res(true);
res.size = static_cast<size_t>(f->size);
res.offset = (size_t)f->pack_offset;
res.data_ptr = f->data;
res.name_ext = f->name_ext;
res.m_time = f->write_time;
if (f->data != nullptr) {
// This is an in-memory file so we just copy the pathtype name + file name
res.full_name = Pathtypes[f->pathtype_index].path;
res.full_name += DIR_SEPARATOR_STR;
res.full_name += f->sub_path;
res.full_name += f->name_ext;
} else if (f->pack_offset < 1) {
// This is a real file, return the actual file path
res.full_name = f->real_name;
} else {
// File is in a pack file
cf_root *r = cf_get_root(f->root_index);
res.full_name = r->path;
}
return res;
}
}
return CFileLocation();
}
// -- from parselo.cpp --
extern char *stristr(char *str, const char *substr);
/**
* Searches for a file.
*
* @note Follows all rules and precedence and searches CD's and pack files. Searches all locations in order for first filename using filter list.
* @note This function is exponentially slow, so don't use it unless truely needed
*
* @param filename Filename & extension
* @param ext_num Number of extensions to look for
* @param ext_list Extension filter list
* @param pathtype See CF_TYPE_ defines in CFILE.H
* @param max_out Maximum string length that should be stuffed into pack_filename
*
* @return A structure containing information about the found file
*/
CFileLocationExt cf_find_file_location_ext(const char *filename, const int ext_num, const char **ext_list, int pathtype)
{
int cur_ext, i;
uint ui;
int cfs_slow_search = 0;
SCP_string longname;
Assert( (filename != NULL) && (strlen(filename) < MAX_FILENAME_LEN) );
Assert( (ext_list != NULL) && (ext_num > 1) ); // if we are searching for just one ext
// then this is the wrong function to use
// if we have a full path already then fail. this function if for searching via filter only!
if (is_absolute_path(filename)) { // do we have a full path already?
Int3();
return CFileLocationExt();
}
// Search the hard drive for files first.
uint num_search_dirs = 0;
int search_order[CF_MAX_PATH_TYPES];
if ( CF_TYPE_SPECIFIED(pathtype) ) {
search_order[num_search_dirs++] = pathtype;
} else {
for (i = CF_TYPE_ROOT; i < CF_MAX_PATH_TYPES; i++)
search_order[num_search_dirs++] = i;
}
// fixup filename and sub directory path, if needed
SCP_string filespec = filename;
SCP_string sub_path;
normalize_directory_separators(filespec);
auto seperator = filespec.rfind(DIR_SEPARATOR_CHAR);
if (seperator != SCP_string::npos) {
sub_path = filespec.substr(0, seperator);
sub_path += DIR_SEPARATOR_CHAR;
filespec.erase(0, seperator+1);
}
// strip any existing extension
// (NOTE: to be fully retail compatible, we need to support multiple periods for something like *_1.5.wav,
// which means that we need to strip a length of >2 only, assuming that all valid ext are at least 2 chars)
auto dot = filespec.rfind('.');
if ( (dot != SCP_string::npos) && ((filespec.length() - dot) > 2) ) {
filespec.erase(dot);
}
SCP_string filespec_ext;
for (ui = 0; ui < num_search_dirs; ui++) {
cfs_slow_search = 0;
// always hit the disk if we are looking in only one path
if (num_search_dirs == 1) {
cfs_slow_search = 1;
}
// otherwise hit based on a directory type
else {
switch (search_order[ui])
{
case CF_TYPE_ROOT:
case CF_TYPE_DATA:
case CF_TYPE_PLAYERS:
case CF_TYPE_SINGLE_PLAYERS:
case CF_TYPE_MULTI_PLAYERS:
case CF_TYPE_MULTI_CACHE:
case CF_TYPE_MISSIONS:
case CF_TYPE_CACHE:
cfs_slow_search = 1;
break;
}
}
if ( !cfs_slow_search )
continue;
for (cur_ext = 0; cur_ext < ext_num; cur_ext++) {
filespec_ext = filespec + ext_list[cur_ext];
if ( !cf_create_default_path_string(longname, search_order[ui], filespec_ext.c_str()) ) {
continue;
}
FILE *fp = fopen(longname.c_str(), "rb" );
if (fp) {
CFileLocationExt res(cur_ext);
res.found = true;
res.size = static_cast<size_t>(filelength( fileno(fp) ));
res.m_time = get_mtime(fileno(fp));
fclose(fp);
res.offset = 0;
res.full_name = longname;
res.name_ext = filespec_ext;
return res;
}
}
}
// Search the pak files and CD-ROM.
// get total length, with extension, which is used to test with later
// (FIXME: this assumes that everything in ext_list[] is the same length!)
size_t filespec_len_big = filespec.length() + strlen(ext_list[0]);
SCP_vector< cf_file* > file_list_index;
int last_root_index = -1;
int last_path_index = -1;
file_list_index.reserve( MIN(ext_num * 4, (int)Num_files) );
// next, run though and pick out base matches
for (ui = 0; ui < Num_files; ui++) {
cf_file *f = cf_get_file(ui);
// ... only search paths that we're supposed to
if ( (num_search_dirs == 1) && (pathtype != f->pathtype_index) )
continue;
// ... match subdirectories (if specified)
if ( !sub_path_match(sub_path, f->sub_path) ) {
continue;
}
// ... check that our names are the same length (accounting for the missing extension on our own name)
if (f->name_ext.length() != filespec_len_big )
continue;
// ... check that we match the base filename
if ( strnicmp(f->name_ext.c_str(), filespec.c_str(), filespec.length()) != 0 )
continue;
// ... make sure that it's one of our supported types
bool found_one = false;
for (cur_ext = 0; cur_ext < ext_num; cur_ext++) {
if ( stristr(f->name_ext.c_str(), ext_list[cur_ext]) ) {
found_one = true;
break;
}
}
if ( !found_one )
continue;
// ... we check based on location, so if location changes after the first find then bail
if (last_root_index == -1) {
last_root_index = f->root_index;
last_path_index = f->pathtype_index;
} else {
if (f->root_index != last_root_index)
break;
if (f->pathtype_index != last_path_index)
break;
}
// ok, we have a good base match, so add it to our cache
file_list_index.push_back( f );
}
// now try and find our preferred match
for (cur_ext = 0; cur_ext < ext_num; cur_ext++) {
for (SCP_vector<cf_file*>::iterator fli = file_list_index.begin(); fli != file_list_index.end(); ++fli) {
cf_file *f = *fli;
filespec_ext = filespec + ext_list[cur_ext];
// file either not localized or localized version not found
if ( !stricmp(filespec_ext.c_str(), f->name_ext.c_str()) ) {
CFileLocationExt res(cur_ext);
res.found = true;
res.size = static_cast<size_t>(f->size);
res.offset = (size_t)f->pack_offset;
res.data_ptr = f->data;
res.name_ext = f->name_ext;
res.m_time = f->write_time;
if (f->data != nullptr) {
// This is an in-memory file so we just copy the pathtype name + file name
res.full_name = Pathtypes[f->pathtype_index].path;
res.full_name += DIR_SEPARATOR_STR;
res.full_name += f->sub_path;
res.full_name += f->name_ext;
} else if (f->pack_offset < 1) {
// This is a real file, return the actual file path
res.full_name = f->real_name;
} else {
// File is in a pack file
cf_root *r = cf_get_root(f->root_index);
res.full_name = r->path;
}
// found it, so cleanup and return
file_list_index.clear();
return res;
}
}
}
return CFileLocationExt();
}
// Returns true if filename matches filespec, else zero if not
int cf_matches_spec(const char *filespec, const char *filename)
{
const char *src_ext;
const char *dst_ext;
src_ext = strrchr(filespec, '*');
if(!src_ext)
{
src_ext = strrchr(filespec, '.');
if (!src_ext)
return 1;
}
else
{
src_ext++;
}
if(strlen(filespec) > strlen(filename))
{
return 0;
}
dst_ext = filename + strlen(filename) - ((filespec + strlen(filespec)) - src_ext);
if (!dst_ext)
return 1;
if(src_ext == filespec)
{
return !stricmp(dst_ext, src_ext);
}
else
{
return (!stricmp(dst_ext, src_ext) && !strnicmp(dst_ext, src_ext, src_ext - filespec));
}
}
int (*Get_file_list_filter)(const char *filename) = NULL;
const char *Get_file_list_child = NULL;
int Skip_packfile_search = 0;
bool Skip_memory_files = false;
static bool verify_file_list_child()
{
if (Get_file_list_child == NULL) {
return false;
}
// empty or too long
size_t len = strlen(Get_file_list_child);
if ( !len || (len > MAX_FILENAME_LEN) ) {
return false;
}
// can not being with directory separator
if (Get_file_list_child[0] == DIR_SEPARATOR_CHAR) {
return false;
}
// no ':' or spaces
if ( strchr(Get_file_list_child, ':') || strchr(Get_file_list_child, ' ') ) {
return false;
}
return true;
}
static int cf_file_already_in_list( SCP_vector<SCP_string> &list, const char *filename )
{
char name_no_extension[MAX_PATH_LEN];
size_t i, size = list.size();
if (size == 0) {
return 0;
}
strcpy_s(name_no_extension, filename );
char *p = strrchr( name_no_extension, '.' );
if ( p ) *p = 0;
for (i = 0; i < size; i++) {
if ( !stricmp(list[i].c_str(), name_no_extension ) ) {
// Match found!
return 1;
}
}
// Not found
return 0;
}
// An alternative cf_get_file_list*(), true dynamic list version.
// This one has a 'type', which is a CF_TYPE_* value. Because this specifies the directory
// location, 'filter' only needs to be the filter itself, with no path information.
// See above descriptions of cf_get_file_list() for more information about how it all works.
// Note that filesystem listing is always sorted by name *before* sorting by the
// provided sort order. This isn't strictly needed on NTFS, which always provides the list
// sorted by name. But on mac/linux it's always needed.
int cf_get_file_list(SCP_vector<SCP_string>& list, int pathtype, const char* _filter, int sort,
SCP_vector<file_list_info>* info, uint32_t location_flags)
{
uint i;
int own_flag = 0;
SCP_vector<file_list_info> my_info;
file_list_info tinfo;
if ( !info && (sort == CF_SORT_TIME) ) {
info = &my_info;
own_flag = 1;
}
SCP_string filespec;
SCP_vector<_file_list_t> files;
// do we have a full path already?
if (is_absolute_path(_filter)) {
// check for full path and only grab matching files from it if specified
auto last_separator = strrchr(_filter, DIR_SEPARATOR_CHAR);
Assertion(Get_file_list_filter == nullptr, "File list filter not supported with absolute paths!");
const char *fltr = last_separator + 1;
filespec = SCP_string(_filter, strlen(_filter) - strlen(last_separator));
cf_get_list_of_files(filespec, files, fltr);
for (auto &file : files) {
SCP_string::size_type pos = file.name.find_last_of('.');
if (pos != SCP_string::npos) {
list.push_back(file.name.substr(0, pos));
} else {
list.push_back(file.name);
}
if (info) {
tinfo.write_time = file.m_time;
info->push_back(tinfo);
}
}
if (sort != CF_SORT_NONE) {
cf_sort_filenames(list, sort, info);
}
return (int)list.size();
}
bool check_duplicates = !list.empty();
if ( check_duplicates && (sort != CF_SORT_NONE) ) {
Int3();
sort = CF_SORT_NONE;
}
if (Get_file_list_child && !verify_file_list_child() ) {
Get_file_list_child = nullptr;
}
cf_create_default_path_string(filespec, pathtype, (char*)Get_file_list_child, location_flags);
// fixup filter and sub directory path, if needed
SCP_string filter = _filter;
SCP_string sub_path;
bool glob = false;
normalize_directory_separators(filter);
auto seperator = filter.rfind(DIR_SEPARATOR_CHAR);
if (seperator != SCP_string::npos) {
sub_path = filter.substr(0, seperator);
if (sub_path == "*") {
glob = true;
} else {
sub_path += DIR_SEPARATOR_CHAR;
}
filter.erase(0, seperator+1);
}
SCP_string fullname;
cf_get_list_of_files(filespec, files, filter.c_str());
for (auto &file : files) {
if ( !glob && !sub_path_match(sub_path, file.sub_path) ) {
continue;
}
auto pos = file.name.rfind('.');
if ( !sub_path.empty() ) {
// prepend file name with sub directory path
// allows us to open the specific file and allows duplicate filenames in different paths
fullname = file.sub_path + file.name.substr(0, pos);
} else {
fullname = file.name.substr(0, pos);
}
if (check_duplicates && cf_file_already_in_list(list, fullname.c_str())) {
continue;
}
if ( !Get_file_list_filter || (*Get_file_list_filter)(file.name.c_str()) ) {
list.push_back(fullname);
if (info) {
tinfo.write_time = file.m_time;
info->push_back(tinfo);
}
}
}
bool skip_packfiles = false;
if ( (pathtype == CF_TYPE_SINGLE_PLAYERS) || (pathtype == CF_TYPE_MULTI_PLAYERS) ) {
skip_packfiles = true;
} else if (Get_file_list_child != NULL) {
skip_packfiles = true;
}
// Search all the packfiles and CD.
if ( !skip_packfiles ) {
for (i=0; i<Num_files; i++ ) {
cf_file * f = cf_get_file(i);
if (Skip_packfile_search && f->pack_offset != 0) {
// If the packfile skip flag is set we skip files in VPs but still search in directories
continue;
}
if (Skip_memory_files && f->data != nullptr) {
// If we want to skip memory files and this is a memory file then ignore it
continue;
}
// only search paths we're supposed to...
if ( (pathtype != CF_TYPE_ANY) && (pathtype != f->pathtype_index) ) {
continue;
}
if (location_flags != CF_LOCATION_ALL) {
// If a location flag was specified we need to check if the root of this file satisfies the request
auto root = cf_get_root(f->root_index);
if (!cf_check_location_flags(root->location_flags, location_flags)) {
// Root does not satisfy location flags
continue;
}
}
if ( !glob && !sub_path_match(sub_path, f->sub_path) ) {
continue;
}
if ( !cf_matches_spec(filter.c_str(), f->name_ext.c_str()) ) {
continue;
}
auto pos = f->name_ext.rfind('.');
if ( !sub_path.empty() ) {
// prepend file name with sub directory path
// allows us to open the specific file and allows duplicate filenames in different paths
fullname = f->sub_path + f->name_ext.substr(0, pos);
} else {
fullname = f->name_ext.substr(0, pos);
}
if ( cf_file_already_in_list(list, fullname.c_str()) ) {
continue;
}
if ( !Get_file_list_filter || (*Get_file_list_filter)(f->name_ext.c_str()) ) {
//mprintf(( "Found '%s' in root %d path %d\n", f->name_ext, f->root_index, f->pathtype_index ));
list.push_back(fullname);
if (info) {
tinfo.write_time = f->write_time;
info->push_back( tinfo );
}
}
}
}
if (sort != CF_SORT_NONE) {
cf_sort_filenames( list, sort, info );
}
if (own_flag) {
my_info.clear();
}
Get_file_list_filter = NULL;
Get_file_list_child = NULL;
return (int)list.size();
}
int cf_file_already_in_list( int num_files, char **list, const char *filename )
{
int i;
char name_no_extension[MAX_PATH_LEN];
strcpy_s(name_no_extension, filename );
char *p = strrchr( name_no_extension, '.' );
if ( p ) *p = 0;
for (i=0; i<num_files; i++ ) {
if ( !stricmp(list[i], name_no_extension ) ) {
// Match found!
return 1;
}
}
// Not found
return 0;
}
// An alternative cf_get_file_list(), dynamic list version.
// This one has a 'type', which is a CF_TYPE_* value. Because this specifies the directory
// location, 'filter' only needs to be the filter itself, with no path information.
// See above descriptions of cf_get_file_list() for more information about how it all works.
int cf_get_file_list(int max, char** list, int pathtype, const char* _filter, int sort, file_list_info* info,
uint32_t location_flags)
{
uint i;
int num_files = 0, own_flag = 0;
if (max < 1) {
Get_file_list_filter = NULL;
return 0;
}
Assert(list);
if (!info && (sort == CF_SORT_TIME)) {
info = (file_list_info *) vm_malloc(sizeof(file_list_info) * max);
own_flag = 1;
}
SCP_string filespec;
if (Get_file_list_child && !verify_file_list_child() ) {
Get_file_list_child = nullptr;
}
cf_create_default_path_string(filespec, pathtype, (char*)Get_file_list_child, location_flags);
// fixup filter and sub directory path, if needed
SCP_string filter = _filter;
SCP_string sub_path;
bool glob = false;
normalize_directory_separators(filter);
auto seperator = filter.rfind(DIR_SEPARATOR_CHAR);
if (seperator != SCP_string::npos) {
sub_path = filter.substr(0, seperator);
if (sub_path == "*") {
glob = true;
} else {
sub_path += DIR_SEPARATOR_CHAR;
}
filter.erase(0, seperator+1);
}
SCP_string fullname;
SCP_vector<_file_list_t> files;
cf_get_list_of_files(filespec, files, filter.c_str());
for (auto &file : files) {
if (num_files >= max) {
break;
}
if ( !glob && !sub_path_match(sub_path, file.sub_path) ) {
continue;
}
auto pos = file.name.rfind('.');
if ( !sub_path.empty() ) {
// prepend file name with sub directory path
// allows us to open the specific file and allows duplicate filenames in different paths
fullname = file.sub_path + file.name.substr(0, pos);
} else {
fullname = file.name.substr(0, pos);
}
if ( !Get_file_list_filter || (*Get_file_list_filter)(file.name.c_str()) ) {
list[num_files] = vm_strdup(fullname.c_str());
if (info) {
info[num_files].write_time = file.m_time;
}
++num_files;
}
}
bool skip_packfiles = false;
if ((pathtype == CF_TYPE_SINGLE_PLAYERS) || (pathtype == CF_TYPE_MULTI_PLAYERS)) {
skip_packfiles = true;
}
else if (Get_file_list_child != NULL) {
skip_packfiles = true;
}
// Search all the packfiles and CD.
if ( !skip_packfiles) {
for (i=0; i<Num_files; i++ ) {
cf_file * f = cf_get_file(i);
if (num_files >= max) {
break;
}
if (Skip_packfile_search && f->pack_offset != 0) {
// If the packfile skip flag is set we skip files in VPs but still search in directories
continue;
}
if (Skip_memory_files && f->data != nullptr) {
// If we want to skip memory files and this is a memory file then ignore it
continue;
}
// only search paths we're supposed to...
if ( (pathtype != CF_TYPE_ANY) && (pathtype != f->pathtype_index) ) {
continue;
}
if (location_flags != CF_LOCATION_ALL) {
// If a location flag was specified we need to check if the root of this file satisfies the request
auto root = cf_get_root(f->root_index);
if (!cf_check_location_flags(root->location_flags, location_flags)) {
// Root does not satisfy location flags
continue;
}
}
if ( !glob && !sub_path_match(sub_path, f->sub_path) ) {
continue;
}
if ( !cf_matches_spec(filter.c_str(), f->name_ext.c_str())) {
continue;
}
auto pos = f->name_ext.rfind('.');
if ( !sub_path.empty() ) {
// prepend file name with sub directory path
// allows us to open the specific file and allows duplicate filenames in different paths
fullname = f->sub_path + f->name_ext.substr(0, pos);
} else {
fullname = f->name_ext.substr(0, pos);
}
if ( cf_file_already_in_list(num_files, list, fullname.c_str()) ) {
continue;
}
if ( !Get_file_list_filter || (*Get_file_list_filter)(f->name_ext.c_str()) ) {
//mprintf(( "Found '%s' in root %d path %d\n", f->name_ext, f->root_index, f->pathtype_index ));
list[num_files] = vm_strdup(fullname.c_str());
if (info) {
info[num_files].write_time = f->write_time;
}
num_files++;
}
}
}
if (sort != CF_SORT_NONE) {
cf_sort_filenames( num_files, list, sort, info );
}
if (own_flag) {
vm_free(info);
}
Get_file_list_filter = NULL;
return num_files;
}
int cf_file_already_in_list_preallocated( int num_files, char arr[][MAX_FILENAME_LEN], const char *filename )
{
int i;
char name_no_extension[MAX_PATH_LEN];
strcpy_s(name_no_extension, filename );
char *p = strrchr( name_no_extension, '.' );
if ( p ) *p = 0;
for (i=0; i<num_files; i++ ) {
if ( !stricmp(arr[i], name_no_extension ) ) {
// Match found!
return 1;
}
}
// Not found
return 0;
}
// An alternative cf_get_file_list(), fixed array version.
// This one has a 'type', which is a CF_TYPE_* value. Because this specifies the directory
// location, 'filter' only needs to be the filter itself, with no path information.
// See above descriptions of cf_get_file_list() for more information about how it all works.
int cf_get_file_list_preallocated(int max, char arr[][MAX_FILENAME_LEN], char** list, int pathtype, const char* _filter,
int sort, file_list_info* info, uint32_t location_flags)
{
int num_files = 0, own_flag = 0;
if (max < 1) {
Get_file_list_filter = NULL;
return 0;
}
if (list) {
for (int i=0; i<max; i++) {
list[i] = arr[i];
}
} else {
sort = CF_SORT_NONE; // sorting of array directly not supported. Sorting done on list only
}
if (!info && (sort == CF_SORT_TIME)) {
info = (file_list_info *) vm_malloc(sizeof(file_list_info) * max);
if ( info )
own_flag = 1;
}
SCP_string filespec;
if (Get_file_list_child && !verify_file_list_child() ) {
Get_file_list_child = nullptr;
}
// Search the default directories
cf_create_default_path_string(filespec, pathtype, (char*)Get_file_list_child, location_flags);
// fixup filter and sub directory path, if needed
SCP_string filter = _filter;
SCP_string sub_path;
normalize_directory_separators(filter);
auto seperator = filter.rfind(DIR_SEPARATOR_CHAR);
if (seperator != SCP_string::npos) {
sub_path = filter.substr(0, seperator);
if (sub_path != "*") {
sub_path += DIR_SEPARATOR_CHAR;
}
filter.erase(0, seperator+1);
// due to the limited filename length we can't reliably add sub paths to it
// so we just strip off the sub path and do a regular search instead, after a warning
Warning(LOCATION, "Subdirectory searches aren't available for cf_get_file_list_preallocated()!");
}
SCP_vector<_file_list_t> files;
cf_get_list_of_files(filespec, files, filter.c_str());
for (auto &file : files) {
if (num_files >= max) {
break;
}
if (file.name.length() >= MAX_FILENAME_LEN) {
continue;
}
if ( !Get_file_list_filter || (*Get_file_list_filter)(file.name.c_str()) ) {
SDL_strlcpy(arr[num_files], file.name.c_str(), MAX_FILENAME_LEN);
char *ptr = strrchr(arr[num_files], '.');
if (ptr) {
*ptr = 0;
}
if (info) {
info[num_files].write_time = file.m_time;
}
++num_files;
}
}
bool skip_packfiles = false;
if ((pathtype == CF_TYPE_SINGLE_PLAYERS) || (pathtype == CF_TYPE_MULTI_PLAYERS)) {
skip_packfiles = true;
}
else if (Get_file_list_child != NULL) {
skip_packfiles = true;
}
// Search all the packfiles and CD.
if (!skip_packfiles) {
for (uint i=0; i<Num_files; i++ ) {
cf_file * f = cf_get_file(i);
// only search paths we're supposed to...
if ( (pathtype != CF_TYPE_ANY) && (pathtype != f->pathtype_index) ) {
continue;
}
if (location_flags != CF_LOCATION_ALL) {
// If a location flag was specified we need to check if the root of this file satisfies the request
auto root = cf_get_root(f->root_index);
if (!cf_check_location_flags(root->location_flags, location_flags)) {
// Root does not satisfy location flags
continue;
}
}
if (num_files >= max)
break;
if ( !cf_matches_spec(filter.c_str(), f->name_ext.c_str()) ) {
continue;
}
if ( cf_file_already_in_list_preallocated(num_files, arr, f->name_ext.c_str()) ) {
continue;
}
if (Skip_packfile_search && f->pack_offset != 0) {
// If the packfile skip flag is set we skip files in VPs but still search in directories
continue;
}
if (Skip_memory_files && f->data != nullptr) {
// If we want to skip memory files and this is a memory file then ignore it
continue;
}
if ( !Get_file_list_filter || (*Get_file_list_filter)(f->name_ext.c_str()) ) {
//mprintf(( "Found '%s' in root %d path %d\n", f->name_ext, f->root_index, f->pathtype_index ));
strncpy(arr[num_files], f->name_ext.c_str(), MAX_FILENAME_LEN - 1 );
char *ptr = strrchr(arr[num_files], '.');
if ( ptr ) {
*ptr = 0;
}
if (info) {
info[num_files].write_time = f->write_time;
}
num_files++;
}
}
}
if (sort != CF_SORT_NONE) {
Assert(list);
cf_sort_filenames( num_files, list, sort, info );
}
if (own_flag) {
vm_free(info);
}
Get_file_list_filter = NULL;
return num_files;
}
// Returns the default storage path for files given a
// particular pathtype. In other words, the path to
// the unpacked, non-cd'd, stored on hard drive path.
// If filename isn't null it will also tack the filename
// on the end, creating a completely valid filename.
// Input: pathtype - CF_TYPE_??
// path_max - Maximum characters in the path
// filename - optional, if set, tacks the filename onto end of path.
// Output: path - Fully qualified pathname.
//Returns 0 if the result would be too long (invalid result)
int cf_create_default_path_string(char* path, uint path_max, int pathtype, const char* filename, uint32_t _location_flags)
{
SCP_string fullpath;
cf_create_default_path_string(fullpath, pathtype, filename, _location_flags);
// if truncation would occur, return error
if (fullpath.length() >= path_max) {
if (path_max > 0) {
*path = '\0';
}
return 0;
}
strcpy_s(path, path_max, fullpath.c_str());
return 1;
}
// Returns the default storage path for files given a
// particular pathtype. In other words, the path to
// the unpacked, non-cd'd, stored on hard drive path.
// If filename isn't null it will also tack the filename
// on the end, creating a completely valid filename.
// Input: pathtype - CF_TYPE_??
// filename - optional, if set, tacks the filename onto end of path.
// Output: path - Fully qualified pathname.
//Returns 0 if the result would be too long (invalid result)
int cf_create_default_path_string(SCP_string& path, int pathtype, const char* filename, uint32_t _location_flags)
{
uint32_t location_flags = _location_flags;
if ( filename && is_absolute_path(filename)) {
// Already has full path
path.assign(filename);
} else {
cf_root* root = nullptr;
// override location flags for path types which should always come from the root (NOT mod directories)
// NOTE: cache directories should NOT be added here to avoid possible mod breakage
switch(pathtype) {
case CF_TYPE_PLAYERS:
case CF_TYPE_MULTI_PLAYERS:
case CF_TYPE_SINGLE_PLAYERS:
case CF_TYPE_PLAYER_BINDS:
location_flags = CF_LOCATION_ROOT_USER | CF_LOCATION_ROOT_GAME | CF_LOCATION_TYPE_ROOT;
break;
}
for (auto i = 0; i < Num_roots; ++i) {
auto current_root = cf_get_root(i);
if (current_root->roottype != CF_ROOTTYPE_PATH) {
// We want a "real" path here so only path roots are valid
continue;
}
if (cf_check_location_flags(current_root->location_flags, location_flags)) {
// We found a valid root
root = current_root;
break;
}
}
if (!root) {
Assert( filename != NULL );
path.assign(filename);
return 1;
}
Assert(CF_TYPE_SPECIFIED(pathtype));
path = root->path;
path += cf_get_root_pathtype(root, pathtype);
// Don't add slash for root directory
if (Pathtypes[pathtype].path[0] != '\0') {
if (path.back() != DIR_SEPARATOR_CHAR) {
path += DIR_SEPARATOR_CHAR;
}
}
// add filename
if (filename) {
path += filename;
}
}
return 1;
}
void cfile_spew_pack_file_crcs()
{
int i;
char datetime[45];
uint chksum = 0;
time_t my_time;
FILE *out = fopen(os_get_config_path("vp_crcs.txt").c_str(), "w");
if (out == NULL) {
Int3();
return;
}
my_time = time(NULL);
memset( datetime, 0, sizeof(datetime) );
snprintf(datetime, sizeof(datetime)-1, "%s", ctime(&my_time));
// ctime() adds a newline char, so we have to strip it off
datetime[strlen(datetime)-1] = '\0';
fprintf(out, "Pack file CRC log (%s) ... \n", datetime);
fprintf(out, "-------------------------------------------------------------------------------\n");
for (i = 0; i < Num_roots; i++) {
cf_root *cur_root = cf_get_root(i);
if (cur_root->roottype != CF_ROOTTYPE_PACK)
continue;
chksum = 0;
cf_chksum_pack(cur_root->path.c_str(), &chksum, true);
fprintf(out, " %s -- 0x%x\n", cur_root->path.c_str(), chksum);
}
fprintf(out, "-------------------------------------------------------------------------------\n");
fclose(out);
}
bool cf_check_location_flags(uint32_t check_flags, uint32_t desired_flags)
{
Assertion((check_flags & CF_LOCATION_ROOT_MASK) != 0, "check_flags must have a valid root value");
Assertion((check_flags & CF_LOCATION_TYPE_MASK) != 0, "check_flags must have a valid type value");
auto check_root = check_flags & CF_LOCATION_ROOT_MASK;
auto desired_root_flags = desired_flags & CF_LOCATION_ROOT_MASK;
// If the root part is not set then assume that every root matches
if (desired_root_flags != 0 && (check_root & desired_root_flags) == 0) {
return false;
}
auto check_type = check_flags & CF_LOCATION_TYPE_MASK;
auto desired_type_flags = desired_flags & CF_LOCATION_TYPE_MASK;
if (desired_type_flags != 0 && (check_type & desired_type_flags) == 0) {
return false;
}
return true;
}
|