From 82b3efff0c1a735dc4e8a691d479df1685025969 Mon Sep 17 00:00:00 2001 From: "Scott E. Graves" Date: Wed, 28 Aug 2024 11:00:44 -0500 Subject: [PATCH] updated build system --- cmake/versions.cmake | 12 +- .../providers/encrypt/encrypt_provider.hpp | 7 +- .../librepertory/include/types/repertory.hpp | 62 ++++---- .../src/drives/directory_cache.cpp | 2 +- .../src/drives/fuse/fuse_base.cpp | 8 +- .../src/drives/fuse/fuse_drive.cpp | 2 +- .../src/drives/fuse/fuse_drive_base.cpp | 6 +- .../drives/remote/remote_open_file_table.cpp | 14 +- .../src/drives/winfsp/winfsp_drive.cpp | 2 +- .../src/file_manager/file_manager.cpp | 14 +- .../file_manager/file_manager_open_file.cpp | 2 +- .../file_manager_open_file_base.cpp | 2 +- .../file_manager_ring_buffer_open_file.cpp | 10 +- .../src/providers/base_provider.cpp | 12 +- .../providers/encrypt/encrypt_provider.cpp | 140 ++++++++++-------- .../librepertory/src/providers/meta_db.cpp | 2 +- .../src/providers/s3/s3_provider.cpp | 24 +-- .../src/providers/sia/sia_provider.cpp | 17 ++- .../librepertory/src/utils/file_utils.cpp | 2 +- support/include/utils/config.hpp | 5 - support/include/utils/file.hpp | 29 +++- support/include/utils/path.hpp | 38 ++++- support/src/utils/file_directory.cpp | 16 ++ support/src/utils/file_file.cpp | 16 ++ support/src/utils/file_smb_directory.cpp | 19 +++ support/src/utils/file_smb_file.cpp | 19 +++ support/src/utils/path.cpp | 40 ++++- support/test/src/test.cpp | 2 +- support/test/src/utils/path_test.cpp | 22 +-- 29 files changed, 358 insertions(+), 188 deletions(-) diff --git a/cmake/versions.cmake b/cmake/versions.cmake index cd3ac409..60142bfe 100644 --- a/cmake/versions.cmake +++ b/cmake/versions.cmake @@ -1,18 +1,18 @@ set(BINUTILS_VERSION 2.41) -set(BOOST2_MAJOR_VERSION 1) -set(BOOST2_MINOR_VERSION 76) -set(BOOST2_PATCH_VERSION 0) set(BOOST_MAJOR_VERSION 1) set(BOOST_MINOR_VERSION 85) set(BOOST_PATCH_VERSION 0) +set(BOOST2_MAJOR_VERSION 1) +set(BOOST2_MINOR_VERSION 76) +set(BOOST2_PATCH_VERSION 0) set(CLI11_VERSION 2.4.2) set(CPP_HTTPLIB_VERSION 0.16.3) -set(CURL2_VERSION 8_9_1) set(CURL_VERSION 8.9.1) +set(CURL2_VERSION 8_9_1) set(CXXOPTS_VERSION 3.2.1) set(DTL_VERSION 2.01) -set(EXPAT2_VERSION 2_6_2) set(EXPAT_VERSION 2.6.2) +set(EXPAT2_VERSION 2_6_2) set(FLAC_VERSION 1.4.3) set(FMT_VERSION 11.0.2) set(FONTCONFIG_VERSION 2.15.0) @@ -44,8 +44,8 @@ set(SDL_VERSION 2.30.6) set(SECP256K1_VERSION 0.1.0.20) set(SFML_VERSION 2.6.1) set(SPDLOG_VERSION 1.14.1) -set(SQLITE2_VERSION 3.46.1) set(SQLITE_VERSION 3460100) +set(SQLITE2_VERSION 3.46.1) set(STDUUID_VERSION 1.2.3) set(VLC_VERSION 3.0) set(VORBIS_VERSION 1.3.7) diff --git a/repertory/librepertory/include/providers/encrypt/encrypt_provider.hpp b/repertory/librepertory/include/providers/encrypt/encrypt_provider.hpp index 18351f57..fd9c9cc8 100644 --- a/repertory/librepertory/include/providers/encrypt/encrypt_provider.hpp +++ b/repertory/librepertory/include/providers/encrypt/encrypt_provider.hpp @@ -73,10 +73,9 @@ private: const std::string &source_path)> callback) const -> api_error; - auto - process_directory_entry(const std::filesystem::directory_entry &dir_entry, - const encrypt_config &cfg, - std::string &api_path) const -> bool; + auto process_directory_entry(const utils::file::i_fs_item &dir_entry, + const encrypt_config &cfg, + std::string &api_path) const -> bool; void remove_deleted_files(); diff --git a/repertory/librepertory/include/types/repertory.hpp b/repertory/librepertory/include/types/repertory.hpp index 45c38fc5..db917117 100644 --- a/repertory/librepertory/include/types/repertory.hpp +++ b/repertory/librepertory/include/types/repertory.hpp @@ -150,28 +150,28 @@ enum class provider_type : std::size_t { }; #if defined(_WIN32) -struct open_file_data { +struct open_file_data final { void *directory_buffer{}; }; #else using open_file_data = int; #endif -struct api_file { - std::string api_path; - std::string api_parent; +struct api_file final { + std::string api_path{}; + std::string api_parent{}; std::uint64_t accessed_date{}; std::uint64_t changed_date{}; std::uint64_t creation_date{}; std::uint64_t file_size{}; - std::string key; + std::string key{}; std::uint64_t modified_date{}; std::string source_path; }; -struct directory_item { - std::string api_path; - std::string api_parent; +struct directory_item final { + std::string api_path{}; + std::string api_parent{}; bool directory{false}; std::uint64_t size{}; api_meta_map meta{}; @@ -188,26 +188,30 @@ struct directory_item { } [[nodiscard]] auto to_json() const -> json { - return {{"path", api_path}, - {"parent", api_parent}, - {"size", size}, - {"directory", directory}, - {"meta", meta}}; + return { + {"path", api_path}, {"parent", api_parent}, {"size", size}, + {"directory", directory}, {"meta", meta}, + }; } }; -struct filesystem_item { - std::string api_path; - std::string api_parent; - bool directory{false}; - std::uint64_t size{}; - std::string source_path; +struct encrypt_config final { + std::string encryption_token{}; + std::string path{}; }; -struct host_config { - std::string agent_string; - std::string api_password; - std::string api_user; +struct filesystem_item final { + std::string api_path{}; + std::string api_parent{}; + bool directory{false}; + std::uint64_t size{}; + std::string source_path{}; +}; + +struct host_config final { + std::string agent_string{}; + std::string api_password{}; + std::string api_user{}; std::uint16_t api_port{}; std::string host_name_or_ip{"localhost"}; std::string path{}; @@ -263,15 +267,15 @@ from_json(const json &j, host_config &hc) { j.at("TimeoutMs").get_to(hc.timeout_ms); } -struct s3_config { - std::string access_key; - std::string bucket; +struct s3_config final { + std::string access_key{}; + std::string bucket{}; std::uint16_t cache_timeout_secs{60U}; - std::string encryption_token; + std::string encryption_token{}; std::string region{"any"}; - std::string secret_key; + std::string secret_key{}; std::uint32_t timeout_ms{60000U}; - std::string url; + std::string url{}; bool use_path_style{false}; bool use_region_in_url{false}; }; diff --git a/repertory/librepertory/src/drives/directory_cache.cpp b/repertory/librepertory/src/drives/directory_cache.cpp index 34ccbc41..025c2844 100644 --- a/repertory/librepertory/src/drives/directory_cache.cpp +++ b/repertory/librepertory/src/drives/directory_cache.cpp @@ -85,7 +85,7 @@ void directory_cache::service_function() { auto lookup = directory_lookup_; directory_lock.unlock(); - for (const auto &kv : lookup) { + for (auto &&kv : lookup) { if (std::chrono::duration_cast( std::chrono::system_clock::now() - kv.second.last_update) >= 120s) { directory_lock.lock(); diff --git a/repertory/librepertory/src/drives/fuse/fuse_base.cpp b/repertory/librepertory/src/drives/fuse/fuse_base.cpp index 3abb42b5..ef128bfa 100644 --- a/repertory/librepertory/src/drives/fuse/fuse_base.cpp +++ b/repertory/librepertory/src/drives/fuse/fuse_base.cpp @@ -437,7 +437,7 @@ auto fuse_base::mount(std::vector args) -> int { auto ret = parse_args(args); if (ret == 0) { std::vector fuse_argv(args.size()); - for (std::size_t i = 0u; i < args.size(); i++) { + for (std::size_t i = 0u; i < args.size(); ++i) { fuse_argv[i] = args[i].c_str(); } @@ -677,14 +677,14 @@ void fuse_base::notify_fuse_args_parsed(const std::vector &args) { auto fuse_base::parse_args(std::vector &args) -> int { auto force_no_console = false; - for (std::size_t i = 1u; !force_no_console && (i < args.size()); i++) { + for (std::size_t i = 1u; !force_no_console && (i < args.size()); ++i) { if (args[i] == "-nc") { force_no_console = true; } } utils::collection::remove_element(args, "-nc"); - for (std::size_t i = 1u; i < args.size(); i++) { + for (std::size_t i = 1u; i < args.size(); ++i) { if (args[i] == "-f") { console_enabled_ = not force_no_console; } else if (args[i].find("-o") == 0) { @@ -698,7 +698,7 @@ auto fuse_base::parse_args(std::vector &args) -> int { } const auto option_parts = utils::string::split(options, ',', true); - for (const auto &option : option_parts) { + for (auto &&option : option_parts) { if (option.find("gid") == 0) { const auto parts = utils::string::split(option, '=', true); if (parts.size() == 2u) { diff --git a/repertory/librepertory/src/drives/fuse/fuse_drive.cpp b/repertory/librepertory/src/drives/fuse/fuse_drive.cpp index 7e9db611..53993645 100644 --- a/repertory/librepertory/src/drives/fuse/fuse_drive.cpp +++ b/repertory/librepertory/src/drives/fuse/fuse_drive.cpp @@ -915,7 +915,7 @@ auto fuse_drive::listxattr_impl(std::string api_path, char *buffer, size_t size, api_meta_map meta; if ((res = provider_.get_item_meta(api_path, meta)) == api_error::success) { - for (const auto &meta_item : meta) { + for (auto &&meta_item : meta) { if (utils::collection::excludes(META_USED_NAMES, meta_item.first)) { auto attribute_name = meta_item.first; #if defined(__APPLE__) diff --git a/repertory/librepertory/src/drives/fuse/fuse_drive_base.cpp b/repertory/librepertory/src/drives/fuse/fuse_drive_base.cpp index fc98f2d8..8f6e806b 100644 --- a/repertory/librepertory/src/drives/fuse/fuse_drive_base.cpp +++ b/repertory/librepertory/src/drives/fuse/fuse_drive_base.cpp @@ -166,9 +166,9 @@ auto fuse_drive_base::check_parent_access(const std::string &api_path, // Ignore root if (api_path != "/") { if ((mask & X_OK) == X_OK) { - for (auto parent = utils::path::get_parent_directory(api_path); + for (auto parent = utils::path::get_parent_path(api_path); (ret == api_error::success) && not parent.empty(); - parent = utils::path::get_parent_directory(parent)) { + parent = utils::path::get_parent_path(parent)) { if (((ret = check_access(parent, X_OK)) == api_error::success) && (parent == "/")) { break; @@ -179,7 +179,7 @@ auto fuse_drive_base::check_parent_access(const std::string &api_path, if (ret == api_error::success) { mask &= (~X_OK); if (mask != 0) { - ret = check_access(utils::path::get_parent_directory(api_path), mask); + ret = check_access(utils::path::get_parent_path(api_path), mask); } } } diff --git a/repertory/librepertory/src/drives/remote/remote_open_file_table.cpp b/repertory/librepertory/src/drives/remote/remote_open_file_table.cpp index 1bbf7395..b4925dfc 100644 --- a/repertory/librepertory/src/drives/remote/remote_open_file_table.cpp +++ b/repertory/librepertory/src/drives/remote/remote_open_file_table.cpp @@ -39,14 +39,14 @@ void remote_open_file_table::add_directory(const std::string &client_id, void remote_open_file_table::close_all(const std::string &client_id) { std::vector compat_handles; unique_recur_mutex_lock compat_lock(compat_mutex_); - for (auto &kv : compat_lookup_) { + for (auto &&kv : compat_lookup_) { if (kv.second.client_id == client_id) { compat_handles.emplace_back(kv.first); } } compat_lock.unlock(); - for (auto &handle : compat_handles) { + for (auto &&handle : compat_handles) { #if defined(_WIN32) _close(static_cast(handle)); #else @@ -57,14 +57,14 @@ void remote_open_file_table::close_all(const std::string &client_id) { std::vector handles; unique_recur_mutex_lock file_lock(file_mutex_); - for (auto &kv : file_lookup_) { + for (auto &&kv : file_lookup_) { if (kv.second.client_id == client_id) { handles.emplace_back(kv.first); } } file_lock.unlock(); - for (auto &handle : handles) { + for (auto &&handle : handles) { #if defined(_WIN32) ::CloseHandle(handle); #else @@ -75,7 +75,7 @@ void remote_open_file_table::close_all(const std::string &client_id) { std::vector dirs; unique_recur_mutex_lock directory_lock(directory_mutex_); - for (auto &kv : directory_lookup_) { + for (auto &&kv : directory_lookup_) { if (kv.first == client_id) { dirs.insert(dirs.end(), kv.second.begin(), kv.second.end()); } @@ -183,11 +183,11 @@ void remote_open_file_table::remove_all(const std::string &file_path) { }); file_lock.unlock(); - for (auto &handle : open_list) { + for (auto &&handle : open_list) { remove_open_info(handle); } - for (auto &handle : compat_open_list) { + for (auto &&handle : compat_open_list) { remove_compat_open_info(handle); } } diff --git a/repertory/librepertory/src/drives/winfsp/winfsp_drive.cpp b/repertory/librepertory/src/drives/winfsp/winfsp_drive.cpp index 31d6f79d..ce49421c 100644 --- a/repertory/librepertory/src/drives/winfsp/winfsp_drive.cpp +++ b/repertory/librepertory/src/drives/winfsp/winfsp_drive.cpp @@ -592,7 +592,7 @@ auto winfsp_drive::mount(const std::vector &drive_args) -> int { const auto force_no_console = utils::collection::includes(drive_args, "-nc"); auto enable_console = false; - for (const auto &arg : drive_args) { + for (auto &&arg : drive_args) { if (arg == "-f") { if (not force_no_console) { enable_console = true; diff --git a/repertory/librepertory/src/file_manager/file_manager.cpp b/repertory/librepertory/src/file_manager/file_manager.cpp index 45c0ee5c..296a8a27 100644 --- a/repertory/librepertory/src/file_manager/file_manager.cpp +++ b/repertory/librepertory/src/file_manager/file_manager.cpp @@ -116,7 +116,7 @@ file_manager::file_manager(app_config &config, i_provider &provider) } db_.reset(db3); - for (const auto &create_item : sql_create_tables) { + for (auto &&create_item : sql_create_tables) { std::string err; if (not db::execute_sql(*db_, create_item.second, err)) { db_.reset(); @@ -176,12 +176,12 @@ void file_manager::close_timed_out_files() { } return items; }); - for (const auto &closeable_file : closeable_list) { + for (auto &&closeable_file : closeable_list) { open_file_lookup_.erase(closeable_file->get_api_path()); } file_lock.unlock(); - for (auto &closeable_file : closeable_list) { + for (auto &&closeable_file : closeable_list) { closeable_file->close(); event_system::instance().raise( closeable_file->get_api_path()); @@ -329,7 +329,7 @@ auto file_manager::get_open_files() const std::unordered_map ret; recur_mutex_lock open_lock(open_file_mtx_); - for (const auto &item : open_file_lookup_) { + for (auto &&item : open_file_lookup_) { ret[item.first] = item.second->get_open_file_count(); } @@ -843,7 +843,7 @@ void file_manager::start() { } } - for (const auto &active_item : active_items) { + for (auto &&active_item : active_items) { queue_upload(active_item.api_path, active_item.source_path, false); } active_items.clear(); @@ -937,7 +937,7 @@ void file_manager::stop() { open_file_lookup_.clear(); upload_lock.lock(); - for (auto &item : upload_lookup_) { + for (auto &&item : upload_lookup_) { item.second->stop(); } upload_notify_.notify_all(); @@ -1125,7 +1125,7 @@ void file_manager::upload_handler() { void file_manager::update_used_space(std::uint64_t &used_space) const { recur_mutex_lock open_lock(open_file_mtx_); - for (const auto &item : open_file_lookup_) { + for (auto &&item : open_file_lookup_) { std::uint64_t file_size{}; auto res = provider_.get_file_size(item.second->get_api_path(), file_size); if ((res == api_error::success) && diff --git a/repertory/librepertory/src/file_manager/file_manager_open_file.cpp b/repertory/librepertory/src/file_manager/file_manager_open_file.cpp index da2dfe45..fdb0c3a7 100644 --- a/repertory/librepertory/src/file_manager/file_manager_open_file.cpp +++ b/repertory/librepertory/src/file_manager/file_manager_open_file.cpp @@ -464,7 +464,7 @@ auto file_manager::open_file::close() -> bool { utils::get_last_error_code(), "failed to delete file"); } - auto parent = utils::path::get_parent_directory(fsi_.source_path); + auto parent = utils::path::get_parent_path(fsi_.source_path); fsi_.source_path = utils::path::combine(parent, {utils::create_uuid_string()}); const auto res = provider_.set_item_meta(fsi_.api_path, META_SOURCE, diff --git a/repertory/librepertory/src/file_manager/file_manager_open_file_base.cpp b/repertory/librepertory/src/file_manager/file_manager_open_file_base.cpp index 32e50bce..7ee9adc8 100644 --- a/repertory/librepertory/src/file_manager/file_manager_open_file_base.cpp +++ b/repertory/librepertory/src/file_manager/file_manager_open_file_base.cpp @@ -157,7 +157,7 @@ auto file_manager::open_file_base::get_handles() const -> std::vector { recur_mutex_lock file_lock(file_mtx_); std::vector ret; - for (const auto &item : open_data_) { + for (auto &&item : open_data_) { ret.emplace_back(item.first); } diff --git a/repertory/librepertory/src/file_manager/file_manager_ring_buffer_open_file.cpp b/repertory/librepertory/src/file_manager/file_manager_ring_buffer_open_file.cpp index 5f925a6d..f61540f8 100644 --- a/repertory/librepertory/src/file_manager/file_manager_ring_buffer_open_file.cpp +++ b/repertory/librepertory/src/file_manager/file_manager_ring_buffer_open_file.cpp @@ -168,8 +168,8 @@ void file_manager::ring_buffer_open_file::forward(std::size_t count) { last_chunk_ = std::min(total_chunks_ - 1U, first_chunk_ + ring_state_.size() - 1U); } else { - for (std::size_t i = 0U; i < added; i++) { - ring_state_[(first_chunk_ + i) % ring_state_.size()] = true; + for (std::size_t idx = 0U; idx < added; ++idx) { + ring_state_[(first_chunk_ + idx) % ring_state_.size()] = true; } first_chunk_ += added; current_chunk_ += count; @@ -220,8 +220,8 @@ void file_manager::ring_buffer_open_file::reverse(std::size_t count) { last_chunk_ = std::min(total_chunks_ - 1U, first_chunk_ + ring_state_.size() - 1U); } else { - for (std::size_t i = 0U; i < removed; i++) { - ring_state_[(last_chunk_ - i) % ring_state_.size()] = true; + for (std::size_t idx = 0U; idx < removed; ++idx) { + ring_state_[(last_chunk_ - idx) % ring_state_.size()] = true; } first_chunk_ -= removed; current_chunk_ -= count; @@ -254,7 +254,7 @@ auto file_manager::ring_buffer_open_file::read(std::size_t read_size, auto res = api_error::success; for (std::size_t chunk = start_chunk_index; - (res == api_error::success) && (read_size > 0U); chunk++) { + (res == api_error::success) && (read_size > 0U); ++chunk) { if (chunk > current_chunk_) { forward(chunk - current_chunk_); } else if (chunk < current_chunk_) { diff --git a/repertory/librepertory/src/providers/base_provider.cpp b/repertory/librepertory/src/providers/base_provider.cpp index db920767..3d2877de 100644 --- a/repertory/librepertory/src/providers/base_provider.cpp +++ b/repertory/librepertory/src/providers/base_provider.cpp @@ -455,7 +455,7 @@ void base_provider::remove_deleted_files() { std::vector removed_list{}; - for (const auto &api_path : db3_->get_api_path_list()) { + for (auto &&api_path : db3_->get_api_path_list()) { api_meta_map meta{}; if (get_item_meta(api_path, meta) == api_error::success) { if (utils::string::to_bool(meta[META_DIRECTORY])) { @@ -480,7 +480,7 @@ void base_provider::remove_deleted_files() { } } - for (const auto &item : removed_list) { + for (auto &&item : removed_list) { if (not item.directory) { if (utils::file::file(item.source_path).exists()) { const auto orphaned_directory = @@ -520,7 +520,7 @@ void base_provider::remove_deleted_files() { } } - for (const auto &item : removed_list) { + for (auto &&item : removed_list) { if (item.directory) { db3_->remove_api_path(item.api_path); event_system::instance().raise( @@ -641,9 +641,9 @@ auto base_provider::start(api_item_added_callback api_item_added, repertory::event_consumer consumer( "unmount_requested", [&unmount_requested](const event &) { unmount_requested = true; }); - for (std::uint16_t i = 0U; not online && not unmount_requested && - (i < config_.get_online_check_retry_secs()); - i++) { + for (std::uint16_t idx = 0U; not online && not unmount_requested && + (idx < config_.get_online_check_retry_secs()); + ++idx) { online = is_online(); if (not online) { event_system::instance().raise( diff --git a/repertory/librepertory/src/providers/encrypt/encrypt_provider.cpp b/repertory/librepertory/src/providers/encrypt/encrypt_provider.cpp index 7b0216ca..4b38598e 100644 --- a/repertory/librepertory/src/providers/encrypt/encrypt_provider.cpp +++ b/repertory/librepertory/src/providers/encrypt/encrypt_provider.cpp @@ -191,7 +191,11 @@ auto encrypt_provider::do_fs_operation( : api_error::item_not_found; } - auto exists = utils::file::file(source_path).exists(); + auto exists = + utils::file::file{ + source_path, + } + .exists(); if (exists && directory) { return api_error::item_exists; } @@ -199,7 +203,11 @@ auto encrypt_provider::do_fs_operation( return api_error::item_not_found; } - exists = utils::file::directory(source_path).exists(); + exists = + utils::file::directory{ + source_path, + } + .exists(); if (exists && not directory) { return api_error::item_exists; } @@ -265,10 +273,7 @@ auto encrypt_provider::get_directory_item_count( [&api_path, &count](const encrypt_config & /* cfg */, const std::string &source_path) -> api_error { try { - for ([[maybe_unused]] const auto &dir_entry : - std::filesystem::directory_iterator(source_path)) { - count++; - } + count = utils::file::directory{source_path}.count(); } catch (const std::exception &ex) { utils::error::raise_api_path_error( function_name, api_path, source_path, ex, @@ -295,15 +300,15 @@ auto encrypt_provider::get_directory_items( [this, &list](const encrypt_config &cfg, const std::string &source_path) -> api_error { try { - for (const auto &dir_entry : - std::filesystem::directory_iterator(source_path)) { + for (auto &&dir_entry : + utils::file::directory{source_path}.get_items()) { try { std::string current_api_path{}; - if (dir_entry.is_directory()) { + if (dir_entry->is_directory_item()) { auto result = db::db_select{*db_, directory_table} .column("api_path") .where("source_path") - .equals(dir_entry.path().string()) + .equals(dir_entry->get_path()) .go(); std::optional row; if (result.get_row(row) && row.has_value()) { @@ -311,12 +316,13 @@ auto encrypt_provider::get_directory_items( row->get_column("api_path").get_value(); } if (current_api_path.empty()) { - process_directory_entry(dir_entry, cfg, current_api_path); + process_directory_entry(*dir_entry.get(), cfg, + current_api_path); result = db::db_select{*db_, directory_table} .column("api_path") .where("source_path") - .equals(dir_entry.path().string()) + .equals(dir_entry->get_path()) .go(); row.reset(); if (not(result.get_row(row) && row.has_value())) { @@ -331,7 +337,7 @@ auto encrypt_provider::get_directory_items( auto result = db::db_select{*db_, file_table} .column("data") .where("source_path") - .equals(dir_entry.path().string()) + .equals(dir_entry->get_path()) .go(); std::optional row; if (result.get_row(row) && row.has_value()) { @@ -340,7 +346,7 @@ auto encrypt_provider::get_directory_items( } if (api_path_data.empty()) { - if (not process_directory_entry(dir_entry, cfg, + if (not process_directory_entry(*dir_entry.get(), cfg, current_api_path)) { continue; } @@ -351,14 +357,14 @@ auto encrypt_provider::get_directory_items( } } - auto file = - create_api_file(current_api_path, dir_entry.is_directory(), - dir_entry.path().string()); + auto file = create_api_file(current_api_path, + dir_entry->is_directory_item(), + dir_entry->get_path()); directory_item dir_item{}; dir_item.api_parent = file.api_parent; dir_item.api_path = file.api_path; - dir_item.directory = dir_entry.is_directory(); + dir_item.directory = dir_entry->is_directory_item(); dir_item.resolved = true; dir_item.size = file.file_size; create_item_meta(dir_item.meta, dir_item.directory, file); @@ -366,7 +372,7 @@ auto encrypt_provider::get_directory_items( list.emplace_back(std::move(dir_item)); } catch (const std::exception &ex) { utils::error::raise_error(function_name, ex, - dir_entry.path().string(), + dir_entry->get_path(), "failed to process directory item"); } } @@ -431,12 +437,11 @@ auto encrypt_provider::get_file_list(api_file_list &list) const -> api_error { const auto cfg = config_.get_encrypt_config(); try { - for (const auto &dir_entry : - std::filesystem::recursive_directory_iterator(cfg.path)) { + for (auto &&dir_entry : utils::file::directory{cfg.path}.get_items()) { std::string api_path{}; - if (process_directory_entry(dir_entry, cfg, api_path)) { - list.emplace_back(create_api_file(api_path, dir_entry.is_directory(), - dir_entry.path().string())); + if (process_directory_entry(*dir_entry.get(), cfg, api_path)) { + list.emplace_back(create_api_file( + api_path, dir_entry->is_directory_item(), dir_entry->get_path())); } } @@ -655,9 +660,11 @@ auto encrypt_provider::is_directory(const std::string &api_path, return api_error::success; } - exists = utils::file::directory( - row->get_column("source_path").get_value()) - .exists(); + exists = + utils::file::directory{ + row->get_column("source_path").get_value(), + } + .exists(); return api_error::success; } @@ -675,7 +682,9 @@ auto encrypt_provider::is_file(const std::string &api_path, } exists = - utils::file::file(row->get_column("source_path").get_value()) + utils::file::file{ + row->get_column("source_path").get_value(), + } .exists(); return api_error::success; } @@ -686,20 +695,24 @@ auto encrypt_provider::is_file_writeable(const std::string & /*api_path*/) const } auto encrypt_provider::is_online() const -> bool { - return std::filesystem::exists(config_.get_encrypt_config().path); + return utils::file::file{ + config_.get_encrypt_config().path, + } + .exists(); } auto encrypt_provider::is_rename_supported() const -> bool { return false; } auto encrypt_provider::process_directory_entry( - const std::filesystem::directory_entry &dir_entry, - const encrypt_config &cfg, std::string &api_path) const -> bool { - const auto add_directory = [this, &cfg](auto dir_path) -> std::string { + const utils::file::i_fs_item &dir_entry, const encrypt_config &cfg, + std::string &api_path) const -> bool { + const auto add_directory = [this, + &cfg](std::string_view dir_path) -> std::string { auto encrypted_parts = utils::string::split( - utils::path::create_api_path(dir_path.string()), '/', false); + utils::path::create_api_path(dir_path), '/', false); for (std::size_t part_idx = 1U; part_idx < encrypted_parts.size(); - part_idx++) { + ++part_idx) { data_buffer encrypted_data; utils::encryption::encrypt_data( cfg.encryption_token, @@ -714,14 +727,9 @@ auto encrypt_provider::process_directory_entry( std::size_t current_idx{1U}; std::string current_encrypted_path{}; - std::string current_source_path{cfg.path}; - for (const auto &part : dir_path) { - if (part.string() == "/") { - continue; - } - - current_source_path = - utils::path::combine(current_source_path, {part.string()}); + auto current_source_path{cfg.path}; + for (auto &&part : utils::path::get_parts(dir_path)) { + current_source_path = utils::path::combine(current_source_path, {part}); std::string current_api_path{}; auto result = db::db_select{*db_, directory_table} @@ -763,19 +771,21 @@ auto encrypt_provider::process_directory_entry( return current_encrypted_path; }; - if (dir_entry.is_directory()) { - api_path = add_directory(dir_entry.path().lexically_relative(cfg.path)); + if (dir_entry.is_directory_item()) { + api_path = add_directory( + utils::path::get_relative_path(dir_entry.get_path(), cfg.path)); return false; } - if (dir_entry.is_regular_file() && not dir_entry.is_symlink()) { - const auto relative_path = dir_entry.path().lexically_relative(cfg.path); + if (dir_entry.is_file_item() && not dir_entry.is_symlink()) { + auto relative_path = + utils::path::get_relative_path(dir_entry.get_path(), cfg.path); std::string api_path_data{}; auto result = db::db_select{*db_, file_table} .column("data") .where("source_path") - .equals(dir_entry.path().string()) + .equals(dir_entry.get_path()) .go(); std::optional row; if (result.get_row(row) && row.has_value()) { @@ -786,7 +796,7 @@ auto encrypt_provider::process_directory_entry( result = db::db_select{*db_, directory_table} .column("api_path") .where("source_path") - .equals(dir_entry.path().parent_path().string()) + .equals(utils::path::get_parent_path(dir_entry.get_path())) .go(); row.reset(); if (result.get_row(row) && row.has_value()) { @@ -794,15 +804,15 @@ auto encrypt_provider::process_directory_entry( } if (api_parent.empty()) { - api_parent = add_directory(relative_path.parent_path()); + api_parent = add_directory(utils::path::get_parent_path(relative_path)); } if (api_path_data.empty()) { stop_type stop_requested = false; utils::encryption::encrypting_reader reader( - relative_path.filename().string(), dir_entry.path().string(), + utils::path::strip_to_file_name(relative_path), dir_entry.get_path(), stop_requested, cfg.encryption_token, - relative_path.parent_path().string()); + utils::path::get_parent_path(relative_path)); api_path = utils::path::create_api_path(api_parent + "/" + reader.get_encrypted_file_name()); @@ -810,17 +820,22 @@ auto encrypt_provider::process_directory_entry( json data = { {"api_path", api_path}, {"iv_list", iv_list}, - {"original_file_size", dir_entry.file_size()}, + { + "original_file_size", + (dynamic_cast(&dir_entry) + ->size() + .value_or(0U)), + }, }; auto ins_res = db::db_insert{*db_, file_table} - .column_value("source_path", dir_entry.path().string()) + .column_value("source_path", dir_entry.get_path()) .column_value("data", data.dump()) .go(); // TODO handle error ins_res = db::db_insert{*db_, source_table} .column_value("api_path", api_path) - .column_value("source_path", dir_entry.path().string()) + .column_value("source_path", dir_entry.get_path()) .go(); // TODO handle error event_system::instance().raise(api_path, @@ -886,13 +901,12 @@ auto encrypt_provider::read_file_bytes(const std::string &api_path, unique_recur_mutex_lock reader_lookup_lock(reader_lookup_mtx_); if (file_data.at("original_file_size").get() != file_size) { - const auto relative_path = - std::filesystem::path(source_path).lexically_relative(cfg.path); + auto relative_path = utils::path::get_relative_path(source_path, cfg.path); auto info = std::make_shared(); info->reader = std::make_unique( - relative_path.filename().string(), source_path, stop_requested, - cfg.encryption_token, relative_path.parent_path().string()); + relative_path, source_path, stop_requested, cfg.encryption_token, + utils::path::get_parent_path(relative_path)); reader_lookup_[source_path] = info; iv_list = info->reader->get_iv_list(); @@ -963,9 +977,9 @@ void encrypt_provider::remove_deleted_files() { } } - for (const auto &row : row_list) { + for (auto &&row : row_list) { auto source_path = row.get_column("source_path").get_value(); - if (not std::filesystem::exists(source_path)) { + if (not utils::path::exists(source_path)) { auto api_path = row.get_column("api_path").get_value(); result = db::db_select{*db_, file_table} .column("source_path") @@ -977,7 +991,7 @@ void encrypt_provider::remove_deleted_files() { } } - for (const auto &item : removed_list) { + for (auto &&item : removed_list) { if (not item.directory) { auto del_res = db::db_select{*db_, source_table} .delete_query() @@ -996,7 +1010,7 @@ void encrypt_provider::remove_deleted_files() { } } - for (const auto &item : removed_list) { + for (auto &&item : removed_list) { if (item.directory) { auto del_res = db::db_select{*db_, source_table} .delete_query() @@ -1041,7 +1055,7 @@ auto encrypt_provider::start(api_item_added_callback /*api_item_added*/, } db_.reset(db3); - for (const auto &create : sql_create_tables) { + for (auto &&create : sql_create_tables) { std::string err; if (not db::execute_sql(*db_, create.second, err)) { utils::error::raise_error(function_name, "failed to create table|" + diff --git a/repertory/librepertory/src/providers/meta_db.cpp b/repertory/librepertory/src/providers/meta_db.cpp index 2a3396e1..cfdd22df 100644 --- a/repertory/librepertory/src/providers/meta_db.cpp +++ b/repertory/librepertory/src/providers/meta_db.cpp @@ -288,7 +288,7 @@ auto meta_db::set_item_meta(const std::string &api_path, // TODO handle error } - for (const auto &item : meta) { + for (auto &&item : meta) { existing_meta[item.first] = item.second; } diff --git a/repertory/librepertory/src/providers/s3/s3_provider.cpp b/repertory/librepertory/src/providers/s3/s3_provider.cpp index 37beab2a..b91dcbd7 100644 --- a/repertory/librepertory/src/providers/s3/s3_provider.cpp +++ b/repertory/librepertory/src/providers/s3/s3_provider.cpp @@ -179,13 +179,13 @@ auto s3_provider::create_path_directories( std::string cur_key{'/'}; std::string cur_path{'/'}; - for (std::size_t i = 0U; i < path_parts.size(); i++) { + for (std::size_t idx = 0U; idx < path_parts.size(); ++idx) { if (is_encrypted) { cur_key = utils::path::create_api_path( - utils::path::combine(cur_key, {key_parts.at(i)})); + utils::path::combine(cur_key, {key_parts.at(idx)})); } cur_path = utils::path::create_api_path( - utils::path::combine(cur_path, {path_parts.at(i)})); + utils::path::combine(cur_path, {path_parts.at(idx)})); api_meta_map meta{}; auto res = get_item_meta(cur_path, meta); @@ -206,7 +206,7 @@ auto s3_provider::create_path_directories( auto s3_provider::decrypt_object_name(std::string &object_name) const -> api_error { auto parts = utils::string::split(object_name, '/', false); - for (auto &part : parts) { + for (auto &&part : parts) { if (not utils::encryption::decrypt_file_name( get_config().get_s3_config().encryption_token, part)) { return api_error::decryption_error; @@ -368,14 +368,14 @@ auto s3_provider::get_directory_items_impl( }; auto node_list = doc.select_nodes("/ListBucketResult/CommonPrefixes/Prefix"); - for (const auto &node : node_list) { + for (auto &&node : node_list) { add_directory_item( true, node.node().text().as_string(), [](const directory_item &) -> std::uint64_t { return 0U; }); } node_list = doc.select_nodes("/ListBucketResult/Contents"); - for (const auto &node : node_list) { + for (auto &&node : node_list) { auto child_object_name = utils::path::create_api_path( node.node().select_node("Key").node().text().as_string()); if (child_object_name != utils::path::create_api_path(prefix)) { @@ -446,7 +446,7 @@ auto s3_provider::get_file_list(api_file_list &list) const -> api_error { } auto node_list = doc.select_nodes("/ListBucketResult/Contents"); - for (const auto &node : node_list) { + for (auto &&node : node_list) { auto api_path = std::string{node.node().select_node("Key").node().text().as_string()}; if (not utils::string::ends_with(api_path, "/")) { @@ -689,10 +689,10 @@ auto s3_provider::read_file_bytes(const std::string &api_path, std::size_t size, &stop_requested](std::size_t read_size, std::size_t read_offset, data_buffer &read_buffer) -> api_error { auto res = api_error::error; - for (std::uint32_t i = 0U; + for (std::uint32_t idx = 0U; not stop_requested && res != api_error::success && - i < get_config().get_retry_read_count() + 1U; - i++) { + idx < get_config().get_retry_read_count() + 1U; + ++idx) { curl::requests::http_get get{}; get.aws_service = "aws:amz:" + cfg.region + ":s3"; get.headers["response-content-type"] = "binary/octet-stream"; @@ -713,13 +713,13 @@ auto s3_provider::read_file_bytes(const std::string &api_path, std::size_t size, function_name, api_path, api_error::comm_error, "read file bytes failed|offset|" + std::to_string(read_offset) + "|size|" + std::to_string(read_size) + "|retry|" + - std::to_string(i + 1U)); + std::to_string(idx + 1U)); } else { utils::error::raise_api_path_error( function_name, api_path, response_code, "read file bytes failed|offset|" + std::to_string(read_offset) + "|size|" + std::to_string(read_size) + "|retry|" + - std::to_string(i + 1U)); + std::to_string(idx + 1U)); } std::this_thread::sleep_for(1s); }; diff --git a/repertory/librepertory/src/providers/sia/sia_provider.cpp b/repertory/librepertory/src/providers/sia/sia_provider.cpp index 4e0d05ff..ecf18538 100644 --- a/repertory/librepertory/src/providers/sia/sia_provider.cpp +++ b/repertory/librepertory/src/providers/sia/sia_provider.cpp @@ -80,7 +80,7 @@ auto sia_provider::get_directory_item_count(const std::string &api_path) const std::uint64_t item_count{}; if (object_list.contains("entries")) { - for (const auto &entry : object_list.at("entries")) { + for (auto &&entry : object_list.at("entries")) { try { auto name = entry.at("name").get(); auto entry_api_path = utils::path::create_api_path(name); @@ -118,7 +118,7 @@ auto sia_provider::get_directory_items_impl( } if (object_list.contains("entries")) { - for (const auto &entry : object_list.at("entries")) { + for (auto &&entry : object_list.at("entries")) { try { auto name = entry.at("name").get(); auto entry_api_path = utils::path::create_api_path(name); @@ -208,7 +208,7 @@ auto sia_provider::get_file_list(api_file_list &list) const -> api_error { } if (object_list.contains("entries")) { - for (const auto &entry : object_list.at("entries")) { + for (auto &&entry : object_list.at("entries")) { auto name = entry.at("name").get(); auto entry_api_path = utils::path::create_api_path(name); @@ -534,9 +534,10 @@ auto sia_provider::read_file_bytes(const std::string &api_path, long /*response_code*/) { buffer = data; }; auto res = api_error::comm_error; - for (std::uint32_t i = 0U; not stop_requested && res != api_error::success && - i < get_config().get_retry_read_count() + 1U; - i++) { + for (std::uint32_t idx = 0U; + not stop_requested && res != api_error::success && + idx < get_config().get_retry_read_count() + 1U; + ++idx) { long response_code{}; const auto notify_retry = [&]() { if (response_code == 0) { @@ -544,13 +545,13 @@ auto sia_provider::read_file_bytes(const std::string &api_path, function_name, api_path, api_error::comm_error, "read file bytes failed|offset|" + std::to_string(offset) + "|size|" + std::to_string(size) + "|retry|" + - std::to_string(i + 1U)); + std::to_string(idx + 1U)); } else { utils::error::raise_api_path_error( function_name, api_path, response_code, "read file bytes failed|offset|" + std::to_string(offset) + "|size|" + std::to_string(size) + "|retry|" + - std::to_string(i + 1U)); + std::to_string(idx + 1U)); } std::this_thread::sleep_for(1s); }; diff --git a/repertory/librepertory/src/utils/file_utils.cpp b/repertory/librepertory/src/utils/file_utils.cpp index 5b282515..2a2c2a86 100644 --- a/repertory/librepertory/src/utils/file_utils.cpp +++ b/repertory/librepertory/src/utils/file_utils.cpp @@ -49,7 +49,7 @@ void change_to_process_directory() { #else readlink("/proc/self/exe", &path[0U], path.size()); #endif - path = utils::path::get_parent_directory(path); + path = utils::path::get_parent_path(path); chdir(path.c_str()); #endif } diff --git a/support/include/utils/config.hpp b/support/include/utils/config.hpp index eee595c0..8794abba 100644 --- a/support/include/utils/config.hpp +++ b/support/include/utils/config.hpp @@ -458,11 +458,6 @@ struct http_range final { using http_headers = std::unordered_map; using http_query_parameters = std::map; using http_ranges = std::vector; - -struct encrypt_config { - std::string encryption_token; - std::string path; -}; #endif // defined(PROJECT_ENABLE_CURL) } // namespace repertory #endif // defined(__cplusplus) diff --git a/support/include/utils/file.hpp b/support/include/utils/file.hpp index ba20716b..28ba1440 100644 --- a/support/include/utils/file.hpp +++ b/support/include/utils/file.hpp @@ -102,6 +102,12 @@ struct i_fs_item { [[nodiscard]] virtual auto is_directory_item() const -> bool = 0; + [[nodiscard]] auto is_file_item() const -> bool { + return not is_directory_item(); + } + + [[nodiscard]] virtual auto is_symlink() const -> bool = 0; + [[nodiscard]] virtual auto move_to(std::string_view new_path) -> bool = 0; [[nodiscard]] virtual auto move_to(std::wstring_view new_path) -> bool { @@ -279,7 +285,9 @@ public: [[nodiscard]] auto is_read_only() const -> bool override { return read_only_; - }; + } + + [[nodiscard]] auto is_symlink() const -> bool override; [[nodiscard]] auto move_to(std::string_view new_path) -> bool override; @@ -374,7 +382,11 @@ public: [[nodiscard]] auto is_read_only() const -> bool override { return file_->is_read_only(); - }; + } + + [[nodiscard]] auto is_symlink() const -> bool override { + return file_->is_symlink(); + } [[nodiscard]] auto move_to(std::string_view new_path) -> bool override; @@ -489,7 +501,11 @@ public: [[nodiscard]] auto is_read_only() const -> bool override { return file_->is_read_only(); - }; + } + + [[nodiscard]] auto is_symlink() const -> bool override { + return file_->is_symlink(); + } [[nodiscard]] auto move_to(std::string_view new_path) -> bool override; @@ -624,6 +640,8 @@ public: [[nodiscard]] auto get_path() const -> std::string override { return path_; } + [[nodiscard]] auto is_symlink() const -> bool override; + [[nodiscard]] auto move_to(std::string_view new_path) -> bool override; [[nodiscard]] auto remove() -> bool override; @@ -724,8 +742,9 @@ public: [[nodiscard]] auto is_read_only() const -> bool override { return read_only_; - }; + } + [[nodiscard]] auto is_symlink() const -> bool override; [[nodiscard]] auto move_to(std::string_view new_path) -> bool override; [[nodiscard]] auto open(bool read_only) -> bool; @@ -853,6 +872,8 @@ public: return smb_get_uri_path(path_, user, password); } + [[nodiscard]] auto is_symlink() const -> bool override; + [[nodiscard]] auto move_to(std::string_view new_path) -> bool override; [[nodiscard]] auto remove() -> bool override; diff --git a/support/include/utils/path.hpp b/support/include/utils/path.hpp index 3ac31d24..7eebd638 100644 --- a/support/include/utils/path.hpp +++ b/support/include/utils/path.hpp @@ -221,6 +221,10 @@ combine(std::wstring_view path, [[nodiscard]] auto inline create_api_path(std::wstring_view path) -> std::wstring; +[[nodiscard]] auto exists(std::string_view path) -> bool; + +[[nodiscard]] auto exists(std::wstring_view path) -> bool; + [[nodiscard]] inline auto finalize(std::string_view path) -> std::string; [[nodiscard]] inline auto finalize(std::wstring_view path) -> std::wstring; @@ -244,9 +248,22 @@ get_parent_api_path(std::string_view path) -> std::string; [[nodiscard]] inline auto get_parent_api_path(std::wstring_view path) -> std::wstring; -[[nodiscard]] auto get_parent_directory(std::string_view path) -> std::string; +[[nodiscard]] auto get_parent_path(std::string_view path) -> std::string; -[[nodiscard]] auto get_parent_directory(std::wstring_view path) -> std::wstring; +[[nodiscard]] auto get_parent_path(std::wstring_view path) -> std::wstring; + +[[nodiscard]] inline auto +get_parts(std::string_view path) -> std::vector; + +[[nodiscard]] inline auto +get_parts_w(std::wstring_view path) -> std::vector; + +[[nodiscard]] auto get_relative_path(std::string_view path, + std::string_view root_path) -> std::string; + +[[nodiscard]] auto +get_relative_path(std::wstring_view path, + std::wstring_view root_path) -> std::wstring; [[nodiscard]] auto make_file_uri(std::string_view path) -> std::string; @@ -485,6 +502,23 @@ inline auto get_parent_api_path(std::string_view path) -> std::string { inline auto get_parent_api_path(std::wstring_view path) -> std::wstring { return get_parent_api_path_t(path); } + +template +[[nodiscard]] inline auto +get_parts_t(std::basic_string_view path) + -> std::vector { + return utils::string::split( + path, get_directory_seperator().at(0U), + false); +} + +inline auto get_parts(std::string_view path) -> std::vector { + return get_parts_t(path); +} + +inline auto get_parts_w(std::wstring_view path) -> std::vector { + return get_parts_t(path); +} } // namespace repertory::utils::path #endif // REPERTORY_INCLUDE_UTILS_PATH_HPP_ diff --git a/support/src/utils/file_directory.cpp b/support/src/utils/file_directory.cpp index c1f82ea1..2647025a 100644 --- a/support/src/utils/file_directory.cpp +++ b/support/src/utils/file_directory.cpp @@ -328,6 +328,22 @@ auto directory::get_items() const -> std::vector { return {}; } +auto directory::is_symlink() const -> bool { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + try { + return std::filesystem::is_symlink(path_); + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + return false; +} + auto directory::move_to(std::string_view new_path) -> bool { return false; } auto directory::remove() -> bool { diff --git a/support/src/utils/file_file.cpp b/support/src/utils/file_file.cpp index 77fd660b..75d747ef 100644 --- a/support/src/utils/file_file.cpp +++ b/support/src/utils/file_file.cpp @@ -259,6 +259,22 @@ auto file::get_time(time_types type) const -> std::uint64_t { return i_fs_item::get_time(type); } +auto file::is_symlink() const -> bool { + static constexpr const std::string_view function_name{ + static_cast(__FUNCTION__), + }; + + try { + return std::filesystem::is_symlink(path_); + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + return false; +} + auto file::move_to(std::string_view path) -> bool { static constexpr const std::string_view function_name{ static_cast(__FUNCTION__), diff --git a/support/src/utils/file_smb_directory.cpp b/support/src/utils/file_smb_directory.cpp index d9aa3185..54e9162e 100644 --- a/support/src/utils/file_smb_directory.cpp +++ b/support/src/utils/file_smb_directory.cpp @@ -484,6 +484,25 @@ auto smb_directory::get_items() const -> std::vector { return {}; } +auto smb_directory::is_symlink() 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_); + } + + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + return false; +} + auto smb_directory::move_to(std::string_view new_path) -> bool { 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 59d4f503..41ab9968 100644 --- a/support/src/utils/file_smb_file.cpp +++ b/support/src/utils/file_smb_file.cpp @@ -139,6 +139,25 @@ auto smb_file::get_time(smb_session *session, smb_tid tid, std::string path, return 0U; } +auto smb_file::is_symlink() 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_); + } + + } catch (const std::exception &e) { + utils::error::handle_exception(function_name, e); + } catch (...) { + utils::error::handle_exception(function_name); + } + + return false; +} + auto smb_file::move_to(std::string_view new_path) -> bool { static constexpr const std::string_view function_name{ static_cast(__FUNCTION__), diff --git a/support/src/utils/path.cpp b/support/src/utils/path.cpp index 6204123c..84037685 100644 --- a/support/src/utils/path.cpp +++ b/support/src/utils/path.cpp @@ -112,6 +112,15 @@ auto absolute(std::wstring_view path) -> std::wstring { return utils::string::from_utf8(absolute(utils::string::to_utf8(path))); } +auto exists(std::string_view path) -> bool { + return utils::file::file{path}.exists() || + utils::file::directory{path}.exists(); +} + +auto exists(std::wstring_view path) -> bool { + return exists(utils::string::to_utf8(path)); +} + auto find_program_in_path(const std::string &name_without_extension) -> std::string { static std::mutex mtx{}; @@ -143,7 +152,7 @@ auto find_program_in_path(const std::string &name_without_extension) static constexpr const auto split_char = ':'; #endif // defined(_WIN32) - const auto search_path_list = utils::string::split(path, split_char, false); + auto search_path_list = utils::string::split(path, split_char, false); for (auto &&search_path : search_path_list) { for (auto &&extension : extension_list) { auto exec_path = combine( @@ -164,7 +173,7 @@ find_program_in_path(std::wstring_view name_without_extension) -> std::wstring { find_program_in_path(utils::string::to_utf8(name_without_extension))); } -auto get_parent_directory(std::string_view path) -> std::string { +auto get_parent_path(std::string_view path) -> std::string { auto abs_path = absolute(path); #if defined(_WIN32) @@ -177,9 +186,32 @@ auto get_parent_directory(std::string_view path) -> std::string { return finalize(abs_path); } -auto get_parent_directory(std::wstring_view path) -> std::wstring { +auto get_parent_path(std::wstring_view path) -> std::wstring { return utils::string::from_utf8( - get_parent_directory(utils::string::to_utf8(path))); + get_parent_path(utils::string::to_utf8(path))); +} + +auto get_relative_path(std::string_view path, + std::string_view root_path) -> std::string { + auto abs_path = absolute(path); + auto abs_root_path = + absolute(root_path) + std::string{get_directory_seperator()}; +#if defined(_WIN32) + if (utils::string::to_lower(abs_path).starts_with( + utils::string::to_lower(abs_root_path))) { +#else // !defined(_WIN32) + if (abs_path.starts_with(abs_root_path)) { +#endif // defined(_WIN32) + return abs_path.substr(abs_root_path.size()); + } + + return abs_path; +} + +auto get_relative_path(std::wstring_view path, + std::wstring_view root_path) -> std::wstring { + return utils::string::from_utf8(get_relative_path( + utils::string::to_utf8(path), utils::string::to_utf8(root_path))); } auto contains_trash_directory(std::string_view path) -> bool { diff --git a/support/test/src/test.cpp b/support/test/src/test.cpp index 927f5d81..1536a5e0 100644 --- a/support/test/src/test.cpp +++ b/support/test/src/test.cpp @@ -33,7 +33,7 @@ static void delete_generated_files() { for (auto &&path : generated_files) { if (parent_path->empty()) { parent_path = - repertory::utils::path::get_parent_directory(path->get_path()); + repertory::utils::path::get_parent_path(path->get_path()); } [[maybe_unused]] auto removed = path->remove(); diff --git a/support/test/src/utils/path_test.cpp b/support/test/src/utils/path_test.cpp index 6b64ff2d..fff28838 100644 --- a/support/test/src/utils/path_test.cpp +++ b/support/test/src/utils/path_test.cpp @@ -423,53 +423,53 @@ TEST(utils_path, absolute_can_resolve_path_variables) { #endif // defined(_WIN32) } -TEST(utils_path, get_parent_directory) { +TEST(utils_path, get_parent_path) { #if defined(_WIN32) { auto dir = R"(c:\test)"; - auto parent = utils::path::get_parent_directory(dir); + auto parent = utils::path::get_parent_path(dir); EXPECT_STREQ("c:", parent.c_str()); dir = R"(c:\test\file.txt)"; - parent = utils::path::get_parent_directory(dir); + parent = utils::path::get_parent_path(dir); EXPECT_STREQ(R"(c:\test)", parent.c_str()); dir = "c:"; - parent = utils::path::get_parent_directory(dir); + parent = utils::path::get_parent_path(dir); EXPECT_STREQ("c:", parent.c_str()); } { auto dir = LR"(c:\test)"; - auto parent = utils::path::get_parent_directory(dir); + auto parent = utils::path::get_parent_path(dir); EXPECT_STREQ(L"c:", parent.c_str()); dir = LR"(c:\test\file.txt)"; - parent = utils::path::get_parent_directory(dir); + parent = utils::path::get_parent_path(dir); EXPECT_STREQ(LR"(c:\test)", parent.c_str()); dir = L"c:"; - parent = utils::path::get_parent_directory(dir); + parent = utils::path::get_parent_path(dir); EXPECT_STREQ(L"c:", parent.c_str()); } #else // !defined(_WIN32) { auto dir = "/test"; - auto parent = utils::path::get_parent_directory(dir); + auto parent = utils::path::get_parent_path(dir); EXPECT_STREQ("/", parent.c_str()); dir = "/test/test"; - parent = utils::path::get_parent_directory(dir); + parent = utils::path::get_parent_path(dir); EXPECT_STREQ("/test", parent.c_str()); } { auto dir = L"/test"; - auto parent = utils::path::get_parent_directory(dir); + auto parent = utils::path::get_parent_path(dir); EXPECT_STREQ(L"/", parent.c_str()); dir = L"/test/test"; - parent = utils::path::get_parent_directory(dir); + parent = utils::path::get_parent_path(dir); EXPECT_STREQ(L"/test", parent.c_str()); } #endif // defined(_WIN32)