Skip to content

Commit

Permalink
Revert "Revert "[JENKINS-10629] - Migrate the Tar archives handling c…
Browse files Browse the repository at this point in the history
…ode to commons-compress""

This reverts commit 8f1280a.
  • Loading branch information
KostyaSha committed Aug 24, 2015
1 parent 80abb98 commit 03bd595
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 55 deletions.
5 changes: 5 additions & 0 deletions core/pom.xml
Expand Up @@ -276,6 +276,11 @@ THE SOFTWARE.
<artifactId>commons-beanutils</artifactId>
<version>1.8.3</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
Expand Down
32 changes: 9 additions & 23 deletions core/src/main/java/hudson/FilePath.java
Expand Up @@ -33,7 +33,6 @@
import hudson.model.Computer;
import hudson.model.Item;
import hudson.model.TaskListener;
import hudson.org.apache.tools.tar.TarInputStream;
import hudson.os.PosixAPI;
import hudson.os.PosixException;
import hudson.remoting.Callable;
Expand Down Expand Up @@ -70,7 +69,6 @@
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.tar.TarEntry;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.kohsuke.stapler.Stapler;
Expand Down Expand Up @@ -120,6 +118,8 @@
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import jenkins.security.MasterToSlaveCallable;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.jenkinsci.remoting.RoleChecker;
import org.jenkinsci.remoting.RoleSensitive;

Expand Down Expand Up @@ -2268,12 +2268,15 @@ private Integer writeToTar(File baseDir, DirScanner scanner, OutputStream out) t

