Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/org/apache/commons/io/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -1302,7 +1302,7 @@ public static boolean deleteQuietly(final File file) {
return false;
}
try {
if (file.isDirectory()) {
if (file.isDirectory() && !isSymlink(file)) {
cleanDirectory(file);
}
} catch (final Exception ignored) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -188,6 +190,30 @@ void testCorrectlyIdentifySymlinkWithParentSymLink() throws Exception {
assertFalse(FileUtils.isSymlink(realChild));
}

@Test
void testDeleteQuietlyWithASymlinkDirDeletesOnlyLink() throws Exception {
if (SystemProperties.getOsName().startsWith("Win")) {
// Can't use "ln" for symlinks on the command line in Windows.
return;
}

final File randomDirectory = new File(top, "randomDir");
assertTrue(randomDirectory.mkdirs());

final File randomFile = new File(randomDirectory, "randomfile");
FileUtils.touch(randomFile);
assertEquals(1, randomDirectory.list().length);

final File symlinkDirectory = new File(top, "fakeDir");
assertTrue(setupSymlink(randomDirectory, symlinkDirectory));

assertTrue(FileUtils.deleteQuietly(symlinkDirectory));
assertFalse(Files.exists(symlinkDirectory.toPath(), LinkOption.NOFOLLOW_LINKS));
assertTrue(randomDirectory.exists());
assertTrue(randomFile.exists());
assertEquals(1, randomDirectory.list().length, "Contents of symbolic link should not have been removed");
}

@Test
void testIdentifiesBrokenSymlinkFile() throws Exception {
if (SystemProperties.getOsName().startsWith("Win")) {
Expand Down