Skip to content

Commit

Permalink
Improve write_file atomically.
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiocaccamo committed Dec 14, 2023
1 parent 69f3c15 commit 11fbc5e
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions fsutil/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1325,18 +1325,22 @@ def _write_file_atomic(
if append:
content = read_file(path, encoding=encoding) + content
dirpath, _ = split_filepath(path)
with tempfile.NamedTemporaryFile(
mode=mode,
dir=dirpath,
delete=False,
encoding=encoding,
) as file:
file.write(content)
file.flush()
os.fsync(file.fileno())
temp_path = file.name
os.replace(temp_path, path)
remove_file(temp_path)
try:
with tempfile.NamedTemporaryFile(
mode=mode,
dir=dirpath,
delete=True,
encoding=encoding,
) as file:
file.write(content)
file.flush()
os.fsync(file.fileno())
os.replace(file.name, path)
except FileNotFoundError:
# success - the NamedTemporaryFile has not been able
# to remove the temp file on __exit__ because the temp file
# has replaced atomically the file at path.
pass


def _write_file_non_atomic(
Expand Down

0 comments on commit 11fbc5e

Please sign in to comment.