JDK-7098404 : File.setWritable() / File.canWrite() not behaving as expected
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.io
  • Affected Version: 6u23
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: windows
  • CPU: generic
  • Submitted: 2011-10-06
  • Updated: 2012-02-03
  • Resolved: 2012-02-03
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 Availabitlity Release.

To download the current JDK release, click here.
JDK 6
6u32 b01Fixed
Related Reports
Relates :  
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
EVALUATION as per suggested fix.
2011-11-14

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