From 4eaafe41671648c1d6b1e205e3e7370e0703a5d1 Mon Sep 17 00:00:00 2001 From: lilydjwg Date: Fri, 27 Oct 2023 17:13:02 +0800 Subject: [PATCH] fixup! feat(core): resolve symlinks on file write --- nvchecker/core.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/nvchecker/core.py b/nvchecker/core.py index 3c22f8b..fdb7110 100644 --- a/nvchecker/core.py +++ b/nvchecker/core.py @@ -114,7 +114,8 @@ def safe_overwrite(file: Path, data: Union[bytes, str], *, method: str = 'write', mode: str = 'w', encoding: Optional[str] = None) -> None: # FIXME: directory has no read perm # FIXME: hard links - tmpname = str(file) + '.tmp' + resolved_path = file.resolve() + tmpname = str(resolved_path) + '.tmp' # if not using "with", write can fail without exception with open(tmpname, mode, encoding=encoding) as f: getattr(f, method)(data) @@ -122,7 +123,7 @@ def safe_overwrite(file: Path, data: Union[bytes, str], *, f.flush() os.fsync(f.fileno()) # if the above write failed (because disk is full etc), the old data should be kept - os.rename(tmpname, file.resolve()) + os.rename(tmpname, resolved_path) def read_verfile(file: Path) -> VersData: try: