-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfuse.go
90 lines (74 loc) · 1.54 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
package main
import (
"context"
"syscall"
"bazil.org/fuse"
"bazil.org/fuse/fs"
)
func (entry *listingEntry) fuseDirEntType() fuse.DirentType {
switch {
case entry.IsDir():
return fuse.DT_Dir
default:
return fuse.DT_Dir
}
}
func (entry *listingEntry) toDirEnt() fuse.Dirent {
return fuse.Dirent{
Inode: entry.inode,
Type: entry.fuseDirEntType(),
Name: entry.name,
}
}
type FS struct {
RootEntry *Dir
}
func (fs FS) Root() (fs.Node, error) {
return *fs.RootEntry, nil
}
type Dir struct {
entry *listingEntry
}
func (d Dir) Attr(ctx context.Context, a *fuse.Attr) error {
a.Inode = d.entry.inode
a.Mode = d.entry.Mode()
a.Size = uint64(d.entry.Size())
return nil
}
func (d Dir) Lookup(ctx context.Context, name string) (fs.Node, error) {
entry := d.entry.Find(name, ctx)
if entry == nil {
return nil, syscall.ENOENT
}
if entry.IsDir() {
return Dir{entry}, nil
} else {
return File{entry}, nil
}
}
func (d Dir) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
dirs := []fuse.Dirent{
{0, fuse.DT_Dir, "."},
{0, fuse.DT_Dir, ".."},
}
err := d.entry.retrieveDirectoryListing(ctx)
if err != nil {
return nil, err
}
for _, child := range d.entry.children {
dirs = append(dirs, child.toDirEnt())
}
return dirs, nil
}
type File struct {
entry *listingEntry
}
func (f File) Attr(ctx context.Context, a *fuse.Attr) error {
a.Inode = f.entry.inode
a.Mode = f.entry.Mode()
a.Size = uint64(f.entry.Size())
return nil
}
func (f File) ReadAll(ctx context.Context) ([]byte, error) {
return f.entry.Download(ctx)
}