forked from pachyderm/pachyderm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mounter.go
113 lines (103 loc) · 2.06 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
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
package fuse
import (
"fmt"
"os"
"os/signal"
"sync"
"bazil.org/fuse"
"bazil.org/fuse/fs"
"github.com/pachyderm/pachyderm/src/client"
log "github.com/sirupsen/logrus"
)
const (
namePrefix = "pfs://"
subtype = "pfs"
)
type mounter struct {
address string
apiClient *client.APIClient
}
func newMounter(address string, apiClient *client.APIClient) Mounter {
return &mounter{
address,
apiClient,
}
}
func (m *mounter) MountAndCreate(
mountPoint string,
commitMounts []*CommitMount,
ready chan bool,
debug bool,
oneMount bool,
) error {
if err := os.MkdirAll(mountPoint, 0777); err != nil {
return err
}
return m.Mount(mountPoint, commitMounts, ready, debug, oneMount)
}
func (m *mounter) Mount(
mountPoint string,
commitMounts []*CommitMount,
ready chan bool,
debug bool,
oneMount bool,
) (retErr error) {
var once sync.Once
defer once.Do(func() {
if ready != nil {
close(ready)
}
})
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)
}
})
config := &fs.Config{}
if debug {
config.Debug = func(msg interface{}) { log.Printf("%+v", msg) }
} else {
log.SetLevel(log.ErrorLevel)
}
var filesystem fs.FS
if oneMount {
if len(commitMounts) != 1 {
return fmt.Errorf("expect 1 CommitMount, got %d", len(commitMounts))
}
filesystem = newRepoFilesystem(m.apiClient, commitMounts[0])
} else {
filesystem = newFilesystem(m.apiClient, commitMounts)
}
if err := fs.New(conn, config).Serve(filesystem); err != nil {
return err
}
<-conn.Ready
return conn.MountError
}
func (m *mounter) Unmount(mountPoint string) error {
return fuse.Unmount(mountPoint)
}