Use Windows Vista API to get the user's home directory
authorPawel Bylica <chfast@gmail.com>
Fri, 16 Oct 2015 09:08:59 +0000 (09:08 +0000)
committerPawel Bylica <chfast@gmail.com>
Fri, 16 Oct 2015 09:08:59 +0000 (09:08 +0000)
Summary: This patch replaces usage of deprecated SHGetFolderPathW with SHGetKnownFolderPath. The usage of SHGetKnownFolderPath is wrapped to allow queries for other "known" folders in the near future.

Reviewers: aaron.ballman, gbedwell

Subscribers: chapuni, llvm-commits

Differential Revision: http://reviews.llvm.org/D13753

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

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

index 2c111321c60ce89859cb8b6451376a7b3cd73ab6..839beebfcb6494c3dc3802f24d91a54c431df94f 100644 (file)
@@ -753,16 +753,20 @@ std::error_code openFileForWrite(const Twine &Name, int &ResultFD,
 
 namespace path {
 
-bool home_directory(SmallVectorImpl<char> &result) {
-  wchar_t Path[MAX_PATH];
-  if (::SHGetFolderPathW(0, CSIDL_APPDATA | CSIDL_FLAG_CREATE, 0,
-                         /*SHGFP_TYPE_CURRENT*/0, Path) != S_OK)
+namespace {
+bool getKnownFolderPath(KNOWNFOLDERID folderId, SmallVectorImpl<char> &result) {
+  wchar_t *path = nullptr;
+  if (::SHGetKnownFolderPath(folderId, KF_FLAG_CREATE, nullptr, &path) != S_OK)
     return false;
 
-  if (UTF16ToUTF8(Path, ::wcslen(Path), result))
-    return false;
+  bool ok = !UTF16ToUTF8(path, ::wcslen(path), result);
+  ::CoTaskMemFree(path);
+  return ok;
+}
+}
 
-  return true;
+bool home_directory(SmallVectorImpl<char> &result) {
+  return getKnownFolderPath(FOLDERID_Profile, result);
 }
 
 static bool getTempDirEnvVar(const char *Var, SmallVectorImpl<char> &Res) {
index 5d883c4041c0883b00e32f95a634c302db1bcbbb..416412f327c6b7bead0103c24f3e0a9e15a6bc70 100644 (file)
@@ -8,6 +8,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Support/Path.h"
+#include "llvm/Support/ConvertUTF.h"
 #include "llvm/Support/Errc.h"
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/FileSystem.h"
@@ -299,16 +300,19 @@ TEST(Support, AbsolutePathIteratorEnd) {
 }
 
 TEST(Support, HomeDirectory) {
-#ifdef LLVM_ON_UNIX
-  // This test only makes sense on Unix if $HOME is set.
-  if (::getenv("HOME")) {
-#endif
-    SmallString<128> HomeDir;
-    EXPECT_TRUE(path::home_directory(HomeDir));
-    EXPECT_FALSE(HomeDir.empty());
-#ifdef LLVM_ON_UNIX
-  }
+#ifdef LLVM_ON_WIN32
+  wchar_t *path = ::_wgetenv(L"USERPROFILE");
+  auto pathLen = ::wcslen(path);
+  ArrayRef<char> ref{reinterpret_cast<char*>(path), pathLen * sizeof(wchar_t)};
+  std::string expected;
+  convertUTF16ToUTF8String(ref, expected);
+#else
+  std::string expected{::getenv("HOME")};
 #endif
+  SmallString<128> HomeDir;
+  auto status = path::home_directory(HomeDir);
+  EXPECT_TRUE(status ^ HomeDir.empty());
+  EXPECT_EQ(expected, HomeDir);
 }
 
 class FileSystemTest : public testing::Test {