Skip to content

Commit 4289a4e

Browse files
authored
Merge pull request #156 from pjbgf/goroot
Align ReadDir interface with upstream `io/fs.ReadDirFS`
2 parents d6fa0e9 + 5ec8e72 commit 4289a4e

File tree

17 files changed

+177
-104
lines changed

17 files changed

+177
-104
lines changed

embedfs/embed.go

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@ import (
88
"io/fs"
99
"os"
1010
"path/filepath"
11-
"sort"
1211
"strings"
1312
"sync"
1413

1514
"github.com/go-git/go-billy/v6"
16-
"github.com/go-git/go-billy/v6/memfs"
1715
)
1816

1917
type Embed struct {
@@ -90,21 +88,8 @@ func (fs *Embed) Join(elem ...string) string {
9088
return ""
9189
}
9290

93-
func (fs *Embed) ReadDir(path string) ([]os.FileInfo, error) {
94-
e, err := fs.underlying.ReadDir(path)
95-
if err != nil {
96-
return nil, err
97-
}
98-
99-
entries := make([]os.FileInfo, 0, len(e))
100-
for _, f := range e {
101-
fi, _ := f.Info()
102-
entries = append(entries, fi)
103-
}
104-
105-
sort.Sort(memfs.ByName(entries))
106-
107-
return entries, nil
91+
func (fs *Embed) ReadDir(path string) ([]fs.DirEntry, error) {
92+
return fs.underlying.ReadDir(path)
10893
}
10994

11095
// Chroot is not supported.
@@ -219,16 +204,6 @@ func (f *file) Close() error {
219204
return nil
220205
}
221206

222-
// Lock for embedfs file is a no-op.
223-
func (f *file) Lock() error {
224-
return nil
225-
}
226-
227-
// Unlock for embedfs file is a no-op.
228-
func (f *file) Unlock() error {
229-
return nil
230-
}
231-
232207
// Truncate is not supported.
233208
//
234209
// Calls will always return billy.ErrReadOnly.

fs.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ type TempFile interface {
109109
type Dir interface {
110110
// ReadDir reads the directory named by dirname and returns a list of
111111
// directory entries sorted by filename.
112-
ReadDir(path string) ([]fs.FileInfo, error)
112+
ReadDir(path string) ([]fs.DirEntry, error)
113113
// MkdirAll creates a directory named path, along with any necessary
114114
// parents, and returns nil, or else returns an error. The permission bits
115115
// perm are used for all directories that MkdirAll creates. If path is/
@@ -173,13 +173,18 @@ type File interface {
173173
io.WriterAt
174174
io.ReaderAt
175175
io.Seeker
176+
// Truncate the file.
177+
Truncate(size int64) error
178+
}
179+
180+
// Locker abstracts the lock and unlock of a File within the filesystem. Not
181+
// all filesystem implementations support it.
182+
type Locker interface {
176183
// Lock locks the file like e.g. flock. It protects against access from
177184
// other processes.
178185
Lock() error
179186
// Unlock unlocks the file.
180187
Unlock() error
181-
// Truncate the file.
182-
Truncate(size int64) error
183188
}
184189

185190
// Syncer interface can be implemented by filesystems that support syncing.

helper/chroot/chroot.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func (fs *ChrootHelper) TempFile(dir, prefix string) (billy.File, error) {
134134
return newFile(fs, f, fs.Join(dir, filepath.Base(f.Name()))), nil
135135
}
136136

137-
func (fs *ChrootHelper) ReadDir(path string) ([]os.FileInfo, error) {
137+
func (fs *ChrootHelper) ReadDir(path string) ([]fs.DirEntry, error) {
138138
fullpath, err := fs.underlyingPath(path)
139139
if err != nil {
140140
return nil, err

helper/iofs/iofs.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,7 @@ func (a *adapterFs) Open(name string) (fs.File, error) {
5252

5353
// ReadDir reads the named directory, implementing fs.ReadDirFS (returning a listing or error).
5454
func (a *adapterFs) ReadDir(name string) ([]fs.DirEntry, error) {
55-
items, err := a.fs.ReadDir(name)
56-
if err != nil {
57-
return nil, err
58-
}
59-
entries := make([]fs.DirEntry, len(items))
60-
for i, item := range items {
61-
entries[i] = fs.FileInfoToDirEntry(item)
62-
}
63-
return entries, nil
55+
return a.fs.ReadDir(name)
6456
}
6557

6658
// Stat returns information on the named file, implementing fs.StatFS (returning FileInfo or error).

helper/mount/mount.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func (h *Mount) Remove(path string) error {
110110
return fs.Remove(fullpath)
111111
}
112112

113-
func (h *Mount) ReadDir(path string) ([]os.FileInfo, error) {
113+
func (h *Mount) ReadDir(path string) ([]fs.DirEntry, error) {
114114
fs, fullpath, err := h.getDirAndPath(path)
115115
if err != nil {
116116
return nil, err

helper/polyfill/polyfill.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func (h *Polyfill) TempFile(dir, prefix string) (billy.File, error) {
4040
return h.Basic.(billy.TempFile).TempFile(dir, prefix)
4141
}
4242

43-
func (h *Polyfill) ReadDir(path string) ([]os.FileInfo, error) {
43+
func (h *Polyfill) ReadDir(path string) ([]fs.DirEntry, error) {
4444
if !h.c.dir {
4545
return nil, billy.ErrNotSupported
4646
}

internal/test/mock.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ type DirMock struct {
8080
MkdirAllArgs [][2]interface{}
8181
}
8282

83-
func (fs *DirMock) ReadDir(path string) ([]os.FileInfo, error) {
83+
func (fs *DirMock) ReadDir(path string) ([]fs.DirEntry, error) {
8484
fs.ReadDirArgs = append(fs.ReadDirArgs, path)
8585
return nil, nil
8686
}

memfs/file.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -133,16 +133,6 @@ func (f *file) Stat() (os.FileInfo, error) {
133133
}, nil
134134
}
135135

136-
// Lock is a no-op in memfs.
137-
func (f *file) Lock() error {
138-
return nil
139-
}
140-
141-
// Unlock is a no-op in memfs.
142-
func (f *file) Unlock() error {
143-
return nil
144-
}
145-
146136
type fileInfo struct {
147137
name string
148138
size int

memfs/memory.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ package memfs // import "github.com/go-git/go-billy/v6/memfs"
44
import (
55
"errors"
66
"fmt"
7-
"io/fs"
7+
gofs "io/fs"
88
"log"
99
"os"
1010
"path/filepath"
@@ -49,7 +49,7 @@ func (fs *Memory) Open(filename string) (billy.File, error) {
4949
return fs.OpenFile(filename, os.O_RDONLY, 0)
5050
}
5151

52-
func (fs *Memory) OpenFile(filename string, flag int, perm fs.FileMode) (billy.File, error) {
52+
func (fs *Memory) OpenFile(filename string, flag int, perm gofs.FileMode) (billy.File, error) {
5353
f, has := fs.s.Get(filename)
5454
if !has {
5555
if !isCreate(flag) {
@@ -132,13 +132,13 @@ func (fs *Memory) Lstat(filename string) (os.FileInfo, error) {
132132
return f.Stat()
133133
}
134134

135-
type ByName []os.FileInfo
135+
type ByName []gofs.DirEntry
136136

137137
func (a ByName) Len() int { return len(a) }
138138
func (a ByName) Less(i, j int) bool { return a[i].Name() < a[j].Name() }
139139
func (a ByName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
140140

141-
func (fs *Memory) ReadDir(path string) ([]os.FileInfo, error) {
141+
func (fs *Memory) ReadDir(path string) ([]gofs.DirEntry, error) {
142142
if f, has := fs.s.Get(path); has {
143143
if target, isLink := fs.resolveLink(path, f); isLink {
144144
if target != path {
@@ -149,18 +149,18 @@ func (fs *Memory) ReadDir(path string) ([]os.FileInfo, error) {
149149
return nil, &os.PathError{Op: "open", Path: path, Err: syscall.ENOENT}
150150
}
151151

152-
var entries []os.FileInfo
152+
var entries []gofs.DirEntry
153153
for _, f := range fs.s.Children(path) {
154154
fi, _ := f.Stat()
155-
entries = append(entries, fi)
155+
entries = append(entries, gofs.FileInfoToDirEntry(fi))
156156
}
157157

158158
sort.Sort(ByName(entries))
159159

160160
return entries, nil
161161
}
162162

163-
func (fs *Memory) MkdirAll(path string, perm fs.FileMode) error {
163+
func (fs *Memory) MkdirAll(path string, perm gofs.FileMode) error {
164164
_, err := fs.s.New(path, perm|os.ModeDir, 0)
165165
return err
166166
}
@@ -258,6 +258,6 @@ func isWriteOnly(flag int) bool {
258258
return flag&os.O_WRONLY != 0
259259
}
260260

261-
func isSymlink(m fs.FileMode) bool {
261+
func isSymlink(m gofs.FileMode) bool {
262262
return m&os.ModeSymlink != 0
263263
}

osfs/os.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -79,22 +79,6 @@ const (
7979
BoundOSFS
8080
)
8181

82-
func readDir(dir string) ([]os.FileInfo, error) {
83-
entries, err := os.ReadDir(dir)
84-
if err != nil {
85-
return nil, err
86-
}
87-
infos := make([]fs.FileInfo, 0, len(entries))
88-
for _, entry := range entries {
89-
fi, err := entry.Info()
90-
if err != nil {
91-
return nil, err
92-
}
93-
infos = append(infos, fi)
94-
}
95-
return infos, nil
96-
}
97-
9882
func tempFile(dir, prefix string) (billy.File, error) {
9983
f, err := os.CreateTemp(dir, prefix)
10084
if err != nil {

0 commit comments

Comments
 (0)