jdk/src/windows/classes/sun/nio/fs/WindowsFileAttributes.java
changeset 3423 3dd3615c1748
parent 3065 452aaa2899fc
child 3628 2768d95a0e7d
--- a/jdk/src/windows/classes/sun/nio/fs/WindowsFileAttributes.java	Wed Jul 29 21:45:52 2009 -0700
+++ b/jdk/src/windows/classes/sun/nio/fs/WindowsFileAttributes.java	Fri Jul 31 08:44:28 2009 +0100
@@ -299,6 +299,9 @@
         throws WindowsException
     {
         if (!ensureAccurateMetadata) {
+            WindowsException firstException = null;
+
+            // GetFileAttributesEx is the fastest way to read the attributes
             NativeBuffer buffer =
                 NativeBuffers.getNativeBuffer(SIZEOF_FILE_ATTRIBUTE_DATA);
             try {
@@ -310,9 +313,39 @@
                     .getInt(address + OFFSETOF_FILE_ATTRIBUTE_DATA_ATTRIBUTES);
                 if ((fileAttrs & FILE_ATTRIBUTE_REPARSE_POINT) == 0)
                     return fromFileAttributeData(address, 0);
+            } catch (WindowsException x) {
+                if (x.lastError() != ERROR_SHARING_VIOLATION)
+                    throw x;
+                firstException = x;
             } finally {
                 buffer.release();
             }
+
+            // For sharing violations, fallback to FindFirstFile if the file
+            // is not a root directory.
+            if (firstException != null) {
+                String search = path.getPathForWin32Calls();
+                char last = search.charAt(search.length() -1);
+                if (last == ':' || last == '\\')
+                    throw firstException;
+                buffer = getBufferForFindData();
+                try {
+                    long handle = FindFirstFile(search, buffer.address());
+                    FindClose(handle);
+                    WindowsFileAttributes attrs = fromFindData(buffer.address());
+                    // FindFirstFile does not follow sym links. Even if
+                    // followLinks is false, there isn't sufficient information
+                    // in the WIN32_FIND_DATA structure to know if the reparse
+                    // point is a sym link.
+                    if (attrs.isReparsePoint())
+                        throw firstException;
+                    return attrs;
+                } catch (WindowsException ignore) {
+                    throw firstException;
+                } finally {
+                    buffer.release();
+                }
+            }
         }
 
         // file is reparse point so need to open file to get attributes