Skip to content

Commit

Permalink
operations: fix files moved by rclone move not being counted as trans…
Browse files Browse the repository at this point in the history
…fers

Before this change we were only counting moves as checks. This means
that when using `rclone move` the `Transfers` stat did not count up
like it should do.

This changes introduces a new primitive operations.MoveTransfers which
counts moves as Transfers for use where that is appropriate, such as
rclone move/moveto. Otherwise moves are counted as checks and their
bytes are not accounted.

See: rclone#7183
See: https://forum.rclone.org/t/stats-one-line-date-broken-in-1-64-0-and-later/43263/
  • Loading branch information
ncw committed Jan 7, 2024
1 parent d392f9f commit fbdf71a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
26 changes: 24 additions & 2 deletions fs/operations/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,9 +332,29 @@ func SameObject(src, dst fs.Object) bool {
//
// It returns the destination object if possible. Note that this may
// be nil.
//
// This is accounted as a check.
func Move(ctx context.Context, fdst fs.Fs, dst fs.Object, remote string, src fs.Object) (newDst fs.Object, err error) {
return move(ctx, fdst, dst, remote, src, false)
}

// MoveTransfer moves src object to dst or fdst if nil. If dst is nil
// then it uses remote as the name of the new object.
//
// This is identical to Move but is accounted as a transfer.
func MoveTransfer(ctx context.Context, fdst fs.Fs, dst fs.Object, remote string, src fs.Object) (newDst fs.Object, err error) {
return move(ctx, fdst, dst, remote, src, true)
}

// move - see Move for help
func move(ctx context.Context, fdst fs.Fs, dst fs.Object, remote string, src fs.Object, isTransfer bool) (newDst fs.Object, err error) {
ci := fs.GetConfig(ctx)
tr := accounting.Stats(ctx).NewCheckingTransfer(src, "moving")
var tr *accounting.Transfer
if isTransfer {
tr = accounting.Stats(ctx).NewTransfer(src)
} else {
tr = accounting.Stats(ctx).NewCheckingTransfer(src, "moving")
}
defer func() {
if err == nil {
accounting.Stats(ctx).Renames(1)
Expand Down Expand Up @@ -1695,7 +1715,7 @@ func moveOrCopyFile(ctx context.Context, fdst fs.Fs, fsrc fs.Fs, dstFileName str
}

// Choose operations
Op := Move
Op := MoveTransfer
if cp {
Op = Copy
}
Expand Down Expand Up @@ -1797,6 +1817,8 @@ func moveOrCopyFile(ctx context.Context, fdst fs.Fs, fsrc fs.Fs, dstFileName str
}

// MoveFile moves a single file possibly to a new name
//
// This is treated as a transfer.
func MoveFile(ctx context.Context, fdst fs.Fs, fsrc fs.Fs, dstFileName string, srcFileName string) (err error) {
return moveOrCopyFile(ctx, fdst, fsrc, dstFileName, srcFileName, false)
}
Expand Down
2 changes: 1 addition & 1 deletion fs/sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ func (s *syncCopyMove) pairCopyOrMove(ctx context.Context, in *pipe, fdst fs.Fs,
dst := pair.Dst
if s.DoMove {
if src != dst {
_, err = operations.Move(ctx, fdst, dst, src.Remote(), src)
_, err = operations.MoveTransfer(ctx, fdst, dst, src.Remote(), src)
} else {
// src == dst signals delete the src
err = operations.DeleteFile(ctx, src)
Expand Down

0 comments on commit fbdf71a

Please sign in to comment.