From 27c6392b66afaa72ea14d0827b759baea83e146d Mon Sep 17 00:00:00 2001 From: InfinityPacer <160988576+InfinityPacer@users.noreply.github.com> Date: Tue, 4 Jun 2024 21:54:37 +0800 Subject: [PATCH] =?UTF-8?q?fix=20#667=20=E4=BC=98=E5=8C=96=E5=8E=86?= =?UTF-8?q?=E5=8F=B2=E5=85=BC=E5=AE=B9=E6=9E=81=E7=A9=BA=E9=97=B4=E7=A1=AC?= =?UTF-8?q?=E9=93=BE=E6=8E=A5=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 | 16 ++++++++++++---- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/app/chain/transfer.py b/app/chain/transfer.py index 93bfb2bd..29f64eb2 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_disk(file_path, transferinfo.target_path): + if not SystemUtils.is_same_file(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 d4ab0d46..8fd4b37a 100644 --- a/app/utils/system.py +++ b/app/utils/system.py @@ -118,13 +118,14 @@ class SystemUtils: 硬链接 """ try: - # link到当前目录并改名 - tmp_path = src.parent / (dest.name + ".mp") + # 准备目标路径,增加后缀 .mp + tmp_path = dest.with_suffix(dest.suffix + ".mp") + # 检查目标路径是否已存在,如果存在则先unlink if tmp_path.exists(): tmp_path.unlink() tmp_path.hardlink_to(src) - # 移动到目标目录 - shutil.move(tmp_path, dest) + # 硬链接完成,移除 .mp 后缀 + tmp_path.rename(dest) return 0, "" except Exception as err: print(str(err)) @@ -466,6 +467,13 @@ class SystemUtils: print(str(err)) return False, f"重启时发生错误:{str(err)}" + @staticmethod + def is_same_file(src: Path, dest: Path) -> bool: + """判断是否为同一个文件""" + if not src.exists() or not dest.exists(): + return False + return src.samefile(dest) + @staticmethod def is_same_disk(src: Path, dest: Path) -> bool: """