Skip to content

Commit b881b04

Browse files
author
Marcus Linke
committed
Merge branch 'denlap007-master't push origin master
2 parents e0d418b + 328cd67 commit b881b04

4 files changed

Lines changed: 81 additions & 3 deletions

File tree

src/main/java/com/github/dockerjava/core/util/CompressArchiveUtil.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,11 @@ public static void tar(Path inputPath, Path outputPath, boolean gZipped, boolean
6767

6868
try (TarArchiveOutputStream tarArchiveOutputStream = buildTarStream(outputPath, gZipped)) {
6969
if (!Files.isDirectory(inputPath)) {
70-
putTarEntry(tarArchiveOutputStream, new TarArchiveEntry(inputPath.getFileName().toString()), inputPath);
70+
TarArchiveEntry tarEntry = new TarArchiveEntry(inputPath.getFileName().toString());
71+
if (inputPath.toFile().canExecute()) {
72+
tarEntry.setMode(tarEntry.getMode() | 0755);
73+
}
74+
putTarEntry(tarArchiveOutputStream, tarEntry, inputPath);
7175
} else {
7276
Path sourcePath = inputPath;
7377
if (!childrenOnly) {

src/main/java/com/github/dockerjava/core/util/TarDirWalker.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,11 @@ public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) th
3333

3434
@Override
3535
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
36-
CompressArchiveUtil.putTarEntry(tarArchiveOutputStream,
37-
new TarArchiveEntry(FilePathUtil.relativize(basePath, file)), file);
36+
TarArchiveEntry tarEntry = new TarArchiveEntry(FilePathUtil.relativize(basePath, file));
37+
if (file.toFile().canExecute()) {
38+
tarEntry.setMode(tarEntry.getMode() | 0755);
39+
}
40+
CompressArchiveUtil.putTarEntry(tarArchiveOutputStream, tarEntry, file);
3841
return FileVisitResult.CONTINUE;
3942
}
4043

src/test/java/com/github/dockerjava/core/command/CopyArchiveToContainerCmdImplTest.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static org.hamcrest.MatcherAssert.assertThat;
44
import static org.hamcrest.Matchers.isEmptyOrNullString;
55
import static org.hamcrest.Matchers.not;
6+
import static org.hamcrest.Matchers.equalTo;
67

78
import java.io.IOException;
89
import java.io.InputStream;
@@ -118,5 +119,39 @@ public void copyDirWithLastAddedTarEntryEmptyDir() throws Exception{
118119
// cleanup dir
119120
FileUtils.deleteDirectory(localDir.toFile());
120121
}
122+
123+
@Test
124+
public void copyFileWithExecutePermission() throws Exception {
125+
// create script file, add permission to execute
126+
Path scriptPath = Files.createTempFile("run", ".sh");
127+
boolean executable = scriptPath.toFile().setExecutable(true, false);
128+
if (!executable){
129+
throw new Exception("Execute permission on file not set!");
130+
}
131+
String snippet = "Running script with execute permission.";
132+
String scriptTextStr = "#!/bin/sh\necho \"" + snippet + "\"";
133+
// write content for created script
134+
Files.write(scriptPath, scriptTextStr.getBytes());
135+
// create a test container which starts and waits 3 seconds for the
136+
// script to be copied to the container's home dir and then executes it
137+
String containerCmd = "sleep 3; /home/" + scriptPath.getFileName().toString();
138+
CreateContainerResponse container = dockerClient.createContainerCmd("busybox")
139+
.withName("test")
140+
.withCmd("/bin/sh", "-c", containerCmd)
141+
.exec();
142+
// start the container
143+
dockerClient.startContainerCmd(container.getId()).exec();
144+
// copy script to container home dir
145+
dockerClient.copyArchiveToContainerCmd(container.getId())
146+
.withRemotePath("/home")
147+
.withHostResource(scriptPath.toString())
148+
.exec();
149+
// await exid code
150+
int exitCode = dockerClient.waitContainerCmd(container.getId())
151+
.exec(new WaitContainerResultCallback())
152+
.awaitStatusCode();
153+
// check result
154+
assertThat(exitCode, equalTo(0));
155+
}
121156

122157
}

src/test/java/com/github/dockerjava/netty/exec/CopyArchiveToContainerCmdExecTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.github.dockerjava.netty.exec;
22

33
import static org.hamcrest.MatcherAssert.assertThat;
4+
import static org.hamcrest.Matchers.equalTo;
45
import static org.hamcrest.Matchers.isEmptyOrNullString;
56
import static org.hamcrest.Matchers.not;
67

@@ -21,6 +22,7 @@
2122

2223
import com.github.dockerjava.api.command.CreateContainerResponse;
2324
import com.github.dockerjava.api.exception.NotFoundException;
25+
import com.github.dockerjava.core.command.WaitContainerResultCallback;
2426
import com.github.dockerjava.core.util.CompressArchiveUtil;
2527
import com.github.dockerjava.netty.AbstractNettyDockerClientTest;
2628

@@ -119,4 +121,38 @@ public void copyDirWithLastAddedTarEntryEmptyDir() throws Exception{
119121
FileUtils.deleteDirectory(localDir.toFile());
120122
}
121123

124+
@Test
125+
public void copyFileWithExecutePermission() throws Exception {
126+
// create script file, add permission to execute
127+
Path scriptPath = Files.createTempFile("run", ".sh");
128+
boolean executable = scriptPath.toFile().setExecutable(true, false);
129+
if (!executable){
130+
throw new Exception("Execute permission on file not set!");
131+
}
132+
String snippet = "Running script with execute permission.";
133+
String scriptTextStr = "#!/bin/sh\necho \"" + snippet + "\"";
134+
// write content for created script
135+
Files.write(scriptPath, scriptTextStr.getBytes());
136+
// create a test container which starts and waits 3 seconds for the
137+
// script to be copied to the container's home dir and then executes it
138+
String containerCmd = "sleep 3; /home/" + scriptPath.getFileName().toString();
139+
CreateContainerResponse container = dockerClient.createContainerCmd("busybox")
140+
.withName("test")
141+
.withCmd("/bin/sh", "-c", containerCmd)
142+
.exec();
143+
// start the container
144+
dockerClient.startContainerCmd(container.getId()).exec();
145+
// copy script to container home dir
146+
dockerClient.copyArchiveToContainerCmd(container.getId())
147+
.withRemotePath("/home")
148+
.withHostResource(scriptPath.toString())
149+
.exec();
150+
// await exid code
151+
int exitCode = dockerClient.waitContainerCmd(container.getId())
152+
.exec(new WaitContainerResultCallback())
153+
.awaitStatusCode();
154+
// check result
155+
assertThat(exitCode, equalTo(0));
156+
}
157+
122158
}

0 commit comments

Comments
 (0)