Migrate os calls to pathlib calls in hardlink util function

See discussion here: https://github.com/beetbox/beets/pull/5684#discussion_r2502432781
This commit is contained in:
Emi Katagiri-Simpson 2025-11-07 15:05:56 -05:00
parent 0e74605efd
commit b405d2fded
No known key found for this signature in database

View file

@ -578,12 +578,14 @@ def hardlink(path: bytes, dest: bytes, replace: bool = False):
if samefile(path, dest): if samefile(path, dest):
return 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)) raise FilesystemError("file exists", "rename", (path, dest))
try: try:
# This step dereferences any symlinks and converts to an absolute path dest_path.hardlink_to(origin_path)
resolved_origin = Path(syspath(path)).resolve()
os.link(resolved_origin, syspath(dest))
except NotImplementedError: except NotImplementedError:
raise FilesystemError( raise FilesystemError(
"OS does not support hard links.link", "OS does not support hard links.link",