File: mount_darwin.go

package info (click to toggle)
golang-github-anacrolix-fuse 0.3.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,000 kB
  • sloc: makefile: 5; sh: 3
file content (270 lines) | stat: -rw-r--r-- 5,952 bytes parent folder | download
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package fuse

import (
	"bytes"
	"fmt"
	"os"
	"os/exec"
	"strconv"
	"syscall"
	"unsafe"
)

const FUSET_SRV_PATH = "/usr/local/bin/go-nfsv4"

func fusetBinary() (string, error) {
	srv_path := os.Getenv("FUSE_NFSSRV_PATH")
	if srv_path == "" {
		srv_path = FUSET_SRV_PATH
	}

	if _, err := os.Stat(srv_path); err == nil {
		return srv_path, nil
	}

	return "", fmt.Errorf("FUSE-T not found")
}

func mountFuseT(
	bin string,
	mountPoint string,
	conf *mountConfig,
	ready chan<- struct{},
	errp *error,
) (_ *os.File, _ fuseTBackendState, err error) {
	fds, err := syscall.Socketpair(syscall.AF_UNIX, syscall.SOCK_STREAM, 0)
	if err != nil {
		return
	}
	local := fds[0]
	remote := fds[1]

	defer syscall.Close(remote)

	fds, err = syscall.Socketpair(syscall.AF_UNIX, syscall.SOCK_STREAM, 0)
	if err != nil {
		return
	}

	local_mon := fds[0]
	remote_mon := fds[1]

	defer syscall.Close(remote_mon)

	args := []string{
		"--attrcache=false",
	}
	if conf.isReadonly() {
		args = append(args, "-r")
	}
	if conf.fsname() != "" {
		args = append(args, "--volname="+conf.fsname())
	}
	// TODO: apply more args

	remote_file := os.NewFile(uintptr(remote), "")
	remote_mon_file := os.NewFile(uintptr(remote_mon), "")
	local_file := os.NewFile(uintptr(local), "")
	local_mon_file := os.NewFile(uintptr(local_mon), "")

	args = append(args, fmt.Sprintf("--rwsize=%d", maxWrite))
	args = append(args, mountPoint)
	cmd := exec.Command(bin, args...)
	cmd.ExtraFiles = []*os.File{remote_file, remote_mon_file} // fd would be (index + 3)
	cmd.Stderr = nil
	cmd.Stdout = nil
	// daemonize
	cmd.SysProcAttr = &syscall.SysProcAttr{
		Setsid: true,
	}

	envs := []string{}
	envs = append(envs, "_FUSE_COMMFD=3")
	envs = append(envs, "_FUSE_MONFD=4")
	envs = append(envs, "_FUSE_COMMVERS=2")
	cmd.Env = append(os.Environ(), envs...)

	syscall.CloseOnExec(local)
	syscall.CloseOnExec(local_mon)

	if err = cmd.Start(); err != nil {
		return
	}

	cmd.Process.Release()
	go func() {
		var err error
		if _, err = local_mon_file.Write([]byte("mount")); err != nil {
			err = fmt.Errorf("fuse-t failed: %v", err)
		} else {
			reply := make([]byte, 4)
			if _, err = local_mon_file.Read(reply); err != nil {
				err = fmt.Errorf("fuse-t failed: %v", err)
			}
			if !bytes.Equal(reply, []byte{0x0, 0x0, 0x0, 0x0}) {
				err = fmt.Errorf("moint failed")
			}
		}

		*errp = err
		close(ready)
	}()

	return local_file, fuseTBackendState{
		// Prevent these files from being GCed.
		extraFiles: []*os.File{
			local_mon_file,
		},
	}, err
}

