@@ -4,13 +4,13 @@ import (
44 "context"
55 _ "crypto/sha256" // for opencontainers/go-digest
66 "os"
7+ "path"
78 "path/filepath"
89 "strconv"
910 "strings"
1011 "time"
1112
1213 "github.com/moby/buildkit/solver/pb"
13- "github.com/moby/buildkit/util/system"
1414 digest "github.com/opencontainers/go-digest"
1515 "github.com/pkg/errors"
1616)
@@ -55,7 +55,7 @@ type CopyInput interface {
5555}
5656
5757type subAction interface {
58- toProtoAction (ctx context.Context , parent string , base pb.InputIndex , platformOS string ) (pb.IsFileAction , error )
58+ toProtoAction (context.Context , string , pb.InputIndex ) (pb.IsFileAction , error )
5959}
6060
6161type capAdder interface {
@@ -146,6 +146,7 @@ func Mkdir(p string, m os.FileMode, opt ...MkdirOption) *FileAction {
146146 for _ , o := range opt {
147147 o .SetMkdirOption (& mi )
148148 }
149+
149150 return & FileAction {
150151 action : & fileActionMkdir {
151152 file : p ,
@@ -161,14 +162,10 @@ type fileActionMkdir struct {
161162 info MkdirInfo
162163}
163164
164- func (a * fileActionMkdir ) toProtoAction (ctx context.Context , parent string , base pb.InputIndex , platformOS string ) (pb.IsFileAction , error ) {
165- normalizedPath , err := system .NormalizePath (parent , a .file , platformOS , false )
166- if err != nil {
167- return nil , errors .Wrap (err , "normalizing path" )
168- }
165+ func (a * fileActionMkdir ) toProtoAction (ctx context.Context , parent string , base pb.InputIndex ) (pb.IsFileAction , error ) {
169166 return & pb.FileAction_Mkdir {
170167 Mkdir : & pb.FileActionMkDir {
171- Path : normalizedPath ,
168+ Path : normalizePath ( parent , a . file , false ) ,
172169 Mode : int32 (a .mode & 0777 ),
173170 MakeParents : a .info .MakeParents ,
174171 Owner : a .info .ChownOpt .marshal (base ),
@@ -339,14 +336,10 @@ type fileActionMkfile struct {
339336 info MkfileInfo
340337}
341338
342- func (a * fileActionMkfile ) toProtoAction (ctx context.Context , parent string , base pb.InputIndex , platformOS string ) (pb.IsFileAction , error ) {
343- normalizedPath , err := system .NormalizePath (parent , a .file , platformOS , false )
344- if err != nil {
345- return nil , errors .Wrap (err , "normalizing path" )
346- }
339+ func (a * fileActionMkfile ) toProtoAction (ctx context.Context , parent string , base pb.InputIndex ) (pb.IsFileAction , error ) {
347340 return & pb.FileAction_Mkfile {
348341 Mkfile : & pb.FileActionMkFile {
349- Path : normalizedPath ,
342+ Path : normalizePath ( parent , a . file , false ) ,
350343 Mode : int32 (a .mode & 0777 ),
351344 Data : a .dt ,
352345 Owner : a .info .ChownOpt .marshal (base ),
@@ -411,14 +404,10 @@ type fileActionRm struct {
411404 info RmInfo
412405}
413406
414- func (a * fileActionRm ) toProtoAction (ctx context.Context , parent string , base pb.InputIndex , platformOS string ) (pb.IsFileAction , error ) {
415- normalizedPath , err := system .NormalizePath (parent , a .file , platformOS , false )
416- if err != nil {
417- return nil , errors .Wrap (err , "normalizing path" )
418- }
407+ func (a * fileActionRm ) toProtoAction (ctx context.Context , parent string , base pb.InputIndex ) (pb.IsFileAction , error ) {
419408 return & pb.FileAction_Rm {
420409 Rm : & pb.FileActionRm {
421- Path : normalizedPath ,
410+ Path : normalizePath ( parent , a . file , false ) ,
422411 AllowNotFound : a .info .AllowNotFound ,
423412 AllowWildcard : a .info .AllowWildcard ,
424413 },
@@ -504,18 +493,14 @@ type fileActionCopy struct {
504493 info CopyInfo
505494}
506495
507- func (a * fileActionCopy ) toProtoAction (ctx context.Context , parent string , base pb.InputIndex , platformOS string ) (pb.IsFileAction , error ) {
508- src , err := a .sourcePath (ctx , platformOS )
496+ func (a * fileActionCopy ) toProtoAction (ctx context.Context , parent string , base pb.InputIndex ) (pb.IsFileAction , error ) {
497+ src , err := a .sourcePath (ctx )
509498 if err != nil {
510499 return nil , err
511500 }
512- normalizedPath , err := system .NormalizePath (parent , a .dest , platformOS , false )
513- if err != nil {
514- return nil , errors .Wrap (err , "normalizing path" )
515- }
516501 c := & pb.FileActionCopy {
517502 Src : src ,
518- Dest : normalizedPath ,
503+ Dest : normalizePath ( parent , a . dest , true ) ,
519504 Owner : a .info .ChownOpt .marshal (base ),
520505 IncludePatterns : a .info .IncludePatterns ,
521506 ExcludePatterns : a .info .ExcludePatterns ,
@@ -537,13 +522,13 @@ func (a *fileActionCopy) toProtoAction(ctx context.Context, parent string, base
537522 }, nil
538523}
539524
540- func (a * fileActionCopy ) sourcePath (ctx context.Context , platform string ) (string , error ) {
525+ func (a * fileActionCopy ) sourcePath (ctx context.Context ) (string , error ) {
541526 // filepath.Clean() also does a filepath.FromSlash(). Explicitly convert back to UNIX path
542527 // separators.
543528 p := filepath .ToSlash (filepath .Clean (a .src ))
544529 dir := "/"
545530 var err error
546- if ! system .IsAbs (p , platform ) {
531+ if ! path .IsAbs (p ) {
547532 if a .state != nil {
548533 dir , err = a .state .GetDir (ctx )
549534 } else if a .fas != nil {
@@ -553,11 +538,7 @@ func (a *fileActionCopy) sourcePath(ctx context.Context, platform string) (strin
553538 return "" , err
554539 }
555540 }
556- p , err = system .NormalizePath (dir , p , platform , false )
557- if err != nil {
558- return "" , errors .Wrap (err , "normalizing source path" )
559- }
560- return p , nil
541+ return path .Join (dir , p ), nil
561542}
562543
563544func (a * fileActionCopy ) addCaps (f * FileOp ) {
@@ -772,7 +753,7 @@ func (f *FileOp) Marshal(ctx context.Context, c *Constraints) (digest.Digest, []
772753 }
773754 }
774755
775- action , err := st .action .toProtoAction (ctx , parent , st .base , f . constraints . Platform . OS )
756+ action , err := st .action .toProtoAction (ctx , parent , st .base )
776757 if err != nil {
777758 return "" , nil , nil , nil , err
778759 }
@@ -793,6 +774,25 @@ func (f *FileOp) Marshal(ctx context.Context, c *Constraints) (digest.Digest, []
793774 return f .Load ()
794775}
795776
777+ func normalizePath (parent , p string , keepSlash bool ) string {
778+ origPath := p
779+ p = path .Clean (p )
780+ if ! path .IsAbs (p ) {
781+ p = path .Join ("/" , parent , p )
782+ }
783+ if keepSlash {
784+ if strings .HasSuffix (origPath , "/" ) && ! strings .HasSuffix (p , "/" ) {
785+ p += "/"
786+ } else if strings .HasSuffix (origPath , "/." ) {
787+ if p != "/" {
788+ p += "/"
789+ }
790+ p += "."
791+ }
792+ }
793+ return p
794+ }
795+
796796func (f * FileOp ) Output () Output {
797797 return f .output
798798}
0 commit comments