RFR: 8365807: (fs) Two-arg UnixFileAttributes.getIfExists should not use exception for control flow
Brian Burkhalter
bpb at openjdk.org
Fri Aug 22 15:12:51 UTC 2025
On Fri, 22 Aug 2025 09:40:26 GMT, Alan Bateman <alanb at openjdk.org> wrote:
>> 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)`.
>
> 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.
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/26889#discussion_r2294004383
More information about the nio-dev
mailing list