-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.go
307 lines (278 loc) · 5.62 KB
/
cache.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package filecache
import (
"errors"
"log"
"os"
"path"
"path/filepath"
"sync"
"time"
"github.com/anacrolix/missinggo"
"github.com/anacrolix/missinggo/pproffd"
)
const (
dirPerm = 0755
filePerm = 0644
)
type Cache struct {
mu sync.Mutex
capacity int64
filled int64
items *lruItems
paths map[string]ItemInfo
root string
}
type CacheInfo struct {
Capacity int64
Filled int64
NumItems int
}
type ItemInfo struct {
Accessed time.Time
Size int64
Path string
}
// Calls the function for every item known to the cache. The ItemInfo should
// not be modified.
func (me *Cache) WalkItems(cb func(ItemInfo)) {
me.mu.Lock()
defer me.mu.Unlock()
for e := me.items.Front(); e != nil; e = e.Next() {
cb(e.Value().(ItemInfo))
}
}
func (me *Cache) Info() (ret CacheInfo) {
me.mu.Lock()
defer me.mu.Unlock()
ret.Capacity = me.capacity
ret.Filled = me.filled
ret.NumItems = len(me.paths)
return
}
// Setting a negative capacity means unlimited.
func (me *Cache) SetCapacity(capacity int64) {
me.mu.Lock()
defer me.mu.Unlock()
me.capacity = capacity
}
func NewCache(root string) (ret *Cache, err error) {
if !filepath.IsAbs(root) {
err = errors.New("root is not an absolute filepath")
return
}
ret = &Cache{
root: root,
capacity: -1, // unlimited
}
ret.mu.Lock()
go func() {
defer ret.mu.Unlock()
ret.rescan()
}()
return
}
// An empty return path is an error.
func sanitizePath(p string) (ret string) {
if p == "" {
return
}
ret = path.Clean("/" + p)
if ret[0] == '/' {
ret = ret[1:]
}
return
}
// Leaf is a descendent of root.
func pruneEmptyDirs(root string, leaf string) (err error) {
rootInfo, err := os.Stat(root)
if err != nil {
return
}
for {
var leafInfo os.FileInfo
leafInfo, err = os.Stat(leaf)
if os.IsNotExist(err) {
goto parent
}
if err != nil {
return
}
if !leafInfo.IsDir() {
return
}
if os.SameFile(rootInfo, leafInfo) {
return
}
if os.Remove(leaf) != nil {
return
}
parent:
leaf = filepath.Dir(leaf)
}
}
func (me *Cache) Remove(path string) (err error) {
path = sanitizePath(path)
me.mu.Lock()
defer me.mu.Unlock()
err = me.remove(path)
return
}
var (
ErrBadPath = errors.New("bad path")
ErrIsDir = errors.New("is directory")
)
func (me *Cache) StatFile(path string) (os.FileInfo, error) {
return os.Stat(me.realpath(sanitizePath(path)))
}
func (me *Cache) OpenFile(path string, flag int) (ret *File, err error) {
path = sanitizePath(path)
if path == "" {
err = ErrIsDir
return
}
f, err := os.OpenFile(me.realpath(path), flag, filePerm)
if flag&os.O_CREATE != 0 && os.IsNotExist(err) {
os.MkdirAll(me.root, dirPerm)
os.MkdirAll(filepath.Dir(me.realpath(path)), dirPerm)
f, err = os.OpenFile(me.realpath(path), flag, filePerm)
if err != nil {
me.pruneEmptyDirs(path)
}
}
if err != nil {
return
}
ret = &File{
c: me,
path: path,
f: pproffd.WrapOSFile(f),
}
me.mu.Lock()
go func() {
defer me.mu.Unlock()
me.statItem(path, time.Now())
}()
return
}
func (me *Cache) rescan() {
me.filled = 0
me.items = newLRUItems()
me.paths = make(map[string]ItemInfo)
err := filepath.Walk(me.root, func(path string, info os.FileInfo, err error) error {
if os.IsNotExist(err) {
return nil
}
if err != nil {
return err
}
if info.IsDir() {
return nil
}
path, err = filepath.Rel(me.root, path)
if err != nil {
log.Print(err)
return nil
}
me.statItem(path, time.Time{})
return nil
})
if err != nil {
panic(err)
}
}
func (me *Cache) insertItem(i ItemInfo) {
me.items.Insert(i)
}
func (me *Cache) removeInfo(path string) (ret ItemInfo, ok bool) {
ret, ok = me.paths[path]
if !ok {
return
}
if !me.items.Remove(ret) {
panic(ret)
}
me.filled -= ret.Size
delete(me.paths, path)
return
}
// Triggers the item for path to be updated. If access is non-zero, set the
// item's access time to that value, otherwise deduce it appropriately.
func (me *Cache) statItem(path string, access time.Time) {
info, ok := me.removeInfo(path)
fi, err := os.Stat(me.realpath(path))
if os.IsNotExist(err) {
return
}
if err != nil {
panic(err)
}
if !ok {
info.Path = path
}
if !access.IsZero() {
info.Accessed = access
}
if info.Accessed.IsZero() {
info.Accessed = missinggo.FileInfoAccessTime(fi)
}
info.Size = fi.Size()
me.filled += info.Size
me.insertItem(info)
me.paths[path] = info
}
func (me *Cache) realpath(path string) string {
return filepath.Join(me.root, filepath.FromSlash(sanitizePath(path)))
}
func (me *Cache) TrimToCapacity() {
me.mu.Lock()
defer me.mu.Unlock()
me.trimToCapacity()
}
func (me *Cache) pruneEmptyDirs(path string) {
pruneEmptyDirs(me.root, me.realpath(path))
}
func (me *Cache) remove(path string) (err error) {
err = os.Remove(me.realpath(path))
if os.IsNotExist(err) {
err = nil
}
me.pruneEmptyDirs(path)
me.removeInfo(path)
return
}
func (me *Cache) trimToCapacity() {
if me.capacity < 0 {
return
}
for me.filled > me.capacity {
item := me.items.LRU()
me.remove(item.Path)
}
}
func (me *Cache) pathInfo(p string) ItemInfo {
return me.paths[p]
}
func (me *Cache) Rename(from, to string) (err error) {
_from := sanitizePath(from)
_to := sanitizePath(to)
me.mu.Lock()
defer me.mu.Unlock()
err = os.MkdirAll(filepath.Dir(me.realpath(_to)), dirPerm)
if err != nil {
return
}
err = os.Rename(me.realpath(_from), me.realpath(_to))
if err != nil {
return
}
me.removeInfo(_from)
me.pruneEmptyDirs(_from)
me.statItem(_to, time.Now())
return
}
func (me *Cache) Stat(path string) (os.FileInfo, error) {
return os.Stat(me.realpath(path))
}
func (me *Cache) AsFileStore() missinggo.FileStore {
return fileStore{me}
}