diff options
author | Martin Polden <mpolden@mpolden.no> | 2019-11-06 16:34:41 +0100 |
---|---|---|
committer | Martin Polden <mpolden@mpolden.no> | 2019-11-07 08:38:26 +0100 |
commit | 88fb8dccc75454cee484a9ba177ff006c7fc77b9 (patch) | |
tree | 5d2a4e7671c98bac3a5d1707a780a9c89a7f1476 /node-admin | |
parent | 4187b1391bf383028a3ffd1480aacd70c1db287b (diff) |
Do not follow links when deleting recursively
...like the Javadoc already says.
Diffstat (limited to 'node-admin')
2 files changed, 36 insertions, 2 deletions
diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/UnixPath.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/UnixPath.java index 20a02e91bce..abb3318d31a 100644 --- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/UnixPath.java +++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/UnixPath.java @@ -178,10 +178,19 @@ public class UnixPath { return this; } + /** + * Returns whether this path is a directory. Symlinks are followed, so this returns true for symlinks pointing to a + * directory. + */ public boolean isDirectory() { return uncheck(() -> Files.isDirectory(path)); } + /** Returns whether this is a symlink */ + public boolean isSymbolicSymlink() { + return Files.isSymbolicLink(path); + } + /** * Similar to rm -rf file: * - It's not an error if file doesn't exist @@ -189,12 +198,11 @@ public class UnixPath { * - For symlinks: Only the symlink is removed, not what the symlink points to */ public boolean deleteRecursively() { - if (isDirectory()) { + if (!isSymbolicSymlink() && isDirectory()) { for (UnixPath path : listContentsOfDirectory()) { path.deleteRecursively(); } } - return uncheck(() -> Files.deleteIfExists(path)); } diff --git a/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/file/UnixPathTest.java b/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/file/UnixPathTest.java index 802ca43ef56..3b839f7f446 100644 --- a/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/file/UnixPathTest.java +++ b/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/file/UnixPathTest.java @@ -98,4 +98,30 @@ public class UnixPathTest { assertArrayEquals(new byte[]{42}, path.readBytesIfExists().get()); } + @Test + public void deleteRecursively() throws Exception { + // Create the following file tree: + // + // /dir1 + // |--- dir2 + // |--- file1 + // /link1 -> /dir1/dir2 + // + var dir1 = fs.getPath("/dir1"); + var dir2 = dir1.resolve("dir2"); + var file1 = dir2.resolve("file1"); + Files.createDirectories(dir2); + Files.writeString(file1, "file1"); + var link1 = Files.createSymbolicLink(fs.getPath("/link1"), dir2); + + new UnixPath(link1).deleteRecursively(); + assertTrue("Deleting " + link1 + " recursively does not remove " + dir2, Files.exists(dir2)); + assertTrue("Deleting " + link1 + " recursively does not remove " + file1, Files.exists(file1)); + + new UnixPath(dir1).deleteRecursively(); + assertFalse(dir1 + " deleted recursively", Files.exists(file1)); + assertFalse(dir1 + " deleted recursively", Files.exists(dir2)); + assertFalse(dir1 + " deleted recursively", Files.exists(dir1)); + } + } |