Skip to content

Commit 50f9c93

Browse files
author
Niall Pemberton
committed
IO-147 - Deletion of orphaned Softlinks does not work - reported by Stefan Lischke, patch from Sebb
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@608338 13f79535-47bb-0310-9956-ffa450edef68
1 parent 33ab3ce commit 50f9c93

3 files changed

Lines changed: 18 additions & 3 deletions

File tree

RELEASE-NOTES.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ Semantic compatible - ?
2626

2727
Bug fixes from 1.3.2
2828
--------------------
29+
- FileUtils
30+
- forceDelete of orphaned Softlinks does not work [IO-147]
2931

3032
Enhancements from 1.3.2
3133
-----------------------

src/java/org/apache/commons/io/FileUtils.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,16 +1232,18 @@ public static void writeLines(File file, Collection lines, String lineEnding) th
12321232
*
12331233
* @param file file or directory to delete, must not be <code>null</code>
12341234
* @throws NullPointerException if the directory is <code>null</code>
1235+
* @throws FileNotFoundException if the file was not found
12351236
* @throws IOException in case deletion is unsuccessful
12361237
*/
12371238
public static void forceDelete(File file) throws IOException {
12381239
if (file.isDirectory()) {
12391240
deleteDirectory(file);
12401241
} else {
1241-
if (!file.exists()) {
1242-
throw new FileNotFoundException("File does not exist: " + file);
1243-
}
1242+
boolean filePresent = file.exists();
12441243
if (!file.delete()) {
1244+
if (!filePresent){
1245+
throw new FileNotFoundException("File does not exist: " + file);
1246+
}
12451247
String message =
12461248
"Unable to delete file: " + file;
12471249
throw new IOException(message);

src/test/org/apache/commons/io/FileUtilsTestCase.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.io.File;
2020
import java.io.FileInputStream;
21+
import java.io.FileNotFoundException;
2122
import java.io.FileOutputStream;
2223
import java.io.IOException;
2324
import java.io.OutputStream;
@@ -780,6 +781,16 @@ public void testForceDeleteAFile2() throws Exception {
780781
assertTrue("Check No Exist", !destination.exists());
781782
}
782783

784+
public void testForceDeleteAFile3() throws Exception {
785+
File destination = new File(getTestDirectory(), "no_such_file");
786+
assertTrue("Check No Exist", !destination.exists());
787+
try {
788+
FileUtils.forceDelete(destination);
789+
fail("Should generate FileNotFoundException");
790+
} catch (FileNotFoundException ignored){
791+
}
792+
}
793+
783794
// copyFileToDirectory
784795

785796
public void testCopyFile1ToDir() throws Exception {

0 commit comments

Comments
 (0)