sftp: fix RangeOption support in Open #1825

This commit is contained in:
Fabian Möller 2018-01-23 21:21:19 +01:00 committed by Nick Craig-Wood
parent a3dd2c691e
commit 9520992a54

View File

@ -20,6 +20,7 @@ import (
"github.com/ncw/rclone/fs/config/obscure" "github.com/ncw/rclone/fs/config/obscure"
"github.com/ncw/rclone/fs/fshttp" "github.com/ncw/rclone/fs/fshttp"
"github.com/ncw/rclone/fs/hash" "github.com/ncw/rclone/fs/hash"
"github.com/ncw/rclone/lib/readers"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/pkg/sftp" "github.com/pkg/sftp"
"github.com/xanzy/ssh-agent" "github.com/xanzy/ssh-agent"
@ -854,12 +855,13 @@ func (file *ObjectReader) Close() (err error) {
// Open a remote sftp file object for reading. Seek is supported // Open a remote sftp file object for reading. Seek is supported
func (o *Object) Open(options ...fs.OpenOption) (in io.ReadCloser, err error) { func (o *Object) Open(options ...fs.OpenOption) (in io.ReadCloser, err error) {
var offset int64 var offset, limit int64
offset = 0
for _, option := range options { for _, option := range options {
switch x := option.(type) { switch x := option.(type) {
case *fs.SeekOption: case *fs.SeekOption:
offset = x.Offset offset, limit = x.Offset, 0
case *fs.RangeOption:
offset, limit = x.Decode(o.Size())
default: default:
if option.Mandatory() { if option.Mandatory() {
fs.Logf(o, "Unsupported mandatory option: %v", option) fs.Logf(o, "Unsupported mandatory option: %v", option)
@ -881,10 +883,10 @@ func (o *Object) Open(options ...fs.OpenOption) (in io.ReadCloser, err error) {
return nil, errors.Wrap(err, "Open Seek failed") return nil, errors.Wrap(err, "Open Seek failed")
} }
} }
in = &ObjectReader{ in = readers.NewLimitedReadCloser(&ObjectReader{
object: o, object: o,
sftpFile: sftpFile, sftpFile: sftpFile,
} }, limit)
return in, nil return in, nil
} }