From e11875735bd4ad11376379a9aa0d38a4886b650d Mon Sep 17 00:00:00 2001 From: Aaron Lehmann Date: Wed, 28 Apr 2021 15:23:44 -0700 Subject: [PATCH] Update vendored fsutil fork Signed-off-by: Aaron Lehmann --- cache/contenthash/checksum.go | 2 +- go.mod | 2 +- go.sum | 4 +- .../github.com/tonistiigi/fsutil/copy/copy.go | 99 ++++++++++--------- .../tonistiigi/fsutil/prefix/match.go | 20 +++- vendor/github.com/tonistiigi/fsutil/walker.go | 2 +- vendor/modules.txt | 2 +- 7 files changed, 71 insertions(+), 60 deletions(-) diff --git a/cache/contenthash/checksum.go b/cache/contenthash/checksum.go index e0c6fb82b4f46..d5246adeb1395 100644 --- a/cache/contenthash/checksum.go +++ b/cache/contenthash/checksum.go @@ -574,7 +574,7 @@ func shouldIncludePath( lastIncludedDir string, ) (bool, bool, error) { if wildcard { - include, err := path.Match(p, candidate) + include, err := path.Match(p, candidate, true) if err != nil { return include, false, err } diff --git a/go.mod b/go.mod index 14e796858f02f..72e248e443965 100644 --- a/go.mod +++ b/go.mod @@ -75,7 +75,7 @@ replace ( github.com/hashicorp/go-immutable-radix => github.com/tonistiigi/go-immutable-radix v0.0.0-20170803185627-826af9ccf0fe github.com/jaguilar/vt100 => github.com/tonistiigi/vt100 v0.0.0-20190402012908-ad4c4a574305 // temporary until https://github.com/tonistiigi/fsutil/pull/101 is merged - github.com/tonistiigi/fsutil => github.com/aaronlehmann/fsutil v0.0.0-20210421230417-620ac87d5620 + github.com/tonistiigi/fsutil => github.com/aaronlehmann/fsutil v0.0.0-20210428220603-accc0fae8749 // genproto: corresponds to containerd google.golang.org/genproto => google.golang.org/genproto v0.0.0-20200224152610-e50cd9704f63 // grpc: corresponds to protobuf diff --git a/go.sum b/go.sum index 3f5b202344798..7b9368ab11286 100644 --- a/go.sum +++ b/go.sum @@ -61,8 +61,8 @@ github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAE github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0= github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE= github.com/Shopify/logrus-bugsnag v0.0.0-20171204204709-577dee27f20d/go.mod h1:HI8ITrYtUY+O+ZhtlqUnD8+KwNPOyugEhfP9fdUIaEQ= -github.com/aaronlehmann/fsutil v0.0.0-20210421230417-620ac87d5620 h1:QvaYmkJ1bcslW9/y3HXoe96Pz51pr3Hg55DhskwauZE= -github.com/aaronlehmann/fsutil v0.0.0-20210421230417-620ac87d5620/go.mod h1:4Bcxev2PKmz1bFF6Mg3opy8w4kYQVvEXQGKVkQkP2Ac= +github.com/aaronlehmann/fsutil v0.0.0-20210428220603-accc0fae8749 h1:Pe6Ex38zK9fsv73pYe5c3/pf2ZNKIHxlw4VMt2hbpl8= +github.com/aaronlehmann/fsutil v0.0.0-20210428220603-accc0fae8749/go.mod h1:4Bcxev2PKmz1bFF6Mg3opy8w4kYQVvEXQGKVkQkP2Ac= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= diff --git a/vendor/github.com/tonistiigi/fsutil/copy/copy.go b/vendor/github.com/tonistiigi/fsutil/copy/copy.go index 2053ecb593467..65dad8cd95175 100644 --- a/vendor/github.com/tonistiigi/fsutil/copy/copy.go +++ b/vendor/github.com/tonistiigi/fsutil/copy/copy.go @@ -114,8 +114,8 @@ func Copy(ctx context.Context, srcRoot, src, dstRoot, dst string, opts ...Opt) e if err != nil { return err } - includeAll := len(c.includePatterns) == 0 - if err := c.copy(ctx, srcFollowed, "", dst, false, includeAll); err != nil { + skipIncludePatterns := len(c.includePatterns) == 0 + if err := c.copy(ctx, srcFollowed, "", dst, false, skipIncludePatterns); err != nil { return err } } @@ -227,6 +227,13 @@ type copier struct { xattrErrorHandler XAttrErrorHandler includePatterns []string excludePatternMatcher *fileutils.PatternMatcher + parentDirs []parentDir +} + +type parentDir struct { + srcPath string + dstPath string + copied bool } func newCopier(chown Chowner, tm *time.Time, mode *int, xeh XAttrErrorHandler, includePatterns, excludePatterns []string) (*copier, error) { @@ -257,7 +264,7 @@ func newCopier(chown Chowner, tm *time.Time, mode *int, xeh XAttrErrorHandler, i } // dest is always clean -func (c *copier) copy(ctx context.Context, src, srcComponents, target string, overwriteTargetMetadata, includeAll bool) error { +func (c *copier) copy(ctx context.Context, src, srcComponents, target string, overwriteTargetMetadata, skipIncludePatterns bool) error { select { case <-ctx.Done(): return ctx.Err() @@ -271,8 +278,8 @@ func (c *copier) copy(ctx context.Context, src, srcComponents, target string, ov var include bool if srcComponents != "" { - if !includeAll { - include, includeAll, err = c.include(srcComponents, fi) + if !skipIncludePatterns { + include, skipIncludePatterns, err = c.include(srcComponents, fi) if err != nil { return err } @@ -298,7 +305,7 @@ func (c *copier) copy(ctx context.Context, src, srcComponents, target string, ov if err := ensureEmptyFileTarget(target); err != nil { return err } - } else if includeAll { + } else if skipIncludePatterns { if err := c.createParentDirs(src, srcComponents, target, overwriteTargetMetadata); err != nil { return err } @@ -308,9 +315,9 @@ func (c *copier) copy(ctx context.Context, src, srcComponents, target string, ov switch { case fi.IsDir(): - if created, err := c.copyDirectory(ctx, src, srcComponents, target, fi, overwriteTargetMetadata, includeAll); err != nil { + if created, err := c.copyDirectory(ctx, src, srcComponents, target, fi, overwriteTargetMetadata, skipIncludePatterns); err != nil { return err - } else if !overwriteTargetMetadata || !includeAll { + } else if !overwriteTargetMetadata || !skipIncludePatterns { copyFileInfo = created } case (fi.Mode() & os.ModeType) == 0: @@ -362,7 +369,7 @@ func (c *copier) include(path string, fi os.FileInfo) (bool, bool, error) { pattern = strings.TrimSuffix(pattern, string(filepath.Separator)) } - if ok, p := prefix.Match(pattern, path); ok { + if ok, p := prefix.Match(pattern, path, false); ok { matched = true if !p { partial = false @@ -411,55 +418,39 @@ func (c *copier) exclude(path string, fi os.FileInfo) (bool, error) { // Delayed creation of parent directories when a file or dir matches an include // pattern. func (c *copier) createParentDirs(src, srcComponents, target string, overwriteTargetMetadata bool) error { - if len(c.includePatterns) == 0 { - return nil - } - - count := strings.Count(srcComponents, string(filepath.Separator)) - if count != 0 { - srcPaths := []string{src} - targetPaths := []string{target} - for i := 0; i != count; i++ { - srcParentDir, _ := filepath.Split(srcPaths[len(srcPaths)-1]) - if len(srcParentDir) > 1 { - srcParentDir = strings.TrimSuffix(srcParentDir, string(filepath.Separator)) - } - srcPaths = append(srcPaths, srcParentDir) + for i, parentDir := range c.parentDirs { + if parentDir.copied { + continue + } - targetParentDir, _ := filepath.Split(targetPaths[len(targetPaths)-1]) - if len(targetParentDir) > 1 { - targetParentDir = strings.TrimSuffix(targetParentDir, string(filepath.Separator)) - } - targetPaths = append(targetPaths, targetParentDir) + fi, err := os.Stat(parentDir.srcPath) + if err != nil { + return errors.Wrapf(err, "failed to stat %s", src) + } + if !fi.IsDir() { + return errors.Errorf("%s is not a directory", parentDir.srcPath) } - for i := count; i > 0; i-- { - fi, err := os.Stat(srcPaths[i]) - if err != nil { - return errors.Wrapf(err, "failed to stat %s", src) - } - if !fi.IsDir() { - return errors.Errorf("%s is not a directory", srcPaths[i]) - } - created, err := copyDirectoryOnly(srcPaths[i], targetPaths[i], fi, overwriteTargetMetadata) - if err != nil { - return err + created, err := copyDirectoryOnly(parentDir.srcPath, parentDir.dstPath, fi, overwriteTargetMetadata) + if err != nil { + return err + } + if created { + if err := c.copyFileInfo(fi, parentDir.dstPath); err != nil { + return errors.Wrap(err, "failed to copy file info") } - if created { - if err := c.copyFileInfo(fi, targetPaths[i]); err != nil { - return errors.Wrap(err, "failed to copy file info") - } - if err := copyXAttrs(targetPaths[i], srcPaths[i], c.xattrErrorHandler); err != nil { - return errors.Wrap(err, "failed to copy xattrs") - } + if err := copyXAttrs(parentDir.dstPath, parentDir.srcPath, c.xattrErrorHandler); err != nil { + return errors.Wrap(err, "failed to copy xattrs") } } + + c.parentDirs[i].copied = true } return nil } -func (c *copier) copyDirectory(ctx context.Context, src, srcComponents, dst string, stat os.FileInfo, overwriteTargetMetadata, includeAll bool) (bool, error) { +func (c *copier) copyDirectory(ctx context.Context, src, srcComponents, dst string, stat os.FileInfo, overwriteTargetMetadata, skipIncludePatterns bool) (bool, error) { if !stat.IsDir() { return false, errors.Errorf("source is not directory") } @@ -470,7 +461,7 @@ func (c *copier) copyDirectory(ctx context.Context, src, srcComponents, dst stri // pattern exactly, go ahead and create the directory. Otherwise, delay to // handle include patterns like a/*/c where we do not want to create a/b // until we encounter a/b/c. - if includeAll { + if skipIncludePatterns { var err error created, err = copyDirectoryOnly(src, dst, stat, overwriteTargetMetadata) if err != nil { @@ -478,13 +469,23 @@ func (c *copier) copyDirectory(ctx context.Context, src, srcComponents, dst stri } } + c.parentDirs = append(c.parentDirs, parentDir{ + srcPath: src, + dstPath: dst, + copied: skipIncludePatterns, + }) + + defer func() { + c.parentDirs = c.parentDirs[:len(c.parentDirs)-1] + }() + fis, err := ioutil.ReadDir(src) if err != nil { return false, errors.Wrapf(err, "failed to read %s", src) } for _, fi := range fis { - if err := c.copy(ctx, filepath.Join(src, fi.Name()), filepath.Join(srcComponents, fi.Name()), filepath.Join(dst, fi.Name()), true, includeAll); err != nil { + if err := c.copy(ctx, filepath.Join(src, fi.Name()), filepath.Join(srcComponents, fi.Name()), filepath.Join(dst, fi.Name()), true, skipIncludePatterns); err != nil { return false, err } } diff --git a/vendor/github.com/tonistiigi/fsutil/prefix/match.go b/vendor/github.com/tonistiigi/fsutil/prefix/match.go index 8a1e81b39948c..8e263b8147c27 100644 --- a/vendor/github.com/tonistiigi/fsutil/prefix/match.go +++ b/vendor/github.com/tonistiigi/fsutil/prefix/match.go @@ -1,18 +1,28 @@ package prefix import ( + "path" "path/filepath" "strings" ) -func Match(pattern, name string) (bool, bool) { - count := strings.Count(name, string(filepath.Separator)) +func Match(pattern, name string, slashSeparator bool) (bool, bool) { + separator := filepath.Separator + if slashSeparator { + separator = '/' + } + count := strings.Count(name, string(separator)) partial := false - if strings.Count(pattern, string(filepath.Separator)) > count { - pattern = trimUntilIndex(pattern, string(filepath.Separator), count) + if strings.Count(pattern, string(separator)) > count { + pattern = trimUntilIndex(pattern, string(separator), count) partial = true } - m, _ := filepath.Match(pattern, name) + var m bool + if slashSeparator { + m, _ = path.Match(pattern, name) + } else { + m, _ = filepath.Match(pattern, name) + } return m, partial } diff --git a/vendor/github.com/tonistiigi/fsutil/walker.go b/vendor/github.com/tonistiigi/fsutil/walker.go index f3e377e682da2..cc6143db49c73 100644 --- a/vendor/github.com/tonistiigi/fsutil/walker.go +++ b/vendor/github.com/tonistiigi/fsutil/walker.go @@ -98,7 +98,7 @@ func Walk(ctx context.Context, p string, opt *WalkOpt, fn filepath.WalkFunc) err matched := false partial := true for _, pattern := range includePatterns { - if ok, p := prefix.Match(pattern, path); ok { + if ok, p := prefix.Match(pattern, path, false); ok { matched = true if !p { partial = false diff --git a/vendor/modules.txt b/vendor/modules.txt index 4e7e8e1991e30..be8af75951083 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -325,7 +325,7 @@ github.com/sirupsen/logrus # github.com/stretchr/testify v1.7.0 github.com/stretchr/testify/assert github.com/stretchr/testify/require -# github.com/tonistiigi/fsutil v0.0.0-20201103201449-0834f99b7b85 => github.com/aaronlehmann/fsutil v0.0.0-20210421230417-620ac87d5620 +# github.com/tonistiigi/fsutil v0.0.0-20201103201449-0834f99b7b85 => github.com/aaronlehmann/fsutil v0.0.0-20210428220603-accc0fae8749 github.com/tonistiigi/fsutil github.com/tonistiigi/fsutil/copy github.com/tonistiigi/fsutil/prefix