diff options
| author | jaybeebabageyus <jaybeebabageyus@users.noreply.github.com> | 2026-11-22 17:01:52 +0100 |
|---|---|---|
| committer | jaybeebabageyus <jaybeebabageyus@users.noreply.github.com> | 2026-11-22 17:01:52 +0100 |
| commit | 403d59060e89d706429ec9eca23b6ce7b7ef03c4 (patch) | |
| tree | 58e563b0093062d6a2aa309e09ee1ef875f28bc8 /runtime/test/file_system_test.cpp | |
| parent | 6a6704df1592e1dc4a3215ddf1423a78adb52045 (diff) | |
runtime: shim CreateFile2 and FindFirstFileExA/W for GAME: paths and overlay
Diffstat (limited to 'runtime/test/file_system_test.cpp')
| -rw-r--r-- | runtime/test/file_system_test.cpp | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/runtime/test/file_system_test.cpp b/runtime/test/file_system_test.cpp index 1fcaf6710..0ba915cf4 100644 --- a/runtime/test/file_system_test.cpp +++ b/runtime/test/file_system_test.cpp | |||
| @@ -17,8 +17,13 @@ namespace { | |||
| 17 | 17 | ||
| 18 | using CreateFileWFn = HANDLE(WINAPI*)(LPCWSTR, DWORD, DWORD, LPSECURITY_ATTRIBUTES, DWORD, DWORD, HANDLE); | 18 | using CreateFileWFn = HANDLE(WINAPI*)(LPCWSTR, DWORD, DWORD, LPSECURITY_ATTRIBUTES, DWORD, DWORD, HANDLE); |
| 19 | using CreateFileAFn = HANDLE(WINAPI*)(LPCSTR, DWORD, DWORD, LPSECURITY_ATTRIBUTES, DWORD, DWORD, HANDLE); | 19 | using CreateFileAFn = HANDLE(WINAPI*)(LPCSTR, DWORD, DWORD, LPSECURITY_ATTRIBUTES, DWORD, DWORD, HANDLE); |
| 20 | using CreateFile2Fn = HANDLE(WINAPI*)(LPCWSTR, DWORD, DWORD, DWORD, LPCREATEFILE2_EXTENDED_PARAMETERS); | ||
| 20 | using FindFirstFileWFn = HANDLE(WINAPI*)(LPCWSTR, LPWIN32_FIND_DATAW); | 21 | using FindFirstFileWFn = HANDLE(WINAPI*)(LPCWSTR, LPWIN32_FIND_DATAW); |
| 21 | using FindFirstFileAFn = HANDLE(WINAPI*)(LPCSTR, LPWIN32_FIND_DATAA); | 22 | using FindFirstFileAFn = HANDLE(WINAPI*)(LPCSTR, LPWIN32_FIND_DATAA); |
| 23 | using FindFirstFileExWFn = | ||
| 24 | HANDLE(WINAPI*)(LPCWSTR, FINDEX_INFO_LEVELS, LPVOID, FINDEX_SEARCH_OPS, LPVOID, DWORD); | ||
| 25 | using FindFirstFileExAFn = | ||
| 26 | HANDLE(WINAPI*)(LPCSTR, FINDEX_INFO_LEVELS, LPVOID, FINDEX_SEARCH_OPS, LPVOID, DWORD); | ||
| 22 | using FindNextFileWFn = BOOL(WINAPI*)(HANDLE, LPWIN32_FIND_DATAW); | 27 | using FindNextFileWFn = BOOL(WINAPI*)(HANDLE, LPWIN32_FIND_DATAW); |
| 23 | using FindCloseFn = BOOL(WINAPI*)(HANDLE); | 28 | using FindCloseFn = BOOL(WINAPI*)(HANDLE); |
| 24 | using GetFileAttributesWFn = DWORD(WINAPI*)(LPCWSTR); | 29 | using GetFileAttributesWFn = DWORD(WINAPI*)(LPCWSTR); |
| @@ -284,6 +289,78 @@ int main() { | |||
| 284 | "a null listing buffer is refused"); | 289 | "a null listing buffer is refused"); |
| 285 | } | 290 | } |
| 286 | 291 | ||
| 292 | // CreateFile2 and FindFirstFileExA/W (used by GDK/OneCore titles and std::filesystem). | ||
| 293 | { | ||
| 294 | const auto create2 = resolve<CreateFile2Fn>("api-ms-win-core-file-l1-2-0.dll", "CreateFile2"); | ||
| 295 | const auto first_ex_w = resolve<FindFirstFileExWFn>("api-ms-win-core-file-l1-1-0.dll", "FindFirstFileExW"); | ||
| 296 | const auto first_ex_a = resolve<FindFirstFileExAFn>("kernelbase.dll", "FindFirstFileExA"); | ||
| 297 | check(create2 && first_ex_w && first_ex_a, "CreateFile2 and FindFirstFileExA/W are registered"); | ||
| 298 | |||
| 299 | // CreateFile2 reads an untouched package file without modifying it. | ||
| 300 | write_path(package / "data" / "cf2_pkg.txt", "pkg-cf2"); | ||
| 301 | CREATEFILE2_EXTENDED_PARAMETERS ext{}; | ||
| 302 | ext.dwSize = sizeof(ext); | ||
| 303 | ext.dwFileAttributes = FILE_ATTRIBUTE_NORMAL; | ||
| 304 | const HANDLE h_read = create2(L"GAME:\\data\\cf2_pkg.txt", GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING, &ext); | ||
| 305 | check(h_read != INVALID_HANDLE_VALUE && read_handle(h_read) == "pkg-cf2", | ||
| 306 | "CreateFile2 reads a GAME: file from the package"); | ||
| 307 | if (h_read != INVALID_HANDLE_VALUE) ::CloseHandle(h_read); | ||
| 308 | |||
| 309 | // CreateFile2 write goes to the overlay, leaving the package untouched. | ||
| 310 | const HANDLE h_write = create2(L"GAME:\\data\\cf2.txt", GENERIC_WRITE, 0, CREATE_ALWAYS, &ext); | ||
| 311 | check(h_write != INVALID_HANDLE_VALUE, "CreateFile2 creates a new GAME: file in the overlay"); | ||
| 312 | if (h_write != INVALID_HANDLE_VALUE) { | ||
| 313 | DWORD written = 0; | ||
| 314 | ::WriteFile(h_write, "cf2", 3, &written, nullptr); | ||
| 315 | ::CloseHandle(h_write); | ||
| 316 | } | ||
| 317 | check(read_path(overlay / "data" / "cf2.txt") == "cf2" && !std::filesystem::exists(package / "data" / "cf2.txt"), | ||
| 318 | "CreateFile2 writes land in the overlay and never in the package"); | ||
| 319 | |||
| 320 | // CreateFile2 refuses path traversal. | ||
| 321 | SetLastError(0); | ||
| 322 | const HANDLE h_esc = create2(L"GAME:\\..\\cf2_escape.txt", GENERIC_WRITE, 0, CREATE_ALWAYS, &ext); | ||
| 323 | check(h_esc == INVALID_HANDLE_VALUE && GetLastError() == ERROR_INVALID_NAME, | ||
| 324 | "CreateFile2 refuses a path above GAME:"); | ||
| 325 | if (h_esc != INVALID_HANDLE_VALUE) ::CloseHandle(h_esc); | ||
| 326 | |||
| 327 | // FindFirstFileExW merges package + overlay and honours FindExInfoBasic & FindExSearchLimitToDirectories. | ||
| 328 | WIN32_FIND_DATAW fd{}; | ||
| 329 | std::vector<std::wstring> basic_names; | ||
| 330 | bool alt_empty = true; | ||
| 331 | HANDLE hf = first_ex_w(L"GAME:\\data\\*", FindExInfoBasic, &fd, FindExSearchNameMatch, nullptr, 0); | ||
| 332 | check(hf != INVALID_HANDLE_VALUE, "FindFirstFileExW opens a GAME: directory listing"); | ||
| 333 | if (hf != INVALID_HANDLE_VALUE) { | ||
| 334 | do { | ||
| 335 | basic_names.emplace_back(fd.cFileName); | ||
| 336 | if (fd.cAlternateFileName[0] != L'\0') alt_empty = false; | ||
| 337 | } while (next_w(hf, &fd)); | ||
| 338 | find_close(hf); | ||
| 339 | } | ||
| 340 | check(contains(basic_names, L"hello.txt") && contains(basic_names, L"cf2.txt") && !contains(basic_names, L"bye.txt"), | ||
| 341 | "FindFirstFileExW merges overlay + package and hides deleted files"); | ||
| 342 | check(alt_empty, "FindFirstFileExW(FindExInfoBasic) leaves cAlternateFileName empty"); | ||
| 343 | |||
| 344 | std::vector<std::wstring> dir_names; | ||
| 345 | HANDLE hdir = first_ex_w(L"GAME:\\data\\*", FindExInfoStandard, &fd, FindExSearchLimitToDirectories, nullptr, 0); | ||
| 346 | check(hdir != INVALID_HANDLE_VALUE, "FindFirstFileExW(FindExSearchLimitToDirectories) succeeds"); | ||
| 347 | if (hdir != INVALID_HANDLE_VALUE) { | ||
| 348 | do { | ||
| 349 | dir_names.emplace_back(fd.cFileName); | ||
| 350 | } while (next_w(hdir, &fd)); | ||
| 351 | find_close(hdir); | ||
| 352 | } | ||
| 353 | check(contains(dir_names, L"sub") && !contains(dir_names, L"hello.txt"), | ||
| 354 | "FindFirstFileExW(FindExSearchLimitToDirectories) returns only directories"); | ||
| 355 | |||
| 356 | // FindFirstFileExA works on GAME: paths too. | ||
| 357 | WIN32_FIND_DATAA fda{}; | ||
| 358 | HANDLE hfa = first_ex_a("GAME:\\data\\cf2.txt", FindExInfoBasic, &fda, FindExSearchNameMatch, nullptr, 0); | ||
| 359 | check(hfa != INVALID_HANDLE_VALUE && std::string(fda.cFileName) == "cf2.txt", | ||
| 360 | "FindFirstFileExA finds an overlay file under GAME:"); | ||
| 361 | if (hfa != INVALID_HANDLE_VALUE) find_close(hfa); | ||
| 362 | } | ||
| 363 | |||
| 287 | std::filesystem::remove_all(root); | 364 | std::filesystem::remove_all(root); |
| 288 | std::printf("%d check(s), %d failure(s)\n", g_checks, g_failures); | 365 | std::printf("%d check(s), %d failure(s)\n", g_checks, g_failures); |
| 289 | return g_failures == 0 ? 0 : 1; | 366 | return g_failures == 0 ? 0 : 1; |