vfs: don't open the file when using a RW handle for a null Seek

Background: cmd/mount/file.go Open() function does a Seek(0, 1) to see
if the file handle is seekable to set a FUSE hint.  Before this change
the file was downloaded before it needed to be which was inefficient
(and broke beta.rclone.org because HEAD requests caused downloads!).
This commit is contained in:
Nick Craig-Wood 2018-02-22 17:28:21 +00:00
parent 1383df4f58
commit 9252224d82
2 changed files with 16 additions and 1 deletions

View File

@ -401,6 +401,9 @@ func (fh *RWFileHandle) Seek(offset int64, whence int) (ret int64, err error) {
if fh.closed {
return 0, ECLOSED
}
if !fh.opened && offset == 0 && whence != 2 {
return 0, nil
}
if err = fh.openPending(false); err != nil {
return ret, err
}

View File

@ -110,10 +110,22 @@ func TestRWFileHandleSeek(t *testing.T) {
vfs, fh := rwHandleCreateReadOnly(t, r)
defer cleanup(t, r, vfs)
assert.Equal(t, fh.opened, false)
// Check null seeks don't open the file
n, err := fh.Seek(0, 0)
assert.NoError(t, err)
assert.Equal(t, int64(0), n)
assert.Equal(t, fh.opened, false)
n, err = fh.Seek(0, 1)
assert.NoError(t, err)
assert.Equal(t, int64(0), n)
assert.Equal(t, fh.opened, false)
assert.Equal(t, "0", rwReadString(t, fh, 1))
// 0 means relative to the origin of the file,
n, err := fh.Seek(5, 0)
n, err = fh.Seek(5, 0)
assert.NoError(t, err)
assert.Equal(t, int64(5), n)
assert.Equal(t, "5", rwReadString(t, fh, 1))