mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Merge pull request #8026 from tonistiigi/validate-context-error-handling
Improve ValidateContextDirectory error handling.
This commit is contained in:
commit
23bb6513a0
1 changed files with 16 additions and 20 deletions
|
@ -523,48 +523,44 @@ func TreeSize(dir string) (size int64, err error) {
|
||||||
// can be read and returns an error if some files can't be read
|
// 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
|
// symlinks which point to non-existing files don't trigger an error
|
||||||
func ValidateContextDirectory(srcPath string, excludes []string) error {
|
func ValidateContextDirectory(srcPath string, excludes []string) error {
|
||||||
var finalError error
|
return filepath.Walk(filepath.Join(srcPath, "."), func(filePath string, f os.FileInfo, err error) 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
|
// skip this directory/file if it's not in the path, it won't get added to the context
|
||||||
relFilePath, err := filepath.Rel(srcPath, filePath)
|
if relFilePath, err := filepath.Rel(srcPath, filePath); err != nil {
|
||||||
if err != nil && os.IsPermission(err) {
|
return err
|
||||||
return nil
|
} else if skip, err := Matches(relFilePath, excludes); err != nil {
|
||||||
}
|
return err
|
||||||
|
} else if skip {
|
||||||
skip, err := Matches(relFilePath, excludes)
|
|
||||||
if err != nil {
|
|
||||||
finalError = err
|
|
||||||
}
|
|
||||||
if skip {
|
|
||||||
if f.IsDir() {
|
if f.IsDir() {
|
||||||
return filepath.SkipDir
|
return filepath.SkipDir
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := os.Stat(filePath); err != nil && os.IsPermission(err) {
|
if err != nil {
|
||||||
finalError = fmt.Errorf("can't stat '%s'", filePath)
|
if os.IsPermission(err) {
|
||||||
|
return fmt.Errorf("can't stat '%s'", filePath)
|
||||||
|
}
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// skip checking if symlinks point to non-existing files, such symlinks can be useful
|
// skip checking if symlinks point to non-existing files, such symlinks can be useful
|
||||||
// also skip named pipes, because they hanging on open
|
// also skip named pipes, because they hanging on open
|
||||||
lstat, _ := os.Lstat(filePath)
|
if f.Mode()&(os.ModeSymlink|os.ModeNamedPipe) != 0 {
|
||||||
if lstat != nil && lstat.Mode()&(os.ModeSymlink|os.ModeNamedPipe) != 0 {
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if !f.IsDir() {
|
if !f.IsDir() {
|
||||||
currentFile, err := os.Open(filePath)
|
currentFile, err := os.Open(filePath)
|
||||||
if err != nil && os.IsPermission(err) {
|
if err != nil && os.IsPermission(err) {
|
||||||
finalError = fmt.Errorf("no permission to read from '%s'", filePath)
|
return fmt.Errorf("no permission to read from '%s'", filePath)
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
currentFile.Close()
|
currentFile.Close()
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
return finalError
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func StringsContainsNoCase(slice []string, s string) bool {
|
func StringsContainsNoCase(slice []string, s string) bool {
|
||||||
|
|
Loading…
Reference in a new issue