-
Notifications
You must be signed in to change notification settings - Fork 1
/
cachestat.go
executable file
·74 lines (64 loc) · 1.51 KB
/
cachestat.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package fscache
import (
"os"
"time"
)
// Calls os.Stat() on the file or folder that backs the given key.
func (cd *CacheDir) Stat(key ...CacheKey) (stat os.FileInfo, err error) {
lock, err := cd.Lock(key...)
if err != nil {
return
}
defer lock.Unlock()
fh, err := cd.Open(key...)
if err != nil {
return
}
defer fh.Close()
return fh.Stat()
}
// Updates the mtime of the file backing the given key.
//
// Creates an empty file if it doesn't exist.
func (cd *CacheDir) Touch(key ...CacheKey) (err error) {
lock, err := cd.Lock(key...)
switch {
case err == nil:
// AOK
defer lock.Unlock()
case os.IsNotExist(err):
// new file
default:
return
}
if err = cd.ChtimeNoLock(time.Now(), key...); err == nil {
return
}
fh, err := cd.OpenFlags(os.O_APPEND|os.O_CREATE, key...)
switch {
case err == nil:
// AOK
case os.IsNotExist(err): // directory path not created yet
fh, err = cd.Create(key...)
if err != nil {
return
}
default:
return
}
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...)
}