Skip to content

Commit

Permalink
Merge pull request #39 from kolyshkin/mountinfo-from-reader
Browse files Browse the repository at this point in the history
mountinfo: make GetMountinfoFromReader Linux-specific
  • Loading branch information
thaJeztah committed Sep 24, 2020
2 parents 810cc47 + b83e72a commit ee4725f
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 31 deletions.
9 changes: 0 additions & 9 deletions mountinfo/mountinfo.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package mountinfo

import (
"io"
"os"
)

Expand All @@ -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.
Expand Down
12 changes: 9 additions & 3 deletions mountinfo/mountinfo_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
16 changes: 8 additions & 8 deletions mountinfo/mountinfo_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,31 +428,31 @@ 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)
}
}

func TestParseUbuntuMountinfo(t *testing.T) {
r := bytes.NewBuffer([]byte(ubuntuMountinfo))
_, err := parseInfoFile(r, nil)
_, err := GetMountsFromReader(r, nil)
if err != nil {
t.Fatal(err)
}
}

func TestParseGentooMountinfo(t *testing.T) {
r := bytes.NewBuffer([]byte(gentooMountinfo))
_, err := parseInfoFile(r, nil)
_, err := GetMountsFromReader(r, nil)
if err != nil {
t.Fatal(err)
}
}

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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 0 additions & 5 deletions mountinfo/mountinfo_unsupported.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package mountinfo

import (
"fmt"
"io"
"runtime"
)

Expand All @@ -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
}
6 changes: 0 additions & 6 deletions mountinfo/mountinfo_windows.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit ee4725f

Please sign in to comment.