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

        Hide
        Yohan Gilbert added a comment -

        With this patch, you can move the directory to any directory, even to a directory which exists inside the source directory.

        Patch Logic:
        ------------
        1. If the source directory lies on the destination directory, a temporary source directory will be created (inside the system temporary folder).
        2. Deletes original source directory.
        3. Move from temporary source directory to the destination directory as per the usual way.

        Show
        Yohan Gilbert added a comment - With this patch, you can move the directory to any directory, even to a directory which exists inside the source directory. Patch Logic: ------------ 1. If the source directory lies on the destination directory, a temporary source directory will be created (inside the system temporary folder). 2. Deletes original source directory. 3. Move from temporary source directory to the destination directory as per the usual way.
        Hide
        Sebb added a comment -

        Thanks for the report.

        Added check for subdirectory if rename fails.

        Show
        Sebb added a comment - Thanks for the report. Added check for subdirectory if rename fails.
        Hide
        Gary Gregory added a comment -

        Version 2.2 has been released and addresses this issue.

        Show
        Gary Gregory added a comment - Version 2.2 has been released and addresses this issue.

          People

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

            Dates

            • Created:
              Updated:
              Resolved:

              Development