It looks like the intent of this code is to leave behind the .bak file (old_file) on success. So why is it necessary to remove it on rename failure? Also, are you seeing undesirable behavior in practice? If so, under what circumstances?