[BUGFIX] Invalid cross-device link workaround

This commit is contained in:
Jesse Bannon 2023-01-04 14:56:54 -08:00
parent e54bf7d467
commit 85d71f4d10
2 changed files with 19 additions and 5 deletions

View file

@ -1,4 +1,3 @@
import shutil
import subprocess import subprocess
import tempfile import tempfile
from typing import Dict from typing import Dict
@ -7,6 +6,7 @@ from typing import Optional
from ytdl_sub.utils.chapters import Chapters from ytdl_sub.utils.chapters import Chapters
from ytdl_sub.utils.exceptions import ValidationException from ytdl_sub.utils.exceptions import ValidationException
from ytdl_sub.utils.file_handler import FileHandler
from ytdl_sub.utils.logger import Logger from ytdl_sub.utils.logger import Logger
logger = Logger.get(name="ffmpeg") logger = Logger.get(name="ffmpeg")
@ -132,8 +132,7 @@ def set_ffmpeg_metadata_chapters(
output_file_path, output_file_path,
] ]
) )
FileHandler.move(output_file_path, file_path)
shutil.move(src=output_file_path, dst=file_path)
def add_ffmpeg_metadata_key_values(file_path: str, key_values: Dict[str, str]) -> None: def add_ffmpeg_metadata_key_values(file_path: str, key_values: Dict[str, str]) -> None:
@ -154,4 +153,4 @@ def add_ffmpeg_metadata_key_values(file_path: str, key_values: Dict[str, str]) -
ffmpeg_args.extend(["-codec", "copy", output_file_path]) ffmpeg_args.extend(["-codec", "copy", output_file_path])
FFMPEG.run(ffmpeg_args) FFMPEG.run(ffmpeg_args)
shutil.move(src=output_file_path, dst=file_path) FileHandler.move(output_file_path, file_path)

View file

@ -353,8 +353,23 @@ class FileHandler:
Source file Source file
dst_file_path dst_file_path
Destination file Destination file
Raises
------
OSError
Cross-device link workaround
""" """
try:
shutil.move(src=src_file_path, dst=dst_file_path) shutil.move(src=src_file_path, dst=dst_file_path)
except OSError as os_error_exc:
# Invalid cross-device link
# Can happen from using os.rename under the hood, which requires the two file on the
# same filesystem. Work around it by copying and deleting the file
if os_error_exc.errno == 18:
cls.copy(src_file_path, dst_file_path)
cls.delete(src_file_path)
else:
raise
@classmethod @classmethod
def delete(cls, file_path: Union[str, Path]): def delete(cls, file_path: Union[str, Path]):