From 32578f7520dfd9bec4d72ffd095f36fb47c84dc6 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Tue, 25 Feb 2020 12:29:15 +0100 Subject: [PATCH] Respect the XDG base directory specification This helps unclutter the home directory from useless dotfiles, moving them where they belong. See https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html --- hecl/lib/Runtime/FileStoreManager.cpp | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/hecl/lib/Runtime/FileStoreManager.cpp b/hecl/lib/Runtime/FileStoreManager.cpp index bd828e4cb..fc88eee6f 100644 --- a/hecl/lib/Runtime/FileStoreManager.cpp +++ b/hecl/lib/Runtime/FileStoreManager.cpp @@ -36,11 +36,20 @@ FileStoreManager::FileStoreManager(SystemStringView domain) : m_domain(domain) { hecl::MakeDir(path.c_str()); m_storeRoot = path; #else - const char* home = getenv("HOME"); - if (!home) - Log.report(logvisor::Fatal, fmt("unable to locate $HOME for file store")); - std::string path(home); - path += "/.heclrun"; + const char* xdg_data_home = getenv("XDG_DATA_HOME"); + std::string path; + if (xdg_data_home) { + if (xdg_data_home[0] != '/') + Log.report(logvisor::Fatal, fmt("invalid $XDG_DATA_HOME for file store (must be absolute)")); + path = xdg_data_home; + } else { + const char* home = getenv("HOME"); + if (!home) + Log.report(logvisor::Fatal, fmt("unable to locate $HOME for file store")); + path = home; + path += "/.local/share" + } + path += "/hecl"; if (mkdir(path.c_str(), 0755) && errno != EEXIST) Log.report(logvisor::Fatal, fmt("unable to mkdir at {}"), path); path += '/';