7038105: File.isHidden() should return true for pagefile.sys and hiberfil.sys
authorrobm
Tue, 28 May 2013 16:35:17 +0100
changeset 17722 d891aa9e1b74
parent 17721 1d6516794d05
child 17723 ae7be7852a99
7038105: File.isHidden() should return true for pagefile.sys and hiberfil.sys Reviewed-by: alanb
jdk/src/windows/native/java/io/WinNTFileSystem_md.c
jdk/test/java/io/File/IsHidden.java
--- a/jdk/src/windows/native/java/io/WinNTFileSystem_md.c	Tue May 28 18:46:39 2013 +0400
+++ b/jdk/src/windows/native/java/io/WinNTFileSystem_md.c	Tue May 28 16:35:17 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -199,7 +199,7 @@
 
 /**
  * If the given attributes are the attributes of a reparse point, then
- * read and return the attributes of the final target.
+ * read and return the attributes of the special cases.
  */
 DWORD getFinalAttributesIfReparsePoint(WCHAR *path, DWORD a)
 {
@@ -213,6 +213,28 @@
     return a;
 }
 
+/**
+ * Take special cases into account when retrieving the attributes
+ * of path
+ */
+DWORD getFinalAttributes(WCHAR *path)
+{
+    DWORD attr = INVALID_FILE_ATTRIBUTES;
+
+    WIN32_FILE_ATTRIBUTE_DATA wfad;
+    WIN32_FIND_DATAW wfd;
+    HANDLE h;
+
+    if (GetFileAttributesExW(path, GetFileExInfoStandard, &wfad)) {
+        attr = getFinalAttributesIfReparsePoint(path, wfad.dwFileAttributes);
+    } else if (GetLastError() == ERROR_SHARING_VIOLATION &&
+               (h = FindFirstFileW(path, &wfd)) != INVALID_HANDLE_VALUE) {
+        attr = getFinalAttributesIfReparsePoint(path, wfd.dwFileAttributes);
+        CloseHandle(h);
+    }
+    return attr;
+}
+
 JNIEXPORT jstring JNICALL
 Java_java_io_WinNTFileSystem_canonicalize0(JNIEnv *env, jobject this,
                                            jstring pathname)
@@ -337,38 +359,21 @@
 Java_java_io_WinNTFileSystem_getBooleanAttributes(JNIEnv *env, jobject this,
                                                   jobject file)
 {
-
     jint rv = 0;
     jint pathlen;
 
-    /* both pagefile.sys and hiberfil.sys have length 12 */
-#define SPECIALFILE_NAMELEN 12
-
     WCHAR *pathbuf = fileToNTPath(env, file, ids.path);
-    WIN32_FILE_ATTRIBUTE_DATA wfad;
     if (pathbuf == NULL)
         return rv;
     if (!isReservedDeviceNameW(pathbuf)) {
-        if (GetFileAttributesExW(pathbuf, GetFileExInfoStandard, &wfad)) {
-            DWORD a = getFinalAttributesIfReparsePoint(pathbuf, wfad.dwFileAttributes);
-            if (a != INVALID_FILE_ATTRIBUTES) {
-                rv = (java_io_FileSystem_BA_EXISTS
-                    | ((a & FILE_ATTRIBUTE_DIRECTORY)
-                        ? java_io_FileSystem_BA_DIRECTORY
-                        : java_io_FileSystem_BA_REGULAR)
-                    | ((a & FILE_ATTRIBUTE_HIDDEN)
-                        ? java_io_FileSystem_BA_HIDDEN : 0));
-            }
-        } else { /* pagefile.sys is a special case */
-            if (GetLastError() == ERROR_SHARING_VIOLATION) {
-                rv = java_io_FileSystem_BA_EXISTS;
-                if ((pathlen = (jint)wcslen(pathbuf)) >= SPECIALFILE_NAMELEN &&
-                    (_wcsicmp(pathbuf + pathlen - SPECIALFILE_NAMELEN,
-                              L"pagefile.sys") == 0) ||
-                    (_wcsicmp(pathbuf + pathlen - SPECIALFILE_NAMELEN,
-                              L"hiberfil.sys") == 0))
-                  rv |= java_io_FileSystem_BA_REGULAR;
-            }
+        DWORD a = getFinalAttributes(pathbuf);
+        if (a != INVALID_FILE_ATTRIBUTES) {
+            rv = (java_io_FileSystem_BA_EXISTS
+                | ((a & FILE_ATTRIBUTE_DIRECTORY)
+                    ? java_io_FileSystem_BA_DIRECTORY
+                    : java_io_FileSystem_BA_REGULAR)
+                | ((a & FILE_ATTRIBUTE_HIDDEN)
+                    ? java_io_FileSystem_BA_HIDDEN : 0));
         }
     }
     free(pathbuf);
--- a/jdk/test/java/io/File/IsHidden.java	Tue May 28 18:46:39 2013 +0400
+++ b/jdk/test/java/io/File/IsHidden.java	Tue May 28 16:35:17 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -46,6 +46,11 @@
         Files.getFileAttributeView(f.toPath(), DosFileAttributeView.class).setHidden(value);
     }
 
+    private static void checkHidden(File f) {
+        if (!f.isHidden())
+            throw new RuntimeException(f + " should be hidden");
+    }
+
     private static void testWin32() throws Exception {
         File f = new File(dir, "test");
         f.deleteOnExit();
@@ -58,6 +63,11 @@
         }
         ck(".foo", false);
         ck("foo", false);
+
+        File pagefile = new File("C:\\pagefile.sys");
+        File hiberfil = new File("C:\\hiberfil.sys");
+        if (pagefile.exists()) checkHidden(pagefile);
+        if (hiberfil.exists()) checkHidden(hiberfil);
     }
 
     private static void testUnix() throws Exception {