Skip to content
This repository was archived by the owner on Jun 3, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 3 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
56 changes: 39 additions & 17 deletions pkg/commands/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/GoogleContainerTools/kaniko/pkg/constants"
"github.com/moby/buildkit/frontend/dockerfile/instructions"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"

"github.com/GoogleContainerTools/kaniko/pkg/constants"

"github.com/GoogleContainerTools/kaniko/pkg/dockerfile"
"github.com/GoogleContainerTools/kaniko/pkg/util"
v1 "github.com/google/go-containerregistry/pkg/v1"
Expand Down Expand Up @@ -59,11 +59,15 @@ func (c *CopyCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.Bu
if err != nil {
return err
}
if fi.IsDir() && !strings.HasSuffix(fullPath, string(os.PathSeparator)) {
Comment thread
cvgw marked this conversation as resolved.
fullPath += "/"
Comment thread
cvgw marked this conversation as resolved.
}
cwd := config.WorkingDir
if cwd == "" {
cwd = constants.RootDir
}
destPath, err := util.DestinationFilepath(src, dest, cwd)

destPath, err := util.DestinationFilepath(fullPath, dest, cwd)
Comment thread
cvgw marked this conversation as resolved.
if err != nil {
return err
}
Expand All @@ -76,11 +80,7 @@ func (c *CopyCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.Bu
}

if fi.IsDir() {
if !filepath.IsAbs(dest) {
// we need to add '/' to the end to indicate the destination is a directory
dest = filepath.Join(cwd, dest) + "/"
}
copiedFiles, err := util.CopyDir(fullPath, dest, c.buildcontext)
copiedFiles, err := util.CopyDir(fullPath, destPath, c.buildcontext)
if err != nil {
return err
}
Expand Down Expand Up @@ -197,21 +197,43 @@ func (cr *CachingCopyCommand) From() string {
}

func resolveIfSymlink(destPath string) (string, error) {
baseDir := filepath.Dir(destPath)
if info, err := os.Lstat(baseDir); err == nil {
if !filepath.IsAbs(destPath) {
Comment thread
cvgw marked this conversation as resolved.
return "", errors.New("dest path must be abs")
}

pathPrefix := "/"
pathTokens := strings.Split(destPath, string(os.PathSeparator))
for i, pathToken := range pathTokens {
Comment thread
cvgw marked this conversation as resolved.
Outdated
currentPath := filepath.Join(pathPrefix, pathToken)
info, err := os.Lstat(currentPath)
if err != nil {
if os.IsNotExist(err) {
pathPrefix = filepath.Join(append([]string{currentPath}, pathTokens[i+1:]...)...)
break
} else {
return "", errors.Wrap(err, "failed to lstat")
}
}

switch mode := info.Mode(); {
case mode&os.ModeSymlink != 0:
linkPath, err := os.Readlink(baseDir)
linkPath, err := os.Readlink(currentPath)
if err != nil {
return "", errors.Wrap(err, "error reading symlink")
return "", errors.Wrap(err, "failed to read symlink")
}
if filepath.IsAbs(linkPath) {
pathPrefix = linkPath
} else {
pathPrefix = filepath.Join(pathPrefix, linkPath)
}
absLinkPath := filepath.Join(filepath.Dir(baseDir), linkPath)
newPath := filepath.Join(absLinkPath, filepath.Base(destPath))
logrus.Tracef("Updating destination path from %v to %v due to symlink", destPath, newPath)
return newPath, nil
default:
pathPrefix = filepath.Join(pathPrefix, pathToken)
}
}
return destPath, nil
if destPath != pathPrefix {
logrus.Tracef("Updating destination path from %v to %v due to symlink", destPath, pathPrefix)
}
return filepath.Clean(pathPrefix), nil
}

func copyCmdFilesUsedFromContext(
Expand Down
14 changes: 13 additions & 1 deletion pkg/commands/copy_test.go
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,19 @@ func Test_resolveIfSymlink(t *testing.T) {
if err := os.Symlink(filepath.Base(thepath), symLink); err != nil {
t.Error(err)
}
cases = append(cases, testCase{filepath.Join(symLink, "foo.txt"), filepath.Join(thepath, "foo.txt"), nil})
cases = append(cases,
testCase{filepath.Join(symLink, "foo.txt"), filepath.Join(thepath, "foo.txt"), nil},
testCase{filepath.Join(symLink, "inner", "foo.txt"), filepath.Join(thepath, "inner", "foo.txt"), nil},
)

absSymlink := filepath.Join(tmpDir, "abs-symlink")
if err := os.Symlink(thepath, absSymlink); err != nil {
t.Error(err)
}
cases = append(cases,
testCase{filepath.Join(absSymlink, "foo.txt"), filepath.Join(thepath, "foo.txt"), nil},
testCase{filepath.Join(absSymlink, "inner", "foo.txt"), filepath.Join(thepath, "inner", "foo.txt"), nil},
)

for i, c := range cases {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
Expand Down
25 changes: 15 additions & 10 deletions pkg/util/command_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,20 +164,25 @@ func IsDestDir(path string) bool {
// If dest is a dir, copy it to /dest/relpath
// If dest is a file, copy directly to dest
// If source is a dir:
// Assume dest is also a dir, and copy to dest/relpath
// Assume dest is also a dir, and copy to dest/
// If dest is not an absolute filepath, add /cwd to the beginning
func DestinationFilepath(src, dest, cwd string) (string, error) {
if IsDestDir(dest) {
destPath := filepath.Join(dest, filepath.Base(src))
if filepath.IsAbs(dest) {
return destPath, nil
}
return filepath.Join(cwd, destPath), nil
_, srcFileName := filepath.Split(src)
newDest := dest

if IsDestDir(newDest) {
newDest = filepath.Join(newDest, srcFileName)
}
if filepath.IsAbs(dest) {
return dest, nil

if !filepath.IsAbs(newDest) {
newDest = filepath.Join(cwd, newDest)
}
return filepath.Join(cwd, dest), nil

if len(srcFileName) <= 0 && !strings.HasSuffix(newDest, "/") {
newDest += "/"
}

return newDest, nil
}

// URLDestinationFilepath gives the destination a file from a remote URL should be saved to
Expand Down
6 changes: 3 additions & 3 deletions pkg/util/command_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,13 @@ var destinationFilepathTests = []struct {
src: "context/bar/",
cwd: "/",
dest: "pkg/",
expectedFilepath: "/pkg/bar",
expectedFilepath: "/pkg/",
},
{
src: "context/bar/",
cwd: "/newdir",
dest: "pkg/",
expectedFilepath: "/newdir/pkg/bar",
expectedFilepath: "/newdir/pkg/",
},
{
src: "./context/empty",
Expand All @@ -177,7 +177,7 @@ var destinationFilepathTests = []struct {
src: "./",
cwd: "/",
dest: "/dir",
expectedFilepath: "/dir",
expectedFilepath: "/dir/",
},
{
src: "context/foo",
Expand Down