Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}

}