[PATCH] D13753: Use Windows Vista API to get the user's home directory

Paweł Bylica via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 16 02:11:11 PDT 2015


This revision was automatically updated to reflect the committed changes.
Closed by commit rL250501: Use Windows Vista API to get the user's home directory (authored by chfast).

Changed prior to commit:
  http://reviews.llvm.org/D13753?vs=37524&id=37564#toc

Repository:
  rL LLVM

http://reviews.llvm.org/D13753

Files:
  llvm/trunk/lib/Support/Windows/Path.inc
  llvm/trunk/unittests/Support/Path.cpp

Index: llvm/trunk/lib/Support/Windows/Path.inc
===================================================================
--- llvm/trunk/lib/Support/Windows/Path.inc
+++ llvm/trunk/lib/Support/Windows/Path.inc
@@ -753,16 +753,20 @@
 
 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: llvm/trunk/unittests/Support/Path.cpp
===================================================================
--- llvm/trunk/unittests/Support/Path.cpp
+++ llvm/trunk/unittests/Support/Path.cpp
@@ -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, 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 {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D13753.37564.patch
Type: text/x-patch
Size: 2376 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20151016/af7e9fef/attachment.bin>


More information about the llvm-commits mailing list