X Tutup
Skip to content

Commit dfd3dfa

Browse files
author
Marcus Linke
committed
Merge branch 'denlap007-master't push origin master
2 parents 8b5bada + 572a1ea commit dfd3dfa

File tree

4 files changed

+61
-4
lines changed

4 files changed

+61
-4
lines changed

src/main/java/com/github/dockerjava/core/command/CopyArchiveToContainerCmdImpl.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import org.apache.commons.lang.builder.ToStringBuilder;
1313

1414
import com.github.dockerjava.api.command.CopyArchiveToContainerCmd;
15-
import com.github.dockerjava.api.exception.BadRequestException;
15+
import com.github.dockerjava.api.exception.DockerClientException;
1616
import com.github.dockerjava.api.exception.NotFoundException;
1717
import com.github.dockerjava.core.util.CompressArchiveUtil;
1818

@@ -127,18 +127,18 @@ public Void exec() throws NotFoundException {
127127
if (StringUtils.isNotEmpty(this.hostResource)) {
128128
// User set host resource and not directly a stream
129129
if (this.tarInputStream != null) {
130-
throw new BadRequestException(
130+
throw new DockerClientException(
131131
"Only one of host resource or tar input stream should be defined to perform the copy, not both");
132132
}
133133
// We compress the given path, call exec so that the stream is consumed and then close it our self
134134
try (InputStream uploadStream = buildUploadStream(this.hostResource, this.dirChildrenOnly)) {
135135
this.tarInputStream = uploadStream;
136136
return super.exec();
137137
} catch (IOException e) {
138-
throw new BadRequestException("Unable to perform tar on host resource " + this.hostResource);
138+
throw new DockerClientException("Unable to perform tar on host resource " + this.hostResource, e);
139139
}
140140
} else if (this.tarInputStream == null) {
141-
throw new BadRequestException(
141+
throw new DockerClientException(
142142
"One of host resource or tar input stream must be defined to perform the copy");
143143
}
144144
// User set a stream, so we will just consume it and let the user close it by him self

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public TarDirWalker(Path basePath, TarArchiveOutputStream tarArchiveOutputStream
2626
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
2727
if (!dir.equals(basePath)) {
2828
tarArchiveOutputStream.putArchiveEntry(new TarArchiveEntry(FilePathUtil.relativize(basePath, dir)));
29+
tarArchiveOutputStream.closeArchiveEntry();
2930
}
3031
return FileVisitResult.CONTINUE;
3132
}

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.nio.file.Path;
1212
import java.nio.file.Paths;
1313

14+
import org.apache.commons.io.FileUtils;
1415
import org.testng.ITestResult;
1516
import org.testng.annotations.AfterMethod;
1617
import org.testng.annotations.AfterTest;
@@ -91,4 +92,31 @@ public void copyToNonExistingContainer() throws Exception {
9192
}
9293
}
9394

95+
@Test
96+
public void copyDirWithLastAddedTarEntryEmptyDir() throws Exception{
97+
// create a temp dir
98+
Path localDir = Files.createTempDirectory(null);
99+
localDir.toFile().deleteOnExit();
100+
// create empty sub-dir with name b
101+
Files.createDirectory(localDir.resolve("b"));
102+
// create sub-dir with name a
103+
Path dirWithFile = Files.createDirectory(localDir.resolve("a"));
104+
// create file in sub-dir b, name or conter are irrelevant
105+
Files.createFile(dirWithFile.resolve("file"));
106+
107+
// create a test container
108+
CreateContainerResponse container = dockerClient.createContainerCmd("busybox")
109+
.withCmd("sleep", "9999")
110+
.exec();
111+
// start the container
112+
dockerClient.startContainerCmd(container.getId()).exec();
113+
// copy data from local dir to container
114+
dockerClient.copyArchiveToContainerCmd(container.getId())
115+
.withHostResource(localDir.toString())
116+
.exec();
117+
118+
// cleanup dir
119+
FileUtils.deleteDirectory(localDir.toFile());
120+
}
121+
94122
}

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.nio.file.Path;
1212
import java.nio.file.Paths;
1313

14+
import org.apache.commons.io.FileUtils;
1415
import org.testng.ITestResult;
1516
import org.testng.annotations.AfterMethod;
1617
import org.testng.annotations.AfterTest;
@@ -91,4 +92,31 @@ public void copyToNonExistingContainer() throws Exception {
9192
}
9293
}
9394

95+
@Test
96+
public void copyDirWithLastAddedTarEntryEmptyDir() throws Exception{
97+
// create a temp dir
98+
Path localDir = Files.createTempDirectory(null);
99+
localDir.toFile().deleteOnExit();
100+
// create empty sub-dir with name b
101+
Files.createDirectory(localDir.resolve("b"));
102+
// create sub-dir with name a
103+
Path dirWithFile = Files.createDirectory(localDir.resolve("a"));
104+
// create file in sub-dir b, name or conter are irrelevant
105+
Files.createFile(dirWithFile.resolve("file"));
106+
107+
// create a test container
108+
CreateContainerResponse container = dockerClient.createContainerCmd("busybox")
109+
.withCmd("sleep", "9999")
110+
.exec();
111+
// start the container
112+
dockerClient.startContainerCmd(container.getId()).exec();
113+
// copy data from local dir to container
114+
dockerClient.copyArchiveToContainerCmd(container.getId())
115+
.withHostResource(localDir.toString())
116+
.exec();
117+
118+
// cleanup dir
119+
FileUtils.deleteDirectory(localDir.toFile());
120+
}
121+
94122
}

0 commit comments

Comments
 (0)
X Tutup