vfs: remove items from cache when deleted #1860

Also fixes Error message when items have been deleted from the cache
(eg when Moved) when the cache reaper comes to delete them.
This commit is contained in:
Nick Craig-Wood 2017-11-27 19:48:25 +00:00
parent aab8051f50
commit d1b19f975d
2 changed files with 16 additions and 7 deletions

View File

@ -181,6 +181,17 @@ func (c *cache) close(name string) {
c.itemMu.Unlock()
}
// remove should be called if name is deleted
func (c *cache) remove(name string) {
osPath := filepath.Join(c.root, filepath.FromSlash(name))
err := os.Remove(osPath)
if err != nil && !os.IsNotExist(err) {
fs.Errorf(name, "Failed to remove from cache: %v", err)
} else {
fs.Debugf(name, "Removed from cache")
}
}
// cleanUp empties the cache of everything
func (c *cache) cleanUp() error {
return os.RemoveAll(c.root)
@ -219,13 +230,7 @@ func (c *cache) purgeOld(maxAge time.Duration) {
dt := item.atime.Sub(cutoff)
// fs.Debugf(name, "atime=%v cutoff=%v, dt=%v", item.atime, cutoff, dt)
if item.opens == 0 && dt < 0 {
osPath := filepath.Join(c.root, filepath.FromSlash(name))
err := os.Remove(osPath)
if err != nil {
fs.Errorf(name, "Failed to remove from cache: %v", err)
} else {
fs.Debugf(name, "Removed from cache")
}
c.remove(name)
// Remove the entry
delete(c.item, name)
}

View File

@ -314,6 +314,10 @@ func (f *File) Remove() error {
}
// Remove the item from the directory listing
f.d.delObject(f.Name())
// Remove the object from the cache
if f.d.vfs.Opt.CacheMode >= CacheModeMinimal {
f.d.vfs.cache.remove(f.Path())
}
return nil
}