func mount(
	mountPoint string,
	conf *mountConfig,
	ready chan<- struct{},
	errp *error,
) (
	f *os.File,
	be Backend,
	bes backendState,
	err error,
) {
	if forcedBackend.IsUnset() || forcedBackend.IsFuseT() {
		var fuseTBin string
		fuseTBin, err = fusetBinary()
		if err == nil {
			f, bes, err = mountFuseT(fuseTBin, mountPoint, conf, ready, errp)
			be = fuseTBackend
			return
		}
		if forcedBackend.IsFuseT() {
			return
		}
	}

	locations := conf.osxfuseLocations
	if locations == nil {
		locations = []OSXFUSEPaths{
			OSXFUSELocationV4,
			OSXFUSELocationV3,
		}
	}

	var binLocation string
	for _, loc := range locations {
		if _, err := os.Stat(loc.Mount); os.IsNotExist(err) {
			// try the other locations
			continue
		}
		binLocation = loc.Mount
		break
	}
	if binLocation == "" {
		err = ErrOSXFUSENotFound
		return
	}

	local, remote, err := unixgramSocketpair()
	if err != nil {
		return
	}

	defer local.Close()
	defer remote.Close()

	cmd := exec.Command(binLocation,
		"-o", conf.getOptions(),

		// 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.
		"-o", "iosize="+strconv.FormatUint(maxWrite, 10),

		mountPoint)

	cmd.ExtraFiles = []*os.File{remote} // fd would be (index + 3)
	cmd.Env = append(os.Environ(),
		"_FUSE_CALL_BY_LIB=",
		"_FUSE_DAEMON_PATH="+os.Args[0],
		"_FUSE_COMMFD=3",
		"_FUSE_COMMVERS=2",
		"MOUNT_OSXFUSE_CALL_BY_LIB=",
		"MOUNT_OSXFUSE_DAEMON_PATH="+os.Args[0])

	var out, errOut bytes.Buffer
	cmd.Stdout = &out
	cmd.Stderr = &errOut

	if err = cmd.Start(); err != nil {
		return
	}

	fd, err := getConnection(local)
	if err != nil {
		return
	}

	go func() {
		// wait inside a goroutine, or otherwise it would block forever for unknown reasons
		if err := cmd.Wait(); err != nil {
			err = fmt.Errorf("mount_osxfusefs failed: %v. Stderr: %s, Stdout: %s",
				err, errOut.String(), out.String())
			*errp = err
		}
		close(ready)
	}()

	dup, err := syscall.Dup(int(fd.Fd()))
	if err != nil {
		return
	}

	syscall.CloseOnExec(int(fd.Fd()))
	syscall.CloseOnExec(dup)

	f = os.NewFile(uintptr(dup), "macfuse")
	be = osxfuseBackend
	bes = nopBackendState{}
	return
}

func unixgramSocketpair() (l, r *os.File, err error) {
	fd, err := syscall.Socketpair(syscall.AF_UNIX, syscall.SOCK_STREAM, 0)
	if err != nil {
		return nil, nil, os.NewSyscallError("socketpair",
			err.(syscall.Errno))
	}
	l = os.NewFile(uintptr(fd[0]), "socketpair-half1")
	r = os.NewFile(uintptr(fd[1]), "socketpair-half2")
	return
}

func getConnection(local *os.File) (*os.File, error) {
	var data [4]byte
	control := make([]byte, 4*256)

	// n, oobn, recvflags, from, errno  - todo: error checking.
	_, oobn, _, _,
		err := syscall.Recvmsg(
		int(local.Fd()), data[:], control[:], 0)
	if err != nil {
		return nil, err
	}

	message := *(*syscall.Cmsghdr)(unsafe.Pointer(&control[0]))
	fd := *(*int32)(unsafe.Pointer(uintptr(unsafe.Pointer(&control[0])) + syscall.SizeofCmsghdr))

	if message.Type != syscall.SCM_RIGHTS {
		return nil, fmt.Errorf("getConnection: recvmsg returned wrong control type: %d", message.Type)
	}
	if oobn <= syscall.SizeofCmsghdr {
		return nil, fmt.Errorf("getConnection: too short control message. Length: %d", oobn)
	}
	if fd < 0 {
		return nil, fmt.Errorf("getConnection: fd < 0: %d", fd)
	}

	return os.NewFile(uintptr(fd), "macfuse"), nil
}