diff --git a/core/src/main/java/hudson/util/IOUtils.java b/core/src/main/java/hudson/util/IOUtils.java index 839e1d784a..4dcc230c5b 100644 --- a/core/src/main/java/hudson/util/IOUtils.java +++ b/core/src/main/java/hudson/util/IOUtils.java @@ -1,11 +1,11 @@ package hudson.util; -import java.io.OutputStream; import java.io.File; import java.io.FileInputStream; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; -import java.io.FileOutputStream; +import java.io.OutputStream; /** * Adds more to commons-io. @@ -31,4 +31,27 @@ public class IOUtils extends org.apache.commons.io.IOUtils { closeQuietly(fos); } } + + /** + * Ensures that the given directory exists (if not, it's created, including all the parent directories.) + * + * @return + * This method returns the 'dir' parameter so that the method call flows better. + */ + public static File mkdirs(File dir) throws IOException { + if(dir.mkdirs() || dir.exists()) + return dir; + + // following Ant task to avoid possible race condition. + try { + Thread.sleep(10); + } catch (InterruptedException e) { + // ignore + } + + if (dir.mkdirs() || dir.exists()) + return dir; + + throw new IOException("Failed to create a directory at "+dir); + } }