File: mountinfo_linux.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 (56 lines) | stat: -rw-r--r-- 1,428 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
package fstestutil

import (
	"errors"
	"io/ioutil"
	"strings"
	"time"
)

// Linux /proc/mounts shows current mounts.
// Same format as /etc/fstab. Quoting getmntent(3):
//
// Since fields in the mtab and fstab files are separated by whitespace,
// octal escapes are used to represent the four characters space (\040),
// tab (\011), newline (\012) and backslash (\134) in those files when
// they occur in one of the four strings in a mntent structure.
//
// http://linux.die.net/man/3/getmntent

var fstabUnescape = strings.NewReplacer(
	`\040`, "\040",
	`\011`, "\011",
	`\012`, "\012",
	`\134`, "\134",
)

var errNotFound = errors.New("mount not found")

func getMountInfo(mnt string) (*MountInfo, error) {
	// TODO delay a little to minimize an undiagnosed race between
	// fuse.Conn.Ready and /proc/mounts
	// https://github.com/bazil/fuse/issues/228
	time.Sleep(10 * time.Millisecond)
	data, err := ioutil.ReadFile("/proc/mounts")
	if err != nil {
		return nil, err
	}
	for _, line := range strings.Split(string(data), "\n") {
		fields := strings.Fields(line)
		if len(fields) < 3 {
			continue
		}
		// Fields are: fsname dir type opts freq passno
		fsname := fstabUnescape.Replace(fields[0])
		dir := fstabUnescape.Replace(fields[1])
		fstype := fstabUnescape.Replace(fields[2])
		if mnt == dir {
			info := &MountInfo{
				FSName: fsname,
				Type:   fstype,
			}
			return info, nil
		}
	}
	return nil, errNotFound
}