You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by an...@apache.org on 2017/12/09 02:36:05 UTC

[03/11] mesos git commit: Added `path::from_uri` and `uri::from_path` to stout.

Added `path::from_uri` and `uri::from_path` to stout.

The former converts a well-formed URI to a path, fixing the path
separator for Windows.

The latter reverses the process, and produces a well-formed URI from a
path, keeping in mind that the URI path separator is always a forward
slash, and so converting from the Windows backslash path separator.

Review: https://reviews.apache.org/r/60622


Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/07ca3306
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/07ca3306
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/07ca3306

Branch: refs/heads/master
Commit: 07ca3306e5c9f0e70e8abffe7ff5ce11c289bc48
Parents: 590f17b
Author: Jeff Coffler <je...@taltos.com>
Authored: Thu Jun 1 16:01:45 2017 -0700
Committer: Andrew Schwartzmeyer <an...@schwartzmeyer.com>
Committed: Fri Dec 8 16:35:39 2017 -0800

----------------------------------------------------------------------
 3rdparty/stout/Makefile.am            |  1 +
 3rdparty/stout/include/Makefile.am    |  1 +
 3rdparty/stout/include/stout/path.hpp | 27 +++++++++++++++++++++
 3rdparty/stout/include/stout/uri.hpp  | 39 ++++++++++++++++++++++++++++++
 3rdparty/stout/tests/CMakeLists.txt   |  1 +
 3rdparty/stout/tests/path_tests.cpp   | 20 +++++++++++++++
 3rdparty/stout/tests/uri_tests.cpp    | 29 ++++++++++++++++++++++
 7 files changed, 118 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/07ca3306/3rdparty/stout/Makefile.am
----------------------------------------------------------------------
diff --git a/3rdparty/stout/Makefile.am b/3rdparty/stout/Makefile.am
index ee774c3..313e4fe 100644
--- a/3rdparty/stout/Makefile.am
+++ b/3rdparty/stout/Makefile.am
@@ -173,6 +173,7 @@ stout_tests_SOURCES =			\
   tests/subcommand_tests.cpp		\
   tests/svn_tests.cpp			\
   tests/try_tests.cpp			\
+  tests/uri_tests.cpp			\
   tests/uuid_tests.cpp			\
   tests/variant_tests.cpp		\
   tests/version_tests.cpp

http://git-wip-us.apache.org/repos/asf/mesos/blob/07ca3306/3rdparty/stout/include/Makefile.am
----------------------------------------------------------------------
diff --git a/3rdparty/stout/include/Makefile.am b/3rdparty/stout/include/Makefile.am
index b27ef1d..e5f1104 100644
--- a/3rdparty/stout/include/Makefile.am
+++ b/3rdparty/stout/include/Makefile.am
@@ -222,6 +222,7 @@ nobase_include_HEADERS =			\
   stout/traits.hpp				\
   stout/unimplemented.hpp			\
   stout/unreachable.hpp				\
+  stout/uri.hpp				\
   stout/utils.hpp				\
   stout/uuid.hpp				\
   stout/variant.hpp				\

