mirror of
https://github.com/weaveworks/scope.git
synced 2026-07-20 22:10:30 +00:00
Review feedback
This commit is contained in:
@@ -7,8 +7,8 @@ import (
|
||||
"syscall"
|
||||
)
|
||||
|
||||
// T is the filesystem interface type.
|
||||
type T interface {
|
||||
// Interface is the filesystem interface type.
|
||||
type Interface interface {
|
||||
ReadDir(string) ([]os.FileInfo, error)
|
||||
ReadFile(string) ([]byte, error)
|
||||
Lstat(string, *syscall.Stat_t) error
|
||||
@@ -19,17 +19,7 @@ type T interface {
|
||||
type realFS struct{}
|
||||
|
||||
// FS is the way you should access the filesystem.
|
||||
var FS T = realFS{}
|
||||
|
||||
// Mock is used to switch out the filesystem for a mock.
|
||||
func Mock(fs T) {
|
||||
FS = fs
|
||||
}
|
||||
|
||||
// Restore puts back the real filesystem.
|
||||
func Restore() {
|
||||
FS = realFS{}
|
||||
}
|
||||
var fs Interface = realFS{}
|
||||
|
||||
func (realFS) ReadDir(path string) ([]os.FileInfo, error) {
|
||||
return ioutil.ReadDir(path)
|
||||
@@ -50,3 +40,40 @@ func (realFS) Stat(path string, stat *syscall.Stat_t) error {
|
||||
func (realFS) Open(path string) (io.ReadWriteCloser, error) {
|
||||
return os.Open(path)
|
||||
}
|
||||
|
||||
// trampolines here to allow users to do fs.ReadDir etc
|
||||
|
||||
// ReadDir see ioutil.ReadDir
|
||||
func ReadDir(path string) ([]os.FileInfo, error) {
|
||||
return fs.ReadDir(path)
|
||||
}
|
||||
|
||||
// ReadFile see ioutil.ReadFile
|
||||
func ReadFile(path string) ([]byte, error) {
|
||||
return fs.ReadFile(path)
|
||||
}
|
||||
|
||||
// Lstat see syscall.Lstat
|
||||
func Lstat(path string, stat *syscall.Stat_t) error {
|
||||
return fs.Lstat(path, stat)
|
||||
}
|
||||
|
||||
// Stat see syscall.Stat
|
||||
func Stat(path string, stat *syscall.Stat_t) error {
|
||||
return fs.Stat(path, stat)
|
||||
}
|
||||
|
||||
// Open see os.Open
|
||||
func Open(path string) (io.ReadWriteCloser, error) {
|
||||
return fs.Open(path)
|
||||
}
|
||||
|
||||
// Mock is used to switch out the filesystem for a mock.
|
||||
func Mock(mock Interface) {
|
||||
fs = mock
|
||||
}
|
||||
|
||||
// Restore puts back the real filesystem.
|
||||
func Restore() {
|
||||
fs = realFS{}
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@ func walkProcPid(buf *bytes.Buffer, walker process.Walker) (map[uint64]Proc, err
|
||||
walker.Walk(func(p process.Process) {
|
||||
dirName := strconv.Itoa(p.PID)
|
||||
fdBase := filepath.Join(procRoot, dirName, "fd")
|
||||
fds, err := fs.FS.ReadDir(fdBase)
|
||||
fds, err := fs.ReadDir(fdBase)
|
||||
if err != nil {
|
||||
// Process is be gone by now, or we don't have access.
|
||||
return
|
||||
@@ -42,7 +42,7 @@ func walkProcPid(buf *bytes.Buffer, walker process.Walker) (map[uint64]Proc, err
|
||||
|
||||
// Read network namespace, and if we haven't seen it before,
|
||||
// read /proc/<pid>/net/tcp
|
||||
err = fs.FS.Lstat(filepath.Join(procRoot, dirName, "/ns/net"), &statT)
|
||||
err = fs.Lstat(filepath.Join(procRoot, dirName, "/ns/net"), &statT)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -55,7 +55,7 @@ func walkProcPid(buf *bytes.Buffer, walker process.Walker) (map[uint64]Proc, err
|
||||
|
||||
for _, fd := range fds {
|
||||
// Direct use of syscall.Stat() to save garbage.
|
||||
err = fs.FS.Stat(filepath.Join(fdBase, fd.Name()), &statT)
|
||||
err = fs.Stat(filepath.Join(fdBase, fd.Name()), &statT)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
@@ -79,7 +79,7 @@ func walkProcPid(buf *bytes.Buffer, walker process.Walker) (map[uint64]Proc, err
|
||||
// be overwritten for benchmarks. That's bad practice and we should change it
|
||||
// to be a dependency.
|
||||
var readFile = func(filename string, buf *bytes.Buffer) error {
|
||||
f, err := fs.FS.Open(filename)
|
||||
f, err := fs.Open(filename)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ func NewWalker(procRoot string) Walker {
|
||||
// passes one-by-one to the supplied function. Walk is only made public
|
||||
// so that is can be tested.
|
||||
func (w *walker) Walk(f func(Process)) error {
|
||||
dirEntries, err := fs.FS.ReadDir(w.procRoot)
|
||||
dirEntries, err := fs.ReadDir(w.procRoot)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -35,7 +35,7 @@ func (w *walker) Walk(f func(Process)) error {
|
||||
continue
|
||||
}
|
||||
|
||||
stat, err := fs.FS.ReadFile(path.Join(w.procRoot, filename, "stat"))
|
||||
stat, err := fs.ReadFile(path.Join(w.procRoot, filename, "stat"))
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
@@ -51,13 +51,13 @@ func (w *walker) Walk(f func(Process)) error {
|
||||
}
|
||||
|
||||
cmdline := ""
|
||||
if cmdlineBuf, err := fs.FS.ReadFile(path.Join(w.procRoot, filename, "cmdline")); err == nil {
|
||||
if cmdlineBuf, err := fs.ReadFile(path.Join(w.procRoot, filename, "cmdline")); err == nil {
|
||||
cmdlineBuf = bytes.Replace(cmdlineBuf, []byte{'\000'}, []byte{' '}, -1)
|
||||
cmdline = string(cmdlineBuf)
|
||||
}
|
||||
|
||||
comm := "(unknown)"
|
||||
if commBuf, err := fs.FS.ReadFile(path.Join(w.procRoot, filename, "comm")); err == nil {
|
||||
if commBuf, err := fs.ReadFile(path.Join(w.procRoot, filename, "comm")); err == nil {
|
||||
comm = strings.TrimSpace(string(commBuf))
|
||||
}
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@ type File struct {
|
||||
// Entry is an entry in the mock filesystem
|
||||
type Entry interface {
|
||||
os.FileInfo
|
||||
fs.T
|
||||
fs.Interface
|
||||
}
|
||||
|
||||
// Dir creates a new directory with the given entries.
|
||||
|
||||
Reference in New Issue
Block a user