1、实现目录读取功能

This commit is contained in:
asypost committed 2026-06-27 18:37:37 +08:00
1 parent 6e60886fd3
commit fe476dff69
11 files changed
+320 -116

No files matched your search

+102 -103
View File
@@ -1,5 +1,8 @@
#include "seafile/fs/seafile_file_system.hpp"
#include "seafile/fs/file_context.hpp"
#include "util/string_util.hpp"
namespace seafile::fs
{
namespace
@@ -31,38 +34,24 @@ namespace seafile::fs
FileInfo->ChangeTime = ft;
FileInfo->IndexNumber = 0;
}
void SplitPath(const std::wstring& path, std::string& repo_id, std::string& sub_path)
{
repo_id.clear();
sub_path = "/";
auto p = path;
while (!p.empty() && p[0] == L'\\')
p = p.substr(1);
if (p.empty())
return;
auto sep = p.find(L'\\');
auto repo_id_w = (sep == std::wstring::npos) ? p : p.substr(0, sep);
repo_id.assign(repo_id_w.begin(), repo_id_w.end());
if (sep != std::wstring::npos)
{
auto rest = p.substr(sep);
sub_path.clear();
for (auto c : rest)
sub_path += static_cast<char>(c);
while (!sub_path.empty() && sub_path.front() == '\\')
sub_path = sub_path.substr(1);
sub_path = "/" + sub_path;
}
}
}
void SeafileFileSystem::auth(const std::string& username, const std::string& password)
tl::expected<void, SeafileException> SeafileFileSystem::initialize(const std::string& username,
const std::string& password)
{
if (const auto authToken = api.authToken(username, password); authToken.has_value())
const auto authToken = api.authToken(username, password);
if (authToken.has_value())
{
this->token = authToken.value().token;
if (const auto repo = api.defaultRepo(this->token, this->token_type); repo.has_value())
{
this->repo_id = repo.value().repo_id.value_or("");
return {};
}
return tl::make_unexpected(SeafileException("No default repo found"));
}
return tl::make_unexpected(authToken.error());
}
void SeafileFileSystem::Close(PVOID FileContext)
@@ -74,13 +63,13 @@ namespace seafile::fs
PSECURITY_DESCRIPTOR SecurityDescriptor,
SIZE_T* PSecurityDescriptorSize)
{
if (PFileAttributes)
{
if (wcscmp(FileName, L"\\") == 0)
{
*PFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
}else
}
else
{
*PFileAttributes = FILE_ATTRIBUTE_NORMAL;
}
@@ -91,22 +80,25 @@ namespace seafile::fs
return STATUS_SUCCESS;
}
NTSTATUS SeafileFileSystem::Overwrite(PVOID FileContext, UINT32 FileAttributes, BOOLEAN ReplaceFileAttributes, UINT64 AllocationSize, FSP_FSCTL_FILE_INFO* FileInfo)
NTSTATUS SeafileFileSystem::Overwrite(PVOID FileContext, UINT32 FileAttributes, BOOLEAN ReplaceFileAttributes,
UINT64 AllocationSize, FSP_FSCTL_FILE_INFO* FileInfo)
{
return STATUS_NOT_IMPLEMENTED;
return STATUS_MEDIA_WRITE_PROTECTED;
}
NTSTATUS SeafileFileSystem::GetVolumeInfo(FSP_FSCTL_VOLUME_INFO* VolumeInfo)
NTSTATUS SeafileFileSystem::GetVolumeInfo(FSP_FSCTL_VOLUME_INFO* VolumeInfo) const
{
const auto userInfo = api.accountInfo(this->token, this->token_type);
if (userInfo.has_value())
if (const auto userInfo = api.accountInfo(this->token, this->token_type); userInfo.has_value())
{
const auto& info = userInfo.value();
VolumeInfo->FreeSize = info.total - info.usage;
VolumeInfo->TotalSize = info.total;
wcscpy(VolumeInfo->VolumeLabel, L"Seafile");
VolumeInfo->VolumeLabelLength = static_cast<UINT16>((wcslen(VolumeInfo->VolumeLabel) * sizeof(WCHAR)));
if (!this->volume_label.empty())
{
wcscpy_s(VolumeInfo->VolumeLabel, this->volume_label.data());
VolumeInfo->VolumeLabelLength = static_cast<UINT16>((wcslen(VolumeInfo->VolumeLabel) * sizeof(WCHAR)));
}
return STATUS_SUCCESS;
}
return STATUS_END_OF_MEDIA;
@@ -114,31 +106,25 @@ namespace seafile::fs
void SeafileFileSystem::Cleanup(PVOID FileContext, PWSTR FileName, ULONG Flags)
{
}
NTSTATUS SeafileFileSystem::Open(PWSTR FileName, UINT32 CreateOptions, UINT32 GrantedAccess, PVOID* PFileContext,
FSP_FSCTL_FILE_INFO* FileInfo)
{
std::string repo_id, sub_path;
SplitPath(FileName, repo_id, sub_path);
const bool expect_dir = (CreateOptions & FILE_DIRECTORY_FILE) != 0;
if (repo_id.empty())
{
auto ctx = new SeaFileContext{"", "/", 0, ""};
*PFileContext = ctx;
SetFileInfo(FileInfo, FILE_ATTRIBUTE_DIRECTORY, 0, 0);
return STATUS_SUCCESS;
return STATUS_OBJECT_NAME_NOT_FOUND;
}
if (sub_path == "/")
const auto path = shell::util::StringUtil::utf16le_to_utf8(FileName);
if (wcscmp(FileName, L"\\") == 0)
{
auto repo = api.repoInfo(repo_id, token, token_type);
if (!repo.has_value())
return STATUS_OBJECT_NAME_NOT_FOUND;
auto ctx = new SeaFileContext{repo->id, "/", 0, ""};
const auto ctx = new SeaFileContext{repo->id, path, SeaFileContext::Type::DIRECTORY, 0, ""};
*PFileContext = ctx;
SetFileInfo(FileInfo, FILE_ATTRIBUTE_DIRECTORY, repo->size, repo->mtime);
return STATUS_SUCCESS;
@@ -146,31 +132,26 @@ namespace seafile::fs
if (expect_dir)
{
auto entries = api.repoDir(repo_id, sub_path, token, token_type);
if (entries.has_value())
if (const auto dirInfo = api.dirDetail(repo_id, path, token, token_type); dirInfo.has_value())
{
auto ctx = new SeaFileContext{repo_id, sub_path, 0, ""};
const auto& info = dirInfo.value();
const auto ctx = new SeaFileContext{info.repo_id, info.path, SeaFileContext::Type::DIRECTORY, 0, ""};
*PFileContext = ctx;
SetFileInfo(FileInfo, FILE_ATTRIBUTE_DIRECTORY, 0, 0);
SetFileInfo(FileInfo,FILE_ATTRIBUTE_DIRECTORY, 0, 0);
return STATUS_SUCCESS;
}
auto fdetail = api.fileDetail(repo_id, sub_path, token, token_type);
if (fdetail.has_value())
return STATUS_NOT_A_DIRECTORY;
return STATUS_OBJECT_NAME_NOT_FOUND;
}
auto fdetail = api.fileDetail(repo_id, sub_path, token, token_type);
if (fdetail.has_value())
else
{
auto ctx = new SeaFileContext{repo_id, sub_path, fdetail->size, ""};
*PFileContext = ctx;
SetFileInfo(FileInfo, FILE_ATTRIBUTE_NORMAL, fdetail->size, fdetail->mtime);
return STATUS_SUCCESS;
if (const auto fileInfo = api.fileDetail(repo_id, path, token, token_type); fileInfo.has_value())
{
const auto& info = fileInfo.value();
const auto ctx = new SeaFileContext(repo_id, path, SeaFileContext::Type::FILE, info.size, "");
*PFileContext = ctx;
SetFileInfo(FileInfo,FILE_ATTRIBUTE_ARCHIVE, 0, 0);
return STATUS_SUCCESS;
}
}
auto entries = api.repoDir(repo_id, sub_path, token, token_type);
if (entries.has_value())
return STATUS_FILE_IS_A_DIRECTORY;
return STATUS_OBJECT_NAME_NOT_FOUND;
}
@@ -198,46 +179,62 @@ namespace seafile::fs
{
const auto fc = static_cast<SeaFileContext*>(FileContext);
*PBytesTransferred = 0;
const auto path = std::ranges::replace(fc->path,'\\','/');
const auto entries_result = api.repoDir(fc->repo_id, fc->path, token, token_type);
auto add_entry = [&](const std::wstring& name, UINT32 attrs, uint64_t size, int64_t mtime) -> bool
if (entries_result.has_value())
{
if (Marker && wcscmp(name.c_str(), Marker) <= 0)
return true;
const auto& entries = entries_result.value();
size_t start = 0;
if (Marker)
{
const auto marker = shell::util::StringUtil::utf16le_to_utf8(Marker);
for (auto i = 0; i < entries.size(); ++i)
{
if (entries[i].name == marker)
{
start = i + 1;
break;
}
}
}
for (auto i = start; i < entries.size(); ++i)
{
const auto entry = entries[i];
const auto entry_name = shell::util::StringUtil::utf8_to_utf16le(entry.name);
const auto size = sizeof(FSP_FSCTL_DIR_INFO) + ((entry_name.size() + 1) * sizeof(WCHAR));
const auto name_bytes = (wcslen(name.c_str()) + 1) * sizeof(WCHAR);
const auto entry_size = static_cast<ULONG>(offsetof(FSP_FSCTL_DIR_INFO, FileNameBuf) + name_bytes);
if (*PBytesTransferred + entry_size > BufferLength)
return false;
const auto dir_info = static_cast<FSP_FSCTL_DIR_INFO*>(std::malloc(size));
std::memset(dir_info, 0, size);
dir_info->Size = size;
dir_info->FileInfo.FileSize = entry.size.value_or(0);
if (entry.type == "file")
{
dir_info->FileInfo.FileAttributes = FILE_ATTRIBUTE_ARCHIVE;
}
else
{
dir_info->FileInfo.FileAttributes = FILE_ATTRIBUTE_DIRECTORY;
}
wcscpy_s(dir_info->FileNameBuf, entry_name.size() + 1, entry_name.data());
if (*PBytesTransferred + size < BufferLength)
{
if (!FspFileSystemAddDirInfo(dir_info, Buffer, BufferLength, PBytesTransferred))
{
break;
}
}
else
{
break;
}
}
auto DirInfo = reinterpret_cast<FSP_FSCTL_DIR_INFO*>(
static_cast<PUINT8>(Buffer) + *PBytesTransferred);
memset(DirInfo, 0, sizeof(FSP_FSCTL_DIR_INFO));
DirInfo->Size = entry_size;
SetFileInfo(&DirInfo->FileInfo, attrs, size, mtime);
memcpy(DirInfo->FileNameBuf, name.c_str(), name_bytes);
*PBytesTransferred += entry_size;
return true;
};
if (fc->repo_id.empty())
{
return STATUS_OBJECT_NAME_NOT_FOUND;
}
bool ok = true;
auto entries = api.repoDir(fc->repo_id, fc->path, token, token_type);
if (!entries.has_value())
return STATUS_UNSUCCESSFUL;
for (const auto& entry : entries.value())
{
auto wname = std::wstring(entry.name.begin(), entry.name.end());
const auto attrs = (entry.type == "dir") ? FILE_ATTRIBUTE_DIRECTORY : FILE_ATTRIBUTE_NORMAL;
const auto sz = entry.size.value_or(0);
ok = add_entry(wname, attrs, sz, entry.mtime);
if (!ok) break;
return STATUS_SUCCESS;
}
return STATUS_SUCCESS;
std::cout<<entries_result.error().what()<<std::endl;
return STATUS_UNSUCCESSFUL;
}
NTSTATUS SeafileFileSystem::GetFileInfo(HANDLE Handle, FSP_FSCTL_FILE_INFO* FileInfo)
@@ -263,9 +260,11 @@ namespace seafile::fs
return STATUS_SUCCESS;
}
NTSTATUS SeafileFileSystem::Create(PWSTR FileName, UINT32 CreateOptions, UINT32 GrantedAccess, UINT32 FileAttributes, PSECURITY_DESCRIPTOR SecurityDescriptor, UINT64 AllocationSize, PVOID* PFileContext, FSP_FSCTL_FILE_INFO* FileInfo)
NTSTATUS SeafileFileSystem::Create(PWSTR FileName, UINT32 CreateOptions, UINT32 GrantedAccess,
UINT32 FileAttributes, PSECURITY_DESCRIPTOR SecurityDescriptor,
UINT64 AllocationSize, PVOID* PFileContext, FSP_FSCTL_FILE_INFO* FileInfo)
{
return STATUS_SUCCESS;
return Open(FileName, CreateOptions, GrantedAccess, PFileContext, FileInfo);
}
NTSTATUS SeafileFileSystem::GetDirInfoByName(PVOID FileContext, PWSTR FileName,
@@ -302,7 +301,7 @@ namespace seafile::fs
continue;
memset(DirInfo, 0, sizeof(FSP_FSCTL_DIR_INFO));
const auto name_bytes = (name.size() + 1) * sizeof(WCHAR);
DirInfo->Size = static_cast<UINT64>(offsetof(FSP_FSCTL_DIR_INFO, FileNameBuf) + name_bytes);
DirInfo->Size = offsetof(FSP_FSCTL_DIR_INFO, FileNameBuf) + name_bytes;
const auto attrs = (entry.type == "dir") ? FILE_ATTRIBUTE_DIRECTORY : FILE_ATTRIBUTE_NORMAL;
SetFileInfo(&DirInfo->FileInfo, attrs, entry.size.value_or(0), entry.mtime);
wcscpy_s(DirInfo->FileNameBuf, name.size() + 1, FileName);
@@ -314,6 +313,6 @@ namespace seafile::fs
NTSTATUS SeafileFileSystem::SetVolumeLabel_(PWSTR VolumeLabel, FSP_FSCTL_VOLUME_INFO* VolumeInfo)
{
return STATUS_SUCCESS;
return STATUS_ACCESS_DENIED;
}
}
+9 -5
View File
@@ -65,15 +65,19 @@ namespace seafile::fs
}
auto mount_point = std::wstring(L"X:");
auto seafile_file_system = new SeafileFileSystem("https://pan.hanwangsoft.com", mount_point);
seafile_file_system->auth("asypost@yeah.net", "Shell0805@china");
if (const auto ret= seafile_file_system->initialize("asypost@yeah.net", "Shell0805@china"); !ret.has_value())
{
Result = STATUS_UNSUCCESSFUL;
FspFileSystemDelete(FileSystem);
delete seafile_file_system;
return Result;
}
FileSystem->UserContext = seafile_file_system;
fstab.insert({FileSystem, seafile_file_system});
Result = FspFileSystemSetMountPoint(FileSystem, static_cast<PWSTR>(mount_point.data()));
DWORD LastError = GetLastError();
std::cout << LastError << std::endl;
if (!NT_SUCCESS(Result))
{
FspFileSystemDelete(FileSystem);
@@ -208,7 +212,7 @@ namespace seafile::fs
SECURITY_INFORMATION SecurityInformation,
PSECURITY_DESCRIPTOR ModificationDescriptor)
{
return STATUS_NOT_IMPLEMENTED;
return STATUS_ACCESS_DENIED;
}
NTSTATUS WinFSPAdapter::GetSecurity(FSP_FILE_SYSTEM* FileSystem, PVOID FileContext,