MCPcopy Create free account
hub / github.com/prometheus/procfs / parseMountInfoString

Function parseMountInfoString

mountinfo.go:75–116  ·  view source on GitHub ↗

Parses a mountinfo file line, and converts it to a MountInfo struct. An important check here is to see if the hyphen separator, as if it does not exist, it means that the line is malformed.

(mountString string)

Source from the content-addressed store, hash-verified

73// An important check here is to see if the hyphen separator, as if it does not exist,
74// it means that the line is malformed.
75func parseMountInfoString(mountString string) (*MountInfo, error) {
76 var err error
77
78 mountInfo := strings.Split(mountString, " ")
79 mountInfoLength := len(mountInfo)
80 if mountInfoLength < 10 {
81 return nil, fmt.Errorf("%w: Too few fields in mount string: %s", ErrFileParse, mountString)
82 }
83
84 if mountInfo[mountInfoLength-4] != "-" {
85 return nil, fmt.Errorf("%w: couldn't find separator in expected field: %s", ErrFileParse, mountInfo[mountInfoLength-4])
86 }
87
88 mount := &MountInfo{
89 MajorMinorVer: mountInfo[2],
90 Root: mountInfo[3],
91 MountPoint: mountInfo[4],
92 Options: mountOptionsParser(mountInfo[5]),
93 OptionalFields: nil,
94 FSType: mountInfo[mountInfoLength-3],
95 Source: mountInfo[mountInfoLength-2],
96 SuperOptions: mountOptionsParser(mountInfo[mountInfoLength-1]),
97 }
98
99 mount.MountID, err = strconv.Atoi(mountInfo[0])
100 if err != nil {
101 return nil, fmt.Errorf("%w: mount ID: %q", ErrFileParse, mount.MountID)
102 }
103 mount.ParentID, err = strconv.Atoi(mountInfo[1])
104 if err != nil {
105 return nil, fmt.Errorf("%w: parent ID: %q", ErrFileParse, mount.ParentID)
106 }
107 // Has optional fields, which is a space separated list of values.
108 // Example: shared:2 master:7
109 if mountInfo[6] != "" {
110 mount.OptionalFields, err = mountOptionsParseOptionalFields(mountInfo[6 : mountInfoLength-4])
111 if err != nil {
112 return nil, fmt.Errorf("%w: %w", ErrFileParse, err)
113 }
114 }
115 return mount, nil
116}
117
118// mountOptionsIsValidField checks a string against a valid list of optional fields keys.
119func mountOptionsIsValidField(s string) bool {

Callers 2

parseMountInfoFunction · 0.85
TestMountInfoFunction · 0.85

Calls 2

mountOptionsParserFunction · 0.85

Tested by 1

TestMountInfoFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…