Issue:FileChannel.force() on channel object from a FileInputStream object.

Issue in testcases in FileChannel submodule in java_nio/channels/ module during JCK testing

The following test cases in api/java_nio/channels/FileChannel/index.html#Methods fail in our propreitary unix based on SVR4, with the following error.

FileChannel0031: Failed. Unexpected exception java.io.IOException: Bad file number

FileChannel0037: Failed. Unexpected exception java.io.IOException: Bad file number

FileChannel0040: Failed. Unexpected exception java.io.IOException: Bad file number

FileChannel0043: Failed. Unexpected exception java.io.IOException: Bad file number

FileChannel0044: Failed. Unexpected exception java.io.IOException: Bad file number

The stack trace printed is as follows.

java.io.IOException: Bad file number

at sun.nio.ch.FileChannelImpl.force0(Native Method)

at sun.nio.ch.FileChannelImpl.force(FileChannelImpl.java:359)

at javasoft.sqe.tests.api.java.nio.channels.FileChannel.MethodsTests$30.run(MethodsTests.java:662)

at javasoft.sqe.jck.lib.SecurityTestRunner.runTestWithTCKSM(SecurityTestRunner.java:278)

at javasoft.sqe.jck.lib.SecurityTestRunner.runTestWithPermissions(SecurityTestRunner.java:235)

at javasoft.sqe.jck.lib.SecurityTestRunner.runTestWithAllPermissions(SecurityTestRunner.java:157)

at javasoft.sqe.jck.lib.AllPermissionSM.testRun(AllPermissionSM.java:86)

at javasoft.sqe.jck.lib.AllPermissionSM.testRun(AllPermissionSM.java:133)

at javasoft.sqe.tests.api.java.nio.channels.FileChannel.MethodsTests.FileChannel0031(MethodsTests.java:682)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

at java.lang.reflect.Method.invoke(Method.java:585)

at javasoft.sqe.javatest.lib.MultiTest.invokeTestCase(MultiTest.java:399)

at javasoft.sqe.javatest.lib.MultiTest.run(MultiTest.java:195)

at javasoft.sqe.javatest.lib.MultiTest.run(MultiTest.java:127)

at javasoft.sqe.tests.api.java.nio.channels.FileChannel.MethodsTests.main(MethodsTests.java:59)

Analysis on the test cases revealed that force() method is invoked on the channel obtained from a FileInputStream object in the above test cases and this causes an exception to be thrown.

The force() method is implemented internally by making use of fsync() function. The man page of fsync() in Linux clearly says that

fsync fails if one or more of the following are true:

EBADF fildes is not a valid file descriptor open for writing.

The FileInputStream object will always open the file in read only mode and hence the file descriptor will not be a valid descriptor for writing. Even then in Linux the fsync() will succeed on a file descriptor valid only for reading.

In our proprietary unix based on SVR4, the man page specifies the same condition and fsync() fails if the file descriptor is not valid for writing and hence an exception is thrown.

The FileInputStream object can be used only for reading raw bytes from a file. A channel obtained via the getChannel method of a FileInputStream instance will be open for reading. Hence logically also it doesn抰 make sense to do a force() on a FileChannel object obtained from FileInputStream.

[3432 byte] By [mary_priyaa] at [2007-11-27 0:34:28]
# 1
(ignore msync comment as this is not a file mapping). Can you trace the system calls to see if the file descriptor has been closed before the fsync?
alan.batemana at 2007-7-11 22:42:11 > top of Java-index,Core,Core APIs...
# 2

I traced the system calls and ensured that the file descriptor is not closed before fsync.

I have also tried a sample C program which will actually create the file and open it in the same way as it is done through the java program and then do a fsync on that file. This fails on our proprietary unix based on SVR4 and passes in Linux.

My query is : Does it make sense to do fsync on a channel obtained from a FileInputStream object. Isn't it a problem with the logic of test case?

mary_priyaa at 2007-7-11 22:42:11 > top of Java-index,Core,Core APIs...
# 3
The force method may be used but when the file is open for read-only then it is not required to do anything. If this SVR4 returns EBADF if the file is opened read-only then I suggest making it a no-op for that case.
alan.batemana at 2007-7-11 22:42:11 > top of Java-index,Core,Core APIs...