geode-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jensde...@apache.org
Subject [14/50] [abbrv] incubator-geode git commit: GEODE-912: FileUtil would throw a more detailed stack trace if unable to delete a file
Date Thu, 25 Feb 2016 20:27:02 GMT
GEODE-912: FileUtil would throw a more detailed stack trace if unable to delete a file


Project: http://git-wip-us.apache.org/repos/asf/incubator-geode/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-geode/commit/7815581e
Tree: http://git-wip-us.apache.org/repos/asf/incubator-geode/tree/7815581e
Diff: http://git-wip-us.apache.org/repos/asf/incubator-geode/diff/7815581e

Branch: refs/heads/feature/GEODE-17
Commit: 7815581ee28450c5ae7b084d8e45e923bf0da0a1
Parents: 9e8543a
Author: Jinmei Liao <jiliao@pivotal.io>
Authored: Tue Feb 23 11:48:34 2016 -0800
Committer: Jinmei Liao <jiliao@pivotal.io>
Committed: Tue Feb 23 15:23:52 2016 -0800

----------------------------------------------------------------------
 .../com/gemstone/gemfire/internal/FileUtil.java | 29 ++++++++++++++------
 .../gemfire/internal/FileUtilJUnitTest.java     | 20 ++++++++++++++
 2 files changed, 40 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/7815581e/geode-core/src/main/java/com/gemstone/gemfire/internal/FileUtil.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/com/gemstone/gemfire/internal/FileUtil.java b/geode-core/src/main/java/com/gemstone/gemfire/internal/FileUtil.java
index b06b503..c87e950 100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/internal/FileUtil.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/FileUtil.java
@@ -25,6 +25,7 @@ import java.io.InputStream;
 import java.io.OutputStream;
 import java.net.URL;
 import java.nio.channels.FileChannel;
+import java.nio.file.Files;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -156,15 +157,16 @@ public class FileUtil {
    *           which just returns false.
    */
   public static void delete(File file) throws IOException {
-    if(file.exists() && file.isDirectory()) {
+    if(!file.exists())
+      return;
+
+    if(file.isDirectory()) {
       for(File child: listFiles(file)) {
         delete(child);
       }
     }
-    
-    if(file.exists() && !file.delete()) {
-      throw new IOException("Could not delete " + file);
-    }
+
+    Files.delete(file.toPath());
   }
   
   /**
@@ -175,15 +177,24 @@ public class FileUtil {
    * This method tries to delete as much as possible.
    */
   public static void delete(File file, StringBuilder failures) {
-    if(file.exists() && file.isDirectory()) {
+    if(!file.exists())
+      return;
+
+    if(file.isDirectory()) {
       for(File child: listFiles(file)) {
         delete(child, failures);
       }
     }
-    
-    if(file.exists() && !file.delete()) {
+
+    try{
+      Files.delete(file.toPath());
+    } catch (IOException e) {
       if (failures != null) {
-        failures.append("Could not delete ").append(file).append('\n');
+        failures.append("Could not delete ")
+            .append(file)
+            .append(" due to ")
+            .append(e.getMessage())
+            .append('\n');
       }
     }
   }

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/7815581e/geode-core/src/test/java/com/gemstone/gemfire/internal/FileUtilJUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/FileUtilJUnitTest.java
b/geode-core/src/test/java/com/gemstone/gemfire/internal/FileUtilJUnitTest.java
index 2c9b0c8..d3a4ebd 100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/internal/FileUtilJUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/FileUtilJUnitTest.java
@@ -84,4 +84,24 @@ public class FileUtilJUnitTest {
     fileName = "/dir/dir/dir/dir/filename.txt.log";
     assertEquals("/dir/dir/dir/dir/filename.txt", FileUtil.stripOffExtension(fileName));
   }
+
+  @Test
+  public void testDeleteFile() throws IOException {
+    File file = File.createTempFile("FileUtilJUnitTest", null);
+    assertTrue(file.exists());
+    FileUtil.delete(file);
+    assertFalse(file.exists());
+  }
+
+  @Test
+  public void testDeleteDir() throws IOException {
+    File dir = new File("testDirName");
+    dir.mkdir();
+    File file = File.createTempFile("testFile", null, dir);
+    assertTrue(dir.exists());
+    assertTrue(file.exists());
+    FileUtil.delete(dir);
+    assertFalse(file.exists());
+    assertFalse(dir.exists());
+  }
 }


Mime
View raw message