diff options
Diffstat (limited to 'runtime/shims/file_system.cpp')
| -rw-r--r-- | runtime/shims/file_system.cpp | 147 |
1 files changed, 140 insertions, 7 deletions
diff --git a/runtime/shims/file_system.cpp b/runtime/shims/file_system.cpp index dad6f37a8..8a20b21e9 100644 --- a/runtime/shims/file_system.cpp +++ b/runtime/shims/file_system.cpp | |||
| @@ -6,7 +6,8 @@ | |||
| 6 | // here (docs/runtime_imports.md, "Console paths and the write overlay"; the prefixes come from ivall/xcl, | 6 | // here (docs/runtime_imports.md, "Console paths and the write overlay"; the prefixes come from ivall/xcl, |
| 7 | // GPL-2.0-only, as facts). The rules: | 7 | // GPL-2.0-only, as facts). The rules: |
| 8 | // - "GAME:" (any case, "\\" or "/") maps under --game-dir, "G:\\" is the same folder and "T:\\" a scratch folder in | 8 | // - "GAME:" (any case, "\\" or "/") maps under --game-dir, "G:\\" is the same folder and "T:\\" a scratch folder in |
| 9 | // the overlay (RT-218); any other path is a host path and goes to the host; | 9 | // the overlay (RT-218), <overlay>\\.gta6rt-temp\\<run>, a new one reserved by every process; any other path is a |
| 10 | // host path and goes to the host; | ||
| 10 | // - an open that can write, and DeleteFile, go to the overlay (--overlay, default <game-dir>\\overlay), so the | 11 | // - an open that can write, and DeleteFile, go to the overlay (--overlay, default <game-dir>\\overlay), so the |
| 11 | // package stays exactly as installed. A write-open of a package file copies it into the overlay first, so | 12 | // package stays exactly as installed. A write-open of a package file copies it into the overlay first, so |
| 12 | // OPEN_EXISTING keeps its bytes; a delete of a package file leaves an empty marker under | 13 | // OPEN_EXISTING keeps its bytes; a delete of a package file leaves an empty marker under |
| @@ -26,6 +27,7 @@ | |||
| 26 | #include <intrin.h> | 27 | #include <intrin.h> |
| 27 | 28 | ||
| 28 | #include <atomic> | 29 | #include <atomic> |
| 30 | #include <cstdio> | ||
| 29 | #include <cstring> | 31 | #include <cstring> |
| 30 | #include <cwchar> | 32 | #include <cwchar> |
| 31 | #include <cwctype> | 33 | #include <cwctype> |
| @@ -49,7 +51,7 @@ namespace { | |||
| 49 | constexpr wchar_t kGamePrefix[] = L"game:"; | 51 | constexpr wchar_t kGamePrefix[] = L"game:"; |
| 50 | constexpr size_t kGamePrefixLength = 5; | 52 | constexpr size_t kGamePrefixLength = 5; |
| 51 | constexpr wchar_t kDeletedDir[] = L".gta6rt-deleted"; | 53 | constexpr wchar_t kDeletedDir[] = L".gta6rt-deleted"; |
| 52 | constexpr wchar_t kScratchDir[] = L".gta6rt-temp"; // what "T:\" names, inside the overlay | 54 | constexpr wchar_t kScratchDir[] = L".gta6rt-temp"; // holds one folder per process; "T:\" names this process's |
| 53 | // A removed package folder is marked by this file inside its marker folder: the marker folder itself can already | 55 | // A removed package folder is marked by this file inside its marker folder: the marker folder itself can already |
| 54 | // exist because files below it were deleted, and that alone does not hide the folder. | 56 | // exist because files below it were deleted, and that alone does not hide the folder. |
| 55 | constexpr wchar_t kRemovedDirMarker[] = L".gta6rt-dir"; | 57 | constexpr wchar_t kRemovedDirMarker[] = L".gta6rt-dir"; |
| @@ -167,9 +169,68 @@ std::filesystem::path win32_find_path(const std::filesystem::path& path, const s | |||
| 167 | // other console layers map those two drives (RT-218). Only the executable can say which spelling the title uses, so | 169 | // other console layers map those two drives (RT-218). Only the executable can say which spelling the title uses, so |
| 168 | // a host drive of the same letter must keep working: a path under --game-dir or the overlay (a package on drive G:, | 170 | // a host drive of the same letter must keep working: a path under --game-dir or the overlay (a package on drive G:, |
| 169 | // or a path GetModuleFileName handed back) stays a host path, and so does every G:/T: path while no roots are | 171 | // or a path GetModuleFileName handed back) stays a host path, and so does every G:/T: path while no roots are |
| 170 | // configured. The scratch folder lives in the overlay, as `.gta6rt-temp`. | 172 | // configured. The scratch folder lives in the overlay, as `.gta6rt-temp\<run>`, one per process (scratch_folder()). |
| 171 | enum class Drive { None, Game, Scratch }; | 173 | enum class Drive { None, Game, Scratch }; |
| 172 | 174 | ||
| 175 | // The stem of this process's T: folder below `.gta6rt-temp`: the process's start time (UTC, to the millisecond) and | ||
| 176 | // its id, e.g. "20261123-181502-123-pid4242". On the console T: is the title's own temporary drive, empty at launch | ||
| 177 | // and shared with nothing; one folder that every run shared kept the files earlier runs left, so a title that makes a | ||
| 178 | // file there with CREATE_NEW (its crash dump and crash log, measured under Wine) got ERROR_FILE_EXISTS. The stem only | ||
| 179 | // makes the folder easy to find: a reused process id in the same millisecond gives the same stem, so the folder is | ||
| 180 | // reserved by reserve_unique_dir, which never hands out one that already existed. | ||
| 181 | const std::wstring& scratch_run() { | ||
| 182 | static const std::wstring name = [] { | ||
| 183 | const DWORD last_error = ::GetLastError(); | ||
| 184 | FILETIME created{}, exited{}, kernel{}, user{}; | ||
| 185 | SYSTEMTIME t{}; | ||
| 186 | if (!::GetProcessTimes(::GetCurrentProcess(), &created, &exited, &kernel, &user) || | ||
| 187 | !::FileTimeToSystemTime(&created, &t)) | ||
| 188 | ::GetSystemTime(&t); | ||
| 189 | wchar_t buffer[64]; | ||
| 190 | const unsigned long pid = ::GetCurrentProcessId(); | ||
| 191 | std::swprintf(buffer, sizeof(buffer) / sizeof(buffer[0]), L"%04u%02u%02u-%02u%02u%02u-%03u-pid%lu", | ||
| 192 | unsigned{t.wYear}, unsigned{t.wMonth}, unsigned{t.wDay}, unsigned{t.wHour}, unsigned{t.wMinute}, | ||
| 193 | unsigned{t.wSecond}, unsigned{t.wMilliseconds}, pid); | ||
| 194 | ::SetLastError(last_error); | ||
| 195 | return std::wstring(buffer); | ||
| 196 | }(); | ||
| 197 | return name; | ||
| 198 | } | ||
| 199 | |||
| 200 | // The T: folder this process reserved in each overlay it was given (configure() may name several, as the test does), | ||
| 201 | // or the error that stopped the reservation. Decided once per overlay, under the mutex, so concurrent first T: calls | ||
| 202 | // get the same folder. | ||
| 203 | struct ScratchFolder { | ||
| 204 | std::wstring name; // empty: the reservation failed with `error` | ||
| 205 | DWORD error = 0; | ||
| 206 | }; | ||
| 207 | std::mutex g_scratch_mutex; | ||
| 208 | std::map<std::wstring, ScratchFolder> g_scratch_folders; // keyed by the overlay path, lowercased | ||
| 209 | |||
| 210 | // The name of this process's folder below <overlay>\.gta6rt-temp, reserved on the first call for that overlay; empty | ||
| 211 | // when it could not be reserved (logged once). With no overlay configured nothing is made and the stem is the name. | ||
| 212 | std::wstring scratch_folder(const std::filesystem::path& overlay) { | ||
| 213 | if (overlay.empty()) return scratch_run(); | ||
| 214 | std::lock_guard lock(g_scratch_mutex); | ||
| 215 | ScratchFolder& folder = g_scratch_folders[lower(overlay.native())]; | ||
| 216 | if (folder.name.empty() && folder.error == 0) { | ||
| 217 | const DWORD last_error = ::GetLastError(); | ||
| 218 | DWORD error = 0; | ||
| 219 | const std::filesystem::path reserved = reserve_unique_dir(overlay / kScratchDir, scratch_run(), error); | ||
| 220 | if (reserved.empty()) { | ||
| 221 | folder.error = error ? error : ERROR_CANNOT_MAKE; | ||
| 222 | std::fprintf(stderr, | ||
| 223 | "[rt] file_system: no T: folder could be reserved below %ls (Win32 error %lu); T: paths " | ||
| 224 | "fail with ERROR_INVALID_NAME\n", | ||
| 225 | (overlay / kScratchDir).c_str(), static_cast<unsigned long>(folder.error)); | ||
| 226 | } else { | ||
| 227 | folder.name = reserved.filename().native(); | ||
| 228 | } | ||
| 229 | ::SetLastError(last_error); | ||
| 230 | } | ||
| 231 | return folder.name; | ||
| 232 | } | ||
| 233 | |||
| 173 | Drive console_drive(const std::wstring& path, size_t& prefix_length) { | 234 | Drive console_drive(const std::wstring& path, size_t& prefix_length) { |
| 174 | if (path.size() >= kGamePrefixLength && lower(path.substr(0, kGamePrefixLength)) == kGamePrefix) { | 235 | if (path.size() >= kGamePrefixLength && lower(path.substr(0, kGamePrefixLength)) == kGamePrefix) { |
| 175 | prefix_length = kGamePrefixLength; | 236 | prefix_length = kGamePrefixLength; |
| @@ -191,11 +252,15 @@ Parsed parse_console(const std::wstring& path, std::vector<std::wstring>& parts) | |||
| 191 | if (drive == Drive::None) return Parsed::Host; | 252 | if (drive == Drive::None) return Parsed::Host; |
| 192 | parts.clear(); | 253 | parts.clear(); |
| 193 | if (drive == Drive::Scratch) { | 254 | if (drive == Drive::Scratch) { |
| 194 | parts.push_back(kScratchDir); | ||
| 195 | const std::filesystem::path overlay = roots().overlay; | 255 | const std::filesystem::path overlay = roots().overlay; |
| 256 | const std::wstring folder = scratch_folder(overlay); | ||
| 257 | if (folder.empty()) return Parsed::Refused; | ||
| 258 | parts.push_back(kScratchDir); | ||
| 259 | parts.push_back(folder); | ||
| 260 | // The folder is this process's from its reservation on; made again if something removed it since. | ||
| 196 | const DWORD last_error = ::GetLastError(); | 261 | const DWORD last_error = ::GetLastError(); |
| 197 | std::error_code ec; | 262 | std::error_code ec; |
| 198 | if (!overlay.empty()) std::filesystem::create_directories(win32_io_path(overlay / kScratchDir), ec); | 263 | if (!overlay.empty()) std::filesystem::create_directories(win32_io_path(overlay / kScratchDir / folder), ec); |
| 199 | ::SetLastError(last_error); | 264 | ::SetLastError(last_error); |
| 200 | } | 265 | } |
| 201 | const size_t floor = parts.size(); // ".." may not climb out of the drive's own root | 266 | const size_t floor = parts.size(); // ".." may not climb out of the drive's own root |
| @@ -227,7 +292,7 @@ Parsed parse_console(const std::wstring& path, std::vector<std::wstring>& parts) | |||
| 227 | if (!flush()) return Parsed::Refused; | 292 | if (!flush()) return Parsed::Refused; |
| 228 | // The overlay's own folders are not part of the title's GAME: tree: hidden from its listings (internal_dir) and | 293 | // The overlay's own folders are not part of the title's GAME: tree: hidden from its listings (internal_dir) and |
| 229 | // refused as an explicit path too, in every spelling the loop above normalises (case, slashes, "." and ".."). | 294 | // refused as an explicit path too, in every spelling the loop above normalises (case, slashes, "." and ".."). |
| 230 | // T: still names the scratch folder: its first part is pushed above, not parsed from the title's path. | 295 | // T: still names the scratch folder: its first two parts are pushed above, not parsed from the title's path. |
| 231 | if (drive == Drive::Game && !parts.empty() && internal_dir(parts.front().c_str())) return Parsed::Refused; | 296 | if (drive == Drive::Game && !parts.empty() && internal_dir(parts.front().c_str())) return Parsed::Refused; |
| 232 | return Parsed::Console; | 297 | return Parsed::Console; |
| 233 | } | 298 | } |
| @@ -244,12 +309,44 @@ std::filesystem::path disk_path(const std::filesystem::path& root, const std::ve | |||
| 244 | 309 | ||
| 245 | bool drive_root(const std::wstring& path, const std::vector<std::wstring>& parts) { | 310 | bool drive_root(const std::wstring& path, const std::vector<std::wstring>& parts) { |
| 246 | size_t prefix_length = 0; | 311 | size_t prefix_length = 0; |
| 247 | return parts.empty() || (parts.size() == 1 && console_drive(path, prefix_length) == Drive::Scratch); | 312 | return parts.empty() || (parts.size() == 2 && console_drive(path, prefix_length) == Drive::Scratch); |
| 313 | } | ||
| 314 | |||
| 315 | // A folder's entries change one step at a time, as on the host: RemoveDirectory holds the folder's lock from its | ||
| 316 | // emptiness check to its marker, and a GAME: write-open or CreateDirectory holds the lock of the folder it creates in | ||
| 317 | // from its "does the folder exist" check, through mirroring that folder into the overlay, to the host call that makes | ||
| 318 | // the entry. One of the two goes first: the create fails with ERROR_PATH_NOT_FOUND, or the remove with | ||
| 319 | // ERROR_DIR_NOT_EMPTY. Without it both succeeded and the removed folder was back, the new entry in it. The locks are | ||
| 320 | // striped by the folder's name, so calls in different folders rarely wait on each other; reads never take one. | ||
| 321 | // Namespace scope, not function-local statics: these run on the title's threads. Taken after roots(); nothing holding | ||
| 322 | // one takes another of them or calls back into the title (a copy-up's lock may be taken inside one, never around one). | ||
| 323 | constexpr size_t kFolderLocks = 64; | ||
| 324 | std::mutex g_folder_locks[kFolderLocks]; | ||
| 325 | |||
| 326 | std::mutex& folder_lock(const std::vector<std::wstring>& folder) { | ||
| 327 | // FNV-1a over the upper-cased components: names compare without case, so every spelling of a folder gets its lock. | ||
| 328 | uint64_t hash = 14695981039346656037ull; | ||
| 329 | const auto mix = [&hash](wchar_t c) { | ||
| 330 | hash ^= static_cast<uint64_t>(c); | ||
| 331 | hash *= 1099511628211ull; | ||
| 332 | }; | ||
| 333 | for (const auto& part : folder) { | ||
| 334 | std::wstring upper(part.size(), L'\0'); | ||
| 335 | if (part.empty() || LCMapStringEx(LOCALE_NAME_INVARIANT, LCMAP_UPPERCASE, part.data(), | ||
| 336 | static_cast<int>(part.size()), upper.data(), static_cast<int>(upper.size()), | ||
| 337 | nullptr, nullptr, 0) != static_cast<int>(part.size())) | ||
| 338 | upper = part; | ||
| 339 | for (const wchar_t c : upper) mix(c); | ||
| 340 | mix(L'\\'); | ||
| 341 | } | ||
| 342 | return g_folder_locks[hash % kFolderLocks]; | ||
| 248 | } | 343 | } |
| 249 | 344 | ||
| 250 | struct Target { | 345 | struct Target { |
| 251 | std::filesystem::path path; | 346 | std::filesystem::path path; |
| 252 | DWORD error = 0; // non-zero: the caller fails with this error and does not touch the host | 347 | DWORD error = 0; // non-zero: the caller fails with this error and does not touch the host |
| 348 | // A write-open's hold on the folder it opens in, until the Target goes after the host open (g_folder_locks). | ||
| 349 | std::unique_lock<std::mutex> folder; | ||
| 253 | }; | 350 | }; |
| 254 | 351 | ||
| 255 | Target fail(DWORD error) { | 352 | Target fail(DWORD error) { |
| @@ -438,10 +535,14 @@ Target write_target(const std::wstring& path, bool copy_up) { | |||
| 438 | // below a package file (which then read as a folder) and inside a folder the title had removed. The overlay may | 535 | // below a package file (which then read as a folder) and inside a folder the title had removed. The overlay may |
| 439 | // still lack a folder only the package has, so it is mirrored here, which the title cannot see. | 536 | // still lack a folder only the package has, so it is mirrored here, which the title cannot see. |
| 440 | // T:'s scratch folder is the exception: it creates its parent folders, as merged with RT-218. | 537 | // T:'s scratch folder is the exception: it creates its parent folders, as merged with RT-218. |
| 538 | // The folder's lock is held from this check to the caller's host open, so a RemoveDirectory of the folder comes | ||
| 539 | // wholly before or after (g_folder_locks). | ||
| 540 | std::unique_lock folder(folder_lock(parent_of(parts))); | ||
| 441 | const bool scratch = !parts.empty() && same_name(parts.front().c_str(), kScratchDir); | 541 | const bool scratch = !parts.empty() && same_name(parts.front().c_str(), kScratchDir); |
| 442 | if (!scratch && !is_folder(r, parent_of(parts))) return fail(ERROR_PATH_NOT_FOUND); | 542 | if (!scratch && !is_folder(r, parent_of(parts))) return fail(ERROR_PATH_NOT_FOUND); |
| 443 | 543 | ||
| 444 | Target t; | 544 | Target t; |
| 545 | t.folder = std::move(folder); | ||
| 445 | t.path = disk_path(r.overlay, parts); | 546 | t.path = disk_path(r.overlay, parts); |
| 446 | 547 | ||
| 447 | std::error_code ec; | 548 | std::error_code ec; |
| @@ -687,6 +788,34 @@ std::filesystem::path overlay_dir() { | |||
| 687 | return g_overlay_dir; | 788 | return g_overlay_dir; |
| 688 | } | 789 | } |
| 689 | 790 | ||
| 791 | std::filesystem::path scratch_dir() { | ||
| 792 | const std::filesystem::path overlay = overlay_dir(); | ||
| 793 | if (overlay.empty()) return {}; | ||
| 794 | const std::wstring folder = scratch_folder(overlay); | ||
| 795 | return folder.empty() ? std::filesystem::path() : overlay / kScratchDir / folder; | ||
| 796 | } | ||
| 797 | |||
| 798 | std::wstring scratch_stem() { return scratch_run(); } | ||
| 799 | |||
| 800 | std::filesystem::path reserve_unique_dir(const std::filesystem::path& base, const std::wstring& stem, DWORD& error, | ||
| 801 | unsigned max_tries) { | ||
| 802 | error = 0; | ||
| 803 | std::error_code ec; | ||
| 804 | std::filesystem::create_directories(win32_io_path(base), ec); | ||
| 805 | for (unsigned n = 1; n <= max_tries; ++n) { | ||
| 806 | const std::filesystem::path candidate = base / (n == 1 ? stem : stem + L"-" + std::to_wstring(n)); | ||
| 807 | // Create-only: success means no other process or run had this name, so the folder is new and ours. Through | ||
| 808 | // win32_io_path, as every other create here, so a long overlay root keeps working. | ||
| 809 | if (::CreateDirectoryW(win32_io_path(candidate).c_str(), nullptr)) { | ||
| 810 | error = 0; | ||
| 811 | return candidate; | ||
| 812 | } | ||
| 813 | error = ::GetLastError(); | ||
| 814 | if (error != ERROR_ALREADY_EXISTS) return {}; // another name will not get past this error | ||
| 815 | } | ||
| 816 | return {}; | ||
| 817 | } | ||
| 818 | |||
| 690 | // gta6rt --trace FILE: a failed open of a guest path goes to the startup trace as a "file_open_failed" event (the | 819 | // gta6rt --trace FILE: a failed open of a guest path goes to the startup trace as a "file_open_failed" event (the |
| 691 | // path the title passed, the Win32 error, the caller as module+RVA), so a boot that stops on a missing or refused | 820 | // path the title passed, the Win32 error, the caller as module+RVA), so a boot that stops on a missing or refused |
| 692 | // file names it. Failures only, and at most 256 of them, so a title probing many optional files can't fill the file. | 821 | // file names it. Failures only, and at most 256 of them, so a title probing many optional files can't fill the file. |
| @@ -1071,6 +1200,8 @@ BOOL create_directory(LPCWSTR template_name, LPCWSTR name, LPSECURITY_ATTRIBUTES | |||
| 1071 | const Roots r = roots(); | 1200 | const Roots r = roots(); |
| 1072 | if (r.game.empty() || r.overlay.empty()) return fail_bool(ERROR_PATH_NOT_FOUND); | 1201 | if (r.game.empty() || r.overlay.empty()) return fail_bool(ERROR_PATH_NOT_FOUND); |
| 1073 | if (view(r, parts).kind != Kind::None) return fail_bool(ERROR_ALREADY_EXISTS); // "GAME:" included | 1202 | if (view(r, parts).kind != Kind::None) return fail_bool(ERROR_ALREADY_EXISTS); // "GAME:" included |
| 1203 | // From the parent's check to the new folder: a RemoveDirectory of the parent comes wholly before or after. | ||
| 1204 | std::lock_guard folder(folder_lock(parent_of(parts))); | ||
| 1074 | if (!is_folder(r, parent_of(parts))) return fail_bool(ERROR_PATH_NOT_FOUND); | 1205 | if (!is_folder(r, parent_of(parts))) return fail_bool(ERROR_PATH_NOT_FOUND); |
| 1075 | 1206 | ||
| 1076 | const std::filesystem::path target = disk_path(r.overlay, parts); | 1207 | const std::filesystem::path target = disk_path(r.overlay, parts); |
| @@ -1124,6 +1255,8 @@ BOOL WINAPI remove_directory_w(LPCWSTR name) { | |||
| 1124 | const Roots r = roots(); | 1255 | const Roots r = roots(); |
| 1125 | if (r.game.empty() || r.overlay.empty()) return fail_bool(ERROR_PATH_NOT_FOUND); | 1256 | if (r.game.empty() || r.overlay.empty()) return fail_bool(ERROR_PATH_NOT_FOUND); |
| 1126 | if (drive_root(name, parts)) return fail_bool(ERROR_ACCESS_DENIED); // "GAME:" and "T:" themselves stay | 1257 | if (drive_root(name, parts)) return fail_bool(ERROR_ACCESS_DENIED); // "GAME:" and "T:" themselves stay |
| 1258 | // From the emptiness check to the marker: a create in the folder comes wholly before or after (g_folder_locks). | ||
| 1259 | std::lock_guard folder(folder_lock(parts)); | ||
| 1127 | const View v = view(r, parts); | 1260 | const View v = view(r, parts); |
| 1128 | if (v.kind == Kind::None) return fail_bool(missing_error(r, parts)); | 1261 | if (v.kind == Kind::None) return fail_bool(missing_error(r, parts)); |
| 1129 | if (v.kind == Kind::File) return fail_bool(ERROR_DIRECTORY); | 1262 | if (v.kind == Kind::File) return fail_bool(ERROR_DIRECTORY); |