TarArchiver.java 4.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/*
 * The MIT License
 *
 * Copyright (c) 2010, InfraDNA, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package hudson.util.io;

import hudson.Functions;
import hudson.org.apache.tools.tar.TarOutputStream;
import hudson.util.FileVisitor;
K
kohsuke 已提交
30
import hudson.util.IOException2;
31 32 33 34 35 36 37
import org.apache.tools.tar.TarEntry;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
K
kohsuke 已提交
38 39 40
import java.lang.reflect.Field;

import static org.apache.tools.tar.TarConstants.LF_SYMLINK;
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

/**
 * {@link FileVisitor} that creates a tar archive.
 *
 * @see ArchiverFactory#TAR
 */
final class TarArchiver extends Archiver {
    private final byte[] buf = new byte[8192];
    private final TarOutputStream tar;

    TarArchiver(OutputStream out) {
        tar = new TarOutputStream(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);
    }

K
kohsuke 已提交
64 65 66
    @Override
    public void visitSymlink(File link, String target, String relativePath) throws IOException {
        TarEntry e = new TarEntry(relativePath, LF_SYMLINK);
67
        e.setMode(getPathMode(link.getPath()));
K
kohsuke 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85

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

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

    @Override
    public boolean understandsSymlink() {
        return true;
    }

86 87 88 89 90 91 92
    public void visit(File file, String relativePath) throws IOException {
        if(Functions.isWindows())
            relativePath = relativePath.replace('\\','/');

        if(file.isDirectory())
            relativePath+='/';
        TarEntry te = new TarEntry(relativePath);
93
        te.setMode(getPathMode(file.getPath()));
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
        te.setModTime(file.lastModified());
        if(!file.isDirectory())
            te.setSize(file.length());

        tar.putNextEntry(te);

        if (!file.isDirectory()) {
            FileInputStream in = new FileInputStream(file);
            try {
                int len;
                while((len=in.read(buf))>=0)
                    tar.write(buf,0,len);
            } finally {
                in.close();
            }
        }

        tar.closeEntry();
        entriesWritten++;
    }

    public void close() throws IOException {
        tar.close();
    }
K
kohsuke 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130 131

    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);
        }
    }
132
}