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
158 changes: 148 additions & 10 deletions runtime/shims/file_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,17 @@ std::wstring scratch_folder(const std::filesystem::path& overlay) {
return folder.name;
}

// Non-creating lookup for this process's already-reserved T: folder below <overlay>\.gta6rt-temp.
std::wstring existing_scratch_folder(const std::filesystem::path& overlay, bool& failed) {
failed = false;
if (overlay.empty()) return scratch_run();
std::lock_guard lock(g_scratch_mutex);
const auto it = g_scratch_folders.find(lower(overlay.native()));
if (it == g_scratch_folders.end()) return {};
failed = (it->second.error != 0);
return it->second.name;
}

Drive console_drive(const std::wstring& path, size_t& prefix_length) {
if (path.size() >= kGamePrefixLength && lower(path.substr(0, kGamePrefixLength)) == kGamePrefix) {
prefix_length = kGamePrefixLength;
Expand All @@ -246,22 +257,31 @@ Drive console_drive(const std::wstring& path, size_t& prefix_length) {
return letter == L'g' ? Drive::Game : Drive::Scratch;
}

Parsed parse_console(const std::wstring& path, std::vector<std::wstring>& parts) {
Parsed parse_console_parts(const std::wstring& path, std::vector<std::wstring>& parts, Drive* out_drive = nullptr,
bool create_scratch = true) {
size_t prefix_length = 0;
const Drive drive = console_drive(path, prefix_length);
if (out_drive) *out_drive = drive;
if (drive == Drive::None) return Parsed::Host;
parts.clear();
if (drive == Drive::Scratch) {
const std::filesystem::path overlay = roots().overlay;
const std::wstring folder = scratch_folder(overlay);
if (folder.empty()) return Parsed::Refused;
parts.push_back(kScratchDir);
parts.push_back(folder);
// The folder is this process's from its reservation on; made again if something removed it since.
const DWORD last_error = ::GetLastError();
std::error_code ec;
if (!overlay.empty()) std::filesystem::create_directories(win32_io_path(overlay / kScratchDir / folder), ec);
::SetLastError(last_error);
if (create_scratch) {
const std::wstring folder = scratch_folder(overlay);
if (folder.empty()) return Parsed::Refused;
parts.push_back(folder);
// The folder is this process's from its reservation on; made again if something removed it since.
const DWORD last_error = ::GetLastError();
std::error_code ec;
if (!overlay.empty()) std::filesystem::create_directories(win32_io_path(overlay / kScratchDir / folder), ec);
::SetLastError(last_error);
} else {
bool failed = false;
const std::wstring folder = existing_scratch_folder(overlay, failed);
if (failed) return Parsed::Refused;
parts.push_back(folder);
}
}
const size_t floor = parts.size(); // ".." may not climb out of the drive's own root
std::wstring part;
Expand Down Expand Up @@ -297,6 +317,10 @@ Parsed parse_console(const std::wstring& path, std::vector<std::wstring>& parts)
return Parsed::Console;
}

Parsed parse_console(const std::wstring& path, std::vector<std::wstring>& parts) {
return parse_console_parts(path, parts);
}

std::filesystem::path join(const std::filesystem::path& root, const std::vector<std::wstring>& parts) {
std::filesystem::path p = root;
for (const auto& part : parts) p /= part;
Expand Down Expand Up @@ -439,6 +463,96 @@ View view(const Roots& r, const std::vector<std::wstring>& parts) {
return v;
}

// Walks up `p` without creating directories to find the nearest existing directory on its volume.
std::filesystem::path existing_ancestor_dir(std::filesystem::path p, DWORD missing_error, DWORD& error) {
std::error_code ec;
while (!p.empty()) {
const std::filesystem::path io = win32_io_path(p);
const auto st = std::filesystem::status(io, ec);
if (st.type() == std::filesystem::file_type::directory) return io;
if (st.type() != std::filesystem::file_type::not_found && !ec) {
error = ERROR_DIRECTORY;
return {};
}
const std::filesystem::path parent = p.parent_path();
if (parent == p) break;
p = parent;
}
error = missing_error;
return {};
}

// Resolves a console path for read-only disk-space or drive-type queries without creating `overlay` or `.gta6rt-temp`.
// Sets `handled` true when `path` is refused or console-owned (`out_dir` valid on success, `error` set on failure).
bool resolve_console_readonly_dir(const wchar_t* path, DWORD missing_error, bool& handled,
std::filesystem::path& out_dir, bool& is_root, DWORD& error) {
handled = false;
is_root = false;
error = 0;
std::vector<std::wstring> parts;
Drive drive = Drive::None;
const Parsed parsed = parse_console_parts(path, parts, &drive, /*create_scratch=*/false);
if (parsed == Parsed::Host) return false;
handled = true;
if (parsed == Parsed::Refused) {
error = ERROR_INVALID_NAME;
return false;
}
const Roots r = roots();
std::error_code ec;
if (drive == Drive::Scratch) {
if (r.overlay.empty()) {
error = ERROR_PATH_NOT_FOUND;
return false;
}
is_root = (parts.size() == 2);
if (is_root) {
const std::filesystem::path scratch_root =
parts[1].empty() ? (r.overlay / kScratchDir) : (r.overlay / kScratchDir / parts[1]);
out_dir = existing_ancestor_dir(scratch_root, missing_error, error);
return !out_dir.empty();
}
if (parts[1].empty()) {
error = missing_error;
return false;
}
const std::filesystem::path p = disk_path(r.overlay, parts);
const auto st = std::filesystem::status(p, ec);
if (st.type() == std::filesystem::file_type::directory) {
out_dir = p;
return true;
}
error = (st.type() != std::filesystem::file_type::not_found && !ec) ? ERROR_DIRECTORY : missing_error;
return false;
}
if (r.game.empty()) {
error = ERROR_PATH_NOT_FOUND;
return false;
}
is_root = parts.empty();
if (is_root) {
const std::filesystem::path g = win32_io_path(r.game);
const auto st = std::filesystem::status(g, ec);
if (st.type() == std::filesystem::file_type::directory) {
out_dir = g;
return true;
}
error = (st.type() != std::filesystem::file_type::not_found && !ec) ? ERROR_DIRECTORY : missing_error;
return false;
}
const View v = view(r, parts);
if (v.kind == Kind::None) {
error = missing_error;
return false;
}
if (v.kind == Kind::File) {
error = ERROR_DIRECTORY;
return false;
}
out_dir = v.in_overlay ? disk_path(r.overlay, parts) : disk_path(r.game, parts);
return true;
}

bool is_folder(const Roots& r, const std::vector<std::wstring>& parts) { return view(r, parts).kind == Kind::Directory; }

// Whether two console paths name the same entry: names compare without case, as the lookups do.
Expand Down Expand Up @@ -1715,6 +1829,28 @@ BOOL WINAPI get_disk_free_space_a(LPCSTR root_path_name,
number_of_free_clusters, total_number_of_clusters);
}

UINT WINAPI get_drive_type_w(LPCWSTR root_path) {
if (!root_path || !*root_path) return ::GetDriveTypeW(root_path);
bool handled = false;
bool is_root = false;
DWORD error = 0;
std::filesystem::path resolved;
if (!resolve_console_readonly_dir(root_path, ERROR_PATH_NOT_FOUND, handled, resolved, is_root, error)) {
return handled ? DRIVE_NO_ROOT_DIR : ::GetDriveTypeW(root_path);
}
if (!is_root) return DRIVE_NO_ROOT_DIR;
std::vector<wchar_t> volume_root((std::max<size_t>)(MAX_PATH, resolved.native().size() + 16), L'\0');
if (!::GetVolumePathNameW(resolved.c_str(), volume_root.data(), static_cast<DWORD>(volume_root.size())))
return DRIVE_NO_ROOT_DIR;
return ::GetDriveTypeW(volume_root.data());
}

UINT WINAPI get_drive_type_a(LPCSTR root_path) {
if (!root_path) return ::GetDriveTypeW(nullptr);
const std::wstring wide = widen_ansi(root_path);
return get_drive_type_w(wide.c_str());
}

} // namespace rt::fs

namespace {
Expand Down Expand Up @@ -1790,7 +1926,9 @@ BOOL WINAPI read_file_scatter(HANDLE file, FILE_SEGMENT_ELEMENT segments[], DWOR
{dll, "GetDiskFreeSpaceExW", reinterpret_cast<void*>(&get_disk_free_space_ex_w)}, \
{dll, "ReadFile", reinterpret_cast<void*>(&read_file)}, \
{dll, "ReadFileEx", reinterpret_cast<void*>(&read_file_ex)}, \
{dll, "ReadFileScatter", reinterpret_cast<void*>(&read_file_scatter)}
{dll, "ReadFileScatter", reinterpret_cast<void*>(&read_file_scatter)}, \
{dll, "GetDriveTypeA", reinterpret_cast<void*>(&get_drive_type_a)}, \
{dll, "GetDriveTypeW", reinterpret_cast<void*>(&get_drive_type_w)}
} // namespace

SHIM_TABLE(file_system,
Expand Down
73 changes: 73 additions & 0 deletions runtime/test/file_system_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,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 GetDriveTypeWFn = UINT(WINAPI*)(LPCWSTR);
using GetDriveTypeAFn = UINT(WINAPI*)(LPCSTR);

int g_checks = 0;
int g_failures = 0;
Expand Down Expand Up @@ -1990,6 +1992,77 @@ void run_checks(const std::filesystem::path& root) {
std::filesystem::remove_all(extended(fixture), cleanup_error);
check(!cleanup_error, "the synthetic long paths are removed after the test");
}

// Drive-type queries for GAME:, G:, and T: match host semantics without creating folders.
{
const auto drive_w = resolve<GetDriveTypeWFn>("api-ms-win-core-file-l1-1-0.dll", "GetDriveTypeW");
const auto drive_a = resolve<GetDriveTypeAFn>("kernel32.dll", "GetDriveTypeA");
check(drive_w && drive_a, "GetDriveTypeW/A resolve through the shim registry");

if (drive_w && drive_a) {
write_path(package / "disk_pkg" / "file.dat", "pkg");
check(mkdir_w(L"GAME:\\disk_ov", nullptr) != FALSE, "overlay subdir created for drive queries");
write_path(package / "disk_del" / "inner.txt", "pkg");
check(delete_w(L"GAME:\\disk_del\\inner.txt") != FALSE && rmdir_w(L"GAME:\\disk_del") != FALSE,
"deleted package dir prepared for drive queries");
check(mkdir_w(L"T:\\disk_scratch", nullptr) != FALSE, "T:\\ subdir created for drive queries");
{
const HANDLE hf = create_w(L"T:\\disk_scratch\\s.bin", GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, 0,
nullptr);
check(hf != INVALID_HANDLE_VALUE, "T:\\ file created for drive queries");
if (hf != INVALID_HANDLE_VALUE) ::CloseHandle(hf);
}

wchar_t host_vol_root[MAX_PATH] = {};
const bool have_vol = ::GetVolumePathNameW(package.c_str(), host_vol_root, MAX_PATH) != FALSE;
const UINT host_drive = have_vol ? ::GetDriveTypeW(host_vol_root) : DRIVE_UNKNOWN;

// Missing overlay / .gta6rt-temp: read-only queries on T: succeed via existing ancestor and create nothing.
const std::filesystem::path fresh_overlay = root / "disk_fresh_overlay";
rt::fs::configure(package, fresh_overlay);
check(!have_vol || drive_w(L"T:\\") == host_drive, "GetDriveTypeW(T:\\) matches backing drive type");
check(!std::filesystem::exists(fresh_overlay),
"read-only drive queries on T:\\ do not create the overlay or .gta6rt-temp");
rt::fs::configure(package, overlay);

for (const wchar_t* bad : {L"GAME:\\..\\escape", L"GAME:\\.gta6rt-deleted", L"GAME:\\bad:stream"}) {
check(drive_w(bad) == DRIVE_NO_ROOT_DIR,
"GetDriveTypeW returns DRIVE_NO_ROOT_DIR on refused path");
}

// GetDriveTypeW/A: console roots report backing drive type; subdirs/files/missing report DRIVE_NO_ROOT_DIR.
if (have_vol) {
for (const wchar_t* rt : {L"GAME:", L"GAME:\\", L"game:/", L"GAME:\\.", L"G:", L"G:\\", L"T:",
L"T:\\", L"T:\\."}) {
check(drive_w(rt) == host_drive, "GetDriveTypeW answers backing drive type for console root");
}
for (const char* rt_a : {"GAME:", "GAME:\\", "G:\\", "T:\\"}) {
check(drive_a(rt_a) == host_drive, "GetDriveTypeA answers backing drive type for console root");
}
}
for (const wchar_t* non_root : {L"GAME:\\disk_pkg", L"GAME:\\disk_ov", L"GAME:\\disk_pkg\\file.dat",
L"GAME:\\disk_missing", L"GAME:\\disk_del", L"T:\\disk_scratch",
L"T:\\disk_scratch\\s.bin", L"T:\\disk_missing", L""}) {
check(drive_w(non_root) == DRIVE_NO_ROOT_DIR,
"GetDriveTypeW returns DRIVE_NO_ROOT_DIR for non-root/missing path");
}
check(drive_a("GAME:\\disk_pkg") == DRIVE_NO_ROOT_DIR,
"GetDriveTypeA returns DRIVE_NO_ROOT_DIR for non-root path");

// Host paths and nullptr (current directory) pass through matching ::GetDriveType* directly.
check(drive_w(nullptr) == ::GetDriveTypeW(nullptr) && drive_a(nullptr) == ::GetDriveTypeW(nullptr),
"GetDriveTypeW/A(nullptr) matches host");
check(!have_vol || drive_w(host_vol_root) == host_drive, "GetDriveTypeW forwards host path directly");

// Unconfigured roots: GAME: and empty-overlay T: report DRIVE_NO_ROOT_DIR.
rt::fs::configure({}, {});
check(drive_w(L"GAME:\\") == DRIVE_NO_ROOT_DIR, "unconfigured GAME:\\ reports DRIVE_NO_ROOT_DIR");
rt::fs::configure(package, {});
check(!have_vol || drive_w(L"GAME:\\") == host_drive, "GAME:\\ succeeds even with empty overlay");
check(drive_w(L"T:\\") == DRIVE_NO_ROOT_DIR, "T:\\ with empty overlay reports DRIVE_NO_ROOT_DIR");
rt::fs::configure(package, overlay);
}
}
}

} // namespace
Loading