From 6718eb374d923854b4a4e2c514965734a11f71eb Mon Sep 17 00:00:00 2001 From: "Scott E. Graves" Date: Fri, 2 Aug 2024 20:59:41 -0500 Subject: [PATCH] fix --- repertory/librepertory/include/app_config.hpp | 2 +- repertory/librepertory/src/app_config.cpp | 30 +++++++---- repertory/repertory/include/cli/mount.hpp | 1 + support/3rd_party/include/utils/path.hpp | 12 +---- support/3rd_party/src/utils/path.cpp | 34 +++++++++++++ .../3rd_party/test/src/utils/path_test.cpp | 50 ++++++++++++------- 6 files changed, 91 insertions(+), 38 deletions(-) diff --git a/repertory/librepertory/include/app_config.hpp b/repertory/librepertory/include/app_config.hpp index 224d1b4d..8e042fcb 100644 --- a/repertory/librepertory/include/app_config.hpp +++ b/repertory/librepertory/include/app_config.hpp @@ -55,7 +55,7 @@ public: get_provider_name(const provider_type &prov) -> std::string; public: - app_config(const provider_type &prov, const std::string &data_directory = ""); + app_config(const provider_type &prov, std::string_view data_directory = ""); ~app_config() { save(); } diff --git a/repertory/librepertory/src/app_config.cpp b/repertory/librepertory/src/app_config.cpp index 9fc65531..2170749b 100644 --- a/repertory/librepertory/src/app_config.cpp +++ b/repertory/librepertory/src/app_config.cpp @@ -54,7 +54,7 @@ constexpr const auto retry_save_count = 5U; namespace repertory { app_config::app_config(const provider_type &prov, - const std::string &data_directory) + std::string_view data_directory) : prov_(prov), api_auth_(utils::generate_random_string(default_api_auth_size)), api_port_(default_rpc_port(prov)), @@ -151,19 +151,28 @@ auto app_config::default_api_port(const provider_type &prov) -> std::uint16_t { auto app_config::default_data_directory(const provider_type &prov) -> std::string { #if defined(_WIN32) - auto data_directory = utils::path::combine( - utils::get_local_app_data_directory(), - {std::string{REPERTORY_DATA_NAME}, app_config::get_provider_name(prov)}); + auto data_directory = + utils::path::combine(utils::get_local_app_data_directory(), + { + REPERTORY_DATA_NAME, + app_config::get_provider_name(prov), + }); #else #if defined(__APPLE__) - auto data_directory = - utils::path::resolve(std::string{"~/Library/Application Support/"} + - std::string{REPERTORY_DATA_NAME} + '/' + - app_config::get_provider_name(prov)); + auto data_directory = utils::path::resolve( + utils::path::combine("~", { + "Library", + "Application Support", + REPERTORY_DATA_NAME, + app_config::get_provider_name(prov), + })); #else auto data_directory = utils::path::resolve( - std::string{"~/.local/"} + std::string{REPERTORY_DATA_NAME} + '/' + - app_config::get_provider_name(prov)); + utils::path::combine("~", { + ".local", + REPERTORY_DATA_NAME, + app_config::get_provider_name(prov), + })); #endif #endif return data_directory; @@ -528,6 +537,7 @@ auto app_config::load() -> bool { auto ret = false; const auto config_file_path = get_config_file_path(); + std::cout << config_file_path << std::endl; recur_mutex_lock lock(read_write_mutex_); if (utils::file::is_file(config_file_path)) { try { diff --git a/repertory/repertory/include/cli/mount.hpp b/repertory/repertory/include/cli/mount.hpp index aa177bda..7487deac 100644 --- a/repertory/repertory/include/cli/mount.hpp +++ b/repertory/repertory/include/cli/mount.hpp @@ -91,6 +91,7 @@ mount(std::vector args, std::string data_directory, #endif const auto drive_args = utils::cli::parse_drive_options(args, prov, data_directory); + std::cout << data_directory << std::endl; app_config config(prov, data_directory); #if defined(_WIN32) if (config.get_enable_mount_manager() && diff --git a/support/3rd_party/include/utils/path.hpp b/support/3rd_party/include/utils/path.hpp index 2b465b19..bfcaedc4 100644 --- a/support/3rd_party/include/utils/path.hpp +++ b/support/3rd_party/include/utils/path.hpp @@ -141,9 +141,9 @@ get_not_directory_seperator() -> std::basic_string_view { return not_directory_seperator_w; } -[[nodiscard]] inline auto absolute(std::string_view path) -> std::string; +[[nodiscard]] auto absolute(std::string_view path) -> std::string; -[[nodiscard]] inline auto absolute(std::wstring_view path) -> std::wstring; +[[nodiscard]] auto absolute(std::wstring_view path) -> std::wstring; [[nodiscard]] inline auto combine(std::string path, @@ -226,14 +226,6 @@ get_parent_api_path(std::wstring_view path) -> std::wstring; [[nodiscard]] auto unmake_file_uri(std::wstring_view uri) -> std::wstring; -inline auto absolute(std::string_view path) -> std::string { - return std::filesystem::absolute(finalize(path)).lexically_normal().string(); -} - -inline auto absolute(std::wstring_view path) -> std::wstring { - return std::filesystem::absolute(finalize(path)).lexically_normal().wstring(); -} - template [[nodiscard]] inline auto combine_t( string_t path, diff --git a/support/3rd_party/src/utils/path.cpp b/support/3rd_party/src/utils/path.cpp index 3d118450..94f3bc8b 100644 --- a/support/3rd_party/src/utils/path.cpp +++ b/support/3rd_party/src/utils/path.cpp @@ -36,6 +36,40 @@ static const std::wstring directory_seperator_str_w{ } // namespace namespace repertory::utils::path { +inline auto absolute(std::string_view path) -> std::string { + std::string abs_path{path}; +#ifdef _WIN32 + if (not abs_path.empty() && ::PathIsRelative(abs_path.c_str())) { + std::string temp; + temp.resize(MAX_PATH + 1); + abs_path = _fullpath(temp.c_str(), abs_path.data(), MAX_PATH); + } +#else + if (not abs_path.empty() && (abs_path.at(0U) != '/')) { + auto found{false}; + std::string tmp{abs_path}; + do { + auto *res = realpath(tmp.c_str(), nullptr); + if (res) { + abs_path = combine(res, {abs_path.substr(tmp.size())}); + free(res); + found = true; + } else if (tmp == ".") { + found = true; + } else { + tmp = dirname(tmp.c_str()); + } + } while (not found); + } +#endif + + return finalize(abs_path); +} + +inline auto absolute(std::wstring_view path) -> std::wstring { + return utils::string::from_utf8(absolute(utils::string::to_utf8(path))); +} + auto find_program_in_path(const std::string &name_without_extension) -> std::string { static std::mutex mtx{}; diff --git a/support/3rd_party/test/src/utils/path_test.cpp b/support/3rd_party/test/src/utils/path_test.cpp index 44b1caae..f6c7ddfb 100644 --- a/support/3rd_party/test/src/utils/path_test.cpp +++ b/support/3rd_party/test/src/utils/path_test.cpp @@ -120,43 +120,43 @@ TEST(utils_path, combine) { } TEST(utils_path, format_path) { - std::string path{"\\"}; - utils::path::format_path(path, utils::path::directory_seperator, - utils::path::not_directory_seperator); + std::string path{"./"}; + utils::path::format_path(path, utils::path::slash, utils::path::backslash); + EXPECT_STREQ(".", path.c_str()); + + path = "~/.test"; + utils::path::format_path(path, utils::path::slash, utils::path::backslash); + EXPECT_STREQ("~/.test", path.c_str()); + + path = "\\"; + utils::path::format_path(path, utils::path::slash, utils::path::backslash); EXPECT_STREQ("/", path.c_str()); path = "\\\\"; - utils::path::format_path(path, utils::path::directory_seperator, - utils::path::not_directory_seperator); + utils::path::format_path(path, utils::path::slash, utils::path::backslash); EXPECT_STREQ("/", path.c_str()); path = "\\\\\\"; - utils::path::format_path(path, utils::path::directory_seperator, - utils::path::not_directory_seperator); + utils::path::format_path(path, utils::path::slash, utils::path::backslash); EXPECT_STREQ("/", path.c_str()); path = "\\\\\\\\"; - utils::path::format_path(path, utils::path::directory_seperator, - utils::path::not_directory_seperator); + utils::path::format_path(path, utils::path::slash, utils::path::backslash); EXPECT_STREQ("/", path.c_str()); path = "/"; - utils::path::format_path(path, utils::path::directory_seperator, - utils::path::not_directory_seperator); + utils::path::format_path(path, utils::path::slash, utils::path::backslash); EXPECT_STREQ("/", path.c_str()); path = "//"; - utils::path::format_path(path, utils::path::directory_seperator, - utils::path::not_directory_seperator); + utils::path::format_path(path, utils::path::slash, utils::path::backslash); EXPECT_STREQ("/", path.c_str()); path = "///"; - utils::path::format_path(path, utils::path::directory_seperator, - utils::path::not_directory_seperator); + utils::path::format_path(path, utils::path::slash, utils::path::backslash); EXPECT_STREQ("/", path.c_str()); path = "////"; - utils::path::format_path(path, utils::path::directory_seperator, - utils::path::not_directory_seperator); + utils::path::format_path(path, utils::path::slash, utils::path::backslash); EXPECT_STREQ("/", path.c_str()); } @@ -245,4 +245,20 @@ TEST(utils_path, finalize) { EXPECT_STREQ("/cow/moose/dog/chicken", s.c_str()); #endif } + +#if !defined(_WIN32) +TEST(utils_path, resolve) { + std::cout << utils::path::resolve("~") << std::endl; + std::cout << utils::path::combine("~", + { + ".local", + }) + << std::endl; + std::cout << utils::path::resolve(utils::path::combine("~", + { + ".local", + })) + << std::endl; +} +#endif // !defined(_WIN32) } // namespace repertory