when copying, skip files with the same name

When using the COPY command, if the source and destination have the same
the file should be skipped rather than copied. This is to prevent the
file from being overwritten and therefore producing an empty file.

fixes #904
This commit is contained in:
poy 2019-12-08 00:29:25 -07:00
parent 034ac9e258
commit 0a2f2957ec
2 changed files with 44 additions and 0 deletions

View File

@ -551,6 +551,12 @@ func CopyFile(src, dest, buildcontext string) (bool, error) {
logrus.Debugf("%s found in .dockerignore, ignoring", src)
return true, nil
}
if src == dest {
// This is a no-op. Move on, but don't list it as ignored.
// We have to make sure we do this so we don't overwrite our own file.
// See iusse #904 for an example.
return false, nil
}
fi, err := os.Stat(src)
if err != nil {
return false, err

View File

@ -825,3 +825,41 @@ func Test_correctDockerignoreFileIsUsed(t *testing.T) {
}
}
}
func Test_CopyFile_skips_self(t *testing.T) {
t.Parallel()
tempDir, err := ioutil.TempDir("", "kaniko_test")
if err != nil {
t.Fatal(err)
}
tempFile := filepath.Join(tempDir, "foo")
expected := "bar"
if err := ioutil.WriteFile(
tempFile,
[]byte(expected),
0755,
); err != nil {
t.Fatal(err)
}
ignored, err := CopyFile(tempFile, tempFile, "")
if err != nil {
t.Fatal(err)
}
if ignored {
t.Fatal("expected file to NOT be ignored")
}
// Ensure file has expected contents
actualData, err := ioutil.ReadFile(tempFile)
if err != nil {
t.Fatal(err)
}
if actual := string(actualData); actual != expected {
t.Fatalf("expected file contents to be %q, but got %q", expected, actual)
}
}