RFR: 8315273: (fs) Path.toRealPath(LinkOption.NOFOLLOW_LINKS) fails when "../../" follows a link (win) [v5]

Daniel Jeliński djelinski at openjdk.org
Thu Sep 12 08:01:13 UTC 2024


On Fri, 1 Mar 2024 21:48:22 GMT, Brian Burkhalter <bpb at openjdk.org> wrote:

>> Windows implementation of integrated pull request #15397. The test java/nio/file/Path/ToRealPath.java is also removed from the problem list.
>
> Brian Burkhalter has updated the pull request with a new target base due to a merge or a rebase. The pull request now contains seven commits:
> 
>  - 8315273: Re-remove ToRealPath test
>  - Merge
>  - 8315273: Revert ProblemList
>  - Merge
>  - Merge
>  - 8315273: Add bug ID to test
>  - 8315273: (fs) Path.toRealPath(LinkOption.NOFOLLOW_LINKS) fails when "../../" follows a link (win)

Well let me show you a counterexample:
Setup:

C:\>mkdir tmp\cwd\dir\subdir

C:\>cd tmp\cwd

C:\tmp\cwd>mklink /d link dir\subdir
symbolic link created for link <<===>> dir\subdir

C:\tmp\cwd>jshell.exe

Before your changes:

jshell> Path.of("link/../../file")
$1 ==> link....\file

jshell> Files.createFile($1)
$2 ==> link....\file

jshell> Files.exists($1)
$3 ==> true

jshell> Files.exists($1.toRealPath())
$4 ==> true

After your changes:

jshell> Path.of("link/../../file")
$1 ==> link....\file

jshell> Files.createFile($1)
$2 ==> link....\file

jshell> Files.exists($1)
$3 ==> true

jshell> Files.exists($1.toRealPath())
|  Exception java.nio.file.NoSuchFileException: C:\tmp\cwd\file
|        at WindowsException.translateToIOException (WindowsException.java:85)
|        at WindowsException.rethrowAsIOException (WindowsException.java:103)
|        at WindowsException.rethrowAsIOException (WindowsException.java:108)
|        at WindowsLinkSupport.collapseDots (WindowsLinkSupport.java:238)
|        at WindowsLinkSupport.getRealPath (WindowsLinkSupport.java:285)
|        at WindowsPath.toRealPath (WindowsPath.java:944)
|        at WindowsPath.toRealPath (WindowsPath.java:42)
|        at (#4:1)

`path` and `path.toRealPath()` are supposed to point to the same file, but as you can see, this does not happen here.

I guess you could adjust all the methods in `Files` class to be consistent with the new `toRealPath` implementation, but this would break consistency with other Windows apps, see for example:

C:\tmp\cwd>type file
The system cannot find the file specified.

C:\tmp\cwd>type link....\file

C:\tmp\cwd>

Based on the above, I think the current `toRealPath` implementation on Windows is doing just fine, only the `ToRealPath` test needs to be fixed.

As far as I could tell, `Path.toRealPath` does not make any promises about what the resulting path should be. The only requirement is that the path should refer to the same file, so the current implementation does not violate the spec.

-------------

PR Comment: https://git.openjdk.org/jdk/pull/15525#issuecomment-2345533160


More information about the nio-dev mailing list