I need a reviewer for 4939819. This is one that the desktop folks have
been asking to be fixed for some time. On Windows the DOS read-only
attribute doesn't make a directory read-only. Instead this attribute
just prevents a directory from being deleted. The bug is that
java.io.File's canWrite method returns false when this attribute is set
whereas it should be ignored when checking access to directories. The
attached patch fixes this by ignoring the attribute on directories.
There shouldn't be any side effects moving from _waccess to
GetFileAttributes because the Microsoft CRT has always implemented
_waccess as a call to GetFileAttributesW.
Thanks,
Alan.
diff -r f4205a7bdfd4 src/windows/native/java/io/WinNTFileSystem_md.c
--- a/src/windows/native/java/io/WinNTFileSystem_md.c Mon Apr 07 14:19:23
2008 -0700
+++ b/src/windows/native/java/io/WinNTFileSystem_md.c Wed Apr 09 14:10:23
2008 +0100
@@ -229,26 +229,29 @@ JNICALL Java_java_io_WinNTFileSystem_che
JNICALL Java_java_io_WinNTFileSystem_checkAccess(JNIEnv *env, jobject this,
jobject file, jint access)
{
- jboolean rv = JNI_FALSE;
- int mode;
- WCHAR *pathbuf = fileToNTPath(env, file, ids.path);
- if (pathbuf == NULL)
+ 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);
- }
- if (_waccess(pathbuf, mode) == 0) {
- rv = JNI_TRUE;
- }
- free(pathbuf);
- return rv;
+ /* 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;
+ }
}
JNIEXPORT jboolean JNICALL
diff -r f4205a7bdfd4 test/java/io/File/SetReadOnly.java
--- a/test/java/io/File/SetReadOnly.java Mon Apr 07 14:19:23 2008 -0700
+++ b/test/java/io/File/SetReadOnly.java Wed Apr 09 14:10:23 2008 +0100
@@ -22,7 +22,7 @@
*/
/* @test
- @bug 4091757
+ @bug 4091757 4939819
@summary Basic test for setReadOnly method
*/
@@ -59,8 +59,15 @@ public class SetReadOnly {
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");