Commons IO
  1. Commons IO
  2. IO-280

Dubious use of mkdirs() return code

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Minor 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.

        Issue Links

          Activity

          Hide
          Gary Gregory added a comment -

          Closing, we released version 2.1.

          Show
          Gary Gregory added a comment - Closing, we released version 2.1.

            People

            • Assignee:
              Unassigned
              Reporter:
              Sebb
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development