From: Diogo Franco (Kovensky) Date: Wed, 17 Jul 2013 03:11:45 +0000 (-0300) Subject: Add (*CacheDir).Chtime and (*CacheDir).ChtimeNoLock X-Git-Tag: v0.0.1~2 X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=9c42224433ac2b0cf65af518a66c4c747fef964a;p=go-fscache.git Add (*CacheDir).Chtime and (*CacheDir).ChtimeNoLock ChtimeNoLock is exported since it might be useful if the user already called (*CacheDir).Lock. --- diff --git a/cachestat.go b/cachestat.go index a714829..f726c59 100755 --- a/cachestat.go +++ b/cachestat.go @@ -37,7 +37,7 @@ func (cd *CacheDir) Touch(key ...CacheKey) (err error) { return } - if err = os.Chtimes(cd.cachePath(key...), time.Now(), time.Now()); err == nil { + if err = cd.ChtimeNoLock(time.Now(), key...); err == nil { return } @@ -55,3 +55,20 @@ func (cd *CacheDir) Touch(key ...CacheKey) (err error) { } return fh.Close() } + +// Same as Chtime, but does not try to acquire a file lock on the file +// before. Only call this if you already hold a lock to the file. +func (cd *CacheDir) ChtimeNoLock(t time.Time, key ...CacheKey) (err error) { + return os.Chtimes(cd.cachePath(key...), time.Now(), t) +} + +// Sets the mtime of the file backing the given key to the specified time. +func (cd *CacheDir) Chtime(t time.Time, key ...CacheKey) (err error) { + lock, err := cd.Lock(key...) + if err != nil { + return + } + defer lock.Unlock() + + return cd.ChtimeNoLock(t, key...) +}