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
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

@ -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)
}

View file

@ -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 {