Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust fileutils to use corrected MatchesUsingParentResults function #114

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bench/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ github.com/dnaeon/go-vcr v1.0.1/go.mod h1:aBB1+wY4s93YsC3HHjMBMrwTj2R9FHDzUr9KyG
github.com/docker/distribution v0.0.0-20190905152932-14b96e55d84c/go.mod h1:0+TTO4EOBfRPhZXAeF1Vu+W3hHZ8eLp8PgKVZlcvtFY=
github.com/docker/distribution v2.7.1-0.20190205005809-0d3efadf0154+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w=
github.com/docker/distribution v2.7.1+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w=
github.com/docker/docker v20.10.3-0.20210817025855-ba2adeebdb8d+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
github.com/docker/docker v20.10.3-0.20211124173851-93d560d5b3b0+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
github.com/docker/docker v20.10.7+incompatible h1:Z6O9Nhsjv+ayUEeI1IojKbYcsGdgYSNqxe1s2MYzUhQ=
github.com/docker/docker v20.10.7+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
github.com/docker/go-events v0.0.0-20170721190031-9461782956ad/go.mod h1:Uw6UezgYA44ePAFQYUehOuCzmy5zmg/+nl2ZfMWGkpA=
Expand Down
44 changes: 24 additions & 20 deletions copy/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func Copy(ctx context.Context, srcRoot, src, dstRoot, dst string, opts ...Opt) e
if err != nil {
return err
}
if err := c.copy(ctx, srcFollowed, "", dst, false, false, false); err != nil {
if err := c.copy(ctx, srcFollowed, "", dst, false, fileutils.MatchInfo{}, fileutils.MatchInfo{}); err != nil {
return err
}
}
Expand Down Expand Up @@ -284,7 +284,7 @@ func newCopier(root string, chown Chowner, tm *time.Time, mode *int, xeh XAttrEr
}

