Refine atomic_write() and rename() with better error handling and reporting.

--HG--
extra : amend_source : 382c4bda1b2eeb95b504f4ee2b0af752e0418a1c