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 2018/11/19 05:41:54 UTC

[mesos] branch master updated (c5ecd42 -> b866fc3)

This is an automated email from the ASF dual-hosted git repository.

jieyu pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/mesos.git.


    from c5ecd42  Fixed an issue about inheriting user for nested containers.
     new 516c0bd  Added Stout `path::normalize` function for POSIX paths.
     new b866fc3  Added unit tests for Stout `path::normalize` function in POSIX.

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 3rdparty/stout/include/stout/path.hpp | 57 +++++++++++++++++++++++++++++++++++
 3rdparty/stout/tests/path_tests.cpp   | 36 ++++++++++++++++++++++
 2 files changed, 93 insertions(+)


[mesos] 01/02: Added Stout `path::normalize` function for POSIX paths.

Posted by ji...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jieyu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mesos.git

commit 516c0bd70c50ae5aa6682b3b8675ef75d99dfc3f
Author: Jason Lai <ja...@jasonlai.net>
AuthorDate: Sun Nov 18 21:12:06 2018 -0800

    Added Stout `path::normalize` function for POSIX paths.
    
    Added `path::normalize` to normalize a given pathname and remove
    redundant separators and up-level references.
    
    This function follows the rules described in `path_resolution(7)`
    for Linux. However, it only performs pure lexical processing without
    touching the actual filesystem.
    
    Review: https://reviews.apache.org/r/65811/
---
 3rdparty/stout/include/stout/path.hpp | 57 +++++++++++++++++++++++++++++++++++
 1 file changed, 57 insertions(+)

diff --git a/3rdparty/stout/include/stout/path.hpp b/3rdparty/stout/include/stout/path.hpp
index ef5a2f3..ba1f665 100644
--- a/3rdparty/stout/include/stout/path.hpp
+++ b/3rdparty/stout/include/stout/path.hpp
@@ -52,6 +52,63 @@ inline std::string from_uri(const std::string& uri)
 }
 
 
+// Normalizes a given pathname and removes redundant separators and up-level
+// references.
+//
+// Pathnames like `A/B/`, `A///B`, `A/./B`, 'A/foobar/../B` are all normalized
+// to `A/B`. An empty pathname is normalized to `.`. Up-level entry also cannot
+// escape a root path, in which case an error will be returned.
+//
+// This function follows the rules described in path_resolution(7) for Linux.
+// However, it only performs pure lexical processing without touching the
+// actual filesystem.
+inline Try<std::string> normalize(
+    const std::string& path,
+    const char _separator = os::PATH_SEPARATOR)
+{
+  if (path.empty()) {
+    return ".";
+  }
+
+  std::vector<std::string> components;
+  const bool isAbs = (path[0] == _separator);
+  const std::string separator(1, _separator);
+
+  // TODO(jasonlai): Handle pathnames (including absolute paths) in Windows.
+
+  foreach (const std::string& component, strings::tokenize(path, separator)) {
+    // Skips empty components and "." (current directory).
+    if (component == "." || component.empty()) {
+      continue;
+    }
+
+    if (component == "..") {
+      if (components.empty()) {
+        if (isAbs) {
+          return Error("Absolute path '" + path + "' tries to escape root");
+        }
+        components.push_back(component);
+      } else if (components.back() == "..") {
+        components.push_back(component);
+      } else {
+        components.pop_back();
+      }
+    } else {
+      components.push_back(component);
+    }
+  }
+
+  if (components.empty()) {
+    return isAbs ? separator : ".";
+  } else if (isAbs) {
+    // Make sure that a separator is prepended if it is an absolute path.
+    components.insert(components.begin(), "");
+  }
+
+  return strings::join(separator, components);
+}
+
+
 // Base case.
 inline std::string join(
     const std::string& path1,


[mesos] 02/02: Added unit tests for Stout `path::normalize` function in POSIX.

Posted by ji...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jieyu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mesos.git

commit b866fc3278dc4fd48d1a50493bcde1efdfa91cc7
Author: Jason Lai <ja...@jasonlai.net>
AuthorDate: Sun Nov 18 21:12:28 2018 -0800

    Added unit tests for Stout `path::normalize` function in POSIX.
    
    Review: https://reviews.apache.org/r/68832/
---
 3rdparty/stout/tests/path_tests.cpp | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)

diff --git a/3rdparty/stout/tests/path_tests.cpp b/3rdparty/stout/tests/path_tests.cpp
index 452865b..19dd910 100644
--- a/3rdparty/stout/tests/path_tests.cpp
+++ b/3rdparty/stout/tests/path_tests.cpp
@@ -200,6 +200,42 @@ TEST(PathTest, Extension)
 }
 
 
+TEST(PathTest, Normalize)
+{
+  EXPECT_SOME_EQ(".", path::normalize(""));
+
+#ifndef __WINDOWS__
+  EXPECT_SOME_EQ("a/b/c", path::normalize("a/b/c/"));
+  EXPECT_SOME_EQ("a/b/c", path::normalize("a///b//c"));
+  EXPECT_SOME_EQ("a/b/c", path::normalize("a/foobar/../b//c/"));
+  EXPECT_SOME_EQ("a/b/c/.d", path::normalize("a/b/c/./.d/"));
+
+  EXPECT_SOME_EQ(".", path::normalize("a/b/../c/../.."));
+  EXPECT_SOME_EQ(".", path::normalize("a/b/../c/../../"));
+
+  EXPECT_SOME_EQ("..", path::normalize("a/../b/c/../../.."));
+  EXPECT_SOME_EQ("../..", path::normalize("a/../../.."));
+  EXPECT_SOME_EQ("../../a", path::normalize("../.././a/"));
+  EXPECT_SOME_EQ("../../b", path::normalize("../../a///../b"));
+  EXPECT_SOME_EQ("../../c", path::normalize("a/../b/.././../../c"));
+
+  EXPECT_SOME_EQ("/a/b/c", path::normalize("/a/b/c"));
+  EXPECT_SOME_EQ("/a/b/c", path::normalize("//a///b/c"));
+  EXPECT_SOME_EQ("/a/b/c", path::normalize("/a/foobar/../b//c/"));
+  EXPECT_SOME_EQ("/a/b/c/.d", path::normalize("/a/b/c/./.d/"));
+
+  EXPECT_SOME_EQ("/", path::normalize("/a/b/../c/../.."));
+  EXPECT_SOME_EQ("/", path::normalize("/a/b/../c/../../"));
+
+  EXPECT_ERROR(path::normalize("/a/../b/c/../../.."));
+  EXPECT_ERROR(path::normalize("/a/../../.."));
+  EXPECT_ERROR(path::normalize("/../.././a/"));
+  EXPECT_ERROR(path::normalize("/../../a///../b"));
+  EXPECT_ERROR(path::normalize("//a/../b/.././../../c"));
+#endif // __WINDOWS__
+}
+
+
 TEST(PathTest, Join)
 {
   EXPECT_EQ("a%b", path::join("a", "b", '%'));