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
|
// Checks the console path mapping the file shims install for a mapped title (RT-97).
//
// Every function is resolved through rt::shims::find under the DLL and name an import uses, so the test runs against
// the table the runtime installs rather than a private copy. It works on folders it creates in %TEMP%: a package
// folder that stands in for the game install, an overlay folder, and a "GAME:" path in each variant.
#include "test_temp_dir.h"
#include <windows.h>
#include <aclapi.h>
#include <algorithm>
#include <atomic>
#include <clocale>
#include <cstring>
#include <cstdio>
#include <exception>
#include <filesystem>
#include <fstream>
#include <sstream>
#include <initializer_list>
#include <string>
#include <thread>
#include <vector>
#include "file_system.h"
#include "shims.h"
#include "startup_trace.h"
namespace {
using CreateFileWFn = HANDLE(WINAPI*)(LPCWSTR, DWORD, DWORD, LPSECURITY_ATTRIBUTES, DWORD, DWORD, HANDLE);
using CreateFileAFn = HANDLE(WINAPI*)(LPCSTR, DWORD, DWORD, LPSECURITY_ATTRIBUTES, DWORD, DWORD, HANDLE);
using CreateFile2Fn = HANDLE(WINAPI*)(LPCWSTR, DWORD, DWORD, DWORD, LPCREATEFILE2_EXTENDED_PARAMETERS);
using FindFirstFileWFn = HANDLE(WINAPI*)(LPCWSTR, LPWIN32_FIND_DATAW);
using FindFirstFileAFn = HANDLE(WINAPI*)(LPCSTR, LPWIN32_FIND_DATAA);
using FindFirstFileExWFn =
HANDLE(WINAPI*)(LPCWSTR, FINDEX_INFO_LEVELS, LPVOID, FINDEX_SEARCH_OPS, LPVOID, DWORD);
using FindFirstFileExAFn =
HANDLE(WINAPI*)(LPCSTR, FINDEX_INFO_LEVELS, LPVOID, FINDEX_SEARCH_OPS, LPVOID, DWORD);
using FindNextFileWFn = BOOL(WINAPI*)(HANDLE, LPWIN32_FIND_DATAW);
using FindCloseFn = BOOL(WINAPI*)(HANDLE);
using GetFileAttributesWFn = DWORD(WINAPI*)(LPCWSTR);
using GetFileAttributesAFn = DWORD(WINAPI*)(LPCSTR);
using GetFileAttributesExWFn = BOOL(WINAPI*)(LPCWSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
using DeleteFileWFn = BOOL(WINAPI*)(LPCWSTR);
using DeleteFileAFn = BOOL(WINAPI*)(LPCSTR);
using CreateDirectoryWFn = BOOL(WINAPI*)(LPCWSTR, LPSECURITY_ATTRIBUTES);
using CreateDirectoryAFn = BOOL(WINAPI*)(LPCSTR, LPSECURITY_ATTRIBUTES);
using CreateDirectoryExWFn = BOOL(WINAPI*)(LPCWSTR, LPCWSTR, LPSECURITY_ATTRIBUTES);
using RemoveDirectoryWFn = BOOL(WINAPI*)(LPCWSTR);
using RemoveDirectoryAFn = BOOL(WINAPI*)(LPCSTR);
using MoveFileWFn = BOOL(WINAPI*)(LPCWSTR, LPCWSTR);
using MoveFileExWFn = BOOL(WINAPI*)(LPCWSTR, LPCWSTR, DWORD);
using MoveFileAFn = BOOL(WINAPI*)(LPCSTR, LPCSTR);
using CopyFileWFn = BOOL(WINAPI*)(LPCWSTR, LPCWSTR, BOOL);
using CopyFileAFn = BOOL(WINAPI*)(LPCSTR, LPCSTR, BOOL);
using CopyFileExWFn = BOOL(WINAPI*)(LPCWSTR, LPCWSTR, LPPROGRESS_ROUTINE, LPVOID, LPBOOL, DWORD);
using CopyFile2Fn = HRESULT(WINAPI*)(PCWSTR, PCWSTR, COPYFILE2_EXTENDED_PARAMETERS*);
using SetFileAttributesWFn = BOOL(WINAPI*)(LPCWSTR, DWORD);
using SetFileAttributesAFn = BOOL(WINAPI*)(LPCSTR, DWORD);
int g_checks = 0;
int g_failures = 0;
void check(bool condition, const char* what) {
++g_checks;
std::printf("%s %s\n", condition ? "PASS" : "FAIL", what);
if (!condition) ++g_failures;
}
template <typename Fn>
Fn resolve(const char* dll, const char* name) {
return reinterpret_cast<Fn>(rt::shims::find(dll, name));
}
void write_path(const std::filesystem::path& path, const std::string& text) {
std::filesystem::create_directories(path.parent_path());
const HANDLE h =
::CreateFileW(path.c_str(), GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
if (h == INVALID_HANDLE_VALUE) return;
DWORD written = 0;
::WriteFile(h, text.data(), static_cast<DWORD>(text.size()), &written, nullptr);
::CloseHandle(h);
}
std::string read_handle(HANDLE h) {
std::string out;
char buffer[256];
DWORD got = 0;
while (::ReadFile(h, buffer, sizeof(buffer), &got, nullptr) && got != 0) out.append(buffer, got);
return out;
}
std::string read_path(const std::filesystem::path& path) {
const HANDLE h = ::CreateFileW(path.c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
if (h == INVALID_HANDLE_VALUE) return {};
std::string text = read_handle(h);
::CloseHandle(h);
return text;
}
std::vector<std::wstring> list(FindFirstFileWFn first, FindNextFileWFn next, FindCloseFn close, const wchar_t* pattern) {
std::vector<std::wstring> names;
WIN32_FIND_DATAW found{};
const HANDLE h = first(pattern, &found);
if (h == INVALID_HANDLE_VALUE) return names;
names.emplace_back(found.cFileName);
while (next(h, &found)) names.emplace_back(found.cFileName);
close(h);
return names;
}
int count(const std::vector<std::wstring>& names, const wchar_t* name) {
int n = 0;
for (const auto& candidate : names)
if (_wcsicmp(candidate.c_str(), name) == 0) ++n;
return n;
}
bool contains(const std::vector<std::wstring>& names, const wchar_t* name) { return count(names, name) != 0; }
// The folder "T:\" names in another process `pid`: the one below <overlay>\.gta6rt-temp whose name ends in
// "-pid<pid>" or "-pid<pid>-<n>" (rt::fs::scratch_dir() in that process). Empty when there is none, as when every
// process shared .gta6rt-temp.
std::filesystem::path t_folder(const std::filesystem::path& overlay, DWORD pid) {
const std::wstring marker = L"-pid" + std::to_wstring(pid);
std::error_code ec;
for (auto it = std::filesystem::directory_iterator(overlay / ".gta6rt-temp", ec);
!ec && it != std::filesystem::directory_iterator(); it.increment(ec)) {
const std::wstring name = it->path().filename().wstring();
const size_t at = name.rfind(marker);
std::error_code type_ec;
if (it->is_directory(type_ec) && at != std::wstring::npos &&
(at + marker.size() == name.size() || name[at + marker.size()] == L'-'))
return it->path();
}
return {};
}
// This process's T: folder in the configured overlay.
std::filesystem::path own_t_folder(const std::filesystem::path& overlay) {
return rt::fs::overlay_dir() == overlay ? rt::fs::scratch_dir() : std::filesystem::path();
}
// file_system_test started by itself as a second process (run_t_drive_child below): it opens T:\crash.dmp with
// CREATE_NEW on the overlay its parent uses, writes "child process" and exits with 0, or with the open's Win32 error.
constexpr wchar_t kChildPackage[] = L"LEONIDA_FS_TEST_CHILD_PACKAGE";
constexpr wchar_t kChildOverlay[] = L"LEONIDA_FS_TEST_CHILD_OVERLAY";
std::wstring environment(const wchar_t* name) {
std::wstring buffer(32768, L'\0');
const DWORD n = ::GetEnvironmentVariableW(name, buffer.data(), static_cast<DWORD>(buffer.size()));
if (n == 0 || n >= buffer.size()) return {};
buffer.resize(n);
return buffer;
}
int t_drive_child(const std::wstring& package, const std::wstring& overlay) {
rt::fs::configure(package, overlay);
const auto create_w = resolve<CreateFileWFn>("kernel32.dll", "CreateFileW");
if (!create_w) return 1000;
const HANDLE h = create_w(L"T:\\crash.dmp", GENERIC_WRITE, 0, nullptr, CREATE_NEW, 0, nullptr);
if (h == INVALID_HANDLE_VALUE) return static_cast<int>(::GetLastError());
DWORD written = 0;
::WriteFile(h, "child process", 13, &written, nullptr);
::CloseHandle(h);
return 0;
}
// Runs t_drive_child in a new process of this executable on the given folders. Returns its exit code (~0u when it
// could not be started or did not finish) and its process id in `pid`.
DWORD run_t_drive_child(const std::filesystem::path& package, const std::filesystem::path& overlay, DWORD& pid) {
pid = 0;
std::wstring self(32768, L'\0');
const DWORD length = ::GetModuleFileNameW(nullptr, self.data(), static_cast<DWORD>(self.size()));
if (length == 0 || length >= self.size()) return ~0u;
self.resize(length);
std::wstring command = L"\"" + self + L"\"";
::SetEnvironmentVariableW(kChildPackage, package.c_str());
::SetEnvironmentVariableW(kChildOverlay, overlay.c_str());
STARTUPINFOW startup{};
startup.cb = sizeof(startup);
PROCESS_INFORMATION process{};
const BOOL started = ::CreateProcessW(self.c_str(), command.data(), nullptr, nullptr, FALSE, 0, nullptr, nullptr,
&startup, &process);
::SetEnvironmentVariableW(kChildPackage, nullptr);
::SetEnvironmentVariableW(kChildOverlay, nullptr);
if (!started) return ~0u;
DWORD code = ~0u;
if (::WaitForSingleObject(process.hProcess, 60000) == WAIT_OBJECT_0) ::GetExitCodeProcess(process.hProcess, &code);
pid = process.dwProcessId;
::CloseHandle(process.hThread);
::CloseHandle(process.hProcess);
return code;
}
// Denies the current user `rights` on a folder while it lives and puts the folder's old DACL back when it goes, on
// every way out of the check. Both changes go through one handle opened before the deny. Under Wine the DACL is the
// folder's unix mode: a deny of FILE_LIST_DIRECTORY clears its owner and group read bits (server/file.c, sd_to_mode),
// and Wine opens a folder with open(O_RDONLY) (server/fd.c, open_fd), which then fails with EACCES. A restore through
// the folder's name (SetNamedSecurityInfoW) could not open it again, so the folder stayed unlistable and the closing
// remove_all threw. Windows checks access when a handle is opened, so the handle keeps WRITE_DAC there as well.
class FolderDeny {
public:
FolderDeny(const std::wstring& folder, DWORD rights) {
handle_ = ::CreateFileW(folder.c_str(), READ_CONTROL | WRITE_DAC,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nullptr, OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS, nullptr);
if (handle_ == INVALID_HANDLE_VALUE) return;
if (GetSecurityInfo(handle_, SE_FILE_OBJECT, DACL_SECURITY_INFORMATION, nullptr, nullptr, &old_dacl_,
nullptr, &sd_) != ERROR_SUCCESS)
return;
EXPLICIT_ACCESS_W deny{};
deny.grfAccessPermissions = rights;
deny.grfAccessMode = DENY_ACCESS;
deny.grfInheritance = NO_INHERITANCE;
BuildTrusteeWithNameW(&deny.Trustee, const_cast<LPWSTR>(L"CURRENT_USER"));
PACL new_dacl = nullptr;
if (SetEntriesInAclW(1, &deny, old_dacl_, &new_dacl) != ERROR_SUCCESS) return;
denied_ = SetSecurityInfo(handle_, SE_FILE_OBJECT, DACL_SECURITY_INFORMATION, nullptr, nullptr, new_dacl,
nullptr) == ERROR_SUCCESS;
LocalFree(new_dacl);
}
FolderDeny(const FolderDeny&) = delete;
FolderDeny& operator=(const FolderDeny&) = delete;
~FolderDeny() {
restore();
if (sd_) LocalFree(sd_);
if (handle_ != INVALID_HANDLE_VALUE) ::CloseHandle(handle_);
}
bool denied() const { return denied_; }
// Puts the old DACL back; true when it is back (or was never changed).
bool restore() {
if (denied_ && SetSecurityInfo(handle_, SE_FILE_OBJECT, DACL_SECURITY_INFORMATION, nullptr, nullptr,
old_dacl_, nullptr) == ERROR_SUCCESS)
denied_ = false;
return !denied_;
}
private:
HANDLE handle_ = INVALID_HANDLE_VALUE;
PACL old_dacl_ = nullptr; // points into sd_
PSECURITY_DESCRIPTOR sd_ = nullptr;
bool denied_ = false;
};
// Whether the host's own MoveFileW changes a file's name to the same name in another case. Windows does. Wine 11.20
// looks the new name up without case, finds the file itself and returns success without renaming it (server/fd.c,
// set_fd_name: "if (!stat( name, &st ))" with the same inode returns early), so a listing keeps the old spelling.
bool host_renames_case(const std::filesystem::path& folder) {
write_path(folder / "case_probe.tmp", "probe");
const BOOL moved = ::MoveFileW((folder / "case_probe.tmp").c_str(), (folder / "CASE_PROBE.TMP").c_str());
const std::vector<std::wstring> names =
list(::FindFirstFileW, ::FindNextFileW, ::FindClose, (folder / "*").c_str());
::DeleteFileW((folder / "CASE_PROBE.TMP").c_str());
return moved && std::find(names.begin(), names.end(), std::wstring(L"CASE_PROBE.TMP")) != names.end();
}
void run_checks(const std::filesystem::path& root);
// Title memory committed on first touch, as xmem maps physical pages: a view of a reserve-only pagefile section whose
// pages a vectored handler commits when user-mode code faults on them. The kernel's write in ReadFile raises no such
// fault, so the host ReadFile fails with ERROR_NOACCESS; the shim commits the pages and reads again.
char* g_first_touch = nullptr;
SIZE_T g_first_touch_size = 0;
LONG CALLBACK commit_on_touch(EXCEPTION_POINTERS* info) {
const EXCEPTION_RECORD* r = info->ExceptionRecord;
if (r->ExceptionCode != EXCEPTION_ACCESS_VIOLATION || r->NumberParameters < 2) return EXCEPTION_CONTINUE_SEARCH;
auto* at = reinterpret_cast<char*>(r->ExceptionInformation[1]);
if (at < g_first_touch || at >= g_first_touch + g_first_touch_size) return EXCEPTION_CONTINUE_SEARCH;
auto* page = reinterpret_cast<char*>(reinterpret_cast<uintptr_t>(at) & ~uintptr_t{0xfff});
return VirtualAlloc(page, 0x1000, MEM_COMMIT, PAGE_READWRITE) ? EXCEPTION_CONTINUE_EXECUTION
: EXCEPTION_CONTINUE_SEARCH;
}
void check_read_file_first_touch(const std::filesystem::path& root) {
using ReadFileFn = BOOL(WINAPI*)(HANDLE, LPVOID, DWORD, LPDWORD, LPOVERLAPPED);
const auto shim_read = reinterpret_cast<ReadFileFn>(rt::shims::find("kernel32.dll", "ReadFile"));
check(shim_read != nullptr, "ReadFile is served by the file shims");
if (!shim_read) return;
std::filesystem::create_directories(root);
const std::filesystem::path file = root / "first_touch.bin";
constexpr DWORD kSize = 3 * 0x10000 + 123; // crosses several pages and ends inside one
std::vector<unsigned char> data(kSize);
for (DWORD i = 0; i < kSize; ++i) data[i] = static_cast<unsigned char>(i * 7 + 3);
std::ofstream(file, std::ios::binary).write(reinterpret_cast<const char*>(data.data()), kSize);
g_first_touch_size = 4 * 0x10000;
HANDLE section = CreateFileMappingW(INVALID_HANDLE_VALUE, nullptr, PAGE_READWRITE | SEC_RESERVE, 0,
static_cast<DWORD>(g_first_touch_size), nullptr);
g_first_touch = section ? static_cast<char*>(MapViewOfFile(section, FILE_MAP_WRITE, 0, 0, 0)) : nullptr;
check(g_first_touch != nullptr, "a reserve-only section view stands in for first-touch title memory");
if (!g_first_touch) return;
void* const handler = AddVectoredExceptionHandler(1, &commit_on_touch);
auto read_into_view = [&](ReadFileFn read) {
HANDLE h = CreateFileW(file.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
DWORD got = 0;
const BOOL ok = read(h, g_first_touch + 0x800, kSize, &got, nullptr);
const DWORD error = ok ? 0 : GetLastError();
CloseHandle(h);
return std::make_pair(ok ? got : 0, error);
};
const auto host = read_into_view(&::ReadFile);
check(host.second == ERROR_NOACCESS, "the host ReadFile into uncommitted pages fails with ERROR_NOACCESS");
const auto shimmed = read_into_view(shim_read);
check(shimmed.second == 0 && shimmed.first == kSize, "the shimmed ReadFile commits the pages and reads it all");
check(std::memcmp(g_first_touch + 0x800, data.data(), kSize) == 0, "the bytes read are the file's");
RemoveVectoredExceptionHandler(handler);
UnmapViewOfFile(g_first_touch);
CloseHandle(section);
g_first_touch = nullptr;
}
} // namespace
int main() {
if (const std::wstring child_overlay = environment(kChildOverlay); !child_overlay.empty())
return t_drive_child(environment(kChildPackage), child_overlay);
// A folder of this run's own: Wine hands each fresh prefix the same small process ids, so a folder named after
// the process id alone can be one an earlier run left behind.
LARGE_INTEGER now{};
QueryPerformanceCounter(&now);
const std::filesystem::path root =
testfiles::temp_dir() / ("leonida_file_system_" + std::to_string(GetCurrentProcessId()) +
"_" + std::to_string(now.QuadPart));
std::error_code ec;
std::filesystem::remove_all(root, ec);
// An exception from one check (std::filesystem throws on a host error) ends the checks, not the run: it is a
// failure, the folder is still removed and the summary is still printed.
try {
check_read_file_first_touch(root / "first_touch");
run_checks(root);
} catch (const std::exception& e) {
check(false, (std::string("the checks run to the end (stopped by: ") + e.what() + ")").c_str());
}
std::filesystem::remove_all(root, ec);
if (ec) std::printf("NOTE could not remove %s: %s\n", root.string().c_str(), ec.message().c_str());
std::printf("%d check(s), %d failure(s)\n", g_checks, g_failures);
return g_failures == 0 ? 0 : 1;
}
namespace {
void run_checks(const std::filesystem::path& root) {
const std::filesystem::path package = root / "package";
const std::filesystem::path overlay = root / "overlay";
write_path(package / "data" / "hello.txt", "package");
write_path(package / "data" / "dup.txt", "package");
write_path(package / "data" / "bye.txt", "package");
write_path(package / "data" / "sub" / "nested.txt", "nested");
rt::fs::configure(package, overlay);
check(rt::fs::game_dir() == package, "configure() sets the game folder");
check(rt::fs::overlay_dir() == overlay, "configure() sets the overlay folder");
// The table the runtime installs: kernel32 and the api sets a GDK title links against.
const auto create_w = resolve<CreateFileWFn>("kernel32.dll", "CreateFileW");
const auto create_a = resolve<CreateFileAFn>("kernelbase.dll", "CreateFileA");
const auto first_w = resolve<FindFirstFileWFn>("api-ms-win-core-file-l1-1-0.dll", "FindFirstFileW");
const auto first_a = resolve<FindFirstFileAFn>("kernel32.dll", "FindFirstFileA");
const auto next_w = resolve<FindNextFileWFn>("kernel32.dll", "FindNextFileW");
const auto find_close = resolve<FindCloseFn>("kernel32.dll", "FindClose");
const auto attributes_w = resolve<GetFileAttributesWFn>("kernel32.dll", "GetFileAttributesW");
const auto attributes_a = resolve<GetFileAttributesAFn>("kernel32.dll", "GetFileAttributesA");
const auto attributes_ex_w = resolve<GetFileAttributesExWFn>("kernel32.dll", "GetFileAttributesExW");
const auto delete_w = resolve<DeleteFileWFn>("kernel32.dll", "DeleteFileW");
const auto delete_a = resolve<DeleteFileAFn>("api-ms-win-core-file-l1-1-0.dll", "DeleteFileA");
check(create_w && create_a && first_w && first_a && next_w && find_close && attributes_w && attributes_a &&
attributes_ex_w && delete_w && delete_a,
"the file API resolves through the registry under kernel32/kernelbase and the api sets");
// A read of a package file, through the mixed-case prefix and through forward slashes.
{
HANDLE h = create_w(L"GAME:\\data\\hello.txt", GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
check(h != INVALID_HANDLE_VALUE, "CreateFileW opens GAME:\\data\\hello.txt from the package");
check(read_handle(h) == "package", "the package file's bytes come back unchanged");
if (h != INVALID_HANDLE_VALUE) ::CloseHandle(h);
h = create_w(L"game:/data/hello.txt", GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
check(h != INVALID_HANDLE_VALUE, "the prefix is case-insensitive and accepts forward slashes");
if (h != INVALID_HANDLE_VALUE) ::CloseHandle(h);
h = create_a("GAME:\\data\\hello.txt", GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
check(h != INVALID_HANDLE_VALUE, "CreateFileA takes the same path");
if (h != INVALID_HANDLE_VALUE) ::CloseHandle(h);
}
// --trace: a failed open of a guest path is a "file_open_failed" event, a successful one is not, and with the
// trace off nothing is written.
{
const std::wstring trace = (root / "open_trace.jsonl").wstring();
const auto events = [&] {
std::ifstream in(std::filesystem::path(trace), std::ios::binary);
std::stringstream text;
text << in.rdbuf();
return text.str();
};
const auto count = [](const std::string& hay, const std::string& needle) {
int n = 0;
for (size_t at = hay.find(needle); at != std::string::npos; at = hay.find(needle, at + 1)) ++n;
return n;
};
HANDLE h = create_w(L"GAME:\\data\\absent_off.txt", GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
check(h == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_NOT_FOUND, "a missing open fails with the trace off");
check(rt::startup_trace::open(trace.c_str()), "open the startup trace file");
h = create_w(L"GAME:\\data\\hello.txt", GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
check(h != INVALID_HANDLE_VALUE, "an open that succeeds");
if (h != INVALID_HANDLE_VALUE) ::CloseHandle(h);
check(count(events(), "file_open_failed") == 0, "a successful open writes no file_open_failed event");
h = create_w(L"GAME:\\data\\absent_on.txt", GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
check(h == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_NOT_FOUND,
"a missing open still fails the same way with the trace on");
rt::startup_trace::close();
const std::string log = events();
check(count(log, "\"kind\":\"file_open_failed\"") == 1, "one file_open_failed event");
check(log.find("\"path\":\"GAME:\\\\data\\\\absent_on.txt\"") != std::string::npos, "the event names the path");
check(log.find("\"error\":2,") != std::string::npos, "the event carries error 2");
check(log.find("\"caller\":\"host:") != std::string::npos, "the event names a caller");
check(count(log, "absent_off") == 0, "nothing was traced before the trace was opened");
}
// Attributes, wide and narrow.
check(attributes_w(L"GAME:\\data\\hello.txt") != INVALID_FILE_ATTRIBUTES, "GetFileAttributesW stats a package file");
check(attributes_a("GAME:\\data\\hello.txt") != INVALID_FILE_ATTRIBUTES, "GetFileAttributesA stats a package file");
const std::filesystem::path host_file = package / "data" / "hello.txt";
check(attributes_w(host_file.c_str()) != INVALID_FILE_ATTRIBUTES, "a host path is passed through unchanged");
// A write-open copies the package file into the overlay first, and the package is left alone.
{
const HANDLE h =
create_w(L"GAME:\\data\\hello.txt", GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, 0, nullptr);
check(h != INVALID_HANDLE_VALUE, "a write-open of a package file succeeds");
DWORD written = 0;
if (h != INVALID_HANDLE_VALUE) {
::WriteFile(h, "overlay", 7, &written, nullptr);
::CloseHandle(h);
}
check(read_path(package / "data" / "hello.txt") == "package", "the package file is not modified");
check(read_path(overlay / "data" / "hello.txt") == "overlay", "the write landed in the overlay copy");
HANDLE r = create_w(L"GAME:\\data\\hello.txt", GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
check(r != INVALID_HANDLE_VALUE && read_handle(r) == "overlay", "reads prefer the overlay copy");
if (r != INVALID_HANDLE_VALUE) ::CloseHandle(r);
}
// A new file, in a folder the package does not have: the title makes the folder first, as it must on the host.
{
const auto make_folder = resolve<CreateDirectoryWFn>("kernel32.dll", "CreateDirectoryW");
check(make_folder && make_folder(L"GAME:\\save", nullptr) != FALSE, "CreateDirectoryW makes GAME:\\save");
const HANDLE h = create_w(L"GAME:\\save\\slot1.bin", GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, 0, nullptr);
check(h != INVALID_HANDLE_VALUE, "CREATE_ALWAYS makes a new console file");
DWORD written = 0;
if (h != INVALID_HANDLE_VALUE) {
::WriteFile(h, "12345", 5, &written, nullptr);
::CloseHandle(h);
}
check(read_path(overlay / "save" / "slot1.bin") == "12345", "the new file is in the overlay");
check(!std::filesystem::exists(package / "save"), "nothing was created in the package");
WIN32_FILE_ATTRIBUTE_DATA data{};
check(attributes_ex_w(L"GAME:\\save\\slot1.bin", GetFileExInfoStandard, &data) != FALSE &&
data.nFileSizeLow == 5,
"GetFileAttributesExW reports the overlay copy's size");
}
// One listing, merged, overlay first and without duplicates.
{
const std::vector<std::wstring> names = list(first_w, next_w, find_close, L"GAME:\\data\\*");
check(contains(names, L"hello.txt") && count(names, L"hello.txt") == 1, "the merged listing has hello.txt once");
check(contains(names, L"dup.txt"), "the merged listing has the package's own files");
check(contains(names, L"sub"), "the merged listing has the package's folders");
check(!contains(names, L".gta6rt-deleted"), "the overlay's marker folder is not listed");
const std::vector<std::wstring> root_names = list(first_w, next_w, find_close, L"GAME:\\");
check(contains(root_names, L"data") && contains(root_names, L"save"),
"GAME:\\ lists the package's folder and the overlay's");
WIN32_FIND_DATAA ansi{};
const HANDLE h = first_a("GAME:\\data\\*", &ansi);
check(h != INVALID_HANDLE_VALUE && ansi.cFileName[0] != '\0', "FindFirstFileA fills an ANSI listing too");
if (h != INVALID_HANDLE_VALUE) find_close(h);
SetLastError(0);
WIN32_FIND_DATAW none{};
const HANDLE missing = first_w(L"GAME:\\missing\\*", &none);
check(missing == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
"a listing inside a folder that does not exist reports ERROR_PATH_NOT_FOUND, as the host does");
}
// Names that differ only in case are one entry, as on the host, and the overlay's spelling is the one listed.
{
write_path(overlay / "data" / "Mixed.TXT", "overlay");
write_path(package / "data" / "mixed.txt", "package");
const std::vector<std::wstring> names = list(first_w, next_w, find_close, L"GAME:\\data\\*");
check(count(names, L"mixed.txt") == 1, "names that differ only in case are listed once");
check(std::find(names.begin(), names.end(), std::wstring(L"Mixed.TXT")) != names.end(),
"and the overlay's spelling wins");
}
// A folder with no deletion markers at all (its marker folder does not exist) lists every package entry once.
{
for (int i = 0; i < 300; ++i) write_path(package / "many" / ("f" + std::to_string(i) + ".bin"), "x");
write_path(overlay / "many" / "F7.BIN", "overlay");
check(!std::filesystem::exists(overlay / ".gta6rt-deleted" / "many"), "GAME:\\many has no marker folder");
const std::vector<std::wstring> names = list(first_w, next_w, find_close, L"GAME:\\many\\*");
int files = 0;
for (const auto& n : names)
if (n != L"." && n != L"..") ++files;
check(files == 300 && count(names, L"f7.bin") == 1 && count(names, L"f299.bin") == 1,
"a folder without markers lists all 300 files, with the overlay's copy once");
// Once the folder has a marker folder, the listing reads it once: a delete under another ASCII case hides
// exactly that file and every other file stays.
check(delete_w(L"GAME:\\many\\F3.BIN") != FALSE, "DeleteFileW hides f3.bin under another case");
const std::vector<std::wstring> after = list(first_w, next_w, find_close, L"GAME:\\many\\*");
int left = 0;
for (const auto& n : after)
if (n != L"." && n != L"..") ++left;
check(left == 299 && !contains(after, L"f3.bin") && count(after, L"f30.bin") == 1,
"with a marker folder the listing hides only the deleted file");
}
// Names with non-ASCII letters keep the file system's case rule: deleting U+00E9 hides the package's U+00C9.
{
write_path(package / "names" / L"Éclair.txt", "package");
write_path(package / "names" / "plain.txt", "package");
check(delete_w(L"GAME:\\names\\éclair.txt") != FALSE, "DeleteFileW takes a name that differs in a non-ASCII case");
const std::vector<std::wstring> names = list(first_w, next_w, find_close, L"GAME:\\names\\*");
check(std::find(names.begin(), names.end(), std::wstring(L"Éclair.txt")) == names.end() &&
contains(names, L"plain.txt"),
"and the listing hides it, as the file system's compare does");
}
// Duplicates are decided by _wcsicmp (same_name) in the title's locale, not by a lower-cased key: under .UTF-8,
// _wcslwr_s folds U+00C9 and U+00E9 while _wcsicmp keeps them apart.
{
write_path(overlay / "loc" / L"Éclair.txt", "overlay");
write_path(package / "loc" / L"éclair.txt", "package");
const char* old = std::setlocale(LC_CTYPE, nullptr);
const std::string saved = old ? old : "C";
if (std::setlocale(LC_CTYPE, ".UTF-8")) {
const int expected = _wcsicmp(L"Éclair.txt", L"éclair.txt") == 0 ? 1 : 2;
const std::vector<std::wstring> names = list(first_w, next_w, find_close, L"GAME:\\loc\\*");
int files = 0;
for (const auto& n : names)
if (n != L"." && n != L"..") ++files;
check(files == expected, "under .UTF-8 the merged listing keeps exactly the names _wcsicmp tells apart");
}
std::setlocale(LC_CTYPE, saved.c_str());
}
// A marker folder that cannot be listed (listing denied, lookups by path still allowed) is not "no markers":
// the deleted file stays hidden, as GetFileAttributesW already reports it.
{
write_path(package / "acl" / "a.txt", "package");
write_path(package / "acl" / "b.txt", "package");
check(delete_w(L"GAME:\\acl\\a.txt") != FALSE, "DeleteFileW hides a.txt");
const std::wstring marks = (overlay / ".gta6rt-deleted" / "acl").wstring();
FolderDeny deny(marks, FILE_LIST_DIRECTORY);
const bool denied = deny.denied();
WIN32_FIND_DATAW probe{};
const HANDLE ph = ::FindFirstFileW((marks + L"\\*").c_str(), &probe);
const bool listing_denied = ph == INVALID_HANDLE_VALUE && GetLastError() == ERROR_ACCESS_DENIED;
if (ph != INVALID_HANDLE_VALUE) ::FindClose(ph);
if (denied && listing_denied) {
check(attributes_w(L"GAME:\\acl\\a.txt") == INVALID_FILE_ATTRIBUTES, "a.txt is still missing by path");
const std::vector<std::wstring> names = list(first_w, next_w, find_close, L"GAME:\\acl\\*");
check(!contains(names, L"a.txt") && contains(names, L"b.txt"),
"a marker folder that cannot be listed still hides its deleted files");
} else {
std::printf("SKIP could not deny listing the marker folder\n");
}
if (denied) {
const bool restored = deny.restore();
const HANDLE again = ::FindFirstFileW((marks + L"\\*").c_str(), &probe);
check(restored && again != INVALID_HANDLE_VALUE,
"the marker folder lists again once its old DACL is back");
if (again != INVALID_HANDLE_VALUE) ::FindClose(again);
}
}
// A delete hides the package file without removing it.
{
check(delete_w(L"GAME:\\data\\dup.txt") != FALSE, "DeleteFileW accepts a package file");
check(std::filesystem::exists(package / "data" / "dup.txt"), "the package file is still there");
SetLastError(0);
check(attributes_w(L"GAME:\\data\\dup.txt") == INVALID_FILE_ATTRIBUTES &&
GetLastError() == ERROR_FILE_NOT_FOUND,
"the deleted name no longer resolves");
SetLastError(0);
const HANDLE h =
create_w(L"GAME:\\data\\dup.txt", GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
check(h == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_NOT_FOUND, "and cannot be opened");
check(!contains(list(first_w, next_w, find_close, L"GAME:\\data\\*"), L"dup.txt"),
"and is absent from the listing");
// A write-open of the hidden name fails, and must leave the name deleted: no copy-up from the package, no
// overlay file, and still hidden afterwards.
SetLastError(0);
const HANDLE write_open =
create_w(L"GAME:\\data\\dup.txt", GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, 0, nullptr);
check(write_open == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_NOT_FOUND,
"a write-open of the deleted name fails with ERROR_FILE_NOT_FOUND");
if (write_open != INVALID_HANDLE_VALUE) ::CloseHandle(write_open);
check(!std::filesystem::exists(overlay / "data" / "dup.txt"), "the failed write-open left no overlay copy");
SetLastError(0);
check(attributes_w(L"GAME:\\data\\dup.txt") == INVALID_FILE_ATTRIBUTES &&
GetLastError() == ERROR_FILE_NOT_FOUND,
"and the name is still deleted");
// Re-creating it makes it readable again, and it is listed.
const HANDLE again = create_w(L"GAME:\\data\\dup.txt", GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, 0, nullptr);
DWORD written = 0;
check(again != INVALID_HANDLE_VALUE, "the name can be created again");
if (again != INVALID_HANDLE_VALUE) {
::WriteFile(again, "again", 5, &written, nullptr);
::CloseHandle(again);
}
check(read_path(overlay / "data" / "dup.txt") == "again", "the new copy is in the overlay");
check(contains(list(first_w, next_w, find_close, L"GAME:\\data\\*"), L"dup.txt"),
"and it is listed again");
check(delete_a("GAME:\\data\\bye.txt") != FALSE, "DeleteFileA takes a console path");
check(std::filesystem::exists(package / "data" / "bye.txt"), "the narrow delete leaves the package file too");
SetLastError(0);
check(delete_w(L"GAME:\\data\\bye.txt") == FALSE && GetLastError() == ERROR_FILE_NOT_FOUND,
"deleting an already hidden file reports ERROR_FILE_NOT_FOUND");
SetLastError(0);
check(delete_w(L"GAME:\\nope\\missing.txt") == FALSE && GetLastError() == ERROR_PATH_NOT_FOUND,
"deleting a file in a folder that never existed reports ERROR_PATH_NOT_FOUND");
// Deleting a nested package file creates parent directories inside .gta6rt-deleted/; those marker parent
// directories must not hide the corresponding ancestor directories in the package.
check(delete_w(L"GAME:\\data\\sub\\nested.txt") != FALSE, "DeleteFileW accepts a nested package file");
const DWORD sub_attrs = attributes_w(L"GAME:\\data\\sub");
check(sub_attrs != INVALID_FILE_ATTRIBUTES && (sub_attrs & FILE_ATTRIBUTE_DIRECTORY) != 0,
"deleting a nested package file does not hide its parent folder");
check(contains(list(first_w, next_w, find_close, L"GAME:\\data\\*"), L"sub"),
"the parent folder stays listed after deleting a nested package file");
}
// DeleteFile checks the visible file's read-only bit. A package file is hidden by a marker instead of being
// passed to the host DeleteFile, but that must not bypass the same refusal. Compare each API with a real host
// file and keep the package and overlay attributes independent.
{
struct DeleteCase {
const char* name;
DWORD package_attributes;
DWORD overlay_attributes; // zero means there is no overlay copy
};
constexpr DWORD mixed = FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_ARCHIVE;
const DeleteCase cases[] = {
{"package-readonly", FILE_ATTRIBUTE_READONLY, 0},
{"package-normal", FILE_ATTRIBUTE_NORMAL, 0},
{"package-mixed-readonly", FILE_ATTRIBUTE_READONLY | mixed, 0},
{"package-mixed-writable", mixed, 0},
{"readonly-package-normal-overlay", FILE_ATTRIBUTE_READONLY, FILE_ATTRIBUTE_NORMAL},
{"normal-package-readonly-overlay", FILE_ATTRIBUTE_NORMAL, FILE_ATTRIBUTE_READONLY},
{"readonly-package-readonly-overlay", FILE_ATTRIBUTE_READONLY, FILE_ATTRIBUTE_READONLY},
{"readonly-package-mixed-overlay", FILE_ATTRIBUTE_READONLY, mixed},
{"normal-package-mixed-readonly-overlay", FILE_ATTRIBUTE_NORMAL, FILE_ATTRIBUTE_READONLY | mixed},
};
for (const bool narrow : {false, true}) {
for (const auto& item : cases) {
const std::string leaf = std::string(narrow ? "a-" : "w-") + item.name + ".bin";
const std::filesystem::path relative = std::filesystem::path("delete-attributes") / leaf;
const auto package_file = package / relative, overlay_file = overlay / relative;
const auto native_file = root / "native-delete" / leaf;
const auto deletion_marker = overlay / ".gta6rt-deleted" / relative;
const bool has_overlay = item.overlay_attributes != 0;
const DWORD visible_attributes = has_overlay ? item.overlay_attributes : item.package_attributes;
const char* visible_bytes = has_overlay ? "overlay bytes" : "package bytes";
std::printf("DeleteFile%c case: %s\n", narrow ? 'A' : 'W', item.name);
write_path(package_file, "package bytes");
check(::SetFileAttributesW(package_file.c_str(), item.package_attributes) != FALSE,
"set the synthetic package attributes");
const DWORD package_before = ::GetFileAttributesW(package_file.c_str());
if (has_overlay) {
write_path(overlay_file, visible_bytes);
check(::SetFileAttributesW(overlay_file.c_str(), item.overlay_attributes) != FALSE,
"set the visible overlay attributes");
}
write_path(native_file, visible_bytes);
check(::SetFileAttributesW(native_file.c_str(), visible_attributes) != FALSE,
"set the independent native file attributes");
std::string native_a;
if (narrow) {
const UINT code_page = ::AreFileApisANSI() ? CP_ACP : CP_OEMCP;
const int size = ::WideCharToMultiByte(code_page, 0, native_file.c_str(), -1, nullptr, 0,
nullptr, nullptr);
native_a.resize(size > 0 ? static_cast<size_t>(size) : 1);
check(size > 0 && ::WideCharToMultiByte(code_page, 0, native_file.c_str(), -1, native_a.data(),
size, nullptr, nullptr) == size,
"encode the native DeleteFileA control path");
}
SetLastError(0);
const BOOL native_ok = narrow ? ::DeleteFileA(native_a.c_str()) : ::DeleteFileW(native_file.c_str());
const DWORD native_error = native_ok ? 0 : GetLastError();
const bool readonly = (visible_attributes & FILE_ATTRIBUTE_READONLY) != 0;
check((native_ok != FALSE) == !readonly && (native_ok || native_error == ERROR_ACCESS_DENIED),
"native deletion rejects read-only and accepts other attribute combinations");
const std::wstring logical = L"GAME:\\" + relative.wstring();
const std::string logical_a = "GAME:\\delete-attributes\\" + leaf;
SetLastError(0);
const BOOL mapped_ok = narrow ? delete_a(logical_a.c_str()) : delete_w(logical.c_str());
const DWORD mapped_error = mapped_ok ? 0 : GetLastError();
check((mapped_ok != FALSE) == (native_ok != FALSE) && mapped_error == native_error,
"shim deletion matches its native API's result and failure error");
check(read_path(package_file) == "package bytes" &&
::GetFileAttributesW(package_file.c_str()) == package_before,
"deletion leaves package bytes and attributes unchanged");
check(std::filesystem::exists(deletion_marker) == !readonly,
"only an accepted delete publishes a marker");
check(std::filesystem::exists(overlay_file) == (has_overlay && readonly),
"a refused delete keeps its overlay and never copies the package up");
if (readonly) {
// The host's answer for a real file given the same attributes, which survived its refused
// delete: Windows reads back exactly what was set, while Wine 9 adds ARCHIVE and keeps neither
// HIDDEN nor SYSTEM without its xattr support (0x21 for every read-only case here).
const DWORD native_attributes = ::GetFileAttributesW(native_file.c_str());
check(native_attributes != INVALID_FILE_ATTRIBUTES &&
attributes_w(logical.c_str()) == native_attributes,
"a refused delete retains the visible attributes");
const HANDLE retained = create_w(logical.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr,
OPEN_EXISTING, 0, nullptr);
check(retained != INVALID_HANDLE_VALUE && read_handle(retained) == visible_bytes,
"a refused delete leaves the same logical bytes readable");
if (retained != INVALID_HANDLE_VALUE) ::CloseHandle(retained);
} else {
SetLastError(0);
check(attributes_w(logical.c_str()) == INVALID_FILE_ATTRIBUTES &&
GetLastError() == ERROR_FILE_NOT_FOUND,
"an accepted delete hides the logical file");
}
// Restore only these synthetic files, including on a failed assertion, for remove_all below.
::SetFileAttributesW(package_file.c_str(), FILE_ATTRIBUTE_NORMAL);
if (std::filesystem::exists(overlay_file)) ::SetFileAttributesW(overlay_file.c_str(), FILE_ATTRIBUTE_NORMAL);
if (std::filesystem::exists(native_file)) ::SetFileAttributesW(native_file.c_str(), FILE_ATTRIBUTE_NORMAL);
}
}
}
// Deleting one package file must not hide its folder. The overlay has never written to GAME:\pkgonly, so the
// folder is only in the package, and its marker folder under .gta6rt-deleted is not a marker for the folder.
{
write_path(package / "pkgonly" / "a.txt", "package");
write_path(package / "pkgonly" / "b.txt", "package");
check(delete_w(L"GAME:\\pkgonly\\a.txt") != FALSE, "DeleteFileW hides a file in a package-only folder");
const DWORD folder = attributes_w(L"GAME:\\pkgonly");
check(folder != INVALID_FILE_ATTRIBUTES && (folder & FILE_ATTRIBUTE_DIRECTORY) != 0,
"the folder of a deleted package file still resolves");
check(contains(list(first_w, next_w, find_close, L"GAME:\\*"), L"pkgonly"),
"and is still in its parent's listing");
check(contains(list(first_w, next_w, find_close, L"GAME:\\pkgonly\\*"), L"b.txt"),
"and its other files are still listed");
const HANDLE h =
create_w(L"GAME:\\pkgonly\\b.txt", GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
check(h != INVALID_HANDLE_VALUE, "and its other files still open");
if (h != INVALID_HANDLE_VALUE) ::CloseHandle(h);
}
// Folders, moves, copies and attributes: the same mapping, through the names a title imports.
const auto mkdir_w = resolve<CreateDirectoryWFn>("api-ms-win-core-file-l1-1-0.dll", "CreateDirectoryW");
const auto mkdir_a = resolve<CreateDirectoryAFn>("kernel32.dll", "CreateDirectoryA");
const auto mkdir_ex_w = resolve<CreateDirectoryExWFn>("kernel32.dll", "CreateDirectoryExW");
const auto rmdir_w = resolve<RemoveDirectoryWFn>("api-ms-win-core-file-l1-1-0.dll", "RemoveDirectoryW");
const auto rmdir_a = resolve<RemoveDirectoryAFn>("kernelbase.dll", "RemoveDirectoryA");
const auto move_w = resolve<MoveFileWFn>("kernel32.dll", "MoveFileW");
const auto move_ex_w = resolve<MoveFileExWFn>("api-ms-win-core-file-l2-1-0.dll", "MoveFileExW");
const auto move_a = resolve<MoveFileAFn>("kernel32.dll", "MoveFileA");
const auto copy_w = resolve<CopyFileWFn>("kernel32.dll", "CopyFileW");
const auto copy_a = resolve<CopyFileAFn>("kernel32.dll", "CopyFileA");
const auto copy_ex_w = resolve<CopyFileExWFn>("api-ms-win-core-file-l2-1-0.dll", "CopyFileExW");
const auto copy2 = resolve<CopyFile2Fn>("api-ms-win-core-file-l2-1-0.dll", "CopyFile2");
const auto setattr_w = resolve<SetFileAttributesWFn>("api-ms-win-core-file-l1-1-0.dll", "SetFileAttributesW");
const auto setattr_a = resolve<SetFileAttributesAFn>("kernel32.dll", "SetFileAttributesA");
check(mkdir_w && mkdir_a && mkdir_ex_w && rmdir_w && rmdir_a && move_w && move_ex_w && move_a && copy_w &&
copy_a && copy_ex_w && copy2 && setattr_w && setattr_a,
"the folder, move, copy and attribute calls resolve through the registry");
write_path(package / "misc" / "one.txt", "one");
write_path(package / "misc" / "two.txt", "two");
write_path(package / "misc" / "three.txt", "three");
write_path(package / "misc" / "inner" / "deep.txt", "deep");
write_path(package / "misc" / "keep.txt", "keep");
// CreateDirectory: the new folder is in the overlay; names the title can already see are refused.
{
check(mkdir_w(L"GAME:\\cache", nullptr) != FALSE, "CreateDirectoryW makes GAME:\\cache");
check(std::filesystem::is_directory(overlay / "cache"), "in the overlay");
check(!std::filesystem::exists(package / "cache"), "and not in the package");
const DWORD a = attributes_w(L"GAME:\\cache");
check(a != INVALID_FILE_ATTRIBUTES && (a & FILE_ATTRIBUTE_DIRECTORY) != 0, "the new folder resolves");
check(contains(list(first_w, next_w, find_close, L"GAME:\\*"), L"cache"), "and is listed");
check(mkdir_a("GAME:\\cache\\narrow", nullptr) != FALSE && std::filesystem::is_directory(overlay / "cache" / "narrow"),
"CreateDirectoryA takes a console path");
check(mkdir_ex_w(L"GAME:\\misc", L"GAME:\\cache\\from_template", nullptr) != FALSE &&
std::filesystem::is_directory(overlay / "cache" / "from_template"),
"CreateDirectoryExW maps both the template and the new folder");
SetLastError(0);
check(mkdir_w(L"GAME:\\misc", nullptr) == FALSE && GetLastError() == ERROR_ALREADY_EXISTS,
"a folder only the package has already exists");
check(!std::filesystem::exists(overlay / "misc"), "and nothing is mirrored for it");
SetLastError(0);
check(mkdir_w(L"GAME:\\misc\\one.txt", nullptr) == FALSE && GetLastError() == ERROR_ALREADY_EXISTS,
"a package file's name already exists");
SetLastError(0);
check(mkdir_w(L"GAME:\\", nullptr) == FALSE && GetLastError() == ERROR_ALREADY_EXISTS, "GAME:\\ already exists");
SetLastError(0);
check(mkdir_w(L"GAME:\\nowhere\\child", nullptr) == FALSE && GetLastError() == ERROR_PATH_NOT_FOUND,
"a folder whose parent does not exist reports ERROR_PATH_NOT_FOUND");
check(!std::filesystem::exists(overlay / "nowhere"), "and leaves no parent behind");
check(mkdir_w(L"GAME:\\misc\\made", nullptr) != FALSE && std::filesystem::is_directory(overlay / "misc" / "made"),
"a new folder inside a package folder lands in the overlay");
}
// RemoveDirectory: an overlay folder goes; a package folder is hidden by a marker once it is empty.
{
check(rmdir_w(L"GAME:\\cache\\narrow") != FALSE && !std::filesystem::exists(overlay / "cache" / "narrow"),
"RemoveDirectoryW removes an empty overlay folder");
SetLastError(0);
check(rmdir_w(L"GAME:\\cache\\narrow") == FALSE && GetLastError() == ERROR_FILE_NOT_FOUND,
"removing it again reports ERROR_FILE_NOT_FOUND");
SetLastError(0);
check(rmdir_w(L"GAME:\\misc\\inner") == FALSE && GetLastError() == ERROR_DIR_NOT_EMPTY,
"a package folder with files is not empty");
SetLastError(0);
check(rmdir_w(L"GAME:\\misc\\keep.txt") == FALSE && GetLastError() == ERROR_DIRECTORY,
"a file is not a folder");
SetLastError(0);
check(rmdir_w(L"GAME:\\") == FALSE && GetLastError() == ERROR_ACCESS_DENIED, "GAME:\\ cannot be removed");
check(delete_w(L"GAME:\\misc\\inner\\deep.txt") != FALSE, "once its file is deleted");
check(rmdir_a("GAME:\\misc\\inner") != FALSE, "RemoveDirectoryA removes the now empty package folder");
check(std::filesystem::exists(package / "misc" / "inner" / "deep.txt"), "the package keeps the folder and file");
SetLastError(0);
check(attributes_w(L"GAME:\\misc\\inner") == INVALID_FILE_ATTRIBUTES && GetLastError() == ERROR_FILE_NOT_FOUND,
"the removed folder no longer resolves");
check(!contains(list(first_w, next_w, find_close, L"GAME:\\misc\\*"), L"inner"), "and is not listed");
const DWORD misc = attributes_w(L"GAME:\\misc");
check(misc != INVALID_FILE_ATTRIBUTES && (misc & FILE_ATTRIBUTE_DIRECTORY) != 0, "its parent is still there");
check(mkdir_w(L"GAME:\\misc\\inner", nullptr) != FALSE, "the removed name can be made again");
check(list(first_w, next_w, find_close, L"GAME:\\misc\\inner\\*").size() == 2,
"and the new folder is empty: only . and ..");
}
{
const std::filesystem::path host = root / "host_missing";
write_path(host / "file.txt", "file");
const std::wstring host_dest = (host / "dest.txt").wstring();
auto error_of = [](BOOL ok) { return ok ? DWORD{0} : GetLastError(); };
struct Name {
const wchar_t* game;
std::filesystem::path host;
DWORD expected;
const char* what;
};
const Name names[] = {
{L"GAME:\\nowhere\\x.txt", host / "nowhere" / "x.txt", ERROR_PATH_NOT_FOUND, "inside a missing folder"},
{L"GAME:\\misc\\keep.txt\\x.txt", host / "file.txt" / "x.txt", ERROR_PATH_NOT_FOUND, "below a file"},
{L"GAME:\\misc\\absent.txt", host / "absent.txt", ERROR_FILE_NOT_FOUND, "missing from a folder that exists"},
};
for (const Name& n : names) {
const std::wstring native = n.host.wstring();
const std::string suffix = std::string(" of a name ") + n.what + " fails as on the host";
SetLastError(0);
const DWORD host_delete = error_of(::DeleteFileW(native.c_str()));
SetLastError(0);
check(error_of(delete_w(n.game)) == n.expected && host_delete == n.expected, ("DeleteFileW" + suffix).c_str());
SetLastError(0);
const DWORD host_rmdir = error_of(::RemoveDirectoryW(native.c_str()));
SetLastError(0);
check(error_of(rmdir_w(n.game)) == n.expected && host_rmdir == n.expected,
("RemoveDirectoryW" + suffix).c_str());
SetLastError(0);
const DWORD host_setattr = error_of(::SetFileAttributesW(native.c_str(), FILE_ATTRIBUTE_NORMAL));
SetLastError(0);
check(error_of(setattr_w(n.game, FILE_ATTRIBUTE_NORMAL)) == n.expected && host_setattr == n.expected,
("SetFileAttributesW" + suffix).c_str());
SetLastError(0);
const DWORD host_move = error_of(::MoveFileExW(native.c_str(), host_dest.c_str(), MOVEFILE_REPLACE_EXISTING));
SetLastError(0);
check(error_of(move_ex_w(n.game, L"GAME:\\misc\\dest.txt", MOVEFILE_REPLACE_EXISTING)) == n.expected &&
host_move == n.expected,
("MoveFileExW" + suffix).c_str());
}
check(!std::filesystem::exists(overlay / "nowhere") && !std::filesystem::exists(overlay / "misc" / "dest.txt"),
"and none of them leaves anything in the overlay");
}
// Recreating a removed package directory clears its own marker, but not nested deletion markers.
{
std::filesystem::create_directories(package / "recreate_dir");
const auto mark = overlay / ".gta6rt-deleted" / "recreate_dir" / ".gta6rt-dir";
check(rmdir_w(L"GAME:\\recreate_dir") != FALSE && std::filesystem::exists(mark),
"removing a package directory creates its marker");
check(mkdir_w(L"GAME:\\recreate_dir", nullptr) != FALSE, "recreating the package directory succeeds");
const DWORD attrs = attributes_w(L"GAME:\\recreate_dir");
check(attrs != INVALID_FILE_ATTRIBUTES && (attrs & FILE_ATTRIBUTE_DIRECTORY), "recreated directory has directory attributes");
check(count(list(first_w, next_w, find_close, L"GAME:\\*"), L"recreate_dir") == 1,
"recreated directory is listed once");
check(!std::filesystem::exists(mark), "recreated directory has no removal marker");
check(mkdir_w(L"GAME:\\never_removed_dir", nullptr) != FALSE &&
std::filesystem::is_directory(overlay / "never_removed_dir") &&
!std::filesystem::exists(overlay / ".gta6rt-deleted" / "never_removed_dir"),
"creating a never-removed directory is unchanged");
}
// A marker held without delete sharing makes recreation fail and rolls back the overlay directory.
{
std::filesystem::create_directories(package / "held_marker_dir");
const auto mark = overlay / ".gta6rt-deleted" / "held_marker_dir" / ".gta6rt-dir";
check(rmdir_w(L"GAME:\\held_marker_dir") != FALSE, "removing the directory creates a marker to hold");
const HANDLE held = ::CreateFileW(mark.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, nullptr);
check(held != INVALID_HANDLE_VALUE, "opened the removal marker without delete sharing");
if (held != INVALID_HANDLE_VALUE) {
const BOOL created = mkdir_w(L"GAME:\\held_marker_dir", nullptr);
const DWORD error = GetLastError();
check(!created && (error == ERROR_SHARING_VIOLATION || error == ERROR_ACCESS_DENIED),
"recreating a directory reports the marker deletion error");
check(!std::filesystem::exists(overlay / "held_marker_dir") && std::filesystem::exists(mark),
"failed recreation removes the overlay directory and keeps the marker");
::CloseHandle(held);
check(mkdir_w(L"GAME:\\held_marker_dir", nullptr) != FALSE,
"recreating the directory succeeds after releasing the marker");
}
}
// RemoveDirectory of a folder on one thread and, on another, a creating open of a file in it (CREATE_NEW,
// CREATE_ALWAYS, OPEN_ALWAYS) or CreateDirectory of a folder in it. The host orders the two: the remove goes first
// and the create fails with ERROR_PATH_NOT_FOUND, or the create goes first and the remove fails with
// ERROR_DIR_NOT_EMPTY. Both succeeding left the removed folder back in the title's view with the new entry in it.
// Each round takes a folder of its own that only the package has, empty in the title's view, and starts the two
// threads together; the remover waits a few more spins each round so the calls meet at different points.
{
constexpr int kRounds = 2000;
const auto race = [&](bool make_folder, const wchar_t* prefix, const char* what) {
for (int i = 0; i < kRounds; ++i)
std::filesystem::create_directories(package / "rmrace" / (prefix + std::to_wstring(i)));
int create_first = 0, remove_first = 0, both = 0, other = 0, back = 0;
for (int i = 0; i < kRounds; ++i) {
const std::wstring folder = L"GAME:\\rmrace\\" + (prefix + std::to_wstring(i));
const std::wstring child = folder + (make_folder ? L"\\sub" : L"\\new.tmp");
const DWORD disposition = i % 3 == 0 ? CREATE_NEW : i % 3 == 1 ? CREATE_ALWAYS : OPEN_ALWAYS;
std::atomic<int> ready{0};
HANDLE handle = INVALID_HANDLE_VALUE;
bool made = false;
DWORD create_error = 0;
std::thread creator([&] {
ready.fetch_add(1);
while (ready.load() < 2) {
}
if (make_folder) {
made = mkdir_w(child.c_str(), nullptr) != FALSE;
} else {
handle = create_w(child.c_str(), GENERIC_WRITE, 0, nullptr, disposition, 0, nullptr);
made = handle != INVALID_HANDLE_VALUE;
}
create_error = made ? 0 : GetLastError();
});
ready.fetch_add(1);
while (ready.load() < 2) {
}
for (int spin = 0; spin < (i % 64) * 64; ++spin) YieldProcessor();
const bool removed = rmdir_w(folder.c_str()) != FALSE;
const DWORD remove_error = removed ? 0 : GetLastError();
creator.join();
if (handle != INVALID_HANDLE_VALUE) ::CloseHandle(handle);
if (made && removed) {
++both;
} else if (made && remove_error == ERROR_DIR_NOT_EMPTY) {
++create_first;
} else if (removed && create_error == ERROR_PATH_NOT_FOUND) {
++remove_first;
} else {
++other;
}
// A folder the remove took away stays away, whatever the create did.
if (removed) {
SetLastError(0);
if (attributes_w(folder.c_str()) != INVALID_FILE_ATTRIBUTES || GetLastError() != ERROR_FILE_NOT_FOUND)
++back;
}
}
std::printf("NOTE %s against RemoveDirectoryW, %d rounds: create first %d, remove first %d, both succeeded "
"%d, other %d, removed folder back %d\n",
what, kRounds, create_first, remove_first, both, other, back);
const std::string w = what;
check(both == 0, (w + " and RemoveDirectoryW of its folder never both succeed").c_str());
check(other == 0, (w + " or RemoveDirectoryW fails as on the host (ERROR_PATH_NOT_FOUND or "
"ERROR_DIR_NOT_EMPTY)").c_str());
check(back == 0, (w + ": a folder RemoveDirectoryW removed stays removed").c_str());
};
race(false, L"f", "a creating CreateFileW");
race(true, L"d", "CreateDirectoryW");
}
// MoveFile: the destination is in the overlay; a moved package file is deleted by a marker.
{
check(move_w(L"GAME:\\misc\\one.txt", L"GAME:\\misc\\moved.txt") != FALSE, "MoveFileW moves a package file");
check(read_path(overlay / "misc" / "moved.txt") == "one", "its bytes are in the overlay under the new name");
check(read_path(package / "misc" / "one.txt") == "one", "the package file is not modified");
SetLastError(0);
check(attributes_w(L"GAME:\\misc\\one.txt") == INVALID_FILE_ATTRIBUTES && GetLastError() == ERROR_FILE_NOT_FOUND,
"the old name is gone");
SetLastError(0);
check(move_ex_w(L"GAME:\\misc\\moved.txt", L"GAME:\\misc\\two.txt", 0) == FALSE &&
GetLastError() == ERROR_ALREADY_EXISTS,
"MoveFileExW will not replace a package file without MOVEFILE_REPLACE_EXISTING");
check(move_ex_w(L"GAME:\\misc\\moved.txt", L"GAME:\\misc\\two.txt", MOVEFILE_REPLACE_EXISTING) != FALSE,
"and replaces it with the flag");
HANDLE h = create_w(L"GAME:\\misc\\two.txt", GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
check(h != INVALID_HANDLE_VALUE && read_handle(h) == "one", "the replaced name reads the moved bytes");
if (h != INVALID_HANDLE_VALUE) ::CloseHandle(h);
check(read_path(package / "misc" / "two.txt") == "two", "the package's own two.txt is untouched");
check(!std::filesystem::exists(overlay / "misc" / "moved.txt"), "the overlay source of the move is gone");
check(move_a("GAME:\\misc\\three.txt", "GAME:\\cache\\three.txt") != FALSE &&
read_path(overlay / "cache" / "three.txt") == "three",
"MoveFileA moves into another folder");
check(move_w(L"GAME:\\cache\\from_template", L"GAME:\\cache\\renamed") != FALSE &&
std::filesystem::is_directory(overlay / "cache" / "renamed"),
"an overlay folder is renamed");
SetLastError(0);
check(move_w(L"GAME:\\misc", L"GAME:\\misc2") == FALSE && GetLastError() == ERROR_ACCESS_DENIED,
"a package folder is not moved");
SetLastError(0);
// G: and T: outside the configured roots are console aliases, even when TEMP uses that host drive.
// Paths within the synthetic package root are explicitly host paths on every drive.
check(move_w(L"GAME:\\misc\\keep.txt", (package / "host.txt").c_str()) == FALSE &&
GetLastError() == ERROR_NOT_SAME_DEVICE,
"a move between GAME: and a host path is refused as cross-volume");
SetLastError(0);
check(move_ex_w(L"GAME:\\misc\\keep.txt", nullptr, MOVEFILE_DELAY_UNTIL_REBOOT) == FALSE &&
GetLastError() == ERROR_INVALID_PARAMETER,
"a boot-time delete of a console file is refused");
SetLastError(0);
check(move_w(L"GAME:\\misc\\gone.txt", L"GAME:\\misc\\x.txt") == FALSE && GetLastError() == ERROR_FILE_NOT_FOUND,
"moving a missing file reports ERROR_FILE_NOT_FOUND");
SetLastError(0);
check(move_w(L"GAME:\\misc\\keep.txt", L"GAME:\\nowhere\\keep.txt") == FALSE &&
GetLastError() == ERROR_PATH_NOT_FOUND,
"moving into a missing folder reports ERROR_PATH_NOT_FOUND");
}
// MOVEFILE_WRITE_THROUGH on a package file: the copy and the marker are flushed before the call returns.
{
write_path(package / "misc" / "wt.txt", "wt");
check(move_ex_w(L"GAME:\\misc\\wt.txt", L"GAME:\\misc\\wt_moved.txt", MOVEFILE_WRITE_THROUGH) != FALSE,
"MoveFileExW with MOVEFILE_WRITE_THROUGH moves a package file");
check(read_path(overlay / "misc" / "wt_moved.txt") == "wt", "its bytes are in the overlay");
SetLastError(0);
check(attributes_w(L"GAME:\\misc\\wt.txt") == INVALID_FILE_ATTRIBUTES && GetLastError() == ERROR_FILE_NOT_FOUND,
"and the old name is deleted");
}
// CopyFile: reads where a read would, writes the overlay; an existing name counts even if only the package has it.
{
check(copy_w(L"GAME:\\misc\\keep.txt", L"GAME:\\cache\\keep_copy.txt", TRUE) != FALSE &&
read_path(overlay / "cache" / "keep_copy.txt") == "keep",
"CopyFileW copies a package file into the overlay");
check(read_path(package / "misc" / "keep.txt") == "keep", "and the source stays");
SetLastError(0);
check(copy_w(L"GAME:\\misc\\keep.txt", L"GAME:\\data\\sub", FALSE) == FALSE && GetLastError() == ERROR_ACCESS_DENIED,
"a copy onto a folder is refused");
write_path(package / "misc" / "target.txt", "target");
SetLastError(0);
check(copy_w(L"GAME:\\misc\\keep.txt", L"GAME:\\misc\\target.txt", TRUE) == FALSE &&
GetLastError() == ERROR_FILE_EXISTS,
"fail-if-exists sees a name only the package has");
check(copy_a("GAME:\\misc\\keep.txt", "GAME:\\misc\\target.txt", FALSE) != FALSE &&
read_path(overlay / "misc" / "target.txt") == "keep" && read_path(package / "misc" / "target.txt") == "target",
"CopyFileA overwrites through the overlay and leaves the package file alone");
// Keep this host path inside a configured root too; an unqualified G:/T: path is virtual.
const std::filesystem::path host_copy = package / "host_copy.txt";
check(copy_w(L"GAME:\\misc\\keep.txt", host_copy.c_str(), TRUE) != FALSE && read_path(host_copy) == "keep",
"a copy from GAME: to a host path works");
check(copy_ex_w(host_copy.c_str(), L"GAME:\\cache\\from_host.txt", nullptr, nullptr, nullptr, COPY_FILE_FAIL_IF_EXISTS) !=
FALSE &&
read_path(overlay / "cache" / "from_host.txt") == "keep",
"CopyFileExW copies from a host path into GAME:");
COPYFILE2_EXTENDED_PARAMETERS params{};
params.dwSize = sizeof(params);
params.dwCopyFlags = COPY_FILE_FAIL_IF_EXISTS;
check(copy2(L"GAME:\\misc\\keep.txt", L"GAME:\\cache\\copy2.txt", ¶ms) == S_OK &&
read_path(overlay / "cache" / "copy2.txt") == "keep",
"CopyFile2 copies too");
check(copy2(L"GAME:\\misc\\keep.txt", L"GAME:\\misc\\two.txt", ¶ms) == HRESULT_FROM_WIN32(ERROR_FILE_EXISTS),
"and returns its HRESULT for an existing name");
// CopyFile2 again, through the host's CopyFile2 (where it has one) and through the CopyFileExW fallback the
// shim uses where it has none (Wine 9), so both are checked wherever the test runs.
for (const bool fallback : {false, true}) {
rt::fs::force_copy_file2_fallback(fallback);
const std::string mode = fallback ? " (fallback)" : " (host CopyFile2, or the fallback without one)";
const std::wstring dir = fallback ? L"GAME:\\copy2_fallback" : L"GAME:\\copy2_host";
const std::filesystem::path out = overlay / (fallback ? "copy2_fallback" : "copy2_host");
const auto to = [&](const wchar_t* name) { return dir + L"\\" + name; };
check(mkdir_w(dir.c_str(), nullptr) != FALSE, ("a folder for the CopyFile2 checks" + mode).c_str());
check(copy2(L"GAME:\\misc\\keep.txt", to(L"null.txt").c_str(), nullptr) == S_OK &&
read_path(out / "null.txt") == "keep",
("CopyFile2 without a parameter block copies" + mode).c_str());
for (const DWORD size : {DWORD{0}, DWORD{4}, DWORD{sizeof(COPYFILE2_EXTENDED_PARAMETERS) - 1}}) {
COPYFILE2_EXTENDED_PARAMETERS short_block{};
short_block.dwSize = size;
check(copy2(L"GAME:\\misc\\keep.txt", to(L"short.txt").c_str(), &short_block) == E_INVALIDARG &&
!std::filesystem::exists(out / "short.txt"),
("a parameter block of " + std::to_string(size) +
" bytes is E_INVALIDARG and writes nothing" + mode)
.c_str());
}
COPYFILE2_EXTENDED_PARAMETERS block{};
block.dwSize = sizeof(block);
block.dwCopyFlags = COPY_FILE_FAIL_IF_EXISTS | 0x00040000 /* NO_OFFLOAD, a hint */;
check(copy2(L"GAME:\\misc\\keep.txt", to(L"hint.txt").c_str(), &block) == S_OK &&
read_path(out / "hint.txt") == "keep",
("fail-if-exists with a hint flag copies" + mode).c_str());
check(copy2(L"GAME:\\misc\\keep.txt", to(L"hint.txt").c_str(), &block) ==
HRESULT_FROM_WIN32(ERROR_FILE_EXISTS),
("and refuses an existing name" + mode).c_str());
BOOL cancel = TRUE;
block.dwCopyFlags = 0;
block.pfCancel = &cancel;
const auto parent = fallback ? "copy2_package_fallback" : "copy2_package_host";
const auto destination = fallback ? L"GAME:\\copy2_package_fallback\\copy.txt" :
L"GAME:\\copy2_package_host\\copy.txt";
std::filesystem::create_directories(package / parent);
check(copy2(L"GAME:\\misc\\keep.txt", destination, &block) ==
HRESULT_FROM_WIN32(ERROR_REQUEST_ABORTED) &&
!std::filesystem::exists(overlay / parent),
("a cancelled copy does not create its overlay parent" + mode).c_str());
cancel = FALSE;
check(copy2(L"GAME:\\misc\\keep.txt", destination, &block) == S_OK &&
read_path(overlay / parent / "copy.txt") == "keep",
("an uncancelled copy creates its overlay parent" + mode).c_str());
cancel = TRUE;
check(copy2(L"GAME:\\misc\\keep.txt", to(L"cancelled.txt").c_str(), &block) ==
HRESULT_FROM_WIN32(ERROR_REQUEST_ABORTED) &&
!std::filesystem::exists(out / "cancelled.txt"),
("a cancel flag set before the copy cancels it" + mode).c_str());
cancel = FALSE;
check(copy2(L"GAME:\\misc\\keep.txt", to(L"cancelled.txt").c_str(), &block) == S_OK &&
read_path(out / "cancelled.txt") == "keep",
("clearing the cancel flag allows a retry" + mode).c_str());
block.pfCancel = nullptr;
if (fallback) {
// A flag that changes what the copy produces, and which CopyFileExW can't do, is refused before
// anything is written: the fallback doesn't copy the alternate streams it was told to skip.
block.dwCopyFlags = 0x00008000 /* COPY_FILE_SKIP_ALTERNATE_STREAMS */;
check(copy2(L"GAME:\\misc\\keep.txt", to(L"streams.txt").c_str(), &block) ==
HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED) &&
!std::filesystem::exists(out / "streams.txt"),
"the fallback refuses SKIP_ALTERNATE_STREAMS and writes nothing");
block.dwCopyFlags = 0x00000080 /* COPY_FILE_DIRECTORY */;
check(copy2(L"GAME:\\misc\\keep.txt", to(L"directory.txt").c_str(), &block) ==
HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED),
"and COPY_FILE_DIRECTORY");
block.dwCopyFlags = 0;
block.dwSize = sizeof(block) + 8; // a longer (V2-sized) block: its extra fields have no equivalent
check(copy2(L"GAME:\\misc\\keep.txt", to(L"v2.txt").c_str(), &block) ==
HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED) &&
!std::filesystem::exists(out / "v2.txt"),
"and a block longer than COPYFILE2_EXTENDED_PARAMETERS");
}
}
rt::fs::force_copy_file2_fallback(false);
SetLastError(0);
check(copy_w(L"GAME:\\misc\\one.txt", L"GAME:\\cache\\one.txt", FALSE) == FALSE && GetLastError() == ERROR_FILE_NOT_FOUND,
"a deleted name cannot be copied");
}
// SetFileAttributes: the overlay copy gets the attributes; the package keeps its own.
{
const DWORD before = ::GetFileAttributesW((package / "misc" / "keep.txt").c_str());
check(setattr_w(L"GAME:\\misc\\keep.txt", FILE_ATTRIBUTE_READONLY) != FALSE, "SetFileAttributesW on a package file");
const DWORD now = attributes_w(L"GAME:\\misc\\keep.txt");
check(now != INVALID_FILE_ATTRIBUTES && (now & FILE_ATTRIBUTE_READONLY) != 0, "the title sees the new attributes");
check(read_path(overlay / "misc" / "keep.txt") == "keep", "on a copy of the file in the overlay");
check(::GetFileAttributesW((package / "misc" / "keep.txt").c_str()) == before, "the package file keeps its own");
check(setattr_a("GAME:\\misc\\keep.txt", FILE_ATTRIBUTE_NORMAL) != FALSE &&
(attributes_w(L"GAME:\\misc\\keep.txt") & FILE_ATTRIBUTE_READONLY) == 0,
"SetFileAttributesA changes the overlay copy back");
check(setattr_w(L"GAME:\\data\\sub", FILE_ATTRIBUTE_HIDDEN) != FALSE &&
(attributes_w(L"GAME:\\data\\sub") & FILE_ATTRIBUTE_HIDDEN) != 0,
"a package folder's attributes are set on an overlay mirror");
check(count(list(first_w, next_w, find_close, L"GAME:\\data\\*"), L"sub") == 1, "which is listed once");
SetLastError(0);
check(setattr_w(L"GAME:\\misc\\one.txt", FILE_ATTRIBUTE_NORMAL) == FALSE && GetLastError() == ERROR_FILE_NOT_FOUND,
"a deleted name has no attributes to set");
}
// Nothing leaves the package and the overlay.
{
SetLastError(0);
check(mkdir_w(L"GAME:\\..\\escape_dir", nullptr) == FALSE && GetLastError() == ERROR_INVALID_NAME &&
!std::filesystem::exists(root / "escape_dir"),
"a folder above GAME: is refused");
SetLastError(0);
check(move_w(L"GAME:\\misc\\keep.txt", L"GAME:\\..\\escape.txt") == FALSE && GetLastError() == ERROR_INVALID_NAME,
"a move above GAME: is refused");
SetLastError(0);
check(copy_w(L"GAME:\\misc\\keep.txt", L"GAME:\\C:\\x", FALSE) == FALSE && GetLastError() == ERROR_INVALID_NAME,
"a copy to a drive inside GAME: is refused");
SetLastError(0);
check(rmdir_w(L"GAME:\\..") == FALSE && GetLastError() == ERROR_INVALID_NAME, "removing above GAME: is refused");
}
// Nothing leaves the package and the overlay.
{
SetLastError(0);
const HANDLE h = create_w(L"GAME:\\..\\escape.txt", GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, 0, nullptr);
check(h == INVALID_HANDLE_VALUE && GetLastError() == ERROR_INVALID_NAME, "a path above GAME: is refused");
if (h != INVALID_HANDLE_VALUE) ::CloseHandle(h);
check(!std::filesystem::exists(root / "escape.txt"), "and nothing was created outside");
SetLastError(0);
check(attributes_w(L"GAME:\\..\\x") == INVALID_FILE_ATTRIBUTES && GetLastError() == ERROR_INVALID_NAME,
"a stat above GAME: is refused");
SetLastError(0);
check(create_w(L"GAME:\\C:\\x", GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, 0, nullptr) == INVALID_HANDLE_VALUE &&
GetLastError() == ERROR_INVALID_NAME,
"a drive inside GAME: is refused");
SetLastError(0);
WIN32_FIND_DATAW found{};
check(first_w(L"GAME:\\..\\*", &found) == INVALID_HANDLE_VALUE && GetLastError() == ERROR_INVALID_NAME,
"a listing above GAME: is refused");
SetLastError(0);
check(first_w(L"GAME:\\data\\*", nullptr) == INVALID_HANDLE_VALUE &&
GetLastError() == ERROR_INVALID_PARAMETER,
"a null listing buffer is refused");
}
// CreateFile2 and FindFirstFileExA/W (used by GDK/OneCore titles and std::filesystem).
{
const auto create2 = resolve<CreateFile2Fn>("api-ms-win-core-file-l1-2-0.dll", "CreateFile2");
const auto first_ex_w = resolve<FindFirstFileExWFn>("api-ms-win-core-file-l1-1-0.dll", "FindFirstFileExW");
const auto first_ex_a = resolve<FindFirstFileExAFn>("kernelbase.dll", "FindFirstFileExA");
check(create2 && first_ex_w && first_ex_a, "CreateFile2 and FindFirstFileExA/W are registered");
// CreateFile2 reads an untouched package file without modifying it.
write_path(package / "data" / "cf2_pkg.txt", "pkg-cf2");
CREATEFILE2_EXTENDED_PARAMETERS ext{};
ext.dwSize = sizeof(ext);
ext.dwFileAttributes = FILE_ATTRIBUTE_NORMAL;
const HANDLE h_read = create2(L"GAME:\\data\\cf2_pkg.txt", GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING, &ext);
check(h_read != INVALID_HANDLE_VALUE && read_handle(h_read) == "pkg-cf2",
"CreateFile2 reads a GAME: file from the package");
if (h_read != INVALID_HANDLE_VALUE) ::CloseHandle(h_read);
// CreateFile2 write goes to the overlay, leaving the package untouched.
const HANDLE h_write = create2(L"GAME:\\data\\cf2.txt", GENERIC_WRITE, 0, CREATE_ALWAYS, &ext);
check(h_write != INVALID_HANDLE_VALUE, "CreateFile2 creates a new GAME: file in the overlay");
if (h_write != INVALID_HANDLE_VALUE) {
DWORD written = 0;
::WriteFile(h_write, "cf2", 3, &written, nullptr);
::CloseHandle(h_write);
}
check(read_path(overlay / "data" / "cf2.txt") == "cf2" && !std::filesystem::exists(package / "data" / "cf2.txt"),
"CreateFile2 writes land in the overlay and never in the package");
// CreateFile2 refuses path traversal.
SetLastError(0);
const HANDLE h_esc = create2(L"GAME:\\..\\cf2_escape.txt", GENERIC_WRITE, 0, CREATE_ALWAYS, &ext);
check(h_esc == INVALID_HANDLE_VALUE && GetLastError() == ERROR_INVALID_NAME,
"CreateFile2 refuses a path above GAME:");
if (h_esc != INVALID_HANDLE_VALUE) ::CloseHandle(h_esc);
// MAXIMUM_ALLOWED asks for every right the file grants, write included, without naming one. Such an open of
// a package file is a write-open: it gets the overlay copy, so a write through the handle cannot reach the
// package. CreateFileW, CreateFileA and CreateFile2 share the rule. The write is shorter than the file, so
// the bytes after it show the overlay copy started as the package file's contents (OPEN_EXISTING keeps them).
{
write_path(package / "data" / "max_w.txt", "pkg-max");
write_path(package / "data" / "max_a.txt", "pkg-max");
write_path(package / "data" / "max_2.txt", "pkg-max");
const auto write_through = [](HANDLE h) {
if (h == INVALID_HANDLE_VALUE) return false;
DWORD written = 0;
const BOOL ok = ::WriteFile(h, "OV", 2, &written, nullptr);
::CloseHandle(h);
return ok != FALSE && written == 2;
};
check(write_through(create_w(L"GAME:\\data\\max_w.txt", MAXIMUM_ALLOWED, 0, nullptr, OPEN_EXISTING, 0,
nullptr)),
"CreateFileW(MAXIMUM_ALLOWED, OPEN_EXISTING) opens a package file for writing");
check(read_path(package / "data" / "max_w.txt") == "pkg-max",
"a write through a MAXIMUM_ALLOWED handle does not reach the package (CreateFileW)");
check(read_path(overlay / "data" / "max_w.txt") == "OVg-max",
"the write through a MAXIMUM_ALLOWED handle is in the overlay copy (CreateFileW)");
check(write_through(create_a("GAME:\\data\\max_a.txt", MAXIMUM_ALLOWED | GENERIC_READ, 0, nullptr,
OPEN_EXISTING, 0, nullptr)),
"CreateFileA(MAXIMUM_ALLOWED | GENERIC_READ, OPEN_EXISTING) opens a package file for writing");
check(read_path(package / "data" / "max_a.txt") == "pkg-max" &&
read_path(overlay / "data" / "max_a.txt") == "OVg-max",
"the write goes to the overlay and not the package (CreateFileA)");
check(write_through(create2(L"GAME:\\data\\max_2.txt", MAXIMUM_ALLOWED, 0, OPEN_EXISTING, &ext)),
"CreateFile2(MAXIMUM_ALLOWED, OPEN_EXISTING) opens a package file for writing");
check(read_path(package / "data" / "max_2.txt") == "pkg-max" &&
read_path(overlay / "data" / "max_2.txt") == "OVg-max",
"the write goes to the overlay and not the package (CreateFile2)");
// A plain read still opens the package file in place: no overlay copy appears for it.
write_path(package / "data" / "max_read.txt", "pkg-max");
const HANDLE h_plain = create_w(L"GAME:\\data\\max_read.txt", GENERIC_READ, FILE_SHARE_READ, nullptr,
OPEN_EXISTING, 0, nullptr);
check(h_plain != INVALID_HANDLE_VALUE && read_handle(h_plain) == "pkg-max" &&
!std::filesystem::exists(overlay / "data" / "max_read.txt"),
"a GENERIC_READ open is still a read: no overlay copy is made");
if (h_plain != INVALID_HANDLE_VALUE) ::CloseHandle(h_plain);
}
// FindFirstFileExW merges package + overlay and honours FindExInfoBasic & FindExSearchLimitToDirectories.
WIN32_FIND_DATAW fd{};
std::vector<std::wstring> basic_names;
bool alt_empty = true;
HANDLE hf = first_ex_w(L"GAME:\\data\\*", FindExInfoBasic, &fd, FindExSearchNameMatch, nullptr, 0);
check(hf != INVALID_HANDLE_VALUE, "FindFirstFileExW opens a GAME: directory listing");
if (hf != INVALID_HANDLE_VALUE) {
do {
basic_names.emplace_back(fd.cFileName);
if (fd.cAlternateFileName[0] != L'\0') alt_empty = false;
} while (next_w(hf, &fd));
find_close(hf);
}
check(contains(basic_names, L"hello.txt") && contains(basic_names, L"cf2.txt") && !contains(basic_names, L"bye.txt"),
"FindFirstFileExW merges overlay + package and hides deleted files");
check(alt_empty, "FindFirstFileExW(FindExInfoBasic) leaves cAlternateFileName empty");
std::vector<std::wstring> dir_names;
HANDLE hdir = first_ex_w(L"GAME:\\data\\*", FindExInfoStandard, &fd, FindExSearchLimitToDirectories, nullptr, 0);
check(hdir != INVALID_HANDLE_VALUE, "FindFirstFileExW(FindExSearchLimitToDirectories) succeeds");
if (hdir != INVALID_HANDLE_VALUE) {
do {
dir_names.emplace_back(fd.cFileName);
} while (next_w(hdir, &fd));
find_close(hdir);
}
check(contains(dir_names, L"sub") && !contains(dir_names, L"hello.txt"),
"FindFirstFileExW(FindExSearchLimitToDirectories) returns only directories");
// FindFirstFileExA works on GAME: paths too.
WIN32_FIND_DATAA fda{};
HANDLE hfa = first_ex_a("GAME:\\data\\cf2.txt", FindExInfoBasic, &fda, FindExSearchNameMatch, nullptr, 0);
check(hfa != INVALID_HANDLE_VALUE && std::string(fda.cFileName) == "cf2.txt",
"FindFirstFileExA finds an overlay file under GAME:");
if (hfa != INVALID_HANDLE_VALUE) find_close(hfa);
}
// Copy-up under two threads: both write-open the same untouched package file at once. Each open must succeed and
// the overlay copy must hold the package's bytes; the copy goes through a private name, so neither thread can find
// the name taken mid-copy or read a half-written file.
{
const std::string big(256 * 1024, 'p'); // long enough that the two copies overlap
bool all_opened = true, all_intact = true;
for (int round = 0; round < 50; ++round) {
const std::wstring name = L"race" + std::to_wstring(round) + L".bin";
write_path(package / "data" / name, big);
const std::wstring game = L"GAME:\\data\\" + name;
HANDLE handles[2] = {INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE};
std::thread threads[2];
for (int i = 0; i < 2; ++i)
threads[i] = std::thread([&, i] {
handles[i] = create_w(game.c_str(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
nullptr, OPEN_EXISTING, 0, nullptr); });
for (auto& t : threads) t.join();
for (HANDLE h : handles) {
if (h == INVALID_HANDLE_VALUE) all_opened = false;
else ::CloseHandle(h);
}
if (read_path(overlay / "data" / name) != big) all_intact = false;
}
check(all_opened, "two threads write-opening one package file both succeed");
check(all_intact, "the overlay copy made under two threads holds the whole package file");
std::error_code ec;
check(std::filesystem::is_empty(overlay / ".gta6rt-copy", ec) && !ec, "copy-up leaves no staging file behind");
// A leftover from a process killed mid-copy stays out of the title's view.
write_path(overlay / ".gta6rt-copy" / "123-1", "half");
check(!contains(list(first_w, next_w, find_close, L"GAME:\\*"), L".gta6rt-copy"),
"the copy staging folder is hidden from a GAME: listing");
}
// RT-218: "G:\" is the package and "T:\" a writable scratch folder in the overlay, as in the other console layers.
// A host path under the configured folders stays a host path, and a path that climbs out of the drive is refused.
{
write_path(package / "drives" / "sub" / "probe.txt", "probe");
HANDLE h = create_w(L"G:\\drives\\sub\\probe.txt", GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
check(h != INVALID_HANDLE_VALUE && read_handle(h) == "probe", "CreateFileW opens G:\\drives\\sub\\probe.txt from the package");
if (h != INVALID_HANDLE_VALUE) ::CloseHandle(h);
h = create_w(L"g:/drives/sub/probe.txt", GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
check(h != INVALID_HANDLE_VALUE, "the drive letter is case-insensitive and accepts forward slashes");
if (h != INVALID_HANDLE_VALUE) ::CloseHandle(h);
check(attributes_w(L"G:\\data\\sub") != INVALID_FILE_ATTRIBUTES, "GetFileAttributesW stats a G: folder");
SetLastError(0);
check(create_w(L"G:\\..\\x.txt", GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr) ==
INVALID_HANDLE_VALUE &&
GetLastError() == ERROR_INVALID_NAME,
"G:\\.. is refused with ERROR_INVALID_NAME");
int fresh_count = 0;
auto fresh_overlay = [&] {
const std::filesystem::path dir = root / ("fresh_overlay" + std::to_string(++fresh_count));
rt::fs::configure(package, dir);
return dir;
};
fresh_overlay();
SetLastError(0);
const DWORD t_root = attributes_w(L"T:\\");
check(t_root != INVALID_FILE_ATTRIBUTES && (t_root & FILE_ATTRIBUTE_DIRECTORY) != 0 && GetLastError() == 0,
"on a fresh overlay GetFileAttributesW(T:\\) is a folder, and making it leaves the last error alone");
fresh_overlay();
const std::vector<std::wstring> t_names = list(first_w, next_w, find_close, L"T:\\*");
check(t_names.size() == 2 && count(t_names, L".") == 1 && count(t_names, L"..") == 1,
"on a fresh overlay FindFirstFileW(T:\\*) lists the empty root as . and ..");
fresh_overlay();
check(mkdir_w(L"T:\\shadercache", nullptr) != FALSE,
"on a fresh overlay CreateDirectoryW(T:\\shadercache) succeeds");
std::filesystem::path fresh = fresh_overlay();
check(copy_w(L"GAME:\\data\\hello.txt", L"T:\\hello.tmp", FALSE) != FALSE &&
read_path(own_t_folder(fresh) / "hello.tmp") == "package",
"on a fresh overlay CopyFileW(GAME:\\data\\hello.txt, T:\\hello.tmp) succeeds");
fresh = fresh_overlay();
SetLastError(0);
check(rmdir_w(L"T:\\") == FALSE && GetLastError() == ERROR_ACCESS_DENIED &&
std::filesystem::is_directory(own_t_folder(fresh)),
"T:\\ cannot be removed, like GAME:\\");
fresh = fresh_overlay();
SetLastError(0);
check(move_w(L"T:\\", L"GAME:\\moved") == FALSE && GetLastError() == ERROR_ACCESS_DENIED &&
attributes_w(L"GAME:\\moved") == INVALID_FILE_ATTRIBUTES &&
std::filesystem::is_directory(own_t_folder(fresh)),
"T:\\ cannot be moved");
// The T:\\ root is a drive root like GAME:\\ for every call that names a file, not its host folder: no
// attributes are set on it, and it is not deleted, write-opened or copied onto (kevincommits, #1608).
fresh = fresh_overlay();
attributes_w(L"T:\\");
SetLastError(0);
check(setattr_w(L"T:\\", FILE_ATTRIBUTE_HIDDEN) == FALSE && GetLastError() == ERROR_ACCESS_DENIED &&
(::GetFileAttributesW(own_t_folder(fresh).c_str()) & FILE_ATTRIBUTE_HIDDEN) == 0,
"SetFileAttributesW(T:\\) is refused (ERROR_ACCESS_DENIED) and leaves the host folder alone");
SetLastError(0);
check(delete_w(L"T:\\") == FALSE && GetLastError() == ERROR_INVALID_NAME &&
std::filesystem::is_directory(own_t_folder(fresh)),
"DeleteFileW(T:\\) is ERROR_INVALID_NAME, as for GAME:\\");
SetLastError(0);
HANDLE t_root_write = create_w(L"T:\\", GENERIC_WRITE, 0, nullptr, OPEN_ALWAYS, 0, nullptr);
check(t_root_write == INVALID_HANDLE_VALUE && GetLastError() == ERROR_INVALID_NAME,
"a write-open of T:\\ is ERROR_INVALID_NAME, as for GAME:\\");
if (t_root_write != INVALID_HANDLE_VALUE) ::CloseHandle(t_root_write);
SetLastError(0);
check(copy_w(L"GAME:\\data\\hello.txt", L"T:\\", FALSE) == FALSE && GetLastError() == ERROR_INVALID_NAME,
"CopyFileW onto T:\\ is ERROR_INVALID_NAME, as onto GAME:\\");
fresh_overlay();
attributes_w(L"T:\\");
SetLastError(0);
const BOOL removed_by_game_path = rmdir_w(L"GAME:\\.gta6rt-temp");
check(removed_by_game_path != FALSE || GetLastError() != ERROR_ACCESS_DENIED,
"only a T: path names the T:\\ root: GAME:\\.gta6rt-temp is not refused as a drive root");
rt::fs::configure(package, overlay);
h = create_w(L"T:\\cache\\scratch.bin", GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, 0, nullptr);
check(h != INVALID_HANDLE_VALUE, "CreateFileW creates a file on T: (parent folders and all)");
if (h != INVALID_HANDLE_VALUE) {
DWORD written = 0;
::WriteFile(h, "scratch", 7, &written, nullptr);
::CloseHandle(h);
}
check(read_path(own_t_folder(overlay) / "cache" / "scratch.bin") == "scratch",
"a T: file lands in this process's scratch folder in the overlay");
check(!std::filesystem::exists(package / "cache" / "scratch.bin") && !std::filesystem::exists(package / ".gta6rt-temp"),
"and the package is untouched");
h = create_w(L"t:/cache/scratch.bin", GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
check(h != INVALID_HANDLE_VALUE && read_handle(h) == "scratch", "the T: file reads back");
if (h != INVALID_HANDLE_VALUE) ::CloseHandle(h);
check(attributes_w(L"GAME:\\cache\\scratch.bin") == INVALID_FILE_ATTRIBUTES,
"T: is its own drive: GAME: does not see the scratch file");
check(!contains(list(first_w, next_w, find_close, L"GAME:\\*"), L".gta6rt-temp"),
"a listing of GAME:\\ does not show the scratch folder");
SetLastError(0);
check(create_w(L"T:\\..\\data\\hello.txt", GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr) ==
INVALID_HANDLE_VALUE &&
GetLastError() == ERROR_INVALID_NAME,
"T:\\.. cannot climb out of the scratch folder into the package");
// The overlay's own folders (.gta6rt-deleted, .gta6rt-temp, .gta6rt-copy) are hidden from GAME:\ listings and,
// as an explicit GAME: or G: path, refused like any other invalid name, in every spelling parse_console()
// normalises (RT-294). T:\ still names the scratch folder, and an ordinary dotted name is not reserved.
write_path(overlay / ".gta6rt-temp" / "runtime.tmp", "runtime");
if (const std::filesystem::path t_dir = own_t_folder(overlay); !t_dir.empty())
write_path(t_dir / "runtime.tmp", "runtime");
write_path(overlay / ".gta6rt-deleted" / "data" / "gone.txt", "");
write_path(overlay / ".gta6rt-other" / "ok.txt", "ok");
for (const wchar_t* reserved : {L"GAME:\\.gta6rt-temp\\runtime.tmp", L"GAME:\\.GTA6RT-TEMP\\runtime.tmp",
L"game:/.gta6rt-temp/runtime.tmp", L"GAME:\\.\\.gta6rt-temp\\runtime.tmp",
L"GAME:\\data\\..\\.gta6rt-temp\\runtime.tmp", L"G:\\.gta6rt-temp\\runtime.tmp",
L"GAME:\\.gta6rt-deleted\\data\\gone.txt", L"GAME:\\.gta6rt-copy\\x.bin",
L"GAME:\\.gta6rt-temp"}) {
SetLastError(0);
const HANDLE refused = create_w(reserved, GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
check(refused == INVALID_HANDLE_VALUE && GetLastError() == ERROR_INVALID_NAME,
"an explicit GAME: path into a reserved overlay folder is refused (CreateFileW)");
if (refused != INVALID_HANDLE_VALUE) ::CloseHandle(refused);
SetLastError(0);
check(attributes_w(reserved) == INVALID_FILE_ATTRIBUTES && GetLastError() == ERROR_INVALID_NAME,
"and has no attributes (GetFileAttributesW)");
}
WIN32_FIND_DATAW marker_data{};
SetLastError(0);
const HANDLE markers = first_w(L"GAME:\\.gta6rt-deleted\\data\\*", &marker_data);
check(markers == INVALID_HANDLE_VALUE && GetLastError() == ERROR_INVALID_NAME,
"deletion markers cannot be listed through GAME:\\.gta6rt-deleted");
if (markers != INVALID_HANDLE_VALUE) find_close(markers);
SetLastError(0);
const HANDLE probe = create_w(L"GAME:\\.gta6rt-temp\\probe.txt", GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, 0, nullptr);
check(probe == INVALID_HANDLE_VALUE && !std::filesystem::exists(overlay / ".gta6rt-temp" / "probe.txt") &&
!std::filesystem::exists(own_t_folder(overlay) / "probe.txt"),
"a write through GAME:\\.gta6rt-temp creates nothing in the scratch folder");
if (probe != INVALID_HANDLE_VALUE) ::CloseHandle(probe);
check(mkdir_w(L"GAME:\\.gta6rt-copy\\sub", nullptr) == FALSE && !std::filesystem::exists(overlay / ".gta6rt-copy" / "sub"),
"CreateDirectoryW refuses a reserved folder");
check(delete_w(L"GAME:\\.gta6rt-deleted\\data\\gone.txt") == FALSE &&
std::filesystem::exists(overlay / ".gta6rt-deleted" / "data" / "gone.txt"),
"DeleteFileW cannot remove a deletion marker through GAME:");
HANDLE through = create_w(L"T:\\runtime.tmp", GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
check(through != INVALID_HANDLE_VALUE && read_handle(through) == "runtime", "T:\\ still reaches the scratch folder");
if (through != INVALID_HANDLE_VALUE) ::CloseHandle(through);
through = create_w(L"GAME:\\.gta6rt-other\\ok.txt", GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
check(through != INVALID_HANDLE_VALUE && read_handle(through) == "ok", "an ordinary dotted name under GAME: is not reserved");
if (through != INVALID_HANDLE_VALUE) ::CloseHandle(through);
// A package that itself sits on drive G: is reached by host paths, which must stay host paths.
rt::fs::configure(L"G:\\leonida_missing_package", L"G:\\leonida_missing_overlay");
SetLastError(0);
check(attributes_w(L"G:\\leonida_missing_package\\data.txt") == INVALID_FILE_ATTRIBUTES &&
GetLastError() != ERROR_INVALID_NAME,
"a path under a configured G: game folder is a host path");
// No roots configured: nothing to map G: and T: onto, so they are host paths.
rt::fs::configure({}, {});
SetLastError(0);
check(attributes_w(L"T:\\x") == INVALID_FILE_ATTRIBUTES && GetLastError() != ERROR_INVALID_NAME,
"without configured folders G: and T: stay host drives");
rt::fs::configure(package, overlay);
}
// T: is the title's temporary drive, which on the console no other run shares: each process gets a folder of its
// own below <overlay>\.gta6rt-temp. With one folder for every run, the title's crash dump and crash log, made with
// CREATE_NEW, failed with ERROR_FILE_EXISTS on names an earlier run had left (old upstream issue 7913, measured
// under Wine).
{
const std::filesystem::path t_overlay = root / "t_overlay";
const std::filesystem::path temp = t_overlay / ".gta6rt-temp";
// What earlier runs left: a file in the folder they all shared, and one in an earlier process's own folder.
write_path(temp / "crash.dmp", "earlier run");
write_path(temp / "20200101-000000-000-pid1" / "crash.dmp", "earlier process");
write_path(package / "crash.dmp", "package"); // and a GAME: file of the same name
rt::fs::configure(package, t_overlay);
SetLastError(0);
HANDLE h = create_w(L"T:\\crash.dmp", GENERIC_WRITE, 0, nullptr, CREATE_NEW, 0, nullptr);
const DWORD create_error = h == INVALID_HANDLE_VALUE ? GetLastError() : 0;
check(h != INVALID_HANDLE_VALUE,
"CreateFileW(T:\\crash.dmp, CREATE_NEW) succeeds when earlier runs left that name in .gta6rt-temp");
if (create_error) std::printf(" (failed with Win32 error %lu)\n", static_cast<unsigned long>(create_error));
if (h != INVALID_HANDLE_VALUE) {
DWORD written = 0;
::WriteFile(h, "this process", 12, &written, nullptr);
::CloseHandle(h);
}
const std::filesystem::path mine = own_t_folder(t_overlay);
check(!mine.empty() && mine.parent_path() == temp && read_path(mine / "crash.dmp") == "this process",
"T:\\crash.dmp lands in this process's own folder <overlay>\\.gta6rt-temp\\<start time>-pid<id>");
const std::wstring name = mine.filename().wstring();
const auto digits = [&](size_t from, size_t n) {
if (name.size() < from + n) return false;
for (size_t i = from; i < from + n; ++i)
if (name[i] < L'0' || name[i] > L'9') return false;
return true;
};
check(digits(0, 8) && name.size() > 20 && name[8] == L'-' && digits(9, 6) && name[15] == L'-' &&
digits(16, 3) && name.compare(19, 4, L"-pid") == 0,
"the folder is named YYYYMMDD-HHMMSS-mmm-pid<id>, the process's start time and id");
check(name == rt::fs::scratch_stem(), "with no folder of that name yet, it is the stem itself");
check(read_path(temp / "crash.dmp") == "earlier run" &&
read_path(temp / "20200101-000000-000-pid1" / "crash.dmp") == "earlier process" &&
read_path(package / "crash.dmp") == "package",
"the earlier runs' files and the package file of the same name are left as they were");
// The same folder for every T: call of this process, also after configure() names the overlay again.
rt::fs::configure(package, t_overlay);
h = create_w(L"t:/crash.dmp", GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
check(h != INVALID_HANDLE_VALUE && read_handle(h) == "this process",
"T: keeps naming the same folder for the rest of the process");
if (h != INVALID_HANDLE_VALUE) ::CloseHandle(h);
const std::vector<std::wstring> t_names = list(first_w, next_w, find_close, L"T:\\*");
check(count(t_names, L"crash.dmp") == 1 && !contains(t_names, L"20200101-000000-000-pid1") &&
!contains(t_names, L".gta6rt-temp"),
"a T:\\ listing shows this process's files only");
SetLastError(0);
check(create_w(L"T:\\..\\crash.dmp", GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr) ==
INVALID_HANDLE_VALUE &&
GetLastError() == ERROR_INVALID_NAME,
"T:\\.. cannot climb into the shared .gta6rt-temp folder");
SetLastError(0);
check(rmdir_w(L"T:\\") == FALSE && GetLastError() == ERROR_ACCESS_DENIED && std::filesystem::is_directory(mine),
"this process's T:\\ folder cannot be removed, like GAME:\\");
// A second process on the same overlay gets a folder of its own, and its CREATE_NEW of the same name succeeds.
DWORD child_pid = 0;
const DWORD child_code = run_t_drive_child(package, t_overlay, child_pid);
check(child_code == 0,
"a second process's CreateFileW(T:\\crash.dmp, CREATE_NEW) on the same overlay succeeds");
if (child_code != 0)
std::printf(" (the second process exited with %lu)\n", static_cast<unsigned long>(child_code));
const std::filesystem::path theirs = child_pid ? t_folder(t_overlay, child_pid) : std::filesystem::path();
check(!theirs.empty() && theirs != mine && theirs.parent_path() == temp &&
read_path(theirs / "crash.dmp") == "child process",
"the second process wrote into a folder of its own");
check(read_path(mine / "crash.dmp") == "this process", "and this process's T: file is unchanged");
// A reused process id in the same millisecond gives the same stem as a folder an earlier run kept. The folder
// is reserved with a create-only call, so that one (and the next name, taken too) is never used again.
const std::filesystem::path stale_overlay = root / "t_stale_overlay";
const std::filesystem::path stale = stale_overlay / ".gta6rt-temp" / rt::fs::scratch_stem();
const std::filesystem::path stale_2 = stale_overlay / ".gta6rt-temp" / (rt::fs::scratch_stem() + L"-2");
write_path(stale / "crash.dmp", "same stem, earlier run");
std::filesystem::create_directories(stale_2);
rt::fs::configure(package, stale_overlay);
h = create_w(L"T:\\crash.dmp", GENERIC_WRITE, 0, nullptr, CREATE_NEW, 0, nullptr);
check(h != INVALID_HANDLE_VALUE,
"CreateFileW(T:\\crash.dmp, CREATE_NEW) succeeds when a kept folder has this process's own stem");
if (h != INVALID_HANDLE_VALUE) ::CloseHandle(h);
check(rt::fs::scratch_dir() == stale_overlay / ".gta6rt-temp" / (rt::fs::scratch_stem() + L"-3") &&
std::filesystem::exists(rt::fs::scratch_dir() / "crash.dmp"),
"T: is <stem>-3 when <stem> and <stem>-2 already existed");
check(read_path(stale / "crash.dmp") == "same stem, earlier run" && std::filesystem::is_empty(stale_2),
"and the folders that already existed are left as they were");
// Concurrent first T: calls on a new overlay reserve one folder between them.
const std::filesystem::path race_overlay = root / "t_race_overlay";
rt::fs::configure(package, race_overlay);
{
std::vector<std::thread> threads;
for (int i = 0; i < 8; ++i) threads.emplace_back([&] { attributes_w(L"T:\\"); });
for (auto& thread : threads) thread.join();
}
size_t reserved = 0;
for (const auto& entry : std::filesystem::directory_iterator(race_overlay / ".gta6rt-temp")) {
(void)entry;
++reserved;
}
check(reserved == 1 && std::filesystem::is_directory(rt::fs::scratch_dir()),
"eight threads making their first T: call at once reserve one folder");
rt::fs::configure(package, t_overlay);
// GAME: is not affected: reads come from the package, writes land in the overlay's mirror of the package.
h = create_w(L"GAME:\\crash.dmp", GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
check(h != INVALID_HANDLE_VALUE && read_handle(h) == "package",
"GAME:\\crash.dmp still reads the package file");
if (h != INVALID_HANDLE_VALUE) ::CloseHandle(h);
h = create_w(L"GAME:\\data\\t_written.txt", GENERIC_WRITE, 0, nullptr, CREATE_NEW, 0, nullptr);
check(h != INVALID_HANDLE_VALUE, "a GAME: write-open still succeeds");
if (h != INVALID_HANDLE_VALUE) ::CloseHandle(h);
check(std::filesystem::exists(t_overlay / "data" / "t_written.txt") &&
!std::filesystem::exists(mine / "data") &&
!std::filesystem::exists(package / "data" / "t_written.txt"),
"and lands at <overlay>\\data, not in T:'s folder or the package");
rt::fs::configure(package, overlay);
}
// reserve_unique_dir: a create-only CreateDirectoryW per name, so the folder it returns is new; a name that exists
// (a folder or a file) is skipped, and after max_tries names the call fails instead of reusing one.
{
const std::filesystem::path base = root / "reserve";
std::filesystem::create_directories(base / "stem");
write_path(base / "stem-2" / "old.txt", "old");
DWORD error = 1;
const std::filesystem::path third = rt::fs::reserve_unique_dir(base, L"stem", error);
check(third == base / "stem-3" && error == 0 && std::filesystem::is_directory(third) &&
std::filesystem::is_empty(third),
"reserve_unique_dir(base, stem) with stem and stem-2 taken makes stem-3, new and empty");
check(read_path(base / "stem-2" / "old.txt") == "old", "and leaves the taken folders alone");
check(rt::fs::reserve_unique_dir(base, L"stem", error) == base / "stem-4",
"a second call never hands out stem-3 again: it makes stem-4");
write_path(base / "stem-5", "a file");
check(rt::fs::reserve_unique_dir(base, L"stem", error) == base / "stem-6",
"a file with the next name counts as taken too");
error = 0;
check(rt::fs::reserve_unique_dir(base, L"stem", error, 6).empty() && error == ERROR_ALREADY_EXISTS,
"with every name up to max_tries taken it fails with ERROR_ALREADY_EXISTS");
const std::filesystem::path fresh_base = root / "reserve_new" / "deep";
check(rt::fs::reserve_unique_dir(fresh_base, L"stem", error) == fresh_base / "stem" &&
std::filesystem::is_directory(fresh_base / "stem"),
"a missing base folder is made first, and the stem itself is the first name");
}
// An open that writes needs its folder to exist in the title's view, and a failed open changes nothing.
{
const auto overlay_has = [&](const wchar_t* name) { return std::filesystem::exists(overlay / name); };
write_path(package / "rules" / "file.txt", "package");
write_path(package / "rules" / "gone" / "only.txt", "package");
SetLastError(0);
HANDLE h = create_w(L"GAME:\\nodir\\sub\\x.dat", GENERIC_READ | GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, 0,
nullptr);
check(h == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
"a write-open of an existing file in a missing folder fails with ERROR_PATH_NOT_FOUND");
if (h != INVALID_HANDLE_VALUE) ::CloseHandle(h);
check(attributes_w(L"GAME:\\nodir") == INVALID_FILE_ATTRIBUTES && !overlay_has(L"nodir"),
"the failed open did not create the missing folders");
SetLastError(0);
h = create_w(L"GAME:\\nodir2\\x.dat", GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, 0, nullptr);
check(h == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
"CREATE_ALWAYS in a missing folder fails with ERROR_PATH_NOT_FOUND, as on the host");
if (h != INVALID_HANDLE_VALUE) ::CloseHandle(h);
check(attributes_w(L"GAME:\\nodir2") == INVALID_FILE_ATTRIBUTES && !overlay_has(L"nodir2"),
"and creates neither the folder nor the file");
SetLastError(0);
h = create_w(L"GAME:\\rules\\file.txt\\x.dat", GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, 0, nullptr);
check(h == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
"CREATE_ALWAYS below a package file fails with ERROR_PATH_NOT_FOUND");
if (h != INVALID_HANDLE_VALUE) ::CloseHandle(h);
const DWORD file_attributes = attributes_w(L"GAME:\\rules\\file.txt");
check(file_attributes != INVALID_FILE_ATTRIBUTES && (file_attributes & FILE_ATTRIBUTE_DIRECTORY) == 0,
"and the package file is still a file in the title's view");
check(delete_w(L"GAME:\\rules\\gone\\only.txt") != FALSE && rmdir_w(L"GAME:\\rules\\gone") != FALSE,
"a package folder is emptied and removed");
SetLastError(0);
h = create_w(L"GAME:\\rules\\gone\\new.dat", GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, 0, nullptr);
check(h == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
"CREATE_ALWAYS inside a removed folder fails with ERROR_PATH_NOT_FOUND");
if (h != INVALID_HANDLE_VALUE) ::CloseHandle(h);
check(attributes_w(L"GAME:\\rules\\gone") == INVALID_FILE_ATTRIBUTES,
"and does not bring the removed folder back");
// The folders that do exist still work: one only the package has, and one the title made.
h = create_w(L"GAME:\\rules\\new.dat", GENERIC_WRITE, 0, nullptr, CREATE_NEW, 0, nullptr);
check(h != INVALID_HANDLE_VALUE, "CREATE_NEW in a folder only the package has still succeeds");
if (h != INVALID_HANDLE_VALUE) ::CloseHandle(h);
check(overlay_has(L"rules\\new.dat") && !std::filesystem::exists(package / "rules" / "new.dat"),
"and the file is in the overlay");
check(mkdir_w(L"GAME:\\rules\\made", nullptr) != FALSE, "a folder the title makes");
h = create_w(L"GAME:\\rules\\made\\x.dat", GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, 0, nullptr);
check(h != INVALID_HANDLE_VALUE, "takes a new file");
if (h != INVALID_HANDLE_VALUE) ::CloseHandle(h);
}
// A listing needs its folder to exist in the title's view: a removed or missing folder is a missing path, not an
// empty folder and not a missing file.
{
write_path(package / "listing" / "old" / "a.txt", "package");
WIN32_FIND_DATAW found{};
check(delete_w(L"GAME:\\listing\\old\\a.txt") != FALSE && rmdir_w(L"GAME:\\listing\\old") != FALSE,
"a package folder is emptied and removed (listing)");
SetLastError(0);
HANDLE h = first_w(L"GAME:\\listing\\old\\*", &found);
check(h == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
"FindFirstFileW inside a removed package folder fails with ERROR_PATH_NOT_FOUND");
if (h != INVALID_HANDLE_VALUE) find_close(h);
SetLastError(0);
h = first_w(L"GAME:\\listing\\never\\*", &found);
check(h == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
"FindFirstFileW inside a folder that never existed fails with ERROR_PATH_NOT_FOUND");
if (h != INVALID_HANDLE_VALUE) find_close(h);
SetLastError(0);
h = first_w(L"GAME:\\listing\\old", &found);
check(h == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_NOT_FOUND,
"FindFirstFileW of the removed folder's own name is ERROR_FILE_NOT_FOUND: its parent exists");
if (h != INVALID_HANDLE_VALUE) find_close(h);
SetLastError(0);
h = first_w(L"GAME:\\listing\\nothing*.bin", &found);
check(h == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_NOT_FOUND,
"a pattern that matches nothing in an existing folder is still ERROR_FILE_NOT_FOUND");
if (h != INVALID_HANDLE_VALUE) find_close(h);
check(list(first_w, next_w, find_close, L"GAME:\\listing\\*").size() == 2,
"and the emptied parent lists only . and ..");
// A listing whose folder is a file is neither: the host answers ERROR_DIRECTORY. One level further down the
// path is simply missing again. (Case measured on the host and reported in review by shdann.)
write_path(package / "listing" / "file.txt", "package");
for (const wchar_t* pattern : {L"GAME:\\listing\\file.txt\\*", L"GAME:\\listing\\file.txt\\x"}) {
SetLastError(0);
h = first_w(pattern, &found);
check(h == INVALID_HANDLE_VALUE && GetLastError() == ERROR_DIRECTORY,
"FindFirstFileW below a file fails with ERROR_DIRECTORY, as the host does");
if (h != INVALID_HANDLE_VALUE) find_close(h);
}
SetLastError(0);
h = first_w(L"GAME:\\listing\\file.txt\\sub\\*", &found);
check(h == INVALID_HANDLE_VALUE && GetLastError() == ERROR_PATH_NOT_FOUND,
"and two levels below a file with ERROR_PATH_NOT_FOUND");
if (h != INVALID_HANDLE_VALUE) find_close(h);
}
// A move onto the same name succeeds and changes nothing; a rename that changes only the case renames.
{
const auto names_in = [&](const wchar_t* pattern) { return list(first_w, next_w, find_close, pattern); };
const auto has_exact = [](const std::vector<std::wstring>& names, const wchar_t* name) {
for (const std::wstring& n : names)
if (n == name) return true;
return false;
};
write_path(package / "moves" / "pkg.dat", "package");
write_path(package / "moves" / "same.dat", "package");
check(mkdir_w(L"GAME:\\moves\\dir", nullptr) != FALSE, "an overlay folder for the case rename");
HANDLE h = create_w(L"GAME:\\moves\\save.dat", GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, 0, nullptr);
DWORD written = 0;
if (h != INVALID_HANDLE_VALUE) {
::WriteFile(h, "save", 4, &written, nullptr);
::CloseHandle(h);
}
check(move_w(L"GAME:\\moves\\save.dat", L"GAME:\\moves\\save.dat") != FALSE,
"MoveFileW of an overlay file onto itself succeeds");
check(move_w(L"GAME:\\moves\\same.dat", L"GAME:\\moves\\same.dat") != FALSE,
"MoveFileW of a package file onto itself succeeds");
check(read_path(package / "moves" / "same.dat") == "package" && !std::filesystem::exists(overlay / "moves" / "same.dat"),
"and a package file moved onto itself is neither copied nor hidden");
HANDLE r = create_w(L"GAME:\\moves\\same.dat", GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
check(r != INVALID_HANDLE_VALUE && read_handle(r) == "package", "it still reads");
if (r != INVALID_HANDLE_VALUE) ::CloseHandle(r);
// An overlay entry's case rename is the host's own rename, so the new spelling is only checked where the
// host's MoveFileW changes it (not under Wine); the entry is still listed exactly once everywhere.
const bool host_case = host_renames_case(root / "case_probe");
if (!host_case)
std::printf("SKIP the host's MoveFileW keeps the old case on a case-only rename (Wine): the listing's "
"spelling of a renamed overlay entry is not checked\n");
check(move_w(L"GAME:\\moves\\save.dat", L"GAME:\\moves\\SAVE.DAT") != FALSE,
"MoveFileW renames an overlay file to the same name in another case");
std::vector<std::wstring> names = names_in(L"GAME:\\moves\\*");
if (host_case)
check(has_exact(names, L"SAVE.DAT") && !has_exact(names, L"save.dat") && count(names, L"save.dat") == 1,
"the listing shows the new case, once");
else
check(count(names, L"save.dat") == 1, "the listing shows the renamed file once");
check(read_path(overlay / "moves" / "SAVE.DAT") == "save", "and the bytes are the same");
check(move_w(L"GAME:\\moves\\pkg.dat", L"GAME:\\moves\\PKG.DAT") != FALSE,
"MoveFileW renames a package file to the same name in another case");
names = names_in(L"GAME:\\moves\\*");
check(has_exact(names, L"PKG.DAT") && !has_exact(names, L"pkg.dat") && count(names, L"pkg.dat") == 1,
"the listing shows the package file under its new case, once");
r = create_w(L"GAME:\\moves\\pkg.dat", GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
check(r != INVALID_HANDLE_VALUE && read_handle(r) == "package", "and it opens under either case");
if (r != INVALID_HANDLE_VALUE) ::CloseHandle(r);
check(read_path(package / "moves" / "pkg.dat") == "package", "the package file is untouched");
check(move_w(L"GAME:\\moves\\dir", L"GAME:\\moves\\DIR") != FALSE,
"MoveFileW renames an overlay folder to the same name in another case");
names = names_in(L"GAME:\\moves\\*");
if (host_case)
check(has_exact(names, L"DIR"), "the listing shows the folder's new case");
else
check(count(names, L"dir") == 1, "the listing shows the renamed folder once");
check(move_w(L"GAME:\\moves", L"GAME:\\moves") != FALSE, "MoveFileW of a package folder onto itself succeeds");
SetLastError(0);
check(move_w(L"GAME:\\moves", L"GAME:\\MOVES") == FALSE && GetLastError() == ERROR_ACCESS_DENIED,
"a case rename of a package folder is refused like any other move of one");
}
// The guest can reach a deeply nested file even when its executable and the configured roots are short.
// Use the explicit Win32 namespace to set up the fixtures, independently of the shim under test.
{
const auto extended = [](const std::filesystem::path& path) {
const auto& native = path.native();
if (native.starts_with(L"\\\\?\\")) return path;
if (native.starts_with(L"\\\\")) return std::filesystem::path(L"\\\\?\\UNC\\" + native.substr(2));
return std::filesystem::path(L"\\\\?\\" + native);
};
const auto fixture = root / "long_paths";
const auto long_package = fixture / "package";
const auto long_overlay = fixture / "overlay";
const std::wstring first(105, L'a'), second(105, L'b');
const auto relative = std::filesystem::path(first) / second;
const auto package_dir = extended(long_package / relative);
const auto overlay_dir = extended(long_overlay / relative);
const auto leaf = L"pr\u00f8be.bin";
const std::wstring guest_dir = L"GAME:\\" + relative.native() + L"\\";
const std::wstring guest_file = guest_dir + leaf;
check((long_package / relative / leaf).native().size() > MAX_PATH,
"the fixture resolves to a host path beyond MAX_PATH");
write_path(package_dir / leaf, "package");
write_path(package_dir / "legacy.bin", "legacy");
std::filesystem::create_directories(package_dir / "empty");
check(read_path(package_dir / leaf) == "package", "native extended paths can provision the deep fixture");
rt::fs::configure(long_package, long_overlay);
const auto read_guest = [&](const std::wstring& name) {
const HANDLE h = create_w(name.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
if (h == INVALID_HANDLE_VALUE) return std::string{};
auto bytes = read_handle(h);
::CloseHandle(h);
return bytes;
};
check(read_guest(guest_file) == "package", "GAME: reads a deeply nested Unicode path from the package");
check(read_guest(guest_file + L". ") == "package",
"a long resolved name retains Win32 trailing-dot and trailing-space normalization");
check(attributes_w(guest_file.c_str()) != INVALID_FILE_ATTRIBUTES, "attributes resolve the same long path");
const std::string guest_ansi = "G:\\" + std::string(first.size(), 'a') + "\\" + std::string(second.size(), 'b') +
"\\legacy.bin";
HANDLE h = create_a(guest_ansi.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
check(h != INVALID_HANDLE_VALUE && read_handle(h) == "legacy", "the G: alias opens a long path through CreateFileA");
if (h != INVALID_HANDLE_VALUE) ::CloseHandle(h);
h = create_w(guest_file.c_str(), GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, 0, nullptr);
DWORD written = 0;
check(h != INVALID_HANDLE_VALUE && ::WriteFile(h, "overlay", 7, &written, nullptr) && written == 7,
"the write-open copies up and writes a long path");
if (h != INVALID_HANDLE_VALUE) ::CloseHandle(h);
check(read_guest(guest_file) == "overlay" && read_path(overlay_dir / leaf) == "overlay" &&
read_path(package_dir / leaf) == "package",
"long paths prefer the overlay copy and keep the original package immutable");
const std::wstring copy = guest_dir + L"copy.bin", moved = guest_dir + L"moved.bin";
check(copy_w(guest_file.c_str(), copy.c_str(), TRUE) && move_w(copy.c_str(), moved.c_str()) &&
read_guest(moved) == "overlay", "copy and move use the same long resolved overlay paths");
check(delete_w((guest_dir + L"legacy.bin").c_str()) && rmdir_w((guest_dir + L"empty").c_str()) &&
read_path(package_dir / "legacy.bin") == "legacy",
"long file and directory deletions create markers and preserve the package");
const auto names = list(first_w, next_w, find_close, (guest_dir + L"*").c_str());
check(count(names, leaf) == 1 && contains(names, L"moved.bin") &&
!contains(names, L"legacy.bin") && !contains(names, L"empty"),
"listing long paths merges both roots and honors file and directory markers");
check(attributes_w((guest_dir + L"legacy.bin").c_str()) == INVALID_FILE_ATTRIBUTES,
"a read-only query respects the long file's delete marker");
// Recreating the removed folder takes its marker away through an extended-length path: the marker lies
// beyond MAX_PATH (kevincommits, #1608).
const DWORD recreated = mkdir_w((guest_dir + L"empty").c_str(), nullptr) != FALSE
? attributes_w((guest_dir + L"empty").c_str()) : INVALID_FILE_ATTRIBUTES;
check(recreated != INVALID_FILE_ATTRIBUTES && (recreated & FILE_ATTRIBUTE_DIRECTORY) != 0,
"CreateDirectoryW recreates a removed folder whose marker is a path beyond MAX_PATH");
check(contains(list(first_w, next_w, find_close, (guest_dir + L"*").c_str()), L"empty"),
"the recreated long folder is listed again");
// Compare the same merged listing against the short-path Win32 search, including the names
// hidden by package deletion markers.
const wchar_t* const masks[] = {L"*.", L"?.", L"mesh*.", L"mesh*..", L"mesh?.", L"mesh.*.",
L"*.?.", L"*. ", L"*.*", L"mesh?.txt"};
const auto list_masks = [&](const std::filesystem::path& pack, const std::filesystem::path& layer,
const std::filesystem::path& subdir) {
for (const wchar_t* name : {L"a", L"a.txt", L"mesh", L"mesh1", L"mesh.txt", L"mesh-old", L".dot"})
write_path(extended(pack / subdir / name), "package");
for (const wchar_t* name : {L"mesh", L"mesh-cache", L"mesh-cache.tmp"})
write_path(extended(layer / subdir / name), "overlay");
rt::fs::configure(pack, layer);
const auto guest = L"GAME:\\" + subdir.native() + L"\\";
check(delete_w((guest + L"mesh-old").c_str()), "the wildcard fixture hides an extensionless package file");
std::vector<std::vector<std::wstring>> results;
for (const wchar_t* mask : masks) {
auto entries = list(first_w, next_w, find_close, (guest + mask).c_str());
std::sort(entries.begin(), entries.end());
results.push_back(std::move(entries));
}
return results;
};
const auto short_matches = list_masks(fixture / "short_package", fixture / "short_overlay", L"masks");
const auto long_matches = list_masks(long_package, long_overlay, relative / L"masks");
check(count(short_matches[0], L"mesh") == 1 && contains(short_matches[0], L"mesh-cache") &&
!contains(short_matches[0], L"mesh.txt") && !contains(short_matches[0], L"mesh-old"),
"the short *. control merges extensionless entries and hides the deleted name");
for (size_t i = 0; i < short_matches.size(); ++i) {
char detail[96];
std::snprintf(detail, sizeof detail, "the long listing matches short Win32 for %ls", masks[i]);
check(long_matches[i] == short_matches[i], detail);
}
// The scratch drive is usable with a short guest name and a long configured overlay root.
const auto scratch_overlay = fixture / relative / "scratch_overlay";
rt::fs::configure(long_package, scratch_overlay);
h = create_a("T:\\scratch.bin", GENERIC_WRITE, 0, nullptr, CREATE_NEW, 0, nullptr);
written = 0;
check(h != INVALID_HANDLE_VALUE && ::WriteFile(h, "test", 4, &written, nullptr) && written == 4,
"T: creates and writes its scratch directory under a long overlay root");
if (h != INVALID_HANDLE_VALUE) ::CloseHandle(h);
const std::filesystem::path scratch_folder = rt::fs::scratch_dir(); // configured on scratch_overlay above
check(read_guest(L"T:\\scratch.bin") == "test" && !scratch_folder.empty() &&
scratch_folder.parent_path() == scratch_overlay / ".gta6rt-temp" &&
read_path(extended(scratch_folder / "scratch.bin")) == "test",
"T: reads from the same long backing directory, this process's own folder in it");
rt::fs::configure(package, overlay);
std::error_code cleanup_error;
std::filesystem::remove_all(extended(fixture), cleanup_error);
check(!cleanup_error, "the synthetic long paths are removed after the test");
}
}
} // namespace
|