The mv
operation is atomic when used within a file system (e.g. within a directory), so you should use that. The typical pattern is as Gunnar says,
tfilename = filename * ".tmp"
write(tfilename, newcontent)
mv(tfilename, filename, force=true)
This will ensure that filename
always contains valid content.