RFR: 8154364: (fs) Files.isSameFile() throws NoSuchFileException with broken symbolic links [v8]

Brian Burkhalter bpb at openjdk.org
Mon Aug 18 21:52:32 UTC 2025


On Mon, 18 Aug 2025 16:05:18 GMT, Brian Burkhalter <bpb at openjdk.org> wrote:

>> src/java.base/unix/classes/sun/nio/fs/UnixFileSystemProvider.java line 379:
>> 
>>> 377:         }
>>> 378:         return keys;
>>> 379:     }
>> 
>> I'm a bit puzzled as to why it looks at each element in the path. It should be possible to just enumerate the files of the sym links that exist, something like this
>> 
>>         var fileKeys = new HashSet<UnixFileKey>();
>>         while (path != null) {
>>             UnixFileAttributes attrs = UnixFileAttributes.getIfExists(path, false);
>>             if (attrs != null && attrs.isSymbolicLink()) {
>>                 fileKeys.add(attrs.fileKey());
>>                 path = (UnixPath) readSymbolicLink(path);
>>             } else {
>>                 path = null;
>>             }
>>         }
>> 
>> 
>> or better, just return the file key of last accessible link in the chain, something like this, and then just check if they are equal, e.g.
>> 
>> 
>>     private UnixFileKey lastFileKey(UnixPath path) throws UnixException {
>>         var fileKeys = new HashSet<UnixFileKey>();
>>         UnixFileKey lastFileKey = null;
>>         while (path != null) {
>>             UnixFileAttributes attrs = UnixFileAttributes.getIfExists(path, false);
>>             if (attrs == null) {
>>                 break;
>>             }
>>             UnixFileKey fileKey = attrs.fileKey();
>>             if (!attrs.isSymbolicLink()) {
>>                 return fileKey;
>>             }
>>             if (!fileKeys.add(fileKey)) {
>>                 throw new UnixException(ELOOP);
>>             }
>>             byte[] target = readlink(path);
>>             path = new UnixPath(theFileSystem, target);
>>         }
>>         return lastFileKey;
>>     }
>
>> or better, just return the file key of last accessible link in the chain, something like this, and then just check if they are equal
> 
> Thanks for the suggestion, will investigate.

So updated in b2bd8f2.

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

PR Review Comment: https://git.openjdk.org/jdk/pull/26736#discussion_r2283567109


More information about the nio-dev mailing list