Uploaded image for project: 'Commons IO'
  1. Commons IO
  2. IO-372

FileUtils.moveDirectory can produce misleading error message on failiure

    XMLWordPrintableJSON

    Details

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

      Description

      I am seeing the following error message when trying to move a directory:

      "Cannot move directory: /path/to/directory to a subdirectory of itself: /path/to/directory_renamed"

      This statement is iincorrect the destination path is not a sub directory of the source path.

      Looking at the source l.2752 the destination directory is being compared to the source directory without using a File.separator

      Here's a patch for the current trunk.

      Index: src/main/java/org/apache/commons/io/FileUtils.java
      ===================================================================
      --- src/main/java/org/apache/commons/io/FileUtils.java	(revision 1453996)
      +++ src/main/java/org/apache/commons/io/FileUtils.java	(working copy)
      @@ -2803,7 +2803,7 @@
               }
               final boolean rename = srcDir.renameTo(destDir);
               if (!rename) {
      -            if (destDir.getCanonicalPath().startsWith(srcDir.getCanonicalPath())) {
      +            if (destDir.getCanonicalPath().startsWith(srcDir.getCanonicalPath() + File.separator)) {
                       throw new IOException("Cannot move directory: "+srcDir+" to a subdirectory of itself: "+destDir);
                   }
                   copyDirectory( srcDir, destDir );
        

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              maxmil23 Max
            • Votes:
              1 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: