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/01/20 01:15:17 UTC
[08/15] mesos git commit: Windows: Fixed hanging symlink bug in
`os::rmdir`.
Windows: Fixed hanging symlink bug in `os::rmdir`.
The Windows implementation of `os::rmdir` will fail to delete "hanging"
symlinks (i.e., symlinks whose targets do not exist). Note that on
Windows this bug is specific to symlinks whose targets are _deleted_,
since it is impossible to create a symlink whose target does not exist.
The primary issue that causes this problem is that it is very difficult
to tell whether a symlink points at a directory or a file unless you
resolve the symlink and determine whether the target is a directory or a
file. In situations where the target does not exist, we can't use this
information, and so `os::rmdir` occasionally mis-routes a symlink to
(what was) a directory to a `::remove` call, which will fail with a
cryptic error.
To fix this behavior, this commit will introduce code that simply tries
to remove the reparse point with both `RemoveDirectory` and
`DeleteFile`, and if either succeeds, we report success for the
operation. This represents a "best effort"; in the case that the reparse
point represents something more exotic than a symlink, we will still
fail, but by choosing not to verify whether the target is a directory or
a file, we simplify the code and still obtain the outcome of having
deleted the directory.
This commit is the primary blocker for MESOS-6707, as deleting the Agent
sandbox will sometimes cause us to delete the latest run directory for
the executor before the symlinked `latest` directory itself. This causes
the delete to fail, and then the GC tests to fail, since they tend to
assert the directory does not exist.
Review: https://reviews.apache.org/r/55327/
Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/5b52217f
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/5b52217f
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/5b52217f
Branch: refs/heads/master
Commit: 5b52217f34197a459fffe3c09be9167046be9df6
Parents: d561395
Author: Alex Clemmer <cl...@gmail.com>
Authored: Wed Jan 18 14:53:38 2017 -0800
Committer: Joseph Wu <jo...@apache.org>
Committed: Thu Jan 19 17:04:57 2017 -0800
----------------------------------------------------------------------
.../stout/include/stout/os/windows/rmdir.hpp | 30 ++++++++++++++++----
3rdparty/stout/tests/os/rmdir_tests.cpp | 20 +++++++++++++
2 files changed, 45 insertions(+), 5 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/mesos/blob/5b52217f/3rdparty/stout/include/stout/os/windows/rmdir.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/stout/include/stout/os/windows/rmdir.hpp b/3rdparty/stout/include/stout/os/windows/rmdir.hpp
index 4437484..05eceea 100644
--- a/3rdparty/stout/include/stout/os/windows/rmdir.hpp
+++ b/3rdparty/stout/include/stout/os/windows/rmdir.hpp
@@ -89,10 +89,31 @@ inline Try<Nothing> recursive_remove_directory(
// Path to remove.
const std::string current_absolute_path = current_path + current_file;
- const bool is_directory = os::stat::isdir(current_absolute_path);
-
- // Delete current path, whether it's a directory, file, or symlink.
- if (is_directory) {
+ Try<bool> is_reparse_point =
+ ::internal::windows::reparse_point_attribute_set(current_absolute_path);
+
+ // Delete current path, whether it's a symlink, directory, or file.
+ if (!is_reparse_point.isError() && is_reparse_point.get()) {
+ // NOTE: This is a best-effort attempt to delete symlinks even when they
+ // are "hanging" (i.e., when the target has since been deleted). We call
+ // both `RemoveDirectory` and `DeleteFile` here because we are not sure
+ // whether the deleted target was a directory or a file, which in general
+ // is hard to determine on Windows.
+ //
+ // If either `RemoveDirectory` or `DeleteFile` succeeds, the reparse
+ // point has been successfully removed, and we report success.
+ const BOOL rmdir = ::RemoveDirectory(current_absolute_path.c_str());
+
+ if (rmdir == FALSE) {
+ const BOOL rm = ::DeleteFile(current_absolute_path.c_str());
+
+ if (rm == FALSE) {
+ return WindowsError(
+ "Failed to remove reparse point at '" +
+ current_absolute_path + "'");
+ }
+ }
+ } else if (os::stat::isdir(current_absolute_path)) {
Try<Nothing> removed = recursive_remove_directory(
current_absolute_path, true, continueOnError);
@@ -105,7 +126,6 @@ inline Try<Nothing> recursive_remove_directory(
}
}
} else {
- // NOTE: this also handles symbolic links.
if (::remove(current_absolute_path.c_str()) != 0) {
if (continueOnError) {
LOG(WARNING)
http://git-wip-us.apache.org/repos/asf/mesos/blob/5b52217f/3rdparty/stout/tests/os/rmdir_tests.cpp
----------------------------------------------------------------------
diff --git a/3rdparty/stout/tests/os/rmdir_tests.cpp b/3rdparty/stout/tests/os/rmdir_tests.cpp
index 988d41b..ed43b44 100644
--- a/3rdparty/stout/tests/os/rmdir_tests.cpp
+++ b/3rdparty/stout/tests/os/rmdir_tests.cpp
@@ -299,6 +299,26 @@ TEST_F_TEMP_DISABLED_ON_WINDOWS(RmdirTest, RemoveDirectoryNoTargetSymbolicLink)
}
+// This test verifies that `rmdir` can remove a directory with a
+// "hanging" symlink whose target has been deleted.
+TEST_F(RmdirTest, RemoveDirectoryHangingSymlink)
+{
+ const string newDirectory = path::join(os::getcwd(), "newDirectory");
+ ASSERT_SOME(os::mkdir(newDirectory));
+
+ const string link = path::join(newDirectory, "link");
+
+ // Create a hanging symlink to a directory.
+ ASSERT_SOME(os::mkdir("tmp"));
+ ASSERT_SOME(fs::symlink("tmp", link));
+ ASSERT_SOME(os::rmdir("tmp"));
+
+ // Remove the parent directory to exercise the recursive deletion path of
+ // `os::rmdir`.
+ EXPECT_SOME(os::rmdir(newDirectory));
+}
+
+
// This test verifies that `rmdir` will only remove the symbolic link and not
// the target directory.
TEST_F(RmdirTest, RemoveDirectoryWithSymbolicLinkTargetDirectory)