From ea595359c578b22bbea013cfc798d776a83109d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Fri, 15 Jul 2022 12:27:01 +0200 Subject: [PATCH] Make mem.File implement fs.ReadDirFile --- internal/common/adapters.go | 27 +++++++++++++++++++++++++++ iofs.go | 17 ++++------------- iofs_test.go | 6 ++++-- mem/file.go | 18 ++++++++++++++++++ 4 files changed, 53 insertions(+), 15 deletions(-) create mode 100644 internal/common/adapters.go diff --git a/internal/common/adapters.go b/internal/common/adapters.go new file mode 100644 index 00000000..60685caa --- /dev/null +++ b/internal/common/adapters.go @@ -0,0 +1,27 @@ +// Copyright © 2022 Steve Francia . +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package common + +import "io/fs" + +// FileInfoDirEntry provides an adapter from os.FileInfo to fs.DirEntry +type FileInfoDirEntry struct { + fs.FileInfo +} + +var _ fs.DirEntry = FileInfoDirEntry{} + +func (d FileInfoDirEntry) Type() fs.FileMode { return d.FileInfo.Mode().Type() } + +func (d FileInfoDirEntry) Info() (fs.FileInfo, error) { return d.FileInfo, nil } diff --git a/iofs.go b/iofs.go index 8bc97354..0ba1d4c1 100644 --- a/iofs.go +++ b/iofs.go @@ -10,6 +10,8 @@ import ( "path" "sort" "time" + + "github.com/spf13/afero/internal/common" ) // IOFS adopts afero.Fs to stdlib io/fs.FS @@ -87,7 +89,7 @@ func (iofs IOFS) ReadDir(name string) ([]fs.DirEntry, error) { ret := make([]fs.DirEntry, len(items)) for i := range items { - ret[i] = dirEntry{items[i]} + ret[i] = common.FileInfoDirEntry{FileInfo: items[i]} } return ret, nil @@ -122,17 +124,6 @@ func (IOFS) wrapError(op, path string, err error) error { } } -// dirEntry provides adapter from os.FileInfo to fs.DirEntry -type dirEntry struct { - fs.FileInfo -} - -var _ fs.DirEntry = dirEntry{} - -func (d dirEntry) Type() fs.FileMode { return d.FileInfo.Mode().Type() } - -func (d dirEntry) Info() (fs.FileInfo, error) { return d.FileInfo, nil } - // readDirFile provides adapter from afero.File to fs.ReadDirFile needed for correct Open type readDirFile struct { File @@ -148,7 +139,7 @@ func (r readDirFile) ReadDir(n int) ([]fs.DirEntry, error) { ret := make([]fs.DirEntry, len(items)) for i := range items { - ret[i] = dirEntry{items[i]} + ret[i] = common.FileInfoDirEntry{FileInfo: items[i]} } return ret, nil diff --git a/iofs_test.go b/iofs_test.go index 32f0d9d8..279c0143 100644 --- a/iofs_test.go +++ b/iofs_test.go @@ -15,6 +15,8 @@ import ( "testing" "testing/fstest" "time" + + "github.com/spf13/afero/internal/common" ) func TestIOFS(t *testing.T) { @@ -94,7 +96,7 @@ func TestIOFSNativeDirEntryWhenPossible(t *testing.T) { t.Fatalf("expected 2, got %d", len(entries)) } for _, entry := range entries { - if _, ok := entry.(dirEntry); ok { + if _, ok := entry.(common.FileInfoDirEntry); ok { t.Fatal("DirEntry not native") } } @@ -118,7 +120,7 @@ func TestIOFSNativeDirEntryWhenPossible(t *testing.T) { fileCount++ } - if _, ok := d.(dirEntry); ok { + if _, ok := d.(common.FileInfoDirEntry); ok { t.Fatal("DirEntry not native") } diff --git a/mem/file.go b/mem/file.go index 258880bf..3cf4693b 100644 --- a/mem/file.go +++ b/mem/file.go @@ -18,15 +18,20 @@ import ( "bytes" "errors" "io" + "io/fs" "os" "path/filepath" "sync" "sync/atomic" "time" + + "github.com/spf13/afero/internal/common" ) const FilePathSeparator = string(filepath.Separator) +var _ fs.ReadDirFile = &File{} + type File struct { // atomic requires 64-bit alignment for struct field access at int64 @@ -183,6 +188,19 @@ func (f *File) Readdirnames(n int) (names []string, err error) { return names, err } +// Implements fs.ReadDirFile +func (f *File) ReadDir(n int) ([]fs.DirEntry, error) { + fi, err := f.Readdir(n) + if err != nil { + return nil, err + } + di := make([]fs.DirEntry, len(fi)) + for i, f := range fi { + di[i] = common.FileInfoDirEntry{FileInfo: f} + } + return di, nil +} + func (f *File) Read(b []byte) (n int, err error) { f.fileData.Lock() defer f.fileData.Unlock()