Skip to content

8355342: File.getCanonicalPath on Java 24 resolves paths on network drives to UNC format - #27324

Closed
bplb wants to merge 17 commits into
openjdk:masterfrom
bplb:File-getCanonicalPath-8355342
Closed

bplb wants to merge 17 commits into
openjdk:masterfrom
bplb:File-getCanonicalPath-8355342

Conversation

@bplb

@bplb bplb commented Sep 16, 2025 •

Copy link
Copy Markdown
Member

File.getCanonicalPath invokes GetFinalPathNameByHandle on the result of canonicalize0 which causes the drive letter of a mapped drive to be converted to a UNC prefix. If such a substitution is detected, this request proposes to revert the conversion of drive letter to UNC prefix before returning the canonical path.


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8355342: File.getCanonicalPath on Java 24 resolves paths on network drives to UNC format (Bug - P3)

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/27324/head:pull/27324
$ git checkout pull/27324

Update a local copy of the PR:
$ git checkout pull/27324
$ git pull https://git.openjdk.org/jdk.git pull/27324/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 27324

View PR using the GUI difftool:
$ git pr show -t 27324

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/27324.diff

Using Webrev

Link to Webrev Comment

@bridgekeeper

bridgekeeper Bot commented Sep 16, 2025

Copy link
Copy Markdown

👋 Welcome back bpb! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk

openjdk Bot commented Sep 16, 2025 •

Copy link
Copy Markdown

@bplb This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8355342: File.getCanonicalPath on Java 24 resolves paths on network drives to UNC format

Reviewed-by: alanb

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 153 new commits pushed to the master branch:

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk

openjdk Bot commented Sep 16, 2025 •

Copy link
Copy Markdown

@bplb The following label will be automatically applied to this pull request:

  • core-libs

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk Bot added core-libs [email protected] rfr Pull request is ready for review labels Sep 16, 2025
@mlbridge

mlbridge Bot commented Sep 16, 2025 •

Copy link
Copy Markdown

@AlanBateman

Copy link
Copy Markdown
Contributor

Would it be possible to provide a summary as to why we don't run into this with toRealPath?

@bplb

bplb commented Sep 17, 2025

Copy link
Copy Markdown
Member Author

In toRealPath, the function GetFinalPathNameByHandle is not invoked unless links are being followed and a reparse point is encountered. Otherwise the real path is built up by analyzing the input path. I had actually already been wondering whether the canonicalized path should be checked for links before invoking getFinalPath.

@AlanBateman

Copy link
Copy Markdown
Contributor

I had actually already been wondering whether the canonicalized path should be checked for links before invoking getFinalPath.

I assume you've checked the flags that can be provided to GetFinalPathNameByHandleW and that there isn't a solution there. In that case, I think it would be better to check the attributes in the WIN32_FIND_DATA to see if it's a reparse point. That would mean deeper changes but should mean you avoid the post-processing the result from getFinalPath, which I think is a bit too hacky to have in this code.

@bplb

bplb commented Sep 17, 2025

Copy link
Copy Markdown
Member Author

I assume you've checked the flags that can be provided to GetFinalPathNameByHandleW and that there isn't a solution there.

Yes, I checked all combinations before pursuing this change. There's no help there.

In that case, I think it would be better to check the attributes in the WIN32_FIND_DATA to see if it's a reparse point. That would mean deeper changes

Will check it out.

but should mean you avoid the post-processing the result from getFinalPath, which I think is a bit too hacky to have in this code.

I think it's correct but I agree about its "hackiness."

@bplb

bplb commented Sep 17, 2025

Copy link
Copy Markdown
Member Author

Would it be possible to provide a summary as to why we don't run into this with toRealPath?

In toRealPath, the function GetFinalPathNameByHandle is not invoked unless links are being followed and a reparse point is encountered.

In this scenario

 Directory of Z:\

09/17/2025  03:39 PM    <DIR>          .
09/17/2025  08:36 AM    <SYMLINK>      link [target.txt]
09/10/2025  04:41 PM                18 target.txt

where Z: is mapped as net use Z: \\localhost\c$\Temp, in other words to C:\Temp, a similar problem happens with toRealPath:

jshell> Path.of("Z:\\target.txt").toRealPath()
$7 ==> Z:\target.txt

