Debian Codegraph @

filter.go

raw symbols SCIP
1 package fsutilscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/
2
3 import (
4 "contextscip-go gomod github.com/golang/go/src go1.21 context/"
5 "ioscip-go gomod github.com/golang/go/src go1.21 io/"
6 gofsscip-go gomod github.com/golang/go/src go1.21 `io/fs`/ "io/fsscip-go gomod github.com/golang/go/src go1.21 `io/fs`/"
7 "osscip-go gomod github.com/golang/go/src go1.21 os/"
8 "path/filepathscip-go gomod github.com/golang/go/src go1.21 `path/filepath`/"
9 "stringsscip-go gomod github.com/golang/go/src go1.21 strings/"
10 "syscallscip-go gomod github.com/golang/go/src go1.21 syscall/"
11
12 "github.com/moby/patternmatcherscip-go gomod github.com/moby/patternmatcher v0.5.0 `github.com/moby/patternmatcher`/"
13 "github.com/pkg/errorsscip-go gomod github.com/pkg/errors v0.9.1 `github.com/pkg/errors`/"
14 "github.com/tonistiigi/fsutil/typesscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil/types`/"
15 )
16
17 type FilterOptscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/FilterOpt# struct {
18 // IncludePatterns requires that the path matches at least one of the
19 // specified patterns.
20 IncludePatternsscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/FilterOpt#IncludePatterns. []string
21
22 // ExcludePatterns requires that the path does not match any of the
23 // specified patterns.
24 ExcludePatternsscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/FilterOpt#ExcludePatterns. []string
25
26 // FollowPaths contains symlinks that are resolved into IncludePatterns
27 // at the time of the call to NewFilterFS.
28 FollowPathsscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/FilterOpt#FollowPaths. []string
29
30 // Map is called for each path that is included in the result.
31 // The function can modify the stat info for each element, while the result
32 // of the function controls both how Walk continues.
33 Mapscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/FilterOpt#Map. MapFuncscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/MapFunc#
34 }
35
36 type MapFuncscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/MapFunc# func(string, *typesscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil/types`/.Statscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil/types`/Stat#) MapResultscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/MapResult#
37
38 // The result of the walk function controls
39 // both how WalkDir continues and whether the path is kept.
40 type MapResultscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/MapResult# int
41
42 const (
43 // Keep the current path and continue.
44 MapResultKeepscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/MapResultKeep. MapResultscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/MapResult# = iota
45
46 // Exclude the current path and continue.
47 MapResultExcludescip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/MapResultExclude.
48
49 // Exclude the current path, and skip the rest of the dir.
50 // If path is a dir, skip the current directory.
51 // If path is a file, skip the rest of the parent directory.
52 // (This matches the semantics of fs.SkipDir.)
53 MapResultSkipDirscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/MapResultSkipDir.
54 )
55
56 type filterFSscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/filterFS# struct {
57 fsscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/filterFS#fs. FSscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/FS#
58
59 includeMatcherscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/filterFS#includeMatcher. *patternmatcherscip-go gomod github.com/moby/patternmatcher v0.5.0 `github.com/moby/patternmatcher`/.PatternMatcherscip-go gomod github.com/moby/patternmatcher v0.5.0 `github.com/moby/patternmatcher`/PatternMatcher#
60 excludeMatcherscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/filterFS#excludeMatcher. *patternmatcherscip-go gomod github.com/moby/patternmatcher v0.5.0 `github.com/moby/patternmatcher`/.PatternMatcherscip-go gomod github.com/moby/patternmatcher v0.5.0 `github.com/moby/patternmatcher`/PatternMatcher#
61 onlyPrefixIncludesscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/filterFS#onlyPrefixIncludes. bool
62 onlyPrefixExcludeExceptionsscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/filterFS#onlyPrefixExcludeExceptions. bool
63
64 mapFnscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/filterFS#mapFn. MapFuncscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/MapFunc#
65 }
66
67 // NewFilterFS creates a new FS that filters the given FS using the given
68 // FilterOpt.
69 //
70 // The returned FS will not contain any paths that do not match the provided
71 // include and exclude patterns, or that are are excluded using the mapping
72 // function.
73 //
74 // The FS is assumed to be a snapshot of the filesystem at the time of the
75 // call to NewFilterFS. If the underlying filesystem changes, calls to the
76 // underlying FS may be inconsistent.
77 func NewFilterFSscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/NewFilterFS().(fs FSscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/FS#, opt *FilterOptscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/FilterOpt#) (FSscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/FS#, error) {
78 if opt == nil {
79 return fs, nil
80 }
81
82 var includePatterns []string
83 if opt.IncludePatternsscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/FilterOpt#IncludePatterns. != nil {
84 includePatterns = make([]string, len(opt.IncludePatternsscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/FilterOpt#IncludePatterns.))
85 copy(includePatterns, opt.IncludePatternsscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/FilterOpt#IncludePatterns.)
86 }
87 if opt.FollowPathsscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/FilterOpt#FollowPaths. != nil {
88 targets, err := FollowLinksscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/FollowLinks().(fs, opt.FollowPathsscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/FilterOpt#FollowPaths.)
89 if err != nil {
90 return nil, err
91 }
92 if targets != nil {
93 includePatterns = append(includePatterns, targets...)
94 includePatterns = dedupePathsscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/dedupePaths().(includePatterns)
95 }
96 }
97
98 patternChars := "*[]?^"
99 if filepathscip-go gomod github.com/golang/go/src go1.21 `path/filepath`/.Separatorscip-go gomod github.com/golang/go/src go1.21 `path/filepath`/Separator. != '\\' {
100 patternChars += `\`
101 }
102
103 var (
104 includeMatcher *patternmatcherscip-go gomod github.com/moby/patternmatcher v0.5.0 `github.com/moby/patternmatcher`/.PatternMatcherscip-go gomod github.com/moby/patternmatcher v0.5.0 `github.com/moby/patternmatcher`/PatternMatcher#
105 excludeMatcher *patternmatcherscip-go gomod github.com/moby/patternmatcher v0.5.0 `github.com/moby/patternmatcher`/.PatternMatcherscip-go gomod github.com/moby/patternmatcher v0.5.0 `github.com/moby/patternmatcher`/PatternMatcher#
106 err error
107 onlyPrefixIncludes = true
108 onlyPrefixExcludeExceptions = true
109 )
110
111 if len(includePatterns) > 0 {
112 includeMatcher, err = patternmatcherscip-go gomod github.com/moby/patternmatcher v0.5.0 `github.com/moby/patternmatcher`/.Newscip-go gomod github.com/moby/patternmatcher v0.5.0 `github.com/moby/patternmatcher`/New().(includePatterns)
113 if err != nil {
114 return nil, errorsscip-go gomod github.com/pkg/errors v0.9.1 `github.com/pkg/errors`/.Wrapfscip-go gomod github.com/pkg/errors v0.9.1 `github.com/pkg/errors`/Wrapf().(err, "invalid includepatterns: %s", includePatterns)
115 }
116
117 for _, p := range includeMatcher.Patternsscip-go gomod github.com/moby/patternmatcher v0.5.0 `github.com/moby/patternmatcher`/PatternMatcher#Patterns().() {
118 if !p.Exclusionscip-go gomod github.com/moby/patternmatcher v0.5.0 `github.com/moby/patternmatcher`/Pattern#Exclusion().() && stringsscip-go gomod github.com/golang/go/src go1.21 strings/.ContainsAnyscip-go gomod github.com/golang/go/src go1.21 strings/ContainsAny().(patternWithoutTrailingGlobscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/patternWithoutTrailingGlob().(p), patternChars) {
119 onlyPrefixIncludes = false
120 break
121 }
122 }
123
124 }
125
126 if len(opt.ExcludePatternsscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/FilterOpt#ExcludePatterns.) > 0 {
127 excludeMatcher, err = patternmatcherscip-go gomod github.com/moby/patternmatcher v0.5.0 `github.com/moby/patternmatcher`/.Newscip-go gomod github.com/moby/patternmatcher v0.5.0 `github.com/moby/patternmatcher`/New().(opt.ExcludePatternsscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/FilterOpt#ExcludePatterns.)
128 if err != nil {
129 return nil, errorsscip-go gomod github.com/pkg/errors v0.9.1 `github.com/pkg/errors`/.Wrapfscip-go gomod github.com/pkg/errors v0.9.1 `github.com/pkg/errors`/Wrapf().(err, "invalid excludepatterns: %s", opt.ExcludePatternsscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/FilterOpt#ExcludePatterns.)
130 }
131
132 for _, p := range excludeMatcher.Patternsscip-go gomod github.com/moby/patternmatcher v0.5.0 `github.com/moby/patternmatcher`/PatternMatcher#Patterns().() {
133 if p.Exclusionscip-go gomod github.com/moby/patternmatcher v0.5.0 `github.com/moby/patternmatcher`/Pattern#Exclusion().() && stringsscip-go gomod github.com/golang/go/src go1.21 strings/.ContainsAnyscip-go gomod github.com/golang/go/src go1.21 strings/ContainsAny().(patternWithoutTrailingGlobscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/patternWithoutTrailingGlob().(p), patternChars) {
134 onlyPrefixExcludeExceptions = false
135 break
136 }
137 }
138 }
139
140 return &filterFSscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/filterFS#{
141 fsscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/filterFS#fs.: fs,
142 includeMatcherscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/filterFS#includeMatcher.: includeMatcher,
143 excludeMatcherscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/filterFS#excludeMatcher.: excludeMatcher,
144 onlyPrefixIncludesscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/filterFS#onlyPrefixIncludes.: onlyPrefixIncludes,
145 onlyPrefixExcludeExceptionsscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/filterFS#onlyPrefixExcludeExceptions.: onlyPrefixExcludeExceptions,
146 mapFnscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/filterFS#mapFn.: opt.Mapscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/FilterOpt#Map.,
147 }, nil
148 }
149
150 func (fs *filterFSscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/filterFS#) Openscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/filterFS#Open().(p string) (ioscip-go gomod github.com/golang/go/src go1.21 io/.ReadCloserscip-go gomod github.com/golang/go/src go1.21 io/ReadCloser#, error) {
151 if fs.includeMatcherscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/filterFS#includeMatcher. != nil {
152 m, err := fs.includeMatcherscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/filterFS#includeMatcher..MatchesOrParentMatchesscip-go gomod github.com/moby/patternmatcher v0.5.0 `github.com/moby/patternmatcher`/PatternMatcher#MatchesOrParentMatches().(p)
153 if err != nil {
154 return nil, err
155 }
156 if !m {
157 return nil, errorsscip-go gomod github.com/pkg/errors v0.9.1 `github.com/pkg/errors`/.Wrapfscip-go gomod github.com/pkg/errors v0.9.1 `github.com/pkg/errors`/Wrapf().(osscip-go gomod github.com/golang/go/src go1.21 os/.ErrNotExistscip-go gomod github.com/golang/go/src go1.21 os/ErrNotExist., "open %s", p)
158 }
159 }
160 if fs.excludeMatcherscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/filterFS#excludeMatcher. != nil {
161 m, err := fs.excludeMatcherscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/filterFS#excludeMatcher..MatchesOrParentMatchesscip-go gomod github.com/moby/patternmatcher v0.5.0 `github.com/moby/patternmatcher`/PatternMatcher#MatchesOrParentMatches().(p)
162 if err != nil {
163 return nil, err
164 }
165 if m {
166 return nil, errorsscip-go gomod github.com/pkg/errors v0.9.1 `github.com/pkg/errors`/.Wrapfscip-go gomod github.com/pkg/errors v0.9.1 `github.com/pkg/errors`/Wrapf().(osscip-go gomod github.com/golang/go/src go1.21 os/.ErrNotExistscip-go gomod github.com/golang/go/src go1.21 os/ErrNotExist., "open %s", p)
167 }
168 }
169 return fs.fsscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/filterFS#fs..Openscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/FS#Open.(p)
170 }
171
172 func (fs *filterFSscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/filterFS#) Walkscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/filterFS#Walk().(ctx contextscip-go gomod github.com/golang/go/src go1.21 context/.Contextscip-go gomod github.com/golang/go/src go1.21 context/Context#, target string, fn gofsscip-go gomod github.com/golang/go/src go1.21 `io/fs`/.WalkDirFuncscip-go gomod github.com/golang/go/src go1.21 `io/fs`/WalkDirFunc#) error {
173 type visitedDir struct {
174 entry gofsscip-go gomod github.com/golang/go/src go1.21 `io/fs`/.DirEntryscip-go gomod github.com/golang/go/src go1.21 `io/fs`/DirEntry#
175 pathWithSep string
176 includeMatchInfo patternmatcherscip-go gomod github.com/moby/patternmatcher v0.5.0 `github.com/moby/patternmatcher`/.MatchInfoscip-go gomod github.com/moby/patternmatcher v0.5.0 `github.com/moby/patternmatcher`/MatchInfo#
177 excludeMatchInfo patternmatcherscip-go gomod github.com/moby/patternmatcher v0.5.0 `github.com/moby/patternmatcher`/.MatchInfoscip-go gomod github.com/moby/patternmatcher v0.5.0 `github.com/moby/patternmatcher`/MatchInfo#
178 calledFn bool
179 skipFn bool
180 }
181
182 // used only for include/exclude handling
183 var parentDirs []visitedDir
184
185 return fs.fsscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/filterFS#fs..Walkscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/FS#Walk.(ctx, target, func(path string, dirEntry gofsscip-go gomod github.com/golang/go/src go1.21 `io/fs`/.DirEntryscip-go gomod github.com/golang/go/src go1.21 `io/fs`/DirEntry#, walkErr error) (retErr error) {
186 defer func() {
187 if retErr != nil && isNotExistscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/isNotExist().(retErr) {
188 retErr = filepathscip-go gomod github.com/golang/go/src go1.21 `path/filepath`/.SkipDirscip-go gomod github.com/golang/go/src go1.21 `path/filepath`/SkipDir.
189 }
190 }()
191
192 var (
193 dir visitedDir
194 isDir bool
195 )
196 if dirEntry != nil {
197 isDir = dirEntry.IsDirscip-go gomod github.com/golang/go/src go1.21 `io/fs`/DirEntry#IsDir().()
198 }
199
200 if fs.includeMatcherscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/filterFS#includeMatcher. != nil || fs.excludeMatcherscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/filterFS#excludeMatcher. != nil {
201 for len(parentDirs) != 0 {
202 lastParentDir := parentDirs[len(parentDirs)-1].pathWithSep
203 if stringsscip-go gomod github.com/golang/go/src go1.21 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.21 strings/HasPrefix().(path, lastParentDir) {
204 break
205 }
206 parentDirs = parentDirs[:len(parentDirs)-1]
207 }
208
209 if isDir {
210 dir = visitedDir{
211 entry: dirEntry,
212 pathWithSep: path + string(filepathscip-go gomod github.com/golang/go/src go1.21 `path/filepath`/.Separatorscip-go gomod github.com/golang/go/src go1.21 `path/filepath`/Separator.),
213 }
214 }
215 }
216
217 skip := false
218
219 if fs.includeMatcherscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/filterFS#includeMatcher. != nil {
220 var parentIncludeMatchInfo patternmatcherscip-go gomod github.com/moby/patternmatcher v0.5.0 `github.com/moby/patternmatcher`/.MatchInfoscip-go gomod github.com/moby/patternmatcher v0.5.0 `github.com/moby/patternmatcher`/MatchInfo#
221 if len(parentDirs) != 0 {
222 parentIncludeMatchInfo = parentDirs[len(parentDirs)-1].includeMatchInfo
223 }
224 m, matchInfo, err := fs.includeMatcherscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/filterFS#includeMatcher..MatchesUsingParentResultsscip-go gomod github.com/moby/patternmatcher v0.5.0 `github.com/moby/patternmatcher`/PatternMatcher#MatchesUsingParentResults().(path, parentIncludeMatchInfo)
225 if err != nil {
226 return errorsscip-go gomod github.com/pkg/errors v0.9.1 `github.com/pkg/errors`/.Wrapscip-go gomod github.com/pkg/errors v0.9.1 `github.com/pkg/errors`/Wrap().(err, "failed to match includepatterns")
227 }
228
229 if isDir {
230 dir.includeMatchInfo = matchInfo
231 }
232
233 if !m {
234 if isDir && fs.onlyPrefixIncludesscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/filterFS#onlyPrefixIncludes. {
235 // Optimization: we can skip walking this dir if no include
236 // patterns could match anything inside it.
237 dirSlash := path + string(filepathscip-go gomod github.com/golang/go/src go1.21 `path/filepath`/.Separatorscip-go gomod github.com/golang/go/src go1.21 `path/filepath`/Separator.)
238 for _, pat := range fs.includeMatcherscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/filterFS#includeMatcher..Patternsscip-go gomod github.com/moby/patternmatcher v0.5.0 `github.com/moby/patternmatcher`/PatternMatcher#Patterns().() {
239 if pat.Exclusionscip-go gomod github.com/moby/patternmatcher v0.5.0 `github.com/moby/patternmatcher`/Pattern#Exclusion().() {
240 continue
241 }
242 patStr := patternWithoutTrailingGlobscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/patternWithoutTrailingGlob().(pat) + string(filepathscip-go gomod github.com/golang/go/src go1.21 `path/filepath`/.Separatorscip-go gomod github.com/golang/go/src go1.21 `path/filepath`/Separator.)
243 if stringsscip-go gomod github.com/golang/go/src go1.21 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.21 strings/HasPrefix().(patStr, dirSlash) {
244 goto passedIncludeFilter
245 }
246 }
247 return filepathscip-go gomod github.com/golang/go/src go1.21 `path/filepath`/.SkipDirscip-go gomod github.com/golang/go/src go1.21 `path/filepath`/SkipDir.
248 }
249 passedIncludeFilter:
250 skip = true
251 }
252 }
253
254 if fs.excludeMatcherscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/filterFS#excludeMatcher. != nil {
255 var parentExcludeMatchInfo patternmatcherscip-go gomod github.com/moby/patternmatcher v0.5.0 `github.com/moby/patternmatcher`/.MatchInfoscip-go gomod github.com/moby/patternmatcher v0.5.0 `github.com/moby/patternmatcher`/MatchInfo#
256 if len(parentDirs) != 0 {
257 parentExcludeMatchInfo = parentDirs[len(parentDirs)-1].excludeMatchInfo
258 }
259 m, matchInfo, err := fs.excludeMatcherscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/filterFS#excludeMatcher..MatchesUsingParentResultsscip-go gomod github.com/moby/patternmatcher v0.5.0 `github.com/moby/patternmatcher`/PatternMatcher#MatchesUsingParentResults().(path, parentExcludeMatchInfo)
260 if err != nil {
261 return errorsscip-go gomod github.com/pkg/errors v0.9.1 `github.com/pkg/errors`/.Wrapscip-go gomod github.com/pkg/errors v0.9.1 `github.com/pkg/errors`/Wrap().(err, "failed to match excludepatterns")
262 }
263
264 if isDir {
265 dir.excludeMatchInfo = matchInfo
266 }
267
268 if m {
269 if isDir && fs.onlyPrefixExcludeExceptionsscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/filterFS#onlyPrefixExcludeExceptions. {
270 // Optimization: we can skip walking this dir if no
271 // exceptions to exclude patterns could match anything
272 // inside it.
273 if !fs.excludeMatcherscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/filterFS#excludeMatcher..Exclusionsscip-go gomod github.com/moby/patternmatcher v0.5.0 `github.com/moby/patternmatcher`/PatternMatcher#Exclusions().() {
274 return filepathscip-go gomod github.com/golang/go/src go1.21 `path/filepath`/.SkipDirscip-go gomod github.com/golang/go/src go1.21 `path/filepath`/SkipDir.
275 }
276
277 dirSlash := path + string(filepathscip-go gomod github.com/golang/go/src go1.21 `path/filepath`/.Separatorscip-go gomod github.com/golang/go/src go1.21 `path/filepath`/Separator.)
278 for _, pat := range fs.excludeMatcherscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/filterFS#excludeMatcher..Patternsscip-go gomod github.com/moby/patternmatcher v0.5.0 `github.com/moby/patternmatcher`/PatternMatcher#Patterns().() {
279 if !pat.Exclusionscip-go gomod github.com/moby/patternmatcher v0.5.0 `github.com/moby/patternmatcher`/Pattern#Exclusion().() {
280 continue
281 }
282 patStr := patternWithoutTrailingGlobscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/patternWithoutTrailingGlob().(pat) + string(filepathscip-go gomod github.com/golang/go/src go1.21 `path/filepath`/.Separatorscip-go gomod github.com/golang/go/src go1.21 `path/filepath`/Separator.)
283 if stringsscip-go gomod github.com/golang/go/src go1.21 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.21 strings/HasPrefix().(patStr, dirSlash) {
284 goto passedExcludeFilter
285 }
286 }
287 return filepathscip-go gomod github.com/golang/go/src go1.21 `path/filepath`/.SkipDirscip-go gomod github.com/golang/go/src go1.21 `path/filepath`/SkipDir.
288 }
289 passedExcludeFilter:
290 skip = true
291 }
292 }
293
294 if walkErr != nil {
295 if skip && errorsscip-go gomod github.com/pkg/errors v0.9.1 `github.com/pkg/errors`/.Isscip-go gomod github.com/pkg/errors v0.9.1 `github.com/pkg/errors`/Is().(walkErr, osscip-go gomod github.com/golang/go/src go1.21 os/.ErrPermissionscip-go gomod github.com/golang/go/src go1.21 os/ErrPermission.) {
296 return nil
297 }
298 return walkErr
299 }
300
301 if fs.includeMatcherscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/filterFS#includeMatcher. != nil || fs.excludeMatcherscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/filterFS#excludeMatcher. != nil {
302 defer func() {
303 if isDir {
304 parentDirs = append(parentDirs, dir)
305 }
306 }()
307 }
308
309 if skip {
310 return nil
311 }
312
313 dir.calledFn = true
314
315 fi, err := dirEntry.Infoscip-go gomod github.com/golang/go/src go1.21 `io/fs`/DirEntry#Info().()
316 if err != nil {
317 return err
318 }
319 stat, ok := fi.Sysscip-go gomod github.com/golang/go/src go1.21 `io/fs`/FileInfo#Sys().().(*typesscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil/types`/.Statscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil/types`/Stat#)
320 if !ok {
321 return errorsscip-go gomod github.com/pkg/errors v0.9.1 `github.com/pkg/errors`/.WithStackscip-go gomod github.com/pkg/errors v0.9.1 `github.com/pkg/errors`/WithStack().(&osscip-go gomod github.com/golang/go/src go1.21 os/.PathErrorscip-go gomod github.com/golang/go/src go1.21 os/PathError#{Pathscip-go gomod github.com/golang/go/src go1.21 `io/fs`/PathError#Path.: path, Errscip-go gomod github.com/golang/go/src go1.21 `io/fs`/PathError#Err.: syscallscip-go gomod github.com/golang/go/src go1.21 syscall/.EBADMSGscip-go gomod github.com/golang/go/src go1.21 syscall/EBADMSG., Opscip-go gomod github.com/golang/go/src go1.21 `io/fs`/PathError#Op.: "fileinfo without stat info"})
322 }
323
324 select {
325 case <-ctx.Donescip-go gomod github.com/golang/go/src go1.21 context/Context#Done().():
326 return ctx.Errscip-go gomod github.com/golang/go/src go1.21 context/Context#Err().()
327 default:
328 if fs.mapFnscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/filterFS#mapFn. != nil {
329 result := fs.mapFnscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/filterFS#mapFn.(stat.Pathscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil/types`/Stat#Path., stat)
330 if result == MapResultSkipDirscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/MapResultSkipDir. {
331 return filepathscip-go gomod github.com/golang/go/src go1.21 `path/filepath`/.SkipDirscip-go gomod github.com/golang/go/src go1.21 `path/filepath`/SkipDir.
332 } else if result == MapResultExcludescip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/MapResultExclude. {
333 return nil
334 }
335 }
336 for i, parentDir := range parentDirs {
337 if parentDir.skipFn {
338 return filepathscip-go gomod github.com/golang/go/src go1.21 `path/filepath`/.SkipDirscip-go gomod github.com/golang/go/src go1.21 `path/filepath`/SkipDir.
339 }
340 if parentDir.calledFn {
341 continue
342 }
343 parentFi, err := parentDir.entry.Infoscip-go gomod github.com/golang/go/src go1.21 `io/fs`/DirEntry#Info().()
344 if err != nil {
345 return err
346 }
347 parentStat, ok := parentFi.Sysscip-go gomod github.com/golang/go/src go1.21 `io/fs`/FileInfo#Sys().().(*typesscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil/types`/.Statscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil/types`/Stat#)
348 if !ok {
349 return errorsscip-go gomod github.com/pkg/errors v0.9.1 `github.com/pkg/errors`/.WithStackscip-go gomod github.com/pkg/errors v0.9.1 `github.com/pkg/errors`/WithStack().(&osscip-go gomod github.com/golang/go/src go1.21 os/.PathErrorscip-go gomod github.com/golang/go/src go1.21 os/PathError#{Pathscip-go gomod github.com/golang/go/src go1.21 `io/fs`/PathError#Path.: path, Errscip-go gomod github.com/golang/go/src go1.21 `io/fs`/PathError#Err.: syscallscip-go gomod github.com/golang/go/src go1.21 syscall/.EBADMSGscip-go gomod github.com/golang/go/src go1.21 syscall/EBADMSG., Opscip-go gomod github.com/golang/go/src go1.21 `io/fs`/PathError#Op.: "fileinfo without stat info"})
350 }
351
352 select {
353 case <-ctx.Donescip-go gomod github.com/golang/go/src go1.21 context/Context#Done().():
354 return ctx.Errscip-go gomod github.com/golang/go/src go1.21 context/Context#Err().()
355 default:
356 }
357 if fs.mapFnscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/filterFS#mapFn. != nil {
358 result := fs.mapFnscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/filterFS#mapFn.(parentStat.Pathscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil/types`/Stat#Path., parentStat)
359 if result == MapResultExcludescip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/MapResultExclude. {
360 continue
361 } else if result == MapResultSkipDirscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/MapResultSkipDir. {
362 parentDirs[i].skipFn = true
363 return filepathscip-go gomod github.com/golang/go/src go1.21 `path/filepath`/.SkipDirscip-go gomod github.com/golang/go/src go1.21 `path/filepath`/SkipDir.
364 }
365 }
366
367 parentDirs[i].calledFn = true
368 if err := fn(parentStat.Pathscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil/types`/Stat#Path., &DirEntryInfoscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/DirEntryInfo#{Statscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/DirEntryInfo#Stat.: parentStat}, nil); err == filepathscip-go gomod github.com/golang/go/src go1.21 `path/filepath`/.SkipDirscip-go gomod github.com/golang/go/src go1.21 `path/filepath`/SkipDir. {
369 parentDirs[i].skipFn = true
370 return filepathscip-go gomod github.com/golang/go/src go1.21 `path/filepath`/.SkipDirscip-go gomod github.com/golang/go/src go1.21 `path/filepath`/SkipDir.
371 } else if err != nil {
372 return err
373 }
374 }
375 if err := fn(stat.Pathscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil/types`/Stat#Path., &DirEntryInfoscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/DirEntryInfo#{Statscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/DirEntryInfo#Stat.: stat}, nil); err != nil {
376 return err
377 }
378 }
379 return nil
380 })
381 }
382
383 func Walkscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/Walk().(ctx contextscip-go gomod github.com/golang/go/src go1.21 context/.Contextscip-go gomod github.com/golang/go/src go1.21 context/Context#, p string, opt *FilterOptscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/FilterOpt#, fn filepathscip-go gomod github.com/golang/go/src go1.21 `path/filepath`/.WalkFuncscip-go gomod github.com/golang/go/src go1.21 `path/filepath`/WalkFunc#) error {
384 f, err := NewFSscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/NewFS().(p)
385 if err != nil {
386 return err
387 }
388 f, err = NewFilterFSscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/NewFilterFS().(f, opt)
389 if err != nil {
390 return err
391 }
392 return f.Walkscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/FS#Walk.(ctx, "/", func(path string, d gofsscip-go gomod github.com/golang/go/src go1.21 `io/fs`/.DirEntryscip-go gomod github.com/golang/go/src go1.21 `io/fs`/DirEntry#, err error) error {
393 var info gofsscip-go gomod github.com/golang/go/src go1.21 `io/fs`/.FileInfoscip-go gomod github.com/golang/go/src go1.21 `io/fs`/FileInfo#
394 if d != nil {
395 var err2 error
396 info, err2 = d.Infoscip-go gomod github.com/golang/go/src go1.21 `io/fs`/DirEntry#Info().()
397 if err == nil {
398 err = err2
399 }
400 }
401 return fn(path, info, err)
402 })
403 }
404
405 func WalkDirscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/WalkDir().(ctx contextscip-go gomod github.com/golang/go/src go1.21 context/.Contextscip-go gomod github.com/golang/go/src go1.21 context/Context#, p string, opt *FilterOptscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/FilterOpt#, fn gofsscip-go gomod github.com/golang/go/src go1.21 `io/fs`/.WalkDirFuncscip-go gomod github.com/golang/go/src go1.21 `io/fs`/WalkDirFunc#) error {
406 f, err := NewFSscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/NewFS().(p)
407 if err != nil {
408 return err
409 }
410 f, err = NewFilterFSscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/NewFilterFS().(f, opt)
411 if err != nil {
412 return err
413 }
414 return f.Walkscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/FS#Walk.(ctx, "/", fn)
415 }
416
417 func patternWithoutTrailingGlobscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/patternWithoutTrailingGlob().(p *patternmatcherscip-go gomod github.com/moby/patternmatcher v0.5.0 `github.com/moby/patternmatcher`/.Patternscip-go gomod github.com/moby/patternmatcher v0.5.0 `github.com/moby/patternmatcher`/Pattern#) string {
418 patStr := p.Stringscip-go gomod github.com/moby/patternmatcher v0.5.0 `github.com/moby/patternmatcher`/Pattern#String().()
419 // We use filepath.Separator here because patternmatcher.Pattern patterns
420 // get transformed to use the native path separator:
421 // https://github.com/moby/patternmatcher/blob/130b41bafc16209dc1b52a103fdac1decad04f1a/patternmatcher.go#L52
422 patStr = stringsscip-go gomod github.com/golang/go/src go1.21 strings/.TrimSuffixscip-go gomod github.com/golang/go/src go1.21 strings/TrimSuffix().(patStr, string(filepathscip-go gomod github.com/golang/go/src go1.21 `path/filepath`/.Separatorscip-go gomod github.com/golang/go/src go1.21 `path/filepath`/Separator.)+"**")
423 patStr = stringsscip-go gomod github.com/golang/go/src go1.21 strings/.TrimSuffixscip-go gomod github.com/golang/go/src go1.21 strings/TrimSuffix().(patStr, string(filepathscip-go gomod github.com/golang/go/src go1.21 `path/filepath`/.Separatorscip-go gomod github.com/golang/go/src go1.21 `path/filepath`/Separator.)+"*")
424 return patStr
425 }
426
427 func isNotExistscip-go gomod github.com/tonistiigi/fsutil . `github.com/tonistiigi/fsutil`/isNotExist().(err error) bool {
428 return errorsscip-go gomod github.com/pkg/errors v0.9.1 `github.com/pkg/errors`/.Isscip-go gomod github.com/pkg/errors v0.9.1 `github.com/pkg/errors`/Is().(err, osscip-go gomod github.com/golang/go/src go1.21 os/.ErrNotExistscip-go gomod github.com/golang/go/src go1.21 os/ErrNotExist.) || errorsscip-go gomod github.com/pkg/errors v0.9.1 `github.com/pkg/errors`/.Isscip-go gomod github.com/pkg/errors v0.9.1 `github.com/pkg/errors`/Is().(err, syscallscip-go gomod github.com/golang/go/src go1.21 syscall/.ENOTDIRscip-go gomod github.com/golang/go/src go1.21 syscall/ENOTDIR.)
429 }