Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/runtime_imports.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ and the `api-ms-win-core-file-*` sets a GDK title links against.
name, and hides deleted names, the marker folder and the copy folder.
- A path that climbs above `GAME:` with `..`, or names another drive, fails with `ERROR_INVALID_NAME`, so a title
cannot reach outside the package and the overlay.
- `GetFullPathNameA/W` answers a `GAME:` path as itself (one separator style, no `.` or `..`), because the host would
turn `GAME:\x` into `<current folder>\GAME:\x`, which no longer names the game folder. Callers that make a path absolute
before opening it (Rust's `std::fs`, C++ `std::filesystem::absolute`) otherwise failed every `GAME:` open with
`ERROR_PATH_NOT_FOUND`. `G:` and `T:` already survive the host's answer. The C runtime's `_fullpath` calls the host
function inside the UCRT and is not covered.

- `CreateDirectory*` makes the folder in the overlay. A name the title can already see, including a folder only the
package has, fails with `ERROR_ALREADY_EXISTS`; a missing parent with `ERROR_PATH_NOT_FOUND`.
Expand Down
59 changes: 59 additions & 0 deletions runtime/shims/file_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,63 @@ DWORD WINAPI get_file_attributes_a(LPCSTR name) {
return get_file_attributes_w(wide.c_str());
}

// GetFullPathName on a "GAME:" path. The host function reads "GAME:\x" as a relative name and answers
// "<current folder>\GAME:\x", which no longer names the console drive, so a caller that makes its path absolute
// before opening it (Rust's std::fs, std::filesystem::absolute) lost the game folder: every "GAME:" open failed
// with ERROR_PATH_NOT_FOUND while the same name through CreateFileW worked. A "GAME:" path is already absolute: it
// comes back with one separator style and no "." or ".." parts, and a trailing separator stays, as Windows keeps it
// for "C:\foo\" (then there is no file part). A path that climbs above GAME:\ is refused with ERROR_INVALID_NAME,
// as CreateFileW refuses the same name, and never reaches the host. Every other path goes to the host.
enum class FullPath { NotGame, Refused, Answered };

FullPath full_game_path(const wchar_t* name, std::wstring& full, bool& has_file_part) {
const std::wstring path = name;
size_t prefix_length = 0;
if (console_drive(path, prefix_length) != Drive::Game || prefix_length != kGamePrefixLength) return FullPath::NotGame;
std::vector<std::wstring> parts;
if (parse_console(path, parts) != Parsed::Console) return FullPath::Refused;
full = L"GAME:";
for (const auto& part : parts) full += L'\\' + part;
const bool trailing_separator = !path.empty() && is_separator(path.back());
if (parts.empty() || trailing_separator) full += L'\\';
has_file_part = !parts.empty() && !trailing_separator;
return FullPath::Answered;
}

DWORD WINAPI get_full_path_name_w(LPCWSTR name, DWORD length, LPWSTR buffer, LPWSTR* file_part) {
std::wstring full;
bool has_file_part = false;
const FullPath kind = name ? full_game_path(name, full, has_file_part) : FullPath::NotGame;
if (kind == FullPath::NotGame) return ::GetFullPathNameW(name, length, buffer, file_part);
if (file_part) *file_part = nullptr; // as when the host fails or the buffer is too small: no file part
if (kind == FullPath::Refused) {
SetLastError(ERROR_INVALID_NAME);
return 0;
}
if (full.size() + 1 > length) return static_cast<DWORD>(full.size() + 1); // too small: the size with the NUL
std::wmemcpy(buffer, full.c_str(), full.size() + 1);
if (file_part) *file_part = has_file_part ? buffer + full.rfind(L'\\') + 1 : nullptr;
return static_cast<DWORD>(full.size());
}

DWORD WINAPI get_full_path_name_a(LPCSTR name, DWORD length, LPSTR buffer, LPSTR* file_part) {
std::wstring full;
bool has_file_part = false;
const FullPath kind = name ? full_game_path(widen_ansi(name).c_str(), full, has_file_part) : FullPath::NotGame;
if (kind == FullPath::NotGame) return ::GetFullPathNameA(name, length, buffer, file_part);
if (file_part) *file_part = nullptr;
if (kind == FullPath::Refused) {
SetLastError(ERROR_INVALID_NAME);
return 0;
}
const int needed = WideCharToMultiByte(CP_ACP, 0, full.c_str(), -1, nullptr, 0, nullptr, nullptr); // with the NUL
if (needed <= 0) return 0;
if (static_cast<DWORD>(needed) > length) return static_cast<DWORD>(needed);
WideCharToMultiByte(CP_ACP, 0, full.c_str(), -1, buffer, needed, nullptr, nullptr);
if (file_part) *file_part = has_file_part ? std::strrchr(buffer, '\\') + 1 : nullptr;
return static_cast<DWORD>(needed - 1);
}

BOOL WINAPI get_file_attributes_ex_w(LPCWSTR name, GET_FILEEX_INFO_LEVELS level, LPVOID info) {
if (!name || !info) {
SetLastError(ERROR_INVALID_PARAMETER);
Expand Down Expand Up @@ -1106,6 +1163,8 @@ using namespace rt::fs;
{dll, "FindNextFileW", reinterpret_cast<void*>(&find_next_file_w)}, \
{dll, "FindClose", reinterpret_cast<void*>(&find_close)}, \
{dll, "GetFileAttributesA", reinterpret_cast<void*>(&get_file_attributes_a)}, \
{dll, "GetFullPathNameA", reinterpret_cast<void*>(&get_full_path_name_a)}, \
{dll, "GetFullPathNameW", reinterpret_cast<void*>(&get_full_path_name_w)}, \
{dll, "GetFileAttributesW", reinterpret_cast<void*>(&get_file_attributes_w)}, \
{dll, "GetFileAttributesExA", reinterpret_cast<void*>(&get_file_attributes_ex_a)}, \
{dll, "GetFileAttributesExW", reinterpret_cast<void*>(&get_file_attributes_ex_w)}, \
Expand Down
97 changes: 97 additions & 0 deletions runtime/test/file_system_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ using CopyFileExWFn = BOOL(WINAPI*)(LPCWSTR, LPCWSTR, LPPROGRESS_ROUTINE, LPVOID
using CopyFile2Fn = HRESULT(WINAPI*)(PCWSTR, PCWSTR, COPYFILE2_EXTENDED_PARAMETERS*);
using SetFileAttributesWFn = BOOL(WINAPI*)(LPCWSTR, DWORD);
using SetFileAttributesAFn = BOOL(WINAPI*)(LPCSTR, DWORD);
using GetFullPathNameWFn = DWORD(WINAPI*)(LPCWSTR, DWORD, LPWSTR, LPWSTR*);
using GetFullPathNameAFn = DWORD(WINAPI*)(LPCSTR, DWORD, LPSTR, LPSTR*);

int g_checks = 0;
int g_failures = 0;
Expand Down Expand Up @@ -698,6 +700,101 @@ int main() {
rt::fs::configure(package, overlay);
}

// A caller that makes its path absolute before opening it (Rust's std::fs, std::filesystem::absolute) goes through
// GetFullPathName. The host reads "GAME:\x" as a relative name and answers "<current folder>\GAME:\x", so the
// open that follows never reached the game folder: "GAME:\a\b\c.bin" failed with ERROR_PATH_NOT_FOUND
// while the same name through CreateFileW opened.
{
write_path(package / "nested" / "deeper" / "c.bin", "deep");
const auto full_w = resolve<GetFullPathNameWFn>("kernel32.dll", "GetFullPathNameW");
const auto full_a = resolve<GetFullPathNameAFn>("kernel32.dll", "GetFullPathNameA");
check(full_w && full_a, "kernel32.dll serves GetFullPathNameW and GetFullPathNameA");
if (full_w && full_a) {
wchar_t full[MAX_PATH] = {};
wchar_t* file_part = nullptr;
const DWORD n = full_w(L"GAME:/nested/./deeper/../deeper/c.bin", MAX_PATH, full, &file_part);
check(n == 25 && std::wstring(full) == L"GAME:\\nested\\deeper\\c.bin", "GetFullPathNameW keeps a GAME: path on GAME:, normalized");
check(file_part && std::wstring(file_part) == L"c.bin", "and points the file part at the last name");
HANDLE h = create_w(full, GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
check(h != INVALID_HANDLE_VALUE && read_handle(h) == "deep", "so the path it answers still opens the package file");
if (h != INVALID_HANDLE_VALUE) ::CloseHandle(h);
check(full_w(L"GAME:\\nested\\deeper\\c.bin", 5, full, nullptr) == 26, "a buffer that is too small gets the size with the NUL");
check(full_w(L"game:", MAX_PATH, full, &file_part) == 6 && std::wstring(full) == L"GAME:\\" && file_part == nullptr,
"GAME: alone is the root with no file part");
char narrow[MAX_PATH] = {};
char* narrow_part = nullptr;
check(full_a("GAME:\\nested\\deeper\\c.bin", MAX_PATH, narrow, &narrow_part) == 25 &&
std::string(narrow) == "GAME:\\nested\\deeper\\c.bin" && narrow_part && std::string(narrow_part) == "c.bin",
"GetFullPathNameA answers the same");

// Every other path is still the host's answer.
wchar_t host_ours[MAX_PATH] = {};
wchar_t host_real[MAX_PATH] = {};
const DWORD ours = full_w(L"relative\\x.txt", MAX_PATH, host_ours, nullptr);
const DWORD real = ::GetFullPathNameW(L"relative\\x.txt", MAX_PATH, host_real, nullptr);
check(ours == real && std::wstring(host_ours) == std::wstring(host_real), "a host path is answered by the host");
// Empty string: the host's answer (0), not a GAME: path.
wchar_t empty_ours[4] = {1}, empty_real[4] = {1};
check(full_w(L"", 4, empty_ours, nullptr) == ::GetFullPathNameW(L"", 4, empty_real, nullptr),
"an empty name is answered by the host");

// A trailing separator stays and leaves no file part, as GetFullPathNameW(L"C:\\foo\\") does; a trailing "." does not.
file_part = nullptr;
check(full_w(L"GAME:\\nested\\", MAX_PATH, full, &file_part) == 13 && std::wstring(full) == L"GAME:\\nested\\" &&
file_part == nullptr,
"a trailing separator is kept and there is no file part");
check(full_w(L"GAME:/nested/./", MAX_PATH, full, &file_part) == 13 && std::wstring(full) == L"GAME:\\nested\\" &&
file_part == nullptr,
"\".\\\" before a trailing separator does not remove it");
check(full_w(L"GAME:\\nested\\.", MAX_PATH, full, &file_part) == 12 && std::wstring(full) == L"GAME:\\nested" &&
file_part && std::wstring(file_part) == L"nested",
"a trailing \".\" removes itself, not the folder name");
check(full_w(L"GAME:\\nested\\..\\", MAX_PATH, full, &file_part) == 6 && std::wstring(full) == L"GAME:\\" &&
file_part == nullptr,
"a folder left with \"..\\\" is the root");
check(full_a("GAME:\\nested\\", MAX_PATH, narrow, &narrow_part) == 13 && std::string(narrow) == "GAME:\\nested\\" &&
narrow_part == nullptr,
"GetFullPathNameA keeps the trailing separator too");
check(full_a("GAME:\\nested\\deeper\\c.bin", 5, narrow, &narrow_part) == 26,
"GetFullPathNameA with a small buffer answers the size with the NUL");

// lpFilePart is NULL when the buffer is too small and when the name is refused, not left as the caller had it
// (Wine's GetFullPathNameW, which the Windows function follows here).
wchar_t* sentinel_w = reinterpret_cast<wchar_t*>(1);
char* sentinel_a = reinterpret_cast<char*>(1);
check(full_w(L"GAME:\\nested\\deeper\\c.bin", 5, full, &sentinel_w) == 26 && sentinel_w == nullptr,
"a buffer that is too small clears the file part (W)");
check(full_a("GAME:\\nested\\deeper\\c.bin", 5, narrow, &sentinel_a) == 26 && sentinel_a == nullptr,
"a buffer that is too small clears the file part (A)");
sentinel_w = reinterpret_cast<wchar_t*>(1);
sentinel_a = reinterpret_cast<char*>(1);
check(full_w(L"GAME:\\..\\x", MAX_PATH, full, &sentinel_w) == 0 && sentinel_w == nullptr,
"a refused name clears the file part (W)");
check(full_a("GAME:\\..\\x", MAX_PATH, narrow, &sentinel_a) == 0 && sentinel_a == nullptr,
"a refused name clears the file part (A)");

// A path that climbs above GAME:\ is refused the way CreateFileW refuses the same name (ERROR_INVALID_NAME), and
// never reaches the host: Windows would clamp "C:\..\x" at the drive root, but the open of "GAME:\..\x" fails, and
// an answer that opens something else than the open would is worse than none.
for (const wchar_t* climbing : {L"GAME:\\..", L"GAME:\\..\\x", L"GAME:\\nested\\..\\..\\x", L"game:/../x"}) {
wchar_t untouched[MAX_PATH] = L"untouched";
SetLastError(0);
const bool refused = full_w(climbing, MAX_PATH, untouched, nullptr) == 0 && GetLastError() == ERROR_INVALID_NAME &&
std::wstring(untouched) == L"untouched";
SetLastError(0);
const bool same_as_open = create_w(climbing, GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr) ==
INVALID_HANDLE_VALUE &&
GetLastError() == ERROR_INVALID_NAME;
check(refused && same_as_open, "a GAME: path above the root is refused by GetFullPathNameW as CreateFileW refuses it");
}
SetLastError(0);
char narrow_untouched[MAX_PATH] = "untouched";
check(full_a("GAME:\\..\\x", MAX_PATH, narrow_untouched, nullptr) == 0 && GetLastError() == ERROR_INVALID_NAME &&
std::string(narrow_untouched) == "untouched",
"GetFullPathNameA refuses it too");
}
}

std::filesystem::remove_all(root);
std::printf("%d check(s), %d failure(s)\n", g_checks, g_failures);
return g_failures == 0 ? 0 : 1;
Expand Down