forked from hanwen/go-fuse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachingfs.go
162 lines (137 loc) · 3.47 KB
/
cachingfs.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
package unionfs
import (
"fmt"
"github.com/hanwen/go-fuse/fuse"
"log"
"os"
"strings"
)
var _ = fmt.Println
const _XATTRSEP = "@XATTR@"
type attrResponse struct {
*os.FileInfo
fuse.Status
}
type xattrResponse struct {
data []byte
fuse.Status
}
type dirResponse struct {
entries []fuse.DirEntry
fuse.Status
}
type linkResponse struct {
linkContent string
fuse.Status
}
// Caches filesystem metadata.
type CachingFileSystem struct {
fuse.FileSystem
attributes *TimedCache
dirs *TimedCache
links *TimedCache
xattr *TimedCache
}
func readDir(fs fuse.FileSystem, name string) *dirResponse {
origStream, code := fs.OpenDir(name, nil)
r := &dirResponse{nil, code}
if code != fuse.OK {
return r
}
for {
d, ok := <-origStream
if !ok {
break
}
r.entries = append(r.entries, d)
}
return r
}
func getAttr(fs fuse.FileSystem, name string) *attrResponse {
a, code := fs.GetAttr(name, nil)
return &attrResponse{
FileInfo: a,
Status: code,
}
}
func getXAttr(fs fuse.FileSystem, nameAttr string) *xattrResponse {
ns := strings.SplitN(nameAttr, _XATTRSEP, 2)
a, code := fs.GetXAttr(ns[0], ns[1], nil)
return &xattrResponse{
data: a,
Status: code,
}
}
func readLink(fs fuse.FileSystem, name string) *linkResponse {
a, code := fs.Readlink(name, nil)
return &linkResponse{
linkContent: a,
Status: code,
}
}
func NewCachingFileSystem(fs fuse.FileSystem, ttlNs int64) *CachingFileSystem {
c := new(CachingFileSystem)
c.FileSystem = fs
c.attributes = NewTimedCache(func(n string) (interface{}, bool) {
a := getAttr(fs, n)
return a, a.Ok()
}, ttlNs)
c.dirs = NewTimedCache(func(n string) (interface{}, bool) {
d := readDir(fs, n)
return d, d.Ok()
}, ttlNs)
c.links = NewTimedCache(func(n string) (interface{}, bool) {
l := readLink(fs, n)
return l, l.Ok()
}, ttlNs)
c.xattr = NewTimedCache(func(n string) (interface{}, bool) {
l := getXAttr(fs, n)
return l, l.Ok()
}, ttlNs)
return c
}
func (me *CachingFileSystem) DropCache() {
for _, c := range []*TimedCache{me.attributes, me.dirs, me.links, me.xattr} {
c.DropAll(nil)
}
}
func (me *CachingFileSystem) GetAttr(name string, context *fuse.Context) (*os.FileInfo, fuse.Status) {
if name == _DROP_CACHE {
return &os.FileInfo{
Mode: fuse.S_IFREG | 0777,
}, fuse.OK
}
r := me.attributes.Get(name).(*attrResponse)
return r.FileInfo, r.Status
}
func (me *CachingFileSystem) GetXAttr(name string, attr string, context *fuse.Context) ([]byte, fuse.Status) {
key := name + _XATTRSEP + attr
r := me.xattr.Get(key).(*xattrResponse)
return r.data, r.Status
}
func (me *CachingFileSystem) Readlink(name string, context *fuse.Context) (string, fuse.Status) {
r := me.links.Get(name).(*linkResponse)
return r.linkContent, r.Status
}
func (me *CachingFileSystem) OpenDir(name string, context *fuse.Context) (stream chan fuse.DirEntry, status fuse.Status) {
r := me.dirs.Get(name).(*dirResponse)
if r.Status.Ok() {
stream = make(chan fuse.DirEntry, len(r.entries))
for _, d := range r.entries {
stream <- d
}
close(stream)
return stream, r.Status
}
return nil, r.Status
}
func (me *CachingFileSystem) String() string {
return fmt.Sprintf("CachingFileSystem(%v)", me.FileSystem)
}
func (me *CachingFileSystem) Open(name string, flags uint32, context *fuse.Context) (f fuse.File, status fuse.Status) {
if flags&fuse.O_ANYWRITE != 0 && name == _DROP_CACHE {
log.Println("Dropping cache for", me)
me.DropCache()
}
return me.FileSystem.Open(name, flags, context)
}