X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Futil%2FFileUtils.java;h=983ba750e91b34ef4e6f584220ced30b21b34977;hb=refs%2Fheads%2Ffeature%2FJAL-4274_configurable_bitmap_export_preferences;hp=79cac0a1f8bda257ab6d0dd7149d7e894803c1b3;hpb=871535152992c147f2175c3006b6bec4615fead4;p=jalview.git diff --git a/src/jalview/util/FileUtils.java b/src/jalview/util/FileUtils.java index 79cac0a..983ba75 100644 --- a/src/jalview/util/FileUtils.java +++ b/src/jalview/util/FileUtils.java @@ -17,6 +17,8 @@ import java.util.EnumSet; import java.util.List; import java.util.stream.Collectors; +import jalview.bin.Console; + public class FileUtils { /* @@ -226,4 +228,47 @@ public class FileUtils } return path.toString(); } + + public static File getParentDir(File file) + { + if (file == null) + { + return null; + } + File parentDir = file.getAbsoluteFile().getParentFile(); + return parentDir; + } + + public static boolean checkParentDir(File file, boolean mkdirs) + { + if (file == null) + { + return false; + } + File parentDir = getParentDir(file); + if (parentDir.exists()) + { + // already exists, nothing to do so nothing to worry about! + return true; + } + + if (!mkdirs) + { + return false; + } + + Path path = file.toPath(); + for (int i = 0; i < path.getNameCount(); i++) + { + Path p = path.getName(i); + if ("..".equals(p.toString())) + { + Console.warn("Cautiously not running mkdirs on " + file.toString() + + " because the path to be made contains '..'"); + return false; + } + } + + return parentDir.mkdirs(); + } }