You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by jo...@apache.org on 2017/07/11 01:39:10 UTC

[24/50] mesos git commit: Windows: Updated `os::mktemp()` to support long paths.

Windows: Updated `os::mktemp()` to support long paths.

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


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

Branch: refs/heads/master
Commit: 41debf9714829749d812af63ee2c64cbe608a3e7
Parents: ffe2f2d
Author: Andrew Schwartzmeyer <an...@schwartzmeyer.com>
Authored: Mon Jul 10 07:36:43 2017 -0700
Committer: Joseph Wu <jo...@apache.org>
Committed: Mon Jul 10 17:15:35 2017 -0700

----------------------------------------------------------------------
 3rdparty/stout/include/Makefile.am              |  2 +
 3rdparty/stout/include/stout/os/mktemp.hpp      | 49 ++----------
 .../stout/include/stout/os/posix/mktemp.hpp     | 61 +++++++++++++++
 .../stout/include/stout/os/windows/mktemp.hpp   | 79 ++++++++++++++++++++
 3rdparty/stout/include/stout/windows.hpp        | 34 ---------
 5 files changed, 147 insertions(+), 78 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/41debf97/3rdparty/stout/include/Makefile.am
----------------------------------------------------------------------
diff --git a/3rdparty/stout/include/Makefile.am b/3rdparty/stout/include/Makefile.am
index b0c28b2..4068f0e 100644
--- a/3rdparty/stout/include/Makefile.am
+++ b/3rdparty/stout/include/Makefile.am
@@ -132,6 +132,7 @@ nobase_include_HEADERS =			\
   stout/os/posix/killtree.hpp			\
   stout/os/posix/mkdir.hpp			\
   stout/os/posix/mkdtemp.hpp			\
+  stout/os/posix/mktemp.hpp			\
   stout/os/posix/pagesize.hpp			\
   stout/os/posix/pipe.hpp			\
   stout/os/posix/read.hpp			\
@@ -167,6 +168,7 @@ nobase_include_HEADERS =			\
   stout/os/windows/kill.hpp			\
   stout/os/windows/killtree.hpp			\
   stout/os/windows/mkdir.hpp			\
+  stout/os/windows/mktemp.hpp			\
   stout/os/windows/mkdtemp.hpp			\
   stout/os/windows/pagesize.hpp			\
   stout/os/windows/pipe.hpp			\

http://git-wip-us.apache.org/repos/asf/mesos/blob/41debf97/3rdparty/stout/include/stout/os/mktemp.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/stout/include/stout/os/mktemp.hpp b/3rdparty/stout/include/stout/os/mktemp.hpp
index 55cf8b8..57cbf7a 100644
--- a/3rdparty/stout/include/stout/os/mktemp.hpp
+++ b/3rdparty/stout/include/stout/os/mktemp.hpp
@@ -13,53 +13,14 @@
 #ifndef __STOUT_OS_MKTEMP_HPP__
 #define __STOUT_OS_MKTEMP_HPP__
 
-#include <stdlib.h>
-#include <string.h>
-
-#include <string>
-
-#include <stout/error.hpp>
-#include <stout/path.hpp>
-#include <stout/try.hpp>
 
+// For readability, we minimize the number of #ifdef blocks in the code by
+// splitting platform specific system calls into separate directories.
 #ifdef __WINDOWS__
-#include <stout/windows.hpp> // To be certain we're using the right `mkstemp`.
+#include <stout/os/windows/mktemp.hpp>
+#else
+#include <stout/os/posix/mktemp.hpp>
 #endif // __WINDOWS__
 
