From 6082096f7e635d535dcb0f3c4cb8645a8672cad3 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Wed, 19 Aug 2020 16:56:22 +0100 Subject: [PATCH] vfs: check file exists in cache before renaming/setmodtime/deleting Before this change we didn't check the file exists before renaming it, setting its modification time or deleting it. If the file isn't in the cache we don't need to do the action since it has been done on the actual object, so these errors were producing unecessary log messages. This change checks to see if the file exists first before doing those actions. --- vfs/file.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/vfs/file.go b/vfs/file.go index 466b44a2f..db2bc8a27 100644 --- a/vfs/file.go +++ b/vfs/file.go @@ -211,7 +211,7 @@ func (f *File) rename(ctx context.Context, destDir *Dir, newName string) error { } } // Rename in the cache - if d.vfs.cache != nil { + if d.vfs.cache != nil && d.vfs.cache.Exists(oldPath) { if err := d.vfs.cache.Rename(oldPath, newPath, newObject); err != nil { fs.Infof(f.Path(), "File.Rename failed in Cache: %v", err) } @@ -366,7 +366,6 @@ func (f *File) _applyPendingModTime() error { if f.pendingModTime.IsZero() { return nil } - defer func() { f.pendingModTime = time.Time{} }() if f.o == nil { @@ -377,16 +376,16 @@ func (f *File) _applyPendingModTime() error { err := f.o.SetModTime(context.TODO(), f.pendingModTime) switch err { case nil: - fs.Debugf(f._path(), "File._applyPendingModTime OK") + fs.Debugf(f.o, "Applied pending mod time %v OK", f.pendingModTime) case fs.ErrorCantSetModTime, fs.ErrorCantSetModTimeWithoutDelete: // do nothing, in order to not break "touch somefile" if it exists already default: - fs.Debugf(f._path(), "File._applyPendingModTime error: %v", err) + fs.Errorf(f.o, "Failed to apply pending mod time %v: %v", f.pendingModTime, err) return err } // set the time of the file in the cache - if f.d.vfs.cache != nil { + if f.d.vfs.cache != nil && f.d.vfs.cache.Exists(f._path()) { f.d.vfs.cache.SetModTime(f._path(), f.pendingModTime) } @@ -537,7 +536,7 @@ func (f *File) Remove() (err error) { // Remove the object from the cache wasWriting := false - if d.vfs.cache != nil { + if d.vfs.cache != nil && d.vfs.cache.Exists(f.Path()) { wasWriting = d.vfs.cache.Remove(f.Path()) }