From a2c7dde909f2d7afa5b08e35b918f4a9e241e91b Mon Sep 17 00:00:00 2001 From: Ceiridge Date: Sun, 1 Jan 2023 23:24:11 +0100 Subject: [PATCH] Fix ReadDirectory marker bug --- WinFsp-MemFs-Extended/basic.cpp | 4 ---- WinFsp-MemFs-Extended/comparisons.cpp | 9 +++++---- WinFsp-MemFs-Extended/dirinfo.cpp | 11 ++++------- WinFsp-MemFs-Extended/filemap.cpp | 15 ++++++--------- WinFsp-MemFs-Extended/memfs.h | 4 +--- WinFsp-MemFs-Extended/nodes.cpp | 2 +- 6 files changed, 17 insertions(+), 28 deletions(-) diff --git a/WinFsp-MemFs-Extended/basic.cpp b/WinFsp-MemFs-Extended/basic.cpp index a4033e1..eaa1403 100644 --- a/WinFsp-MemFs-Extended/basic.cpp +++ b/WinFsp-MemFs-Extended/basic.cpp @@ -8,10 +8,6 @@ FSP_FILE_SYSTEM* MemFs::GetRawFileSystem() const { return this->fileSystem.get(); } -FileNodeMap& MemFs::GetRawFileMap() { - return this->fileMap; -} - std::wstring& MemFs::GetVolumeLabel() { return this->volumeLabel; } diff --git a/WinFsp-MemFs-Extended/comparisons.cpp b/WinFsp-MemFs-Extended/comparisons.cpp index 58b2fee..84b9679 100644 --- a/WinFsp-MemFs-Extended/comparisons.cpp +++ b/WinFsp-MemFs-Extended/comparisons.cpp @@ -30,7 +30,9 @@ namespace Memfs::Utils { int v = 0; for (const void* e = t + n; e > (const void*)t; ++s, ++t) { - unsigned sc = *s, tc = *t; + const unsigned sc = *s; + const unsigned tc = *t; + if (0xffffff80 & (sc | tc)) { v = CompareStringW(LOCALE_INVARIANT, NORM_IGNORECASE, s0, n, t0, n); @@ -83,7 +85,7 @@ namespace Memfs::Utils { len = plen < qlen ? plen : qlen; - if (caseInsensitive) { + if (caseInsensitive) [[likely]] { res = EfficientWcsnicmp(partp, partq, len); } else res = wcsncmp(partp, partq, len); @@ -110,9 +112,8 @@ namespace Memfs::Utils { int EaNameCompare(const PCSTR a, const PCSTR b) { /* EA names are always case-insensitive in MEMFS (to be inline with NTFS) */ - int res; + int res = CompareStringA(LOCALE_INVARIANT, NORM_IGNORECASE, a, -1, b, -1); - res = CompareStringA(LOCALE_INVARIANT, NORM_IGNORECASE, a, -1, b, -1); if (0 != res) res -= 2; else diff --git a/WinFsp-MemFs-Extended/dirinfo.cpp b/WinFsp-MemFs-Extended/dirinfo.cpp index 05680be..741f6d8 100644 --- a/WinFsp-MemFs-Extended/dirinfo.cpp +++ b/WinFsp-MemFs-Extended/dirinfo.cpp @@ -3,8 +3,8 @@ namespace Memfs::Interface { NTSTATUS ReadDirectory(FSP_FILE_SYSTEM* fileSystem, - PVOID fileNode0, PWSTR pattern, PWSTR marker, - PVOID buffer, ULONG length, PULONG pBytesTransferred) { + PVOID fileNode0, PWSTR pattern, PWSTR marker, + PVOID buffer, ULONG length, PULONG pBytesTransferred) { assert(nullptr == pattern); MemFs* memfs = GetMemFs(fileSystem); @@ -33,10 +33,7 @@ namespace Memfs::Interface { } } - const std::wstring_view markerView = marker ? marker : L""; - const std::refoptional markerOpt = (marker == nullptr) ? std::nullopt : std::make_optional(markerView); - - for (const auto& child : memfs->EnumerateDirChildren(*fileNode, markerOpt)) { + for (const auto& child : memfs->EnumerateDirChildren(*fileNode, marker)) { if (!CompatAddDirInfo(child, nullptr, buffer, length, pBytesTransferred)) { return STATUS_SUCCESS; // Without end } @@ -47,7 +44,7 @@ namespace Memfs::Interface { } NTSTATUS GetDirInfoByName(FSP_FILE_SYSTEM* fileSystem, - PVOID parentNode0, PWSTR fileName, FSP_FSCTL_DIR_INFO* dirInfo) { + PVOID parentNode0, PWSTR fileName, FSP_FSCTL_DIR_INFO* dirInfo) { MemFs* memfs = GetMemFs(fileSystem); FileNode* parentNode = GetFileNode(parentNode0); diff --git a/WinFsp-MemFs-Extended/filemap.cpp b/WinFsp-MemFs-Extended/filemap.cpp index 28fc265..6d17532 100644 --- a/WinFsp-MemFs-Extended/filemap.cpp +++ b/WinFsp-MemFs-Extended/filemap.cpp @@ -81,8 +81,7 @@ bool MemFs::HasChild(const FileNode& node) { std::pair MemFs::InsertNode(FileNode* node) { try { - std::wstring fileName = node->fileName; - const auto [iter, success] = this->fileMap.insert(FileNodeMap::value_type(fileName, node)); + const auto [iter, success] = this->fileMap.emplace(node->fileName, node); if (success) { iter->second->Reference(); @@ -102,13 +101,10 @@ std::pair MemFs::InsertNode(FileNode&& node) { } void MemFs::RemoveNode(FileNode& node, const bool reportDeletedSize) { - const auto iter = this->fileMap.find(node.fileName); - if (iter == this->fileMap.end()) { + if (!this->fileMap.erase(node.fileName)) { return; } - this->fileMap.erase(iter); - // memefs: Quickly report counter about deleted sectors if (reportDeletedSize) { // TODO: toBeDeleted stuff @@ -152,12 +148,13 @@ std::vector MemFs::EnumerateDescendants(const FileNode& node, const b return descendants; } -std::vector MemFs::EnumerateDirChildren(const FileNode& node, const std::refoptional marker) { +std::vector MemFs::EnumerateDirChildren(const FileNode& node, const wchar_t* marker) { std::vector children; FileNodeMap::iterator iter; - if (marker.has_value()) { - iter = this->fileMap.upper_bound(node.fileName + L"\\" + std::wstring(marker.value())); + if (marker) { + const bool needsSlash = node.fileName.length() != 1 || node.fileName[0] != L'\\'; + iter = this->fileMap.upper_bound(node.fileName + (needsSlash ? L"\\" : L"") + marker); } else iter = this->fileMap.upper_bound(node.fileName); diff --git a/WinFsp-MemFs-Extended/memfs.h b/WinFsp-MemFs-Extended/memfs.h index 40efe1e..2109b96 100644 --- a/WinFsp-MemFs-Extended/memfs.h +++ b/WinFsp-MemFs-Extended/memfs.h @@ -64,9 +64,7 @@ namespace Memfs { std::vector EnumerateNamedStreams(const FileNode& node, const bool references); std::vector EnumerateDescendants(const FileNode& node, const bool references); - std::vector EnumerateDirChildren(const FileNode& node, const std::refoptional marker); - - FileNodeMap& GetRawFileMap(); + std::vector EnumerateDirChildren(const FileNode& node, const wchar_t* marker); private: std::unique_ptr fileSystem; diff --git a/WinFsp-MemFs-Extended/nodes.cpp b/WinFsp-MemFs-Extended/nodes.cpp index dbc11cd..406f7c7 100644 --- a/WinFsp-MemFs-Extended/nodes.cpp +++ b/WinFsp-MemFs-Extended/nodes.cpp @@ -100,7 +100,7 @@ FileNodeEaMap& FileNode::GetEaMap() { } if (!this->eaMap.has_value()) { - this->eaMap = std::make_optional(FileNodeEaMap()); + this->eaMap = FileNodeEaMap(); } return this->eaMap.value();