/
simsata.go
79 lines (67 loc) · 1.31 KB
/
simsata.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
package main
import (
"os"
"os/signal"
"syscall"
"time"
"bazil.org/fuse"
"bazil.org/fuse/fs"
"github.com/chzyer/flagx"
"gopkg.in/logex.v1"
)
var (
FsName = "simsata"
)
type Config struct {
Base string `flag:"def=/Volumes/fuse"`
Target string `flag:"def=/tmp/fuse"`
}
func NewConfig() *Config {
var c Config
flagx.Parse(&c)
return &c
}
func main() {
config := NewConfig()
fuse.Unmount(config.Base)
os.MkdirAll(config.Base, 0777)
conn, err := process(config)
if err != nil {
logex.Fatal(err)
}
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, os.Kill, syscall.SIGTERM, syscall.SIGHUP)
<-c
fuse.Unmount(config.Base)
conn.Close()
}
func process(c *Config) (conn *fuse.Conn, err error) {
if err = fuse.Unmount(c.Base); err == nil {
logex.Info("last not unmount")
time.Sleep(1000 * time.Millisecond)
err = nil
} else {
err = nil
}
ops := []fuse.MountOption{
fuse.AllowOther(),
fuse.FSName(FsName),
fuse.LocalVolume(),
}
conn, err = fuse.Mount(c.Base, ops...)
if err != nil {
return nil, logex.Trace(err)
}
go fs.Serve(conn, NewTree("/", c.Target))
logex.Info("connected.")
return conn, nil
}
type Tree struct {
root *Node
}
func NewTree(base, target string) *Tree {
return &Tree{NewNode(base, target)}
}
func (t *Tree) Root() (fs.Node, error) {
return t.root, nil
}