From b83e72acf6948d584742cf6d3b131190142e28c1 Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Thu, 24 Sep 2020 11:46:42 -0700 Subject: [PATCH] mountinfo: make GetMountinfoFromReader Linux-specific ... as no other platform provides something like /proc/self/mountinfo. Signed-off-by: Kir Kolyshkin --- mountinfo/mountinfo.go | 9 --------- mountinfo/mountinfo_linux.go | 12 +++++++++--- mountinfo/mountinfo_linux_test.go | 16 ++++++++-------- mountinfo/mountinfo_unsupported.go | 5 ----- mountinfo/mountinfo_windows.go | 6 ------ 5 files changed, 17 insertions(+), 31 deletions(-) diff --git a/mountinfo/mountinfo.go b/mountinfo/mountinfo.go index 1987fcbb..b8b45269 100644 --- a/mountinfo/mountinfo.go +++ b/mountinfo/mountinfo.go @@ -1,7 +1,6 @@ package mountinfo import ( - "io" "os" ) @@ -11,14 +10,6 @@ func GetMounts(f FilterFunc) ([]*Info, error) { return parseMountTable(f) } -// GetMountsFromReader retrieves a list of mounts from the -// reader provided, with an optional filter applied (use nil -// for no filter). This can be useful in tests or benchmarks -// that provide a fake mountinfo data. -func GetMountsFromReader(reader io.Reader, f FilterFunc) ([]*Info, error) { - return parseInfoFile(reader, f) -} - // Mounted determines if a specified path is a mount point. // // The argument must be an absolute path, with all symlinks resolved, and clean. diff --git a/mountinfo/mountinfo_linux.go b/mountinfo/mountinfo_linux.go index a78799cd..fd4fb341 100644 --- a/mountinfo/mountinfo_linux.go +++ b/mountinfo/mountinfo_linux.go @@ -11,7 +11,13 @@ import ( "strings" ) -func parseInfoFile(r io.Reader, filter FilterFunc) ([]*Info, error) { +// GetMountsFromReader retrieves a list of mounts from the +// reader provided, with an optional filter applied (use nil +// for no filter). This can be useful in tests or benchmarks +// that provide a fake mountinfo data. +// +// This function is Linux-specific. +func GetMountsFromReader(r io.Reader, filter FilterFunc) ([]*Info, error) { s := bufio.NewScanner(r) out := []*Info{} var err error @@ -141,7 +147,7 @@ func parseMountTable(filter FilterFunc) ([]*Info, error) { } defer f.Close() - return parseInfoFile(f, filter) + return GetMountsFromReader(f, filter) } // PidMountInfo collects the mounts for a specific process ID. If the process @@ -154,7 +160,7 @@ func PidMountInfo(pid int) ([]*Info, error) { } defer f.Close() - return parseInfoFile(f, nil) + return GetMountsFromReader(f, nil) } // A few specific characters in mountinfo path entries (root and mountpoint) diff --git a/mountinfo/mountinfo_linux_test.go b/mountinfo/mountinfo_linux_test.go index 868bad90..b77fb571 100644 --- a/mountinfo/mountinfo_linux_test.go +++ b/mountinfo/mountinfo_linux_test.go @@ -428,7 +428,7 @@ const ( func TestParseFedoraMountinfo(t *testing.T) { r := bytes.NewBuffer([]byte(fedoraMountinfo)) - _, err := parseInfoFile(r, nil) + _, err := GetMountsFromReader(r, nil) if err != nil { t.Fatal(err) } @@ -436,7 +436,7 @@ func TestParseFedoraMountinfo(t *testing.T) { func TestParseUbuntuMountinfo(t *testing.T) { r := bytes.NewBuffer([]byte(ubuntuMountinfo)) - _, err := parseInfoFile(r, nil) + _, err := GetMountsFromReader(r, nil) if err != nil { t.Fatal(err) } @@ -444,7 +444,7 @@ func TestParseUbuntuMountinfo(t *testing.T) { func TestParseGentooMountinfo(t *testing.T) { r := bytes.NewBuffer([]byte(gentooMountinfo)) - _, err := parseInfoFile(r, nil) + _, err := GetMountsFromReader(r, nil) if err != nil { t.Fatal(err) } @@ -452,7 +452,7 @@ func TestParseGentooMountinfo(t *testing.T) { func TestParseFedoraMountinfoFields(t *testing.T) { r := bytes.NewBuffer([]byte(fedoraMountinfo)) - infos, err := parseInfoFile(r, nil) + infos, err := GetMountsFromReader(r, nil) if err != nil { t.Fatal(err) } @@ -481,7 +481,7 @@ func TestParseFedoraMountinfoFields(t *testing.T) { func TestParseMountinfoWithSpaces(t *testing.T) { r := bytes.NewBuffer([]byte(mountInfoWithSpaces)) - infos, err := parseInfoFile(r, nil) + infos, err := GetMountsFromReader(r, nil) if err != nil { t.Fatal(err) } @@ -558,7 +558,7 @@ func TestParseMountinfoFilters(t *testing.T) { var r bytes.Reader for _, tc := range cases { r.Reset([]byte(fedoraMountinfo)) - infos, err := parseInfoFile(&r, tc.filter) + infos, err := GetMountsFromReader(&r, tc.filter) if err != nil { t.Error(err) continue @@ -575,7 +575,7 @@ func BenchmarkParseMountinfo(b *testing.B) { b.ResetTimer() for i := 0; i < b.N; i++ { r := bytes.NewReader(buf) - _, err := parseInfoFile(r, PrefixFilter("/sys")) + _, err := GetMountsFromReader(r, PrefixFilter("/sys")) if err != nil { b.Fatal(err) } @@ -642,7 +642,7 @@ func TestParseMountinfoExtraCases(t *testing.T) { for _, tc := range testcases { r := bytes.NewBufferString(tc.entry) - info, err := parseInfoFile(r, nil) + info, err := GetMountsFromReader(r, nil) if !tc.valid { if err == nil { t.Errorf("case %q: expected error, got nil", tc.name) diff --git a/mountinfo/mountinfo_unsupported.go b/mountinfo/mountinfo_unsupported.go index 1eb8558c..b9acfbdc 100644 --- a/mountinfo/mountinfo_unsupported.go +++ b/mountinfo/mountinfo_unsupported.go @@ -4,7 +4,6 @@ package mountinfo import ( "fmt" - "io" "runtime" ) @@ -14,10 +13,6 @@ func parseMountTable(_ FilterFunc) ([]*Info, error) { return nil, errNotImplemented } -func parseInfoFile(_ io.Reader, f FilterFunc) ([]*Info, error) { - return parseMountTable(f) -} - func mounted(path string) (bool, error) { return false, errNotImplemented } diff --git a/mountinfo/mountinfo_windows.go b/mountinfo/mountinfo_windows.go index 5659c1b0..13fad165 100644 --- a/mountinfo/mountinfo_windows.go +++ b/mountinfo/mountinfo_windows.go @@ -1,16 +1,10 @@ package mountinfo -import "io" - func parseMountTable(_ FilterFunc) ([]*Info, error) { // Do NOT return an error! return nil, nil } -func parseInfoFile(_ io.Reader, f FilterFunc) ([]*Info, error) { - return parseMountTable(f) -} - func mounted(_ string) (bool, error) { return false, nil }