From 620c8c72535670e8358cebc65a2fb424be58a93c Mon Sep 17 00:00:00 2001
From: Alexandr Morozov <lk4d4math@gmail.com>
Date: Mon, 19 May 2014 23:07:31 +0400
Subject: [PATCH] Make chmod on ADDed files

Fixes #3979
Docker-DCO-1.1-Signed-off-by: Alexandr Morozov <lk4d4math@gmail.com> (github: LK4D4)
---
 .../build_tests/TestAdd/SingleFileToRoot/Dockerfile |  1 +
 .../build_tests/TestAdd/WholeDirToRoot/Dockerfile   |  2 ++
 server/buildfile.go                                 | 13 ++++++++-----
 3 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/integration-cli/build_tests/TestAdd/SingleFileToRoot/Dockerfile b/integration-cli/build_tests/TestAdd/SingleFileToRoot/Dockerfile
index d6375debe1..e96201d858 100644
--- a/integration-cli/build_tests/TestAdd/SingleFileToRoot/Dockerfile
+++ b/integration-cli/build_tests/TestAdd/SingleFileToRoot/Dockerfile
@@ -5,4 +5,5 @@ RUN touch /exists
 RUN chown dockerio.dockerio /exists
 ADD test_file /
 RUN [ $(ls -l /test_file | awk '{print $3":"$4}') = 'root:root' ]
+RUN [ $(ls -l /test_file | awk '{print $1}') = '-rwxr-xr-x' ]
 RUN [ $(ls -l /exists | awk '{print $3":"$4}') = 'dockerio:dockerio' ]
diff --git a/integration-cli/build_tests/TestAdd/WholeDirToRoot/Dockerfile b/integration-cli/build_tests/TestAdd/WholeDirToRoot/Dockerfile
index 3db6d3fd95..2f10979487 100644
--- a/integration-cli/build_tests/TestAdd/WholeDirToRoot/Dockerfile
+++ b/integration-cli/build_tests/TestAdd/WholeDirToRoot/Dockerfile
@@ -5,5 +5,7 @@ RUN touch /exists
 RUN chown dockerio.dockerio exists
 ADD test_dir /test_dir
 RUN [ $(ls -l / | grep test_dir | awk '{print $3":"$4}') = 'root:root' ]
+RUN [ $(ls -l / | grep test_dir | awk '{print $1}') = 'drwxr-xr-x' ]
 RUN [ $(ls -l /test_dir/test_file | awk '{print $3":"$4}') = 'root:root' ]
+RUN [ $(ls -l /test_dir/test_file | awk '{print $1}') = '-rwxr-xr-x' ]
 RUN [ $(ls -l /exists | awk '{print $3":"$4}') = 'dockerio:dockerio' ]
diff --git a/server/buildfile.go b/server/buildfile.go
index 9953904941..efe1869509 100644
--- a/server/buildfile.go
+++ b/server/buildfile.go
@@ -431,9 +431,12 @@ func (b *buildFile) addContext(container *daemon.Container, orig, dest string, r
 		return err
 	}
 
-	chownR := func(destPath string, uid, gid int) error {
+	fixPermsR := func(destPath string, uid, gid int) error {
 		return filepath.Walk(destPath, func(path string, info os.FileInfo, err error) error {
-			if err := os.Lchown(path, uid, gid); err != nil {
+			if err := os.Lchown(path, uid, gid); err != nil && !os.IsNotExist(err) {
+				return err
+			}
+			if err := os.Chmod(path, 0755); err != nil && !os.IsNotExist(err) {
 				return err
 			}
 			return nil
@@ -450,12 +453,12 @@ func (b *buildFile) addContext(container *daemon.Container, orig, dest string, r
 				return err
 			}
 			for _, file := range files {
-				if err := chownR(filepath.Join(destPath, file.Name()), 0, 0); err != nil {
+				if err := fixPermsR(filepath.Join(destPath, file.Name()), 0, 0); err != nil {
 					return err
 				}
 			}
 		} else {
-			if err := chownR(destPath, 0, 0); err != nil {
+			if err := fixPermsR(destPath, 0, 0); err != nil {
 				return err
 			}
 		}
@@ -494,7 +497,7 @@ func (b *buildFile) addContext(container *daemon.Container, orig, dest string, r
 		resPath = path.Join(destPath, path.Base(origPath))
 	}
 
-	if err := chownR(resPath, 0, 0); err != nil {
+	if err := fixPermsR(resPath, 0, 0); err != nil {
 		return err
 	}
 	return nil