forked from cryptix/mountMgo
/
fuse.go
111 lines (88 loc) · 2.05 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
package main
import (
"log"
"os"
"strings"
"bazil.org/fuse"
"bazil.org/fuse/fs"
"golang.org/x/net/context"
)
func mount(point string, fsname string) error {
// startup mount
c, err := fuse.Mount(
point,
fuse.FSName(fsname),
fuse.VolumeName(fsname),
fuse.LocalVolume(),
)
checkErrorAndExit(err, 1)
defer c.Close()
log.Println("Mounted: ", point)
if err = fs.Serve(c, mgoFS{}); err != nil {
log.Fatal(err)
return err
}
// check if the mount process has an error to report
<-c.Ready
if err := c.MountError; err != nil {
log.Fatal(err)
return err
}
return nil
}
// mgoFS implements my mgo fuse filesystem
type mgoFS struct{}
func (mgoFS) Root() (fs.Node, error) {
log.Println("returning root node")
return Dir{"Root"}, nil
}
// Dir implements both Node and Handle for the root directory.
type Dir struct {
name string
}
func (d Dir) Attr(a *fuse.Attr) {
log.Println("Dir.Attr() for ", d.name)
a.Inode = 1
a.Mode = os.ModeDir | 0555
}
func (Dir) Lookup(ctx context.Context, name string) (fs.Node, error) {
log.Println("Dir.Lookup():", name)
// Check if lookup is on the GridFS
if name == gridfsPrefix {
return GridFs{Name: gridfsPrefix}, nil
}
db, s := getDb()
defer s.Close()
names, err := db.CollectionNames()
if err != nil {
log.Panic(err)
return nil, fuse.EIO
}
for _, collName := range names {
if collName == name {
return CollFile{Name: name}, nil
}
}
return nil, fuse.ENOENT
}
func (d Dir) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
log.Println("Dir.ReadDirAll():", d.name)
db, s := getDb()
defer s.Close()
names, err := db.CollectionNames()
if err != nil {
log.Panic(err)
return nil, fuse.EIO
}
ents := make([]fuse.Dirent, 0, len(names)+1) // one more for GridFS
// Append GridFS prefix
ents = append(ents, fuse.Dirent{Name: gridfsPrefix, Type: fuse.DT_Dir})
// Append the rest of the collections
for _, name := range names {
if strings.HasSuffix(name, ".indexes") {
continue
}
ents = append(ents, fuse.Dirent{Name: name, Type: fuse.DT_Dir})
}
return ents, nil
}