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 18, 2022
1 parent b0a534a commit ea59535
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 15 deletions.
27 changes: 27 additions & 0 deletions internal/common/adapters.go
@@ -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
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 @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 4 additions & 2 deletions iofs_test.go
Expand Up @@ -15,6 +15,8 @@ import (
"testing"
"testing/fstest"
"time"

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

func TestIOFS(t *testing.T) {
Expand Down Expand Up @@ -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")
}
}
Expand All @@ -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")
}

Expand Down
18 changes: 18 additions & 0 deletions mem/file.go
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 ea59535

Please sign in to comment.