SysReadFile is a simplified os.ReadFile that invokes syscall.Read directly. https://github.com/prometheus/node_exporter/pull/728/files Note that this function will not read files larger than 128 bytes.
(file string)
| 28 | // |
| 29 | // Note that this function will not read files larger than 128 bytes. |
| 30 | func SysReadFile(file string) (string, error) { |
| 31 | f, err := os.Open(file) |
| 32 | if err != nil { |
| 33 | return "", err |
| 34 | } |
| 35 | defer f.Close() |
| 36 | |
| 37 | // On some machines, hwmon drivers are broken and return EAGAIN. This causes |
| 38 | // Go's os.ReadFile implementation to poll forever. |
| 39 | // |
| 40 | // Since we either want to read data or bail immediately, do the simplest |
| 41 | // possible read using syscall directly. |
| 42 | const sysFileBufferSize = 128 |
| 43 | b := make([]byte, sysFileBufferSize) |
| 44 | n, err := syscall.Read(int(f.Fd()), b) |
| 45 | if err != nil { |
| 46 | return "", err |
| 47 | } |
| 48 | |
| 49 | return string(bytes.TrimSpace(b[:n])), nil |
| 50 | } |
| 51 | |
| 52 | // SysReadUintFromFile reads a file using SysReadFile and attempts to parse a uint64 from it. |
| 53 | func SysReadUintFromFile(path string) (uint64, error) { |
no outgoing calls
no test coverage detected
searching dependent graphs…