Commons IO
  1. Commons IO
  2. IO-217

FileUtils.copyDirectoryToDirectory makes infinite loops

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.4
    • Fix Version/s: 2.0
    • Component/s: None
    • Labels:
      None
    • Environment:

      Windows XP

      Description

      When using FileUtils.copyDirectoryToDirectory, infinite loops has occurred.

      – detail –
      src directory : D:\a
      dest directory : D:\a
      call : FileUtils.copyDirectoryToDirectory(new File("D:\a"), new File("D:\a"));
      expected result : directory D:\a\a will be created
      actual result : D:\a\a\a\a....... was created

      i guess FileUtils.copyDirectoryToDirectory causes of this result.
      ("destDir.mkdir()" is done before "srcDir.listFiles()")

      am i calling wrong method?

      thank you.

        Activity

        Hide
        Niall Pemberton added a comment -

        I created a test case to reproduce this and Roun Lee's patch does fix it - thanks, applied:

        http://svn.apache.org/viewvc?view=revision&revision=1002424

        Show
        Niall Pemberton added a comment - I created a test case to reproduce this and Roun Lee's patch does fix it - thanks, applied: http://svn.apache.org/viewvc?view=revision&revision=1002424
        Hide
        Roun Lee added a comment - - edited

        I have same trouble. I think this is a bug of commons-io.

        When you try to copying a directory from source to sub-directory of source directory, infinite loop happens.

        ex) FileUtils.copy(new File("test"), new File("test/test")):
        will fall into infinite loop.

        This is why the program copy source include the destination directory.

        I change the order of make destination directory and list of source directory. (attachment)

        This will list the files of source directory and create the subdirectory which is the destination.

        Show
        Roun Lee added a comment - - edited I have same trouble. I think this is a bug of commons-io. When you try to copying a directory from source to sub-directory of source directory, infinite loop happens. ex) FileUtils.copy(new File("test"), new File("test/test")): will fall into infinite loop. This is why the program copy source include the destination directory. I change the order of make destination directory and list of source directory. (attachment) This will list the files of source directory and create the subdirectory which is the destination.
        Hide
        Gwenaël Le Roux added a comment - - edited

        This issue is not reproduced on revision 982449 (trunk). An exception is throwned because the source and the target directories are the same, and no infinite loop is run.
        The aim of this issue (ie throwing an exception or creating a directory D:\a\a as required) should be precised by project leaders.

        The following code extract is testing the new execution.

         
        public void testCopyDirectoryToItself() throws Exception {
                File sourceDir = new File(getTestDirectory(), "source");
                if (!sourceDir.exists())
                    sourceDir.mkdirs();
        
                File childDir = new File(sourceDir, "child");
                if (!childDir.exists())
                    childDir.mkdirs();
                File file = new File(sourceDir, "file");
                createFile(file, 12345);
        
                try {
                    FileUtils.copyDirectory(sourceDir, sourceDir);
                    fail();
                } catch( IOException ex) {          
                }
        }
        

        Hope this could help.

        Show
        Gwenaël Le Roux added a comment - - edited This issue is not reproduced on revision 982449 (trunk). An exception is throwned because the source and the target directories are the same, and no infinite loop is run. The aim of this issue (ie throwing an exception or creating a directory D:\a\a as required) should be precised by project leaders. The following code extract is testing the new execution. public void testCopyDirectoryToItself() throws Exception { File sourceDir = new File(getTestDirectory(), "source"); if (!sourceDir.exists()) sourceDir.mkdirs(); File childDir = new File(sourceDir, "child"); if (!childDir.exists()) childDir.mkdirs(); File file = new File(sourceDir, "file"); createFile(file, 12345); try { FileUtils.copyDirectory(sourceDir, sourceDir); fail(); } catch( IOException ex) { } } Hope this could help.

          People

          • Assignee:
            Niall Pemberton
            Reporter:
            tora yamada
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development