From 785cb3a75476033ba6eec2fef334d47d8e64388a Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Fri, 22 Nov 2019 16:06:44 +0100 Subject: refactor(tvix): getEnv(): Return std::optional This allows distinguishing between an empty value and no value. Patch ported from upstream at https://github.com/NixOS/nix/commit/ba87b08f8529e4d9f8c58d8c625152058ceadb75 Change-Id: I061cc8e16b1a7a0341adfc3b0edca1c0c51d5c97 Reviewed-on: https://cl.tvl.fyi/c/depot/+/1884 Tested-by: BuildkiteCI Reviewed-by: kanepyork --- third_party/nix/src/libutil/util.cc | 45 +++++++++++++++---------------------- third_party/nix/src/libutil/util.hh | 2 +- 2 files changed, 19 insertions(+), 28 deletions(-) (limited to 'third_party/nix/src/libutil') diff --git a/third_party/nix/src/libutil/util.cc b/third_party/nix/src/libutil/util.cc index 939b6361d13c..aea1e68e3c77 100644 --- a/third_party/nix/src/libutil/util.cc +++ b/third_party/nix/src/libutil/util.cc @@ -45,9 +45,10 @@ std::string SysError::addErrno(const std::string& s) { return s + ": " + strerror(errNo); } -std::string getEnv(const std::string& key, const std::string& def) { +std::optional getEnv(const std::string& key) { char* value = getenv(key.c_str()); - return value != nullptr ? std::string(value) : def; + if (value == nullptr) return {}; + return std::string(value); } std::map getEnv() { @@ -466,8 +467,9 @@ void deletePath(const Path& path, unsigned long long& bytesFreed) { static Path tempName(Path tmpRoot, const Path& prefix, bool includePid, int& counter) { - tmpRoot = - canonPath(tmpRoot.empty() ? getEnv("TMPDIR", "/tmp") : tmpRoot, true); + tmpRoot = canonPath( + tmpRoot.empty() ? getEnv("TMPDIR").value_or("/tmp") : tmpRoot, true); + if (includePid) { return (format("%1%/%2%-%3%-%4%") % tmpRoot % prefix % getpid() % counter++) .str(); @@ -507,16 +509,17 @@ Path createTempDir(const Path& tmpRoot, const Path& prefix, bool includePid, std::string getUserName() { auto pw = getpwuid(geteuid()); - std::string name = pw != nullptr ? pw->pw_name : getEnv("USER", ""); - if (name.empty()) { + std::optional name = + pw != nullptr ? pw->pw_name : getEnv("USER"); + if (!name.has_value()) { throw Error("cannot figure out user name"); } - return name; + return *name; } static Lazy getHome2([]() { - Path homeDir = getEnv("HOME"); - if (homeDir.empty()) { + std::optional homeDir = getEnv("HOME"); + if (!homeDir) { std::vector buf(16384); struct passwd pwbuf; struct passwd* pw; @@ -524,32 +527,24 @@ static Lazy getHome2([]() { (pw == nullptr) || (pw->pw_dir == nullptr) || (pw->pw_dir[0] == 0)) { throw Error("cannot determine user's home directory"); } - homeDir = pw->pw_dir; + return std::string(pw->pw_dir); } - return homeDir; + return homeDir.value(); }); Path getHome() { return getHome2(); } Path getCacheDir() { - Path cacheDir = getEnv("XDG_CACHE_HOME"); - if (cacheDir.empty()) { - cacheDir = getHome() + "/.cache"; - } - return cacheDir; + return getEnv("XDG_CACHE_HOME").value_or(getHome() + "/.cache"); } Path getConfigDir() { - Path configDir = getEnv("XDG_CONFIG_HOME"); - if (configDir.empty()) { - configDir = getHome() + "/.config"; - } - return configDir; + return getEnv("XDG_CONFIG_HOME").value_or(getHome() + "/.config"); } std::vector getConfigDirs() { Path configHome = getConfigDir(); - std::string configDirs = getEnv("XDG_CONFIG_DIRS"); + std::string configDirs = getEnv("XDG_CONFIG_DIRS").value_or(""); std::vector result = absl::StrSplit(configDirs, absl::ByChar(':'), absl::SkipEmpty()); result.insert(result.begin(), configHome); @@ -557,11 +552,7 @@ std::vector getConfigDirs() { } Path getDataDir() { - Path dataDir = getEnv("XDG_DATA_HOME"); - if (dataDir.empty()) { - dataDir = getHome() + "/.local/share"; - } - return dataDir; + return getEnv("XDG_DATA_HOME").value_or(getHome() + "/.local/share"); } // TODO(grfn): Remove in favor of std::filesystem::create_directories diff --git a/third_party/nix/src/libutil/util.hh b/third_party/nix/src/libutil/util.hh index b6d726b46c01..b3349c4f39c4 100644 --- a/third_party/nix/src/libutil/util.hh +++ b/third_party/nix/src/libutil/util.hh @@ -26,7 +26,7 @@ struct Source; extern const std::string nativeSystem; /* Return an environment variable. */ -std::string getEnv(const std::string& key, const std::string& def = ""); +std::optional getEnv(const std::string& key); /* Get the entire environment. */ std::map getEnv(); -- cgit 1.4.1