From 7be262b18291c1cb5fcf8d0cd774f2dc73061f04 Mon Sep 17 00:00:00 2001 From: InfinityPacer <160988576+InfinityPacer@users.noreply.github.com> Date: Wed, 5 Jun 2024 22:24:17 +0800 Subject: [PATCH] =?UTF-8?q?fix=20#2265=20=E4=BC=98=E5=8C=96=E7=A1=AC?= =?UTF-8?q?=E9=93=BE=E6=8E=A5=E7=9A=84=E5=88=A4=E6=96=AD=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/chain/transfer.py | 6 +++--- app/utils/system.py | 27 ++++++++++++++++++++++----- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/app/chain/transfer.py b/app/chain/transfer.py index 29f64eb2..73f0765b 100644 --- a/app/chain/transfer.py +++ b/app/chain/transfer.py @@ -360,11 +360,11 @@ class TransferChain(ChainBase): # 硬链接检查 temp_transfer_type = transfer_type if transfer_type == "link": - if not SystemUtils.is_same_file(file_path, transferinfo.target_path): + if not SystemUtils.is_hardlink(file_path, transferinfo.target_path): logger.warn( - f"{file_path} 与 {transferinfo.target_path} 不是同一硬链接文件,请检查存储空间占用和整理耗时,确认是否为复制") + f"{file_path} 与 {transferinfo.target_path} 不是同一硬链接文件路径,请检查存储空间占用和整理耗时,确认是否为复制") self.messagehelper.put( - f"{file_path} 与 {transferinfo.target_path} 不是同一硬链接文件,疑似硬链接失败,请检查是否为复制", + f"{file_path} 与 {transferinfo.target_path} 不是同一硬链接文件路径,疑似硬链接失败,请检查是否为复制", title="硬链接失败", role="system") temp_transfer_type = "copy" diff --git a/app/utils/system.py b/app/utils/system.py index 8fd4b37a..65d941a6 100644 --- a/app/utils/system.py +++ b/app/utils/system.py @@ -125,7 +125,7 @@ class SystemUtils: tmp_path.unlink() tmp_path.hardlink_to(src) # 硬链接完成,移除 .mp 后缀 - tmp_path.rename(dest) + shutil.move(tmp_path, dest) return 0, "" except Exception as err: print(str(err)) @@ -468,11 +468,28 @@ class SystemUtils: return False, f"重启时发生错误:{str(err)}" @staticmethod - def is_same_file(src: Path, dest: Path) -> bool: - """判断是否为同一个文件""" - if not src.exists() or not dest.exists(): + def is_hardlink(src: Path, dest: Path) -> bool: + """判断是否为硬链接""" + try: + if not src.exists() or not dest.exists(): + return False + if src.is_file(): + # 如果是文件,直接比较文件 + return src.samefile(dest) + else: + for src_file in src.glob("**/*"): + if src_file.is_dir(): + continue + # 计算目标文件路径 + relative_path = src_file.relative_to(src) + target_file = dest.joinpath(relative_path) + # 检查是否是硬链接 + if not target_file.exists() or not src_file.samefile(target_file): + return False + return True + except (PermissionError, FileNotFoundError, ValueError, OSError) as e: + print(f"Error occurred: {e}") return False - return src.samefile(dest) @staticmethod def is_same_disk(src: Path, dest: Path) -> bool: