United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-7098404 File.setWritable() / File.canWrite() not behaving as expected
JDK-7098404 : File.setWritable() / File.canWrite() not behaving as expected

Details
Type:
Bug
Submit Date:
2011-10-06
Status:
Closed
Updated Date:
2012-02-03
Project Name:
JDK
Resolved Date:
2012-02-03
Component:
core-libs
OS:
windows
Sub-Component:
java.io
CPU:
generic
Priority:
P3
Resolution:
Fixed
Affected Versions:
6u23
Fixed Versions:
6u32 (b01)

Related Reports
Relates:

Sub Tasks

Description
SYNOPSIS
--------
File.setWritable() / File.canWrite() not behaving as expected

OPERATING SYSTEM
----------------
Windows

FULL JDK VERSION
----------------
Java 6, from 1.6.0_23 onwards
Not reproducible on Java 7

PROBLEM DESCRIPTION from LICENSEE
---------------------------------
If setWritable(false) returns true, canWrite() should return false.

From 1.6.0_23, canWrite() is returning true under these circumstances, which is not correct. The problem seems to be caused by an incomplete backport of the changes for CR 6728842 from Java 7 (see suggested fix).

TESTCASE
--------
import java.io.File;

public class Test {
    public static void main(String[] args){
        File f = new File("wibblefish");
        f.deleteOnExit();
        if (f.mkdirs())
            if (f.setWritable(false,false)) {
                if (f.canWrite()) {
                    System.out.println("Test failed");
                } else {
                    System.out.println("Test passed (setWritable() succeeded)");
                }
            } else {
                System.out.println("Test Passed (setWritable() failed)");
            }
    }
}

REPRODUCTION INSTRUCTIONS
-------------------------
Compile and execute the above given testcase.

Expected output:
Test passed (setWritable() succeeded)
OR
Test Passed (setWritable() failed)

Observed output:
Test failed

SUGGESTED FIX from LICENSEE
---------------------------
See Suggested Fix section of CR.

                                    

Comments
SUGGESTED FIX

According to Licensee, the fix is to apply the missing part of the Java 7 fix
for 6728842. Specifically, in Java_java_io_WinNTFileSystem_setPermission():

+++ j2se/src/windows/native/java/io/WinNTFileSystem_md.c
@@ -254,7 +254,9 @@
     if (pathbuf == NULL)
         return JNI_FALSE;
     a = GetFileAttributesW(pathbuf);
-    if (a != INVALID_FILE_ATTRIBUTES) {
+    if ((a != INVALID_FILE_ATTRIBUTES) &&
+        ((a & FILE_ATTRIBUTE_DIRECTORY) == 0))
+    {
         if (enable)
             a =  a & ~FILE_ATTRIBUTE_READONLY;
         else
                                     
2011-10-06
EVALUATION

as per suggested fix.
                                     
2011-11-14



Hardware and Software, Engineered to Work Together