Skip to content

Commit b71e718

Browse files
committed
Migrate os calls to pathlib calls in hardlink util function
See discussion here: #5684 (comment)
1 parent 0e74605 commit b71e718

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

beets/util/__init__.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -578,12 +578,16 @@ def hardlink(path: bytes, dest: bytes, replace: bool = False):
578578
if samefile(path, dest):
579579
return
580580

581-
if os.path.exists(syspath(dest)) and not replace:
581+
# Dereference symlinks, expand "~", and convert relative paths to absolute
582+
origin_path = Path(os.fsdecode(path)).expanduser().resolve()
583+
dest_path = Path(os.fsdecode(dest)).expanduser().resolve()
584+
585+
if dest_path.exists() and not replace:
582586
raise FilesystemError("file exists", "rename", (path, dest))
583587
try:
584-
# This step dereferences any symlinks and converts to an absolute path
585-
resolved_origin = Path(syspath(path)).resolve()
586-
os.link(resolved_origin, syspath(dest))
588+
os.link(origin_path, dest_path)
589+
# When support for Python 3.9 is dropped, this can be replaced with:
590+
# dest_path.hardlink_to(origin_path)
587591
except NotImplementedError:
588592
raise FilesystemError(
589593
"OS does not support hard links.link",

0 commit comments

Comments
 (0)