From c451a077b2f7791b62dcca5bb3795094a9cb44d2 Mon Sep 17 00:00:00 2001 From: shell Date: Fri, 31 Jul 2026 07:18:23 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B8=85=E7=90=86=E6=97=A7=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E6=93=8D=E4=BD=9C=E6=97=A0=E6=95=88=E9=97=AE=E9=A2=98=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 2 ++ include/seafile/fs/setting.hpp | 5 +++ include/seafile/literals/units.hpp | 26 ++++++++++++++ src/seafile/fs/seafile_file_system.cpp | 2 ++ src/seafile/fs/setting.cpp | 25 +++++++++++++ src/seafile/fs/winfsp_adapter.cpp | 22 +++--------- src/util/string_util.cpp | 50 +++++++++++--------------- 7 files changed, 84 insertions(+), 48 deletions(-) create mode 100644 include/seafile/literals/units.hpp create mode 100644 src/seafile/fs/setting.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index a76a9e0..4f4598f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -65,6 +65,8 @@ add_executable(seadisk src/main.cpp include/seafile/fs/storage/repo_info.hpp src/seafile/fs/storage/repo_info.cpp include/seafile/fs/setting.hpp + src/seafile/fs/setting.cpp + include/seafile/literals/units.hpp ) target_compile_definitions(seadisk PRIVATE diff --git a/include/seafile/fs/setting.hpp b/include/seafile/fs/setting.hpp index 1a8fe40..5de20d6 100644 --- a/include/seafile/fs/setting.hpp +++ b/include/seafile/fs/setting.hpp @@ -6,6 +6,7 @@ #define SEADISK_SETTING_HPP #include #include +#include namespace seafile::fs { @@ -18,6 +19,10 @@ namespace seafile::fs std::string mount_label; NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(Setting, server, user, password, mount_point, mount_label); + + static auto load(const std::string& path) -> tl::expected; + + static auto load(const std::filesystem::path& path) -> tl::expected; }; } #endif //SEADISK_SETTING_HPP diff --git a/include/seafile/literals/units.hpp b/include/seafile/literals/units.hpp new file mode 100644 index 0000000..ebb6acf --- /dev/null +++ b/include/seafile/literals/units.hpp @@ -0,0 +1,26 @@ +// +// Created by Shell on 2026/7/30. +// + +#ifndef SEADISK_UNITS_HPP +#define SEADISK_UNITS_HPP +#include + +namespace seafile::literals { + constexpr std::size_t operator"" _B(unsigned long long v) { + return v; + } + + constexpr std::size_t operator"" _KB(unsigned long long v) { + return v * 1024ULL; + } + + constexpr std::size_t operator"" _MB(unsigned long long v) { + return v * 1024ULL * 1024ULL; + } + + constexpr std::size_t operator"" _GB(unsigned long long v) { + return v * 1024ULL * 1024ULL * 1024ULL; + } +} +#endif //SEADISK_UNITS_HPP diff --git a/src/seafile/fs/seafile_file_system.cpp b/src/seafile/fs/seafile_file_system.cpp index 457b55d..5d7b2c9 100644 --- a/src/seafile/fs/seafile_file_system.cpp +++ b/src/seafile/fs/seafile_file_system.cpp @@ -7,6 +7,8 @@ namespace seafile::fs { + using RepoInfo = storage::RepoInfo; + using RepoEntry = storage::RepoEntry; namespace { constexpr int64_t UNIX_EPOCH_AS_FILETIME = 116444736000000000LL; diff --git a/src/seafile/fs/setting.cpp b/src/seafile/fs/setting.cpp new file mode 100644 index 0000000..5de47e0 --- /dev/null +++ b/src/seafile/fs/setting.cpp @@ -0,0 +1,25 @@ +// +// Created by Shell on 2026/7/30. +// +#include "seafile/fs/setting.hpp" +#include + +namespace seafile::fs { + auto Setting::load(const std::filesystem::path &path) -> tl::expected { + try{ + const auto size = std::filesystem::file_size(path); + std::string data(size, '\0'); + std::ifstream file(path, std::ios::binary); + file.read(data.data(), size); + return nlohmann::json::parse(data).get(); + } + catch (const std::exception& ex) + { + return tl::make_unexpected(ex); + } + } + + auto Setting::load(const std::string &path) -> tl::expected { + return load(std::filesystem::path(path)); + } +} diff --git a/src/seafile/fs/winfsp_adapter.cpp b/src/seafile/fs/winfsp_adapter.cpp index be42179..1c8d40a 100644 --- a/src/seafile/fs/winfsp_adapter.cpp +++ b/src/seafile/fs/winfsp_adapter.cpp @@ -12,27 +12,13 @@ #include "seafile/fs/setting.hpp" #include "util/application_path.hpp" #include "util/string_util.hpp" +#include "seafile/literals/units.hpp" namespace seafile::fs { + using namespace seafile::literals; namespace { - tl::expected readSetting() - { - try - { - const auto path = shell::util::ApplicationPath::get().parent_path() / "seafile.json"; - const auto size = std::filesystem::file_size(path); - std::string data(size, '\0'); - std::ifstream file(path, std::ios::binary); - file.read(data.data(), size); - return nlohmann::json::parse(data).get(); - } - catch (const std::exception& ex) - { - return tl::make_unexpected(ex); - } - } FSP_FILE_SYSTEM_INTERFACE FsInterface = { .GetVolumeInfo = WinFSPAdapter::GetVolumeInfo, @@ -62,7 +48,7 @@ namespace seafile::fs GetSystemTimeAsFileTime(&CreationTime); - const auto setting = readSetting(); + const auto setting = Setting::load(shell::util::ApplicationPath::get().parent_path() / "seafile.json"); if (!setting.has_value()) { spdlog::error(std::format("read setting failed: {}", setting.error().what())); @@ -70,7 +56,7 @@ namespace seafile::fs } - VolumeParams.SectorSize = 4096; + VolumeParams.SectorSize = 4_KB; VolumeParams.SectorsPerAllocationUnit = 1; VolumeParams.MaxComponentLength = 255; VolumeParams.VolumeCreationTime = reinterpret_cast(&CreationTime)->QuadPart; diff --git a/src/util/string_util.cpp b/src/util/string_util.cpp index bdde9b4..b10001b 100644 --- a/src/util/string_util.cpp +++ b/src/util/string_util.cpp @@ -9,22 +9,19 @@ #include #include #include +#include #include "util/icu_raii.hpp" -namespace shell::util -{ - std::vector StringUtil::split(const std::string& s, const std::string& delim) - { - if (delim.empty()) - { +namespace shell::util { + std::vector StringUtil::split(const std::string &s, const std::string &delim) { + if (delim.empty()) { return {s}; } std::vector result; 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)); start = pos + delim.length(); } @@ -33,42 +30,36 @@ namespace shell::util 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); } - std::string StringUtil::detect_encoding(const std::string& data) - { + std::string StringUtil::detect_encoding(const std::string &data) { UErrorCode status = U_ZERO_ERROR; 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))); } // 设置待检测数据 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))); } // 检测 - const UCharsetMatch* match = ucsdet_detect(detector.get(), &status); + const UCharsetMatch *match = ucsdet_detect(detector.get(), &status); std::string encoding; - if (match && U_SUCCESS(status)) - { + if (match && U_SUCCESS(status)) { encoding = ucsdet_getName(match, &status); } 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; // 先获取输出长度 @@ -90,8 +81,7 @@ namespace shell::util &status ); - if (U_FAILURE(status)) - { + if (U_FAILURE(status)) { throw std::runtime_error(std::format("convert from {} to {} failed:{}", from, to, u_errorName(status))); } @@ -100,19 +90,19 @@ 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); - return std::wstring(unicode.getTerminatedBuffer()); + return std::wstring{ + reinterpret_cast(unicode.getBuffer()), + static_cast(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()); } - 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); std::string utf8; return unicode.toUTF8String(utf8);