Added backport of MESOS-6360 (stout part) to 1.1.x.
Project: http://git-wip-us.apache.org/repos/asf/mesos/repo Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/2d655760 Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/2d655760 Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/2d655760 Branch: refs/heads/1.1.x Commit: 2d6557601ead04d51b9d5ca461c2c20c3f5f65a4 Parents: 14355b7 Author: Qian Zhang <zhq527...@gmail.com> Authored: Wed Nov 23 17:38:11 2016 +0800 Committer: Qian Zhang <zhq527...@gmail.com> Committed: Thu Nov 24 09:37:56 2016 +0800 ---------------------------------------------------------------------- 3rdparty/stout/include/Makefile.am | 3 + 3rdparty/stout/include/stout/os.hpp | 1 + 3rdparty/stout/include/stout/os/posix/xattr.hpp | 99 ++++++++++++++++++++ .../stout/include/stout/os/windows/xattr.hpp | 38 ++++++++ 3rdparty/stout/include/stout/os/xattr.hpp | 26 +++++ 3rdparty/stout/tests/os/filesystem_tests.cpp | 38 ++++++++ 6 files changed, 205 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mesos/blob/2d655760/3rdparty/stout/include/Makefile.am ---------------------------------------------------------------------- diff --git a/3rdparty/stout/include/Makefile.am b/3rdparty/stout/include/Makefile.am index 1eb9c14..d5bc728 100644 --- a/3rdparty/stout/include/Makefile.am +++ b/3rdparty/stout/include/Makefile.am @@ -104,6 +104,7 @@ nobase_include_HEADERS = \ stout/os/utime.hpp \ stout/os/wait.hpp \ stout/os/write.hpp \ + stout/os/xattr.hpp \ stout/os/posix/bootid.hpp \ stout/os/posix/chown.hpp \ stout/os/posix/chroot.hpp \ @@ -128,6 +129,7 @@ nobase_include_HEADERS = \ stout/os/posix/stat.hpp \ stout/os/posix/su.hpp \ stout/os/posix/write.hpp \ + stout/os/posix/xattr.hpp \ stout/os/raw/argv.hpp \ stout/os/raw/environment.hpp \ stout/os/windows/bootid.hpp \ @@ -153,6 +155,7 @@ nobase_include_HEADERS = \ stout/os/windows/stat.hpp \ stout/os/windows/su.hpp \ stout/os/windows/write.hpp \ + stout/os/windows/xattr.hpp \ stout/path.hpp \ stout/preprocessor.hpp \ stout/proc.hpp \ http://git-wip-us.apache.org/repos/asf/mesos/blob/2d655760/3rdparty/stout/include/stout/os.hpp ---------------------------------------------------------------------- diff --git a/3rdparty/stout/include/stout/os.hpp b/3rdparty/stout/include/stout/os.hpp index 96e8621..af43bbf 100644 --- a/3rdparty/stout/include/stout/os.hpp +++ b/3rdparty/stout/include/stout/os.hpp @@ -64,6 +64,7 @@ #include <stout/os/touch.hpp> #include <stout/os/utime.hpp> #include <stout/os/wait.hpp> +#include <stout/os/xattr.hpp> #include <stout/os/raw/argv.hpp> #include <stout/os/raw/environment.hpp> http://git-wip-us.apache.org/repos/asf/mesos/blob/2d655760/3rdparty/stout/include/stout/os/posix/xattr.hpp ---------------------------------------------------------------------- diff --git a/3rdparty/stout/include/stout/os/posix/xattr.hpp b/3rdparty/stout/include/stout/os/posix/xattr.hpp new file mode 100644 index 0000000..518940f --- /dev/null +++ b/3rdparty/stout/include/stout/os/posix/xattr.hpp @@ -0,0 +1,99 @@ +// 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_XATTR_HPP__ +#define __STOUT_OS_POSIX_XATTR_HPP__ + +#include <sys/xattr.h> + +namespace os { + +inline Try<Nothing> setxattr( + const std::string& path, + const std::string& name, + const std::string& value, + int flags) +{ +#ifdef __APPLE__ + if (::setxattr( + path.c_str(), + name.c_str(), + value.c_str(), + value.length(), + 0, + flags) < 0) { +#else + if (::setxattr( + path.c_str(), + name.c_str(), + value.c_str(), + value.length(), + flags) < 0) { +#endif + return ErrnoError(); + } + + return Nothing(); +} + + +inline Try<std::string> getxattr( + const std::string& path, + const std::string& name) +{ + // Get the current size of the attribute. +#ifdef __APPLE__ + ssize_t size = ::getxattr(path.c_str(), name.c_str(), nullptr, 0, 0, 0); +#else + ssize_t size = ::getxattr(path.c_str(), name.c_str(), nullptr, 0); +#endif + if (size < 0) { + return ErrnoError(); + } + + char* temp = new char[size + 1]; + ::memset(temp, 0, (size_t)size + 1); + +#ifdef __APPLE__ + if (::getxattr(path.c_str(), name.c_str(), temp, (size_t)size, 0, 0) < 0) { +#else + if (::getxattr(path.c_str(), name.c_str(), temp, (size_t)size) < 0) { +#endif + delete[] temp; + return ErrnoError(); + } + + std::string result(temp); + delete[] temp; + + return result; +} + + +inline Try<Nothing> removexattr( + const std::string& path, + const std::string& name) +{ +#ifdef __APPLE__ + if (::removexattr(path.c_str(), name.c_str(), 0) < 0) { +#else + if (::removexattr(path.c_str(), name.c_str()) < 0) { +#endif + return ErrnoError(); + } + + return Nothing(); +} + +} // namespace os { + +#endif /* __STOUT_OS_POSIX_XATTR_HPP__ */ http://git-wip-us.apache.org/repos/asf/mesos/blob/2d655760/3rdparty/stout/include/stout/os/windows/xattr.hpp ---------------------------------------------------------------------- diff --git a/3rdparty/stout/include/stout/os/windows/xattr.hpp b/3rdparty/stout/include/stout/os/windows/xattr.hpp new file mode 100644 index 0000000..3157f72 --- /dev/null +++ b/3rdparty/stout/include/stout/os/windows/xattr.hpp @@ -0,0 +1,38 @@ +// 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_XATTR_HPP__ +#define __STOUT_OS_WINDOWS_XATTR_HPP__ + +namespace os { + +// NOTE: These functions are deleted because Windows does +// not support POSIX extended attribute semantics. +inline Try<Nothing> setxattr( + const std::string& path, + const std::string& name, + const std::string& value, + int flags) = delete; + + +inline Try<std::string> getxattr( + const std::string& path, + const std::string& name) = delete; + + +inline Try<std::string> removexattr( + const std::string& path, + const std::string& name) = delete; + +} // namespace os { + +#endif /* __STOUT_OS_WINDOWS_XATTR_HPP__ */ http://git-wip-us.apache.org/repos/asf/mesos/blob/2d655760/3rdparty/stout/include/stout/os/xattr.hpp ---------------------------------------------------------------------- diff --git a/3rdparty/stout/include/stout/os/xattr.hpp b/3rdparty/stout/include/stout/os/xattr.hpp new file mode 100644 index 0000000..b86b50a --- /dev/null +++ b/3rdparty/stout/include/stout/os/xattr.hpp @@ -0,0 +1,26 @@ +// 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_XATTR_HPP__ +#define __STOUT_OS_XATTR_HPP__ + +#ifdef __WINDOWS__ +#include <stout/os/windows/xattr.hpp> +#else +#include <stout/os/posix/xattr.hpp> +#endif // __WINDOWS__ + +#endif // __STOUT_OS_XATTR_HPP__ http://git-wip-us.apache.org/repos/asf/mesos/blob/2d655760/3rdparty/stout/tests/os/filesystem_tests.cpp ---------------------------------------------------------------------- diff --git a/3rdparty/stout/tests/os/filesystem_tests.cpp b/3rdparty/stout/tests/os/filesystem_tests.cpp index e3894ef..2246084 100644 --- a/3rdparty/stout/tests/os/filesystem_tests.cpp +++ b/3rdparty/stout/tests/os/filesystem_tests.cpp @@ -31,6 +31,7 @@ #include <stout/os/rm.hpp> #include <stout/os/touch.hpp> #include <stout/os/write.hpp> +#include <stout/os/xattr.hpp> #include <stout/tests/utils.hpp> @@ -436,3 +437,40 @@ TEST_F(FsTest, Close) _CrtSetReportMode(_CRT_ASSERT, previous_report_mode); #endif // __WINDOWS__ } + + +#if defined(__linux__) || defined(__APPLE__) +TEST_F(FsTest, Xattr) +{ + const string file = path::join(os::getcwd(), UUID::random().toString()); + + // Create file. + ASSERT_SOME(os::touch(file)); + ASSERT_TRUE(os::exists(file)); + + // Set an extended attribute. + Try<Nothing> setxattr = os::setxattr( + file, + "user.mesos.test", + "y", + 0); + + // Only run this test if extended attribute is supported. + if (setxattr.isError() && setxattr.error() == os::strerror(ENOTSUP)) { + return; + } + + ASSERT_SOME(setxattr); + + // Get the extended attribute. + Try<string> value = os::getxattr(file, "user.mesos.test"); + ASSERT_SOME(value); + EXPECT_EQ(value.get(), "y"); + + // Remove the extended attribute. + ASSERT_SOME(os::removexattr(file, "user.mesos.test")); + + // Get the extended attribute again which should not exist. + ASSERT_ERROR(os::getxattr(file, "user.mesos.test")); +} +#endif // __linux__ || __APPLE__