From 6c14050b3882d97f503dc72148dcd74764d16f79 Mon Sep 17 00:00:00 2001 From: CrazyMax <1951866+crazy-max@users.noreply.github.com> Date: Sat, 10 Feb 2024 11:54:14 +0100 Subject: [PATCH] vendor: github.com/crazy-max/fsutil@fs-root-getter Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com> --- build/build.go | 17 +++++++------- build/fs.go | 21 ------------------ build/git.go | 6 ++++- build/git_test.go | 6 ++--- go.mod | 2 ++ go.sum | 4 ++-- .../github.com/tonistiigi/fsutil/.gitignore | 6 +---- .../tonistiigi/fsutil/.golangci.yml | 16 ++++++++++++++ .../github.com/tonistiigi/fsutil/Dockerfile | 10 +++++++-- .../tonistiigi/fsutil/diff_containerd.go | 7 +++--- .../tonistiigi/fsutil/docker-bake.hcl | 10 +++++++-- vendor/github.com/tonistiigi/fsutil/filter.go | 6 ++--- .../tonistiigi/fsutil/followlinks.go | 8 +++---- vendor/github.com/tonistiigi/fsutil/fs.go | 10 +++++++++ vendor/github.com/tonistiigi/fsutil/readme.md | 5 +++++ vendor/github.com/tonistiigi/fsutil/stat.go | 2 +- .../github.com/tonistiigi/fsutil/validator.go | 22 ++++++++----------- vendor/modules.txt | 3 ++- 18 files changed, 92 insertions(+), 69 deletions(-) delete mode 100644 build/fs.go create mode 100644 vendor/github.com/tonistiigi/fsutil/.golangci.yml diff --git a/build/build.go b/build/build.go index aacd445de89b..c6de32db33c2 100644 --- a/build/build.go +++ b/build/build.go @@ -387,11 +387,12 @@ func toSolveOpt(ctx context.Context, node builder.Node, multiDriver bool, opt Op defers = append(defers, releaseLoad) if lm, ok := so.LocalMounts["context"]; ok { - sharedKey := lm.(fs).root - if p, err := filepath.Abs(sharedKey); err == nil { - sharedKey = filepath.Base(p) + if sharedKey, ok := fsutil.GetRoot(lm); ok && sharedKey != "" { + if p, err := filepath.Abs(sharedKey); err == nil { + sharedKey = filepath.Base(p) + } + so.SharedKey = sharedKey + ":" + tryNodeIdentifier(configDir) } - so.SharedKey = sharedKey + ":" + tryNodeIdentifier(configDir) } if opt.Pull { @@ -1147,7 +1148,7 @@ func LoadInputs(ctx context.Context, d *driver.DriverHandle, inp Inputs, pw prog dockerfileReader = buf inp.ContextPath, _ = os.MkdirTemp("", "empty-dir") toRemove = append(toRemove, inp.ContextPath) - lm, err := newFS(inp.ContextPath) + lm, err := fsutil.NewFS(inp.ContextPath) if err != nil { return nil, err } else { @@ -1156,7 +1157,7 @@ func LoadInputs(ctx context.Context, d *driver.DriverHandle, inp Inputs, pw prog } } case isLocalDir(inp.ContextPath): - lm, err := newFS(inp.ContextPath) + lm, err := fsutil.NewFS(inp.ContextPath) if err != nil { return nil, err } @@ -1212,7 +1213,7 @@ func LoadInputs(ctx context.Context, d *driver.DriverHandle, inp Inputs, pw prog } if dockerfileDir != "" { - lm, err := newFS(dockerfileDir) + lm, err := fsutil.NewFS(dockerfileDir) if err != nil { return nil, err } @@ -1311,7 +1312,7 @@ func LoadInputs(ctx context.Context, d *driver.DriverHandle, inp Inputs, pw prog if k == "context" || k == "dockerfile" { localName = "_" + k // underscore to avoid collisions } - lm, err := newFS(v.Path) + lm, err := fsutil.NewFS(v.Path) if err != nil { return nil, err } else { diff --git a/build/fs.go b/build/fs.go deleted file mode 100644 index 0ad793e258cb..000000000000 --- a/build/fs.go +++ /dev/null @@ -1,21 +0,0 @@ -package build - -import ( - "github.com/tonistiigi/fsutil" -) - -type fs struct { - fsutil.FS - root string -} - -func newFS(root string) (fs, error) { - f, err := fsutil.NewFS(root) - if err != nil { - return fs{}, err - } - return fs{ - FS: f, - root: root, - }, nil -} diff --git a/build/git.go b/build/git.go index 79c3ba056db6..5b9220b0d0e0 100644 --- a/build/git.go +++ b/build/git.go @@ -12,6 +12,7 @@ import ( "github.com/moby/buildkit/client" specs "github.com/opencontainers/image-spec/specs-go/v1" "github.com/pkg/errors" + "github.com/tonistiigi/fsutil" ) const DockerfileLabel = "com.docker.image.source.entrypoint" @@ -116,7 +117,10 @@ func getGitAttributes(ctx context.Context, contextPath string, dockerfilePath st return } for k, lm := range so.LocalMounts { - dir := lm.(fs).root + dir, ok := fsutil.GetRoot(lm) + if !ok || dir == "" { + continue + } dir, err = filepath.EvalSymlinks(dir) if err != nil { continue diff --git a/build/git_test.go b/build/git_test.go index 85ba7e4c4d3f..b5e21b2bb9d6 100644 --- a/build/git_test.go +++ b/build/git_test.go @@ -160,7 +160,7 @@ func TestGetGitAttributesDirty(t *testing.T) { func TestLocalDirs(t *testing.T) { setupTest(t) - wdfs, err := newFS(".") + wdfs, err := fsutil.NewFS(".") require.NoError(t, err) so := &client.SolveOpt{ @@ -197,10 +197,10 @@ func TestLocalDirsSub(t *testing.T) { gitutil.GitCommit(c, t, "initial commit") gitutil.GitSetRemote(c, t, "origin", "git@github.com:docker/buildx.git") - ctxfs, err := newFS(".") + ctxfs, err := fsutil.NewFS(".") require.NoError(t, err) - dockerfilefs, err := newFS("app") + dockerfilefs, err := fsutil.NewFS("app") require.NoError(t, err) so := &client.SolveOpt{ diff --git a/go.mod b/go.mod index 50deb4892a15..fd461980c689 100644 --- a/go.mod +++ b/go.mod @@ -173,3 +173,5 @@ require ( sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect sigs.k8s.io/yaml v1.3.0 // indirect ) + +replace github.com/tonistiigi/fsutil => github.com/crazy-max/fsutil v0.0.0-20240210105035-b48ba0a28213 diff --git a/go.sum b/go.sum index 822ec52cb91d..7c0911fc7ebe 100644 --- a/go.sum +++ b/go.sum @@ -112,6 +112,8 @@ github.com/containerd/ttrpc v1.2.2/go.mod h1:sIT6l32Ph/H9cvnJsfXM5drIVzTr5A2flTf github.com/containerd/typeurl/v2 v2.1.1 h1:3Q4Pt7i8nYwy2KmQWIw2+1hTvwTE/6w9FqcttATPO/4= github.com/containerd/typeurl/v2 v2.1.1/go.mod h1:IDp2JFvbwZ31H8dQbEIY7sDl2L3o3HZj1hsSQlywkQ0= github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/crazy-max/fsutil v0.0.0-20240210105035-b48ba0a28213 h1:V3gWlzhVvrZmytk4AmrNy+Xk8J3c6LgB+o5owRqUU4Y= +github.com/crazy-max/fsutil v0.0.0-20240210105035-b48ba0a28213/go.mod h1:9kMVqMyQ/Sx2df5LtnGG+nbrmiZzCS7V6gjW3oGHsvI= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/creack/pty v1.1.18 h1:n56/Zwd5o6whRC5PMGretI4IdRLlmBXYNjScPaBgsbY= github.com/creack/pty v1.1.18/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4= @@ -452,8 +454,6 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/theupdateframework/notary v0.7.0 h1:QyagRZ7wlSpjT5N2qQAh/pN+DVqgekv4DzbAiAiEL3c= github.com/theupdateframework/notary v0.7.0/go.mod h1:c9DRxcmhHmVLDay4/2fUYdISnHqbFDGRSlXPO0AhYWw= -github.com/tonistiigi/fsutil v0.0.0-20230825212630-f09800878302 h1:ZT8ibgassurSISJ1Pj26NsM3vY2jxFZn63Nd/TpHmRw= -github.com/tonistiigi/fsutil v0.0.0-20230825212630-f09800878302/go.mod h1:9kMVqMyQ/Sx2df5LtnGG+nbrmiZzCS7V6gjW3oGHsvI= github.com/tonistiigi/units v0.0.0-20180711220420-6950e57a87ea h1:SXhTLE6pb6eld/v/cCndK0AMpt1wiVFb/YYmqB3/QG0= github.com/tonistiigi/units v0.0.0-20180711220420-6950e57a87ea/go.mod h1:WPnis/6cRcDZSUvVmezrxJPkiO87ThFYsoUiMwWNDJk= github.com/tonistiigi/vt100 v0.0.0-20230623042737-f9a4f7ef6531 h1:Y/M5lygoNPKwVNLMPXgVfsRT40CSFKXCxuU8LoHySjs= diff --git a/vendor/github.com/tonistiigi/fsutil/.gitignore b/vendor/github.com/tonistiigi/fsutil/.gitignore index 51b9602c84a3..e5c343e2d7ba 100644 --- a/vendor/github.com/tonistiigi/fsutil/.gitignore +++ b/vendor/github.com/tonistiigi/fsutil/.gitignore @@ -1,9 +1,5 @@ # if you want to ignore files created by your editor/tools, consider using a # global .gitignore or .git/info/exclude see https://help.github.com/articles/ignoring-files -.* -!.github -!.gitignore -!.travis.yml -*.prof +bin/ # support running go modules in vendor mode for local development vendor/ diff --git a/vendor/github.com/tonistiigi/fsutil/.golangci.yml b/vendor/github.com/tonistiigi/fsutil/.golangci.yml new file mode 100644 index 000000000000..2a8b86ae2860 --- /dev/null +++ b/vendor/github.com/tonistiigi/fsutil/.golangci.yml @@ -0,0 +1,16 @@ +run: + timeout: 10m + skip-files: + - ".*\\.pb\\.go$" + +linters: + enable: + - gofmt + - govet + - goimports + - ineffassign + - misspell + - unused + - staticcheck + - typecheck + disable-all: true diff --git a/vendor/github.com/tonistiigi/fsutil/Dockerfile b/vendor/github.com/tonistiigi/fsutil/Dockerfile index 9584648d0583..5fd395020d5f 100644 --- a/vendor/github.com/tonistiigi/fsutil/Dockerfile +++ b/vendor/github.com/tonistiigi/fsutil/Dockerfile @@ -18,13 +18,19 @@ FROM base AS test ARG TESTFLAGS RUN --mount=target=. --mount=target=/go/pkg/mod,type=cache \ --mount=target=/root/.cache,type=cache \ - CGO_ENABLED=0 xx-go test -test.v ${TESTFLAGS} ./... + CGO_ENABLED=0 xx-go test -v -coverprofile=/tmp/coverage.txt -covermode=atomic ${TESTFLAGS} ./... FROM base AS test-noroot RUN mkdir /go/pkg && chmod 0777 /go/pkg USER 1000:1000 RUN --mount=target=. \ --mount=target=/tmp/.cache,type=cache \ - CGO_ENABLED=0 GOCACHE=/tmp/gocache xx-go test -test.v ./... + CGO_ENABLED=0 GOCACHE=/tmp/gocache xx-go test -v -coverprofile=/tmp/coverage.txt -covermode=atomic ./... + +FROM scratch AS test-coverage +COPY --from=test /tmp/coverage.txt /coverage-root.txt + +FROM scratch AS test-noroot-coverage +COPY --from=test-noroot /tmp/coverage.txt /coverage-noroot.txt FROM build diff --git a/vendor/github.com/tonistiigi/fsutil/diff_containerd.go b/vendor/github.com/tonistiigi/fsutil/diff_containerd.go index d8619abf1c08..84fdc89dc5bf 100644 --- a/vendor/github.com/tonistiigi/fsutil/diff_containerd.go +++ b/vendor/github.com/tonistiigi/fsutil/diff_containerd.go @@ -5,6 +5,7 @@ import ( "context" "io" "os" + "path/filepath" "strings" "github.com/tonistiigi/fsutil/types" @@ -110,7 +111,7 @@ func doubleWalkDiff(ctx context.Context, changeFn ChangeFunc, a, b walkerFn, fil if filter != nil { filter(f2.path, &statCopy) } - f2copy = ¤tPath{path: f2.path, stat: &statCopy} + f2copy = ¤tPath{path: filepath.FromSlash(f2.path), stat: &statCopy} } k, p := pathChange(f1, f2copy) switch k { @@ -127,7 +128,7 @@ func doubleWalkDiff(ctx context.Context, changeFn ChangeFunc, a, b walkerFn, fil f1 = nil continue } else if rmdir == "" && f1.stat.IsDir() { - rmdir = f1.path + string(os.PathSeparator) + rmdir = f1.path + string(filepath.Separator) } else if rmdir != "" { rmdir = "" } @@ -138,7 +139,7 @@ func doubleWalkDiff(ctx context.Context, changeFn ChangeFunc, a, b walkerFn, fil return err } if f1.stat.IsDir() && !f2copy.stat.IsDir() { - rmdir = f1.path + string(os.PathSeparator) + rmdir = f1.path + string(filepath.Separator) } else if rmdir != "" { rmdir = "" } diff --git a/vendor/github.com/tonistiigi/fsutil/docker-bake.hcl b/vendor/github.com/tonistiigi/fsutil/docker-bake.hcl index 6ba3c867247c..7df6d2096c34 100644 --- a/vendor/github.com/tonistiigi/fsutil/docker-bake.hcl +++ b/vendor/github.com/tonistiigi/fsutil/docker-bake.hcl @@ -2,6 +2,10 @@ variable "GO_VERSION" { default = "1.20" } +variable "DESTDIR" { + default = "./bin" +} + group "default" { targets = ["build"] } @@ -18,12 +22,14 @@ group "test" { target "test-root" { inherits = ["build"] - target = "test" + target = "test-coverage" + output = ["${DESTDIR}/coverage"] } target "test-noroot" { inherits = ["build"] - target = "test-noroot" + target = "test-noroot-coverage" + output = ["${DESTDIR}/coverage"] } target "lint" { diff --git a/vendor/github.com/tonistiigi/fsutil/filter.go b/vendor/github.com/tonistiigi/fsutil/filter.go index 05f4a466f9ff..5257c285d8cf 100644 --- a/vendor/github.com/tonistiigi/fsutil/filter.go +++ b/vendor/github.com/tonistiigi/fsutil/filter.go @@ -66,9 +66,9 @@ type filterFS struct { // NewFilterFS creates a new FS that filters the given FS using the given // FilterOpt. - +// // The returned FS will not contain any paths that do not match the provided -// include and exclude patterns, or that are are exlcluded using the mapping +// include and exclude patterns, or that are are excluded using the mapping // function. // // The FS is assumed to be a snapshot of the filesystem at the time of the @@ -96,7 +96,7 @@ func NewFilterFS(fs FS, opt *FilterOpt) (FS, error) { } patternChars := "*[]?^" - if os.PathSeparator != '\\' { + if filepath.Separator != '\\' { patternChars += `\` } diff --git a/vendor/github.com/tonistiigi/fsutil/followlinks.go b/vendor/github.com/tonistiigi/fsutil/followlinks.go index 3a0bd2bb790a..559291d79d9a 100644 --- a/vendor/github.com/tonistiigi/fsutil/followlinks.go +++ b/vendor/github.com/tonistiigi/fsutil/followlinks.go @@ -137,8 +137,8 @@ func (r *symlinkResolver) readSymlink(p string, allowWildcard bool) ([]string, e func statFile(fs FS, root string) (os.DirEntry, error) { var out os.DirEntry - root = filepath.Clean(root) - if root == "/" || root == "." { + root = filepath.FromSlash(filepath.Clean(root)) + if root == string(filepath.Separator) || root == "." { return nil, nil } @@ -168,8 +168,8 @@ func statFile(fs FS, root string) (os.DirEntry, error) { func readDir(fs FS, root string) ([]os.DirEntry, error) { var out []os.DirEntry - root = filepath.Clean(root) - if root == "/" || root == "." { + root = filepath.FromSlash(filepath.Clean(root)) + if root == string(filepath.Separator) || root == "." { root = "." out = make([]gofs.DirEntry, 0) } diff --git a/vendor/github.com/tonistiigi/fsutil/fs.go b/vendor/github.com/tonistiigi/fsutil/fs.go index fe370194bed3..007f8f5c13c9 100644 --- a/vendor/github.com/tonistiigi/fsutil/fs.go +++ b/vendor/github.com/tonistiigi/fsutil/fs.go @@ -44,6 +44,16 @@ type fs struct { root string } +// GetRoot returns root dir for FS implementations created by NewFS. +// Returns an empty string and false if the FS implementation does not have a +// root path. +func GetRoot(fsys FS) (string, bool) { + if fsi, ok := fsys.(*fs); ok { + return fsi.root, true + } + return "", false +} + func (fs *fs) Walk(ctx context.Context, target string, fn gofs.WalkDirFunc) error { seenFiles := make(map[uint64]string) return filepath.WalkDir(filepath.Join(fs.root, target), func(path string, dirEntry gofs.DirEntry, walkErr error) (retErr error) { diff --git a/vendor/github.com/tonistiigi/fsutil/readme.md b/vendor/github.com/tonistiigi/fsutil/readme.md index 5ce685b7edc7..c417168876fa 100644 --- a/vendor/github.com/tonistiigi/fsutil/readme.md +++ b/vendor/github.com/tonistiigi/fsutil/readme.md @@ -1,3 +1,8 @@ +[![PkgGoDev](https://img.shields.io/badge/go.dev-docs-007d9c?style=flat-square&logo=go&logoColor=white)](https://pkg.go.dev/github.com/tonistiigi/fsutil) +[![CI Status](https://img.shields.io/github/actions/workflow/status/tonistiigi/fsutil/ci.yml?label=ci&logo=github&style=flat-square)](https://github.com/tonistiigi/fsutil/actions?query=workflow%3Aci) +[![Go Report Card](https://goreportcard.com/badge/github.com/tonistiigi/fsutil?style=flat-square)](https://goreportcard.com/report/github.com/tonistiigi/fsutil) +[![Codecov](https://img.shields.io/codecov/c/github/tonistiigi/fsutil?logo=codecov&style=flat-square)](https://codecov.io/gh/tonistiigi/fsutil) + Incremental file directory sync tools in golang. ``` diff --git a/vendor/github.com/tonistiigi/fsutil/stat.go b/vendor/github.com/tonistiigi/fsutil/stat.go index 2ab8da118e2c..44441cb65696 100644 --- a/vendor/github.com/tonistiigi/fsutil/stat.go +++ b/vendor/github.com/tonistiigi/fsutil/stat.go @@ -19,7 +19,7 @@ func mkstat(path, relpath string, fi os.FileInfo, inodemap map[uint64]string) (* relpath = filepath.ToSlash(relpath) stat := &types.Stat{ - Path: relpath, + Path: filepath.FromSlash(relpath), Mode: uint32(fi.Mode()), ModTime: fi.ModTime().UnixNano(), } diff --git a/vendor/github.com/tonistiigi/fsutil/validator.go b/vendor/github.com/tonistiigi/fsutil/validator.go index 9bd7d94d3643..8b0dd91ec4df 100644 --- a/vendor/github.com/tonistiigi/fsutil/validator.go +++ b/vendor/github.com/tonistiigi/fsutil/validator.go @@ -2,8 +2,7 @@ package fsutil import ( "os" - "path" - "runtime" + "path/filepath" "sort" "strings" "syscall" @@ -28,21 +27,18 @@ func (v *Validator) HandleChange(kind ChangeKind, p string, fi os.FileInfo, err if v.parentDirs == nil { v.parentDirs = make([]parent, 1, 10) } - if runtime.GOOS == "windows" { - p = strings.Replace(p, "\\", "", -1) - } - if p != path.Clean(p) { + if p != filepath.Clean(p) { return errors.WithStack(&os.PathError{Path: p, Err: syscall.EINVAL, Op: "unclean path"}) } - if path.IsAbs(p) { + if filepath.IsAbs(p) { return errors.WithStack(&os.PathError{Path: p, Err: syscall.EINVAL, Op: "absolute path"}) } - dir := path.Dir(p) - base := path.Base(p) + dir := filepath.Dir(p) + base := filepath.Base(p) if dir == "." { dir = "" } - if dir == ".." || strings.HasPrefix(p, "../") { + if dir == ".." || strings.HasPrefix(p, filepath.FromSlash("../")) { return errors.WithStack(&os.PathError{Path: p, Err: syscall.EINVAL, Op: "escape check"}) } @@ -56,12 +52,12 @@ func (v *Validator) HandleChange(kind ChangeKind, p string, fi os.FileInfo, err } if dir != v.parentDirs[len(v.parentDirs)-1].dir || v.parentDirs[i].last >= base { - return errors.Errorf("changes out of order: %q %q", p, path.Join(v.parentDirs[i].dir, v.parentDirs[i].last)) + return errors.Errorf("changes out of order: %q %q", p, filepath.Join(v.parentDirs[i].dir, v.parentDirs[i].last)) } v.parentDirs[i].last = base if kind != ChangeKindDelete && fi.IsDir() { v.parentDirs = append(v.parentDirs, parent{ - dir: path.Join(dir, base), + dir: filepath.Join(dir, base), last: "", }) } @@ -76,7 +72,7 @@ func ComparePath(p1, p2 string) int { switch { case p1[i] == p2[i]: continue - case p2[i] != '/' && p1[i] < p2[i] || p1[i] == '/': + case p2[i] != filepath.Separator && p1[i] < p2[i] || p1[i] == filepath.Separator: return -1 default: return 1 diff --git a/vendor/modules.txt b/vendor/modules.txt index d94fee249907..90500e81634d 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -699,7 +699,7 @@ github.com/theupdateframework/notary/tuf/data github.com/theupdateframework/notary/tuf/signed github.com/theupdateframework/notary/tuf/utils github.com/theupdateframework/notary/tuf/validation -# github.com/tonistiigi/fsutil v0.0.0-20230825212630-f09800878302 +# github.com/tonistiigi/fsutil v0.0.0-20230825212630-f09800878302 => github.com/crazy-max/fsutil v0.0.0-20240210105035-b48ba0a28213 ## explicit; go 1.19 github.com/tonistiigi/fsutil github.com/tonistiigi/fsutil/types @@ -1305,3 +1305,4 @@ sigs.k8s.io/structured-merge-diff/v4/value # sigs.k8s.io/yaml v1.3.0 ## explicit; go 1.12 sigs.k8s.io/yaml +# github.com/tonistiigi/fsutil => github.com/crazy-max/fsutil v0.0.0-20240210105035-b48ba0a28213