Fix .dockerignore when ignoring unreadable dirs

The initial `ValidateContextDirectory` implementation fails loudly when a file
lacks read permissions in the current context. However that situation is valid
if the file is included in the `.dockerignore` patterns.

Docker-DCO-1.1-Signed-off-by: Bruno Renié <brutasse@gmail.com> (github: brutasse)
Upstream-commit: 27cca4c70cc7f0cccfda693ca7bbb2422eec3e13
Component: engine
This commit is contained in:
Bruno Renié
2014-07-07 14:23:07 +02:00
committed by Michael Crosby
parent 0c3330ed80
commit 604012d742
7 changed files with 86 additions and 38 deletions

View File

@ -684,16 +684,27 @@ func TreeSize(dir string) (size int64, err error) {
// 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) error {
func ValidateContextDirectory(srcPath string, excludes []string) error {
var finalError error
filepath.Walk(filepath.Join(srcPath, "."), 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
_, err = filepath.Rel(srcPath, filePath)
relFilePath, err := filepath.Rel(srcPath, filePath)
if err != nil && os.IsPermission(err) {
return nil
}
skip, err := Matches(relFilePath, excludes)
if err != nil {
finalError = err
}
if skip {
if f.IsDir() {
return filepath.SkipDir
}
return nil
}
if _, err := os.Stat(filePath); err != nil && os.IsPermission(err) {
finalError = fmt.Errorf("can't stat '%s'", filePath)
return err
@ -726,3 +737,23 @@ func StringsContainsNoCase(slice []string, s string) bool {
}
return false
}
// Matches returns true if relFilePath matches any of the patterns
func Matches(relFilePath string, patterns []string) (bool, error) {
for _, exclude := range patterns {
matched, err := filepath.Match(exclude, relFilePath)
if err != nil {
Errorf("Error matching: %s (pattern: %s)", relFilePath, exclude)
return false, err
}
if matched {
if filepath.Clean(relFilePath) == "." {
Errorf("Can't exclude whole path, excluding pattern: %s", exclude)
continue
}
Debugf("Skipping excluded path: %s", relFilePath)
return true, nil
}
}
return false, nil
}