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 2012/10/13 15:06:03 UTC

[jira] [Commented] (IO-340) The use of file.exists() is failure prone. Where file.exists() is checked, it should be replaced with the following dual check: exists = ( file.exists() || file.length()>0 )

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

Sebb commented on IO-340:
-------------------------

If file.length() really does refresh the NFS cache, then it should be more reliable to check that first, and then use exists() to catch empty files:

{code}file.length() > 0 || file.exists(){code}

However, as far as I can tell this would only be needed for NFS files and file.length() may be much more expensive than file.exists().

I'm not convinced that it is necessary to attempt to fix the behaviour; applications should be prepared for files to appear/disappear at any time.
                
> The use of file.exists() is failure prone. Where file.exists() is checked, it should be replaced with the following dual check:  exists = ( file.exists() || file.length()>0 )
> ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
>
>                 Key: IO-340
>                 URL: https://issues.apache.org/jira/browse/IO-340
>             Project: Commons IO
>          Issue Type: Bug
>          Components: Utilities
>    Affects Versions: 2.5, 3.x
>         Environment: All
>            Reporter: Feeling Groovy
>              Labels: File, exists, false
>             Fix For: 2.5, 3.x
>
>   Original Estimate: 24h
>  Remaining Estimate: 24h
>
> The use of file.exists() is failure prone. In many cases the jvm can report that a file does not exist even when it does. This is due to the caching in the operating system.
> Since file.exists() is prone to report false values, a second check can and should be made to file.length()>0.
> Where file.exists() is checked, it should be replaced with the following dual check:  
> A file more reliably exists if:  file.exists() || file.length()>0
> Then if the file does not exist, file.length will be 0.

--
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