summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRobin Rosenberg <robin.rosenberg@dewire.com>2013-02-20 22:54:59 +0100
committerRobin Stocker <robin@nibor.org>2013-03-08 16:26:10 +0100
commit08d5ede281843701ba709ef4f6f4336958f215d5 (patch)
treeb1bacd169cfc60279053cfed2fb38c96cb7d2dfe
parent13ea3b0957d1621c1aea986a97f8d2ec0b37491a (diff)
downloadjgit-08d5ede281843701ba709ef4f6f4336958f215d5.tar.gz
jgit-08d5ede281843701ba709ef4f6f4336958f215d5.zip
Extend FileUtils.delete with option to delete empty directories only
The new option EMPTY_DIRECTORIES_ONLY will make delete() only delete empty directories. Any attempt to delete files will fail. Can be combined with RECURSIVE to wipe out entire tree structures and IGNORE_ERRORS to silently ignore any files or non-empty directories. Change-Id: Icaa9a30e5302ee5c0ba23daad11c7b93e26b7445 Signed-off-by: Robin Stocker <robin@nibor.org>
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/util/FileUtilTest.java46
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/util/FileUtils.java25
2 files changed, 70 insertions, 1 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/FileUtilTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/FileUtilTest.java
index 08e755752b..7790b3024a 100644
--- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/FileUtilTest.java
+++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/FileUtilTest.java
@@ -43,6 +43,8 @@
package org.eclipse.jgit.util;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.endsWith;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
@@ -175,6 +177,7 @@ public class FileUtilTest {
assertTrue(f.delete());
}
+ @Test
public void testCreateNewFile() throws IOException {
File f = new File(trash, "x");
FileUtils.createNewFile(f);
@@ -190,4 +193,47 @@ public class FileUtilTest {
FileUtils.delete(f);
}
+ @Test
+ public void testDeleteEmptyTreeOk() throws IOException {
+ File t = new File(trash, "t");
+ FileUtils.mkdir(t);
+ FileUtils.mkdir(new File(t, "d"));
+ FileUtils.mkdir(new File(new File(t, "d"), "e"));
+ FileUtils.delete(t, FileUtils.EMPTY_DIRECTORIES_ONLY | FileUtils.RECURSIVE);
+ assertFalse(t.exists());
+ }
+
+ @Test
+ public void testDeleteNotEmptyTreeNotOk() throws IOException {
+ File t = new File(trash, "t");
+ FileUtils.mkdir(t);
+ FileUtils.mkdir(new File(t, "d"));
+ File f = new File(new File(t, "d"), "f");
+ FileUtils.createNewFile(f);
+ FileUtils.mkdir(new File(new File(t, "d"), "e"));
+ try {
+ FileUtils.delete(t, FileUtils.EMPTY_DIRECTORIES_ONLY | FileUtils.RECURSIVE);
+ fail("expected failure to delete f");
+ } catch (IOException e) {
+ assertThat(e.getMessage(), endsWith(f.getAbsolutePath()));
+ }
+ assertTrue(t.exists());
+ }
+
+ @Test
+ public void testDeleteNotEmptyTreeNotOkButIgnoreFail() throws IOException {
+ File t = new File(trash, "t");
+ FileUtils.mkdir(t);
+ FileUtils.mkdir(new File(t, "d"));
+ File f = new File(new File(t, "d"), "f");
+ FileUtils.createNewFile(f);
+ File e = new File(new File(t, "d"), "e");
+ FileUtils.mkdir(e);
+ FileUtils.delete(t, FileUtils.EMPTY_DIRECTORIES_ONLY | FileUtils.RECURSIVE
+ | FileUtils.IGNORE_ERRORS);
+ // Should have deleted as much as possible, but not all
+ assertTrue(t.exists());
+ assertTrue(f.exists());
+ assertFalse(e.exists());
+ }
}
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/util/FileUtils.java b/org.eclipse.jgit/src/org/eclipse/jgit/util/FileUtils.java
index 11215c8c10..67f371b618 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/util/FileUtils.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/util/FileUtils.java
@@ -84,6 +84,14 @@ public class FileUtils {
public static final int IGNORE_ERRORS = 8;
/**
+ * Option to only delete empty directories. This option can be combined with
+ * {@link #RECURSIVE}
+ *
+ * @since 2.4
+ */
+ public static final int EMPTY_DIRECTORIES_ONLY = 16;
+
+ /**
* Delete file or empty folder
*
* @param f
@@ -126,7 +134,22 @@ public class FileUtils {
delete(c, options);
}
}
- if (!f.delete()) {
+
+ boolean delete = false;
+ if ((options & EMPTY_DIRECTORIES_ONLY) != 0) {
+ if (f.isDirectory()) {
+ delete = true;
+ } else {
+ if ((options & IGNORE_ERRORS) == 0)
+ throw new IOException(MessageFormat.format(
+ JGitText.get().deleteFileFailed,
+ f.getAbsolutePath()));
+ }
+ } else {
+ delete = true;
+ }
+
+ if (delete && !f.delete()) {
if ((options & RETRY) != 0 && f.exists()) {
for (int i = 1; i < 10; i++) {
try {