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

Function SysReadFile

internal/util/sysreadfile.go:30–50  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

28//
29// Note that this function will not read files larger than 128 bytes.
30func 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.
53func SysReadUintFromFile(path string) (uint64, error) {

Callers 2

SysReadUintFromFileFunction · 0.70
SysReadIntFromFileFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…