Skip to content

Commit a9b78cc

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

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

.idea/codeStyleSettings.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 19 additions & 11 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
@@ -130,16 +124,30 @@ public Void exec() throws NotFoundException {
130124
throw new DockerClientException(
131125
"Only one of host resource or tar input stream should be defined to perform the copy, not both");
132126
}
133-
// 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)) {
127+
// create TAR package for the given path so docker can consume it
128+
Path toUpload = null;
129+
try {
130+
toUpload = Files.createTempFile("docker-java", ".tar.gz");
131+
CompressArchiveUtil.tar(Paths.get(hostResource), toUpload, true, dirChildrenOnly);
132+
} catch (IOException createFileIOException) {
133+
if (toUpload != null) {
134+
// remove tmp docker-javaxxx.tar.gz
135+
toUpload.toFile().delete();
136+
}
137+
throw new DockerClientException("Unable to perform tar on host resource " + this.hostResource, createFileIOException);
138+
}
139+
// send the tar stream, call exec so that the stream is consumed and then closed by try-with-resources
140+
try (InputStream uploadStream = Files.newInputStream(toUpload)) {
135141
this.tarInputStream = uploadStream;
136142
return super.exec();
137143
} catch (IOException e) {
138-
throw new DockerClientException("Unable to perform tar on host resource " + this.hostResource, e);
144+
throw new DockerClientException("Unable to read temp file " + toUpload.toFile().getAbsolutePath(), e);
145+
} finally {
146+
// remove tmp docker-javaxxx.tar.gz
147+
toUpload.toFile().delete();
139148
}
140149
} else if (this.tarInputStream == null) {
141-
throw new DockerClientException(
142-
"One of host resource or tar input stream must be defined to perform the copy");
150+
throw new DockerClientException("One of host resource or tar input stream must be defined to perform the copy");
143151
}
144152
// User set a stream, so we will just consume it and let the user close it by him self
145153
return super.exec();

0 commit comments

Comments
 (0)