@cryptotaxi247 / kubo / commits / 6ff472be6

mount: move Mount func into mount obj

Juan Batiz-Benet committed Nov 16, 2014 at 01:56 UTC 6ff472be61e6463f2189af7bf7d74551c8602729
3 files changed +81 -62
fuse/ipns/mount_unix.go
+33 -27
@@ -19,34 +19,12 @@ func Mount(ipfs *core.IpfsNode, fpath string, ipfspath string) (mount.Mount, err
19 log.Infof("Mounting ipns at %s...", fpath)
20
21 // setup the Mount abstraction.
22 - m := mount.New(ipfs.Context(), fpath, unmount)
22 + m := mount.New(ipfs.Context(), fpath)
23
24 // go serve the mount
25 - mount.ServeMount(m, func(m mount.Mount) error {
26 -
27 - c, err := fuse.Mount(fpath)
28 - if err != nil {
29 - return err
30 - }
31 - defer c.Close()
32 -
33 - fsys, err := NewIpns(ipfs, ipfspath)
34 - if err != nil {
35 - return err
36 - }
37 -
38 - log.Infof("Mounted ipns at %s.", fpath)
39 - if err := fs.Serve(c, fsys); err != nil {
40 - return err
41 - }
42 -
43 - // check if the mount process has an error to report
44 - <-c.Ready
45 - if err := c.MountError; err != nil {
46 - return err
47 - }
48 - return nil
49 - })
25 + m.Mount(func(m mount.Mount) error {
26 + return internalMount(ipfs, fpath, ipfspath)
27 + }, internalUnmount)
28
29 select {
30 case <-m.Closed():
@@ -61,9 +39,37 @@ func Mount(ipfs *core.IpfsNode, fpath string, ipfspath string) (mount.Mount, err
39 return m, nil
40 }
41
42 +// mount attempts to mount at the provided FUSE mount point
43 +func internalMount(ipfs *core.IpfsNode, fpath string, ipfspath string) error {
44 +
45 + c, err := fuse.Mount(fpath)
46 + if err != nil {
47 + return err
48 + }
49 + defer c.Close()
50 +
51 + fsys, err := NewIpns(ipfs, ipfspath)
52 + if err != nil {
53 + return err
54 + }
55 +
56 + log.Infof("Mounted ipns at %s.", fpath)
57 + if err := fs.Serve(c, fsys); err != nil {
58 + return err
59 + }
60 +
61 + // check if the mount process has an error to report
62 + <-c.Ready
63 + if err := c.MountError; err != nil {
64 + return err
65 + }
66 + return nil
67 +}
68 +
69 // unmount attempts to unmount the provided FUSE mount point, forcibly
70 // if necessary.
66 -func unmount(point string) error {
71 +func internalUnmount(m mount.Mount) error {
72 + point := m.MountPoint()
73 log.Infof("Unmounting ipns at %s...", point)
74
75 var cmd *exec.Cmd
fuse/mount/mount.go
+17 -9
@@ -19,23 +19,26 @@ type Mount interface {
19 // MountPoint is the path at which this mount is mounted
20 MountPoint() string
21
22 + // Mount function sets up a mount + registers the unmount func
23 + Mount(mount MountFunc, unmount UnmountFunc)
24 +
25 // Unmount calls Close.
26 Unmount() error
27
28 ctxc.ContextCloser
29 }
30
28 -// UnmountFunc is a function used to unmount a mount
29 -type UnmountFunc func(mountpoint string) error
31 +// UnmountFunc is a function used to Unmount a mount
32 +type UnmountFunc func(Mount) error
33 +
34 +// MountFunc is a function used to Mount a mount
35 +type MountFunc func(Mount) error
36
37 // New constructs a new Mount instance. ctx is a context to wait upon,
38 // the mountpoint is the directory that the mount was mounted at, and unmount
39 // in an UnmountFunc to perform the unmounting logic.
34 -func New(ctx context.Context, mountpoint string, unmount UnmountFunc) Mount {
35 - m := &mount{
36 - mpoint: mountpoint,
37 - unmount: unmount,
38 - }
40 +func New(ctx context.Context, mountpoint string) Mount {
41 + m := &mount{mpoint: mountpoint}
42 m.ContextCloser = ctxc.NewContextCloser(ctx, m.persistentUnmount)
43 return m
44 }
@@ -50,10 +53,14 @@ type mount struct {
53 // umount is called after the mount is closed.
54 // TODO this is hacky, make it better.
55 func (m *mount) persistentUnmount() error {
56 + // no unmount func.
57 + if m.unmount == nil {
58 + return nil
59 + }
60
61 // ok try to unmount a whole bunch of times...
62 for i := 0; i < 34; i++ {
56 - err := m.unmount(m.mpoint)
63 + err := m.unmount(m)
64 if err == nil {
65 return nil
66 }
@@ -72,8 +79,9 @@ func (m *mount) Unmount() error {
79 return m.Close()
80 }
81
75 -func ServeMount(m Mount, mount func(Mount) error) {
82 +func (m *mount) Mount(mount MountFunc, unmount UnmountFunc) {
83 m.Children().Add(1)
84 + m.unmount = unmount
85
86 // go serve the mount
87 go func() {
fuse/readonly/readonly_unix.go
+31 -26
@@ -165,32 +165,12 @@ func Mount(ipfs *core.IpfsNode, fpath string) (mount.Mount, error) {
165 log.Infof("Mounting ipfs at %s...", fpath)
166
167 // setup the Mount abstraction.
168 - m := mount.New(ipfs.Context(), fpath, unmount)
168 + m := mount.New(ipfs.Context(), fpath)
169
170 // go serve the mount
171 - mount.ServeMount(m, func(m mount.Mount) error {
172 -
173 - c, err := fuse.Mount(m.MountPoint())
174 - if err != nil {
175 - return err
176 - }
177 - defer c.Close()
178 -
179 - fsys := FileSystem{Ipfs: ipfs}
180 -
181 - log.Infof("Mounted ipfs at %s.", fpath)
182 - if err := fs.Serve(c, fsys); err != nil {
183 - return err
184 - }
185 -
186 - // check if the mount process has an error to report
187 - <-c.Ready
188 - if err := c.MountError; err != nil {
189 - m.Unmount()
190 - return err
191 - }
192 - return nil
193 - })
171 + m.Mount(func(m mount.Mount) error {
172 + return internalMount(ipfs, m)
173 + }, internalUnmount)
174
175 select {
176 case <-m.Closed():
@@ -205,9 +185,34 @@ func Mount(ipfs *core.IpfsNode, fpath string) (mount.Mount, error) {
185 return m, nil
186 }
187
208 -// Unmount attempts to unmount the provided FUSE mount point, forcibly
188 +// mount attempts to mount the provided FUSE mount point
189 +func internalMount(ipfs *core.IpfsNode, m mount.Mount) error {
190 + c, err := fuse.Mount(m.MountPoint())
191 + if err != nil {
192 + return err
193 + }
194 + defer c.Close()
195 +
196 + fsys := FileSystem{Ipfs: ipfs}
197 +
198 + log.Infof("Mounted ipfs at %s.", m.MountPoint())
199 + if err := fs.Serve(c, fsys); err != nil {
200 + return err
201 + }
202 +
203 + // check if the mount process has an error to report
204 + <-c.Ready
205 + if err := c.MountError; err != nil {
206 + m.Unmount()
207 + return err
208 + }
209 + return nil
210 +}
211 +
212 +// unmount attempts to unmount the provided FUSE mount point, forcibly
213 // if necessary.
210 -func unmount(point string) error {
214 +func internalUnmount(m mount.Mount) error {
215 + point := m.MountPoint()
216 log.Infof("Unmounting ipfs at %s...", point)
217
218 var cmd *exec.Cmd