8201407: Files.move throws DirectoryNonEmptyException when moving directory across file system
Alan Bateman
Alan.Bateman at oracle.com
Sun May 13 07:52:42 UTC 2018
On 11/05/2018 00:26, Brian Burkhalter wrote:
> https://bugs.openjdk.java.net/browse/JDK-8201407
>
> On Unix, when attempting to move a non-empty directory to a different file system, Files.move() fails with a DirectoryNotEmptyException when it tries to delete the non-empty source directory after copying the top level directory only. The operative part of the specification of Files.move() is:
>
> "When invoked to move a directory that is not empty then the directory is moved if it does not require moving the entries in the directory. For example, renaming a directory on the same FileStore will usually not require moving the entries in the directory. When moving a directory requires that its entries be moved then this method fails (by throwing an IOException).”
>
> Although the DirectoryNotEmptyException is an IOException, it is not clearly specified that one will be thrown when attempting to move a non-empty source directory to a different file system. Also this situation is not detected in the code as early as it could be. A partial (no Windows change, no tests) fix is here:
>
> http://cr.openjdk.java.net/~bpb/8201407/webrev.00/
>
> If this looks like the correct thing to do, then I will file a CSR, develop an analogous Windows fix, and add tests.
>
This subtly changes DirectoryNotEmptyException from an "optional
specific exception" to a mandated exception. I think it would be better
to just add to the existing @throws DirectoryNotEmptyException as the
spec can't guarantee that you can throw this specific exception in all
scenarios.
As regards whether to attempt to check for entries before creating the
target directory then that seems worth exploring.
-Alan
More information about the nio-dev
mailing list