You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by jo...@apache.org on 2017/05/15 17:33:40 UTC
[2/6] mesos git commit: Windows: Stout: Properly implemented
path::absolute.
Windows: Stout: Properly implemented path::absolute.
On Posix, an absolute path simply starts with a forward-slash.
On Windows, there are a few more cases to consider:
* Absolute paths can start with a drive letter (A-Z, a-z)
and can be followed by a backslash or forward slash (i.e. `C:\`)
* Anything that begins with `\\?\` is an absolute path.
* Network paths like `\\server\...` are also absolute.
* On the other hand, starting with a *single* backslash is not
sufficient to make an absolute path.
NOTE: As with the Posix implementation, `path::absolute` returns
a valid only when the path itself is valid.
Review: https://reviews.apache.org/r/58673/
Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/7b742271
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/7b742271
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/7b742271
Branch: refs/heads/master
Commit: 7b742271b4e1bf56645433806b05a220bee31832
Parents: 8ca6e6f
Author: Jeff Coffler <je...@taltos.com>
Authored: Mon May 8 17:44:41 2017 -0700
Committer: Joseph Wu <jo...@apache.org>
Committed: Mon May 15 09:44:07 2017 -0700
----------------------------------------------------------------------
3rdparty/stout/include/stout/path.hpp | 39 ++++++++++++++++++++++++++++++
3rdparty/stout/tests/CMakeLists.txt | 2 +-
3rdparty/stout/tests/path_tests.cpp | 36 ++++++++++++++++++++++++---
3 files changed, 72 insertions(+), 5 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/mesos/blob/7b742271/3rdparty/stout/include/stout/path.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/stout/include/stout/path.hpp b/3rdparty/stout/include/stout/path.hpp
index 2d2088a..6ee3a44 100644
--- a/3rdparty/stout/include/stout/path.hpp
+++ b/3rdparty/stout/include/stout/path.hpp
@@ -62,9 +62,48 @@ inline std::string join(const std::vector<std::string>& paths)
}
+/**
+ * Returns whether the given path is an absolute path.
+ * If an invalid path is given, the return result is also invalid.
+ */
inline bool absolute(const std::string& path)
{
+#ifndef __WINDOWS__
return strings::startsWith(path, os::PATH_SEPARATOR);
+#else
+ // NOTE: We do not use `PathIsRelative` Windows utility function
+ // here because it does not support long paths.
+ //
+ // See https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
+ // for details on paths. In short, an absolute path for files on Windows
+ // looks like one of the following:
+ // * "[A-Za-z]:\"
+ // * "[A-Za-z]:/"
+ // * "\\?\..."
+ // * "\\server\..." where "server" is a network host.
+ //
+ // NOLINT(whitespace/line_length)
+
+ // A uniform naming convention (UNC) name of any format,
+ // always starts with two backslash characters.
+ if (strings::startsWith(path, "\\\\")) {
+ return true;
+ }
+
+ // A disk designator with a slash, for example "C:\" or "d:/".
+ if (path.length() < 3) {
+ return false;
+ }
+
+ const char letter = path[0];
+ if (!((letter >= 'A' && letter <= 'Z') ||
+ (letter >= 'a' && letter <= 'z'))) {
+ return false;
+ }
+
+ std::string colon = path.substr(1, 2);
+ return colon == ":\\" || colon == ":/";
+#endif // __WINDOWS__
}
} // namespace path {
http://git-wip-us.apache.org/repos/asf/mesos/blob/7b742271/3rdparty/stout/tests/CMakeLists.txt
----------------------------------------------------------------------
diff --git a/3rdparty/stout/tests/CMakeLists.txt b/3rdparty/stout/tests/CMakeLists.txt
index 4bbe713..1ed8da5 100644
--- a/3rdparty/stout/tests/CMakeLists.txt
+++ b/3rdparty/stout/tests/CMakeLists.txt
@@ -41,6 +41,7 @@ set(STOUT_ROOT_TESTS_SRC
numify_tests.cpp
option_tests.cpp
os_tests.cpp
+ path_tests.cpp
protobuf_tests.pb.h
protobuf_tests.proto
recordio_tests.cpp
@@ -56,7 +57,6 @@ set(STOUT_ROOT_TESTS_SRC
if (NOT WIN32)
set(STOUT_ROOT_TESTS_SRC
${STOUT_ROOT_TESTS_SRC}
- path_tests.cpp
protobuf_tests.cpp
protobuf_tests.pb.cc
svn_tests.cpp
http://git-wip-us.apache.org/repos/asf/mesos/blob/7b742271/3rdparty/stout/tests/path_tests.cpp
----------------------------------------------------------------------
diff --git a/3rdparty/stout/tests/path_tests.cpp b/3rdparty/stout/tests/path_tests.cpp
index 0490d93..f8c14d5 100644
--- a/3rdparty/stout/tests/path_tests.cpp
+++ b/3rdparty/stout/tests/path_tests.cpp
@@ -31,7 +31,7 @@ using std::vector;
// Test many corner cases of Path::basename.
-TEST(PathTest, Basename)
+TEST_TEMP_DISABLED_ON_WINDOWS(PathTest, Basename)
{
// Empty path check.
EXPECT_EQ(".", Path("").basename());
@@ -63,7 +63,7 @@ TEST(PathTest, Basename)
// Test many corner cases of Path::dirname.
-TEST(PathTest, Dirname)
+TEST_TEMP_DISABLED_ON_WINDOWS(PathTest, Dirname)
{
// Empty path check.
EXPECT_EQ(".", Path("").dirname());
@@ -100,7 +100,7 @@ TEST(PathTest, Dirname)
}
-TEST(PathTest, Extension)
+TEST_TEMP_DISABLED_ON_WINDOWS(PathTest, Extension)
{
EXPECT_NONE(Path(".").extension());
EXPECT_NONE(Path("..").extension());
@@ -123,7 +123,7 @@ TEST(PathTest, Extension)
}
-TEST(PathTest, Join)
+TEST_TEMP_DISABLED_ON_WINDOWS(PathTest, Join)
{
EXPECT_EQ("a/b/c", path::join("a", "b", "c"));
EXPECT_EQ("/a/b/c", path::join("/a", "b", "c"));
@@ -152,6 +152,33 @@ TEST(PathTest, Join)
TEST(PathTest, Absolute)
{
+#ifdef __WINDOWS__
+ // Check absolute paths.
+ EXPECT_TRUE(path::absolute("C:\\foo\\bar\\baz"));
+ EXPECT_TRUE(path::absolute("c:\\"));
+ EXPECT_TRUE(path::absolute("C:/"));
+ EXPECT_TRUE(path::absolute("c:/"));
+ EXPECT_TRUE(path::absolute("X:\\foo"));
+ EXPECT_TRUE(path::absolute("X:\\foo"));
+ EXPECT_TRUE(path::absolute("y:\\bar"));
+ EXPECT_TRUE(path::absolute("y:/bar"));
+ EXPECT_TRUE(path::absolute("\\\\?\\"));
+ EXPECT_TRUE(path::absolute("\\\\?\\C:\\Program Files"));
+ EXPECT_TRUE(path::absolute("\\\\?\\C:/Program Files"));
+ EXPECT_TRUE(path::absolute("\\\\?\\C:\\Path"));
+ EXPECT_TRUE(path::absolute("\\\\server\\share"));
+
+ // Check invalid paths.
+ EXPECT_FALSE(path::absolute("abc:/"));
+ EXPECT_FALSE(path::absolute("1:/"));
+ EXPECT_TRUE(path::absolute("\\\\?\\relative"));
+
+ // Check relative paths.
+ EXPECT_FALSE(path::absolute("relative"));
+ EXPECT_FALSE(path::absolute("\\file-without-disk"));
+ EXPECT_FALSE(path::absolute("/file-without-disk"));
+ EXPECT_FALSE(path::absolute("N:file-without-dir"));
+#else
// Check absolute paths.
EXPECT_TRUE(path::absolute("/"));
EXPECT_TRUE(path::absolute("/foo"));
@@ -165,6 +192,7 @@ TEST(PathTest, Absolute)
EXPECT_FALSE(path::absolute("../"));
EXPECT_FALSE(path::absolute("./foo"));
EXPECT_FALSE(path::absolute("../foo"));
+#endif // __WINDOWS__
}