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
|
/*
Copyright (C) 1997-2001 Id Software, Inc.
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.
*/
#include "com_local.h"
#include "files.h"
#include "sys_public.h"
#include "cl_public.h"
#include "d_pak.h"
#if USE_ZLIB
#include <zlib.h>
#include "unzip.h"
#endif
#include <errno.h>
/*
=============================================================================
QUAKE FILESYSTEM
- transparently merged from several sources
- relative to the single virtual root
- case insensitive at pakfiles level,
but may be case sensitive at host OS level
- uses / as path separators internally
=============================================================================
*/
#define MAX_FILES_IN_PK2 0x4000
#define MAX_FILE_HANDLES 8
// macros for dealing portably with files at OS level
#ifdef _WIN32
#define FS_strcmp Q_strcasecmp
#define FS_strncmp Q_strncasecmp
#else
#define FS_strcmp strcmp
#define FS_strncmp strncmp
#endif
#define MAX_READ 0x40000 // read in blocks of 256k
#define MAX_WRITE 0x40000 // write in blocks of 256k
//
// in memory
//
typedef struct packfile_s {
char *name;
size_t filepos;
size_t filelen;
struct packfile_s *hashNext;
} packfile_t;
typedef struct pack_s {
#if USE_ZLIB
unzFile zFile;
#endif
FILE *fp;
int numfiles;
packfile_t *files;
packfile_t **fileHash;
int hashSize;
char filename[1];
} pack_t;
typedef struct searchpath_s {
struct searchpath_s *next;
int mode;
struct pack_s *pack; // only one of filename / pack will be used
char filename[1];
} searchpath_t;
typedef enum fsFileType_e {
FS_FREE,
FS_REAL,
FS_PAK,
#if USE_ZLIB
FS_PK2,
FS_GZIP,
#endif
FS_BAD
} fsFileType_t;
typedef struct fsFile_s {
fsFileType_t type;
unsigned mode;
FILE *fp;
#if USE_ZLIB
void *zfp;
#endif
packfile_t *pak;
qboolean unique;
size_t length;
} fsFile_t;
typedef struct fsLink_s {
char *target;
size_t targlen, namelen;
struct fsLink_s *next;
char name[1];
} fsLink_t;
// these point to user home directory
static char fs_gamedir[MAX_OSPATH];
//static char fs_basedir[MAX_OSPATH];
static cvar_t *fs_debug;
static cvar_t *fs_restrict_mask;
static searchpath_t *fs_searchpaths;
static searchpath_t *fs_base_searchpaths;
static fsLink_t *fs_links;
static fsFile_t fs_files[MAX_FILE_HANDLES];
static qboolean fs_fileFromPak;
static int fs_count_read, fs_count_strcmp, fs_count_open;
cvar_t *fs_game;
/*
All of Quake's data access is through a hierchal file system,
but the contents of the file system can be transparently merged from several sources.
The "base directory" is the path to the directory holding the quake.exe and all game directories.
The sys_* files pass this to host_init in quakeparms_t->basedir.
This can be overridden with the "-basedir" command line parm to allow code debugging in a different directory.
The base directory is only used during filesystem initialization.
The "game directory" is the first tree on the search path and directory that
all generated files (savegames, screenshots, demos, config files) will be saved to.
This can be overridden with the "-game" command line parameter.
The game directory can never be changed while quake is executing.
This is a precacution against having a malicious server instruct clients to write files over areas they shouldn't.
*/
/*
================
FS_pathcmp
Portably compares quake paths
================
*/
int FS_pathcmp( const char *s1, const char *s2 ) {
int c1, c2;
do {
c1 = *s1++;
c2 = *s2++;
if( c1 != c2 ) {
c1 = c1 == '\\' ? '/' : Q_tolower( c1 );
c2 = c2 == '\\' ? '/' : Q_tolower( c2 );
if( c1 < c2 )
return -1;
if( c1 > c2 )
return 1; /* strings not equal */
}
} while( c1 );
return 0; /* strings are equal */
}
int FS_pathcmpn( const char *s1, const char *s2, size_t n ) {
int c1, c2;
do {
c1 = *s1++;
c2 = *s2++;
if( !n-- )
return 0; /* strings are equal until end point */
if( c1 != c2 ) {
c1 = c1 == '\\' ? '/' : Q_tolower( c1 );
c2 = c2 == '\\' ? '/' : Q_tolower( c2 );
if( c1 < c2 )
return -1;
if( c1 > c2 )
return 1; /* strings not equal */
}
} while( c1 );
return 0; /* strings are equal */
}
/*
================
FS_DPrintf
================
*/
static void FS_DPrintf( char *format, ... ) {
va_list argptr;
char string[MAXPRINTMSG];
if( !fs_debug || !fs_debug->integer ) {
return;
}
va_start( argptr, format );
Q_vsnprintf( string, sizeof( string ), format, argptr );
va_end( argptr );
Com_Printf( S_COLOR_CYAN "%s", string );
}
/*
================
FS_AllocHandle
================
*/
static fsFile_t *FS_AllocHandle( fileHandle_t *f ) {
fsFile_t *file;
int i;
for( i = 0, file = fs_files; i < MAX_FILE_HANDLES; i++, file++ ) {
if( file->type == FS_FREE ) {
break;
}
}
if( i == MAX_FILE_HANDLES ) {
Com_Error( ERR_FATAL, "%s: none free", __func__ );
}
*f = i + 1;
return file;
}
/*
================
FS_FileForHandle
================
*/
static fsFile_t *FS_FileForHandle( fileHandle_t f ) {
fsFile_t *file;
if( f <= 0 || f >= MAX_FILE_HANDLES + 1 ) {
Com_Error( ERR_FATAL, "%s: invalid handle: %i", __func__, f );
}
file = &fs_files[f - 1];
if( file->type <= FS_FREE || file->type >= FS_BAD ) {
Com_Error( ERR_FATAL, "%s: invalid file type: %i", __func__, file->type );
}
return file;
}
/*
================
FS_ValidatePath
================
*/
static qboolean FS_ValidatePath( const char *s ) {
const char *start;
// check for leading slash
// check for empty path
if( *s == '/' || *s == '\\' /*|| *s == 0*/ ) {
return qfalse;
}
start = s;
while( *s ) {
// check for ".."
if( *s == '.' && s[1] == '.' ) {
return qfalse;
}
if( *s == '/' || *s == '\\' ) {
// check for two slashes in a row
// check for trailing slash
if( ( s[1] == '/' || s[1] == '\\' || s[1] == 0 ) ) {
return qfalse;
}
}
#ifdef _WIN32
if( *s == ':' ) {
// check for "X:\"
if( s[1] == '\\' || s[1] == '/' ) {
return qfalse;
}
}
#endif
s++;
}
// check length
if( s - start > MAX_OSPATH ) {
return qfalse;
}
return qtrue;
}
#ifdef _WIN32
/*
================
FS_ReplaceSeparators
================
*/
char *FS_ReplaceSeparators( char *s, int separator ) {
char *p;
p = s;
while( *p ) {
if( *p == '/' || *p == '\\' ) {
*p = separator;
}
p++;
}
return s;
}
#endif
/*
================
FS_GetFileLength
Returns current length for files opened for writing.
Returns cached length for files opened for reading.
Returns compressed length for GZIP files.
================
*/
size_t FS_GetFileLength( fileHandle_t f ) {
fsFile_t *file = FS_FileForHandle( f );
fsFileInfo_t info;
switch( file->type ) {
case FS_REAL:
if( !Sys_GetFileInfo( file->fp, &info ) ) {
return INVALID_LENGTH;
}
return info.size;
case FS_PAK:
return file->length;
#if USE_ZLIB
case FS_PK2:
return file->length;
case FS_GZIP:
return INVALID_LENGTH;
#endif
default:
Com_Error( ERR_FATAL, "%s: bad file type", __func__ );
}
return INVALID_LENGTH;
}
/*
============
FS_CreatePath
Creates any directories needed to store the given filename.
Expects a fully qualified quake path (i.e. with / separators).
============
*/
static void FS_CreatePath( char *path ) {
char *ofs;
if( !*path ) {
return;
}
for( ofs = path + 1; *ofs; ofs++ ) {
if( *ofs == '/' ) {
// create the directory
*ofs = 0;
Q_mkdir( path );
*ofs = '/';
}
}
}
qboolean FS_FilterFile( fileHandle_t f ) {
#if USE_ZLIB
fsFile_t *file = FS_FileForHandle( f );
int mode;
char *modeStr;
void *zfp;
switch( file->type ) {
case FS_GZIP:
return qtrue;
case FS_REAL:
break;
default:
return qfalse;
}
mode = file->mode & FS_MODE_MASK;
switch( mode ) {
case FS_MODE_READ:
modeStr = "rb";
break;
case FS_MODE_WRITE:
modeStr = "wb";
break;
default:
return qfalse;
}
fseek( file->fp, 0, SEEK_SET );
zfp = gzdopen( fileno( file->fp ), modeStr );
if( !zfp ) {
return qfalse;
}
file->zfp = zfp;
file->type = FS_GZIP;
return qtrue;
#else
return qfalse;
#endif
}
/*
==============
FS_FCloseFile
==============
*/
void FS_FCloseFile( fileHandle_t f ) {
fsFile_t *file = FS_FileForHandle( f );
FS_DPrintf( "%s: %u\n", __func__, f );
switch( file->type ) {
case FS_REAL:
fclose( file->fp );
break;
case FS_PAK:
if( file->unique ) {
fclose( file->fp );
}
break;
#if USE_ZLIB
case FS_GZIP:
gzclose( file->zfp );
break;
case FS_PK2:
unzCloseCurrentFile( file->zfp );
if( file->unique ) {
unzClose( file->zfp );
}
break;
#endif
default:
break;
}
// don't clear name and mode, in case
// this handle will be reopened later
file->type = FS_FREE;
file->fp = NULL;
#if USE_ZLIB
file->zfp = NULL;
#endif
file->pak = NULL;
file->unique = qfalse;
}
/*
============
FS_FOpenFileWrite
============
*/
static size_t FS_FOpenFileWrite( fsFile_t *file, const char *name ) {
char fullpath[MAX_OSPATH];
FILE *fp;
char *modeStr;
unsigned mode;
size_t len;
if( !FS_ValidatePath( name ) ) {
FS_DPrintf( "%s: refusing invalid path: %s\n", __func__, name );
return INVALID_LENGTH;
}
if( ( file->mode & FS_PATH_MASK ) == FS_PATH_BASE ) {
if( sys_homedir->string[0] ) {
len = Q_concat( fullpath, sizeof( fullpath ),
sys_homedir->string, "/" BASEGAME "/", name, NULL );
} else {
len = Q_concat( fullpath, sizeof( fullpath ),
sys_basedir->string, "/" BASEGAME "/", name, NULL );
}
} else {
len = Q_concat( fullpath, sizeof( fullpath ),
fs_gamedir, "/", name, NULL );
}
if( len >= sizeof( fullpath ) ) {
FS_DPrintf( "%s: refusing oversize path: %s\n", __func__, name );
return INVALID_LENGTH;
}
mode = file->mode & FS_MODE_MASK;
switch( mode ) {
case FS_MODE_APPEND:
modeStr = "ab";
break;
case FS_MODE_WRITE:
modeStr = "wb";
break;
case FS_MODE_RDWR:
modeStr = "r+b";
break;
default:
Com_Error( ERR_FATAL, "%s: %s: invalid mode mask",
__func__, name );
modeStr = NULL;
break;
}
FS_CreatePath( fullpath );
fp = fopen( fullpath, modeStr );
if( !fp ) {
FS_DPrintf( "%s: %s: fopen(%s): %s\n",
__func__, fullpath, modeStr, strerror( errno ) );
return INVALID_LENGTH;
}
#ifdef __unix__
if( !Sys_GetFileInfo( fp, NULL ) ) {
FS_DPrintf( "%s: %s: couldn't get info\n",
__func__, fullpath );
fclose( fp );
return INVALID_LENGTH;
}
#endif
FS_DPrintf( "%s: %s: succeeded\n", __func__, fullpath );
file->fp = fp;
file->type = FS_REAL;
file->length = 0;
file->unique = qtrue;
if( mode == FS_MODE_WRITE ) {
return 0;
}
if( mode == FS_MODE_RDWR ) {
fseek( fp, 0, SEEK_END );
}
return ( size_t )ftell( fp );
}
static size_t FS_FOpenFromPak( fsFile_t *file, pack_t *pak, packfile_t *entry, qboolean unique ) {
fs_fileFromPak = qtrue;
// open a new file on the pakfile
#if USE_ZLIB
if( pak->zFile ) {
void *zfp;
if( unique ) {
zfp = unzReOpen( pak->filename, pak->zFile );
if( !zfp ) {
Com_Error( ERR_FATAL, "%s: couldn't reopen %s",
__func__, pak->filename );
}
} else {
zfp = pak->zFile;
}
if( unzSetCurrentFileInfoPosition( zfp, entry->filepos ) == -1 ) {
Com_Error( ERR_FATAL, "%s: couldn't seek into %s",
__func__, pak->filename );
}
if( unzOpenCurrentFile( zfp ) != UNZ_OK ) {
Com_Error( ERR_FATAL, "%s: couldn't open curfile from %s",
__func__, pak->filename );
}
file->zfp = zfp;
file->type = FS_PK2;
} else
#endif
{
FILE *fp;
if( unique ) {
fp = fopen( pak->filename, "rb" );
if( !fp ) {
Com_Error( ERR_FATAL, "%s: couldn't reopen %s",
__func__, pak->filename );
}
} else {
fp = pak->fp;
}
if( fseek( fp, entry->filepos, SEEK_SET ) == -1 ) {
Com_Error( ERR_FATAL, "%s: couldn't seek into %s",
__func__, pak->filename );
}
file->fp = fp;
file->type = FS_PAK;
}
file->pak = entry;
file->length = entry->filelen;
file->unique = unique;
FS_DPrintf( "%s: %s/%s: succeeded\n",
__func__, pak->filename, entry->name );
return file->length;
}
/*
===========
FS_FOpenFileRead
Finds the file in the search path.
Fills fsFile_t and returns file length.
In case of GZIP files, returns *raw* (compressed) length!
Used for streaming data out of either a pak file or
a seperate file.
===========
*/
static size_t FS_FOpenFileRead( fsFile_t *file, const char *name, qboolean unique ) {
char fullpath[MAX_OSPATH];
searchpath_t *search;
pack_t *pak;
unsigned hash;
packfile_t *entry;
FILE *fp;
fsFileInfo_t info;
int valid = -1;
size_t len;
fs_fileFromPak = qfalse;
fs_count_read++;
//
// search through the path, one element at a time
//
hash = Com_HashPath( name, 0 );
for( search = fs_searchpaths; search; search = search->next ) {
if( file->mode & FS_PATH_MASK ) {
if( ( file->mode & search->mode & FS_PATH_MASK ) == 0 ) {
continue;
}
}
// is the element a pak file?
if( search->pack ) {
if( ( file->mode & FS_TYPE_MASK ) == FS_TYPE_REAL ) {
continue;
}
// look through all the pak file elements
pak = search->pack;
entry = pak->fileHash[ hash & ( pak->hashSize - 1 ) ];
for( ; entry; entry = entry->hashNext ) {
fs_count_strcmp++;
if( !FS_pathcmp( entry->name, name ) ) {
// found it!
return FS_FOpenFromPak( file, pak, entry, unique );
}
}
} else {
if( ( file->mode & FS_TYPE_MASK ) == FS_TYPE_PAK ) {
continue;
}
if( valid == -1 ) {
if( !FS_ValidatePath( name ) ) {
FS_DPrintf( "%s: refusing invalid path: %s\n",
__func__, name );
valid = 0;
}
}
if( valid == 0 ) {
continue;
}
// check a file in the directory tree
len = Q_concat( fullpath, sizeof( fullpath ),
search->filename, "/", name, NULL );
if( len >= sizeof( fullpath ) ) {
FS_DPrintf( "%s: refusing oversize path: %s\n",
__func__, name );
continue;
}
fs_count_open++;
fp = fopen( fullpath, "rb" );
if( !fp ) {
continue;
}
if( !Sys_GetFileInfo( fp, &info ) ) {
FS_DPrintf( "%s: %s: couldn't get info\n",
__func__, fullpath );
fclose( fp );
continue;
}
file->fp = fp;
file->type = FS_REAL;
file->unique = qtrue;
file->length = info.size;
FS_DPrintf( "%s: %s: succeeded\n", __func__, fullpath );
return file->length;
}
}
FS_DPrintf( "%s: %s: not found\n", __func__, name );
return INVALID_LENGTH;
}
/*
=================
FS_LastFileFromPak
=================
*/
qboolean FS_LastFileFromPak( void ) {
return fs_fileFromPak;
}
/*
=================
FS_ReadFile
Properly handles partial reads
=================
*/
size_t FS_Read( void *buffer, size_t len, fileHandle_t hFile ) {
size_t block, remaining = len, read = 0;
byte *buf = (byte *)buffer;
fsFile_t *file = FS_FileForHandle( hFile );
// read in chunks for progress bar
while( remaining ) {
block = remaining;
if( block > MAX_READ )
block = MAX_READ;
switch( file->type ) {
case FS_REAL:
case FS_PAK:
read = fread( buf, 1, block, file->fp );
break;
#if USE_ZLIB
case FS_GZIP:
read = gzread( file->zfp, buf, block );
break;
case FS_PK2:
read = unzReadCurrentFile( file->zfp, buf, block );
break;
#endif
default:
break;
}
if( read == 0 ) {
return len - remaining;
}
if( read > block ) {
Com_Error( ERR_FATAL, "FS_Read: %"PRIz" bytes read", read );
}
remaining -= read;
buf += read;
}
return len;
}
size_t FS_ReadLine( fileHandle_t f, char *buffer, int size ) {
fsFile_t *file = FS_FileForHandle( f );
char *s;
size_t len;
if( file->type != FS_REAL ) {
return 0;
}
do {
s = fgets( buffer, size, file->fp );
if( !s ) {
return 0;
}
len = strlen( s );
} while( len < 2 );
s[ len - 1 ] = 0;
return len - 1;
}
void FS_Flush( fileHandle_t f ) {
fsFile_t *file = FS_FileForHandle( f );
switch( file->type ) {
case FS_REAL:
fflush( file->fp );
break;
#if USE_ZLIB
case FS_GZIP:
gzflush( file->zfp, Z_SYNC_FLUSH );
break;
#endif
default:
break;
}
}
/*
=================
FS_Write
Properly handles partial writes
=================
*/
size_t FS_Write( const void *buffer, size_t len, fileHandle_t hFile ) {
size_t block, remaining = len, write = 0;
byte *buf = (byte *)buffer;
fsFile_t *file = FS_FileForHandle( hFile );
// read in chunks for progress bar
while( remaining ) {
block = remaining;
if( block > MAX_WRITE )
block = MAX_WRITE;
switch( file->type ) {
case FS_REAL:
write = fwrite( buf, 1, block, file->fp );
break;
#if USE_ZLIB
case FS_GZIP:
write = gzwrite( file->zfp, buf, block );
break;
#endif
default:
Com_Error( ERR_FATAL, "FS_Write: illegal file type" );
break;
}
if( write == 0 ) {
return len - remaining;
}
if( write > block ) {
Com_Error( ERR_FATAL, "FS_Write: %"PRIz" bytes written", write );
}
remaining -= write;
buf += write;
}
if( ( file->mode & FS_FLUSH_MASK ) == FS_FLUSH_SYNC ) {
switch( file->type ) {
case FS_REAL:
fflush( file->fp );
break;
#if USE_ZLIB
case FS_GZIP:
gzflush( file->zfp, Z_SYNC_FLUSH );
break;
#endif
default:
break;
}
}
return len;
}
static char *FS_ExpandLinks( const char *filename ) {
static char buffer[MAX_OSPATH];
fsLink_t *l;
size_t length;
length = strlen( filename );
for( l = fs_links; l; l = l->next ) {
if( l->namelen > length ) {
continue;
}
if( !FS_pathcmpn( l->name, filename, l->namelen ) ) {
if( l->targlen + length - l->namelen >= MAX_OSPATH ) {
FS_DPrintf( "%s: %s: MAX_OSPATH exceeded\n", __func__, filename );
return ( char * )filename;
}
memcpy( buffer, l->target, l->targlen );
memcpy( buffer + l->targlen, filename + l->namelen,
length - l->namelen + 1 );
FS_DPrintf( "%s: %s --> %s\n", __func__, filename, buffer );
return buffer;
}
}
return ( char * )filename;
}
/*
============
FS_FOpenFile
============
*/
size_t FS_FOpenFile( const char *name, fileHandle_t *f, int mode ) {
fsFile_t *file;
fileHandle_t hFile;
size_t ret = INVALID_LENGTH;
if( !name || !f ) {
Com_Error( ERR_FATAL, "%s: NULL", __func__ );
}
*f = 0;
if( !fs_searchpaths ) {
return ret; // not yet initialized
}
if( *name == '/' ) {
name++;
}
if( ( mode & FS_MODE_MASK ) == FS_MODE_READ ) {
name = FS_ExpandLinks( name );
}
// allocate new file handle
file = FS_AllocHandle( &hFile );
file->mode = mode;
mode &= FS_MODE_MASK;
switch( mode ) {
case FS_MODE_READ:
ret = FS_FOpenFileRead( file, name, qtrue );
break;
case FS_MODE_WRITE:
case FS_MODE_APPEND:
case FS_MODE_RDWR:
ret = FS_FOpenFileWrite( file, name );
break;
default:
Com_Error( ERR_FATAL, "%s: illegal mode: %u", __func__, mode );
break;
}
// if succeeded, store file handle
if( ret != -1 ) {
*f = hFile;
}
return ret;
}
/*
============
FS_Tell
============
*/
int FS_Tell( fileHandle_t f ) {
fsFile_t *file = FS_FileForHandle( f );
int length;
switch( file->type ) {
case FS_REAL:
length = ftell( file->fp );
break;
case FS_PAK:
length = ftell( file->fp ) - file->pak->filepos;
break;
#if USE_ZLIB
case FS_GZIP:
length = gztell( file->zfp );
break;
case FS_PK2:
length = unztell( file->zfp );
break;
#endif
default:
length = -1;
break;
}
return length;
}
/*
============
FS_RawTell
============
*/
int FS_RawTell( fileHandle_t f ) {
fsFile_t *file = FS_FileForHandle( f );
int length;
switch( file->type ) {
case FS_REAL:
length = ftell( file->fp );
break;
case FS_PAK:
length = ftell( file->fp ) - file->pak->filepos;
break;
#if USE_ZLIB
case FS_GZIP:
length = ftell( file->fp );
break;
case FS_PK2:
length = unztell( file->zfp );
break;
#endif
default:
length = -1;
break;
}
return length;
}
#define MAX_LOAD_BUFFER 0x100000 // 1 MiB
// static buffer for small, stacked file loads and temp allocations
// the last allocation may be easily undone
static byte loadBuffer[MAX_LOAD_BUFFER];
static byte *loadLast;
static size_t loadSaved;
static size_t loadInuse;
static int loadStack;
// for statistics
static int loadCount;
static int loadCountStatic;
/*
============
FS_LoadFile
Filenames are relative to the quake search path
a null buffer will just return the file length without loading
============
*/
size_t FS_LoadFileEx( const char *path, void **buffer, int flags, memtag_t tag ) {
fsFile_t *file;
fileHandle_t f;
byte *buf;
size_t len;
if( !path ) {
Com_Error( ERR_FATAL, "%s: NULL", __func__ );
}
if( buffer ) {
*buffer = NULL;
}
if( !fs_searchpaths ) {
return INVALID_LENGTH; // not yet initialized
}
if( *path == '/' ) {
path++;
}
path = FS_ExpandLinks( path );
// allocate new file handle
file = FS_AllocHandle( &f );
flags &= ~FS_MODE_MASK;
file->mode = flags | FS_MODE_READ;
// look for it in the filesystem or pack files
len = FS_FOpenFileRead( file, path, qfalse );
if( len == INVALID_LENGTH ) {
return len;
}
if( buffer ) {
#if USE_ZLIB
if( file->type == FS_GZIP ) {
len = INVALID_LENGTH; // unknown length
} else
#endif
{
if( tag == TAG_FREE ) {
buf = FS_AllocTempMem( len + 1 );
} else {
buf = Z_TagMalloc( len + 1, tag );
}
if( FS_Read( buf, len, f ) == len ) {
*buffer = buf;
buf[len] = 0;
} else {
Com_EPrintf( "%s: error reading file: %s\n", __func__, path );
if( tag == TAG_FREE ) {
FS_FreeFile( buf );
} else {
Z_Free( buf );
}
len = INVALID_LENGTH;
}
}
}
FS_FCloseFile( f );
return len;
}
size_t FS_LoadFile( const char *path, void **buffer ) {
return FS_LoadFileEx( path, buffer, 0, TAG_FREE );
}
void *FS_AllocTempMem( size_t length ) {
byte *buf;
if( loadInuse + length <= MAX_LOAD_BUFFER && !( fs_restrict_mask->integer & 16 ) ) {
buf = &loadBuffer[loadInuse];
loadLast = buf;
loadSaved = loadInuse;
loadInuse += length;
loadInuse = ( loadInuse + 31 ) & ~31;
loadStack++;
loadCountStatic++;
} else {
// Com_Printf(S_COLOR_MAGENTA"alloc %d\n",length);
buf = FS_Malloc( length );
loadCount++;
}
return buf;
}
/*
=============
FS_FreeFile
=============
*/
void FS_FreeFile( void *buffer ) {
if( !buffer ) {
Com_Error( ERR_FATAL, "%s: NULL", __func__ );
}
if( ( byte * )buffer >= loadBuffer && ( byte * )buffer < loadBuffer + MAX_LOAD_BUFFER ) {
if( loadStack == 0 ) {
Com_Error( ERR_FATAL, "%s: empty load stack", __func__ );
}
loadStack--;
if( loadStack == 0 ) {
loadInuse = 0;
// Com_Printf(S_COLOR_MAGENTA"clear\n");
} else if( buffer == loadLast ) {
loadInuse = loadSaved;
// Com_Printf(S_COLOR_MAGENTA"partial\n");
}
} else {
Z_Free( buffer );
}
}
#if USE_CLIENT
/*
================
FS_RenameFile
================
*/
qboolean FS_RenameFile( const char *from, const char *to ) {
char frompath[MAX_OSPATH];
char topath[MAX_OSPATH];
size_t len;
if( *from == '/' ) {
from++;
}
if( !FS_ValidatePath( from ) ) {
FS_DPrintf( "%s: refusing invalid source path: %s\n", __func__, from );
return qfalse;
}
len = Q_concat( frompath, sizeof( frompath ), fs_gamedir, "/", from, NULL );
if( len >= sizeof( frompath ) ) {
FS_DPrintf( "%s: refusing oversize source path: %s\n", __func__, frompath );
return qfalse;
}
if( *to == '/' ) {
to++;
}
if( !FS_ValidatePath( to ) ) {
FS_DPrintf( "%s: refusing invalid destination path: %s\n", __func__, to );
return qfalse;
}
len = Q_concat( topath, sizeof( topath ), fs_gamedir, "/", to, NULL );
if( len >= sizeof( topath ) ) {
FS_DPrintf( "%s: refusing oversize destination path: %s\n", __func__, topath );
return qfalse;
}
if( rename( frompath, topath ) ) {
return qfalse;
}
return qtrue;
}
#endif // USE_CLIENT
/*
================
FS_FPrintf
================
*/
void FS_FPrintf( fileHandle_t f, const char *format, ... ) {
va_list argptr;
char string[MAXPRINTMSG];
size_t len;
va_start( argptr, format );
len = Q_vsnprintf( string, sizeof( string ), format, argptr );
va_end( argptr );
if( len >= sizeof( string ) ) {
len = sizeof( string ) - 1;
}
FS_Write( string, len, f );
}
/*
=================
FS_LoadPakFile
Takes an explicit (not game tree related) path to a pak file.
Loads the header and directory, adding the files at the beginning
of the list so they override previous pack files.
=================
*/
static pack_t *FS_LoadPakFile( const char *packfile ) {
dpackheader_t header;
int i;
packfile_t *file;
dpackfile_t *dfile;
int numpackfiles;
char *names;
size_t namesLength;
pack_t *pack;
FILE *packhandle;
dpackfile_t info[MAX_FILES_IN_PACK];
int hashSize;
unsigned hash;
size_t len;
packhandle = fopen( packfile, "rb" );
if( !packhandle ) {
Com_WPrintf( "Couldn't open %s\n", packfile );
return NULL;
}
if( fread( &header, 1, sizeof( header ), packhandle ) != sizeof( header ) ) {
Com_WPrintf( "Reading header failed on %s\n", packfile );
goto fail;
}
if( LittleLong( header.ident ) != IDPAKHEADER ) {
Com_WPrintf( "%s is not a 'PACK' file\n", packfile );
goto fail;
}
header.dirlen = LittleLong( header.dirlen );
if( header.dirlen % sizeof( dpackfile_t ) ) {
Com_WPrintf( "%s has bad directory length\n", packfile );
goto fail;
}
numpackfiles = header.dirlen / sizeof( dpackfile_t );
if( numpackfiles < 1 ) {
Com_WPrintf( "%s has bad number of files: %i\n", packfile, numpackfiles );
goto fail;
}
if( numpackfiles > MAX_FILES_IN_PACK ) {
Com_WPrintf( "%s has too many files: %i > %i\n", packfile, numpackfiles, MAX_FILES_IN_PACK );
goto fail;
}
header.dirofs = LittleLong( header.dirofs );
if( fseek( packhandle, header.dirofs, SEEK_SET ) ) {
Com_WPrintf( "Seeking to directory failed on %s\n", packfile );
goto fail;
}
if( fread( info, 1, header.dirlen, packhandle ) != header.dirlen ) {
Com_WPrintf( "Reading directory failed on %s\n", packfile );
goto fail;
}
namesLength = 0;
for( i = 0, dfile = info; i < numpackfiles; i++, dfile++ ) {
dfile->name[sizeof( dfile->name ) - 1] = 0;
namesLength += strlen( dfile->name ) + 1;
}
hashSize = Q_CeilPowerOfTwo( numpackfiles );
if( hashSize > 32 ) {
hashSize >>= 1;
}
len = strlen( packfile );
len = ( len + 3 ) & ~3;
pack = FS_Malloc( sizeof( pack_t ) +
numpackfiles * sizeof( packfile_t ) +
hashSize * sizeof( packfile_t * ) +
namesLength + len );
strcpy( pack->filename, packfile );
pack->fp = packhandle;
#if USE_ZLIB
pack->zFile = NULL;
#endif
pack->numfiles = numpackfiles;
pack->hashSize = hashSize;
pack->files = ( packfile_t * )( ( byte * )pack + sizeof( pack_t ) + len );
pack->fileHash = ( packfile_t ** )( pack->files + numpackfiles );
names = ( char * )( pack->fileHash + hashSize );
memset( pack->fileHash, 0, hashSize * sizeof( packfile_t * ) );
// parse the directory
for( i = 0, file = pack->files, dfile = info; i < pack->numfiles; i++, file++, dfile++ ) {
len = strlen( dfile->name ) + 1;
file->name = memcpy( names, dfile->name, len );
names += len;
file->filepos = LittleLong( dfile->filepos );
file->filelen = LittleLong( dfile->filelen );
hash = Com_HashPath( file->name, hashSize );
file->hashNext = pack->fileHash[hash];
pack->fileHash[hash] = file;
}
FS_DPrintf( "%s: %d files, %d hash table entries\n",
packfile, numpackfiles, hashSize );
return pack;
fail:
fclose( packhandle );
return NULL;
}
#if USE_ZLIB
/*
=================
FS_LoadZipFile
=================
*/
static pack_t *FS_LoadZipFile( const char *packfile ) {
int i;
packfile_t *file;
char *names;
int numFiles;
pack_t *pack;
unzFile zFile;
unz_global_info zGlobalInfo;
unz_file_info zInfo;
char name[MAX_QPATH];
size_t namesLength;
int hashSize;
unsigned hash;
size_t len;
zFile = unzOpen( packfile );
if( !zFile ) {
Com_WPrintf( "unzOpen() failed on %s\n", packfile );
return NULL;
}
if( unzGetGlobalInfo( zFile, &zGlobalInfo ) != UNZ_OK ) {
Com_WPrintf( "unzGetGlobalInfo() failed on %s\n", packfile );
goto fail;
}
numFiles = zGlobalInfo.number_entry;
if( numFiles > MAX_FILES_IN_PK2 ) {
Com_WPrintf( "%s has too many files, %i > %i\n", packfile, numFiles, MAX_FILES_IN_PK2 );
goto fail;
}
if( unzGoToFirstFile( zFile ) != UNZ_OK ) {
Com_WPrintf( "unzGoToFirstFile() failed on %s\n", packfile );
goto fail;
}
namesLength = 0;
for( i = 0; i < numFiles; i++ ) {
if( unzGetCurrentFileInfo( zFile, &zInfo, name, sizeof( name ), NULL, 0, NULL, 0 ) != UNZ_OK ) {
Com_WPrintf( "unzGetCurrentFileInfo() failed on %s\n", packfile );
goto fail;
}
namesLength += strlen( name ) + 1;
if( i != numFiles - 1 && unzGoToNextFile( zFile ) != UNZ_OK ) {
Com_WPrintf( "unzGoToNextFile() failed on %s\n", packfile );
}
}
hashSize = Q_CeilPowerOfTwo( numFiles );
if( hashSize > 32 ) {
hashSize >>= 1;
}
len = strlen( packfile );
len = ( len + 3 ) & ~3;
pack = FS_Malloc( sizeof( pack_t ) +
numFiles * sizeof( packfile_t ) +
hashSize * sizeof( packfile_t * ) +
namesLength + len );
strcpy( pack->filename, packfile );
pack->zFile = zFile;
pack->fp = NULL;
pack->numfiles = numFiles;
pack->hashSize = hashSize;
pack->files = ( packfile_t * )( ( byte * )pack + sizeof( pack_t ) + len );
pack->fileHash = ( packfile_t ** )( pack->files + numFiles );
names = ( char * )( pack->fileHash + hashSize );
memset( pack->fileHash, 0, hashSize * sizeof( packfile_t * ) );
// parse the directory
unzGoToFirstFile( zFile );
for( i = 0, file = pack->files; i < numFiles; i++, file++ ) {
unzGetCurrentFileInfo( zFile, &zInfo, name, sizeof( name ), NULL, 0, NULL, 0 );
len = strlen( name ) + 1;
file->name = names;
strcpy( file->name, name );
file->filepos = unzGetCurrentFileInfoPosition( zFile );
file->filelen = zInfo.uncompressed_size;
hash = Com_HashPath( file->name, hashSize );
file->hashNext = pack->fileHash[hash];
pack->fileHash[hash] = file;
names += len;
if( i != numFiles - 1 ) {
unzGoToNextFile( zFile );
}
}
FS_DPrintf( "%s: %d files, %d hash table entries\n",
packfile, numFiles, hashSize );
return pack;
fail:
unzClose( zFile );
return NULL;
}
#endif
// this is complicated as we need pakXX.pak loaded first,
// sorted in numerical order, then the rest of the paks in
// alphabetical order, e.g. pak0.pak, pak2.pak, pak17.pak, abc.pak...
static int QDECL pakcmp( const void *p1, const void *p2 ) {
char *s1 = *( char ** )p1;
char *s2 = *( char ** )p2;
if( !FS_strncmp( s1, "pak", 3 ) ) {
if( !FS_strncmp( s2, "pak", 3 ) ) {
int n1 = strtoul( s1 + 3, &s1, 10 );
int n2 = strtoul( s2 + 3, &s2, 10 );
if( n1 > n2 ) {
return 1;
}
if( n1 < n2 ) {
return -1;
}
goto alphacmp;
}
return -1;
}
if( !FS_strncmp( s2, "pak", 3 ) ) {
return 1;
}
alphacmp:
return FS_strcmp( s1, s2 );
}
static void FS_LoadPackFiles( int mode, const char *extension, pack_t *(loadfunc)( const char * ) ) {
int i;
searchpath_t *search;
pack_t *pak;
void **list;
int numFiles;
char path[MAX_OSPATH];
list = Sys_ListFiles( fs_gamedir, extension, FS_SEARCH_NOSORT, 0, &numFiles );
if( !list ) {
return;
}
qsort( list, numFiles, sizeof( list[0] ), pakcmp );
for( i = 0; i < numFiles; i++ ) {
Q_concat( path, sizeof( path ), fs_gamedir, "/", list[i], NULL );
pak = (*loadfunc)( path );
if( !pak )
continue;
search = FS_Malloc( sizeof( searchpath_t ) );
search->mode = mode;
search->filename[0] = 0;
search->pack = pak;
search->next = fs_searchpaths;
fs_searchpaths = search;
}
FS_FreeList( list );
}
/*
================
FS_AddGameDirectory
Sets fs_gamedir, adds the directory to the head of the path,
then loads and adds pak*.pak, then anything else in alphabethical order.
================
*/
static void q_printf( 2, 3 ) FS_AddGameDirectory( int mode, const char *fmt, ... ) {
va_list argptr;
searchpath_t *search;
size_t len;
va_start( argptr, fmt );
len = Q_vsnprintf( fs_gamedir, sizeof( fs_gamedir ), fmt, argptr );
va_end( argptr );
if( len >= sizeof( fs_gamedir ) ) {
Com_EPrintf( "%s: refusing oversize path\n", __func__ );
return;
}
#ifdef _WIN32
FS_ReplaceSeparators( fs_gamedir, '/' );
#endif
//
// add the directory to the search path
//
if( !( fs_restrict_mask->integer & 1 ) ) {
search = FS_Malloc( sizeof( searchpath_t ) + len );
search->mode = mode;
search->pack = NULL;
memcpy( search->filename, fs_gamedir, len + 1 );
search->next = fs_searchpaths;
fs_searchpaths = search;
}
//
// add any pak files in the format *.pak
//
if( !( fs_restrict_mask->integer & 2 ) ) {
FS_LoadPackFiles( mode, ".pak", FS_LoadPakFile );
}
#if USE_ZLIB
//
// add any zip files in the format *.pkz
//
if( !( fs_restrict_mask->integer & 4 ) ) {
FS_LoadPackFiles( mode, ".pkz", FS_LoadZipFile );
}
#endif
}
/*
=================
FS_CopyInfo
=================
*/
fsFileInfo_t *FS_CopyInfo( const char *name, size_t size, time_t ctime, time_t mtime ) {
fsFileInfo_t *out;
size_t len;
if( !name ) {
return NULL;
}
len = strlen( name );
out = FS_Mallocz( sizeof( *out ) + len );
out->size = size;
out->ctime = ctime;
out->mtime = mtime;
memcpy( out->name, name, len + 1 );
return out;
}
void **FS_CopyList( void **list, int count ) {
void **out;
int i;
out = FS_Malloc( sizeof( void * ) * ( count + 1 ) );
for( i = 0; i < count; i++ ) {
out[i] = list[i];
}
out[i] = NULL;
return out;
}
#if 0
// foo*bar
// foobar
// fooblahbar
static qboolean FS_WildCmp_r( const char *filter, const char *string ) {
while( *filter && *filter != ';' ) {
if( *filter == '*' ) {
return FS_WildCmp_r( filter + 1, string ) ||
( *string && FS_WildCmp_r( filter, string + 1 ) );
}
if( *filter == '[' ) {
filter++;
continue;
}
if( *filter != '?' && Q_toupper( *filter ) != Q_toupper( *string ) ) {
return qfalse;
}
filter++;
string++;
}
return !*string;
}
#endif
static int FS_WildCmp_r( const char *filter, const char *string ) {
switch( *filter ) {
case '\0':
case ';':
return !*string;
case '*':
return FS_WildCmp_r( filter + 1, string ) || (*string && FS_WildCmp_r( filter, string + 1 ));
case '?':
return *string && FS_WildCmp_r( filter + 1, string + 1 );
default:
return ((*filter == *string) || (Q_toupper( *filter ) == Q_toupper( *string ))) && FS_WildCmp_r( filter + 1, string + 1 );
}
}
qboolean FS_WildCmp( const char *filter, const char *string ) {
do {
if( FS_WildCmp_r( filter, string ) ) {
return qtrue;
}
filter = strchr( filter, ';' );
if( filter ) filter++;
} while( filter );
return qfalse;
}
qboolean FS_ExtCmp( const char *ext, const char *name ) {
int c1, c2;
const char *e, *n, *l;
if( !name[0] || !ext[0] ) {
return qfalse;
}
for( l = name; l[1]; l++ )
;
for( e = ext; e[1]; e++ )
;
rescan:
n = l;
do {
c1 = *e--;
c2 = *n--;
if( c1 == ';' ) {
break; // matched
}
if( c1 != c2 ) {
c1 = Q_tolower( c1 );
c2 = Q_tolower( c2 );
if( c1 != c2 ) {
while( e > ext ) {
c1 = *e--;
if( c1 == ';' ) {
goto rescan;
}
}
return qfalse;
}
}
if( n < name ) {
return qfalse;
}
} while( e >= ext );
return qtrue;
}
static int infocmp( const void *p1, const void *p2 ) {
fsFileInfo_t *n1 = *( fsFileInfo_t ** )p1;
fsFileInfo_t *n2 = *( fsFileInfo_t ** )p2;
return FS_pathcmp( n1->name, n2->name );
}
static int alphacmp( const void *p1, const void *p2 ) {
char *s1 = *( char ** )p1;
char *s2 = *( char ** )p2;
return FS_pathcmp( s1, s2 );
}
/*
=================
FS_ListFiles
=================
*/
void **FS_ListFiles( const char *path,
const char *extension,
int flags,
int *numFiles )
{
searchpath_t *search;
void *listedFiles[MAX_LISTED_FILES];
int count, total;
char buffer[MAX_OSPATH];
void **dirlist;
int dircount;
void **list;
int i;
size_t len, pathlen;
char *s;
int valid = -1;
if( flags & FS_SEARCH_BYFILTER ) {
if( !extension ) {
Com_Error( ERR_FATAL, "FS_ListFiles: NULL filter" );
}
}
count = 0;
if( numFiles ) {
*numFiles = 0;
}
if( !path ) {
path = "";
pathlen = 0;
} else {
if( *path == '/' ) {
path++;
}
pathlen = strlen( path );
}
for( search = fs_searchpaths; search; search = search->next ) {
if( flags & FS_PATH_MASK ) {
if( ( flags & search->mode & FS_PATH_MASK ) == 0 ) {
continue;
}
}
if( search->pack ) {
if( ( flags & FS_TYPE_MASK ) == FS_TYPE_REAL ) {
// don't search in paks
continue;
}
// TODO: add directory search support for pak files
if( ( flags & FS_SEARCHDIRS_MASK ) == FS_SEARCHDIRS_ONLY ) {
continue;
}
if( flags & FS_SEARCH_BYFILTER ) {
for( i = 0; i < search->pack->numfiles; i++ ) {
s = search->pack->files[i].name;
// check path
if( pathlen ) {
if( FS_pathcmpn( s, path, pathlen ) ) {
continue;
}
s += pathlen + 1;
}
// check filter
if( !FS_WildCmp( extension, s ) ) {
continue;
}
// copy filename
if( count == MAX_LISTED_FILES ) {
break;
}
if( !( flags & FS_SEARCH_SAVEPATH ) ) {
s = COM_SkipPath( s );
}
if( flags & FS_SEARCH_EXTRAINFO ) {
listedFiles[count++] = FS_CopyInfo( s,
search->pack->files[i].filelen, 0, 0 );
} else {
listedFiles[count++] = FS_CopyString( s );
}
}
} else {
for( i = 0; i < search->pack->numfiles; i++ ) {
s = search->pack->files[i].name;
// check path
if( pathlen && FS_pathcmpn( s, path, pathlen ) ) {
continue;
}
// check extension
if( extension && !FS_ExtCmp( extension, s ) ) {
continue;
}
// copy filename
if( count == MAX_LISTED_FILES ) {
break;
}
if( !( flags & FS_SEARCH_SAVEPATH ) ) {
s = COM_SkipPath( s );
}
if( flags & FS_SEARCH_EXTRAINFO ) {
listedFiles[count++] = FS_CopyInfo( s,
search->pack->files[i].filelen, 0, 0 );
} else {
listedFiles[count++] = FS_CopyString( s );
}
}
}
} else {
if( ( flags & FS_TYPE_MASK ) == FS_TYPE_PAK ) {
// don't search in OS filesystem
continue;
}
len = strlen( search->filename );
if( pathlen ) {
if( len + pathlen + 1 >= MAX_OSPATH ) {
continue;
}
if( valid == -1 ) {
if( !FS_ValidatePath( path ) ) {
FS_DPrintf( "%s: refusing invalid path: %s\n",
__func__, path );
valid = 0;
}
}
if( valid == 0 ) {
continue;
}
memcpy( buffer, search->filename, len );
buffer[len++] = '/';
memcpy( buffer + len, path, pathlen + 1 );
s = buffer;
} else {
s = search->filename;
}
if( flags & FS_SEARCH_BYFILTER ) {
len += pathlen + 1;
}
dirlist = Sys_ListFiles( s, extension,
flags|FS_SEARCH_NOSORT, len, &dircount );
if( !dirlist ) {
continue;
}
if( count + dircount > MAX_LISTED_FILES ) {
dircount = MAX_LISTED_FILES - count;
}
for( i = 0; i < dircount; i++ ) {
listedFiles[count++] = dirlist[i];
}
Z_Free( dirlist );
}
if( count == MAX_LISTED_FILES ) {
break;
}
}
if( !count ) {
return NULL;
}
if( flags & FS_SEARCH_EXTRAINFO ) {
// TODO
qsort( listedFiles, count, sizeof( listedFiles[0] ), infocmp );
total = count;
} else {
// sort alphabetically (ignoring FS_SEARCH_NOSORT)
qsort( listedFiles, count, sizeof( listedFiles[0] ), alphacmp );
// remove duplicates
total = 1;
for( i = 1; i < count; i++ ) {
if( !FS_pathcmp( listedFiles[ i - 1 ], listedFiles[i] ) ) {
Z_Free( listedFiles[ i - 1 ] );
listedFiles[i-1] = NULL;
} else {
total++;
}
}
}
list = FS_Malloc( sizeof( void * ) * ( total + 1 ) );
total = 0;
for( i = 0; i < count; i++ ) {
if( listedFiles[i] ) {
list[total++] = listedFiles[i];
}
}
list[total] = NULL;
if( numFiles ) {
*numFiles = total;
}
return list;
}
/*
=================
FS_FreeList
=================
*/
void FS_FreeList( void **list ) {
void **p = list;
while( *p ) {
Z_Free( *p++ );
}
Z_Free( list );
}
void FS_File_g( const char *path, const char *ext, int flags, genctx_t *ctx ) {
int i, numFiles;
void **list;
char *s, *p;
list = FS_ListFiles( path, ext, flags, &numFiles );
if( !list ) {
return;
}
for( i = 0; i < numFiles; i++ ) {
s = list[i];
if( flags & 0x80000000 ) {
p = COM_FileExtension( s );
*p = 0;
}
if( ctx->count < ctx->size && !strncmp( s, ctx->partial, ctx->length ) ) {
ctx->matches[ctx->count++] = s;
} else {
Z_Free( s );
}
}
Z_Free( list );
}
/*
============
FS_FDir_f
============
*/
static void FS_FDir_f( void ) {
void **list;
int ndirs = 0;
int i;
char *filter;
if( Cmd_Argc() < 2 ) {
Com_Printf( "Usage: %s <filter> [fullPath]\n", Cmd_Argv( 0 ) );
return;
}
filter = Cmd_Argv( 1 );
i = FS_SEARCH_BYFILTER;
if( Cmd_Argc() > 2 ) {
i |= FS_SEARCH_SAVEPATH;
}
if( ( list = FS_ListFiles( NULL, filter, i, &ndirs ) ) != NULL ) {
for( i = 0; i < ndirs; i++ ) {
Com_Printf( "%s\n", ( char * )list[i] );
}
FS_FreeList( list );
}
Com_Printf( "%i files listed\n", ndirs );
}
/*
============
FS_Dir_f
============
*/
static void FS_Dir_f( void ) {
void **list;
int ndirs = 0;
int i;
char *ext;
if( Cmd_Argc() < 2 ) {
Com_Printf( "Usage: %s <directory> [.extension]\n", Cmd_Argv( 0 ) );
return;
}
if( Cmd_Argc() > 2 ) {
ext = Cmd_Argv( 2 );
} else {
ext = NULL;
}
list = FS_ListFiles( Cmd_Argv( 1 ), ext, 0, &ndirs );
if( list ) {
for( i = 0; i < ndirs; i++ ) {
Com_Printf( "%s\n", ( char * )list[i] );
}
FS_FreeList( list );
}
Com_Printf( "%i files listed\n", ndirs );
}
/*
============
FS_WhereIs_f
============
*/
static void FS_WhereIs_f( void ) {
searchpath_t *search;
pack_t *pak;
packfile_t *entry;
unsigned hash;
char filename[MAX_OSPATH];
char fullpath[MAX_OSPATH];
char *path;
fsFileInfo_t info;
int total;
size_t len;
if( Cmd_Argc() < 2 ) {
Com_Printf( "Usage: %s <path> [all]\n", Cmd_Argv( 0 ) );
return;
}
Cmd_ArgvBuffer( 1, filename, sizeof( filename ) );
path = FS_ExpandLinks( filename );
if( path != filename ) {
Com_Printf( "%s linked to %s\n", filename, path );
}
hash = Com_HashPath( path, 0 );
total = 0;
for( search = fs_searchpaths; search; search = search->next ) {
// is the element a pak file?
if( search->pack ) {
// look through all the pak file elements
pak = search->pack;
entry = pak->fileHash[ hash & ( pak->hashSize - 1 ) ];
for( ; entry; entry = entry->hashNext ) {
if( !FS_pathcmp( entry->name, path ) ) {
Com_Printf( "%s/%s (%"PRIz" bytes)\n", pak->filename,
path, entry->filelen );
if( Cmd_Argc() < 3 ) {
return;
}
total++;
}
}
} else {
len = Q_concat( fullpath, sizeof( fullpath ),
search->filename, "/", path, NULL );
if( len >= sizeof( fullpath ) ) {
continue;
}
//FS_ConvertToSysPath( fullpath );
if( Sys_GetPathInfo( fullpath, &info ) ) {
Com_Printf( "%s/%s (%"PRIz" bytes)\n", search->filename, filename, info.size );
if( Cmd_Argc() < 3 ) {
return;
}
total++;
}
}
}
if( total ) {
Com_Printf( "%d instances of %s\n", total, path );
} else {
Com_Printf( "%s was not found\n", path );
}
}
/*
============
FS_Path_f
============
*/
void FS_Path_f( void ) {
searchpath_t *s;
int numFilesInPAK = 0;
#if USE_ZLIB
int numFilesInPK2 = 0;
#endif
Com_Printf( "Current search path:\n" );
for( s = fs_searchpaths; s; s = s->next ) {
if( s->pack ) {
#if USE_ZLIB
if( s->pack->zFile ) {
numFilesInPK2 += s->pack->numfiles;
} else
#endif
{
numFilesInPAK += s->pack->numfiles;
}
}
if( s->pack )
Com_Printf( "%s (%i files)\n", s->pack->filename, s->pack->numfiles );
else
Com_Printf( "%s\n", s->filename );
}
if( numFilesInPAK ) {
Com_Printf( "%i files in PAK files\n", numFilesInPAK );
}
#if USE_ZLIB
if( numFilesInPK2 ) {
Com_Printf( "%i files in PKZ files\n", numFilesInPK2 );
}
#endif
}
/*
================
FS_Stats_f
================
*/
static void FS_Stats_f( void ) {
searchpath_t *path;
pack_t *pack, *maxpack = NULL;
packfile_t *file, *max = NULL;
int i;
int len, maxLen = 0;
int totalHashSize, totalLen;
totalHashSize = totalLen = 0;
for( path = fs_searchpaths; path; path = path->next ) {
if( !( pack = path->pack ) ) {
continue;
}
for( i = 0; i < pack->hashSize; i++ ) {
if( !( file = pack->fileHash[i] ) ) {
continue;
}
len = 0;
for( ; file ; file = file->hashNext ) {
len++;
}
if( maxLen < len ) {
max = pack->fileHash[i];
maxpack = pack;
maxLen = len;
}
totalLen += len;
totalHashSize++;
}
//totalHashSize += pack->hashSize;
}
Com_Printf( "LoadFile counter: %d\n", loadCount );
Com_Printf( "Static LoadFile counter: %d\n", loadCountStatic );
Com_Printf( "Total calls to OpenFileRead: %d\n", fs_count_read );
Com_Printf( "Total path comparsions: %d\n", fs_count_strcmp );
Com_Printf( "Total calls to fopen: %d\n", fs_count_open );
if( !totalHashSize ) {
Com_Printf( "No stats to display\n" );
return;
}
Com_Printf( "Maximum hash bucket length is %d, average is %.2f\n", maxLen, ( float )totalLen / totalHashSize );
if( max ) {
Com_Printf( "Dumping longest bucket (%s):\n", maxpack->filename );
for( file = max; file ; file = file->hashNext ) {
Com_Printf( "%s\n", file->name );
}
}
}
static void FS_Link_g( genctx_t *ctx ) {
fsLink_t *link;
for( link = fs_links; link; link = link->next ) {
if( !Prompt_AddMatch( ctx, link->name ) ) {
break;
}
}
}
static void FS_Link_c( genctx_t *ctx, int argnum ) {
if( argnum == 1 ) {
FS_Link_g( ctx );
}
}
static void FS_UnLink_f( void ) {
static const cmd_option_t options[] = {
{ "a", "all", "delete all links" },
{ "h", "help", "display this message" },
{ NULL }
};
fsLink_t *l, *next, **back;
char *name;
int c;
while( ( c = Cmd_ParseOptions( options ) ) != -1 ) {
switch( c ) {
case 'h':
Com_Printf( "Usage: %s [-ah] <name> -- delete symbolic link\n",
Cmd_Argv( 0 ) );
Cmd_PrintHelp( options );
return;
case 'a':
for( l = fs_links; l; l = next ) {
next = l->next;
Z_Free( l->target );
Z_Free( l );
}
fs_links = NULL;
Com_Printf( "Deleted all symbolic links.\n" );
return;
default:
return;
}
}
name = cmd_optarg;
if( !name[0] ) {
Com_Printf( "Missing name argument.\n"
"Try '%s --help' for more information.\n", Cmd_Argv( 0 ) );
return;
}
for( l = fs_links, back = &fs_links; l; l = l->next ) {
if( !FS_pathcmp( l->name, name ) ) {
break;
}
back = &l->next;
}
if( !l ) {
Com_Printf( "Symbolic link %s does not exist.\n", name );
return;
}
*back = l->next;
Z_Free( l->target );
Z_Free( l );
}
static void FS_Link_f( void ) {
int argc, count;
size_t length;
fsLink_t *l;
char *name, *target;
argc = Cmd_Argc();
if( argc == 1 ) {
for( l = fs_links, count = 0; l; l = l->next, count++ ) {
Com_Printf( "%s --> %s\n", l->name, l->target );
}
Com_Printf( "------------------\n"
"%d symbolic links listed.\n", count );
return;
}
if( argc != 3 ) {
Com_Printf( "Usage: %s <name> <target>\n"
"Creates symbolic link to target with the specified name.\n"
"Virtual quake paths are accepted.\n"
"Links are effective only for reading.\n",
Cmd_Argv( 0 ) );
return;
}
name = Cmd_Argv( 1 );
for( l = fs_links; l; l = l->next ) {
if( !FS_pathcmp( l->name, name ) ) {
break;
}
}
if( !l ) {
length = strlen( name );
l = FS_Malloc( sizeof( *l ) + length );
strcpy( l->name, name );
l->namelen = length;
l->next = fs_links;
fs_links = l;
} else {
Z_Free( l->target );
}
target = Cmd_Argv( 2 );
l->target = FS_CopyString( target );
l->targlen = strlen( target );
}
static void FS_FreeSearchPath( searchpath_t *path ) {
pack_t *pak;
if( ( pak = path->pack ) != NULL ) {
#if USE_ZLIB
if( pak->zFile ) {
unzClose( pak->zFile );
} else
#endif
{
fclose( pak->fp );
}
Z_Free( pak );
}
Z_Free( path );
}
/*
================
FS_Shutdown
================
*/
void FS_Shutdown( qboolean total ) {
searchpath_t *path, *next;
fsLink_t *l, *nextLink;
fsFile_t *file;
int i;
if( !fs_searchpaths ) {
return;
}
if( total ) {
// close file handles
for( i = 0, file = fs_files; i < MAX_FILE_HANDLES; i++, file++ ) {
if( file->type != FS_FREE ) {
Com_WPrintf( "%s: closing handle %d\n", __func__, i + 1 );
FS_FCloseFile( i + 1 );
}
}
// free symbolic links
for( l = fs_links; l; l = nextLink ) {
nextLink = l->next;
Z_Free( l->target );
Z_Free( l );
}
fs_links = NULL;
}
// free search paths
for( path = fs_searchpaths; path; path = next ) {
next = path->next;
FS_FreeSearchPath( path );
}
fs_searchpaths = NULL;
if( total ) {
Z_LeakTest( TAG_FILESYSTEM );
}
Cmd_RemoveCommand( "path" );
Cmd_RemoveCommand( "fdir" );
Cmd_RemoveCommand( "dir" );
Cmd_RemoveCommand( "fs_stats" );
Cmd_RemoveCommand( "link" );
Cmd_RemoveCommand( "unlink" );
}
/*
================
FS_DefaultGamedir
================
*/
static void FS_DefaultGamedir( void ) {
if( sys_homedir->string[0] ) {
FS_AddGameDirectory( FS_PATH_BASE|FS_PATH_GAME,
"%s/"BASEGAME, sys_homedir->string );
}
Cvar_Set( "game", "" );
Cvar_Set( "gamedir", "" );
Cvar_Set( "fs_gamedir", fs_gamedir );
}
/*
================
FS_SetupGamedir
Sets the gamedir and path to a different directory.
================
*/
static void FS_SetupGamedir( void ) {
fs_game = Cvar_Get( "game", "", CVAR_FILES|CVAR_LATCH|CVAR_SERVERINFO );
cvar_modified &= ~CVAR_FILES;
if( !fs_game->string[0] || !FS_strcmp( fs_game->string, BASEGAME ) ) {
FS_DefaultGamedir();
return;
}
if( !FS_ValidatePath( fs_game->string ) ||
strchr( fs_game->string, '/' ) ||
strchr( fs_game->string, '\\' ) )
{
Com_WPrintf( "Gamedir should be a single filename, not a path.\n" );
FS_DefaultGamedir();
return;
}
// this one is left for compatibility with server browsers, etc
Cvar_FullSet( "gamedir", fs_game->string, CVAR_ROM|CVAR_SERVERINFO,
CVAR_SET_DIRECT );
FS_AddGameDirectory( FS_PATH_GAME, "%s/%s", sys_basedir->string, fs_game->string );
// home paths override system paths
if( sys_homedir->string[0] ) {
FS_AddGameDirectory( FS_PATH_BASE, "%s/"BASEGAME, sys_homedir->string );
FS_AddGameDirectory( FS_PATH_GAME, "%s/%s", sys_homedir->string, fs_game->string );
}
Cvar_Set( "fs_gamedir", fs_gamedir );
}
qboolean FS_SafeToRestart( void ) {
fsFile_t *file;
int i;
// make sure no files are opened for reading
for( i = 0, file = fs_files; i < MAX_FILE_HANDLES; i++, file++ ) {
if( file->type == FS_FREE ) {
continue;
}
if( file->mode == FS_MODE_READ ) {
return qfalse;
}
}
return qtrue;
}
/*
================
FS_Restart
================
*/
void FS_Restart( void ) {
fsFile_t *file;
int i;
fileHandle_t temp;
searchpath_t *path, *next;
Com_Printf( "---------- FS_Restart ----------\n" );
// temporary disable logfile
temp = com_logFile;
com_logFile = 0;
// make sure no files are opened for reading
for( i = 0, file = fs_files; i < MAX_FILE_HANDLES; i++, file++ ) {
if( file->type == FS_FREE ) {
continue;
}
if( file->mode == FS_MODE_READ ) {
Com_Error( ERR_FATAL, "%s: closing handle %d", __func__, i + 1 );
}
}
if( fs_restrict_mask->latched_string ) {
// perform full reset
FS_Shutdown( qfalse );
FS_Init();
} else {
// just change gamedir
for( path = fs_searchpaths; path != fs_base_searchpaths; path = next ) {
next = path->next;
FS_FreeSearchPath( path );
}
fs_searchpaths = fs_base_searchpaths;
FS_SetupGamedir();
FS_Path_f();
}
// re-enable logfile
com_logFile = temp;
Com_Printf( "--------------------------------\n" );
}
/*
============
FS_Restart_f
Console command to re-start the file system.
============
*/
static void FS_Restart_f( void ) {
if( !FS_SafeToRestart() ) {
Com_Printf( "Can't \"%s\", there are some open file handles.\n", Cmd_Argv( 0 ) );
return;
}
CL_RestartFilesystem();
}
static const cmdreg_t c_fs[] = {
{ "path", FS_Path_f },
{ "fdir", FS_FDir_f },
{ "dir", FS_Dir_f },
{ "fs_stats", FS_Stats_f },
{ "whereis", FS_WhereIs_f },
{ "link", FS_Link_f, FS_Link_c },
{ "unlink", FS_UnLink_f, FS_Link_c },
{ "fs_restart", FS_Restart_f },
{ NULL }
};
/*
================
FS_Init
================
*/
void FS_Init( void ) {
unsigned start, end;
start = Sys_Milliseconds();
Com_Printf( "---------- FS_Init ----------\n" );
Cmd_Register( c_fs );
fs_debug = Cvar_Get( "fs_debug", "0", 0 );
fs_restrict_mask = Cvar_Get( "fs_restrict_mask", "0", CVAR_NOSET );
Cvar_Get( "fs_gamedir", "", CVAR_ROM );
if( ( fs_restrict_mask->integer & 7 ) == 7 ) {
Com_WPrintf( "Invalid fs_restrict_mask value %d. "
"Falling back to default.\n",
fs_restrict_mask->integer );
Cvar_Set( "fs_restrict_mask", "0" );
}
// start up with baseq2 by default
FS_AddGameDirectory( FS_PATH_BASE|FS_PATH_GAME, "%s/"BASEGAME, sys_basedir->string );
fs_base_searchpaths = fs_searchpaths;
// check for game override
FS_SetupGamedir();
FS_Path_f();
end = Sys_Milliseconds();
Com_DPrintf( "%i msec to init filesystem\n", end - start );
Com_Printf( "-----------------------------\n" );
}
/*
================
FS_NeedRestart
================
*/
qboolean FS_NeedRestart( void ) {
if( cvar_modified & CVAR_FILES ) {
return qtrue;
}
if( fs_game->latched_string || fs_restrict_mask->latched_string ) {
return qtrue;
}
return qfalse;
}
|