8374441: (fs) FileSystemProvider.readAttributesIfExists throws "Not a directory" when element in path is not directory should return null for ENOTDIR (unix)

Reviewed-by: alanb
This commit is contained in:
Fabian Meumertzheim 2026-01-06 08:09:42 +00:00 committed by Alan Bateman
parent d063c9546b
commit 2fbc4162e8
2 changed files with 25 additions and 5 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -98,7 +98,8 @@ class UnixFileAttributes
path, flag, attrs);
if (errno == 0)
return attrs;
else if (errno == UnixConstants.ENOENT)
else if (errno == UnixConstants.ENOENT ||
errno == UnixConstants.ENOTDIR)
return null;
else
throw new UnixException(errno);

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2025, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -22,9 +22,10 @@
*/
/* @test
* @bug 8356678
* @bug 8356678 8374441
* @requires (os.family != "windows")
* @summary Test Files operations when a path component is not a directory
* @summary Test Files and FileSystemProvider operations when a path component
* is not a directory
* @run junit NotADirectory
*/
@ -32,10 +33,12 @@ import java.io.IOException;
import java.nio.file.CopyOption;
import java.nio.file.Files;
import java.nio.file.FileSystemException;
import java.nio.file.FileSystems;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.PosixFileAttributes;
import java.nio.file.spi.FileSystemProvider;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
@ -51,10 +54,14 @@ import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class NotADirectory {
private static final FileSystemProvider PROVIDER =
FileSystems.getDefault().provider();
private static final Path ROOT = Path.of(".");
private static final Path NOT_EXIST = ROOT.resolve("notExist");
private static final Path DIR = ROOT.resolve("dir");
@ -143,6 +150,18 @@ public class NotADirectory {
() -> Files.readAttributes(BOGUS, PosixFileAttributes.class));
}
@Test
public void readBasicIfExists() throws IOException {
assertNull(
PROVIDER.readAttributesIfExists(BOGUS, BasicFileAttributes.class));
}
@Test
public void readPosixIfExists() throws IOException {
assertNull(
PROVIDER.readAttributesIfExists(BOGUS, PosixFileAttributes.class));
}
@Test
public void exists() throws IOException {
assertFalse(Files.exists(BOGUS));