-
Notifications
You must be signed in to change notification settings - Fork 19
/
file.go
188 lines (156 loc) · 3.71 KB
/
file.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
package main
import (
"errors"
"sync"
"syscall"
"bazil.org/fuse"
"bazil.org/fuse/fs"
"bazil.org/fuse/fuseutil"
"github.com/boltdb/bolt"
"golang.org/x/net/context"
)
type File struct {
dir *Dir
name []byte
mu sync.Mutex
// number of write-capable handles currently open
writers uint
// only valid if writers > 0
data []byte
}
var _ = fs.Node(&File{})
var _ = fs.Handle(&File{})
// load calls fn inside a View with the contents of the file. Caller
// must make a copy of the data if needed, because once we're out of
// the transaction, bolt might reuse the db page.
func (f *File) load(fn func([]byte)) error {
err := f.dir.fs.db.View(func(tx *bolt.Tx) error {
b := f.dir.bucket(tx)
if b == nil {
return errors.New("bucket no longer exists")
}
v := b.Get(f.name)
if v == nil {
return fuse.ESTALE
}
fn(v)
return nil
})
return err
}
func (f *File) Attr(ctx context.Context, a *fuse.Attr) error {
f.mu.Lock()
defer f.mu.Unlock()
a.Mode = 0644
a.Size = uint64(len(f.data))
if f.writers == 0 {
// not in memory, fetch correct size.
// Attr can't fail, so ignore errors
_ = f.load(func(b []byte) { a.Size = uint64(len(b)) })
}
return nil
}
var _ = fs.NodeOpener(&File{})
func (f *File) Open(ctx context.Context, req *fuse.OpenRequest, resp *fuse.OpenResponse) (fs.Handle, error) {
if req.Flags.IsReadOnly() {
// we don't need to track read-only handles
return f, nil
}
f.mu.Lock()
defer f.mu.Unlock()
if f.writers == 0 {
// load data
fn := func(b []byte) {
f.data = append([]byte(nil), b...)
}
if err := f.load(fn); err != nil {
return nil, err
}
}
f.writers++
return f, nil
}
var _ = fs.HandleReleaser(&File{})
func (f *File) Release(ctx context.Context, req *fuse.ReleaseRequest) error {
if req.Flags.IsReadOnly() {
// we don't need to track read-only handles
return nil
}
f.mu.Lock()
defer f.mu.Unlock()
f.writers--
if f.writers == 0 {
f.data = nil
}
return nil
}
var _ = fs.HandleReader(&File{})
func (f *File) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadResponse) error {
f.mu.Lock()
defer f.mu.Unlock()
fn := func(b []byte) {
fuseutil.HandleRead(req, resp, b)
}
if f.writers == 0 {
f.load(fn)
} else {
fn(f.data)
}
return nil
}
var _ = fs.HandleWriter(&File{})
const maxInt = int(^uint(0) >> 1)
func (f *File) Write(ctx context.Context, req *fuse.WriteRequest, resp *fuse.WriteResponse) error {
f.mu.Lock()
defer f.mu.Unlock()
// expand the buffer if necessary
newLen := req.Offset + int64(len(req.Data))
if newLen > int64(maxInt) {
return fuse.Errno(syscall.EFBIG)
}
if newLen := int(newLen); newLen > len(f.data) {
f.data = append(f.data, make([]byte, newLen-len(f.data))...)
}
n := copy(f.data[req.Offset:], req.Data)
resp.Size = n
return nil
}
var _ = fs.HandleFlusher(&File{})
func (f *File) Flush(ctx context.Context, req *fuse.FlushRequest) error {
f.mu.Lock()
defer f.mu.Unlock()
if f.writers == 0 {
// Read-only handles also get flushes. Make sure we don't
// overwrite valid file contents with a nil buffer.
return nil
}
err := f.dir.fs.db.Update(func(tx *bolt.Tx) error {
b := f.dir.bucket(tx)
if b == nil {
return fuse.ESTALE
}
return b.Put(f.name, f.data)
})
if err != nil {
return err
}
return nil
}
var _ = fs.NodeSetattrer(&File{})
func (f *File) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *fuse.SetattrResponse) error {
f.mu.Lock()
defer f.mu.Unlock()
if req.Valid.Size() {
if req.Size > uint64(maxInt) {
return fuse.Errno(syscall.EFBIG)
}
newLen := int(req.Size)
switch {
case newLen > len(f.data):
f.data = append(f.data, make([]byte, newLen-len(f.data))...)
case newLen < len(f.data):
f.data = f.data[:newLen]
}
}
return nil
}