Skip to content

Commit e1004d5

Browse files
committed
clean tmp file after upload
1 parent a14ace4 commit e1004d5

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,6 @@ public String toString() {
112112
.append(remotePath).toString();
113113
}
114114

115-
private InputStream buildUploadStream(String hostResource, boolean dirChildrenOnly) throws IOException {
116-
Path toUpload = Files.createTempFile("docker-java", ".tar.gz");
117-
CompressArchiveUtil.tar(Paths.get(hostResource), toUpload, true, dirChildrenOnly);
118-
return Files.newInputStream(toUpload);
119-
}
120-
121115
/**
122116
* @throws com.github.dockerjava.api.exception.NotFoundException
123117
* No such container
@@ -131,11 +125,20 @@ public Void exec() throws NotFoundException {
131125
"Only one of host resource or tar input stream should be defined to perform the copy, not both");
132126
}
133127
// We compress the given path, call exec so that the stream is consumed and then close it our self
134-
try (InputStream uploadStream = buildUploadStream(this.hostResource, this.dirChildrenOnly)) {
128+
Path toUpload;
129+
try {
130+
toUpload = Files.createTempFile("docker-java", ".tar.gz");
131+
CompressArchiveUtil.tar(Paths.get(hostResource), toUpload, true, dirChildrenOnly);
132+
} catch (IOException createFileIOException) {
133+
throw new DockerClientException("Unable to perform tar on host resource " + this.hostResource, createFileIOException);
134+
}
135+
try (InputStream uploadStream = Files.newInputStream(toUpload)) {
135136
this.tarInputStream = uploadStream;
136137
return super.exec();
137138
} catch (IOException e) {
138-
throw new DockerClientException("Unable to perform tar on host resource " + this.hostResource, e);
139+
throw new DockerClientException("Unable to read temp file " + toUpload.toFile().getAbsolutePath(), e);
140+
} finally {
141+
toUpload.toFile().delete();
139142
}
140143
} else if (this.tarInputStream == null) {
141144
throw new DockerClientException(

0 commit comments

Comments
 (0)