From 96cf2f7cf9cbbe716d024f266a6cc79ef66b92cd Mon Sep 17 00:00:00 2001 From: Suyunjing <69945917+Suyunmeng@users.noreply.github.com> Date: Mon, 23 Jun 2025 19:48:17 +0800 Subject: [PATCH] fix(fs):Repair file loss caused by special reasons when moving files (#321) * fix(move):Fix file move logic * fix(move):fixed move logic --- internal/fs/move.go | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/internal/fs/move.go b/internal/fs/move.go index 2f404335..ec629641 100644 --- a/internal/fs/move.go +++ b/internal/fs/move.go @@ -116,7 +116,7 @@ func moveBetween2Storages(t *MoveTask, srcStorage, dstStorage driver.Driver, src func moveFileBetween2Storages(tsk *MoveTask, srcStorage, dstStorage driver.Driver, srcFilePath, dstDirPath string) error { tsk.Status = "copying file to destination" - + copyTask := &CopyTask{ TaskExtension: task.TaskExtension{ Creator: tsk.GetCreator(), @@ -128,34 +128,46 @@ func moveFileBetween2Storages(tsk *MoveTask, srcStorage, dstStorage driver.Drive SrcStorageMp: srcStorage.GetStorage().MountPath, DstStorageMp: dstStorage.GetStorage().MountPath, } - - copyTask.SetCtx(tsk.Ctx()) - err := copyBetween2Storages(copyTask, srcStorage, dstStorage, srcFilePath, dstDirPath) if err != nil { - // Check if this is an upload-related error and provide a clearer message if errors.Is(err, errs.UploadNotSupported) { return errors.WithMessagef(err, "destination storage [%s] does not support file uploads", dstStorage.GetStorage().MountPath) } return errors.WithMessagef(err, "failed to copy [%s] to destination storage [%s]", srcFilePath, dstStorage.GetStorage().MountPath) } - + tsk.SetProgress(50) - + tsk.Status = "verifying file in destination" + + // check target files + dstFilePath := stdpath.Join(dstDirPath, stdpath.Base(srcFilePath)) + const maxRetries = 3 + const retryInterval = time.Second + var checkErr error + for i := 0; i < maxRetries; i++ { + _, checkErr = op.Get(tsk.Ctx(), dstStorage, dstFilePath) + if checkErr == nil { + break + } + time.Sleep(retryInterval) + } + if checkErr != nil { + return errors.WithMessagef(checkErr, "file not found in destination [%s] after copy", dstFilePath) + } + tsk.Status = "deleting source file" err = op.Remove(tsk.Ctx(), srcStorage, srcFilePath) if err != nil { return errors.WithMessagef(err, "failed to delete src [%s] file from storage [%s] after successful copy", srcFilePath, srcStorage.GetStorage().MountPath) } - + tsk.SetProgress(100) tsk.Status = "completed" return nil } - func _move(ctx context.Context, srcObjPath, dstDirPath string, lazyCache ...bool) (task.TaskExtensionInfo, error) { srcStorage, srcObjActualPath, err := op.GetStorageAndActualPath(srcObjPath) if err != nil {