forked from someonegg/pathfs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_fs.go
More file actions
292 lines (252 loc) · 7.3 KB
/
test_fs.go
File metadata and controls
292 lines (252 loc) · 7.3 KB
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
package pathfs
import (
"github.com/hanwen/go-fuse/v2/fuse"
"io"
"os"
"path/filepath"
"syscall"
"time"
)
type testFileSystem struct {
defaultFileSystem
root string
xattrs map[string]map[string][]byte
}
// NewTestFileSystem construct A FileSystem
// that forward most of the requests to native filesystem
// and process Extended attributes requests in case of some filesystems that don't support xattr operations
func NewTestFileSystem(root string) FileSystem {
if root[0] != '/' {
panic("not a absolute path")
}
s := syscall.Stat_t{}
err := syscall.Stat(root, &s)
if err != nil {
panic(err)
}
return &testFileSystem{
root: root,
xattrs: make(map[string]map[string][]byte),
}
}
func (fs *testFileSystem) absPath(relPath string) string {
return filepath.Join(fs.root, relPath)
}
func (fs *testFileSystem) GetAttr(ctx *Context, path string, uFh uint32, out *fuse.Attr) fuse.Status {
st := syscall.Stat_t{}
err := syscall.Lstat(fs.absPath(path), &st)
if err != nil {
return fuse.ToStatus(err)
}
out.FromStat(&st)
return fuse.OK
}
func (fs *testFileSystem) Access(ctx *Context, path string, mask uint32) fuse.Status {
return fuse.ToStatus(syscall.Access(fs.absPath(path), mask))
}
func (fs *testFileSystem) Mknod(ctx *Context, path string, mode uint32, dev uint32) fuse.Status {
return fuse.ToStatus(syscall.Mknod(fs.absPath(path), mode, int(dev)))
}
func (fs *testFileSystem) Mkdir(ctx *Context, path string, mode uint32) (code fuse.Status) {
return fuse.ToStatus(syscall.Mkdir(fs.absPath(path), mode))
}
func (fs *testFileSystem) Unlink(ctx *Context, path string) (code fuse.Status) {
return fuse.ToStatus(syscall.Unlink(fs.absPath(path)))
}
func (fs *testFileSystem) Rmdir(ctx *Context, path string) (code fuse.Status) {
return fuse.ToStatus(syscall.Rmdir(fs.absPath(path)))
}
func (fs *testFileSystem) Rename(ctx *Context, path string, newPath string) fuse.Status {
return fuse.ToStatus(syscall.Rename(fs.absPath(path), fs.absPath(newPath)))
}
func (fs *testFileSystem) Link(ctx *Context, path string, newPath string) fuse.Status {
return fuse.ToStatus(syscall.Link(fs.absPath(path), fs.absPath(newPath)))
}
func (fs *testFileSystem) Symlink(ctx *Context, path string, target string) fuse.Status {
return fuse.ToStatus(syscall.Symlink(target, fs.absPath(path)))
}
func (fs *testFileSystem) Readlink(ctx *Context, path string) (target string, code fuse.Status) {
f, err := os.Readlink(fs.absPath(path))
return f, fuse.ToStatus(err)
}
func (fs *testFileSystem) Create(ctx *Context, path string, flags uint32, mode uint32) (uFh uint32, forceDIO bool, code fuse.Status) {
flags = flags &^ syscall.O_APPEND
fd, err := syscall.Open(fs.absPath(path), int(flags)|os.O_CREATE, mode)
if err != nil {
syscall.Close(fd)
code = fuse.ToStatus(err)
return
}
uFh = uint32(fd)
return
}
func (fs *testFileSystem) Open(ctx *Context, path string, flags uint32) (uFh uint32, keepCache, forceDIO bool, code fuse.Status) {
fd, err := syscall.Open(fs.absPath(path), int(flags), 0)
forceDIO = true
if err != nil {
syscall.Close(fd)
code = fuse.ToStatus(err)
return
}
uFh = uint32(fd)
return
}
func (fs *testFileSystem) Read(ctx *Context, path string, uFh uint32, dest []byte, off uint64) (result fuse.ReadResult, code fuse.Status) {
var err error
var sz int
var fd int
if uFh == 0 {
fd, err = syscall.Open(fs.absPath(path), syscall.O_RDONLY, 0)
defer syscall.Close(fd)
if err != nil {
return nil, fuse.ToStatus(err)
}
} else {
fd = int(uFh)
}
sz, err = syscall.Pread(fd, dest, int64(off))
return fuse.ReadResultData(dest[:sz]), fuse.ToStatus(err)
}
func (fs *testFileSystem) Write(ctx *Context, path string, uFh uint32, data []byte, off uint64) (written uint32, code fuse.Status) {
var err error
var sz int
var fd int
if uFh == 0 {
fd, err = syscall.Open(fs.absPath(path), syscall.O_RDONLY, 0)
defer syscall.Close(fd)
if err != nil {
return 0, fuse.ToStatus(err)
}
} else {
fd = int(uFh)
}
sz, err = syscall.Pwrite(fd, data, int64(off))
return uint32(sz), fuse.ToStatus(err)
}
func (fs *testFileSystem) Fsync(ctx *Context, path string, uFh uint32, flags uint32) fuse.Status {
if uFh != 0 {
return fuse.ToStatus(syscall.Fsync(int(uFh)))
} else {
return fuse.OK
}
}
func (fs *testFileSystem) Release(ctx *Context, path string, uFh uint32) {
if uFh != 0 {
syscall.Close(int(uFh))
}
}
func (fs *testFileSystem) Chmod(ctx *Context, path string, uFh uint32, mode uint32) fuse.Status {
var err error
if uFh != 0 {
err = syscall.Fchmod(int(uFh), mode)
} else {
err = syscall.Chmod(fs.absPath(path), mode)
}
return fuse.ToStatus(err)
}
func (fs *testFileSystem) Chown(ctx *Context, path string, uFh uint32, uid uint32, gid uint32) fuse.Status {
// for chown command can only be used by root, we ignore this operation
return fuse.ENOSYS
}
func (fs *testFileSystem) Truncate(ctx *Context, path string, uFh uint32, size uint64) fuse.Status {
var err error
if uFh != 0 {
err = syscall.Ftruncate(int(uFh), int64(size))
} else {
err = os.Truncate(fs.absPath(path), int64(size))
}
return fuse.ToStatus(err)
}
func (fs *testFileSystem) Lsdir(ctx *Context, path string) (stream []fuse.DirEntry, code fuse.Status) {
f, err := os.Open(fs.absPath(path))
defer f.Close()
if err != nil {
return nil, fuse.ToStatus(err)
}
batch := 256
stream = make([]fuse.DirEntry, 0, batch)
for {
infos, err := f.Readdir(batch)
for i := range infos {
name := infos[i].Name()
d := fuse.DirEntry{
Name: name,
}
if s := fuse.ToStatT(infos[i]); s != nil {
d.Mode = uint32(s.Mode)
d.Ino = s.Ino
}
stream = append(stream, d)
}
if len(infos) < batch || err == io.EOF {
break
}
if err != nil {
code = fuse.ToStatus(err)
break
}
}
return
}
func (fs *testFileSystem) StatFs(ctx *Context, path string, out *fuse.StatfsOut) fuse.Status {
s := syscall.Statfs_t{}
err := syscall.Statfs(fs.absPath(path), &s)
if err != nil {
return fuse.ToStatus(err)
}
out.FromStatfsT(&s)
return fuse.OK
}
func (fs *testFileSystem) Utimens(ctx *Context, path string, uFh uint32, atime *time.Time, mtime *time.Time) fuse.Status {
var err error
if uFh != 0 {
err = fUtimes(int(uFh), atime, mtime)
} else {
err = utimes(fs.absPath(path), atime, mtime)
}
return fuse.ToStatus(err)
}
func (fs *testFileSystem) SetXAttr(ctx *Context, path string, attr string, data []byte, flags uint32) fuse.Status {
var m map[string][]byte
var ok bool
if m, ok = fs.xattrs[path]; !ok {
m = make(map[string][]byte)
fs.xattrs[path] = m
}
m[attr] = data
return fuse.OK
}
func (fs *testFileSystem) GetXAttr(ctx *Context, path string, attr string) (data []byte, code fuse.Status) {
var m map[string][]byte
var ok bool
if m, ok = fs.xattrs[path]; !ok {
return nil, fuse.ENODATA
}
if data, ok = m[attr]; !ok {
return nil, fuse.ENODATA
}
return data, fuse.OK
}
func (fs *testFileSystem) ListXAttr(ctx *Context, path string) (attrs []string, code fuse.Status) {
var m map[string][]byte
var ok bool
if m, ok = fs.xattrs[path]; !ok {
return nil, fuse.ENODATA
}
for k := range m {
attrs = append(attrs, k)
}
return attrs, fuse.OK
}
func (fs *testFileSystem) RemoveXAttr(ctx *Context, path string, attr string) fuse.Status {
var m map[string][]byte
var ok bool
if m, ok = fs.xattrs[path]; !ok {
return fuse.ENODATA
}
delete(m, attr)
if len(m) == 0 {
delete(fs.xattrs, path)
}
return fuse.OK
}