diff --git a/client/solve.go b/client/solve.go index f14d9c410d79..e58905044c7b 100644 --- a/client/solve.go +++ b/client/solve.go @@ -24,6 +24,7 @@ import ( "github.com/moby/buildkit/util/entitlements" ocispecs "github.com/opencontainers/image-spec/specs-go/v1" "github.com/pkg/errors" + "github.com/tonistiigi/fsutil" fstypes "github.com/tonistiigi/fsutil/types" "go.opentelemetry.io/otel/trace" "golang.org/x/sync/errgroup" @@ -342,10 +343,10 @@ func prepareSyncedDirs(def *llb.Definition, localDirs map[string]string) ([]file return nil, errors.Errorf("%s not a directory", d) } } - resetUIDAndGID := func(p string, st *fstypes.Stat) bool { + resetUIDAndGID := func(p string, st *fstypes.Stat) fsutil.MapResult { st.Uid = 0 st.Gid = 0 - return true + return fsutil.MapResultKeep } dirs := make([]filesync.SyncedDir, 0, len(localDirs)) diff --git a/exporter/local/export.go b/exporter/local/export.go index 5daa4aa4268c..b9169ac5b20c 100644 --- a/exporter/local/export.go +++ b/exporter/local/export.go @@ -93,17 +93,17 @@ func (e *localExporterInstance) Export(ctx context.Context, inp exporter.Source, walkOpt := &fsutil.WalkOpt{} if idmap != nil { - walkOpt.Map = func(p string, st *fstypes.Stat) bool { + walkOpt.Map = func(p string, st *fstypes.Stat) fsutil.MapResult { uid, gid, err := idmap.ToContainer(idtools.Identity{ UID: int(st.Uid), GID: int(st.Gid), }) if err != nil { - return false + return fsutil.MapResultExclude } st.Uid = uint32(uid) st.Gid = uint32(gid) - return true + return fsutil.MapResultKeep } } diff --git a/exporter/tar/export.go b/exporter/tar/export.go index 0febefd0b023..13540b947de7 100644 --- a/exporter/tar/export.go +++ b/exporter/tar/export.go @@ -109,17 +109,17 @@ func (e *localExporterInstance) Export(ctx context.Context, inp exporter.Source, walkOpt := &fsutil.WalkOpt{} if idmap != nil { - walkOpt.Map = func(p string, st *fstypes.Stat) bool { + walkOpt.Map = func(p string, st *fstypes.Stat) fsutil.MapResult { uid, gid, err := idmap.ToContainer(idtools.Identity{ UID: int(st.Uid), GID: int(st.Gid), }) if err != nil { - return false + return fsutil.MapResultExclude } st.Uid = uint32(uid) st.Gid = uint32(gid) - return true + return fsutil.MapResultKeep } } diff --git a/go.mod b/go.mod index 6356c368f45c..8441530d5f3e 100644 --- a/go.mod +++ b/go.mod @@ -49,7 +49,7 @@ require ( github.com/serialx/hashring v0.0.0-20190422032157-8b2912629002 github.com/sirupsen/logrus v1.8.1 github.com/stretchr/testify v1.7.0 - github.com/tonistiigi/fsutil v0.0.0-20220413024721-3c5c7e848994 + github.com/tonistiigi/fsutil v0.0.0-20220510150904-0dbf3a8a7d58 github.com/tonistiigi/go-actions-cache v0.0.0-20220404170428-0bdeb6e1eac7 github.com/tonistiigi/go-archvariant v1.0.0 github.com/tonistiigi/units v0.0.0-20180711220420-6950e57a87ea diff --git a/go.sum b/go.sum index 375197e74531..2758499d4da5 100644 --- a/go.sum +++ b/go.sum @@ -1241,8 +1241,8 @@ github.com/tmc/grpc-websocket-proxy v0.0.0-20201229170055-e5319fda7802/go.mod h1 github.com/tommy-muehle/go-mnd v1.1.1/go.mod h1:dSUh0FtTP8VhvkL1S+gUR1OKd9ZnSaozuI6r3m6wOig= github.com/tommy-muehle/go-mnd v1.3.1-0.20200224220436-e6f9a994e8fa/go.mod h1:dSUh0FtTP8VhvkL1S+gUR1OKd9ZnSaozuI6r3m6wOig= github.com/tonistiigi/fsutil v0.0.0-20201103201449-0834f99b7b85/go.mod h1:a7cilN64dG941IOXfhJhlH0qB92hxJ9A1ewrdUmJ6xo= -github.com/tonistiigi/fsutil v0.0.0-20220413024721-3c5c7e848994 h1:kui3WK5NgTQUD0NdxsCohHLXaN43AzLBI0BES0uoaYQ= -github.com/tonistiigi/fsutil v0.0.0-20220413024721-3c5c7e848994/go.mod h1:oPAfvw32vlUJSjyDcQ3Bu0nb2ON2B+G0dtVN/SZNJiA= +github.com/tonistiigi/fsutil v0.0.0-20220510150904-0dbf3a8a7d58 h1:rNya5ozLqz0lK46XhmsmqJuxmQLM8dAWabiT+5gZqLY= +github.com/tonistiigi/fsutil v0.0.0-20220510150904-0dbf3a8a7d58/go.mod h1:oPAfvw32vlUJSjyDcQ3Bu0nb2ON2B+G0dtVN/SZNJiA= github.com/tonistiigi/go-actions-cache v0.0.0-20220404170428-0bdeb6e1eac7 h1:8eY6m1mjgyB8XySUR7WvebTM8D/Vs86jLJzD/Tw7zkc= github.com/tonistiigi/go-actions-cache v0.0.0-20220404170428-0bdeb6e1eac7/go.mod h1:qqvyZqkfwkoJuPU/bw61bItaoO0SJ8YSW0vSVRRvsRg= github.com/tonistiigi/go-archvariant v1.0.0 h1:5LC1eDWiBNflnTF1prCiX09yfNHIxDC/aukdhCdTyb0= diff --git a/session/filesync/filesync.go b/session/filesync/filesync.go index ae3f29f86c3d..be8cf9b8b62c 100644 --- a/session/filesync/filesync.go +++ b/session/filesync/filesync.go @@ -36,7 +36,7 @@ type SyncedDir struct { Name string Dir string Excludes []string - Map func(string, *fstypes.Stat) bool + Map func(string, *fstypes.Stat) fsutil.MapResult } // NewFSSyncProvider creates a new provider for sending files from client diff --git a/vendor/github.com/tonistiigi/fsutil/walker.go b/vendor/github.com/tonistiigi/fsutil/walker.go index 83045bec850f..c206b1a6aea8 100644 --- a/vendor/github.com/tonistiigi/fsutil/walker.go +++ b/vendor/github.com/tonistiigi/fsutil/walker.go @@ -19,9 +19,29 @@ type WalkOpt struct { // FollowPaths contains symlinks that are resolved into include patterns // before performing the fs walk FollowPaths []string - Map FilterFunc + Map MapFunc } +type MapFunc func(string, *types.Stat) MapResult + +// The result of the walk function controls +// both how WalkDir continues and whether the path is kept. +type MapResult int + +const ( + // Keep the current path and continue. + MapResultKeep MapResult = iota + + // Exclude the current path and continue. + MapResultExclude + + // Exclude the current path, and skip the rest of the dir. + // If path is a dir, skip the current directory. + // If path is a file, skip the rest of the parent directory. + // (This matches the semantics of fs.SkipDir.) + MapResultSkipDir +) + func Walk(ctx context.Context, p string, opt *WalkOpt, fn filepath.WalkFunc) error { root, err := filepath.EvalSymlinks(p) if err != nil { @@ -258,7 +278,10 @@ func Walk(ctx context.Context, p string, opt *WalkOpt, fn filepath.WalkFunc) err return ctx.Err() default: if opt != nil && opt.Map != nil { - if allowed := opt.Map(stat.Path, stat); !allowed { + result := opt.Map(stat.Path, stat) + if result == MapResultSkipDir { + return filepath.SkipDir + } else if result == MapResultExclude { return nil } } @@ -277,7 +300,8 @@ func Walk(ctx context.Context, p string, opt *WalkOpt, fn filepath.WalkFunc) err default: } if opt != nil && opt.Map != nil { - if allowed := opt.Map(parentStat.Path, parentStat); !allowed { + result := opt.Map(parentStat.Path, parentStat) + if result == MapResultSkipDir || result == MapResultExclude { continue } } diff --git a/vendor/modules.txt b/vendor/modules.txt index 1edef8a0e900..172374d334fb 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -449,7 +449,7 @@ github.com/sirupsen/logrus ## explicit; go 1.13 github.com/stretchr/testify/assert github.com/stretchr/testify/require -# github.com/tonistiigi/fsutil v0.0.0-20220413024721-3c5c7e848994 +# github.com/tonistiigi/fsutil v0.0.0-20220510150904-0dbf3a8a7d58 ## explicit; go 1.13 github.com/tonistiigi/fsutil github.com/tonistiigi/fsutil/copy