From 5ed05830398779d5d15b9e82758b52ff4a0dbd5c Mon Sep 17 00:00:00 2001 From: Jeremy Long Date: Sun, 16 Oct 2016 07:36:38 -0400 Subject: [PATCH] added new temp directory creation function --- .../dependencycheck/utils/FileUtils.java | 28 +++++++++++++++++-- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/dependency-check-utils/src/main/java/org/owasp/dependencycheck/utils/FileUtils.java b/dependency-check-utils/src/main/java/org/owasp/dependencycheck/utils/FileUtils.java index 2486e31f0..e0c62e4e1 100644 --- a/dependency-check-utils/src/main/java/org/owasp/dependencycheck/utils/FileUtils.java +++ b/dependency-check-utils/src/main/java/org/owasp/dependencycheck/utils/FileUtils.java @@ -65,7 +65,8 @@ public final class FileUtils { } /** - * Deletes a file. If the File is a directory it will recursively delete the contents. + * Deletes a file. If the File is a directory it will recursively delete the + * contents. * * @param file the File to delete * @return true if the file was deleted successfully, otherwise false @@ -79,13 +80,33 @@ public final class FileUtils { return success; } + /** + * Creates a unique temporary directory in the given directory. + * + * @param base the base directory to create a temporary directory within + * @return the temporary directory + * @throws IOException thrown when a directory cannot be created within the + * base directory + */ + public static File createTempDirectory(File base) throws IOException { + final File tempDir = new File(base, "dctemp" + UUID.randomUUID().toString()); + if (tempDir.exists()) { + return createTempDirectory(base); + } + if (!tempDir.mkdirs()) { + throw new IOException("Could not create temp directory `" + tempDir.getAbsolutePath() + "`"); + } + return tempDir; + } + /** * Generates a new temporary file name that is guaranteed to be unique. * * @param prefix the prefix for the file name to generate * @param extension the extension of the generated file name * @return a temporary File - * @throws java.io.IOException thrown if the temporary folder could not be created + * @throws java.io.IOException thrown if the temporary folder could not be + * created */ public static File getTempFile(String prefix, String extension) throws IOException { final File dir = Settings.getTempDirectory(); @@ -98,7 +119,8 @@ public final class FileUtils { } /** - * Return the bit bucket for the OS. '/dev/null' for Unix and 'NUL' for Windows + * Return the bit bucket for the OS. '/dev/null' for Unix and 'NUL' for + * Windows * * @return a String containing the bit bucket */