From baf9ee5cf7643816e56aa3334052320835dd22ec Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Sat, 24 Feb 2018 11:22:26 +0000 Subject: [PATCH] vfs: update cached copy if we know it has changed before using it Before this change we would have to wait for the --vfs-cache-max-age to expire before getting an update. --- vfs/read_write.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/vfs/read_write.go b/vfs/read_write.go index 581a7e6b6..ade4375e1 100644 --- a/vfs/read_write.go +++ b/vfs/read_write.go @@ -109,6 +109,18 @@ func (fh *RWFileHandle) openPending(truncate bool) (err error) { cacheFileOpenFlags := fh.flags // if not truncating the file, need to read it first if fh.flags&os.O_TRUNC == 0 && !truncate { + // If the remote object exists AND its cached file exists locally AND there are no + // other handles with it open writers, then attempt to update it. + if fh.o != nil && fh.d.vfs.cache.opens(fh.remote) <= 1 { + cacheObj, err := fh.d.vfs.cache.f.NewObject(fh.remote) + if err == nil && cacheObj != nil { + cacheObj, err = copyObj(fh.d.vfs.cache.f, cacheObj, fh.remote, fh.o) + if err != nil { + return errors.Wrap(err, "open RW handle failed to update cached file") + } + } + } + // try to open a exising cache file fd, err = os.OpenFile(fh.osPath, cacheFileOpenFlags&^os.O_CREATE, 0600) if os.IsNotExist(err) {