Files
mandy/runtime/shims/file_system.h
Jan Trencansky 1d37e033eb file_system: reserve T:'s folder with a create-only call, never reuse one
Xiiano's review of old upstream PR 8161: the folder name (start time to
the millisecond plus process id) is not guaranteed unique, and earlier
runs' folders are kept, so a reused id in the same millisecond would
land in a stale folder and bring ERROR_FILE_EXISTS back. Under Wine
every fresh run gets the same small ids (both run_tiny_game_dir runs
here were pid 32).

rt::fs::reserve_unique_dir(base, stem) makes base\stem with
CreateDirectoryW and takes success as ownership; on ERROR_ALREADY_EXISTS
it tries stem-2, stem-3, ... up to 1000 names, and fails with the Win32
error otherwise. T:'s folder is reserved once per overlay under a mutex
(the first T: call or rt::fs::scratch_dir(), which the startup line
calls), so concurrent first calls share it. A failed reservation is
printed once and T: paths fail with ERROR_INVALID_NAME.
rt::fs::scratch_stem() gives the first name tried.

file_system_test: with stem and stem-2 taken, reserve_unique_dir makes
stem-3, new and empty, then stem-4; a file with the next name counts as
taken; max_tries names taken fail with ERROR_ALREADY_EXISTS; a missing
base is made. T: with folders of this process's own stem and stem-2
already kept takes stem-3 and CREATE_NEW succeeds; eight threads making
their first T: call at once reserve one folder. The second-process test
stays.

Restored from old upstream PR 8161 and ported onto the new main: the
base folder and every candidate go through main's win32_io_path, like
the other creates in file_system.cpp, so a long overlay root keeps
working.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-24 06:54:37 -05:00

50 lines
3.1 KiB
C++

// Console (GDK) file paths for a mapped title.
//
// A title's file API sees the console's drives: "GAME:\\data\\shaders\\x.sps" is a file inside the installed game
// folder. ivall/xcl (GPL-2.0-only; facts only here) documents for Xbox Series GDK titles that GAME: names the game
// folder and that writes go to a writable overlay rather than to the package, which is read-only. gta6rt does the
// same:
// GAME:<rest> a read opens the overlay copy when one exists, else <game dir>\<rest>;
// an open that can write opens <overlay dir>\<rest>, copying the package file there first when the
// disposition keeps its contents, so the package is never modified;
// a delete leaves a marker in the overlay instead of removing a package file.
// anything else is a host path and is passed through unchanged.
// A GAME: path that climbs above the root with ".." or names another drive is refused with ERROR_INVALID_NAME.
//
// The shims that use this live in file_system.cpp: CreateFile*, FindFirstFile*/FindNextFile*/FindClose,
// GetFileAttributes*/GetFileAttributesEx*, DeleteFile*, CreateDirectory*/RemoveDirectory*, MoveFile*/MoveFileEx*,
// CopyFile*/CopyFileEx*/CopyFile2 and SetFileAttributes* (the rules for each are in docs/runtime_imports.md).
#pragma once
#include <filesystem>
namespace rt::fs {
inline constexpr wchar_t kPersistentStorageDir[] = L".gta6rt-persistent";
// Sets the roots. main.cpp calls this once, before anything is mapped; the test calls it per case. The overlay is
// created on the first write, not here.
void configure(const std::filesystem::path& game_dir, const std::filesystem::path& overlay_dir);
std::filesystem::path game_dir();
std::filesystem::path overlay_dir();
// The folder "T:\" names in this process: <overlay dir>\.gta6rt-temp\<stem>, or <stem>-2, -3, ... when that name was
// taken (scratch_stem(), reserve_unique_dir). Reserved on the first T: call or the first call of this function for the
// overlay and kept for the rest of the process; empty while no overlay is configured or when no folder could be made.
std::filesystem::path scratch_dir();
// The first name tried for that folder: the process's start time (UTC) and id, "YYYYMMDD-HHMMSS-mmm-pid<id>".
std::wstring scratch_stem();
// Makes a new folder base\stem, or base\stem-2 ... base\stem-<max_tries> when the names before it exist, each with a
// create-only CreateDirectoryW, so the folder returned did not exist before and no other process got it. Makes `base`
// first. Returns an empty path, with the Win32 error in `error`, when every name was taken (ERROR_ALREADY_EXISTS) or
// a create failed for another reason.
std::filesystem::path reserve_unique_dir(const std::filesystem::path& base, const std::wstring& stem,
unsigned long& error, unsigned max_tries = 1000);
// For tests: true makes the CopyFile2 shim use its CopyFileExW fallback even where the host has CopyFile2, so the
// path Wine 9 takes can be checked on Windows. Off by default; main.cpp never sets it.
void force_copy_file2_fallback(bool force);
} // namespace rt::fs