diff options
Diffstat (limited to 'runtime/shims/file_system.cpp')
| -rw-r--r-- | runtime/shims/file_system.cpp | 86 |
1 files changed, 74 insertions, 12 deletions
diff --git a/runtime/shims/file_system.cpp b/runtime/shims/file_system.cpp index 3c5417101..8acacb10f 100644 --- a/runtime/shims/file_system.cpp +++ b/runtime/shims/file_system.cpp | |||
| @@ -32,7 +32,9 @@ | |||
| 32 | #include <map> | 32 | #include <map> |
| 33 | #include <memory> | 33 | #include <memory> |
| 34 | #include <mutex> | 34 | #include <mutex> |
| 35 | #include <set> | ||
| 35 | #include <string> | 36 | #include <string> |
| 37 | #include <unordered_set> | ||
| 36 | #include <vector> | 38 | #include <vector> |
| 37 | 39 | ||
| 38 | #include "file_system.h" | 40 | #include "file_system.h" |
| @@ -482,6 +484,76 @@ bool collect(const std::wstring& pattern, const std::vector<std::wstring>& parts | |||
| 482 | return false; | 484 | return false; |
| 483 | } | 485 | } |
| 484 | 486 | ||
| 487 | // Every deletion marker for an entry of this folder lives inside its marker folder, so that folder is read once | ||
| 488 | // instead of probing it twice per package entry (deleted()). An entry is deleted when its marker is a file, or a | ||
| 489 | // folder holding kRemovedDirMarker: the rule deleted() applies. The lookup folds ASCII letters only, which matches | ||
| 490 | // the file system's case-insensitive compare exactly when both names are ASCII. Any other name, a non-ASCII entry | ||
| 491 | // or a marker that is not plain ASCII or is a reparse point, goes through deleted() and the file system's rule. | ||
| 492 | auto ascii = [](const wchar_t* s) { | ||
| 493 | for (; *s; ++s) | ||
| 494 | if (*s > 0x7f) return false; | ||
| 495 | return true; | ||
| 496 | }; | ||
| 497 | auto ascii_key = [](const wchar_t* s) { | ||
| 498 | std::wstring k = s; | ||
| 499 | for (auto& c : k) | ||
| 500 | if (c >= L'A' && c <= L'Z') c = static_cast<wchar_t>(c - L'A' + L'a'); | ||
| 501 | return k; | ||
| 502 | }; | ||
| 503 | bool has_markers = false; | ||
| 504 | bool markers_exact = true; | ||
| 505 | std::unordered_set<std::wstring> deleted_here; | ||
| 506 | if (!r.overlay.empty()) { | ||
| 507 | const std::filesystem::path marks = marker(r.overlay, dirs); | ||
| 508 | WIN32_FIND_DATAW m{}; | ||
| 509 | const HANDLE mh = ::FindFirstFileW((marks / L"*").c_str(), &m); | ||
| 510 | if (mh != INVALID_HANDLE_VALUE) { | ||
| 511 | has_markers = true; | ||
| 512 | do { | ||
| 513 | if (same_name(m.cFileName, L".") || same_name(m.cFileName, L"..")) continue; | ||
| 514 | if (!ascii(m.cFileName) || (m.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)) { | ||
| 515 | markers_exact = false; | ||
| 516 | continue; | ||
| 517 | } | ||
| 518 | if (!(m.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { | ||
| 519 | deleted_here.insert(ascii_key(m.cFileName)); | ||
| 520 | continue; | ||
| 521 | } | ||
| 522 | // A marker folder is a removed folder only with kRemovedDirMarker in it; without one it just holds the | ||
| 523 | // markers of a nested delete. A missing sentinel is that ordinary answer (status() reports not_found | ||
| 524 | // with an error code set); any other error leaves the decision to deleted() per entry. | ||
| 525 | std::error_code ec; | ||
| 526 | const auto st = std::filesystem::status(marks / m.cFileName / kRemovedDirMarker, ec); | ||
| 527 | if (st.type() == std::filesystem::file_type::regular) deleted_here.insert(ascii_key(m.cFileName)); | ||
| 528 | else if (st.type() != std::filesystem::file_type::not_found && ec) markers_exact = false; | ||
| 529 | } while (::FindNextFileW(mh, &m)); | ||
| 530 | if (::GetLastError() != ERROR_NO_MORE_FILES) markers_exact = false; // an incomplete scan | ||
| 531 | ::FindClose(mh); | ||
| 532 | } else { | ||
| 533 | // Only a missing marker folder means "nothing deleted here". Any other failure (a folder that cannot be | ||
| 534 | // listed although its markers can be queried by path, for one) leaves the decision to deleted(). | ||
| 535 | const DWORD e = ::GetLastError(); | ||
| 536 | if (e != ERROR_FILE_NOT_FOUND && e != ERROR_PATH_NOT_FOUND) { | ||
| 537 | has_markers = true; | ||
| 538 | markers_exact = false; | ||
| 539 | } | ||
| 540 | } | ||
| 541 | } | ||
| 542 | auto entry_deleted = [&](const wchar_t* name) { | ||
| 543 | if (!has_markers) return false; | ||
| 544 | if (markers_exact && ascii(name)) return deleted_here.count(ascii_key(name)) != 0; | ||
| 545 | std::vector<std::wstring> entry_parts = dirs; | ||
| 546 | entry_parts.push_back(name); | ||
| 547 | return deleted(r.overlay, entry_parts); | ||
| 548 | }; | ||
| 549 | // Names already listed, ordered by _wcsicmp itself, so two names are one entry exactly when same_name says so, in | ||
| 550 | // whatever locale the title set (a lower-cased key is not: under .UTF-8 _wcslwr_s folds É and é, _wcsicmp not). | ||
| 551 | struct NoCase { | ||
| 552 | bool operator()(const std::wstring& a, const std::wstring& b) const { return _wcsicmp(a.c_str(), b.c_str()) < 0; } | ||
| 553 | }; | ||
| 554 | std::set<std::wstring, NoCase> seen; | ||
| 555 | for (const auto& e : out) seen.insert(e.cFileName); | ||
| 556 | |||
| 485 | auto add_root = [&](const std::filesystem::path& root, bool overlay_root) { | 557 | auto add_root = [&](const std::filesystem::path& root, bool overlay_root) { |
| 486 | if (root.empty()) return; | 558 | if (root.empty()) return; |
| 487 | std::filesystem::path search = join(root, dirs); | 559 | std::filesystem::path search = join(root, dirs); |
| @@ -492,18 +564,8 @@ bool collect(const std::wstring& pattern, const std::vector<std::wstring>& parts | |||
| 492 | do { | 564 | do { |
| 493 | if (overlay_root && internal_dir(found.cFileName)) continue; | 565 | if (overlay_root && internal_dir(found.cFileName)) continue; |
| 494 | const bool dot = same_name(found.cFileName, L".") || same_name(found.cFileName, L".."); | 566 | const bool dot = same_name(found.cFileName, L".") || same_name(found.cFileName, L".."); |
| 495 | if (!overlay_root && !dot) { | 567 | if (!overlay_root && !dot && entry_deleted(found.cFileName)) continue; |
| 496 | std::vector<std::wstring> entry_parts = dirs; | 568 | if (seen.insert(found.cFileName).second) out.push_back(found); |
| 497 | entry_parts.push_back(found.cFileName); | ||
| 498 | if (deleted(r.overlay, entry_parts)) continue; | ||
| 499 | } | ||
| 500 | bool duplicate = false; | ||
| 501 | for (const auto& e : out) | ||
| 502 | if (same_name(e.cFileName, found.cFileName)) { | ||
| 503 | duplicate = true; | ||
| 504 | break; | ||
| 505 | } | ||
| 506 | if (!duplicate) out.push_back(found); | ||
| 507 | } while (::FindNextFileW(h, &found)); | 569 | } while (::FindNextFileW(h, &found)); |
| 508 | ::FindClose(h); | 570 | ::FindClose(h); |
| 509 | }; | 571 | }; |