添加文件缓存
This commit is contained in:
1 parent
e8b91b1a59
commit
133424f5f8
7 files changed
+169
-34
No files matched your search
@@ -63,6 +63,7 @@ add_executable(seadisk src/main.cpp
|
||||
include/seafile/fs/seafile_store.hpp
|
||||
src/seafile/fs/seafile_store.cpp
|
||||
include/seafile/fs/storage/repo_entry.hpp
|
||||
src/seafile/fs/storage/repo_entry.cpp
|
||||
)
|
||||
|
||||
target_compile_definitions(seadisk PRIVATE
|
||||
|
||||
@@ -7,8 +7,8 @@
|
||||
#include <string>
|
||||
#include <winfsp/winfsp.h>
|
||||
|
||||
#include "../api/seafile_api.hpp"
|
||||
#include "../api/token_type.hpp"
|
||||
#include "seafile/api/seafile_api.hpp"
|
||||
#include "seafile/api/token_type.hpp"
|
||||
#include "seafile/api/cached_seafile_api.hpp"
|
||||
#include "seafile/cache/cache.hpp"
|
||||
|
||||
|
||||
@@ -9,38 +9,43 @@
|
||||
|
||||
#include "storage/repo_entry.hpp"
|
||||
|
||||
namespace seafile::fs {
|
||||
|
||||
|
||||
class SeafileStore final {
|
||||
namespace seafile::fs
|
||||
{
|
||||
class SeafileStore final
|
||||
{
|
||||
public:
|
||||
|
||||
/// construction
|
||||
/// @param db_path database file path
|
||||
/// @param server the server url of seafile
|
||||
/// @param user user
|
||||
/// @param password password
|
||||
SeafileStore(const std::string &db_path, const std::string &server, const std::string &user,
|
||||
const std::string &password);
|
||||
SeafileStore(const std::string& db_path, const std::string& server, const std::string& user,
|
||||
const std::string& password);
|
||||
|
||||
SeafileStore(const SeafileStore &) = default;
|
||||
SeafileStore(const SeafileStore&) = delete;
|
||||
|
||||
tl::expected<void,api::SeafileException> auth();
|
||||
tl::expected<void, api::SeafileException> auth();
|
||||
|
||||
[[nodiscard]] tl::expected<std::string, api::SeafileException> defaultRepoId() const;
|
||||
|
||||
///初始化数据库信息
|
||||
[[nodiscard]] tl::expected<void, api::SeafileException> init() const;
|
||||
|
||||
/// 检查是否已经更新到最新
|
||||
/// @return true 已经最新,false 需要更新
|
||||
tl::expected<bool, api::SeafileException> checkIsUpdated(const std::string& repo_id);
|
||||
|
||||
/// 检查是否需要更新元素据,如果需要同步则自动同步
|
||||
tl::expected<void, api::SeafileException> checkAndSync(const std::string& repo_id,);
|
||||
tl::expected<void, api::SeafileException> checkAndSync(const std::string& repo_id);
|
||||
|
||||
/// 同步元数据
|
||||
tl::expected<void, api::SeafileException> sync(const std::string& repo_id);
|
||||
[[nodiscard]] tl::expected<void, api::SeafileException> sync(const std::string& repo_id) const;
|
||||
|
||||
/// 获取路径信息
|
||||
/// @param path 路径
|
||||
/// @return 路径信息
|
||||
tl::expected<storage::RepoEntry, api::SeafileException> findRepoEntry(const std::string& repo_id, const std::string &path);
|
||||
tl::expected<storage::RepoEntry, api::SeafileException> findRepoEntry(
|
||||
const std::string& repo_id, const std::string& path);
|
||||
|
||||
private:
|
||||
struct Database;
|
||||
|
||||
@@ -7,8 +7,10 @@
|
||||
#include "seafile/api/repo_entry.hpp"
|
||||
#include <string>
|
||||
|
||||
namespace seafile::fs::storage {
|
||||
struct RepoEntry final {
|
||||
namespace seafile::fs::storage
|
||||
{
|
||||
struct RepoEntry final
|
||||
{
|
||||
///repo id
|
||||
std::string repo_id;
|
||||
|
||||
@@ -56,6 +58,9 @@ namespace seafile::fs::storage {
|
||||
|
||||
/// Whether the entry is starred by the current user.
|
||||
std::optional<bool> starred{};
|
||||
|
||||
static auto from_api_model(const std::string& repo_id, const std::string& path,
|
||||
const api::RepoEntry& entry) -> RepoEntry;
|
||||
};
|
||||
}
|
||||
#endif //SEADISK_STORAGE_REPO_ENTRY_HPP
|
||||
@@ -1,6 +1,5 @@
|
||||
#include <winfsp/winfsp.h>
|
||||
#include "seafile/fs/winfsp_adapter.hpp"
|
||||
#include "seafile/api/seafile_api.hpp"
|
||||
|
||||
using namespace seafile::fs;
|
||||
|
||||
|
||||
@@ -6,17 +6,21 @@
|
||||
#include "seafile/api/account_info.hpp"
|
||||
#include "seafile/api/repo_info.hpp"
|
||||
#include "seafile/fs/storage/repo_entry.hpp"
|
||||
#include <stack>
|
||||
|
||||
//
|
||||
// Created by Shell on 2026/7/16.
|
||||
//
|
||||
namespace seafile::fs {
|
||||
namespace seafile::fs
|
||||
{
|
||||
using RepoEntry = storage::RepoEntry;
|
||||
using AccountInfo = api::AccountInfo;
|
||||
using RepoInfo = api::RepoInfo;
|
||||
|
||||
namespace {
|
||||
auto make_storage(const std::string &path) {
|
||||
namespace
|
||||
{
|
||||
auto make_storage(const std::string& path)
|
||||
{
|
||||
// clang-format off
|
||||
return sqlite_orm::make_storage(path,
|
||||
sqlite_orm::make_table("account_info",
|
||||
@@ -75,17 +79,34 @@ namespace seafile::fs {
|
||||
}
|
||||
}
|
||||
|
||||
struct SeafileStore::Database {
|
||||
struct SeafileStore::Database
|
||||
{
|
||||
using Storage = decltype(make_storage(""));
|
||||
Storage storage;
|
||||
|
||||
explicit Database(const std::string &path)
|
||||
: storage(make_storage(path)) {
|
||||
explicit Database(const std::string& path)
|
||||
: storage(make_storage(path))
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
SeafileStore::SeafileStore(const std::string &db_path, const std::string &server, const std::string &user,
|
||||
const std::string &password) : api(server) {
|
||||
tl::expected<void, api::SeafileException> SeafileStore::auth()
|
||||
{
|
||||
if (const auto auth_info = this->api.authToken(this->user, this->password))
|
||||
{
|
||||
this->token = auth_info->token;
|
||||
this->token_type = api::TokenType::TOKEN;
|
||||
return {};
|
||||
}
|
||||
else
|
||||
{
|
||||
return tl::make_unexpected(api::SeafileException(auth_info.error()));
|
||||
}
|
||||
}
|
||||
|
||||
SeafileStore::SeafileStore(const std::string& db_path, const std::string& server, const std::string& user,
|
||||
const std::string& password) : api(server)
|
||||
{
|
||||
this->database = std::make_unique<Database>(db_path);
|
||||
this->user = user;
|
||||
this->password = password;
|
||||
@@ -93,8 +114,9 @@ namespace seafile::fs {
|
||||
}
|
||||
|
||||
tl::expected<RepoEntry, api::SeafileException> SeafileStore::findRepoEntry(
|
||||
const std::string &repo_id, const std::string &path) {
|
||||
auto &storage = this->database->storage;
|
||||
const std::string& repo_id, const std::string& path)
|
||||
{
|
||||
auto& storage = this->database->storage;
|
||||
const auto entry = storage.get_optional<RepoEntry>(
|
||||
sqlite_orm::where(
|
||||
sqlite_orm::and_(
|
||||
@@ -102,27 +124,99 @@ namespace seafile::fs {
|
||||
sqlite_orm::c(&RepoEntry::repo_id) == repo_id
|
||||
)
|
||||
));
|
||||
if (entry) {
|
||||
if (entry)
|
||||
{
|
||||
return *entry;
|
||||
}
|
||||
return tl::make_unexpected(api::SeafileException("entry not exists"));
|
||||
}
|
||||
|
||||
tl::expected<bool, api::SeafileException> SeafileStore::checkIsUpdated(const std::string &repo_id) {
|
||||
auto &storage = this->database->storage;
|
||||
tl::expected<std::string,api::SeafileException> SeafileStore::defaultRepoId() const
|
||||
{
|
||||
const auto repo = this->api.defaultRepo(this->token,this->token_type);
|
||||
if (repo)
|
||||
{
|
||||
if (repo->exists)
|
||||
{
|
||||
return repo->repo_id.value();
|
||||
}
|
||||
return tl::make_unexpected(api::SeafileException("No default repo"));
|
||||
}
|
||||
return tl::make_unexpected(api::SeafileException(repo.error()));
|
||||
}
|
||||
|
||||
tl::expected<bool, api::SeafileException> SeafileStore::checkIsUpdated(const std::string& repo_id)
|
||||
{
|
||||
auto& storage = this->database->storage;
|
||||
const auto repo = storage.get_optional<RepoInfo>(
|
||||
sqlite_orm::where(
|
||||
sqlite_orm::c(&RepoInfo::id) == repo_id
|
||||
));
|
||||
if (!repo) {
|
||||
return true;
|
||||
if (!repo)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
const auto& commit_id = repo.value().head_commit_id;
|
||||
const auto repo_info = this->api.repoInfo(repo_id,this->token,this->token_type);
|
||||
if (repo_info) {
|
||||
const auto repo_info = this->api.repoInfo(repo_id, this->token, this->token_type);
|
||||
if (repo_info)
|
||||
{
|
||||
return repo_info.value().head_commit_id == commit_id;
|
||||
}
|
||||
|
||||
return tl::make_unexpected(repo_info.error());
|
||||
}
|
||||
|
||||
tl::expected<void, api::SeafileException> SeafileStore::sync(const std::string& repo_id) const
|
||||
{
|
||||
std::stack<RepoEntry> entry_stack;
|
||||
std::string path = "/";
|
||||
std::vector<RepoEntry> data;
|
||||
|
||||
entry_stack.push(RepoEntry{.repo_id = repo_id, .path = path});
|
||||
|
||||
while (!entry_stack.empty())
|
||||
{
|
||||
const auto parent = entry_stack.top();
|
||||
entry_stack.pop();
|
||||
path = parent.path;
|
||||
auto entries = this->api.repoDir(repo_id, path, this->token, this->token_type);
|
||||
if (!entries)
|
||||
{
|
||||
return tl::make_unexpected(entries.error());
|
||||
}
|
||||
for (auto& entry : *entries)
|
||||
{
|
||||
const auto entry_info = RepoEntry::from_api_model(repo_id, std::format("{}/{}", path, entry.name),
|
||||
entry);
|
||||
if (entry_info.type == "dir")
|
||||
{
|
||||
entry_stack.push(entry_info);
|
||||
}
|
||||
data.push_back(entry_info);
|
||||
}
|
||||
}try
|
||||
{
|
||||
this->database->storage.transaction([&]
|
||||
{
|
||||
for (const auto& entry : data)
|
||||
{
|
||||
this->database->storage.insert(entry);
|
||||
}
|
||||
return true;
|
||||
});
|
||||
}catch (std::exception& e)
|
||||
{
|
||||
return tl::make_unexpected(api::SeafileException(e.what()));
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
tl::expected<void, api::SeafileException> SeafileStore::checkAndSync(const std::string& repo_id)
|
||||
{
|
||||
if (!this->checkIsUpdated(repo_id))
|
||||
{
|
||||
return this->sync(repo_id);
|
||||
}
|
||||
return {};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
//
|
||||
// Created by Shell on 2026-07-17.
|
||||
//
|
||||
|
||||
#include "seafile/fs/storage/repo_entry.hpp"
|
||||
|
||||
namespace seafile::fs::storage
|
||||
{
|
||||
auto RepoEntry::from_api_model(const std::string& repo_id, const std::string& path,
|
||||
const api::RepoEntry& entry) -> RepoEntry
|
||||
{
|
||||
return RepoEntry{
|
||||
.repo_id = repo_id,
|
||||
.path = path,
|
||||
.type = entry.type,
|
||||
.name = entry.name,
|
||||
.id = entry.id,
|
||||
.mtime = entry.mtime,
|
||||
.modifier_email = entry.modifier_email,
|
||||
.size = entry.size,
|
||||
.is_locked = entry.is_locked,
|
||||
.lock_owner = entry.lock_owner,
|
||||
.lock_time = entry.lock_time,
|
||||
.locked_by_me = entry.locked_by_me,
|
||||
.permission = entry.permission,
|
||||
.modifier_contact_email = entry.modifier_contact_email,
|
||||
.modifier_name = entry.modifier_name,
|
||||
.starred = entry.starred
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user