@@ -312,9 +312,41 @@ bool drive_root(const std::wstring& path, const std::vector<std::wstring>& parts
312312 return parts.empty () || (parts.size () == 2 && console_drive (path, prefix_length) == Drive::Scratch);
313313}
314314
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 ];
343+ }
344+
315345struct Target {
316346 std::filesystem::path path;
317347 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;
318350};
319351
320352Target fail (DWORD error) {
@@ -503,10 +535,14 @@ Target write_target(const std::wstring& path, bool copy_up) {
503535 // below a package file (which then read as a folder) and inside a folder the title had removed. The overlay may
504536 // still lack a folder only the package has, so it is mirrored here, which the title cannot see.
505537 // 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)));
506541 const bool scratch = !parts.empty () && same_name (parts.front ().c_str (), kScratchDir );
507542 if (!scratch && !is_folder (r, parent_of (parts))) return fail (ERROR_PATH_NOT_FOUND );
508543
509544 Target t;
545+ t.folder = std::move (folder);
510546 t.path = disk_path (r.overlay , parts);
511547
512548 std::error_code ec;
@@ -1164,6 +1200,8 @@ BOOL create_directory(LPCWSTR template_name, LPCWSTR name, LPSECURITY_ATTRIBUTES
11641200 const Roots r = roots ();
11651201 if (r.game .empty () || r.overlay .empty ()) return fail_bool (ERROR_PATH_NOT_FOUND );
11661202 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)));
11671205 if (!is_folder (r, parent_of (parts))) return fail_bool (ERROR_PATH_NOT_FOUND );
11681206
11691207 const std::filesystem::path target = disk_path (r.overlay , parts);
@@ -1217,6 +1255,8 @@ BOOL WINAPI remove_directory_w(LPCWSTR name) {
12171255 const Roots r = roots ();
12181256 if (r.game .empty () || r.overlay .empty ()) return fail_bool (ERROR_PATH_NOT_FOUND );
12191257 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));
12201260 const View v = view (r, parts);
12211261 if (v.kind == Kind::None) return fail_bool (missing_error (r, parts));
12221262 if (v.kind == Kind::File) return fail_bool (ERROR_DIRECTORY );
0 commit comments