Windows: Moved `os::mktemp` to its own file, `stout/os/mktemp.hpp`. Review: https://reviews.apache.org/r/39539
Project: http://git-wip-us.apache.org/repos/asf/mesos/repo Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/e24889e5 Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/e24889e5 Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/e24889e5 Branch: refs/heads/master Commit: e24889e5288948b47d21f82f43f62d1899b176f6 Parents: 0cd54ff Author: Alex Clemmer <[email protected]> Authored: Mon Oct 26 12:37:51 2015 -0500 Committer: Joris Van Remoortere <[email protected]> Committed: Wed Oct 28 17:11:03 2015 -0500 ---------------------------------------------------------------------- .../3rdparty/stout/include/Makefile.am | 1 + .../3rdparty/stout/include/stout/os.hpp | 26 +--- .../3rdparty/stout/include/stout/os/mktemp.hpp | 58 ++++++++ .../3rdparty/stout/include/stout/windows.hpp | 131 +++++++++++-------- 4 files changed, 134 insertions(+), 82 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mesos/blob/e24889e5/3rdparty/libprocess/3rdparty/stout/include/Makefile.am ---------------------------------------------------------------------- diff --git a/3rdparty/libprocess/3rdparty/stout/include/Makefile.am b/3rdparty/libprocess/3rdparty/stout/include/Makefile.am index 13e4380..45a33b0 100644 --- a/3rdparty/libprocess/3rdparty/stout/include/Makefile.am +++ b/3rdparty/libprocess/3rdparty/stout/include/Makefile.am @@ -75,6 +75,7 @@ nobase_include_HEADERS = \ stout/os/posix/signals.hpp \ stout/os/posix/stat.hpp \ stout/os/ls.hpp \ + stout/os/mktemp.hpp \ stout/os/open.hpp \ stout/os/os.hpp \ stout/os/osx.hpp \ http://git-wip-us.apache.org/repos/asf/mesos/blob/e24889e5/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp ---------------------------------------------------------------------- diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp index a702e6b..5b11ea0 100644 --- a/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp +++ b/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp @@ -79,6 +79,7 @@ #include <stout/os/fork.hpp> #include <stout/os/killtree.hpp> #include <stout/os/ls.hpp> +#include <stout/os/mktemp.hpp> #include <stout/os/permissions.hpp> #include <stout/os/os.hpp> #include <stout/os/read.hpp> @@ -135,31 +136,6 @@ inline Try<bool> access(const std::string& path, int how) } -// 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 = "/tmp/XXXXXX") -{ - char* temp = new char[path.size() + 1]; - int fd = ::mkstemp(::strcpy(temp, path.c_str())); - - 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; -} - - inline Try<Nothing> mkdir(const std::string& directory, bool recursive = true) { if (!recursive) { http://git-wip-us.apache.org/repos/asf/mesos/blob/e24889e5/3rdparty/libprocess/3rdparty/stout/include/stout/os/mktemp.hpp ---------------------------------------------------------------------- diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/os/mktemp.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/os/mktemp.hpp new file mode 100644 index 0000000..fa109ba --- /dev/null +++ b/3rdparty/libprocess/3rdparty/stout/include/stout/os/mktemp.hpp @@ -0,0 +1,58 @@ +/** + * 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_MKTEMP_HPP__ +#define __STOUT_OS_MKTEMP_HPP__ + +#include <string> + +#include <stout/error.hpp> +#include <stout/try.hpp> + +#ifdef __WINDOWS__ +#include <stout/windows.hpp> // To be certain we're using the right `mkstemp`. +#endif // __WINDOWS__ + +#include <stout/os/close.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 = "/tmp/XXXXXX") +{ + char* temp = new char[path.size() + 1]; + int fd = ::mkstemp(::strcpy(temp, path.c_str())); + + 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/e24889e5/3rdparty/libprocess/3rdparty/stout/include/stout/windows.hpp ---------------------------------------------------------------------- diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/windows.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/windows.hpp index 7a88195..4fdd34d 100644 --- a/3rdparty/libprocess/3rdparty/stout/include/stout/windows.hpp +++ b/3rdparty/libprocess/3rdparty/stout/include/stout/windows.hpp @@ -82,63 +82,6 @@ typedef SSIZE_T ssize_t; // have to change any socket code. constexpr int SHUT_RD = SD_RECEIVE; -// 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 write(int fd, const void* buffer, size_t count) -> -decltype(_write(fd, buffer, count)) -{ - return _write(fd, buffer, count); -} - - -inline auto open(const char* path, int flags) -> -decltype(_open(path, flags)) -{ - return _open(path, flags); -} - - -inline auto close(int fd) -> -decltype(_close(fd)) -{ - return _close(fd); -} - - -// Filesystem function aliases. -inline auto mkdir(const char* path, mode_t mode) -> -decltype(_mkdir(path)) -{ - return _mkdir(path); -} - - -inline auto mkstemp(char* path) -> -decltype(_mktemp_s(path, strlen(path) + 1)) -{ - return _mktemp_s(path, strlen(path) + 1); -} - - - -inline auto realpath(const char* path, char* resolved) -> -decltype(_fullpath(resolved, path, PATH_MAX)) -{ - return _fullpath(resolved, path, PATH_MAX); -} - - -inline auto access(const char* fileName, int accessMode) -> -decltype(_access(fileName, accessMode)) -{ - return _access(fileName, accessMode); -} - // Permissions API. (cf. MESOS-3176 to track ongoing permissions work.) // @@ -232,4 +175,78 @@ const mode_t S_ISGID = 0x04000000; // No-op. const mode_t S_ISVTX = 0x02000000; // No-op. +// 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 write(int fd, const void* buffer, size_t count) -> +decltype(_write(fd, buffer, count)) +{ + return _write(fd, buffer, count); +} + + +inline auto open(const char* path, int flags) -> +decltype(_open(path, flags)) +{ + return _open(path, flags); +} + + +inline auto close(int fd) -> +decltype(_close(fd)) +{ + return _close(fd); +} + + +inline auto mkdir(const char* path, mode_t mode) -> +decltype(_mkdir(path)) +{ + return _mkdir(path); +} + + +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`. + return _open(path, S_IRUSR | S_IWUSR); +} + + +inline auto realpath(const char* path, char* resolved) -> +decltype(_fullpath(resolved, path, PATH_MAX)) +{ + return _fullpath(resolved, path, PATH_MAX); +} + + +inline auto access(const char* fileName, int accessMode) -> +decltype(_access(fileName, accessMode)) +{ + return _access(fileName, accessMode); +} + + #endif // __STOUT_WINDOWS_HPP__
