@@ -20,18 +20,19 @@ type adapterFs struct {
20
20
fs billyfs.Filesystem
21
21
}
22
22
23
+ // Type assertion that adapterFS implements the following interfaces:
23
24
var _ fs.FS = (* adapterFs )(nil )
24
25
var _ fs.ReadDirFS = (* adapterFs )(nil )
25
26
var _ fs.StatFS = (* adapterFs )(nil )
26
27
var _ fs.ReadFileFS = (* adapterFs )(nil )
27
28
28
- // GlobFS would be harder, we don't implement for now .
29
+ // TODO: implement fs. GlobFS, which will be a fair bit more code .
29
30
30
- // Open implements fs.FS.
31
+ // Open opens the named file on the underlying FS, implementing fs.FS (returning a file or error) .
31
32
func (a * adapterFs ) Open (name string ) (fs.File , error ) {
32
33
if 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 .
35
36
return nil , & fs.PathError {Op : "open" , Path : name , Err : fs .ErrInvalid }
36
37
}
37
38
stat , err := a .fs .Stat (name )
@@ -49,7 +50,7 @@ func (a *adapterFs) Open(name string) (fs.File, error) {
49
50
return & adapterFile {file : file , info : stat }, err
50
51
}
51
52
52
- // ReadDir implements fs.ReadDirFS.
53
+ // ReadDir reads the named directory, implementing fs.ReadDirFS (returning a listing or error) .
53
54
func (a * adapterFs ) ReadDir (name string ) ([]fs.DirEntry , error ) {
54
55
items , err := a .fs .ReadDir (name )
55
56
if err != nil {
@@ -62,12 +63,12 @@ func (a *adapterFs) ReadDir(name string) ([]fs.DirEntry, error) {
62
63
return entries , nil
63
64
}
64
65
65
- // Stat implements fs.StatFS.
66
+ // Stat returns information on the named file, implementing fs.StatFS (returning FileInfo or error) .
66
67
func (a * adapterFs ) Stat (name string ) (fs.FileInfo , error ) {
67
68
return a .fs .Stat (name )
68
69
}
69
70
70
- // ReadFile implements fs.ReadFileFS.
71
+ // ReadFile reads the named file and returns its contents, implementing fs.ReadFileFS (returning contents or error) .
71
72
func (a * adapterFs ) ReadFile (name string ) ([]byte , error ) {
72
73
stat , err := a .fs .Stat (name )
73
74
if err != nil {
@@ -90,17 +91,17 @@ type adapterFile struct {
90
91
91
92
var _ fs.File = (* adapterFile )(nil )
92
93
93
- // Close implements fs.File.
94
+ // Close closes the file, implementing fs.File (and io.Closer) .
94
95
func (a * adapterFile ) Close () error {
95
96
return a .file .Close ()
96
97
}
97
98
98
- // Read implements fs.File.
99
+ // Read reads bytes from the file, implementing fs.File (and io.Reader) .
99
100
func (a * adapterFile ) Read (b []byte ) (int , error ) {
100
101
return a .file .Read (b )
101
102
}
102
103
103
- // Stat implements fs.File.
104
+ // Stat returns file information, implementing fs.File (returning FileInfo or error) .
104
105
func (a * adapterFile ) Stat () (fs.FileInfo , error ) {
105
106
return a .info , nil
106
107
}
@@ -119,24 +120,21 @@ func makeDir(stat fs.FileInfo, entries []fs.DirEntry) *adapterDirFile {
119
120
}
120
121
}
121
122
122
- // Close implements fs.File.
123
+ // Close closes the directory, implementing fs.File (and io.Closer) .
123
124
// Subtle: note that this is shadowing adapterFile.Close.
124
125
func (a * adapterDirFile ) Close () error {
125
126
return nil
126
127
}
127
128
128
- // ReadDir implements fs.ReadDirFile.
129
+ // ReadDir reads the directory contents, implementing fs.ReadDirFile (returning directory listing or error) .
129
130
func (a * adapterDirFile ) ReadDir (n int ) ([]fs.DirEntry , error ) {
130
131
if len (a .entries ) == 0 && n > 0 {
131
132
return nil , io .EOF
132
133
}
133
- if n <= 0 {
134
- n = len (a .entries )
135
- }
136
- if n > len (a .entries ) {
134
+ if n <= 0 || n > len (a .entries ) {
137
135
n = len (a .entries )
138
136
}
139
137
entries := a .entries [:n ]
140
138
a .entries = a .entries [n :]
141
139
return entries , nil
142
- }
140
+ }
0 commit comments