-#include <stout/os/close.hpp>
-#include <stout/os/int_fd.hpp>
-#include <stout/os/temp.hpp>
-
-
-namespace os {
-
-// Creates a temporary file using the specified path template. The
-// template may be any path with _6_ `Xs' appended to it, for example
-// /tmp/temp.XXXXXX. The trailing `Xs' are replaced with a unique
-// alphanumeric combination.
-inline Try<std::string> mktemp(
-    const std::string& path = path::join(os::temp(), "XXXXXX"))
-{
-  char* temp = new char[path.size() + 1];
-  ::memcpy(temp, path.c_str(), path.size() + 1);
-
-  int_fd fd = ::mkstemp(temp);
-  if (fd < 0) {
-    delete[] temp;
-    return ErrnoError();
-  }
-
-  // We ignore the return value of close(). This is because users
-  // calling this function are interested in the return value of
-  // mkstemp(). Also an unsuccessful close() doesn't affect the file.
-  os::close(fd);
-
-  std::string result(temp);
-  delete[] temp;
-  return result;
-}
-
-} // namespace os {
-
 
 #endif // __STOUT_OS_MKTEMP_HPP__

http://git-wip-us.apache.org/repos/asf/mesos/blob/41debf97/3rdparty/stout/include/stout/os/posix/mktemp.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/stout/include/stout/os/posix/mktemp.hpp b/3rdparty/stout/include/stout/os/posix/mktemp.hpp
new file mode 100644
index 0000000..63b3d1a
--- /dev/null
+++ b/3rdparty/stout/include/stout/os/posix/mktemp.hpp
@@ -0,0 +1,61 @@
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//  http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef __STOUT_OS_POSIX_MKTEMP_HPP__
+#define __STOUT_OS_POSIX_MKTEMP_HPP__
+
+#include <stdlib.h>
+#include <string.h>
+
+#include <string>
+
+#include <stout/error.hpp>
+#include <stout/path.hpp>
+#include <stout/try.hpp>
+
+#include <stout/os/close.hpp>
+#include <stout/os/int_fd.hpp>
+#include <stout/os/temp.hpp>
+
+
+namespace os {
+
+// Creates a temporary file using the specified path template. The
+// template may be any path with _6_ `Xs' appended to it, for example
+// /tmp/temp.XXXXXX. The trailing `Xs' are replaced with a unique
+// alphanumeric combination.
+inline Try<std::string> mktemp(
+    const std::string& path = path::join(os::temp(), "XXXXXX"))
+{
+  char* temp = new char[path.size() + 1];
+  ::memcpy(temp, path.c_str(), path.size() + 1);
+
+  int_fd fd = ::mkstemp(temp);
+  if (fd < 0) {
+    delete[] temp;
+    return ErrnoError();
+  }
+
+  // We ignore the return value of close(). This is because users
+  // calling this function are interested in the return value of
+  // mkstemp(). Also an unsuccessful close() doesn't affect the file.
+  os::close(fd);
+
+  std::string result(temp);
+  delete[] temp;
+  return result;
+}
+
+} // namespace os {
+
+
+#endif // __STOUT_OS_POSIX_MKTEMP_HPP__

