Debian Codegraph @

fs/fstestutil/mountinfo_linux.go

raw symbols SCIP
1 package fstestutil
2
3 import (
4 "errors"
5 "io/ioutil"
6 "strings"
7 )
8
9 // Linux /proc/mounts shows current mounts.
10 // Same format as /etc/fstab. Quoting getmntent(3):
11 //
12 // Since fields in the mtab and fstab files are separated by whitespace,
13 // octal escapes are used to represent the four characters space (\040),
14 // tab (\011), newline (\012) and backslash (\134) in those files when
15 // they occur in one of the four strings in a mntent structure.
16 //
17 // http://linux.die.net/man/3/getmntent
18
19 var fstabUnescape = strings.NewReplacer(
20 `\040`, "\040",
21 `\011`, "\011",
22 `\012`, "\012",
23 `\134`, "\134",
24 )
25
26 var errNotFound = errors.New("mount not found")
27
28 func getMountInfo(mnt string) (*MountInfo, error) {
29 data, err := ioutil.ReadFile("/proc/mounts")
30 if err != nil {
31 return nil, err
32 }
33 for _, line := range strings.Split(string(data), "\n") {
34 fields := strings.Fields(line)
35 if len(fields) < 3 {
36 continue
37 }
38 // Fields are: fsname dir type opts freq passno
39 fsname := fstabUnescape.Replace(fields[0])
40 dir := fstabUnescape.Replace(fields[1])
41 fstype := fstabUnescape.Replace(fields[2])
42 if mnt == dir {
43 info := &MountInfo{
44 FSName: fsname,
45 Type: fstype,
46 }
47 return info, nil
48 }
49 }
50 return nil, errNotFound
51 }