summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Wolf <thomas.wolf@paranor.ch>2015-10-22 21:23:18 +0200
committerAndrey Loskutov <loskutov@gmx.de>2015-10-22 15:39:38 -0400
commitd9e9015a0061efe96cae0c98225443abc5e88bbb (patch)
treef8f675ab981fb2f4723ab88639e781e6dcf24457
parent80c7884ea641e7f14cbdc2b70727275e202304ea (diff)
downloadjgit-d9e9015a0061efe96cae0c98225443abc5e88bbb.tar.gz
jgit-d9e9015a0061efe96cae0c98225443abc5e88bbb.zip
Add best-effort variant of File.getCanonicalFile()
See https://git.eclipse.org/r/#/c/58405/. Change-Id: I097c4b1369754f59137eb8848a680c61b06510ad Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/util/FileUtils.java25
1 files changed, 25 insertions, 0 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/util/FileUtils.java b/org.eclipse.jgit/src/org/eclipse/jgit/util/FileUtils.java
index 720fdedefb..6d0318c029 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/util/FileUtils.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/util/FileUtils.java
@@ -735,4 +735,29 @@ public class FileUtils {
}
return name;
}
+
+ /**
+ * Best-effort variation of {@link File#getCanonicalFile()} returning the
+ * input file if the file cannot be canonicalized instead of throwing
+ * {@link IOException}.
+ *
+ * @param file
+ * to be canonicalized; may be {@code null}
+ * @return canonicalized file, or the unchanged input file if
+ * canonicalization failed or if {@code file == null}
+ * @throws SecurityException
+ * if {@link File#getCanonicalFile()} throws one
+ * @since 4.2
+ */
+ public static File canonicalize(File file) {
+ if (file == null) {
+ return null;
+ }
+ try {
+ return file.getCanonicalFile();
+ } catch (IOException e) {
+ return file;
+ }
+ }
+
}