From 238f26cc90365ac19af66e1f5a7b672ffd2e2236 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Tue, 14 Apr 2020 18:03:45 +0100 Subject: [PATCH] vfs: stop reading File members from outside file.go This also fixes locking for ReadFileHandle and WriteFileHandle accessing File members --- vfs/file.go | 7 +++++++ vfs/read.go | 10 +++++----- vfs/write.go | 6 +++--- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/vfs/file.go b/vfs/file.go index 7159b8965..c330f33db 100644 --- a/vfs/file.go +++ b/vfs/file.go @@ -607,6 +607,13 @@ func (f *File) VFS() *VFS { return f.d.vfs } +// Fs returns the underlying Fs for the file +func (f *File) Fs() fs.Fs { + f.mu.RLock() + defer f.mu.RUnlock() + return f.d.f +} + // Open a file according to the flags provided // // O_RDONLY open the file read-only. diff --git a/vfs/read.go b/vfs/read.go index 526467616..6324593dc 100644 --- a/vfs/read.go +++ b/vfs/read.go @@ -47,7 +47,7 @@ func newReadFileHandle(f *File) (*ReadFileHandle, error) { var mhash *hash.MultiHasher var err error o := f.getObject() - if !f.d.vfs.Opt.NoChecksum { + if !f.VFS().Opt.NoChecksum { hashes := hash.NewHashSet(o.Fs().Hashes().GetOne()) // just pick one hash mhash, err = hash.NewMultiHasherTypes(hashes) if err != nil { @@ -57,7 +57,7 @@ func newReadFileHandle(f *File) (*ReadFileHandle, error) { fh := &ReadFileHandle{ remote: o.Remote(), - noSeek: f.d.vfs.Opt.NoSeek, + noSeek: f.VFS().Opt.NoSeek, file: f, hash: mhash, size: nonNegative(o.Size()), @@ -74,7 +74,7 @@ func (fh *ReadFileHandle) openPending() (err error) { return nil } o := fh.file.getObject() - r, err := chunkedreader.New(context.TODO(), o, int64(fh.file.d.vfs.Opt.ChunkSize), int64(fh.file.d.vfs.Opt.ChunkSizeLimit)).Open() + r, err := chunkedreader.New(context.TODO(), o, int64(fh.file.VFS().Opt.ChunkSize), int64(fh.file.VFS().Opt.ChunkSizeLimit)).Open() if err != nil { return err } @@ -147,7 +147,7 @@ func (fh *ReadFileHandle) seek(offset int64, reopen bool) (err error) { } // re-open with a seek o := fh.file.getObject() - r = chunkedreader.New(context.TODO(), o, int64(fh.file.d.vfs.Opt.ChunkSize), int64(fh.file.d.vfs.Opt.ChunkSizeLimit)) + r = chunkedreader.New(context.TODO(), o, int64(fh.file.VFS().Opt.ChunkSize), int64(fh.file.VFS().Opt.ChunkSizeLimit)) _, err := r.Seek(offset, 0) if err != nil { fs.Debugf(fh.remote, "ReadFileHandle.Read seek failed: %v", err) @@ -235,7 +235,7 @@ func (fh *ReadFileHandle) readAt(p []byte, off int64) (n int, err error) { // The default time here was made by finding the // smallest when mounting a local backend that didn't // cause seeks. - maxWait := fh.file.d.vfs.Opt.ReadWait + maxWait := fh.file.VFS().Opt.ReadWait timeout := time.NewTimer(maxWait) done := make(chan struct{}) abort := int32(0) diff --git a/vfs/write.go b/vfs/write.go index e45752360..b7401bbe9 100644 --- a/vfs/write.go +++ b/vfs/write.go @@ -69,7 +69,7 @@ func (fh *WriteFileHandle) openPending() (err error) { pipeReader, fh.pipeWriter = io.Pipe() go func() { // NB Rcat deals with Stats.Transferring etc - o, err := operations.Rcat(context.TODO(), fh.file.d.f, fh.remote, pipeReader, time.Now()) + o, err := operations.Rcat(context.TODO(), fh.file.Fs(), fh.remote, pipeReader, time.Now()) if err != nil { fs.Errorf(fh.remote, "WriteFileHandle.New Rcat failed: %v", err) } @@ -80,7 +80,7 @@ func (fh *WriteFileHandle) openPending() (err error) { }() fh.file.setSize(0) fh.truncated = true - fh.file.d.addObject(fh.file) // make sure the directory has this object in it now + fh.file.Dir().addObject(fh.file) // make sure the directory has this object in it now fh.opened = true return nil } @@ -132,7 +132,7 @@ func (fh *WriteFileHandle) writeAt(p []byte, off int64) (n int, err error) { } if fh.offset != off { // Set a background timer so we don't wait forever - maxWait := fh.file.d.vfs.Opt.WriteWait + maxWait := fh.file.VFS().Opt.WriteWait timeout := time.NewTimer(maxWait) done := make(chan struct{}) abort := int32(0)