From 040a152f1f3ec484d9ea68ea3d0c932d21397f8b Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 16 Jul 2021 08:44:25 +0530 Subject: [PATCH] Preserve stat attributes when modifying rc files atomically --- kitty/shell_integration.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/kitty/shell_integration.py b/kitty/shell_integration.py index bdae178d8..49850c031 100644 --- a/kitty/shell_integration.py +++ b/kitty/shell_integration.py @@ -4,6 +4,8 @@ import os +import shutil +from tempfile import mkstemp from typing import Optional, Union from .constants import shell_integration_dir @@ -20,10 +22,12 @@ posix_template = ''' def atomic_write(path: str, data: Union[str, bytes]) -> None: - tmp = path + '_ksi_tmp' - with open(tmp, 'w' + ('b' if isinstance(data, bytes) else '')) as f: + mode = 'w' + ('b' if isinstance(data, bytes) else '') + fd, tpath = mkstemp(dir=os.path.dirname(path), text=isinstance(data, str)) + shutil.copystat(path, tpath) + with open(fd, mode) as f: f.write(data) - os.rename(tmp, path) + os.rename(tpath, path) def setup_integration(shell_name: str, rc_path: str, template: str = posix_template) -> None: