Skip to content

Commit

Permalink
Merge pull request #5 from olivergondza/truezip
Browse files Browse the repository at this point in the history
[FIX JENKINS-27042] Use TrueZIP to access archive
  • Loading branch information
olivergondza committed Mar 1, 2015
2 parents f33ad89 + 34c0ef0 commit f56397d
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 21 deletions.
19 changes: 18 additions & 1 deletion pom.xml
Expand Up @@ -12,6 +12,11 @@
<name>Compress Artifacts Plugin</name>
<description>Keeps build artifacts compressed to save disk space on the master.</description>
<url>https://wiki.jenkins-ci.org/display/JENKINS/Compress+Artifacts+Plugin</url>

<properties>
<truezip.version>7.7.8</truezip.version>
</properties>

<licenses>
<license>
<name>MIT License</name>
Expand All @@ -22,7 +27,7 @@
<connection>scm:git:git://github.com/jenkinsci/${project.artifactId}-plugin.git</connection>
<developerConnection>scm:git:git@github.com:jenkinsci/${project.artifactId}-plugin.git</developerConnection>
<url>https://github.com/jenkinsci/${project.artifactId}-plugin</url>
<tag>HEAD</tag>
<tag>HEAD</tag>
</scm>
<developers>
<developer>
Expand All @@ -31,6 +36,18 @@
<email>ogondza@gmail.com</email>
</developer>
</developers>
<dependencies>
<dependency>
<groupId>de.schlichtherle.truezip</groupId>
<artifactId>truezip-driver-zip</artifactId>
<version>${truezip.version}</version>
</dependency>
<dependency>
<groupId>de.schlichtherle.truezip</groupId>
<artifactId>truezip-file</artifactId>
<version>${truezip.version}</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>repo.jenkins-ci.org</id>
Expand Down
Expand Up @@ -30,26 +30,31 @@

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import javax.annotation.Nonnull;

import jenkins.util.VirtualFile;

import org.apache.commons.httpclient.URIException;
import org.apache.commons.httpclient.util.URIUtil;
import org.apache.tools.ant.types.selectors.SelectorUtils;

import de.schlichtherle.truezip.file.TArchiveDetector;
import de.schlichtherle.truezip.file.TFile;
import de.schlichtherle.truezip.file.TVFS;
import de.schlichtherle.truezip.zip.ZipEntry;
import de.schlichtherle.truezip.zip.ZipFile;

final class ZipStorage extends VirtualFile {

static VirtualFile root(File archive) {
Expand All @@ -59,14 +64,21 @@ static VirtualFile root(File archive) {
// TODO support updating entries
static void archive(File archive, FilePath workspace, Launcher launcher, BuildListener listener, Map<String,String> artifacts) throws IOException, InterruptedException {
// Use temporary file for writing, rename when done
File writingArchive = new File(archive.getAbsolutePath() + ".writing");
OutputStream os = new FileOutputStream(writingArchive);
try {
workspace.zip(os, new FilePath.ExplicitlySpecifiedDirScanner(artifacts));
} finally {
os.close();
File tempArchive = new File(archive.getAbsolutePath() + ".writing.zip");

TFile zip = new TFile(tempArchive, new TArchiveDetector("zip"));
zip.mkdir(); // Create new archive file
for (Entry<String, String> afs: artifacts.entrySet()) {
FilePath src = workspace.child(afs.getKey());
TFile dst = new TFile(zip, afs.getValue(), TArchiveDetector.NULL);
if (src.isDirectory()) {
dst.mkdirs();
} else {
TFile.cp(src.read(), dst);
}
}
writingArchive.renameTo(archive);
TVFS.umount(zip);
tempArchive.renameTo(archive);
}

static boolean delete(File archive) throws IOException, InterruptedException {
Expand Down Expand Up @@ -206,7 +218,7 @@ private boolean looksLikeDir() {
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
if ((! entry.isDirectory()) && entry.getName().startsWith(path)) {
String name = entry.toString().substring(path.length());
String name = entry.getName().substring(path.length());
if (SelectorUtils.match(glob, name)) {
files.add(name);
}
Expand Down Expand Up @@ -257,7 +269,7 @@ private boolean looksLikeDir() {
@Override public boolean canRead() throws IOException {
return true;
}

@Override public InputStream open() throws IOException {
if (!archive.exists()) throw new FileNotFoundException(path + " (No such file or directory)");

Expand All @@ -271,10 +283,21 @@ private boolean looksLikeDir() {
zf.close();
throw new FileNotFoundException(path + " (No such file or directory)");
}
return new FilterInputStream(zf.getInputStream(entry)) {
@Override public void close() throws IOException {
zf.close();
}
};

return new EntryInputStream(zf, entry);
}

private static final class EntryInputStream extends FilterInputStream {
private final @Nonnull ZipFile archive;
private EntryInputStream(ZipFile archive, ZipEntry entry) throws IOException {
super(archive.getInputStream(entry));
this.archive = archive;
}

@Override
public void close() throws IOException {
super.close();
archive.close();
}
}
}
Expand Up @@ -120,8 +120,12 @@ public void archiveThousandsFiles() throws Exception {

// read the content
for (Run<FreeStyleProject, FreeStyleBuild>.Artifact a: artifacts) {
String content = IOUtils.toString(build.getArtifactManager().root().child(a.relativePath).open());
assertThat(content, endsWith("txt"));
final InputStream artifactStream = build.getArtifactManager().root().child(a.relativePath).open();
try {
assertThat(IOUtils.toString(artifactStream), endsWith("txt"));
} finally {
artifactStream.close();
}
}
}

Expand Down

0 comments on commit f56397d

Please sign in to comment.