Repository: mesos Updated Branches: refs/heads/master 81fbf484d -> 69d7e8e37
Windows: Implemented `os::mkdtemp`. Review: https://reviews.apache.org/r/39559 Project: http://git-wip-us.apache.org/repos/asf/mesos/repo Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/69d7e8e3 Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/69d7e8e3 Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/69d7e8e3 Branch: refs/heads/master Commit: 69d7e8e377bc1ccdd497e951ba64b2c447b3c6e8 Parents: 81fbf48 Author: Alex Clemmer <[email protected]> Authored: Fri Dec 11 13:00:59 2015 -0800 Committer: Joris Van Remoortere <[email protected]> Committed: Fri Dec 11 13:36:52 2015 -0800 ---------------------------------------------------------------------- .../3rdparty/stout/include/Makefile.am | 3 + .../3rdparty/stout/include/stout/os.hpp | 1 + .../3rdparty/stout/include/stout/os/mkdtemp.hpp | 30 +++++++ .../stout/include/stout/os/posix/mkdtemp.hpp | 45 ++++++++++ .../stout/include/stout/os/windows/mkdtemp.hpp | 91 ++++++++++++++++++++ .../3rdparty/stout/include/stout/posix/os.hpp | 18 ---- .../3rdparty/stout/include/stout/windows/os.hpp | 10 --- 7 files changed, 170 insertions(+), 28 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mesos/blob/69d7e8e3/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 a8c35c0..a25e2c1 100644 --- a/3rdparty/libprocess/3rdparty/stout/include/Makefile.am +++ b/3rdparty/libprocess/3rdparty/stout/include/Makefile.am @@ -64,6 +64,7 @@ nobase_include_HEADERS = \ stout/os/linux.hpp \ stout/os/ls.hpp \ stout/os/mkdir.hpp \ + stout/os/mkdtemp.hpp \ stout/os/mktemp.hpp \ stout/os/open.hpp \ stout/os/os.hpp \ @@ -91,6 +92,7 @@ nobase_include_HEADERS = \ stout/os/posix/ftruncate.hpp \ stout/os/posix/killtree.hpp \ stout/os/posix/ls.hpp \ + stout/os/posix/mkdtemp.hpp \ stout/os/posix/process.hpp \ stout/os/posix/pstree.hpp \ stout/os/posix/sendfile.hpp \ @@ -105,6 +107,7 @@ nobase_include_HEADERS = \ stout/os/windows/ftruncate.hpp \ stout/os/windows/killtree.hpp \ stout/os/windows/ls.hpp \ + stout/os/windows/mkdtemp.hpp \ stout/os/windows/process.hpp \ stout/os/windows/pstree.hpp \ stout/os/windows/sendfile.hpp \ http://git-wip-us.apache.org/repos/asf/mesos/blob/69d7e8e3/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 03e6f75..581ec5b 100644 --- a/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp +++ b/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp @@ -81,6 +81,7 @@ #include <stout/os/killtree.hpp> #include <stout/os/ls.hpp> #include <stout/os/mkdir.hpp> +#include <stout/os/mkdtemp.hpp> #include <stout/os/mktemp.hpp> #include <stout/os/os.hpp> #include <stout/os/permissions.hpp> http://git-wip-us.apache.org/repos/asf/mesos/blob/69d7e8e3/3rdparty/libprocess/3rdparty/stout/include/stout/os/mkdtemp.hpp ---------------------------------------------------------------------- diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/os/mkdtemp.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/os/mkdtemp.hpp new file mode 100644 index 0000000..a3cc8cd --- /dev/null +++ b/3rdparty/libprocess/3rdparty/stout/include/stout/os/mkdtemp.hpp @@ -0,0 +1,30 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you 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_MKDTEMP_HPP__ +#define __STOUT_OS_MKDTEMP_HPP__ + + +// For readability, we minimize the number of #ifdef blocks in the code by +// splitting platform specifc system calls into separate directories. +#ifdef __WINDOWS__ +#include <stout/os/windows/mkdtemp.hpp> +#else +#include <stout/os/posix/mkdtemp.hpp> +#endif // __WINDOWS__ + + +#endif // __STOUT_OS_MKDTEMP_HPP__ http://git-wip-us.apache.org/repos/asf/mesos/blob/69d7e8e3/3rdparty/libprocess/3rdparty/stout/include/stout/os/posix/mkdtemp.hpp ---------------------------------------------------------------------- diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/os/posix/mkdtemp.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/os/posix/mkdtemp.hpp new file mode 100644 index 0000000..bbfbb8a --- /dev/null +++ b/3rdparty/libprocess/3rdparty/stout/include/stout/os/posix/mkdtemp.hpp @@ -0,0 +1,45 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you 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_MKDTEMP_HPP__ +#define __STOUT_OS_POSIX_MKDTEMP_HPP__ + +#include <stout/try.hpp> + + +namespace os { + +// Creates a temporary directory 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> mkdtemp(const std::string& path = "/tmp/XXXXXX") +{ + char* temp = new char[path.size() + 1]; + if (::mkdtemp(::strcpy(temp, path.c_str())) != NULL) { + std::string result(temp); + delete[] temp; + return result; + } else { + delete[] temp; + return ErrnoError(); + } +} + +} // namespace os { + + +#endif // __STOUT_OS_POSIX_MKDTEMP_HPP__ http://git-wip-us.apache.org/repos/asf/mesos/blob/69d7e8e3/3rdparty/libprocess/3rdparty/stout/include/stout/os/windows/mkdtemp.hpp ---------------------------------------------------------------------- diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/os/windows/mkdtemp.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/os/windows/mkdtemp.hpp new file mode 100644 index 0000000..2cb73bb --- /dev/null +++ b/3rdparty/libprocess/3rdparty/stout/include/stout/os/windows/mkdtemp.hpp @@ -0,0 +1,91 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you 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_MKDTEMP_HPP__ +#define __STOUT_OS_WINDOWS_MKDTEMP_HPP__ + +#include <stdlib.h> + +#include <random> +#include <string> + +#include <stout/error.hpp> +#include <stout/nothing.hpp> +#include <stout/strings.hpp> +#include <stout/thread_local.hpp> +#include <stout/try.hpp> + +#include <stout/os/mkdir.hpp> + + +namespace os { + +// Creates a temporary directory 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> mkdtemp(const std::string& path = "/tmp/XXXXXX") +{ + // NOTE: We'd like to avoid reallocating `postfixTemplate` and `alphabet`, + // and to avoid recomputing their sizes on each call to `mkdtemp`, so we + // make them `static const` and use the slightly awkward `sizeof` trick to + // compute their sizes once instead of calling `strlen` for each call. + static const char postfixTemplate[] = "XXXXXX"; + static const size_t postfixSize = sizeof(postfixTemplate) - 1; + + if (!strings::endsWith(path, postfixTemplate)) { + return Error( + "Invalid template passed to `os::mkdtemp`: template '" + path + + "' should end with 6 'X' characters"); + } + + static const char alphabet[] = + "0123456789" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz"; + + // NOTE: The maximum addressable index in a string is the total length of the + // string minus 1; but C strings have an extra null character at the end, so + // the size of the array is actually one more than the length of the string, + // which is why we're subtracting 2 here. + static const size_t maxAlphabetIndex = sizeof(alphabet) - 2; + + std::string postfix(postfixTemplate); + static THREAD_LOCAL std::mt19937 generator((std::random_device())()); + + for (int i = 0; i < postfixSize; ++i) { + int index = generator() % maxAlphabetIndex; + postfix[i] = alphabet[index]; + } + + // Replace template, make directory. + std::string tempPath = path + .substr(0, path.length() - postfixSize) + .append(postfix); + + Try<Nothing> mkdir = os::mkdir(tempPath); + + if (mkdir.isError()) { + return Error(mkdir.error()); + } + + return tempPath; +} + +} // namespace os { + + +#endif // __STOUT_OS_WINDOWS_MKDTEMP_HPP__ http://git-wip-us.apache.org/repos/asf/mesos/blob/69d7e8e3/3rdparty/libprocess/3rdparty/stout/include/stout/posix/os.hpp ---------------------------------------------------------------------- diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/posix/os.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/posix/os.hpp index 99e0ae4..42733d4 100644 --- a/3rdparty/libprocess/3rdparty/stout/include/stout/posix/os.hpp +++ b/3rdparty/libprocess/3rdparty/stout/include/stout/posix/os.hpp @@ -106,24 +106,6 @@ inline void unsetenv(const std::string& key) } -// Creates a temporary directory 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> mkdtemp(const std::string& path = "/tmp/XXXXXX") -{ - char* temp = new char[path.size() + 1]; - if (::mkdtemp(::strcpy(temp, path.c_str())) != NULL) { - std::string result(temp); - delete[] temp; - return result; - } else { - delete[] temp; - return ErrnoError(); - } -} - - // By default, recursively deletes a directory akin to: 'rm -r'. If the // programmer sets recursive to false, it deletes a directory akin to: 'rmdir'. // Note that this function expects an absolute path. http://git-wip-us.apache.org/repos/asf/mesos/blob/69d7e8e3/3rdparty/libprocess/3rdparty/stout/include/stout/windows/os.hpp ---------------------------------------------------------------------- diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/windows/os.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/windows/os.hpp index e2d7424..e738cdb 100644 --- a/3rdparty/libprocess/3rdparty/stout/include/stout/windows/os.hpp +++ b/3rdparty/libprocess/3rdparty/stout/include/stout/windows/os.hpp @@ -55,16 +55,6 @@ inline void unsetenv(const std::string& key) } -// Creates a temporary directory 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> mkdtemp(const std::string& path = "/tmp/XXXXXX") -{ - UNIMPLEMENTED; -} - - // By default, recursively deletes a directory akin to: 'rm -r'. If the // programmer sets recursive to false, it deletes a directory akin to: 'rmdir'. // Note that this function expects an absolute path.