jshell> Path.of("Z:\\link").toRealPath()
$8 ==> \\localhost\c$\Temp\target.txt

@AlanBateman

Copy link
Copy Markdown
Contributor

where Z: is mapped as net use Z: \\localhost\c$\Temp, in other words to C:\Temp, a similar problem happens with toRealPath:

jshell> Path.of("Z:\\target.txt").toRealPath()
$7 ==> Z:\target.txt

jshell> Path.of("Z:\\link").toRealPath()
$8 ==> \\localhost\c$\Temp\target.txt

Are you able to check how it behaves when mapped to a location on different host?

@bplb

bplb commented Sep 19, 2025

Copy link
Copy Markdown
Member Author

[...] post-processing the result from getFinalPath, which I think is a bit too hacky to have in this code.

This hack does not work when Z: is mapped to a drive on another machine, at least on my laptop because there is no key

HKEY_CURRENT_USER\Network\Z\RemotePath

in the registry in this case.

@AlanBateman

Copy link
Copy Markdown
Contributor

This issue is going to require stepping back and re-visiting the question as to what is the canonical path when a file is located with a UNC path. We wrestled with this in JDK 7 and I thought got to the best answer with toRealPath but it seems we may have to re-visit this now.

@bplb

bplb commented Sep 22, 2025

Copy link
Copy Markdown
Member Author

Are you able to check how it behaves when mapped to a location on different host?

From issue JDK-8368165:

Path.of("Z:\link").toRealPath() throws an exception on the Windows host where Z: is mapped to a remote drive on a Mac host via Samba:

| Exception java.nio.file.FileSystemException: Z:\link: The name of the file cannot be resolved by the system
| at WindowsException.translateToIOException (WindowsException.java:92)
| at WindowsException.rethrowAsIOException (WindowsException.java:103)
| at WindowsException.rethrowAsIOException (WindowsException.java:108)
| at WindowsLinkSupport.getFinalPath (WindowsLinkSupport.java:104)
| at WindowsLinkSupport.getRealPath (WindowsLinkSupport.java:271)
| at WindowsPath.toRealPath (WindowsPath.java:944)
| at WindowsPath.toRealPath (WindowsPath.java:42)

@AlanBateman

AlanBateman commented Sep 23, 2025 •

Copy link
Copy Markdown
Contributor

Path.of("Z:\link").toRealPath() throws an exception on the Windows host where Z: is mapped to a remote drive on a Mac host via Samba:

I assume "link" is a sym link. I haven't configured SAMBA in a while but isn't there a config setting in smb.conf that controls this?

@bplb

bplb commented Sep 23, 2025

Copy link
Copy Markdown
Member Author

I assume "link" is a sym link.

Yes.

I haven't configured SAMBA in a while but isn't there a config setting in smb.conf that controls this?

I don't know but will check.

@bplb

bplb commented Sep 23, 2025

Copy link
Copy Markdown
Member Author

[I]sn't there a config setting in smb.conf that controls this?

"This option is enabled (i.e. smbd will follow symbolic links) by default."

https://www.samba.org/samba/docs/current/man-html/smb.conf.5.html#FOLLOWSYMLINKS

@bplb

bplb commented Oct 15, 2025

Copy link
Copy Markdown
Member Author

Commit ebe88f7 changes WinNTFileSystem::canonicalize to fall back to the result of canonicalize0 if getFinalPath converts a drive letter + : to a path beginning with \\. I have investigated and tested various Windows API functions and there does not appear to be a way to determine to exactly which path prefix a drive letter will be mapped by GetFinalPathNameByHandleW. No changes to the test are needed.

@AlanBateman

Copy link
Copy Markdown
Contributor

Commit ebe88f7 changes WinNTFileSystem::canonicalize to fall back to the result of canonicalize0 if getFinalPath converts a drive letter + : to a path beginning with \\. I have investigated and tested various Windows API functions and there does not appear to be a way to determine to exactly which path prefix a drive letter will be mapped by GetFinalPathNameByHandleW.

Would it be possible to paste in the outcome from the latest change with files that exist, and do not exist, on the UNC share. I think it would be useful to have both the toRealPath and File.getCanonicalPath outcome. This is a tricky issue as the current implementation isn't wrong, it's just surprising (and in the case of the bug report, awkward when attempting to use the real path as a working directory for a subprocess).

