修改掉其中有问题的地方

This commit is contained in:
asypost committed 2026-07-31 17:20:58 +08:00
1 parent c451a077b2
commit 45060f03b6
6 files changed
+146 -104

No files matched your search

+2 -2
View File
@@ -48,7 +48,7 @@ namespace seafile::fs
/// 返回路径下面的子项目信息 /// 返回路径下面的子项目信息
/// @param path 路径 /// @param path 路径
/// @return 子项目信息 /// @return 子项目信息
[[nodiscard]] tl::expected<std::variant<std::vector<storage::RepoInfo>, std::vector<storage::RepoEntry> >, [[nodiscard]] tl::expected<std::variant<std::vector<storage::RepoInfo>, std::vector<storage::RepoEntry>>,
api::SeafileException> listDir(const std::string& path); api::SeafileException> listDir(const std::string& path);
/// 读取文件内容 /// 读取文件内容
@@ -90,7 +90,7 @@ namespace seafile::fs
std::vector<storage::RepoEntry> queryDirectoryChildren( std::vector<storage::RepoEntry> queryDirectoryChildren(
const std::string& repo_id, const std::string& parent_path); const std::string& repo_id, const std::string& parent_path);
struct Database; class Database;
std::unique_ptr<Database> database; std::unique_ptr<Database> database;
std::string api_server; std::string api_server;
std::string username; std::string username;
+1 -1
View File
@@ -10,7 +10,7 @@ auto wmain(int argc, wchar_t** argv) -> int
if (!NT_SUCCESS(FspLoad(nullptr))) if (!NT_SUCCESS(FspLoad(nullptr)))
return ERROR_DELAY_LOAD_FAILED; return ERROR_DELAY_LOAD_FAILED;
spdlog::set_level(spdlog::level::err); spdlog::set_level(spdlog::level::info);
FspDebugLogSetHandle(GetStdHandle(STD_ERROR_HANDLE)); FspDebugLogSetHandle(GetStdHandle(STD_ERROR_HANDLE));
+1 -1
View File
@@ -232,7 +232,7 @@ namespace seafile::fs
return STATUS_UNSUCCESSFUL; return STATUS_UNSUCCESSFUL;
} }
auto add_entry = [&](const std::wstring& name, uint64_t size, uint32_t attrs, int64_t mtime) auto add_entry = [&](const std::wstring& name, const uint64_t size, const uint32_t attrs, const int64_t mtime)
{ {
const auto entry_size = sizeof(FSP_FSCTL_DIR_INFO) + ((name.size() + 1) * sizeof(WCHAR)); const auto entry_size = sizeof(FSP_FSCTL_DIR_INFO) + ((name.size() + 1) * sizeof(WCHAR));
const auto dir_info = static_cast<FSP_FSCTL_DIR_INFO*>(std::malloc(entry_size)); const auto dir_info = static_cast<FSP_FSCTL_DIR_INFO*>(std::malloc(entry_size));
+64 -42
View File
@@ -130,18 +130,51 @@ namespace seafile::fs
} }
} }
struct SeafileStore::Database class SeafileStore::Database
{ {
using Storage = decltype(make_storage("")); using Storage = decltype(make_storage(""));
Storage storage; Storage storage;
std::recursive_mutex mutex; std::recursive_mutex mutex;
class LockProxy final
{
Storage& storage;
std::lock_guard<std::recursive_mutex> lock;
public:
LockProxy(Storage& storage, std::recursive_mutex& mutex) : storage(storage), lock(mutex)
{
}
LockProxy(const LockProxy&) = delete;
LockProxy& operator=(const LockProxy&) = delete;
LockProxy(LockProxy&&) = delete;
LockProxy& operator=(LockProxy&&) = delete;
Storage* operator->() const
{
return &this->storage;
}
};
public:
explicit Database(const std::string& path) explicit Database(const std::string& path)
: storage(make_storage(path)) : storage(make_storage(path))
{ {
storage.pragma.journal_mode(sqlite_orm::journal_mode::WAL); storage.pragma.journal_mode(sqlite_orm::journal_mode::WAL);
storage.pragma.synchronous(1); storage.pragma.synchronous(1);
} }
Database(const Database&) = delete;
Database& operator=(const Database&) = delete;
[[nodiscard]]
LockProxy operator->()
{
return LockProxy{storage, mutex};
}
}; };
tl::expected<void, api::SeafileException> SeafileStore::auth() tl::expected<void, api::SeafileException> SeafileStore::auth()
@@ -166,9 +199,8 @@ namespace seafile::fs
tl::expected<RepoEntry, api::SeafileException> SeafileStore::findRepoEntry( tl::expected<RepoEntry, api::SeafileException> SeafileStore::findRepoEntry(
const std::string& repo_id, const std::string& path) const std::string& repo_id, const std::string& path)
{ {
std::lock_guard lock(database->mutex); auto& storage = *(this->database);
auto& storage = this->database->storage; const auto entries = storage->get_all<RepoEntry>(
const auto entries = storage.get_all<RepoEntry>(
sqlite_orm::where( sqlite_orm::where(
sqlite_orm::and_( sqlite_orm::and_(
sqlite_orm::c(&RepoEntry::repo_id) == repo_id, sqlite_orm::c(&RepoEntry::repo_id) == repo_id,
@@ -185,9 +217,8 @@ namespace seafile::fs
tl::expected<bool, api::SeafileException> SeafileStore::checkIsUpdated(const std::string& repo_id) tl::expected<bool, api::SeafileException> SeafileStore::checkIsUpdated(const std::string& repo_id)
{ {
std::lock_guard lock(database->mutex); auto& storage = *(this->database);
auto& storage = this->database->storage; const auto repos = storage->get_all<RepoInfo>(
const auto repos = storage.get_all<RepoInfo>(
sqlite_orm::where( sqlite_orm::where(
sqlite_orm::c(&RepoInfo::id) == repo_id sqlite_orm::c(&RepoInfo::id) == repo_id
)); ));
@@ -218,9 +249,8 @@ namespace seafile::fs
try try
{ {
{ {
std::lock_guard lock(database->mutex); auto& storage = *this->database;
auto& storage = this->database->storage; storage->remove_all<RepoEntry>(sqlite_orm::where(
storage.remove_all<RepoEntry>(sqlite_orm::where(
sqlite_orm::c(&RepoEntry::repo_id) == repo_id sqlite_orm::c(&RepoEntry::repo_id) == repo_id
) )
); );
@@ -231,8 +261,7 @@ namespace seafile::fs
return tl::make_unexpected(repo_entries.error()); return tl::make_unexpected(repo_entries.error());
} }
{ {
std::lock_guard lock(database->mutex); auto& storage = *this->database;
auto& storage = this->database->storage;
std::vector<RepoEntry> entries; std::vector<RepoEntry> entries;
entries.reserve(repo_entries->size()); entries.reserve(repo_entries->size());
std::transform(repo_entries->begin(), repo_entries->end(), std::back_inserter(entries), std::transform(repo_entries->begin(), repo_entries->end(), std::back_inserter(entries),
@@ -240,9 +269,9 @@ namespace seafile::fs
{ {
return RepoEntry::from_api_model(repo_id, "/", entry); return RepoEntry::from_api_model(repo_id, "/", entry);
}); });
storage.transaction([&entries,&storage] storage->transaction([&entries,&storage]
{ {
storage.replace_range(entries.begin(), entries.end()); storage->replace_range(entries.begin(), entries.end());
return true; return true;
}); });
} }
@@ -259,8 +288,7 @@ namespace seafile::fs
{ {
try try
{ {
std::lock_guard lock(database->mutex); (*this->database)->sync_schema();
this->database->storage.sync_schema();
} }
catch (std::exception& e) catch (std::exception& e)
{ {
@@ -280,8 +308,7 @@ namespace seafile::fs
{ {
return tl::make_unexpected(user_info.error()); return tl::make_unexpected(user_info.error());
} }
std::lock_guard lock(database->mutex); (*this->database)->replace(*user_info);
this->database->storage.replace(*user_info);
return {}; return {};
} }
@@ -292,8 +319,7 @@ namespace seafile::fs
{ {
return tl::make_unexpected(repos.error()); return tl::make_unexpected(repos.error());
} }
std::lock_guard lock(database->mutex); auto& storage = *this->database;
auto& storage = this->database->storage;
std::vector<RepoInfo> result; std::vector<RepoInfo> result;
result.reserve(repos->size()); result.reserve(repos->size());
for (const auto& repo : *repos) for (const auto& repo : *repos)
@@ -302,7 +328,7 @@ namespace seafile::fs
result.push_back(storage_repo); result.push_back(storage_repo);
try try
{ {
const auto existings = storage.get_all<RepoInfo>( const auto existings = storage->get_all<RepoInfo>(
sqlite_orm::where(sqlite_orm::c(&RepoInfo::id) == storage_repo.id)); sqlite_orm::where(sqlite_orm::c(&RepoInfo::id) == storage_repo.id));
std::optional<RepoInfo> existing = std::nullopt; std::optional<RepoInfo> existing = std::nullopt;
if (!existings.empty()) if (!existings.empty())
@@ -311,10 +337,10 @@ namespace seafile::fs
} }
if (existing && existing->head_commit_id != storage_repo.head_commit_id) if (existing && existing->head_commit_id != storage_repo.head_commit_id)
{ {
storage.remove_all<RepoEntry>(sqlite_orm::where( storage->remove_all<RepoEntry>(sqlite_orm::where(
sqlite_orm::c(&RepoEntry::repo_id) == storage_repo.id)); sqlite_orm::c(&RepoEntry::repo_id) == storage_repo.id));
} }
storage.replace(storage_repo); storage->replace(storage_repo);
} }
catch (std::exception& e) catch (std::exception& e)
{ {
@@ -330,9 +356,8 @@ namespace seafile::fs
{ {
std::vector<RepoInfo> repos; std::vector<RepoInfo> repos;
{ {
std::lock_guard lock(database->mutex); auto& storage = *this->database;
auto& storage = this->database->storage; repos = storage->get_all<RepoInfo>(
repos = storage.get_all<RepoInfo>(
sqlite_orm::where(sqlite_orm::c(&RepoInfo::name) == name)); sqlite_orm::where(sqlite_orm::c(&RepoInfo::name) == name));
} }
if (!repos.empty()) if (!repos.empty())
@@ -379,17 +404,17 @@ namespace seafile::fs
{ {
return RepoEntry::from_api_model(repo_id, parent_path, entry); return RepoEntry::from_api_model(repo_id, parent_path, entry);
}); });
std::lock_guard lock(database->mutex); auto& storage = *this->database;
auto& storage = this->database->storage;
constexpr std::size_t batch_size = 500; constexpr std::size_t batch_size = 500;
storage.transaction([&] storage->transaction([&]
{ {
for (auto it = repo_entries.begin(); it != repo_entries.end();) for (auto it = repo_entries.begin(); it != repo_entries.end();)
{ {
auto end = it; auto end = it;
std::advance(end, std::min(batch_size,static_cast<std::size_t>(std::distance(it, repo_entries.end())))); std::advance(
end, std::min(batch_size, static_cast<std::size_t>(std::distance(it, repo_entries.end()))));
storage.replace_range(it, end); storage->replace_range(it, end);
it = end; it = end;
} }
@@ -406,16 +431,15 @@ namespace seafile::fs
std::vector<RepoEntry> SeafileStore::queryDirectoryChildren( std::vector<RepoEntry> SeafileStore::queryDirectoryChildren(
const std::string& repo_id, const std::string& parent_path) const std::string& repo_id, const std::string& parent_path)
{ {
std::lock_guard lock(database->mutex); auto& storage = *this->database;
auto& storage = this->database->storage;
const std::string prefix = (parent_path == "/") ? "" : parent_path; const std::string prefix = (parent_path == "/") ? "" : parent_path;
const auto all = storage.get_all<RepoEntry>( const auto all = storage->get_all<RepoEntry>(
sqlite_orm::where( sqlite_orm::where(
sqlite_orm::and_( sqlite_orm::and_(
sqlite_orm::c(&RepoEntry::repo_id) == repo_id, sqlite_orm::c(&RepoEntry::repo_id) == repo_id,
sqlite_orm::like(&RepoEntry::path, prefix + "/%") sqlite_orm::like(&RepoEntry::path, prefix + "/%")
) )
),sqlite_orm::order_by(&RepoEntry::name)); ), sqlite_orm::order_by(&RepoEntry::name));
std::vector<RepoEntry> children; std::vector<RepoEntry> children;
for (auto& entry : all) for (auto& entry : all)
{ {
@@ -478,14 +502,13 @@ namespace seafile::fs
if (parts.empty()) if (parts.empty())
{ {
std::lock_guard lock(database->mutex); auto& storage = *this->database;
auto& storage = this->database->storage; auto repos = storage->get_all<RepoInfo>(sqlite_orm::order_by(&RepoInfo::id));
auto repos = storage.get_all<RepoInfo>(sqlite_orm::order_by(&RepoInfo::id));
if (repos.empty()) if (repos.empty())
{ {
if (const auto synced = this->syncRepos(); synced) if (const auto synced = this->syncRepos(); synced)
{ {
repos = storage.get_all<RepoInfo>(); repos = storage->get_all<RepoInfo>();
} }
else else
{ {
@@ -527,9 +550,8 @@ namespace seafile::fs
tl::expected<api::AccountInfo, api::SeafileException> SeafileStore::accountInfo() const tl::expected<api::AccountInfo, api::SeafileException> SeafileStore::accountInfo() const
{ {
std::lock_guard lock(database->mutex); auto& storage = *this->database;
auto& storage = this->database->storage; if (const auto infos = storage->get_all<AccountInfo>(); !infos.empty())
if (const auto infos = storage.get_all<AccountInfo>(); !infos.empty())
{ {
return infos.front(); return infos.front();
} }
+48 -40
View File
@@ -5,7 +5,6 @@
// Created by Shell on 2026/6/20. // Created by Shell on 2026/6/20.
// //
#include <shared_mutex> #include <shared_mutex>
#include <unordered_map>
#include <spdlog/spdlog.h> #include <spdlog/spdlog.h>
#include <format> #include <format>
@@ -17,9 +16,9 @@
namespace seafile::fs namespace seafile::fs
{ {
using namespace seafile::literals; using namespace seafile::literals;
namespace namespace
{ {
FSP_FILE_SYSTEM_INTERFACE FsInterface = { FSP_FILE_SYSTEM_INTERFACE FsInterface = {
.GetVolumeInfo = WinFSPAdapter::GetVolumeInfo, .GetVolumeInfo = WinFSPAdapter::GetVolumeInfo,
.SetVolumeLabel = WinFSPAdapter::SetVolumeLabel_, .SetVolumeLabel = WinFSPAdapter::SetVolumeLabel_,
@@ -36,8 +35,6 @@ namespace seafile::fs
.ReadDirectory = WinFSPAdapter::ReadDirectory, .ReadDirectory = WinFSPAdapter::ReadDirectory,
.GetDirInfoByName = WinFSPAdapter::GetDirInfoByName .GetDirInfoByName = WinFSPAdapter::GetDirInfoByName
}; };
std::unordered_map<FSP_FILE_SYSTEM*, SeafileFileSystem*> fstab;
std::shared_mutex fstab_mutex;
} }
NTSTATUS WinFSPAdapter::SvcStart(FSP_SERVICE* Service, ULONG argc, PWSTR* argv) NTSTATUS WinFSPAdapter::SvcStart(FSP_SERVICE* Service, ULONG argc, PWSTR* argv)
@@ -61,7 +58,7 @@ namespace seafile::fs
VolumeParams.MaxComponentLength = 255; VolumeParams.MaxComponentLength = 255;
VolumeParams.VolumeCreationTime = reinterpret_cast<PLARGE_INTEGER>(&CreationTime)->QuadPart; VolumeParams.VolumeCreationTime = reinterpret_cast<PLARGE_INTEGER>(&CreationTime)->QuadPart;
VolumeParams.VolumeSerialNumber = 0; VolumeParams.VolumeSerialNumber = 0;
VolumeParams.FileInfoTimeout = 1000; VolumeParams.FileInfoTimeout = 30000;
VolumeParams.CaseSensitiveSearch = 0; VolumeParams.CaseSensitiveSearch = 0;
VolumeParams.CasePreservedNames = 1; VolumeParams.CasePreservedNames = 1;
VolumeParams.UnicodeOnDisk = 1; VolumeParams.UnicodeOnDisk = 1;
@@ -99,62 +96,51 @@ namespace seafile::fs
shell::util::StringUtil::utf8_to_utf16le( shell::util::StringUtil::utf8_to_utf16le(
setting.value().mount_label)); setting.value().mount_label));
FileSystem->UserContext = seafile_file_system; FileSystem->UserContext = seafile_file_system;
{
std::lock_guard lock(fstab_mutex);
fstab.insert({FileSystem, seafile_file_system});
}
Result = FspFileSystemSetMountPoint(FileSystem, mount_point.data()); Result = FspFileSystemSetMountPoint(FileSystem, mount_point.data());
if (!NT_SUCCESS(Result)) if (!NT_SUCCESS(Result))
{ {
{
std::lock_guard lock(fstab_mutex);
fstab.erase(FileSystem);
}
FspFileSystemDelete(FileSystem); FspFileSystemDelete(FileSystem);
delete seafile_file_system; delete seafile_file_system;
return Result; return Result;
} }
Result = FspFileSystemStartDispatcher(FileSystem, 0);
if (!NT_SUCCESS(Result))
{
{
std::lock_guard lock(fstab_mutex);
fstab.erase(FileSystem);
}
FspFileSystemDelete(FileSystem);
delete seafile_file_system;
return Result;
}
FspFileSystemSetDebugLog( FspFileSystemSetDebugLog(
FileSystem, FileSystem,
TRUE); TRUE);
Result = FspFileSystemStartDispatcher(FileSystem, 0);
if (!NT_SUCCESS(Result))
{
FspFileSystemDelete(FileSystem);
delete seafile_file_system;
return Result;
}
Service->UserContext = FileSystem;
return Result; return Result;
} }
NTSTATUS WinFSPAdapter::SvcStop(FSP_SERVICE* Service) NTSTATUS WinFSPAdapter::SvcStop(FSP_SERVICE* Service)
{ {
decltype(fstab) snapshot; if (!Service)
{ return STATUS_INVALID_PARAMETER;
std::lock_guard lock(fstab_mutex); auto* FileSystem = static_cast<FSP_FILE_SYSTEM*>(Service->UserContext);
snapshot = std::move(fstab); if (!FileSystem)
} return STATUS_SUCCESS;
for (auto& [fileSystem, seafileFs] : snapshot) const auto* SeafileFS = static_cast<SeafileFileSystem*>(FileSystem->UserContext);
{ FspFileSystemStopDispatcher(FileSystem);
FspFileSystemStopDispatcher(fileSystem); delete SeafileFS;
FspFileSystemDelete(fileSystem); FspFileSystemDelete(FileSystem);
delete seafileFs;
}
return STATUS_SUCCESS; return STATUS_SUCCESS;
} }
NTSTATUS WinFSPAdapter::GetVolumeInfo(FSP_FILE_SYSTEM* FileSystem, FSP_FSCTL_VOLUME_INFO* VolumeInfo) NTSTATUS WinFSPAdapter::GetVolumeInfo(FSP_FILE_SYSTEM* FileSystem, FSP_FSCTL_VOLUME_INFO* VolumeInfo)
{ {
if (!FileSystem)
return STATUS_INVALID_PARAMETER;
const auto fs = static_cast<SeafileFileSystem*>(FileSystem->UserContext); const auto fs = static_cast<SeafileFileSystem*>(FileSystem->UserContext);
if (!fs) return STATUS_INVALID_PARAMETER; if (!fs) return STATUS_INVALID_PARAMETER;
return fs->GetVolumeInfo(VolumeInfo); return fs->GetVolumeInfo(VolumeInfo);
@@ -164,6 +150,8 @@ namespace seafile::fs
BOOLEAN ReplaceFileAttributes, UINT64 AllocationSize, BOOLEAN ReplaceFileAttributes, UINT64 AllocationSize,
FSP_FSCTL_FILE_INFO* FileInfo) FSP_FSCTL_FILE_INFO* FileInfo)
{ {
if (!FileSystem)
return STATUS_INVALID_PARAMETER;
const auto fs = static_cast<SeafileFileSystem*>(FileSystem->UserContext); const auto fs = static_cast<SeafileFileSystem*>(FileSystem->UserContext);
if (!fs) return STATUS_INVALID_PARAMETER; if (!fs) return STATUS_INVALID_PARAMETER;
return fs->Overwrite(FileContext, FileAttributes, ReplaceFileAttributes, AllocationSize, FileInfo); return fs->Overwrite(FileContext, FileAttributes, ReplaceFileAttributes, AllocationSize, FileInfo);
@@ -171,6 +159,7 @@ namespace seafile::fs
void WinFSPAdapter::Close(FSP_FILE_SYSTEM* FileSystem, PVOID FileContext) void WinFSPAdapter::Close(FSP_FILE_SYSTEM* FileSystem, PVOID FileContext)
{ {
if (!FileSystem) return;
const auto fs = static_cast<SeafileFileSystem*>(FileSystem->UserContext); const auto fs = static_cast<SeafileFileSystem*>(FileSystem->UserContext);
if (!fs) return; if (!fs) return;
fs->Close(FileContext); fs->Close(FileContext);
@@ -180,6 +169,9 @@ namespace seafile::fs
UINT32 GrantedAccess, UINT32 FileAttributes, PSECURITY_DESCRIPTOR SecurityDescriptor, UINT32 GrantedAccess, UINT32 FileAttributes, PSECURITY_DESCRIPTOR SecurityDescriptor,
UINT64 AllocationSize, PVOID* PFileContext, FSP_FSCTL_FILE_INFO* FileInfo) UINT64 AllocationSize, PVOID* PFileContext, FSP_FSCTL_FILE_INFO* FileInfo)
{ {
if (!FileSystem)
return STATUS_INVALID_PARAMETER;
const auto fs = static_cast<SeafileFileSystem*>(FileSystem->UserContext); const auto fs = static_cast<SeafileFileSystem*>(FileSystem->UserContext);
if (!fs) return STATUS_INVALID_PARAMETER; if (!fs) return STATUS_INVALID_PARAMETER;
return fs->Create(FileName, CreateOptions, return fs->Create(FileName, CreateOptions,
@@ -189,6 +181,7 @@ namespace seafile::fs
VOID WinFSPAdapter::Cleanup(FSP_FILE_SYSTEM* FileSystem, PVOID FileContext, PWSTR FileName, ULONG Flags) VOID WinFSPAdapter::Cleanup(FSP_FILE_SYSTEM* FileSystem, PVOID FileContext, PWSTR FileName, ULONG Flags)
{ {
if (!FileSystem) return;
const auto fs = static_cast<SeafileFileSystem*>(FileSystem->UserContext); const auto fs = static_cast<SeafileFileSystem*>(FileSystem->UserContext);
if (!fs) return; if (!fs) return;
fs->Cleanup(FileContext, FileName, Flags); fs->Cleanup(FileContext, FileName, Flags);
@@ -197,6 +190,9 @@ namespace seafile::fs
NTSTATUS WinFSPAdapter::ReadDirectory(FSP_FILE_SYSTEM* FileSystem, PVOID FileContext0, PWSTR Pattern, PWSTR Marker, NTSTATUS WinFSPAdapter::ReadDirectory(FSP_FILE_SYSTEM* FileSystem, PVOID FileContext0, PWSTR Pattern, PWSTR Marker,
PVOID Buffer, ULONG BufferLength, PULONG PBytesTransferred) PVOID Buffer, ULONG BufferLength, PULONG PBytesTransferred)
{ {
if (!FileSystem)
return STATUS_INVALID_PARAMETER;
const auto fs = static_cast<SeafileFileSystem*>(FileSystem->UserContext); const auto fs = static_cast<SeafileFileSystem*>(FileSystem->UserContext);
if (!fs) return STATUS_INVALID_PARAMETER; if (!fs) return STATUS_INVALID_PARAMETER;
return fs->ReadDirectory(FileContext0, Pattern, Marker, Buffer, BufferLength, PBytesTransferred); return fs->ReadDirectory(FileContext0, Pattern, Marker, Buffer, BufferLength, PBytesTransferred);
@@ -206,6 +202,8 @@ namespace seafile::fs
PVOID FileContext, PWSTR FileName, PVOID FileContext, PWSTR FileName,
FSP_FSCTL_DIR_INFO* DirInfo) FSP_FSCTL_DIR_INFO* DirInfo)
{ {
if (!FileSystem)
return STATUS_INVALID_PARAMETER;
const auto fs = static_cast<SeafileFileSystem*>(FileSystem->UserContext); const auto fs = static_cast<SeafileFileSystem*>(FileSystem->UserContext);
if (!fs) return STATUS_INVALID_PARAMETER; if (!fs) return STATUS_INVALID_PARAMETER;
return fs->GetDirInfoByName(FileContext, FileName, DirInfo); return fs->GetDirInfoByName(FileContext, FileName, DirInfo);
@@ -216,6 +214,8 @@ namespace seafile::fs
PSECURITY_DESCRIPTOR SecurityDescriptor, PSECURITY_DESCRIPTOR SecurityDescriptor,
SIZE_T* PSecurityDescriptorSize) SIZE_T* PSecurityDescriptorSize)
{ {
if (!FileSystem)
return STATUS_INVALID_PARAMETER;
const auto fs = static_cast<SeafileFileSystem*>(FileSystem->UserContext); const auto fs = static_cast<SeafileFileSystem*>(FileSystem->UserContext);
if (!fs) return STATUS_INVALID_PARAMETER; if (!fs) return STATUS_INVALID_PARAMETER;
return fs->GetSecurityByName(FileName, PFileAttributes, SecurityDescriptor, PSecurityDescriptorSize); return fs->GetSecurityByName(FileName, PFileAttributes, SecurityDescriptor, PSecurityDescriptorSize);
@@ -224,6 +224,8 @@ namespace seafile::fs
NTSTATUS WinFSPAdapter::SetVolumeLabel_(FSP_FILE_SYSTEM* FileSystem, PWSTR VolumeLabel, NTSTATUS WinFSPAdapter::SetVolumeLabel_(FSP_FILE_SYSTEM* FileSystem, PWSTR VolumeLabel,
FSP_FSCTL_VOLUME_INFO* VolumeInfo) FSP_FSCTL_VOLUME_INFO* VolumeInfo)
{ {
if (!FileSystem)
return STATUS_INVALID_PARAMETER;
const auto fs = static_cast<SeafileFileSystem*>(FileSystem->UserContext); const auto fs = static_cast<SeafileFileSystem*>(FileSystem->UserContext);
if (!fs) return STATUS_INVALID_PARAMETER; if (!fs) return STATUS_INVALID_PARAMETER;
return fs->SetVolumeLabel_(VolumeLabel, VolumeInfo); return fs->SetVolumeLabel_(VolumeLabel, VolumeInfo);
@@ -232,6 +234,8 @@ namespace seafile::fs
NTSTATUS WinFSPAdapter::Open(FSP_FILE_SYSTEM* FileSystem, PWSTR FileName, UINT32 CreateOptions, NTSTATUS WinFSPAdapter::Open(FSP_FILE_SYSTEM* FileSystem, PWSTR FileName, UINT32 CreateOptions,
UINT32 GrantedAccess, PVOID* PFileContext, FSP_FSCTL_FILE_INFO* FileInfo) UINT32 GrantedAccess, PVOID* PFileContext, FSP_FSCTL_FILE_INFO* FileInfo)
{ {
if (!FileSystem)
return STATUS_INVALID_PARAMETER;
const auto fs = static_cast<SeafileFileSystem*>(FileSystem->UserContext); const auto fs = static_cast<SeafileFileSystem*>(FileSystem->UserContext);
if (!fs) return STATUS_INVALID_PARAMETER; if (!fs) return STATUS_INVALID_PARAMETER;
return fs->Open(FileName, CreateOptions, GrantedAccess, PFileContext, FileInfo); return fs->Open(FileName, CreateOptions, GrantedAccess, PFileContext, FileInfo);
@@ -240,6 +244,8 @@ namespace seafile::fs
NTSTATUS WinFSPAdapter::Read(FSP_FILE_SYSTEM* FileSystem, PVOID FileContext, PVOID Buffer, UINT64 Offset, NTSTATUS WinFSPAdapter::Read(FSP_FILE_SYSTEM* FileSystem, PVOID FileContext, PVOID Buffer, UINT64 Offset,
ULONG Length, PULONG PBytesTransferred) ULONG Length, PULONG PBytesTransferred)
{ {
if (!FileSystem)
return STATUS_INVALID_PARAMETER;
const auto fs = static_cast<SeafileFileSystem*>(FileSystem->UserContext); const auto fs = static_cast<SeafileFileSystem*>(FileSystem->UserContext);
if (!fs) return STATUS_INVALID_PARAMETER; if (!fs) return STATUS_INVALID_PARAMETER;
return fs->Read(FileContext, Buffer, Offset, Length, PBytesTransferred); return fs->Read(FileContext, Buffer, Offset, Length, PBytesTransferred);
@@ -248,6 +254,8 @@ namespace seafile::fs
NTSTATUS WinFSPAdapter::GetFileInfo(FSP_FILE_SYSTEM* FileSystem, HANDLE Handle, NTSTATUS WinFSPAdapter::GetFileInfo(FSP_FILE_SYSTEM* FileSystem, HANDLE Handle,
FSP_FSCTL_FILE_INFO* FileInfo) FSP_FSCTL_FILE_INFO* FileInfo)
{ {
if (!FileSystem)
return STATUS_INVALID_PARAMETER;
const auto fs = static_cast<SeafileFileSystem*>(FileSystem->UserContext); const auto fs = static_cast<SeafileFileSystem*>(FileSystem->UserContext);
if (!fs) return STATUS_INVALID_PARAMETER; if (!fs) return STATUS_INVALID_PARAMETER;
return fs->GetFileInfo(Handle, FileInfo); return fs->GetFileInfo(Handle, FileInfo);
@@ -257,12 +265,12 @@ namespace seafile::fs
SECURITY_INFORMATION SecurityInformation, SECURITY_INFORMATION SecurityInformation,
PSECURITY_DESCRIPTOR ModificationDescriptor) PSECURITY_DESCRIPTOR ModificationDescriptor)
{ {
return STATUS_ACCESS_DENIED; return STATUS_SUCCESS;
} }
NTSTATUS WinFSPAdapter::GetSecurity(FSP_FILE_SYSTEM* FileSystem, PVOID FileContext, NTSTATUS WinFSPAdapter::GetSecurity(FSP_FILE_SYSTEM* FileSystem, PVOID FileContext,
PSECURITY_DESCRIPTOR SecurityDescriptor, SIZE_T* PSecurityDescriptorSize) PSECURITY_DESCRIPTOR SecurityDescriptor, SIZE_T* PSecurityDescriptorSize)
{ {
return STATUS_NOT_IMPLEMENTED; return STATUS_SUCCESS;
} }
} }
+30 -18
View File
@@ -13,15 +13,19 @@
#include "util/icu_raii.hpp" #include "util/icu_raii.hpp"
namespace shell::util { namespace shell::util
std::vector<std::string> StringUtil::split(const std::string &s, const std::string &delim) { {
if (delim.empty()) { std::vector<std::string> StringUtil::split(const std::string& s, const std::string& delim)
{
if (delim.empty())
{
return {s}; return {s};
} }
std::vector<std::string> result; std::vector<std::string> result;
size_t start = 0, pos; size_t start = 0, pos;
while ((pos = s.find(delim, start)) != std::string::npos) { while ((pos = s.find(delim, start)) != std::string::npos)
{
result.emplace_back(s.substr(start, pos - start)); result.emplace_back(s.substr(start, pos - start));
start = pos + delim.length(); start = pos + delim.length();
} }
@@ -30,36 +34,42 @@ namespace shell::util {
return result; return result;
} }
std::string StringUtil::convert_encoding(const std::string &input, const std::string &to) { std::string StringUtil::convert_encoding(const std::string& input, const std::string& to)
{
return convert_encoding(input, detect_encoding(input), to); return convert_encoding(input, detect_encoding(input), to);
} }
std::string StringUtil::detect_encoding(const std::string &data) { std::string StringUtil::detect_encoding(const std::string& data)
{
UErrorCode status = U_ZERO_ERROR; UErrorCode status = U_ZERO_ERROR;
const auto detector = UCharsetDetectorPtr(ucsdet_open(&status)); const auto detector = UCharsetDetectorPtr(ucsdet_open(&status));
if (U_FAILURE(status)) { if (U_FAILURE(status))
{
throw std::runtime_error(std::format("ucsdet_open failed:{}", u_errorName(status))); throw std::runtime_error(std::format("ucsdet_open failed:{}", u_errorName(status)));
} }
// 设置待检测数据 // 设置待检测数据
ucsdet_setText(detector.get(), data.data(), data.size(), &status); ucsdet_setText(detector.get(), data.data(), data.size(), &status);
if (U_FAILURE(status)) { if (U_FAILURE(status))
{
throw std::runtime_error(std::format("ucsdet_open failed,can't set text:{}", u_errorName(status))); throw std::runtime_error(std::format("ucsdet_open failed,can't set text:{}", u_errorName(status)));
} }
// 检测 // 检测
const UCharsetMatch *match = ucsdet_detect(detector.get(), &status); const UCharsetMatch* match = ucsdet_detect(detector.get(), &status);
std::string encoding; std::string encoding;
if (match && U_SUCCESS(status)) { if (match && U_SUCCESS(status))
{
encoding = ucsdet_getName(match, &status); encoding = ucsdet_getName(match, &status);
} }
return encoding; return encoding;
} }
std::string StringUtil::convert_encoding(const std::string &input, const std::string &from, const std::string &to) { std::string StringUtil::convert_encoding(const std::string& input, const std::string& from, const std::string& to)
{
UErrorCode status = U_ZERO_ERROR; UErrorCode status = U_ZERO_ERROR;
// 先获取输出长度 // 先获取输出长度
@@ -81,7 +91,8 @@ namespace shell::util {
&status &status
); );
if (U_FAILURE(status)) { if (U_FAILURE(status))
{
throw std::runtime_error(std::format("convert from {} to {} failed:{}", from, to, u_errorName(status))); throw std::runtime_error(std::format("convert from {} to {} failed:{}", from, to, u_errorName(status)));
} }
@@ -89,23 +100,24 @@ namespace shell::util {
} }
#ifdef _WIN32 std::wstring StringUtil::utf8_to_utf16le(const std::string& input)
std::wstring StringUtil::utf8_to_utf16le(const std::string &input) { {
const icu::UnicodeString unicode = icu::UnicodeString::fromUTF8(input); const icu::UnicodeString unicode = icu::UnicodeString::fromUTF8(input);
return std::wstring{ return std::wstring{
reinterpret_cast<const wchar_t *>(unicode.getBuffer()), reinterpret_cast<const wchar_t*>(unicode.getBuffer()),
static_cast<size_t>(unicode.length()) static_cast<size_t>(unicode.length())
}; };
} }
std::string StringUtil::utf16le_to_utf8(const std::wstring &input) { std::string StringUtil::utf16le_to_utf8(const std::wstring& input)
{
return utf16le_to_utf8(input.data()); return utf16le_to_utf8(input.data());
} }
std::string StringUtil::utf16le_to_utf8(const wchar_t *input) { std::string StringUtil::utf16le_to_utf8(const wchar_t* input)
{
const auto unicode = icu::UnicodeString(input); const auto unicode = icu::UnicodeString(input);
std::string utf8; std::string utf8;
return unicode.toUTF8String(utf8); return unicode.toUTF8String(utf8);
} }
#endif
} }