From c65ed26a7ecbd06d74b9dae083e104328b4496a7 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Fri, 3 Jul 2020 17:16:49 +0100 Subject: [PATCH] vfs: vfscache: Fix renaming of items while they are being uploaded Previous to the fix, if an item was being uploaded and it was renamed, the upload would fail with missing checksum errors. This change cancels any uploads in progress if the file is renamed. --- vfs/vfscache/item.go | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/vfs/vfscache/item.go b/vfs/vfscache/item.go index 796719eec..b7dbec428 100644 --- a/vfs/vfscache/item.go +++ b/vfs/vfscache/item.go @@ -1018,36 +1018,35 @@ func (item *Item) Sync() (err error) { // rename the item func (item *Item) rename(name string, newName string, newObj fs.Object) (err error) { - var downloaders *downloaders.Downloaders - // close downloader with mutex unlocked - defer func() { - if downloaders != nil { - _ = downloaders.Close(nil) - } - }() - item.mu.Lock() - defer item.mu.Unlock() // stop downloader - downloaders = item.downloaders + downloaders := item.downloaders item.downloaders = nil + // id for writeback cancel + id := item.writeBackID + // Set internal state item.name = newName item.o = newObj // Rename cache file if it exists err = rename(item.c.toOSPath(name), item.c.toOSPath(newName)) // No locking in Cache - if err != nil { - return err - } // Rename meta file if it exists - err = rename(item.c.toOSPathMeta(name), item.c.toOSPathMeta(newName)) // No locking in Cache - if err != nil { - return err + err2 := rename(item.c.toOSPathMeta(name), item.c.toOSPathMeta(newName)) // No locking in Cache + if err2 != nil { + err = err2 } - return nil + item.mu.Unlock() + + // close downloader and cancel writebacks with mutex unlocked + if downloaders != nil { + _ = downloaders.Close(nil) + } + item.c.writeback.Rename(id, newName) + + return err }