Skip to content
Closed
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 docs/runtime_imports.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,9 @@ and the `api-ms-win-core-file-*` sets a GDK title links against.
when its folder is missing, was removed, or is a file, and creates nothing (`T:` is the exception: a create there
makes its parent folders); `FindFirstFile` fails the same way for a
listing inside such a folder (a pattern that matches nothing in a folder that exists is still
`ERROR_FILE_NOT_FOUND`, and a listing directly below a file is `ERROR_DIRECTORY`, as on the host).
`ERROR_FILE_NOT_FOUND`, and a listing directly below a file is `ERROR_DIRECTORY`, as on the host). `DeleteFile*`,
`RemoveDirectory*`, `SetFileAttributes*` and the source of `MoveFile*` answer a name inside such a folder with
`ERROR_PATH_NOT_FOUND` too, and a missing name in a folder that exists with `ERROR_FILE_NOT_FOUND`, as on the host.
- `MoveFile*` onto the same name succeeds and changes nothing. A rename that changes only the case renames the overlay
entry; a package file gets an overlay copy under the new spelling, and a package folder is refused like any other
move of one.
Expand Down
15 changes: 8 additions & 7 deletions runtime/shims/file_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,10 @@ std::vector<std::wstring> parent_of(const std::vector<std::wstring>& parts) {
return parts.empty() ? parts : std::vector<std::wstring>(parts.begin(), parts.end() - 1);
}

DWORD missing_error(const Roots& r, const std::vector<std::wstring>& parts) {
return is_folder(r, parent_of(parts)) ? ERROR_FILE_NOT_FOUND : ERROR_PATH_NOT_FOUND;
}

// Whether a folder has nothing in the title's view: no overlay entry and no package entry that is not deleted.
bool empty_in_view(const Roots& r, const std::vector<std::wstring>& parts) {
std::error_code ec;
Expand Down Expand Up @@ -989,10 +993,7 @@ BOOL WINAPI delete_file_w(LPCWSTR name) {
removed = true;
}
}
if (!removed) {
SetLastError(ERROR_FILE_NOT_FOUND);
return FALSE;
}
if (!removed) return fail_bool(missing_error(r, parts));
return TRUE;
}

