mount: use standalone shutdown hook via goroutine
This commit is contained in:
parent
93135dc705
commit
86c7909f41
2 changed files with 25 additions and 26 deletions
|
@ -152,28 +152,15 @@ func runMount(ctx context.Context, opts MountOptions, gopts GlobalOptions, args
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
AddCleanupHandler(func(code int) (int, error) {
|
systemFuse.Debug = func(msg interface{}) {
|
||||||
debug.Log("running umount cleanup handler for mount at %v", mountpoint)
|
debug.Log("fuse: %v", msg)
|
||||||
err := umount(mountpoint)
|
}
|
||||||
if err != nil {
|
|
||||||
Warnf("unable to umount (maybe already umounted or still in use?): %v\n", err)
|
|
||||||
}
|
|
||||||
// replace error code of sigint
|
|
||||||
if code == 130 {
|
|
||||||
code = 0
|
|
||||||
}
|
|
||||||
return code, nil
|
|
||||||
})
|
|
||||||
|
|
||||||
c, err := systemFuse.Mount(mountpoint, mountOptions...)
|
c, err := systemFuse.Mount(mountpoint, mountOptions...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
systemFuse.Debug = func(msg interface{}) {
|
|
||||||
debug.Log("fuse: %v", msg)
|
|
||||||
}
|
|
||||||
|
|
||||||
cfg := fuse.Config{
|
cfg := fuse.Config{
|
||||||
OwnerIsRoot: opts.OwnerRoot,
|
OwnerIsRoot: opts.OwnerRoot,
|
||||||
Filter: opts.SnapshotFilter,
|
Filter: opts.SnapshotFilter,
|
||||||
|
@ -187,15 +174,26 @@ func runMount(ctx context.Context, opts MountOptions, gopts GlobalOptions, args
|
||||||
Printf("When finished, quit with Ctrl-c here or umount the mountpoint.\n")
|
Printf("When finished, quit with Ctrl-c here or umount the mountpoint.\n")
|
||||||
|
|
||||||
debug.Log("serving mount at %v", mountpoint)
|
debug.Log("serving mount at %v", mountpoint)
|
||||||
err = fs.Serve(c, root)
|
|
||||||
if err != nil {
|
done := make(chan struct{})
|
||||||
return err
|
|
||||||
|
go func() {
|
||||||
|
defer close(done)
|
||||||
|
err = fs.Serve(c, root)
|
||||||
|
}()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
debug.Log("running umount cleanup handler for mount at %v", mountpoint)
|
||||||
|
err := systemFuse.Unmount(mountpoint)
|
||||||
|
if err != nil {
|
||||||
|
Warnf("unable to umount (maybe already umounted or still in use?): %v\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
case <-done:
|
||||||
|
// clean shutdown, nothing to do
|
||||||
}
|
}
|
||||||
|
|
||||||
<-c.Ready
|
return err
|
||||||
return c.MountError
|
|
||||||
}
|
|
||||||
|
|
||||||
func umount(mountpoint string) error {
|
|
||||||
return systemFuse.Unmount(mountpoint)
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,6 +12,7 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
systemFuse "github.com/anacrolix/fuse"
|
||||||
"github.com/restic/restic/internal/restic"
|
"github.com/restic/restic/internal/restic"
|
||||||
rtest "github.com/restic/restic/internal/test"
|
rtest "github.com/restic/restic/internal/test"
|
||||||
)
|
)
|
||||||
|
@ -65,7 +66,7 @@ func testRunMount(t testing.TB, gopts GlobalOptions, dir string, wg *sync.WaitGr
|
||||||
func testRunUmount(t testing.TB, dir string) {
|
func testRunUmount(t testing.TB, dir string) {
|
||||||
var err error
|
var err error
|
||||||
for i := 0; i < mountWait; i++ {
|
for i := 0; i < mountWait; i++ {
|
||||||
if err = umount(dir); err == nil {
|
if err = systemFuse.Unmount(dir); err == nil {
|
||||||
t.Logf("directory %v umounted", dir)
|
t.Logf("directory %v umounted", dir)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue