Skip to content

Commit

Permalink
Use shutil.move() to move files. (#5123)
Browse files Browse the repository at this point in the history
Fixes #4622.

I don't think this changes other behavior of the function, but it fixes
the problem with the moved files being created with the wrong
permissions for me.
  • Loading branch information
snejus authored Dec 26, 2024
2 parents bcf516b + 34af24b commit 2277e2a
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions beets/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,10 +510,7 @@ def copy(path: bytes, dest: bytes, replace: bool = False):
def move(path: bytes, dest: bytes, replace: bool = False):
"""Rename a file. `dest` may not be a directory. If `dest` already
exists, raises an OSError unless `replace` is True. Has no effect if
`path` is the same as `dest`. If the paths are on different
filesystems (or the rename otherwise fails), a copy is attempted
instead, in which case metadata will *not* be preserved. Paths are
translated to system paths.
`path` is the same as `dest`. Paths are translated to system paths.
"""
if os.path.isdir(syspath(path)):
raise FilesystemError("source is directory", "move", (path, dest))
Expand Down Expand Up @@ -548,6 +545,14 @@ def move(path: bytes, dest: bytes, replace: bool = False):
finally:
tmp.close()

try:
# Copy file metadata
shutil.copystat(syspath(path), tmp.name)
except OSError:
# Ignore errors because it doesn't matter too much. We may be on a
# filesystem that doesn't support this.
pass

# Move the copied file into place.
tmp_filename = tmp.name
try:
Expand Down

0 comments on commit 2277e2a

Please sign in to comment.