From 9252224d827e01b8fac61d18d78e04ca48a381ee Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Thu, 22 Feb 2018 17:28:21 +0000 Subject: [PATCH] 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!). --- vfs/read_write.go | 3 +++ vfs/read_write_test.go | 14 +++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/vfs/read_write.go b/vfs/read_write.go index cf8631395..20bde2294 100644 --- a/vfs/read_write.go +++ b/vfs/read_write.go @@ -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 } diff --git a/vfs/read_write_test.go b/vfs/read_write_test.go index 06af52f59..78acc332e 100644 --- a/vfs/read_write_test.go +++ b/vfs/read_write_test.go @@ -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))