Skip to content

Commit e80c386

Browse files
author
Marcus Linke
committed
Merge branch 'master' of https://github.com/denlap007/docker-java into denlap007-master
2 parents e0d418b + 381b7db commit e80c386

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

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
}

0 commit comments

Comments
 (0)