Commons IO
  1. Commons IO
  2. IO-300

FileUtils.moveDirectoryToDirectory removes source directory if destination is a subdirectory

    Details

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

      Description

      Since moveDirectoryToDirectory performs a copy then delete, if you specify a target directory that is a subdirectory of the source, everything under the source directory is deleted.

      Steps to recreate:
      File dest = new File("/tmp/dir1/dir2");
      File src = new File("/tmp/dir1/");

      dest.mkdirs();

      System.out.println(src.exists());

      FileUtils.moveDirectoryToDirectory(src, dest, false);

      System.out.println(src.exists());

      Output:
      true
      false

      If you try the same thing with a move command on Linux, you receive: "mv: cannot move `dir1/' to a subdirectory of itself, `dir1/dir2/dir1'"

      Maybe throw an exception if dest.getCanonicalPath().startsWith(src.getCanonicalPath())

      1. IO-300.patch
        2 kB
        Yohan Gilbert

        Activity

        dennis lucero created issue -
        Yohan Gilbert made changes -
        Field Original Value New Value
        Attachment IO-300.patch [ 12514371 ]
        Yohan Gilbert made changes -
        Attachment IO-300.patch [ 12514371 ]
        Yohan Gilbert made changes -
        Comment [ I have added a check before deleting the source directory; so that the it only proceeds with the deletion if the source directory not on the target path. ]
        Yohan Gilbert made changes -
        Attachment IO-300.patch [ 12514496 ]
        Sebb made changes -
        Summary FileUtils.moveDirectoryToDirectory removing source directory FileUtils.moveDirectoryToDirectory removes source directory if destination is a subdirectory
        Sebb made changes -
        Status Open [ 1 ] Resolved [ 5 ]
        Fix Version/s 2.2 [ 12318448 ]
        Resolution Fixed [ 1 ]
        Gary Gregory made changes -
        Status Resolved [ 5 ] Closed [ 6 ]

          People

          • Assignee:
            Unassigned
            Reporter:
            dennis lucero
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development