添加文件缓存

This commit is contained in:
asypost committed 2026-07-22 22:00:04 +08:00
1 parent f0c5de374e
commit f23c6a3fbb
4 files changed
+142 -49

No files matched your search

+1
View File
@@ -66,6 +66,7 @@ add_executable(seadisk src/main.cpp
src/seafile/fs/storage/repo_entry.cpp src/seafile/fs/storage/repo_entry.cpp
include/seafile/util/version.hpp include/seafile/util/version.hpp
src/seafile/util/version.cpp src/seafile/util/version.cpp
src/util/application_path.cpp
) )
target_compile_definitions(seadisk PRIVATE target_compile_definitions(seadisk PRIVATE
+3 -1
View File
@@ -24,6 +24,8 @@ namespace seafile::fs
SeafileStore(const SeafileStore&) = delete; SeafileStore(const SeafileStore&) = delete;
~SeafileStore();
tl::expected<void, api::SeafileException> auth(); tl::expected<void, api::SeafileException> auth();
[[nodiscard]] tl::expected<std::string, api::SeafileException> defaultRepoId() const; [[nodiscard]] tl::expected<std::string, api::SeafileException> defaultRepoId() const;
@@ -50,7 +52,7 @@ namespace seafile::fs
/// 同步 /// 同步
tl::expected<void, api::SeafileException> sync(); tl::expected<void, api::SeafileException> sync();
std::filesystem::path storePath() const; static std::filesystem::path storePath() ;
/// 获取路径信息 /// 获取路径信息
+8
View File
@@ -1,4 +1,6 @@
#include <winfsp/winfsp.h> #include <winfsp/winfsp.h>
#include "seafile/fs/seafile_store.hpp"
#include "seafile/fs/winfsp_adapter.hpp" #include "seafile/fs/winfsp_adapter.hpp"
using namespace seafile::fs; using namespace seafile::fs;
@@ -9,6 +11,12 @@ auto wmain(int argc, wchar_t** argv) -> int
return ERROR_DELAY_LOAD_FAILED; return ERROR_DELAY_LOAD_FAILED;
FspDebugLogSetHandle(GetStdHandle(STD_ERROR_HANDLE)); FspDebugLogSetHandle(GetStdHandle(STD_ERROR_HANDLE));
SeafileStore store{SeafileStore::storePath().string(),"https://pan.hanwangsoft.com","asypost@yeah.net","Shell0805@china"};
const auto result = store.init();
if (!result)
{
std::wcout << result.error().what() << std::endl;
}
return FspServiceRun(const_cast<PWSTR>(L"SeaDisk"), WinFSPAdapter::SvcStart, WinFSPAdapter::SvcStop, nullptr); return FspServiceRun(const_cast<PWSTR>(L"SeaDisk"), WinFSPAdapter::SvcStart, WinFSPAdapter::SvcStop, nullptr);
} }
+130 -48
View File
@@ -12,23 +12,26 @@
// //
// Created by Shell on 2026/7/16. // Created by Shell on 2026/7/16.
// //
namespace seafile::fs { namespace seafile::fs
{
using RepoEntry = storage::RepoEntry; using RepoEntry = storage::RepoEntry;
using AccountInfo = api::AccountInfo; using AccountInfo = api::AccountInfo;
using RepoInfo = api::RepoInfo; using RepoInfo = api::RepoInfo;
namespace { namespace
auto make_storage(const std::string &path) { {
auto make_storage(const std::string& path)
{
// clang-format off // clang-format off
return sqlite_orm::make_storage(path, return sqlite_orm::make_storage(path,
sqlite_orm::make_table("account_info", sqlite_orm::make_table("account_info",
sqlite_orm::make_column("space_usage", &AccountInfo::space_usage), sqlite_orm::make_column("space_usage", &AccountInfo::space_usage),
sqlite_orm::make_column("avatar_url", &AccountInfo::avatar_url), sqlite_orm::make_column("avatar_url", &AccountInfo::avatar_url),
sqlite_orm::make_column("email", &AccountInfo::email), sqlite_orm::make_column("email", &AccountInfo::email, sqlite_orm::primary_key()),
sqlite_orm::make_column("name", &AccountInfo::name), sqlite_orm::make_column("name", &AccountInfo::name),
sqlite_orm::make_column("total", &AccountInfo::total), sqlite_orm::make_column("total", &AccountInfo::total),
sqlite_orm::make_column("usage", &AccountInfo::usage), sqlite_orm::make_column("usage", &AccountInfo::usage),
sqlite_orm::make_column("login_id", &AccountInfo::login_id,sqlite_orm::primary_key()), sqlite_orm::make_column("login_id", &AccountInfo::login_id),
sqlite_orm::make_column("department", &AccountInfo::department), sqlite_orm::make_column("department", &AccountInfo::department),
sqlite_orm::make_column("contact_email", &AccountInfo::contact_email), sqlite_orm::make_column("contact_email", &AccountInfo::contact_email),
sqlite_orm::make_column("institution", &AccountInfo::institution), sqlite_orm::make_column("institution", &AccountInfo::institution),
@@ -38,7 +41,7 @@ namespace seafile::fs {
), ),
sqlite_orm::make_table("repo_info", sqlite_orm::make_table("repo_info",
sqlite_orm::make_column("type",&RepoInfo::type), sqlite_orm::make_column("type",&RepoInfo::type),
sqlite_orm::make_column("id",&RepoInfo::id), sqlite_orm::make_column("id",&RepoInfo::id, sqlite_orm::primary_key()),
sqlite_orm::make_column("owner",&RepoInfo::owner), sqlite_orm::make_column("owner",&RepoInfo::owner),
sqlite_orm::make_column("name",&RepoInfo::name), sqlite_orm::make_column("name",&RepoInfo::name),
sqlite_orm::make_column("mtime",&RepoInfo::mtime), sqlite_orm::make_column("mtime",&RepoInfo::mtime),
@@ -60,7 +63,7 @@ namespace seafile::fs {
sqlite_orm::make_column("type",&RepoEntry::type), sqlite_orm::make_column("type",&RepoEntry::type),
sqlite_orm::make_column("name",&RepoEntry::name), sqlite_orm::make_column("name",&RepoEntry::name),
sqlite_orm::make_column("path",&RepoEntry::path), sqlite_orm::make_column("path",&RepoEntry::path),
sqlite_orm::make_column("id",&RepoEntry::id), sqlite_orm::make_column("id", &RepoEntry::id, sqlite_orm::primary_key()),
sqlite_orm::make_column("mtime",&RepoEntry::mtime), sqlite_orm::make_column("mtime",&RepoEntry::mtime),
sqlite_orm::make_column("modifier_email",&RepoEntry::modifier_email), sqlite_orm::make_column("modifier_email",&RepoEntry::modifier_email),
sqlite_orm::make_column("size",&RepoEntry::size), sqlite_orm::make_column("size",&RepoEntry::size),
@@ -77,25 +80,32 @@ namespace seafile::fs {
} }
} }
struct SeafileStore::Database { struct SeafileStore::Database
{
using Storage = decltype(make_storage("")); using Storage = decltype(make_storage(""));
Storage storage; Storage storage;
explicit Database(const std::string &path) explicit Database(const std::string& path)
: storage(make_storage(path)) { : storage(make_storage(path))
{
} }
}; };
tl::expected<void, api::SeafileException> SeafileStore::auth() { tl::expected<void, api::SeafileException> SeafileStore::auth()
if (const auto auth_info = this->api.auth(this->username, this->password)) { {
if (const auto auth_info = this->api.auth(this->username, this->password))
{
return {}; return {};
} else { }
else
{
return tl::make_unexpected(api::SeafileException(auth_info.error())); return tl::make_unexpected(api::SeafileException(auth_info.error()));
} }
} }
SeafileStore::SeafileStore(const std::string &db_path, const std::string &server, const std::string &user, SeafileStore::SeafileStore(const std::string& db_path, const std::string& server, const std::string& user,
const std::string &password) : api(server) { const std::string& password) : api(server)
{
this->database = std::make_unique<Database>(db_path); this->database = std::make_unique<Database>(db_path);
this->username = user; this->username = user;
this->password = password; this->password = password;
@@ -103,8 +113,9 @@ 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)
auto &storage = this->database->storage; {
auto& storage = this->database->storage;
const auto entry = storage.get_optional<RepoEntry>( const auto entry = storage.get_optional<RepoEntry>(
sqlite_orm::where( sqlite_orm::where(
sqlite_orm::and_( sqlite_orm::and_(
@@ -112,16 +123,20 @@ namespace seafile::fs {
sqlite_orm::c(&RepoEntry::repo_id) == repo_id sqlite_orm::c(&RepoEntry::repo_id) == repo_id
) )
)); ));
if (entry) { if (entry)
{
return *entry; return *entry;
} }
return tl::make_unexpected(api::SeafileException("entry not exists")); return tl::make_unexpected(api::SeafileException("entry not exists"));
} }
tl::expected<std::string, api::SeafileException> SeafileStore::defaultRepoId() const { tl::expected<std::string, api::SeafileException> SeafileStore::defaultRepoId() const
{
const auto repo = this->api.defaultRepo(); const auto repo = this->api.defaultRepo();
if (repo) { if (repo)
if (repo->exists) { {
if (repo->exists)
{
return repo->repo_id.value(); return repo->repo_id.value();
} }
return tl::make_unexpected(api::SeafileException("No default repo")); return tl::make_unexpected(api::SeafileException("No default repo"));
@@ -129,90 +144,157 @@ namespace seafile::fs {
return tl::make_unexpected(api::SeafileException(repo.error())); return tl::make_unexpected(api::SeafileException(repo.error()));
} }
tl::expected<bool, api::SeafileException> SeafileStore::checkIsUpdated(const std::string &repo_id) { tl::expected<bool, api::SeafileException> SeafileStore::checkIsUpdated(const std::string& repo_id)
auto &storage = this->database->storage; {
auto& storage = this->database->storage;
const auto repo = storage.get_optional<RepoInfo>( const auto repo = storage.get_optional<RepoInfo>(
sqlite_orm::where( sqlite_orm::where(
sqlite_orm::c(&RepoInfo::id) == repo_id sqlite_orm::c(&RepoInfo::id) == repo_id
)); ));
if (!repo) { if (!repo)
{
return false; return false;
} }
const auto &commit_id = repo.value().head_commit_id; const auto& commit_id = repo.value().head_commit_id;
const auto repo_info = this->api.repoInfo(repo_id); const auto repo_info = this->api.repoInfo(repo_id);
if (repo_info) { if (repo_info)
{
return repo_info.value().head_commit_id == commit_id; return repo_info.value().head_commit_id == commit_id;
} }
return tl::make_unexpected(repo_info.error()); return tl::make_unexpected(repo_info.error());
} }
tl::expected<void, api::SeafileException> SeafileStore::syncRepoEntry(const std::string &repo_id) const { tl::expected<void, api::SeafileException> SeafileStore::syncRepoEntry(const std::string& repo_id) const
{
std::stack<RepoEntry> entry_stack; std::stack<RepoEntry> entry_stack;
std::string path = "/"; std::string path = "/";
std::vector<RepoEntry> data; std::vector<RepoEntry> data;
entry_stack.push(RepoEntry{.repo_id = repo_id, .path = path}); entry_stack.push(RepoEntry{.repo_id = repo_id, .path = path});
while (!entry_stack.empty()) { while (!entry_stack.empty())
{
const auto parent = entry_stack.top(); const auto parent = entry_stack.top();
entry_stack.pop(); entry_stack.pop();
path = parent.path; path = parent.path;
auto entries = this->api.repoDir(repo_id, path); auto entries = this->api.repoDir(repo_id, path);
if (!entries) { if (!entries)
{
return tl::make_unexpected(entries.error()); return tl::make_unexpected(entries.error());
} }
for (auto &entry: *entries) { for (auto& entry : *entries)
{
const auto entry_info = RepoEntry::from_api_model(repo_id, std::format("{}/{}", path, entry.name), const auto entry_info = RepoEntry::from_api_model(repo_id, std::format("{}/{}", path, entry.name),
entry); entry);
if (entry_info.type == "dir") { if (entry_info.type == "dir")
{
entry_stack.push(entry_info); entry_stack.push(entry_info);
} }
data.push_back(entry_info); data.push_back(entry_info);
} }
} }
try { try
this->database->storage.transaction([&] { {
for (const auto &entry: data) { this->database->storage.transaction([&]
this->database->storage.insert(entry); {
for (const auto& entry : data)
{
this->database->storage.replace(entry);
} }
return true; return true;
}); });
} catch (std::exception &e) { }
catch (std::exception& e)
{
return tl::make_unexpected(api::SeafileException(e.what())); return tl::make_unexpected(api::SeafileException(e.what()));
} }
return {}; return {};
} }
tl::expected<void, api::SeafileException> SeafileStore::checkAndSync(const std::string &repo_id) { tl::expected<void, api::SeafileException> SeafileStore::checkAndSync(const std::string& repo_id)
if (!this->checkIsUpdated(repo_id)) { {
if (!this->checkIsUpdated(repo_id))
{
return this->syncRepoEntry(repo_id); return this->syncRepoEntry(repo_id);
} }
return {}; return {};
} }
tl::expected<void, api::SeafileException> SeafileStore::init() { tl::expected<void, api::SeafileException> SeafileStore::init()
if (const auto &store_path = this->storePath(); !std::filesystem::exists(store_path)) { {
this->database->storage.sync_schema(); if (const auto& store_path = storePath(); !std::filesystem::exists(store_path))
return this->sync(); {
try
{
this->database->storage.sync_schema();
}
catch (std::exception& e)
{
return tl::make_unexpected(api::SeafileException(e.what()));
}
} }
return {}; const auto auth_token = this->auth();
if (!auth_token)
{
return tl::make_unexpected(auth_token.error());
}
return this->sync();;
} }
tl::expected<void, api::SeafileException> SeafileStore::syncUserInfo() { tl::expected<void, api::SeafileException> SeafileStore::syncUserInfo()
{
const auto user_info = this->api.accountInfo(); const auto user_info = this->api.accountInfo();
if (!user_info) { if (!user_info)
{
return tl::make_unexpected(user_info.error()); return tl::make_unexpected(user_info.error());
} }
this->database->storage.replace(*user_info); this->database->storage.replace(*user_info);
return {}; return {};
} }
tl::expected<void, api::SeafileException> SeafileStore::syncRepos() { tl::expected<void, api::SeafileException> SeafileStore::syncRepos()
{
const auto repos = this->api.repos("mine", "");
if (!repos)
{
return tl::make_unexpected(repos.error());
}
for (const auto& repo : *repos)
{
try
{
this->database->storage.replace(repo);
}
catch (std::exception& e)
{
return tl::make_unexpected(api::SeafileException(e.what()));
}
if (const auto sync_repo = syncRepoEntry(repo.id); !sync_repo)
{
return tl::make_unexpected(sync_repo.error());
}
}
return {}; return {};
} }
std::filesystem::path SeafileStore::storePath() const { tl::expected<void, api::SeafileException> SeafileStore::sync()
return shell::util::ApplicationPath::get() / "seafile.sqlite"; {
if (const auto sync_account = this->syncUserInfo(); !sync_account)
{
return tl::make_unexpected(sync_account.error());
}
if (const auto sync_repos = syncRepos(); !sync_repos)
{
return tl::make_unexpected(sync_repos.error());
}
return {};
}
SeafileStore::~SeafileStore() = default;
std::filesystem::path SeafileStore::storePath()
{
return shell::util::ApplicationPath::get().parent_path() / "seafile.sqlite";
} }
} }