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

Dubious use of mkdirs() return code

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • None
    • 2.1
    • None
    • None

    Description

      FileUtils.openOutputStream() has the following code:

      File parent = file.getParentFile();
      if (parent != null && parent.exists() == false) {
          if (parent.mkdirs() == false) {
              throw new IOException("File '" + file + "' could not be created");
          }
      }
      

      Now mkdirs() returns true only if the method actually created the directories; it's theoretically possible for the directory to be created in the window between the exists() and mkdirs() invocations. [Indeed the class actually checks for this in the forceMkdir() method]

      It would be safer to use:

      File parent = file.getParentFile();
      if (parent != null && !parent.mkdirs() && !parent.isDirectory()) {
          throw new IOException("Directory '" + parent + "' could not be created"); // note changed text
      }
      

      Similarly elsewhere in the class where mkdirs() is used.

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              sebb Sebb
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: