X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Futil%2FFileUtils.java;h=e7d274c117fcb01c12901dedcfc728889c568d5d;hb=3fdc889794e3566af57628f0b6a308eb23886f96;hp=79cac0a1f8bda257ab6d0dd7149d7e894803c1b3;hpb=871535152992c147f2175c3006b6bec4615fead4;p=jalview.git diff --git a/src/jalview/util/FileUtils.java b/src/jalview/util/FileUtils.java index 79cac0a..e7d274c 100644 --- a/src/jalview/util/FileUtils.java +++ b/src/jalview/util/FileUtils.java @@ -1,3 +1,23 @@ +/* + * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) + * Copyright (C) $$Year-Rel$$ The Jalview Authors + * + * This file is part of Jalview. + * + * Jalview is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 + * of the License, or (at your option) any later version. + * + * Jalview is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Jalview. If not, see . + * The Jalview Authors are detailed in the 'AUTHORS' file. + */ package jalview.util; import java.io.File; @@ -17,6 +37,8 @@ import java.util.EnumSet; import java.util.List; import java.util.stream.Collectors; +import jalview.bin.Console; + public class FileUtils { /* @@ -226,4 +248,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(); + } }