Commons IO
  1. Commons IO
  2. IO-276

"FileUtils#deleteDirectoryOnExit(File)" does not work

    Details

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

      Commons IO 1.4

      Description

      I called this method, but the target directory was not deleted.

      The source of this method is as follows.
      ----------
      private static void deleteDirectoryOnExit(File directory) throws IOException {
      if (!directory.exists())

      { return; }

      cleanDirectoryOnExit(directory);
      directory.deleteOnExit();
      }
      ----------
      In other words, this method calls "java.io.File#deleteOnExit" in order of "grandchildren -> children -> parent" for files of the target directory hierarchy.

      There is following mention in javadoc of "java.io.File#deleteOnExit".
      ----------
      Files (or directories) are deleted in the reverse order that they are registered.
      ----------

      Therefore, I think the calling order of "java.io.File#deleteOnExit" should be reverse.

      How will be it?

        Activity

        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.
        Hide
        Sebb added a comment -

        BTW FileUtils#cleanDirectoryOnExit is also a private method

        Show
        Sebb added a comment - BTW FileUtils#cleanDirectoryOnExit is also a private method
        Hide
        Sebb added a comment -

        The test code needs to clean up as it goes along; so changing it to delete files on exit will likely cause problems.

        Show
        Sebb added a comment - The test code needs to clean up as it goes along; so changing it to delete files on exit will likely cause problems.
        Hide
        Ron Kuris added a comment -

        I forgot to mention that my patch also actually should fix the reported bug by removing the parent before removing its children.

        Show
        Ron Kuris added a comment - I forgot to mention that my patch also actually should fix the reported bug by removing the parent before removing its children.
        Hide
        Ron Kuris added a comment -

        deleteDirectoryOnExit is a private method. However, you probably meant FileUtils#cleanDirectoryOnExit, which indeed does have this problem.

        This is difficult to write a junit test for this. However, the test code never even calls this method. I modified the cleanup code to call it; hopefully someone will discover stray files if the cleanDirectoryOnExit method fails.

        Patch attached.

        Show
        Ron Kuris added a comment - deleteDirectoryOnExit is a private method. However, you probably meant FileUtils#cleanDirectoryOnExit, which indeed does have this problem. This is difficult to write a junit test for this. However, the test code never even calls this method. I modified the cleanup code to call it; hopefully someone will discover stray files if the cleanDirectoryOnExit method fails. Patch attached.

          People

          • Assignee:
            Unassigned
            Reporter:
            nkami
          • Votes:
            2 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development