Skip to content

Commit

Permalink
Use target platform as a flag when handling paths
Browse files Browse the repository at this point in the history
Signed-off-by: Gabriel Adrian Samfira <[email protected]>
  • Loading branch information
gabriel-samfira committed Apr 7, 2023
1 parent c379286 commit 7abc3ed
Show file tree
Hide file tree
Showing 11 changed files with 287 additions and 252 deletions.
79 changes: 28 additions & 51 deletions client/llb/fileop.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package llb
import (
"context"
_ "crypto/sha256" // for opencontainers/go-digest
"fmt"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -55,7 +54,7 @@ type CopyInput interface {
}

type subAction interface {
toProtoAction(context.Context, string, pb.InputIndex) (pb.IsFileAction, error)
toProtoAction(ctx context.Context, parent string, base pb.InputIndex, platform string) (pb.IsFileAction, error)
}

type capAdder interface {
Expand Down Expand Up @@ -153,8 +152,8 @@ type fileActionMkdir struct {
info MkdirInfo
}

func (a *fileActionMkdir) toProtoAction(ctx context.Context, parent string, base pb.InputIndex) (pb.IsFileAction, error) {
normalizedPath, err := normalizePath(parent, a.file, false)
func (a *fileActionMkdir) toProtoAction(ctx context.Context, parent string, base pb.InputIndex, platform string) (pb.IsFileAction, error) {
normalizedPath, err := system.NormalizePath(parent, a.file, platform, false)
if err != nil {
return nil, errors.Wrap(err, "normalizing path")
}
Expand Down Expand Up @@ -326,8 +325,8 @@ type fileActionMkfile struct {
info MkfileInfo
}

func (a *fileActionMkfile) toProtoAction(ctx context.Context, parent string, base pb.InputIndex) (pb.IsFileAction, error) {
normalizedPath, err := normalizePath(parent, a.file, false)
func (a *fileActionMkfile) toProtoAction(ctx context.Context, parent string, base pb.InputIndex, platform string) (pb.IsFileAction, error) {
normalizedPath, err := system.NormalizePath(parent, a.file, platform, false)
if err != nil {
return nil, errors.Wrap(err, "normalizing path")
}
Expand Down Expand Up @@ -394,8 +393,8 @@ type fileActionRm struct {
info RmInfo
}

func (a *fileActionRm) toProtoAction(ctx context.Context, parent string, base pb.InputIndex) (pb.IsFileAction, error) {
normalizedPath, err := normalizePath(parent, a.file, false)
func (a *fileActionRm) toProtoAction(ctx context.Context, parent string, base pb.InputIndex, platform string) (pb.IsFileAction, error) {
normalizedPath, err := system.NormalizePath(parent, a.file, platform, false)
if err != nil {
return nil, errors.Wrap(err, "normalizing path")
}
Expand Down Expand Up @@ -469,12 +468,12 @@ type fileActionCopy struct {
info CopyInfo
}

func (a *fileActionCopy) toProtoAction(ctx context.Context, parent string, base pb.InputIndex) (pb.IsFileAction, error) {
src, err := a.sourcePath(ctx)
func (a *fileActionCopy) toProtoAction(ctx context.Context, parent string, base pb.InputIndex, platform string) (pb.IsFileAction, error) {
src, err := a.sourcePath(ctx, platform)
if err != nil {
return nil, err
}
normalizedPath, err := normalizePath(parent, a.dest, true)
normalizedPath, err := system.NormalizePath(parent, a.dest, platform, false)
if err != nil {
return nil, errors.Wrap(err, "normalizing path")
}
Expand Down Expand Up @@ -502,21 +501,22 @@ func (a *fileActionCopy) toProtoAction(ctx context.Context, parent string, base
}, nil
}

func (a *fileActionCopy) sourcePath(ctx context.Context) (string, error) {
func (a *fileActionCopy) sourcePath(ctx context.Context, platform string) (string, error) {
p := filepath.Clean(a.src)
if !system.IsAbs(p) {
if !system.IsAbs(p, platform) {
var dir string
var err error
if a.state != nil {
dir, err := a.state.GetDir(ctx)
if err != nil {
return "", err
}
p = filepath.Join("/", dir, p)
dir, err = a.state.GetDir(ctx)
} else if a.fas != nil {
dir, err := a.fas.state.GetDir(ctx)
if err != nil {
return "", err
}
p = filepath.Join("/", dir, p)
dir, err = a.fas.state.GetDir(ctx)
}
if err != nil {
return "", err
}
p, err = system.NormalizePath(dir, p, platform, false)
if err != nil {
return "", errors.Wrap(err, "normalizing source path")
}
}
return p, nil
Expand Down Expand Up @@ -733,7 +733,11 @@ func (f *FileOp) Marshal(ctx context.Context, c *Constraints) (digest.Digest, []
}
}

action, err := st.action.toProtoAction(ctx, parent, st.base)
var platform string
if f.constraints.Platform != nil {
platform = f.constraints.Platform.OS
}
action, err := st.action.toProtoAction(ctx, parent, st.base, platform)
if err != nil {
return "", nil, nil, nil, err
}
Expand All @@ -754,33 +758,6 @@ func (f *FileOp) Marshal(ctx context.Context, c *Constraints) (digest.Digest, []
return f.Load()
}

func normalizePath(parent, p string, keepSlash bool) (string, error) {
var err error
parent, err = system.CheckSystemDriveAndRemoveDriveLetter(parent)
if err != nil {
return "", errors.Wrap(err, "cleaning parent")
}
p, err = system.CheckSystemDriveAndRemoveDriveLetter(p)
if err != nil {
return "", errors.Wrap(err, "cleaning path")
}
origPath := filepath.FromSlash(p)
if !system.IsAbs(p) {
p = filepath.Join("/", parent, p)
}
if keepSlash {
if strings.HasSuffix(origPath, string(filepath.Separator)) && !strings.HasSuffix(p, string(filepath.Separator)) {
p += string(filepath.Separator)
} else if strings.HasSuffix(origPath, fmt.Sprintf("%c.", filepath.Separator)) {
if p != string(filepath.Separator) {
p += string(filepath.Separator)
}
p += "."
}
}
return p, nil
}

func (f *FileOp) Output() Output {
return f.output
}
Expand Down
22 changes: 10 additions & 12 deletions client/llb/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"net"
"path/filepath"

"github.com/containerd/containerd/platforms"
"github.com/google/shlex"
Expand Down Expand Up @@ -68,22 +67,21 @@ func dirf(value string, replace bool, v ...interface{}) StateOption {
}
return func(s State) State {
return s.withValue(keyDir, func(ctx context.Context, c *Constraints) (interface{}, error) {
if !system.IsAbs(value) {
var platform string
if c != nil && c.Platform != nil {
platform = c.Platform.OS
}
if !system.IsAbs(value, platform) {
prev, err := getDir(s)(ctx, c)
if err != nil {
return nil, err
return nil, errors.Wrap(err, "getting dir from state")
}

if prev == "" {
prev = "/"
value, err = system.NormalizePath(prev, value, platform, false)
if err != nil {
return nil, errors.Wrap(err, "normalizing path")
}
value = filepath.Join(prev, value)
}
cleaned, err := system.CheckSystemDriveAndRemoveDriveLetter(value)
if err != nil {
return nil, errors.Wrap(err, "removing drive letter")
}
return cleaned, nil
return value, nil
})
}
}
Expand Down
27 changes: 13 additions & 14 deletions frontend/dockerfile/dockerfile2llb/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -1040,17 +1040,12 @@ func dispatchRun(d *dispatchState, c *instructions.RunCommand, proxy *llb.ProxyE
}

func dispatchWorkdir(d *dispatchState, c *instructions.WorkdirCommand, commit bool, opt *dispatchOpt) error {
withoutDriveLetter, err := system.CheckSystemDriveAndRemoveDriveLetter(c.Path)
if err != nil {
return errors.Wrap(err, "removing drive letter")
}

d.state = d.state.Dir(withoutDriveLetter)
wd, err := system.NormalizeWorkdir(d.image.Config.WorkingDir, withoutDriveLetter)
wd, err := system.NormalizeWorkdir(d.image.Config.WorkingDir, c.Path, d.platform.OS)
if err != nil {
return errors.Wrap(err, "normalizing workdir")
}

d.state = d.state.Dir(wd)
d.image.Config.WorkingDir = wd
if commit {
withLayer := false
Expand Down Expand Up @@ -1079,7 +1074,11 @@ func dispatchWorkdir(d *dispatchState, c *instructions.WorkdirCommand, commit bo
}

func dispatchCopy(d *dispatchState, cfg copyConfig) error {
pp, err := pathRelativeToWorkingDir(d.state, cfg.params.DestPath)
var platformOS string
if d.platform != nil {
platformOS = d.platform.OS
}
pp, err := pathRelativeToWorkingDir(d.state, cfg.params.DestPath, platformOS)
if err != nil {
return err
}
Expand Down Expand Up @@ -1188,7 +1187,7 @@ func dispatchCopy(d *dispatchState, cfg copyConfig) error {
a = a.Copy(st, f, dest, opts...)
}
} else {
src, err = system.CheckSystemDriveAndRemoveDriveLetter(src)
src, err = system.CheckSystemDriveAndRemoveDriveLetter(src, platformOS)
if err != nil {
return errors.Wrap(err, "removing drive letter")
}
Expand All @@ -1215,7 +1214,7 @@ func dispatchCopy(d *dispatchState, cfg copyConfig) error {
commitMessage.WriteString(" <<" + src.Path)

data := src.Data
f, err := system.CheckSystemDriveAndRemoveDriveLetter(src.Path)
f, err := system.CheckSystemDriveAndRemoveDriveLetter(src.Path, platformOS)
if err != nil {
return errors.Wrap(err, "removing drive letter")
}
Expand All @@ -1229,7 +1228,7 @@ func dispatchCopy(d *dispatchState, cfg copyConfig) error {
CreateDestPath: true,
}}, copyOpt...)

dest, err = system.CheckSystemDriveAndRemoveDriveLetter(dest)
dest, err = system.CheckSystemDriveAndRemoveDriveLetter(dest, platformOS)
if err != nil {
return errors.Wrap(err, "removing drive letter")
}
Expand Down Expand Up @@ -1460,7 +1459,7 @@ func dispatchArg(d *dispatchState, c *instructions.ArgCommand, metaArgs []instru
return commitToHistory(&d.image, "ARG "+strings.Join(commitStrs, " "), false, nil, d.epoch)
}

func pathRelativeToWorkingDir(s llb.State, p string) (string, error) {
func pathRelativeToWorkingDir(s llb.State, p, platform string) (string, error) {
dir, err := s.GetDir(context.TODO())
if err != nil {
return "", err
Expand All @@ -1470,12 +1469,12 @@ func pathRelativeToWorkingDir(s llb.State, p string) (string, error) {
return dir, nil
}

p, err = system.CheckSystemDriveAndRemoveDriveLetter(p)
p, err = system.CheckSystemDriveAndRemoveDriveLetter(p, platform)
if err != nil {
return "", errors.Wrap(err, "remving drive letter")
}

if system.IsAbs(p) {
if system.IsAbs(p, platform) {
return p, nil
}
return filepath.Join(dir, p), nil
Expand Down
6 changes: 3 additions & 3 deletions frontend/gateway/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func NewContainer(ctx context.Context, w worker.Worker, sm *session.Manager, g s
cm = refs[m.Input].Worker.CacheManager()
}
return cm.New(ctx, ref, g)
})
}, platform.OS)
if err != nil {
for i := len(p.Actives) - 1; i >= 0; i-- { // call in LIFO order
p.Actives[i].Ref.Release(context.TODO())
Expand Down Expand Up @@ -137,7 +137,7 @@ type MountMutableRef struct {

type MakeMutable func(m *opspb.Mount, ref cache.ImmutableRef) (cache.MutableRef, error)

func PrepareMounts(ctx context.Context, mm *mounts.MountManager, cm cache.Manager, g session.Group, cwd string, mnts []*opspb.Mount, refs []*worker.WorkerRef, makeMutable MakeMutable) (p PreparedMounts, err error) {
func PrepareMounts(ctx context.Context, mm *mounts.MountManager, cm cache.Manager, g session.Group, cwd string, mnts []*opspb.Mount, refs []*worker.WorkerRef, makeMutable MakeMutable, platform string) (p PreparedMounts, err error) {
// loop over all mounts, fill in mounts, root and outputs
for i, m := range mnts {
var (
Expand Down Expand Up @@ -260,7 +260,7 @@ func PrepareMounts(ctx context.Context, mm *mounts.MountManager, cm cache.Manage
} else {
mws := mountWithSession(mountable, g)
dest := m.Dest
if !system.IsAbs(filepath.Clean(dest)) {
if !system.IsAbs(filepath.Clean(dest), platform) {
dest = filepath.Join("/", cwd, dest)
}
mws.Dest = dest
Expand Down
10 changes: 5 additions & 5 deletions solver/llbsolver/file/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package file

import (
"context"
"fmt"
"log"
"os"
"path/filepath"
"runtime"
"strings"
"time"

Expand Down Expand Up @@ -68,11 +68,11 @@ func mapUserToChowner(user *copy.User, idmap *idtools.IdentityMapping) (copy.Cho
}

func mkdir(ctx context.Context, d string, action pb.FileActionMkDir, user *copy.User, idmap *idtools.IdentityMapping) error {
actionPath, err := system.CheckSystemDriveAndRemoveDriveLetter(action.Path)
actionPath, err := system.NormalizePath("/", action.Path, runtime.GOOS, false)
if err != nil {
return errors.Wrap(err, "removing drive letter")
}
p, err := fs.RootPath(d, filepath.Join("/", actionPath))
p, err := fs.RootPath(d, filepath.FromSlash(actionPath))
if err != nil {
return err
}
Expand Down Expand Up @@ -352,12 +352,12 @@ func (fb *Backend) Copy(ctx context.Context, m1, m2, user, group fileoptypes.Mou
}

func cleanPath(s string) (string, error) {
s, err := system.CheckSystemDriveAndRemoveDriveLetter(s)
s, err := system.CheckSystemDriveAndRemoveDriveLetter(s, runtime.GOOS)
if err != nil {
return "", errors.Wrap(err, "removing drive letter")
}
s2 := filepath.Join("/", s)
if strings.HasSuffix(filepath.FromSlash(s), fmt.Sprintf("%c.", filepath.Separator)) {
if strings.HasSuffix(filepath.FromSlash(s), string(filepath.Separator)+".") {
if s2 != string(filepath.Separator) {
s2 += string(filepath.Separator)
}
Expand Down
2 changes: 1 addition & 1 deletion solver/llbsolver/ops/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ func (e *ExecOp) Exec(ctx context.Context, g session.Group, inputs []solver.Resu
p, err := gateway.PrepareMounts(ctx, e.mm, e.cm, g, e.op.Meta.Cwd, e.op.Mounts, refs, func(m *pb.Mount, ref cache.ImmutableRef) (cache.MutableRef, error) {
desc := fmt.Sprintf("mount %s from exec %s", m.Dest, strings.Join(e.op.Meta.Args, " "))
return e.cm.New(ctx, ref, g, cache.WithDescription(desc))
})
}, e.platform.OS)
defer func() {
if err != nil {
execInputs := make([]solver.Result, len(e.op.Mounts))
Expand Down
Loading

0 comments on commit 7abc3ed

Please sign in to comment.