fix(internal/fsx): remove pre Go 1.16 definitions (#270)

Occurred to me while working on https://github.com/ooni/probe/issues/1299.
This commit is contained in:
Simone Basso 2021-03-29 19:37:32 +02:00 committed by GitHub
parent 87e5234586
commit 1da64f6d9f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 15 additions and 23 deletions

View File

@ -5,6 +5,7 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"io/fs"
"github.com/ooni/probe-cli/v3/internal/engine/internal/fsx" "github.com/ooni/probe-cli/v3/internal/engine/internal/fsx"
"github.com/ooni/probe-cli/v3/internal/engine/model" "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. // 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 // readfile reads inputs from the specified file. The open argument should be
// compatibile with stdlib's fs.Open and helps us with unit testing. // compatibile with stdlib's fs.Open and helps us with unit testing.

View File

@ -4,13 +4,13 @@ import (
"context" "context"
"errors" "errors"
"io" "io"
"io/fs"
"os" "os"
"syscall" "syscall"
"testing" "testing"
"github.com/apex/log" "github.com/apex/log"
"github.com/google/go-cmp/cmp" "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/kvstore"
"github.com/ooni/probe-cli/v3/internal/engine/model" "github.com/ooni/probe-cli/v3/internal/engine/model"
) )
@ -284,7 +284,7 @@ func TestInputLoaderInputOrQueryBackendWithEmptyFile(t *testing.T) {
type InputLoaderBrokenFS struct{} type InputLoaderBrokenFS struct{}
func (InputLoaderBrokenFS) Open(filepath string) (fsx.File, error) { func (InputLoaderBrokenFS) Open(filepath string) (fs.File, error) {
return InputLoaderBrokenFile{}, nil return InputLoaderBrokenFile{}, nil
} }

View File

@ -3,31 +3,18 @@ package fsx
import ( import (
"fmt" "fmt"
"io/fs"
"os" "os"
"syscall" "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. // 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) return OpenWithFS(filesystem{}, pathname)
} }
// OpenWithFS is like Open but with explicit file system argument. // 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) file, err := fs.Open(pathname)
if err != nil { if err != nil {
return nil, err return nil, err
@ -39,13 +26,16 @@ func OpenWithFS(fs FS, pathname string) (File, error) {
} }
if info.IsDir() { if info.IsDir() {
file.Close() 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 return file, nil
} }
// filesystem is a private implementation of fs.FS.
type filesystem struct{} 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) return os.Open(pathname)
} }

View File

@ -2,6 +2,7 @@ package fsx_test
import ( import (
"errors" "errors"
"io/fs"
"os" "os"
"sync/atomic" "sync/atomic"
"syscall" "syscall"
@ -26,8 +27,8 @@ func (FailingStatFile) Stat() (os.FileInfo, error) {
return nil, errStatFailed return nil, errStatFailed
} }
func (fs FailingStatFS) Open(pathname string) (fsx.File, error) { func (f FailingStatFS) Open(pathname string) (fs.File, error) {
return FailingStatFile{CloseCount: fs.CloseCount}, nil return FailingStatFile(f), nil
} }
func (fs FailingStatFile) Close() error { func (fs FailingStatFile) Close() error {