fix: normalize paths

This commit is contained in:
2025-12-17 02:58:23 +03:00
parent 1c2486d476
commit ef0854aa39
8 changed files with 38 additions and 26 deletions

View File

@@ -1,5 +1,6 @@
#include "utils.hpp"
#include <iostream>
#include <filesystem>
namespace clrsync::core
{
@@ -15,20 +16,19 @@ std::string get_default_config_path()
{
const char* env_path = std::getenv("CLRSYNC_CONFIG_PATH");
if (env_path && env_path[0] != '\0')
return expand_user(env_path);
return normalize_path(env_path).string();
auto home = expand_user("~");
#ifdef _WIN32
return home + "\\.config\\clrsync\\config.toml";
#else
return home + "/.config/clrsync/config.toml";
#endif
std::filesystem::path home = normalize_path("~");
std::filesystem::path config_path = home / ".config" / "clrsync" / "config.toml";
return config_path.string();
}
std::string expand_user(const std::string &path)
{
if (!path.empty() && path[0] == '~')
if (path.empty() || path[0] != '~')
return path;
if (path.length() == 1 || path[1] == '/' || path[1] == '\\')
{
#ifdef _WIN32
const char *home = std::getenv("USERPROFILE");
@@ -37,9 +37,21 @@ std::string expand_user(const std::string &path)
#endif
if (!home)
return path;
if (path.length() == 1)
return std::string(home);
return std::string(home) + path.substr(1);
}
return path;
}
std::filesystem::path normalize_path(const std::string &path)
{
std::string expanded = expand_user(path);
std::filesystem::path fs_path(expanded);
// lexically_normal() resolves . and .. and normalizes separators
return fs_path.lexically_normal();
}
} // namespace clrsync::core