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:
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 b3cc6b219..8a20b21e9 100644
--- a/runtime/shims/file_system.cpp
+++ b/runtime/shims/file_system.cpp
@@ -312,9 +312,41 @@ bool drive_root(const std::wstring& path, const std::vector<std::wstring>& parts
312 return parts.empty() || (parts.size() == 2 && console_drive(path, prefix_length) == Drive::Scratch); 312 return parts.empty() || (parts.size() == 2 && console_drive(path, prefix_length) == Drive::Scratch);
313} 313}
314 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).
323constexpr size_t kFolderLocks = 64;
324std::mutex g_folder_locks[kFolderLocks];
325
326std::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
315struct Target { 345struct Target {
316 std::filesystem::path path; 346 std::filesystem::path path;
317 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;
318}; 350};
319 351
320Target fail(DWORD error) { 352Target fail(DWORD error) {
@@ -503,10 +535,14 @@ Target write_target(const std::wstring& path, bool copy_up) {
503 // 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
504 // 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.
505 // 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)));
506 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);
507 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);
508 543
509 Target t; 544 Target t;
545 t.folder = std::move(folder);
510 t.path = disk_path(r.overlay, parts); 546 t.path = disk_path(r.overlay, parts);
511 547
512 std::error_code ec; 548 std::error_code ec;
@@ -1164,6 +1200,8 @@ BOOL create_directory(LPCWSTR template_name, LPCWSTR name, LPSECURITY_ATTRIBUTES
1164 const Roots r = roots(); 1200 const Roots r = roots();
1165 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);
1166 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)));
1167 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);
1168 1206
1169 const std::filesystem::path target = disk_path(r.overlay, parts); 1207 const std::filesystem::path target = disk_path(r.overlay, parts);
@@ -1217,6 +1255,8 @@ BOOL WINAPI remove_directory_w(LPCWSTR name) {
1217 const Roots r = roots(); 1255 const Roots r = roots();
1218 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);
1219 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));
1220 const View v = view(r, parts); 1260 const View v = view(r, parts);
1221 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));
1222 if (v.kind == Kind::File) return fail_bool(ERROR_DIRECTORY); 1262 if (v.kind == Kind::File) return fail_bool(ERROR_DIRECTORY);