@@ -5,7 +5,6 @@ package assetfs
5
5
6
6
import (
7
7
"context"
8
- "errors"
9
8
"fmt"
10
9
"io"
11
10
"io/fs"
@@ -102,6 +101,29 @@ func (l *LayeredFS) ReadLayeredFile(elems ...string) ([]byte, string, error) {
102
101
return nil , "" , fs .ErrNotExist
103
102
}
104
103
104
+ func shouldInclude (info fs.FileInfo , fileMode ... bool ) bool {
105
+ if util .CommonSkip (info .Name ()) {
106
+ return false
107
+ }
108
+ if len (fileMode ) == 0 {
109
+ return true
110
+ } else if len (fileMode ) == 1 {
111
+ return fileMode [0 ] == ! info .Mode ().IsDir ()
112
+ }
113
+ panic ("too many arguments for fileMode in shouldInclude" )
114
+ }
115
+
116
+ func readDir (layer * Layer , name string ) ([]fs.FileInfo , error ) {
117
+ f , err := layer .Open (name )
118
+ if os .IsNotExist (err ) {
119
+ return nil , nil
120
+ } else if err != nil {
121
+ return nil , err
122
+ }
123
+ defer f .Close ()
124
+ return f .Readdir (- 1 )
125
+ }
126
+
105
127
// ListFiles lists files/directories in the given directory. The fileMode controls the returned files.
106
128
// * omitted: all files and directories will be returned.
107
129
// * true: only files will be returned.
@@ -110,28 +132,12 @@ func (l *LayeredFS) ReadLayeredFile(elems ...string) ([]byte, string, error) {
110
132
func (l * LayeredFS ) ListFiles (name string , fileMode ... bool ) ([]string , error ) {
111
133
fileMap := map [string ]bool {}
112
134
for _ , layer := range l .layers {
113
- f , err := layer .Open (name )
114
- if os .IsNotExist (err ) {
115
- continue
116
- } else if err != nil {
117
- return nil , err
118
- }
119
- infos , err := f .Readdir (- 1 )
120
- _ = f .Close ()
135
+ infos , err := readDir (layer , name )
121
136
if err != nil {
122
137
return nil , err
123
138
}
124
139
for _ , info := range infos {
125
- include := false
126
- if len (fileMode ) == 0 {
127
- include = true
128
- } else if len (fileMode ) == 1 && fileMode [0 ] == ! info .Mode ().IsDir () {
129
- include = true
130
- } else if len (fileMode ) > 1 {
131
- return nil , errors .New ("too many arguments" )
132
- }
133
- include = include && ! util .CommonSkip (info .Name ())
134
- if include {
140
+ if shouldInclude (info , fileMode ... ) {
135
141
fileMap [info .Name ()] = true
136
142
}
137
143
}
@@ -159,29 +165,13 @@ func listAllFiles(layers []*Layer, name string, fileMode ...bool) ([]string, err
159
165
var list func (dir string ) error
160
166
list = func (dir string ) error {
161
167
for _ , layer := range layers {
162
- f , err := layer .Open (dir )
163
- if os .IsNotExist (err ) {
164
- continue
165
- } else if err != nil {
166
- return err
167
- }
168
- infos , err := f .Readdir (- 1 )
169
- _ = f .Close ()
168
+ infos , err := readDir (layer , dir )
170
169
if err != nil {
171
170
return err
172
171
}
173
172
for _ , info := range infos {
174
- include := false
175
- if len (fileMode ) == 0 {
176
- include = true
177
- } else if len (fileMode ) == 1 && fileMode [0 ] == ! info .Mode ().IsDir () {
178
- include = true
179
- } else if len (fileMode ) > 1 {
180
- return errors .New ("too many arguments" )
181
- }
182
173
path := util .PathJoinRelX (dir , info .Name ())
183
- include = include && ! util .CommonSkip (info .Name ())
184
- if include {
174
+ if shouldInclude (info , fileMode ... ) {
185
175
fileMap [path ] = true
186
176
}
187
177
if info .IsDir () {
0 commit comments