You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Sebb (JIRA)" <ji...@apache.org> on 2013/08/27 16:54:51 UTC

[jira] [Comment Edited] (JCI-67) Dubious use of mkdirs() return code

    [ https://issues.apache.org/jira/browse/JCI-67?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13085339#comment-13085339 ] 

Sebb edited comment on JCI-67 at 8/27/13 2:54 PM:
--------------------------------------------------

Safer would be the following, as it checks the path is actually a directory:

{code}
final File parent = file.getParentFile();
if (!parent.mkdirs() && !parent.isDirectory()) {
    throw new IOException("could not create" + parent);
}
{code}
                
      was (Author: sebb@apache.org):
    Safer would be the following, as it checks the path is actually a directory:

{code}
final File parent = file.getParentFile();
if (!parent.mkdirs() && !parent.isDirectory()) {
        throw new IOException("could not create" + parent);
    }
}
{code}
                  
> Dubious use of mkdirs() return code
> -----------------------------------
>
>                 Key: JCI-67
>                 URL: https://issues.apache.org/jira/browse/JCI-67
>             Project: Commons JCI
>          Issue Type: Bug
>            Reporter: Sebb
>            Priority: Minor
>             Fix For: 1.1
>
>
> FileRestoreStore.java uses mkdirs() as follows:
> {code}
> final File parent = file.getParentFile();
> if (!parent.exists()) {
>     if (!parent.mkdirs()) {
>         throw new IOException("could not create" + parent);
>     }
> }
> {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.
> Also, the initial exists() call is redundant, because that's what mkdirs() does anyway (in the RI implementation, at least).
> I suggest the following instead:
> {code}
> final File parent = file.getParentFile();
> if (!parent.mkdirs() && !parent.exists()) {
>         throw new IOException("could not create" + parent);
>     }
> }
> {code}
> If mkdirs() returns false, the code then checks to see if the directory exists, so the throws clause will only be invoked if the parent really cannot be created.
> The same code also appears in AbstractTestCase and FilesystemAlterationMonitorTestCase.

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira