Commit 81b7354c authored by Iustin Pop's avatar Iustin Pop
Browse files

Small optimisation in utils.WriteFile



Currently we always try to remove the new file, even if the rename
succeeded. This patch tracks the existence of the new file and doesn't
try to remove it if we managed to rename it.
Signed-off-by: default avatarIustin Pop <iustin@google.com>
Reviewed-by: default avatarGuido Trotter <ultrotter@google.com>
parent dd36d829
......@@ -1284,6 +1284,7 @@ def WriteFile(file_name, fn=None, data=None,
dir_name, base_name = os.path.split(file_name)
fd, new_name = tempfile.mkstemp('.new', base_name, dir_name)
do_remove = True
# here we need to make sure we remove the temp file, if any error
# leaves it in place
try:
......@@ -1304,13 +1305,15 @@ def WriteFile(file_name, fn=None, data=None,
os.utime(new_name, (atime, mtime))
if not dry_run:
os.rename(new_name, file_name)
do_remove = False
finally:
if close:
os.close(fd)
result = None
else:
result = fd
RemoveFile(new_name)
if do_remove:
RemoveFile(new_name)
return result
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment