forked from go-git/go-billy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiofs.go
140 lines (120 loc) · 3.71 KB
/
iofs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Package iofs provides an adapter from billy.Filesystem to a the
// standard library io.fs.FS interface.
package iofs
import (
"io"
"io/fs"
"path/filepath"
billyfs "github.com/go-git/go-billy/v6"
"github.com/go-git/go-billy/v6/helper/polyfill"
)
// Wrap adapts a billy.Filesystem to a io.fs.FS.
func New(fs billyfs.Basic) fs.FS {
return &adapterFs{fs: polyfill.New(fs)}
}
type adapterFs struct {
fs billyfs.Filesystem
}
// Type assertion that adapterFS implements the following interfaces:
var _ fs.FS = (*adapterFs)(nil)
var _ fs.ReadDirFS = (*adapterFs)(nil)
var _ fs.StatFS = (*adapterFs)(nil)
var _ fs.ReadFileFS = (*adapterFs)(nil)
// TODO: implement fs.GlobFS, which will be a fair bit more code.
// Open opens the named file on the underlying FS, implementing fs.FS (returning a file or error).
func (a *adapterFs) Open(name string) (fs.File, error) {
if name[0] == '/' || name != filepath.Clean(name) {
// fstest.TestFS explicitly checks that these should return error.
// MemFS performs the clean internally, so we need to block that here for testing purposes.
return nil, &fs.PathError{Op: "open", Path: name, Err: fs.ErrInvalid}
}
stat, err := a.fs.Stat(name)
if err != nil {
return nil, err
}
if stat.IsDir() {
entries, err := a.ReadDir(name)
if err != nil {
return nil, err
}
return makeDir(stat, entries), nil
}
file, err := a.fs.Open(name)
return &adapterFile{file: file, info: stat}, err
}
// ReadDir reads the named directory, implementing fs.ReadDirFS (returning a listing or error).
func (a *adapterFs) ReadDir(name string) ([]fs.DirEntry, error) {
items, err := a.fs.ReadDir(name)
if err != nil {
return nil, err
}
entries := make([]fs.DirEntry, len(items))
for i, item := range items {
entries[i] = fs.FileInfoToDirEntry(item)
}
return entries, nil
}
// Stat returns information on the named file, implementing fs.StatFS (returning FileInfo or error).
func (a *adapterFs) Stat(name string) (fs.FileInfo, error) {
return a.fs.Stat(name)
}
// ReadFile reads the named file and returns its contents, implementing fs.ReadFileFS (returning contents or error).
func (a *adapterFs) ReadFile(name string) ([]byte, error) {
stat, err := a.fs.Stat(name)
if err != nil {
return nil, err
}
b := make([]byte, stat.Size())
file, err := a.Open(name)
if err != nil {
return nil, err
}
defer file.Close()
_, err = file.Read(b)
return b, err
}
type adapterFile struct {
file billyfs.File
info fs.FileInfo
}
var _ fs.File = (*adapterFile)(nil)
// Close closes the file, implementing fs.File (and io.Closer).
func (a *adapterFile) Close() error {
return a.file.Close()
}
// Read reads bytes from the file, implementing fs.File (and io.Reader).
func (a *adapterFile) Read(b []byte) (int, error) {
return a.file.Read(b)
}
// Stat returns file information, implementing fs.File (returning FileInfo or error).
func (a *adapterFile) Stat() (fs.FileInfo, error) {
return a.info, nil
}
type adapterDirFile struct {
adapterFile
entries []fs.DirEntry
}
var _ fs.ReadDirFile = (*adapterDirFile)(nil)
func makeDir(stat fs.FileInfo, entries []fs.DirEntry) *adapterDirFile {
return &adapterDirFile{
adapterFile: adapterFile{info: stat},
entries: entries,
}
}
// Close closes the directory, implementing fs.File (and io.Closer).
// Subtle: note that this is shadowing adapterFile.Close.
func (a *adapterDirFile) Close() error {
return nil
}
// ReadDir reads the directory contents, implementing fs.ReadDirFile (returning directory listing or error).
func (a *adapterDirFile) ReadDir(n int) ([]fs.DirEntry, error) {
if len(a.entries) == 0 && n > 0 {
return nil, io.EOF
}
if n <= 0 || n > len(a.entries) {
n = len(a.entries)
}
entries := a.entries[:n]
a.entries = a.entries[n:]
return entries, nil
}