This is an automated email from the ASF dual-hosted git repository.

astefanutti pushed a commit to branch release-1.4.x
in repository https://gitbox.apache.org/repos/asf/camel-k.git


The following commit(s) were added to refs/heads/release-1.4.x by this push:
     new cad7f9c  fix(cmd): run file system stat errors
cad7f9c is described below

commit cad7f9ce58b52dc29ff252d8aca0ec15b7811eb4
Author: Pasquale Congiusti <pasquale.congiu...@gmail.com>
AuthorDate: Fri Apr 30 16:01:59 2021 +0200

    fix(cmd): run file system stat errors
    
    * Introducing a check to make sure that we don't panic if any kind of 
filesystem error but file not found is reported
    * Adding a unit test to check a permission error on /root won't panic
---
 pkg/cmd/run.go          | 18 +++++++++++++-----
 pkg/cmd/run_test.go     |  7 +++++++
 pkg/cmd/util_content.go |  7 ++++++-
 pkg/cmd/util_sources.go |  7 ++++++-
 4 files changed, 32 insertions(+), 7 deletions(-)

diff --git a/pkg/cmd/run.go b/pkg/cmd/run.go
index 255feaf..d178805 100644
--- a/pkg/cmd/run.go
+++ b/pkg/cmd/run.go
@@ -376,7 +376,11 @@ func (o *runCmdOptions) syncIntegration(cmd 
*cobra.Command, c client.Client, sou
        files = append(files, o.OpenAPIs...)
 
        for _, s := range files {
-               if isLocalAndFileExists(s) {
+               ok, err := isLocalAndFileExists(s)
+               if err != nil {
+                       return err
+               }
+               if ok {
                        changes, err := sync.File(o.Context, s)
                        if err != nil {
                                return err
@@ -680,12 +684,16 @@ func (o *runCmdOptions) configureTraits(integration 
*v1.Integration, options []s
        return nil
 }
 
-func isLocalAndFileExists(fileName string) bool {
+func isLocalAndFileExists(fileName string) (bool, error) {
        info, err := os.Stat(fileName)
-       if os.IsNotExist(err) {
-               return false
+       if err != nil {
+               if os.IsNotExist(err) {
+                       return false, nil
+               }
+               // If it is a different error (ie, permission denied) we should 
report it back
+               return false, errors.Wrap(err, fmt.Sprintf("file system error 
while looking for %s", fileName))
        }
-       return !info.IsDir()
+       return !info.IsDir(), nil
 }
 
 func addPropertyFile(fileName string, spec *v1.IntegrationSpec) error {
diff --git a/pkg/cmd/run_test.go b/pkg/cmd/run_test.go
index 212765b..17db272 100644
--- a/pkg/cmd/run_test.go
+++ b/pkg/cmd/run_test.go
@@ -513,3 +513,10 @@ func TestRunTextCompressedResource(t *testing.T) {
        assert.Equal(t, "text/plain", textResourceSpec.ContentType)
        assert.True(t, textResourceSpec.Compression)
 }
+
+func TestIsLocalFileAndExists(t *testing.T) {
+       value, err := isLocalAndFileExists("/root/test")
+       // must not panic because a permission error
+       assert.NotNil(t, err)
+       assert.False(t, value)
+}
diff --git a/pkg/cmd/util_content.go b/pkg/cmd/util_content.go
index 7950e44..c02fda2 100644
--- a/pkg/cmd/util_content.go
+++ b/pkg/cmd/util_content.go
@@ -30,7 +30,12 @@ func loadRawContent(source string) ([]byte, string, error) {
        var content []byte
        var err error
 
-       if isLocalAndFileExists(source) {
+       ok, err := isLocalAndFileExists(source)
+       if err != nil {
+               return nil, "", err
+       }
+
+       if ok {
                content, err = ioutil.ReadFile(source)
        } else {
                u, err := url.Parse(source)
diff --git a/pkg/cmd/util_sources.go b/pkg/cmd/util_sources.go
index f539705..6368d05 100644
--- a/pkg/cmd/util_sources.go
+++ b/pkg/cmd/util_sources.go
@@ -65,7 +65,12 @@ func ResolveSources(ctx context.Context, locations []string, 
compress bool) ([]S
        sources := make([]Source, 0, len(locations))
 
        for _, location := range locations {
-               if isLocalAndFileExists(location) {
+               ok, err := isLocalAndFileExists(location)
+               if err != nil {
+                       return sources, err
+               }
+
+               if ok {
                        answer, err := ResolveLocalSource(location, compress)
                        if err != nil {
                                return sources, err

Reply via email to