forked from moby/moby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mountinfo.go
56 lines (48 loc) · 1.07 KB
/
mountinfo.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
54
55
56
package mount
import (
"bufio"
"fmt"
"io"
"os"
)
const (
// We only parse upto the mountinfo because that is all we
// care about right now
mountinfoFormat = "%d %d %d:%d %s %s %s"
)
// Represents one line from /proc/self/mountinfo
type procEntry struct {
id, parent, major, minor int
source, mountpoint, opts string
}
// Parse /proc/self/mountinfo because comparing Dev and ino does not work from bind mounts
func parseMountTable() ([]*procEntry, error) {
f, err := os.Open("/proc/self/mountinfo")
if err != nil {
return nil, err
}
defer f.Close()
return parseInfoFile(f)
}
func parseInfoFile(r io.Reader) ([]*procEntry, error) {
var (
s = bufio.NewScanner(r)
out = []*procEntry{}
)
for s.Scan() {
if err := s.Err(); err != nil {
return nil, err
}
var (
p = &procEntry{}
text = s.Text()
)
if _, err := fmt.Sscanf(text, mountinfoFormat,
&p.id, &p.parent, &p.major, &p.minor,
&p.source, &p.mountpoint, &p.opts); err != nil {
return nil, fmt.Errorf("Scanning '%s' failed: %s", text, err)
}
out = append(out, p)
}
return out, nil
}