Skip to content

Commit edae0c6

Browse files
Ensure we use proper path separators
In dockerfile2llb we ensure we use proper path separators before we send them to LLB. In LLB we default to linux/amd64 if no constraints are set by the caller. Signed-off-by: Gabriel Adrian Samfira <[email protected]>
1 parent 0519491 commit edae0c6

File tree

3 files changed

+27
-23
lines changed

3 files changed

+27
-23
lines changed

client/llb/fileop.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,6 @@ func Copy(input CopyInput, src, dest string, opts ...CopyOption) *FileAction {
460460
for _, o := range opts {
461461
o.SetCopyOption(&mi)
462462
}
463-
464463
return &FileAction{
465464
action: &fileActionCopy{
466465
state: state,
@@ -539,10 +538,12 @@ func (a *fileActionCopy) toProtoAction(ctx context.Context, parent string, base
539538
}
540539

541540
func (a *fileActionCopy) sourcePath(ctx context.Context, platform string) (string, error) {
542-
p := filepath.Clean(a.src)
541+
// filepath.Clean() also does a filepath.FromSlash(). Explicitly convert back to UNIX path
542+
// separators.
543+
p := filepath.ToSlash(filepath.Clean(a.src))
544+
dir := "/"
545+
var err error
543546
if !system.IsAbs(p, platform) {
544-
var dir string
545-
var err error
546547
if a.state != nil {
547548
dir, err = a.state.GetDir(ctx)
548549
} else if a.fas != nil {
@@ -551,10 +552,10 @@ func (a *fileActionCopy) sourcePath(ctx context.Context, platform string) (strin
551552
if err != nil {
552553
return "", err
553554
}
554-
p, err = system.NormalizePath(dir, p, platform, false)
555-
if err != nil {
556-
return "", errors.Wrap(err, "normalizing source path")
557-
}
555+
}
556+
p, err = system.NormalizePath(dir, p, platform, false)
557+
if err != nil {
558+
return "", errors.Wrap(err, "normalizing source path")
558559
}
559560
return p, nil
560561
}

client/llb/state.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,10 @@ func (s State) File(a *FileAction, opts ...ConstraintsOpt) State {
301301

302302
// No platform was set by constraint options. Set the current OS and arch.
303303
if c.Platform == nil {
304-
platform := platforms.DefaultSpec()
305-
c.Platform = &platform
304+
c.Platform = &ocispecs.Platform{
305+
OS: "linux",
306+
Architecture: "amd64",
307+
}
306308
}
307309
return s.WithOutput(NewFileOp(s, a, c).Output())
308310
}

frontend/dockerfile/dockerfile2llb/convert.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -997,8 +997,15 @@ func dispatchWorkdir(d *dispatchState, c *instructions.WorkdirCommand, commit bo
997997
return errors.Wrap(err, "normalizing workdir")
998998
}
999999

1000-
d.state = d.state.Dir(wd)
1000+
// NormalizeWorkdir returns paths with platform specific separators. For Windows
1001+
// this will be of the form: \some\path, which is needed later when we pass it to
1002+
// HCS.
10011003
d.image.Config.WorkingDir = wd
1004+
1005+
// From this point forward, we can use UNIX style paths.
1006+
wd = filepath.ToSlash(wd)
1007+
d.state = d.state.Dir(wd)
1008+
10021009
if commit {
10031010
withLayer := false
10041011
if wd != "/" {
@@ -1027,11 +1034,10 @@ func dispatchWorkdir(d *dispatchState, c *instructions.WorkdirCommand, commit bo
10271034
}
10281035

10291036
func dispatchCopy(d *dispatchState, cfg copyConfig) error {
1030-
pp, err := pathRelativeToWorkingDir(d.state, cfg.params.DestPath, *d.platform)
1037+
dest, err := pathRelativeToWorkingDir(d.state, cfg.params.DestPath, *d.platform)
10311038
if err != nil {
10321039
return err
10331040
}
1034-
dest := filepath.Join("/", pp)
10351041

10361042
if cfg.params.DestPath == "." || cfg.params.DestPath == "" || cfg.params.DestPath[len(cfg.params.DestPath)-1] == filepath.Separator {
10371043
dest += string(filepath.Separator)
@@ -1136,7 +1142,7 @@ func dispatchCopy(d *dispatchState, cfg copyConfig) error {
11361142
a = a.Copy(st, f, dest, opts...)
11371143
}
11381144
} else {
1139-
src, err = system.CheckSystemDriveAndRemoveDriveLetter(src, d.platform.OS)
1145+
src, err = system.NormalizePath("/", src, d.platform.OS, false)
11401146
if err != nil {
11411147
return errors.Wrap(err, "removing drive letter")
11421148
}
@@ -1152,9 +1158,9 @@ func dispatchCopy(d *dispatchState, cfg copyConfig) error {
11521158
}}, copyOpt...)
11531159

11541160
if a == nil {
1155-
a = llb.Copy(cfg.source, filepath.Join("/", src), dest, opts...)
1161+
a = llb.Copy(cfg.source, src, dest, opts...)
11561162
} else {
1157-
a = a.Copy(cfg.source, filepath.Join("/", src), dest, opts...)
1163+
a = a.Copy(cfg.source, src, dest, opts...)
11581164
}
11591165
}
11601166
}
@@ -1178,15 +1184,10 @@ func dispatchCopy(d *dispatchState, cfg copyConfig) error {
11781184
CreateDestPath: true,
11791185
}}, copyOpt...)
11801186

1181-
dest, err = system.CheckSystemDriveAndRemoveDriveLetter(dest, d.platform.OS)
1182-
if err != nil {
1183-
return errors.Wrap(err, "removing drive letter")
1184-
}
1185-
11861187
if a == nil {
1187-
a = llb.Copy(st, f, dest, opts...)
1188+
a = llb.Copy(st, filepath.ToSlash(f), dest, opts...)
11881189
} else {
1189-
a = a.Copy(st, f, dest, opts...)
1190+
a = a.Copy(st, filepath.ToSlash(f), dest, opts...)
11901191
}
11911192
}
11921193

0 commit comments

Comments
 (0)