From 4d4a0fbea2f0e31d18f51cb05eabd73a00be57db Mon Sep 17 00:00:00 2001 From: madk Date: Tue, 12 Jun 2018 11:20:04 +0800 Subject: [PATCH] change NewNetUnixByPath to NewNetUnixByReader for unix domain parser Signed-off-by: madk --- net_unix.go | 9 ++++++++- net_unix_test.go | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/net_unix.go b/net_unix.go index 59745ea59..fa289f4db 100644 --- a/net_unix.go +++ b/net_unix.go @@ -17,6 +17,7 @@ import ( "bufio" "errors" "fmt" + "io" "os" "strconv" "strings" @@ -103,10 +104,16 @@ func NewNetUnixByPath(path string) (*NetUnix, error) { return nil, err } defer f.Close() + return NewNetUnixByReader(f) +} + +// NewNetUnixByReader returns data read from /proc/net/unix by a reader. +// It might returns an error with partial parsed data, if an error occur after some data parsed. +func NewNetUnixByReader(reader io.Reader) (*NetUnix, error) { nu := &NetUnix{ Rows: make([]*NetUnixLine, 0, 32), } - scanner := bufio.NewScanner(f) + scanner := bufio.NewScanner(reader) // Omit the header line. scanner.Scan() header := scanner.Text() diff --git a/net_unix_test.go b/net_unix_test.go index 7ffb77900..0a2ce19ed 100644 --- a/net_unix_test.go +++ b/net_unix_test.go @@ -14,6 +14,7 @@ package procfs import ( + "os" "testing" ) @@ -118,7 +119,6 @@ func TestNewNetUnixWithoutInode(t *testing.T) { t.Fatal(err) } nu, err := NewNetUnixByPath(fs.proc.Path("net/unix_without_inode")) - if err != nil { t.Fatal(err) }