8202076: test/jdk/java/io/File/WinSpecialFiles.java on windows with VS2017
authorbpb
Tue, 22 May 2018 16:19:31 -0700
changeset 50227 7b259287cdd2
parent 50226 408021edf22f
child 50228 45093fb73c6d
child 56593 3223aa3fcc6c
8202076: test/jdk/java/io/File/WinSpecialFiles.java on windows with VS2017 Summary: If all other means to obtain the file length fail, fall back to FindFirstFile Reviewed-by: igerasim, alanb
src/java.base/windows/native/libjava/WinNTFileSystem_md.c
test/jdk/java/io/File/WinSpecialFiles.java
--- a/src/java.base/windows/native/libjava/WinNTFileSystem_md.c	Wed May 23 00:21:58 2018 +0200
+++ b/src/java.base/windows/native/libjava/WinNTFileSystem_md.c	Tue May 22 16:19:31 2018 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2001, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2001, 2018, 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
@@ -35,6 +35,7 @@
 #include <direct.h>
 #include <windows.h>
 #include <io.h>
+#include <limits.h>
 
 #include "jni.h"
 #include "io_util.h"
@@ -527,13 +528,40 @@
         }
     } else {
         if (GetLastError() == ERROR_SHARING_VIOLATION) {
-            /* The error is "share violation", which means the file/dir
-               must exists. Try _wstati64, we know this at least works
-               for pagefile.sys and hiberfil.sys.
-            */
-            struct _stati64 sb;
-            if (_wstati64(pathbuf, &sb) == 0) {
-                rv = sb.st_size;
+            //
+            // The error is a "share violation", which means the file/dir
+            // must exist. Try FindFirstFile, we know this at least works
+            // for pagefile.sys.
+            //
+
+            WIN32_FIND_DATAW fileData;
+            HANDLE h = FindFirstFileW(pathbuf, &fileData);
+            if (h != INVALID_HANDLE_VALUE) {
+                if ((fileData.dwFileAttributes &
+                     FILE_ATTRIBUTE_REPARSE_POINT) == 0) {
+                    WCHAR backslash = L'\\';
+                    WCHAR *pslash = wcsrchr(pathbuf, backslash);
+                    if (pslash == NULL) {
+                        pslash = pathbuf;
+                    } else {
+                        pslash++;
+                    }
+                    WCHAR *fslash = wcsrchr(fileData.cFileName, backslash);
+                    if (fslash == NULL) {
+                        fslash = fileData.cFileName;
+                    } else {
+                        fslash++;
+                    }
+                    if (wcscmp(pslash, fslash) == 0) {
+                        ULARGE_INTEGER length;
+                        length.LowPart = fileData.nFileSizeLow;
+                        length.HighPart = fileData.nFileSizeHigh;
+                        if (length.QuadPart <= _I64_MAX) {
+                            rv = (jlong)length.QuadPart;
+                        }
+                    }
+                }
+                FindClose(h);
             }
         }
     }
--- a/test/jdk/java/io/File/WinSpecialFiles.java	Wed May 23 00:21:58 2018 +0200
+++ b/test/jdk/java/io/File/WinSpecialFiles.java	Tue May 22 16:19:31 2018 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2006, 2018, 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
@@ -22,7 +22,7 @@
  */
 
 /* @test
-   @bug 6192331 6348207
+   @bug 6192331 6348207 8202076
    @summary Check if File.exists()/length() works correctly on Windows
             special files hiberfil.sys and pagefile.sys
  */
@@ -45,9 +45,9 @@
             if (name.indexOf("pagefile.sys") != -1 ||
                 name.indexOf("hiberfil.sys") != -1) {
                 if (dir[i].length() == 0) {
-                    throw new Exception("Size of existing <"
+                    throw new Exception("Size of existing file \""
                                         + dir[i].getPath()
-                                        + " is ZERO");
+                                        + "\" is ZERO");
                 }
             }
         }