Repository: mesos Updated Branches: refs/heads/master e530b4e43 -> d4f6fde17
Added a function that reports file size. Whether or not the symlink should be followed can be specified as a parameter to the function. Review: https://reviews.apache.org/r/30609 Project: http://git-wip-us.apache.org/repos/asf/mesos/repo Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/d4f6fde1 Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/d4f6fde1 Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/d4f6fde1 Branch: refs/heads/master Commit: d4f6fde172a08330a92e70e4d0053a08ec98d0e6 Parents: e530b4e Author: Bernd Mathiske <[email protected]> Authored: Thu May 21 19:05:22 2015 -0700 Committer: Benjamin Hindman <[email protected]> Committed: Thu May 21 19:28:59 2015 -0700 ---------------------------------------------------------------------- .../3rdparty/stout/include/stout/os/stat.hpp | 43 ++++++++++++++++++++ .../3rdparty/stout/tests/os_tests.cpp | 33 +++++++++++++++ 2 files changed, 76 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mesos/blob/d4f6fde1/3rdparty/libprocess/3rdparty/stout/include/stout/os/stat.hpp ---------------------------------------------------------------------- diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/os/stat.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/os/stat.hpp index 270c4c8..c8203d6 100644 --- a/3rdparty/libprocess/3rdparty/stout/include/stout/os/stat.hpp +++ b/3rdparty/libprocess/3rdparty/stout/include/stout/os/stat.hpp @@ -19,7 +19,9 @@ #include <string> +#include <stout/bytes.hpp> #include <stout/try.hpp> +#include <stout/unreachable.hpp> namespace os { namespace stat { @@ -58,6 +60,47 @@ inline bool islink(const std::string& path) } +// Describes the different semantics supported for the implementation +// of `size` defined below. +enum FollowSymlink +{ + DO_NOT_FOLLOW_SYMLINK, + FOLLOW_SYMLINK +}; + + +// Returns the size in Bytes of a given file system entry. When +// applied to a symbolic link with `follow` set to +// `DO_NOT_FOLLOW_SYMLINK`, this will return the length of the entry +// name (strlen). +inline Try<Bytes> size( + const std::string& path, + const FollowSymlink follow = FOLLOW_SYMLINK) +{ + struct stat s; + + switch (follow) { + case DO_NOT_FOLLOW_SYMLINK: { + if (::lstat(path.c_str(), &s) < 0) { + return ErrnoError("Error invoking lstat for '" + path + "'"); + } + break; + } + case FOLLOW_SYMLINK: { + if (::stat(path.c_str(), &s) < 0) { + return ErrnoError("Error invoking stat for '" + path + "'"); + } + break; + } + default: { + UNREACHABLE(); + } + } + + return Bytes(s.st_size); +} + + inline Try<long> mtime(const std::string& path) { struct stat s; http://git-wip-us.apache.org/repos/asf/mesos/blob/d4f6fde1/3rdparty/libprocess/3rdparty/stout/tests/os_tests.cpp ---------------------------------------------------------------------- diff --git a/3rdparty/libprocess/3rdparty/stout/tests/os_tests.cpp b/3rdparty/libprocess/3rdparty/stout/tests/os_tests.cpp index 343f95b..12b2e1b 100644 --- a/3rdparty/libprocess/3rdparty/stout/tests/os_tests.cpp +++ b/3rdparty/libprocess/3rdparty/stout/tests/os_tests.cpp @@ -16,6 +16,7 @@ #include <stout/duration.hpp> #include <stout/foreach.hpp> +#include <stout/fs.hpp> #include <stout/gtest.hpp> #include <stout/hashmap.hpp> #include <stout/hashset.hpp> @@ -205,6 +206,38 @@ TEST_F(OsTest, readWriteString) } +// Tests whether a file's size is reported by os::stat::size as expected. +// Tests all four combinations of following a link or not and of a file +// or a link as argument. Also tests that an error is returned for a +// non-existing file. +TEST_F(OsTest, size) +{ + const string& file = path::join(os::getcwd(), UUID::random().toString()); + + const Bytes size = 1053; + + ASSERT_SOME(os::write(file, string(size.bytes(), 'X'))); + + // The reported file size should be the same whether following links + // or not, given that the input parameter is not a link. + EXPECT_SOME_EQ(size, os::stat::size(file, os::stat::FOLLOW_SYMLINK)); + EXPECT_SOME_EQ(size, os::stat::size(file, os::stat::DO_NOT_FOLLOW_SYMLINK)); + + EXPECT_ERROR(os::stat::size("aFileThatDoesNotExist")); + + const string& link = path::join(os::getcwd(), UUID::random().toString()); + + ASSERT_SOME(fs::symlink(file, link)); + + // Following links we expect the file's size, not the link's. + EXPECT_SOME_EQ(size, os::stat::size(link, os::stat::FOLLOW_SYMLINK)); + + // Not following links, we expect the string length of the linked path. + EXPECT_SOME_EQ(Bytes(file.size()), + os::stat::size(link, os::stat::DO_NOT_FOLLOW_SYMLINK)); +} + + TEST_F(OsTest, find) { const string& testdir = path::join(os::getcwd(), UUID::random().toString());
