6728842: File.setReadOnly does not make a directory read-only (win)

6464744: java/io/File/SetAccess.java ignores sticky bit

Reviewed-by: forax
This commit is contained in:
Alan Bateman 2010-10-12 08:49:33 +01:00
parent 48e04c75c5
commit 79abb3e80f
3 changed files with 48 additions and 54 deletions

View File

@ -434,7 +434,9 @@ Java_java_io_WinNTFileSystem_setPermission(JNIEnv *env, jobject this,
a = GetFileAttributesW(pathbuf); a = GetFileAttributesW(pathbuf);
} }
} }
if (a != INVALID_FILE_ATTRIBUTES) { if ((a != INVALID_FILE_ATTRIBUTES) &&
((a & FILE_ATTRIBUTE_DIRECTORY) == 0))
{
if (enable) if (enable)
a = a & ~FILE_ATTRIBUTE_READONLY; a = a & ~FILE_ATTRIBUTE_READONLY;
else else
@ -796,9 +798,10 @@ Java_java_io_WinNTFileSystem_setReadOnly(JNIEnv *env, jobject this,
} }
} }
if (a != INVALID_FILE_ATTRIBUTES) { if ((a != INVALID_FILE_ATTRIBUTES) &&
((a & FILE_ATTRIBUTE_DIRECTORY) == 0)) {
if (SetFileAttributesW(pathbuf, a | FILE_ATTRIBUTE_READONLY)) if (SetFileAttributesW(pathbuf, a | FILE_ATTRIBUTE_READONLY))
rv = JNI_TRUE; rv = JNI_TRUE;
} }
free(pathbuf); free(pathbuf);
return rv; return rv;

View File

