-
Notifications
You must be signed in to change notification settings - Fork 1
/
mounter_nix.go
53 lines (45 loc) · 1.18 KB
/
mounter_nix.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
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
// +build !windows
package mounter
import (
"errors"
"fmt"
"os/exec"
"runtime"
"github.com/keybase/client/go/libkb"
"bazil.org/fuse"
)
// Unmount tries to unmount normally and then if force if unsuccessful.
func Unmount(g *libkb.GlobalContext, dir string, force bool) error {
if !force {
mounted, err := IsMounted(g, dir)
if err != nil {
return err
}
if !mounted {
return fmt.Errorf("Not mounted")
}
}
g.Log.Info("Trying to unmount: %s", dir)
err := fuse.Unmount(dir)
if err != nil {
if !force {
return err
}
// Unmount failed and we want to force it.
g.Log.Info("Unmount %s failed: %s; We will try to force it", dir, err)
err = ForceUnmount(g, dir)
}
return err
}
func ForceUnmount(g *libkb.GlobalContext, dir string) (err error) {
if runtime.GOOS == "darwin" {
_, err = exec.Command("/usr/sbin/diskutil", "unmountDisk", "force", dir).Output()
} else if runtime.GOOS == "linux" {
_, err = exec.Command("umount", "-l", dir).Output()
} else {
err = errors.New("Forced unmount is not supported on this platform yet")
}
return
}