diff --git a/source/net/filebot/Main.java b/source/net/filebot/Main.java index bea62340..2e6a13ae 100644 --- a/source/net/filebot/Main.java +++ b/source/net/filebot/Main.java @@ -134,7 +134,7 @@ public class Main { // make sure java.io.tmpdir exists File tmpdir = new File(System.getProperty("java.io.tmpdir")); - tmpdir.mkdirs(); + createFolders(tmpdir); // initialize this stuff before anything else initializeCache(); @@ -437,9 +437,9 @@ public class Main { try { for (int i = 0; true; i++) { File cache = new File(cacheRoot, String.format("%d", i)); - if (!cache.isDirectory() && !cache.mkdirs()) { - throw new IOException("Failed to create cache dir: " + cache); - } + + // make sure cache is accessible + createFolders(cache); final File lockFile = new File(cache, ".lock"); boolean isNewCache = !lockFile.exists(); diff --git a/source/net/filebot/Settings.java b/source/net/filebot/Settings.java index 791c016e..a0eb237c 100644 --- a/source/net/filebot/Settings.java +++ b/source/net/filebot/Settings.java @@ -1,5 +1,6 @@ package net.filebot; +import static net.filebot.util.FileUtilities.*; import static net.filebot.util.StringUtilities.*; import java.awt.GraphicsEnvironment; @@ -143,9 +144,12 @@ public final class Settings { } // create folder if necessary - if (!applicationFolder.exists()) { - applicationFolder.mkdirs(); + try { + createFolders(applicationFolder); + } catch (Exception e) { + throw new IllegalStateException("application.dir", e); } + return applicationFolder; } @@ -160,9 +164,12 @@ public final class Settings { } // create folder if necessary - if (!cacheFolder.exists()) { - cacheFolder.mkdirs(); + try { + createFolders(cacheFolder); + } catch (Exception e) { + throw new IllegalStateException("application.cache", e); } + return cacheFolder; } diff --git a/source/net/filebot/cli/ScriptShellMethods.java b/source/net/filebot/cli/ScriptShellMethods.java index 41566c12..0c8f37ac 100644 --- a/source/net/filebot/cli/ScriptShellMethods.java +++ b/source/net/filebot/cli/ScriptShellMethods.java @@ -310,7 +310,7 @@ public class ScriptShellMethods { file = file.getAbsoluteFile(); // make sure parent folders exist - file.getParentFile().mkdirs(); + FileUtilities.createFolders(file.getParentFile()); return FileUtilities.writeFile(self, file); } @@ -324,7 +324,7 @@ public class ScriptShellMethods { file = file.getAbsoluteFile(); // make sure parent folders exist - file.getParentFile().mkdirs(); + FileUtilities.createFolders(file.getParentFile()); org.apache.commons.io.FileUtils.copyURLToFile(self, file); return file; diff --git a/source/net/filebot/util/FileUtilities.java b/source/net/filebot/util/FileUtilities.java index ecbdc839..11343c11 100644 --- a/source/net/filebot/util/FileUtilities.java +++ b/source/net/filebot/util/FileUtilities.java @@ -156,11 +156,17 @@ public final class FileUtilities { return org.apache.commons.io.FileUtils.deleteQuietly(file); } + public static void createFolders(File folder) throws IOException { + if (!folder.isDirectory()) { + Files.createDirectories(folder.toPath()); + } + } + public static void createFileIfNotExists(File file) throws IOException { if (!file.isFile()) { // create parent folder structure if necessary & create file Files.createDirectories(file.getParentFile().toPath()); - Files.createFile(file.toPath()).toFile(); + Files.createFile(file.toPath()); } } diff --git a/source/net/filebot/util/TemporaryFolder.java b/source/net/filebot/util/TemporaryFolder.java index be48d96a..b450f8d0 100644 --- a/source/net/filebot/util/TemporaryFolder.java +++ b/source/net/filebot/util/TemporaryFolder.java @@ -73,7 +73,6 @@ public final class TemporaryFolder { * if an I/O error occurred */ public File createFile(String name) throws IOException { - // if the directory does not exist it will be created File file = new File(getFolder(), name); file.createNewFile(); @@ -97,7 +96,7 @@ public final class TemporaryFolder { return File.createTempFile(prefix, suffix, getFolder()); } - public boolean deleteFile(String name) { + public boolean deleteFile(String name) throws IOException { return new File(getFolder(), name).delete(); } @@ -106,15 +105,12 @@ public final class TemporaryFolder { * * @return the {@link File} object for this {@link TemporaryFolder} */ - public File getFolder() { - if (!root.exists()) { - root.mkdirs(); - } - + public File getFolder() throws IOException { + FileUtilities.createFolders(root); return root; } - public TemporaryFolder subFolder(String name) { + public TemporaryFolder subFolder(String name) throws IOException { return new TemporaryFolder(new File(getFolder(), name)); }