-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathffsdir.go
119 lines (95 loc) · 2.23 KB
/
ffsdir.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
/* TACIXAT 2020 */
package main
import (
"bazil.org/fuse"
"bazil.org/fuse/fs"
"context"
"encoding/json"
"os"
"sync"
"syscall"
)
type FFSDir struct {
Name string
Interfaces map[string]*FFSInterface
Children map[string]*FFSWorm
Index uint64
Mutex *sync.Mutex
}
func NewFFSDir(name string) *FFSDir {
ffsd := &FFSDir{
Name: name,
Interfaces: make(map[string]*FFSInterface),
Children: make(map[string]*FFSWorm),
Index: lidx.Next(),
Mutex: new(sync.Mutex),
}
ffsd.Interfaces["info"] = InfoInterface()
return ffsd
}
func InfoInterface() *FFSInterface {
ffsiInfo := NewFFSInterface("info")
ffsiInfo.ReadHandler = func() ([]byte, error) {
return getInfo()
}
ffsiInfo.AttrHandler = func(a *fuse.Attr) error {
a.Valid = 0
a.Inode = ffsiInfo.Index
a.Mode = 0o444
out, _ := getInfo()
a.Size = uint64(len(out))
return nil
}
return ffsiInfo
}
func getInfo() ([]byte, error) {
info := struct {
Seed int64 `json:"seed"`
BatchSize uint `json:"batch_size"`
}{}
info.Seed = *seed
info.BatchSize = *batchSize
out, err := json.Marshal(info)
if err != nil {
return []byte{}, nil
}
out = append(out, '\n')
return out, nil
}
func (ffsd *FFSDir) Attr(ctx context.Context, a *fuse.Attr) error {
a.Valid = 0
a.Inode = ffsd.Index
a.Mode = os.ModeDir | 0o644
return nil
}
func (ffsd *FFSDir) Create(ctx context.Context, req *fuse.CreateRequest, resp *fuse.CreateResponse) (fs.Node, fs.Handle, error) {
ffsd.Mutex.Lock()
defer ffsd.Mutex.Unlock()
ffsw := NewFFSWorm(req.Name)
ffsd.Children[req.Name] = ffsw
resp.EntryValid = 0
return ffsw, ffsw, nil
}
func (ffsd *FFSDir) Lookup(ctx context.Context, name string) (fs.Node, error) {
ffsd.Mutex.Lock()
defer ffsd.Mutex.Unlock()
if f, ok := ffsd.Children[name]; ok {
return f, nil
}
if f, ok := ffsd.Interfaces[name]; ok {
return f, nil
}
return nil, syscall.ENOENT
}
func (ffsd *FFSDir) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
ffsd.Mutex.Lock()
defer ffsd.Mutex.Unlock()
l := make([]fuse.Dirent, 0)
for n, c := range ffsd.Children {
l = append(l, fuse.Dirent{c.Index, fuse.DT_Dir, n})
}
for n, i := range ffsd.Interfaces {
l = append(l, fuse.Dirent{i.Index, fuse.DT_File, n})
}
return l, nil
}