/**
* Reads from a tar stream and stores obtained files to the base dir.
* @since TODO supports large files > 10 GB, migration to commons-compress
*/
private void readFromTar(String name, File baseDir, InputStream in) throws IOException {
TarInputStream t = new TarInputStream(in);
TarArchiveInputStream t = new TarArchiveInputStream(in);

// TarInputStream t = new TarInputStream(in);
try {
TarEntry te;
while ((te = t.getNextEntry()) != null) {
TarArchiveEntry te;
while ((te = t.getNextTarEntry()) != null) {
File f = new File(baseDir,te.getName());
if(te.isDirectory()) {
mkdirs(f);
Expand All @@ -2282,8 +2285,7 @@ private void readFromTar(String name, File baseDir, InputStream in) throws IOExc
if (parent != null) mkdirs(parent);
writing(f);

byte linkFlag = (Byte) LINKFLAG_FIELD.get(te);
if (linkFlag==TarEntry.LF_SYMLINK) {
if (te.isSymbolicLink()) {
new FilePath(f).symlinkTo(te.getLinkName(), TaskListener.NULL);
} else {
IOUtils.copy(t,f);
Expand All @@ -2300,8 +2302,6 @@ private void readFromTar(String name, File baseDir, InputStream in) throws IOExc
} catch (InterruptedException e) {
Thread.currentThread().interrupt(); // process this later
throw new IOException("Failed to extract "+name,e);
} catch (IllegalAccessException e) {
throw new IOException("Failed to extract "+name,e);
} finally {
t.close();
}
Expand Down Expand Up @@ -2725,20 +2725,6 @@ public int compare(String o1, String o2) {
}
};

private static final Field LINKFLAG_FIELD = getTarEntryLinkFlagField();

private static Field getTarEntryLinkFlagField() {
try {
Field f = TarEntry.class.getDeclaredField("linkFlag");
f.setAccessible(true);
return f;
} catch (SecurityException e) {
throw new AssertionError(e);
} catch (NoSuchFieldException e) {
throw new AssertionError(e);
}
}

/**
* Gets the {@link FilePath} representation of the "~" directory
* (User's home directory in the Unix sense) of the given channel.
Expand Down
Expand Up @@ -37,8 +37,9 @@
* methods are provided to position at each successive entry in
* the archive, and the read each entry as a normal input stream
* using read().
*
* @deprecated Use {@link org.apache.commons.compress.archivers.tar.TarArchiveInputStream} instead
*/
@Deprecated
public class TarInputStream extends FilterInputStream {

// CheckStyle:VisibilityModifier OFF - bc
Expand Down
Expand Up @@ -35,8 +35,11 @@
* The TarOutputStream writes a UNIX tar archive as an OutputStream.
* Methods are provided to put entries, and then write their contents
* by writing to this stream using write().
*
* @deprecated Use {@link org.apache.commons.compress.archivers.tar.TarArchiveOutputStream} instead
*
*/
@Deprecated
public class TarOutputStream extends FilterOutputStream {
/** Fail if a long file name is required in the archive. */
public static final int LONGFILE_ERROR = 0;
Expand Down
45 changes: 14 additions & 31 deletions core/src/main/java/hudson/util/io/TarArchiver.java
Expand Up @@ -37,6 +37,8 @@
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Field;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;

import static org.apache.tools.tar.TarConstants.LF_SYMLINK;

Expand All @@ -47,24 +49,24 @@
*/
final class TarArchiver extends Archiver {
private final byte[] buf = new byte[8192];
private final TarOutputStream tar;
private final TarArchiveOutputStream tar;

TarArchiver(OutputStream out) {
tar = new TarOutputStream(new BufferedOutputStream(out) {
tar = new TarArchiveOutputStream(new BufferedOutputStream(out) {
// TarOutputStream uses TarBuffer internally,
// which flushes the stream for each block. this creates unnecessary
// data stream fragmentation, and flush request to a remote, which slows things down.
@Override
public void flush() throws IOException {
// so don't do anything in flush
}
});
tar.setLongFileMode(TarOutputStream.LONGFILE_GNU);
});
tar.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
}

@Override
public void visitSymlink(File link, String target, String relativePath) throws IOException {
TarEntry e = new TarEntry(relativePath, LF_SYMLINK);
TarArchiveEntry e = new TarArchiveEntry(relativePath, LF_SYMLINK);
try {
int mode = IOUtils.mode(link);
if (mode != -1) {
Expand All @@ -73,16 +75,11 @@ public void visitSymlink(File link, String target, String relativePath) throws I
} catch (PosixException x) {
// ignore
}

e.setLinkName(target);

try {
StringBuffer linkName = (StringBuffer) LINKNAME_FIELD.get(e);
linkName.setLength(0);
linkName.append(target);
} catch (IllegalAccessException x) {
throw new IOException("Failed to set linkName", x);
}

tar.putNextEntry(e);
tar.putArchiveEntry(e);
tar.closeArchiveEntry();
entriesWritten++;
}

Expand All @@ -97,14 +94,14 @@ public void visit(File file, String relativePath) throws IOException {

if(file.isDirectory())
relativePath+='/';
TarEntry te = new TarEntry(relativePath);
TarArchiveEntry te = new TarArchiveEntry(relativePath);
int mode = IOUtils.mode(file);
if (mode!=-1) te.setMode(mode);
te.setModTime(file.lastModified());
if(!file.isDirectory())
te.setSize(file.length());

tar.putNextEntry(te);
tar.putArchiveEntry(te);

if (!file.isDirectory()) {
FileInputStream in = new FileInputStream(file);
Expand All @@ -117,25 +114,11 @@ public void visit(File file, String relativePath) throws IOException {
}
}

tar.closeEntry();
tar.closeArchiveEntry();
entriesWritten++;
}

public void close() throws IOException {
tar.close();
}

private static final Field LINKNAME_FIELD = getTarEntryLinkNameField();

private static Field getTarEntryLinkNameField() {
try {
Field f = TarEntry.class.getDeclaredField("linkName");
f.setAccessible(true);
return f;
} catch (SecurityException e) {
throw new AssertionError(e);
} catch (NoSuchFieldException e) {
throw new AssertionError(e);
}
}
}

0 comments on commit 03bd595

Please sign in to comment.