You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Gary D. Gregory (Closed) (JIRA)" <ji...@apache.org> on 2011/10/10 17:49:30 UTC
[jira] [Closed] (IO-280) Dubious use of mkdirs() return code
[ https://issues.apache.org/jira/browse/IO-280?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Gary D. Gregory closed IO-280.
------------------------------
Closing, we released version 2.1.
> 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.
If you think it was sent incorrectly, please contact your JIRA administrators: https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira