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)
| 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. |
| 75 | func 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. |
| 119 | func mountOptionsIsValidField(s string) bool { |
searching dependent graphs…