2017-05-15 17:13:34 -04:00
|
|
|
package fsutil
|
|
|
|
|
|
|
|
import (
|
2018-06-08 21:07:42 -04:00
|
|
|
"context"
|
2017-05-15 17:13:34 -04:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2019-03-18 11:45:12 -04:00
|
|
|
"syscall"
|
2017-05-15 17:13:34 -04:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/fileutils"
|
|
|
|
"github.com/pkg/errors"
|
2018-10-05 05:05:42 -04:00
|
|
|
"github.com/tonistiigi/fsutil/types"
|
2017-05-15 17:13:34 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type WalkOpt struct {
|
2017-09-29 06:32:26 -04:00
|
|
|
IncludePatterns []string
|
2017-05-15 17:13:34 -04:00
|
|
|
ExcludePatterns []string
|
2018-06-08 21:07:42 -04:00
|
|
|
// FollowPaths contains symlinks that are resolved into include patterns
|
|
|
|
// before performing the fs walk
|
|
|
|
FollowPaths []string
|
2019-04-03 02:23:23 -04:00
|
|
|
Map FilterFunc
|
2017-05-15 17:13:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func Walk(ctx context.Context, p string, opt *WalkOpt, fn filepath.WalkFunc) error {
|
|
|
|
root, err := filepath.EvalSymlinks(p)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to resolve %s", root)
|
|
|
|
}
|
|
|
|
fi, err := os.Stat(root)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to stat: %s", root)
|
|
|
|
}
|
|
|
|
if !fi.IsDir() {
|
|
|
|
return errors.Errorf("%s is not a directory", root)
|
|
|
|
}
|
|
|
|
|
|
|
|
var pm *fileutils.PatternMatcher
|
|
|
|
if opt != nil && opt.ExcludePatterns != nil {
|
|
|
|
pm, err = fileutils.NewPatternMatcher(opt.ExcludePatterns)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "invalid excludepaths %s", opt.ExcludePatterns)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-08 21:07:42 -04:00
|
|
|
var includePatterns []string
|
|
|
|
if opt != nil && opt.IncludePatterns != nil {
|
|
|
|
includePatterns = make([]string, len(opt.IncludePatterns))
|
|
|
|
for k := range opt.IncludePatterns {
|
|
|
|
includePatterns[k] = filepath.Clean(opt.IncludePatterns[k])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if opt != nil && opt.FollowPaths != nil {
|
|
|
|
targets, err := FollowLinks(p, opt.FollowPaths)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if targets != nil {
|
|
|
|
includePatterns = append(includePatterns, targets...)
|
|
|
|
includePatterns = dedupePaths(includePatterns)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-08 05:26:10 -04:00
|
|
|
var lastIncludedDir string
|
|
|
|
|
2017-05-15 17:13:34 -04:00
|
|
|
seenFiles := make(map[uint64]string)
|
2018-06-08 05:26:10 -04:00
|
|
|
return filepath.Walk(root, func(path string, fi os.FileInfo, err error) (retErr error) {
|
2017-05-15 17:13:34 -04:00
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return filepath.SkipDir
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2018-06-08 05:26:10 -04:00
|
|
|
defer func() {
|
2019-03-18 11:45:12 -04:00
|
|
|
if retErr != nil && isNotExist(retErr) {
|
2018-06-08 05:26:10 -04:00
|
|
|
retErr = filepath.SkipDir
|
|
|
|
}
|
|
|
|
}()
|
2017-05-15 17:13:34 -04:00
|
|
|
origpath := path
|
|
|
|
path, err = filepath.Rel(root, path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Skip root
|
|
|
|
if path == "." {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if opt != nil {
|
2018-06-08 21:07:42 -04:00
|
|
|
if includePatterns != nil {
|
|
|
|
skip := false
|
2018-06-08 05:26:10 -04:00
|
|
|
if lastIncludedDir != "" {
|
|
|
|
if strings.HasPrefix(path, lastIncludedDir+string(filepath.Separator)) {
|
2018-06-08 21:07:42 -04:00
|
|
|
skip = true
|
2017-05-15 17:13:34 -04:00
|
|
|
}
|
|
|
|
}
|
2018-06-08 21:07:42 -04:00
|
|
|
|
|
|
|
if !skip {
|
|
|
|
matched := false
|
|
|
|
partial := true
|
|
|
|
for _, p := range includePatterns {
|
|
|
|
if ok, p := matchPrefix(p, path); ok {
|
2018-06-08 05:26:10 -04:00
|
|
|
matched = true
|
2018-06-08 21:07:42 -04:00
|
|
|
if !p {
|
|
|
|
partial = false
|
|
|
|
break
|
|
|
|
}
|
2018-06-08 05:26:10 -04:00
|
|
|
}
|
|
|
|
}
|
2018-06-08 21:07:42 -04:00
|
|
|
if !matched {
|
2018-06-10 13:11:22 -04:00
|
|
|
if fi.IsDir() {
|
|
|
|
return filepath.SkipDir
|
|
|
|
}
|
2018-06-08 05:26:10 -04:00
|
|
|
return nil
|
2018-06-08 21:07:42 -04:00
|
|
|
}
|
|
|
|
if !partial && fi.IsDir() {
|
|
|
|
lastIncludedDir = path
|
2017-05-15 17:13:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if pm != nil {
|
|
|
|
m, err := pm.Matches(path)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to match excludepatterns")
|
|
|
|
}
|
|
|
|
|
|
|
|
if m {
|
|
|
|
if fi.IsDir() {
|
|
|
|
if !pm.Exclusions() {
|
|
|
|
return filepath.SkipDir
|
|
|
|
}
|
|
|
|
dirSlash := path + string(filepath.Separator)
|
|
|
|
for _, pat := range pm.Patterns() {
|
|
|
|
if !pat.Exclusion() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
patStr := pat.String() + string(filepath.Separator)
|
|
|
|
if strings.HasPrefix(patStr, dirSlash) {
|
|
|
|
goto passedFilter
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return filepath.SkipDir
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
passedFilter:
|
2018-10-05 05:05:42 -04:00
|
|
|
stat, err := mkstat(origpath, path, fi, seenFiles)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-05-15 17:13:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
default:
|
2017-09-29 06:32:26 -04:00
|
|
|
if opt != nil && opt.Map != nil {
|
2019-04-03 02:23:23 -04:00
|
|
|
if allowed := opt.Map(stat.Path, stat); !allowed {
|
2017-09-29 06:32:26 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := fn(stat.Path, &StatInfo{stat}, nil); err != nil {
|
2017-05-15 17:13:34 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
type StatInfo struct {
|
2018-10-05 05:05:42 -04:00
|
|
|
*types.Stat
|
2017-05-15 17:13:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StatInfo) Name() string {
|
|
|
|
return filepath.Base(s.Stat.Path)
|
|
|
|
}
|
|
|
|
func (s *StatInfo) Size() int64 {
|
|
|
|
return s.Stat.Size_
|
|
|
|
}
|
|
|
|
func (s *StatInfo) Mode() os.FileMode {
|
|
|
|
return os.FileMode(s.Stat.Mode)
|
|
|
|
}
|
|
|
|
func (s *StatInfo) ModTime() time.Time {
|
|
|
|
return time.Unix(s.Stat.ModTime/1e9, s.Stat.ModTime%1e9)
|
|
|
|
}
|
|
|
|
func (s *StatInfo) IsDir() bool {
|
|
|
|
return s.Mode().IsDir()
|
|
|
|
}
|
|
|
|
func (s *StatInfo) Sys() interface{} {
|
|
|
|
return s.Stat
|
|
|
|
}
|
2018-06-08 05:26:10 -04:00
|
|
|
|
2018-06-08 21:07:42 -04:00
|
|
|
func matchPrefix(pattern, name string) (bool, bool) {
|
|
|
|
count := strings.Count(name, string(filepath.Separator))
|
|
|
|
partial := false
|
|
|
|
if strings.Count(pattern, string(filepath.Separator)) > count {
|
|
|
|
pattern = trimUntilIndex(pattern, string(filepath.Separator), count)
|
|
|
|
partial = true
|
2018-06-08 05:26:10 -04:00
|
|
|
}
|
2018-06-08 21:07:42 -04:00
|
|
|
m, _ := filepath.Match(pattern, name)
|
|
|
|
return m, partial
|
2018-06-08 05:26:10 -04:00
|
|
|
}
|
|
|
|
|
2018-06-08 21:07:42 -04:00
|
|
|
func trimUntilIndex(str, sep string, count int) string {
|
|
|
|
s := str
|
|
|
|
i := 0
|
|
|
|
c := 0
|
|
|
|
for {
|
|
|
|
idx := strings.Index(s, sep)
|
|
|
|
s = s[idx+len(sep):]
|
|
|
|
i += idx + len(sep)
|
|
|
|
c++
|
2018-06-10 13:11:22 -04:00
|
|
|
if c > count {
|
2018-06-08 21:07:42 -04:00
|
|
|
return str[:i-len(sep)]
|
2018-06-08 05:26:10 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-18 11:45:12 -04:00
|
|
|
|
|
|
|
func isNotExist(err error) bool {
|
2020-07-28 10:19:21 -04:00
|
|
|
return errors.Is(err, os.ErrNotExist) || errors.Is(err, syscall.ENOTDIR)
|
2019-03-18 11:45:12 -04:00
|
|
|
}
|