From 2f39a553ed0849f9c932f87cc971690cc00a0753 Mon Sep 17 00:00:00 2001 From: Ben Brown Date: Sat, 24 Sep 2022 12:52:18 +0100 Subject: [PATCH] Preserve file mode 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. --- library/yedit.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/library/yedit.py b/library/yedit.py index 1b9a2fc..ce7219f 100644 --- a/library/yedit.py +++ b/library/yedit.py @@ -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