mesos-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From josep...@apache.org
Subject [4/4] mesos git commit: Windows: Stout: Removed dependency on Shell API.
Date Fri, 13 Jan 2017 02:28:31 GMT
Windows: Stout: Removed dependency on Shell API.

The API `SHGetKnownFolderPath` requires `Shell32.dll`, which is not
available on Nano server (a version of Windows Server 2016).
An equivalent API `GetAllUsersProfileDirectory` is.

This API is also friendlier, as we own the allocation.

The Unicode version `GetAllUsersProfileDirectoryW` is explicitly used
so that we are guaranteed a Unicode path, which we then convert from
UTF-16 to UTF-8, instead of using the ANSI version which depends on a
varying Windows code-page, and is not recommended.

A `vector<wchar_t>` is used over a `wstring` to avoid dealing
with the placement of the null-terminating character.

Review: https://reviews.apache.org/r/54877/


Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/104532d8
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/104532d8
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/104532d8

Branch: refs/heads/master
Commit: 104532d867214b6d3c6fe96d6ebb144aa8803f7c
Parents: a92b8ad
Author: Andrew Schwartzmeyer <andrew@schwartzmeyer.com>
Authored: Thu Jan 12 17:54:14 2017 -0800
Committer: Joseph Wu <josephwu@apache.org>
Committed: Thu Jan 12 17:59:08 2017 -0800

----------------------------------------------------------------------
 3rdparty/stout/include/stout/windows.hpp    |  5 ----
 3rdparty/stout/include/stout/windows/os.hpp | 35 +++++++++++-------------
 2 files changed, 16 insertions(+), 24 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/104532d8/3rdparty/stout/include/stout/windows.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/stout/include/stout/windows.hpp b/3rdparty/stout/include/stout/windows.hpp
index e641c46..d89c709 100644
--- a/3rdparty/stout/include/stout/windows.hpp
+++ b/3rdparty/stout/include/stout/windows.hpp
@@ -50,11 +50,6 @@
 #error "Mesos doesn't currently support the `_UNICODE` Windows header constant"
 #endif // _UNICODE
 
-// Similarly, the Windows API uses `PWSTR` to mean `wchar_t*`,
-// but rather than assuming this is true, it should be asserted.
-static_assert(std::is_same<PWSTR, wchar_t*>::value,
-              "Expected `PWSTR` to be of type `wchar_t*`.");
-
 // An RAII `HANDLE`.
 class SharedHandle : public std::shared_ptr<void>
 {

http://git-wip-us.apache.org/repos/asf/mesos/blob/104532d8/3rdparty/stout/include/stout/windows/os.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/stout/include/stout/windows/os.hpp b/3rdparty/stout/include/stout/windows/os.hpp
index 5cd9254..b4a66ba 100644
--- a/3rdparty/stout/include/stout/windows/os.hpp
+++ b/3rdparty/stout/include/stout/windows/os.hpp
@@ -16,8 +16,8 @@
 #include <direct.h>
 #include <io.h>
 #include <Psapi.h>
-#include <shlobj.h>
 #include <TlHelp32.h>
+#include <Userenv.h>
 
 #include <sys/utime.h>
 
@@ -724,27 +724,24 @@ inline Try<Nothing> kill_job(pid_t pid)
 
 inline Try<std::string> var()
 {
-  wchar_t* var_folder = nullptr;
-
-  // Retrieves the directory of `ProgramData` using the default options.
-  // NOTE: The location of `ProgramData` is fixed and so does not
-  // depend on the current user.
-  if (::SHGetKnownFolderPath(
-          FOLDERID_ProgramData,
-          KF_FLAG_DEFAULT,
-          nullptr,
-          &var_folder) // `PWSTR` is `typedef wchar_t*`.
-      != S_OK) {
-    return WindowsError("os::var: Call to `SHGetKnownFolderPath` failed");
+  // Get the `ProgramData` path. First, find the size of the output buffer.
+  // This size includes the null-terminating character.
+  DWORD size = 0;
+  if (::GetAllUsersProfileDirectoryW(nullptr, &size)) {
+    // The expected behavior here is for the function to "fail"
+    // and return `false`, and `size` receives necessary buffer size.
+    return WindowsError(
+        "os::var: `GetAllUsersProfileDirectory` succeeded unexpectedly");
   }
 
-  // Convert `wchar_t*` to `wstring`.
-  std::wstring wvar_folder(var_folder);
-
-  // Free the buffer allocated by `SHGetKnownFolderPath`.
-  CoTaskMemFree(static_cast<void*>(var_folder));
+  std::vector<wchar_t> var_folder(size);
+  if (!::GetAllUsersProfileDirectoryW(&var_folder[0], &size)) {
+    return WindowsError(
+        "os::var: `GetAllUsersProfileDirectory` failed");
+  }
 
-  // Convert UTF-16 `wstring` to UTF-8 `string`.
+  // Convert UTF-16 `wchar[]` to UTF-8 `string`.
+  std::wstring wvar_folder(&var_folder[0]);
   std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> converter;
   return converter.to_bytes(wvar_folder);
 }


Mime
View raw message