1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-23 03:02:36 +01:00

[Support] Add path::user_config_directory for $XDG_CONFIG_HOME etc

Reviewers: hokein

Subscribers: hiraditya, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D83128
This commit is contained in:
Sam McCall 2020-07-03 15:49:36 +02:00
parent 91d014b246
commit 8c3580fff2
4 changed files with 79 additions and 0 deletions

View File

@ -371,6 +371,13 @@ void system_temp_directory(bool erasedOnReboot, SmallVectorImpl<char> &result);
/// @result True if a home directory is set, false otherwise.
bool home_directory(SmallVectorImpl<char> &result);
/// Get the directory where packages should read user-specific configurations.
/// e.g. $XDG_CONFIG_HOME.
///
/// @param result Holds the resulting path name.
/// @result True if the appropriate path was determined, it need not exist.
bool user_config_directory(SmallVectorImpl<char> &result);
/// Get the directory where installed packages should put their
/// machine-local cache, e.g. $XDG_CACHE_HOME.
///

View File

@ -1158,6 +1158,30 @@ static bool getDarwinConfDir(bool TempDir, SmallVectorImpl<char> &Result) {
return false;
}
bool user_config_directory(SmallVectorImpl<char> &result) {
#ifdef __APPLE__
// Mac: ~/Library/Preferences/
if (home_directory(result)) {
append("Library", "Preferences");
return true;
}
#else
// XDG_CONFIG_HOME as defined in the XDG Base Directory Specification:
// http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
if (const char *RequestedDir = getenv("XDG_CONFIG_HOME")) {
result.clear();
result.append(RequestedDir, RequestedDir + strlen(RequestedDir));
return true;
}
#endif
// Fallback: ~/.config
if (!home_directory(result)) {
return false;
}
append(result, ".config");
return true;
}
bool cache_directory(SmallVectorImpl<char> &result) {
#ifdef __APPLE__
if (getDarwinConfDir(false/*tempDir*/, result)) {

View File

@ -1372,6 +1372,12 @@ bool home_directory(SmallVectorImpl<char> &result) {
return getKnownFolderPath(FOLDERID_Profile, result);
}
bool user_config_directory(SmallVectorImpl<char> &result) {
// Either local or roaming appdata may be suitable in some cases, depending
// on the data. Local is more conservative, Roaming may not always be correct.
return getKnownFolderPath(FOLDERID_LocalAppData, result);
}
bool cache_directory(SmallVectorImpl<char> &result) {
return getKnownFolderPath(FOLDERID_LocalAppData, result);
}

View File

@ -439,6 +439,26 @@ TEST(Support, HomeDirectoryWithNoEnv) {
EXPECT_EQ(PwDir, HomeDir);
}
TEST(Support, ConfigDirectoryWithEnv) {
WithEnv Env("XDG_CONFIG_HOME", "/xdg/config");
SmallString<128> ConfigDir;
EXPECT_TRUE(path::user_config_directory(ConfigDir));
EXPECT_EQ("/xdg/config", ConfigDir);
}
TEST(Support, ConfigDirectoryNoEnv) {
WithEnv Env("XDG_CONFIG_HOME", nullptr);
SmallString<128> Fallback;
ASSERT_TRUE(path::home_directory(Fallback));
path::append(Fallback, ".config");
SmallString<128> CacheDir;
EXPECT_TRUE(path::user_config_directory(CacheDir));
EXPECT_EQ(Fallback, CacheDir);
}
TEST(Support, CacheDirectoryWithEnv) {
WithEnv Env("XDG_CACHE_HOME", "/xdg/cache");
@ -460,7 +480,29 @@ TEST(Support, CacheDirectoryNoEnv) {
}
#endif
#ifdef __APPLE__
TEST(Support, ConfigDirectory) {
SmallString<128> Fallback;
ASSERT_TRUE(path::home_directory(Fallback));
path::append(Fallback, "Library/Preferences");
SmallString<128> ConfigDir;
EXPECT_TRUE(path::user_config_directory(ConfigDir));
EXPECT_EQ(Fallback, ConfigDir);
}
#endif
#ifdef _WIN32
TEST(Support, ConfigDirectory) {
std::string Expected = getEnvWin(L"LOCALAPPDATA");
// Do not try to test it if we don't know what to expect.
if (!Expected.empty()) {
SmallString<128> CacheDir;
EXPECT_TRUE(path::user_config_directory(CacheDir));
EXPECT_EQ(Expected, CacheDir);
}
}
TEST(Support, CacheDirectory) {
std::string Expected = getEnvWin(L"LOCALAPPDATA");
// Do not try to test it if we don't know what to expect.