Skip to content

Commit

Permalink
Make mem.File implement fs.ReadDirFile
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Jul 19, 2022
1 parent 0aa65ed commit 2a70f2b
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 20 deletions.
32 changes: 27 additions & 5 deletions afero_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"bytes"
"fmt"
"io"
iofs "io/fs"
"io/ioutil"
"os"
"path/filepath"
Expand Down Expand Up @@ -530,22 +531,43 @@ func TestReaddirSimple(t *testing.T) {

func TestReaddir(t *testing.T) {
defer removeAllTestFiles(t)
for num := 0; num < 6; num++ {
const nums = 6
for num := 0; num < nums; num++ {
outputs := make([]string, len(Fss))
infos := make([]string, len(Fss))
for i, fs := range Fss {
testSubDir := setupTestDir(t, fs)
//tDir := filepath.Dir(testSubDir)
root, err := fs.Open(testSubDir)
if err != nil {
t.Fatal(err)
}
defer root.Close()

for j := 0; j < 6; j++ {
infosn := make([]string, nums)

for j := 0; j < nums; j++ {
info, err := root.Readdir(num)
outputs[i] += fmt.Sprintf("%v Error: %v\n", myFileInfo(info), err)
infos[i] += fmt.Sprintln(len(info), err)
s := fmt.Sprintln(len(info), err)
infosn[j] = s
infos[i] += s
}
root.Close()

// Also check fs.ReadDirFile interface if implemented
if _, ok := root.(iofs.ReadDirFile); ok {
root, err = fs.Open(testSubDir)
if err != nil {
t.Fatal(err)
}
defer root.Close()

for j := 0; j < nums; j++ {
dirEntries, err := root.(iofs.ReadDirFile).ReadDir(num)
s := fmt.Sprintln(len(dirEntries), err)
if s != infosn[j] {
t.Fatalf("%s: %s != %s", fs.Name(), s, infosn[j])
}
}
}
}

Expand Down
27 changes: 27 additions & 0 deletions internal/common/adapters.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright © 2022 Steve Francia <spf@spf13.com>.
//
// 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 }
17 changes: 4 additions & 13 deletions iofs.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"path"
"sort"
"time"

"github.com/spf13/afero/internal/common"
)

// IOFS adopts afero.Fs to stdlib io/fs.FS
Expand Down Expand Up @@ -92,7 +94,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
Expand Down Expand Up @@ -127,17 +129,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
Expand All @@ -153,7 +144,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
Expand Down
6 changes: 4 additions & 2 deletions iofs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"testing"
"testing/fstest"
"time"

"github.com/spf13/afero/internal/common"
)

func TestIOFS(t *testing.T) {
Expand Down Expand Up @@ -105,7 +107,7 @@ func TestIOFSNativeDirEntryWhenPossible(t *testing.T) {
t.Fatalf("expected %d, got %d", numFiles, len(entries))
}
for i, entry := range entries {
if _, ok := entry.(dirEntry); ok {
if _, ok := entry.(common.FileInfoDirEntry); ok {
t.Fatal("DirEntry not native")
}
if ordered && entry.Name() != fmt.Sprintf("test%d.txt", i) {
Expand Down Expand Up @@ -138,7 +140,7 @@ func TestIOFSNativeDirEntryWhenPossible(t *testing.T) {
fileCount++
}

if _, ok := d.(dirEntry); ok {
if _, ok := d.(common.FileInfoDirEntry); ok {
t.Fatal("DirEntry not native")
}

Expand Down
18 changes: 18 additions & 0 deletions mem/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 2a70f2b

Please sign in to comment.