From fd7af1a5e7a6d72559b0378c4bdef2214a93e9d3 Mon Sep 17 00:00:00 2001 From: Paul Holzinger Date: Mon, 14 Jun 2021 17:31:27 +0200 Subject: [PATCH] Fix copy into workdir for a single file When copying a single file to the workdir make sure to create the workdir and not to overwrite it as file. Fixes containers/podman#10671 Signed-off-by: Paul Holzinger --- add.go | 2 +- tests/bud.bats | 14 ++++++++++++++ tests/bud/copy-workdir/Dockerfile.2 | 4 ++++ 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 tests/bud/copy-workdir/Dockerfile.2 diff --git a/add.go b/add.go index 0a77e9f9d4..45ee50636c 100644 --- a/add.go +++ b/add.go @@ -287,7 +287,7 @@ func (b *Builder) Add(destination string, extract bool, options AddAndCopyOption destination = tmpDestination } } - destMustBeDirectory := (len(sources) > 1) || strings.HasSuffix(destination, string(os.PathSeparator)) + destMustBeDirectory := (len(sources) > 1) || strings.HasSuffix(destination, string(os.PathSeparator)) || destination == b.WorkDir() destCanBeFile := false if len(sources) == 1 { if len(remoteSources) == 1 { diff --git a/tests/bud.bats b/tests/bud.bats index f1349f2b7c..dd64376798 100644 --- a/tests/bud.bats +++ b/tests/bud.bats @@ -1830,6 +1830,20 @@ _EOF test -s "${root}"/subdir/file2.txt } +# regression test for https://github.com/containers/podman/issues/10671 +@test "bud-copy-workdir --layers" { + _prefetch alpine + + target=testimage + run_buildah bud --signature-policy ${TESTSDIR}/policy.json --layers -t ${target} -f Dockerfile.2 ${TESTSDIR}/bud/copy-workdir + run_buildah from ${target} + cid="$output" + run_buildah mount "${cid}" + root="$output" + test -d "${root}"/subdir + test -s "${root}"/subdir/file1.txt +} + @test "bud-build-arg-cache" { _prefetch busybox alpine target=derived-image diff --git a/tests/bud/copy-workdir/Dockerfile.2 b/tests/bud/copy-workdir/Dockerfile.2 new file mode 100644 index 0000000000..e156e78d64 --- /dev/null +++ b/tests/bud/copy-workdir/Dockerfile.2 @@ -0,0 +1,4 @@ +FROM alpine +WORKDIR /subdir +COPY file1.txt /subdir +RUN ls