Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix resolve link for dirs with trailing / #1113

Merged
merged 1 commit into from
Mar 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/commands/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (c *CopyCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.Bu
fullPath := filepath.Join(c.buildcontext, src)
fi, err := os.Lstat(fullPath)
if err != nil {
return err
return errors.Wrap(err, "could not copy source")
}
if fi.IsDir() && !strings.HasSuffix(fullPath, string(os.PathSeparator)) {
fullPath += "/"
Expand Down
3 changes: 2 additions & 1 deletion pkg/executor/composite_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"strings"

"github.com/GoogleContainerTools/kaniko/pkg/util"
"github.com/pkg/errors"
)

// NewCompositeCache returns an initialized composite cache object.
Expand Down Expand Up @@ -58,7 +59,7 @@ func (s *CompositeCache) AddPath(p, context string) error {
sha := sha256.New()
fi, err := os.Lstat(p)
if err != nil {
return err
return errors.Wrap(err, "could not add path")
}

if fi.Mode().IsDir() {
Expand Down
4 changes: 1 addition & 3 deletions pkg/filesystem/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func resolveSymlinkAncestor(path string) (string, error) {
}

last := ""
newPath := path
newPath := filepath.Clean(path)

loop:
for newPath != "/" {
Expand All @@ -149,7 +149,6 @@ loop:
if err != nil {
return "", err
}

if target != newPath {
last = filepath.Base(newPath)
newPath = filepath.Dir(newPath)
Expand All @@ -158,7 +157,6 @@ loop:
}
}
}

newPath = filepath.Join(newPath, last)
return filepath.Clean(newPath), nil
}
22 changes: 22 additions & 0 deletions pkg/filesystem/resolve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package filesystem

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
Expand Down Expand Up @@ -234,6 +235,27 @@ func Test_resolveSymlinkAncestor(t *testing.T) {
}
})

t.Run("dir ends with / is not a symlink", func(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a test for "dir ends with / is a symlink" ?

testDir, _ := setupDirs(t)
defer os.RemoveAll(testDir)

linkDir := filepath.Join(testDir, "var", "www")
if err := os.MkdirAll(linkDir, 0777); err != nil {
t.Fatal(err)
}

expected := linkDir

actual, err := resolveSymlinkAncestor(fmt.Sprintf("%s/", linkDir))
if err != nil {
t.Errorf("expected err to be nil but was %s", err)
}

if actual != expected {
t.Errorf("expected result to be %s not %s", expected, actual)
}
})

t.Run("path is a dead symlink", func(t *testing.T) {
testDir, targetPath := setupDirs(t)
defer os.RemoveAll(testDir)
Expand Down
3 changes: 1 addition & 2 deletions pkg/util/fs_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -577,8 +577,7 @@ func CopyDir(src, dest, buildcontext string, uid, gid int64) ([]string, error) {
fullPath := filepath.Join(src, file)
fi, err := os.Lstat(fullPath)
if err != nil {
fmt.Println(" i am returning from here this", err)
return nil, err
return nil, errors.Wrap(err, "copying dir")
}
if ExcludeFile(fullPath, buildcontext) {
logrus.Debugf("%s found in .dockerignore, ignoring", src)
Expand Down