-
Notifications
You must be signed in to change notification settings - Fork 0
/
fuse.go
341 lines (292 loc) · 7.53 KB
/
fuse.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package main
import (
"context"
"fmt"
"os"
"os/signal"
"os/user"
"strconv"
"sync"
"syscall"
"time"
"github.com/hanwen/go-fuse/v2/fs"
"github.com/hanwen/go-fuse/v2/fuse"
)
type (
node interface {
node() *GenericNode
size() uint64
mode() uint32
}
dirnode interface {
node
readdir() []node
lookup(name string) (node, bool)
}
GenericNode struct {
fs.Inode
Ino uint64
Filename string
Timestamp time.Time
}
RootNode struct {
GenericNode
Cats *sync.Map // map[string]*CategoryNode
}
CategoryNode struct {
GenericNode
Feeds *sync.Map // map[string]*FeedNode
}
FeedNode struct {
GenericNode
Feed
News *sync.Map // map[string]*NewsNode
}
NewsNode struct {
GenericNode
Data []byte
}
)
var root RootNode
func (g *GenericNode) node() *GenericNode { return g }
func (dir *GenericNode) size() uint64 { return 0 }
func (dir *GenericNode) mode() uint32 { return 0755 | uint32(syscall.S_IFDIR) }
func (file *NewsNode) size() uint64 { return uint64(len(file.Data)) }
func (file *NewsNode) mode() uint32 { return 0644 | uint32(syscall.S_IFREG) }
func setAttributes(n node, out *fuse.Attr) {
gn := n.node()
user, err := user.Current()
die(err)
out.SetTimes(&gn.Timestamp, &gn.Timestamp, &gn.Timestamp)
out.Mode = n.mode()
out.Size = n.size()
out.Ino = gn.Ino
uid32, _ := strconv.ParseUint(user.Uid, 10, 32)
gid32, _ := strconv.ParseUint(user.Gid, 10, 32)
out.Uid = uint32(uid32)
out.Gid = uint32(gid32)
}
func (gn *GenericNode) Getattr(ctx context.Context, fh fs.FileHandle, out *fuse.AttrOut) syscall.Errno {
setAttributes(gn, &out.Attr)
return fs.OK
}
func (nn *NewsNode) Getattr(ctx context.Context, fh fs.FileHandle, out *fuse.AttrOut) syscall.Errno {
setAttributes(nn, &out.Attr)
return fs.OK
}
func (root *RootNode) readdir() []node {
ret := make([]node, 0)
root.Cats.Range(func(_, v interface{}) bool {
cat := v.(*CategoryNode)
ret = append(ret, cat)
return true
})
return ret
}
func (cn *CategoryNode) readdir() []node {
ret := make([]node, 0)
cn.Feeds.Range(func(_, v interface{}) bool {
feed := v.(*FeedNode)
ret = append(ret, feed)
return true
})
return ret
}
func (fn *FeedNode) readdir() []node {
fn.News = &sync.Map{}
ret := make([]node, 0)
feeds, _ := GetFeedFiles(fn)
for i := 0; i < len(feeds); i++ {
fn.News.Store(feeds[i].Filename, &feeds[i])
ret = append(ret, &feeds[i])
}
return ret
}
func nodesToDirstream(nodes []node, ctx context.Context) (fs.DirStream, syscall.Errno) {
entries := make([]fuse.DirEntry, len(nodes))
for i, node := range nodes {
gn := node.node()
entries[i] = fuse.DirEntry{
Name: gn.Filename,
Mode: gn.mode(),
Ino: gn.Ino,
}
}
ds := fs.NewListDirStream(entries)
return ds, fs.OK
}
// Readdir returns a list of file entries for currentPath().
func (fn *FeedNode) Readdir(ctx context.Context) (fs.DirStream, syscall.Errno) {
return nodesToDirstream(fn.readdir(), ctx)
}
func (cn *CategoryNode) Readdir(ctx context.Context) (fs.DirStream, syscall.Errno) {
return nodesToDirstream(cn.readdir(), ctx)
}
func (root *RootNode) Readdir(ctx context.Context) (fs.DirStream, syscall.Errno) {
return nodesToDirstream(root.readdir(), ctx)
}
func dirlookup(dn dirnode, ctx context.Context, name string, out *fuse.EntryOut) (*fs.Inode, syscall.Errno) {
n, ok := dn.lookup(name)
if !ok {
return nil, syscall.ENOENT
}
var child *fs.Inode
dir := dn.node()
switch v := n.(type) {
case *CategoryNode:
setAttributes(v, &out.Attr)
sa := fs.StableAttr{
Mode: v.mode(),
Gen: 1,
Ino: v.Ino,
}
child = dir.NewPersistentInode(ctx, v, sa)
case *FeedNode:
setAttributes(n, &out.Attr)
setAttributes(v, &out.Attr)
sa := fs.StableAttr{
Mode: v.mode(),
Gen: 1,
Ino: v.Ino,
}
child = dir.NewPersistentInode(ctx, v, sa)
case *NewsNode:
setAttributes(v, &out.Attr)
sa := fs.StableAttr{
Mode: v.mode(),
Gen: 1,
Ino: v.Ino,
}
child = dir.NewPersistentInode(ctx, v, sa)
default:
panic("Unknown type")
}
return child, fs.OK
}
func lookupSyncMap(sm *sync.Map, name string) (node, bool) {
v, ok := sm.Load(name)
if !ok {
return nil, false
}
return v.(node), true
}
func (root *RootNode) lookup(name string) (node, bool) {
return lookupSyncMap(root.Cats, name)
}
func (cn *CategoryNode) lookup(name string) (node, bool) {
return lookupSyncMap(cn.Feeds, name)
}
func (fn *FeedNode) lookup(name string) (node, bool) {
return lookupSyncMap(fn.News, name)
}
// Lookup generates a node for the given file.
func (fn *FeedNode) Lookup(ctx context.Context, name string, out *fuse.EntryOut) (*fs.Inode, syscall.Errno) {
return dirlookup(fn, ctx, name, out)
}
func (cn *CategoryNode) Lookup(ctx context.Context, name string, out *fuse.EntryOut) (*fs.Inode, syscall.Errno) {
return dirlookup(cn, ctx, name, out)
}
func (root *RootNode) Lookup(ctx context.Context, name string, out *fuse.EntryOut) (*fs.Inode, syscall.Errno) {
return dirlookup(root, ctx, name, out)
}
// Read returns the requested file as bytes.
func (nn *NewsNode) Read(ctx context.Context, fh fs.FileHandle, dest []byte, off int64) (fuse.ReadResult, syscall.Errno) {
end := int(off) + len(dest)
if end > len(nn.Data) {
end = len(nn.Data)
}
return fuse.ReadResultData(nn.Data[off:end]), fs.OK
}
func (nn *NewsNode) Flush(ctx context.Context, fh fs.FileHandle) syscall.Errno {
return fs.OK
}
// Open parses the file into a file struct.
func (nn *NewsNode) Open(ctx context.Context, mode uint32) (fh fs.FileHandle, fuseFlags uint32, errno syscall.Errno) {
return nn, 0, 0
}
func (f *FeedNode) Opendir(ctx context.Context) syscall.Errno { return fs.OK }
func (c *CategoryNode) Opendir(ctx context.Context) syscall.Errno { return fs.OK }
func (_ *RootNode) Opendir(ctx context.Context) syscall.Errno { return fs.OK }
func getFeedNode(f *Feed) (fn FeedNode) {
feeddata, err := getFeedData(f.URL)
die(err)
return FeedNode{
GenericNode{
Timestamp: time.Now(),
Filename: feeddata.Title,
Ino: getInode([]byte("feed " + feeddata.Title)),
},
Feed{f.URL},
&sync.Map{},
}
}
func getCategoryNode(cat *Category) (cn CategoryNode) {
cn = CategoryNode{
GenericNode: GenericNode{
Timestamp: time.Now(),
Filename: cat.Name,
Ino: getInode([]byte("Cat " + cat.Name)),
},
Feeds: &sync.Map{},
}
ch := make(chan FeedNode, 20)
for _, f := range cat.Feeds {
go func(ch chan<- FeedNode, f Feed) {
ch <- getFeedNode(&f)
}(ch, *f)
}
for range cat.Feeds {
feed := <-ch
feed.Filename = fileNameClean(feed.Filename)
cn.Feeds.Store(feed.Filename, &feed)
}
close(ch)
return
}
func getRootNode(cats []*Category) (root RootNode) {
root = RootNode{
GenericNode: GenericNode{Timestamp: time.Now()},
Cats: &sync.Map{},
}
ch := make(chan CategoryNode, 20)
for _, c := range cats {
go func(ch chan<- CategoryNode, c Category) {
ch <- getCategoryNode(&c)
}(ch, *c)
}
for range cats {
cat := <-ch
cat.Filename = fileNameClean(cat.Filename)
root.Cats.Store(cat.Filename, &cat)
}
close(ch)
return
}
func Mount(cfg RssfsConfig) {
root = getRootNode(cfg.Categories)
sec := time.Second
opts := &fs.Options{
AttrTimeout: &sec,
EntryTimeout: &sec,
MountOptions: fuse.MountOptions{
AllowOther: true,
Debug: false,
FsName: "rssfs",
},
}
fs := fs.NewNodeFS(&root, opts)
mountPoint := cfg.MountPoint
server, err := fuse.NewServer(fs, mountPoint, &opts.MountOptions)
die(err)
go func() {
c := make(chan os.Signal)
signal.Notify(c, syscall.SIGTERM, syscall.SIGINT)
for {
<-c
fmt.Println("\nTrying to unmount...\n")
go server.Unmount()
}
}()
fmt.Println("Serving...")
server.Serve()
}