Skip to content

Commit

Permalink
Preserve file mode
Browse files Browse the repository at this point in the history
Open the temporary file with the same permissions as set on the original
file to ensure permissions are preserved when we overwrite via the
atomic rename.
  • Loading branch information
benjamb committed Sep 24, 2022
1 parent 0ae7b9c commit 2f39a55
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion library/yedit.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,9 @@ def _write(filename, contents):

tmp_filename = filename + '.yedit'

with open(tmp_filename, 'w') as yfd:
original_mode = os.stat(filename).st_mode
fd = os.open(tmp_filename, flags=(os.O_WRONLY | os.O_CREAT | os.O_TRUNC), mode=original_mode)
with open(fd, 'w') as yfd:
fcntl.flock(yfd, fcntl.LOCK_EX | fcntl.LOCK_NB)
yfd.write(contents)
yfd.flush() # flush internal buffers
Expand Down

0 comments on commit 2f39a55

Please sign in to comment.