Skip to content

Commit

Permalink
[FIXED JENKINS-17330] FilePath.installIfNecessaryFrom should avoid ro…
Browse files Browse the repository at this point in the history
…uting download over remoting channel.(cherry picked from commit b67272e)

Conflicts:
	changelog.html
  • Loading branch information
jglick authored and vjuranek committed Apr 14, 2013
1 parent 6924bad commit 3aaca05
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
4 changes: 1 addition & 3 deletions changelog.html
Expand Up @@ -55,9 +55,7 @@
<!-- Record your changes in the trunk here. -->
<div id="trunk" style="display:none"><!--=TRUNK-BEGIN=-->
<ul class=image>
<li class=bug>
“Build Now” context menu item broken for parameterized jobs.
(<a href="https://issues.jenkins-ci.org/browse/JENKINS-17110">issue 17110</a>)
<li class=>
</ul>
</div><!--=TRUNK-END=-->

Expand Down
43 changes: 41 additions & 2 deletions core/src/main/java/hudson/FilePath.java
Expand Up @@ -493,9 +493,10 @@ public Void invoke(File dir, VirtualChannel channel) throws IOException {
});
}

private void unzip(File dir, InputStream in) throws IOException {
private static void unzip(File dir, InputStream in) throws IOException {
File tmpFile = File.createTempFile("tmpzip", null); // uses java.io.tmpdir
try {
// XXX why does this not simply use ZipInputStream?
IOUtils.copy(in, tmpFile);
unzip(dir,tmpFile);
}
Expand All @@ -504,7 +505,7 @@ private void unzip(File dir, InputStream in) throws IOException {
}
}

private void unzip(File dir, File zipFile) throws IOException {
static private void unzip(File dir, File zipFile) throws IOException {
dir = dir.getAbsoluteFile(); // without absolutization, getParentFile below seems to fail
ZipFile zip = new ZipFile(zipFile);
@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -724,6 +725,19 @@ public boolean installIfNecessaryFrom(URL archive, TaskListener listener, String
if(listener!=null)
listener.getLogger().println(message);

if (isRemote()) {
// First try to download from the slave machine.
try {
act(new Unpack(archive));
timestamp.touch(sourceTimestamp);
return true;
} catch (IOException x) {
if (listener != null) {
x.printStackTrace(listener.error("Failed to download " + archive + " from slave; will retry from master"));
}
}
}

// for HTTP downloads, enable automatic retry for added resilience
InputStream in = archive.getProtocol().startsWith("http") ? ProxyConfiguration.getInputStream(archive) : con.getInputStream();
CountingInputStream cis = new CountingInputStream(in);
Expand All @@ -743,6 +757,31 @@ public boolean installIfNecessaryFrom(URL archive, TaskListener listener, String
}
}

private static final class Unpack implements FileCallable<Void> {
private final URL archive;
Unpack(URL archive) {
this.archive = archive;
}
@Override public Void invoke(File dir, VirtualChannel channel) throws IOException, InterruptedException {
InputStream in = archive.openStream();
try {
CountingInputStream cis = new CountingInputStream(in);
try {
if (archive.toExternalForm().endsWith(".zip")) {
unzip(dir, cis);
} else {
readFromTar("input stream", dir, GZIP.extract(cis));
}
} catch (IOException x) {
throw new IOException2(String.format("Failed to unpack %s (%d bytes read)", archive, cis.getByteCount()), x);
}
} finally {
in.close();
}
return null;
}
}

/**
* Reads the URL on the current VM, and writes all the data to this {@link FilePath}
* (this is different from resolving URL remotely.)
Expand Down

0 comments on commit 3aaca05

Please sign in to comment.