From cea424f3573900e973732f726161a901a1d666de Mon Sep 17 00:00:00 2001 From: syfyra <258908070+syfyra@users.noreply.github.com> Date: Thu, 26 Nov 2026 17:25:34 +0100 Subject: [PATCH] file_system: a move onto an existing GAME: folder reports ERROR_ALREADY_EXISTS (FS-MOVE-ONTO-FOLDER) MoveFileEx onto a folder failed with ERROR_ACCESS_DENIED whatever the flags. The host reports any existing destination, a folder too, as ERROR_ALREADY_EXISTS without MOVEFILE_REPLACE_EXISTING, and ERROR_ACCESS_DENIED only with it (measured on Windows 11 for a file and a folder source, onto an empty and a non-empty folder). A title that checks for ERROR_ALREADY_EXISTS to decide to clear the way first now gets it. MoveFileW, which passes no replace flag, follows. Co-Authored-By: Claude Opus 5.5 --- runtime/shims/file_system.cpp | 4 +++- runtime/test/file_system_test.cpp | 25 +++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/runtime/shims/file_system.cpp b/runtime/shims/file_system.cpp index 8a20b21e9b..8a8177f554 100644 --- a/runtime/shims/file_system.cpp +++ b/runtime/shims/file_system.cpp @@ -1317,8 +1317,10 @@ BOOL WINAPI move_file_ex_w(LPCWSTR from, LPCWSTR to, DWORD flags) { } const View to_view = view(r, dst); + // Any existing name, a folder too, is already there without MOVEFILE_REPLACE_EXISTING; with it a folder cannot be + // replaced (measured on Windows 11: ERROR_ALREADY_EXISTS, and ERROR_ACCESS_DENIED with the flag). + if (to_view.kind != Kind::None && (flags & MOVEFILE_REPLACE_EXISTING) == 0) return fail_bool(ERROR_ALREADY_EXISTS); if (to_view.kind == Kind::Directory) return fail_bool(ERROR_ACCESS_DENIED); - if (to_view.kind == Kind::File && (flags & MOVEFILE_REPLACE_EXISTING) == 0) return fail_bool(ERROR_ALREADY_EXISTS); if (!is_folder(r, parent_of(dst))) return fail_bool(ERROR_PATH_NOT_FOUND); std::error_code ec; diff --git a/runtime/test/file_system_test.cpp b/runtime/test/file_system_test.cpp index af37935f6b..4c103989c0 100644 --- a/runtime/test/file_system_test.cpp +++ b/runtime/test/file_system_test.cpp @@ -1042,6 +1042,31 @@ void run_checks(const std::filesystem::path& root) { 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"); + // Onto a folder (the package's data\sub, the overlay's cache\renamed), as MoveFileEx answers on the host: the + // name is already there without MOVEFILE_REPLACE_EXISTING, and a folder is not replaced with it. Nothing moves. + const struct { + const wchar_t* folder; + std::string name; + } folders[] = {{L"GAME:\\data\\sub", "GAME:\\data\\sub"}, {L"GAME:\\cache\\renamed", "GAME:\\cache\\renamed"}}; + for (const auto& [folder, name] : folders) { + SetLastError(0); + check(move_ex_w(L"GAME:\\misc\\keep.txt", folder, 0) == FALSE && GetLastError() == ERROR_ALREADY_EXISTS, + ("MoveFileExW of a file onto " + name + " reports ERROR_ALREADY_EXISTS").c_str()); + SetLastError(0); + check(move_w(L"GAME:\\misc\\keep.txt", folder) == FALSE && GetLastError() == ERROR_ALREADY_EXISTS, + ("MoveFileW of a file onto " + name + " reports ERROR_ALREADY_EXISTS").c_str()); + SetLastError(0); + check(move_ex_w(L"GAME:\\misc\\keep.txt", folder, MOVEFILE_REPLACE_EXISTING) == FALSE && + GetLastError() == ERROR_ACCESS_DENIED, + ("with MOVEFILE_REPLACE_EXISTING a file is not moved onto " + name + " (ERROR_ACCESS_DENIED)").c_str()); + } + check(attributes_w(L"GAME:\\misc\\keep.txt") != INVALID_FILE_ATTRIBUTES && + read_path(package / "misc" / "keep.txt") == "keep", + "the file refused at a folder stays where it was"); + SetLastError(0); + check(move_w(L"GAME:\\cache\\renamed", L"GAME:\\data\\sub") == FALSE && GetLastError() == ERROR_ALREADY_EXISTS && + std::filesystem::is_directory(overlay / "cache" / "renamed"), + "an overlay folder moved onto a folder reports ERROR_ALREADY_EXISTS and stays"); } // MOVEFILE_WRITE_THROUGH on a package file: the copy and the marker are flushed before the call returns.