From b405d2fded8a7bb0591c0cd6ff7a9fd1c8c9bc18 Mon Sep 17 00:00:00 2001 From: Emi Katagiri-Simpson Date: Fri, 7 Nov 2025 15:05:56 -0500 Subject: [PATCH] Migrate `os` calls to `pathlib` calls in hardlink util function See discussion here: https://github.com/beetbox/beets/pull/5684#discussion_r2502432781 --- beets/util/__init__.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/beets/util/__init__.py b/beets/util/__init__.py index b053e9c73..c95c2e523 100644 --- a/beets/util/__init__.py +++ b/beets/util/__init__.py @@ -578,12 +578,14 @@ def hardlink(path: bytes, dest: bytes, replace: bool = False): if samefile(path, dest): return - if os.path.exists(syspath(dest)) and not replace: + # Dereference symlinks, expand "~", and convert relative paths to absolute + origin_path = Path(os.fsdecode(path)).expanduser().resolve() + dest_path = Path(os.fsdecode(dest)).expanduser().resolve() + + if dest_path.exists() and not replace: raise FilesystemError("file exists", "rename", (path, dest)) try: - # This step dereferences any symlinks and converts to an absolute path - resolved_origin = Path(syspath(path)).resolve() - os.link(resolved_origin, syspath(dest)) + dest_path.hardlink_to(origin_path) except NotImplementedError: raise FilesystemError( "OS does not support hard links.link",