diff options
author | Pawel Bylica <chfast@gmail.com> | 2015-11-02 09:49:17 +0000 |
---|---|---|
committer | Pawel Bylica <chfast@gmail.com> | 2015-11-02 09:49:17 +0000 |
commit | 0e97e5cb1907ef55966fd27055698515e522c0f0 (patch) | |
tree | 96385d621315224f0adcac2af1413d7e64d7bb23 /llvm/lib/Support/Unix/Path.inc | |
parent | 8671c6e03dc6a1e10d41ad825075cb912b05c570 (diff) | |
download | llvm-0e97e5cb1907ef55966fd27055698515e522c0f0.zip llvm-0e97e5cb1907ef55966fd27055698515e522c0f0.tar.gz llvm-0e97e5cb1907ef55966fd27055698515e522c0f0.tar.bz2 |
[Support] Extend sys::path with user_cache_directory function.
Summary:
The new function sys::path::user_cache_directory tries to discover
a directory suitable for cache storage for current system user.
On Windows and Darwin it returns a path to system-specific user cache directory.
On Linux it follows XDG Base Directory Specification, what is:
- use non-empty $XDG_CACHE_HOME env var,
- use $HOME/.cache.
Reviewers: chapuni, aaron.ballman, rafael
Subscribers: rafael, aaron.ballman, llvm-commits
Differential Revision: http://reviews.llvm.org/D13801
llvm-svn: 251784
Diffstat (limited to 'llvm/lib/Support/Unix/Path.inc')
-rw-r--r-- | llvm/lib/Support/Unix/Path.inc | 74 |
1 files changed, 53 insertions, 21 deletions
diff --git a/llvm/lib/Support/Unix/Path.inc b/llvm/lib/Support/Unix/Path.inc index 042f639..e79abcb 100644 --- a/llvm/lib/Support/Unix/Path.inc +++ b/llvm/lib/Support/Unix/Path.inc @@ -560,6 +560,57 @@ bool home_directory(SmallVectorImpl<char> &result) { return false; } +namespace { +bool getDarwinConfDir(bool TempDir, SmallVectorImpl<char> &Result) { + #if defined(_CS_DARWIN_USER_TEMP_DIR) && defined(_CS_DARWIN_USER_CACHE_DIR) + // On Darwin, use DARWIN_USER_TEMP_DIR or DARWIN_USER_CACHE_DIR. + // macros defined in <unistd.h> on darwin >= 9 + int ConfName = TempDir ? _CS_DARWIN_USER_TEMP_DIR + : _CS_DARWIN_USER_CACHE_DIR; + size_t ConfLen = confstr(ConfName, nullptr, 0); + if (ConfLen > 0) { + do { + Result.resize(ConfLen); + ConfLen = confstr(ConfName, Result.data(), Result.size()); + } while (ConfLen > 0 && ConfLen != Result.size()); + + if (ConfLen > 0) { + assert(Result.back() == 0); + Result.pop_back(); + return true; + } + + Result.clear(); + } + #endif + return false; +} + +bool getUserCacheDir(SmallVectorImpl<char> &Result) { + // First try using XDS_CACHE_HOME env variable, + // as specified in XDG Base Directory Specification at + // http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html + if (const char *XdsCacheDir = std::getenv("XDS_CACHE_HOME")) { + Result.clear(); + Result.append(XdsCacheDir, XdsCacheDir + strlen(XdsCacheDir)); + return true; + } + + // Try Darwin configuration query + if (getDarwinConfDir(false, Result)) + return true; + + // Use "$HOME/.cache" if $HOME is available + if (home_directory(Result)) { + append(Result, ".cache"); + return true; + } + + return false; +} +} + + static const char *getEnvTempDir() { // Check whether the temporary directory is specified by an environment // variable. @@ -594,27 +645,8 @@ void system_temp_directory(bool ErasedOnReboot, SmallVectorImpl<char> &Result) { } } -#if defined(_CS_DARWIN_USER_TEMP_DIR) && defined(_CS_DARWIN_USER_CACHE_DIR) - // On Darwin, use DARWIN_USER_TEMP_DIR or DARWIN_USER_CACHE_DIR. - // macros defined in <unistd.h> on darwin >= 9 - int ConfName = ErasedOnReboot? _CS_DARWIN_USER_TEMP_DIR - : _CS_DARWIN_USER_CACHE_DIR; - size_t ConfLen = confstr(ConfName, nullptr, 0); - if (ConfLen > 0) { - do { - Result.resize(ConfLen); - ConfLen = confstr(ConfName, Result.data(), Result.size()); - } while (ConfLen > 0 && ConfLen != Result.size()); - - if (ConfLen > 0) { - assert(Result.back() == 0); - Result.pop_back(); - return; - } - - Result.clear(); - } -#endif + if (getDarwinConfDir(ErasedOnReboot, Result)) + return; const char *RequestedDir = getDefaultTempDir(ErasedOnReboot); Result.append(RequestedDir, RequestedDir + strlen(RequestedDir)); |