forked from pachyderm/pachyderm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mounter.go
86 lines (77 loc) · 1.49 KB
/
mounter.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
package fuse
import (
"os"
"os/signal"
"sync"
"bazil.org/fuse"
"bazil.org/fuse/fs"
pfsclient "github.com/pachyderm/pachyderm/src/client/pfs"
)
const (
namePrefix = "pfs://"
subtype = "pfs"
)
type mounter struct {
address string
apiClient pfsclient.APIClient
}
func newMounter(address string, apiClient pfsclient.APIClient) Mounter {
return &mounter{
address,
apiClient,
}
}
func (m *mounter) Mount(
mountPoint string,
shard *pfsclient.Shard,
commitMounts []*CommitMount,
ready chan bool,
) (retErr error) {
var once sync.Once
defer once.Do(func() {
if ready != nil {
close(ready)
}
})
// TODO: should we make the caller do this?
if err := os.MkdirAll(mountPoint, 0777); err != nil {
return err
}
name := namePrefix + m.address
conn, err := fuse.Mount(
mountPoint,
fuse.FSName(name),
fuse.VolumeName(name),
fuse.Subtype(subtype),
fuse.AllowOther(),
fuse.WritebackCache(),
fuse.MaxReadahead(1<<32-1),
)
if err != nil {
return err
}
defer func() {
if err := conn.Close(); err != nil && retErr == nil {
retErr = err
}
}()
sigChan := make(chan os.Signal,1)
signal.Notify(sigChan, os.Interrupt)
go func() {
<-sigChan
m.Unmount(mountPoint)
}()
once.Do(func() {
if ready != nil {
close(ready)
}
})
if err := fs.Serve(conn, newFilesystem(m.apiClient, shard, commitMounts)); err != nil {
return err
}
<-conn.Ready
return conn.MountError
}
func (m *mounter) Unmount(mountPoint string) error {
return fuse.Unmount(mountPoint)
}