-
Notifications
You must be signed in to change notification settings - Fork 17.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
os: Add comment on proper way to check if a file exists #8456
Labels
Comments
The proper way to check if a file exists is to not check if a file exists as that information is unavoidably racy. os.Stat is not about file existence but about file metadata. Even though the error possibly returned may mean the file didn't existed when os.Stat was executing, it does not mean the file does not exists now, when os.Stat completed. Use os.O_EXCL when creating a file which must not exists already and the err from os.Open when a file must exist before opening. |
I don't understand your comment that "the information is unavoidably racy". Programs can be structured such that the same goroutine that may create the file is also the same one that checks if the file exists. Or, one can use channels to pass "ownership" of the filename. Files don't magically appear on computers, and in many cases the file won't be created outside the program. |
A process is typically not executing completely isolated. A user can type a shell command/script which can create a file or delete a file at any time. Or any other process can do the same. Sometimes it's the same program, still running in its previous invocation (which one possibly forgot to kill), etc. |
There is no proper way to test if a file exists. As 0xjnml says, any such test is normally racy, because if you make any decision based on whether the file exists, the file may have been created or deleted by some entirely independent process by the time you execute that decision. What you need are operations like "create this file, but only if it does not exist" (which is done by using os.OpenFile with O_CREATE|O_EXCL). Status changed to Invalid. |
You don't think it's worth putting a comment somewhere? This has come up a number of times, many of whom think using os.Stat is the right way to do it. http://stackoverflow.com/questions/12518876/how-to-check-if-a-file-exists-in-go https://golang.org/issue/1312 http://stackoverflow.com/questions/10510691/how-to-check-whether-a-file-or-directory-denoted-by-a-path-exists-in-golang https://groups.google.com/forum/#!topic/golang-nuts/TiskIM23rMI http://golang-examples.tumblr.com/post/46579246576/check-if-file-exists https://www.socketloop.com/tutorials/golang-check-if-a-file-exist-or-not |
Where would you suggest putting a comment about this? To my mind it really has nothing to do with Go, it has to do with the nature of multi-process machines. Writing a program that assumes that it can test whether a file exists can be a security issue, but that too has nothing to do with Go. It's fine to use os.Stat for the very rare cases where it's OK to simply test whether a file exists. That doesn't mean that we should document it as such. |
This issue was closed.
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
The text was updated successfully, but these errors were encountered: