Fix a regression introduced in PR#9467 when a single file was added or
copied.
Signed-off-by: Arnaud Porterie <arnaud.porterie@docker.com>
... | ... |
@@ -660,11 +660,19 @@ func copyAsDirectory(source, destination string) error { |
660 | 660 |
} |
661 | 661 |
|
662 | 662 |
func fixPermissions(source, destination string, uid, gid int) error { |
663 |
+ // The copied root permission should not be changed for previously existing |
|
664 |
+ // directories. |
|
665 |
+ s, err := os.Stat(destination) |
|
666 |
+ if err != nil && !os.IsNotExist(err) { |
|
667 |
+ return err |
|
668 |
+ } |
|
669 |
+ fixRootPermission := (err != nil) || !s.IsDir() |
|
670 |
+ |
|
663 | 671 |
// We Walk on the source rather than on the destination because we don't |
664 | 672 |
// want to change permissions on things we haven't created or modified. |
665 | 673 |
return filepath.Walk(source, func(fullpath string, info os.FileInfo, err error) error { |
666 | 674 |
// Do not alter the walk root itself as it potentially existed before. |
667 |
- if source == fullpath { |
|
675 |
+ if !fixRootPermission && (source == fullpath) { |
|
668 | 676 |
return nil |
669 | 677 |
} |
670 | 678 |
// Path is prefixed by source: substitute with destination instead. |
... | ... |
@@ -3564,3 +3564,31 @@ func TestBuildStderr(t *testing.T) { |
3564 | 3564 |
} |
3565 | 3565 |
logDone("build - testing stderr") |
3566 | 3566 |
} |
3567 |
+ |
|
3568 |
+func TestBuildChownSingleFile(t *testing.T) { |
|
3569 |
+ name := "testbuildchownsinglefile" |
|
3570 |
+ defer deleteImages(name) |
|
3571 |
+ |
|
3572 |
+ ctx, err := fakeContext(` |
|
3573 |
+FROM busybox |
|
3574 |
+COPY test / |
|
3575 |
+RUN ls -l /test |
|
3576 |
+RUN [ $(ls -l /test | awk '{print $3":"$4}') = 'root:root' ] |
|
3577 |
+`, map[string]string{ |
|
3578 |
+ "test": "test", |
|
3579 |
+ }) |
|
3580 |
+ if err != nil { |
|
3581 |
+ t.Fatal(err) |
|
3582 |
+ } |
|
3583 |
+ defer ctx.Close() |
|
3584 |
+ |
|
3585 |
+ if err := os.Chown(filepath.Join(ctx.Dir, "test"), 4242, 4242); err != nil { |
|
3586 |
+ t.Fatal(err) |
|
3587 |
+ } |
|
3588 |
+ |
|
3589 |
+ if _, err := buildImageFromContext(name, ctx, true); err != nil { |
|
3590 |
+ t.Fatal(err) |
|
3591 |
+ } |
|
3592 |
+ |
|
3593 |
+ logDone("build - change permission on single file") |
|
3594 |
+} |