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

Dubious use of mkdirs() return code

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 2.1
    • Component/s: None
    • Labels:
      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

              • Assignee:
                Unassigned
                Reporter:
                sebb@apache.org Sebb
              • Votes:
                0 Vote for this issue
                Watchers:
                0 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: