fusego/mount_darwin.go

210 lines
5.1 KiB
Go
Raw Normal View History

2015-07-24 08:45:16 +03:00
package fuse
import (
"bytes"
"errors"
"fmt"
"os"
"os/exec"
"strconv"
"strings"
"syscall"
2015-07-24 08:48:23 +03:00
"github.com/jacobsa/fuse/internal/buffer"
)
var errNoAvail = errors.New("no available fuse devices")
var errNotLoaded = errors.New("osxfuse is not loaded")
// errOSXFUSENotFound is returned from Mount when the OSXFUSE installation is
// not detected. Make sure OSXFUSE is installed.
var errOSXFUSENotFound = errors.New("cannot locate OSXFUSE")
2016-10-16 10:56:44 +03:00
// osxfuseInstallation describes the paths used by an installed OSXFUSE
// version.
type osxfuseInstallation struct {
// Prefix for the device file. At mount time, an incrementing number is
// suffixed until a free FUSE device is found.
DevicePrefix string
// Path of the load helper, used to load the kernel extension if no device
// files are found.
Load string
// Path of the mount helper, used for the actual mount operation.
Mount string
// Environment variable used to pass the path to the executable calling the
// mount helper.
DaemonVar string
}
var (
2016-10-16 10:56:44 +03:00
osxfuseInstallations = []osxfuseInstallation{
// v3
{
DevicePrefix: "/dev/osxfuse",
Load: "/Library/Filesystems/osxfuse.fs/Contents/Resources/load_osxfuse",
Mount: "/Library/Filesystems/osxfuse.fs/Contents/Resources/mount_osxfuse",
DaemonVar: "MOUNT_OSXFUSE_DAEMON_PATH",
},
// v2
{
DevicePrefix: "/dev/osxfuse",
Load: "/Library/Filesystems/osxfusefs.fs/Support/load_osxfusefs",
Mount: "/Library/Filesystems/osxfusefs.fs/Support/mount_osxfusefs",
DaemonVar: "MOUNT_FUSEFS_DAEMON_PATH",
},
}
)
func loadOSXFUSE(bin string) error {
cmd := exec.Command(bin)
cmd.Dir = "/"
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
return err
}
func openOSXFUSEDev(devPrefix string) (dev *os.File, err error) {
2015-07-24 08:48:17 +03:00
// Try each device name.
for i := uint64(0); ; i++ {
path := devPrefix + strconv.FormatUint(i, 10)
2015-07-24 08:48:17 +03:00
dev, err = os.OpenFile(path, os.O_RDWR, 0000)
if os.IsNotExist(err) {
if i == 0 {
2015-07-24 08:48:17 +03:00
// Not even the first device was found. Fuse must not be loaded.
return nil, errNotLoaded
}
2015-07-24 08:48:17 +03:00
// Otherwise we've run out of kernel-provided devices
return nil, errNoAvail
}
if err2, ok := err.(*os.PathError); ok && err2.Err == syscall.EBUSY {
2015-07-24 08:48:17 +03:00
// This device is in use; try the next one.
continue
}
return dev, nil
}
}
2015-07-24 08:51:18 +03:00
func callMount(
bin string,
daemonVar string,
2015-07-24 08:51:18 +03:00
dir string,
2015-07-24 08:54:02 +03:00
cfg *MountConfig,
2015-07-24 08:51:18 +03:00
dev *os.File,
ready chan<- error) error {
2015-07-24 08:51:18 +03:00
// The mount helper doesn't understand any escaping.
2015-07-24 08:54:02 +03:00
for k, v := range cfg.toMap() {
if strings.Contains(k, ",") || strings.Contains(v, ",") {
2015-07-24 08:51:18 +03:00
return fmt.Errorf(
"mount options cannot contain commas on darwin: %q=%q",
k,
v)
}
}
2015-07-24 08:51:18 +03:00
// Call the mount helper, passing in the device file and saving output into a
// buffer.
cmd := exec.Command(
bin,
2015-07-24 08:54:02 +03:00
"-o", cfg.toOptionsString(),
// Tell osxfuse-kext how large our buffer is. It must split
// writes larger than this into multiple writes.
//
// OSXFUSE seems to ignore InitResponse.MaxWrite, and uses
// this instead.
2015-07-24 08:48:23 +03:00
"-o", "iosize="+strconv.FormatUint(buffer.MaxWriteSize, 10),
// refers to fd passed in cmd.ExtraFiles
"3",
dir,
)
2015-07-24 08:51:18 +03:00
cmd.ExtraFiles = []*os.File{dev}
cmd.Env = os.Environ()
// OSXFUSE <3.3.0
cmd.Env = append(cmd.Env, "MOUNT_FUSEFS_CALL_BY_LIB=")
// OSXFUSE >=3.3.0
cmd.Env = append(cmd.Env, "MOUNT_OSXFUSE_CALL_BY_LIB=")
daemon := os.Args[0]
if daemonVar != "" {
cmd.Env = append(cmd.Env, daemonVar+"="+daemon)
}
2015-07-24 08:51:18 +03:00
var buf bytes.Buffer
cmd.Stdout = &buf
cmd.Stderr = &buf
if err := cmd.Start(); err != nil {
return err
}
2015-07-24 08:51:18 +03:00
// In the background, wait for the command to complete.
go func() {
err := cmd.Wait()
if err != nil {
if buf.Len() > 0 {
output := buf.Bytes()
output = bytes.TrimRight(output, "\n")
2015-07-24 08:51:18 +03:00
err = fmt.Errorf("%v: %s", err, output)
}
}
2015-07-24 08:51:18 +03:00
ready <- err
}()
2015-07-24 08:51:18 +03:00
return nil
}
2015-07-24 08:45:16 +03:00
// Begin the process of mounting at the given directory, returning a connection
// to the kernel. Mounting continues in the background, and is complete when an
// error is written to the supplied channel. The file system may need to
// service the connection in order for mounting to complete.
func mount(
dir string,
2015-07-24 08:54:02 +03:00
cfg *MountConfig,
2015-07-24 08:45:16 +03:00
ready chan<- error) (dev *os.File, err error) {
// Find the version of osxfuse installed on this machine.
2016-10-16 10:56:44 +03:00
for _, loc := range osxfuseInstallations {
if _, err := os.Stat(loc.Mount); os.IsNotExist(err) {
// try the other locations
continue
}
// Open the device.
dev, err = openOSXFUSEDev(loc.DevicePrefix)
// Special case: we may need to explicitly load osxfuse. Load it, then
// try again.
if err == errNotLoaded {
err = loadOSXFUSE(loc.Load)
if err != nil {
return nil, fmt.Errorf("loadOSXFUSE: %v", err)
}
2015-07-24 08:45:16 +03:00
dev, err = openOSXFUSEDev(loc.DevicePrefix)
}
// Propagate errors.
if err != nil {
return nil, fmt.Errorf("openOSXFUSEDev: %v", err)
}
2015-07-24 08:45:16 +03:00
// Call the mount binary with the device.
if err := callMount(loc.Mount, loc.DaemonVar, dir, cfg, dev, ready); err != nil {
dev.Close()
return nil, fmt.Errorf("callMount: %v", err)
}
2015-07-24 08:45:16 +03:00
return dev, nil
}
2015-07-24 08:45:16 +03:00
return nil, errOSXFUSENotFound
}