Skip to content
Draft
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
4 changes: 3 additions & 1 deletion runtime/shims/file_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
25 changes: 25 additions & 0 deletions runtime/test/file_system_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down