From d9e9015a0061efe96cae0c98225443abc5e88bbb Mon Sep 17 00:00:00 2001 From: Thomas Wolf Date: Thu, 22 Oct 2015 21:23:18 +0200 Subject: [PATCH] Add best-effort variant of File.getCanonicalFile() See https://git.eclipse.org/r/#/c/58405/. Change-Id: I097c4b1369754f59137eb8848a680c61b06510ad Signed-off-by: Thomas Wolf --- .../src/org/eclipse/jgit/util/FileUtils.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) 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 720fdedef..6d0318c02 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; + } + } + }