diff --git a/internal/engine/inputloader.go b/internal/engine/inputloader.go index d96e06d..5561693 100644 --- a/internal/engine/inputloader.go +++ b/internal/engine/inputloader.go @@ -5,6 +5,7 @@ import ( "context" "errors" "fmt" + "io/fs" "github.com/ooni/probe-cli/v3/internal/engine/internal/fsx" "github.com/ooni/probe-cli/v3/internal/engine/model" @@ -182,7 +183,7 @@ func (il inputLoader) loadLocal() ([]model.URLInfo, error) { } // inputLoaderOpenFn is the type of the function to open a file. -type inputLoaderOpenFn func(filepath string) (fsx.File, error) +type inputLoaderOpenFn func(filepath string) (fs.File, error) // readfile reads inputs from the specified file. The open argument should be // compatibile with stdlib's fs.Open and helps us with unit testing. diff --git a/internal/engine/inputloader_test.go b/internal/engine/inputloader_test.go index 7dbf9b1..e722944 100644 --- a/internal/engine/inputloader_test.go +++ b/internal/engine/inputloader_test.go @@ -4,13 +4,13 @@ import ( "context" "errors" "io" + "io/fs" "os" "syscall" "testing" "github.com/apex/log" "github.com/google/go-cmp/cmp" - "github.com/ooni/probe-cli/v3/internal/engine/internal/fsx" "github.com/ooni/probe-cli/v3/internal/engine/kvstore" "github.com/ooni/probe-cli/v3/internal/engine/model" ) @@ -284,7 +284,7 @@ func TestInputLoaderInputOrQueryBackendWithEmptyFile(t *testing.T) { type InputLoaderBrokenFS struct{} -func (InputLoaderBrokenFS) Open(filepath string) (fsx.File, error) { +func (InputLoaderBrokenFS) Open(filepath string) (fs.File, error) { return InputLoaderBrokenFile{}, nil } diff --git a/internal/engine/internal/fsx/fsx.go b/internal/engine/internal/fsx/fsx.go index 6805ed1..d5a8bf7 100644 --- a/internal/engine/internal/fsx/fsx.go +++ b/internal/engine/internal/fsx/fsx.go @@ -3,31 +3,18 @@ package fsx import ( "fmt" + "io/fs" "os" "syscall" ) -// File is a generic file. This interface is taken from the draft -// iofs golang design. We'll use fs.File when available. -type File interface { - Stat() (os.FileInfo, error) - Read([]byte) (int, error) - Close() error -} - -// FS is a generic file system. Like File, it's adapted from -// the draft iofs golang design document. -type FS interface { - Open(name string) (File, error) -} - // Open is a wrapper for os.Open that ensures that we're opening a file. -func Open(pathname string) (File, error) { +func Open(pathname string) (fs.File, error) { return OpenWithFS(filesystem{}, pathname) } // OpenWithFS is like Open but with explicit file system argument. -func OpenWithFS(fs FS, pathname string) (File, error) { +func OpenWithFS(fs fs.FS, pathname string) (fs.File, error) { file, err := fs.Open(pathname) if err != nil { return nil, err @@ -39,13 +26,16 @@ func OpenWithFS(fs FS, pathname string) (File, error) { } if info.IsDir() { file.Close() - return nil, fmt.Errorf("input path points to a directory: %w", syscall.EISDIR) + return nil, fmt.Errorf( + "input path points to a directory: %w", syscall.EISDIR) } return file, nil } +// filesystem is a private implementation of fs.FS. type filesystem struct{} -func (filesystem) Open(pathname string) (File, error) { +// Open implements fs.FS.Open. +func (filesystem) Open(pathname string) (fs.File, error) { return os.Open(pathname) } diff --git a/internal/engine/internal/fsx/fsx_test.go b/internal/engine/internal/fsx/fsx_test.go index 9555923..e38da71 100644 --- a/internal/engine/internal/fsx/fsx_test.go +++ b/internal/engine/internal/fsx/fsx_test.go @@ -2,6 +2,7 @@ package fsx_test import ( "errors" + "io/fs" "os" "sync/atomic" "syscall" @@ -26,8 +27,8 @@ func (FailingStatFile) Stat() (os.FileInfo, error) { return nil, errStatFailed } -func (fs FailingStatFS) Open(pathname string) (fsx.File, error) { - return FailingStatFile{CloseCount: fs.CloseCount}, nil +func (f FailingStatFS) Open(pathname string) (fs.File, error) { + return FailingStatFile(f), nil } func (fs FailingStatFile) Close() error {