On 09/06/2012 10:35 AM, Bernhard Voelker wrote: > Why can't 'sed -i' be made atomic for the user? > Today, it creates a temporary file for the output. > At the end, it calls rename(). What if it instead > rewinds the input and that temporary file and copies > it's content to the input file?
That's kind of what 'sort -o' does, and it also has race conditions. For example, in that last phase while it's copying the content to the input file, some other process might be reading the input file. There is no good general and portable atomic solution to this sort of problem, not in POSIX anyway. Practical implementations of utilities like 'sed' and 'sort' and 'shuf' all involve races of some sort or another.