diff options
author | Martin Braun <martin.braun@ettus.com> | 2020-03-05 16:23:07 -0800 |
---|---|---|
committer | Aaron Rossetto <aaron.rossetto@ni.com> | 2020-04-02 12:36:20 -0500 |
commit | 1383fde3457168ed759d6ed3b913dfae8a6085ef (patch) | |
tree | 60acf5dfd0a917e6fcc562005dd2405332e1f8bb /host/lib/utils/paths.cpp | |
parent | c65166562f1278f6633b183b7188789158210544 (diff) | |
download | uhd-1383fde3457168ed759d6ed3b913dfae8a6085ef.tar.gz uhd-1383fde3457168ed759d6ed3b913dfae8a6085ef.tar.bz2 uhd-1383fde3457168ed759d6ed3b913dfae8a6085ef.zip |
uhd: paths: Harmonize around XDG Base Directory specification
Up until now, we completely ignore the XDG specification.
This commit does the following to change that:
- It uses XDG_DATA_HOME and XDG_CONFIG_HOME for cal and config data,
respectively.
- If config data is in ~/.uhd/uhd.conf, that is still accepted, but if
it conflicts with $XDG_CONFIG_HOME/uhd.conf, it is ignored and a
warning is displayed
- The default location for cal data is thus ${HOME}/.local/share/uhd/cal
on Unix, and %LOCALAPPDATA%\uhd\cal on Windows. This is a change in
location!
- The UHD_CONFIG_DIR environment variable was confusingly named and is
now removed. It provided an alternative location than the home
directory. The same purpose is now much better served by XDG_DATA_HOME
and XDG_CONFIG_HOME.
The specification can be found here:
specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
Diffstat (limited to 'host/lib/utils/paths.cpp')
-rw-r--r-- | host/lib/utils/paths.cpp | 117 |
1 files changed, 89 insertions, 28 deletions
diff --git a/host/lib/utils/paths.cpp b/host/lib/utils/paths.cpp index 3ecd5fe38..54b8e0323 100644 --- a/host/lib/utils/paths.cpp +++ b/host/lib/utils/paths.cpp @@ -35,6 +35,8 @@ namespace fs = boost::filesystem; +static constexpr char UHD_CAL_DATA_PATH_VAR[] = "UHD_CAL_DATA_PATH"; + /*! Get the value of an environment variable. * * The returned std::string is the full environment variable string, and thus @@ -128,6 +130,89 @@ static std::string expand_home_directory(std::string path) } #endif +fs::path uhd::get_xdg_data_home() +{ + std::string xdg_data_home_str = get_env_var("XDG_DATA_HOME", ""); + fs::path xdg_data_home(xdg_data_home_str); + if (!xdg_data_home_str.empty()) { + return fs::path(xdg_data_home_str); + } +#ifdef UHD_PLATFORM_WIN32 + const std::string localappdata = get_env_var("LOCALAPPDATA", ""); + if (!localappdata.empty()) { + return fs::path(localappdata); + } + const std::string appdata = get_env_var("APPDATA", ""); + if (!appdata.empty()) { + return fs::path(appdata); + } +#endif + const std::string home = get_env_var("HOME", ""); + if (home.empty()) { +#ifdef UHD_PLATFORM_WIN32 + const std::string err_msg = + "get_xdg_data_home(): Unable to find \%HOME\%, \%XDG_DATA_HOME\%, " + "\%LOCALAPPDATA\% or \%APPDATA\%."; +#else + const std::string err_msg = + "get_xdg_data_home(): Unable to find $HOME or $XDG_DATA_HOME."; +#endif + throw uhd::runtime_error(err_msg); + } + return fs::path(home) / ".local" / "share"; +} + +fs::path uhd::get_xdg_config_home() +{ + std::string xdg_config_home_str = get_env_var("XDG_CONFIG_HOME", ""); + fs::path xdg_config_home(xdg_config_home_str); + if (!xdg_config_home_str.empty()) { + return fs::path(xdg_config_home_str); + } +#ifdef UHD_PLATFORM_WIN32 + const std::string localappdata = get_env_var("LOCALAPPDATA", ""); + if (!localappdata.empty()) { + return fs::path(localappdata); + } + const std::string appdata = get_env_var("APPDATA", ""); + if (!appdata.empty()) { + return fs::path(appdata); + } +#endif + const std::string home = get_env_var("HOME", ""); + if (home.empty()) { +#ifdef UHD_PLATFORM_WIN32 + const std::string err_msg = + "get_xdg_config_home(): Unable to find \%HOME\%, \%XDG_CONFIG_HOME\%, " + "\%LOCALAPPDATA\% or \%APPDATA\%."; +#else + const std::string err_msg = + "get_xdg_config_home(): Unable to find $HOME or $XDG_CONFIG_HOME."; +#endif + throw uhd::runtime_error(err_msg); + } + return fs::path(home) / ".config"; +} + +fs::path uhd::get_legacy_config_home() +{ +#ifdef UHD_PLATFORM_WIN32 + const std::string localappdata = get_env_var("LOCALAPPDATA", ""); + if (!localappdata.empty()) { + return fs::path(localappdata) / ".uhd"; + } + const std::string appdata = get_env_var("APPDATA", ""); + if (!appdata.empty()) { + return fs::path(appdata) / ".uhd"; + } +#endif + const std::string home = get_env_var("HOME", ""); + if (home.empty()) { + throw uhd::runtime_error("Unable to find $HOME."); + } + return fs::path(home) / ".uhd"; +} + /*********************************************************************** * Implement the functions in paths.hpp **********************************************************************/ @@ -173,23 +258,6 @@ std::string uhd::get_tmp_path(void) #endif } -std::string uhd::get_app_path(void) -{ - const std::string uhdcalib_path = get_env_var("UHD_CONFIG_DIR"); - if (not uhdcalib_path.empty()) - return uhdcalib_path; - - const std::string appdata_path = get_env_var("APPDATA"); - if (not appdata_path.empty()) - return appdata_path; - - const std::string home_path = get_env_var("HOME"); - if (not home_path.empty()) - return home_path; - - return uhd::get_tmp_path(); -} - // Only used for deprecated CSV file loader. Delete this once CSV support is // removed. std::string uhd::get_appdata_path(void) @@ -242,21 +310,14 @@ std::string uhd::get_lib_path(void) std::string uhd::get_cal_data_path(void) { - const std::string uhdcalib_path = get_env_var("UHD_CAL_DATA_PATH"); + // The easy case: User has set the environment variable + const std::string uhdcalib_path = get_env_var(UHD_CAL_DATA_PATH_VAR); if (not uhdcalib_path.empty()) { return uhdcalib_path; } - std::string xdg_data_home_str = get_env_var("XDG_DATA_HOME", ""); - fs::path xdg_data_home(xdg_data_home_str); - if (xdg_data_home_str.empty()) { - const std::string home = get_env_var("HOME", ""); - xdg_data_home = fs::path(home) / ".local" / "share"; - } - - // FIXME: This needs to check if paths make sense, work on Windows, etc. - - fs::path cal_data_path = fs::path(xdg_data_home) / "uhd" / "cal_data"; + // If not, we use the default location + const fs::path cal_data_path = get_xdg_data_home() / "uhd" / "cal"; return cal_data_path.string(); } |