This commit is contained in:
Scott E. Graves 2024-08-02 21:30:17 -05:00
parent e37a375c18
commit c0dba76ecd
5 changed files with 46 additions and 57 deletions

View File

@ -159,20 +159,20 @@ auto app_config::default_data_directory(const provider_type &prov)
});
#else
#if defined(__APPLE__)
auto data_directory = utils::path::resolve(
auto data_directory =
utils::path::combine("~", {
"Library",
"Application Support",
REPERTORY_DATA_NAME,
app_config::get_provider_name(prov),
}));
});
#else
auto data_directory = utils::path::resolve(
auto data_directory =
utils::path::combine("~", {
".local",
REPERTORY_DATA_NAME,
app_config::get_provider_name(prov),
}));
});
#endif
#endif
return data_directory;
@ -327,18 +327,25 @@ auto app_config::get_max_cache_size_bytes() const -> std::uint64_t {
auto app_config::get_provider_api_password(const provider_type &prov)
-> std::string {
#if defined(_WIN32)
auto api_file =
utils::path::combine(utils::get_local_app_data_directory(),
{get_provider_display_name(prov), "apipassword"});
auto api_file = utils::path::combine(utils::get_local_app_data_directory(),
{
get_provider_display_name(prov),
"apipassword",
});
#else
#if defined(__APPLE__)
auto api_file =
utils::path::combine(utils::path::resolve("~"),
{"/Library/Application Support",
get_provider_display_name(prov), "apipassword"});
auto api_file = utils::path::combine("~", {
"/",
"Library",
"Application Support",
get_provider_display_name(prov),
"apipassword",
});
#else
auto api_file = utils::path::combine(
utils::path::resolve("~/."), {get_provider_name(prov), "apipassword"});
auto api_file = utils::path::combine("~/.", {
get_provider_name(prov),
"apipassword",
});
#endif
#endif
auto lines = utils::file::read_file_lines(api_file);

View File

@ -93,11 +93,11 @@ auto lock_data::get_mount_state(json &mount_state) -> bool {
auto lock_data::get_state_directory() -> std::string {
#if defined(__APPLE__)
return utils::path::resolve("~/Library/Application Support/" +
std::string{REPERTORY_DATA_NAME} + "/state");
return utils::path::absolute("~/Library/Application Support/" +
std::string{REPERTORY_DATA_NAME} + "/state");
#else
return utils::path::resolve("~/.local/" + std::string{REPERTORY_DATA_NAME} +
"/state");
return utils::path::absolute("~/.local/" + std::string{REPERTORY_DATA_NAME} +
"/state");
#endif
}

View File

@ -212,12 +212,6 @@ get_parent_api_path(std::wstring_view path) -> std::wstring;
[[nodiscard]] auto remove_file_name(std::wstring_view path) -> std::wstring;
#if !defined(_WIN32)
[[nodiscard]] auto resolve(std::string path) -> std::string;
[[nodiscard]] auto resolve(std::wstring_view path) -> std::wstring;
#endif // !defined(_WIN32)
[[nodiscard]] auto strip_to_file_name(std::string path) -> std::string;
[[nodiscard]] auto strip_to_file_name(std::wstring path) -> std::wstring;

View File

@ -33,6 +33,26 @@ static const std::string directory_seperator_str{
static const std::wstring directory_seperator_str_w{
repertory::utils::path::directory_seperator_w,
};
#if !defined(_WIN32)
[[nodiscard]] auto resolve(std::string path) -> std::string {
std::string home{};
repertory::utils::use_getpwuid(getuid(), [&home](struct passwd *pw) {
home = (pw->pw_dir ? pw->pw_dir : "");
if (home.empty() || ((home == "/") && (getuid() != 0))) {
home = repertory::utils::path::combine("/home", {pw->pw_name});
}
});
return repertory::utils::string::replace(path, "~", home);
}
[[nodiscard]] auto resolve(std::wstring_view path) -> std::wstring {
return repertory::utils::string::from_utf8(
resolve(repertory::utils::string::to_utf8(path)));
}
#endif // !defined(_WIN32)
} // namespace
namespace repertory::utils::path {
@ -45,6 +65,8 @@ auto absolute(std::string_view path) -> std::string {
abs_path = _fullpath(temp.c_str(), abs_path.data(), MAX_PATH);
}
#else
abs_path = resolve(abs_path);
if (not abs_path.empty() && (abs_path.at(0U) != '/')) {
auto found{false};
std::string tmp{abs_path};
@ -185,24 +207,6 @@ auto remove_file_name(std::string_view path) -> std::string {
return abs_path;
}
#if !defined(_WIN32)
auto resolve(std::string path) -> std::string {
std::string home{};
use_getpwuid(getuid(), [&home](struct passwd *pw) {
home = (pw->pw_dir ? pw->pw_dir : "");
if (home.empty() || ((home == "/") && (getuid() != 0))) {
home = combine("/home", {pw->pw_name});
}
});
return absolute(utils::string::replace(path, "~", home));
}
auto resolve(std::wstring_view path) -> std::wstring {
return utils::string::from_utf8(resolve(utils::string::to_utf8(path)));
}
#endif // !defined(_WIN32)
auto strip_to_file_name(std::string path) -> std::string {
#if defined(_WIN32)
return ::PathFindFileName(path.c_str());

View File

@ -245,20 +245,4 @@ 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