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.
This commit is contained in:
Nick Craig-Wood 2020-07-03 17:16:49 +01:00
parent df5dbaf49b
commit c65ed26a7e

View File

@ -1018,36 +1018,35 @@ func (item *Item) Sync() (err error) {
// rename the item // rename the item
func (item *Item) rename(name string, newName string, newObj fs.Object) (err error) { 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() item.mu.Lock()
defer item.mu.Unlock()
// stop downloader // stop downloader
downloaders = item.downloaders downloaders := item.downloaders
item.downloaders = nil item.downloaders = nil
// id for writeback cancel
id := item.writeBackID
// Set internal state // Set internal state
item.name = newName item.name = newName
item.o = newObj item.o = newObj
// Rename cache file if it exists // Rename cache file if it exists
err = rename(item.c.toOSPath(name), item.c.toOSPath(newName)) // No locking in Cache 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 // Rename meta file if it exists
err = rename(item.c.toOSPathMeta(name), item.c.toOSPathMeta(newName)) // No locking in Cache err2 := rename(item.c.toOSPathMeta(name), item.c.toOSPathMeta(newName)) // No locking in Cache
if err != nil { if err2 != nil {
return err 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
} }