From eca350c84829307397055fdfd5fde88488a9faf1 Mon Sep 17 00:00:00 2001 From: alanb Date: Mon, 23 Aug 2010 17:11:07 +0100 Subject: [PATCH] 6978511: (file) Path.toRealPath should fail if not resolving links and file does not exist Reviewed-by: forax, chegar --- src/solaris/classes/sun/nio/fs/UnixPath.java | 7 +++++++ test/java/nio/file/Path/Misc.java | 15 +++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/solaris/classes/sun/nio/fs/UnixPath.java b/src/solaris/classes/sun/nio/fs/UnixPath.java index 75a000b70..53a32c14a 100644 --- a/src/solaris/classes/sun/nio/fs/UnixPath.java +++ b/src/solaris/classes/sun/nio/fs/UnixPath.java @@ -1141,6 +1141,13 @@ class UnixPath } result = result.resolve(element); } + + // check file exists (without following links) + try { + UnixFileAttributes.get(result, false); + } catch (UnixException x) { + x.rethrowAsIOException(result); + } return result; } diff --git a/test/java/nio/file/Path/Misc.java b/test/java/nio/file/Path/Misc.java index 8b2223b23..3b24c7862 100644 --- a/test/java/nio/file/Path/Misc.java +++ b/test/java/nio/file/Path/Misc.java @@ -260,6 +260,21 @@ public class Misc { */ assertTrue(file.toRealPath(true).isSameFile(file.toRealPath(false))); + /** + * Test: toRealPath should fail if file does not exist + */ + Path doesNotExist = dir.resolve("DoesNotExist"); + try { + doesNotExist.toRealPath(true); + throw new RuntimeException("IOException expected"); + } catch (IOException expected) { + } + try { + doesNotExist.toRealPath(false); + throw new RuntimeException("IOException expected"); + } catch (IOException expected) { + } + /** * Test: toRealPath(true) should resolve links */ -- GitLab