sync: use operations.DirMove instead of sync.MoveDir for --fix-case - #7591

This should be more efficient for the purposes of --fix-case, as operations.DirMove
accepts `srcRemote` and `dstRemote` arguments, while sync.MoveDir does not.

This also factors the two-step-move logic to operations.DirMoveCaseInsensitive, so
that it is reusable by other commands.
This commit is contained in:
nielash 2024-02-09 10:37:14 -05:00
parent dfe76570a1
commit 137f7f62fb
2 changed files with 15 additions and 19 deletions

View File

@ -2288,6 +2288,17 @@ func DirMove(ctx context.Context, f fs.Fs, srcRemote, dstRemote string) (err err
return nil return nil
} }
// DirMoveCaseInsensitive does DirMove in two steps (to temp name, then real name)
// which is necessary for some case-insensitive backends
func DirMoveCaseInsensitive(ctx context.Context, f fs.Fs, srcRemote, dstRemote string) (err error) {
tmpDstRemote := dstRemote + "-rclone-move-" + random.String(8)
err = DirMove(ctx, f, srcRemote, tmpDstRemote)
if err != nil {
return err
}
return DirMove(ctx, f, tmpDstRemote, dstRemote)
}
// FsInfo provides information about a remote // FsInfo provides information about a remote
type FsInfo struct { type FsInfo struct {
// Name of the remote (as passed into NewFs) // Name of the remote (as passed into NewFs)

View File

@ -6,7 +6,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"path" "path"
"path/filepath"
"sort" "sort"
"strings" "strings"
"sync" "sync"
@ -19,7 +18,6 @@ import (
"github.com/rclone/rclone/fs/hash" "github.com/rclone/rclone/fs/hash"
"github.com/rclone/rclone/fs/march" "github.com/rclone/rclone/fs/march"
"github.com/rclone/rclone/fs/operations" "github.com/rclone/rclone/fs/operations"
"github.com/rclone/rclone/lib/random"
) )
// ErrorMaxDurationReached defines error when transfer duration is reached // ErrorMaxDurationReached defines error when transfer duration is reached
@ -1151,25 +1149,12 @@ func (s *syncCopyMove) Match(ctx context.Context, dst, src fs.DirEntry) (recurse
if s.ci.FixCase && !s.ci.Immutable && src.Remote() != dst.Remote() { if s.ci.FixCase && !s.ci.Immutable && src.Remote() != dst.Remote() {
// Fix case for case insensitive filesystems // Fix case for case insensitive filesystems
// Fix each dir before recursing into subdirs and files // Fix each dir before recursing into subdirs and files
oldDirFs, err := fs.NewFs(s.ctx, filepath.Join(fs.ConfigStringFull(s.fdst), dst.Remote())) err := operations.DirMoveCaseInsensitive(s.ctx, s.fdst, dst.Remote(), src.Remote())
s.processError(err) if err != nil {
newDirPath := filepath.Join(fs.ConfigStringFull(s.fdst), filepath.Dir(dst.Remote()), filepath.Base(src.Remote())) fs.Errorf(dst, "Error while attempting to rename to %s: %v", src.Remote(), err)
newDirFs, err := fs.NewFs(s.ctx, newDirPath)
s.processError(err)
// Create random name to temporarily move dir to
tmpDirName := newDirPath + "-rclone-move-" + random.String(8)
tmpDirFs, err := fs.NewFs(s.ctx, tmpDirName)
s.processError(err)
if err = MoveDir(s.ctx, tmpDirFs, oldDirFs, s.deleteEmptySrcDirs, s.copyEmptySrcDirs); err != nil {
fs.Errorf(dst, "Error while attempting to move dir to temporary location %s: %v", tmpDirName, err)
s.processError(err) s.processError(err)
} else { } else {
if err = MoveDir(s.ctx, newDirFs, tmpDirFs, s.deleteEmptySrcDirs, s.copyEmptySrcDirs); err != nil { fs.Infof(dst, "Fixed case by renaming to: %s", src.Remote())
fs.Errorf(dst, "Error while attempting to rename to %s: %v", src.Remote(), err)
s.processError(err)
} else {
fs.Infof(dst, "Fixed case by renaming to: %s", src.Remote())
}
} }
} }