Expand Down Expand Up @@ -1081,7 +1082,7 @@ BOOL WINAPI remove_directory_w(LPCWSTR name) {
if (r.game.empty() || r.overlay.empty()) return fail_bool(ERROR_PATH_NOT_FOUND);
if (drive_root(name, parts)) return fail_bool(ERROR_ACCESS_DENIED); // "GAME:" and "T:" themselves stay
const View v = view(r, parts);
if (v.kind == Kind::None) return fail_bool(ERROR_FILE_NOT_FOUND);
if (v.kind == Kind::None) return fail_bool(missing_error(r, parts));
if (v.kind == Kind::File) return fail_bool(ERROR_DIRECTORY);
if (!empty_in_view(r, parts)) return fail_bool(ERROR_DIR_NOT_EMPTY);

Expand Down Expand Up @@ -1118,7 +1119,7 @@ BOOL WINAPI move_file_ex_w(LPCWSTR from, LPCWSTR to, DWORD flags) {
if (r.game.empty() || r.overlay.empty()) return fail_bool(ERROR_PATH_NOT_FOUND);
if (drive_root(from, src) || drive_root(to, dst)) return fail_bool(ERROR_ACCESS_DENIED);
const View from_view = view(r, src);
if (from_view.kind == Kind::None) return fail_bool(ERROR_FILE_NOT_FOUND);
if (from_view.kind == Kind::None) return fail_bool(missing_error(r, src));
const std::filesystem::path overlay_from = disk_path(r.overlay, src), overlay_to = disk_path(r.overlay, dst);
if (same_entry(src, dst)) {
// The destination is the source, so it is not "already there". The host succeeds at a move onto the same
Expand Down Expand Up @@ -1369,7 +1370,7 @@ BOOL WINAPI set_file_attributes_w(LPCWSTR name, DWORD attributes) {
if (r.game.empty() || r.overlay.empty()) return fail_bool(ERROR_PATH_NOT_FOUND);
if (parts.empty()) return fail_bool(ERROR_ACCESS_DENIED);
const View v = view(r, parts);
if (v.kind == Kind::None) return fail_bool(ERROR_FILE_NOT_FOUND);
if (v.kind == Kind::None) return fail_bool(missing_error(r, parts));

// The attributes belong to the overlay copy: a package file is copied up and a package folder mirrored first,
// so the package keeps its own attributes and reads, which prefer the overlay, see the new ones.
Expand Down
47 changes: 45 additions & 2 deletions runtime/test/file_system_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -492,8 +492,8 @@ void run_checks(const std::filesystem::path& root) {
check(delete_w(L"GAME:\\data\\bye.txt") == FALSE && GetLastError() == ERROR_FILE_NOT_FOUND,
"deleting an already hidden file reports ERROR_FILE_NOT_FOUND");
SetLastError(0);
check(delete_w(L"GAME:\\nope\\missing.txt") == FALSE && GetLastError() == ERROR_FILE_NOT_FOUND,
"deleting a file that never existed reports ERROR_FILE_NOT_FOUND");
check(delete_w(L"GAME:\\nope\\missing.txt") == FALSE && GetLastError() == ERROR_PATH_NOT_FOUND,
"deleting a file in a folder that never existed reports ERROR_PATH_NOT_FOUND");

// Deleting a nested package file creates parent directories inside .gta6rt-deleted/; those marker parent
// directories must not hide the corresponding ancestor directories in the package.
Expand Down Expand Up @@ -709,6 +709,49 @@ void run_checks(const std::filesystem::path& root) {
"and the new folder is empty: only . and ..");
}

{
const std::filesystem::path host = root / "host_missing";
write_path(host / "file.txt", "file");
const std::wstring host_dest = (host / "dest.txt").wstring();
auto error_of = [](BOOL ok) { return ok ? DWORD{0} : GetLastError(); };
struct Name {
const wchar_t* game;
std::filesystem::path host;
DWORD expected;
const char* what;
};
const Name names[] = {
{L"GAME:\\nowhere\\x.txt", host / "nowhere" / "x.txt", ERROR_PATH_NOT_FOUND, "inside a missing folder"},
{L"GAME:\\misc\\keep.txt\\x.txt", host / "file.txt" / "x.txt", ERROR_PATH_NOT_FOUND, "below a file"},
{L"GAME:\\misc\\absent.txt", host / "absent.txt", ERROR_FILE_NOT_FOUND, "missing from a folder that exists"},
};
for (const Name& n : names) {
const std::wstring native = n.host.wstring();
const std::string suffix = std::string(" of a name ") + n.what + " fails as on the host";
SetLastError(0);
const DWORD host_delete = error_of(::DeleteFileW(native.c_str()));
SetLastError(0);
check(error_of(delete_w(n.game)) == n.expected && host_delete == n.expected, ("DeleteFileW" + suffix).c_str());
SetLastError(0);
const DWORD host_rmdir = error_of(::RemoveDirectoryW(native.c_str()));
SetLastError(0);
check(error_of(rmdir_w(n.game)) == n.expected && host_rmdir == n.expected,
("RemoveDirectoryW" + suffix).c_str());
SetLastError(0);
const DWORD host_setattr = error_of(::SetFileAttributesW(native.c_str(), FILE_ATTRIBUTE_NORMAL));
SetLastError(0);
check(error_of(setattr_w(n.game, FILE_ATTRIBUTE_NORMAL)) == n.expected && host_setattr == n.expected,
("SetFileAttributesW" + suffix).c_str());
SetLastError(0);
const DWORD host_move = error_of(::MoveFileExW(native.c_str(), host_dest.c_str(), MOVEFILE_REPLACE_EXISTING));
SetLastError(0);
check(error_of(move_ex_w(n.game, L"GAME:\\misc\\dest.txt", MOVEFILE_REPLACE_EXISTING)) == n.expected &&
host_move == n.expected,
("MoveFileExW" + suffix).c_str());
}
check(!std::filesystem::exists(overlay / "nowhere") && !std::filesystem::exists(overlay / "misc" / "dest.txt"),
"and none of them leaves anything in the overlay");
}
// Recreating a removed package directory clears its own marker, but not nested deletion markers.
{
std::filesystem::create_directories(package / "recreate_dir");
Expand Down
Loading