3 ms·
Check the update at the bottom: > Update: many people have read this post and suggested that, in the first file example, you should use the much simpler protoc
by timcederman 7y ago
Check the update at the bottom:
> Update: many people have read this post and suggested that, in the first file example, you should use the much simpler protocol of copying the file to modified to a temp file, modifying the temp file, and then renaming the temp file to overwrite the original file. In fact, that's probably the most common comment I've gotten on this post. If you think this solves the problem, I'm going to ask you to pause for five seconds and consider the problems this might have.
> The main problems this has are:
> rename isn't atomic on crash. POSIX says that rename is atomic, but this only applies to normal operation, not to crashes.
> even if the techinque worked, the performance is very poor
> how do you handle hardlinks?
> metadata can be lost; this can sometimes be preserved, under some filesystems, with ioctls, but now you have filesystem specific code just for the non-crash case
etc.
> The fact that so many people thought that this was a simple solution to the problem demonstrates that this problem is one that people are prone to underestimating, even they're explicitly warned that people tend to underestimate this problem!