Add safe_unlink function to do the check-and-unlink work.