-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
143 lines (114 loc) · 2.86 KB
/
main.go
File metadata and controls
143 lines (114 loc) · 2.86 KB
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
package main
import (
"fmt"
"log"
"os"
"os/signal"
"runtime"
"sync"
"syscall"
"golang.org/x/net/context"
"github.com/jacobsa/fuse"
"github.com/jacobsa/fuse/fuseutil"
"github.com/jessevdk/go-flags"
)
var (
version = "compiled manually"
)
// Options are global settings.
type Options struct {
Version bool `long:"version" short:"V" description:"print version number"`
Verbose bool `long:"verbose" short:"v" description:"be verbose"`
Debug bool `long:"debug" description:"output debug messages"`
Seed int64 `long:"seed" default:"23" description:"initial random seed"`
NumFiles int `long:"files-per-dir" short:"n" default:"100" description:"number of files per directory"`
MaxSize int `long:"maxsize" short:"m" default:"100" description:"max individual file size, in KiB"`
mountpoint string
}
var opts = Options{}
var parser = flags.NewParser(&opts, flags.HelpFlag|flags.PassDoubleDash)
var ctx context.Context
func init() {
parser.Usage = "mountpoint"
var cancel context.CancelFunc
ctx, cancel = context.WithCancel(context.Background())
c := make(chan os.Signal)
signal.Notify(c, syscall.SIGINT)
go func() {
once := &sync.Once{}
for range c {
once.Do(func() {
fmt.Println("Interrupt received, cleaning up")
cancel()
})
}
}()
}
// V prints debug messages if verbose mode is requested.
func V(format string, data ...interface{}) {
if opts.Verbose {
fmt.Printf(format, data...)
}
}
// M prints a message to stdout.
func M(format string, data ...interface{}) {
fmt.Printf(format, data...)
}
func mount(opts Options) (*fuse.MountedFileSystem, error) {
fakefs, err := NewFakeDataFS(ctx, opts.Seed, opts.MaxSize*1024, opts.NumFiles)
if err != nil {
return nil, err
}
cfg := &fuse.MountConfig{
FSName: "fakedatafs",
ReadOnly: true,
ErrorLogger: log.New(os.Stderr, "ERROR: ", log.LstdFlags),
}
if opts.Debug {
cfg.DebugLogger = log.New(os.Stderr, "DEBUG: ", log.LstdFlags)
}
fs, err := fuse.Mount(
opts.mountpoint,
fuseutil.NewFileSystemServer(fakefs),
cfg,
)
if err != nil {
return nil, err
}
M("filesystem mounted at %v\n", opts.mountpoint)
return fs, nil
}
func main() {
args, err := parser.Parse()
if e, ok := err.(*flags.Error); ok && e.Type == flags.ErrHelp {
parser.WriteHelp(os.Stdout)
os.Exit(0)
}
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
}
if err != nil {
os.Exit(1)
}
if opts.Version {
fmt.Printf("version %v using %v on %v/%v\n",
version, runtime.Version(), runtime.GOOS, runtime.GOARCH)
return
}
if len(args) == 0 {
parser.WriteHelp(os.Stderr)
os.Exit(1)
}
opts.mountpoint = args[0]
fs, err := mount(opts)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(2)
}
fs.Join(ctx)
err = fuse.Unmount(fs.Dir())
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(3)
}
}