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 14:48:03 UTC

[jira] [Comment Edited] (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=13428418#comment-13428418 ] 

Sebb edited comment on IO-340 at 10/13/12 12:47 PM:
----------------------------------------------------

Thanks.

The bug [1] says that the problem is with the stat() call, which may return ESTALE.
Surely stat() is also used to provide the file length? So how can this help?
If it's just a question of retrying, maybe (file.exists() || file.exists()) would be a better solution?
The StackOverflow link suggests listing the directory might flush the cache, so another possibility might be to use File.list(), though that might be expensive

However, even for non-NFS file systems, a file might disappear or appear at any time after its existence is checked, so subsequent code needs to be able to handle such conditions.

I'm not 100% convinced that it is necessary to perform the additional check.
Nor am I convinced that using file.length() is the best method, given that it does not distinguish empty files.

[1] http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5003595
                
      was (Author: sebb@apache.org):
    Thanks.

The bug says that the problem is with the stat() call, which may return ESTALE.
Surely stat() is also used to provide the file length? So how can this help?
If it's just a question of retrying, maybe (file.exists() || file.exists()) would be a better solution?
The StackOverflow link suggests listing the directory might flush the cache, so another possibility might be to use File.list(), though that might be expensive

However, even for non-NFS file systems, a file might disappear or appear at any time after its existence is checked, so subsequent code needs to be able to handle such conditions.

I'm not 100% convinced that it is necessary to perform the additional check.
Nor am I convinced that using file.length() is the best method, given that it does not distinguish empty files.
                  
> 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