|
@@ -1067,3 +1067,40 @@ func TreeSize(dir string) (size int64, err error) {
|
|
|
})
|
|
|
return
|
|
|
}
|
|
|
+
|
|
|
+// 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 {
|
|
|
+ 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)
|
|
|
+ if err != nil && os.IsPermission(err) {
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+
|
|
|
+ if _, err := os.Stat(filePath); err != nil && os.IsPermission(err) {
|
|
|
+ finalError = fmt.Errorf("can't stat '%s'", filePath)
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ // skip checking if symlinks point to non-existing files, such symlinks can be useful
|
|
|
+ lstat, _ := os.Lstat(filePath)
|
|
|
+ if lstat.Mode()&os.ModeSymlink == os.ModeSymlink {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ if !f.IsDir() {
|
|
|
+ currentFile, err := os.Open(filePath)
|
|
|
+ if err != nil && os.IsPermission(err) {
|
|
|
+ finalError = fmt.Errorf("no permission to read from '%s'", filePath)
|
|
|
+ return err
|
|
|
+ } else {
|
|
|
+ currentFile.Close()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+ })
|
|
|
+ return finalError
|
|
|
+}
|