[Issue286] Add support for atomic file renaming (Backport on 1.6.x)

See #286. This is a backport on the 1.6.x branch.