Browse Source

Merge pull request #18835 from dnephin/move_validate_context_dir

Move utils.ValidateContextDirectory to the one package that uses it
Vincent Demeester 9 years ago
parent
commit
6cd167386b
4 changed files with 52 additions and 54 deletions
  1. 50 3
      api/client/build.go
  2. 1 1
      api/client/utils_unix.go
  3. 1 1
      api/client/utils_windows.go
  4. 0 49
      utils/utils.go

+ 50 - 3
api/client/build.go

@@ -30,7 +30,6 @@ import (
 	"github.com/docker/docker/pkg/urlutil"
 	"github.com/docker/docker/reference"
 	runconfigopts "github.com/docker/docker/runconfig/opts"
-	"github.com/docker/docker/utils"
 	"github.com/docker/go-units"
 )
 
@@ -150,7 +149,7 @@ func (cli *DockerCli) CmdBuild(args ...string) error {
 		}
 	}
 
-	if err := utils.ValidateContextDirectory(contextDir, excludes); err != nil {
+	if err := validateContextDirectory(contextDir, excludes); err != nil {
 		return fmt.Errorf("Error checking context: '%s'.", err)
 	}
 
@@ -160,7 +159,7 @@ func (cli *DockerCli) CmdBuild(args ...string) error {
 	// .dockerignore is needed to know if either one needs to be
 	// removed. The daemon will remove them for us, if needed, after it
 	// parses the Dockerfile. Ignore errors here, as they will have been
-	// caught by ValidateContextDirectory above.
+	// caught by validateContextDirectory above.
 	var includes = []string{"."}
 	keepThem1, _ := fileutils.Matches(".dockerignore", excludes)
 	keepThem2, _ := fileutils.Matches(relDockerfile, excludes)
@@ -278,6 +277,54 @@ func (cli *DockerCli) CmdBuild(args ...string) error {
 	return nil
 }
 
+// validateContextDirectory checks if all the contents of the directory
+// can be read and returns an error if some files can't be read
+// symlinks which point to non-existing files don't trigger an error
+func validateContextDirectory(srcPath string, excludes []string) error {
+	contextRoot, err := getContextRoot(srcPath)
+	if err != nil {
+		return err
+	}
+	return filepath.Walk(contextRoot, func(filePath string, f os.FileInfo, err error) error {
+		// skip this directory/file if it's not in the path, it won't get added to the context
+		if relFilePath, err := filepath.Rel(contextRoot, filePath); err != nil {
+			return err
+		} else if skip, err := fileutils.Matches(relFilePath, excludes); err != nil {
+			return err
+		} else if skip {
+			if f.IsDir() {
+				return filepath.SkipDir
+			}
+			return nil
+		}
+
+		if err != nil {
+			if os.IsPermission(err) {
+				return fmt.Errorf("can't stat '%s'", filePath)
+			}
+			if os.IsNotExist(err) {
+				return nil
+			}
+			return err
+		}
+
+		// skip checking if symlinks point to non-existing files, such symlinks can be useful
+		// also skip named pipes, because they hanging on open
+		if f.Mode()&(os.ModeSymlink|os.ModeNamedPipe) != 0 {
+			return nil
+		}
+
+		if !f.IsDir() {
+			currentFile, err := os.Open(filePath)
+			if err != nil && os.IsPermission(err) {
+				return fmt.Errorf("no permission to read from '%s'", filePath)
+			}
+			currentFile.Close()
+		}
+		return nil
+	})
+}
+
 // validateTag checks if the given image name can be resolved.
 func validateTag(rawRepo string) (string, error) {
 	_, err := reference.ParseNamed(rawRepo)

+ 1 - 1
utils/utils_unix.go → api/client/utils_unix.go

@@ -1,6 +1,6 @@
 // +build !windows
 
-package utils
+package client
 
 import (
 	"path/filepath"

+ 1 - 1
utils/utils_windows.go → api/client/utils_windows.go

@@ -1,6 +1,6 @@
 // +build windows
 
-package utils
+package client
 
 import (
 	"path/filepath"

+ 0 - 49
utils/utils.go

@@ -15,7 +15,6 @@ import (
 	"github.com/docker/distribution/registry/api/errcode"
 	"github.com/docker/docker/dockerversion"
 	"github.com/docker/docker/pkg/archive"
-	"github.com/docker/docker/pkg/fileutils"
 	"github.com/docker/docker/pkg/stringid"
 )
 
@@ -195,54 +194,6 @@ func ReplaceOrAppendEnvValues(defaults, overrides []string) []string {
 	return defaults
 }
 
-// ValidateContextDirectory checks if all the contents of the directory
-// can be read and returns an error if some files can't be read
-// symlinks which point to non-existing files don't trigger an error
-func ValidateContextDirectory(srcPath string, excludes []string) error {
-	contextRoot, err := getContextRoot(srcPath)
-	if err != nil {
-		return err
-	}
-	return filepath.Walk(contextRoot, func(filePath string, f os.FileInfo, err error) error {
-		// skip this directory/file if it's not in the path, it won't get added to the context
-		if relFilePath, err := filepath.Rel(contextRoot, filePath); err != nil {
-			return err
-		} else if skip, err := fileutils.Matches(relFilePath, excludes); err != nil {
-			return err
-		} else if skip {
-			if f.IsDir() {
-				return filepath.SkipDir
-			}
-			return nil
-		}
-
-		if err != nil {
-			if os.IsPermission(err) {
-				return fmt.Errorf("can't stat '%s'", filePath)
-			}
-			if os.IsNotExist(err) {
-				return nil
-			}
-			return err
-		}
-
-		// skip checking if symlinks point to non-existing files, such symlinks can be useful
-		// also skip named pipes, because they hanging on open
-		if f.Mode()&(os.ModeSymlink|os.ModeNamedPipe) != 0 {
-			return nil
-		}
-
-		if !f.IsDir() {
-			currentFile, err := os.Open(filePath)
-			if err != nil && os.IsPermission(err) {
-				return fmt.Errorf("no permission to read from '%s'", filePath)
-			}
-			currentFile.Close()
-		}
-		return nil
-	})
-}
-
 // GetErrorMessage returns the human readable message associated with
 // the passed-in error. In some cases the default Error() func returns
 // something that is less than useful so based on its types this func