6728842: File.setReadOnly does not make a directory read-only (win)
authoralanb
Tue, 12 Oct 2010 08:49:33 +0100
changeset 6884 65b1fa0a0fae
parent 6883 bbd0cba11983
child 6885 5605d7d60c9c
6728842: File.setReadOnly does not make a directory read-only (win) 6464744: java/io/File/SetAccess.java ignores sticky bit Reviewed-by: forax
jdk/src/windows/native/java/io/WinNTFileSystem_md.c
jdk/test/java/io/File/SetAccess.java
jdk/test/java/io/File/SetReadOnly.java
--- a/jdk/src/windows/native/java/io/WinNTFileSystem_md.c	Mon Oct 11 22:32:55 2010 -0700
+++ b/jdk/src/windows/native/java/io/WinNTFileSystem_md.c	Tue Oct 12 08:49:33 2010 +0100
@@ -434,7 +434,9 @@
             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
@@ -796,9 +798,10 @@
         }
     }
 
-    if (a != INVALID_FILE_ATTRIBUTES) {
+    if ((a != INVALID_FILE_ATTRIBUTES) &&
+        ((a & FILE_ATTRIBUTE_DIRECTORY) == 0)) {
         if (SetFileAttributesW(pathbuf, a | FILE_ATTRIBUTE_READONLY))
-        rv = JNI_TRUE;
+            rv = JNI_TRUE;
     }
     free(pathbuf);
     return rv;
--- a/jdk/test/java/io/File/SetAccess.java	Mon Oct 11 22:32:55 2010 -0700
+++ b/jdk/test/java/io/File/SetAccess.java	Tue Oct 12 08:49:33 2010 +0100
@@ -22,11 +22,12 @@
  */
 
 /* @test
-   @bug 4167472 5097703 6216563 6284003
+   @bug 4167472 5097703 6216563 6284003 6728842 6464744
    @summary Basic test for setWritable/Readable/Executable methods
  */
 
 import java.io.*;
+import java.nio.file.attribute.*;
 
 public class SetAccess {
     public static void main(String[] args) throws Exception {
@@ -49,8 +50,9 @@
     }
 
     public static void doTest(File f) throws Exception {
-        f.setReadOnly();
         if (!System.getProperty("os.name").startsWith("Windows")) {
+            if (!f.setReadOnly())
+                 throw new Exception(f + ": setReadOnly Failed");
             if (!f.setWritable(true, true) ||
                 !f.canWrite() ||
                 permission(f).charAt(2) != 'w')
@@ -119,40 +121,44 @@
                 throw new Exception(f + ": setReadable(false, true) Failed");
         } else {
             //Windows platform
-            if (!f.setWritable(true, true) || !f.canWrite())
-                throw new Exception(f + ": setWritable(true, ture) Failed");
-            if (!f.setWritable(true, false) || !f.canWrite())
-                throw new Exception(f + ": setWritable(true, false) Failed");
-            if (!f.setWritable(true) || !f.canWrite())
-                throw new Exception(f + ": setWritable(true, ture) Failed");
-            if (!f.setExecutable(true, true) || !f.canExecute())
-                throw new Exception(f + ": setExecutable(true, true) Failed");
-            if (!f.setExecutable(true, false) || !f.canExecute())
-                throw new Exception(f + ": setExecutable(true, false) Failed");
-            if (!f.setExecutable(true) || !f.canExecute())
-                throw new Exception(f + ": setExecutable(true, true) Failed");
-            if (!f.setReadable(true, true) || !f.canRead())
-                throw new Exception(f + ": setReadable(true, true) Failed");
-            if (!f.setReadable(true, false) || !f.canRead())
-                throw new Exception(f + ": setReadable(true, false) Failed");
-            if (!f.setReadable(true) || !f.canRead())
-                throw new Exception(f + ": setReadable(true, true) Failed");
+            if (f.isFile()) {
+                if (!f.setReadOnly())
+                    throw new Exception(f + ": setReadOnly Failed");
+                if (!f.setWritable(true, true) || !f.canWrite())
+                    throw new Exception(f + ": setWritable(true, ture) Failed");
+                if (!f.setWritable(true, false) || !f.canWrite())
+                    throw new Exception(f + ": setWritable(true, false) Failed");
+                if (!f.setWritable(true) || !f.canWrite())
+                    throw new Exception(f + ": setWritable(true, ture) Failed");
+                if (!f.setExecutable(true, true) || !f.canExecute())
+                    throw new Exception(f + ": setExecutable(true, true) Failed");
+                if (!f.setExecutable(true, false) || !f.canExecute())
+                    throw new Exception(f + ": setExecutable(true, false) Failed");
+                if (!f.setExecutable(true) || !f.canExecute())
+                    throw new Exception(f + ": setExecutable(true, true) Failed");
+                if (!f.setReadable(true, true) || !f.canRead())
+                    throw new Exception(f + ": setReadable(true, true) Failed");
+                if (!f.setReadable(true, false) || !f.canRead())
+                    throw new Exception(f + ": setReadable(true, false) Failed");
+                if (!f.setReadable(true) || !f.canRead())
+                    throw new Exception(f + ": setReadable(true, true) Failed");
+            }
             if (f.isDirectory()) {
-                //All directories on Windows always have read&write access perm,
-                //setting a directory to "unwritable" actually means "not deletable"
-                if (!f.setWritable(false, true) || !f.canWrite())
-                    throw new Exception(f + ": setWritable(false, true) Failed");
-                if (!f.setWritable(false, false) || !f.canWrite())
-                    throw new Exception(f + ": setWritable(false, true) Failed");
-                if (!f.setWritable(false) || !f.canWrite())
-                    throw new Exception(f + ": setWritable(false, true) Failed");
+                // setWritable should fail on directories because the DOS readonly
+                // attribute prevents a directory from being deleted.
+                if (f.setWritable(false, true))
+                    throw new Exception(f + ": setWritable(false, true) Succeeded");
+                if (f.setWritable(false, false))
+                    throw new Exception(f + ": setWritable(false, false) Succeeded");
+                if (f.setWritable(false))
+                    throw new Exception(f + ": setWritable(false) Succeeded");
             } else {
                 if (!f.setWritable(false, true) || f.canWrite())
                     throw new Exception(f + ": setWritable(false, true) Failed");
                 if (!f.setWritable(false, false) || f.canWrite())
-                    throw new Exception(f + ": setWritable(false, true) Failed");
+                    throw new Exception(f + ": setWritable(false, false) Failed");
                 if (!f.setWritable(false) || f.canWrite())
-                    throw new Exception(f + ": setWritable(false, true) Failed");
+                    throw new Exception(f + ": setWritable(false) Failed");
             }
             if (f.setExecutable(false, true))
                 throw new Exception(f + ": setExecutable(false, true) Failed");
@@ -172,14 +178,8 @@
     }
 
     private static String permission(File f) throws Exception {
-        byte[] bb = new byte[1024];
-        String command = f.isDirectory()?"ls -dl ":"ls -l ";
-        int len = Runtime.getRuntime()
-                         .exec(command + f.getPath())
-                         .getInputStream()
-                         .read(bb, 0, 1024);
-        if (len > 0)
-            return new String(bb, 0, len).substring(0, 10);
-        return "";
+        PosixFileAttributes attrs = Attributes.readPosixFileAttributes(f.toPath());
+        String type = attrs.isDirectory() ? "d" : " ";
+        return type + PosixFilePermissions.toString(attrs.permissions());
     }
 }
--- a/jdk/test/java/io/File/SetReadOnly.java	Mon Oct 11 22:32:55 2010 -0700
+++ b/jdk/test/java/io/File/SetReadOnly.java	Tue Oct 12 08:49:33 2010 +0100
@@ -22,7 +22,7 @@
  */
 
 /* @test
-   @bug 4091757 4939819
+   @bug 4091757 4939819 6728842
    @summary Basic test for setReadOnly method
  */
 
@@ -57,17 +57,8 @@
         }
         if (!f.mkdir())
             throw new Exception(f + ": Cannot create directory");
-        if (!f.setReadOnly())
-            throw new Exception(f + ": Failed on directory");
-        // 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.setReadOnly() && f.canWrite())
+            throw new Exception(f + ": Directory is writeable");
         if (!f.delete())
             throw new Exception(f + ": Cannot delete directory");