From 9210bf90a92739b01c18933e0baacefe0af402aa Mon Sep 17 00:00:00 2001 From: neru Date: Thu, 7 May 2026 02:53:28 -0300 Subject: [PATCH] fix: misc fixes, cleanup --- src/lib/seallib/vfs.cpp | 90 ++++++++++++++++------------------------- src/lib/seallib/vfs.h | 1 - 2 files changed, 34 insertions(+), 57 deletions(-) diff --git a/src/lib/seallib/vfs.cpp b/src/lib/seallib/vfs.cpp index a2adfb8..f9ea20c 100644 --- a/src/lib/seallib/vfs.cpp +++ b/src/lib/seallib/vfs.cpp @@ -30,14 +30,16 @@ std::string FileBuffer::toString() const void VirtualFileSystem::mount(const std::string& virtualPath, std::unique_ptr provider, int priority) { if (!provider) return; + std::string path = trimPathSeparators(normalizePath(virtualPath)); - std::string normalizedPath = normalizePath(virtualPath); - normalizedPath = trimPathSeparators(normalizedPath); + std::unique_lock lock(_mutex); - std::unique_lock lock(_mutex); + _mounts.emplace_back(path, std::move(provider), priority); - _mounts.emplace_back(normalizedPath, std::move(provider), priority); - sortMounts(); + std::sort(_mounts.begin(), _mounts.end(), [](const MountPoint& a, const MountPoint& b) { + if (a.priority != b.priority) return a.priority > b.priority; + return a.pathDepth > b.pathDepth; + }); } bool VirtualFileSystem::unmount(const std::string& virtualPath) @@ -84,64 +86,62 @@ void VirtualFileSystem::unmountAll() bool VirtualFileSystem::exists(const std::string& path) const { + std::shared_lock lock(_mutex); std::string relativePath; const MountPoint* mount = findBestMount(path, relativePath); - if (!mount) return false; - - return mount->provider->exists(relativePath); + return mount ? mount->provider->exists(relativePath) : false; } VFSResult VirtualFileSystem::readFile(const std::string& path, FileBuffer& out_buffer) const { + std::shared_lock lock(_mutex); std::string relativePath; const MountPoint* mount = findBestMount(path, relativePath); if (!mount) return VFSResult::FileNotFound; - - VFSResult result = mount->provider->readFile(relativePath, out_buffer); - return result; + return mount->provider->readFile(relativePath, out_buffer); } VFSResult VirtualFileSystem::writeFile(const std::string& path, const FileBuffer& buffer) { if (!buffer.isValid()) return VFSResult::Unknown; + std::shared_lock lock(_mutex); std::string relativePath; MountPoint* mount = findBestMount(path, relativePath); - if (!mount) return VFSResult::AccessDenied; - + if (!mount) return VFSResult::InvalidPath; return mount->provider->writeFile(relativePath, buffer); } VFSResult VirtualFileSystem::deleteFile(const std::string& path) { + std::shared_lock lock(_mutex); std::string relativePath; MountPoint* mount = findBestMount(path, relativePath); if (!mount) return VFSResult::FileNotFound; - return mount->provider->deleteFile(relativePath); } VFSResult VirtualFileSystem::createDirectory(const std::string& path) { + std::shared_lock lock(_mutex); std::string relativePath; MountPoint* mount = findBestMount(path, relativePath); if (!mount) return VFSResult::InvalidPath; - return mount->provider->createDirectory(relativePath); } std::vector VirtualFileSystem::listDirectory(const std::string& path) const { + std::shared_lock lock(_mutex); std::string relativePath; const MountPoint* mount = findBestMount(path, relativePath); if (!mount) return {}; - return mount->provider->listDirectory(relativePath); } @@ -157,7 +157,7 @@ std::string VirtualFileSystem::normalizePath(const std::string& path) const if (isPathSeparator(c)) result += '/'; else - result += static_cast(std::tolower(static_cast(c))); + result += c; } std::vector parts; @@ -194,51 +194,36 @@ std::string VirtualFileSystem::normalizePath(const std::string& path) const const VirtualFileSystem::MountPoint* VirtualFileSystem::findBestMount(const std::string& path, std::string& relativePath) const { - std::string normalizedFullPath = normalizePath(path); - std::string searchPath = trimPathSeparators(normalizedFullPath); - - std::shared_lock lock(_mutex); + std::string searchPath = trimPathSeparators(normalizePath(path)); for (const auto& mount : _mounts) { - bool match = false; - if (searchPath == mount.virtualPath) { - match = true; relativePath = "/"; - } - else if (searchPath.length() > mount.virtualPath.length() && searchPath.find(mount.virtualPath + '/') == 0) - { - match = true; - relativePath = searchPath.substr(mount.virtualPath.length() + 1); + return &mount; } - if (match) + if (mount.virtualPath == "/") { - if (relativePath.empty()) relativePath = "/"; + relativePath = searchPath; + return &mount; + } + + std::string prefix = mount.virtualPath + '/'; + if (searchPath.length() > prefix.length() && searchPath.compare(0, prefix.length(), prefix) == 0) + { + relativePath = searchPath.substr(prefix.length()); return &mount; } } - return nullptr; } VirtualFileSystem::MountPoint* VirtualFileSystem::findBestMount(const std::string& path, std::string& relativePath) { - const auto* constResult = const_cast(this)->findBestMount(path, relativePath); - return const_cast(constResult); -} - -void VirtualFileSystem::sortMounts() -{ - for (auto& m : _mounts) - m.pathDepth = MountPoint::countPathDepth(m.virtualPath); - - std::sort(_mounts.begin(), _mounts.end(), [](const MountPoint& a, const MountPoint& b) { - if (a.priority != b.priority) return a.priority > b.priority; - return a.pathDepth > b.pathDepth; - }); + const auto* res = static_cast(this)->findBestMount(path, relativePath); + return const_cast(res); } bool VirtualFileSystem::isPathSeparator(char c) @@ -248,18 +233,11 @@ bool VirtualFileSystem::isPathSeparator(char c) std::string VirtualFileSystem::trimPathSeparators(const std::string& path) { - if (path.empty()) return path; + size_t start = path.find_first_not_of("/\\"); + if (start == std::string::npos) return "/"; - size_t start = 0; - size_t end = path.length(); - - while (start < end && isPathSeparator(path[start])) - start++; - - while (end > start && isPathSeparator(path[end - 1])) - end--; - - return path.substr(start, end - start); + size_t end = path.find_last_not_of("/\\"); + return path.substr(start, end - start + 1); } /* diff --git a/src/lib/seallib/vfs.h b/src/lib/seallib/vfs.h index 5e05d19..c71a3fc 100644 --- a/src/lib/seallib/vfs.h +++ b/src/lib/seallib/vfs.h @@ -97,7 +97,6 @@ namespace seallib const MountPoint* findBestMount(const std::string& path, std::string& relativePath) const; MountPoint* findBestMount(const std::string& path, std::string& relativePath); - void sortMounts(); static bool isPathSeparator(char c); static std::string trimPathSeparators(const std::string& path); };