JDK-8066915 : (fs) Files.newByteChannel opens directories for cases where subsequent reads may fail
Type:Bug
Component:core-libs
Sub-Component:java.nio
Affected Version:9
Priority:P4
Status:Closed
Resolution:Fixed
Submitted:2014-12-08
Updated:2015-06-04
Resolved:2014-12-10
The Version table provides details related to the release that this issue/RFE will be addressed.
Unresolved : Release in which this issue/RFE will be addressed. Resolved: Release in which this issue/RFE has been resolved. Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.
On Unix/like systems it is currently possible to open a directory for read access but subsequent reads fail. It would be more desirable for the open to fail.
There is a related issue on Windows in that the IOException that is thrown is AccessDeniedException. This is because Windows returns error 5 "Access Denied" for this case. The best we can do is react to this error and attempt to check if the file is a directory. This is something for another issue.
09-12-2014
Easier fixed by checking if the file is a directory after opening the file:
-- old/src/java.base/unix/classes/sun/nio/fs/UnixChannelFactory.java 2014-12-08 13:04:17.512348430 +0000
+++ new/src/java.base/unix/classes/sun/nio/fs/UnixChannelFactory.java 2014-12-08 13:04:17.356348430 +0000
@@ -270,6 +270,22 @@
throw x;
}
+ // fail if the file is a directory
+ if (flags.read) {
+ UnixException exc = null;
+ try {
+ if (UnixFileAttributes.get(fd).isDirectory()) {
+ exc = new UnixException(EISDIR);
+ }
+ } catch (UnixException x) {
+ exc = x;
+ }
+ if (exc != null) {
+ close(fd);
+ throw exc;
+ }
+ }
+
// unlink file immediately if delete on close. The spec is clear that
// an implementation cannot guarantee to unlink the correct file when
// replaced by an attacker after it is opened.