Skip to content

8365807: (fs) Two-arg UnixFileAttributes.getIfExists should not use exception for control flow #26889

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

bplb
Copy link
Member

@bplb bplb commented Aug 21, 2025

In the Unix implementation of sun.nio.fs, change

  1. UnixFileAttributes.getIfExists(UnixPath,boolean) to use the system call success status and errno for control flow; and
  2. UnixFileAttributes.getIfExists(UnixPath) to simply return UnixFileAttributes.getIfExists(UnixPath,true).

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-8365807: (fs) Two-arg UnixFileAttributes.getIfExists should not use exception for control flow (Bug - P4)

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 26889

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

Using diff file

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

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Aug 21, 2025

👋 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
Copy link

openjdk bot commented Aug 21, 2025

❗ This change is not yet ready to be integrated.
See the Progress checklist in the description for automated requirements.

@openjdk
Copy link

openjdk bot commented Aug 21, 2025

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

  • nio

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 nio nio-dev@openjdk.org rfr Pull request is ready for review labels Aug 21, 2025
@mlbridge
Copy link

mlbridge bot commented Aug 21, 2025

Webrevs

@@ -130,7 +119,7 @@ static UnixFileAttributes get(int dfd, UnixPath path, boolean followLinks)
{
UnixFileAttributes attrs = new UnixFileAttributes();
int flag = (followLinks) ? 0 : UnixConstants.AT_SYMLINK_NOFOLLOW;
UnixNativeDispatcher.fstatat(dfd, path.asByteArray(), flag, attrs);
UnixNativeDispatcher.fstatat(dfd, path, flag, attrs);
Copy link
Contributor

Choose a reason for hiding this comment

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

If dfd/path does not exist then won't this now return an unpopulated UnixFileAttributes?

I think my concern is that the changes create a bit of a hazard. Most UnixNativeDispatcher native methods throw if the syscall fails, a small number return the errno, now we have a native method that special cases ENOENT. I think we should try to limit it to two variants, one that throws, the other that returns errno, and establish some naming convention to reduce the possibility of mis-use. It would be good to audit out tests to make sure that every method exercises error cases.

Copy link
Member Author

Choose a reason for hiding this comment

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

This change was intended to address this comment

a version that doesn't use exceptions for control flow,

but I think I misinterpreted that. I'll reassess the change.

Copy link
Member Author

Choose a reason for hiding this comment

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

Commit 7ea1b08:

  1. Change fstatat0 and stat0 to throw instead of returning errno.
  2. Remove the stat2 method.
  3. Change calling code to handle the changes.

Now all ?stat{at}0 functions are void, expressing errors via UnixException.

All UnixNativeDispatcher methods are now void except:

jbytearray getcwd, getgrid, getgrnam, getpwnam, getpwuid, readdir, realpath, strerror
jint       access, dup, fgetxattr, flistxattr, getlinelen, openat, read, write
jlong      fdopendir, opendir

All these functions return a "useful" value, except access which returns errno. It would be a little disruptive to change access to throw instead of returning errno as there are a number of usages like

return access(path, mode) == 0;

@AlanBateman
Copy link
Contributor

UnixFileAttributes.getIfExists is performance critical so this is why it the native method returns errno rather than throws. The suggestion is the new 2-arg getIfExits work the same way and only throw if the error is not ENOENT. My comment on void vs. int return is to point out the hazard when we have two styles, it's too easy to call the method that does not throw when an exception is expected. This lead to the suggestion to choose a naming convention to avoid mistakes.

@bplb
Copy link
Member Author

bplb commented Aug 22, 2025

UnixFileAttributes.getIfExists is performance critical so this is why it the native method returns errno rather than throws.

So the stat0 and fstatat0 methods should revert to returning errno.

@bplb
Copy link
Member Author

bplb commented Aug 23, 2025

This lead to the suggestion to choose a naming convention to avoid mistakes.

As there are only a couple of methods that return errno, then maybe something like fstatat0NoThrow or fstatat0errno and leave the ones that throw as they are?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
nio nio-dev@openjdk.org rfr Pull request is ready for review
Development

Successfully merging this pull request may close these issues.

2 participants