vendor: github.com/moby/buildkit 5ae9b23c40a9 (master / v0.13.0-dev)

full diff:

- 36ef4d8c0d...f098008783
- d5c1d785b0...5ae9b23c40

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2023-11-15 15:59:23 +01:00
parent 898a8eeddf
commit c855277d53
23 changed files with 644 additions and 196 deletions

View File

@@ -1,17 +1,21 @@
package fsutil
import (
"context"
gofs "io/fs"
"os"
"path/filepath"
"runtime"
"sort"
strings "strings"
"syscall"
"github.com/pkg/errors"
"github.com/tonistiigi/fsutil/types"
)
func FollowLinks(root string, paths []string) ([]string, error) {
r := &symlinkResolver{root: root, resolved: map[string]struct{}{}}
func FollowLinks(fs FS, paths []string) ([]string, error) {
r := &symlinkResolver{fs: fs, resolved: map[string]struct{}{}}
for _, p := range paths {
if err := r.append(p); err != nil {
return nil, err
@@ -26,7 +30,7 @@ func FollowLinks(root string, paths []string) ([]string, error) {
}
type symlinkResolver struct {
root string
fs FS
resolved map[string]struct{}
}
@@ -76,10 +80,9 @@ func (r *symlinkResolver) append(p string) error {
}
func (r *symlinkResolver) readSymlink(p string, allowWildcard bool) ([]string, error) {
realPath := filepath.Join(r.root, p)
base := filepath.Base(p)
if allowWildcard && containsWildcards(base) {
fis, err := os.ReadDir(filepath.Dir(realPath))
fis, err := readDir(r.fs, filepath.Dir(p))
if err != nil {
if isNotFound(err) {
return nil, nil
@@ -99,21 +102,30 @@ func (r *symlinkResolver) readSymlink(p string, allowWildcard bool) ([]string, e
return out, nil
}
fi, err := os.Lstat(realPath)
entry, err := statFile(r.fs, p)
if err != nil {
if isNotFound(err) {
return nil, nil
}
return nil, errors.WithStack(err)
}
if fi.Mode()&os.ModeSymlink == 0 {
if entry == nil {
return nil, nil
}
link, err := os.Readlink(realPath)
if entry.Type()&os.ModeSymlink == 0 {
return nil, nil
}
fi, err := entry.Info()
if err != nil {
return nil, errors.WithStack(err)
}
link = filepath.Clean(link)
stat, ok := fi.Sys().(*types.Stat)
if !ok {
return nil, errors.WithStack(&os.PathError{Path: p, Err: syscall.EBADMSG, Op: "fileinfo without stat info"})
}
link := filepath.Clean(stat.Linkname)
if filepath.IsAbs(link) {
return []string{link}, nil
}
@@ -122,6 +134,76 @@ func (r *symlinkResolver) readSymlink(p string, allowWildcard bool) ([]string, e
}, nil
}
func statFile(fs FS, root string) (os.DirEntry, error) {
var out os.DirEntry
root = filepath.Clean(root)
if root == "/" || root == "." {
return nil, nil
}
err := fs.Walk(context.TODO(), root, func(p string, entry os.DirEntry, err error) error {
if err != nil {
return err
}
if p != root {
return errors.Errorf("expected single entry %q but got %q", root, p)
}
out = entry
if entry.IsDir() {
return filepath.SkipDir
}
return nil
})
if err != nil {
return nil, err
}
if out == nil {
return nil, errors.Wrapf(os.ErrNotExist, "readFile %s", root)
}
return out, nil
}
func readDir(fs FS, root string) ([]os.DirEntry, error) {
var out []os.DirEntry
root = filepath.Clean(root)
if root == "/" || root == "." {
root = "."
out = make([]gofs.DirEntry, 0)
}
err := fs.Walk(context.TODO(), root, func(p string, entry os.DirEntry, err error) error {
if err != nil {
return err
}
if p == root {
if !entry.IsDir() {
return errors.WithStack(&os.PathError{Op: "walk", Path: root, Err: syscall.ENOTDIR})
}
out = make([]gofs.DirEntry, 0)
return nil
}
if out == nil {
return errors.Errorf("expected to read parent entry %q before child %q", root, p)
}
out = append(out, entry)
if entry.IsDir() {
return filepath.SkipDir
}
return nil
})
if err != nil {
return nil, err
}
if out == nil && root != "." {
return nil, errors.Wrapf(os.ErrNotExist, "readDir %s", root)
}
return out, nil
}
func containsWildcards(name string) bool {
isWindows := runtime.GOOS == "windows"
for i := 0; i < len(name); i++ {