4939819: File.canWrite() returns false for the "My Documents" directory (win)
authoralanb
Thu, 05 Jun 2008 14:44:30 +0100
changeset 691 2033c9b86813
parent 687 874e25a9844a
child 692 50a393caed65
4939819: File.canWrite() returns false for the "My Documents" directory (win) Reviewed-by: iris
jdk/src/windows/native/java/io/WinNTFileSystem_md.c
jdk/test/java/io/File/SetReadOnly.java
--- a/jdk/src/windows/native/java/io/WinNTFileSystem_md.c	Thu Jun 05 13:42:47 2008 +0200
+++ b/jdk/src/windows/native/java/io/WinNTFileSystem_md.c	Thu Jun 05 14:44:30 2008 +0100
@@ -229,26 +229,29 @@
 JNICALL Java_java_io_WinNTFileSystem_checkAccess(JNIEnv *env, jobject this,
                                                  jobject file, jint access)
 {
-    jboolean rv = JNI_FALSE;
-    int mode;
+    DWORD attr;
     WCHAR *pathbuf = fileToNTPath(env, file, ids.path);
     if (pathbuf == NULL)
         return JNI_FALSE;
+    attr = GetFileAttributesW(pathbuf);
+    free(pathbuf);
+    if (attr == INVALID_FILE_ATTRIBUTES)
+        return JNI_FALSE;
     switch (access) {
     case java_io_FileSystem_ACCESS_READ:
     case java_io_FileSystem_ACCESS_EXECUTE:
-        mode = 4;
-        break;
+        return JNI_TRUE;
     case java_io_FileSystem_ACCESS_WRITE:
-        mode = 2;
-        break;
-    default: assert(0);
+        /* Read-only attribute ignored on directories */
+        if ((attr & FILE_ATTRIBUTE_DIRECTORY) ||
+            (attr & FILE_ATTRIBUTE_READONLY) == 0)
+            return JNI_TRUE;
+        else
+            return JNI_FALSE;
+    default:
+        assert(0);
+        return JNI_FALSE;
     }
-    if (_waccess(pathbuf, mode) == 0) {
-        rv = JNI_TRUE;
-    }
-    free(pathbuf);
-    return rv;
 }
 
 JNIEXPORT jboolean JNICALL
--- a/jdk/test/java/io/File/SetReadOnly.java	Thu Jun 05 13:42:47 2008 +0200
+++ b/jdk/test/java/io/File/SetReadOnly.java	Thu Jun 05 14:44:30 2008 +0100
@@ -22,7 +22,7 @@
  */
 
 /* @test
-   @bug 4091757
+   @bug 4091757 4939819
    @summary Basic test for setReadOnly method
  */
 
@@ -59,8 +59,15 @@
             throw new Exception(f + ": Cannot create directory");
         if (!f.setReadOnly())
             throw new Exception(f + ": Failed on directory");
-        if (f.canWrite())
-            throw new Exception(f + ": Directory is writeable");
+        // The readonly attribute on Windows does not make a folder read-only
+        if (System.getProperty("os.name").startsWith("Windows")) {
+            if (!f.canWrite())
+                throw new Exception(f + ": Directory is not writeable");
+        } else {
+            if (f.canWrite())
+                throw new Exception(f + ": Directory is writeable");
+        }
+
         if (!f.delete())
             throw new Exception(f + ": Cannot delete directory");