diff --git a/vfs/cache.go b/vfs/cache.go index 55946c6dc..4e3e3eff6 100644 --- a/vfs/cache.go +++ b/vfs/cache.go @@ -145,16 +145,20 @@ func (c *cache) mkdir(name string) (string, error) { // _get gets name from the cache or creates a new one // +// It returns the item and found as to whether this item was found in +// the cache (or just created). +// // name should be a remote path not an osPath // // must be called with itemMu held -func (c *cache) _get(isFile bool, name string) *cacheItem { - item := c.item[name] - if item == nil { +func (c *cache) _get(isFile bool, name string) (item *cacheItem, found bool) { + item = c.item[name] + found = item != nil + if !found { item = newCacheItem(isFile) c.item[name] = item } - return item + return item, found } // get gets name from the cache or creates a new one @@ -162,7 +166,7 @@ func (c *cache) _get(isFile bool, name string) *cacheItem { // name should be a remote path not an osPath func (c *cache) get(name string) *cacheItem { c.itemMu.Lock() - item := c._get(true, name) + item, _ := c._get(true, name) c.itemMu.Unlock() return item } @@ -173,8 +177,8 @@ func (c *cache) get(name string) *cacheItem { // name should be a remote path not an osPath func (c *cache) updateTime(name string, when time.Time) { c.itemMu.Lock() - item := c._get(true, name) - if when.Sub(item.atime) > 0 { + item, found := c._get(true, name) + if !found || when.Sub(item.atime) > 0 { fs.Debugf(name, "updateTime: setting atime to %v", when) item.atime = when } @@ -186,7 +190,7 @@ func (c *cache) updateTime(name string, when time.Time) { // name should be a remote path not an osPath func (c *cache) _open(isFile bool, name string) { for { - item := c._get(isFile, name) + item, _ := c._get(isFile, name) item.opens++ item.atime = time.Now() if name == "" { @@ -228,7 +232,7 @@ func (c *cache) cacheDir(name string) { // _close marks name as closed - must be called with the lock held func (c *cache) _close(isFile bool, name string) { for { - item := c._get(isFile, name) + item, _ := c._get(isFile, name) item.opens-- item.atime = time.Now() if item.opens < 0 { diff --git a/vfs/cache_test.go b/vfs/cache_test.go index f7eb62d90..6ef959862 100644 --- a/vfs/cache_test.go +++ b/vfs/cache_test.go @@ -3,7 +3,6 @@ package vfs import ( "fmt" "io/ioutil" - "log" "os" "path/filepath" "sort" @@ -123,7 +122,6 @@ name="potato" isFile=true opens=1`, itemAsString(c)) atime := times.Get(fi).AccessTime() // updateAtimes - log.Printf("updateAtimes") item = c.get("potato") item.atime = time.Now().Add(-24 * time.Hour) err = c.updateAtimes() @@ -133,6 +131,17 @@ name="potato" isFile=true opens=1`, itemAsString(c)) item = c.get("potato") assert.Equal(t, atime, item.atime) + // updateAtimes - not in the cache + oldItem := item + delete(c.item, "potato") // remove from cache + err = c.updateAtimes() + require.NoError(t, err) + assert.Equal(t, `name="" isFile=false opens=1 +name="potato" isFile=true opens=0`, itemAsString(c)) + item = c.get("potato") + assert.Equal(t, atime, item.atime) + c.item["potato"] = oldItem // restore to cache + // try purging with file open c.purgeOld(10 * time.Second) _, err = os.Stat(p)