// dest is always clean
func (c *copier) copy(ctx context.Context, src, srcComponents, target string, overwriteTargetMetadata, parentMatchedInclude, parentMatchedExclude bool) error {
func (c *copier) copy(ctx context.Context, src, srcComponents, target string, overwriteTargetMetadata bool, parentIncludeMatchInfo, parentExcludeMatchInfo fileutils.MatchInfo) error {
select {
case <-ctx.Done():
return ctx.Err()
Expand All @@ -297,16 +297,20 @@ func (c *copier) copy(ctx context.Context, src, srcComponents, target string, ov
}

include := true
matchesIncludePattern := false
matchesExcludePattern := false
var (
includeMatchInfo fileutils.MatchInfo
excludeMatchInfo fileutils.MatchInfo
)
if srcComponents != "" {
matchesIncludePattern, err = c.include(srcComponents, fi, parentMatchedInclude)
matchesIncludePattern := false
matchesExcludePattern := false
matchesIncludePattern, includeMatchInfo, err = c.include(srcComponents, fi, parentIncludeMatchInfo)
if err != nil {
return err
}
include = matchesIncludePattern

matchesExcludePattern, err = c.exclude(srcComponents, fi, parentMatchedExclude)
matchesExcludePattern, excludeMatchInfo, err = c.exclude(srcComponents, fi, parentExcludeMatchInfo)
if err != nil {
return err
}
Expand Down Expand Up @@ -338,7 +342,7 @@ func (c *copier) copy(ctx context.Context, src, srcComponents, target string, ov
case fi.IsDir():
if created, err := c.copyDirectory(
ctx, src, srcComponents, target, fi, overwriteTargetMetadata,
include, matchesIncludePattern, matchesExcludePattern,
include, includeMatchInfo, excludeMatchInfo,
); err != nil {
return err
} else if !overwriteTargetMetadata || c.includePatternMatcher != nil {
Expand Down Expand Up @@ -400,28 +404,28 @@ func (c *copier) notifyChange(target string, fi os.FileInfo) error {
return nil
}

func (c *copier) include(path string, fi os.FileInfo, parentMatchedInclude bool) (bool, error) {
func (c *copier) include(path string, fi os.FileInfo, parentIncludeMatchInfo fileutils.MatchInfo) (bool, fileutils.MatchInfo, error) {
if c.includePatternMatcher == nil {
return true, nil
return true, fileutils.MatchInfo{}, nil
}

m, err := c.includePatternMatcher.MatchesUsingParentResult(path, parentMatchedInclude)
m, matchInfo, err := c.includePatternMatcher.MatchesUsingParentResults(path, parentIncludeMatchInfo)
if err != nil {
return false, errors.Wrap(err, "failed to match includepatterns")
return false, matchInfo, errors.Wrap(err, "failed to match includepatterns")
}
return m, nil
return m, matchInfo, nil
}

func (c *copier) exclude(path string, fi os.FileInfo, parentMatchedExclude bool) (bool, error) {
func (c *copier) exclude(path string, fi os.FileInfo, parentExcludeMatchInfo fileutils.MatchInfo) (bool, fileutils.MatchInfo, error) {
if c.excludePatternMatcher == nil {
return false, nil
return false, fileutils.MatchInfo{}, nil
}

m, err := c.excludePatternMatcher.MatchesUsingParentResult(path, parentMatchedExclude)
m, matchInfo, err := c.excludePatternMatcher.MatchesUsingParentResults(path, parentExcludeMatchInfo)
if err != nil {
return false, errors.Wrap(err, "failed to match excludepatterns")
return false, matchInfo, errors.Wrap(err, "failed to match excludepatterns")
}
return m, nil
return m, matchInfo, nil
}

// Delayed creation of parent directories when a file or dir matches an include
Expand Down Expand Up @@ -467,8 +471,8 @@ func (c *copier) copyDirectory(
stat os.FileInfo,
overwriteTargetMetadata bool,
include bool,
matchesIncludePattern bool,
matchesExcludePattern bool,
includeMatchInfo fileutils.MatchInfo,
excludeMatchInfo fileutils.MatchInfo,
) (bool, error) {
if !stat.IsDir() {
return false, errors.Errorf("source is not directory")
Expand Down Expand Up @@ -515,7 +519,7 @@ func (c *copier) copyDirectory(
ctx,
filepath.Join(src, fi.Name()), filepath.Join(srcComponents, fi.Name()),
filepath.Join(dst, fi.Name()),
true, matchesIncludePattern, matchesExcludePattern,
true, includeMatchInfo, excludeMatchInfo,
); err != nil {
return false, err
}
Expand Down
10 changes: 10 additions & 0 deletions copy/copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,16 @@ func TestCopyIncludeExclude(t *testing.T) {
opts: []Opt{WithExcludePattern("bar*"), WithExcludePattern("!bar/baz")},
expectedResults: []string{"bar", "bar/baz", "bar/baz/foo3", "foo2"},
},
{
name: "exclude **, !bar/baz",
opts: []Opt{WithExcludePattern("**"), WithExcludePattern("!bar/baz")},
expectedResults: []string{"bar", "bar/baz", "bar/baz/foo3"},
},
{
name: "exclude **, !bar/baz, bar/baz/foo3",
opts: []Opt{WithExcludePattern("**"), WithExcludePattern("!bar/baz"), WithExcludePattern("bar/baz/foo3")},
expectedResults: []string{"bar", "bar/baz"},
},
{
name: "include bar, exclude bar/baz",
opts: []Opt{WithIncludePattern("bar"), WithExcludePattern("bar/baz")},
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.13

require (
github.com/containerd/continuity v0.1.0
github.com/docker/docker v20.10.3-0.20210817025855-ba2adeebdb8d+incompatible // master (v21.xx-dev)
github.com/docker/docker v20.10.3-0.20211124173851-93d560d5b3b0+incompatible // master (v21.xx-dev)
github.com/gogo/protobuf v1.3.2
github.com/golang/protobuf v1.4.3 // indirect
github.com/google/go-cmp v0.5.2 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
github.com/docker/docker v20.10.3-0.20210817025855-ba2adeebdb8d+incompatible h1:tSd7TeZCH0j9m4P14bfe/eO1KYawrt3DztHI8gZAmLM=
github.com/docker/docker v20.10.3-0.20210817025855-ba2adeebdb8d+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
github.com/docker/docker v20.10.3-0.20211124173851-93d560d5b3b0+incompatible h1:/J7vHTKUYNaTlSBJ7vqJd/EYVAmTwzaOrIqKLXD0AKY=
github.com/docker/docker v20.10.3-0.20211124173851-93d560d5b3b0+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
Expand Down
47 changes: 15 additions & 32 deletions walker.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ func Walk(ctx context.Context, p string, opt *WalkOpt, fn filepath.WalkFunc) err
}

type visitedDir struct {
fi os.FileInfo
path string
origpath string
pathWithSep string
matchedInclude bool
matchedExclude bool
calledFn bool
fi os.FileInfo
path string
origpath string
pathWithSep string
includeMatchInfo fileutils.MatchInfo
excludeMatchInfo fileutils.MatchInfo
calledFn bool
}

// used only for include/exclude handling
Expand Down Expand Up @@ -127,17 +127,17 @@ func Walk(ctx context.Context, p string, opt *WalkOpt, fn filepath.WalkFunc) err
skip := false

if includeMatcher != nil {
var parentMatchedInclude *bool
var parentIncludeMatchInfo fileutils.MatchInfo
if len(parentDirs) != 0 {
parentMatchedInclude = &parentDirs[len(parentDirs)-1].matchedInclude
parentIncludeMatchInfo = parentDirs[len(parentDirs)-1].includeMatchInfo
}
m, err := matchesPatterns(includeMatcher, path, parentMatchedInclude)
m, matchInfo, err := includeMatcher.MatchesUsingParentResults(path, parentIncludeMatchInfo)
if err != nil {
return errors.Wrap(err, "failed to match includepatterns")
}

if fi.IsDir() {
dir.matchedInclude = m
dir.includeMatchInfo = matchInfo
}

if !m {
Expand All @@ -146,17 +146,17 @@ func Walk(ctx context.Context, p string, opt *WalkOpt, fn filepath.WalkFunc) err
}

if excludeMatcher != nil {
var parentMatchedExclude *bool
var parentExcludeMatchInfo fileutils.MatchInfo
if len(parentDirs) != 0 {
parentMatchedExclude = &parentDirs[len(parentDirs)-1].matchedExclude
parentExcludeMatchInfo = parentDirs[len(parentDirs)-1].excludeMatchInfo
}
m, err := matchesPatterns(excludeMatcher, path, parentMatchedExclude)
m, matchInfo, err := excludeMatcher.MatchesUsingParentResults(path, parentExcludeMatchInfo)
if err != nil {
return errors.Wrap(err, "failed to match excludepatterns")
}

if fi.IsDir() {
dir.matchedExclude = m
dir.excludeMatchInfo = matchInfo
}

if m {
Expand Down Expand Up @@ -228,23 +228,6 @@ func Walk(ctx context.Context, p string, opt *WalkOpt, fn filepath.WalkFunc) err
})
}

func matchesPatterns(pm *fileutils.PatternMatcher, path string, parentMatched *bool) (bool, error) {
var (
m bool
err error
)
if parentMatched != nil {
m, err = pm.MatchesUsingParentResult(path, *parentMatched)
} else {
m, err = pm.MatchesOrParentMatches(path)
}
if err != nil {
return false, err
}

return m, nil
}

type StatInfo struct {
*types.Stat
}
Expand Down