diff options
4 files changed, 166 insertions, 5 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 26f53ed382..cc53a5a74d 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 @@ -49,8 +49,21 @@ import java.io.IOException; import junit.framework.TestCase; public class FileUtilTest extends TestCase { + + private final File trash = new File(new File("target"), "trash"); + + @Override + protected void setUp() throws Exception { + assertTrue(trash.mkdirs()); + } + + @Override + protected void tearDown() throws Exception { + FileUtils.delete(trash, FileUtils.RECURSIVE | FileUtils.RETRY); + } + public void testDeleteFile() throws IOException { - File f = new File("test"); + File f = new File(trash, "test"); assertTrue(f.createNewFile()); FileUtils.delete(f); assertFalse(f.exists()); @@ -70,12 +83,12 @@ public class FileUtilTest extends TestCase { } public void testDeleteRecursive() throws IOException { - File f1 = new File("test/test/a"); + File f1 = new File(trash, "test/test/a"); f1.mkdirs(); f1.createNewFile(); - File f2 = new File("test/test/b"); + File f2 = new File(trash, "test/test/b"); f2.createNewFile(); - File d = new File("test"); + File d = new File(trash, "test"); FileUtils.delete(d, FileUtils.RECURSIVE); assertFalse(d.exists()); @@ -92,4 +105,64 @@ public class FileUtilTest extends TestCase { fail("recursive deletion of non-existing directory must not fail with option SKIP_MISSING"); } } + + public void testMkdir() throws IOException { + File d = new File(trash, "test"); + FileUtils.mkdir(d); + assertTrue(d.exists() && d.isDirectory()); + + try { + FileUtils.mkdir(d); + fail("creation of existing directory must fail"); + } catch (IOException e) { + // expected + } + + FileUtils.mkdir(d, true); + assertTrue(d.exists() && d.isDirectory()); + + assertTrue(d.delete()); + File f = new File(trash, "test"); + assertTrue(f.createNewFile()); + try { + FileUtils.mkdir(d); + fail("creation of directory having same path as existing file must" + + " fail"); + } catch (IOException e) { + // expected + } + assertTrue(f.delete()); + } + + public void testMkdirs() throws IOException { + File root = new File(trash, "test"); + assertTrue(root.mkdir()); + + File d = new File(root, "test/test"); + FileUtils.mkdirs(d); + assertTrue(d.exists() && d.isDirectory()); + + try { + FileUtils.mkdirs(d); + fail("creation of existing directory hierarchy must fail"); + } catch (IOException e) { + // expected + } + + FileUtils.mkdirs(d, true); + assertTrue(d.exists() && d.isDirectory()); + + FileUtils.delete(root, FileUtils.RECURSIVE); + File f = new File(trash, "test"); + assertTrue(f.createNewFile()); + try { + FileUtils.mkdirs(d); + fail("creation of directory having path conflicting with existing" + + " file must fail"); + } catch (IOException e) { + // expected + } + assertTrue(f.delete()); + } + } diff --git a/org.eclipse.jgit/resources/org/eclipse/jgit/JGitText.properties b/org.eclipse.jgit/resources/org/eclipse/jgit/JGitText.properties index 07f0cb1d7d..05a429ddfa 100644 --- a/org.eclipse.jgit/resources/org/eclipse/jgit/JGitText.properties +++ b/org.eclipse.jgit/resources/org/eclipse/jgit/JGitText.properties @@ -268,6 +268,8 @@ missingPrerequisiteCommits=missing prerequisite commits: missingRequiredParameter=Parameter "{0}" is missing missingSecretkey=Missing secretkey. mixedStagesNotAllowed=Mixed stages not allowed +mkDirFailed=Creating directory {0} failed +mkDirsFailed=Creating directories for {0} failed multipleMergeBasesFor=Multiple merge bases for:\n {0}\n {1} found:\n {2}\n {3} need2Arguments=Need 2 arguments needPackOut=need packOut diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/JGitText.java b/org.eclipse.jgit/src/org/eclipse/jgit/JGitText.java index 87053a8c95..2e7503a133 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/JGitText.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/JGitText.java @@ -328,6 +328,8 @@ public class JGitText extends TranslationBundle { /***/ public String missingRequiredParameter; /***/ public String missingSecretkey; /***/ public String mixedStagesNotAllowed; + /***/ public String mkDirFailed; + /***/ public String mkDirsFailed; /***/ public String multipleMergeBasesFor; /***/ public String need2Arguments; /***/ public String needPackOut; @@ -486,7 +488,7 @@ public class JGitText extends TranslationBundle { /***/ public String unknownRepositoryFormat; /***/ public String unknownZlibError; /***/ public String unmergedPath; - /***/ public String unmergedPaths; + /***/ public String unmergedPaths; /***/ public String unpackException; /***/ public String unreadablePackIndex; /***/ public String unrecognizedRef; 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 383afd0839..568f2882ba 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/util/FileUtils.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/util/FileUtils.java @@ -135,4 +135,88 @@ public class FileUtils { } } + /** + * Creates the directory named by this abstract pathname. + * + * @param d + * directory to be created + * @throws IOException + * if creation of {@code d} fails. This may occur if {@code d} + * did exist when the method was called. This can therefore + * cause IOExceptions during race conditions when multiple + * concurrent threads all try to create the same directory. + */ + public static void mkdir(final File d) + throws IOException { + mkdir(d, false); + } + + /** + * Creates the directory named by this abstract pathname. + * + * @param d + * directory to be created + * @param skipExisting + * if {@code true} skip creation of the given directory if it + * already exists in the file system + * @throws IOException + * if creation of {@code d} fails. This may occur if {@code d} + * did exist when the method was called. This can therefore + * cause IOExceptions during race conditions when multiple + * concurrent threads all try to create the same directory. + */ + public static void mkdir(final File d, boolean skipExisting) + throws IOException { + if (!d.mkdir()) { + if (skipExisting && d.isDirectory()) + return; + throw new IOException(MessageFormat.format( + JGitText.get().mkDirFailed, d.getAbsolutePath())); + } + } + + /** + * Creates the directory named by this abstract pathname, including any + * necessary but nonexistent parent directories. Note that if this operation + * fails it may have succeeded in creating some of the necessary parent + * directories. + * + * @param d + * directory to be created + * @throws IOException + * if creation of {@code d} fails. This may occur if {@code d} + * did exist when the method was called. This can therefore + * cause IOExceptions during race conditions when multiple + * concurrent threads all try to create the same directory. + */ + public static void mkdirs(final File d) throws IOException { + mkdirs(d, false); + } + + /** + * Creates the directory named by this abstract pathname, including any + * necessary but nonexistent parent directories. Note that if this operation + * fails it may have succeeded in creating some of the necessary parent + * directories. + * + * @param d + * directory to be created + * @param skipExisting + * if {@code true} skip creation of the given directory if it + * already exists in the file system + * @throws IOException + * if creation of {@code d} fails. This may occur if {@code d} + * did exist when the method was called. This can therefore + * cause IOExceptions during race conditions when multiple + * concurrent threads all try to create the same directory. + */ + public static void mkdirs(final File d, boolean skipExisting) + throws IOException { + if (!d.mkdirs()) { + if (skipExisting && d.isDirectory()) + return; + throw new IOException(MessageFormat.format( + JGitText.get().mkDirsFailed, d.getAbsolutePath())); + } + } } |