diff options
| author | airp1ane <279825357+airp1ane@users.noreply.github.com> | 2026-11-22 14:15:36 -0500 |
|---|---|---|
| committer | airp1ane <279825357+airp1ane@users.noreply.github.com> | 2026-11-22 14:15:36 -0500 |
| commit | fc1e0058e0e3ecdbac61fe89bc69e7c5ec15bc21 (patch) | |
| tree | 323bac2c0c358db892b55e3b776e28a68717bc8b /runtime/shims/file_system.cpp | |
| parent | 162821017ea3c286c8bec860f3ff94f037912061 (diff) | |
file_system: copy-up through a staging name, one at a time
A write-open of a package file copied it straight to its overlay name with
no lock. A process killed mid-copy left a truncated file there that every
later read preferred over the package; two threads write-opening the same
package file both copied, and the second copy_file failed on the taken name,
so that open failed.
write_target now copies into <overlay>\.gta6rt-copy under a private name and
renames it into place, under a mutex that only copy-ups take. Listings hide
the staging folder like .gta6rt-deleted. Without the mutex, copy-then-rename
alone still failed about 2 in 100 runs: a losing rename onto the existing
name briefly holds it, and the winner's open got ERROR_SHARING_VIOLATION.
file_system_test: two threads write-open one 256 KiB package file, 50 rounds;
both opens succeed and the overlay copy matches (fails 3 of 3 runs on the
old code, passes 200 of 200 with this); no staging file is left; a crash
leftover is hidden from a GAME: listing.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/shims/file_system.cpp')
| -rw-r--r-- | runtime/shims/file_system.cpp | 31 |
1 files changed, 28 insertions, 3 deletions
diff --git a/runtime/shims/file_system.cpp b/runtime/shims/file_system.cpp index 2e7d93718..f245f9a22 100644 --- a/runtime/shims/file_system.cpp +++ b/runtime/shims/file_system.cpp | |||
| @@ -45,6 +45,11 @@ constexpr wchar_t kDeletedDir[] = L".gta6rt-deleted"; | |||
| 45 | // A removed package folder is marked by this file inside its marker folder: the marker folder itself can already | 45 | // A removed package folder is marked by this file inside its marker folder: the marker folder itself can already |
| 46 | // exist because files below it were deleted, and that alone does not hide the folder. | 46 | // exist because files below it were deleted, and that alone does not hide the folder. |
| 47 | constexpr wchar_t kRemovedDirMarker[] = L".gta6rt-dir"; | 47 | constexpr wchar_t kRemovedDirMarker[] = L".gta6rt-dir"; |
| 48 | // Copy-up writes here first and then renames into place, so a half-copied file never sits under the title's name. | ||
| 49 | constexpr wchar_t kCopyDir[] = L".gta6rt-copy"; | ||
| 50 | |||
| 51 | // The overlay root's own folders, which a title never sees in a listing. | ||
| 52 | bool internal_dir(const wchar_t* name) { return _wcsicmp(name, kDeletedDir) == 0 || _wcsicmp(name, kCopyDir) == 0; } | ||
| 48 | 53 | ||
| 49 | std::mutex g_mutex; | 54 | std::mutex g_mutex; |
| 50 | std::filesystem::path g_game_dir; | 55 | std::filesystem::path g_game_dir; |
| @@ -234,7 +239,7 @@ bool empty_in_view(const Roots& r, const std::vector<std::wstring>& parts) { | |||
| 234 | const std::filesystem::path p = join(r.overlay, parts); | 239 | const std::filesystem::path p = join(r.overlay, parts); |
| 235 | for (auto it = std::filesystem::directory_iterator(p, ec); !ec && it != std::filesystem::directory_iterator(); | 240 | for (auto it = std::filesystem::directory_iterator(p, ec); !ec && it != std::filesystem::directory_iterator(); |
| 236 | it.increment(ec)) { | 241 | it.increment(ec)) { |
| 237 | if (parts.empty() && same_name(it->path().filename().c_str(), kDeletedDir)) continue; | 242 | if (parts.empty() && internal_dir(it->path().filename().c_str())) continue; |
| 238 | return false; | 243 | return false; |
| 239 | } | 244 | } |
| 240 | } | 245 | } |
| @@ -314,13 +319,33 @@ Target write_target(const std::wstring& path, bool copy_up) { | |||
| 314 | // the contents (OPEN_EXISTING, TRUNCATE_EXISTING) has to fail on it instead. CREATE_ALWAYS and CREATE_NEW still | 319 | // the contents (OPEN_EXISTING, TRUNCATE_EXISTING) has to fail on it instead. CREATE_ALWAYS and CREATE_NEW still |
| 315 | // make a new overlay file, which reads prefer over the package and so over the marker. | 320 | // make a new overlay file, which reads prefer over the package and so over the marker. |
| 316 | const std::filesystem::path package_path = join(r.game, parts); | 321 | const std::filesystem::path package_path = join(r.game, parts); |
| 322 | // One copy-up at a time: a second thread writing the same file waits here and then finds the first one's copy, so | ||
| 323 | // it neither fails on the taken name nor collides with that file's open through a rename of its own. | ||
| 324 | static std::mutex s_copy_mutex; | ||
| 325 | std::unique_lock copy_lock(s_copy_mutex, std::defer_lock); | ||
| 326 | if (copy_up) copy_lock.lock(); | ||
| 317 | if (copy_up && !deleted(r.overlay, parts) && !std::filesystem::exists(t.path, ec) && | 327 | if (copy_up && !deleted(r.overlay, parts) && !std::filesystem::exists(t.path, ec) && |
| 318 | std::filesystem::is_regular_file(package_path, ec)) { | 328 | std::filesystem::is_regular_file(package_path, ec)) { |
| 319 | std::filesystem::copy_file(package_path, t.path, std::filesystem::copy_options::none, ec); | 329 | // Copied to a private name, then renamed into place: a process killed mid-copy leaves only a hidden leftover, |
| 330 | // not a truncated file that reads would prefer over the package. | ||
| 331 | static unsigned s_copies = 0; // under s_copy_mutex | ||
| 332 | const std::filesystem::path staging = r.overlay / kCopyDir; | ||
| 333 | std::filesystem::create_directories(staging, ec); | ||
| 334 | const std::filesystem::path tmp = | ||
| 335 | staging / (std::to_wstring(::GetCurrentProcessId()) + L"-" + std::to_wstring(++s_copies)); | ||
| 336 | if (!ec) std::filesystem::copy_file(package_path, tmp, std::filesystem::copy_options::overwrite_existing, ec); | ||
| 320 | if (ec) { | 337 | if (ec) { |
| 321 | t.error = static_cast<DWORD>(ec.value()); | 338 | t.error = static_cast<DWORD>(ec.value()); |
| 339 | std::filesystem::remove(tmp, ec); | ||
| 322 | return t; | 340 | return t; |
| 323 | } | 341 | } |
| 342 | if (!::MoveFileExW(tmp.c_str(), t.path.c_str(), 0)) { | ||
| 343 | // The name taken means the title created the file itself meanwhile (CREATE_NEW and CREATE_ALWAYS don't | ||
| 344 | // copy up, so they don't take the lock); that file is what it wrote, so keep it. | ||
| 345 | const DWORD error = ::GetLastError(); | ||
| 346 | std::filesystem::remove(tmp, ec); | ||
| 347 | if (!std::filesystem::exists(t.path, ec)) t.error = error; | ||
| 348 | } | ||
| 324 | } | 349 | } |
| 325 | return t; | 350 | return t; |
| 326 | } | 351 | } |
| @@ -395,7 +420,7 @@ bool collect(const std::wstring& pattern, const std::vector<std::wstring>& parts | |||
| 395 | const HANDLE h = ::FindFirstFileW(search.c_str(), &found); | 420 | const HANDLE h = ::FindFirstFileW(search.c_str(), &found); |
| 396 | if (h == INVALID_HANDLE_VALUE) return; | 421 | if (h == INVALID_HANDLE_VALUE) return; |
| 397 | do { | 422 | do { |
| 398 | if (overlay_root && same_name(found.cFileName, kDeletedDir)) continue; | 423 | if (overlay_root && internal_dir(found.cFileName)) continue; |
| 399 | const bool dot = same_name(found.cFileName, L".") || same_name(found.cFileName, L".."); | 424 | const bool dot = same_name(found.cFileName, L".") || same_name(found.cFileName, L".."); |
| 400 | if (!overlay_root && !dot) { | 425 | if (!overlay_root && !dot) { |
| 401 | std::vector<std::wstring> entry_parts = dirs; | 426 | std::vector<std::wstring> entry_parts = dirs; |