From 9a93f5bad9a5729ca248861bc01e63dd3e8c38e1 Mon Sep 17 00:00:00 2001 From: Priya Wadhwa Date: Mon, 27 Aug 2018 11:39:00 -0700 Subject: [PATCH] Snapshot only specific files for COPY Before #289 was merged, when copying over directories for COPY kaniko would get a list of all files at the destination specified and add them to the list of files to be snapshotted. If the destination was root it would add all files. This worked because the snapshotter made sure the file had been changed before adding it to the layer. After #289, we changed the logic to add all files snapshotted to a layer without checking if the files had been changed. This created the bug in got all the files at root and added them to the layer without checking if they had been changed. This change should fix this bug. Now, the CopyDir function returns a list of files it copied over and only those files are added to the list of files to be snapshotted. Should fix #314 --- pkg/commands/copy.go | 5 +---- pkg/util/fs_util.go | 19 +++++++++++-------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/pkg/commands/copy.go b/pkg/commands/copy.go index 5bd66282d..020a61bb4 100644 --- a/pkg/commands/copy.go +++ b/pkg/commands/copy.go @@ -79,10 +79,7 @@ func (c *CopyCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.Bu // we need to add '/' to the end to indicate the destination is a directory dest = filepath.Join(cwd, dest) + "/" } - if err := util.CopyDir(fullPath, dest); err != nil { - return err - } - copiedFiles, err := util.Files(dest) + copiedFiles, err := util.CopyDir(fullPath, dest) if err != nil { return err } diff --git a/pkg/util/fs_util.go b/pkg/util/fs_util.go index 1a4f958bb..858312b24 100644 --- a/pkg/util/fs_util.go +++ b/pkg/util/fs_util.go @@ -459,16 +459,18 @@ func DownloadFileToDest(rawurl, dest string) error { } // CopyDir copies the file or directory at src to dest -func CopyDir(src, dest string) error { +// It returns a list of files it copied over +func CopyDir(src, dest string) ([]string, error) { files, err := RelativeFiles("", src) if err != nil { - return err + return nil, err } + var copiedFiles []string for _, file := range files { fullPath := filepath.Join(src, file) fi, err := os.Lstat(fullPath) if err != nil { - return err + return nil, err } destPath := filepath.Join(dest, file) if fi.IsDir() { @@ -478,24 +480,25 @@ func CopyDir(src, dest string) error { gid := int(fi.Sys().(*syscall.Stat_t).Gid) if err := os.MkdirAll(destPath, fi.Mode()); err != nil { - return err + return nil, err } if err := os.Chown(destPath, uid, gid); err != nil { - return err + return nil, err } } else if fi.Mode()&os.ModeSymlink != 0 { // If file is a symlink, we want to create the same relative symlink if err := CopySymlink(fullPath, destPath); err != nil { - return err + return nil, err } } else { // ... Else, we want to copy over a file if err := CopyFile(fullPath, destPath); err != nil { - return err + return nil, err } } + copiedFiles = append(copiedFiles, destPath) } - return nil + return copiedFiles, nil } // CopySymlink copies the symlink at src to dest