Skip to content

Commit cd209f1

Browse files
committed
[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
1 parent 51f9310 commit cd209f1

4 files changed

Lines changed: 79 additions & 0 deletions

File tree

‎llvm/include/llvm/Support/Path.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,13 @@ void system_temp_directory(bool erasedOnReboot, SmallVectorImpl<char> &result);
371371
/// @result True if a home directory is set, false otherwise.
372372
bool home_directory(SmallVectorImpl<char> &result);
373373

374+
/// Get the directory where packages should read user-specific configurations.
375+
/// e.g. $XDG_CONFIG_HOME.
376+
///
377+
/// @param result Holds the resulting path name.
378+
/// @result True if the appropriate path was determined, it need not exist.
379+
bool user_config_directory(SmallVectorImpl<char> &result);
380+
374381
/// Get the directory where installed packages should put their
375382
/// machine-local cache, e.g. $XDG_CACHE_HOME.
376383
///

‎llvm/lib/Support/Unix/Path.inc‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,6 +1158,30 @@ static bool getDarwinConfDir(bool TempDir, SmallVectorImpl<char> &Result) {
11581158
return false;
11591159
}
11601160

1161+
bool user_config_directory(SmallVectorImpl<char> &result) {
1162+
#ifdef __APPLE__
1163+
// Mac: ~/Library/Preferences/
1164+
if (home_directory(result)) {
1165+
append("Library", "Preferences");
1166+
return true;
1167+
}
1168+
#else
1169+
// XDG_CONFIG_HOME as defined in the XDG Base Directory Specification:
1170+
// http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
1171+
if (const char *RequestedDir = getenv("XDG_CONFIG_HOME")) {
1172+
result.clear();
1173+
result.append(RequestedDir, RequestedDir + strlen(RequestedDir));
1174+
return true;
1175+
}
1176+
#endif
1177+
// Fallback: ~/.config
1178+
if (!home_directory(result)) {
1179+
return false;
1180+
}
1181+
append(result, ".config");
1182+
return true;
1183+
}
1184+
11611185
bool cache_directory(SmallVectorImpl<char> &result) {
11621186
#ifdef __APPLE__
11631187
if (getDarwinConfDir(false/*tempDir*/, result)) {

‎llvm/lib/Support/Windows/Path.inc‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,6 +1372,12 @@ bool home_directory(SmallVectorImpl<char> &result) {
13721372
return getKnownFolderPath(FOLDERID_Profile, result);
13731373
}
13741374

1375+
bool user_config_directory(SmallVectorImpl<char> &result) {
1376+
// Either local or roaming appdata may be suitable in some cases, depending
1377+
// on the data. Local is more conservative, Roaming may not always be correct.
1378+
return getKnownFolderPath(FOLDERID_LocalAppData, result);
1379+
}
1380+
13751381
bool cache_directory(SmallVectorImpl<char> &result) {
13761382
return getKnownFolderPath(FOLDERID_LocalAppData, result);
13771383
}

‎llvm/unittests/Support/Path.cpp‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,26 @@ TEST(Support, HomeDirectoryWithNoEnv) {
439439
EXPECT_EQ(PwDir, HomeDir);
440440
}
441441

442+
TEST(Support, ConfigDirectoryWithEnv) {
443+
WithEnv Env("XDG_CONFIG_HOME", "/xdg/config");
444+
445+
SmallString<128> ConfigDir;
446+
EXPECT_TRUE(path::user_config_directory(ConfigDir));
447+
EXPECT_EQ("/xdg/config", ConfigDir);
448+
}
449+
450+
TEST(Support, ConfigDirectoryNoEnv) {
451+
WithEnv Env("XDG_CONFIG_HOME", nullptr);
452+
453+
SmallString<128> Fallback;
454+
ASSERT_TRUE(path::home_directory(Fallback));
455+
path::append(Fallback, ".config");
456+
457+
SmallString<128> CacheDir;
458+
EXPECT_TRUE(path::user_config_directory(CacheDir));
459+
EXPECT_EQ(Fallback, CacheDir);
460+
}
461+
442462
TEST(Support, CacheDirectoryWithEnv) {
443463
WithEnv Env("XDG_CACHE_HOME", "/xdg/cache");
444464

@@ -460,7 +480,29 @@ TEST(Support, CacheDirectoryNoEnv) {
460480
}
461481
#endif
462482

483+
#ifdef __APPLE__
484+
TEST(Support, ConfigDirectory) {
485+
SmallString<128> Fallback;
486+
ASSERT_TRUE(path::home_directory(Fallback));
487+
path::append(Fallback, "Library/Preferences");
488+
489+
SmallString<128> ConfigDir;
490+
EXPECT_TRUE(path::user_config_directory(ConfigDir));
491+
EXPECT_EQ(Fallback, ConfigDir);
492+
}
493+
#endif
494+
463495
#ifdef _WIN32
496+
TEST(Support, ConfigDirectory) {
497+
std::string Expected = getEnvWin(L"LOCALAPPDATA");
498+
// Do not try to test it if we don't know what to expect.
499+
if (!Expected.empty()) {
500+
SmallString<128> CacheDir;
501+
EXPECT_TRUE(path::user_config_directory(CacheDir));
502+
EXPECT_EQ(Expected, CacheDir);
503+
}
504+
}
505+
464506
TEST(Support, CacheDirectory) {
465507
std::string Expected = getEnvWin(L"LOCALAPPDATA");
466508
// Do not try to test it if we don't know what to expect.

0 commit comments

Comments
 (0)