-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
79 lines (67 loc) · 1.34 KB
/
main.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 (
"context"
"flag"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"github.com/hanwen/go-fuse/fs"
"github.com/hanwen/go-fuse/fuse"
)
type HelloRoot struct {
fs.Inode
}
func (r *HelloRoot) OnAdd(ctx context.Context) {
ch := r.NewPersistentInode(
ctx, &fs.MemRegularFile{
Data: []byte("hello"),
Attr: fuse.Attr{
Mode: 0644,
},
}, fs.StableAttr{Ino: 2})
r.AddChild("hello", ch, false)
}
func (r *HelloRoot) Getattr(ctx context.Context, fh fs.FileHandle, out *fuse.AttrOut) syscall.Errno {
out.Mode = 0755
return 0
}
var _ = (fs.NodeGetattrer)((*HelloRoot)(nil))
var _ = (fs.NodeOnAdder)((*HelloRoot)(nil))
func main() {
flag.Parse()
if len(flag.Args()) < 1 {
log.Fatal("Usage:\n hello MOUNTPOINT")
}
err := run(flag.Arg(0))
if err != nil {
fmt.Fprintf(os.Stderr, "hellofs: %s", err)
os.Exit(1)
}
}
func run(mountpoint string) error {
// Catch SIGINT to unmount.
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
log.Printf("Mounting hellofs on %q", mountpoint)
opts := &fs.Options{
MountOptions: fuse.MountOptions{
AllowOther: true,
},
}
server, err := fs.Mount(mountpoint, &HelloRoot{}, opts)
if err != nil {
return err
}
go func() {
<-c
log.Printf("Unmounting %q", mountpoint)
err := server.Unmount()
if err != nil {
panic(err)
}
}()
server.Wait()
return nil
}