diff --git a/src/main/java/com/github/dockerjava/core/util/CompressArchiveUtil.java b/src/main/java/com/github/dockerjava/core/util/CompressArchiveUtil.java index 5052dda40..2e75bcc41 100644 --- a/src/main/java/com/github/dockerjava/core/util/CompressArchiveUtil.java +++ b/src/main/java/com/github/dockerjava/core/util/CompressArchiveUtil.java @@ -67,7 +67,11 @@ public static void tar(Path inputPath, Path outputPath, boolean gZipped, boolean try (TarArchiveOutputStream tarArchiveOutputStream = buildTarStream(outputPath, gZipped)) { if (!Files.isDirectory(inputPath)) { - putTarEntry(tarArchiveOutputStream, new TarArchiveEntry(inputPath.getFileName().toString()), inputPath); + TarArchiveEntry tarEntry = new TarArchiveEntry(inputPath.getFileName().toString()); + if (inputPath.toFile().canExecute()) { + tarEntry.setMode(tarEntry.getMode() | 0755); + } + putTarEntry(tarArchiveOutputStream, tarEntry, inputPath); } else { Path sourcePath = inputPath; if (!childrenOnly) { diff --git a/src/main/java/com/github/dockerjava/core/util/TarDirWalker.java b/src/main/java/com/github/dockerjava/core/util/TarDirWalker.java index af263ee9c..bbe834ee6 100644 --- a/src/main/java/com/github/dockerjava/core/util/TarDirWalker.java +++ b/src/main/java/com/github/dockerjava/core/util/TarDirWalker.java @@ -33,8 +33,11 @@ public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) th @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { - CompressArchiveUtil.putTarEntry(tarArchiveOutputStream, - new TarArchiveEntry(FilePathUtil.relativize(basePath, file)), file); + TarArchiveEntry tarEntry = new TarArchiveEntry(FilePathUtil.relativize(basePath, file)); + if (file.toFile().canExecute()) { + tarEntry.setMode(tarEntry.getMode() | 0755); + } + CompressArchiveUtil.putTarEntry(tarArchiveOutputStream, tarEntry, file); return FileVisitResult.CONTINUE; } diff --git a/src/test/java/com/github/dockerjava/core/command/CopyArchiveToContainerCmdImplTest.java b/src/test/java/com/github/dockerjava/core/command/CopyArchiveToContainerCmdImplTest.java index 06967706f..d96a6ad25 100644 --- a/src/test/java/com/github/dockerjava/core/command/CopyArchiveToContainerCmdImplTest.java +++ b/src/test/java/com/github/dockerjava/core/command/CopyArchiveToContainerCmdImplTest.java @@ -3,6 +3,7 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.isEmptyOrNullString; import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.equalTo; import java.io.IOException; import java.io.InputStream; @@ -117,5 +118,39 @@ public void copyDirWithLastAddedTarEnryEmptyDir() throws Exception{ .withHostResource(localDir.toString()) .exec(); } + + @Test + public void copyFileWithExecutePermission() throws Exception { + // create script file, add permission to execute + Path scriptPath = Files.createTempFile("run", ".sh"); + boolean executable = scriptPath.toFile().setExecutable(true, false); + if (!executable){ + throw new Exception("Execute permission on file not set!"); + } + String snippet = "Running script with execute permission."; + String scriptTextStr = "#!/bin/sh\necho \"" + snippet + "\""; + // write content for created script + Files.write(scriptPath, scriptTextStr.getBytes()); + // create a test container which starts and waits 3 seconds for the + // script to be copied to the container's home dir and then executes it + String containerCmd = "sleep 3; /home/" + scriptPath.getFileName().toString(); + CreateContainerResponse container = dockerClient.createContainerCmd("busybox") + .withName("test") + .withCmd("/bin/sh", "-c", containerCmd) + .exec(); + // start the container + dockerClient.startContainerCmd(container.getId()).exec(); + // copy script to container home dir + dockerClient.copyArchiveToContainerCmd(container.getId()) + .withRemotePath("/home") + .withHostResource(scriptPath.toString()) + .exec(); + // await exid code + int exitCode = dockerClient.waitContainerCmd(container.getId()) + .exec(new WaitContainerResultCallback()) + .awaitStatusCode(); + // check result + assertThat(exitCode, equalTo(0)); + } }