8066915: (fs) Files.newByteChannel opens directories for cases where subsequent reads may fail

Reviewed-by: chegar
This commit is contained in:
Alan Bateman 2014-12-10 15:01:26 +00:00
parent b2698ae406
commit ccab6bc563
2 changed files with 33 additions and 1 deletions

View File

@ -270,6 +270,22 @@ class UnixChannelFactory {
throw x; 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 // unlink file immediately if delete on close. The spec is clear that
// an implementation cannot guarantee to unlink the correct file when // an implementation cannot guarantee to unlink the correct file when
// replaced by an attacker after it is opened. // replaced by an attacker after it is opened.

View File

@ -22,7 +22,7 @@
*/ */
/* @test /* @test
* @bug 4313887 * @bug 4313887 8066915
* @summary Unit test for java.nio.file.Files.newByteChannel * @summary Unit test for java.nio.file.Files.newByteChannel
* @library .. * @library ..
*/ */
@ -59,6 +59,7 @@ public class SBC {
dosSharingOptionTests(dir); dosSharingOptionTests(dir);
// misc. tests // misc. tests
directoryOpenTests(dir);
badCombinations(dir); badCombinations(dir);
unsupportedOptions(dir); unsupportedOptions(dir);
nullTests(dir); nullTests(dir);
@ -278,6 +279,21 @@ public class SBC {
} }
} }
// test opening a directory for read or write
static void directoryOpenTests(Path dir) throws Exception {
try (SeekableByteChannel sbc = Files.newByteChannel(dir, READ)) {
throw new RuntimeException("Opened directory for read");
} catch (IOException expected) { }
try (SeekableByteChannel sbc = Files.newByteChannel(dir, WRITE)) {
throw new RuntimeException("Opened directory for write");
} catch (IOException expected) { }
try (SeekableByteChannel sbc = Files.newByteChannel(dir, APPEND)) {
throw new RuntimeException("Opened directory for append ");
} catch (IOException expected) { }
}
// Windows specific options for the use by applications that really want // Windows specific options for the use by applications that really want
// to use legacy DOS sharing options // to use legacy DOS sharing options
static void dosSharingOptionTests(Path dir) throws Exception { static void dosSharingOptionTests(Path dir) throws Exception {