初步实现
This commit is contained in:
1 parent
aace751d01
commit
6e60886fd3
8 files changed
+451
-120
No files matched your search
@@ -1,60 +1,319 @@
|
||||
//
|
||||
// Created by Shell on 2026-06-18.
|
||||
//
|
||||
|
||||
#include "seafile/fs/seafile_file_system.hpp"
|
||||
|
||||
namespace seafile::fs {
|
||||
void SeafileFileSystem::auth(const std::string &username, const std::string &password) {
|
||||
if (const auto authToken = api.authToken(username, password); authToken.has_value()) {
|
||||
this->token = authToken.value().token;
|
||||
namespace seafile::fs
|
||||
{
|
||||
namespace
|
||||
{
|
||||
constexpr int64_t UNIX_EPOCH_AS_FILETIME = 116444736000000000LL;
|
||||
constexpr int64_t HUNDREDS_OF_NANOSECONDS_PER_SECOND = 10000000LL;
|
||||
|
||||
int64_t UnixTimeToFileTime(int64_t unix_time)
|
||||
{
|
||||
return UNIX_EPOCH_AS_FILETIME + unix_time * HUNDREDS_OF_NANOSECONDS_PER_SECOND;
|
||||
}
|
||||
|
||||
int64_t NowFileTime()
|
||||
{
|
||||
FILETIME ft;
|
||||
GetSystemTimeAsFileTime(&ft);
|
||||
return (static_cast<int64_t>(ft.dwHighDateTime) << 32) | ft.dwLowDateTime;
|
||||
}
|
||||
|
||||
void SetFileInfo(FSP_FSCTL_FILE_INFO* FileInfo, uint32_t attributes, uint64_t size, int64_t mtime)
|
||||
{
|
||||
FileInfo->FileAttributes = attributes;
|
||||
FileInfo->AllocationSize = size;
|
||||
FileInfo->FileSize = size;
|
||||
const auto ft = mtime ? UnixTimeToFileTime(mtime) : NowFileTime();
|
||||
FileInfo->CreationTime = ft;
|
||||
FileInfo->LastAccessTime = ft;
|
||||
FileInfo->LastWriteTime = ft;
|
||||
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::Close(PVOID FileContext0) {
|
||||
void SeafileFileSystem::auth(const std::string& username, const std::string& password)
|
||||
{
|
||||
if (const auto authToken = api.authToken(username, password); authToken.has_value())
|
||||
this->token = authToken.value().token;
|
||||
}
|
||||
|
||||
void SeafileFileSystem::Close(PVOID FileContext)
|
||||
{
|
||||
delete static_cast<SeaFileContext*>(FileContext);
|
||||
}
|
||||
|
||||
NTSTATUS SeafileFileSystem::GetSecurityByName(PWSTR FileName, PUINT32 PFileAttributes,
|
||||
PSECURITY_DESCRIPTOR SecurityDescriptor,
|
||||
SIZE_T *PSecurityDescriptorSize) {
|
||||
*PFileAttributes = FILE_ATTRIBUTE_NORMAL;
|
||||
*PSecurityDescriptorSize = 0;
|
||||
SIZE_T* PSecurityDescriptorSize)
|
||||
{
|
||||
|
||||
if (PFileAttributes)
|
||||
{
|
||||
if (wcscmp(FileName, L"\\") == 0)
|
||||
{
|
||||
*PFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
|
||||
}else
|
||||
{
|
||||
*PFileAttributes = FILE_ATTRIBUTE_NORMAL;
|
||||
}
|
||||
}
|
||||
|
||||
if (PSecurityDescriptorSize)
|
||||
*PSecurityDescriptorSize = 0;
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
NTSTATUS SeafileFileSystem::GetVolumeInfo(FSP_FSCTL_VOLUME_INFO *VolumeInfo) {
|
||||
NTSTATUS SeafileFileSystem::Overwrite(PVOID FileContext, UINT32 FileAttributes, BOOLEAN ReplaceFileAttributes, UINT64 AllocationSize, FSP_FSCTL_FILE_INFO* FileInfo)
|
||||
{
|
||||
return STATUS_NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
|
||||
NTSTATUS SeafileFileSystem::GetVolumeInfo(FSP_FSCTL_VOLUME_INFO* VolumeInfo)
|
||||
{
|
||||
const auto userInfo = api.accountInfo(this->token, this->token_type);
|
||||
if (userInfo.has_value()) {
|
||||
const auto &info = userInfo.value();
|
||||
if (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)));
|
||||
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
return STATUS_END_OF_MEDIA;
|
||||
}
|
||||
|
||||
NTSTATUS SeafileFileSystem::Open(PWSTR FileName, UINT32 CreateOptions, UINT32 GrantedAccess, PVOID *PFileContext,
|
||||
FSP_FSCTL_FILE_INFO *FileInfo) {
|
||||
return STATUS_NOT_IMPLEMENTED;
|
||||
void SeafileFileSystem::Cleanup(PVOID FileContext, PWSTR FileName, ULONG Flags)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
NTSTATUS SeafileFileSystem::Read(PVOID FileContext, PVOID Buffer, UINT64 Offset, ULONG Length, PULONG PBytesTransferred) {
|
||||
return STATUS_NOT_IMPLEMENTED;
|
||||
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;
|
||||
}
|
||||
|
||||
if (sub_path == "/")
|
||||
{
|
||||
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, ""};
|
||||
*PFileContext = ctx;
|
||||
SetFileInfo(FileInfo, FILE_ATTRIBUTE_DIRECTORY, repo->size, repo->mtime);
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
if (expect_dir)
|
||||
{
|
||||
auto entries = api.repoDir(repo_id, sub_path, token, token_type);
|
||||
if (entries.has_value())
|
||||
{
|
||||
auto ctx = new SeaFileContext{repo_id, sub_path, 0, ""};
|
||||
*PFileContext = ctx;
|
||||
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())
|
||||
{
|
||||
auto ctx = new SeaFileContext{repo_id, sub_path, fdetail->size, ""};
|
||||
*PFileContext = ctx;
|
||||
SetFileInfo(FileInfo, FILE_ATTRIBUTE_NORMAL, fdetail->size, fdetail->mtime);
|
||||
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;
|
||||
}
|
||||
|
||||
NTSTATUS SeafileFileSystem::ReadDirectory(PVOID FileContext0, PWSTR Pattern, PWSTR Marker, PVOID Buffer,
|
||||
ULONG BufferLength, PULONG PBytesTransferred) {
|
||||
return STATUS_NOT_IMPLEMENTED;
|
||||
NTSTATUS SeafileFileSystem::Read(PVOID FileContext, PVOID Buffer, UINT64 Offset, ULONG Length,
|
||||
PULONG PBytesTransferred)
|
||||
{
|
||||
const auto fc = static_cast<SeaFileContext*>(FileContext);
|
||||
auto result = api.downloadFile(
|
||||
fc->repo_id, fc->path,
|
||||
Offset, Offset + Length - 1,
|
||||
token, token_type);
|
||||
if (result.has_value())
|
||||
{
|
||||
const auto& data = result.value();
|
||||
const auto copy_len = (std::min)(data.size(), static_cast<size_t>(Length));
|
||||
memcpy(Buffer, data.data(), copy_len);
|
||||
*PBytesTransferred = static_cast<ULONG>(copy_len);
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
return STATUS_UNSUCCESSFUL;
|
||||
}
|
||||
|
||||
NTSTATUS SeafileFileSystem::SetVolumeLabel_(PWSTR VolumeLabel, FSP_FSCTL_VOLUME_INFO *VolumeInfo) {
|
||||
return STATUS_NOT_IMPLEMENTED;
|
||||
NTSTATUS SeafileFileSystem::ReadDirectory(PVOID FileContext, PWSTR Pattern, PWSTR Marker,
|
||||
PVOID Buffer, ULONG BufferLength, PULONG PBytesTransferred)
|
||||
{
|
||||
const auto fc = static_cast<SeaFileContext*>(FileContext);
|
||||
*PBytesTransferred = 0;
|
||||
|
||||
auto add_entry = [&](const std::wstring& name, UINT32 attrs, uint64_t size, int64_t mtime) -> bool
|
||||
{
|
||||
if (Marker && wcscmp(name.c_str(), Marker) <= 0)
|
||||
return true;
|
||||
|
||||
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;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
NTSTATUS SeafileFileSystem::GetFileInfo(HANDLE Handle, FSP_FSCTL_FILE_INFO *FileInfo) {
|
||||
return STATUS_NOT_IMPLEMENTED;
|
||||
NTSTATUS SeafileFileSystem::GetFileInfo(HANDLE Handle, FSP_FSCTL_FILE_INFO* FileInfo)
|
||||
{
|
||||
auto fc = static_cast<SeaFileContext*>(Handle);
|
||||
if (fc->repo_id.empty() || fc->path == "/" || fc->path.back() == '/')
|
||||
{
|
||||
if (fc->repo_id.empty())
|
||||
{
|
||||
SetFileInfo(FileInfo, FILE_ATTRIBUTE_DIRECTORY, 0, 0);
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
auto repo = api.repoInfo(fc->repo_id, token, token_type);
|
||||
if (!repo.has_value())
|
||||
return STATUS_UNSUCCESSFUL;
|
||||
SetFileInfo(FileInfo, FILE_ATTRIBUTE_DIRECTORY, repo->size, repo->mtime);
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
auto fdetail = api.fileDetail(fc->repo_id, fc->path, token, token_type);
|
||||
if (!fdetail.has_value())
|
||||
return STATUS_UNSUCCESSFUL;
|
||||
SetFileInfo(FileInfo, FILE_ATTRIBUTE_NORMAL, fdetail->size, fdetail->mtime);
|
||||
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)
|
||||
{
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
NTSTATUS SeafileFileSystem::GetDirInfoByName(PVOID FileContext, PWSTR FileName,
|
||||
FSP_FSCTL_DIR_INFO* DirInfo)
|
||||
{
|
||||
auto fc = static_cast<SeaFileContext*>(FileContext);
|
||||
std::wstring name(FileName);
|
||||
|
||||
if (name == L"." || name == L"..")
|
||||
{
|
||||
memset(DirInfo, 0, sizeof(FSP_FSCTL_DIR_INFO));
|
||||
DirInfo->Size = sizeof(FSP_FSCTL_DIR_INFO) + (wcslen(FileName) + 1) * sizeof(WCHAR);
|
||||
SetFileInfo(&DirInfo->FileInfo, FILE_ATTRIBUTE_DIRECTORY, 0, 0);
|
||||
wcscpy_s(DirInfo->FileNameBuf, wcslen(FileName) + 1, FileName);
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
std::string utf8_name;
|
||||
for (const auto c : name)
|
||||
utf8_name += static_cast<char>(c);
|
||||
|
||||
if (fc->repo_id.empty())
|
||||
{
|
||||
return STATUS_OBJECT_NAME_NOT_FOUND;
|
||||
}
|
||||
|
||||
auto entries = api.repoDir(fc->repo_id, fc->path, token, token_type);
|
||||
if (!entries.has_value())
|
||||
return STATUS_OBJECT_NAME_NOT_FOUND;
|
||||
|
||||
for (const auto& entry : entries.value())
|
||||
{
|
||||
if (entry.name != utf8_name)
|
||||
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);
|
||||
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);
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
return STATUS_OBJECT_NAME_NOT_FOUND;
|
||||
}
|
||||
|
||||
NTSTATUS SeafileFileSystem::SetVolumeLabel_(PWSTR VolumeLabel, FSP_FSCTL_VOLUME_INFO* VolumeInfo)
|
||||
{
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user