@openjdk openjdk Bot removed the ready Pull request is ready to be integrated label Oct 29, 2025
@openjdk openjdk Bot added the ready Pull request is ready to be integrated label Oct 29, 2025
@openjdk openjdk Bot removed the ready Pull request is ready to be integrated label Oct 31, 2025

@AlanBateman AlanBateman left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It took a lot of iterations but I think the latest is good. I assume you'll run tier1+tier2 at least before integrating.

@openjdk openjdk Bot added the ready Pull request is ready to be integrated label Nov 1, 2025
@bplb

bplb commented Nov 3, 2025

Copy link
Copy Markdown
Member Author

I assume you'll run tier1+tier2 at least before integrating.

Certainly. There is still a weird test failure that I am investigating to determine whether it is related to the change. Otherwise tiers 1-3 are clear.

@openjdk openjdk Bot removed the ready Pull request is ready to be integrated label Nov 4, 2025
@bplb

bplb commented Nov 4, 2025

Copy link
Copy Markdown
Member Author

Without commit 0918c3a, JImageExtractTest was failing as

Invoking testExtractToDirBySymlink
jimage [extract, --dir, .\symlink, C:\tmp\jtreg\scratch\.\JImageExtractTest10882108125475978945\smalljdk\lib\modules]
STDERR:
java.lang.reflect.InvocationTargetException
        at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:119)
        at java.base/java.lang.reflect.Method.invoke(Method.java:565)
        at JImageCliTest.runTests(JImageCliTest.java:111)
        at JImageExtractTest.main(JImageExtractTest.java:234)
        at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
        at java.base/java.lang.reflect.Method.invoke(Method.java:565)
        at com.sun.javatest.regtest.agent.MainWrapper$MainTask.run(MainWrapper.java:138)
        at java.base/java.lang.Thread.run(Thread.java:1474)
Caused by: java.lang.RuntimeException: Error: cannot create directory C:\tmp\jtreg\scratch\.\symlink\java.base\META-INF\services
: expected true, was false
        at jdk.test.lib.Asserts.fail(Asserts.java:715)
        at jdk.test.lib.Asserts.assertTrue(Asserts.java:545)
        at JImageCliTest$JImageResult.assertSuccess(JImageCliTest.java:91)

which was apparently due to wcanonicalize() failing when getFinalPath() failed for a symlink. Suppressing this failure reinstates the control flow previously defined in the Java layer. With this change, all tier1-tier3 tests pass.

@AlanBateman

Copy link
Copy Markdown
Contributor

which was apparently due to wcanonicalize() failing when getFinalPath() failed for a symlink. Suppressing this failure reinstates the control flow previously defined in the Java layer. With this change, all tier1-tier3 tests pass.

File::getCanonicalFile is specified to return a result when the file doesn't exist to I think this is okay. Were you able to establish if this was a just a broken link or something else?

@bplb

bplb commented Nov 4, 2025

Copy link
Copy Markdown
Member Author

Were you able to establish if this was a just a broken link or something else?

Not definitively, but I think if could be. The Windows system error code was ERROR_PATH_NOT_FOUND (3).

@AlanBateman

Copy link
Copy Markdown
Contributor

Not definitively, but I think if could be. The Windows system error code was ERROR_PATH_NOT_FOUND (3).

Okay, in that case it should be okay to treat it as a broken link.

@openjdk openjdk Bot added the ready Pull request is ready to be integrated label Nov 5, 2025
@bplb

bplb commented Nov 6, 2025

Copy link
Copy Markdown
Member Author

/integrate

@openjdk

openjdk Bot commented Nov 6, 2025

Copy link
Copy Markdown

Going to push as commit 1f08a3e.
Since your change was applied there have been 179 commits pushed to the master branch:

Your commit was automatically rebased without conflicts.

@openjdk openjdk Bot added the integrated Pull request has been integrated label Nov 6, 2025
@openjdk openjdk Bot closed this Nov 6, 2025
@openjdk openjdk Bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Nov 6, 2025
@openjdk

openjdk Bot commented Nov 6, 2025

Copy link
Copy Markdown

@bplb Pushed as commit 1f08a3e.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core-libs [email protected] integrated Pull request has been integrated

Development

Successfully merging this pull request may close these issues.

3 participants