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
This commit is contained in:
Nick Craig-Wood 2023-08-21 18:14:41 +01:00
parent af95616122
commit 7fc573db27

View File

@ -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)