Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8201407

Files.move throws DirectoryNotEmptyException when moving directory across file system

XMLWordPrintable

    • b18
    • x86
    • os_x

        FULL PRODUCT VERSION :
        java version "1.8.0_152"
        Java(TM) SE Runtime Environment (build 1.8.0_152-b16)
        Java HotSpot(TM) 64-Bit Server VM (build 25.152-b16, mixed mode)


        ADDITIONAL OS VERSION INFORMATION :
        macOS High Sierra (10.13.3)

        A DESCRIPTION OF THE PROBLEM :
        The following test demonstrates the bug. I believe this bug is also in JDK 7, but I have not confirmed.



        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        I ran the attached test case, which demonstrates that I cannot move non-empty directory across file system. If the source directory is empty, then the move succeeds.

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        Expected test to succeed
        ACTUAL -
        java.nio.file.DirectoryNotEmptyException: mySrc

        at sun.nio.fs.UnixFileSystemProvider.implDelete(UnixFileSystemProvider.java:242)
        at sun.nio.fs.AbstractFileSystemProvider.delete(AbstractFileSystemProvider.java:103)
        at java.nio.file.Files.delete(Files.java:1126)
        at java.nio.file.CopyMoveHelper.moveToForeignTarget(CopyMoveHelper.java:158)
        at java.nio.file.Files.move(Files.java:1398)
        at test.file.move.MoveDirectoryAcrossFileSystems.test(MoveDirectoryAcrossFileSystems.java:26)
        ...

        REPRODUCIBILITY :
        This bug can be reproduced always.

        ---------- BEGIN SOURCE ----------
        package test.file.move;

        import com.google.common.jimfs.Configuration;
        import com.google.common.jimfs.Jimfs;
        import org.apache.commons.io.FileUtils;
        import org.junit.jupiter.api.AfterEach;
        import org.junit.jupiter.api.Test;

        import java.nio.channels.FileChannel;
        import java.nio.file.*;

        import static org.junit.jupiter.api.Assertions.assertEquals;
        import static org.junit.jupiter.api.Assertions.assertFalse;
        import static org.junit.jupiter.api.Assertions.assertTrue;

        public class MoveDirectoryAcrossFileSystems {
          Path src = Paths.get("mySrc");

          FileSystem fs = Jimfs.newFileSystem(Configuration.unix());
          Path dest = fs.getPath("/dest");

          @Test
          public void test() throws Exception {
            Files.createDirectory(src);
            FileChannel.open(src.resolve("file"), StandardOpenOption.WRITE, StandardOpenOption.CREATE_NEW).close();
            assertEquals(dest, Files.move(src, dest));

            assertFalse(Files.exists(src));
            assertTrue(Files.exists(dest.resolve("file")));
          }

          @AfterEach
          public void cleanup() {
            FileUtils.deleteQuietly(src.toFile());
            FileUtils.deleteQuietly(dest.toFile());
          }
        }

        ---------- END SOURCE ----------

        CUSTOMER SUBMITTED WORKAROUND :
        I have not found one, but I would expect to be able to code a move across file system manually.

              bpb Brian Burkhalter
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Created:
                Updated:
                Resolved: