@@ -20,18 +20,19 @@ type adapterFs struct {
2020fs billyfs.Filesystem
2121}
2222
23+ // Type assertion that adapterFS implements the following interfaces:
2324var _ fs.FS = (* adapterFs )(nil )
2425var _ fs.ReadDirFS = (* adapterFs )(nil )
2526var _ fs.StatFS = (* adapterFs )(nil )
2627var _ fs.ReadFileFS = (* adapterFs )(nil )
2728
28- // GlobFS would be harder, we don't implement for now .
29+ // TODO: implement fs. GlobFS, which will be a fair bit more code .
2930
30- // Open implements fs.FS.
31+ // Open opens the named file on the underlying FS, implementing fs.FS (returning a file or error) .
3132func (a * adapterFs ) Open (name string ) (fs.File , error ) {
3233if name [0 ] == '/' || name != filepath .Clean (name ) {
33- // fstest.TestFS explicitly checks that these should return error
34- // MemFS is performs the clean internally, so we need to block that here for testing.
34+ // fstest.TestFS explicitly checks that these should return error.
35+ // MemFS performs the clean internally, so we need to block that here for testing purposes .
3536return nil , & fs.PathError {Op : "open" , Path : name , Err : fs .ErrInvalid }
3637}
3738stat , err := a .fs .Stat (name )
@@ -49,7 +50,7 @@ func (a *adapterFs) Open(name string) (fs.File, error) {
4950return & adapterFile {file : file , info : stat }, err
5051}
5152
52- // ReadDir implements fs.ReadDirFS.
53+ // ReadDir reads the named directory, implementing fs.ReadDirFS (returning a listing or error) .
5354func (a * adapterFs ) ReadDir (name string ) ([]fs.DirEntry , error ) {
5455items , err := a .fs .ReadDir (name )
5556if err != nil {
@@ -62,12 +63,12 @@ func (a *adapterFs) ReadDir(name string) ([]fs.DirEntry, error) {
6263return entries , nil
6364}
6465
65- // Stat implements fs.StatFS.
66+ // Stat returns information on the named file, implementing fs.StatFS (returning FileInfo or error) .
6667func (a * adapterFs ) Stat (name string ) (fs.FileInfo , error ) {
6768return a .fs .Stat (name )
6869}
6970
70- // ReadFile implements fs.ReadFileFS.
71+ // ReadFile reads the named file and returns its contents, implementing fs.ReadFileFS (returning contents or error) .
7172func (a * adapterFs ) ReadFile (name string ) ([]byte , error ) {
7273stat , err := a .fs .Stat (name )
7374if err != nil {
@@ -90,17 +91,17 @@ type adapterFile struct {
9091
9192var _ fs.File = (* adapterFile )(nil )
9293
93- // Close implements fs.File.
94+ // Close closes the file, implementing fs.File (and io.Closer) .
9495func (a * adapterFile ) Close () error {
9596return a .file .Close ()
9697}
9798
98- // Read implements fs.File.
99+ // Read reads bytes from the file, implementing fs.File (and io.Reader) .
99100func (a * adapterFile ) Read (b []byte ) (int , error ) {
100101return a .file .Read (b )
101102}
102103
103- // Stat implements fs.File.
104+ // Stat returns file information, implementing fs.File (returning FileInfo or error) .
104105func (a * adapterFile ) Stat () (fs.FileInfo , error ) {
105106return a .info , nil
106107}
@@ -119,24 +120,21 @@ func makeDir(stat fs.FileInfo, entries []fs.DirEntry) *adapterDirFile {
119120}
120121}
121122
122- // Close implements fs.File.
123+ // Close closes the directory, implementing fs.File (and io.Closer) .
123124// Subtle: note that this is shadowing adapterFile.Close.
124125func (a * adapterDirFile ) Close () error {
125126return nil
126127}
127128
128- // ReadDir implements fs.ReadDirFile.
129+ // ReadDir reads the directory contents, implementing fs.ReadDirFile (returning directory listing or error) .
129130func (a * adapterDirFile ) ReadDir (n int ) ([]fs.DirEntry , error ) {
130131if len (a .entries ) == 0 && n > 0 {
131132return nil , io .EOF
132133}
133- if n <= 0 {
134- n = len (a .entries )
135- }
136- if n > len (a .entries ) {
134+ if n <= 0 || n > len (a .entries ) {
137135n = len (a .entries )
138136}
139137entries := a .entries [:n ]
140138a .entries = a .entries [n :]
141139return entries , nil
142- }
140+ }
0 commit comments