You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by ji...@apache.org on 2017/11/29 03:35:15 UTC
[8/9] mesos git commit: Added a findByTarget method for
fs::MountInfoTable.
Added a findByTarget method for fs::MountInfoTable.
This method allows us to get the mount table entry that contains the
specified target.
Review: https://reviews.apache.org/r/63212
Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/38ccb8c3
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/38ccb8c3
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/38ccb8c3
Branch: refs/heads/master
Commit: 38ccb8c36ee15c403cf30b90992cc2a285e93e8f
Parents: e9ba0a1
Author: Jie Yu <yu...@gmail.com>
Authored: Sun Oct 22 21:38:03 2017 -0700
Committer: Jie Yu <yu...@gmail.com>
Committed: Tue Nov 28 18:45:43 2017 -0800
----------------------------------------------------------------------
src/linux/fs.cpp | 39 +++++++++++++++++++++
src/linux/fs.hpp | 7 ++--
src/tests/containerizer/fs_tests.cpp | 56 +++++++++++++++++++++++++++++++
3 files changed, 100 insertions(+), 2 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/mesos/blob/38ccb8c3/src/linux/fs.cpp
----------------------------------------------------------------------
diff --git a/src/linux/fs.cpp b/src/linux/fs.cpp
index c69e7c2..6539f31 100644
--- a/src/linux/fs.cpp
+++ b/src/linux/fs.cpp
@@ -49,6 +49,7 @@ extern "C" {
#include <stout/os.hpp>
#include <stout/os/read.hpp>
+#include <stout/os/realpath.hpp>
#include <stout/os/shell.hpp>
#include <stout/os/stat.hpp>
@@ -233,6 +234,44 @@ Try<MountInfoTable> MountInfoTable::read(
}
+Try<MountInfoTable::Entry> MountInfoTable::findByTarget(
+ const std::string& target)
+{
+ Result<string> realTarget = os::realpath(target);
+ if (!realTarget.isSome()) {
+ return Error(
+ "Failed to get the realpath of '" + target + "'"
+ ": " + (realTarget.isError() ? realTarget.error() : "Not found"));
+ }
+
+ Try<MountInfoTable> table = read(None(), true);
+ if (table.isError()) {
+ return Error("Failed to get mount table: " + table.error());
+ }
+
+ // Trying to find the mount entry that contains the 'realTarget'. We
+ // achieve that by doing a reverse traverse of the mount table to
+ // find the first entry whose target is a prefix of the specified
+ // 'realTarget'.
+ foreach (const Entry& entry, adaptor::reverse(table->entries)) {
+ if (entry.target == realTarget.get()) {
+ return entry;
+ }
+
+ // NOTE: We have to use `path::join(entry.target, "")` here
+ // to make sure the parent is a directory.
+ if (strings::startsWith(realTarget.get(), path::join(entry.target, ""))) {
+ return entry;
+ }
+ }
+
+ // It's unlikely that we cannot find the immediate parent because
+ // '/' is always mounted and will be the immediate parent if no
+ // other mounts found in between.
+ return Error("Not found");
+}
+
+
Try<MountInfoTable::Entry> MountInfoTable::Entry::parse(const string& s)
{
MountInfoTable::Entry entry;
http://git-wip-us.apache.org/repos/asf/mesos/blob/38ccb8c3/src/linux/fs.hpp
----------------------------------------------------------------------
diff --git a/src/linux/fs.hpp b/src/linux/fs.hpp
index cbc8bf7..a79e646 100644
--- a/src/linux/fs.hpp
+++ b/src/linux/fs.hpp
@@ -271,8 +271,11 @@ struct MountInfoTable {
const std::string& lines,
bool hierarchicalSort = true);
- // TODO(jieyu): Introduce 'find' methods to find entries that match
- // the given conditions (e.g., target, root, devno, etc.).
+ // Find the mount table entry by the given target path. If there is
+ // no mount table entry that matches the exact target path, return
+ // the mount table entry that is the immediate parent of the given
+ // target path (similar to `findmnt --target [TARGET]`).
+ static Try<Entry> findByTarget(const std::string& target);
std::vector<Entry> entries;
};
http://git-wip-us.apache.org/repos/asf/mesos/blob/38ccb8c3/src/tests/containerizer/fs_tests.cpp
----------------------------------------------------------------------
diff --git a/src/tests/containerizer/fs_tests.cpp b/src/tests/containerizer/fs_tests.cpp
index 1022b82..5c0598d 100644
--- a/src/tests/containerizer/fs_tests.cpp
+++ b/src/tests/containerizer/fs_tests.cpp
@@ -32,12 +32,15 @@ extern "C" {
#include <stout/none.hpp>
#include <stout/option.hpp>
#include <stout/os.hpp>
+#include <stout/path.hpp>
#include <stout/try.hpp>
#include <stout/tests/utils.hpp>
#include "linux/fs.hpp"
+#include "tests/environment.hpp"
+
using std::set;
using std::string;
@@ -318,6 +321,59 @@ TEST_F(FsTest, ROOT_SlaveMount)
EXPECT_SOME(fs::unmount(directory));
}
+
+TEST_F(FsTest, ROOT_FindTargetInMountInfoTable)
+{
+ Try<string> base = environment->mkdtemp();
+ ASSERT_SOME(base);
+
+ const string sourceDir = path::join(base.get(), "sourceDir");
+ const string targetDir = path::join(base.get(), "targetDir");
+ const string sourceFile = path::join(base.get(), "sourceFile");
+ const string targetFile = path::join(base.get(), "targetFile");
+
+ // `targetDirUnrelated` is a prefix of `targetDir`, but under the
+ // same base directory.
+ const string sourceDirUnrelated = path::join(base.get(), "source");
+ const string targetDirUnrelated = path::join(base.get(), "target");
+
+ const string file = path::join(targetDir, "file");
+
+ ASSERT_SOME(os::mkdir(sourceDir));
+ ASSERT_SOME(os::mkdir(targetDir));
+ ASSERT_SOME(os::touch(sourceFile));
+ ASSERT_SOME(os::touch(targetFile));
+ ASSERT_SOME(os::mkdir(sourceDirUnrelated));
+ ASSERT_SOME(os::mkdir(targetDirUnrelated));
+
+ ASSERT_SOME(fs::mount(sourceDir, targetDir, None(), MS_BIND, None()));
+ ASSERT_SOME(fs::mount(sourceFile, targetFile, None(), MS_BIND, None()));
+
+ ASSERT_SOME(fs::mount(
+ sourceDirUnrelated,
+ targetDirUnrelated,
+ None(),
+ MS_BIND,
+ None()));
+
+ ASSERT_SOME(os::touch(file));
+
+ Try<MountInfoTable> table = MountInfoTable::read();
+ ASSERT_SOME(table);
+
+ Try<MountInfoTable::Entry> entry = table->findByTarget(targetDir);
+ ASSERT_SOME(entry);
+ EXPECT_EQ(entry->target, targetDir);
+
+ entry = table->findByTarget(file);
+ ASSERT_SOME(entry);
+ EXPECT_EQ(entry->target, targetDir);
+
+ entry = table->findByTarget(targetFile);
+ ASSERT_SOME(entry);
+ EXPECT_EQ(entry->target, targetFile);
+}
+
} // namespace tests {
} // namespace internal {
} // namespace mesos {