RFR: 8365807: (fs) Two-arg UnixFileAttributes.getIfExists should not use exception for control flow [v2]

Brian Burkhalter bpb at openjdk.org
Fri Aug 22 16:29:53 UTC 2025


On Fri, 22 Aug 2025 15:10:15 GMT, Brian Burkhalter <bpb at openjdk.org> wrote:

>> src/java.base/unix/classes/sun/nio/fs/UnixFileAttributes.java line 122:
>> 
>>> 120:         UnixFileAttributes attrs = new UnixFileAttributes();
>>> 121:         int flag = (followLinks) ? 0 : UnixConstants.AT_SYMLINK_NOFOLLOW;
>>> 122:         UnixNativeDispatcher.fstatat(dfd, path, flag, attrs);
>> 
>> 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.
>
> This change was intended to address [this comment](https://github.com/openjdk/jdk/pull/26736#pullrequestreview-3131687830)
> 
> a version that doesn't use exceptions for control flow,
> 
> but I think I misinterpreted that. I'll reassess the change.

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;

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

PR Review Comment: https://git.openjdk.org/jdk/pull/26889#discussion_r2294168512


More information about the nio-dev mailing list