From ea3d69a2ee0b18c3149384f4d538344a08fb5027 Mon Sep 17 00:00:00 2001 From: "Scott E. Graves" Date: Sat, 24 Aug 2024 16:13:28 -0500 Subject: [PATCH] updated build system --- .../librepertory/include/utils/file_utils.hpp | 2 -- .../librepertory/src/utils/file_utils.cpp | 18 ----------- support/include/utils/file.hpp | 26 +++++++++++++++ support/src/utils/file_directory.cpp | 5 +++ support/src/utils/file_enc_file.cpp | 3 ++ support/src/utils/file_file.cpp | 32 +++++++++++++++++-- support/src/utils/file_smb_directory.cpp | 24 ++++++++++++++ support/src/utils/file_smb_file.cpp | 24 ++++++++++++++ support/src/utils/file_thread_file.cpp | 3 ++ 9 files changed, 114 insertions(+), 23 deletions(-) diff --git a/repertory/librepertory/include/utils/file_utils.hpp b/repertory/librepertory/include/utils/file_utils.hpp index a2e183b2..0f975e69 100644 --- a/repertory/librepertory/include/utils/file_utils.hpp +++ b/repertory/librepertory/include/utils/file_utils.hpp @@ -48,8 +48,6 @@ get_total_drive_space(const std::string &path) -> std::uint64_t; is_modified_date_older_than(std::string_view path, const std::chrono::hours &hours) -> bool; -[[nodiscard]] auto move_file(std::string from, std::string to) -> bool; - [[nodiscard]] auto read_file_lines(const std::string &path) -> std::vector; diff --git a/repertory/librepertory/src/utils/file_utils.cpp b/repertory/librepertory/src/utils/file_utils.cpp index 01128adf..c32dfed8 100644 --- a/repertory/librepertory/src/utils/file_utils.cpp +++ b/repertory/librepertory/src/utils/file_utils.cpp @@ -248,24 +248,6 @@ auto is_modified_date_older_than(std::string_view path, utils::time::get_time_now(); } -auto move_file(std::string from, std::string to) -> bool { - from = utils::path::absolute(from); - to = utils::path::absolute(to); - - const auto directory = utils::path::get_parent_directory(to); - if (not utils::file::directory(directory).create_directory()) { - return false; - } - -#if defined(_WIN32) - const bool ret = ::MoveFile(from.c_str(), to.c_str()) != 0; -#else - const bool ret = (rename(from.c_str(), to.c_str()) == 0); -#endif - - return ret; -} - auto read_file_lines(const std::string &path) -> std::vector { std::vector ret; if (utils::file::file(path).exists()) { diff --git a/support/include/utils/file.hpp b/support/include/utils/file.hpp index 43080aca..ba20716b 100644 --- a/support/include/utils/file.hpp +++ b/support/include/utils/file.hpp @@ -86,6 +86,14 @@ struct i_fs_item { virtual ~i_fs_item() = default; + [[nodiscard]] virtual auto copy_to(std::string_view to_path, + bool overwrite) const -> bool = 0; + + [[nodiscard]] virtual auto copy_to(std::wstring_view new_path, + bool overwrite) -> bool { + return copy_to(utils::string::to_utf8(new_path), overwrite); + } + [[nodiscard]] virtual auto exists() const -> bool = 0; [[nodiscard]] virtual auto get_path() const -> std::string = 0; @@ -252,6 +260,9 @@ private: public: void close() override; + [[nodiscard]] auto copy_to(std::string_view new_path, + bool overwrite) const -> bool override; + [[nodiscard]] auto exists() const -> bool override; void flush() const override; @@ -338,6 +349,9 @@ private: public: void close() override; + [[nodiscard]] auto copy_to(std::string_view new_path, + bool overwrite) const -> bool override; + [[nodiscard]] auto exists() const -> bool override { return file_->exists(); } void flush() const override; @@ -450,6 +464,9 @@ private: public: void close() override; + [[nodiscard]] auto copy_to(std::string_view new_path, + bool overwrite) const -> bool override; + [[nodiscard]] auto exists() const -> bool override { return file_->exists(); } void flush() const override; @@ -578,6 +595,9 @@ private: std::string path_; public: + [[nodiscard]] auto copy_to(std::string_view new_path, + bool overwrite) const -> bool override; + [[nodiscard]] auto count(bool recursive = false) const -> std::uint64_t override; @@ -663,6 +683,9 @@ private: public: void close() override; + [[nodiscard]] auto copy_to(std::string_view new_path, + bool overwrite) const -> bool override; + [[nodiscard]] auto exists() const -> bool override; void flush() const override; @@ -786,6 +809,9 @@ public: [[nodiscard]] auto count(bool recursive = false) const -> std::uint64_t override; + [[nodiscard]] auto copy_to(std::string_view new_path, + bool overwrite) const -> bool override; + [[nodiscard]] auto create_directory(std::string_view path = "") const -> fs_directory_t override; diff --git a/support/src/utils/file_directory.cpp b/support/src/utils/file_directory.cpp index f0e41850..29ef0e0b 100644 --- a/support/src/utils/file_directory.cpp +++ b/support/src/utils/file_directory.cpp @@ -86,6 +86,11 @@ auto traverse_directory( } // namespace namespace repertory::utils::file { +auto directory::copy_to(std::string_view new_path, + bool overwrite) const -> bool { + return false; +} + auto directory::count(bool recursive) const -> std::uint64_t { static constexpr const std::string_view function_name{ static_cast(__FUNCTION__), diff --git a/support/src/utils/file_enc_file.cpp b/support/src/utils/file_enc_file.cpp index a0f8f901..2ff40ac4 100644 --- a/support/src/utils/file_enc_file.cpp +++ b/support/src/utils/file_enc_file.cpp @@ -30,6 +30,9 @@ enc_file::enc_file(fs_file_t file) : file_(std::move(file)) {} void enc_file::close() {} +auto enc_file::copy_to(std::string_view new_path, + bool overwrite) const -> bool {} + void enc_file::flush() const {} auto enc_file::move_to(std::string_view path) -> bool {} diff --git a/support/src/utils/file_file.cpp b/support/src/utils/file_file.cpp index 893d1d22..0baf98cd 100644 --- a/support/src/utils/file_file.cpp +++ b/support/src/utils/file_file.cpp @@ -23,11 +23,8 @@ #include "utils/collection.hpp" #include "utils/common.hpp" -#include "utils/encryption.hpp" #include "utils/error.hpp" #include "utils/path.hpp" -#include "utils/string.hpp" -#include "utils/time.hpp" namespace { [[nodiscard]] auto get_file_size(std::string_view path, @@ -190,6 +187,35 @@ void file::close() { file_.reset(); } +auto file::copy_to(std::string_view new_path, bool overwrite) const -> bool { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + try { + auto to_path = utils::path::absolute(new_path); + if (directory(to_path).exists()) { + return false; + } + +#if defined(_WIN32) + return ::CopyFileA(path_.c_str(), to_path.c_str(), + overwrite ? TRUE : FALSE); +#else // !defined(_WIN32) + return std::filesystem::copy_file( + path_, to_path, + overwrite ? std::filesystem::copy_options::overwrite_existing + : std::filesystem::copy_options::skip_existing); +#endif // defined(_WIN32) + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + return false; +} + auto file::exists() const -> bool { #if defined(_WIN32) recur_mutex_lock lock{*mtx_}; diff --git a/support/src/utils/file_smb_directory.cpp b/support/src/utils/file_smb_directory.cpp index 1add16c9..d9aa3185 100644 --- a/support/src/utils/file_smb_directory.cpp +++ b/support/src/utils/file_smb_directory.cpp @@ -102,6 +102,30 @@ auto smb_directory::open(std::wstring_view host, std::wstring_view user, utils::string::to_utf8(share_name)); } +auto smb_directory::copy_to(std::string_view new_path, + bool overwrite) const -> bool { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + try { + if (not session_) { + throw std::runtime_error("session not found|" + path_); + } + + // auto to_path = utils::path::absolute(new_path); + + throw std::runtime_error("failed to copy directory|" + path_ + '|' + + std::string{new_path} + "|not implemented"); + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + return false; +} + auto smb_directory::count(bool recursive) const -> std::uint64_t { static constexpr const std::string_view function_name{ static_cast(__FUNCTION__), diff --git a/support/src/utils/file_smb_file.cpp b/support/src/utils/file_smb_file.cpp index 54ac70d5..59d4f503 100644 --- a/support/src/utils/file_smb_file.cpp +++ b/support/src/utils/file_smb_file.cpp @@ -35,6 +35,30 @@ void smb_file::close() { } } +auto smb_file::copy_to(std::string_view new_path, + bool overwrite) const -> bool { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + try { + if (not session_) { + throw std::runtime_error("session not found|" + path_); + } + + // auto to_path = utils::path::absolute(new_path); + + throw std::runtime_error("failed to copy file|" + path_ + '|' + + std::string{new_path} + "|not implemented"); + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + return false; +} + auto smb_file::exists() const -> bool { static constexpr const std::string_view function_name{ static_cast(__FUNCTION__), diff --git a/support/src/utils/file_thread_file.cpp b/support/src/utils/file_thread_file.cpp index e1ff8f45..7cc7eaa0 100644 --- a/support/src/utils/file_thread_file.cpp +++ b/support/src/utils/file_thread_file.cpp @@ -37,6 +37,9 @@ thread_file::thread_file(fs_file_t file) : file_(std::move(file)) {} void thread_file::close() {} +auto thread_file::copy_to(std::string_view new_path, + bool overwrite) const -> bool {} + void thread_file::flush() const {} auto thread_file::move_to(std::string_view path) -> bool {}