Debian Codegraph @

helper/iofs/iofs.go

raw symbols SCIP
1 // Package iofs provides an adapter from billy.Filesystem to a the
2 // standard library io.fs.FS interface.
3 package iofs
4
5 import (
6 "io"
7 "io/fs"
8 "strings"
9
10 billyfs "github.com/go-git/go-billy/v5"
11 "github.com/go-git/go-billy/v5/helper/polyfill"
12 )
13
14 // Wrap adapts a billy.Filesystem to a io.fs.FS.
15 func New(fs billyfs.Basic) fs.FS {
16 return &adapterFs{fs: polyfill.New(fs)}
17 }
18
19 // NewStatFS adapts a billy.Filesystem to a io.fs.StatFS.
20 func NewStatFS(fs billyfs.Basic) fs.StatFS {
21 return &adapterFs{fs: polyfill.New(fs)}
22 }
23
24 // NewReadDirFS adapts a billy.Filesystem to a io.fs.ReadDirFS.
25 func NewReadDirFS(fs billyfs.Basic) fs.ReadDirFS {
26 return &adapterFs{fs: polyfill.New(fs)}
27 }
28
29 // NewReadFileFS adapts a billy.Filesystem to a io.fs.ReadFileFS.
30 func NewReadFileFS(fs billyfs.Basic) fs.ReadFileFS {
31 return &adapterFs{fs: polyfill.New(fs)}
32 }
33
34 type adapterFs struct {
35 fs billyfs.Filesystem
36 }
37
38 // Type assertion that adapterFS implements the following interfaces:
39 var _ fs.FS = (*adapterFs)(nil)
40 var _ fs.ReadDirFS = (*adapterFs)(nil)
41 var _ fs.StatFS = (*adapterFs)(nil)
42 var _ fs.ReadFileFS = (*adapterFs)(nil)
43
44 // TODO: implement fs.GlobFS, which will be a fair bit more code.
45
46 // validPath checks that name is a valid io/fs path. In addition to the
47 // standard fs.ValidPath checks, it rejects backslashes which on Windows
48 // would be interpreted as path separators by the underlying billy filesystem.
49 func validPath(name string) bool {
50 return fs.ValidPath(name) && !strings.Contains(name, `\`)
51 }
52
53 // Open opens the named file on the underlying FS, implementing fs.FS (returning a file or error).
54 func (a *adapterFs) Open(name string) (fs.File, error) {
55 if !validPath(name) {
56 // fstest.TestFS explicitly checks that these should return error.
57 // MemFS performs the clean internally, so we need to block that here for testing purposes.
58 return nil, &fs.PathError{Op: "open", Path: name, Err: fs.ErrInvalid}
59 }
60 stat, err := a.fs.Stat(name)
61 if err != nil {
62 return nil, err
63 }
64 if stat.IsDir() {
65 entries, err := a.ReadDir(name)
66 if err != nil {
67 return nil, err
68 }
69 return makeDir(stat, entries), nil
70 }
71 file, err := a.fs.Open(name)
72 return &adapterFile{file: file, info: stat}, err
73 }
74
75 // ReadDir reads the named directory, implementing fs.ReadDirFS (returning a listing or error).
76 func (a *adapterFs) ReadDir(name string) ([]fs.DirEntry, error) {
77 if !validPath(name) {
78 return nil, &fs.PathError{Op: "readdir", Path: name, Err: fs.ErrInvalid}
79 }
80 items, err := a.fs.ReadDir(name)
81 if err != nil {
82 return nil, err
83 }
84 entries := make([]fs.DirEntry, len(items))
85 for i, item := range items {
86 entries[i] = fs.FileInfoToDirEntry(item)
87 }
88 return entries, nil
89 }
90
91 // Stat returns information on the named file, implementing fs.StatFS (returning FileInfo or error).
92 func (a *adapterFs) Stat(name string) (fs.FileInfo, error) {
93 if !validPath(name) {
94 return nil, &fs.PathError{Op: "stat", Path: name, Err: fs.ErrInvalid}
95 }
96 return a.fs.Stat(name)
97 }
98
99 // ReadFile reads the named file and returns its contents, implementing fs.ReadFileFS (returning contents or error).
100 func (a *adapterFs) ReadFile(name string) ([]byte, error) {
101 if !validPath(name) {
102 return nil, &fs.PathError{Op: "readfile", Path: name, Err: fs.ErrInvalid}
103 }
104 stat, err := a.fs.Stat(name)
105 if err != nil {
106 return nil, err
107 }
108 b := make([]byte, stat.Size())
109 file, err := a.Open(name)
110 if err != nil {
111 return nil, err
112 }
113 defer file.Close()
114 _, err = file.Read(b)
115 return b, err
116 }
117
118 type adapterFile struct {
119 file billyfs.File
120 info fs.FileInfo
121 }
122
123 var _ fs.File = (*adapterFile)(nil)
124
125 // Close closes the file, implementing fs.File (and io.Closer).
126 func (a *adapterFile) Close() error {
127 return a.file.Close()
128 }
129
130 // Read reads bytes from the file, implementing fs.File (and io.Reader).
131 func (a *adapterFile) Read(b []byte) (int, error) {
132 return a.file.Read(b)
133 }
134
135 // Stat returns file information, implementing fs.File (returning FileInfo or error).
136 func (a *adapterFile) Stat() (fs.FileInfo, error) {
137 return a.info, nil
138 }
139
140 type adapterDirFile struct {
141 adapterFile
142 entries []fs.DirEntry
143 }
144
145 var _ fs.ReadDirFile = (*adapterDirFile)(nil)
146
147 func makeDir(stat fs.FileInfo, entries []fs.DirEntry) *adapterDirFile {
148 return &adapterDirFile{
149 adapterFile: adapterFile{info: stat},
150 entries: entries,
151 }
152 }
153
154 // Close closes the directory, implementing fs.File (and io.Closer).
155 // Subtle: note that this is shadowing adapterFile.Close.
156 func (a *adapterDirFile) Close() error {
157 return nil
158 }
159
160 // ReadDir reads the directory contents, implementing fs.ReadDirFile (returning directory listing or error).
161 func (a *adapterDirFile) ReadDir(n int) ([]fs.DirEntry, error) {
162 if len(a.entries) == 0 && n > 0 {
163 return nil, io.EOF
164 }
165 if n <= 0 || n > len(a.entries) {
166 n = len(a.entries)
167 }
168 entries := a.entries[:n]
169 a.entries = a.entries[n:]
170 return entries, nil
171 }