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