@@ -4,13 +4,13 @@ import (
4
4
"context"
5
5
_ "crypto/sha256" // for opencontainers/go-digest
6
6
"os"
7
+ "path"
7
8
"path/filepath"
8
9
"strconv"
9
10
"strings"
10
11
"time"
11
12
12
13
"github.com/moby/buildkit/solver/pb"
13
- "github.com/moby/buildkit/util/system"
14
14
digest "github.com/opencontainers/go-digest"
15
15
"github.com/pkg/errors"
16
16
)
@@ -55,7 +55,7 @@ type CopyInput interface {
55
55
}
56
56
57
57
type 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 )
59
59
}
60
60
61
61
type capAdder interface {
@@ -146,6 +146,7 @@ func Mkdir(p string, m os.FileMode, opt ...MkdirOption) *FileAction {
146
146
for _ , o := range opt {
147
147
o .SetMkdirOption (& mi )
148
148
}
149
+
149
150
return & FileAction {
150
151
action : & fileActionMkdir {
151
152
file : p ,
@@ -161,14 +162,10 @@ type fileActionMkdir struct {
161
162
info MkdirInfo
162
163
}
163
164
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 ) {
169
166
return & pb.FileAction_Mkdir {
170
167
Mkdir : & pb.FileActionMkDir {
171
- Path : normalizedPath ,
168
+ Path : normalizePath ( parent , a . file , false ) ,
172
169
Mode : int32 (a .mode & 0777 ),
173
170
MakeParents : a .info .MakeParents ,
174
171
Owner : a .info .ChownOpt .marshal (base ),
@@ -339,14 +336,10 @@ type fileActionMkfile struct {
339
336
info MkfileInfo
340
337
}
341
338
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 ) {
347
340
return & pb.FileAction_Mkfile {
348
341
Mkfile : & pb.FileActionMkFile {
349
- Path : normalizedPath ,
342
+ Path : normalizePath ( parent , a . file , false ) ,
350
343
Mode : int32 (a .mode & 0777 ),
351
344
Data : a .dt ,
352
345
Owner : a .info .ChownOpt .marshal (base ),
@@ -411,14 +404,10 @@ type fileActionRm struct {
411
404
info RmInfo
412
405
}
413
406
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 ) {
419
408
return & pb.FileAction_Rm {
420
409
Rm : & pb.FileActionRm {
421
- Path : normalizedPath ,
410
+ Path : normalizePath ( parent , a . file , false ) ,
422
411
AllowNotFound : a .info .AllowNotFound ,
423
412
AllowWildcard : a .info .AllowWildcard ,
424
413
},
@@ -504,18 +493,14 @@ type fileActionCopy struct {
504
493
info CopyInfo
505
494
}
506
495
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 )
509
498
if err != nil {
510
499
return nil , err
511
500
}
512
- normalizedPath , err := system .NormalizePath (parent , a .dest , platformOS , false )
513
- if err != nil {
514
- return nil , errors .Wrap (err , "normalizing path" )
515
- }
516
501
c := & pb.FileActionCopy {
517
502
Src : src ,
518
- Dest : normalizedPath ,
503
+ Dest : normalizePath ( parent , a . dest , true ) ,
519
504
Owner : a .info .ChownOpt .marshal (base ),
520
505
IncludePatterns : a .info .IncludePatterns ,
521
506
ExcludePatterns : a .info .ExcludePatterns ,
@@ -537,13 +522,13 @@ func (a *fileActionCopy) toProtoAction(ctx context.Context, parent string, base
537
522
}, nil
538
523
}
539
524
540
- func (a * fileActionCopy ) sourcePath (ctx context.Context , platform string ) (string , error ) {
525
+ func (a * fileActionCopy ) sourcePath (ctx context.Context ) (string , error ) {
541
526
// filepath.Clean() also does a filepath.FromSlash(). Explicitly convert back to UNIX path
542
527
// separators.
543
528
p := filepath .ToSlash (filepath .Clean (a .src ))
544
529
dir := "/"
545
530
var err error
546
- if ! system .IsAbs (p , platform ) {
531
+ if ! path .IsAbs (p ) {
547
532
if a .state != nil {
548
533
dir , err = a .state .GetDir (ctx )
549
534
} else if a .fas != nil {
@@ -553,11 +538,7 @@ func (a *fileActionCopy) sourcePath(ctx context.Context, platform string) (strin
553
538
return "" , err
554
539
}
555
540
}
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
561
542
}
562
543
563
544
func (a * fileActionCopy ) addCaps (f * FileOp ) {
@@ -772,7 +753,7 @@ func (f *FileOp) Marshal(ctx context.Context, c *Constraints) (digest.Digest, []
772
753
}
773
754
}
774
755
775
- action , err := st .action .toProtoAction (ctx , parent , st .base , f . constraints . Platform . OS )
756
+ action , err := st .action .toProtoAction (ctx , parent , st .base )
776
757
if err != nil {
777
758
return "" , nil , nil , nil , err
778
759
}
@@ -793,6 +774,25 @@ func (f *FileOp) Marshal(ctx context.Context, c *Constraints) (digest.Digest, []
793
774
return f .Load ()
794
775
}
795
776
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
+
796
796
func (f * FileOp ) Output () Output {
797
797
return f .output
798
798
}
0 commit comments