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.
This commit is contained in:
Nick Craig-Wood 2020-08-19 16:56:22 +01:00
parent 9a6fcd035b
commit 6082096f7e

View File

@ -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())
}