http://git-wip-us.apache.org/repos/asf/mesos/blob/07ca3306/3rdparty/stout/include/stout/path.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/stout/include/stout/path.hpp b/3rdparty/stout/include/stout/path.hpp
index 6ee3a44..27438d3 100644
--- a/3rdparty/stout/include/stout/path.hpp
+++ b/3rdparty/stout/include/stout/path.hpp
@@ -25,6 +25,33 @@
 
 namespace path {
 
+// Converts a fully formed URI to a filename for the platform.
+//
+// On all platforms, the optional "file://" prefix is removed if it
+// exists.
+//
+// On Windows, this also converts "/" characters to "\" characters.
+// The Windows file system APIs don't work with "/" in the filename
+// when using long paths (although they do work fine if the file
+// path happens to be short).
+//
+// NOTE: Currently, Mesos uses URIs and files somewhat interchangably.
+// For compatibility, lack of "file://" prefix is not considered an
+// error.
+inline std::string from_uri(const std::string& uri)
+{
+  // Remove the optional "file://" if it exists.
+  // TODO(coffler): Remove the `hostname` component.
+  const std::string path = strings::remove(uri, "file://", strings::PREFIX);
+
+#ifndef __WINDOWS__
+  return path;
+#else
+  return strings::replace(path, "/", "\\");
+#endif // __WINDOWS__
+}
+
+
 // Base case.
 inline std::string join(
     const std::string& path1,

http://git-wip-us.apache.org/repos/asf/mesos/blob/07ca3306/3rdparty/stout/include/stout/uri.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/stout/include/stout/uri.hpp b/3rdparty/stout/include/stout/uri.hpp
new file mode 100644
index 0000000..becfd9f
--- /dev/null
+++ b/3rdparty/stout/include/stout/uri.hpp
@@ -0,0 +1,39 @@
+// 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_URI_HPP__
+#define __STOUT_URI_HPP__
+
+#include <string>
+
+#include <stout/strings.hpp>
+
+
+namespace uri {
+
+// Returns a valid URI containing a filename.
+//
+// On Windows, the / character is replaced with \ since that's the path
+// separator. Note that / will often work, but will absolutely not work if the
+// path is a long path.
+inline std::string from_path(const std::string& filepath)
+{
+#ifdef __WINDOWS__
+  return "file://" + strings::replace(filepath, "\\", "/");
+#else
+  return "file://" + filepath;
+#endif // __WINDOWS__
+}
+
+} // namespace uri {
+
+#endif // __STOUT_URI_HPP__

http://git-wip-us.apache.org/repos/asf/mesos/blob/07ca3306/3rdparty/stout/tests/CMakeLists.txt
----------------------------------------------------------------------
diff --git a/3rdparty/stout/tests/CMakeLists.txt b/3rdparty/stout/tests/CMakeLists.txt
index 100fc50..940fc9f 100644
--- a/3rdparty/stout/tests/CMakeLists.txt
+++ b/3rdparty/stout/tests/CMakeLists.txt
@@ -51,6 +51,7 @@ set(STOUT_ROOT_TESTS_SRC
   strings_tests.cpp
   subcommand_tests.cpp
   try_tests.cpp
+  uri_tests.cpp
   uuid_tests.cpp
   variant_tests.cpp
   version_tests.cpp)

http://git-wip-us.apache.org/repos/asf/mesos/blob/07ca3306/3rdparty/stout/tests/path_tests.cpp
----------------------------------------------------------------------
diff --git a/3rdparty/stout/tests/path_tests.cpp b/3rdparty/stout/tests/path_tests.cpp
index f8c14d5..433a6c6 100644
--- a/3rdparty/stout/tests/path_tests.cpp
+++ b/3rdparty/stout/tests/path_tests.cpp
@@ -220,6 +220,26 @@ TEST(PathTest, Comparison)
 }
 
 
+TEST(PathTest, FromURI)
+{
+#ifdef __WINDOWS__
+  const std::string absolute_path = "C:\\somedir\\somefile";
+#else
+  const std::string absolute_path = "/somedir/somefile";
+#endif // __WINDOWS__
+
+  EXPECT_EQ("", path::from_uri(""));
+  EXPECT_EQ(absolute_path, path::from_uri(absolute_path));
+  EXPECT_EQ(absolute_path, path::from_uri("file://" + absolute_path));
+
+#ifdef __WINDOWS__
+  EXPECT_EQ(absolute_path, path::from_uri("file://C:/somedir/somefile"));
+  EXPECT_EQ(absolute_path, path::from_uri("C:/somedir/somefile"));
+  EXPECT_EQ(absolute_path, path::from_uri("C:\\somedir\\somefile"));
+#endif // __WINDOWS__
+}
+
+
 class PathFileTest : public TemporaryDirectoryTest {};
 
 

http://git-wip-us.apache.org/repos/asf/mesos/blob/07ca3306/3rdparty/stout/tests/uri_tests.cpp
----------------------------------------------------------------------
diff --git a/3rdparty/stout/tests/uri_tests.cpp b/3rdparty/stout/tests/uri_tests.cpp
new file mode 100644
index 0000000..70fa123
--- /dev/null
+++ b/3rdparty/stout/tests/uri_tests.cpp
@@ -0,0 +1,29 @@
+// 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.
+
+#include <gtest/gtest.h>
+
+#include <stout/uri.hpp>
+
+
+TEST(UriTest, TestUriFromPath)
+{
+  EXPECT_EQ("file://", uri::from_path(""));
+  EXPECT_EQ(
+      "file:///absolute/path/on/linux",
+      uri::from_path("/absolute/path/on/linux"));
+
+#ifdef __WINDOWS__
+  EXPECT_EQ(
+      "file://C:/somedir/somefile", uri::from_path("C:\\somedir\\somefile"));
+#endif // __WINDOWS__
+}