You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by mp...@apache.org on 2016/04/10 01:18:51 UTC

[8/8] mesos git commit: Windows: Forked `os::rename()`.

Windows: Forked `os::rename()`.

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


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

Branch: refs/heads/master
Commit: 8c6a55d10a2dc30ba2ffe078105266f1ffa4727f
Parents: 47bef6f
Author: Alex Clemmer <cl...@gmail.com>
Authored: Sat Apr 9 15:56:24 2016 -0700
Committer: Michael Park <mp...@apache.org>
Committed: Sat Apr 9 15:58:36 2016 -0700

----------------------------------------------------------------------
 .../3rdparty/stout/include/Makefile.am          |  2 +
 .../3rdparty/stout/include/stout/os.hpp         |  2 +
 .../stout/include/stout/os/posix/rename.hpp     | 38 +++++++++++++
 .../3rdparty/stout/include/stout/os/rename.hpp  | 25 ++------
 .../stout/include/stout/os/windows/rename.hpp   | 54 ++++++++++++++++++
 .../stout/tests/os/filesystem_tests.cpp         | 60 ++++++++++++++++++++
 6 files changed, 161 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/8c6a55d1/3rdparty/libprocess/3rdparty/stout/include/Makefile.am
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/3rdparty/stout/include/Makefile.am b/3rdparty/libprocess/3rdparty/stout/include/Makefile.am
index 6606e60..03f46d9 100644
--- a/3rdparty/libprocess/3rdparty/stout/include/Makefile.am
+++ b/3rdparty/libprocess/3rdparty/stout/include/Makefile.am
@@ -111,6 +111,7 @@ nobase_include_HEADERS =			\
   stout/os/posix/mkdtemp.hpp			\
   stout/os/posix/pstree.hpp			\
   stout/os/posix/read.hpp			\
+  stout/os/posix/rename.hpp			\
   stout/os/posix/rmdir.hpp			\
   stout/os/posix/sendfile.hpp			\
   stout/os/posix/shell.hpp			\
@@ -131,6 +132,7 @@ nobase_include_HEADERS =			\
   stout/os/windows/mkdtemp.hpp			\
   stout/os/windows/pstree.hpp			\
   stout/os/windows/read.hpp			\
+  stout/os/windows/rename.hpp			\
   stout/os/windows/rmdir.hpp			\
   stout/os/windows/sendfile.hpp			\
   stout/os/windows/shell.hpp			\

http://git-wip-us.apache.org/repos/asf/mesos/blob/8c6a55d1/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp
index 06bf04e..e7accaa 100644
--- a/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp
+++ b/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp
@@ -31,6 +31,7 @@
 #include <stout/bytes.hpp>
 #include <stout/duration.hpp>
 #include <stout/error.hpp>
+#include <stout/exit.hpp>
 #include <stout/foreach.hpp>
 #include <stout/none.hpp>
 #include <stout/nothing.hpp>
@@ -51,6 +52,7 @@
 #include <stout/os/mkdtemp.hpp>
 #include <stout/os/mktemp.hpp>
 #include <stout/os/process.hpp>
+#include <stout/os/rename.hpp>
 #include <stout/os/rm.hpp>
 #include <stout/os/rmdir.hpp>
 #include <stout/os/shell.hpp>

http://git-wip-us.apache.org/repos/asf/mesos/blob/8c6a55d1/3rdparty/libprocess/3rdparty/stout/include/stout/os/posix/rename.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/os/posix/rename.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/os/posix/rename.hpp
new file mode 100644
index 0000000..9cff6db
--- /dev/null
+++ b/3rdparty/libprocess/3rdparty/stout/include/stout/os/posix/rename.hpp
@@ -0,0 +1,38 @@
+// 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_OS_POSIX_RENAME_HPP__
+#define __STOUT_OS_POSIX_RENAME_HPP__
+
+#include <stdio.h>
+
+#include <string>
+
+#include <stout/error.hpp>
+#include <stout/nothing.hpp>
+#include <stout/try.hpp>
+
+
+namespace os {
+
+inline Try<Nothing> rename(const std::string& from, const std::string& to)
+{
+  if (::rename(from.c_str(), to.c_str()) != 0) {
+    return ErrnoError();
+  }
+
+  return Nothing();
+}
+
+} // namespace os {
+
+#endif // __STOUT_OS_POSIX_RENAME_HPP__

http://git-wip-us.apache.org/repos/asf/mesos/blob/8c6a55d1/3rdparty/libprocess/3rdparty/stout/include/stout/os/rename.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/os/rename.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/os/rename.hpp
index 7b22152..8a4e084 100644
--- a/3rdparty/libprocess/3rdparty/stout/include/stout/os/rename.hpp
+++ b/3rdparty/libprocess/3rdparty/stout/include/stout/os/rename.hpp
@@ -13,25 +13,10 @@
 #ifndef __STOUT_OS_RENAME_HPP__
 #define __STOUT_OS_RENAME_HPP__
 
-#include <stdio.h>
-
-#include <string>
-
-#include <stout/error.hpp>
-#include <stout/nothing.hpp>
-#include <stout/try.hpp>
-
-namespace os {
-
-inline Try<Nothing> rename(const std::string& from, const std::string& to)
-{
-  if (::rename(from.c_str(), to.c_str()) != 0) {
-    return ErrnoError();
-  }
-
-  return Nothing();
-}
-
-} // namespace os {
+#ifdef __WINDOWS__
+#include <stout/os/windows/rename.hpp>
+#else
+#include <stout/os/posix/rename.hpp>
+#endif // __WINDOWS__
 
 #endif // __STOUT_OS_RENAME_HPP__

