commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Sebb (JIRA)" <j...@apache.org>
Subject [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 )
Date Sat, 13 Oct 2012 12:48:03 GMT

    [ 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

Mime
View raw message