OSDN Git Service

Make home_directory look in the password database in addition to $HOME.
authorZachary Turner <zturner@google.com>
Wed, 22 Mar 2017 15:24:59 +0000 (15:24 +0000)
committerZachary Turner <zturner@google.com>
Wed, 22 Mar 2017 15:24:59 +0000 (15:24 +0000)
This is something of an edge case, but when the $HOME environment
variable is not set, we can still look in the password database
to get the current user's home directory.

Added a test for this by getting the value of $HOME, then unsetting
it, then calling home_directory() and verifying that it succeeds
and that the value is the same as what we originally read from
the environment.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@298513 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Support/Unix/Path.inc
unittests/Support/Path.cpp

index 7a5816b..93f8982 100644 (file)
@@ -920,12 +920,18 @@ std::error_code real_path(const Twine &path, SmallVectorImpl<char> &dest,
 namespace path {
 
 bool home_directory(SmallVectorImpl<char> &result) {
-  if (char *RequestedDir = getenv("HOME")) {
-    result.clear();
-    result.append(RequestedDir, RequestedDir + strlen(RequestedDir));
-    return true;
+  char *RequestedDir = getenv("HOME");
+  if (!RequestedDir) {
+    struct passwd *pw = getpwuid(getuid());
+    if (pw && pw->pw_dir)
+      RequestedDir = pw->pw_dir;
   }
-  return false;
+  if (!RequestedDir)
+    return false;
+
+  result.clear();
+  result.append(RequestedDir, RequestedDir + strlen(RequestedDir));
+  return true;
 }
 
 static bool getDarwinConfDir(bool TempDir, SmallVectorImpl<char> &Result) {
index 4883ade..afa5a5a 100644 (file)
@@ -328,6 +328,26 @@ TEST(Support, HomeDirectory) {
   }
 }
 
+#ifndef LLVM_ON_WIN32
+TEST(Support, HomeDirectoryWithNoEnv) {
+  std::string Original;
+  char const *path = ::getenv("HOME");
+  // Don't try to test if we don't have something to compare against.
+  if (!path)
+    return;
+  Original = path;
+  ::unsetenv("HOME");
+
+  SmallString<128> HomeDir;
+  auto status = path::home_directory(HomeDir);
+  EXPECT_TRUE(status);
+  EXPECT_EQ(Original, HomeDir);
+
+  // Now put the original environment variable back
+  ::setenv("HOME", Original.c_str(), 1);
+}
+#endif
+
 TEST(Support, UserCacheDirectory) {
   SmallString<13> CacheDir;
   SmallString<20> CacheDir2;