http://git-wip-us.apache.org/repos/asf/mesos/blob/8c6a55d1/3rdparty/libprocess/3rdparty/stout/include/stout/os/windows/rename.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/os/windows/rename.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/os/windows/rename.hpp
new file mode 100644
index 0000000..544ff90
--- /dev/null
+++ b/3rdparty/libprocess/3rdparty/stout/include/stout/os/windows/rename.hpp
@@ -0,0 +1,54 @@
+// 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_OS_WINDOWS_RENAME_HPP__
+#define __STOUT_OS_WINDOWS_RENAME_HPP__
+
+#include <string>
+
+#include <stout/error.hpp>
+#include <stout/nothing.hpp>
+#include <stout/try.hpp>
+#include <stout/windows.hpp>
+
+
+namespace os {
+
+inline Try<Nothing> rename(const std::string& from, const std::string& to)
+{
+  // Use `MoveFile` to perform the file move. The MSVCRT implementation of
+  // `::rename` fails if the `to` file already exists[1], while some UNIX
+  // implementations allow that[2].
+  //
+  // Use `MOVEFILE_COPY_ALLOWED` to allow moving the file to another volume and
+  // `MOVEFILE_REPLACE_EXISTING` to comply with the UNIX implementation and
+  // replace an existing file[3].
+  //
+  // [1] https://msdn.microsoft.com/en-us/library/zw5t957f.aspx
+  // [2] http://man7.org/linux/man-pages/man2/rename.2.html
+  // [3] https://msdn.microsoft.com/en-us/library/windows/desktop/aa365240(v=vs.85).aspx
+  const BOOL result = ::MoveFileEx(
+      from.c_str(),
+      to.c_str(),
+      MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING);
+
+  if (!result) {
+    return WindowsError(
+        "`os::rename` failed to move file '" + from + "' to '" + to + "'");
+  }
+
+  return Nothing();
+}
+
+} // namespace os {
+
+#endif // __STOUT_OS_WINDOWS_RENAME_HPP__

http://git-wip-us.apache.org/repos/asf/mesos/blob/8c6a55d1/3rdparty/libprocess/3rdparty/stout/tests/os/filesystem_tests.cpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/3rdparty/stout/tests/os/filesystem_tests.cpp b/3rdparty/libprocess/3rdparty/stout/tests/os/filesystem_tests.cpp
index 013b467..ab6f595 100644
--- a/3rdparty/libprocess/3rdparty/stout/tests/os/filesystem_tests.cpp
+++ b/3rdparty/libprocess/3rdparty/stout/tests/os/filesystem_tests.cpp
@@ -27,6 +27,7 @@
 #include <stout/os/ls.hpp>
 #include <stout/os/mkdir.hpp>
 #include <stout/os/read.hpp>
+#include <stout/os/rename.hpp>
 #include <stout/os/touch.hpp>
 #include <stout/os/write.hpp>
 
@@ -195,3 +196,62 @@ TEST_F(FsTest, List)
   ASSERT_SOME(noFiles);
   EXPECT_EQ(0u, noFiles.get().size());
 }
+
+
+TEST_F(FsTest, Rename)
+{
+  const string testdir = path::join(os::getcwd(), UUID::random().toString());
+  ASSERT_SOME(os::mkdir(testdir)); // Create the directories.
+
+  // Now write some files.
+  const string file1 = testdir + "/file1.txt";
+  const string file2 = testdir + "/file2.txt";
+  const string file3 = testdir + "/file3.jpg";
+
+  ASSERT_SOME(os::touch(file1));
+  ASSERT_SOME(os::touch(file2));
+
+  // Write something to `file1`.
+  const string message = "hello world!";
+  ASSERT_SOME(os::write(file1, message));
+
+  // Search all files in folder
+  Try<list<string>> allFiles = fs::list(path::join(testdir, "*"));
+  ASSERT_SOME(allFiles);
+  EXPECT_EQ(2u, allFiles.get().size());
+
+  // Rename a `file1` to `file3`, which does not exist yet. Verify `file3`
+  // contains the text that was in `file1`, and make sure the count of files in
+  // the directory has stayed the same.
+  EXPECT_SOME(os::rename(file1, file3));
+
+  Try<string> file3Contents = os::read(file3);
+  ASSERT_SOME(file3Contents);
+  EXPECT_EQ(message, file3Contents.get());
+
+  allFiles = fs::list(path::join(testdir, "*"));
+  ASSERT_SOME(allFiles);
+  EXPECT_EQ(2u, allFiles.get().size());
+
+  // Rename `file3` -> `file2`. `file2` exists, so this will replace it. Verify
+  // text in the file, and that the count of files in the directory have gone
+  // down.
+  EXPECT_SOME(os::rename(file3, file2));
+  Try<string> file2Contents = os::read(file2);
+  ASSERT_SOME(file2Contents);
+  EXPECT_EQ(message, file2Contents.get());
+
+  allFiles = fs::list(path::join(testdir, "*"));
+  ASSERT_SOME(allFiles);
+  EXPECT_EQ(1u, allFiles.get().size());
+
+  // Rename a fake file, verify failure.
+  const string fakeFile = testdir + "does_not_exist";
+  EXPECT_ERROR(os::rename(fakeFile, file1));
+
+  EXPECT_FALSE(os::exists(file1));
+
+  allFiles = fs::list(path::join(testdir, "*"));
+  ASSERT_SOME(allFiles);
+  EXPECT_EQ(1u, allFiles.get().size());
+}