From c6ca443b61372d73a7f1438c995713bad39b5277 Mon Sep 17 00:00:00 2001 From: Matthias Sohn Date: Fri, 10 Dec 2010 21:48:09 +0100 Subject: File utilities for creating directories The java.io.File methods for creating directories report failure by returning false. To ease proper checking of return values provide utility methods wrapping mkdir() and mkdirs() which throw IOException on failure. Also fix the tests to store test data under a trash folder and cleanup after test. Change-Id: I09c7f9909caf7e25feabda9d31e21ce154e7fcd5 Signed-off-by: Matthias Sohn Signed-off-by: Chris Aniszczyk --- .../src/org/eclipse/jgit/util/FileUtils.java | 84 ++++++++++++++++++++++ 1 file changed, 84 insertions(+) (limited to 'org.eclipse.jgit/src/org/eclipse/jgit/util') 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())); + } + } } -- cgit v1.2.3