Skip to content
Merged
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
9 changes: 6 additions & 3 deletions runtime/shims/file_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1220,13 +1220,12 @@ bool fallback_supports(const COPYFILE2_EXTENDED_PARAMETERS* parameters) {
}

// CopyFile2 through CopyFileExW, for hosts without it (only for calls fallback_supports). The cancel flag has the
// same type in both; one already set is answered here, since Wine 9's CopyFileExW doesn't read it. A COPYFILE2
// progress routine has another signature and message set, so it isn't called here.
// same type in both (one already set is answered by copy_file2 before this runs). A COPYFILE2 progress routine has
// another signature and message set, so it isn't called here.
HRESULT copy_file2_fallback(const wchar_t* source, const wchar_t* destination,
const COPYFILE2_EXTENDED_PARAMETERS* parameters) {
const DWORD flags = parameters ? parameters->dwCopyFlags & kFallbackPassedFlags : 0;
BOOL* const cancel = parameters ? parameters->pfCancel : nullptr;
if (cancel && *cancel) return HRESULT_FROM_WIN32(ERROR_REQUEST_ABORTED);
if (::CopyFileExW(source, destination, nullptr, nullptr, cancel, flags)) return S_OK;
const DWORD error = GetLastError();
return HRESULT_FROM_WIN32(error ? error : ERROR_GEN_FAILURE);
Expand All @@ -1248,6 +1247,10 @@ HRESULT WINAPI copy_file2(PCWSTR from, PCWSTR to, COPYFILE2_EXTENDED_PARAMETERS*
std::filesystem::path source, destination;
DWORD error = 0;
if (!copy_paths(from, to, fail_if_exists, source, destination, error)) return HRESULT_FROM_WIN32(error);
// A cancel flag already set is answered here, for the host's CopyFile2 as well as the fallback: Windows returns
// ERROR_REQUEST_ABORTED and writes nothing, while Wine reads the flag in neither call (Wine 11.20's CopyFile2 logs
// "pfCancel is not supported" and copies, dlls/kernelbase/file.c copy_file; Wine 9 has only CopyFileExW).
if (parameters && parameters->pfCancel && *parameters->pfCancel) return HRESULT_FROM_WIN32(ERROR_REQUEST_ABORTED);
if (host) return host(source.c_str(), destination.c_str(), parameters);
return copy_file2_fallback(source.c_str(), destination.c_str(), parameters);
}
Expand Down
149 changes: 118 additions & 31 deletions runtime/test/file_system_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <algorithm>
#include <clocale>
#include <cstdio>
#include <exception>
#include <filesystem>
#include <initializer_list>
#include <string>
Expand Down Expand Up @@ -113,12 +114,100 @@ int count(const std::vector<std::wstring>& names, const wchar_t* name) {

bool contains(const std::vector<std::wstring>& names, const wchar_t* name) { return count(names, name) != 0; }

// 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);

} // namespace

int main() {
// 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 =
std::filesystem::temp_directory_path() / ("leonida_file_system_" + std::to_string(GetCurrentProcessId()));
std::filesystem::remove_all(root);
std::filesystem::temp_directory_path() / ("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 {
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");
Expand Down Expand Up @@ -296,24 +385,8 @@ int main() {
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();
PACL old_dacl = nullptr;
PSECURITY_DESCRIPTOR sd = nullptr;
bool denied = false;
if (GetNamedSecurityInfoW(marks.c_str(), SE_FILE_OBJECT, DACL_SECURITY_INFORMATION, nullptr, nullptr,
&old_dacl, nullptr, &sd) == ERROR_SUCCESS) {
EXPLICIT_ACCESS_W deny{};
deny.grfAccessPermissions = FILE_LIST_DIRECTORY;
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) {
denied = SetNamedSecurityInfoW(const_cast<LPWSTR>(marks.c_str()), SE_FILE_OBJECT,
DACL_SECURITY_INFORMATION, nullptr, nullptr, new_dacl,
nullptr) == ERROR_SUCCESS;
LocalFree(new_dacl);
}
}
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;
Expand All @@ -326,10 +399,13 @@ int main() {
} else {
std::printf("SKIP could not deny listing the marker folder\n");
}
if (denied)
SetNamedSecurityInfoW(const_cast<LPWSTR>(marks.c_str()), SE_FILE_OBJECT, DACL_SECURITY_INFORMATION,
nullptr, nullptr, old_dacl, nullptr);
if (sd) LocalFree(sd);
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.
Expand Down Expand Up @@ -1130,11 +1206,20 @@ int main() {
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\\*");
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");
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,
Expand All @@ -1149,14 +1234,16 @@ int main() {

check(move_w(L"GAME:\\moves\\dir", L"GAME:\\moves\\DIR") != FALSE,
"MoveFileW renames an overlay folder to the same name in another case");
check(has_exact(names_in(L"GAME:\\moves\\*"), L"DIR"), "the listing shows the folder's new 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");
}

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

} // namespace