http://git-wip-us.apache.org/repos/asf/mesos/blob/41debf97/3rdparty/stout/include/stout/os/windows/mktemp.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/stout/include/stout/os/windows/mktemp.hpp b/3rdparty/stout/include/stout/os/windows/mktemp.hpp
new file mode 100644
index 0000000..5c775c4
--- /dev/null
+++ b/3rdparty/stout/include/stout/os/windows/mktemp.hpp
@@ -0,0 +1,79 @@
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//  http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef __STOUT_OS_WINDOWS_MKTEMP_HPP__
+#define __STOUT_OS_WINDOWS_MKTEMP_HPP__
+
+#include <string>
+#include <vector>
+
+#include <stout/error.hpp>
+#include <stout/path.hpp>
+#include <stout/stringify.hpp>
+#include <stout/try.hpp>
+#include <stout/windows.hpp>
+
+#include <stout/os/close.hpp>
+#include <stout/os/int_fd.hpp>
+#include <stout/os/open.hpp>
+#include <stout/os/temp.hpp>
+
+#include <stout/internal/windows/longpath.hpp>
+
+
+namespace os {
+
+// Creates a temporary file using the specified path template. The
+// template may be any path with _6_ `Xs' appended to it, for example
+// /tmp/temp.XXXXXX. The trailing `Xs' are replaced with a unique
+// alphanumeric combination.
+inline Try<std::string> mktemp(
+    const std::string& path = path::join(os::temp(), "XXXXXX"))
+{
+  const std::wstring longpath = ::internal::windows::longpath(path);
+  std::vector<wchar_t> buffer(longpath.begin(), longpath.end());
+
+  // The range does not include the null terminator, needed to reconstruct
+  // the next string.
+  buffer.push_back(L'\0');
+
+  // NOTE: in the POSIX spec, `mkstemp` will generate a random filename from
+  // the `path` template, `open` that filename, and return the resulting file
+  // descriptor. On Windows, `_mktemp_s` will actually only generate the path,
+  // so here we actually have to call `open` ourselves to get a file descriptor
+  // we can return as a result.
+  if (::_wmktemp_s(buffer.data(), buffer.size()) != 0) {
+    return WindowsError();
+  }
+
+  const std::string temp_file = stringify(std::wstring(buffer.data()));
+
+  // NOTE: We open the file with read/write access for the given user, an
+  // attempt to match POSIX's specification of `mkstemp`. We use `_S_IREAD` and
+  // `_S_IWRITE` here instead of the POSIX equivalents. On Windows the file is
+  // is not present, we use `_O_CREAT` option when opening the file.
+  Try<int_fd> fd = os::open(temp_file, _O_CREAT, _S_IREAD | _S_IWRITE);
+  if (fd.isError()) {
+    return Error(fd.error());
+  }
+
+  // We ignore the return value of close(). This is because users
+  // calling this function are interested in the return value of
+  // mkstemp(). Also an unsuccessful close() doesn't affect the file.
+  os::close(fd.get());
+
+  return strings::remove(temp_file, os::LONGPATH_PREFIX, strings::Mode::PREFIX);
+}
+
+} // namespace os {
+
+#endif // __STOUT_OS_WINDOWS_MKTEMP_HPP__

http://git-wip-us.apache.org/repos/asf/mesos/blob/41debf97/3rdparty/stout/include/stout/windows.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/stout/include/stout/windows.hpp b/3rdparty/stout/include/stout/windows.hpp
index 34273dc..6f5b323 100644
--- a/3rdparty/stout/include/stout/windows.hpp
+++ b/3rdparty/stout/include/stout/windows.hpp
@@ -338,40 +338,6 @@ decltype(strerror_s(buffer, length, errnum))
 }
 
 
-// File I/O function aliases.
-//
-// NOTE: The use of `auto` and the trailing return type in the following
-// functions are meant to make it easier for Linux developers to use and
-// maintain the code. It is an explicit marker that we are using the compiler
-// to guarantee that the return type is identical to whatever is in the Windows
-// implementation of the standard.
-inline auto mktemp(char* path) ->
-decltype(_mktemp(path))
-{
-  return _mktemp(path);
-}
-
-
-inline auto mkstemp(char* path) ->
-decltype(_mktemp_s(path, strlen(path) + 1))
-{
-  // NOTE: in the POSIX spec, `mkstemp` will generate a random filename from
-  // the `path` template, `open` that filename, and return the resulting file
-  // descriptor. On Windows, `_mktemp_s` will actually only generate the path,
-  // so here we actually have to call `open` ourselves to get a file descriptor
-  // we can return as a result.
-  if (_mktemp_s(path, strlen(path) + 1) != 0) {
-    return -1;
-  }
-
-  // NOTE: We open the file with read / write access for the given user, an
-  // attempt to match POSIX's specification of `mkstemp`. We use `_S_IREAD` and
-  // `_S_IWRITE` here instead of the POSIX equivalents. On Windows the file is
-  // is not present, we use `_O_CREAT` option when opening the file.
-  return _open(path, _O_CREAT, _S_IREAD | _S_IWRITE);
-}
-
-
 // NOTE: Signals do not exist on Windows, so all signals are unknown.
 // If the signal number is unknown, the Posix specification leaves the
 // return value of `strsignal` unspecified.