Dubious use of mkdirs() return code
-----------------------------------

                 Key: IO-280
                 URL: https://issues.apache.org/jira/browse/IO-280
             Project: Commons IO
          Issue Type: Bug
            Reporter: Sebb
            Priority: Minor


FileUtils.openOutputStream() has the following code:

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

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:

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

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

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

Reply via email to