parseNetUNIX creates a NetUnix structure from the incoming stream.
(r io.Reader)
| 88 | |
| 89 | // parseNetUNIX creates a NetUnix structure from the incoming stream. |
| 90 | func parseNetUNIX(r io.Reader) (*NetUNIX, error) { |
| 91 | // Begin scanning by checking for the existence of Inode. |
| 92 | s := bufio.NewScanner(r) |
| 93 | s.Scan() |
| 94 | |
| 95 | // From the man page of proc(5), it does not contain an Inode field, |
| 96 | // but in actually it exists. This code works for both cases. |
| 97 | hasInode := strings.Contains(s.Text(), "Inode") |
| 98 | |
| 99 | // Expect a minimum number of fields, but Inode and Path are optional: |
| 100 | // Num RefCount Protocol Flags Type St Inode Path |
| 101 | minFields := 6 |
| 102 | if hasInode { |
| 103 | minFields++ |
| 104 | } |
| 105 | |
| 106 | var nu NetUNIX |
| 107 | for s.Scan() { |
| 108 | line := s.Text() |
| 109 | item, err := nu.parseLine(line, hasInode, minFields) |
| 110 | if err != nil { |
| 111 | return nil, fmt.Errorf("%w: /proc/net/unix encountered data %q: %w", ErrFileParse, line, err) |
| 112 | } |
| 113 | |
| 114 | nu.Rows = append(nu.Rows, item) |
| 115 | } |
| 116 | |
| 117 | if err := s.Err(); err != nil { |
| 118 | return nil, fmt.Errorf("%w: /proc/net/unix encountered data: %w", ErrFileParse, err) |
| 119 | } |
| 120 | |
| 121 | return &nu, nil |
| 122 | } |
| 123 | |
| 124 | func (u *NetUNIX) parseLine(line string, hasInode bool, minFields int) (*NetUNIXLine, error) { |
| 125 | fields := strings.Fields(line) |
no test coverage detected