upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/shims/file_system.cpp
diff options
context:
space:
mode:
authorJan Trencansky <jan.trencansky@protonmail.com>2026-11-24 07:02:11 -0500
committerJan Trencansky <jan.trencansky@protonmail.com>2026-11-24 07:05:40 -0500
commitde8cb53d5498ce914ba82bc02c6a3f39dc03f3bf (patch)
tree48e08d747d41b9f46e18325d3fe11f45de34f0af /runtime/shims/file_system.cpp
parent21625bca8c675c62f9dbffc318ce6a59d57faf31 (diff)
file_system: RemoveDirectory and a create inside the folder are ordered as on the host
Restores old upstream PR 9215 in ItsNotRexed/Leonida (closed unmerged when that repository went private), ported onto the current main: the write-open keeps main's long-path disk_path() for its overlay path, and the new race test sits after main's tests that recreate a removed package folder. A GAME: write-open checked that its folder exists, mirrored it into the overlay and then opened the file, and RemoveDirectory checked that the folder was empty and wrote its marker, neither under a lock. A remove that ran between the open's check and its host CreateFile succeeded on an empty folder, and the open then made the folder and the file again: both calls succeeded and the removed folder was back. CreateDirectory of a folder inside it had the same gap. On Windows one of the two fails: the create with ERROR_PATH_NOT_FOUND or the remove with ERROR_DIR_NOT_EMPTY. A striped lock keyed by the folder's upper-cased name (64 namespace-scope std::mutex, no function-local static): write_target holds the lock of the folder it opens in from the parent check until the Target is gone, after the host CreateFile/CreateFile2; CreateDirectory holds the parent's from its check to the host call; RemoveDirectory holds the folder's from its view to the marker. Reads take no lock. file_system_test races the two calls on two threads, 2,000 rounds each for a creating CreateFileW (CREATE_NEW, CREATE_ALWAYS, OPEN_ALWAYS) and for CreateDirectoryW, and checks that they never both succeed, that the loser fails with the host's error and that a removed folder stays removed. 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.cpp40
1 files changed, 40 insertions, 0 deletions
diff --git a/runtime/shims/file_system.cpp b/runtime/shims/file_system.cpp
index 75f5e9810..bafd06b71 100644
--- a/runtime/shims/file_system.cpp
+++ b/runtime/shims/file_system.cpp
@@ -246,9 +246,41 @@ bool drive_root(const std::wstring& path, const std::vector<std::wstring>& parts
246 return parts.empty() || (parts.size() == 1 && console_drive(path, prefix_length) == Drive::Scratch); 246 return parts.empty() || (parts.size() == 1 && console_drive(path, prefix_length) == Drive::Scratch);
247} 247}
248 248
249// A folder's entries change one step at a time, as on the host: RemoveDirectory holds the folder's lock from its
250// emptiness check to its marker, and a GAME: write-open or CreateDirectory holds the lock of the folder it creates in
251// from its "does the folder exist" check, through mirroring that folder into the overlay, to the host call that makes
252// the entry. One of the two goes first: the create fails with ERROR_PATH_NOT_FOUND, or the remove with
253// ERROR_DIR_NOT_EMPTY. Without it both succeeded and the removed folder was back, the new entry in it. The locks are
254// striped by the folder's name, so calls in different folders rarely wait on each other; reads never take one.
255// Namespace scope, not function-local statics: these run on the title's threads. Taken after roots(); nothing holding
256// one takes another of them or calls back into the title (a copy-up's lock may be taken inside one, never around one).
257constexpr size_t kFolderLocks = 64;
258std::mutex g_folder_locks[kFolderLocks];
259
260std::mutex& folder_lock(const std::vector<std::wstring>& folder) {
261 // FNV-1a over the upper-cased components: names compare without case, so every spelling of a folder gets its lock.
262 uint64_t hash = 14695981039346656037ull;
263 const auto mix = [&hash](wchar_t c) {
264 hash ^= static_cast<uint64_t>(c);
265 hash *= 1099511628211ull;
266 };
267 for (const auto& part : folder) {
268 std::wstring upper(part.size(), L'\0');
269 if (part.empty() || LCMapStringEx(LOCALE_NAME_INVARIANT, LCMAP_UPPERCASE, part.data(),
270 static_cast<int>(part.size()), upper.data(), static_cast<int>(upper.size()),
271 nullptr, nullptr, 0) != static_cast<int>(part.size()))
272 upper = part;
273 for (const wchar_t c : upper) mix(c);
274 mix(L'\\');
275 }
276 return g_folder_locks[hash % kFolderLocks];
277}
278
249struct Target { 279struct Target {
250 std::filesystem::path path; 280 std::filesystem::path path;
251 DWORD error = 0; // non-zero: the caller fails with this error and does not touch the host 281 DWORD error = 0; // non-zero: the caller fails with this error and does not touch the host
282 // A write-open's hold on the folder it opens in, until the Target goes after the host open (g_folder_locks).
283 std::unique_lock<std::mutex> folder;
252}; 284};
253 285
254Target fail(DWORD error) { 286Target fail(DWORD error) {
@@ -433,10 +465,14 @@ Target write_target(const std::wstring& path, bool copy_up) {
433 // below a package file (which then read as a folder) and inside a folder the title had removed. The overlay may 465 // below a package file (which then read as a folder) and inside a folder the title had removed. The overlay may
434 // still lack a folder only the package has, so it is mirrored here, which the title cannot see. 466 // still lack a folder only the package has, so it is mirrored here, which the title cannot see.
435 // T:'s scratch folder is the exception: it creates its parent folders, as merged with RT-218. 467 // T:'s scratch folder is the exception: it creates its parent folders, as merged with RT-218.
468 // The folder's lock is held from this check to the caller's host open, so a RemoveDirectory of the folder comes
469 // wholly before or after (g_folder_locks).
470 std::unique_lock folder(folder_lock(parent_of(parts)));
436 const bool scratch = !parts.empty() && same_name(parts.front().c_str(), kScratchDir); 471 const bool scratch = !parts.empty() && same_name(parts.front().c_str(), kScratchDir);
437 if (!scratch && !is_folder(r, parent_of(parts))) return fail(ERROR_PATH_NOT_FOUND); 472 if (!scratch && !is_folder(r, parent_of(parts))) return fail(ERROR_PATH_NOT_FOUND);
438 473
439 Target t; 474 Target t;
475 t.folder = std::move(folder);
440 t.path = disk_path(r.overlay, parts); 476 t.path = disk_path(r.overlay, parts);
441 477
442 std::error_code ec; 478 std::error_code ec;
@@ -1062,6 +1098,8 @@ BOOL create_directory(LPCWSTR template_name, LPCWSTR name, LPSECURITY_ATTRIBUTES
1062 const Roots r = roots(); 1098 const Roots r = roots();
1063 if (r.game.empty() || r.overlay.empty()) return fail_bool(ERROR_PATH_NOT_FOUND); 1099 if (r.game.empty() || r.overlay.empty()) return fail_bool(ERROR_PATH_NOT_FOUND);
1064 if (view(r, parts).kind != Kind::None) return fail_bool(ERROR_ALREADY_EXISTS); // "GAME:" included 1100 if (view(r, parts).kind != Kind::None) return fail_bool(ERROR_ALREADY_EXISTS); // "GAME:" included
1101 // From the parent's check to the new folder: a RemoveDirectory of the parent comes wholly before or after.
1102 std::lock_guard folder(folder_lock(parent_of(parts)));
1065 if (!is_folder(r, parent_of(parts))) return fail_bool(ERROR_PATH_NOT_FOUND); 1103 if (!is_folder(r, parent_of(parts))) return fail_bool(ERROR_PATH_NOT_FOUND);
1066 1104
1067 const std::filesystem::path target = disk_path(r.overlay, parts); 1105 const std::filesystem::path target = disk_path(r.overlay, parts);
@@ -1115,6 +1153,8 @@ BOOL WINAPI remove_directory_w(LPCWSTR name) {
1115 const Roots r = roots(); 1153 const Roots r = roots();
1116 if (r.game.empty() || r.overlay.empty()) return fail_bool(ERROR_PATH_NOT_FOUND); 1154 if (r.game.empty() || r.overlay.empty()) return fail_bool(ERROR_PATH_NOT_FOUND);
1117 if (drive_root(name, parts)) return fail_bool(ERROR_ACCESS_DENIED); // "GAME:" and "T:" themselves stay 1155 if (drive_root(name, parts)) return fail_bool(ERROR_ACCESS_DENIED); // "GAME:" and "T:" themselves stay
1156 // From the emptiness check to the marker: a create in the folder comes wholly before or after (g_folder_locks).
1157 std::lock_guard folder(folder_lock(parts));
1118 const View v = view(r, parts); 1158 const View v = view(r, parts);
1119 if (v.kind == Kind::None) return fail_bool(ERROR_FILE_NOT_FOUND); 1159 if (v.kind == Kind::None) return fail_bool(ERROR_FILE_NOT_FOUND);
1120 if (v.kind == Kind::File) return fail_bool(ERROR_DIRECTORY); 1160 if (v.kind == Kind::File) return fail_bool(ERROR_DIRECTORY);