From 7fc573db27f89e788692f90b6a9dae9b1d35d936 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Mon, 21 Aug 2023 18:14:41 +0100 Subject: [PATCH] serve sftp: fix hash calculations with --vfs-cache-mode full Before this change uploading files with rclone to: rclone serve sftp --vfs-cache-mode full Would return the error: command "md5sum XXX" failed with error: unexpected non file This patch detects that the file is still in the VFS cache and reads the MD5SUM from there rather from the remote. Fixes #7241 --- cmd/serve/sftp/connection.go | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/cmd/serve/sftp/connection.go b/cmd/serve/sftp/connection.go index 42564c151..91c83002c 100644 --- a/cmd/serve/sftp/connection.go +++ b/cmd/serve/sftp/connection.go @@ -123,11 +123,28 @@ func (c *conn) execCommand(ctx context.Context, out io.Writer, command string) ( } o, ok := node.DirEntry().(fs.ObjectInfo) if !ok { - return errors.New("unexpected non file") - } - hashSum, err = o.Hash(ctx, ht) - if err != nil { - return fmt.Errorf("hash failed: %w", err) + fs.Debugf(args, "File uploading - reading hash from VFS cache") + in, err := node.Open(os.O_RDONLY) + if err != nil { + return fmt.Errorf("hash vfs open failed: %w", err) + } + defer func() { + _ = in.Close() + }() + h, err := hash.NewMultiHasherTypes(hash.NewHashSet(ht)) + if err != nil { + return fmt.Errorf("hash vfs create multi-hasher failed: %w", err) + } + _, err = io.Copy(h, in) + if err != nil { + return fmt.Errorf("hash vfs copy failed: %w", err) + } + hashSum = h.Sums()[ht] + } else { + hashSum, err = o.Hash(ctx, ht) + if err != nil { + return fmt.Errorf("hash failed: %w", err) + } } } _, err = fmt.Fprintf(out, "%s %s\n", hashSum, args)