@ -22,11 +22,12 @@
*/ */
/* @test /* @test
@bug 4167472 5097703 6216563 6284003 @bug 4167472 5097703 6216563 6284003 6728842 6464744
@summary Basic test for setWritable/Readable/Executable methods @summary Basic test for setWritable/Readable/Executable methods
*/ */
import java.io.*; import java.io.*;
import java.nio.file.attribute.*;
public class SetAccess { public class SetAccess {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
@ -49,8 +50,9 @@ public class SetAccess {
} }
public static void doTest(File f) throws Exception { public static void doTest(File f) throws Exception {
f.setReadOnly();
if (!System.getProperty("os.name").startsWith("Windows")) { if (!System.getProperty("os.name").startsWith("Windows")) {
if (!f.setReadOnly())
throw new Exception(f + ": setReadOnly Failed");
if (!f.setWritable(true, true) || if (!f.setWritable(true, true) ||
!f.canWrite() || !f.canWrite() ||
permission(f).charAt(2) != 'w') permission(f).charAt(2) != 'w')
@ -119,40 +121,44 @@ public class SetAccess {
throw new Exception(f + ": setReadable(false, true) Failed"); throw new Exception(f + ": setReadable(false, true) Failed");
} else { } else {
//Windows platform //Windows platform
if (!f.setWritable(true, true) || !f.canWrite()) if (f.isFile()) {
throw new Exception(f + ": setWritable(true, ture) Failed"); if (!f.setReadOnly())
if (!f.setWritable(true, false) || !f.canWrite()) throw new Exception(f + ": setReadOnly Failed");
throw new Exception(f + ": setWritable(true, false) Failed"); if (!f.setWritable(true, true) || !f.canWrite())
if (!f.setWritable(true) || !f.canWrite()) throw new Exception(f + ": setWritable(true, ture) Failed");
throw new Exception(f + ": setWritable(true, ture) Failed"); if (!f.setWritable(true, false) || !f.canWrite())
if (!f.setExecutable(true, true) || !f.canExecute()) throw new Exception(f + ": setWritable(true, false) Failed");
throw new Exception(f + ": setExecutable(true, true) Failed"); if (!f.setWritable(true) || !f.canWrite())
if (!f.setExecutable(true, false) || !f.canExecute()) throw new Exception(f + ": setWritable(true, ture) Failed");
throw new Exception(f + ": setExecutable(true, false) Failed"); if (!f.setExecutable(true, true) || !f.canExecute())
if (!f.setExecutable(true) || !f.canExecute()) throw new Exception(f + ": setExecutable(true, true) Failed");
throw new Exception(f + ": setExecutable(true, true) Failed"); if (!f.setExecutable(true, false) || !f.canExecute())
if (!f.setReadable(true, true) || !f.canRead()) throw new Exception(f + ": setExecutable(true, false) Failed");
throw new Exception(f + ": setReadable(true, true) Failed"); if (!f.setExecutable(true) || !f.canExecute())
if (!f.setReadable(true, false) || !f.canRead()) throw new Exception(f + ": setExecutable(true, true) Failed");
throw new Exception(f + ": setReadable(true, false) Failed"); if (!f.setReadable(true, true) || !f.canRead())
if (!f.setReadable(true) || !f.canRead()) throw new Exception(f + ": setReadable(true, true) Failed");
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()) { if (f.isDirectory()) {
//All directories on Windows always have read&write access perm, // setWritable should fail on directories because the DOS readonly
//setting a directory to "unwritable" actually means "not deletable" // attribute prevents a directory from being deleted.
if (!f.setWritable(false, true) || !f.canWrite()) if (f.setWritable(false, true))
throw new Exception(f + ": setWritable(false, true) Failed"); throw new Exception(f + ": setWritable(false, true) Succeeded");
if (!f.setWritable(false, false) || !f.canWrite()) if (f.setWritable(false, false))
throw new Exception(f + ": setWritable(false, true) Failed"); throw new Exception(f + ": setWritable(false, false) Succeeded");
if (!f.setWritable(false) || !f.canWrite()) if (f.setWritable(false))
throw new Exception(f + ": setWritable(false, true) Failed"); throw new Exception(f + ": setWritable(false) Succeeded");
} else { } else {
if (!f.setWritable(false, true) || f.canWrite()) if (!f.setWritable(false, true) || f.canWrite())
throw new Exception(f + ": setWritable(false, true) Failed"); throw new Exception(f + ": setWritable(false, true) Failed");
if (!f.setWritable(false, false) || f.canWrite()) 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()) 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)) if (f.setExecutable(false, true))
throw new Exception(f + ": setExecutable(false, true) Failed"); throw new Exception(f + ": setExecutable(false, true) Failed");
@ -172,14 +178,8 @@ public class SetAccess {
} }
private static String permission(File f) throws Exception { private static String permission(File f) throws Exception {
byte[] bb = new byte[1024]; PosixFileAttributes attrs = Attributes.readPosixFileAttributes(f.toPath());
String command = f.isDirectory()?"ls -dl ":"ls -l "; String type = attrs.isDirectory() ? "d" : " ";
int len = Runtime.getRuntime() return type + PosixFilePermissions.toString(attrs.permissions());
.exec(command + f.getPath())
.getInputStream()
.read(bb, 0, 1024);
if (len > 0)
return new String(bb, 0, len).substring(0, 10);
return "";
} }
} }

View File

@ -22,7 +22,7 @@
*/ */
/* @test /* @test
@bug 4091757 4939819 @bug 4091757 4939819 6728842
@summary Basic test for setReadOnly method @summary Basic test for setReadOnly method
*/ */
@ -57,17 +57,8 @@ public class SetReadOnly {
} }
if (!f.mkdir()) if (!f.mkdir())
throw new Exception(f + ": Cannot create directory"); throw new Exception(f + ": Cannot create directory");
if (!f.setReadOnly()) if (f.setReadOnly() && f.canWrite())
throw new Exception(f + ": Failed on directory"); 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()) if (!f.delete())
throw new Exception(f + ": Cannot delete directory"); throw new Exception(f + ": Cannot delete directory");