8032220: Files.createDirectories throws exception with confusing message for root directories that exist
authoralanb
Tue, 21 Jan 2014 09:33:48 +0000
changeset 22351 d8ac878ba804
parent 22350 4143cdafa0d5
child 22352 ecbf37860ffa
8032220: Files.createDirectories throws exception with confusing message for root directories that exist Reviewed-by: chegar
jdk/src/share/classes/java/nio/file/Files.java
jdk/src/solaris/classes/sun/nio/fs/UnixFileSystemProvider.java
jdk/src/windows/classes/sun/nio/fs/WindowsFileSystemProvider.java
jdk/test/java/nio/file/Files/Misc.java
--- a/jdk/src/share/classes/java/nio/file/Files.java	Tue Jan 21 09:17:23 2014 +0100
+++ b/jdk/src/share/classes/java/nio/file/Files.java	Tue Jan 21 09:33:48 2014 +0000
@@ -752,9 +752,12 @@
         }
         if (parent == null) {
             // unable to find existing parent
-            if (se != null)
+            if (se == null) {
+                throw new FileSystemException(dir.toString(), null,
+                    "Unable to determine if root directory exists");
+            } else {
                 throw se;
-            throw new IOException("Root directory does not exist");
+            }
         }
 
         // create directories
--- a/jdk/src/solaris/classes/sun/nio/fs/UnixFileSystemProvider.java	Tue Jan 21 09:17:23 2014 +0100
+++ b/jdk/src/solaris/classes/sun/nio/fs/UnixFileSystemProvider.java	Tue Jan 21 09:33:48 2014 +0000
@@ -375,11 +375,12 @@
         UnixPath dir = UnixPath.toUnixPath(obj);
         dir.checkWrite();
 
-        int mode = UnixFileModeAttribute
-            .toUnixMode(UnixFileModeAttribute.ALL_PERMISSIONS, attrs);
+        int mode = UnixFileModeAttribute.toUnixMode(UnixFileModeAttribute.ALL_PERMISSIONS, attrs);
         try {
             mkdir(dir, mode);
         } catch (UnixException x) {
+            if (x.errno() == EISDIR)
+                throw new FileAlreadyExistsException(dir.toString());
             x.rethrowAsIOException(dir);
         }
     }
--- a/jdk/src/windows/classes/sun/nio/fs/WindowsFileSystemProvider.java	Tue Jan 21 09:17:23 2014 +0100
+++ b/jdk/src/windows/classes/sun/nio/fs/WindowsFileSystemProvider.java	Tue Jan 21 09:33:48 2014 +0000
@@ -493,6 +493,14 @@
         try {
             CreateDirectory(dir.getPathForWin32Calls(), sd.address());
         } catch (WindowsException x) {
+            // convert ERROR_ACCESS_DENIED to FileAlreadyExistsException if we can
+            // verify that the directory exists
+            if (x.lastError() == ERROR_ACCESS_DENIED) {
+                try {
+                    if (WindowsFileAttributes.get(dir, false).isDirectory())
+                        throw new FileAlreadyExistsException(dir.toString());
+                } catch (WindowsException ignore) { }
+            }
             x.rethrowAsIOException(dir);
         } finally {
             sd.release();
--- a/jdk/test/java/nio/file/Files/Misc.java	Tue Jan 21 09:17:23 2014 +0100
+++ b/jdk/test/java/nio/file/Files/Misc.java	Tue Jan 21 09:33:48 2014 +0000
@@ -22,7 +22,7 @@
  */
 
 /* @test
- * @bug 4313887 6838333 8005566
+ * @bug 4313887 6838333 8005566 8032220
  * @summary Unit test for miscellenous methods in java.nio.file.Files
  * @library ..
  */
@@ -76,6 +76,11 @@
             createDirectories(file.resolve("y"));
             throw new RuntimeException("failure expected");
         } catch (IOException x) { }
+
+        // the root directory always exists
+        Path root = Paths.get("/");
+        Files.createDirectories(root);
+        Files.createDirectories(root.toAbsolutePath());
     }
 
     /**