]> git.lizzy.rs Git - go-fscache.git/commitdiff
Add (*CacheDir).Chtime and (*CacheDir).ChtimeNoLock
authorDiogo Franco (Kovensky) <diogomfranco@gmail.com>
Wed, 17 Jul 2013 03:11:45 +0000 (00:11 -0300)
committerDiogo Franco (Kovensky) <diogomfranco@gmail.com>
Wed, 17 Jul 2013 03:11:45 +0000 (00:11 -0300)
ChtimeNoLock is exported since it might be useful if the user already
called (*CacheDir).Lock.

cachestat.go

index a7148299346d5b99e2486b5fd67a4ce33b49f174..f726c5961cbabb113d021579fbaa9ba574325d21 100755 (executable)
@@ -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...)
+}